fscache.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 ([email protected])
  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. #include "nfstrace.h"
  21. #define NFS_MAX_KEY_LEN 1000
  22. static bool nfs_append_int(char *key, int *_len, unsigned long long x)
  23. {
  24. if (*_len > NFS_MAX_KEY_LEN)
  25. return false;
  26. if (x == 0)
  27. key[(*_len)++] = ',';
  28. else
  29. *_len += sprintf(key + *_len, ",%llx", x);
  30. return true;
  31. }
  32. /*
  33. * Get the per-client index cookie for an NFS client if the appropriate mount
  34. * flag was set
  35. * - We always try and get an index cookie for the client, but get filehandle
  36. * cookies on a per-superblock basis, depending on the mount flags
  37. */
  38. static bool nfs_fscache_get_client_key(struct nfs_client *clp,
  39. char *key, int *_len)
  40. {
  41. const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &clp->cl_addr;
  42. const struct sockaddr_in *sin = (struct sockaddr_in *) &clp->cl_addr;
  43. *_len += snprintf(key + *_len, NFS_MAX_KEY_LEN - *_len,
  44. ",%u.%u,%x",
  45. clp->rpc_ops->version,
  46. clp->cl_minorversion,
  47. clp->cl_addr.ss_family);
  48. switch (clp->cl_addr.ss_family) {
  49. case AF_INET:
  50. if (!nfs_append_int(key, _len, sin->sin_port) ||
  51. !nfs_append_int(key, _len, sin->sin_addr.s_addr))
  52. return false;
  53. return true;
  54. case AF_INET6:
  55. if (!nfs_append_int(key, _len, sin6->sin6_port) ||
  56. !nfs_append_int(key, _len, sin6->sin6_addr.s6_addr32[0]) ||
  57. !nfs_append_int(key, _len, sin6->sin6_addr.s6_addr32[1]) ||
  58. !nfs_append_int(key, _len, sin6->sin6_addr.s6_addr32[2]) ||
  59. !nfs_append_int(key, _len, sin6->sin6_addr.s6_addr32[3]))
  60. return false;
  61. return true;
  62. default:
  63. printk(KERN_WARNING "NFS: Unknown network family '%d'\n",
  64. clp->cl_addr.ss_family);
  65. return false;
  66. }
  67. }
  68. /*
  69. * Get the cache cookie for an NFS superblock.
  70. *
  71. * The default uniquifier is just an empty string, but it may be overridden
  72. * either by the 'fsc=xxx' option to mount, or by inheriting it from the parent
  73. * superblock across an automount point of some nature.
  74. */
  75. int nfs_fscache_get_super_cookie(struct super_block *sb, const char *uniq, int ulen)
  76. {
  77. struct fscache_volume *vcookie;
  78. struct nfs_server *nfss = NFS_SB(sb);
  79. unsigned int len = 3;
  80. char *key;
  81. if (uniq) {
  82. nfss->fscache_uniq = kmemdup_nul(uniq, ulen, GFP_KERNEL);
  83. if (!nfss->fscache_uniq)
  84. return -ENOMEM;
  85. }
  86. key = kmalloc(NFS_MAX_KEY_LEN + 24, GFP_KERNEL);
  87. if (!key)
  88. return -ENOMEM;
  89. memcpy(key, "nfs", 3);
  90. if (!nfs_fscache_get_client_key(nfss->nfs_client, key, &len) ||
  91. !nfs_append_int(key, &len, nfss->fsid.major) ||
  92. !nfs_append_int(key, &len, nfss->fsid.minor) ||
  93. !nfs_append_int(key, &len, sb->s_flags & NFS_SB_MASK) ||
  94. !nfs_append_int(key, &len, nfss->flags) ||
  95. !nfs_append_int(key, &len, nfss->rsize) ||
  96. !nfs_append_int(key, &len, nfss->wsize) ||
  97. !nfs_append_int(key, &len, nfss->acregmin) ||
  98. !nfs_append_int(key, &len, nfss->acregmax) ||
  99. !nfs_append_int(key, &len, nfss->acdirmin) ||
  100. !nfs_append_int(key, &len, nfss->acdirmax) ||
  101. !nfs_append_int(key, &len, nfss->client->cl_auth->au_flavor))
  102. goto out;
  103. if (ulen > 0) {
  104. if (ulen > NFS_MAX_KEY_LEN - len)
  105. goto out;
  106. key[len++] = ',';
  107. memcpy(key + len, uniq, ulen);
  108. len += ulen;
  109. }
  110. key[len] = 0;
  111. /* create a cache index for looking up filehandles */
  112. vcookie = fscache_acquire_volume(key,
  113. NULL, /* preferred_cache */
  114. NULL, 0 /* coherency_data */);
  115. if (IS_ERR(vcookie)) {
  116. if (vcookie != ERR_PTR(-EBUSY)) {
  117. kfree(key);
  118. return PTR_ERR(vcookie);
  119. }
  120. pr_err("NFS: Cache volume key already in use (%s)\n", key);
  121. vcookie = NULL;
  122. }
  123. nfss->fscache = vcookie;
  124. out:
  125. kfree(key);
  126. return 0;
  127. }
  128. /*
  129. * release a per-superblock cookie
  130. */
  131. void nfs_fscache_release_super_cookie(struct super_block *sb)
  132. {
  133. struct nfs_server *nfss = NFS_SB(sb);
  134. fscache_relinquish_volume(nfss->fscache, NULL, false);
  135. nfss->fscache = NULL;
  136. kfree(nfss->fscache_uniq);
  137. }
  138. /*
  139. * Initialise the per-inode cache cookie pointer for an NFS inode.
  140. */
  141. void nfs_fscache_init_inode(struct inode *inode)
  142. {
  143. struct nfs_fscache_inode_auxdata auxdata;
  144. struct nfs_server *nfss = NFS_SERVER(inode);
  145. struct nfs_inode *nfsi = NFS_I(inode);
  146. nfsi->fscache = NULL;
  147. if (!(nfss->fscache && S_ISREG(inode->i_mode)))
  148. return;
  149. nfs_fscache_update_auxdata(&auxdata, inode);
  150. nfsi->fscache = fscache_acquire_cookie(NFS_SB(inode->i_sb)->fscache,
  151. 0,
  152. nfsi->fh.data, /* index_key */
  153. nfsi->fh.size,
  154. &auxdata, /* aux_data */
  155. sizeof(auxdata),
  156. i_size_read(inode));
  157. }
  158. /*
  159. * Release a per-inode cookie.
  160. */
  161. void nfs_fscache_clear_inode(struct inode *inode)
  162. {
  163. struct nfs_inode *nfsi = NFS_I(inode);
  164. struct fscache_cookie *cookie = nfs_i_fscache(inode);
  165. fscache_relinquish_cookie(cookie, false);
  166. nfsi->fscache = NULL;
  167. }
  168. /*
  169. * Enable or disable caching for a file that is being opened as appropriate.
  170. * The cookie is allocated when the inode is initialised, but is not enabled at
  171. * that time. Enablement is deferred to file-open time to avoid stat() and
  172. * access() thrashing the cache.
  173. *
  174. * For now, with NFS, only regular files that are open read-only will be able
  175. * to use the cache.
  176. *
  177. * We enable the cache for an inode if we open it read-only and it isn't
  178. * currently open for writing. We disable the cache if the inode is open
  179. * write-only.
  180. *
  181. * The caller uses the file struct to pin i_writecount on the inode before
  182. * calling us when a file is opened for writing, so we can make use of that.
  183. *
  184. * Note that this may be invoked multiple times in parallel by parallel
  185. * nfs_open() functions.
  186. */
  187. void nfs_fscache_open_file(struct inode *inode, struct file *filp)
  188. {
  189. struct nfs_fscache_inode_auxdata auxdata;
  190. struct fscache_cookie *cookie = nfs_i_fscache(inode);
  191. bool open_for_write = inode_is_open_for_write(inode);
  192. if (!fscache_cookie_valid(cookie))
  193. return;
  194. fscache_use_cookie(cookie, open_for_write);
  195. if (open_for_write) {
  196. nfs_fscache_update_auxdata(&auxdata, inode);
  197. fscache_invalidate(cookie, &auxdata, i_size_read(inode),
  198. FSCACHE_INVAL_DIO_WRITE);
  199. }
  200. }
  201. EXPORT_SYMBOL_GPL(nfs_fscache_open_file);
  202. void nfs_fscache_release_file(struct inode *inode, struct file *filp)
  203. {
  204. struct nfs_fscache_inode_auxdata auxdata;
  205. struct fscache_cookie *cookie = nfs_i_fscache(inode);
  206. loff_t i_size = i_size_read(inode);
  207. nfs_fscache_update_auxdata(&auxdata, inode);
  208. fscache_unuse_cookie(cookie, &auxdata, &i_size);
  209. }
  210. /*
  211. * Fallback page reading interface.
  212. */
  213. static int fscache_fallback_read_page(struct inode *inode, struct page *page)
  214. {
  215. struct netfs_cache_resources cres;
  216. struct fscache_cookie *cookie = nfs_i_fscache(inode);
  217. struct iov_iter iter;
  218. struct bio_vec bvec[1];
  219. int ret;
  220. memset(&cres, 0, sizeof(cres));
  221. bvec[0].bv_page = page;
  222. bvec[0].bv_offset = 0;
  223. bvec[0].bv_len = PAGE_SIZE;
  224. iov_iter_bvec(&iter, ITER_DEST, bvec, ARRAY_SIZE(bvec), PAGE_SIZE);
  225. ret = fscache_begin_read_operation(&cres, cookie);
  226. if (ret < 0)
  227. return ret;
  228. ret = fscache_read(&cres, page_offset(page), &iter, NETFS_READ_HOLE_FAIL,
  229. NULL, NULL);
  230. fscache_end_operation(&cres);
  231. return ret;
  232. }
  233. /*
  234. * Fallback page writing interface.
  235. */
  236. static int fscache_fallback_write_page(struct inode *inode, struct page *page,
  237. bool no_space_allocated_yet)
  238. {
  239. struct netfs_cache_resources cres;
  240. struct fscache_cookie *cookie = nfs_i_fscache(inode);
  241. struct iov_iter iter;
  242. struct bio_vec bvec[1];
  243. loff_t start = page_offset(page);
  244. size_t len = PAGE_SIZE;
  245. int ret;
  246. memset(&cres, 0, sizeof(cres));
  247. bvec[0].bv_page = page;
  248. bvec[0].bv_offset = 0;
  249. bvec[0].bv_len = PAGE_SIZE;
  250. iov_iter_bvec(&iter, ITER_SOURCE, bvec, ARRAY_SIZE(bvec), PAGE_SIZE);
  251. ret = fscache_begin_write_operation(&cres, cookie);
  252. if (ret < 0)
  253. return ret;
  254. ret = cres.ops->prepare_write(&cres, &start, &len, i_size_read(inode),
  255. no_space_allocated_yet);
  256. if (ret == 0)
  257. ret = fscache_write(&cres, page_offset(page), &iter, NULL, NULL);
  258. fscache_end_operation(&cres);
  259. return ret;
  260. }
  261. /*
  262. * Retrieve a page from fscache
  263. */
  264. int __nfs_fscache_read_page(struct inode *inode, struct page *page)
  265. {
  266. int ret;
  267. trace_nfs_fscache_read_page(inode, page);
  268. if (PageChecked(page)) {
  269. ClearPageChecked(page);
  270. ret = 1;
  271. goto out;
  272. }
  273. ret = fscache_fallback_read_page(inode, page);
  274. if (ret < 0) {
  275. nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_FAIL);
  276. SetPageChecked(page);
  277. goto out;
  278. }
  279. /* Read completed synchronously */
  280. nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_OK);
  281. SetPageUptodate(page);
  282. ret = 0;
  283. out:
  284. trace_nfs_fscache_read_page_exit(inode, page, ret);
  285. return ret;
  286. }
  287. /*
  288. * Store a newly fetched page in fscache. We can be certain there's no page
  289. * stored in the cache as yet otherwise we would've read it from there.
  290. */
  291. void __nfs_fscache_write_page(struct inode *inode, struct page *page)
  292. {
  293. int ret;
  294. trace_nfs_fscache_write_page(inode, page);
  295. ret = fscache_fallback_write_page(inode, page, true);
  296. if (ret != 0) {
  297. nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_WRITTEN_FAIL);
  298. nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_UNCACHED);
  299. } else {
  300. nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_WRITTEN_OK);
  301. }
  302. trace_nfs_fscache_write_page_exit(inode, page, ret);
  303. }