fscache-cache.h 19 KB

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