1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/fs/nfs/write.c 4 * 5 * Write file data over NFS. 6 * 7 * Copyright (C) 1996, 1997, Olaf Kirch <[email protected]> 8 */ 9 10 #include <linux/types.h> 11 #include <linux/slab.h> 12 #include <linux/mm.h> 13 #include <linux/pagemap.h> 14 #include <linux/file.h> 15 #include <linux/writeback.h> 16 #include <linux/swap.h> 17 #include <linux/migrate.h> 18 19 #include <linux/sunrpc/clnt.h> 20 #include <linux/nfs_fs.h> 21 #include <linux/nfs_mount.h> 22 #include <linux/nfs_page.h> 23 #include <linux/backing-dev.h> 24 #include <linux/export.h> 25 #include <linux/freezer.h> 26 #include <linux/wait.h> 27 #include <linux/iversion.h> 28 #include <linux/filelock.h> 29 30 #include <linux/uaccess.h> 31 #include <linux/sched/mm.h> 32 33 #include "delegation.h" 34 #include "internal.h" 35 #include "iostat.h" 36 #include "nfs4_fs.h" 37 #include "fscache.h" 38 #include "pnfs.h" 39 40 #include "nfstrace.h" 41 42 #define NFSDBG_FACILITY NFSDBG_PAGECACHE 43 44 #define MIN_POOL_WRITE (32) 45 #define MIN_POOL_COMMIT (4) 46 47 struct nfs_io_completion { 48 void (*complete)(void *data); 49 void *data; 50 struct kref refcount; 51 }; 52 53 /* 54 * Local function declarations 55 */ 56 static void nfs_redirty_request(struct nfs_page *req); 57 static const struct rpc_call_ops nfs_commit_ops; 58 static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops; 59 static const struct nfs_commit_completion_ops nfs_commit_completion_ops; 60 static const struct nfs_rw_ops nfs_rw_write_ops; 61 static void nfs_inode_remove_request(struct nfs_page *req); 62 static void nfs_clear_request_commit(struct nfs_commit_info *cinfo, 63 struct nfs_page *req); 64 static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo, 65 struct inode *inode); 66 67 static struct kmem_cache *nfs_wdata_cachep; 68 static mempool_t *nfs_wdata_mempool; 69 static struct kmem_cache *nfs_cdata_cachep; 70 static mempool_t *nfs_commit_mempool; 71 72 struct nfs_commit_data *nfs_commitdata_alloc(void) 73 { 74 struct nfs_commit_data *p; 75 76 p = kmem_cache_zalloc(nfs_cdata_cachep, nfs_io_gfp_mask()); 77 if (!p) { 78 p = mempool_alloc(nfs_commit_mempool, GFP_NOWAIT); 79 if (!p) 80 return NULL; 81 memset(p, 0, sizeof(*p)); 82 } 83 INIT_LIST_HEAD(&p->pages); 84 return p; 85 } 86 EXPORT_SYMBOL_GPL(nfs_commitdata_alloc); 87 88 void nfs_commit_free(struct nfs_commit_data *p) 89 { 90 mempool_free(p, nfs_commit_mempool); 91 } 92 EXPORT_SYMBOL_GPL(nfs_commit_free); 93 94 static struct nfs_pgio_header *nfs_writehdr_alloc(void) 95 { 96 struct nfs_pgio_header *p; 97 98 p = kmem_cache_zalloc(nfs_wdata_cachep, nfs_io_gfp_mask()); 99 if (!p) { 100 p = mempool_alloc(nfs_wdata_mempool, GFP_NOWAIT); 101 if (!p) 102 return NULL; 103 memset(p, 0, sizeof(*p)); 104 } 105 p->rw_mode = FMODE_WRITE; 106 return p; 107 } 108 109 static void nfs_writehdr_free(struct nfs_pgio_header *hdr) 110 { 111 mempool_free(hdr, nfs_wdata_mempool); 112 } 113 114 static struct nfs_io_completion *nfs_io_completion_alloc(gfp_t gfp_flags) 115 { 116 return kmalloc(sizeof(struct nfs_io_completion), gfp_flags); 117 } 118 119 static void nfs_io_completion_init(struct nfs_io_completion *ioc, 120 void (*complete)(void *), void *data) 121 { 122 ioc->complete = complete; 123 ioc->data = data; 124 kref_init(&ioc->refcount); 125 } 126 127 static void nfs_io_completion_release(struct kref *kref) 128 { 129 struct nfs_io_completion *ioc = container_of(kref, 130 struct nfs_io_completion, refcount); 131 ioc->complete(ioc->data); 132 kfree(ioc); 133 } 134 135 static void nfs_io_completion_get(struct nfs_io_completion *ioc) 136 { 137 if (ioc != NULL) 138 kref_get(&ioc->refcount); 139 } 140 141 static void nfs_io_completion_put(struct nfs_io_completion *ioc) 142 { 143 if (ioc != NULL) 144 kref_put(&ioc->refcount, nfs_io_completion_release); 145 } 146 147 /** 148 * nfs_folio_find_head_request - find head request associated with a folio 149 * @folio: pointer to folio 150 * 151 * must be called while holding the inode lock. 152 * 153 * returns matching head request with reference held, or NULL if not found. 154 */ 155 static struct nfs_page *nfs_folio_find_head_request(struct folio *folio) 156 { 157 struct address_space *mapping = folio->mapping; 158 struct nfs_page *req; 159 160 if (!folio_test_private(folio)) 161 return NULL; 162 spin_lock(&mapping->i_private_lock); 163 req = folio->private; 164 if (req) { 165 WARN_ON_ONCE(req->wb_head != req); 166 kref_get(&req->wb_kref); 167 } 168 spin_unlock(&mapping->i_private_lock); 169 return req; 170 } 171 172 /* Adjust the file length if we're writing beyond the end */ 173 static void nfs_grow_file(struct folio *folio, unsigned int offset, 174 unsigned int count) 175 { 176 struct inode *inode = folio->mapping->host; 177 loff_t end, i_size; 178 pgoff_t end_index; 179 180 spin_lock(&inode->i_lock); 181 i_size = i_size_read(inode); 182 end_index = ((i_size - 1) >> folio_shift(folio)) << folio_order(folio); 183 if (i_size > 0 && folio->index < end_index) 184 goto out; 185 end = folio_pos(folio) + (loff_t)offset + (loff_t)count; 186 if (i_size >= end) 187 goto out; 188 trace_nfs_size_grow(inode, end); 189 i_size_write(inode, end); 190 NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE; 191 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE); 192 out: 193 /* Atomically update timestamps if they are delegated to us. */ 194 nfs_update_delegated_mtime_locked(inode); 195 spin_unlock(&inode->i_lock); 196 nfs_fscache_invalidate(inode, 0); 197 } 198 199 /* A writeback failed: mark the page as bad, and invalidate the page cache */ 200 static void nfs_set_pageerror(struct address_space *mapping) 201 { 202 struct inode *inode = mapping->host; 203 204 nfs_zap_mapping(mapping->host, mapping); 205 /* Force file size revalidation */ 206 spin_lock(&inode->i_lock); 207 nfs_set_cache_invalid(inode, NFS_INO_REVAL_FORCED | 208 NFS_INO_INVALID_CHANGE | 209 NFS_INO_INVALID_SIZE); 210 spin_unlock(&inode->i_lock); 211 } 212 213 static void nfs_mapping_set_error(struct folio *folio, int error) 214 { 215 struct address_space *mapping = folio->mapping; 216 217 filemap_set_wb_err(mapping, error); 218 if (mapping->host) 219 errseq_set(&mapping->host->i_sb->s_wb_err, 220 error == -ENOSPC ? -ENOSPC : -EIO); 221 nfs_set_pageerror(mapping); 222 } 223 224 /* 225 * nfs_page_group_search_locked 226 * @head - head request of page group 227 * @page_offset - offset into page 228 * 229 * Search page group with head @head to find a request that contains the 230 * page offset @page_offset. 231 * 232 * Returns a pointer to the first matching nfs request, or NULL if no 233 * match is found. 234 * 235 * Must be called with the page group lock held 236 */ 237 static struct nfs_page * 238 nfs_page_group_search_locked(struct nfs_page *head, unsigned int page_offset) 239 { 240 struct nfs_page *req; 241 242 req = head; 243 do { 244 if (page_offset >= req->wb_pgbase && 245 page_offset < (req->wb_pgbase + req->wb_bytes)) 246 return req; 247 248 req = req->wb_this_page; 249 } while (req != head); 250 251 return NULL; 252 } 253 254 /* 255 * nfs_page_group_covers_page 256 * @head - head request of page group 257 * 258 * Return true if the page group with head @head covers the whole page, 259 * returns false otherwise 260 */ 261 static bool nfs_page_group_covers_page(struct nfs_page *req) 262 { 263 unsigned int len = nfs_folio_length(nfs_page_to_folio(req)); 264 struct nfs_page *tmp; 265 unsigned int pos = 0; 266 267 nfs_page_group_lock(req); 268 269 for (;;) { 270 tmp = nfs_page_group_search_locked(req->wb_head, pos); 271 if (!tmp) 272 break; 273 pos = tmp->wb_pgbase + tmp->wb_bytes; 274 } 275 276 nfs_page_group_unlock(req); 277 return pos >= len; 278 } 279 280 /* We can set the PG_uptodate flag if we see that a write request 281 * covers the full page. 282 */ 283 static void nfs_mark_uptodate(struct nfs_page *req) 284 { 285 struct folio *folio = nfs_page_to_folio(req); 286 287 if (folio_test_uptodate(folio)) 288 return; 289 if (!nfs_page_group_covers_page(req)) 290 return; 291 folio_mark_uptodate(folio); 292 } 293 294 static int wb_priority(struct writeback_control *wbc) 295 { 296 int ret = 0; 297 298 if (wbc->sync_mode == WB_SYNC_ALL) 299 ret = FLUSH_COND_STABLE; 300 return ret; 301 } 302 303 /* 304 * NFS congestion control 305 */ 306 307 int nfs_congestion_kb; 308 309 #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10)) 310 #define NFS_CONGESTION_OFF_THRESH \ 311 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2)) 312 313 static void nfs_folio_set_writeback(struct folio *folio) 314 { 315 struct nfs_server *nfss = NFS_SERVER(folio->mapping->host); 316 317 folio_start_writeback(folio); 318 if (atomic_long_inc_return(&nfss->writeback) > NFS_CONGESTION_ON_THRESH) 319 nfss->write_congested = 1; 320 } 321 322 static void nfs_folio_end_writeback(struct folio *folio) 323 { 324 struct nfs_server *nfss = NFS_SERVER(folio->mapping->host); 325 326 folio_end_writeback(folio); 327 if (atomic_long_dec_return(&nfss->writeback) < 328 NFS_CONGESTION_OFF_THRESH) { 329 nfss->write_congested = 0; 330 wake_up_all(&nfss->write_congestion_wait); 331 } 332 } 333 334 static void nfs_page_end_writeback(struct nfs_page *req) 335 { 336 if (nfs_page_group_sync_on_bit(req, PG_WB_END)) { 337 nfs_unlock_request(req); 338 nfs_folio_end_writeback(nfs_page_to_folio(req)); 339 } else 340 nfs_unlock_request(req); 341 } 342 343 /* 344 * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests 345 * 346 * @destroy_list - request list (using wb_this_page) terminated by @old_head 347 * @old_head - the old head of the list 348 * 349 * All subrequests must be locked and removed from all lists, so at this point 350 * they are only "active" in this function, and possibly in nfs_wait_on_request 351 * with a reference held by some other context. 352 */ 353 static void 354 nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list, 355 struct nfs_page *old_head, 356 struct inode *inode) 357 { 358 while (destroy_list) { 359 struct nfs_page *subreq = destroy_list; 360 361 destroy_list = (subreq->wb_this_page == old_head) ? 362 NULL : subreq->wb_this_page; 363 364 /* Note: lock subreq in order to change subreq->wb_head */ 365 nfs_page_set_headlock(subreq); 366 WARN_ON_ONCE(old_head != subreq->wb_head); 367 368 /* make sure old group is not used */ 369 subreq->wb_this_page = subreq; 370 subreq->wb_head = subreq; 371 372 clear_bit(PG_REMOVE, &subreq->wb_flags); 373 374 /* Note: races with nfs_page_group_destroy() */ 375 if (!kref_read(&subreq->wb_kref)) { 376 /* Check if we raced with nfs_page_group_destroy() */ 377 if (test_and_clear_bit(PG_TEARDOWN, &subreq->wb_flags)) { 378 nfs_page_clear_headlock(subreq); 379 nfs_free_request(subreq); 380 } else 381 nfs_page_clear_headlock(subreq); 382 continue; 383 } 384 nfs_page_clear_headlock(subreq); 385 386 nfs_release_request(old_head); 387 388 if (test_and_clear_bit(PG_INODE_REF, &subreq->wb_flags)) { 389 nfs_release_request(subreq); 390 atomic_long_dec(&NFS_I(inode)->nrequests); 391 } 392 393 /* subreq is now totally disconnected from page group or any 394 * write / commit lists. last chance to wake any waiters */ 395 nfs_unlock_and_release_request(subreq); 396 } 397 } 398 399 /* 400 * nfs_join_page_group - destroy subrequests of the head req 401 * @head: the page used to lookup the "page group" of nfs_page structures 402 * @inode: Inode to which the request belongs. 403 * 404 * This function joins all sub requests to the head request by first 405 * locking all requests in the group, cancelling any pending operations 406 * and finally updating the head request to cover the whole range covered by 407 * the (former) group. All subrequests are removed from any write or commit 408 * lists, unlinked from the group and destroyed. 409 */ 410 void nfs_join_page_group(struct nfs_page *head, struct nfs_commit_info *cinfo, 411 struct inode *inode) 412 { 413 struct nfs_page *subreq; 414 struct nfs_page *destroy_list = NULL; 415 unsigned int pgbase, off, bytes; 416 417 pgbase = head->wb_pgbase; 418 bytes = head->wb_bytes; 419 off = head->wb_offset; 420 for (subreq = head->wb_this_page; subreq != head; 421 subreq = subreq->wb_this_page) { 422 /* Subrequests should always form a contiguous range */ 423 if (pgbase > subreq->wb_pgbase) { 424 off -= pgbase - subreq->wb_pgbase; 425 bytes += pgbase - subreq->wb_pgbase; 426 pgbase = subreq->wb_pgbase; 427 } 428 bytes = max(subreq->wb_pgbase + subreq->wb_bytes 429 - pgbase, bytes); 430 } 431 432 /* Set the head request's range to cover the former page group */ 433 head->wb_pgbase = pgbase; 434 head->wb_bytes = bytes; 435 head->wb_offset = off; 436 437 /* Now that all requests are locked, make sure they aren't on any list. 438 * Commit list removal accounting is done after locks are dropped */ 439 subreq = head; 440 do { 441 nfs_clear_request_commit(cinfo, subreq); 442 subreq = subreq->wb_this_page; 443 } while (subreq != head); 444 445 /* unlink subrequests from head, destroy them later */ 446 if (head->wb_this_page != head) { 447 /* destroy list will be terminated by head */ 448 destroy_list = head->wb_this_page; 449 head->wb_this_page = head; 450 } 451 452 nfs_destroy_unlinked_subrequests(destroy_list, head, inode); 453 } 454 455 /** 456 * nfs_wait_on_request - Wait for a request to complete. 457 * @req: request to wait upon. 458 * 459 * Interruptible by fatal signals only. 460 * The user is responsible for holding a count on the request. 461 */ 462 static int nfs_wait_on_request(struct nfs_page *req) 463 { 464 if (!test_bit(PG_BUSY, &req->wb_flags)) 465 return 0; 466 set_bit(PG_CONTENDED2, &req->wb_flags); 467 smp_mb__after_atomic(); 468 return wait_on_bit_io(&req->wb_flags, PG_BUSY, 469 TASK_UNINTERRUPTIBLE); 470 } 471 472 /* 473 * nfs_unroll_locks - unlock all newly locked reqs and wait on @req 474 * @head: head request of page group, must be holding head lock 475 * @req: request that couldn't lock and needs to wait on the req bit lock 476 * 477 * This is a helper function for nfs_lock_and_join_requests 478 * returns 0 on success, < 0 on error. 479 */ 480 static void 481 nfs_unroll_locks(struct nfs_page *head, struct nfs_page *req) 482 { 483 struct nfs_page *tmp; 484 485 /* relinquish all the locks successfully grabbed this run */ 486 for (tmp = head->wb_this_page ; tmp != req; tmp = tmp->wb_this_page) { 487 if (!kref_read(&tmp->wb_kref)) 488 continue; 489 nfs_unlock_and_release_request(tmp); 490 } 491 } 492 493 /* 494 * nfs_page_group_lock_subreq - try to lock a subrequest 495 * @head: head request of page group 496 * @subreq: request to lock 497 * 498 * This is a helper function for nfs_lock_and_join_requests which 499 * must be called with the head request and page group both locked. 500 * On error, it returns with the page group unlocked. 501 */ 502 static int 503 nfs_page_group_lock_subreq(struct nfs_page *head, struct nfs_page *subreq) 504 { 505 int ret; 506 507 if (!kref_get_unless_zero(&subreq->wb_kref)) 508 return 0; 509 while (!nfs_lock_request(subreq)) { 510 nfs_page_group_unlock(head); 511 ret = nfs_wait_on_request(subreq); 512 if (!ret) 513 ret = nfs_page_group_lock(head); 514 if (ret < 0) { 515 nfs_unroll_locks(head, subreq); 516 nfs_release_request(subreq); 517 return ret; 518 } 519 } 520 return 0; 521 } 522 523 /* 524 * nfs_lock_and_join_requests - join all subreqs to the head req 525 * @folio: the folio used to lookup the "page group" of nfs_page structures 526 * 527 * This function joins all sub requests to the head request by first 528 * locking all requests in the group, cancelling any pending operations 529 * and finally updating the head request to cover the whole range covered by 530 * the (former) group. All subrequests are removed from any write or commit 531 * lists, unlinked from the group and destroyed. 532 * 533 * Returns a locked, referenced pointer to the head request - which after 534 * this call is guaranteed to be the only request associated with the page. 535 * Returns NULL if no requests are found for @folio, or a ERR_PTR if an 536 * error was encountered. 537 */ 538 static struct nfs_page *nfs_lock_and_join_requests(struct folio *folio) 539 { 540 struct inode *inode = folio->mapping->host; 541 struct nfs_page *head, *subreq; 542 struct nfs_commit_info cinfo; 543 bool removed; 544 int ret; 545 546 /* 547 * A reference is taken only on the head request which acts as a 548 * reference to the whole page group - the group will not be destroyed 549 * until the head reference is released. 550 */ 551 retry: 552 head = nfs_folio_find_head_request(folio); 553 if (!head) 554 return NULL; 555 556 while (!nfs_lock_request(head)) { 557 ret = nfs_wait_on_request(head); 558 if (ret < 0) 559 return ERR_PTR(ret); 560 } 561 562 /* Ensure that nobody removed the request before we locked it */ 563 if (head != folio->private) { 564 nfs_unlock_and_release_request(head); 565 goto retry; 566 } 567 568 ret = nfs_page_group_lock(head); 569 if (ret < 0) 570 goto out_unlock; 571 572 removed = test_bit(PG_REMOVE, &head->wb_flags); 573 574 /* lock each request in the page group */ 575 for (subreq = head->wb_this_page; 576 subreq != head; 577 subreq = subreq->wb_this_page) { 578 if (test_bit(PG_REMOVE, &subreq->wb_flags)) 579 removed = true; 580 ret = nfs_page_group_lock_subreq(head, subreq); 581 if (ret < 0) 582 goto out_unlock; 583 } 584 585 nfs_page_group_unlock(head); 586 587 /* 588 * If PG_REMOVE is set on any request, I/O on that request has 589 * completed, but some requests were still under I/O at the time 590 * we locked the head request. 591 * 592 * In that case the above wait for all requests means that all I/O 593 * has now finished, and we can restart from a clean slate. Let the 594 * old requests go away and start from scratch instead. 595 */ 596 if (removed) { 597 nfs_unroll_locks(head, head); 598 nfs_unlock_and_release_request(head); 599 goto retry; 600 } 601 602 nfs_init_cinfo_from_inode(&cinfo, inode); 603 nfs_join_page_group(head, &cinfo, inode); 604 return head; 605 606 out_unlock: 607 nfs_unlock_and_release_request(head); 608 return ERR_PTR(ret); 609 } 610 611 static void nfs_write_error(struct nfs_page *req, int error) 612 { 613 trace_nfs_write_error(nfs_page_to_inode(req), req, error); 614 nfs_mapping_set_error(nfs_page_to_folio(req), error); 615 nfs_inode_remove_request(req); 616 nfs_page_end_writeback(req); 617 nfs_release_request(req); 618 } 619 620 /* 621 * Find an associated nfs write request, and prepare to flush it out 622 * May return an error if the user signalled nfs_wait_on_request(). 623 */ 624 static int nfs_page_async_flush(struct folio *folio, 625 struct writeback_control *wbc, 626 struct nfs_pageio_descriptor *pgio) 627 { 628 struct nfs_page *req; 629 int ret = 0; 630 631 req = nfs_lock_and_join_requests(folio); 632 if (!req) 633 goto out; 634 ret = PTR_ERR(req); 635 if (IS_ERR(req)) 636 goto out; 637 638 nfs_folio_set_writeback(folio); 639 WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags)); 640 641 /* If there is a fatal error that covers this write, just exit */ 642 ret = pgio->pg_error; 643 if (nfs_error_is_fatal_on_server(ret)) 644 goto out_launder; 645 646 ret = 0; 647 if (!nfs_pageio_add_request(pgio, req)) { 648 ret = pgio->pg_error; 649 /* 650 * Remove the problematic req upon fatal errors on the server 651 */ 652 if (nfs_error_is_fatal_on_server(ret)) 653 goto out_launder; 654 if (wbc->sync_mode == WB_SYNC_NONE) 655 ret = AOP_WRITEPAGE_ACTIVATE; 656 folio_redirty_for_writepage(wbc, folio); 657 nfs_redirty_request(req); 658 pgio->pg_error = 0; 659 } else 660 nfs_add_stats(folio->mapping->host, 661 NFSIOS_WRITEPAGES, 1); 662 out: 663 return ret; 664 out_launder: 665 nfs_write_error(req, ret); 666 return 0; 667 } 668 669 static int nfs_do_writepage(struct folio *folio, struct writeback_control *wbc, 670 struct nfs_pageio_descriptor *pgio) 671 { 672 nfs_pageio_cond_complete(pgio, folio->index); 673 return nfs_page_async_flush(folio, wbc, pgio); 674 } 675 676 /* 677 * Write an mmapped page to the server. 678 */ 679 static int nfs_writepage_locked(struct folio *folio, 680 struct writeback_control *wbc) 681 { 682 struct nfs_pageio_descriptor pgio; 683 struct inode *inode = folio->mapping->host; 684 int err; 685 686 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); 687 nfs_pageio_init_write(&pgio, inode, 0, false, 688 &nfs_async_write_completion_ops); 689 err = nfs_do_writepage(folio, wbc, &pgio); 690 pgio.pg_error = 0; 691 nfs_pageio_complete(&pgio); 692 return err; 693 } 694 695 static int nfs_writepages_callback(struct folio *folio, 696 struct writeback_control *wbc, void *data) 697 { 698 int ret; 699 700 ret = nfs_do_writepage(folio, wbc, data); 701 if (ret != AOP_WRITEPAGE_ACTIVATE) 702 folio_unlock(folio); 703 return ret; 704 } 705 706 static void nfs_io_completion_commit(void *inode) 707 { 708 nfs_commit_inode(inode, 0); 709 } 710 711 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) 712 { 713 struct inode *inode = mapping->host; 714 struct nfs_pageio_descriptor pgio; 715 struct nfs_io_completion *ioc = NULL; 716 unsigned int mntflags = NFS_SERVER(inode)->flags; 717 struct nfs_server *nfss = NFS_SERVER(inode); 718 int priority = 0; 719 int err; 720 721 /* Wait with writeback until write congestion eases */ 722 if (wbc->sync_mode == WB_SYNC_NONE && nfss->write_congested) { 723 err = wait_event_killable(nfss->write_congestion_wait, 724 nfss->write_congested == 0); 725 if (err) 726 return err; 727 } 728 729 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES); 730 731 if (!(mntflags & NFS_MOUNT_WRITE_EAGER) || wbc->for_kupdate || 732 wbc->for_background || wbc->for_sync || wbc->for_reclaim) { 733 ioc = nfs_io_completion_alloc(GFP_KERNEL); 734 if (ioc) 735 nfs_io_completion_init(ioc, nfs_io_completion_commit, 736 inode); 737 priority = wb_priority(wbc); 738 } 739 740 do { 741 nfs_pageio_init_write(&pgio, inode, priority, false, 742 &nfs_async_write_completion_ops); 743 pgio.pg_io_completion = ioc; 744 err = write_cache_pages(mapping, wbc, nfs_writepages_callback, 745 &pgio); 746 pgio.pg_error = 0; 747 nfs_pageio_complete(&pgio); 748 if (err == -EAGAIN && mntflags & NFS_MOUNT_SOFTERR) 749 break; 750 } while (err < 0 && !nfs_error_is_fatal(err)); 751 nfs_io_completion_put(ioc); 752 753 if (err < 0) 754 goto out_err; 755 return 0; 756 out_err: 757 return err; 758 } 759 760 /* 761 * Insert a write request into an inode 762 */ 763 static void nfs_inode_add_request(struct nfs_page *req) 764 { 765 struct folio *folio = nfs_page_to_folio(req); 766 struct address_space *mapping = folio->mapping; 767 struct nfs_inode *nfsi = NFS_I(mapping->host); 768 769 WARN_ON_ONCE(req->wb_this_page != req); 770 771 /* Lock the request! */ 772 nfs_lock_request(req); 773 spin_lock(&mapping->i_private_lock); 774 set_bit(PG_MAPPED, &req->wb_flags); 775 folio_attach_private(folio, req); 776 spin_unlock(&mapping->i_private_lock); 777 atomic_long_inc(&nfsi->nrequests); 778 /* this a head request for a page group - mark it as having an 779 * extra reference so sub groups can follow suit. 780 * This flag also informs pgio layer when to bump nrequests when 781 * adding subrequests. */ 782 WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags)); 783 kref_get(&req->wb_kref); 784 } 785 786 /* 787 * Remove a write request from an inode 788 */ 789 static void nfs_inode_remove_request(struct nfs_page *req) 790 { 791 struct nfs_inode *nfsi = NFS_I(nfs_page_to_inode(req)); 792 793 if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) { 794 struct folio *folio = nfs_page_to_folio(req->wb_head); 795 struct address_space *mapping = folio->mapping; 796 797 spin_lock(&mapping->i_private_lock); 798 if (likely(folio)) { 799 folio_detach_private(folio); 800 clear_bit(PG_MAPPED, &req->wb_head->wb_flags); 801 } 802 spin_unlock(&mapping->i_private_lock); 803 } 804 805 if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags)) { 806 atomic_long_dec(&nfsi->nrequests); 807 nfs_release_request(req); 808 } 809 } 810 811 static void nfs_mark_request_dirty(struct nfs_page *req) 812 { 813 struct folio *folio = nfs_page_to_folio(req); 814 if (folio) 815 filemap_dirty_folio(folio_mapping(folio), folio); 816 } 817 818 /** 819 * nfs_request_add_commit_list_locked - add request to a commit list 820 * @req: pointer to a struct nfs_page 821 * @dst: commit list head 822 * @cinfo: holds list lock and accounting info 823 * 824 * This sets the PG_CLEAN bit, updates the cinfo count of 825 * number of outstanding requests requiring a commit as well as 826 * the MM page stats. 827 * 828 * The caller must hold NFS_I(cinfo->inode)->commit_mutex, and the 829 * nfs_page lock. 830 */ 831 void 832 nfs_request_add_commit_list_locked(struct nfs_page *req, struct list_head *dst, 833 struct nfs_commit_info *cinfo) 834 { 835 set_bit(PG_CLEAN, &req->wb_flags); 836 nfs_list_add_request(req, dst); 837 atomic_long_inc(&cinfo->mds->ncommit); 838 } 839 EXPORT_SYMBOL_GPL(nfs_request_add_commit_list_locked); 840 841 /** 842 * nfs_request_add_commit_list - add request to a commit list 843 * @req: pointer to a struct nfs_page 844 * @cinfo: holds list lock and accounting info 845 * 846 * This sets the PG_CLEAN bit, updates the cinfo count of 847 * number of outstanding requests requiring a commit as well as 848 * the MM page stats. 849 * 850 * The caller must _not_ hold the cinfo->lock, but must be 851 * holding the nfs_page lock. 852 */ 853 void 854 nfs_request_add_commit_list(struct nfs_page *req, struct nfs_commit_info *cinfo) 855 { 856 mutex_lock(&NFS_I(cinfo->inode)->commit_mutex); 857 nfs_request_add_commit_list_locked(req, &cinfo->mds->list, cinfo); 858 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex); 859 nfs_folio_mark_unstable(nfs_page_to_folio(req), cinfo); 860 } 861 EXPORT_SYMBOL_GPL(nfs_request_add_commit_list); 862 863 /** 864 * nfs_request_remove_commit_list - Remove request from a commit list 865 * @req: pointer to a nfs_page 866 * @cinfo: holds list lock and accounting info 867 * 868 * This clears the PG_CLEAN bit, and updates the cinfo's count of 869 * number of outstanding requests requiring a commit 870 * It does not update the MM page stats. 871 * 872 * The caller _must_ hold the cinfo->lock and the nfs_page lock. 873 */ 874 void 875 nfs_request_remove_commit_list(struct nfs_page *req, 876 struct nfs_commit_info *cinfo) 877 { 878 if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) 879 return; 880 nfs_list_remove_request(req); 881 atomic_long_dec(&cinfo->mds->ncommit); 882 } 883 EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list); 884 885 static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo, 886 struct inode *inode) 887 { 888 cinfo->inode = inode; 889 cinfo->mds = &NFS_I(inode)->commit_info; 890 cinfo->ds = pnfs_get_ds_info(inode); 891 cinfo->dreq = NULL; 892 cinfo->completion_ops = &nfs_commit_completion_ops; 893 } 894 895 void nfs_init_cinfo(struct nfs_commit_info *cinfo, 896 struct inode *inode, 897 struct nfs_direct_req *dreq) 898 { 899 if (dreq) 900 nfs_init_cinfo_from_dreq(cinfo, dreq); 901 else 902 nfs_init_cinfo_from_inode(cinfo, inode); 903 } 904 EXPORT_SYMBOL_GPL(nfs_init_cinfo); 905 906 /* 907 * Add a request to the inode's commit list. 908 */ 909 void 910 nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg, 911 struct nfs_commit_info *cinfo, u32 ds_commit_idx) 912 { 913 if (pnfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx)) 914 return; 915 nfs_request_add_commit_list(req, cinfo); 916 } 917 918 static void nfs_folio_clear_commit(struct folio *folio) 919 { 920 if (folio) { 921 long nr = folio_nr_pages(folio); 922 923 node_stat_mod_folio(folio, NR_WRITEBACK, -nr); 924 wb_stat_mod(&inode_to_bdi(folio->mapping->host)->wb, 925 WB_WRITEBACK, -nr); 926 } 927 } 928 929 /* Called holding the request lock on @req */ 930 static void nfs_clear_request_commit(struct nfs_commit_info *cinfo, 931 struct nfs_page *req) 932 { 933 if (test_bit(PG_CLEAN, &req->wb_flags)) { 934 struct nfs_open_context *ctx = nfs_req_openctx(req); 935 struct inode *inode = d_inode(ctx->dentry); 936 937 mutex_lock(&NFS_I(inode)->commit_mutex); 938 if (!pnfs_clear_request_commit(req, cinfo)) { 939 nfs_request_remove_commit_list(req, cinfo); 940 } 941 mutex_unlock(&NFS_I(inode)->commit_mutex); 942 nfs_folio_clear_commit(nfs_page_to_folio(req)); 943 } 944 } 945 946 int nfs_write_need_commit(struct nfs_pgio_header *hdr) 947 { 948 if (hdr->verf.committed == NFS_DATA_SYNC) 949 return hdr->lseg == NULL; 950 return hdr->verf.committed != NFS_FILE_SYNC; 951 } 952 953 static void nfs_async_write_init(struct nfs_pgio_header *hdr) 954 { 955 nfs_io_completion_get(hdr->io_completion); 956 } 957 958 static void nfs_write_completion(struct nfs_pgio_header *hdr) 959 { 960 struct nfs_commit_info cinfo; 961 unsigned long bytes = 0; 962 963 if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) 964 goto out; 965 nfs_init_cinfo_from_inode(&cinfo, hdr->inode); 966 while (!list_empty(&hdr->pages)) { 967 struct nfs_page *req = nfs_list_entry(hdr->pages.next); 968 969 bytes += req->wb_bytes; 970 nfs_list_remove_request(req); 971 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && 972 (hdr->good_bytes < bytes)) { 973 trace_nfs_comp_error(hdr->inode, req, hdr->error); 974 nfs_mapping_set_error(nfs_page_to_folio(req), 975 hdr->error); 976 goto remove_req; 977 } 978 if (nfs_write_need_commit(hdr)) { 979 /* Reset wb_nio, since the write was successful. */ 980 req->wb_nio = 0; 981 memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf)); 982 nfs_mark_request_commit(req, hdr->lseg, &cinfo, 983 hdr->pgio_mirror_idx); 984 goto next; 985 } 986 remove_req: 987 nfs_inode_remove_request(req); 988 next: 989 nfs_page_end_writeback(req); 990 nfs_release_request(req); 991 } 992 out: 993 nfs_io_completion_put(hdr->io_completion); 994 hdr->release(hdr); 995 } 996 997 unsigned long 998 nfs_reqs_to_commit(struct nfs_commit_info *cinfo) 999 { 1000 return atomic_long_read(&cinfo->mds->ncommit); 1001 } 1002 1003 /* NFS_I(cinfo->inode)->commit_mutex held by caller */ 1004 int 1005 nfs_scan_commit_list(struct list_head *src, struct list_head *dst, 1006 struct nfs_commit_info *cinfo, int max) 1007 { 1008 struct nfs_page *req, *tmp; 1009 int ret = 0; 1010 1011 list_for_each_entry_safe(req, tmp, src, wb_list) { 1012 kref_get(&req->wb_kref); 1013 if (!nfs_lock_request(req)) { 1014 nfs_release_request(req); 1015 continue; 1016 } 1017 nfs_request_remove_commit_list(req, cinfo); 1018 clear_bit(PG_COMMIT_TO_DS, &req->wb_flags); 1019 nfs_list_add_request(req, dst); 1020 ret++; 1021 if ((ret == max) && !cinfo->dreq) 1022 break; 1023 cond_resched(); 1024 } 1025 return ret; 1026 } 1027 EXPORT_SYMBOL_GPL(nfs_scan_commit_list); 1028 1029 /* 1030 * nfs_scan_commit - Scan an inode for commit requests 1031 * @inode: NFS inode to scan 1032 * @dst: mds destination list 1033 * @cinfo: mds and ds lists of reqs ready to commit 1034 * 1035 * Moves requests from the inode's 'commit' request list. 1036 * The requests are *not* checked to ensure that they form a contiguous set. 1037 */ 1038 int 1039 nfs_scan_commit(struct inode *inode, struct list_head *dst, 1040 struct nfs_commit_info *cinfo) 1041 { 1042 int ret = 0; 1043 1044 if (!atomic_long_read(&cinfo->mds->ncommit)) 1045 return 0; 1046 mutex_lock(&NFS_I(cinfo->inode)->commit_mutex); 1047 if (atomic_long_read(&cinfo->mds->ncommit) > 0) { 1048 const int max = INT_MAX; 1049 1050 ret = nfs_scan_commit_list(&cinfo->mds->list, dst, 1051 cinfo, max); 1052 ret += pnfs_scan_commit_lists(inode, cinfo, max - ret); 1053 } 1054 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex); 1055 return ret; 1056 } 1057 1058 /* 1059 * Search for an existing write request, and attempt to update 1060 * it to reflect a new dirty region on a given page. 1061 * 1062 * If the attempt fails, then the existing request is flushed out 1063 * to disk. 1064 */ 1065 static struct nfs_page *nfs_try_to_update_request(struct folio *folio, 1066 unsigned int offset, 1067 unsigned int bytes) 1068 { 1069 struct nfs_page *req; 1070 unsigned int rqend; 1071 unsigned int end; 1072 int error; 1073 1074 end = offset + bytes; 1075 1076 req = nfs_lock_and_join_requests(folio); 1077 if (IS_ERR_OR_NULL(req)) 1078 return req; 1079 1080 rqend = req->wb_offset + req->wb_bytes; 1081 /* 1082 * Tell the caller to flush out the request if 1083 * the offsets are non-contiguous. 1084 * Note: nfs_flush_incompatible() will already 1085 * have flushed out requests having wrong owners. 1086 */ 1087 if (offset > rqend || end < req->wb_offset) 1088 goto out_flushme; 1089 1090 /* Okay, the request matches. Update the region */ 1091 if (offset < req->wb_offset) { 1092 req->wb_offset = offset; 1093 req->wb_pgbase = offset; 1094 } 1095 if (end > rqend) 1096 req->wb_bytes = end - req->wb_offset; 1097 else 1098 req->wb_bytes = rqend - req->wb_offset; 1099 req->wb_nio = 0; 1100 return req; 1101 out_flushme: 1102 /* 1103 * Note: we mark the request dirty here because 1104 * nfs_lock_and_join_requests() cannot preserve 1105 * commit flags, so we have to replay the write. 1106 */ 1107 nfs_mark_request_dirty(req); 1108 nfs_unlock_and_release_request(req); 1109 error = nfs_wb_folio(folio->mapping->host, folio); 1110 return (error < 0) ? ERR_PTR(error) : NULL; 1111 } 1112 1113 /* 1114 * Try to update an existing write request, or create one if there is none. 1115 * 1116 * Note: Should always be called with the Page Lock held to prevent races 1117 * if we have to add a new request. Also assumes that the caller has 1118 * already called nfs_flush_incompatible() if necessary. 1119 */ 1120 static struct nfs_page *nfs_setup_write_request(struct nfs_open_context *ctx, 1121 struct folio *folio, 1122 unsigned int offset, 1123 unsigned int bytes) 1124 { 1125 struct nfs_page *req; 1126 1127 req = nfs_try_to_update_request(folio, offset, bytes); 1128 if (req != NULL) 1129 goto out; 1130 req = nfs_page_create_from_folio(ctx, folio, offset, bytes); 1131 if (IS_ERR(req)) 1132 goto out; 1133 nfs_inode_add_request(req); 1134 out: 1135 return req; 1136 } 1137 1138 static int nfs_writepage_setup(struct nfs_open_context *ctx, 1139 struct folio *folio, unsigned int offset, 1140 unsigned int count) 1141 { 1142 struct nfs_page *req; 1143 1144 req = nfs_setup_write_request(ctx, folio, offset, count); 1145 if (IS_ERR(req)) 1146 return PTR_ERR(req); 1147 /* Update file length */ 1148 nfs_grow_file(folio, offset, count); 1149 nfs_mark_uptodate(req); 1150 nfs_mark_request_dirty(req); 1151 nfs_unlock_and_release_request(req); 1152 return 0; 1153 } 1154 1155 int nfs_flush_incompatible(struct file *file, struct folio *folio) 1156 { 1157 struct nfs_open_context *ctx = nfs_file_open_context(file); 1158 struct nfs_lock_context *l_ctx; 1159 struct file_lock_context *flctx = locks_inode_context(file_inode(file)); 1160 struct nfs_page *req; 1161 int do_flush, status; 1162 /* 1163 * Look for a request corresponding to this page. If there 1164 * is one, and it belongs to another file, we flush it out 1165 * before we try to copy anything into the page. Do this 1166 * due to the lack of an ACCESS-type call in NFSv2. 1167 * Also do the same if we find a request from an existing 1168 * dropped page. 1169 */ 1170 do { 1171 req = nfs_folio_find_head_request(folio); 1172 if (req == NULL) 1173 return 0; 1174 l_ctx = req->wb_lock_context; 1175 do_flush = nfs_page_to_folio(req) != folio || 1176 !nfs_match_open_context(nfs_req_openctx(req), ctx); 1177 if (l_ctx && flctx && 1178 !(list_empty_careful(&flctx->flc_posix) && 1179 list_empty_careful(&flctx->flc_flock))) { 1180 do_flush |= l_ctx->lockowner != current->files; 1181 } 1182 nfs_release_request(req); 1183 if (!do_flush) 1184 return 0; 1185 status = nfs_wb_folio(folio->mapping->host, folio); 1186 } while (status == 0); 1187 return status; 1188 } 1189 1190 /* 1191 * Avoid buffered writes when a open context credential's key would 1192 * expire soon. 1193 * 1194 * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL. 1195 * 1196 * Return 0 and set a credential flag which triggers the inode to flush 1197 * and performs NFS_FILE_SYNC writes if the key will expired within 1198 * RPC_KEY_EXPIRE_TIMEO. 1199 */ 1200 int 1201 nfs_key_timeout_notify(struct file *filp, struct inode *inode) 1202 { 1203 struct nfs_open_context *ctx = nfs_file_open_context(filp); 1204 1205 if (nfs_ctx_key_to_expire(ctx, inode) && 1206 !rcu_access_pointer(ctx->ll_cred)) 1207 /* Already expired! */ 1208 return -EACCES; 1209 return 0; 1210 } 1211 1212 /* 1213 * Test if the open context credential key is marked to expire soon. 1214 */ 1215 bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx, struct inode *inode) 1216 { 1217 struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth; 1218 struct rpc_cred *cred, *new, *old = NULL; 1219 struct auth_cred acred = { 1220 .cred = ctx->cred, 1221 }; 1222 bool ret = false; 1223 1224 rcu_read_lock(); 1225 cred = rcu_dereference(ctx->ll_cred); 1226 if (cred && !(cred->cr_ops->crkey_timeout && 1227 cred->cr_ops->crkey_timeout(cred))) 1228 goto out; 1229 rcu_read_unlock(); 1230 1231 new = auth->au_ops->lookup_cred(auth, &acred, 0); 1232 if (new == cred) { 1233 put_rpccred(new); 1234 return true; 1235 } 1236 if (IS_ERR_OR_NULL(new)) { 1237 new = NULL; 1238 ret = true; 1239 } else if (new->cr_ops->crkey_timeout && 1240 new->cr_ops->crkey_timeout(new)) 1241 ret = true; 1242 1243 rcu_read_lock(); 1244 old = rcu_dereference_protected(xchg(&ctx->ll_cred, 1245 RCU_INITIALIZER(new)), 1); 1246 out: 1247 rcu_read_unlock(); 1248 put_rpccred(old); 1249 return ret; 1250 } 1251 1252 /* 1253 * If the page cache is marked as unsafe or invalid, then we can't rely on 1254 * the PageUptodate() flag. In this case, we will need to turn off 1255 * write optimisations that depend on the page contents being correct. 1256 */ 1257 static bool nfs_folio_write_uptodate(struct folio *folio, unsigned int pagelen) 1258 { 1259 struct inode *inode = folio->mapping->host; 1260 struct nfs_inode *nfsi = NFS_I(inode); 1261 1262 if (nfs_have_delegated_attributes(inode)) 1263 goto out; 1264 if (nfsi->cache_validity & 1265 (NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE)) 1266 return false; 1267 smp_rmb(); 1268 if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags) && pagelen != 0) 1269 return false; 1270 out: 1271 if (nfsi->cache_validity & NFS_INO_INVALID_DATA && pagelen != 0) 1272 return false; 1273 return folio_test_uptodate(folio) != 0; 1274 } 1275 1276 static bool 1277 is_whole_file_wrlock(struct file_lock *fl) 1278 { 1279 return fl->fl_start == 0 && fl->fl_end == OFFSET_MAX && 1280 lock_is_write(fl); 1281 } 1282 1283 /* If we know the page is up to date, and we're not using byte range locks (or 1284 * if we have the whole file locked for writing), it may be more efficient to 1285 * extend the write to cover the entire page in order to avoid fragmentation 1286 * inefficiencies. 1287 * 1288 * If the file is opened for synchronous writes then we can just skip the rest 1289 * of the checks. 1290 */ 1291 static int nfs_can_extend_write(struct file *file, struct folio *folio, 1292 unsigned int pagelen) 1293 { 1294 struct inode *inode = file_inode(file); 1295 struct file_lock_context *flctx = locks_inode_context(inode); 1296 struct file_lock *fl; 1297 int ret; 1298 1299 if (file->f_flags & O_DSYNC) 1300 return 0; 1301 if (!nfs_folio_write_uptodate(folio, pagelen)) 1302 return 0; 1303 if (nfs_have_write_delegation(inode)) 1304 return 1; 1305 if (!flctx || (list_empty_careful(&flctx->flc_flock) && 1306 list_empty_careful(&flctx->flc_posix))) 1307 return 1; 1308 1309 /* Check to see if there are whole file write locks */ 1310 ret = 0; 1311 spin_lock(&flctx->flc_lock); 1312 if (!list_empty(&flctx->flc_posix)) { 1313 fl = list_first_entry(&flctx->flc_posix, struct file_lock, 1314 c.flc_list); 1315 if (is_whole_file_wrlock(fl)) 1316 ret = 1; 1317 } else if (!list_empty(&flctx->flc_flock)) { 1318 fl = list_first_entry(&flctx->flc_flock, struct file_lock, 1319 c.flc_list); 1320 if (lock_is_write(fl)) 1321 ret = 1; 1322 } 1323 spin_unlock(&flctx->flc_lock); 1324 return ret; 1325 } 1326 1327 /* 1328 * Update and possibly write a cached page of an NFS file. 1329 * 1330 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad 1331 * things with a page scheduled for an RPC call (e.g. invalidate it). 1332 */ 1333 int nfs_update_folio(struct file *file, struct folio *folio, 1334 unsigned int offset, unsigned int count) 1335 { 1336 struct nfs_open_context *ctx = nfs_file_open_context(file); 1337 struct address_space *mapping = folio->mapping; 1338 struct inode *inode = mapping->host; 1339 unsigned int pagelen = nfs_folio_length(folio); 1340 int status = 0; 1341 1342 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); 1343 1344 dprintk("NFS: nfs_update_folio(%pD2 %d@%lld)\n", file, count, 1345 (long long)(folio_pos(folio) + offset)); 1346 1347 if (!count) 1348 goto out; 1349 1350 if (nfs_can_extend_write(file, folio, pagelen)) { 1351 unsigned int end = count + offset; 1352 1353 offset = round_down(offset, PAGE_SIZE); 1354 if (end < pagelen) 1355 end = min(round_up(end, PAGE_SIZE), pagelen); 1356 count = end - offset; 1357 } 1358 1359 status = nfs_writepage_setup(ctx, folio, offset, count); 1360 if (status < 0) 1361 nfs_set_pageerror(mapping); 1362 out: 1363 dprintk("NFS: nfs_update_folio returns %d (isize %lld)\n", 1364 status, (long long)i_size_read(inode)); 1365 return status; 1366 } 1367 1368 static int flush_task_priority(int how) 1369 { 1370 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { 1371 case FLUSH_HIGHPRI: 1372 return RPC_PRIORITY_HIGH; 1373 case FLUSH_LOWPRI: 1374 return RPC_PRIORITY_LOW; 1375 } 1376 return RPC_PRIORITY_NORMAL; 1377 } 1378 1379 static void nfs_initiate_write(struct nfs_pgio_header *hdr, 1380 struct rpc_message *msg, 1381 const struct nfs_rpc_ops *rpc_ops, 1382 struct rpc_task_setup *task_setup_data, int how) 1383 { 1384 int priority = flush_task_priority(how); 1385 1386 if (IS_SWAPFILE(hdr->inode)) 1387 task_setup_data->flags |= RPC_TASK_SWAPPER; 1388 task_setup_data->priority = priority; 1389 rpc_ops->write_setup(hdr, msg, &task_setup_data->rpc_client); 1390 trace_nfs_initiate_write(hdr); 1391 } 1392 1393 /* If a nfs_flush_* function fails, it should remove reqs from @head and 1394 * call this on each, which will prepare them to be retried on next 1395 * writeback using standard nfs. 1396 */ 1397 static void nfs_redirty_request(struct nfs_page *req) 1398 { 1399 struct nfs_inode *nfsi = NFS_I(nfs_page_to_inode(req)); 1400 1401 /* Bump the transmission count */ 1402 req->wb_nio++; 1403 nfs_mark_request_dirty(req); 1404 atomic_long_inc(&nfsi->redirtied_pages); 1405 nfs_page_end_writeback(req); 1406 nfs_release_request(req); 1407 } 1408 1409 static void nfs_async_write_error(struct list_head *head, int error) 1410 { 1411 struct nfs_page *req; 1412 1413 while (!list_empty(head)) { 1414 req = nfs_list_entry(head->next); 1415 nfs_list_remove_request(req); 1416 if (nfs_error_is_fatal_on_server(error)) 1417 nfs_write_error(req, error); 1418 else 1419 nfs_redirty_request(req); 1420 } 1421 } 1422 1423 static void nfs_async_write_reschedule_io(struct nfs_pgio_header *hdr) 1424 { 1425 nfs_async_write_error(&hdr->pages, 0); 1426 } 1427 1428 static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = { 1429 .init_hdr = nfs_async_write_init, 1430 .error_cleanup = nfs_async_write_error, 1431 .completion = nfs_write_completion, 1432 .reschedule_io = nfs_async_write_reschedule_io, 1433 }; 1434 1435 void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, 1436 struct inode *inode, int ioflags, bool force_mds, 1437 const struct nfs_pgio_completion_ops *compl_ops) 1438 { 1439 struct nfs_server *server = NFS_SERVER(inode); 1440 const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops; 1441 1442 #ifdef CONFIG_NFS_V4_1 1443 if (server->pnfs_curr_ld && !force_mds) 1444 pg_ops = server->pnfs_curr_ld->pg_write_ops; 1445 #endif 1446 nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops, 1447 server->wsize, ioflags); 1448 } 1449 EXPORT_SYMBOL_GPL(nfs_pageio_init_write); 1450 1451 void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio) 1452 { 1453 struct nfs_pgio_mirror *mirror; 1454 1455 if (pgio->pg_ops && pgio->pg_ops->pg_cleanup) 1456 pgio->pg_ops->pg_cleanup(pgio); 1457 1458 pgio->pg_ops = &nfs_pgio_rw_ops; 1459 1460 nfs_pageio_stop_mirroring(pgio); 1461 1462 mirror = &pgio->pg_mirrors[0]; 1463 mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize; 1464 } 1465 EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds); 1466 1467 1468 void nfs_commit_prepare(struct rpc_task *task, void *calldata) 1469 { 1470 struct nfs_commit_data *data = calldata; 1471 1472 NFS_PROTO(data->inode)->commit_rpc_prepare(task, data); 1473 } 1474 1475 static void nfs_writeback_check_extend(struct nfs_pgio_header *hdr, 1476 struct nfs_fattr *fattr) 1477 { 1478 struct nfs_pgio_args *argp = &hdr->args; 1479 struct nfs_pgio_res *resp = &hdr->res; 1480 u64 size = argp->offset + resp->count; 1481 1482 if (!(fattr->valid & NFS_ATTR_FATTR_SIZE)) 1483 fattr->size = size; 1484 if (nfs_size_to_loff_t(fattr->size) < i_size_read(hdr->inode)) { 1485 fattr->valid &= ~NFS_ATTR_FATTR_SIZE; 1486 return; 1487 } 1488 if (size != fattr->size) 1489 return; 1490 /* Set attribute barrier */ 1491 nfs_fattr_set_barrier(fattr); 1492 /* ...and update size */ 1493 fattr->valid |= NFS_ATTR_FATTR_SIZE; 1494 } 1495 1496 void nfs_writeback_update_inode(struct nfs_pgio_header *hdr) 1497 { 1498 struct nfs_fattr *fattr = &hdr->fattr; 1499 struct inode *inode = hdr->inode; 1500 1501 if (nfs_have_delegated_mtime(inode)) { 1502 spin_lock(&inode->i_lock); 1503 nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS); 1504 spin_unlock(&inode->i_lock); 1505 return; 1506 } 1507 1508 spin_lock(&inode->i_lock); 1509 nfs_writeback_check_extend(hdr, fattr); 1510 nfs_post_op_update_inode_force_wcc_locked(inode, fattr); 1511 spin_unlock(&inode->i_lock); 1512 } 1513 EXPORT_SYMBOL_GPL(nfs_writeback_update_inode); 1514 1515 /* 1516 * This function is called when the WRITE call is complete. 1517 */ 1518 static int nfs_writeback_done(struct rpc_task *task, 1519 struct nfs_pgio_header *hdr, 1520 struct inode *inode) 1521 { 1522 int status; 1523 1524 /* 1525 * ->write_done will attempt to use post-op attributes to detect 1526 * conflicting writes by other clients. A strict interpretation 1527 * of close-to-open would allow us to continue caching even if 1528 * another writer had changed the file, but some applications 1529 * depend on tighter cache coherency when writing. 1530 */ 1531 status = NFS_PROTO(inode)->write_done(task, hdr); 1532 if (status != 0) 1533 return status; 1534 1535 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count); 1536 trace_nfs_writeback_done(task, hdr); 1537 1538 if (task->tk_status >= 0) { 1539 enum nfs3_stable_how committed = hdr->res.verf->committed; 1540 1541 if (committed == NFS_UNSTABLE) { 1542 /* 1543 * We have some uncommitted data on the server at 1544 * this point, so ensure that we keep track of that 1545 * fact irrespective of what later writes do. 1546 */ 1547 set_bit(NFS_IOHDR_UNSTABLE_WRITES, &hdr->flags); 1548 } 1549 1550 if (committed < hdr->args.stable) { 1551 /* We tried a write call, but the server did not 1552 * commit data to stable storage even though we 1553 * requested it. 1554 * Note: There is a known bug in Tru64 < 5.0 in which 1555 * the server reports NFS_DATA_SYNC, but performs 1556 * NFS_FILE_SYNC. We therefore implement this checking 1557 * as a dprintk() in order to avoid filling syslog. 1558 */ 1559 static unsigned long complain; 1560 1561 /* Note this will print the MDS for a DS write */ 1562 if (time_before(complain, jiffies)) { 1563 dprintk("NFS: faulty NFS server %s:" 1564 " (committed = %d) != (stable = %d)\n", 1565 NFS_SERVER(inode)->nfs_client->cl_hostname, 1566 committed, hdr->args.stable); 1567 complain = jiffies + 300 * HZ; 1568 } 1569 } 1570 } 1571 1572 /* Deal with the suid/sgid bit corner case */ 1573 if (nfs_should_remove_suid(inode)) { 1574 spin_lock(&inode->i_lock); 1575 nfs_set_cache_invalid(inode, NFS_INO_INVALID_MODE); 1576 spin_unlock(&inode->i_lock); 1577 } 1578 return 0; 1579 } 1580 1581 /* 1582 * This function is called when the WRITE call is complete. 1583 */ 1584 static void nfs_writeback_result(struct rpc_task *task, 1585 struct nfs_pgio_header *hdr) 1586 { 1587 struct nfs_pgio_args *argp = &hdr->args; 1588 struct nfs_pgio_res *resp = &hdr->res; 1589 1590 if (resp->count < argp->count) { 1591 static unsigned long complain; 1592 1593 /* This a short write! */ 1594 nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE); 1595 1596 /* Has the server at least made some progress? */ 1597 if (resp->count == 0) { 1598 if (time_before(complain, jiffies)) { 1599 printk(KERN_WARNING 1600 "NFS: Server wrote zero bytes, expected %u.\n", 1601 argp->count); 1602 complain = jiffies + 300 * HZ; 1603 } 1604 nfs_set_pgio_error(hdr, -EIO, argp->offset); 1605 task->tk_status = -EIO; 1606 return; 1607 } 1608 1609 /* For non rpc-based layout drivers, retry-through-MDS */ 1610 if (!task->tk_ops) { 1611 hdr->pnfs_error = -EAGAIN; 1612 return; 1613 } 1614 1615 /* Was this an NFSv2 write or an NFSv3 stable write? */ 1616 if (resp->verf->committed != NFS_UNSTABLE) { 1617 /* Resend from where the server left off */ 1618 hdr->mds_offset += resp->count; 1619 argp->offset += resp->count; 1620 argp->pgbase += resp->count; 1621 argp->count -= resp->count; 1622 } else { 1623 /* Resend as a stable write in order to avoid 1624 * headaches in the case of a server crash. 1625 */ 1626 argp->stable = NFS_FILE_SYNC; 1627 } 1628 resp->count = 0; 1629 resp->verf->committed = 0; 1630 rpc_restart_call_prepare(task); 1631 } 1632 } 1633 1634 static int wait_on_commit(struct nfs_mds_commit_info *cinfo) 1635 { 1636 return wait_var_event_killable(&cinfo->rpcs_out, 1637 !atomic_read(&cinfo->rpcs_out)); 1638 } 1639 1640 void nfs_commit_begin(struct nfs_mds_commit_info *cinfo) 1641 { 1642 atomic_inc(&cinfo->rpcs_out); 1643 } 1644 1645 bool nfs_commit_end(struct nfs_mds_commit_info *cinfo) 1646 { 1647 if (atomic_dec_and_test(&cinfo->rpcs_out)) { 1648 wake_up_var(&cinfo->rpcs_out); 1649 return true; 1650 } 1651 return false; 1652 } 1653 1654 void nfs_commitdata_release(struct nfs_commit_data *data) 1655 { 1656 put_nfs_open_context(data->context); 1657 nfs_commit_free(data); 1658 } 1659 EXPORT_SYMBOL_GPL(nfs_commitdata_release); 1660 1661 int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data, 1662 const struct nfs_rpc_ops *nfs_ops, 1663 const struct rpc_call_ops *call_ops, 1664 int how, int flags) 1665 { 1666 struct rpc_task *task; 1667 int priority = flush_task_priority(how); 1668 struct rpc_message msg = { 1669 .rpc_argp = &data->args, 1670 .rpc_resp = &data->res, 1671 .rpc_cred = data->cred, 1672 }; 1673 struct rpc_task_setup task_setup_data = { 1674 .task = &data->task, 1675 .rpc_client = clnt, 1676 .rpc_message = &msg, 1677 .callback_ops = call_ops, 1678 .callback_data = data, 1679 .workqueue = nfsiod_workqueue, 1680 .flags = RPC_TASK_ASYNC | flags, 1681 .priority = priority, 1682 }; 1683 1684 if (nfs_server_capable(data->inode, NFS_CAP_MOVEABLE)) 1685 task_setup_data.flags |= RPC_TASK_MOVEABLE; 1686 1687 /* Set up the initial task struct. */ 1688 nfs_ops->commit_setup(data, &msg, &task_setup_data.rpc_client); 1689 trace_nfs_initiate_commit(data); 1690 1691 dprintk("NFS: initiated commit call\n"); 1692 1693 task = rpc_run_task(&task_setup_data); 1694 if (IS_ERR(task)) 1695 return PTR_ERR(task); 1696 if (how & FLUSH_SYNC) 1697 rpc_wait_for_completion_task(task); 1698 rpc_put_task(task); 1699 return 0; 1700 } 1701 EXPORT_SYMBOL_GPL(nfs_initiate_commit); 1702 1703 static loff_t nfs_get_lwb(struct list_head *head) 1704 { 1705 loff_t lwb = 0; 1706 struct nfs_page *req; 1707 1708 list_for_each_entry(req, head, wb_list) 1709 if (lwb < (req_offset(req) + req->wb_bytes)) 1710 lwb = req_offset(req) + req->wb_bytes; 1711 1712 return lwb; 1713 } 1714 1715 /* 1716 * Set up the argument/result storage required for the RPC call. 1717 */ 1718 void nfs_init_commit(struct nfs_commit_data *data, 1719 struct list_head *head, 1720 struct pnfs_layout_segment *lseg, 1721 struct nfs_commit_info *cinfo) 1722 { 1723 struct nfs_page *first; 1724 struct nfs_open_context *ctx; 1725 struct inode *inode; 1726 1727 /* Set up the RPC argument and reply structs 1728 * NB: take care not to mess about with data->commit et al. */ 1729 1730 if (head) 1731 list_splice_init(head, &data->pages); 1732 1733 first = nfs_list_entry(data->pages.next); 1734 ctx = nfs_req_openctx(first); 1735 inode = d_inode(ctx->dentry); 1736 1737 data->inode = inode; 1738 data->cred = ctx->cred; 1739 data->lseg = lseg; /* reference transferred */ 1740 /* only set lwb for pnfs commit */ 1741 if (lseg) 1742 data->lwb = nfs_get_lwb(&data->pages); 1743 data->mds_ops = &nfs_commit_ops; 1744 data->completion_ops = cinfo->completion_ops; 1745 data->dreq = cinfo->dreq; 1746 1747 data->args.fh = NFS_FH(data->inode); 1748 /* Note: we always request a commit of the entire inode */ 1749 data->args.offset = 0; 1750 data->args.count = 0; 1751 data->context = get_nfs_open_context(ctx); 1752 data->res.fattr = &data->fattr; 1753 data->res.verf = &data->verf; 1754 nfs_fattr_init(&data->fattr); 1755 nfs_commit_begin(cinfo->mds); 1756 } 1757 EXPORT_SYMBOL_GPL(nfs_init_commit); 1758 1759 void nfs_retry_commit(struct list_head *page_list, 1760 struct pnfs_layout_segment *lseg, 1761 struct nfs_commit_info *cinfo, 1762 u32 ds_commit_idx) 1763 { 1764 struct nfs_page *req; 1765 1766 while (!list_empty(page_list)) { 1767 req = nfs_list_entry(page_list->next); 1768 nfs_list_remove_request(req); 1769 nfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx); 1770 nfs_folio_clear_commit(nfs_page_to_folio(req)); 1771 nfs_unlock_and_release_request(req); 1772 } 1773 } 1774 EXPORT_SYMBOL_GPL(nfs_retry_commit); 1775 1776 static void nfs_commit_resched_write(struct nfs_commit_info *cinfo, 1777 struct nfs_page *req) 1778 { 1779 struct folio *folio = nfs_page_to_folio(req); 1780 1781 filemap_dirty_folio(folio_mapping(folio), folio); 1782 } 1783 1784 /* 1785 * Commit dirty pages 1786 */ 1787 static int 1788 nfs_commit_list(struct inode *inode, struct list_head *head, int how, 1789 struct nfs_commit_info *cinfo) 1790 { 1791 struct nfs_commit_data *data; 1792 unsigned short task_flags = 0; 1793 1794 /* another commit raced with us */ 1795 if (list_empty(head)) 1796 return 0; 1797 1798 data = nfs_commitdata_alloc(); 1799 if (!data) { 1800 nfs_retry_commit(head, NULL, cinfo, -1); 1801 return -ENOMEM; 1802 } 1803 1804 /* Set up the argument struct */ 1805 nfs_init_commit(data, head, NULL, cinfo); 1806 if (NFS_SERVER(inode)->nfs_client->cl_minorversion) 1807 task_flags = RPC_TASK_MOVEABLE; 1808 return nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(inode), 1809 data->mds_ops, how, 1810 RPC_TASK_CRED_NOREF | task_flags); 1811 } 1812 1813 /* 1814 * COMMIT call returned 1815 */ 1816 static void nfs_commit_done(struct rpc_task *task, void *calldata) 1817 { 1818 struct nfs_commit_data *data = calldata; 1819 1820 /* Call the NFS version-specific code */ 1821 NFS_PROTO(data->inode)->commit_done(task, data); 1822 trace_nfs_commit_done(task, data); 1823 } 1824 1825 static void nfs_commit_release_pages(struct nfs_commit_data *data) 1826 { 1827 const struct nfs_writeverf *verf = data->res.verf; 1828 struct nfs_page *req; 1829 int status = data->task.tk_status; 1830 struct nfs_commit_info cinfo; 1831 struct folio *folio; 1832 1833 while (!list_empty(&data->pages)) { 1834 req = nfs_list_entry(data->pages.next); 1835 nfs_list_remove_request(req); 1836 folio = nfs_page_to_folio(req); 1837 nfs_folio_clear_commit(folio); 1838 1839 dprintk("NFS: commit (%s/%llu %d@%lld)", 1840 nfs_req_openctx(req)->dentry->d_sb->s_id, 1841 (unsigned long long)NFS_FILEID(d_inode(nfs_req_openctx(req)->dentry)), 1842 req->wb_bytes, 1843 (long long)req_offset(req)); 1844 if (status < 0) { 1845 if (folio) { 1846 trace_nfs_commit_error(data->inode, req, 1847 status); 1848 nfs_mapping_set_error(folio, status); 1849 nfs_inode_remove_request(req); 1850 } 1851 dprintk_cont(", error = %d\n", status); 1852 goto next; 1853 } 1854 1855 /* Okay, COMMIT succeeded, apparently. Check the verifier 1856 * returned by the server against all stored verfs. */ 1857 if (nfs_write_match_verf(verf, req)) { 1858 /* We have a match */ 1859 if (folio) 1860 nfs_inode_remove_request(req); 1861 dprintk_cont(" OK\n"); 1862 goto next; 1863 } 1864 /* We have a mismatch. Write the page again */ 1865 dprintk_cont(" mismatch\n"); 1866 nfs_mark_request_dirty(req); 1867 atomic_long_inc(&NFS_I(data->inode)->redirtied_pages); 1868 next: 1869 nfs_unlock_and_release_request(req); 1870 /* Latency breaker */ 1871 cond_resched(); 1872 } 1873 1874 nfs_init_cinfo(&cinfo, data->inode, data->dreq); 1875 nfs_commit_end(cinfo.mds); 1876 } 1877 1878 static void nfs_commit_release(void *calldata) 1879 { 1880 struct nfs_commit_data *data = calldata; 1881 1882 data->completion_ops->completion(data); 1883 nfs_commitdata_release(calldata); 1884 } 1885 1886 static const struct rpc_call_ops nfs_commit_ops = { 1887 .rpc_call_prepare = nfs_commit_prepare, 1888 .rpc_call_done = nfs_commit_done, 1889 .rpc_release = nfs_commit_release, 1890 }; 1891 1892 static const struct nfs_commit_completion_ops nfs_commit_completion_ops = { 1893 .completion = nfs_commit_release_pages, 1894 .resched_write = nfs_commit_resched_write, 1895 }; 1896 1897 int nfs_generic_commit_list(struct inode *inode, struct list_head *head, 1898 int how, struct nfs_commit_info *cinfo) 1899 { 1900 int status; 1901 1902 status = pnfs_commit_list(inode, head, how, cinfo); 1903 if (status == PNFS_NOT_ATTEMPTED) 1904 status = nfs_commit_list(inode, head, how, cinfo); 1905 return status; 1906 } 1907 1908 static int __nfs_commit_inode(struct inode *inode, int how, 1909 struct writeback_control *wbc) 1910 { 1911 LIST_HEAD(head); 1912 struct nfs_commit_info cinfo; 1913 int may_wait = how & FLUSH_SYNC; 1914 int ret, nscan; 1915 1916 how &= ~FLUSH_SYNC; 1917 nfs_init_cinfo_from_inode(&cinfo, inode); 1918 nfs_commit_begin(cinfo.mds); 1919 for (;;) { 1920 ret = nscan = nfs_scan_commit(inode, &head, &cinfo); 1921 if (ret <= 0) 1922 break; 1923 ret = nfs_generic_commit_list(inode, &head, how, &cinfo); 1924 if (ret < 0) 1925 break; 1926 ret = 0; 1927 if (wbc && wbc->sync_mode == WB_SYNC_NONE) { 1928 if (nscan < wbc->nr_to_write) 1929 wbc->nr_to_write -= nscan; 1930 else 1931 wbc->nr_to_write = 0; 1932 } 1933 if (nscan < INT_MAX) 1934 break; 1935 cond_resched(); 1936 } 1937 nfs_commit_end(cinfo.mds); 1938 if (ret || !may_wait) 1939 return ret; 1940 return wait_on_commit(cinfo.mds); 1941 } 1942 1943 int nfs_commit_inode(struct inode *inode, int how) 1944 { 1945 return __nfs_commit_inode(inode, how, NULL); 1946 } 1947 EXPORT_SYMBOL_GPL(nfs_commit_inode); 1948 1949 int nfs_write_inode(struct inode *inode, struct writeback_control *wbc) 1950 { 1951 struct nfs_inode *nfsi = NFS_I(inode); 1952 int flags = FLUSH_SYNC; 1953 int ret = 0; 1954 1955 if (wbc->sync_mode == WB_SYNC_NONE) { 1956 /* no commits means nothing needs to be done */ 1957 if (!atomic_long_read(&nfsi->commit_info.ncommit)) 1958 goto check_requests_outstanding; 1959 1960 /* Don't commit yet if this is a non-blocking flush and there 1961 * are a lot of outstanding writes for this mapping. 1962 */ 1963 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK)) 1964 goto out_mark_dirty; 1965 1966 /* don't wait for the COMMIT response */ 1967 flags = 0; 1968 } 1969 1970 ret = __nfs_commit_inode(inode, flags, wbc); 1971 if (!ret) { 1972 if (flags & FLUSH_SYNC) 1973 return 0; 1974 } else if (atomic_long_read(&nfsi->commit_info.ncommit)) 1975 goto out_mark_dirty; 1976 1977 check_requests_outstanding: 1978 if (!atomic_read(&nfsi->commit_info.rpcs_out)) 1979 return ret; 1980 out_mark_dirty: 1981 __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 1982 return ret; 1983 } 1984 EXPORT_SYMBOL_GPL(nfs_write_inode); 1985 1986 /* 1987 * Wrapper for filemap_write_and_wait_range() 1988 * 1989 * Needed for pNFS in order to ensure data becomes visible to the 1990 * client. 1991 */ 1992 int nfs_filemap_write_and_wait_range(struct address_space *mapping, 1993 loff_t lstart, loff_t lend) 1994 { 1995 int ret; 1996 1997 ret = filemap_write_and_wait_range(mapping, lstart, lend); 1998 if (ret == 0) 1999 ret = pnfs_sync_inode(mapping->host, true); 2000 return ret; 2001 } 2002 EXPORT_SYMBOL_GPL(nfs_filemap_write_and_wait_range); 2003 2004 /* 2005 * flush the inode to disk. 2006 */ 2007 int nfs_wb_all(struct inode *inode) 2008 { 2009 int ret; 2010 2011 trace_nfs_writeback_inode_enter(inode); 2012 2013 ret = filemap_write_and_wait(inode->i_mapping); 2014 if (ret) 2015 goto out; 2016 ret = nfs_commit_inode(inode, FLUSH_SYNC); 2017 if (ret < 0) 2018 goto out; 2019 pnfs_sync_inode(inode, true); 2020 ret = 0; 2021 2022 out: 2023 trace_nfs_writeback_inode_exit(inode, ret); 2024 return ret; 2025 } 2026 EXPORT_SYMBOL_GPL(nfs_wb_all); 2027 2028 int nfs_wb_folio_cancel(struct inode *inode, struct folio *folio) 2029 { 2030 struct nfs_page *req; 2031 int ret = 0; 2032 2033 folio_wait_writeback(folio); 2034 2035 /* blocking call to cancel all requests and join to a single (head) 2036 * request */ 2037 req = nfs_lock_and_join_requests(folio); 2038 2039 if (IS_ERR(req)) { 2040 ret = PTR_ERR(req); 2041 } else if (req) { 2042 /* all requests from this folio have been cancelled by 2043 * nfs_lock_and_join_requests, so just remove the head 2044 * request from the inode / page_private pointer and 2045 * release it */ 2046 nfs_inode_remove_request(req); 2047 nfs_unlock_and_release_request(req); 2048 } 2049 2050 return ret; 2051 } 2052 2053 /** 2054 * nfs_wb_folio - Write back all requests on one page 2055 * @inode: pointer to page 2056 * @folio: pointer to folio 2057 * 2058 * Assumes that the folio has been locked by the caller, and will 2059 * not unlock it. 2060 */ 2061 int nfs_wb_folio(struct inode *inode, struct folio *folio) 2062 { 2063 loff_t range_start = folio_pos(folio); 2064 size_t len = folio_size(folio); 2065 struct writeback_control wbc = { 2066 .sync_mode = WB_SYNC_ALL, 2067 .nr_to_write = 0, 2068 .range_start = range_start, 2069 .range_end = range_start + len - 1, 2070 }; 2071 int ret; 2072 2073 trace_nfs_writeback_folio(inode, range_start, len); 2074 2075 for (;;) { 2076 folio_wait_writeback(folio); 2077 if (folio_clear_dirty_for_io(folio)) { 2078 ret = nfs_writepage_locked(folio, &wbc); 2079 if (ret < 0) 2080 goto out_error; 2081 continue; 2082 } 2083 ret = 0; 2084 if (!folio_test_private(folio)) 2085 break; 2086 ret = nfs_commit_inode(inode, FLUSH_SYNC); 2087 if (ret < 0) 2088 goto out_error; 2089 } 2090 out_error: 2091 trace_nfs_writeback_folio_done(inode, range_start, len, ret); 2092 return ret; 2093 } 2094 2095 #ifdef CONFIG_MIGRATION 2096 int nfs_migrate_folio(struct address_space *mapping, struct folio *dst, 2097 struct folio *src, enum migrate_mode mode) 2098 { 2099 /* 2100 * If the private flag is set, the folio is currently associated with 2101 * an in-progress read or write request. Don't try to migrate it. 2102 * 2103 * FIXME: we could do this in principle, but we'll need a way to ensure 2104 * that we can safely release the inode reference while holding 2105 * the folio lock. 2106 */ 2107 if (folio_test_private(src)) 2108 return -EBUSY; 2109 2110 if (folio_test_private_2(src)) { /* [DEPRECATED] */ 2111 if (mode == MIGRATE_ASYNC) 2112 return -EBUSY; 2113 folio_wait_private_2(src); 2114 } 2115 2116 return migrate_folio(mapping, dst, src, mode); 2117 } 2118 #endif 2119 2120 int __init nfs_init_writepagecache(void) 2121 { 2122 nfs_wdata_cachep = kmem_cache_create("nfs_write_data", 2123 sizeof(struct nfs_pgio_header), 2124 0, SLAB_HWCACHE_ALIGN, 2125 NULL); 2126 if (nfs_wdata_cachep == NULL) 2127 return -ENOMEM; 2128 2129 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, 2130 nfs_wdata_cachep); 2131 if (nfs_wdata_mempool == NULL) 2132 goto out_destroy_write_cache; 2133 2134 nfs_cdata_cachep = kmem_cache_create("nfs_commit_data", 2135 sizeof(struct nfs_commit_data), 2136 0, SLAB_HWCACHE_ALIGN, 2137 NULL); 2138 if (nfs_cdata_cachep == NULL) 2139 goto out_destroy_write_mempool; 2140 2141 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, 2142 nfs_cdata_cachep); 2143 if (nfs_commit_mempool == NULL) 2144 goto out_destroy_commit_cache; 2145 2146 /* 2147 * NFS congestion size, scale with available memory. 2148 * 2149 * 64MB: 8192k 2150 * 128MB: 11585k 2151 * 256MB: 16384k 2152 * 512MB: 23170k 2153 * 1GB: 32768k 2154 * 2GB: 46340k 2155 * 4GB: 65536k 2156 * 8GB: 92681k 2157 * 16GB: 131072k 2158 * 2159 * This allows larger machines to have larger/more transfers. 2160 * Limit the default to 256M 2161 */ 2162 nfs_congestion_kb = (16*int_sqrt(totalram_pages())) << (PAGE_SHIFT-10); 2163 if (nfs_congestion_kb > 256*1024) 2164 nfs_congestion_kb = 256*1024; 2165 2166 return 0; 2167 2168 out_destroy_commit_cache: 2169 kmem_cache_destroy(nfs_cdata_cachep); 2170 out_destroy_write_mempool: 2171 mempool_destroy(nfs_wdata_mempool); 2172 out_destroy_write_cache: 2173 kmem_cache_destroy(nfs_wdata_cachep); 2174 return -ENOMEM; 2175 } 2176 2177 void nfs_destroy_writepagecache(void) 2178 { 2179 mempool_destroy(nfs_commit_mempool); 2180 kmem_cache_destroy(nfs_cdata_cachep); 2181 mempool_destroy(nfs_wdata_mempool); 2182 kmem_cache_destroy(nfs_wdata_cachep); 2183 } 2184 2185 static const struct nfs_rw_ops nfs_rw_write_ops = { 2186 .rw_alloc_header = nfs_writehdr_alloc, 2187 .rw_free_header = nfs_writehdr_free, 2188 .rw_done = nfs_writeback_done, 2189 .rw_result = nfs_writeback_result, 2190 .rw_initiate = nfs_initiate_write, 2191 }; 2192