xref: /openbmc/linux/fs/nfs/read.c (revision ef69d255)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/fs/nfs/read.c
4  *
5  * Block I/O for NFS
6  *
7  * Partial copy of Linus' read cache modifications to fs/nfs/file.c
8  * modified for async RPC by okir@monad.swb.de
9  */
10 
11 #include <linux/time.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/fcntl.h>
15 #include <linux/stat.h>
16 #include <linux/mm.h>
17 #include <linux/slab.h>
18 #include <linux/pagemap.h>
19 #include <linux/sunrpc/clnt.h>
20 #include <linux/nfs_fs.h>
21 #include <linux/nfs_page.h>
22 #include <linux/module.h>
23 
24 #include "nfs4_fs.h"
25 #include "internal.h"
26 #include "iostat.h"
27 #include "fscache.h"
28 #include "pnfs.h"
29 #include "nfstrace.h"
30 
31 #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
32 
33 static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops;
34 static const struct nfs_rw_ops nfs_rw_read_ops;
35 
36 static struct kmem_cache *nfs_rdata_cachep;
37 
38 static struct nfs_pgio_header *nfs_readhdr_alloc(void)
39 {
40 	struct nfs_pgio_header *p = kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL);
41 
42 	if (p)
43 		p->rw_mode = FMODE_READ;
44 	return p;
45 }
46 
47 static void nfs_readhdr_free(struct nfs_pgio_header *rhdr)
48 {
49 	kmem_cache_free(nfs_rdata_cachep, rhdr);
50 }
51 
52 static int nfs_return_empty_folio(struct folio *folio)
53 {
54 	folio_zero_segment(folio, 0, folio_size(folio));
55 	folio_mark_uptodate(folio);
56 	folio_unlock(folio);
57 	return 0;
58 }
59 
60 void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
61 			      struct inode *inode, bool force_mds,
62 			      const struct nfs_pgio_completion_ops *compl_ops)
63 {
64 	struct nfs_server *server = NFS_SERVER(inode);
65 	const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
66 
67 #ifdef CONFIG_NFS_V4_1
68 	if (server->pnfs_curr_ld && !force_mds)
69 		pg_ops = server->pnfs_curr_ld->pg_read_ops;
70 #endif
71 	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_read_ops,
72 			server->rsize, 0);
73 }
74 EXPORT_SYMBOL_GPL(nfs_pageio_init_read);
75 
76 static void nfs_pageio_complete_read(struct nfs_pageio_descriptor *pgio)
77 {
78 	struct nfs_pgio_mirror *pgm;
79 	unsigned long npages;
80 
81 	nfs_pageio_complete(pgio);
82 
83 	/* It doesn't make sense to do mirrored reads! */
84 	WARN_ON_ONCE(pgio->pg_mirror_count != 1);
85 
86 	pgm = &pgio->pg_mirrors[0];
87 	NFS_I(pgio->pg_inode)->read_io += pgm->pg_bytes_written;
88 	npages = (pgm->pg_bytes_written + PAGE_SIZE - 1) >> PAGE_SHIFT;
89 	nfs_add_stats(pgio->pg_inode, NFSIOS_READPAGES, npages);
90 }
91 
92 
93 void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio)
94 {
95 	struct nfs_pgio_mirror *mirror;
96 
97 	if (pgio->pg_ops && pgio->pg_ops->pg_cleanup)
98 		pgio->pg_ops->pg_cleanup(pgio);
99 
100 	pgio->pg_ops = &nfs_pgio_rw_ops;
101 
102 	/* read path should never have more than one mirror */
103 	WARN_ON_ONCE(pgio->pg_mirror_count != 1);
104 
105 	mirror = &pgio->pg_mirrors[0];
106 	mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize;
107 }
108 EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
109 
110 static void nfs_readpage_release(struct nfs_page *req, int error)
111 {
112 	struct inode *inode = d_inode(nfs_req_openctx(req)->dentry);
113 	struct folio *folio = nfs_page_to_folio(req);
114 
115 	dprintk("NFS: read done (%s/%llu %d@%lld)\n", inode->i_sb->s_id,
116 		(unsigned long long)NFS_FILEID(inode), req->wb_bytes,
117 		(long long)req_offset(req));
118 
119 	if (nfs_error_is_fatal_on_server(error) && error != -ETIMEDOUT)
120 		folio_set_error(folio);
121 	if (nfs_page_group_sync_on_bit(req, PG_UNLOCKPAGE)) {
122 		if (folio_test_uptodate(folio))
123 			nfs_fscache_write_page(inode, &folio->page);
124 		folio_unlock(folio);
125 	}
126 	nfs_release_request(req);
127 }
128 
129 struct nfs_readdesc {
130 	struct nfs_pageio_descriptor pgio;
131 	struct nfs_open_context *ctx;
132 };
133 
134 static void nfs_page_group_set_uptodate(struct nfs_page *req)
135 {
136 	if (nfs_page_group_sync_on_bit(req, PG_UPTODATE))
137 		folio_mark_uptodate(nfs_page_to_folio(req));
138 }
139 
140 static void nfs_read_completion(struct nfs_pgio_header *hdr)
141 {
142 	unsigned long bytes = 0;
143 	int error;
144 
145 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
146 		goto out;
147 	while (!list_empty(&hdr->pages)) {
148 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
149 		struct folio *folio = nfs_page_to_folio(req);
150 		unsigned long start = req->wb_pgbase;
151 		unsigned long end = req->wb_pgbase + req->wb_bytes;
152 
153 		if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
154 			/* note: regions of the page not covered by a
155 			 * request are zeroed in readpage_async_filler */
156 			if (bytes > hdr->good_bytes) {
157 				/* nothing in this request was good, so zero
158 				 * the full extent of the request */
159 				folio_zero_segment(folio, start, end);
160 
161 			} else if (hdr->good_bytes - bytes < req->wb_bytes) {
162 				/* part of this request has good bytes, but
163 				 * not all. zero the bad bytes */
164 				start += hdr->good_bytes - bytes;
165 				WARN_ON(start < req->wb_pgbase);
166 				folio_zero_segment(folio, start, end);
167 			}
168 		}
169 		error = 0;
170 		bytes += req->wb_bytes;
171 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
172 			if (bytes <= hdr->good_bytes)
173 				nfs_page_group_set_uptodate(req);
174 			else {
175 				error = hdr->error;
176 				xchg(&nfs_req_openctx(req)->error, error);
177 			}
178 		} else
179 			nfs_page_group_set_uptodate(req);
180 		nfs_list_remove_request(req);
181 		nfs_readpage_release(req, error);
182 	}
183 out:
184 	hdr->release(hdr);
185 }
186 
187 static void nfs_initiate_read(struct nfs_pgio_header *hdr,
188 			      struct rpc_message *msg,
189 			      const struct nfs_rpc_ops *rpc_ops,
190 			      struct rpc_task_setup *task_setup_data, int how)
191 {
192 	rpc_ops->read_setup(hdr, msg);
193 	trace_nfs_initiate_read(hdr);
194 }
195 
196 static void
197 nfs_async_read_error(struct list_head *head, int error)
198 {
199 	struct nfs_page	*req;
200 
201 	while (!list_empty(head)) {
202 		req = nfs_list_entry(head->next);
203 		nfs_list_remove_request(req);
204 		nfs_readpage_release(req, error);
205 	}
206 }
207 
208 static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
209 	.error_cleanup = nfs_async_read_error,
210 	.completion = nfs_read_completion,
211 };
212 
213 /*
214  * This is the callback from RPC telling us whether a reply was
215  * received or some error occurred (timeout or socket shutdown).
216  */
217 static int nfs_readpage_done(struct rpc_task *task,
218 			     struct nfs_pgio_header *hdr,
219 			     struct inode *inode)
220 {
221 	int status = NFS_PROTO(inode)->read_done(task, hdr);
222 	if (status != 0)
223 		return status;
224 
225 	nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, hdr->res.count);
226 	trace_nfs_readpage_done(task, hdr);
227 
228 	if (task->tk_status == -ESTALE) {
229 		nfs_set_inode_stale(inode);
230 		nfs_mark_for_revalidate(inode);
231 	}
232 	return 0;
233 }
234 
235 static void nfs_readpage_retry(struct rpc_task *task,
236 			       struct nfs_pgio_header *hdr)
237 {
238 	struct nfs_pgio_args *argp = &hdr->args;
239 	struct nfs_pgio_res  *resp = &hdr->res;
240 
241 	/* This is a short read! */
242 	nfs_inc_stats(hdr->inode, NFSIOS_SHORTREAD);
243 	trace_nfs_readpage_short(task, hdr);
244 
245 	/* Has the server at least made some progress? */
246 	if (resp->count == 0) {
247 		nfs_set_pgio_error(hdr, -EIO, argp->offset);
248 		return;
249 	}
250 
251 	/* For non rpc-based layout drivers, retry-through-MDS */
252 	if (!task->tk_ops) {
253 		hdr->pnfs_error = -EAGAIN;
254 		return;
255 	}
256 
257 	/* Yes, so retry the read at the end of the hdr */
258 	hdr->mds_offset += resp->count;
259 	argp->offset += resp->count;
260 	argp->pgbase += resp->count;
261 	argp->count -= resp->count;
262 	resp->count = 0;
263 	resp->eof = 0;
264 	rpc_restart_call_prepare(task);
265 }
266 
267 static void nfs_readpage_result(struct rpc_task *task,
268 				struct nfs_pgio_header *hdr)
269 {
270 	if (hdr->res.eof) {
271 		loff_t pos = hdr->args.offset + hdr->res.count;
272 		unsigned int new = pos - hdr->io_start;
273 
274 		if (hdr->good_bytes > new) {
275 			hdr->good_bytes = new;
276 			set_bit(NFS_IOHDR_EOF, &hdr->flags);
277 			clear_bit(NFS_IOHDR_ERROR, &hdr->flags);
278 		}
279 	} else if (hdr->res.count < hdr->args.count)
280 		nfs_readpage_retry(task, hdr);
281 }
282 
283 static int readpage_async_filler(struct nfs_readdesc *desc, struct folio *folio)
284 {
285 	struct inode *inode = folio_file_mapping(folio)->host;
286 	struct nfs_server *server = NFS_SERVER(inode);
287 	size_t fsize = folio_size(folio);
288 	unsigned int rsize = server->rsize;
289 	struct nfs_page *new;
290 	unsigned int len, aligned_len;
291 	int error;
292 
293 	len = nfs_folio_length(folio);
294 	if (len == 0)
295 		return nfs_return_empty_folio(folio);
296 
297 	aligned_len = min_t(unsigned int, ALIGN(len, rsize), fsize);
298 
299 	if (!IS_SYNC(inode)) {
300 		error = nfs_fscache_read_page(inode, &folio->page);
301 		if (error == 0)
302 			goto out_unlock;
303 	}
304 
305 	new = nfs_page_create_from_folio(desc->ctx, folio, 0, aligned_len);
306 	if (IS_ERR(new))
307 		goto out_error;
308 
309 	if (len < fsize)
310 		folio_zero_segment(folio, len, fsize);
311 	if (!nfs_pageio_add_request(&desc->pgio, new)) {
312 		nfs_list_remove_request(new);
313 		error = desc->pgio.pg_error;
314 		nfs_readpage_release(new, error);
315 		goto out;
316 	}
317 	return 0;
318 out_error:
319 	error = PTR_ERR(new);
320 out_unlock:
321 	folio_unlock(folio);
322 out:
323 	return error;
324 }
325 
326 /*
327  * Read a page over NFS.
328  * We read the page synchronously in the following case:
329  *  -	The error flag is set for this page. This happens only when a
330  *	previous async read operation failed.
331  */
332 int nfs_read_folio(struct file *file, struct folio *folio)
333 {
334 	struct nfs_readdesc desc;
335 	struct inode *inode = file_inode(file);
336 	int ret;
337 
338 	trace_nfs_aop_readpage(inode, folio);
339 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
340 
341 	/*
342 	 * Try to flush any pending writes to the file..
343 	 *
344 	 * NOTE! Because we own the folio lock, there cannot
345 	 * be any new pending writes generated at this point
346 	 * for this folio (other folios can be written to).
347 	 */
348 	ret = nfs_wb_folio(inode, folio);
349 	if (ret)
350 		goto out_unlock;
351 	if (folio_test_uptodate(folio))
352 		goto out_unlock;
353 
354 	ret = -ESTALE;
355 	if (NFS_STALE(inode))
356 		goto out_unlock;
357 
358 	desc.ctx = get_nfs_open_context(nfs_file_open_context(file));
359 
360 	xchg(&desc.ctx->error, 0);
361 	nfs_pageio_init_read(&desc.pgio, inode, false,
362 			     &nfs_async_read_completion_ops);
363 
364 	ret = readpage_async_filler(&desc, folio);
365 	if (ret)
366 		goto out;
367 
368 	nfs_pageio_complete_read(&desc.pgio);
369 	ret = desc.pgio.pg_error < 0 ? desc.pgio.pg_error : 0;
370 	if (!ret) {
371 		ret = folio_wait_locked_killable(folio);
372 		if (!folio_test_uptodate(folio) && !ret)
373 			ret = xchg(&desc.ctx->error, 0);
374 	}
375 out:
376 	put_nfs_open_context(desc.ctx);
377 	trace_nfs_aop_readpage_done(inode, folio, ret);
378 	return ret;
379 out_unlock:
380 	folio_unlock(folio);
381 	trace_nfs_aop_readpage_done(inode, folio, ret);
382 	return ret;
383 }
384 
385 void nfs_readahead(struct readahead_control *ractl)
386 {
387 	unsigned int nr_pages = readahead_count(ractl);
388 	struct file *file = ractl->file;
389 	struct nfs_readdesc desc;
390 	struct inode *inode = ractl->mapping->host;
391 	struct folio *folio;
392 	int ret;
393 
394 	trace_nfs_aop_readahead(inode, readahead_pos(ractl), nr_pages);
395 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
396 
397 	ret = -ESTALE;
398 	if (NFS_STALE(inode))
399 		goto out;
400 
401 	if (file == NULL) {
402 		ret = -EBADF;
403 		desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
404 		if (desc.ctx == NULL)
405 			goto out;
406 	} else
407 		desc.ctx = get_nfs_open_context(nfs_file_open_context(file));
408 
409 	nfs_pageio_init_read(&desc.pgio, inode, false,
410 			     &nfs_async_read_completion_ops);
411 
412 	while ((folio = readahead_folio(ractl)) != NULL) {
413 		ret = readpage_async_filler(&desc, folio);
414 		if (ret)
415 			break;
416 	}
417 
418 	nfs_pageio_complete_read(&desc.pgio);
419 
420 	put_nfs_open_context(desc.ctx);
421 out:
422 	trace_nfs_aop_readahead_done(inode, nr_pages, ret);
423 }
424 
425 int __init nfs_init_readpagecache(void)
426 {
427 	nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
428 					     sizeof(struct nfs_pgio_header),
429 					     0, SLAB_HWCACHE_ALIGN,
430 					     NULL);
431 	if (nfs_rdata_cachep == NULL)
432 		return -ENOMEM;
433 
434 	return 0;
435 }
436 
437 void nfs_destroy_readpagecache(void)
438 {
439 	kmem_cache_destroy(nfs_rdata_cachep);
440 }
441 
442 static const struct nfs_rw_ops nfs_rw_read_ops = {
443 	.rw_alloc_header	= nfs_readhdr_alloc,
444 	.rw_free_header		= nfs_readhdr_free,
445 	.rw_done		= nfs_readpage_done,
446 	.rw_result		= nfs_readpage_result,
447 	.rw_initiate		= nfs_initiate_read,
448 };
449