fscache.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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 <linux/iversion.h>
  21. #include "internal.h"
  22. #include "iostat.h"
  23. #include "fscache.h"
  24. #define NFSDBG_FACILITY NFSDBG_FSCACHE
  25. static struct rb_root nfs_fscache_keys = RB_ROOT;
  26. static DEFINE_SPINLOCK(nfs_fscache_keys_lock);
  27. /*
  28. * Layout of the key for an NFS server cache object.
  29. */
  30. struct nfs_server_key {
  31. struct {
  32. uint16_t nfsversion; /* NFS protocol version */
  33. uint16_t family; /* address family */
  34. __be16 port; /* IP port */
  35. } hdr;
  36. union {
  37. struct in_addr ipv4_addr; /* IPv4 address */
  38. struct in6_addr ipv6_addr; /* IPv6 address */
  39. };
  40. } __packed;
  41. /*
  42. * Get the per-client index cookie for an NFS client if the appropriate mount
  43. * flag was set
  44. * - We always try and get an index cookie for the client, but get filehandle
  45. * cookies on a per-superblock basis, depending on the mount flags
  46. */
  47. void nfs_fscache_get_client_cookie(struct nfs_client *clp)
  48. {
  49. const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &clp->cl_addr;
  50. const struct sockaddr_in *sin = (struct sockaddr_in *) &clp->cl_addr;
  51. struct nfs_server_key key;
  52. uint16_t len = sizeof(key.hdr);
  53. memset(&key, 0, sizeof(key));
  54. key.hdr.nfsversion = clp->rpc_ops->version;
  55. key.hdr.family = clp->cl_addr.ss_family;
  56. switch (clp->cl_addr.ss_family) {
  57. case AF_INET:
  58. key.hdr.port = sin->sin_port;
  59. key.ipv4_addr = sin->sin_addr;
  60. len += sizeof(key.ipv4_addr);
  61. break;
  62. case AF_INET6:
  63. key.hdr.port = sin6->sin6_port;
  64. key.ipv6_addr = sin6->sin6_addr;
  65. len += sizeof(key.ipv6_addr);
  66. break;
  67. default:
  68. printk(KERN_WARNING "NFS: Unknown network family '%d'\n",
  69. clp->cl_addr.ss_family);
  70. clp->fscache = NULL;
  71. return;
  72. }
  73. /* create a cache index for looking up filehandles */
  74. clp->fscache = fscache_acquire_cookie(nfs_fscache_netfs.primary_index,
  75. &nfs_fscache_server_index_def,
  76. &key, len,
  77. NULL, 0,
  78. clp, 0, true);
  79. dfprintk(FSCACHE, "NFS: get client cookie (0x%p/0x%p)\n",
  80. clp, clp->fscache);
  81. }
  82. /*
  83. * Dispose of a per-client cookie
  84. */
  85. void nfs_fscache_release_client_cookie(struct nfs_client *clp)
  86. {
  87. dfprintk(FSCACHE, "NFS: releasing client cookie (0x%p/0x%p)\n",
  88. clp, clp->fscache);
  89. fscache_relinquish_cookie(clp->fscache, NULL, false);
  90. clp->fscache = NULL;
  91. }
  92. /*
  93. * Get the cache cookie for an NFS superblock. We have to handle
  94. * uniquification here because the cache doesn't do it for us.
  95. *
  96. * The default uniquifier is just an empty string, but it may be overridden
  97. * either by the 'fsc=xxx' option to mount, or by inheriting it from the parent
  98. * superblock across an automount point of some nature.
  99. */
  100. void nfs_fscache_get_super_cookie(struct super_block *sb, const char *uniq, int ulen)
  101. {
  102. struct nfs_fscache_key *key, *xkey;
  103. struct nfs_server *nfss = NFS_SB(sb);
  104. struct rb_node **p, *parent;
  105. int diff;
  106. if (!uniq) {
  107. uniq = "";
  108. ulen = 1;
  109. }
  110. key = kzalloc(sizeof(*key) + ulen, GFP_KERNEL);
  111. if (!key)
  112. return;
  113. key->nfs_client = nfss->nfs_client;
  114. key->key.super.s_flags = sb->s_flags & NFS_MS_MASK;
  115. key->key.nfs_server.flags = nfss->flags;
  116. key->key.nfs_server.rsize = nfss->rsize;
  117. key->key.nfs_server.wsize = nfss->wsize;
  118. key->key.nfs_server.acregmin = nfss->acregmin;
  119. key->key.nfs_server.acregmax = nfss->acregmax;
  120. key->key.nfs_server.acdirmin = nfss->acdirmin;
  121. key->key.nfs_server.acdirmax = nfss->acdirmax;
  122. key->key.nfs_server.fsid = nfss->fsid;
  123. key->key.rpc_auth.au_flavor = nfss->client->cl_auth->au_flavor;
  124. key->key.uniq_len = ulen;
  125. memcpy(key->key.uniquifier, uniq, ulen);
  126. spin_lock(&nfs_fscache_keys_lock);
  127. p = &nfs_fscache_keys.rb_node;
  128. parent = NULL;
  129. while (*p) {
  130. parent = *p;
  131. xkey = rb_entry(parent, struct nfs_fscache_key, node);
  132. if (key->nfs_client < xkey->nfs_client)
  133. goto go_left;
  134. if (key->nfs_client > xkey->nfs_client)
  135. goto go_right;
  136. diff = memcmp(&key->key, &xkey->key, sizeof(key->key));
  137. if (diff < 0)
  138. goto go_left;
  139. if (diff > 0)
  140. goto go_right;
  141. if (key->key.uniq_len == 0)
  142. goto non_unique;
  143. diff = memcmp(key->key.uniquifier,
  144. xkey->key.uniquifier,
  145. key->key.uniq_len);
  146. if (diff < 0)
  147. goto go_left;
  148. if (diff > 0)
  149. goto go_right;
  150. goto non_unique;
  151. go_left:
  152. p = &(*p)->rb_left;
  153. continue;
  154. go_right:
  155. p = &(*p)->rb_right;
  156. }
  157. rb_link_node(&key->node, parent, p);
  158. rb_insert_color(&key->node, &nfs_fscache_keys);
  159. spin_unlock(&nfs_fscache_keys_lock);
  160. nfss->fscache_key = key;
  161. /* create a cache index for looking up filehandles */
  162. nfss->fscache = fscache_acquire_cookie(nfss->nfs_client->fscache,
  163. &nfs_fscache_super_index_def,
  164. key, sizeof(*key) + ulen,
  165. NULL, 0,
  166. nfss, 0, true);
  167. dfprintk(FSCACHE, "NFS: get superblock cookie (0x%p/0x%p)\n",
  168. nfss, nfss->fscache);
  169. return;
  170. non_unique:
  171. spin_unlock(&nfs_fscache_keys_lock);
  172. kfree(key);
  173. nfss->fscache_key = NULL;
  174. nfss->fscache = NULL;
  175. printk(KERN_WARNING "NFS:"
  176. " Cache request denied due to non-unique superblock keys\n");
  177. }
  178. /*
  179. * release a per-superblock cookie
  180. */
  181. void nfs_fscache_release_super_cookie(struct super_block *sb)
  182. {
  183. struct nfs_server *nfss = NFS_SB(sb);
  184. dfprintk(FSCACHE, "NFS: releasing superblock cookie (0x%p/0x%p)\n",
  185. nfss, nfss->fscache);
  186. fscache_relinquish_cookie(nfss->fscache, NULL, false);
  187. nfss->fscache = NULL;
  188. if (nfss->fscache_key) {
  189. spin_lock(&nfs_fscache_keys_lock);
  190. rb_erase(&nfss->fscache_key->node, &nfs_fscache_keys);
  191. spin_unlock(&nfs_fscache_keys_lock);
  192. kfree(nfss->fscache_key);
  193. nfss->fscache_key = NULL;
  194. }
  195. }
  196. /*
  197. * Initialise the per-inode cache cookie pointer for an NFS inode.
  198. */
  199. void nfs_fscache_init_inode(struct inode *inode)
  200. {
  201. struct nfs_fscache_inode_auxdata auxdata;
  202. struct nfs_inode *nfsi = NFS_I(inode);
  203. nfsi->fscache = NULL;
  204. if (!S_ISREG(inode->i_mode))
  205. return;
  206. memset(&auxdata, 0, sizeof(auxdata));
  207. auxdata.mtime = timespec64_to_timespec(nfsi->vfs_inode.i_mtime);
  208. auxdata.ctime = timespec64_to_timespec(nfsi->vfs_inode.i_ctime);
  209. if (NFS_SERVER(&nfsi->vfs_inode)->nfs_client->rpc_ops->version == 4)
  210. auxdata.change_attr = inode_peek_iversion_raw(&nfsi->vfs_inode);
  211. nfsi->fscache = fscache_acquire_cookie(NFS_SB(inode->i_sb)->fscache,
  212. &nfs_fscache_inode_object_def,
  213. nfsi->fh.data, nfsi->fh.size,
  214. &auxdata, sizeof(auxdata),
  215. nfsi, nfsi->vfs_inode.i_size, false);
  216. }
  217. /*
  218. * Release a per-inode cookie.
  219. */
  220. void nfs_fscache_clear_inode(struct inode *inode)
  221. {
  222. struct nfs_fscache_inode_auxdata auxdata;
  223. struct nfs_inode *nfsi = NFS_I(inode);
  224. struct fscache_cookie *cookie = nfs_i_fscache(inode);
  225. dfprintk(FSCACHE, "NFS: clear cookie (0x%p/0x%p)\n", nfsi, cookie);
  226. memset(&auxdata, 0, sizeof(auxdata));
  227. auxdata.mtime = timespec64_to_timespec(nfsi->vfs_inode.i_mtime);
  228. auxdata.ctime = timespec64_to_timespec(nfsi->vfs_inode.i_ctime);
  229. fscache_relinquish_cookie(cookie, &auxdata, false);
  230. nfsi->fscache = NULL;
  231. }
  232. static bool nfs_fscache_can_enable(void *data)
  233. {
  234. struct inode *inode = data;
  235. return !inode_is_open_for_write(inode);
  236. }
  237. /*
  238. * Enable or disable caching for a file that is being opened as appropriate.
  239. * The cookie is allocated when the inode is initialised, but is not enabled at
  240. * that time. Enablement is deferred to file-open time to avoid stat() and
  241. * access() thrashing the cache.
  242. *
  243. * For now, with NFS, only regular files that are open read-only will be able
  244. * to use the cache.
  245. *
  246. * We enable the cache for an inode if we open it read-only and it isn't
  247. * currently open for writing. We disable the cache if the inode is open
  248. * write-only.
  249. *
  250. * The caller uses the file struct to pin i_writecount on the inode before
  251. * calling us when a file is opened for writing, so we can make use of that.
  252. *
  253. * Note that this may be invoked multiple times in parallel by parallel
  254. * nfs_open() functions.
  255. */
  256. void nfs_fscache_open_file(struct inode *inode, struct file *filp)
  257. {
  258. struct nfs_fscache_inode_auxdata auxdata;
  259. struct nfs_inode *nfsi = NFS_I(inode);
  260. struct fscache_cookie *cookie = nfs_i_fscache(inode);
  261. if (!fscache_cookie_valid(cookie))
  262. return;
  263. memset(&auxdata, 0, sizeof(auxdata));
  264. auxdata.mtime = timespec64_to_timespec(nfsi->vfs_inode.i_mtime);
  265. auxdata.ctime = timespec64_to_timespec(nfsi->vfs_inode.i_ctime);
  266. if (inode_is_open_for_write(inode)) {
  267. dfprintk(FSCACHE, "NFS: nfsi 0x%p disabling cache\n", nfsi);
  268. clear_bit(NFS_INO_FSCACHE, &nfsi->flags);
  269. fscache_disable_cookie(cookie, &auxdata, true);
  270. fscache_uncache_all_inode_pages(cookie, inode);
  271. } else {
  272. dfprintk(FSCACHE, "NFS: nfsi 0x%p enabling cache\n", nfsi);
  273. fscache_enable_cookie(cookie, &auxdata, nfsi->vfs_inode.i_size,
  274. nfs_fscache_can_enable, inode);
  275. if (fscache_cookie_enabled(cookie))
  276. set_bit(NFS_INO_FSCACHE, &NFS_I(inode)->flags);
  277. }
  278. }
  279. EXPORT_SYMBOL_GPL(nfs_fscache_open_file);
  280. /*
  281. * Release the caching state associated with a page, if the page isn't busy
  282. * interacting with the cache.
  283. * - Returns true (can release page) or false (page busy).
  284. */
  285. int nfs_fscache_release_page(struct page *page, gfp_t gfp)
  286. {
  287. if (PageFsCache(page)) {
  288. struct fscache_cookie *cookie = nfs_i_fscache(page->mapping->host);
  289. BUG_ON(!cookie);
  290. dfprintk(FSCACHE, "NFS: fscache releasepage (0x%p/0x%p/0x%p)\n",
  291. cookie, page, NFS_I(page->mapping->host));
  292. if (!fscache_maybe_release_page(cookie, page, gfp))
  293. return 0;
  294. nfs_inc_fscache_stats(page->mapping->host,
  295. NFSIOS_FSCACHE_PAGES_UNCACHED);
  296. }
  297. return 1;
  298. }
  299. /*
  300. * Release the caching state associated with a page if undergoing complete page
  301. * invalidation.
  302. */
  303. void __nfs_fscache_invalidate_page(struct page *page, struct inode *inode)
  304. {
  305. struct fscache_cookie *cookie = nfs_i_fscache(inode);
  306. BUG_ON(!cookie);
  307. dfprintk(FSCACHE, "NFS: fscache invalidatepage (0x%p/0x%p/0x%p)\n",
  308. cookie, page, NFS_I(inode));
  309. fscache_wait_on_page_write(cookie, page);
  310. BUG_ON(!PageLocked(page));
  311. fscache_uncache_page(cookie, page);
  312. nfs_inc_fscache_stats(page->mapping->host,
  313. NFSIOS_FSCACHE_PAGES_UNCACHED);
  314. }
  315. /*
  316. * Handle completion of a page being read from the cache.
  317. * - Called in process (keventd) context.
  318. */
  319. static void nfs_readpage_from_fscache_complete(struct page *page,
  320. void *context,
  321. int error)
  322. {
  323. dfprintk(FSCACHE,
  324. "NFS: readpage_from_fscache_complete (0x%p/0x%p/%d)\n",
  325. page, context, error);
  326. /* if the read completes with an error, we just unlock the page and let
  327. * the VM reissue the readpage */
  328. if (!error) {
  329. SetPageUptodate(page);
  330. unlock_page(page);
  331. } else {
  332. error = nfs_readpage_async(context, page->mapping->host, page);
  333. if (error)
  334. unlock_page(page);
  335. }
  336. }
  337. /*
  338. * Retrieve a page from fscache
  339. */
  340. int __nfs_readpage_from_fscache(struct nfs_open_context *ctx,
  341. struct inode *inode, struct page *page)
  342. {
  343. int ret;
  344. dfprintk(FSCACHE,
  345. "NFS: readpage_from_fscache(fsc:%p/p:%p(i:%lx f:%lx)/0x%p)\n",
  346. nfs_i_fscache(inode), page, page->index, page->flags, inode);
  347. ret = fscache_read_or_alloc_page(nfs_i_fscache(inode),
  348. page,
  349. nfs_readpage_from_fscache_complete,
  350. ctx,
  351. GFP_KERNEL);
  352. switch (ret) {
  353. case 0: /* read BIO submitted (page in fscache) */
  354. dfprintk(FSCACHE,
  355. "NFS: readpage_from_fscache: BIO submitted\n");
  356. nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_OK);
  357. return ret;
  358. case -ENOBUFS: /* inode not in cache */
  359. case -ENODATA: /* page not in cache */
  360. nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_FAIL);
  361. dfprintk(FSCACHE,
  362. "NFS: readpage_from_fscache %d\n", ret);
  363. return 1;
  364. default:
  365. dfprintk(FSCACHE, "NFS: readpage_from_fscache %d\n", ret);
  366. nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_FAIL);
  367. }
  368. return ret;
  369. }
  370. /*
  371. * Retrieve a set of pages from fscache
  372. */
  373. int __nfs_readpages_from_fscache(struct nfs_open_context *ctx,
  374. struct inode *inode,
  375. struct address_space *mapping,
  376. struct list_head *pages,
  377. unsigned *nr_pages)
  378. {
  379. unsigned npages = *nr_pages;
  380. int ret;
  381. dfprintk(FSCACHE, "NFS: nfs_getpages_from_fscache (0x%p/%u/0x%p)\n",
  382. nfs_i_fscache(inode), npages, inode);
  383. ret = fscache_read_or_alloc_pages(nfs_i_fscache(inode),
  384. mapping, pages, nr_pages,
  385. nfs_readpage_from_fscache_complete,
  386. ctx,
  387. mapping_gfp_mask(mapping));
  388. if (*nr_pages < npages)
  389. nfs_add_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_OK,
  390. npages);
  391. if (*nr_pages > 0)
  392. nfs_add_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_FAIL,
  393. *nr_pages);
  394. switch (ret) {
  395. case 0: /* read submitted to the cache for all pages */
  396. BUG_ON(!list_empty(pages));
  397. BUG_ON(*nr_pages != 0);
  398. dfprintk(FSCACHE,
  399. "NFS: nfs_getpages_from_fscache: submitted\n");
  400. return ret;
  401. case -ENOBUFS: /* some pages aren't cached and can't be */
  402. case -ENODATA: /* some pages aren't cached */
  403. dfprintk(FSCACHE,
  404. "NFS: nfs_getpages_from_fscache: no page: %d\n", ret);
  405. return 1;
  406. default:
  407. dfprintk(FSCACHE,
  408. "NFS: nfs_getpages_from_fscache: ret %d\n", ret);
  409. }
  410. return ret;
  411. }
  412. /*
  413. * Store a newly fetched page in fscache
  414. * - PG_fscache must be set on the page
  415. */
  416. void __nfs_readpage_to_fscache(struct inode *inode, struct page *page, int sync)
  417. {
  418. int ret;
  419. dfprintk(FSCACHE,
  420. "NFS: readpage_to_fscache(fsc:%p/p:%p(i:%lx f:%lx)/%d)\n",
  421. nfs_i_fscache(inode), page, page->index, page->flags, sync);
  422. ret = fscache_write_page(nfs_i_fscache(inode), page,
  423. inode->i_size, GFP_KERNEL);
  424. dfprintk(FSCACHE,
  425. "NFS: readpage_to_fscache: p:%p(i:%lu f:%lx) ret %d\n",
  426. page, page->index, page->flags, ret);
  427. if (ret != 0) {
  428. fscache_uncache_page(nfs_i_fscache(inode), page);
  429. nfs_inc_fscache_stats(inode,
  430. NFSIOS_FSCACHE_PAGES_WRITTEN_FAIL);
  431. nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_UNCACHED);
  432. } else {
  433. nfs_inc_fscache_stats(inode,
  434. NFSIOS_FSCACHE_PAGES_WRITTEN_OK);
  435. }
  436. }