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