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