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