b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 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/pagemap.h> |
| 19 | #include <linux/sunrpc/clnt.h> |
| 20 | #include <linux/nfs_fs.h> |
| 21 | #include <linux/nfs_page.h> |
| 22 | #include <linux/module.h> |
| 23 | |
| 24 | #include "nfs4_fs.h" |
| 25 | #include "internal.h" |
| 26 | #include "iostat.h" |
| 27 | #include "fscache.h" |
| 28 | #include "pnfs.h" |
| 29 | #include "nfstrace.h" |
| 30 | |
| 31 | #define NFSDBG_FACILITY NFSDBG_PAGECACHE |
| 32 | |
| 33 | static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops; |
| 34 | static const struct nfs_rw_ops nfs_rw_read_ops; |
| 35 | |
| 36 | static struct kmem_cache *nfs_rdata_cachep; |
| 37 | |
| 38 | static struct nfs_pgio_header *nfs_readhdr_alloc(void) |
| 39 | { |
| 40 | struct nfs_pgio_header *p = kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL); |
| 41 | |
| 42 | if (p) |
| 43 | p->rw_mode = FMODE_READ; |
| 44 | return p; |
| 45 | } |
| 46 | |
| 47 | static void nfs_readhdr_free(struct nfs_pgio_header *rhdr) |
| 48 | { |
| 49 | kmem_cache_free(nfs_rdata_cachep, rhdr); |
| 50 | } |
| 51 | |
| 52 | static |
| 53 | int nfs_return_empty_page(struct page *page) |
| 54 | { |
| 55 | zero_user(page, 0, PAGE_SIZE); |
| 56 | SetPageUptodate(page); |
| 57 | unlock_page(page); |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, |
| 62 | struct inode *inode, bool force_mds, |
| 63 | const struct nfs_pgio_completion_ops *compl_ops) |
| 64 | { |
| 65 | struct nfs_server *server = NFS_SERVER(inode); |
| 66 | const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops; |
| 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 |
| 72 | nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_read_ops, |
| 73 | server->rsize, 0); |
| 74 | } |
| 75 | EXPORT_SYMBOL_GPL(nfs_pageio_init_read); |
| 76 | |
| 77 | void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio) |
| 78 | { |
| 79 | struct nfs_pgio_mirror *mirror; |
| 80 | |
| 81 | if (pgio->pg_ops && pgio->pg_ops->pg_cleanup) |
| 82 | pgio->pg_ops->pg_cleanup(pgio); |
| 83 | |
| 84 | pgio->pg_ops = &nfs_pgio_rw_ops; |
| 85 | |
| 86 | /* read path should never have more than one mirror */ |
| 87 | WARN_ON_ONCE(pgio->pg_mirror_count != 1); |
| 88 | |
| 89 | mirror = &pgio->pg_mirrors[0]; |
| 90 | mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize; |
| 91 | } |
| 92 | EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds); |
| 93 | |
| 94 | static void nfs_readpage_release(struct nfs_page *req, int error) |
| 95 | { |
| 96 | struct inode *inode = d_inode(nfs_req_openctx(req)->dentry); |
| 97 | struct page *page = req->wb_page; |
| 98 | |
| 99 | dprintk("NFS: read done (%s/%llu %d@%lld)\n", inode->i_sb->s_id, |
| 100 | (unsigned long long)NFS_FILEID(inode), req->wb_bytes, |
| 101 | (long long)req_offset(req)); |
| 102 | |
| 103 | if (nfs_error_is_fatal_on_server(error) && error != -ETIMEDOUT) |
| 104 | SetPageError(page); |
| 105 | if (nfs_page_group_sync_on_bit(req, PG_UNLOCKPAGE)) { |
| 106 | if (PageUptodate(page)) |
| 107 | nfs_readpage_to_fscache(inode, page, 0); |
| 108 | unlock_page(page); |
| 109 | } |
| 110 | nfs_release_request(req); |
| 111 | } |
| 112 | |
| 113 | int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode, |
| 114 | struct page *page) |
| 115 | { |
| 116 | struct nfs_page *new; |
| 117 | unsigned int len; |
| 118 | struct nfs_pageio_descriptor pgio; |
| 119 | struct nfs_pgio_mirror *pgm; |
| 120 | |
| 121 | len = nfs_page_length(page); |
| 122 | if (len == 0) |
| 123 | return nfs_return_empty_page(page); |
| 124 | new = nfs_create_request(ctx, page, 0, len); |
| 125 | if (IS_ERR(new)) { |
| 126 | unlock_page(page); |
| 127 | return PTR_ERR(new); |
| 128 | } |
| 129 | if (len < PAGE_SIZE) |
| 130 | zero_user_segment(page, len, PAGE_SIZE); |
| 131 | |
| 132 | nfs_pageio_init_read(&pgio, inode, false, |
| 133 | &nfs_async_read_completion_ops); |
| 134 | if (!nfs_pageio_add_request(&pgio, new)) { |
| 135 | nfs_list_remove_request(new); |
| 136 | nfs_readpage_release(new, pgio.pg_error); |
| 137 | } |
| 138 | nfs_pageio_complete(&pgio); |
| 139 | |
| 140 | /* It doesn't make sense to do mirrored reads! */ |
| 141 | WARN_ON_ONCE(pgio.pg_mirror_count != 1); |
| 142 | |
| 143 | pgm = &pgio.pg_mirrors[0]; |
| 144 | NFS_I(inode)->read_io += pgm->pg_bytes_written; |
| 145 | |
| 146 | return pgio.pg_error < 0 ? pgio.pg_error : 0; |
| 147 | } |
| 148 | |
| 149 | static void nfs_page_group_set_uptodate(struct nfs_page *req) |
| 150 | { |
| 151 | if (nfs_page_group_sync_on_bit(req, PG_UPTODATE)) |
| 152 | SetPageUptodate(req->wb_page); |
| 153 | } |
| 154 | |
| 155 | static void nfs_read_completion(struct nfs_pgio_header *hdr) |
| 156 | { |
| 157 | unsigned long bytes = 0; |
| 158 | int error; |
| 159 | |
| 160 | if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) |
| 161 | goto out; |
| 162 | while (!list_empty(&hdr->pages)) { |
| 163 | struct nfs_page *req = nfs_list_entry(hdr->pages.next); |
| 164 | struct page *page = req->wb_page; |
| 165 | unsigned long start = req->wb_pgbase; |
| 166 | unsigned long end = req->wb_pgbase + req->wb_bytes; |
| 167 | |
| 168 | if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) { |
| 169 | /* note: regions of the page not covered by a |
| 170 | * request are zeroed in nfs_readpage_async / |
| 171 | * readpage_async_filler */ |
| 172 | if (bytes > hdr->good_bytes) { |
| 173 | /* nothing in this request was good, so zero |
| 174 | * the full extent of the request */ |
| 175 | zero_user_segment(page, start, end); |
| 176 | |
| 177 | } else if (hdr->good_bytes - bytes < req->wb_bytes) { |
| 178 | /* part of this request has good bytes, but |
| 179 | * not all. zero the bad bytes */ |
| 180 | start += hdr->good_bytes - bytes; |
| 181 | WARN_ON(start < req->wb_pgbase); |
| 182 | zero_user_segment(page, start, end); |
| 183 | } |
| 184 | } |
| 185 | error = 0; |
| 186 | bytes += req->wb_bytes; |
| 187 | if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) { |
| 188 | if (bytes <= hdr->good_bytes) |
| 189 | nfs_page_group_set_uptodate(req); |
| 190 | else { |
| 191 | error = hdr->error; |
| 192 | xchg(&nfs_req_openctx(req)->error, error); |
| 193 | } |
| 194 | } else |
| 195 | nfs_page_group_set_uptodate(req); |
| 196 | nfs_list_remove_request(req); |
| 197 | nfs_readpage_release(req, error); |
| 198 | } |
| 199 | out: |
| 200 | hdr->release(hdr); |
| 201 | } |
| 202 | |
| 203 | static void nfs_initiate_read(struct nfs_pgio_header *hdr, |
| 204 | struct rpc_message *msg, |
| 205 | const struct nfs_rpc_ops *rpc_ops, |
| 206 | struct rpc_task_setup *task_setup_data, int how) |
| 207 | { |
| 208 | struct inode *inode = hdr->inode; |
| 209 | int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0; |
| 210 | |
| 211 | task_setup_data->flags |= swap_flags; |
| 212 | rpc_ops->read_setup(hdr, msg); |
| 213 | trace_nfs_initiate_read(inode, hdr->io_start, hdr->good_bytes); |
| 214 | } |
| 215 | |
| 216 | static void |
| 217 | nfs_async_read_error(struct list_head *head, int error) |
| 218 | { |
| 219 | struct nfs_page *req; |
| 220 | |
| 221 | while (!list_empty(head)) { |
| 222 | req = nfs_list_entry(head->next); |
| 223 | nfs_list_remove_request(req); |
| 224 | nfs_readpage_release(req, error); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = { |
| 229 | .error_cleanup = nfs_async_read_error, |
| 230 | .completion = nfs_read_completion, |
| 231 | }; |
| 232 | |
| 233 | /* |
| 234 | * This is the callback from RPC telling us whether a reply was |
| 235 | * received or some error occurred (timeout or socket shutdown). |
| 236 | */ |
| 237 | static int nfs_readpage_done(struct rpc_task *task, |
| 238 | struct nfs_pgio_header *hdr, |
| 239 | struct inode *inode) |
| 240 | { |
| 241 | int status = NFS_PROTO(inode)->read_done(task, hdr); |
| 242 | if (status != 0) |
| 243 | return status; |
| 244 | |
| 245 | nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, hdr->res.count); |
| 246 | trace_nfs_readpage_done(inode, task->tk_status, |
| 247 | hdr->args.offset, hdr->res.eof); |
| 248 | |
| 249 | if (task->tk_status == -ESTALE) { |
| 250 | set_bit(NFS_INO_STALE, &NFS_I(inode)->flags); |
| 251 | nfs_mark_for_revalidate(inode); |
| 252 | } |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static void nfs_readpage_retry(struct rpc_task *task, |
| 257 | struct nfs_pgio_header *hdr) |
| 258 | { |
| 259 | struct nfs_pgio_args *argp = &hdr->args; |
| 260 | struct nfs_pgio_res *resp = &hdr->res; |
| 261 | |
| 262 | /* This is a short read! */ |
| 263 | nfs_inc_stats(hdr->inode, NFSIOS_SHORTREAD); |
| 264 | /* Has the server at least made some progress? */ |
| 265 | if (resp->count == 0) { |
| 266 | nfs_set_pgio_error(hdr, -EIO, argp->offset); |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | /* For non rpc-based layout drivers, retry-through-MDS */ |
| 271 | if (!task->tk_ops) { |
| 272 | hdr->pnfs_error = -EAGAIN; |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | /* Yes, so retry the read at the end of the hdr */ |
| 277 | hdr->mds_offset += resp->count; |
| 278 | argp->offset += resp->count; |
| 279 | argp->pgbase += resp->count; |
| 280 | argp->count -= resp->count; |
| 281 | rpc_restart_call_prepare(task); |
| 282 | } |
| 283 | |
| 284 | static void nfs_readpage_result(struct rpc_task *task, |
| 285 | struct nfs_pgio_header *hdr) |
| 286 | { |
| 287 | if (hdr->res.eof) { |
| 288 | loff_t pos = hdr->args.offset + hdr->res.count; |
| 289 | unsigned int new = pos - hdr->io_start; |
| 290 | |
| 291 | if (hdr->good_bytes > new) { |
| 292 | hdr->good_bytes = new; |
| 293 | set_bit(NFS_IOHDR_EOF, &hdr->flags); |
| 294 | clear_bit(NFS_IOHDR_ERROR, &hdr->flags); |
| 295 | } |
| 296 | } else if (hdr->res.count < hdr->args.count) |
| 297 | nfs_readpage_retry(task, hdr); |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * Read a page over NFS. |
| 302 | * We read the page synchronously in the following case: |
| 303 | * - The error flag is set for this page. This happens only when a |
| 304 | * previous async read operation failed. |
| 305 | */ |
| 306 | int nfs_readpage(struct file *file, struct page *page) |
| 307 | { |
| 308 | struct nfs_open_context *ctx; |
| 309 | struct inode *inode = page_file_mapping(page)->host; |
| 310 | int error; |
| 311 | |
| 312 | dprintk("NFS: nfs_readpage (%p %ld@%lu)\n", |
| 313 | page, PAGE_SIZE, page_index(page)); |
| 314 | nfs_inc_stats(inode, NFSIOS_VFSREADPAGE); |
| 315 | nfs_add_stats(inode, NFSIOS_READPAGES, 1); |
| 316 | |
| 317 | /* |
| 318 | * Try to flush any pending writes to the file.. |
| 319 | * |
| 320 | * NOTE! Because we own the page lock, there cannot |
| 321 | * be any new pending writes generated at this point |
| 322 | * for this page (other pages can be written to). |
| 323 | */ |
| 324 | error = nfs_wb_page(inode, page); |
| 325 | if (error) |
| 326 | goto out_unlock; |
| 327 | if (PageUptodate(page)) |
| 328 | goto out_unlock; |
| 329 | |
| 330 | error = -ESTALE; |
| 331 | if (NFS_STALE(inode)) |
| 332 | goto out_unlock; |
| 333 | |
| 334 | if (file == NULL) { |
| 335 | error = -EBADF; |
| 336 | ctx = nfs_find_open_context(inode, NULL, FMODE_READ); |
| 337 | if (ctx == NULL) |
| 338 | goto out_unlock; |
| 339 | } else |
| 340 | ctx = get_nfs_open_context(nfs_file_open_context(file)); |
| 341 | |
| 342 | if (!IS_SYNC(inode)) { |
| 343 | error = nfs_readpage_from_fscache(ctx, inode, page); |
| 344 | if (error == 0) |
| 345 | goto out; |
| 346 | } |
| 347 | |
| 348 | xchg(&ctx->error, 0); |
| 349 | error = nfs_readpage_async(ctx, inode, page); |
| 350 | if (!error) { |
| 351 | error = wait_on_page_locked_killable(page); |
| 352 | if (!PageUptodate(page) && !error) |
| 353 | error = xchg(&ctx->error, 0); |
| 354 | } |
| 355 | out: |
| 356 | put_nfs_open_context(ctx); |
| 357 | return error; |
| 358 | out_unlock: |
| 359 | unlock_page(page); |
| 360 | return error; |
| 361 | } |
| 362 | |
| 363 | struct nfs_readdesc { |
| 364 | struct nfs_pageio_descriptor *pgio; |
| 365 | struct nfs_open_context *ctx; |
| 366 | }; |
| 367 | |
| 368 | static int |
| 369 | readpage_async_filler(void *data, struct page *page) |
| 370 | { |
| 371 | struct nfs_readdesc *desc = (struct nfs_readdesc *)data; |
| 372 | struct nfs_page *new; |
| 373 | unsigned int len; |
| 374 | int error; |
| 375 | |
| 376 | len = nfs_page_length(page); |
| 377 | if (len == 0) |
| 378 | return nfs_return_empty_page(page); |
| 379 | |
| 380 | new = nfs_create_request(desc->ctx, page, 0, len); |
| 381 | if (IS_ERR(new)) |
| 382 | goto out_error; |
| 383 | |
| 384 | if (len < PAGE_SIZE) |
| 385 | zero_user_segment(page, len, PAGE_SIZE); |
| 386 | if (!nfs_pageio_add_request(desc->pgio, new)) { |
| 387 | nfs_list_remove_request(new); |
| 388 | error = desc->pgio->pg_error; |
| 389 | nfs_readpage_release(new, error); |
| 390 | goto out; |
| 391 | } |
| 392 | return 0; |
| 393 | out_error: |
| 394 | error = PTR_ERR(new); |
| 395 | unlock_page(page); |
| 396 | out: |
| 397 | return error; |
| 398 | } |
| 399 | |
| 400 | int nfs_readpages(struct file *filp, struct address_space *mapping, |
| 401 | struct list_head *pages, unsigned nr_pages) |
| 402 | { |
| 403 | struct nfs_pageio_descriptor pgio; |
| 404 | struct nfs_pgio_mirror *pgm; |
| 405 | struct nfs_readdesc desc = { |
| 406 | .pgio = &pgio, |
| 407 | }; |
| 408 | struct inode *inode = mapping->host; |
| 409 | unsigned long npages; |
| 410 | int ret = -ESTALE; |
| 411 | |
| 412 | dprintk("NFS: nfs_readpages (%s/%Lu %d)\n", |
| 413 | inode->i_sb->s_id, |
| 414 | (unsigned long long)NFS_FILEID(inode), |
| 415 | nr_pages); |
| 416 | nfs_inc_stats(inode, NFSIOS_VFSREADPAGES); |
| 417 | |
| 418 | if (NFS_STALE(inode)) |
| 419 | goto out; |
| 420 | |
| 421 | if (filp == NULL) { |
| 422 | desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ); |
| 423 | if (desc.ctx == NULL) |
| 424 | return -EBADF; |
| 425 | } else |
| 426 | desc.ctx = get_nfs_open_context(nfs_file_open_context(filp)); |
| 427 | |
| 428 | /* attempt to read as many of the pages as possible from the cache |
| 429 | * - this returns -ENOBUFS immediately if the cookie is negative |
| 430 | */ |
| 431 | ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping, |
| 432 | pages, &nr_pages); |
| 433 | if (ret == 0) |
| 434 | goto read_complete; /* all pages were read */ |
| 435 | |
| 436 | nfs_pageio_init_read(&pgio, inode, false, |
| 437 | &nfs_async_read_completion_ops); |
| 438 | |
| 439 | ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc); |
| 440 | nfs_pageio_complete(&pgio); |
| 441 | |
| 442 | /* It doesn't make sense to do mirrored reads! */ |
| 443 | WARN_ON_ONCE(pgio.pg_mirror_count != 1); |
| 444 | |
| 445 | pgm = &pgio.pg_mirrors[0]; |
| 446 | NFS_I(inode)->read_io += pgm->pg_bytes_written; |
| 447 | npages = (pgm->pg_bytes_written + PAGE_SIZE - 1) >> |
| 448 | PAGE_SHIFT; |
| 449 | nfs_add_stats(inode, NFSIOS_READPAGES, npages); |
| 450 | read_complete: |
| 451 | put_nfs_open_context(desc.ctx); |
| 452 | out: |
| 453 | return ret; |
| 454 | } |
| 455 | |
| 456 | int __init nfs_init_readpagecache(void) |
| 457 | { |
| 458 | nfs_rdata_cachep = kmem_cache_create("nfs_read_data", |
| 459 | sizeof(struct nfs_pgio_header), |
| 460 | 0, SLAB_HWCACHE_ALIGN, |
| 461 | NULL); |
| 462 | if (nfs_rdata_cachep == NULL) |
| 463 | return -ENOMEM; |
| 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | void nfs_destroy_readpagecache(void) |
| 469 | { |
| 470 | kmem_cache_destroy(nfs_rdata_cachep); |
| 471 | } |
| 472 | |
| 473 | static const struct nfs_rw_ops nfs_rw_read_ops = { |
| 474 | .rw_alloc_header = nfs_readhdr_alloc, |
| 475 | .rw_free_header = nfs_readhdr_free, |
| 476 | .rw_done = nfs_readpage_done, |
| 477 | .rw_result = nfs_readpage_result, |
| 478 | .rw_initiate = nfs_initiate_read, |
| 479 | }; |