fscache.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 <linux/xarray.h>
  18. #include <linux/fscache.h>
  19. #include <linux/netfs.h>
  20. #include "internal.h"
  21. #include "iostat.h"
  22. #include "fscache.h"
  23. #include "nfstrace.h"
  24. #define NFS_MAX_KEY_LEN 1000
  25. static bool nfs_append_int(char *key, int *_len, unsigned long long x)
  26. {
  27. if (*_len > NFS_MAX_KEY_LEN)
  28. return false;
  29. if (x == 0)
  30. key[(*_len)++] = ',';
  31. else
  32. *_len += sprintf(key + *_len, ",%llx", x);
  33. return true;
  34. }
  35. /*
  36. * Get the per-client index cookie for an NFS client if the appropriate mount
  37. * flag was set
  38. * - We always try and get an index cookie for the client, but get filehandle
  39. * cookies on a per-superblock basis, depending on the mount flags
  40. */
  41. static bool nfs_fscache_get_client_key(struct nfs_client *clp,
  42. char *key, int *_len)
  43. {
  44. const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &clp->cl_addr;
  45. const struct sockaddr_in *sin = (struct sockaddr_in *) &clp->cl_addr;
  46. *_len += snprintf(key + *_len, NFS_MAX_KEY_LEN - *_len,
  47. ",%u.%u,%x",
  48. clp->rpc_ops->version,
  49. clp->cl_minorversion,
  50. clp->cl_addr.ss_family);
  51. switch (clp->cl_addr.ss_family) {
  52. case AF_INET:
  53. if (!nfs_append_int(key, _len, sin->sin_port) ||
  54. !nfs_append_int(key, _len, sin->sin_addr.s_addr))
  55. return false;
  56. return true;
  57. case AF_INET6:
  58. if (!nfs_append_int(key, _len, sin6->sin6_port) ||
  59. !nfs_append_int(key, _len, sin6->sin6_addr.s6_addr32[0]) ||
  60. !nfs_append_int(key, _len, sin6->sin6_addr.s6_addr32[1]) ||
  61. !nfs_append_int(key, _len, sin6->sin6_addr.s6_addr32[2]) ||
  62. !nfs_append_int(key, _len, sin6->sin6_addr.s6_addr32[3]))
  63. return false;
  64. return true;
  65. default:
  66. printk(KERN_WARNING "NFS: Unknown network family '%d'\n",
  67. clp->cl_addr.ss_family);
  68. return false;
  69. }
  70. }
  71. /*
  72. * Get the cache cookie for an NFS superblock.
  73. *
  74. * The default uniquifier is just an empty string, but it may be overridden
  75. * either by the 'fsc=xxx' option to mount, or by inheriting it from the parent
  76. * superblock across an automount point of some nature.
  77. */
  78. int nfs_fscache_get_super_cookie(struct super_block *sb, const char *uniq, int ulen)
  79. {
  80. struct fscache_volume *vcookie;
  81. struct nfs_server *nfss = NFS_SB(sb);
  82. unsigned int len = 3;
  83. char *key;
  84. if (uniq) {
  85. nfss->fscache_uniq = kmemdup_nul(uniq, ulen, GFP_KERNEL);
  86. if (!nfss->fscache_uniq)
  87. return -ENOMEM;
  88. }
  89. key = kmalloc(NFS_MAX_KEY_LEN + 24, GFP_KERNEL);
  90. if (!key)
  91. return -ENOMEM;
  92. memcpy(key, "nfs", 3);
  93. if (!nfs_fscache_get_client_key(nfss->nfs_client, key, &len) ||
  94. !nfs_append_int(key, &len, nfss->fsid.major) ||
  95. !nfs_append_int(key, &len, nfss->fsid.minor) ||
  96. !nfs_append_int(key, &len, sb->s_flags & NFS_SB_MASK) ||
  97. !nfs_append_int(key, &len, nfss->flags) ||
  98. !nfs_append_int(key, &len, nfss->rsize) ||
  99. !nfs_append_int(key, &len, nfss->wsize) ||
  100. !nfs_append_int(key, &len, nfss->acregmin) ||
  101. !nfs_append_int(key, &len, nfss->acregmax) ||
  102. !nfs_append_int(key, &len, nfss->acdirmin) ||
  103. !nfs_append_int(key, &len, nfss->acdirmax) ||
  104. !nfs_append_int(key, &len, nfss->client->cl_auth->au_flavor))
  105. goto out;
  106. if (ulen > 0) {
  107. if (ulen > NFS_MAX_KEY_LEN - len)
  108. goto out;
  109. key[len++] = ',';
  110. memcpy(key + len, uniq, ulen);
  111. len += ulen;
  112. }
  113. key[len] = 0;
  114. /* create a cache index for looking up filehandles */
  115. vcookie = fscache_acquire_volume(key,
  116. NULL, /* preferred_cache */
  117. NULL, 0 /* coherency_data */);
  118. if (IS_ERR(vcookie)) {
  119. if (vcookie != ERR_PTR(-EBUSY)) {
  120. kfree(key);
  121. return PTR_ERR(vcookie);
  122. }
  123. pr_err("NFS: Cache volume key already in use (%s)\n", key);
  124. vcookie = NULL;
  125. }
  126. nfss->fscache = vcookie;
  127. out:
  128. kfree(key);
  129. return 0;
  130. }
  131. /*
  132. * release a per-superblock cookie
  133. */
  134. void nfs_fscache_release_super_cookie(struct super_block *sb)
  135. {
  136. struct nfs_server *nfss = NFS_SB(sb);
  137. fscache_relinquish_volume(nfss->fscache, NULL, false);
  138. nfss->fscache = NULL;
  139. kfree(nfss->fscache_uniq);
  140. }
  141. /*
  142. * Initialise the per-inode cache cookie pointer for an NFS inode.
  143. */
  144. void nfs_fscache_init_inode(struct inode *inode)
  145. {
  146. struct nfs_fscache_inode_auxdata auxdata;
  147. struct nfs_server *nfss = NFS_SERVER(inode);
  148. struct nfs_inode *nfsi = NFS_I(inode);
  149. netfs_inode(inode)->cache = NULL;
  150. if (!(nfss->fscache && S_ISREG(inode->i_mode)))
  151. return;
  152. nfs_fscache_update_auxdata(&auxdata, inode);
  153. netfs_inode(inode)->cache = fscache_acquire_cookie(
  154. nfss->fscache,
  155. 0,
  156. nfsi->fh.data, /* index_key */
  157. nfsi->fh.size,
  158. &auxdata, /* aux_data */
  159. sizeof(auxdata),
  160. i_size_read(inode));
  161. if (netfs_inode(inode)->cache)
  162. mapping_set_release_always(inode->i_mapping);
  163. }
  164. /*
  165. * Release a per-inode cookie.
  166. */
  167. void nfs_fscache_clear_inode(struct inode *inode)
  168. {
  169. fscache_relinquish_cookie(netfs_i_cookie(netfs_inode(inode)), false);
  170. netfs_inode(inode)->cache = NULL;
  171. }
  172. /*
  173. * Enable or disable caching for a file that is being opened as appropriate.
  174. * The cookie is allocated when the inode is initialised, but is not enabled at
  175. * that time. Enablement is deferred to file-open time to avoid stat() and
  176. * access() thrashing the cache.
  177. *
  178. * For now, with NFS, only regular files that are open read-only will be able
  179. * to use the cache.
  180. *
  181. * We enable the cache for an inode if we open it read-only and it isn't
  182. * currently open for writing. We disable the cache if the inode is open
  183. * write-only.
  184. *
  185. * The caller uses the file struct to pin i_writecount on the inode before
  186. * calling us when a file is opened for writing, so we can make use of that.
  187. *
  188. * Note that this may be invoked multiple times in parallel by parallel
  189. * nfs_open() functions.
  190. */
  191. void nfs_fscache_open_file(struct inode *inode, struct file *filp)
  192. {
  193. struct nfs_fscache_inode_auxdata auxdata;
  194. struct fscache_cookie *cookie = netfs_i_cookie(netfs_inode(inode));
  195. bool open_for_write = inode_is_open_for_write(inode);
  196. if (!fscache_cookie_valid(cookie))
  197. return;
  198. fscache_use_cookie(cookie, open_for_write);
  199. if (open_for_write) {
  200. nfs_fscache_update_auxdata(&auxdata, inode);
  201. fscache_invalidate(cookie, &auxdata, i_size_read(inode),
  202. FSCACHE_INVAL_DIO_WRITE);
  203. }
  204. }
  205. EXPORT_SYMBOL_GPL(nfs_fscache_open_file);
  206. void nfs_fscache_release_file(struct inode *inode, struct file *filp)
  207. {
  208. struct nfs_fscache_inode_auxdata auxdata;
  209. struct fscache_cookie *cookie = netfs_i_cookie(netfs_inode(inode));
  210. loff_t i_size = i_size_read(inode);
  211. nfs_fscache_update_auxdata(&auxdata, inode);
  212. fscache_unuse_cookie(cookie, &auxdata, &i_size);
  213. }
  214. int nfs_netfs_read_folio(struct file *file, struct folio *folio)
  215. {
  216. if (!netfs_inode(folio_inode(folio))->cache)
  217. return -ENOBUFS;
  218. return netfs_read_folio(file, folio);
  219. }
  220. int nfs_netfs_readahead(struct readahead_control *ractl)
  221. {
  222. struct inode *inode = ractl->mapping->host;
  223. if (!netfs_inode(inode)->cache)
  224. return -ENOBUFS;
  225. netfs_readahead(ractl);
  226. return 0;
  227. }
  228. static atomic_t nfs_netfs_debug_id;
  229. static int nfs_netfs_init_request(struct netfs_io_request *rreq, struct file *file)
  230. {
  231. if (!file) {
  232. if (WARN_ON_ONCE(rreq->origin != NETFS_PGPRIV2_COPY_TO_CACHE))
  233. return -EIO;
  234. return 0;
  235. }
  236. rreq->netfs_priv = get_nfs_open_context(nfs_file_open_context(file));
  237. rreq->debug_id = atomic_inc_return(&nfs_netfs_debug_id);
  238. /* [DEPRECATED] Use PG_private_2 to mark folio being written to the cache. */
  239. __set_bit(NETFS_RREQ_USE_PGPRIV2, &rreq->flags);
  240. rreq->io_streams[0].sreq_max_len = NFS_SB(rreq->inode->i_sb)->rsize;
  241. return 0;
  242. }
  243. static void nfs_netfs_free_request(struct netfs_io_request *rreq)
  244. {
  245. if (rreq->netfs_priv)
  246. put_nfs_open_context(rreq->netfs_priv);
  247. }
  248. static struct nfs_netfs_io_data *nfs_netfs_alloc(struct netfs_io_subrequest *sreq)
  249. {
  250. struct nfs_netfs_io_data *netfs;
  251. netfs = kzalloc(sizeof(*netfs), GFP_KERNEL_ACCOUNT);
  252. if (!netfs)
  253. return NULL;
  254. netfs->sreq = sreq;
  255. refcount_set(&netfs->refcount, 1);
  256. return netfs;
  257. }
  258. static void nfs_netfs_issue_read(struct netfs_io_subrequest *sreq)
  259. {
  260. struct nfs_netfs_io_data *netfs;
  261. struct nfs_pageio_descriptor pgio;
  262. struct inode *inode = sreq->rreq->inode;
  263. struct nfs_open_context *ctx = sreq->rreq->netfs_priv;
  264. struct page *page;
  265. unsigned long idx;
  266. pgoff_t start, last;
  267. int err;
  268. start = (sreq->start + sreq->transferred) >> PAGE_SHIFT;
  269. last = ((sreq->start + sreq->len - sreq->transferred - 1) >> PAGE_SHIFT);
  270. nfs_pageio_init_read(&pgio, inode, false,
  271. &nfs_async_read_completion_ops);
  272. netfs = nfs_netfs_alloc(sreq);
  273. if (!netfs)
  274. return netfs_read_subreq_terminated(sreq, -ENOMEM, false);
  275. pgio.pg_netfs = netfs; /* used in completion */
  276. xa_for_each_range(&sreq->rreq->mapping->i_pages, idx, page, start, last) {
  277. /* nfs_read_add_folio() may schedule() due to pNFS layout and other RPCs */
  278. err = nfs_read_add_folio(&pgio, ctx, page_folio(page));
  279. if (err < 0) {
  280. netfs->error = err;
  281. goto out;
  282. }
  283. }
  284. out:
  285. nfs_pageio_complete_read(&pgio);
  286. nfs_netfs_put(netfs);
  287. }
  288. void nfs_netfs_initiate_read(struct nfs_pgio_header *hdr)
  289. {
  290. struct nfs_netfs_io_data *netfs = hdr->netfs;
  291. if (!netfs)
  292. return;
  293. nfs_netfs_get(netfs);
  294. }
  295. int nfs_netfs_folio_unlock(struct folio *folio)
  296. {
  297. struct inode *inode = folio->mapping->host;
  298. /*
  299. * If fscache is enabled, netfs will unlock pages.
  300. */
  301. if (netfs_inode(inode)->cache)
  302. return 0;
  303. return 1;
  304. }
  305. void nfs_netfs_read_completion(struct nfs_pgio_header *hdr)
  306. {
  307. struct nfs_netfs_io_data *netfs = hdr->netfs;
  308. struct netfs_io_subrequest *sreq;
  309. if (!netfs)
  310. return;
  311. sreq = netfs->sreq;
  312. if (test_bit(NFS_IOHDR_EOF, &hdr->flags) &&
  313. sreq->rreq->origin != NETFS_DIO_READ)
  314. __set_bit(NETFS_SREQ_CLEAR_TAIL, &sreq->flags);
  315. if (hdr->error)
  316. netfs->error = hdr->error;
  317. else
  318. atomic64_add(hdr->res.count, &netfs->transferred);
  319. nfs_netfs_put(netfs);
  320. hdr->netfs = NULL;
  321. }
  322. const struct netfs_request_ops nfs_netfs_ops = {
  323. .init_request = nfs_netfs_init_request,
  324. .free_request = nfs_netfs_free_request,
  325. .issue_read = nfs_netfs_issue_read,
  326. };