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