fscache-cache.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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/workqueue.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. typedef void (*fscache_operation_cancel_t)(struct fscache_operation *op);
  69. enum fscache_operation_state {
  70. FSCACHE_OP_ST_BLANK, /* Op is not yet submitted */
  71. FSCACHE_OP_ST_INITIALISED, /* Op is initialised */
  72. FSCACHE_OP_ST_PENDING, /* Op is blocked from running */
  73. FSCACHE_OP_ST_IN_PROGRESS, /* Op is in progress */
  74. FSCACHE_OP_ST_COMPLETE, /* Op is complete */
  75. FSCACHE_OP_ST_CANCELLED, /* Op has been cancelled */
  76. FSCACHE_OP_ST_DEAD /* Op is now dead */
  77. };
  78. struct fscache_operation {
  79. struct work_struct work; /* record for async ops */
  80. struct list_head pend_link; /* link in object->pending_ops */
  81. struct fscache_object *object; /* object to be operated upon */
  82. unsigned long flags;
  83. #define FSCACHE_OP_TYPE 0x000f /* operation type */
  84. #define FSCACHE_OP_ASYNC 0x0001 /* - async op, processor may sleep for disk */
  85. #define FSCACHE_OP_MYTHREAD 0x0002 /* - processing is done be issuing thread, not pool */
  86. #define FSCACHE_OP_WAITING 4 /* cleared when op is woken */
  87. #define FSCACHE_OP_EXCLUSIVE 5 /* exclusive op, other ops must wait */
  88. #define FSCACHE_OP_DEC_READ_CNT 6 /* decrement object->n_reads on destruction */
  89. #define FSCACHE_OP_UNUSE_COOKIE 7 /* call fscache_unuse_cookie() on completion */
  90. #define FSCACHE_OP_KEEP_FLAGS 0x00f0 /* flags to keep when repurposing an op */
  91. enum fscache_operation_state state;
  92. atomic_t usage;
  93. unsigned debug_id; /* debugging ID */
  94. /* operation processor callback
  95. * - can be NULL if FSCACHE_OP_WAITING is going to be used to perform
  96. * the op in a non-pool thread */
  97. fscache_operation_processor_t processor;
  98. /* Operation cancellation cleanup (optional) */
  99. fscache_operation_cancel_t cancel;
  100. /* operation releaser */
  101. fscache_operation_release_t release;
  102. };
  103. extern atomic_t fscache_op_debug_id;
  104. extern void fscache_op_work_func(struct work_struct *work);
  105. extern void fscache_enqueue_operation(struct fscache_operation *);
  106. extern void fscache_op_complete(struct fscache_operation *, bool);
  107. extern void fscache_put_operation(struct fscache_operation *);
  108. extern void fscache_operation_init(struct fscache_operation *,
  109. fscache_operation_processor_t,
  110. fscache_operation_cancel_t,
  111. fscache_operation_release_t);
  112. /*
  113. * data read operation
  114. */
  115. struct fscache_retrieval {
  116. struct fscache_operation op;
  117. struct fscache_cookie *cookie; /* The netfs cookie */
  118. struct address_space *mapping; /* netfs pages */
  119. fscache_rw_complete_t end_io_func; /* function to call on I/O completion */
  120. void *context; /* netfs read context (pinned) */
  121. struct list_head to_do; /* list of things to be done by the backend */
  122. unsigned long start_time; /* time at which retrieval started */
  123. atomic_t n_pages; /* number of pages to be retrieved */
  124. };
  125. typedef int (*fscache_page_retrieval_func_t)(struct fscache_retrieval *op,
  126. struct page *page,
  127. gfp_t gfp);
  128. typedef int (*fscache_pages_retrieval_func_t)(struct fscache_retrieval *op,
  129. struct list_head *pages,
  130. unsigned *nr_pages,
  131. gfp_t gfp);
  132. /**
  133. * fscache_get_retrieval - Get an extra reference on a retrieval operation
  134. * @op: The retrieval operation to get a reference on
  135. *
  136. * Get an extra reference on a retrieval operation.
  137. */
  138. static inline
  139. struct fscache_retrieval *fscache_get_retrieval(struct fscache_retrieval *op)
  140. {
  141. atomic_inc(&op->op.usage);
  142. return op;
  143. }
  144. /**
  145. * fscache_enqueue_retrieval - Enqueue a retrieval operation for processing
  146. * @op: The retrieval operation affected
  147. *
  148. * Enqueue a retrieval operation for processing by the FS-Cache thread pool.
  149. */
  150. static inline void fscache_enqueue_retrieval(struct fscache_retrieval *op)
  151. {
  152. fscache_enqueue_operation(&op->op);
  153. }
  154. /**
  155. * fscache_retrieval_complete - Record (partial) completion of a retrieval
  156. * @op: The retrieval operation affected
  157. * @n_pages: The number of pages to account for
  158. */
  159. static inline void fscache_retrieval_complete(struct fscache_retrieval *op,
  160. int n_pages)
  161. {
  162. atomic_sub(n_pages, &op->n_pages);
  163. if (atomic_read(&op->n_pages) <= 0)
  164. fscache_op_complete(&op->op, true);
  165. }
  166. /**
  167. * fscache_put_retrieval - Drop a reference to a retrieval operation
  168. * @op: The retrieval operation affected
  169. *
  170. * Drop a reference to a retrieval operation.
  171. */
  172. static inline void fscache_put_retrieval(struct fscache_retrieval *op)
  173. {
  174. fscache_put_operation(&op->op);
  175. }
  176. /*
  177. * cached page storage work item
  178. * - used to do three things:
  179. * - batch writes to the cache
  180. * - do cache writes asynchronously
  181. * - defer writes until cache object lookup completion
  182. */
  183. struct fscache_storage {
  184. struct fscache_operation op;
  185. pgoff_t store_limit; /* don't write more than this */
  186. };
  187. /*
  188. * cache operations
  189. */
  190. struct fscache_cache_ops {
  191. /* name of cache provider */
  192. const char *name;
  193. /* allocate an object record for a cookie */
  194. struct fscache_object *(*alloc_object)(struct fscache_cache *cache,
  195. struct fscache_cookie *cookie);
  196. /* look up the object for a cookie
  197. * - return -ETIMEDOUT to be requeued
  198. */
  199. int (*lookup_object)(struct fscache_object *object);
  200. /* finished looking up */
  201. void (*lookup_complete)(struct fscache_object *object);
  202. /* increment the usage count on this object (may fail if unmounting) */
  203. struct fscache_object *(*grab_object)(struct fscache_object *object);
  204. /* pin an object in the cache */
  205. int (*pin_object)(struct fscache_object *object);
  206. /* unpin an object in the cache */
  207. void (*unpin_object)(struct fscache_object *object);
  208. /* check the consistency between the backing cache and the FS-Cache
  209. * cookie */
  210. bool (*check_consistency)(struct fscache_operation *op);
  211. /* store the updated auxiliary data on an object */
  212. void (*update_object)(struct fscache_object *object);
  213. /* Invalidate an object */
  214. void (*invalidate_object)(struct fscache_operation *op);
  215. /* discard the resources pinned by an object and effect retirement if
  216. * necessary */
  217. void (*drop_object)(struct fscache_object *object);
  218. /* dispose of a reference to an object */
  219. void (*put_object)(struct fscache_object *object);
  220. /* sync a cache */
  221. void (*sync_cache)(struct fscache_cache *cache);
  222. /* notification that the attributes of a non-index object (such as
  223. * i_size) have changed */
  224. int (*attr_changed)(struct fscache_object *object);
  225. /* reserve space for an object's data and associated metadata */
  226. int (*reserve_space)(struct fscache_object *object, loff_t i_size);
  227. /* request a backing block for a page be read or allocated in the
  228. * cache */
  229. fscache_page_retrieval_func_t read_or_alloc_page;
  230. /* request backing blocks for a list of pages be read or allocated in
  231. * the cache */
  232. fscache_pages_retrieval_func_t read_or_alloc_pages;
  233. /* request a backing block for a page be allocated in the cache so that
  234. * it can be written directly */
  235. fscache_page_retrieval_func_t allocate_page;
  236. /* request backing blocks for pages be allocated in the cache so that
  237. * they can be written directly */
  238. fscache_pages_retrieval_func_t allocate_pages;
  239. /* write a page to its backing block in the cache */
  240. int (*write_page)(struct fscache_storage *op, struct page *page);
  241. /* detach backing block from a page (optional)
  242. * - must release the cookie lock before returning
  243. * - may sleep
  244. */
  245. void (*uncache_page)(struct fscache_object *object,
  246. struct page *page);
  247. /* dissociate a cache from all the pages it was backing */
  248. void (*dissociate_pages)(struct fscache_cache *cache);
  249. };
  250. extern struct fscache_cookie fscache_fsdef_index;
  251. /*
  252. * Event list for fscache_object::{event_mask,events}
  253. */
  254. enum {
  255. FSCACHE_OBJECT_EV_NEW_CHILD, /* T if object has a new child */
  256. FSCACHE_OBJECT_EV_PARENT_READY, /* T if object's parent is ready */
  257. FSCACHE_OBJECT_EV_UPDATE, /* T if object should be updated */
  258. FSCACHE_OBJECT_EV_INVALIDATE, /* T if cache requested object invalidation */
  259. FSCACHE_OBJECT_EV_CLEARED, /* T if accessors all gone */
  260. FSCACHE_OBJECT_EV_ERROR, /* T if fatal error occurred during processing */
  261. FSCACHE_OBJECT_EV_KILL, /* T if netfs relinquished or cache withdrew object */
  262. NR_FSCACHE_OBJECT_EVENTS
  263. };
  264. #define FSCACHE_OBJECT_EVENTS_MASK ((1UL << NR_FSCACHE_OBJECT_EVENTS) - 1)
  265. /*
  266. * States for object state machine.
  267. */
  268. struct fscache_transition {
  269. unsigned long events;
  270. const struct fscache_state *transit_to;
  271. };
  272. struct fscache_state {
  273. char name[24];
  274. char short_name[8];
  275. const struct fscache_state *(*work)(struct fscache_object *object,
  276. int event);
  277. const struct fscache_transition transitions[];
  278. };
  279. /*
  280. * on-disk cache file or index handle
  281. */
  282. struct fscache_object {
  283. const struct fscache_state *state; /* Object state machine state */
  284. const struct fscache_transition *oob_table; /* OOB state transition table */
  285. int debug_id; /* debugging ID */
  286. int n_children; /* number of child objects */
  287. int n_ops; /* number of extant ops on object */
  288. int n_obj_ops; /* number of object ops outstanding on object */
  289. int n_in_progress; /* number of ops in progress */
  290. int n_exclusive; /* number of exclusive ops queued or in progress */
  291. atomic_t n_reads; /* number of read ops in progress */
  292. spinlock_t lock; /* state and operations lock */
  293. unsigned long lookup_jif; /* time at which lookup started */
  294. unsigned long oob_event_mask; /* OOB events this object is interested in */
  295. unsigned long event_mask; /* events this object is interested in */
  296. unsigned long events; /* events to be processed by this object
  297. * (order is important - using fls) */
  298. unsigned long flags;
  299. #define FSCACHE_OBJECT_LOCK 0 /* T if object is busy being processed */
  300. #define FSCACHE_OBJECT_PENDING_WRITE 1 /* T if object has pending write */
  301. #define FSCACHE_OBJECT_WAITING 2 /* T if object is waiting on its parent */
  302. #define FSCACHE_OBJECT_IS_LIVE 3 /* T if object is not withdrawn or relinquished */
  303. #define FSCACHE_OBJECT_IS_LOOKED_UP 4 /* T if object has been looked up */
  304. #define FSCACHE_OBJECT_IS_AVAILABLE 5 /* T if object has become active */
  305. #define FSCACHE_OBJECT_RETIRED 6 /* T if object was retired on relinquishment */
  306. #define FSCACHE_OBJECT_KILLED_BY_CACHE 7 /* T if object was killed by the cache */
  307. #define FSCACHE_OBJECT_RUN_AFTER_DEAD 8 /* T if object has been dispatched after death */
  308. struct list_head cache_link; /* link in cache->object_list */
  309. struct hlist_node cookie_link; /* link in cookie->backing_objects */
  310. struct fscache_cache *cache; /* cache that supplied this object */
  311. struct fscache_cookie *cookie; /* netfs's file/index object */
  312. struct fscache_object *parent; /* parent object */
  313. struct work_struct work; /* attention scheduling record */
  314. struct list_head dependents; /* FIFO of dependent objects */
  315. struct list_head dep_link; /* link in parent's dependents list */
  316. struct list_head pending_ops; /* unstarted operations on this object */
  317. #ifdef CONFIG_FSCACHE_OBJECT_LIST
  318. struct rb_node objlist_link; /* link in global object list */
  319. #endif
  320. pgoff_t store_limit; /* current storage limit */
  321. loff_t store_limit_l; /* current storage limit */
  322. };
  323. extern void fscache_object_init(struct fscache_object *, struct fscache_cookie *,
  324. struct fscache_cache *);
  325. extern void fscache_object_destroy(struct fscache_object *);
  326. extern void fscache_object_lookup_negative(struct fscache_object *object);
  327. extern void fscache_obtained_object(struct fscache_object *object);
  328. static inline bool fscache_object_is_live(struct fscache_object *object)
  329. {
  330. return test_bit(FSCACHE_OBJECT_IS_LIVE, &object->flags);
  331. }
  332. static inline bool fscache_object_is_dying(struct fscache_object *object)
  333. {
  334. return !fscache_object_is_live(object);
  335. }
  336. static inline bool fscache_object_is_available(struct fscache_object *object)
  337. {
  338. return test_bit(FSCACHE_OBJECT_IS_AVAILABLE, &object->flags);
  339. }
  340. static inline bool fscache_cache_is_broken(struct fscache_object *object)
  341. {
  342. return test_bit(FSCACHE_IOERROR, &object->cache->flags);
  343. }
  344. static inline bool fscache_object_is_active(struct fscache_object *object)
  345. {
  346. return fscache_object_is_available(object) &&
  347. fscache_object_is_live(object) &&
  348. !fscache_cache_is_broken(object);
  349. }
  350. /**
  351. * fscache_object_destroyed - Note destruction of an object in a cache
  352. * @cache: The cache from which the object came
  353. *
  354. * Note the destruction and deallocation of an object record in a cache.
  355. */
  356. static inline void fscache_object_destroyed(struct fscache_cache *cache)
  357. {
  358. if (atomic_dec_and_test(&cache->object_count))
  359. wake_up_all(&fscache_cache_cleared_wq);
  360. }
  361. /**
  362. * fscache_object_lookup_error - Note an object encountered an error
  363. * @object: The object on which the error was encountered
  364. *
  365. * Note that an object encountered a fatal error (usually an I/O error) and
  366. * that it should be withdrawn as soon as possible.
  367. */
  368. static inline void fscache_object_lookup_error(struct fscache_object *object)
  369. {
  370. set_bit(FSCACHE_OBJECT_EV_ERROR, &object->events);
  371. }
  372. /**
  373. * fscache_set_store_limit - Set the maximum size to be stored in an object
  374. * @object: The object to set the maximum on
  375. * @i_size: The limit to set in bytes
  376. *
  377. * Set the maximum size an object is permitted to reach, implying the highest
  378. * byte that may be written. Intended to be called by the attr_changed() op.
  379. *
  380. * See Documentation/filesystems/caching/backend-api.txt for a complete
  381. * description.
  382. */
  383. static inline
  384. void fscache_set_store_limit(struct fscache_object *object, loff_t i_size)
  385. {
  386. object->store_limit_l = i_size;
  387. object->store_limit = i_size >> PAGE_SHIFT;
  388. if (i_size & ~PAGE_MASK)
  389. object->store_limit++;
  390. }
  391. /**
  392. * fscache_end_io - End a retrieval operation on a page
  393. * @op: The FS-Cache operation covering the retrieval
  394. * @page: The page that was to be fetched
  395. * @error: The error code (0 if successful)
  396. *
  397. * Note the end of an operation to retrieve a page, as covered by a particular
  398. * operation record.
  399. */
  400. static inline void fscache_end_io(struct fscache_retrieval *op,
  401. struct page *page, int error)
  402. {
  403. op->end_io_func(page, op->context, error);
  404. }
  405. static inline void __fscache_use_cookie(struct fscache_cookie *cookie)
  406. {
  407. atomic_inc(&cookie->n_active);
  408. }
  409. /**
  410. * fscache_use_cookie - Request usage of cookie attached to an object
  411. * @object: Object description
  412. *
  413. * Request usage of the cookie attached to an object. NULL is returned if the
  414. * relinquishment had reduced the cookie usage count to 0.
  415. */
  416. static inline bool fscache_use_cookie(struct fscache_object *object)
  417. {
  418. struct fscache_cookie *cookie = object->cookie;
  419. return atomic_inc_not_zero(&cookie->n_active) != 0;
  420. }
  421. static inline bool __fscache_unuse_cookie(struct fscache_cookie *cookie)
  422. {
  423. return atomic_dec_and_test(&cookie->n_active);
  424. }
  425. static inline void __fscache_wake_unused_cookie(struct fscache_cookie *cookie)
  426. {
  427. wake_up_atomic_t(&cookie->n_active);
  428. }
  429. /**
  430. * fscache_unuse_cookie - Cease usage of cookie attached to an object
  431. * @object: Object description
  432. *
  433. * Cease usage of the cookie attached to an object. When the users count
  434. * reaches zero then the cookie relinquishment will be permitted to proceed.
  435. */
  436. static inline void fscache_unuse_cookie(struct fscache_object *object)
  437. {
  438. struct fscache_cookie *cookie = object->cookie;
  439. if (__fscache_unuse_cookie(cookie))
  440. __fscache_wake_unused_cookie(cookie);
  441. }
  442. /*
  443. * out-of-line cache backend functions
  444. */
  445. extern __printf(3, 4)
  446. void fscache_init_cache(struct fscache_cache *cache,
  447. const struct fscache_cache_ops *ops,
  448. const char *idfmt, ...);
  449. extern int fscache_add_cache(struct fscache_cache *cache,
  450. struct fscache_object *fsdef,
  451. const char *tagname);
  452. extern void fscache_withdraw_cache(struct fscache_cache *cache);
  453. extern void fscache_io_error(struct fscache_cache *cache);
  454. extern void fscache_mark_page_cached(struct fscache_retrieval *op,
  455. struct page *page);
  456. extern void fscache_mark_pages_cached(struct fscache_retrieval *op,
  457. struct pagevec *pagevec);
  458. extern bool fscache_object_sleep_till_congested(signed long *timeoutp);
  459. extern enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
  460. const void *data,
  461. uint16_t datalen);
  462. extern void fscache_object_retrying_stale(struct fscache_object *object);
  463. enum fscache_why_object_killed {
  464. FSCACHE_OBJECT_IS_STALE,
  465. FSCACHE_OBJECT_NO_SPACE,
  466. FSCACHE_OBJECT_WAS_RETIRED,
  467. FSCACHE_OBJECT_WAS_CULLED,
  468. };
  469. extern void fscache_object_mark_killed(struct fscache_object *object,
  470. enum fscache_why_object_killed why);
  471. #endif /* _LINUX_FSCACHE_CACHE_H */