1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/fs/nfs/pagelist.c 4 * 5 * A set of helper functions for managing NFS read and write requests. 6 * The main purpose of these routines is to provide support for the 7 * coalescing of several requests into a single RPC call. 8 * 9 * Copyright 2000, 2001 (c) Trond Myklebust <trond.myklebust@fys.uio.no> 10 * 11 */ 12 13 #include <linux/slab.h> 14 #include <linux/file.h> 15 #include <linux/sched.h> 16 #include <linux/sunrpc/clnt.h> 17 #include <linux/nfs.h> 18 #include <linux/nfs3.h> 19 #include <linux/nfs4.h> 20 #include <linux/nfs_fs.h> 21 #include <linux/nfs_page.h> 22 #include <linux/nfs_mount.h> 23 #include <linux/export.h> 24 #include <linux/filelock.h> 25 26 #include "internal.h" 27 #include "pnfs.h" 28 #include "nfstrace.h" 29 #include "fscache.h" 30 31 #define NFSDBG_FACILITY NFSDBG_PAGECACHE 32 33 static struct kmem_cache *nfs_page_cachep; 34 static const struct rpc_call_ops nfs_pgio_common_ops; 35 36 struct nfs_page_iter_page { 37 const struct nfs_page *req; 38 size_t count; 39 }; 40 41 static void nfs_page_iter_page_init(struct nfs_page_iter_page *i, 42 const struct nfs_page *req) 43 { 44 i->req = req; 45 i->count = 0; 46 } 47 48 static void nfs_page_iter_page_advance(struct nfs_page_iter_page *i, size_t sz) 49 { 50 const struct nfs_page *req = i->req; 51 size_t tmp = i->count + sz; 52 53 i->count = (tmp < req->wb_bytes) ? tmp : req->wb_bytes; 54 } 55 56 static struct page *nfs_page_iter_page_get(struct nfs_page_iter_page *i) 57 { 58 const struct nfs_page *req = i->req; 59 struct page *page; 60 61 if (i->count != req->wb_bytes) { 62 size_t base = i->count + req->wb_pgbase; 63 size_t len = PAGE_SIZE - offset_in_page(base); 64 65 page = nfs_page_to_page(req, base); 66 nfs_page_iter_page_advance(i, len); 67 return page; 68 } 69 return NULL; 70 } 71 72 static struct nfs_pgio_mirror * 73 nfs_pgio_get_mirror(struct nfs_pageio_descriptor *desc, u32 idx) 74 { 75 if (desc->pg_ops->pg_get_mirror) 76 return desc->pg_ops->pg_get_mirror(desc, idx); 77 return &desc->pg_mirrors[0]; 78 } 79 80 struct nfs_pgio_mirror * 81 nfs_pgio_current_mirror(struct nfs_pageio_descriptor *desc) 82 { 83 return nfs_pgio_get_mirror(desc, desc->pg_mirror_idx); 84 } 85 EXPORT_SYMBOL_GPL(nfs_pgio_current_mirror); 86 87 static u32 88 nfs_pgio_set_current_mirror(struct nfs_pageio_descriptor *desc, u32 idx) 89 { 90 if (desc->pg_ops->pg_set_mirror) 91 return desc->pg_ops->pg_set_mirror(desc, idx); 92 return desc->pg_mirror_idx; 93 } 94 95 void nfs_pgheader_init(struct nfs_pageio_descriptor *desc, 96 struct nfs_pgio_header *hdr, 97 void (*release)(struct nfs_pgio_header *hdr)) 98 { 99 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc); 100 101 102 hdr->req = nfs_list_entry(mirror->pg_list.next); 103 hdr->inode = desc->pg_inode; 104 hdr->cred = nfs_req_openctx(hdr->req)->cred; 105 hdr->io_start = req_offset(hdr->req); 106 hdr->good_bytes = mirror->pg_count; 107 hdr->io_completion = desc->pg_io_completion; 108 hdr->dreq = desc->pg_dreq; 109 nfs_netfs_set_pgio_header(hdr, desc); 110 hdr->release = release; 111 hdr->completion_ops = desc->pg_completion_ops; 112 if (hdr->completion_ops->init_hdr) 113 hdr->completion_ops->init_hdr(hdr); 114 115 hdr->pgio_mirror_idx = desc->pg_mirror_idx; 116 } 117 EXPORT_SYMBOL_GPL(nfs_pgheader_init); 118 119 void nfs_set_pgio_error(struct nfs_pgio_header *hdr, int error, loff_t pos) 120 { 121 unsigned int new = pos - hdr->io_start; 122 123 trace_nfs_pgio_error(hdr, error, pos); 124 if (hdr->good_bytes > new) { 125 hdr->good_bytes = new; 126 clear_bit(NFS_IOHDR_EOF, &hdr->flags); 127 if (!test_and_set_bit(NFS_IOHDR_ERROR, &hdr->flags)) 128 hdr->error = error; 129 } 130 } 131 132 static inline struct nfs_page *nfs_page_alloc(void) 133 { 134 struct nfs_page *p = 135 kmem_cache_zalloc(nfs_page_cachep, nfs_io_gfp_mask()); 136 if (p) 137 INIT_LIST_HEAD(&p->wb_list); 138 return p; 139 } 140 141 static inline void 142 nfs_page_free(struct nfs_page *p) 143 { 144 kmem_cache_free(nfs_page_cachep, p); 145 } 146 147 /** 148 * nfs_iocounter_wait - wait for i/o to complete 149 * @l_ctx: nfs_lock_context with io_counter to use 150 * 151 * returns -ERESTARTSYS if interrupted by a fatal signal. 152 * Otherwise returns 0 once the io_count hits 0. 153 */ 154 int 155 nfs_iocounter_wait(struct nfs_lock_context *l_ctx) 156 { 157 return wait_var_event_killable(&l_ctx->io_count, 158 !atomic_read(&l_ctx->io_count)); 159 } 160 161 /** 162 * nfs_async_iocounter_wait - wait on a rpc_waitqueue for I/O 163 * to complete 164 * @task: the rpc_task that should wait 165 * @l_ctx: nfs_lock_context with io_counter to check 166 * 167 * Returns true if there is outstanding I/O to wait on and the 168 * task has been put to sleep. 169 */ 170 bool 171 nfs_async_iocounter_wait(struct rpc_task *task, struct nfs_lock_context *l_ctx) 172 { 173 struct inode *inode = d_inode(l_ctx->open_context->dentry); 174 bool ret = false; 175 176 if (atomic_read(&l_ctx->io_count) > 0) { 177 rpc_sleep_on(&NFS_SERVER(inode)->uoc_rpcwaitq, task, NULL); 178 ret = true; 179 } 180 181 if (atomic_read(&l_ctx->io_count) == 0) { 182 rpc_wake_up_queued_task(&NFS_SERVER(inode)->uoc_rpcwaitq, task); 183 ret = false; 184 } 185 186 return ret; 187 } 188 EXPORT_SYMBOL_GPL(nfs_async_iocounter_wait); 189 190 /* 191 * nfs_page_set_headlock - set the request PG_HEADLOCK 192 * @req: request that is to be locked 193 * 194 * this lock must be held when modifying req->wb_head 195 * 196 * return 0 on success, < 0 on error 197 */ 198 int 199 nfs_page_set_headlock(struct nfs_page *req) 200 { 201 if (!test_and_set_bit(PG_HEADLOCK, &req->wb_flags)) 202 return 0; 203 204 set_bit(PG_CONTENDED1, &req->wb_flags); 205 smp_mb__after_atomic(); 206 return wait_on_bit_lock(&req->wb_flags, PG_HEADLOCK, 207 TASK_UNINTERRUPTIBLE); 208 } 209 210 /* 211 * nfs_page_clear_headlock - clear the request PG_HEADLOCK 212 * @req: request that is to be locked 213 */ 214 void 215 nfs_page_clear_headlock(struct nfs_page *req) 216 { 217 clear_bit_unlock(PG_HEADLOCK, &req->wb_flags); 218 smp_mb__after_atomic(); 219 if (!test_bit(PG_CONTENDED1, &req->wb_flags)) 220 return; 221 wake_up_bit(&req->wb_flags, PG_HEADLOCK); 222 } 223 224 /* 225 * nfs_page_group_lock - lock the head of the page group 226 * @req: request in group that is to be locked 227 * 228 * this lock must be held when traversing or modifying the page 229 * group list 230 * 231 * return 0 on success, < 0 on error 232 */ 233 int 234 nfs_page_group_lock(struct nfs_page *req) 235 { 236 int ret; 237 238 ret = nfs_page_set_headlock(req); 239 if (ret || req->wb_head == req) 240 return ret; 241 return nfs_page_set_headlock(req->wb_head); 242 } 243 244 /* 245 * nfs_page_group_unlock - unlock the head of the page group 246 * @req: request in group that is to be unlocked 247 */ 248 void 249 nfs_page_group_unlock(struct nfs_page *req) 250 { 251 if (req != req->wb_head) 252 nfs_page_clear_headlock(req->wb_head); 253 nfs_page_clear_headlock(req); 254 } 255 256 /** 257 * nfs_page_group_sync_on_bit_locked - Test if all requests have @bit set 258 * @req: request in page group 259 * @bit: PG_* bit that is used to sync page group 260 * 261 * must be called with page group lock held 262 */ 263 bool nfs_page_group_sync_on_bit_locked(struct nfs_page *req, unsigned int bit) 264 { 265 struct nfs_page *head = req->wb_head; 266 struct nfs_page *tmp; 267 268 WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_flags)); 269 WARN_ON_ONCE(test_and_set_bit(bit, &req->wb_flags)); 270 271 tmp = req->wb_this_page; 272 while (tmp != req) { 273 if (!test_bit(bit, &tmp->wb_flags)) 274 return false; 275 tmp = tmp->wb_this_page; 276 } 277 278 /* true! reset all bits */ 279 tmp = req; 280 do { 281 clear_bit(bit, &tmp->wb_flags); 282 tmp = tmp->wb_this_page; 283 } while (tmp != req); 284 285 return true; 286 } 287 288 /* 289 * nfs_page_group_sync_on_bit - set bit on current request, but only 290 * return true if the bit is set for all requests in page group 291 * @req - request in page group 292 * @bit - PG_* bit that is used to sync page group 293 */ 294 bool nfs_page_group_sync_on_bit(struct nfs_page *req, unsigned int bit) 295 { 296 bool ret; 297 298 nfs_page_group_lock(req); 299 ret = nfs_page_group_sync_on_bit_locked(req, bit); 300 nfs_page_group_unlock(req); 301 302 return ret; 303 } 304 305 /* 306 * nfs_page_group_init - Initialize the page group linkage for @req 307 * @req - a new nfs request 308 * @prev - the previous request in page group, or NULL if @req is the first 309 * or only request in the group (the head). 310 */ 311 static inline void 312 nfs_page_group_init(struct nfs_page *req, struct nfs_page *prev) 313 { 314 struct inode *inode; 315 WARN_ON_ONCE(prev == req); 316 317 if (!prev) { 318 /* a head request */ 319 req->wb_head = req; 320 req->wb_this_page = req; 321 } else { 322 /* a subrequest */ 323 WARN_ON_ONCE(prev->wb_this_page != prev->wb_head); 324 WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &prev->wb_head->wb_flags)); 325 req->wb_head = prev->wb_head; 326 req->wb_this_page = prev->wb_this_page; 327 prev->wb_this_page = req; 328 329 /* All subrequests take a ref on the head request until 330 * nfs_page_group_destroy is called */ 331 kref_get(&req->wb_head->wb_kref); 332 333 /* grab extra ref and bump the request count if head request 334 * has extra ref from the write/commit path to handle handoff 335 * between write and commit lists. */ 336 if (test_bit(PG_INODE_REF, &prev->wb_head->wb_flags)) { 337 inode = nfs_page_to_inode(req); 338 set_bit(PG_INODE_REF, &req->wb_flags); 339 kref_get(&req->wb_kref); 340 atomic_long_inc(&NFS_I(inode)->nrequests); 341 } 342 } 343 } 344 345 /* 346 * nfs_page_group_destroy - sync the destruction of page groups 347 * @req - request that no longer needs the page group 348 * 349 * releases the page group reference from each member once all 350 * members have called this function. 351 */ 352 static void 353 nfs_page_group_destroy(struct kref *kref) 354 { 355 struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref); 356 struct nfs_page *head = req->wb_head; 357 struct nfs_page *tmp, *next; 358 359 if (!nfs_page_group_sync_on_bit(req, PG_TEARDOWN)) 360 goto out; 361 362 tmp = req; 363 do { 364 next = tmp->wb_this_page; 365 /* unlink and free */ 366 tmp->wb_this_page = tmp; 367 tmp->wb_head = tmp; 368 nfs_free_request(tmp); 369 tmp = next; 370 } while (tmp != req); 371 out: 372 /* subrequests must release the ref on the head request */ 373 if (head != req) 374 nfs_release_request(head); 375 } 376 377 static struct nfs_page *nfs_page_create(struct nfs_lock_context *l_ctx, 378 unsigned int pgbase, pgoff_t index, 379 unsigned int offset, unsigned int count) 380 { 381 struct nfs_page *req; 382 struct nfs_open_context *ctx = l_ctx->open_context; 383 384 if (test_bit(NFS_CONTEXT_BAD, &ctx->flags)) 385 return ERR_PTR(-EBADF); 386 /* try to allocate the request struct */ 387 req = nfs_page_alloc(); 388 if (req == NULL) 389 return ERR_PTR(-ENOMEM); 390 391 req->wb_lock_context = l_ctx; 392 refcount_inc(&l_ctx->count); 393 atomic_inc(&l_ctx->io_count); 394 395 /* Initialize the request struct. Initially, we assume a 396 * long write-back delay. This will be adjusted in 397 * update_nfs_request below if the region is not locked. */ 398 req->wb_pgbase = pgbase; 399 req->wb_index = index; 400 req->wb_offset = offset; 401 req->wb_bytes = count; 402 kref_init(&req->wb_kref); 403 req->wb_nio = 0; 404 return req; 405 } 406 407 static void nfs_page_assign_folio(struct nfs_page *req, struct folio *folio) 408 { 409 if (folio != NULL) { 410 req->wb_folio = folio; 411 folio_get(folio); 412 set_bit(PG_FOLIO, &req->wb_flags); 413 } 414 } 415 416 static void nfs_page_assign_page(struct nfs_page *req, struct page *page) 417 { 418 if (page != NULL) { 419 req->wb_page = page; 420 get_page(page); 421 } 422 } 423 424 /** 425 * nfs_page_create_from_page - Create an NFS read/write request. 426 * @ctx: open context to use 427 * @page: page to write 428 * @pgbase: starting offset within the page for the write 429 * @offset: file offset for the write 430 * @count: number of bytes to read/write 431 * 432 * The page must be locked by the caller. This makes sure we never 433 * create two different requests for the same page. 434 * User should ensure it is safe to sleep in this function. 435 */ 436 struct nfs_page *nfs_page_create_from_page(struct nfs_open_context *ctx, 437 struct page *page, 438 unsigned int pgbase, loff_t offset, 439 unsigned int count) 440 { 441 struct nfs_lock_context *l_ctx = nfs_get_lock_context(ctx); 442 struct nfs_page *ret; 443 444 if (IS_ERR(l_ctx)) 445 return ERR_CAST(l_ctx); 446 ret = nfs_page_create(l_ctx, pgbase, offset >> PAGE_SHIFT, 447 offset_in_page(offset), count); 448 if (!IS_ERR(ret)) { 449 nfs_page_assign_page(ret, page); 450 nfs_page_group_init(ret, NULL); 451 } 452 nfs_put_lock_context(l_ctx); 453 return ret; 454 } 455 456 /** 457 * nfs_page_create_from_folio - Create an NFS read/write request. 458 * @ctx: open context to use 459 * @folio: folio to write 460 * @offset: starting offset within the folio for the write 461 * @count: number of bytes to read/write 462 * 463 * The page must be locked by the caller. This makes sure we never 464 * create two different requests for the same page. 465 * User should ensure it is safe to sleep in this function. 466 */ 467 struct nfs_page *nfs_page_create_from_folio(struct nfs_open_context *ctx, 468 struct folio *folio, 469 unsigned int offset, 470 unsigned int count) 471 { 472 struct nfs_lock_context *l_ctx = nfs_get_lock_context(ctx); 473 struct nfs_page *ret; 474 475 if (IS_ERR(l_ctx)) 476 return ERR_CAST(l_ctx); 477 ret = nfs_page_create(l_ctx, offset, folio->index, offset, count); 478 if (!IS_ERR(ret)) { 479 nfs_page_assign_folio(ret, folio); 480 nfs_page_group_init(ret, NULL); 481 } 482 nfs_put_lock_context(l_ctx); 483 return ret; 484 } 485 486 static struct nfs_page * 487 nfs_create_subreq(struct nfs_page *req, 488 unsigned int pgbase, 489 unsigned int offset, 490 unsigned int count) 491 { 492 struct nfs_page *last; 493 struct nfs_page *ret; 494 struct folio *folio = nfs_page_to_folio(req); 495 struct page *page = nfs_page_to_page(req, pgbase); 496 497 ret = nfs_page_create(req->wb_lock_context, pgbase, req->wb_index, 498 offset, count); 499 if (!IS_ERR(ret)) { 500 if (folio) 501 nfs_page_assign_folio(ret, folio); 502 else 503 nfs_page_assign_page(ret, page); 504 /* find the last request */ 505 for (last = req->wb_head; 506 last->wb_this_page != req->wb_head; 507 last = last->wb_this_page) 508 ; 509 510 nfs_lock_request(ret); 511 nfs_page_group_init(ret, last); 512 ret->wb_nio = req->wb_nio; 513 } 514 return ret; 515 } 516 517 /** 518 * nfs_unlock_request - Unlock request and wake up sleepers. 519 * @req: pointer to request 520 */ 521 void nfs_unlock_request(struct nfs_page *req) 522 { 523 clear_bit_unlock(PG_BUSY, &req->wb_flags); 524 smp_mb__after_atomic(); 525 if (!test_bit(PG_CONTENDED2, &req->wb_flags)) 526 return; 527 wake_up_bit(&req->wb_flags, PG_BUSY); 528 } 529 530 /** 531 * nfs_unlock_and_release_request - Unlock request and release the nfs_page 532 * @req: pointer to request 533 */ 534 void nfs_unlock_and_release_request(struct nfs_page *req) 535 { 536 nfs_unlock_request(req); 537 nfs_release_request(req); 538 } 539 540 /* 541 * nfs_clear_request - Free up all resources allocated to the request 542 * @req: 543 * 544 * Release page and open context resources associated with a read/write 545 * request after it has completed. 546 */ 547 static void nfs_clear_request(struct nfs_page *req) 548 { 549 struct folio *folio = nfs_page_to_folio(req); 550 struct page *page = req->wb_page; 551 struct nfs_lock_context *l_ctx = req->wb_lock_context; 552 struct nfs_open_context *ctx; 553 554 if (folio != NULL) { 555 folio_put(folio); 556 req->wb_folio = NULL; 557 clear_bit(PG_FOLIO, &req->wb_flags); 558 } else if (page != NULL) { 559 put_page(page); 560 req->wb_page = NULL; 561 } 562 if (l_ctx != NULL) { 563 if (atomic_dec_and_test(&l_ctx->io_count)) { 564 wake_up_var(&l_ctx->io_count); 565 ctx = l_ctx->open_context; 566 if (test_bit(NFS_CONTEXT_UNLOCK, &ctx->flags)) 567 rpc_wake_up(&NFS_SERVER(d_inode(ctx->dentry))->uoc_rpcwaitq); 568 } 569 nfs_put_lock_context(l_ctx); 570 req->wb_lock_context = NULL; 571 } 572 } 573 574 /** 575 * nfs_free_request - Release the count on an NFS read/write request 576 * @req: request to release 577 * 578 * Note: Should never be called with the spinlock held! 579 */ 580 void nfs_free_request(struct nfs_page *req) 581 { 582 WARN_ON_ONCE(req->wb_this_page != req); 583 584 /* extra debug: make sure no sync bits are still set */ 585 WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags)); 586 WARN_ON_ONCE(test_bit(PG_UNLOCKPAGE, &req->wb_flags)); 587 WARN_ON_ONCE(test_bit(PG_UPTODATE, &req->wb_flags)); 588 WARN_ON_ONCE(test_bit(PG_WB_END, &req->wb_flags)); 589 WARN_ON_ONCE(test_bit(PG_REMOVE, &req->wb_flags)); 590 591 /* Release struct file and open context */ 592 nfs_clear_request(req); 593 nfs_page_free(req); 594 } 595 596 void nfs_release_request(struct nfs_page *req) 597 { 598 kref_put(&req->wb_kref, nfs_page_group_destroy); 599 } 600 EXPORT_SYMBOL_GPL(nfs_release_request); 601 602 /* 603 * nfs_generic_pg_test - determine if requests can be coalesced 604 * @desc: pointer to descriptor 605 * @prev: previous request in desc, or NULL 606 * @req: this request 607 * 608 * Returns zero if @req cannot be coalesced into @desc, otherwise it returns 609 * the size of the request. 610 */ 611 size_t nfs_generic_pg_test(struct nfs_pageio_descriptor *desc, 612 struct nfs_page *prev, struct nfs_page *req) 613 { 614 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc); 615 616 617 if (mirror->pg_count > mirror->pg_bsize) { 618 /* should never happen */ 619 WARN_ON_ONCE(1); 620 return 0; 621 } 622 623 /* 624 * Limit the request size so that we can still allocate a page array 625 * for it without upsetting the slab allocator. 626 */ 627 if (((mirror->pg_count + req->wb_bytes) >> PAGE_SHIFT) * 628 sizeof(struct page *) > PAGE_SIZE) 629 return 0; 630 631 return min(mirror->pg_bsize - mirror->pg_count, (size_t)req->wb_bytes); 632 } 633 EXPORT_SYMBOL_GPL(nfs_generic_pg_test); 634 635 struct nfs_pgio_header *nfs_pgio_header_alloc(const struct nfs_rw_ops *ops) 636 { 637 struct nfs_pgio_header *hdr = ops->rw_alloc_header(); 638 639 if (hdr) { 640 INIT_LIST_HEAD(&hdr->pages); 641 hdr->rw_ops = ops; 642 } 643 return hdr; 644 } 645 EXPORT_SYMBOL_GPL(nfs_pgio_header_alloc); 646 647 /** 648 * nfs_pgio_data_destroy - make @hdr suitable for reuse 649 * 650 * Frees memory and releases refs from nfs_generic_pgio, so that it may 651 * be called again. 652 * 653 * @hdr: A header that has had nfs_generic_pgio called 654 */ 655 static void nfs_pgio_data_destroy(struct nfs_pgio_header *hdr) 656 { 657 if (hdr->args.context) 658 put_nfs_open_context(hdr->args.context); 659 if (hdr->page_array.pagevec != hdr->page_array.page_array) 660 kfree(hdr->page_array.pagevec); 661 } 662 663 /* 664 * nfs_pgio_header_free - Free a read or write header 665 * @hdr: The header to free 666 */ 667 void nfs_pgio_header_free(struct nfs_pgio_header *hdr) 668 { 669 nfs_pgio_data_destroy(hdr); 670 hdr->rw_ops->rw_free_header(hdr); 671 } 672 EXPORT_SYMBOL_GPL(nfs_pgio_header_free); 673 674 /** 675 * nfs_pgio_rpcsetup - Set up arguments for a pageio call 676 * @hdr: The pageio hdr 677 * @pgbase: base 678 * @count: Number of bytes to read 679 * @how: How to commit data (writes only) 680 * @cinfo: Commit information for the call (writes only) 681 */ 682 static void nfs_pgio_rpcsetup(struct nfs_pgio_header *hdr, unsigned int pgbase, 683 unsigned int count, int how, 684 struct nfs_commit_info *cinfo) 685 { 686 struct nfs_page *req = hdr->req; 687 688 /* Set up the RPC argument and reply structs 689 * NB: take care not to mess about with hdr->commit et al. */ 690 691 hdr->args.fh = NFS_FH(hdr->inode); 692 hdr->args.offset = req_offset(req); 693 /* pnfs_set_layoutcommit needs this */ 694 hdr->mds_offset = hdr->args.offset; 695 hdr->args.pgbase = pgbase; 696 hdr->args.pages = hdr->page_array.pagevec; 697 hdr->args.count = count; 698 hdr->args.context = get_nfs_open_context(nfs_req_openctx(req)); 699 hdr->args.lock_context = req->wb_lock_context; 700 hdr->args.stable = NFS_UNSTABLE; 701 switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) { 702 case 0: 703 break; 704 case FLUSH_COND_STABLE: 705 if (nfs_reqs_to_commit(cinfo)) 706 break; 707 fallthrough; 708 default: 709 hdr->args.stable = NFS_FILE_SYNC; 710 } 711 712 hdr->res.fattr = &hdr->fattr; 713 hdr->res.count = 0; 714 hdr->res.eof = 0; 715 hdr->res.verf = &hdr->verf; 716 nfs_fattr_init(&hdr->fattr); 717 } 718 719 /** 720 * nfs_pgio_prepare - Prepare pageio hdr to go over the wire 721 * @task: The current task 722 * @calldata: pageio header to prepare 723 */ 724 static void nfs_pgio_prepare(struct rpc_task *task, void *calldata) 725 { 726 struct nfs_pgio_header *hdr = calldata; 727 int err; 728 err = NFS_PROTO(hdr->inode)->pgio_rpc_prepare(task, hdr); 729 if (err) 730 rpc_exit(task, err); 731 } 732 733 int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_header *hdr, 734 const struct cred *cred, const struct nfs_rpc_ops *rpc_ops, 735 const struct rpc_call_ops *call_ops, int how, int flags, 736 struct nfsd_file *localio) 737 { 738 struct rpc_task *task; 739 struct rpc_message msg = { 740 .rpc_argp = &hdr->args, 741 .rpc_resp = &hdr->res, 742 .rpc_cred = cred, 743 }; 744 struct rpc_task_setup task_setup_data = { 745 .rpc_client = clnt, 746 .task = &hdr->task, 747 .rpc_message = &msg, 748 .callback_ops = call_ops, 749 .callback_data = hdr, 750 .workqueue = nfsiod_workqueue, 751 .flags = RPC_TASK_ASYNC | flags, 752 }; 753 754 if (nfs_server_capable(hdr->inode, NFS_CAP_MOVEABLE)) 755 task_setup_data.flags |= RPC_TASK_MOVEABLE; 756 757 hdr->rw_ops->rw_initiate(hdr, &msg, rpc_ops, &task_setup_data, how); 758 759 dprintk("NFS: initiated pgio call " 760 "(req %s/%llu, %u bytes @ offset %llu)\n", 761 hdr->inode->i_sb->s_id, 762 (unsigned long long)NFS_FILEID(hdr->inode), 763 hdr->args.count, 764 (unsigned long long)hdr->args.offset); 765 766 if (localio) 767 return nfs_local_doio(NFS_SERVER(hdr->inode)->nfs_client, 768 localio, hdr, call_ops); 769 770 task = rpc_run_task(&task_setup_data); 771 if (IS_ERR(task)) 772 return PTR_ERR(task); 773 rpc_put_task(task); 774 return 0; 775 } 776 EXPORT_SYMBOL_GPL(nfs_initiate_pgio); 777 778 /** 779 * nfs_pgio_error - Clean up from a pageio error 780 * @hdr: pageio header 781 */ 782 static void nfs_pgio_error(struct nfs_pgio_header *hdr) 783 { 784 set_bit(NFS_IOHDR_REDO, &hdr->flags); 785 hdr->completion_ops->completion(hdr); 786 } 787 788 /** 789 * nfs_pgio_release - Release pageio data 790 * @calldata: The pageio header to release 791 */ 792 static void nfs_pgio_release(void *calldata) 793 { 794 struct nfs_pgio_header *hdr = calldata; 795 hdr->completion_ops->completion(hdr); 796 } 797 798 static void nfs_pageio_mirror_init(struct nfs_pgio_mirror *mirror, 799 unsigned int bsize) 800 { 801 INIT_LIST_HEAD(&mirror->pg_list); 802 mirror->pg_bytes_written = 0; 803 mirror->pg_count = 0; 804 mirror->pg_bsize = bsize; 805 mirror->pg_base = 0; 806 mirror->pg_recoalesce = 0; 807 } 808 809 /** 810 * nfs_pageio_init - initialise a page io descriptor 811 * @desc: pointer to descriptor 812 * @inode: pointer to inode 813 * @pg_ops: pointer to pageio operations 814 * @compl_ops: pointer to pageio completion operations 815 * @rw_ops: pointer to nfs read/write operations 816 * @bsize: io block size 817 * @io_flags: extra parameters for the io function 818 */ 819 void nfs_pageio_init(struct nfs_pageio_descriptor *desc, 820 struct inode *inode, 821 const struct nfs_pageio_ops *pg_ops, 822 const struct nfs_pgio_completion_ops *compl_ops, 823 const struct nfs_rw_ops *rw_ops, 824 size_t bsize, 825 int io_flags) 826 { 827 desc->pg_moreio = 0; 828 desc->pg_inode = inode; 829 desc->pg_ops = pg_ops; 830 desc->pg_completion_ops = compl_ops; 831 desc->pg_rw_ops = rw_ops; 832 desc->pg_ioflags = io_flags; 833 desc->pg_error = 0; 834 desc->pg_lseg = NULL; 835 desc->pg_io_completion = NULL; 836 desc->pg_dreq = NULL; 837 nfs_netfs_reset_pageio_descriptor(desc); 838 desc->pg_bsize = bsize; 839 840 desc->pg_mirror_count = 1; 841 desc->pg_mirror_idx = 0; 842 843 desc->pg_mirrors_dynamic = NULL; 844 desc->pg_mirrors = desc->pg_mirrors_static; 845 nfs_pageio_mirror_init(&desc->pg_mirrors[0], bsize); 846 desc->pg_maxretrans = 0; 847 } 848 849 /** 850 * nfs_pgio_result - Basic pageio error handling 851 * @task: The task that ran 852 * @calldata: Pageio header to check 853 */ 854 static void nfs_pgio_result(struct rpc_task *task, void *calldata) 855 { 856 struct nfs_pgio_header *hdr = calldata; 857 struct inode *inode = hdr->inode; 858 859 if (hdr->rw_ops->rw_done(task, hdr, inode) != 0) 860 return; 861 if (task->tk_status < 0) 862 nfs_set_pgio_error(hdr, task->tk_status, hdr->args.offset); 863 else 864 hdr->rw_ops->rw_result(task, hdr); 865 } 866 867 /* 868 * Create an RPC task for the given read or write request and kick it. 869 * The page must have been locked by the caller. 870 * 871 * It may happen that the page we're passed is not marked dirty. 872 * This is the case if nfs_updatepage detects a conflicting request 873 * that has been written but not committed. 874 */ 875 int nfs_generic_pgio(struct nfs_pageio_descriptor *desc, 876 struct nfs_pgio_header *hdr) 877 { 878 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc); 879 880 struct nfs_page *req; 881 struct page **pages, 882 *last_page; 883 struct list_head *head = &mirror->pg_list; 884 struct nfs_commit_info cinfo; 885 struct nfs_page_array *pg_array = &hdr->page_array; 886 unsigned int pagecount, pageused; 887 unsigned int pg_base = offset_in_page(mirror->pg_base); 888 gfp_t gfp_flags = nfs_io_gfp_mask(); 889 890 pagecount = nfs_page_array_len(pg_base, mirror->pg_count); 891 pg_array->npages = pagecount; 892 893 if (pagecount <= ARRAY_SIZE(pg_array->page_array)) 894 pg_array->pagevec = pg_array->page_array; 895 else { 896 pg_array->pagevec = kzalloc_objs(struct page *, pagecount, 897 gfp_flags); 898 if (!pg_array->pagevec) { 899 pg_array->npages = 0; 900 nfs_pgio_error(hdr); 901 desc->pg_error = -ENOMEM; 902 return desc->pg_error; 903 } 904 } 905 906 nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq); 907 pages = hdr->page_array.pagevec; 908 last_page = NULL; 909 pageused = 0; 910 while (!list_empty(head)) { 911 struct nfs_page_iter_page i; 912 struct page *page; 913 914 req = nfs_list_entry(head->next); 915 nfs_list_move_request(req, &hdr->pages); 916 917 if (req->wb_pgbase == 0) 918 last_page = NULL; 919 920 nfs_page_iter_page_init(&i, req); 921 while ((page = nfs_page_iter_page_get(&i)) != NULL) { 922 if (last_page != page) { 923 pageused++; 924 if (pageused > pagecount) 925 goto full; 926 *pages++ = last_page = page; 927 } 928 } 929 } 930 full: 931 if (WARN_ON_ONCE(pageused != pagecount)) { 932 nfs_pgio_error(hdr); 933 desc->pg_error = -EINVAL; 934 return desc->pg_error; 935 } 936 937 if ((desc->pg_ioflags & FLUSH_COND_STABLE) && 938 (desc->pg_moreio || nfs_reqs_to_commit(&cinfo))) 939 desc->pg_ioflags &= ~FLUSH_COND_STABLE; 940 941 /* Set up the argument struct */ 942 nfs_pgio_rpcsetup(hdr, pg_base, mirror->pg_count, desc->pg_ioflags, 943 &cinfo); 944 desc->pg_rpc_callops = &nfs_pgio_common_ops; 945 return 0; 946 } 947 EXPORT_SYMBOL_GPL(nfs_generic_pgio); 948 949 static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc) 950 { 951 struct nfs_pgio_header *hdr; 952 int ret; 953 unsigned short task_flags = 0; 954 955 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops); 956 if (!hdr) { 957 desc->pg_error = -ENOMEM; 958 return desc->pg_error; 959 } 960 nfs_pgheader_init(desc, hdr, nfs_pgio_header_free); 961 ret = nfs_generic_pgio(desc, hdr); 962 if (ret == 0) { 963 struct nfs_client *clp = NFS_SERVER(hdr->inode)->nfs_client; 964 965 struct nfsd_file *localio = 966 nfs_local_open_fh(clp, hdr->cred, hdr->args.fh, 967 &hdr->args.context->nfl, 968 hdr->args.context->mode); 969 970 if (NFS_SERVER(hdr->inode)->nfs_client->cl_minorversion) 971 task_flags = RPC_TASK_MOVEABLE; 972 ret = nfs_initiate_pgio(NFS_CLIENT(hdr->inode), 973 hdr, 974 hdr->cred, 975 NFS_PROTO(hdr->inode), 976 desc->pg_rpc_callops, 977 desc->pg_ioflags, 978 RPC_TASK_CRED_NOREF | task_flags, 979 localio); 980 } 981 return ret; 982 } 983 984 static struct nfs_pgio_mirror * 985 nfs_pageio_alloc_mirrors(struct nfs_pageio_descriptor *desc, 986 unsigned int mirror_count) 987 { 988 struct nfs_pgio_mirror *ret; 989 unsigned int i; 990 991 kfree(desc->pg_mirrors_dynamic); 992 desc->pg_mirrors_dynamic = NULL; 993 if (mirror_count == 1) 994 return desc->pg_mirrors_static; 995 ret = kmalloc_objs(*ret, mirror_count, nfs_io_gfp_mask()); 996 if (ret != NULL) { 997 for (i = 0; i < mirror_count; i++) 998 nfs_pageio_mirror_init(&ret[i], desc->pg_bsize); 999 desc->pg_mirrors_dynamic = ret; 1000 } 1001 return ret; 1002 } 1003 1004 /* 1005 * nfs_pageio_setup_mirroring - determine if mirroring is to be used 1006 * by calling the pg_get_mirror_count op 1007 */ 1008 static void nfs_pageio_setup_mirroring(struct nfs_pageio_descriptor *pgio, 1009 struct nfs_page *req) 1010 { 1011 unsigned int mirror_count = 1; 1012 1013 if (pgio->pg_ops->pg_get_mirror_count) 1014 mirror_count = pgio->pg_ops->pg_get_mirror_count(pgio, req); 1015 if (mirror_count == pgio->pg_mirror_count || pgio->pg_error < 0) 1016 return; 1017 1018 if (!mirror_count || mirror_count > NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX) { 1019 pgio->pg_error = -EINVAL; 1020 return; 1021 } 1022 1023 pgio->pg_mirrors = nfs_pageio_alloc_mirrors(pgio, mirror_count); 1024 if (pgio->pg_mirrors == NULL) { 1025 pgio->pg_error = -ENOMEM; 1026 pgio->pg_mirrors = pgio->pg_mirrors_static; 1027 mirror_count = 1; 1028 } 1029 pgio->pg_mirror_count = mirror_count; 1030 } 1031 1032 static void nfs_pageio_cleanup_mirroring(struct nfs_pageio_descriptor *pgio) 1033 { 1034 pgio->pg_mirror_count = 1; 1035 pgio->pg_mirror_idx = 0; 1036 pgio->pg_mirrors = pgio->pg_mirrors_static; 1037 kfree(pgio->pg_mirrors_dynamic); 1038 pgio->pg_mirrors_dynamic = NULL; 1039 } 1040 1041 static bool nfs_match_lock_context(const struct nfs_lock_context *l1, 1042 const struct nfs_lock_context *l2) 1043 { 1044 return l1->lockowner == l2->lockowner; 1045 } 1046 1047 static bool nfs_page_is_contiguous(const struct nfs_page *prev, 1048 const struct nfs_page *req) 1049 { 1050 size_t prev_end = prev->wb_pgbase + prev->wb_bytes; 1051 1052 if (req_offset(req) != req_offset(prev) + prev->wb_bytes) 1053 return false; 1054 if (req->wb_pgbase == 0) 1055 return prev_end == nfs_page_max_length(prev); 1056 if (req->wb_pgbase == prev_end) { 1057 struct folio *folio = nfs_page_to_folio(req); 1058 if (folio) 1059 return folio == nfs_page_to_folio(prev); 1060 return req->wb_page == prev->wb_page; 1061 } 1062 return false; 1063 } 1064 1065 /** 1066 * nfs_coalesce_size - test two requests for compatibility 1067 * @prev: pointer to nfs_page 1068 * @req: pointer to nfs_page 1069 * @pgio: pointer to nfs_pagio_descriptor 1070 * 1071 * The nfs_page structures 'prev' and 'req' are compared to ensure that the 1072 * page data area they describe is contiguous, and that their RPC 1073 * credentials, NFSv4 open state, and lockowners are the same. 1074 * 1075 * Returns size of the request that can be coalesced 1076 */ 1077 static unsigned int nfs_coalesce_size(struct nfs_page *prev, 1078 struct nfs_page *req, 1079 struct nfs_pageio_descriptor *pgio) 1080 { 1081 struct file_lock_context *flctx; 1082 1083 if (prev) { 1084 if (!nfs_match_open_context(nfs_req_openctx(req), nfs_req_openctx(prev))) 1085 return 0; 1086 flctx = locks_inode_context(d_inode(nfs_req_openctx(req)->dentry)); 1087 if (flctx != NULL && 1088 !(list_empty_careful(&flctx->flc_posix) && 1089 list_empty_careful(&flctx->flc_flock)) && 1090 !nfs_match_lock_context(req->wb_lock_context, 1091 prev->wb_lock_context)) 1092 return 0; 1093 if (!nfs_page_is_contiguous(prev, req)) 1094 return 0; 1095 } 1096 return pgio->pg_ops->pg_test(pgio, prev, req); 1097 } 1098 1099 /** 1100 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list. 1101 * @desc: destination io descriptor 1102 * @req: request 1103 * 1104 * If the request 'req' was successfully coalesced into the existing list 1105 * of pages 'desc', it returns the size of req. 1106 */ 1107 static unsigned int 1108 nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc, 1109 struct nfs_page *req) 1110 { 1111 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc); 1112 struct nfs_page *prev = NULL; 1113 unsigned int size; 1114 1115 if (list_empty(&mirror->pg_list)) { 1116 if (desc->pg_ops->pg_init) 1117 desc->pg_ops->pg_init(desc, req); 1118 if (desc->pg_error < 0) 1119 return 0; 1120 mirror->pg_base = req->wb_pgbase; 1121 mirror->pg_count = 0; 1122 mirror->pg_recoalesce = 0; 1123 } else 1124 prev = nfs_list_entry(mirror->pg_list.prev); 1125 1126 if (desc->pg_maxretrans && req->wb_nio > desc->pg_maxretrans) { 1127 if (NFS_SERVER(desc->pg_inode)->flags & NFS_MOUNT_SOFTERR) 1128 desc->pg_error = -ETIMEDOUT; 1129 else 1130 desc->pg_error = -EIO; 1131 return 0; 1132 } 1133 1134 size = nfs_coalesce_size(prev, req, desc); 1135 if (size < req->wb_bytes) 1136 return size; 1137 nfs_list_move_request(req, &mirror->pg_list); 1138 mirror->pg_count += req->wb_bytes; 1139 return req->wb_bytes; 1140 } 1141 1142 /* 1143 * Helper for nfs_pageio_add_request and nfs_pageio_complete 1144 */ 1145 static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc) 1146 { 1147 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc); 1148 1149 if (!list_empty(&mirror->pg_list)) { 1150 int error = desc->pg_ops->pg_doio(desc); 1151 if (error < 0) 1152 desc->pg_error = error; 1153 if (list_empty(&mirror->pg_list)) 1154 mirror->pg_bytes_written += mirror->pg_count; 1155 } 1156 } 1157 1158 static void 1159 nfs_pageio_cleanup_request(struct nfs_pageio_descriptor *desc, 1160 struct nfs_page *req) 1161 { 1162 LIST_HEAD(head); 1163 1164 nfs_list_move_request(req, &head); 1165 desc->pg_completion_ops->error_cleanup(&head, desc->pg_error); 1166 } 1167 1168 /** 1169 * __nfs_pageio_add_request - Attempt to coalesce a request into a page list. 1170 * @desc: destination io descriptor 1171 * @req: request 1172 * 1173 * This may split a request into subrequests which are all part of the 1174 * same page group. If so, it will submit @req as the last one, to ensure 1175 * the pointer to @req is still valid in case of failure. 1176 * 1177 * Returns true if the request 'req' was successfully coalesced into the 1178 * existing list of pages 'desc'. 1179 */ 1180 static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc, 1181 struct nfs_page *req) 1182 { 1183 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc); 1184 struct nfs_page *subreq; 1185 unsigned int size, subreq_size; 1186 1187 nfs_page_group_lock(req); 1188 1189 subreq = req; 1190 subreq_size = subreq->wb_bytes; 1191 for(;;) { 1192 size = nfs_pageio_do_add_request(desc, subreq); 1193 if (size == subreq_size) { 1194 /* We successfully submitted a request */ 1195 if (subreq == req) 1196 break; 1197 req->wb_pgbase += size; 1198 req->wb_bytes -= size; 1199 req->wb_offset += size; 1200 subreq_size = req->wb_bytes; 1201 subreq = req; 1202 continue; 1203 } 1204 if (WARN_ON_ONCE(subreq != req)) { 1205 nfs_page_group_unlock(req); 1206 nfs_pageio_cleanup_request(desc, subreq); 1207 subreq = req; 1208 subreq_size = req->wb_bytes; 1209 nfs_page_group_lock(req); 1210 } 1211 if (!size) { 1212 /* Can't coalesce any more, so do I/O */ 1213 nfs_page_group_unlock(req); 1214 desc->pg_moreio = 1; 1215 nfs_pageio_doio(desc); 1216 if (desc->pg_error < 0 || mirror->pg_recoalesce) 1217 return 0; 1218 /* retry add_request for this subreq */ 1219 nfs_page_group_lock(req); 1220 continue; 1221 } 1222 subreq = nfs_create_subreq(req, req->wb_pgbase, 1223 req->wb_offset, size); 1224 if (IS_ERR(subreq)) 1225 goto err_ptr; 1226 subreq_size = size; 1227 } 1228 1229 nfs_page_group_unlock(req); 1230 return 1; 1231 err_ptr: 1232 desc->pg_error = PTR_ERR(subreq); 1233 nfs_page_group_unlock(req); 1234 return 0; 1235 } 1236 1237 static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc) 1238 { 1239 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc); 1240 LIST_HEAD(head); 1241 1242 do { 1243 list_splice_init(&mirror->pg_list, &head); 1244 mirror->pg_recoalesce = 0; 1245 1246 while (!list_empty(&head)) { 1247 struct nfs_page *req; 1248 1249 req = list_first_entry(&head, struct nfs_page, wb_list); 1250 if (__nfs_pageio_add_request(desc, req)) 1251 continue; 1252 if (desc->pg_error < 0) { 1253 list_splice_tail(&head, &mirror->pg_list); 1254 mirror->pg_recoalesce = 1; 1255 return 0; 1256 } 1257 break; 1258 } 1259 } while (mirror->pg_recoalesce); 1260 return 1; 1261 } 1262 1263 static int nfs_pageio_add_request_mirror(struct nfs_pageio_descriptor *desc, 1264 struct nfs_page *req) 1265 { 1266 int ret; 1267 1268 do { 1269 ret = __nfs_pageio_add_request(desc, req); 1270 if (ret) 1271 break; 1272 if (desc->pg_error < 0) 1273 break; 1274 ret = nfs_do_recoalesce(desc); 1275 } while (ret); 1276 1277 return ret; 1278 } 1279 1280 static void nfs_pageio_error_cleanup(struct nfs_pageio_descriptor *desc) 1281 { 1282 u32 midx; 1283 struct nfs_pgio_mirror *mirror; 1284 1285 if (!desc->pg_error) 1286 return; 1287 1288 for (midx = 0; midx < desc->pg_mirror_count; midx++) { 1289 mirror = nfs_pgio_get_mirror(desc, midx); 1290 desc->pg_completion_ops->error_cleanup(&mirror->pg_list, 1291 desc->pg_error); 1292 } 1293 } 1294 1295 int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc, 1296 struct nfs_page *req) 1297 { 1298 u32 midx; 1299 unsigned int pgbase, offset, bytes; 1300 struct nfs_page *dupreq; 1301 1302 pgbase = req->wb_pgbase; 1303 offset = req->wb_offset; 1304 bytes = req->wb_bytes; 1305 1306 nfs_pageio_setup_mirroring(desc, req); 1307 if (desc->pg_error < 0) 1308 goto out_failed; 1309 1310 /* Create the mirror instances first, and fire them off */ 1311 for (midx = 1; midx < desc->pg_mirror_count; midx++) { 1312 nfs_page_group_lock(req); 1313 1314 dupreq = nfs_create_subreq(req, 1315 pgbase, offset, bytes); 1316 1317 nfs_page_group_unlock(req); 1318 if (IS_ERR(dupreq)) { 1319 desc->pg_error = PTR_ERR(dupreq); 1320 goto out_failed; 1321 } 1322 1323 nfs_pgio_set_current_mirror(desc, midx); 1324 if (!nfs_pageio_add_request_mirror(desc, dupreq)) 1325 goto out_cleanup_subreq; 1326 } 1327 1328 nfs_pgio_set_current_mirror(desc, 0); 1329 if (!nfs_pageio_add_request_mirror(desc, req)) 1330 goto out_failed; 1331 1332 return 1; 1333 1334 out_cleanup_subreq: 1335 nfs_pageio_cleanup_request(desc, dupreq); 1336 out_failed: 1337 nfs_pageio_error_cleanup(desc); 1338 return 0; 1339 } 1340 1341 /* 1342 * nfs_pageio_complete_mirror - Complete I/O on the current mirror of an 1343 * nfs_pageio_descriptor 1344 * @desc: pointer to io descriptor 1345 * @mirror_idx: pointer to mirror index 1346 */ 1347 static void nfs_pageio_complete_mirror(struct nfs_pageio_descriptor *desc, 1348 u32 mirror_idx) 1349 { 1350 struct nfs_pgio_mirror *mirror; 1351 u32 restore_idx; 1352 1353 restore_idx = nfs_pgio_set_current_mirror(desc, mirror_idx); 1354 mirror = nfs_pgio_current_mirror(desc); 1355 1356 for (;;) { 1357 nfs_pageio_doio(desc); 1358 if (desc->pg_error < 0 || !mirror->pg_recoalesce) 1359 break; 1360 if (!nfs_do_recoalesce(desc)) 1361 break; 1362 } 1363 nfs_pgio_set_current_mirror(desc, restore_idx); 1364 } 1365 1366 /* 1367 * nfs_pageio_resend - Transfer requests to new descriptor and resend 1368 * @hdr - the pgio header to move request from 1369 * @desc - the pageio descriptor to add requests to 1370 * 1371 * Try to move each request (nfs_page) from @hdr to @desc then attempt 1372 * to send them. 1373 * 1374 * Returns 0 on success and < 0 on error. 1375 */ 1376 int nfs_pageio_resend(struct nfs_pageio_descriptor *desc, 1377 struct nfs_pgio_header *hdr) 1378 { 1379 LIST_HEAD(pages); 1380 1381 desc->pg_io_completion = hdr->io_completion; 1382 desc->pg_dreq = hdr->dreq; 1383 nfs_netfs_set_pageio_descriptor(desc, hdr); 1384 list_splice_init(&hdr->pages, &pages); 1385 while (!list_empty(&pages)) { 1386 struct nfs_page *req = nfs_list_entry(pages.next); 1387 1388 if (!nfs_pageio_add_request(desc, req)) 1389 break; 1390 } 1391 nfs_pageio_complete(desc); 1392 if (!list_empty(&pages)) { 1393 int err = desc->pg_error < 0 ? desc->pg_error : -EIO; 1394 hdr->completion_ops->error_cleanup(&pages, err); 1395 nfs_set_pgio_error(hdr, err, hdr->io_start); 1396 return err; 1397 } 1398 return 0; 1399 } 1400 EXPORT_SYMBOL_GPL(nfs_pageio_resend); 1401 1402 /** 1403 * nfs_pageio_complete - Complete I/O then cleanup an nfs_pageio_descriptor 1404 * @desc: pointer to io descriptor 1405 */ 1406 void nfs_pageio_complete(struct nfs_pageio_descriptor *desc) 1407 { 1408 u32 midx; 1409 1410 for (midx = 0; midx < desc->pg_mirror_count; midx++) 1411 nfs_pageio_complete_mirror(desc, midx); 1412 1413 if (desc->pg_error < 0) 1414 nfs_pageio_error_cleanup(desc); 1415 if (desc->pg_ops->pg_cleanup) 1416 desc->pg_ops->pg_cleanup(desc); 1417 nfs_pageio_cleanup_mirroring(desc); 1418 } 1419 1420 /** 1421 * nfs_pageio_cond_complete - Conditional I/O completion 1422 * @desc: pointer to io descriptor 1423 * @index: page index 1424 * 1425 * It is important to ensure that processes don't try to take locks 1426 * on non-contiguous ranges of pages as that might deadlock. This 1427 * function should be called before attempting to wait on a locked 1428 * nfs_page. It will complete the I/O if the page index 'index' 1429 * is not contiguous with the existing list of pages in 'desc'. 1430 */ 1431 void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index) 1432 { 1433 struct nfs_pgio_mirror *mirror; 1434 struct nfs_page *prev; 1435 struct folio *folio; 1436 u32 midx; 1437 1438 for (midx = 0; midx < desc->pg_mirror_count; midx++) { 1439 mirror = nfs_pgio_get_mirror(desc, midx); 1440 if (!list_empty(&mirror->pg_list)) { 1441 prev = nfs_list_entry(mirror->pg_list.prev); 1442 folio = nfs_page_to_folio(prev); 1443 if (folio) { 1444 if (index == folio_next_index(folio)) 1445 continue; 1446 } else if (index == prev->wb_index + 1) 1447 continue; 1448 /* 1449 * We will submit more requests after these. Indicate 1450 * this to the underlying layers. 1451 */ 1452 desc->pg_moreio = 1; 1453 nfs_pageio_complete(desc); 1454 break; 1455 } 1456 } 1457 } 1458 1459 /* 1460 * nfs_pageio_stop_mirroring - stop using mirroring (set mirror count to 1) 1461 */ 1462 void nfs_pageio_stop_mirroring(struct nfs_pageio_descriptor *pgio) 1463 { 1464 nfs_pageio_complete(pgio); 1465 } 1466 1467 int __init nfs_init_nfspagecache(void) 1468 { 1469 nfs_page_cachep = kmem_cache_create("nfs_page", 1470 sizeof(struct nfs_page), 1471 0, SLAB_HWCACHE_ALIGN, 1472 NULL); 1473 if (nfs_page_cachep == NULL) 1474 return -ENOMEM; 1475 1476 return 0; 1477 } 1478 1479 void nfs_destroy_nfspagecache(void) 1480 { 1481 kmem_cache_destroy(nfs_page_cachep); 1482 } 1483 1484 static const struct rpc_call_ops nfs_pgio_common_ops = { 1485 .rpc_call_prepare = nfs_pgio_prepare, 1486 .rpc_call_done = nfs_pgio_result, 1487 .rpc_release = nfs_pgio_release, 1488 }; 1489 1490 const struct nfs_pageio_ops nfs_pgio_rw_ops = { 1491 .pg_test = nfs_generic_pg_test, 1492 .pg_doio = nfs_generic_pg_pgios, 1493 }; 1494