1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#ifndef _LINUX_FSCACHE_CACHE_H
19#define _LINUX_FSCACHE_CACHE_H
20
21#include <linux/fscache.h>
22#include <linux/sched.h>
23#include <linux/slow-work.h>
24
25#define NR_MAXCACHES BITS_PER_LONG
26
27struct fscache_cache;
28struct fscache_cache_ops;
29struct fscache_object;
30struct fscache_operation;
31
32
33
34
35struct fscache_cache_tag {
36 struct list_head link;
37 struct fscache_cache *cache;
38 unsigned long flags;
39#define FSCACHE_TAG_RESERVED 0
40 atomic_t usage;
41 char name[0];
42};
43
44
45
46
47struct fscache_cache {
48 const struct fscache_cache_ops *ops;
49 struct fscache_cache_tag *tag;
50 struct kobject *kobj;
51 struct list_head link;
52 size_t max_index_size;
53 char identifier[36];
54
55
56 struct work_struct op_gc;
57 struct list_head object_list;
58 struct list_head op_gc_list;
59 spinlock_t object_list_lock;
60 spinlock_t op_gc_list_lock;
61 atomic_t object_count;
62 struct fscache_object *fsdef;
63 unsigned long flags;
64#define FSCACHE_IOERROR 0
65#define FSCACHE_CACHE_WITHDRAWN 1
66};
67
68extern wait_queue_head_t fscache_cache_cleared_wq;
69
70
71
72
73
74
75typedef void (*fscache_operation_release_t)(struct fscache_operation *op);
76typedef void (*fscache_operation_processor_t)(struct fscache_operation *op);
77
78struct fscache_operation {
79 union {
80 struct work_struct fast_work;
81 struct slow_work slow_work;
82 };
83 struct list_head pend_link;
84 struct fscache_object *object;
85
86 unsigned long flags;
87#define FSCACHE_OP_TYPE 0x000f
88#define FSCACHE_OP_FAST 0x0001
89#define FSCACHE_OP_SLOW 0x0002
90#define FSCACHE_OP_MYTHREAD 0x0003
91#define FSCACHE_OP_WAITING 4
92#define FSCACHE_OP_EXCLUSIVE 5
93#define FSCACHE_OP_DEAD 6
94#define FSCACHE_OP_DEC_READ_CNT 7
95#define FSCACHE_OP_KEEP_FLAGS 0xc0
96
97 atomic_t usage;
98 unsigned debug_id;
99
100
101
102
103 fscache_operation_processor_t processor;
104
105
106 fscache_operation_release_t release;
107
108#ifdef CONFIG_SLOW_WORK_PROC
109 const char *name;
110 const char *state;
111#define fscache_set_op_name(OP, N) do { (OP)->name = (N); } while(0)
112#define fscache_set_op_state(OP, S) do { (OP)->state = (S); } while(0)
113#else
114#define fscache_set_op_name(OP, N) do { } while(0)
115#define fscache_set_op_state(OP, S) do { } while(0)
116#endif
117};
118
119extern atomic_t fscache_op_debug_id;
120extern const struct slow_work_ops fscache_op_slow_work_ops;
121
122extern void fscache_enqueue_operation(struct fscache_operation *);
123extern void fscache_put_operation(struct fscache_operation *);
124
125
126
127
128
129
130
131
132
133static inline void fscache_operation_init(struct fscache_operation *op,
134 fscache_operation_release_t release)
135{
136 atomic_set(&op->usage, 1);
137 op->debug_id = atomic_inc_return(&fscache_op_debug_id);
138 op->release = release;
139 INIT_LIST_HEAD(&op->pend_link);
140 fscache_set_op_state(op, "Init");
141}
142
143
144
145
146
147
148
149
150static inline
151void fscache_operation_init_slow(struct fscache_operation *op,
152 fscache_operation_processor_t processor)
153{
154 op->processor = processor;
155 slow_work_init(&op->slow_work, &fscache_op_slow_work_ops);
156}
157
158
159
160
161struct fscache_retrieval {
162 struct fscache_operation op;
163 struct address_space *mapping;
164 fscache_rw_complete_t end_io_func;
165 void *context;
166 struct list_head to_do;
167 unsigned long start_time;
168};
169
170typedef int (*fscache_page_retrieval_func_t)(struct fscache_retrieval *op,
171 struct page *page,
172 gfp_t gfp);
173
174typedef int (*fscache_pages_retrieval_func_t)(struct fscache_retrieval *op,
175 struct list_head *pages,
176 unsigned *nr_pages,
177 gfp_t gfp);
178
179
180
181
182
183
184
185static inline
186struct fscache_retrieval *fscache_get_retrieval(struct fscache_retrieval *op)
187{
188 atomic_inc(&op->op.usage);
189 return op;
190}
191
192
193
194
195
196
197
198static inline void fscache_enqueue_retrieval(struct fscache_retrieval *op)
199{
200 fscache_enqueue_operation(&op->op);
201}
202
203
204
205
206
207
208
209static inline void fscache_put_retrieval(struct fscache_retrieval *op)
210{
211 fscache_put_operation(&op->op);
212}
213
214
215
216
217
218
219
220
221struct fscache_storage {
222 struct fscache_operation op;
223 pgoff_t store_limit;
224};
225
226
227
228
229struct fscache_cache_ops {
230
231 const char *name;
232
233
234 struct fscache_object *(*alloc_object)(struct fscache_cache *cache,
235 struct fscache_cookie *cookie);
236
237
238
239
240 int (*lookup_object)(struct fscache_object *object);
241
242
243 void (*lookup_complete)(struct fscache_object *object);
244
245
246 struct fscache_object *(*grab_object)(struct fscache_object *object);
247
248
249 int (*pin_object)(struct fscache_object *object);
250
251
252 void (*unpin_object)(struct fscache_object *object);
253
254
255 void (*update_object)(struct fscache_object *object);
256
257
258
259 void (*drop_object)(struct fscache_object *object);
260
261
262 void (*put_object)(struct fscache_object *object);
263
264
265 void (*sync_cache)(struct fscache_cache *cache);
266
267
268
269 int (*attr_changed)(struct fscache_object *object);
270
271
272 int (*reserve_space)(struct fscache_object *object, loff_t i_size);
273
274
275
276 fscache_page_retrieval_func_t read_or_alloc_page;
277
278
279
280 fscache_pages_retrieval_func_t read_or_alloc_pages;
281
282
283
284 fscache_page_retrieval_func_t allocate_page;
285
286
287
288 fscache_pages_retrieval_func_t allocate_pages;
289
290
291 int (*write_page)(struct fscache_storage *op, struct page *page);
292
293
294
295
296
297 void (*uncache_page)(struct fscache_object *object,
298 struct page *page);
299
300
301 void (*dissociate_pages)(struct fscache_cache *cache);
302};
303
304
305
306
307
308
309
310
311struct fscache_cookie {
312 atomic_t usage;
313 atomic_t n_children;
314 spinlock_t lock;
315 spinlock_t stores_lock;
316 struct hlist_head backing_objects;
317 const struct fscache_cookie_def *def;
318 struct fscache_cookie *parent;
319 void *netfs_data;
320 struct radix_tree_root stores;
321#define FSCACHE_COOKIE_PENDING_TAG 0
322#define FSCACHE_COOKIE_STORING_TAG 1
323
324 unsigned long flags;
325#define FSCACHE_COOKIE_LOOKING_UP 0
326#define FSCACHE_COOKIE_CREATING 1
327#define FSCACHE_COOKIE_NO_DATA_YET 2
328#define FSCACHE_COOKIE_PENDING_FILL 3
329#define FSCACHE_COOKIE_FILLING 4
330#define FSCACHE_COOKIE_UNAVAILABLE 5
331};
332
333extern struct fscache_cookie fscache_fsdef_index;
334
335
336
337
338struct fscache_object {
339 enum fscache_object_state {
340 FSCACHE_OBJECT_INIT,
341 FSCACHE_OBJECT_LOOKING_UP,
342 FSCACHE_OBJECT_CREATING,
343
344
345 FSCACHE_OBJECT_AVAILABLE,
346 FSCACHE_OBJECT_ACTIVE,
347 FSCACHE_OBJECT_UPDATING,
348
349
350 FSCACHE_OBJECT_DYING,
351 FSCACHE_OBJECT_LC_DYING,
352 FSCACHE_OBJECT_ABORT_INIT,
353 FSCACHE_OBJECT_RELEASING,
354 FSCACHE_OBJECT_RECYCLING,
355 FSCACHE_OBJECT_WITHDRAWING,
356 FSCACHE_OBJECT_DEAD,
357 FSCACHE_OBJECT__NSTATES
358 } state;
359
360 int debug_id;
361 int n_children;
362 int n_ops;
363 int n_obj_ops;
364 int n_in_progress;
365 int n_exclusive;
366 atomic_t n_reads;
367 spinlock_t lock;
368
369 unsigned long lookup_jif;
370 unsigned long event_mask;
371 unsigned long events;
372
373#define FSCACHE_OBJECT_EV_REQUEUE 0
374#define FSCACHE_OBJECT_EV_UPDATE 1
375#define FSCACHE_OBJECT_EV_CLEARED 2
376#define FSCACHE_OBJECT_EV_ERROR 3
377#define FSCACHE_OBJECT_EV_RELEASE 4
378#define FSCACHE_OBJECT_EV_RETIRE 5
379#define FSCACHE_OBJECT_EV_WITHDRAW 6
380#define FSCACHE_OBJECT_EVENTS_MASK 0x7f
381
382 unsigned long flags;
383#define FSCACHE_OBJECT_LOCK 0
384#define FSCACHE_OBJECT_PENDING_WRITE 1
385#define FSCACHE_OBJECT_WAITING 2
386
387 struct list_head cache_link;
388 struct hlist_node cookie_link;
389 struct fscache_cache *cache;
390 struct fscache_cookie *cookie;
391 struct fscache_object *parent;
392 struct slow_work work;
393 struct list_head dependents;
394 struct list_head dep_link;
395 struct list_head pending_ops;
396#ifdef CONFIG_FSCACHE_OBJECT_LIST
397 struct rb_node objlist_link;
398#endif
399 pgoff_t store_limit;
400 loff_t store_limit_l;
401};
402
403extern const char *fscache_object_states[];
404
405#define fscache_object_is_active(obj) \
406 (!test_bit(FSCACHE_IOERROR, &(obj)->cache->flags) && \
407 (obj)->state >= FSCACHE_OBJECT_AVAILABLE && \
408 (obj)->state < FSCACHE_OBJECT_DYING)
409
410#define fscache_object_is_dead(obj) \
411 (test_bit(FSCACHE_IOERROR, &(obj)->cache->flags) && \
412 (obj)->state >= FSCACHE_OBJECT_DYING)
413
414extern const struct slow_work_ops fscache_object_slow_work_ops;
415
416
417
418
419
420
421
422
423
424
425static inline
426void fscache_object_init(struct fscache_object *object,
427 struct fscache_cookie *cookie,
428 struct fscache_cache *cache)
429{
430 atomic_inc(&cache->object_count);
431
432 object->state = FSCACHE_OBJECT_INIT;
433 spin_lock_init(&object->lock);
434 INIT_LIST_HEAD(&object->cache_link);
435 INIT_HLIST_NODE(&object->cookie_link);
436 vslow_work_init(&object->work, &fscache_object_slow_work_ops);
437 INIT_LIST_HEAD(&object->dependents);
438 INIT_LIST_HEAD(&object->dep_link);
439 INIT_LIST_HEAD(&object->pending_ops);
440 object->n_children = 0;
441 object->n_ops = object->n_in_progress = object->n_exclusive = 0;
442 object->events = object->event_mask = 0;
443 object->flags = 0;
444 object->store_limit = 0;
445 object->store_limit_l = 0;
446 object->cache = cache;
447 object->cookie = cookie;
448 object->parent = NULL;
449}
450
451extern void fscache_object_lookup_negative(struct fscache_object *object);
452extern void fscache_obtained_object(struct fscache_object *object);
453
454#ifdef CONFIG_FSCACHE_OBJECT_LIST
455extern void fscache_object_destroy(struct fscache_object *object);
456#else
457#define fscache_object_destroy(object) do {} while(0)
458#endif
459
460
461
462
463
464
465
466static inline void fscache_object_destroyed(struct fscache_cache *cache)
467{
468 if (atomic_dec_and_test(&cache->object_count))
469 wake_up_all(&fscache_cache_cleared_wq);
470}
471
472
473
474
475
476
477
478
479static inline void fscache_object_lookup_error(struct fscache_object *object)
480{
481 set_bit(FSCACHE_OBJECT_EV_ERROR, &object->events);
482}
483
484
485
486
487
488
489
490
491
492
493
494
495static inline
496void fscache_set_store_limit(struct fscache_object *object, loff_t i_size)
497{
498 object->store_limit_l = i_size;
499 object->store_limit = i_size >> PAGE_SHIFT;
500 if (i_size & ~PAGE_MASK)
501 object->store_limit++;
502}
503
504
505
506
507
508
509
510
511
512
513static inline void fscache_end_io(struct fscache_retrieval *op,
514 struct page *page, int error)
515{
516 op->end_io_func(page, op->context, error);
517}
518
519
520
521
522extern void fscache_init_cache(struct fscache_cache *cache,
523 const struct fscache_cache_ops *ops,
524 const char *idfmt,
525 ...) __attribute__ ((format (printf, 3, 4)));
526
527extern int fscache_add_cache(struct fscache_cache *cache,
528 struct fscache_object *fsdef,
529 const char *tagname);
530extern void fscache_withdraw_cache(struct fscache_cache *cache);
531
532extern void fscache_io_error(struct fscache_cache *cache);
533
534extern void fscache_mark_pages_cached(struct fscache_retrieval *op,
535 struct pagevec *pagevec);
536
537extern enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
538 const void *data,
539 uint16_t datalen);
540
541#endif
542