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