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