fscache.c 14 KB

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