fscache-cache.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /* General filesystem caching backing cache interface
  2. *
  3. * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * NOTE!!! See:
  12. *
  13. * Documentation/filesystems/caching/backend-api.txt
  14. *
  15. * for a description of the cache backend interface declared here.
  16. */
  17. #ifndef _LINUX_FSCACHE_CACHE_H
  18. #define _LINUX_FSCACHE_CACHE_H
  19. #include <linux/fscache.h>
  20. #include <linux/sched.h>
  21. #include <linux/slow-work.h>
  22. #define NR_MAXCACHES BITS_PER_LONG
  23. struct fscache_cache;
  24. struct fscache_cache_ops;
  25. struct fscache_object;
  26. struct fscache_operation;
  27. /*
  28. * cache tag definition
  29. */
  30. struct fscache_cache_tag {
  31. struct list_head link;
  32. struct fscache_cache *cache; /* cache referred to by this tag */
  33. unsigned long flags;
  34. #define FSCACHE_TAG_RESERVED 0 /* T if tag is reserved for a cache */
  35. atomic_t usage;
  36. char name[0]; /* tag name */
  37. };
  38. /*
  39. * cache definition
  40. */
  41. struct fscache_cache {
  42. const struct fscache_cache_ops *ops;
  43. struct fscache_cache_tag *tag; /* tag representing this cache */
  44. struct kobject *kobj; /* system representation of this cache */
  45. struct list_head link; /* link in list of caches */
  46. size_t max_index_size; /* maximum size of index data */
  47. char identifier[36]; /* cache label */
  48. /* node management */
  49. struct work_struct op_gc; /* operation garbage collector */
  50. struct list_head object_list; /* list of data/index objects */
  51. struct list_head op_gc_list; /* list of ops to be deleted */
  52. spinlock_t object_list_lock;
  53. spinlock_t op_gc_list_lock;
  54. atomic_t object_count; /* no. of live objects in this cache */
  55. struct fscache_object *fsdef; /* object for the fsdef index */
  56. unsigned long flags;
  57. #define FSCACHE_IOERROR 0 /* cache stopped on I/O error */
  58. #define FSCACHE_CACHE_WITHDRAWN 1 /* cache has been withdrawn */
  59. };
  60. extern wait_queue_head_t fscache_cache_cleared_wq;
  61. /*
  62. * operation to be applied to a cache object
  63. * - retrieval initiation operations are done in the context of the process
  64. * that issued them, and not in an async thread pool
  65. */
  66. typedef void (*fscache_operation_release_t)(struct fscache_operation *op);
  67. typedef void (*fscache_operation_processor_t)(struct fscache_operation *op);
  68. struct fscache_operation {
  69. union {
  70. struct work_struct fast_work; /* record for fast ops */
  71. struct slow_work slow_work; /* record for (very) slow ops */
  72. };
  73. struct list_head pend_link; /* link in object->pending_ops */
  74. struct fscache_object *object; /* object to be operated upon */
  75. unsigned long flags;
  76. #define FSCACHE_OP_TYPE 0x000f /* operation type */
  77. #define FSCACHE_OP_FAST 0x0001 /* - fast op, processor may not sleep for disk */
  78. #define FSCACHE_OP_SLOW 0x0002 /* - (very) slow op, processor may sleep for disk */
  79. #define FSCACHE_OP_MYTHREAD 0x0003 /* - processing is done be issuing thread, not pool */
  80. #define FSCACHE_OP_WAITING 4 /* cleared when op is woken */
  81. #define FSCACHE_OP_EXCLUSIVE 5 /* exclusive op, other ops must wait */
  82. #define FSCACHE_OP_DEAD 6 /* op is now dead */
  83. #define FSCACHE_OP_DEC_READ_CNT 7 /* decrement object->n_reads on destruction */
  84. #define FSCACHE_OP_KEEP_FLAGS 0xc0 /* flags to keep when repurposing an op */
  85. atomic_t usage;
  86. unsigned debug_id; /* debugging ID */
  87. /* operation processor callback
  88. * - can be NULL if FSCACHE_OP_WAITING is going to be used to perform
  89. * the op in a non-pool thread */
  90. fscache_operation_processor_t processor;
  91. /* operation releaser */
  92. fscache_operation_release_t release;
  93. #ifdef CONFIG_SLOW_WORK_DEBUG
  94. const char *name; /* operation name */
  95. const char *state; /* operation state */
  96. #define fscache_set_op_name(OP, N) do { (OP)->name = (N); } while(0)
  97. #define fscache_set_op_state(OP, S) do { (OP)->state = (S); } while(0)
  98. #else
  99. #define fscache_set_op_name(OP, N) do { } while(0)
  100. #define fscache_set_op_state(OP, S) do { } while(0)
  101. #endif
  102. };
  103. extern atomic_t fscache_op_debug_id;
  104. extern const struct slow_work_ops fscache_op_slow_work_ops;
  105. extern void fscache_enqueue_operation(struct fscache_operation *);
  106. extern void fscache_put_operation(struct fscache_operation *);
  107. /**
  108. * fscache_operation_init - Do basic initialisation of an operation
  109. * @op: The operation to initialise
  110. * @release: The release function to assign
  111. *
  112. * Do basic initialisation of an operation. The caller must still set flags,
  113. * object, either fast_work or slow_work if necessary, and processor if needed.
  114. */
  115. static inline void fscache_operation_init(struct fscache_operation *op,
  116. fscache_operation_release_t release)
  117. {
  118. atomic_set(&op->usage, 1);
  119. op->debug_id = atomic_inc_return(&fscache_op_debug_id);
  120. op->release = release;
  121. INIT_LIST_HEAD(&op->pend_link);
  122. fscache_set_op_state(op, "Init");
  123. }
  124. /**
  125. * fscache_operation_init_slow - Do additional initialisation of a slow op
  126. * @op: The operation to initialise
  127. * @processor: The processor function to assign
  128. *
  129. * Do additional initialisation of an operation as required for slow work.
  130. */
  131. static inline
  132. void fscache_operation_init_slow(struct fscache_operation *op,
  133. fscache_operation_processor_t processor)
  134. {
  135. op->processor = processor;
  136. slow_work_init(&op->slow_work, &fscache_op_slow_work_ops);
  137. }
  138. /*
  139. * data read operation
  140. */
  141. struct fscache_retrieval {
  142. struct fscache_operation op;
  143. struct address_space *mapping; /* netfs pages */
  144. fscache_rw_complete_t end_io_func; /* function to call on I/O completion */
  145. void *context; /* netfs read context (pinned) */
  146. struct list_head to_do; /* list of things to be done by the backend */
  147. unsigned long start_time; /* time at which retrieval started */
  148. };
  149. typedef int (*fscache_page_retrieval_func_t)(struct fscache_retrieval *op,
  150. struct page *page,
  151. gfp_t gfp);
  152. typedef int (*fscache_pages_retrieval_func_t)(struct fscache_retrieval *op,
  153. struct list_head *pages,
  154. unsigned *nr_pages,
  155. gfp_t gfp);
  156. /**
  157. * fscache_get_retrieval - Get an extra reference on a retrieval operation
  158. * @op: The retrieval operation to get a reference on
  159. *
  160. * Get an extra reference on a retrieval operation.
  161. */
  162. static inline
  163. struct fscache_retrieval *fscache_get_retrieval(struct fscache_retrieval *op)
  164. {
  165. atomic_inc(&op->op.usage);
  166. return op;
  167. }
  168. /**
  169. * fscache_enqueue_retrieval - Enqueue a retrieval operation for processing
  170. * @op: The retrieval operation affected
  171. *
  172. * Enqueue a retrieval operation for processing by the FS-Cache thread pool.
  173. */
  174. static inline void fscache_enqueue_retrieval(struct fscache_retrieval *op)
  175. {
  176. fscache_enqueue_operation(&op->op);
  177. }
  178. /**
  179. * fscache_put_retrieval - Drop a reference to a retrieval operation
  180. * @op: The retrieval operation affected
  181. *
  182. * Drop a reference to a retrieval operation.
  183. */
  184. static inline void fscache_put_retrieval(struct fscache_retrieval *op)
  185. {
  186. fscache_put_operation(&op->op);
  187. }
  188. /*
  189. * cached page storage work item
  190. * - used to do three things:
  191. * - batch writes to the cache
  192. * - do cache writes asynchronously
  193. * - defer writes until cache object lookup completion
  194. */
  195. struct fscache_storage {
  196. struct fscache_operation op;
  197. pgoff_t store_limit; /* don't write more than this */
  198. };
  199. /*
  200. * cache operations
  201. */
  202. struct fscache_cache_ops {
  203. /* name of cache provider */
  204. const char *name;
  205. /* allocate an object record for a cookie */
  206. struct fscache_object *(*alloc_object)(struct fscache_cache *cache,
  207. struct fscache_cookie *cookie);
  208. /* look up the object for a cookie
  209. * - return -ETIMEDOUT to be requeued
  210. */
  211. int (*lookup_object)(struct fscache_object *object);
  212. /* finished looking up */
  213. void (*lookup_complete)(struct fscache_object *object);
  214. /* increment the usage count on this object (may fail if unmounting) */
  215. struct fscache_object *(*grab_object)(struct fscache_object *object);
  216. /* pin an object in the cache */
  217. int (*pin_object)(struct fscache_object *object);
  218. /* unpin an object in the cache */
  219. void (*unpin_object)(struct fscache_object *object);
  220. /* store the updated auxilliary data on an object */
  221. void (*update_object)(struct fscache_object *object);
  222. /* discard the resources pinned by an object and effect retirement if
  223. * necessary */
  224. void (*drop_object)(struct fscache_object *object);
  225. /* dispose of a reference to an object */
  226. void (*put_object)(struct fscache_object *object);
  227. /* sync a cache */
  228. void (*sync_cache)(struct fscache_cache *cache);
  229. /* notification that the attributes of a non-index object (such as
  230. * i_size) have changed */
  231. int (*attr_changed)(struct fscache_object *object);
  232. /* reserve space for an object's data and associated metadata */
  233. int (*reserve_space)(struct fscache_object *object, loff_t i_size);
  234. /* request a backing block for a page be read or allocated in the
  235. * cache */
  236. fscache_page_retrieval_func_t read_or_alloc_page;
  237. /* request backing blocks for a list of pages be read or allocated in
  238. * the cache */
  239. fscache_pages_retrieval_func_t read_or_alloc_pages;
  240. /* request a backing block for a page be allocated in the cache so that
  241. * it can be written directly */
  242. fscache_page_retrieval_func_t allocate_page;
  243. /* request backing blocks for pages be allocated in the cache so that
  244. * they can be written directly */
  245. fscache_pages_retrieval_func_t allocate_pages;
  246. /* write a page to its backing block in the cache */
  247. int (*write_page)(struct fscache_storage *op, struct page *page);
  248. /* detach backing block from a page (optional)
  249. * - must release the cookie lock before returning
  250. * - may sleep
  251. */
  252. void (*uncache_page)(struct fscache_object *object,
  253. struct page *page);
  254. /* dissociate a cache from all the pages it was backing */
  255. void (*dissociate_pages)(struct fscache_cache *cache);
  256. };
  257. /*
  258. * data file or index object cookie
  259. * - a file will only appear in one cache
  260. * - a request to cache a file may or may not be honoured, subject to
  261. * constraints such as disk space
  262. * - indices are created on disk just-in-time
  263. */
  264. struct fscache_cookie {
  265. atomic_t usage; /* number of users of this cookie */
  266. atomic_t n_children; /* number of children of this cookie */
  267. spinlock_t lock;
  268. spinlock_t stores_lock; /* lock on page store tree */
  269. struct hlist_head backing_objects; /* object(s) backing this file/index */
  270. const struct fscache_cookie_def *def; /* definition */
  271. struct fscache_cookie *parent; /* parent of this entry */
  272. void *netfs_data; /* back pointer to netfs */
  273. struct radix_tree_root stores; /* pages to be stored on this cookie */
  274. #define FSCACHE_COOKIE_PENDING_TAG 0 /* pages tag: pending write to cache */
  275. #define FSCACHE_COOKIE_STORING_TAG 1 /* pages tag: writing to cache */
  276. unsigned long flags;
  277. #define FSCACHE_COOKIE_LOOKING_UP 0 /* T if non-index cookie being looked up still */
  278. #define FSCACHE_COOKIE_CREATING 1 /* T if non-index object being created still */
  279. #define FSCACHE_COOKIE_NO_DATA_YET 2 /* T if new object with no cached data yet */
  280. #define FSCACHE_COOKIE_PENDING_FILL 3 /* T if pending initial fill on object */
  281. #define FSCACHE_COOKIE_FILLING 4 /* T if filling object incrementally */
  282. #define FSCACHE_COOKIE_UNAVAILABLE 5 /* T if cookie is unavailable (error, etc) */
  283. };
  284. extern struct fscache_cookie fscache_fsdef_index;
  285. /*
  286. * on-disk cache file or index handle
  287. */
  288. struct fscache_object {
  289. enum fscache_object_state {
  290. FSCACHE_OBJECT_INIT, /* object in initial unbound state */
  291. FSCACHE_OBJECT_LOOKING_UP, /* looking up object */
  292. FSCACHE_OBJECT_CREATING, /* creating object */
  293. /* active states */
  294. FSCACHE_OBJECT_AVAILABLE, /* cleaning up object after creation */
  295. FSCACHE_OBJECT_ACTIVE, /* object is usable */
  296. FSCACHE_OBJECT_UPDATING, /* object is updating */
  297. /* terminal states */
  298. FSCACHE_OBJECT_DYING, /* object waiting for accessors to finish */
  299. FSCACHE_OBJECT_LC_DYING, /* object cleaning up after lookup/create */
  300. FSCACHE_OBJECT_ABORT_INIT, /* abort the init state */
  301. FSCACHE_OBJECT_RELEASING, /* releasing object */
  302. FSCACHE_OBJECT_RECYCLING, /* retiring object */
  303. FSCACHE_OBJECT_WITHDRAWING, /* withdrawing object */
  304. FSCACHE_OBJECT_DEAD, /* object is now dead */
  305. FSCACHE_OBJECT__NSTATES
  306. } state;
  307. int debug_id; /* debugging ID */
  308. int n_children; /* number of child objects */
  309. int n_ops; /* number of ops outstanding on object */
  310. int n_obj_ops; /* number of object ops outstanding on object */
  311. int n_in_progress; /* number of ops in progress */
  312. int n_exclusive; /* number of exclusive ops queued */
  313. atomic_t n_reads; /* number of read ops in progress */
  314. spinlock_t lock; /* state and operations lock */
  315. unsigned long lookup_jif; /* time at which lookup started */
  316. unsigned long event_mask; /* events this object is interested in */
  317. unsigned long events; /* events to be processed by this object
  318. * (order is important - using fls) */
  319. #define FSCACHE_OBJECT_EV_REQUEUE 0 /* T if object should be requeued */
  320. #define FSCACHE_OBJECT_EV_UPDATE 1 /* T if object should be updated */
  321. #define FSCACHE_OBJECT_EV_CLEARED 2 /* T if accessors all gone */
  322. #define FSCACHE_OBJECT_EV_ERROR 3 /* T if fatal error occurred during processing */
  323. #define FSCACHE_OBJECT_EV_RELEASE 4 /* T if netfs requested object release */
  324. #define FSCACHE_OBJECT_EV_RETIRE 5 /* T if netfs requested object retirement */
  325. #define FSCACHE_OBJECT_EV_WITHDRAW 6 /* T if cache requested object withdrawal */
  326. #define FSCACHE_OBJECT_EVENTS_MASK 0x7f /* mask of all events*/
  327. unsigned long flags;
  328. #define FSCACHE_OBJECT_LOCK 0 /* T if object is busy being processed */
  329. #define FSCACHE_OBJECT_PENDING_WRITE 1 /* T if object has pending write */
  330. #define FSCACHE_OBJECT_WAITING 2 /* T if object is waiting on its parent */
  331. struct list_head cache_link; /* link in cache->object_list */
  332. struct hlist_node cookie_link; /* link in cookie->backing_objects */
  333. struct fscache_cache *cache; /* cache that supplied this object */
  334. struct fscache_cookie *cookie; /* netfs's file/index object */
  335. struct fscache_object *parent; /* parent object */
  336. struct slow_work work; /* attention scheduling record */
  337. struct list_head dependents; /* FIFO of dependent objects */
  338. struct list_head dep_link; /* link in parent's dependents list */
  339. struct list_head pending_ops; /* unstarted operations on this object */
  340. #ifdef CONFIG_FSCACHE_OBJECT_LIST
  341. struct rb_node objlist_link; /* link in global object list */
  342. #endif
  343. pgoff_t store_limit; /* current storage limit */
  344. loff_t store_limit_l; /* current storage limit */
  345. };
  346. extern const char *fscache_object_states[];
  347. #define fscache_object_is_active(obj) \
  348. (!test_bit(FSCACHE_IOERROR, &(obj)->cache->flags) && \
  349. (obj)->state >= FSCACHE_OBJECT_AVAILABLE && \
  350. (obj)->state < FSCACHE_OBJECT_DYING)
  351. #define fscache_object_is_dead(obj) \
  352. (test_bit(FSCACHE_IOERROR, &(obj)->cache->flags) && \
  353. (obj)->state >= FSCACHE_OBJECT_DYING)
  354. extern const struct slow_work_ops fscache_object_slow_work_ops;
  355. /**
  356. * fscache_object_init - Initialise a cache object description
  357. * @object: Object description
  358. *
  359. * Initialise a cache object description to its basic values.
  360. *
  361. * See Documentation/filesystems/caching/backend-api.txt for a complete
  362. * description.
  363. */
  364. static inline
  365. void fscache_object_init(struct fscache_object *object,
  366. struct fscache_cookie *cookie,
  367. struct fscache_cache *cache)
  368. {
  369. atomic_inc(&cache->object_count);
  370. object->state = FSCACHE_OBJECT_INIT;
  371. spin_lock_init(&object->lock);
  372. INIT_LIST_HEAD(&object->cache_link);
  373. INIT_HLIST_NODE(&object->cookie_link);
  374. vslow_work_init(&object->work, &fscache_object_slow_work_ops);
  375. INIT_LIST_HEAD(&object->dependents);
  376. INIT_LIST_HEAD(&object->dep_link);
  377. INIT_LIST_HEAD(&object->pending_ops);
  378. object->n_children = 0;
  379. object->n_ops = object->n_in_progress = object->n_exclusive = 0;
  380. object->events = object->event_mask = 0;
  381. object->flags = 0;
  382. object->store_limit = 0;
  383. object->store_limit_l = 0;
  384. object->cache = cache;
  385. object->cookie = cookie;
  386. object->parent = NULL;
  387. }
  388. extern void fscache_object_lookup_negative(struct fscache_object *object);
  389. extern void fscache_obtained_object(struct fscache_object *object);
  390. #ifdef CONFIG_FSCACHE_OBJECT_LIST
  391. extern void fscache_object_destroy(struct fscache_object *object);
  392. #else
  393. #define fscache_object_destroy(object) do {} while(0)
  394. #endif
  395. /**
  396. * fscache_object_destroyed - Note destruction of an object in a cache
  397. * @cache: The cache from which the object came
  398. *
  399. * Note the destruction and deallocation of an object record in a cache.
  400. */
  401. static inline void fscache_object_destroyed(struct fscache_cache *cache)
  402. {
  403. if (atomic_dec_and_test(&cache->object_count))
  404. wake_up_all(&fscache_cache_cleared_wq);
  405. }
  406. /**
  407. * fscache_object_lookup_error - Note an object encountered an error
  408. * @object: The object on which the error was encountered
  409. *
  410. * Note that an object encountered a fatal error (usually an I/O error) and
  411. * that it should be withdrawn as soon as possible.
  412. */
  413. static inline void fscache_object_lookup_error(struct fscache_object *object)
  414. {
  415. set_bit(FSCACHE_OBJECT_EV_ERROR, &object->events);
  416. }
  417. /**
  418. * fscache_set_store_limit - Set the maximum size to be stored in an object
  419. * @object: The object to set the maximum on
  420. * @i_size: The limit to set in bytes
  421. *
  422. * Set the maximum size an object is permitted to reach, implying the highest
  423. * byte that may be written. Intended to be called by the attr_changed() op.
  424. *
  425. * See Documentation/filesystems/caching/backend-api.txt for a complete
  426. * description.
  427. */
  428. static inline
  429. void fscache_set_store_limit(struct fscache_object *object, loff_t i_size)
  430. {
  431. object->store_limit_l = i_size;
  432. object->store_limit = i_size >> PAGE_SHIFT;
  433. if (i_size & ~PAGE_MASK)
  434. object->store_limit++;
  435. }
  436. /**
  437. * fscache_end_io - End a retrieval operation on a page
  438. * @op: The FS-Cache operation covering the retrieval
  439. * @page: The page that was to be fetched
  440. * @error: The error code (0 if successful)
  441. *
  442. * Note the end of an operation to retrieve a page, as covered by a particular
  443. * operation record.
  444. */
  445. static inline void fscache_end_io(struct fscache_retrieval *op,
  446. struct page *page, int error)
  447. {
  448. op->end_io_func(page, op->context, error);
  449. }
  450. /*
  451. * out-of-line cache backend functions
  452. */
  453. extern void fscache_init_cache(struct fscache_cache *cache,
  454. const struct fscache_cache_ops *ops,
  455. const char *idfmt,
  456. ...) __attribute__ ((format (printf, 3, 4)));
  457. extern int fscache_add_cache(struct fscache_cache *cache,
  458. struct fscache_object *fsdef,
  459. const char *tagname);
  460. extern void fscache_withdraw_cache(struct fscache_cache *cache);
  461. extern void fscache_io_error(struct fscache_cache *cache);
  462. extern void fscache_mark_pages_cached(struct fscache_retrieval *op,
  463. struct pagevec *pagevec);
  464. extern enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
  465. const void *data,
  466. uint16_t datalen);
  467. #endif /* _LINUX_FSCACHE_CACHE_H */