xref: /openbmc/linux/fs/nfs/fscache.c (revision 52beb1fc)
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 
8 #include <linux/init.h>
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/mm.h>
12 #include <linux/nfs_fs.h>
13 #include <linux/nfs_fs_sb.h>
14 #include <linux/in6.h>
15 #include <linux/seq_file.h>
16 #include <linux/slab.h>
17 #include <linux/iversion.h>
18 
19 #include "internal.h"
20 #include "iostat.h"
21 #include "fscache.h"
22 
23 #define NFSDBG_FACILITY		NFSDBG_FSCACHE
24 
25 #define NFS_MAX_KEY_LEN 1000
26 
27 static bool nfs_append_int(char *key, int *_len, unsigned long long x)
28 {
29 	if (*_len > NFS_MAX_KEY_LEN)
30 		return false;
31 	if (x == 0)
32 		key[(*_len)++] = ',';
33 	else
34 		*_len += sprintf(key + *_len, ",%llx", x);
35 	return true;
36 }
37 
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 static bool nfs_fscache_get_client_key(struct nfs_client *clp,
45 				       char *key, int *_len)
46 {
47 	const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &clp->cl_addr;
48 	const struct sockaddr_in *sin = (struct sockaddr_in *) &clp->cl_addr;
49 
50 	*_len += snprintf(key + *_len, NFS_MAX_KEY_LEN - *_len,
51 			  ",%u.%u,%x",
52 			  clp->rpc_ops->version,
53 			  clp->cl_minorversion,
54 			  clp->cl_addr.ss_family);
55 
56 	switch (clp->cl_addr.ss_family) {
57 	case AF_INET:
58 		if (!nfs_append_int(key, _len, sin->sin_port) ||
59 		    !nfs_append_int(key, _len, sin->sin_addr.s_addr))
60 			return false;
61 		return true;
62 
63 	case AF_INET6:
64 		if (!nfs_append_int(key, _len, sin6->sin6_port) ||
65 		    !nfs_append_int(key, _len, sin6->sin6_addr.s6_addr32[0]) ||
66 		    !nfs_append_int(key, _len, sin6->sin6_addr.s6_addr32[1]) ||
67 		    !nfs_append_int(key, _len, sin6->sin6_addr.s6_addr32[2]) ||
68 		    !nfs_append_int(key, _len, sin6->sin6_addr.s6_addr32[3]))
69 			return false;
70 		return true;
71 
72 	default:
73 		printk(KERN_WARNING "NFS: Unknown network family '%d'\n",
74 		       clp->cl_addr.ss_family);
75 		return false;
76 	}
77 }
78 
79 /*
80  * Get the cache cookie for an NFS superblock.
81  *
82  * The default uniquifier is just an empty string, but it may be overridden
83  * either by the 'fsc=xxx' option to mount, or by inheriting it from the parent
84  * superblock across an automount point of some nature.
85  */
86 int nfs_fscache_get_super_cookie(struct super_block *sb, const char *uniq, int ulen)
87 {
88 	struct fscache_volume *vcookie;
89 	struct nfs_server *nfss = NFS_SB(sb);
90 	unsigned int len = 3;
91 	char *key;
92 
93 	if (uniq) {
94 		nfss->fscache_uniq = kmemdup_nul(uniq, ulen, GFP_KERNEL);
95 		if (!nfss->fscache_uniq)
96 			return -ENOMEM;
97 	}
98 
99 	key = kmalloc(NFS_MAX_KEY_LEN + 24, GFP_KERNEL);
100 	if (!key)
101 		return -ENOMEM;
102 
103 	memcpy(key, "nfs", 3);
104 	if (!nfs_fscache_get_client_key(nfss->nfs_client, key, &len) ||
105 	    !nfs_append_int(key, &len, nfss->fsid.major) ||
106 	    !nfs_append_int(key, &len, nfss->fsid.minor) ||
107 	    !nfs_append_int(key, &len, sb->s_flags & NFS_SB_MASK) ||
108 	    !nfs_append_int(key, &len, nfss->flags) ||
109 	    !nfs_append_int(key, &len, nfss->rsize) ||
110 	    !nfs_append_int(key, &len, nfss->wsize) ||
111 	    !nfs_append_int(key, &len, nfss->acregmin) ||
112 	    !nfs_append_int(key, &len, nfss->acregmax) ||
113 	    !nfs_append_int(key, &len, nfss->acdirmin) ||
114 	    !nfs_append_int(key, &len, nfss->acdirmax) ||
115 	    !nfs_append_int(key, &len, nfss->client->cl_auth->au_flavor))
116 		goto out;
117 
118 	if (ulen > 0) {
119 		if (ulen > NFS_MAX_KEY_LEN - len)
120 			goto out;
121 		key[len++] = ',';
122 		memcpy(key + len, uniq, ulen);
123 		len += ulen;
124 	}
125 	key[len] = 0;
126 
127 	/* create a cache index for looking up filehandles */
128 	vcookie = fscache_acquire_volume(key,
129 					 NULL, /* preferred_cache */
130 					 NULL, 0 /* coherency_data */);
131 	dfprintk(FSCACHE, "NFS: get superblock cookie (0x%p/0x%p)\n",
132 		 nfss, vcookie);
133 	if (IS_ERR(vcookie)) {
134 		if (vcookie != ERR_PTR(-EBUSY)) {
135 			kfree(key);
136 			return PTR_ERR(vcookie);
137 		}
138 		pr_err("NFS: Cache volume key already in use (%s)\n", key);
139 		vcookie = NULL;
140 	}
141 	nfss->fscache = vcookie;
142 
143 out:
144 	kfree(key);
145 	return 0;
146 }
147 
148 /*
149  * release a per-superblock cookie
150  */
151 void nfs_fscache_release_super_cookie(struct super_block *sb)
152 {
153 	struct nfs_server *nfss = NFS_SB(sb);
154 
155 	dfprintk(FSCACHE, "NFS: releasing superblock cookie (0x%p/0x%p)\n",
156 		 nfss, nfss->fscache);
157 
158 	fscache_relinquish_volume(nfss->fscache, NULL, false);
159 	nfss->fscache = NULL;
160 	kfree(nfss->fscache_uniq);
161 }
162 
163 /*
164  * Initialise the per-inode cache cookie pointer for an NFS inode.
165  */
166 void nfs_fscache_init_inode(struct inode *inode)
167 {
168 	struct nfs_fscache_inode_auxdata auxdata;
169 	struct nfs_server *nfss = NFS_SERVER(inode);
170 	struct nfs_inode *nfsi = NFS_I(inode);
171 
172 	nfsi->fscache = NULL;
173 	if (!(nfss->fscache && S_ISREG(inode->i_mode)))
174 		return;
175 
176 	nfs_fscache_update_auxdata(&auxdata, nfsi);
177 
178 	nfsi->fscache = fscache_acquire_cookie(NFS_SB(inode->i_sb)->fscache,
179 					       0,
180 					       nfsi->fh.data, /* index_key */
181 					       nfsi->fh.size,
182 					       &auxdata,      /* aux_data */
183 					       sizeof(auxdata),
184 					       i_size_read(&nfsi->vfs_inode));
185 }
186 
187 /*
188  * Release a per-inode cookie.
189  */
190 void nfs_fscache_clear_inode(struct inode *inode)
191 {
192 	struct nfs_inode *nfsi = NFS_I(inode);
193 	struct fscache_cookie *cookie = nfs_i_fscache(inode);
194 
195 	dfprintk(FSCACHE, "NFS: clear cookie (0x%p/0x%p)\n", nfsi, cookie);
196 
197 	fscache_relinquish_cookie(cookie, false);
198 	nfsi->fscache = NULL;
199 }
200 
201 /*
202  * Enable or disable caching for a file that is being opened as appropriate.
203  * The cookie is allocated when the inode is initialised, but is not enabled at
204  * that time.  Enablement is deferred to file-open time to avoid stat() and
205  * access() thrashing the cache.
206  *
207  * For now, with NFS, only regular files that are open read-only will be able
208  * to use the cache.
209  *
210  * We enable the cache for an inode if we open it read-only and it isn't
211  * currently open for writing.  We disable the cache if the inode is open
212  * write-only.
213  *
214  * The caller uses the file struct to pin i_writecount on the inode before
215  * calling us when a file is opened for writing, so we can make use of that.
216  *
217  * Note that this may be invoked multiple times in parallel by parallel
218  * nfs_open() functions.
219  */
220 void nfs_fscache_open_file(struct inode *inode, struct file *filp)
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 	bool open_for_write = inode_is_open_for_write(inode);
226 
227 	if (!fscache_cookie_valid(cookie))
228 		return;
229 
230 	fscache_use_cookie(cookie, open_for_write);
231 	if (open_for_write) {
232 		dfprintk(FSCACHE, "NFS: nfsi 0x%p disabling cache\n", nfsi);
233 		nfs_fscache_update_auxdata(&auxdata, nfsi);
234 		fscache_invalidate(cookie, &auxdata, i_size_read(inode),
235 				   FSCACHE_INVAL_DIO_WRITE);
236 	}
237 }
238 EXPORT_SYMBOL_GPL(nfs_fscache_open_file);
239 
240 void nfs_fscache_release_file(struct inode *inode, struct file *filp)
241 {
242 	struct nfs_fscache_inode_auxdata auxdata;
243 	struct nfs_inode *nfsi = NFS_I(inode);
244 	struct fscache_cookie *cookie = nfs_i_fscache(inode);
245 
246 	if (fscache_cookie_valid(cookie)) {
247 		nfs_fscache_update_auxdata(&auxdata, nfsi);
248 		fscache_unuse_cookie(cookie, &auxdata, NULL);
249 	}
250 }
251 
252 static inline void fscache_end_operation(struct netfs_cache_resources *cres)
253 {
254 	const struct netfs_cache_ops *ops = fscache_operation_valid(cres);
255 
256 	if (ops)
257 		ops->end_operation(cres);
258 }
259 
260 /*
261  * Fallback page reading interface.
262  */
263 static int fscache_fallback_read_page(struct inode *inode, struct page *page)
264 {
265 	struct netfs_cache_resources cres;
266 	struct fscache_cookie *cookie = nfs_i_fscache(inode);
267 	struct iov_iter iter;
268 	struct bio_vec bvec[1];
269 	int ret;
270 
271 	memset(&cres, 0, sizeof(cres));
272 	bvec[0].bv_page		= page;
273 	bvec[0].bv_offset	= 0;
274 	bvec[0].bv_len		= PAGE_SIZE;
275 	iov_iter_bvec(&iter, READ, bvec, ARRAY_SIZE(bvec), PAGE_SIZE);
276 
277 	ret = fscache_begin_read_operation(&cres, cookie);
278 	if (ret < 0)
279 		return ret;
280 
281 	ret = fscache_read(&cres, page_offset(page), &iter, NETFS_READ_HOLE_FAIL,
282 			   NULL, NULL);
283 	fscache_end_operation(&cres);
284 	return ret;
285 }
286 
287 /*
288  * Fallback page writing interface.
289  */
290 static int fscache_fallback_write_page(struct inode *inode, struct page *page,
291 				       bool no_space_allocated_yet)
292 {
293 	struct netfs_cache_resources cres;
294 	struct fscache_cookie *cookie = nfs_i_fscache(inode);
295 	struct iov_iter iter;
296 	struct bio_vec bvec[1];
297 	loff_t start = page_offset(page);
298 	size_t len = PAGE_SIZE;
299 	int ret;
300 
301 	memset(&cres, 0, sizeof(cres));
302 	bvec[0].bv_page		= page;
303 	bvec[0].bv_offset	= 0;
304 	bvec[0].bv_len		= PAGE_SIZE;
305 	iov_iter_bvec(&iter, WRITE, bvec, ARRAY_SIZE(bvec), PAGE_SIZE);
306 
307 	ret = fscache_begin_write_operation(&cres, cookie);
308 	if (ret < 0)
309 		return ret;
310 
311 	ret = cres.ops->prepare_write(&cres, &start, &len, i_size_read(inode),
312 				      no_space_allocated_yet);
313 	if (ret == 0)
314 		ret = fscache_write(&cres, page_offset(page), &iter, NULL, NULL);
315 	fscache_end_operation(&cres);
316 	return ret;
317 }
318 
319 /*
320  * Retrieve a page from fscache
321  */
322 int __nfs_readpage_from_fscache(struct inode *inode, struct page *page)
323 {
324 	int ret;
325 
326 	dfprintk(FSCACHE,
327 		 "NFS: readpage_from_fscache(fsc:%p/p:%p(i:%lx f:%lx)/0x%p)\n",
328 		 nfs_i_fscache(inode), page, page->index, page->flags, inode);
329 
330 	if (PageChecked(page)) {
331 		dfprintk(FSCACHE, "NFS:    readpage_from_fscache: PageChecked\n");
332 		ClearPageChecked(page);
333 		return 1;
334 	}
335 
336 	ret = fscache_fallback_read_page(inode, page);
337 	if (ret < 0) {
338 		nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_FAIL);
339 		dfprintk(FSCACHE,
340 			 "NFS:    readpage_from_fscache failed %d\n", ret);
341 		SetPageChecked(page);
342 		return ret;
343 	}
344 
345 	/* Read completed synchronously */
346 	dfprintk(FSCACHE, "NFS:    readpage_from_fscache: read successful\n");
347 	nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_READ_OK);
348 	SetPageUptodate(page);
349 	return 0;
350 }
351 
352 /*
353  * Store a newly fetched page in fscache.  We can be certain there's no page
354  * stored in the cache as yet otherwise we would've read it from there.
355  */
356 void __nfs_readpage_to_fscache(struct inode *inode, struct page *page)
357 {
358 	int ret;
359 
360 	dfprintk(FSCACHE,
361 		 "NFS: readpage_to_fscache(fsc:%p/p:%p(i:%lx f:%lx))\n",
362 		 nfs_i_fscache(inode), page, page->index, page->flags);
363 
364 	ret = fscache_fallback_write_page(inode, page, true);
365 
366 	dfprintk(FSCACHE,
367 		 "NFS:     readpage_to_fscache: p:%p(i:%lu f:%lx) ret %d\n",
368 		 page, page->index, page->flags, ret);
369 
370 	if (ret != 0) {
371 		nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_WRITTEN_FAIL);
372 		nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_UNCACHED);
373 	} else {
374 		nfs_inc_fscache_stats(inode, NFSIOS_FSCACHE_PAGES_WRITTEN_OK);
375 	}
376 }
377