fscache.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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, true);
  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, int ulen)
  60. {
  61. struct nfs_fscache_key *key, *xkey;
  62. struct nfs_server *nfss = NFS_SB(sb);
  63. struct rb_node **p, *parent;
  64. int diff;
  65. if (!uniq) {
  66. uniq = "";
  67. ulen = 1;
  68. }
  69. key = kzalloc(sizeof(*key) + ulen, GFP_KERNEL);
  70. if (!key)
  71. return;
  72. key->nfs_client = nfss->nfs_client;
  73. key->key.super.s_flags = sb->s_flags & NFS_MS_MASK;
  74. key->key.nfs_server.flags = nfss->flags;
  75. key->key.nfs_server.rsize = nfss->rsize;
  76. key->key.nfs_server.wsize = nfss->wsize;
  77. key->key.nfs_server.acregmin = nfss->acregmin;
  78. key->key.nfs_server.acregmax = nfss->acregmax;
  79. key->key.nfs_server.acdirmin = nfss->acdirmin;
  80. key->key.nfs_server.acdirmax = nfss->acdirmax;
  81. key->key.nfs_server.fsid = nfss->fsid;
  82. key->key.rpc_auth.au_flavor = nfss->client->cl_auth->au_flavor;
  83. key->key.uniq_len = ulen;
  84. memcpy(key->key.uniquifier, uniq, ulen);
  85. spin_lock(&nfs_fscache_keys_lock);
  86. p = &nfs_fscache_keys.rb_node;
  87. parent = NULL;
  88. while (*p) {
  89. parent = *p;
  90. xkey = rb_entry(parent, struct nfs_fscache_key, node);
  91. if (key->nfs_client < xkey->nfs_client)
  92. goto go_left;
  93. if (key->nfs_client > xkey->nfs_client)
  94. goto go_right;
  95. diff = memcmp(&key->key, &xkey->key, sizeof(key->key));
  96. if (diff < 0)
  97. goto go_left;
  98. if (diff > 0)
  99. goto go_right;
  100. if (key->key.uniq_len == 0)
  101. goto non_unique;
  102. diff = memcmp(key->key.uniquifier,
  103. xkey->key.uniquifier,
  104. key->key.uniq_len);
  105. if (diff < 0)
  106. goto go_left;
  107. if (diff > 0)
  108. goto go_right;
  109. goto non_unique;
  110. go_left:
  111. p = &(*p)->rb_left;
  112. continue;
  113. go_right:
  114. p = &(*p)->rb_right;
  115. }
  116. rb_link_node(&key->node, parent, p);
  117. rb_insert_color(&key->node, &nfs_fscache_keys);
  118. spin_unlock(&nfs_fscache_keys_lock);
  119. nfss->fscache_key = key;
  120. /* create a cache index for looking up filehandles */
  121. nfss->fscache = fscache_acquire_cookie(nfss->nfs_client->fscache,
  122. &nfs_fscache_super_index_def,
  123. nfss, true);
  124. dfprintk(FSCACHE, "NFS: get superblock cookie (0x%p/0x%p)\n",
  125. nfss, nfss->fscache);
  126. return;
  127. non_unique:
  128. spin_unlock(&nfs_fscache_keys_lock);
  129. kfree(key);
  130. nfss->fscache_key = NULL;
  131. nfss->fscache = NULL;
  132. printk(KERN_WARNING "NFS:"
  133. " Cache request denied due to non-unique superblock keys\n");
  134. }
  135. /*
  136. * release a per-superblock cookie
  137. */
  138. void nfs_fscache_release_super_cookie(struct super_block *sb)
  139. {
  140. struct nfs_server *nfss = NFS_SB(sb);
  141. dfprintk(FSCACHE, "NFS: releasing superblock cookie (0x%p/0x%p)\n",
  142. nfss, nfss->fscache);
  143. fscache_relinquish_cookie(nfss->fscache, 0);
  144. nfss->fscache = NULL;
  145. if (nfss->fscache_key) {
  146. spin_lock(&nfs_fscache_keys_lock);
  147. rb_erase(&nfss->fscache_key->node, &nfs_fscache_keys);
  148. spin_unlock(&nfs_fscache_keys_lock);
  149. kfree(nfss->fscache_key);
  150. nfss->fscache_key = NULL;
  151. }
  152. }
  153. /*
  154. * Initialise the per-inode cache cookie pointer for an NFS inode.
  155. */
  156. void nfs_fscache_init_inode(struct inode *inode)
  157. {
  158. struct nfs_inode *nfsi = NFS_I(inode);
  159. nfsi->fscache = NULL;
  160. if (!S_ISREG(inode->i_mode))
  161. return;
  162. nfsi->fscache = fscache_acquire_cookie(NFS_SB(inode->i_sb)->fscache,
  163. &nfs_fscache_inode_object_def,
  164. nfsi, false);
  165. }
  166. /*
  167. * Release a per-inode cookie.
  168. */
  169. void nfs_fscache_clear_inode(struct inode *inode)
  170. {
  171. struct nfs_inode *nfsi = NFS_I(inode);
  172. struct fscache_cookie *cookie = nfs_i_fscache(inode);
  173. dfprintk(FSCACHE, "NFS: clear cookie (0x%p/0x%p)\n", nfsi, cookie);
  174. fscache_relinquish_cookie(cookie, false);
  175. nfsi->fscache = NULL;
  176. }
  177. static bool nfs_fscache_can_enable(void *data)
  178. {
  179. struct inode *inode = data;
  180. return !inode_is_open_for_write(inode);
  181. }
  182. /*
  183. * Enable or disable caching for a file that is being opened as appropriate.
  184. * The cookie is allocated when the inode is initialised, but is not enabled at
  185. * that time. Enablement is deferred to file-open time to avoid stat() and
  186. * access() thrashing the cache.
  187. *
  188. * For now, with NFS, only regular files that are open read-only will be able
  189. * to use the cache.
  190. *
  191. * We enable the cache for an inode if we open it read-only and it isn't
  192. * currently open for writing. We disable the cache if the inode is open
  193. * write-only.
  194. *
  195. * The caller uses the file struct to pin i_writecount on the inode before
  196. * calling us when a file is opened for writing, so we can make use of that.
  197. *
  198. * Note that this may be invoked multiple times in parallel by parallel
  199. * nfs_open() functions.
  200. */
  201. void nfs_fscache_open_file(struct inode *inode, struct file *filp)
  202. {
  203. struct nfs_inode *nfsi = NFS_I(inode);
  204. struct fscache_cookie *cookie = nfs_i_fscache(inode);
  205. if (!fscache_cookie_valid(cookie))
  206. return;
  207. if (inode_is_open_for_write(inode)) {
  208. dfprintk(FSCACHE, "NFS: nfsi 0x%p disabling cache\n", nfsi);
  209. clear_bit(NFS_INO_FSCACHE, &nfsi->flags);
  210. fscache_disable_cookie(cookie, true);
  211. fscache_uncache_all_inode_pages(cookie, inode);
  212. } else {
  213. dfprintk(FSCACHE, "NFS: nfsi 0x%p enabling cache\n", nfsi);
  214. fscache_enable_cookie(cookie, nfs_fscache_can_enable, inode);
  215. if (fscache_cookie_enabled(cookie))
  216. set_bit(NFS_INO_FSCACHE, &NFS_I(inode)->flags);
  217. }
  218. }
  219. EXPORT_SYMBOL_GPL(nfs_fscache_open_file);
  220. /*
  221. * Release the caching state associated with a page, if the page isn't busy
  222. * interacting with the cache.
  223. * - Returns true (can release page) or false (page busy).
  224. */
  225. int nfs_fscache_release_page(struct page *page, gfp_t gfp)
  226. {
  227. if (PageFsCache(page)) {
  228. struct fscache_cookie *cookie = nfs_i_fscache(page->mapping->host);
  229. BUG_ON(!cookie);
  230. dfprintk(FSCACHE, "NFS: fscache releasepage (0x%p/0x%p/0x%p)\n",
  231. cookie, page, NFS_I(page->mapping->host));
  232. if (!fscache_maybe_release_page(cookie, page, gfp))
  233. return 0;
  234. nfs_inc_fscache_stats(page->mapping->host,
  235. NFSIOS_FSCACHE_PAGES_UNCACHED);
  236. }
  237. return 1;
  238. }
  239. /*
  240. * Release the caching state associated with a page if undergoing complete page
  241. * invalidation.
  242. */
  243. void __nfs_fscache_invalidate_page(struct page *page, struct inode *inode)
  244. {
  245. struct fscache_cookie *cookie = nfs_i_fscache(inode);
  246. BUG_ON(!cookie);
  247. dfprintk(FSCACHE, "NFS: fscache invalidatepage (0x%p/0x%p/0x%p)\n",
  248. cookie, page, NFS_I(inode));
  249. fscache_wait_on_page_write(cookie, page);
  250. BUG_ON(!PageLocked(page));
  251. fscache_uncache_page(cookie, page);
  252. nfs_inc_fscache_stats(page->mapping->host,
  253. NFSIOS_FSCACHE_PAGES_UNCACHED);
  254. }
  255. /*
  256. * Handle completion of a page being read from the cache.
  257. * - Called in process (keventd) context.
  258. */
  259. static void nfs_readpage_from_fscache_complete(struct page *page,
  260. void *context,
  261. int error)
  262. {
  263. dfprintk(FSCACHE,
  264. "NFS: readpage_from_fscache_complete (0x%p/0x%p/%d)\n",
  265. page, context, error);
  266. /* if the read completes with an error, we just unlock the page and let
  267. * the VM reissue the readpage */
  268. if (!error) {
  269. SetPageUptodate(page);
  270. unlock_page(page);
  271. } else {
  272. error = nfs_readpage_async(context, page->mapping->host, page);
  273. if (error)
  274. unlock_page(page);
  275. }
  276. }
  277. /*
  278. * Retrieve a page from fscache
  279. */
  280. int __nfs_readpage_from_fscache(struct nfs_open_context *ctx,
  281. struct inode *inode, struct page *page)
  282. {
  283. int ret;
  284. dfprintk(FSCACHE,
  285. "NFS: readpage_from_fscache(fsc:%p/p:%p(i:%lx f:%lx)/0x%p)\n",
  286. nfs_i_fscache(inode), page, page->index, page->flags, inode);
  287. ret = fscache_read_or_alloc_page(nfs_i_fscache(inode),
  288. page,
  289. nfs_readpage_from_fscache_complete,
  290. ctx,
  291. GFP_KERNEL);
  292. switch (ret) {
  293. case 0: /* read BIO submitted (page in fscache) */
  294. dfprintk(FSCACHE,
  295. "NFS: readpage_from_fscache: BIO submitted\n");
  296. nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_OK);
  297. return ret;
  298. case -ENOBUFS: /* inode not in cache */
  299. case -ENODATA: /* page not in cache */
  300. nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_FAIL);
  301. dfprintk(FSCACHE,
  302. "NFS: readpage_from_fscache %d\n", ret);
  303. return 1;
  304. default:
  305. dfprintk(FSCACHE, "NFS: readpage_from_fscache %d\n", ret);
  306. nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_FAIL);
  307. }
  308. return ret;
  309. }
  310. /*
  311. * Retrieve a set of pages from fscache
  312. */
  313. int __nfs_readpages_from_fscache(struct nfs_open_context *ctx,
  314. struct inode *inode,
  315. struct address_space *mapping,
  316. struct list_head *pages,
  317. unsigned *nr_pages)
  318. {
  319. unsigned npages = *nr_pages;
  320. int ret;
  321. dfprintk(FSCACHE, "NFS: nfs_getpages_from_fscache (0x%p/%u/0x%p)\n",
  322. nfs_i_fscache(inode), npages, inode);
  323. ret = fscache_read_or_alloc_pages(nfs_i_fscache(inode),
  324. mapping, pages, nr_pages,
  325. nfs_readpage_from_fscache_complete,
  326. ctx,
  327. mapping_gfp_mask(mapping));
  328. if (*nr_pages < npages)
  329. nfs_add_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_OK,
  330. npages);
  331. if (*nr_pages > 0)
  332. nfs_add_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_FAIL,
  333. *nr_pages);
  334. switch (ret) {
  335. case 0: /* read submitted to the cache for all pages */
  336. BUG_ON(!list_empty(pages));
  337. BUG_ON(*nr_pages != 0);
  338. dfprintk(FSCACHE,
  339. "NFS: nfs_getpages_from_fscache: submitted\n");
  340. return ret;
  341. case -ENOBUFS: /* some pages aren't cached and can't be */
  342. case -ENODATA: /* some pages aren't cached */
  343. dfprintk(FSCACHE,
  344. "NFS: nfs_getpages_from_fscache: no page: %d\n", ret);
  345. return 1;
  346. default:
  347. dfprintk(FSCACHE,
  348. "NFS: nfs_getpages_from_fscache: ret %d\n", ret);
  349. }
  350. return ret;
  351. }
  352. /*
  353. * Store a newly fetched page in fscache
  354. * - PG_fscache must be set on the page
  355. */
  356. void __nfs_readpage_to_fscache(struct inode *inode, struct page *page, int sync)
  357. {
  358. int ret;
  359. dfprintk(FSCACHE,
  360. "NFS: readpage_to_fscache(fsc:%p/p:%p(i:%lx f:%lx)/%d)\n",
  361. nfs_i_fscache(inode), page, page->index, page->flags, sync);
  362. ret = fscache_write_page(nfs_i_fscache(inode), page, GFP_KERNEL);
  363. dfprintk(FSCACHE,
  364. "NFS: readpage_to_fscache: p:%p(i:%lu f:%lx) ret %d\n",
  365. page, page->index, page->flags, ret);
  366. if (ret != 0) {
  367. fscache_uncache_page(nfs_i_fscache(inode), page);
  368. nfs_inc_fscache_stats(inode,
  369. NFSIOS_FSCACHE_PAGES_WRITTEN_FAIL);
  370. nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_UNCACHED);
  371. } else {
  372. nfs_inc_fscache_stats(inode,
  373. NFSIOS_FSCACHE_PAGES_WRITTEN_OK);
  374. }
  375. }