fscache.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. /* General filesystem caching 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/netfs-api.txt
  14. *
  15. * for a description of the network filesystem interface declared here.
  16. */
  17. #ifndef _LINUX_FSCACHE_H
  18. #define _LINUX_FSCACHE_H
  19. #include <linux/fs.h>
  20. #include <linux/list.h>
  21. #include <linux/pagemap.h>
  22. #include <linux/pagevec.h>
  23. #if defined(CONFIG_FSCACHE) || defined(CONFIG_FSCACHE_MODULE)
  24. #define fscache_available() (1)
  25. #define fscache_cookie_valid(cookie) (cookie)
  26. #else
  27. #define fscache_available() (0)
  28. #define fscache_cookie_valid(cookie) (0)
  29. #endif
  30. /*
  31. * overload PG_private_2 to give us PG_fscache - this is used to indicate that
  32. * a page is currently backed by a local disk cache
  33. */
  34. #define PageFsCache(page) PagePrivate2((page))
  35. #define SetPageFsCache(page) SetPagePrivate2((page))
  36. #define ClearPageFsCache(page) ClearPagePrivate2((page))
  37. #define TestSetPageFsCache(page) TestSetPagePrivate2((page))
  38. #define TestClearPageFsCache(page) TestClearPagePrivate2((page))
  39. /* pattern used to fill dead space in an index entry */
  40. #define FSCACHE_INDEX_DEADFILL_PATTERN 0x79
  41. struct pagevec;
  42. struct fscache_cache_tag;
  43. struct fscache_cookie;
  44. struct fscache_netfs;
  45. typedef void (*fscache_rw_complete_t)(struct page *page,
  46. void *context,
  47. int error);
  48. /* result of index entry consultation */
  49. enum fscache_checkaux {
  50. FSCACHE_CHECKAUX_OKAY, /* entry okay as is */
  51. FSCACHE_CHECKAUX_NEEDS_UPDATE, /* entry requires update */
  52. FSCACHE_CHECKAUX_OBSOLETE, /* entry requires deletion */
  53. };
  54. /*
  55. * fscache cookie definition
  56. */
  57. struct fscache_cookie_def {
  58. /* name of cookie type */
  59. char name[16];
  60. /* cookie type */
  61. uint8_t type;
  62. #define FSCACHE_COOKIE_TYPE_INDEX 0
  63. #define FSCACHE_COOKIE_TYPE_DATAFILE 1
  64. /* select the cache into which to insert an entry in this index
  65. * - optional
  66. * - should return a cache identifier or NULL to cause the cache to be
  67. * inherited from the parent if possible or the first cache picked
  68. * for a non-index file if not
  69. */
  70. struct fscache_cache_tag *(*select_cache)(
  71. const void *parent_netfs_data,
  72. const void *cookie_netfs_data);
  73. /* get an index key
  74. * - should store the key data in the buffer
  75. * - should return the amount of data stored
  76. * - not permitted to return an error
  77. * - the netfs data from the cookie being used as the source is
  78. * presented
  79. */
  80. uint16_t (*get_key)(const void *cookie_netfs_data,
  81. void *buffer,
  82. uint16_t bufmax);
  83. /* get certain file attributes from the netfs data
  84. * - this function can be absent for an index
  85. * - not permitted to return an error
  86. * - the netfs data from the cookie being used as the source is
  87. * presented
  88. */
  89. void (*get_attr)(const void *cookie_netfs_data, uint64_t *size);
  90. /* get the auxiliary data from netfs data
  91. * - this function can be absent if the index carries no state data
  92. * - should store the auxiliary data in the buffer
  93. * - should return the amount of amount stored
  94. * - not permitted to return an error
  95. * - the netfs data from the cookie being used as the source is
  96. * presented
  97. */
  98. uint16_t (*get_aux)(const void *cookie_netfs_data,
  99. void *buffer,
  100. uint16_t bufmax);
  101. /* consult the netfs about the state of an object
  102. * - this function can be absent if the index carries no state data
  103. * - the netfs data from the cookie being used as the target is
  104. * presented, as is the auxiliary data
  105. */
  106. enum fscache_checkaux (*check_aux)(void *cookie_netfs_data,
  107. const void *data,
  108. uint16_t datalen);
  109. /* get an extra reference on a read context
  110. * - this function can be absent if the completion function doesn't
  111. * require a context
  112. */
  113. void (*get_context)(void *cookie_netfs_data, void *context);
  114. /* release an extra reference on a read context
  115. * - this function can be absent if the completion function doesn't
  116. * require a context
  117. */
  118. void (*put_context)(void *cookie_netfs_data, void *context);
  119. /* indicate page that now have cache metadata retained
  120. * - this function should mark the specified page as now being cached
  121. * - the page will have been marked with PG_fscache before this is
  122. * called, so this is optional
  123. */
  124. void (*mark_page_cached)(void *cookie_netfs_data,
  125. struct address_space *mapping,
  126. struct page *page);
  127. /* indicate the cookie is no longer cached
  128. * - this function is called when the backing store currently caching
  129. * a cookie is removed
  130. * - the netfs should use this to clean up any markers indicating
  131. * cached pages
  132. * - this is mandatory for any object that may have data
  133. */
  134. void (*now_uncached)(void *cookie_netfs_data);
  135. };
  136. /*
  137. * fscache cached network filesystem type
  138. * - name, version and ops must be filled in before registration
  139. * - all other fields will be set during registration
  140. */
  141. struct fscache_netfs {
  142. uint32_t version; /* indexing version */
  143. const char *name; /* filesystem name */
  144. struct fscache_cookie *primary_index;
  145. struct list_head link; /* internal link */
  146. };
  147. /*
  148. * data file or index object cookie
  149. * - a file will only appear in one cache
  150. * - a request to cache a file may or may not be honoured, subject to
  151. * constraints such as disk space
  152. * - indices are created on disk just-in-time
  153. */
  154. struct fscache_cookie {
  155. atomic_t usage; /* number of users of this cookie */
  156. atomic_t n_children; /* number of children of this cookie */
  157. atomic_t n_active; /* number of active users of netfs ptrs */
  158. spinlock_t lock;
  159. spinlock_t stores_lock; /* lock on page store tree */
  160. struct hlist_head backing_objects; /* object(s) backing this file/index */
  161. const struct fscache_cookie_def *def; /* definition */
  162. struct fscache_cookie *parent; /* parent of this entry */
  163. void *netfs_data; /* back pointer to netfs */
  164. struct radix_tree_root stores; /* pages to be stored on this cookie */
  165. #define FSCACHE_COOKIE_PENDING_TAG 0 /* pages tag: pending write to cache */
  166. #define FSCACHE_COOKIE_STORING_TAG 1 /* pages tag: writing to cache */
  167. unsigned long flags;
  168. #define FSCACHE_COOKIE_LOOKING_UP 0 /* T if non-index cookie being looked up still */
  169. #define FSCACHE_COOKIE_NO_DATA_YET 1 /* T if new object with no cached data yet */
  170. #define FSCACHE_COOKIE_UNAVAILABLE 2 /* T if cookie is unavailable (error, etc) */
  171. #define FSCACHE_COOKIE_INVALIDATING 3 /* T if cookie is being invalidated */
  172. #define FSCACHE_COOKIE_RELINQUISHED 4 /* T if cookie has been relinquished */
  173. #define FSCACHE_COOKIE_ENABLED 5 /* T if cookie is enabled */
  174. #define FSCACHE_COOKIE_ENABLEMENT_LOCK 6 /* T if cookie is being en/disabled */
  175. };
  176. static inline bool fscache_cookie_enabled(struct fscache_cookie *cookie)
  177. {
  178. return test_bit(FSCACHE_COOKIE_ENABLED, &cookie->flags);
  179. }
  180. /*
  181. * slow-path functions for when there is actually caching available, and the
  182. * netfs does actually have a valid token
  183. * - these are not to be called directly
  184. * - these are undefined symbols when FS-Cache is not configured and the
  185. * optimiser takes care of not using them
  186. */
  187. extern int __fscache_register_netfs(struct fscache_netfs *);
  188. extern void __fscache_unregister_netfs(struct fscache_netfs *);
  189. extern struct fscache_cache_tag *__fscache_lookup_cache_tag(const char *);
  190. extern void __fscache_release_cache_tag(struct fscache_cache_tag *);
  191. extern struct fscache_cookie *__fscache_acquire_cookie(
  192. struct fscache_cookie *,
  193. const struct fscache_cookie_def *,
  194. void *, bool);
  195. extern void __fscache_relinquish_cookie(struct fscache_cookie *, bool);
  196. extern int __fscache_check_consistency(struct fscache_cookie *);
  197. extern void __fscache_update_cookie(struct fscache_cookie *);
  198. extern int __fscache_attr_changed(struct fscache_cookie *);
  199. extern void __fscache_invalidate(struct fscache_cookie *);
  200. extern void __fscache_wait_on_invalidate(struct fscache_cookie *);
  201. extern int __fscache_read_or_alloc_page(struct fscache_cookie *,
  202. struct page *,
  203. fscache_rw_complete_t,
  204. void *,
  205. gfp_t);
  206. extern int __fscache_read_or_alloc_pages(struct fscache_cookie *,
  207. struct address_space *,
  208. struct list_head *,
  209. unsigned *,
  210. fscache_rw_complete_t,
  211. void *,
  212. gfp_t);
  213. extern int __fscache_alloc_page(struct fscache_cookie *, struct page *, gfp_t);
  214. extern int __fscache_write_page(struct fscache_cookie *, struct page *, gfp_t);
  215. extern void __fscache_uncache_page(struct fscache_cookie *, struct page *);
  216. extern bool __fscache_check_page_write(struct fscache_cookie *, struct page *);
  217. extern void __fscache_wait_on_page_write(struct fscache_cookie *, struct page *);
  218. extern bool __fscache_maybe_release_page(struct fscache_cookie *, struct page *,
  219. gfp_t);
  220. extern void __fscache_uncache_all_inode_pages(struct fscache_cookie *,
  221. struct inode *);
  222. extern void __fscache_readpages_cancel(struct fscache_cookie *cookie,
  223. struct list_head *pages);
  224. extern void __fscache_disable_cookie(struct fscache_cookie *, bool);
  225. extern void __fscache_enable_cookie(struct fscache_cookie *,
  226. bool (*)(void *), void *);
  227. /**
  228. * fscache_register_netfs - Register a filesystem as desiring caching services
  229. * @netfs: The description of the filesystem
  230. *
  231. * Register a filesystem as desiring caching services if they're available.
  232. *
  233. * See Documentation/filesystems/caching/netfs-api.txt for a complete
  234. * description.
  235. */
  236. static inline
  237. int fscache_register_netfs(struct fscache_netfs *netfs)
  238. {
  239. if (fscache_available())
  240. return __fscache_register_netfs(netfs);
  241. else
  242. return 0;
  243. }
  244. /**
  245. * fscache_unregister_netfs - Indicate that a filesystem no longer desires
  246. * caching services
  247. * @netfs: The description of the filesystem
  248. *
  249. * Indicate that a filesystem no longer desires caching services for the
  250. * moment.
  251. *
  252. * See Documentation/filesystems/caching/netfs-api.txt for a complete
  253. * description.
  254. */
  255. static inline
  256. void fscache_unregister_netfs(struct fscache_netfs *netfs)
  257. {
  258. if (fscache_available())
  259. __fscache_unregister_netfs(netfs);
  260. }
  261. /**
  262. * fscache_lookup_cache_tag - Look up a cache tag
  263. * @name: The name of the tag to search for
  264. *
  265. * Acquire a specific cache referral tag that can be used to select a specific
  266. * cache in which to cache an index.
  267. *
  268. * See Documentation/filesystems/caching/netfs-api.txt for a complete
  269. * description.
  270. */
  271. static inline
  272. struct fscache_cache_tag *fscache_lookup_cache_tag(const char *name)
  273. {
  274. if (fscache_available())
  275. return __fscache_lookup_cache_tag(name);
  276. else
  277. return NULL;
  278. }
  279. /**
  280. * fscache_release_cache_tag - Release a cache tag
  281. * @tag: The tag to release
  282. *
  283. * Release a reference to a cache referral tag previously looked up.
  284. *
  285. * See Documentation/filesystems/caching/netfs-api.txt for a complete
  286. * description.
  287. */
  288. static inline
  289. void fscache_release_cache_tag(struct fscache_cache_tag *tag)
  290. {
  291. if (fscache_available())
  292. __fscache_release_cache_tag(tag);
  293. }
  294. /**
  295. * fscache_acquire_cookie - Acquire a cookie to represent a cache object
  296. * @parent: The cookie that's to be the parent of this one
  297. * @def: A description of the cache object, including callback operations
  298. * @netfs_data: An arbitrary piece of data to be kept in the cookie to
  299. * represent the cache object to the netfs
  300. * @enable: Whether or not to enable a data cookie immediately
  301. *
  302. * This function is used to inform FS-Cache about part of an index hierarchy
  303. * that can be used to locate files. This is done by requesting a cookie for
  304. * each index in the path to the file.
  305. *
  306. * See Documentation/filesystems/caching/netfs-api.txt for a complete
  307. * description.
  308. */
  309. static inline
  310. struct fscache_cookie *fscache_acquire_cookie(
  311. struct fscache_cookie *parent,
  312. const struct fscache_cookie_def *def,
  313. void *netfs_data,
  314. bool enable)
  315. {
  316. if (fscache_cookie_valid(parent) && fscache_cookie_enabled(parent))
  317. return __fscache_acquire_cookie(parent, def, netfs_data,
  318. enable);
  319. else
  320. return NULL;
  321. }
  322. /**
  323. * fscache_relinquish_cookie - Return the cookie to the cache, maybe discarding
  324. * it
  325. * @cookie: The cookie being returned
  326. * @retire: True if the cache object the cookie represents is to be discarded
  327. *
  328. * This function returns a cookie to the cache, forcibly discarding the
  329. * associated cache object if retire is set to true.
  330. *
  331. * See Documentation/filesystems/caching/netfs-api.txt for a complete
  332. * description.
  333. */
  334. static inline
  335. void fscache_relinquish_cookie(struct fscache_cookie *cookie, bool retire)
  336. {
  337. if (fscache_cookie_valid(cookie))
  338. __fscache_relinquish_cookie(cookie, retire);
  339. }
  340. /**
  341. * fscache_check_consistency - Request that if the cache is updated
  342. * @cookie: The cookie representing the cache object
  343. *
  344. * Request an consistency check from fscache, which passes the request
  345. * to the backing cache.
  346. *
  347. * Returns 0 if consistent and -ESTALE if inconsistent. May also
  348. * return -ENOMEM and -ERESTARTSYS.
  349. */
  350. static inline
  351. int fscache_check_consistency(struct fscache_cookie *cookie)
  352. {
  353. if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
  354. return __fscache_check_consistency(cookie);
  355. else
  356. return 0;
  357. }
  358. /**
  359. * fscache_update_cookie - Request that a cache object be updated
  360. * @cookie: The cookie representing the cache object
  361. *
  362. * Request an update of the index data for the cache object associated with the
  363. * cookie.
  364. *
  365. * See Documentation/filesystems/caching/netfs-api.txt for a complete
  366. * description.
  367. */
  368. static inline
  369. void fscache_update_cookie(struct fscache_cookie *cookie)
  370. {
  371. if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
  372. __fscache_update_cookie(cookie);
  373. }
  374. /**
  375. * fscache_pin_cookie - Pin a data-storage cache object in its cache
  376. * @cookie: The cookie representing the cache object
  377. *
  378. * Permit data-storage cache objects to be pinned in the cache.
  379. *
  380. * See Documentation/filesystems/caching/netfs-api.txt for a complete
  381. * description.
  382. */
  383. static inline
  384. int fscache_pin_cookie(struct fscache_cookie *cookie)
  385. {
  386. return -ENOBUFS;
  387. }
  388. /**
  389. * fscache_pin_cookie - Unpin a data-storage cache object in its cache
  390. * @cookie: The cookie representing the cache object
  391. *
  392. * Permit data-storage cache objects to be unpinned from the cache.
  393. *
  394. * See Documentation/filesystems/caching/netfs-api.txt for a complete
  395. * description.
  396. */
  397. static inline
  398. void fscache_unpin_cookie(struct fscache_cookie *cookie)
  399. {
  400. }
  401. /**
  402. * fscache_attr_changed - Notify cache that an object's attributes changed
  403. * @cookie: The cookie representing the cache object
  404. *
  405. * Send a notification to the cache indicating that an object's attributes have
  406. * changed. This includes the data size. These attributes will be obtained
  407. * through the get_attr() cookie definition op.
  408. *
  409. * See Documentation/filesystems/caching/netfs-api.txt for a complete
  410. * description.
  411. */
  412. static inline
  413. int fscache_attr_changed(struct fscache_cookie *cookie)
  414. {
  415. if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
  416. return __fscache_attr_changed(cookie);
  417. else
  418. return -ENOBUFS;
  419. }
  420. /**
  421. * fscache_invalidate - Notify cache that an object needs invalidation
  422. * @cookie: The cookie representing the cache object
  423. *
  424. * Notify the cache that an object is needs to be invalidated and that it
  425. * should abort any retrievals or stores it is doing on the cache. The object
  426. * is then marked non-caching until such time as the invalidation is complete.
  427. *
  428. * This can be called with spinlocks held.
  429. *
  430. * See Documentation/filesystems/caching/netfs-api.txt for a complete
  431. * description.
  432. */
  433. static inline
  434. void fscache_invalidate(struct fscache_cookie *cookie)
  435. {
  436. if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
  437. __fscache_invalidate(cookie);
  438. }
  439. /**
  440. * fscache_wait_on_invalidate - Wait for invalidation to complete
  441. * @cookie: The cookie representing the cache object
  442. *
  443. * Wait for the invalidation of an object to complete.
  444. *
  445. * See Documentation/filesystems/caching/netfs-api.txt for a complete
  446. * description.
  447. */
  448. static inline
  449. void fscache_wait_on_invalidate(struct fscache_cookie *cookie)
  450. {
  451. if (fscache_cookie_valid(cookie))
  452. __fscache_wait_on_invalidate(cookie);
  453. }
  454. /**
  455. * fscache_reserve_space - Reserve data space for a cached object
  456. * @cookie: The cookie representing the cache object
  457. * @i_size: The amount of space to be reserved
  458. *
  459. * Reserve an amount of space in the cache for the cache object attached to a
  460. * cookie so that a write to that object within the space can always be
  461. * honoured.
  462. *
  463. * See Documentation/filesystems/caching/netfs-api.txt for a complete
  464. * description.
  465. */
  466. static inline
  467. int fscache_reserve_space(struct fscache_cookie *cookie, loff_t size)
  468. {
  469. return -ENOBUFS;
  470. }
  471. /**
  472. * fscache_read_or_alloc_page - Read a page from the cache or allocate a block
  473. * in which to store it
  474. * @cookie: The cookie representing the cache object
  475. * @page: The netfs page to fill if possible
  476. * @end_io_func: The callback to invoke when and if the page is filled
  477. * @context: An arbitrary piece of data to pass on to end_io_func()
  478. * @gfp: The conditions under which memory allocation should be made
  479. *
  480. * Read a page from the cache, or if that's not possible make a potential
  481. * one-block reservation in the cache into which the page may be stored once
  482. * fetched from the server.
  483. *
  484. * If the page is not backed by the cache object, or if it there's some reason
  485. * it can't be, -ENOBUFS will be returned and nothing more will be done for
  486. * that page.
  487. *
  488. * Else, if that page is backed by the cache, a read will be initiated directly
  489. * to the netfs's page and 0 will be returned by this function. The
  490. * end_io_func() callback will be invoked when the operation terminates on a
  491. * completion or failure. Note that the callback may be invoked before the
  492. * return.
  493. *
  494. * Else, if the page is unbacked, -ENODATA is returned and a block may have
  495. * been allocated in the cache.
  496. *
  497. * See Documentation/filesystems/caching/netfs-api.txt for a complete
  498. * description.
  499. */
  500. static inline
  501. int fscache_read_or_alloc_page(struct fscache_cookie *cookie,
  502. struct page *page,
  503. fscache_rw_complete_t end_io_func,
  504. void *context,
  505. gfp_t gfp)
  506. {
  507. if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
  508. return __fscache_read_or_alloc_page(cookie, page, end_io_func,
  509. context, gfp);
  510. else
  511. return -ENOBUFS;
  512. }
  513. /**
  514. * fscache_read_or_alloc_pages - Read pages from the cache and/or allocate
  515. * blocks in which to store them
  516. * @cookie: The cookie representing the cache object
  517. * @mapping: The netfs inode mapping to which the pages will be attached
  518. * @pages: A list of potential netfs pages to be filled
  519. * @nr_pages: Number of pages to be read and/or allocated
  520. * @end_io_func: The callback to invoke when and if each page is filled
  521. * @context: An arbitrary piece of data to pass on to end_io_func()
  522. * @gfp: The conditions under which memory allocation should be made
  523. *
  524. * Read a set of pages from the cache, or if that's not possible, attempt to
  525. * make a potential one-block reservation for each page in the cache into which
  526. * that page may be stored once fetched from the server.
  527. *
  528. * If some pages are not backed by the cache object, or if it there's some
  529. * reason they can't be, -ENOBUFS will be returned and nothing more will be
  530. * done for that pages.
  531. *
  532. * Else, if some of the pages are backed by the cache, a read will be initiated
  533. * directly to the netfs's page and 0 will be returned by this function. The
  534. * end_io_func() callback will be invoked when the operation terminates on a
  535. * completion or failure. Note that the callback may be invoked before the
  536. * return.
  537. *
  538. * Else, if a page is unbacked, -ENODATA is returned and a block may have
  539. * been allocated in the cache.
  540. *
  541. * Because the function may want to return all of -ENOBUFS, -ENODATA and 0 in
  542. * regard to different pages, the return values are prioritised in that order.
  543. * Any pages submitted for reading are removed from the pages list.
  544. *
  545. * See Documentation/filesystems/caching/netfs-api.txt for a complete
  546. * description.
  547. */
  548. static inline
  549. int fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
  550. struct address_space *mapping,
  551. struct list_head *pages,
  552. unsigned *nr_pages,
  553. fscache_rw_complete_t end_io_func,
  554. void *context,
  555. gfp_t gfp)
  556. {
  557. if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
  558. return __fscache_read_or_alloc_pages(cookie, mapping, pages,
  559. nr_pages, end_io_func,
  560. context, gfp);
  561. else
  562. return -ENOBUFS;
  563. }
  564. /**
  565. * fscache_alloc_page - Allocate a block in which to store a page
  566. * @cookie: The cookie representing the cache object
  567. * @page: The netfs page to allocate a page for
  568. * @gfp: The conditions under which memory allocation should be made
  569. *
  570. * Request Allocation a block in the cache in which to store a netfs page
  571. * without retrieving any contents from the cache.
  572. *
  573. * If the page is not backed by a file then -ENOBUFS will be returned and
  574. * nothing more will be done, and no reservation will be made.
  575. *
  576. * Else, a block will be allocated if one wasn't already, and 0 will be
  577. * returned
  578. *
  579. * See Documentation/filesystems/caching/netfs-api.txt for a complete
  580. * description.
  581. */
  582. static inline
  583. int fscache_alloc_page(struct fscache_cookie *cookie,
  584. struct page *page,
  585. gfp_t gfp)
  586. {
  587. if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
  588. return __fscache_alloc_page(cookie, page, gfp);
  589. else
  590. return -ENOBUFS;
  591. }
  592. /**
  593. * fscache_readpages_cancel - Cancel read/alloc on pages
  594. * @cookie: The cookie representing the inode's cache object.
  595. * @pages: The netfs pages that we canceled write on in readpages()
  596. *
  597. * Uncache/unreserve the pages reserved earlier in readpages() via
  598. * fscache_readpages_or_alloc() and similar. In most successful caches in
  599. * readpages() this doesn't do anything. In cases when the underlying netfs's
  600. * readahead failed we need to clean up the pagelist (unmark and uncache).
  601. *
  602. * This function may sleep as it may have to clean up disk state.
  603. */
  604. static inline
  605. void fscache_readpages_cancel(struct fscache_cookie *cookie,
  606. struct list_head *pages)
  607. {
  608. if (fscache_cookie_valid(cookie))
  609. __fscache_readpages_cancel(cookie, pages);
  610. }
  611. /**
  612. * fscache_write_page - Request storage of a page in the cache
  613. * @cookie: The cookie representing the cache object
  614. * @page: The netfs page to store
  615. * @gfp: The conditions under which memory allocation should be made
  616. *
  617. * Request the contents of the netfs page be written into the cache. This
  618. * request may be ignored if no cache block is currently allocated, in which
  619. * case it will return -ENOBUFS.
  620. *
  621. * If a cache block was already allocated, a write will be initiated and 0 will
  622. * be returned. The PG_fscache_write page bit is set immediately and will then
  623. * be cleared at the completion of the write to indicate the success or failure
  624. * of the operation. Note that the completion may happen before the return.
  625. *
  626. * See Documentation/filesystems/caching/netfs-api.txt for a complete
  627. * description.
  628. */
  629. static inline
  630. int fscache_write_page(struct fscache_cookie *cookie,
  631. struct page *page,
  632. gfp_t gfp)
  633. {
  634. if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
  635. return __fscache_write_page(cookie, page, gfp);
  636. else
  637. return -ENOBUFS;
  638. }
  639. /**
  640. * fscache_uncache_page - Indicate that caching is no longer required on a page
  641. * @cookie: The cookie representing the cache object
  642. * @page: The netfs page that was being cached.
  643. *
  644. * Tell the cache that we no longer want a page to be cached and that it should
  645. * remove any knowledge of the netfs page it may have.
  646. *
  647. * Note that this cannot cancel any outstanding I/O operations between this
  648. * page and the cache.
  649. *
  650. * See Documentation/filesystems/caching/netfs-api.txt for a complete
  651. * description.
  652. */
  653. static inline
  654. void fscache_uncache_page(struct fscache_cookie *cookie,
  655. struct page *page)
  656. {
  657. if (fscache_cookie_valid(cookie))
  658. __fscache_uncache_page(cookie, page);
  659. }
  660. /**
  661. * fscache_check_page_write - Ask if a page is being writing to the cache
  662. * @cookie: The cookie representing the cache object
  663. * @page: The netfs page that is being cached.
  664. *
  665. * Ask the cache if a page is being written to the cache.
  666. *
  667. * See Documentation/filesystems/caching/netfs-api.txt for a complete
  668. * description.
  669. */
  670. static inline
  671. bool fscache_check_page_write(struct fscache_cookie *cookie,
  672. struct page *page)
  673. {
  674. if (fscache_cookie_valid(cookie))
  675. return __fscache_check_page_write(cookie, page);
  676. return false;
  677. }
  678. /**
  679. * fscache_wait_on_page_write - Wait for a page to complete writing to the cache
  680. * @cookie: The cookie representing the cache object
  681. * @page: The netfs page that is being cached.
  682. *
  683. * Ask the cache to wake us up when a page is no longer being written to the
  684. * cache.
  685. *
  686. * See Documentation/filesystems/caching/netfs-api.txt for a complete
  687. * description.
  688. */
  689. static inline
  690. void fscache_wait_on_page_write(struct fscache_cookie *cookie,
  691. struct page *page)
  692. {
  693. if (fscache_cookie_valid(cookie))
  694. __fscache_wait_on_page_write(cookie, page);
  695. }
  696. /**
  697. * fscache_maybe_release_page - Consider releasing a page, cancelling a store
  698. * @cookie: The cookie representing the cache object
  699. * @page: The netfs page that is being cached.
  700. * @gfp: The gfp flags passed to releasepage()
  701. *
  702. * Consider releasing a page for the vmscan algorithm, on behalf of the netfs's
  703. * releasepage() call. A storage request on the page may cancelled if it is
  704. * not currently being processed.
  705. *
  706. * The function returns true if the page no longer has a storage request on it,
  707. * and false if a storage request is left in place. If true is returned, the
  708. * page will have been passed to fscache_uncache_page(). If false is returned
  709. * the page cannot be freed yet.
  710. */
  711. static inline
  712. bool fscache_maybe_release_page(struct fscache_cookie *cookie,
  713. struct page *page,
  714. gfp_t gfp)
  715. {
  716. if (fscache_cookie_valid(cookie) && PageFsCache(page))
  717. return __fscache_maybe_release_page(cookie, page, gfp);
  718. return true;
  719. }
  720. /**
  721. * fscache_uncache_all_inode_pages - Uncache all an inode's pages
  722. * @cookie: The cookie representing the inode's cache object.
  723. * @inode: The inode to uncache pages from.
  724. *
  725. * Uncache all the pages in an inode that are marked PG_fscache, assuming them
  726. * to be associated with the given cookie.
  727. *
  728. * This function may sleep. It will wait for pages that are being written out
  729. * and will wait whilst the PG_fscache mark is removed by the cache.
  730. */
  731. static inline
  732. void fscache_uncache_all_inode_pages(struct fscache_cookie *cookie,
  733. struct inode *inode)
  734. {
  735. if (fscache_cookie_valid(cookie))
  736. __fscache_uncache_all_inode_pages(cookie, inode);
  737. }
  738. /**
  739. * fscache_disable_cookie - Disable a cookie
  740. * @cookie: The cookie representing the cache object
  741. * @invalidate: Invalidate the backing object
  742. *
  743. * Disable a cookie from accepting further alloc, read, write, invalidate,
  744. * update or acquire operations. Outstanding operations can still be waited
  745. * upon and pages can still be uncached and the cookie relinquished.
  746. *
  747. * This will not return until all outstanding operations have completed.
  748. *
  749. * If @invalidate is set, then the backing object will be invalidated and
  750. * detached, otherwise it will just be detached.
  751. */
  752. static inline
  753. void fscache_disable_cookie(struct fscache_cookie *cookie, bool invalidate)
  754. {
  755. if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie))
  756. __fscache_disable_cookie(cookie, invalidate);
  757. }
  758. /**
  759. * fscache_enable_cookie - Reenable a cookie
  760. * @cookie: The cookie representing the cache object
  761. * @can_enable: A function to permit enablement once lock is held
  762. * @data: Data for can_enable()
  763. *
  764. * Reenable a previously disabled cookie, allowing it to accept further alloc,
  765. * read, write, invalidate, update or acquire operations. An attempt will be
  766. * made to immediately reattach the cookie to a backing object.
  767. *
  768. * The can_enable() function is called (if not NULL) once the enablement lock
  769. * is held to rule on whether enablement is still permitted to go ahead.
  770. */
  771. static inline
  772. void fscache_enable_cookie(struct fscache_cookie *cookie,
  773. bool (*can_enable)(void *data),
  774. void *data)
  775. {
  776. if (fscache_cookie_valid(cookie) && !fscache_cookie_enabled(cookie))
  777. __fscache_enable_cookie(cookie, can_enable, data);
  778. }
  779. #endif /* _LINUX_FSCACHE_H */