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