fscache.c 14 KB

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