6aad42fbf797a1caf5b9ebd63f4c6cd132176097
[releases.git] / read.c
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
32 #define NFSDBG_FACILITY         NFSDBG_PAGECACHE
33
34 static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops;
35 static const struct nfs_rw_ops nfs_rw_read_ops;
36
37 static struct kmem_cache *nfs_rdata_cachep;
38
39 static struct nfs_pgio_header *nfs_readhdr_alloc(void)
40 {
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;
46 }
47
48 static void nfs_readhdr_free(struct nfs_pgio_header *rhdr)
49 {
50         if (rhdr->res.scratch != NULL)
51                 kfree(rhdr->res.scratch);
52         kmem_cache_free(nfs_rdata_cachep, rhdr);
53 }
54
55 static
56 int nfs_return_empty_page(struct page *page)
57 {
58         zero_user(page, 0, PAGE_SIZE);
59         SetPageUptodate(page);
60         unlock_page(page);
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 #ifdef CONFIG_NFS_V4_1
72         if (server->pnfs_curr_ld && !force_mds)
73                 pg_ops = server->pnfs_curr_ld->pg_read_ops;
74 #endif
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 static 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 inode *inode = d_inode(nfs_req_openctx(req)->dentry);
125         struct page *page = req->wb_page;
126
127         dprintk("NFS: read done (%s/%llu %d@%lld)\n", inode->i_sb->s_id,
128                 (unsigned long long)NFS_FILEID(inode), req->wb_bytes,
129                 (long long)req_offset(req));
130
131         if (nfs_error_is_fatal_on_server(error) && error != -ETIMEDOUT)
132                 SetPageError(page);
133         if (nfs_page_group_sync_on_bit(req, PG_UNLOCKPAGE)) {
134                 if (PageUptodate(page))
135                         nfs_fscache_write_page(inode, page);
136                 unlock_page(page);
137         }
138         nfs_release_request(req);
139 }
140
141 struct nfs_readdesc {
142         struct nfs_pageio_descriptor pgio;
143         struct nfs_open_context *ctx;
144 };
145
146 static void nfs_page_group_set_uptodate(struct nfs_page *req)
147 {
148         if (nfs_page_group_sync_on_bit(req, PG_UPTODATE))
149                 SetPageUptodate(req->wb_page);
150 }
151
152 static void nfs_read_completion(struct nfs_pgio_header *hdr)
153 {
154         unsigned long bytes = 0;
155         int error;
156
157         if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
158                 goto out;
159         while (!list_empty(&hdr->pages)) {
160                 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
161                 struct page *page = req->wb_page;
162                 unsigned long start = req->wb_pgbase;
163                 unsigned long end = req->wb_pgbase + req->wb_bytes;
164
165                 if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
166                         /* note: regions of the page not covered by a
167                          * request are zeroed in readpage_async_filler */
168                         if (bytes > hdr->good_bytes) {
169                                 /* nothing in this request was good, so zero
170                                  * the full extent of the request */
171                                 zero_user_segment(page, start, end);
172
173                         } else if (hdr->good_bytes - bytes < req->wb_bytes) {
174                                 /* part of this request has good bytes, but
175                                  * not all. zero the bad bytes */
176                                 start += hdr->good_bytes - bytes;
177                                 WARN_ON(start < req->wb_pgbase);
178                                 zero_user_segment(page, start, end);
179                         }
180                 }
181                 error = 0;
182                 bytes += req->wb_bytes;
183                 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
184                         if (bytes <= hdr->good_bytes)
185                                 nfs_page_group_set_uptodate(req);
186                         else {
187                                 error = hdr->error;
188                                 xchg(&nfs_req_openctx(req)->error, error);
189                         }
190                 } else
191                         nfs_page_group_set_uptodate(req);
192                 nfs_list_remove_request(req);
193                 nfs_readpage_release(req, error);
194         }
195 out:
196         hdr->release(hdr);
197 }
198
199 static void nfs_initiate_read(struct nfs_pgio_header *hdr,
200                               struct rpc_message *msg,
201                               const struct nfs_rpc_ops *rpc_ops,
202                               struct rpc_task_setup *task_setup_data, int how)
203 {
204         rpc_ops->read_setup(hdr, msg);
205         trace_nfs_initiate_read(hdr);
206 }
207
208 static void
209 nfs_async_read_error(struct list_head *head, int error)
210 {
211         struct nfs_page *req;
212
213         while (!list_empty(head)) {
214                 req = nfs_list_entry(head->next);
215                 nfs_list_remove_request(req);
216                 nfs_readpage_release(req, error);
217         }
218 }
219
220 static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
221         .error_cleanup = nfs_async_read_error,
222         .completion = nfs_read_completion,
223 };
224
225 /*
226  * This is the callback from RPC telling us whether a reply was
227  * received or some error occurred (timeout or socket shutdown).
228  */
229 static int nfs_readpage_done(struct rpc_task *task,
230                              struct nfs_pgio_header *hdr,
231                              struct inode *inode)
232 {
233         int status = NFS_PROTO(inode)->read_done(task, hdr);
234         if (status != 0)
235                 return status;
236
237         nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, hdr->res.count);
238         trace_nfs_readpage_done(task, hdr);
239
240         if (task->tk_status == -ESTALE) {
241                 nfs_set_inode_stale(inode);
242                 nfs_mark_for_revalidate(inode);
243         }
244         return 0;
245 }
246
247 static void nfs_readpage_retry(struct rpc_task *task,
248                                struct nfs_pgio_header *hdr)
249 {
250         struct nfs_pgio_args *argp = &hdr->args;
251         struct nfs_pgio_res  *resp = &hdr->res;
252
253         /* This is a short read! */
254         nfs_inc_stats(hdr->inode, NFSIOS_SHORTREAD);
255         trace_nfs_readpage_short(task, hdr);
256
257         /* Has the server at least made some progress? */
258         if (resp->count == 0) {
259                 nfs_set_pgio_error(hdr, -EIO, argp->offset);
260                 return;
261         }
262
263         /* For non rpc-based layout drivers, retry-through-MDS */
264         if (!task->tk_ops) {
265                 hdr->pnfs_error = -EAGAIN;
266                 return;
267         }
268
269         /* Yes, so retry the read at the end of the hdr */
270         hdr->mds_offset += resp->count;
271         argp->offset += resp->count;
272         argp->pgbase += resp->count;
273         argp->count -= resp->count;
274         resp->count = 0;
275         resp->eof = 0;
276         rpc_restart_call_prepare(task);
277 }
278
279 static void nfs_readpage_result(struct rpc_task *task,
280                                 struct nfs_pgio_header *hdr)
281 {
282         if (hdr->res.eof) {
283                 loff_t pos = hdr->args.offset + hdr->res.count;
284                 unsigned int new = pos - hdr->io_start;
285
286                 if (hdr->good_bytes > new) {
287                         hdr->good_bytes = new;
288                         set_bit(NFS_IOHDR_EOF, &hdr->flags);
289                         clear_bit(NFS_IOHDR_ERROR, &hdr->flags);
290                 }
291         } else if (hdr->res.count < hdr->args.count)
292                 nfs_readpage_retry(task, hdr);
293 }
294
295 static int
296 readpage_async_filler(struct nfs_readdesc *desc, struct page *page)
297 {
298         struct inode *inode = page_file_mapping(page)->host;
299         unsigned int rsize = NFS_SERVER(inode)->rsize;
300         struct nfs_page *new;
301         unsigned int len, aligned_len;
302         int error;
303
304         len = nfs_page_length(page);
305         if (len == 0)
306                 return nfs_return_empty_page(page);
307
308         aligned_len = min_t(unsigned int, ALIGN(len, rsize), PAGE_SIZE);
309
310         if (!IS_SYNC(page->mapping->host)) {
311                 error = nfs_fscache_read_page(page->mapping->host, page);
312                 if (error == 0)
313                         goto out_unlock;
314         }
315
316         new = nfs_create_request(desc->ctx, page, 0, aligned_len);
317         if (IS_ERR(new))
318                 goto out_error;
319
320         if (len < PAGE_SIZE)
321                 zero_user_segment(page, len, PAGE_SIZE);
322         if (!nfs_pageio_add_request(&desc->pgio, new)) {
323                 nfs_list_remove_request(new);
324                 error = desc->pgio.pg_error;
325                 nfs_readpage_release(new, error);
326                 goto out;
327         }
328         return 0;
329 out_error:
330         error = PTR_ERR(new);
331 out_unlock:
332         unlock_page(page);
333 out:
334         return error;
335 }
336
337 /*
338  * Read a page over NFS.
339  * We read the page synchronously in the following case:
340  *  -   The error flag is set for this page. This happens only when a
341  *      previous async read operation failed.
342  */
343 int nfs_read_folio(struct file *file, struct folio *folio)
344 {
345         struct page *page = &folio->page;
346         struct nfs_readdesc desc;
347         struct inode *inode = page_file_mapping(page)->host;
348         int ret;
349
350         trace_nfs_aop_readpage(inode, page);
351         nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
352         task_io_account_read(folio_size(folio));
353
354         /*
355          * Try to flush any pending writes to the file..
356          *
357          * NOTE! Because we own the page lock, there cannot
358          * be any new pending writes generated at this point
359          * for this page (other pages can be written to).
360          */
361         ret = nfs_wb_page(inode, page);
362         if (ret)
363                 goto out_unlock;
364         if (PageUptodate(page))
365                 goto out_unlock;
366
367         ret = -ESTALE;
368         if (NFS_STALE(inode))
369                 goto out_unlock;
370
371         if (file == NULL) {
372                 ret = -EBADF;
373                 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
374                 if (desc.ctx == NULL)
375                         goto out_unlock;
376         } else
377                 desc.ctx = get_nfs_open_context(nfs_file_open_context(file));
378
379         xchg(&desc.ctx->error, 0);
380         nfs_pageio_init_read(&desc.pgio, inode, false,
381                              &nfs_async_read_completion_ops);
382
383         ret = readpage_async_filler(&desc, page);
384         if (ret)
385                 goto out;
386
387         nfs_pageio_complete_read(&desc.pgio);
388         ret = desc.pgio.pg_error < 0 ? desc.pgio.pg_error : 0;
389         if (!ret) {
390                 ret = wait_on_page_locked_killable(page);
391                 if (!PageUptodate(page) && !ret)
392                         ret = xchg(&desc.ctx->error, 0);
393         }
394 out:
395         put_nfs_open_context(desc.ctx);
396         trace_nfs_aop_readpage_done(inode, page, ret);
397         return ret;
398 out_unlock:
399         unlock_page(page);
400         trace_nfs_aop_readpage_done(inode, page, ret);
401         return ret;
402 }
403
404 void nfs_readahead(struct readahead_control *ractl)
405 {
406         unsigned int nr_pages = readahead_count(ractl);
407         struct file *file = ractl->file;
408         struct nfs_readdesc desc;
409         struct inode *inode = ractl->mapping->host;
410         struct page *page;
411         int ret;
412
413         trace_nfs_aop_readahead(inode, readahead_pos(ractl), nr_pages);
414         nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
415         task_io_account_read(readahead_length(ractl));
416
417         ret = -ESTALE;
418         if (NFS_STALE(inode))
419                 goto out;
420
421         if (file == NULL) {
422                 ret = -EBADF;
423                 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
424                 if (desc.ctx == NULL)
425                         goto out;
426         } else
427                 desc.ctx = get_nfs_open_context(nfs_file_open_context(file));
428
429         nfs_pageio_init_read(&desc.pgio, inode, false,
430                              &nfs_async_read_completion_ops);
431
432         while ((page = readahead_page(ractl)) != NULL) {
433                 ret = readpage_async_filler(&desc, page);
434                 put_page(page);
435                 if (ret)
436                         break;
437         }
438
439         nfs_pageio_complete_read(&desc.pgio);
440
441         put_nfs_open_context(desc.ctx);
442 out:
443         trace_nfs_aop_readahead_done(inode, nr_pages, ret);
444 }
445
446 int __init nfs_init_readpagecache(void)
447 {
448         nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
449                                              sizeof(struct nfs_pgio_header),
450                                              0, SLAB_HWCACHE_ALIGN,
451                                              NULL);
452         if (nfs_rdata_cachep == NULL)
453                 return -ENOMEM;
454
455         return 0;
456 }
457
458 void nfs_destroy_readpagecache(void)
459 {
460         kmem_cache_destroy(nfs_rdata_cachep);
461 }
462
463 static const struct nfs_rw_ops nfs_rw_read_ops = {
464         .rw_alloc_header        = nfs_readhdr_alloc,
465         .rw_free_header         = nfs_readhdr_free,
466         .rw_done                = nfs_readpage_done,
467         .rw_result              = nfs_readpage_result,
468         .rw_initiate            = nfs_initiate_read,
469 };