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