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