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/workqueue.h>
24
25#define NR_MAXCACHES BITS_PER_LONG
26
27struct fscache_cache;
28struct fscache_cache_ops;
29struct fscache_object;
30struct fscache_operation;
31
32enum fscache_obj_ref_trace {
33 fscache_obj_get_add_to_deps,
34 fscache_obj_get_queue,
35 fscache_obj_put_alloc_fail,
36 fscache_obj_put_attach_fail,
37 fscache_obj_put_drop_obj,
38 fscache_obj_put_enq_dep,
39 fscache_obj_put_queue,
40 fscache_obj_put_work,
41 fscache_obj_ref__nr_traces
42};
43
44
45
46
47struct fscache_cache_tag {
48 struct list_head link;
49 struct fscache_cache *cache;
50 unsigned long flags;
51#define FSCACHE_TAG_RESERVED 0
52 atomic_t usage;
53 char name[0];
54};
55
56
57
58
59struct fscache_cache {
60 const struct fscache_cache_ops *ops;
61 struct fscache_cache_tag *tag;
62 struct kobject *kobj;
63 struct list_head link;
64 size_t max_index_size;
65 char identifier[36];
66
67
68 struct work_struct op_gc;
69 struct list_head object_list;
70 struct list_head op_gc_list;
71 spinlock_t object_list_lock;
72 spinlock_t op_gc_list_lock;
73 atomic_t object_count;
74 struct fscache_object *fsdef;
75 unsigned long flags;
76#define FSCACHE_IOERROR 0
77#define FSCACHE_CACHE_WITHDRAWN 1
78};
79
80extern wait_queue_head_t fscache_cache_cleared_wq;
81
82
83
84
85
86
87typedef void (*fscache_operation_release_t)(struct fscache_operation *op);
88typedef void (*fscache_operation_processor_t)(struct fscache_operation *op);
89typedef void (*fscache_operation_cancel_t)(struct fscache_operation *op);
90
91enum fscache_operation_state {
92 FSCACHE_OP_ST_BLANK,
93 FSCACHE_OP_ST_INITIALISED,
94 FSCACHE_OP_ST_PENDING,
95 FSCACHE_OP_ST_IN_PROGRESS,
96 FSCACHE_OP_ST_COMPLETE,
97 FSCACHE_OP_ST_CANCELLED,
98 FSCACHE_OP_ST_DEAD
99};
100
101struct fscache_operation {
102 struct work_struct work;
103 struct list_head pend_link;
104 struct fscache_object *object;
105
106 unsigned long flags;
107#define FSCACHE_OP_TYPE 0x000f
108#define FSCACHE_OP_ASYNC 0x0001
109#define FSCACHE_OP_MYTHREAD 0x0002
110#define FSCACHE_OP_WAITING 4
111#define FSCACHE_OP_EXCLUSIVE 5
112#define FSCACHE_OP_DEC_READ_CNT 6
113#define FSCACHE_OP_UNUSE_COOKIE 7
114#define FSCACHE_OP_KEEP_FLAGS 0x00f0
115
116 enum fscache_operation_state state;
117 atomic_t usage;
118 unsigned debug_id;
119
120
121
122
123 fscache_operation_processor_t processor;
124
125
126 fscache_operation_cancel_t cancel;
127
128
129 fscache_operation_release_t release;
130};
131
132extern atomic_t fscache_op_debug_id;
133extern void fscache_op_work_func(struct work_struct *work);
134
135extern void fscache_enqueue_operation(struct fscache_operation *);
136extern void fscache_op_complete(struct fscache_operation *, bool);
137extern void fscache_put_operation(struct fscache_operation *);
138extern void fscache_operation_init(struct fscache_cookie *,
139 struct fscache_operation *,
140 fscache_operation_processor_t,
141 fscache_operation_cancel_t,
142 fscache_operation_release_t);
143
144
145
146
147struct fscache_retrieval {
148 struct fscache_operation op;
149 struct fscache_cookie *cookie;
150 struct address_space *mapping;
151 fscache_rw_complete_t end_io_func;
152 void *context;
153 struct list_head to_do;
154 unsigned long start_time;
155 atomic_t n_pages;
156};
157
158typedef int (*fscache_page_retrieval_func_t)(struct fscache_retrieval *op,
159 struct page *page,
160 gfp_t gfp);
161
162typedef int (*fscache_pages_retrieval_func_t)(struct fscache_retrieval *op,
163 struct list_head *pages,
164 unsigned *nr_pages,
165 gfp_t gfp);
166
167
168
169
170
171
172
173static inline
174struct fscache_retrieval *fscache_get_retrieval(struct fscache_retrieval *op)
175{
176 atomic_inc(&op->op.usage);
177 return op;
178}
179
180
181
182
183
184
185
186static inline void fscache_enqueue_retrieval(struct fscache_retrieval *op)
187{
188 fscache_enqueue_operation(&op->op);
189}
190
191
192
193
194
195
196static inline void fscache_retrieval_complete(struct fscache_retrieval *op,
197 int n_pages)
198{
199 atomic_sub(n_pages, &op->n_pages);
200 if (atomic_read(&op->n_pages) <= 0)
201 fscache_op_complete(&op->op, false);
202}
203
204
205
206
207
208
209
210static inline void fscache_put_retrieval(struct fscache_retrieval *op)
211{
212 fscache_put_operation(&op->op);
213}
214
215
216
217
218
219
220
221
222struct fscache_storage {
223 struct fscache_operation op;
224 pgoff_t store_limit;
225};
226
227
228
229
230struct fscache_cache_ops {
231
232 const char *name;
233
234
235 struct fscache_object *(*alloc_object)(struct fscache_cache *cache,
236 struct fscache_cookie *cookie);
237
238
239
240
241 int (*lookup_object)(struct fscache_object *object);
242
243
244 void (*lookup_complete)(struct fscache_object *object);
245
246
247 struct fscache_object *(*grab_object)(struct fscache_object *object,
248 enum fscache_obj_ref_trace why);
249
250
251 int (*pin_object)(struct fscache_object *object);
252
253
254 void (*unpin_object)(struct fscache_object *object);
255
256
257
258 int (*check_consistency)(struct fscache_operation *op);
259
260
261 void (*update_object)(struct fscache_object *object);
262
263
264 void (*invalidate_object)(struct fscache_operation *op);
265
266
267
268 void (*drop_object)(struct fscache_object *object);
269
270
271 void (*put_object)(struct fscache_object *object,
272 enum fscache_obj_ref_trace why);
273
274
275 void (*sync_cache)(struct fscache_cache *cache);
276
277
278
279 int (*attr_changed)(struct fscache_object *object);
280
281
282 int (*reserve_space)(struct fscache_object *object, loff_t i_size);
283
284
285
286 fscache_page_retrieval_func_t read_or_alloc_page;
287
288
289
290 fscache_pages_retrieval_func_t read_or_alloc_pages;
291
292
293
294 fscache_page_retrieval_func_t allocate_page;
295
296
297
298 fscache_pages_retrieval_func_t allocate_pages;
299
300
301 int (*write_page)(struct fscache_storage *op, struct page *page);
302
303
304
305
306
307 void (*uncache_page)(struct fscache_object *object,
308 struct page *page);
309
310
311 void (*dissociate_pages)(struct fscache_cache *cache);
312};
313
314extern struct fscache_cookie fscache_fsdef_index;
315
316
317
318
319enum {
320 FSCACHE_OBJECT_EV_NEW_CHILD,
321 FSCACHE_OBJECT_EV_PARENT_READY,
322 FSCACHE_OBJECT_EV_UPDATE,
323 FSCACHE_OBJECT_EV_INVALIDATE,
324 FSCACHE_OBJECT_EV_CLEARED,
325 FSCACHE_OBJECT_EV_ERROR,
326 FSCACHE_OBJECT_EV_KILL,
327 NR_FSCACHE_OBJECT_EVENTS
328};
329
330#define FSCACHE_OBJECT_EVENTS_MASK ((1UL << NR_FSCACHE_OBJECT_EVENTS) - 1)
331
332
333
334
335struct fscache_transition {
336 unsigned long events;
337 const struct fscache_state *transit_to;
338};
339
340struct fscache_state {
341 char name[24];
342 char short_name[8];
343 const struct fscache_state *(*work)(struct fscache_object *object,
344 int event);
345 const struct fscache_transition transitions[];
346};
347
348
349
350
351struct fscache_object {
352 const struct fscache_state *state;
353 const struct fscache_transition *oob_table;
354 int debug_id;
355 int n_children;
356 int n_ops;
357 int n_obj_ops;
358 int n_in_progress;
359 int n_exclusive;
360 atomic_t n_reads;
361 spinlock_t lock;
362
363 unsigned long lookup_jif;
364 unsigned long oob_event_mask;
365 unsigned long event_mask;
366 unsigned long events;
367
368
369 unsigned long flags;
370#define FSCACHE_OBJECT_LOCK 0
371#define FSCACHE_OBJECT_PENDING_WRITE 1
372#define FSCACHE_OBJECT_WAITING 2
373#define FSCACHE_OBJECT_IS_LIVE 3
374#define FSCACHE_OBJECT_IS_LOOKED_UP 4
375#define FSCACHE_OBJECT_IS_AVAILABLE 5
376#define FSCACHE_OBJECT_RETIRED 6
377#define FSCACHE_OBJECT_KILLED_BY_CACHE 7
378#define FSCACHE_OBJECT_RUN_AFTER_DEAD 8
379
380 struct list_head cache_link;
381 struct hlist_node cookie_link;
382 struct fscache_cache *cache;
383 struct fscache_cookie *cookie;
384 struct fscache_object *parent;
385 struct work_struct work;
386 struct list_head dependents;
387 struct list_head dep_link;
388 struct list_head pending_ops;
389#ifdef CONFIG_FSCACHE_OBJECT_LIST
390 struct rb_node objlist_link;
391#endif
392 pgoff_t store_limit;
393 loff_t store_limit_l;
394};
395
396extern void fscache_object_init(struct fscache_object *, struct fscache_cookie *,
397 struct fscache_cache *);
398extern void fscache_object_destroy(struct fscache_object *);
399
400extern void fscache_object_lookup_negative(struct fscache_object *object);
401extern void fscache_obtained_object(struct fscache_object *object);
402
403static inline bool fscache_object_is_live(struct fscache_object *object)
404{
405 return test_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
406}
407
408static inline bool fscache_object_is_dying(struct fscache_object *object)
409{
410 return !fscache_object_is_live(object);
411}
412
413static inline bool fscache_object_is_available(struct fscache_object *object)
414{
415 return test_bit(FSCACHE_OBJECT_IS_AVAILABLE, &object->flags);
416}
417
418static inline bool fscache_cache_is_broken(struct fscache_object *object)
419{
420 return test_bit(FSCACHE_IOERROR, &object->cache->flags);
421}
422
423static inline bool fscache_object_is_active(struct fscache_object *object)
424{
425 return fscache_object_is_available(object) &&
426 fscache_object_is_live(object) &&
427 !fscache_cache_is_broken(object);
428}
429
430
431
432
433
434
435
436static inline void fscache_object_destroyed(struct fscache_cache *cache)
437{
438 if (atomic_dec_and_test(&cache->object_count))
439 wake_up_all(&fscache_cache_cleared_wq);
440}
441
442
443
444
445
446
447
448
449static inline void fscache_object_lookup_error(struct fscache_object *object)
450{
451 set_bit(FSCACHE_OBJECT_EV_ERROR, &object->events);
452}
453
454
455
456
457
458
459
460
461
462
463
464
465static inline
466void fscache_set_store_limit(struct fscache_object *object, loff_t i_size)
467{
468 object->store_limit_l = i_size;
469 object->store_limit = i_size >> PAGE_SHIFT;
470 if (i_size & ~PAGE_MASK)
471 object->store_limit++;
472}
473
474
475
476
477
478
479
480
481
482
483static inline void fscache_end_io(struct fscache_retrieval *op,
484 struct page *page, int error)
485{
486 op->end_io_func(page, op->context, error);
487}
488
489static inline void __fscache_use_cookie(struct fscache_cookie *cookie)
490{
491 atomic_inc(&cookie->n_active);
492}
493
494
495
496
497
498
499
500
501static inline bool fscache_use_cookie(struct fscache_object *object)
502{
503 struct fscache_cookie *cookie = object->cookie;
504 return atomic_inc_not_zero(&cookie->n_active) != 0;
505}
506
507static inline bool __fscache_unuse_cookie(struct fscache_cookie *cookie)
508{
509 return atomic_dec_and_test(&cookie->n_active);
510}
511
512static inline void __fscache_wake_unused_cookie(struct fscache_cookie *cookie)
513{
514 wake_up_var(&cookie->n_active);
515}
516
517
518
519
520
521
522
523
524static inline void fscache_unuse_cookie(struct fscache_object *object)
525{
526 struct fscache_cookie *cookie = object->cookie;
527 if (__fscache_unuse_cookie(cookie))
528 __fscache_wake_unused_cookie(cookie);
529}
530
531
532
533
534extern __printf(3, 4)
535void fscache_init_cache(struct fscache_cache *cache,
536 const struct fscache_cache_ops *ops,
537 const char *idfmt, ...);
538
539extern int fscache_add_cache(struct fscache_cache *cache,
540 struct fscache_object *fsdef,
541 const char *tagname);
542extern void fscache_withdraw_cache(struct fscache_cache *cache);
543
544extern void fscache_io_error(struct fscache_cache *cache);
545
546extern void fscache_mark_page_cached(struct fscache_retrieval *op,
547 struct page *page);
548
549extern void fscache_mark_pages_cached(struct fscache_retrieval *op,
550 struct pagevec *pagevec);
551
552extern bool fscache_object_sleep_till_congested(signed long *timeoutp);
553
554extern enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
555 const void *data,
556 uint16_t datalen,
557 loff_t object_size);
558
559extern void fscache_object_retrying_stale(struct fscache_object *object);
560
561enum fscache_why_object_killed {
562 FSCACHE_OBJECT_IS_STALE,
563 FSCACHE_OBJECT_NO_SPACE,
564 FSCACHE_OBJECT_WAS_RETIRED,
565 FSCACHE_OBJECT_WAS_CULLED,
566};
567extern void fscache_object_mark_killed(struct fscache_object *object,
568 enum fscache_why_object_killed why);
569
570#endif
571