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