fscache-cache.h 19 KB

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