fscache.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /* NFS filesystem cache interface
  2. *
  3. * Copyright (C) 2008 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 Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/mm.h>
  15. #include <linux/nfs_fs.h>
  16. #include <linux/nfs_fs_sb.h>
  17. #include <linux/in6.h>
  18. #include <linux/seq_file.h>
  19. #include "internal.h"
  20. #include "iostat.h"
  21. #include "fscache.h"
  22. #define NFSDBG_FACILITY NFSDBG_FSCACHE
  23. static struct rb_root nfs_fscache_keys = RB_ROOT;
  24. static DEFINE_SPINLOCK(nfs_fscache_keys_lock);
  25. /*
  26. * Get the per-client index cookie for an NFS client if the appropriate mount
  27. * flag was set
  28. * - We always try and get an index cookie for the client, but get filehandle
  29. * cookies on a per-superblock basis, depending on the mount flags
  30. */
  31. void nfs_fscache_get_client_cookie(struct nfs_client *clp)
  32. {
  33. /* create a cache index for looking up filehandles */
  34. clp->fscache = fscache_acquire_cookie(nfs_fscache_netfs.primary_index,
  35. &nfs_fscache_server_index_def,
  36. clp);
  37. dfprintk(FSCACHE, "NFS: get client cookie (0x%p/0x%p)\n",
  38. clp, clp->fscache);
  39. }
  40. /*
  41. * Dispose of a per-client cookie
  42. */
  43. void nfs_fscache_release_client_cookie(struct nfs_client *clp)
  44. {
  45. dfprintk(FSCACHE, "NFS: releasing client cookie (0x%p/0x%p)\n",
  46. clp, clp->fscache);
  47. fscache_relinquish_cookie(clp->fscache, 0);
  48. clp->fscache = NULL;
  49. }
  50. /*
  51. * Get the cache cookie for an NFS superblock. We have to handle
  52. * uniquification here because the cache doesn't do it for us.
  53. */
  54. void nfs_fscache_get_super_cookie(struct super_block *sb,
  55. struct nfs_parsed_mount_data *data)
  56. {
  57. struct nfs_fscache_key *key, *xkey;
  58. struct nfs_server *nfss = NFS_SB(sb);
  59. struct rb_node **p, *parent;
  60. const char *uniq = data->fscache_uniq ?: "";
  61. int diff, ulen;
  62. ulen = strlen(uniq);
  63. key = kzalloc(sizeof(*key) + ulen, GFP_KERNEL);
  64. if (!key)
  65. return;
  66. key->nfs_client = nfss->nfs_client;
  67. key->key.super.s_flags = sb->s_flags & NFS_MS_MASK;
  68. key->key.nfs_server.flags = nfss->flags;
  69. key->key.nfs_server.rsize = nfss->rsize;
  70. key->key.nfs_server.wsize = nfss->wsize;
  71. key->key.nfs_server.acregmin = nfss->acregmin;
  72. key->key.nfs_server.acregmax = nfss->acregmax;
  73. key->key.nfs_server.acdirmin = nfss->acdirmin;
  74. key->key.nfs_server.acdirmax = nfss->acdirmax;
  75. key->key.nfs_server.fsid = nfss->fsid;
  76. key->key.rpc_auth.au_flavor = nfss->client->cl_auth->au_flavor;
  77. key->key.uniq_len = ulen;
  78. memcpy(key->key.uniquifier, uniq, ulen);
  79. spin_lock(&nfs_fscache_keys_lock);
  80. p = &nfs_fscache_keys.rb_node;
  81. parent = NULL;
  82. while (*p) {
  83. parent = *p;
  84. xkey = rb_entry(parent, struct nfs_fscache_key, node);
  85. if (key->nfs_client < xkey->nfs_client)
  86. goto go_left;
  87. if (key->nfs_client > xkey->nfs_client)
  88. goto go_right;
  89. diff = memcmp(&key->key, &xkey->key, sizeof(key->key));
  90. if (diff < 0)
  91. goto go_left;
  92. if (diff > 0)
  93. goto go_right;
  94. if (key->key.uniq_len == 0)
  95. goto non_unique;
  96. diff = memcmp(key->key.uniquifier,
  97. xkey->key.uniquifier,
  98. key->key.uniq_len);
  99. if (diff < 0)
  100. goto go_left;
  101. if (diff > 0)
  102. goto go_right;
  103. goto non_unique;
  104. go_left:
  105. p = &(*p)->rb_left;
  106. continue;
  107. go_right:
  108. p = &(*p)->rb_right;
  109. }
  110. rb_link_node(&key->node, parent, p);
  111. rb_insert_color(&key->node, &nfs_fscache_keys);
  112. spin_unlock(&nfs_fscache_keys_lock);
  113. nfss->fscache_key = key;
  114. /* create a cache index for looking up filehandles */
  115. nfss->fscache = fscache_acquire_cookie(nfss->nfs_client->fscache,
  116. &nfs_fscache_super_index_def,
  117. nfss);
  118. dfprintk(FSCACHE, "NFS: get superblock cookie (0x%p/0x%p)\n",
  119. nfss, nfss->fscache);
  120. return;
  121. non_unique:
  122. spin_unlock(&nfs_fscache_keys_lock);
  123. kfree(key);
  124. nfss->fscache_key = NULL;
  125. nfss->fscache = NULL;
  126. printk(KERN_WARNING "NFS:"
  127. " Cache request denied due to non-unique superblock keys\n");
  128. }
  129. /*
  130. * release a per-superblock cookie
  131. */
  132. void nfs_fscache_release_super_cookie(struct super_block *sb)
  133. {
  134. struct nfs_server *nfss = NFS_SB(sb);
  135. dfprintk(FSCACHE, "NFS: releasing superblock cookie (0x%p/0x%p)\n",
  136. nfss, nfss->fscache);
  137. fscache_relinquish_cookie(nfss->fscache, 0);
  138. nfss->fscache = NULL;
  139. if (nfss->fscache_key) {
  140. spin_lock(&nfs_fscache_keys_lock);
  141. rb_erase(&nfss->fscache_key->node, &nfs_fscache_keys);
  142. spin_unlock(&nfs_fscache_keys_lock);
  143. kfree(nfss->fscache_key);
  144. nfss->fscache_key = NULL;
  145. }
  146. }
  147. /*
  148. * Initialise the per-inode cache cookie pointer for an NFS inode.
  149. */
  150. void nfs_fscache_init_inode_cookie(struct inode *inode)
  151. {
  152. NFS_I(inode)->fscache = NULL;
  153. if (S_ISREG(inode->i_mode))
  154. set_bit(NFS_INO_FSCACHE, &NFS_I(inode)->flags);
  155. }
  156. /*
  157. * Get the per-inode cache cookie for an NFS inode.
  158. */
  159. static void nfs_fscache_enable_inode_cookie(struct inode *inode)
  160. {
  161. struct super_block *sb = inode->i_sb;
  162. struct nfs_inode *nfsi = NFS_I(inode);
  163. if (nfsi->fscache || !NFS_FSCACHE(inode))
  164. return;
  165. if ((NFS_SB(sb)->options & NFS_OPTION_FSCACHE)) {
  166. nfsi->fscache = fscache_acquire_cookie(
  167. NFS_SB(sb)->fscache,
  168. &nfs_fscache_inode_object_def,
  169. nfsi);
  170. dfprintk(FSCACHE, "NFS: get FH cookie (0x%p/0x%p/0x%p)\n",
  171. sb, nfsi, nfsi->fscache);
  172. }
  173. }
  174. /*
  175. * Release a per-inode cookie.
  176. */
  177. void nfs_fscache_release_inode_cookie(struct inode *inode)
  178. {
  179. struct nfs_inode *nfsi = NFS_I(inode);
  180. dfprintk(FSCACHE, "NFS: clear cookie (0x%p/0x%p)\n",
  181. nfsi, nfsi->fscache);
  182. fscache_relinquish_cookie(nfsi->fscache, 0);
  183. nfsi->fscache = NULL;
  184. }
  185. /*
  186. * Retire a per-inode cookie, destroying the data attached to it.
  187. */
  188. void nfs_fscache_zap_inode_cookie(struct inode *inode)
  189. {
  190. struct nfs_inode *nfsi = NFS_I(inode);
  191. dfprintk(FSCACHE, "NFS: zapping cookie (0x%p/0x%p)\n",
  192. nfsi, nfsi->fscache);
  193. fscache_relinquish_cookie(nfsi->fscache, 1);
  194. nfsi->fscache = NULL;
  195. }
  196. /*
  197. * Turn off the cache with regard to a per-inode cookie if opened for writing,
  198. * invalidating all the pages in the page cache relating to the associated
  199. * inode to clear the per-page caching.
  200. */
  201. static void nfs_fscache_disable_inode_cookie(struct inode *inode)
  202. {
  203. clear_bit(NFS_INO_FSCACHE, &NFS_I(inode)->flags);
  204. if (NFS_I(inode)->fscache) {
  205. dfprintk(FSCACHE,
  206. "NFS: nfsi 0x%p turning cache off\n", NFS_I(inode));
  207. /* Need to invalidate any mapped pages that were read in before
  208. * turning off the cache.
  209. */
  210. if (inode->i_mapping && inode->i_mapping->nrpages)
  211. invalidate_inode_pages2(inode->i_mapping);
  212. nfs_fscache_zap_inode_cookie(inode);
  213. }
  214. }
  215. /*
  216. * wait_on_bit() sleep function for uninterruptible waiting
  217. */
  218. static int nfs_fscache_wait_bit(void *flags)
  219. {
  220. schedule();
  221. return 0;
  222. }
  223. /*
  224. * Lock against someone else trying to also acquire or relinquish a cookie
  225. */
  226. static inline void nfs_fscache_inode_lock(struct inode *inode)
  227. {
  228. struct nfs_inode *nfsi = NFS_I(inode);
  229. while (test_and_set_bit(NFS_INO_FSCACHE_LOCK, &nfsi->flags))
  230. wait_on_bit(&nfsi->flags, NFS_INO_FSCACHE_LOCK,
  231. nfs_fscache_wait_bit, TASK_UNINTERRUPTIBLE);
  232. }
  233. /*
  234. * Unlock cookie management lock
  235. */
  236. static inline void nfs_fscache_inode_unlock(struct inode *inode)
  237. {
  238. struct nfs_inode *nfsi = NFS_I(inode);
  239. smp_mb__before_clear_bit();
  240. clear_bit(NFS_INO_FSCACHE_LOCK, &nfsi->flags);
  241. smp_mb__after_clear_bit();
  242. wake_up_bit(&nfsi->flags, NFS_INO_FSCACHE_LOCK);
  243. }
  244. /*
  245. * Decide if we should enable or disable local caching for this inode.
  246. * - For now, with NFS, only regular files that are open read-only will be able
  247. * to use the cache.
  248. * - May be invoked multiple times in parallel by parallel nfs_open() functions.
  249. */
  250. void nfs_fscache_set_inode_cookie(struct inode *inode, struct file *filp)
  251. {
  252. if (NFS_FSCACHE(inode)) {
  253. nfs_fscache_inode_lock(inode);
  254. if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
  255. nfs_fscache_disable_inode_cookie(inode);
  256. else
  257. nfs_fscache_enable_inode_cookie(inode);
  258. nfs_fscache_inode_unlock(inode);
  259. }
  260. }
  261. /*
  262. * Replace a per-inode cookie due to revalidation detecting a file having
  263. * changed on the server.
  264. */
  265. void nfs_fscache_reset_inode_cookie(struct inode *inode)
  266. {
  267. struct nfs_inode *nfsi = NFS_I(inode);
  268. struct nfs_server *nfss = NFS_SERVER(inode);
  269. struct fscache_cookie *old = nfsi->fscache;
  270. nfs_fscache_inode_lock(inode);
  271. if (nfsi->fscache) {
  272. /* retire the current fscache cache and get a new one */
  273. fscache_relinquish_cookie(nfsi->fscache, 1);
  274. nfsi->fscache = fscache_acquire_cookie(
  275. nfss->nfs_client->fscache,
  276. &nfs_fscache_inode_object_def,
  277. nfsi);
  278. dfprintk(FSCACHE,
  279. "NFS: revalidation new cookie (0x%p/0x%p/0x%p/0x%p)\n",
  280. nfss, nfsi, old, nfsi->fscache);
  281. }
  282. nfs_fscache_inode_unlock(inode);
  283. }
  284. /*
  285. * Release the caching state associated with a page, if the page isn't busy
  286. * interacting with the cache.
  287. * - Returns true (can release page) or false (page busy).
  288. */
  289. int nfs_fscache_release_page(struct page *page, gfp_t gfp)
  290. {
  291. struct nfs_inode *nfsi = NFS_I(page->mapping->host);
  292. struct fscache_cookie *cookie = nfsi->fscache;
  293. BUG_ON(!cookie);
  294. if (fscache_check_page_write(cookie, page)) {
  295. if (!(gfp & __GFP_WAIT))
  296. return 0;
  297. fscache_wait_on_page_write(cookie, page);
  298. }
  299. if (PageFsCache(page)) {
  300. dfprintk(FSCACHE, "NFS: fscache releasepage (0x%p/0x%p/0x%p)\n",
  301. cookie, page, nfsi);
  302. fscache_uncache_page(cookie, page);
  303. nfs_add_fscache_stats(page->mapping->host,
  304. NFSIOS_FSCACHE_PAGES_UNCACHED, 1);
  305. }
  306. return 1;
  307. }
  308. /*
  309. * Release the caching state associated with a page if undergoing complete page
  310. * invalidation.
  311. */
  312. void __nfs_fscache_invalidate_page(struct page *page, struct inode *inode)
  313. {
  314. struct nfs_inode *nfsi = NFS_I(inode);
  315. struct fscache_cookie *cookie = nfsi->fscache;
  316. BUG_ON(!cookie);
  317. dfprintk(FSCACHE, "NFS: fscache invalidatepage (0x%p/0x%p/0x%p)\n",
  318. cookie, page, nfsi);
  319. fscache_wait_on_page_write(cookie, page);
  320. BUG_ON(!PageLocked(page));
  321. fscache_uncache_page(cookie, page);
  322. nfs_add_fscache_stats(page->mapping->host,
  323. NFSIOS_FSCACHE_PAGES_UNCACHED, 1);
  324. }
  325. /*
  326. * Handle completion of a page being read from the cache.
  327. * - Called in process (keventd) context.
  328. */
  329. static void nfs_readpage_from_fscache_complete(struct page *page,
  330. void *context,
  331. int error)
  332. {
  333. dfprintk(FSCACHE,
  334. "NFS: readpage_from_fscache_complete (0x%p/0x%p/%d)\n",
  335. page, context, error);
  336. /* if the read completes with an error, we just unlock the page and let
  337. * the VM reissue the readpage */
  338. if (!error) {
  339. SetPageUptodate(page);
  340. unlock_page(page);
  341. } else {
  342. error = nfs_readpage_async(context, page->mapping->host, page);
  343. if (error)
  344. unlock_page(page);
  345. }
  346. }
  347. /*
  348. * Retrieve a page from fscache
  349. */
  350. int __nfs_readpage_from_fscache(struct nfs_open_context *ctx,
  351. struct inode *inode, struct page *page)
  352. {
  353. int ret;
  354. dfprintk(FSCACHE,
  355. "NFS: readpage_from_fscache(fsc:%p/p:%p(i:%lx f:%lx)/0x%p)\n",
  356. NFS_I(inode)->fscache, page, page->index, page->flags, inode);
  357. ret = fscache_read_or_alloc_page(NFS_I(inode)->fscache,
  358. page,
  359. nfs_readpage_from_fscache_complete,
  360. ctx,
  361. GFP_KERNEL);
  362. switch (ret) {
  363. case 0: /* read BIO submitted (page in fscache) */
  364. dfprintk(FSCACHE,
  365. "NFS: readpage_from_fscache: BIO submitted\n");
  366. nfs_add_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_OK, 1);
  367. return ret;
  368. case -ENOBUFS: /* inode not in cache */
  369. case -ENODATA: /* page not in cache */
  370. nfs_add_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_FAIL, 1);
  371. dfprintk(FSCACHE,
  372. "NFS: readpage_from_fscache %d\n", ret);
  373. return 1;
  374. default:
  375. dfprintk(FSCACHE, "NFS: readpage_from_fscache %d\n", ret);
  376. nfs_add_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_FAIL, 1);
  377. }
  378. return ret;
  379. }
  380. /*
  381. * Retrieve a set of pages from fscache
  382. */
  383. int __nfs_readpages_from_fscache(struct nfs_open_context *ctx,
  384. struct inode *inode,
  385. struct address_space *mapping,
  386. struct list_head *pages,
  387. unsigned *nr_pages)
  388. {
  389. int ret, npages = *nr_pages;
  390. dfprintk(FSCACHE, "NFS: nfs_getpages_from_fscache (0x%p/%u/0x%p)\n",
  391. NFS_I(inode)->fscache, npages, inode);
  392. ret = fscache_read_or_alloc_pages(NFS_I(inode)->fscache,
  393. mapping, pages, nr_pages,
  394. nfs_readpage_from_fscache_complete,
  395. ctx,
  396. mapping_gfp_mask(mapping));
  397. if (*nr_pages < npages)
  398. nfs_add_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_OK,
  399. npages);
  400. if (*nr_pages > 0)
  401. nfs_add_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_FAIL,
  402. *nr_pages);
  403. switch (ret) {
  404. case 0: /* read submitted to the cache for all pages */
  405. BUG_ON(!list_empty(pages));
  406. BUG_ON(*nr_pages != 0);
  407. dfprintk(FSCACHE,
  408. "NFS: nfs_getpages_from_fscache: submitted\n");
  409. return ret;
  410. case -ENOBUFS: /* some pages aren't cached and can't be */
  411. case -ENODATA: /* some pages aren't cached */
  412. dfprintk(FSCACHE,
  413. "NFS: nfs_getpages_from_fscache: no page: %d\n", ret);
  414. return 1;
  415. default:
  416. dfprintk(FSCACHE,
  417. "NFS: nfs_getpages_from_fscache: ret %d\n", ret);
  418. }
  419. return ret;
  420. }
  421. /*
  422. * Store a newly fetched page in fscache
  423. * - PG_fscache must be set on the page
  424. */
  425. void __nfs_readpage_to_fscache(struct inode *inode, struct page *page, int sync)
  426. {
  427. int ret;
  428. dfprintk(FSCACHE,
  429. "NFS: readpage_to_fscache(fsc:%p/p:%p(i:%lx f:%lx)/%d)\n",
  430. NFS_I(inode)->fscache, page, page->index, page->flags, sync);
  431. ret = fscache_write_page(NFS_I(inode)->fscache, page, GFP_KERNEL);
  432. dfprintk(FSCACHE,
  433. "NFS: readpage_to_fscache: p:%p(i:%lu f:%lx) ret %d\n",
  434. page, page->index, page->flags, ret);
  435. if (ret != 0) {
  436. fscache_uncache_page(NFS_I(inode)->fscache, page);
  437. nfs_add_fscache_stats(inode,
  438. NFSIOS_FSCACHE_PAGES_WRITTEN_FAIL, 1);
  439. nfs_add_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_UNCACHED, 1);
  440. } else {
  441. nfs_add_fscache_stats(inode,
  442. NFSIOS_FSCACHE_PAGES_WRITTEN_OK, 1);
  443. }
  444. }