1 /* 2 * linux/fs/nfs/write.c 3 * 4 * Write file data over NFS. 5 * 6 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de> 7 */ 8 9 #include <linux/types.h> 10 #include <linux/slab.h> 11 #include <linux/mm.h> 12 #include <linux/pagemap.h> 13 #include <linux/file.h> 14 #include <linux/writeback.h> 15 #include <linux/swap.h> 16 17 #include <linux/sunrpc/clnt.h> 18 #include <linux/nfs_fs.h> 19 #include <linux/nfs_mount.h> 20 #include <linux/nfs_page.h> 21 #include <linux/backing-dev.h> 22 23 #include <asm/uaccess.h> 24 25 #include "delegation.h" 26 #include "internal.h" 27 #include "iostat.h" 28 29 #define NFSDBG_FACILITY NFSDBG_PAGECACHE 30 31 #define MIN_POOL_WRITE (32) 32 #define MIN_POOL_COMMIT (4) 33 34 /* 35 * Local function declarations 36 */ 37 static struct nfs_page * nfs_update_request(struct nfs_open_context*, 38 struct page *, 39 unsigned int, unsigned int); 40 static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc, 41 struct inode *inode, int ioflags); 42 static const struct rpc_call_ops nfs_write_partial_ops; 43 static const struct rpc_call_ops nfs_write_full_ops; 44 static const struct rpc_call_ops nfs_commit_ops; 45 46 static struct kmem_cache *nfs_wdata_cachep; 47 static mempool_t *nfs_wdata_mempool; 48 static mempool_t *nfs_commit_mempool; 49 50 struct nfs_write_data *nfs_commit_alloc(void) 51 { 52 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS); 53 54 if (p) { 55 memset(p, 0, sizeof(*p)); 56 INIT_LIST_HEAD(&p->pages); 57 } 58 return p; 59 } 60 61 static void nfs_commit_rcu_free(struct rcu_head *head) 62 { 63 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu); 64 if (p && (p->pagevec != &p->page_array[0])) 65 kfree(p->pagevec); 66 mempool_free(p, nfs_commit_mempool); 67 } 68 69 void nfs_commit_free(struct nfs_write_data *wdata) 70 { 71 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free); 72 } 73 74 struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount) 75 { 76 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS); 77 78 if (p) { 79 memset(p, 0, sizeof(*p)); 80 INIT_LIST_HEAD(&p->pages); 81 p->npages = pagecount; 82 if (pagecount <= ARRAY_SIZE(p->page_array)) 83 p->pagevec = p->page_array; 84 else { 85 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS); 86 if (!p->pagevec) { 87 mempool_free(p, nfs_wdata_mempool); 88 p = NULL; 89 } 90 } 91 } 92 return p; 93 } 94 95 static void nfs_writedata_rcu_free(struct rcu_head *head) 96 { 97 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu); 98 if (p && (p->pagevec != &p->page_array[0])) 99 kfree(p->pagevec); 100 mempool_free(p, nfs_wdata_mempool); 101 } 102 103 static void nfs_writedata_free(struct nfs_write_data *wdata) 104 { 105 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free); 106 } 107 108 void nfs_writedata_release(void *wdata) 109 { 110 nfs_writedata_free(wdata); 111 } 112 113 static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error) 114 { 115 ctx->error = error; 116 smp_wmb(); 117 set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 118 } 119 120 static struct nfs_page *nfs_page_find_request_locked(struct page *page) 121 { 122 struct nfs_page *req = NULL; 123 124 if (PagePrivate(page)) { 125 req = (struct nfs_page *)page_private(page); 126 if (req != NULL) 127 kref_get(&req->wb_kref); 128 } 129 return req; 130 } 131 132 static struct nfs_page *nfs_page_find_request(struct page *page) 133 { 134 struct inode *inode = page->mapping->host; 135 struct nfs_page *req = NULL; 136 137 spin_lock(&inode->i_lock); 138 req = nfs_page_find_request_locked(page); 139 spin_unlock(&inode->i_lock); 140 return req; 141 } 142 143 /* Adjust the file length if we're writing beyond the end */ 144 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count) 145 { 146 struct inode *inode = page->mapping->host; 147 loff_t end, i_size = i_size_read(inode); 148 pgoff_t end_index = (i_size - 1) >> PAGE_CACHE_SHIFT; 149 150 if (i_size > 0 && page->index < end_index) 151 return; 152 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count); 153 if (i_size >= end) 154 return; 155 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE); 156 i_size_write(inode, end); 157 } 158 159 /* A writeback failed: mark the page as bad, and invalidate the page cache */ 160 static void nfs_set_pageerror(struct page *page) 161 { 162 SetPageError(page); 163 nfs_zap_mapping(page->mapping->host, page->mapping); 164 } 165 166 /* We can set the PG_uptodate flag if we see that a write request 167 * covers the full page. 168 */ 169 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count) 170 { 171 if (PageUptodate(page)) 172 return; 173 if (base != 0) 174 return; 175 if (count != nfs_page_length(page)) 176 return; 177 if (count != PAGE_CACHE_SIZE) 178 zero_user_page(page, count, PAGE_CACHE_SIZE - count, KM_USER0); 179 SetPageUptodate(page); 180 } 181 182 static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page, 183 unsigned int offset, unsigned int count) 184 { 185 struct nfs_page *req; 186 int ret; 187 188 for (;;) { 189 req = nfs_update_request(ctx, page, offset, count); 190 if (!IS_ERR(req)) 191 break; 192 ret = PTR_ERR(req); 193 if (ret != -EBUSY) 194 return ret; 195 ret = nfs_wb_page(page->mapping->host, page); 196 if (ret != 0) 197 return ret; 198 } 199 /* Update file length */ 200 nfs_grow_file(page, offset, count); 201 nfs_unlock_request(req); 202 return 0; 203 } 204 205 static int wb_priority(struct writeback_control *wbc) 206 { 207 if (wbc->for_reclaim) 208 return FLUSH_HIGHPRI | FLUSH_STABLE; 209 if (wbc->for_kupdate) 210 return FLUSH_LOWPRI; 211 return 0; 212 } 213 214 /* 215 * NFS congestion control 216 */ 217 218 int nfs_congestion_kb; 219 220 #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10)) 221 #define NFS_CONGESTION_OFF_THRESH \ 222 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2)) 223 224 static int nfs_set_page_writeback(struct page *page) 225 { 226 int ret = test_set_page_writeback(page); 227 228 if (!ret) { 229 struct inode *inode = page->mapping->host; 230 struct nfs_server *nfss = NFS_SERVER(inode); 231 232 if (atomic_long_inc_return(&nfss->writeback) > 233 NFS_CONGESTION_ON_THRESH) 234 set_bdi_congested(&nfss->backing_dev_info, WRITE); 235 } 236 return ret; 237 } 238 239 static void nfs_end_page_writeback(struct page *page) 240 { 241 struct inode *inode = page->mapping->host; 242 struct nfs_server *nfss = NFS_SERVER(inode); 243 244 end_page_writeback(page); 245 if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) { 246 clear_bdi_congested(&nfss->backing_dev_info, WRITE); 247 congestion_end(WRITE); 248 } 249 } 250 251 /* 252 * Find an associated nfs write request, and prepare to flush it out 253 * May return an error if the user signalled nfs_wait_on_request(). 254 */ 255 static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio, 256 struct page *page) 257 { 258 struct inode *inode = page->mapping->host; 259 struct nfs_inode *nfsi = NFS_I(inode); 260 struct nfs_page *req; 261 int ret; 262 263 spin_lock(&inode->i_lock); 264 for(;;) { 265 req = nfs_page_find_request_locked(page); 266 if (req == NULL) { 267 spin_unlock(&inode->i_lock); 268 return 0; 269 } 270 if (nfs_lock_request_dontget(req)) 271 break; 272 /* Note: If we hold the page lock, as is the case in nfs_writepage, 273 * then the call to nfs_lock_request_dontget() will always 274 * succeed provided that someone hasn't already marked the 275 * request as dirty (in which case we don't care). 276 */ 277 spin_unlock(&inode->i_lock); 278 ret = nfs_wait_on_request(req); 279 nfs_release_request(req); 280 if (ret != 0) 281 return ret; 282 spin_lock(&inode->i_lock); 283 } 284 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) { 285 /* This request is marked for commit */ 286 spin_unlock(&inode->i_lock); 287 nfs_unlock_request(req); 288 nfs_pageio_complete(pgio); 289 return 0; 290 } 291 if (nfs_set_page_writeback(page) != 0) { 292 spin_unlock(&inode->i_lock); 293 BUG(); 294 } 295 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, 296 NFS_PAGE_TAG_LOCKED); 297 spin_unlock(&inode->i_lock); 298 nfs_pageio_add_request(pgio, req); 299 return 0; 300 } 301 302 static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio) 303 { 304 struct inode *inode = page->mapping->host; 305 306 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); 307 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1); 308 309 nfs_pageio_cond_complete(pgio, page->index); 310 return nfs_page_async_flush(pgio, page); 311 } 312 313 /* 314 * Write an mmapped page to the server. 315 */ 316 static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc) 317 { 318 struct nfs_pageio_descriptor pgio; 319 int err; 320 321 nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc)); 322 err = nfs_do_writepage(page, wbc, &pgio); 323 nfs_pageio_complete(&pgio); 324 if (err < 0) 325 return err; 326 if (pgio.pg_error < 0) 327 return pgio.pg_error; 328 return 0; 329 } 330 331 int nfs_writepage(struct page *page, struct writeback_control *wbc) 332 { 333 int ret; 334 335 ret = nfs_writepage_locked(page, wbc); 336 unlock_page(page); 337 return ret; 338 } 339 340 static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data) 341 { 342 int ret; 343 344 ret = nfs_do_writepage(page, wbc, data); 345 unlock_page(page); 346 return ret; 347 } 348 349 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) 350 { 351 struct inode *inode = mapping->host; 352 struct nfs_pageio_descriptor pgio; 353 int err; 354 355 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES); 356 357 nfs_pageio_init_write(&pgio, inode, wb_priority(wbc)); 358 err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio); 359 nfs_pageio_complete(&pgio); 360 if (err < 0) 361 return err; 362 if (pgio.pg_error < 0) 363 return pgio.pg_error; 364 return 0; 365 } 366 367 /* 368 * Insert a write request into an inode 369 */ 370 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req) 371 { 372 struct nfs_inode *nfsi = NFS_I(inode); 373 int error; 374 375 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req); 376 BUG_ON(error == -EEXIST); 377 if (error) 378 return error; 379 if (!nfsi->npages) { 380 igrab(inode); 381 if (nfs_have_delegation(inode, FMODE_WRITE)) 382 nfsi->change_attr++; 383 } 384 SetPagePrivate(req->wb_page); 385 set_page_private(req->wb_page, (unsigned long)req); 386 nfsi->npages++; 387 kref_get(&req->wb_kref); 388 return 0; 389 } 390 391 /* 392 * Remove a write request from an inode 393 */ 394 static void nfs_inode_remove_request(struct nfs_page *req) 395 { 396 struct inode *inode = req->wb_context->path.dentry->d_inode; 397 struct nfs_inode *nfsi = NFS_I(inode); 398 399 BUG_ON (!NFS_WBACK_BUSY(req)); 400 401 spin_lock(&inode->i_lock); 402 set_page_private(req->wb_page, 0); 403 ClearPagePrivate(req->wb_page); 404 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index); 405 nfsi->npages--; 406 if (!nfsi->npages) { 407 spin_unlock(&inode->i_lock); 408 iput(inode); 409 } else 410 spin_unlock(&inode->i_lock); 411 nfs_clear_request(req); 412 nfs_release_request(req); 413 } 414 415 static void 416 nfs_redirty_request(struct nfs_page *req) 417 { 418 __set_page_dirty_nobuffers(req->wb_page); 419 } 420 421 /* 422 * Check if a request is dirty 423 */ 424 static inline int 425 nfs_dirty_request(struct nfs_page *req) 426 { 427 struct page *page = req->wb_page; 428 429 if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags)) 430 return 0; 431 return !PageWriteback(req->wb_page); 432 } 433 434 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 435 /* 436 * Add a request to the inode's commit list. 437 */ 438 static void 439 nfs_mark_request_commit(struct nfs_page *req) 440 { 441 struct inode *inode = req->wb_context->path.dentry->d_inode; 442 struct nfs_inode *nfsi = NFS_I(inode); 443 444 spin_lock(&inode->i_lock); 445 nfsi->ncommit++; 446 set_bit(PG_NEED_COMMIT, &(req)->wb_flags); 447 radix_tree_tag_set(&nfsi->nfs_page_tree, 448 req->wb_index, 449 NFS_PAGE_TAG_COMMIT); 450 spin_unlock(&inode->i_lock); 451 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 452 __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 453 } 454 455 static inline 456 int nfs_write_need_commit(struct nfs_write_data *data) 457 { 458 return data->verf.committed != NFS_FILE_SYNC; 459 } 460 461 static inline 462 int nfs_reschedule_unstable_write(struct nfs_page *req) 463 { 464 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) { 465 nfs_mark_request_commit(req); 466 return 1; 467 } 468 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) { 469 nfs_redirty_request(req); 470 return 1; 471 } 472 return 0; 473 } 474 #else 475 static inline void 476 nfs_mark_request_commit(struct nfs_page *req) 477 { 478 } 479 480 static inline 481 int nfs_write_need_commit(struct nfs_write_data *data) 482 { 483 return 0; 484 } 485 486 static inline 487 int nfs_reschedule_unstable_write(struct nfs_page *req) 488 { 489 return 0; 490 } 491 #endif 492 493 /* 494 * Wait for a request to complete. 495 * 496 * Interruptible by signals only if mounted with intr flag. 497 */ 498 static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages) 499 { 500 struct nfs_inode *nfsi = NFS_I(inode); 501 struct nfs_page *req; 502 pgoff_t idx_end, next; 503 unsigned int res = 0; 504 int error; 505 506 if (npages == 0) 507 idx_end = ~0; 508 else 509 idx_end = idx_start + npages - 1; 510 511 next = idx_start; 512 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_LOCKED)) { 513 if (req->wb_index > idx_end) 514 break; 515 516 next = req->wb_index + 1; 517 BUG_ON(!NFS_WBACK_BUSY(req)); 518 519 kref_get(&req->wb_kref); 520 spin_unlock(&inode->i_lock); 521 error = nfs_wait_on_request(req); 522 nfs_release_request(req); 523 spin_lock(&inode->i_lock); 524 if (error < 0) 525 return error; 526 res++; 527 } 528 return res; 529 } 530 531 static void nfs_cancel_commit_list(struct list_head *head) 532 { 533 struct nfs_page *req; 534 535 while(!list_empty(head)) { 536 req = nfs_list_entry(head->next); 537 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 538 nfs_list_remove_request(req); 539 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags); 540 nfs_inode_remove_request(req); 541 nfs_unlock_request(req); 542 } 543 } 544 545 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 546 /* 547 * nfs_scan_commit - Scan an inode for commit requests 548 * @inode: NFS inode to scan 549 * @dst: destination list 550 * @idx_start: lower bound of page->index to scan. 551 * @npages: idx_start + npages sets the upper bound to scan. 552 * 553 * Moves requests from the inode's 'commit' request list. 554 * The requests are *not* checked to ensure that they form a contiguous set. 555 */ 556 static int 557 nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages) 558 { 559 struct nfs_inode *nfsi = NFS_I(inode); 560 int res = 0; 561 562 if (nfsi->ncommit != 0) { 563 res = nfs_scan_list(nfsi, dst, idx_start, npages, 564 NFS_PAGE_TAG_COMMIT); 565 nfsi->ncommit -= res; 566 } 567 return res; 568 } 569 #else 570 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages) 571 { 572 return 0; 573 } 574 #endif 575 576 /* 577 * Try to update any existing write request, or create one if there is none. 578 * In order to match, the request's credentials must match those of 579 * the calling process. 580 * 581 * Note: Should always be called with the Page Lock held! 582 */ 583 static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx, 584 struct page *page, unsigned int offset, unsigned int bytes) 585 { 586 struct address_space *mapping = page->mapping; 587 struct inode *inode = mapping->host; 588 struct nfs_page *req, *new = NULL; 589 pgoff_t rqend, end; 590 591 end = offset + bytes; 592 593 for (;;) { 594 /* Loop over all inode entries and see if we find 595 * A request for the page we wish to update 596 */ 597 spin_lock(&inode->i_lock); 598 req = nfs_page_find_request_locked(page); 599 if (req) { 600 if (!nfs_lock_request_dontget(req)) { 601 int error; 602 603 spin_unlock(&inode->i_lock); 604 error = nfs_wait_on_request(req); 605 nfs_release_request(req); 606 if (error < 0) { 607 if (new) 608 nfs_release_request(new); 609 return ERR_PTR(error); 610 } 611 continue; 612 } 613 spin_unlock(&inode->i_lock); 614 if (new) 615 nfs_release_request(new); 616 break; 617 } 618 619 if (new) { 620 int error; 621 nfs_lock_request_dontget(new); 622 error = nfs_inode_add_request(inode, new); 623 if (error) { 624 spin_unlock(&inode->i_lock); 625 nfs_unlock_request(new); 626 return ERR_PTR(error); 627 } 628 spin_unlock(&inode->i_lock); 629 return new; 630 } 631 spin_unlock(&inode->i_lock); 632 633 new = nfs_create_request(ctx, inode, page, offset, bytes); 634 if (IS_ERR(new)) 635 return new; 636 } 637 638 /* We have a request for our page. 639 * If the creds don't match, or the 640 * page addresses don't match, 641 * tell the caller to wait on the conflicting 642 * request. 643 */ 644 rqend = req->wb_offset + req->wb_bytes; 645 if (req->wb_context != ctx 646 || req->wb_page != page 647 || !nfs_dirty_request(req) 648 || offset > rqend || end < req->wb_offset) { 649 nfs_unlock_request(req); 650 return ERR_PTR(-EBUSY); 651 } 652 653 /* Okay, the request matches. Update the region */ 654 if (offset < req->wb_offset) { 655 req->wb_offset = offset; 656 req->wb_pgbase = offset; 657 req->wb_bytes = rqend - req->wb_offset; 658 } 659 660 if (end > rqend) 661 req->wb_bytes = end - req->wb_offset; 662 663 return req; 664 } 665 666 int nfs_flush_incompatible(struct file *file, struct page *page) 667 { 668 struct nfs_open_context *ctx = nfs_file_open_context(file); 669 struct nfs_page *req; 670 int do_flush, status; 671 /* 672 * Look for a request corresponding to this page. If there 673 * is one, and it belongs to another file, we flush it out 674 * before we try to copy anything into the page. Do this 675 * due to the lack of an ACCESS-type call in NFSv2. 676 * Also do the same if we find a request from an existing 677 * dropped page. 678 */ 679 do { 680 req = nfs_page_find_request(page); 681 if (req == NULL) 682 return 0; 683 do_flush = req->wb_page != page || req->wb_context != ctx 684 || !nfs_dirty_request(req); 685 nfs_release_request(req); 686 if (!do_flush) 687 return 0; 688 status = nfs_wb_page(page->mapping->host, page); 689 } while (status == 0); 690 return status; 691 } 692 693 /* 694 * Update and possibly write a cached page of an NFS file. 695 * 696 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad 697 * things with a page scheduled for an RPC call (e.g. invalidate it). 698 */ 699 int nfs_updatepage(struct file *file, struct page *page, 700 unsigned int offset, unsigned int count) 701 { 702 struct nfs_open_context *ctx = nfs_file_open_context(file); 703 struct inode *inode = page->mapping->host; 704 int status = 0; 705 706 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); 707 708 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n", 709 file->f_path.dentry->d_parent->d_name.name, 710 file->f_path.dentry->d_name.name, count, 711 (long long)(page_offset(page) +offset)); 712 713 /* If we're not using byte range locks, and we know the page 714 * is entirely in cache, it may be more efficient to avoid 715 * fragmenting write requests. 716 */ 717 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) { 718 count = max(count + offset, nfs_page_length(page)); 719 offset = 0; 720 } 721 722 status = nfs_writepage_setup(ctx, page, offset, count); 723 __set_page_dirty_nobuffers(page); 724 725 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n", 726 status, (long long)i_size_read(inode)); 727 if (status < 0) 728 nfs_set_pageerror(page); 729 return status; 730 } 731 732 static void nfs_writepage_release(struct nfs_page *req) 733 { 734 735 if (PageError(req->wb_page)) { 736 nfs_end_page_writeback(req->wb_page); 737 nfs_inode_remove_request(req); 738 } else if (!nfs_reschedule_unstable_write(req)) { 739 /* Set the PG_uptodate flag */ 740 nfs_mark_uptodate(req->wb_page, req->wb_pgbase, req->wb_bytes); 741 nfs_end_page_writeback(req->wb_page); 742 nfs_inode_remove_request(req); 743 } else 744 nfs_end_page_writeback(req->wb_page); 745 nfs_clear_page_tag_locked(req); 746 } 747 748 static inline int flush_task_priority(int how) 749 { 750 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { 751 case FLUSH_HIGHPRI: 752 return RPC_PRIORITY_HIGH; 753 case FLUSH_LOWPRI: 754 return RPC_PRIORITY_LOW; 755 } 756 return RPC_PRIORITY_NORMAL; 757 } 758 759 /* 760 * Set up the argument/result storage required for the RPC call. 761 */ 762 static void nfs_write_rpcsetup(struct nfs_page *req, 763 struct nfs_write_data *data, 764 const struct rpc_call_ops *call_ops, 765 unsigned int count, unsigned int offset, 766 int how) 767 { 768 struct inode *inode; 769 int flags; 770 771 /* Set up the RPC argument and reply structs 772 * NB: take care not to mess about with data->commit et al. */ 773 774 data->req = req; 775 data->inode = inode = req->wb_context->path.dentry->d_inode; 776 data->cred = req->wb_context->cred; 777 778 data->args.fh = NFS_FH(inode); 779 data->args.offset = req_offset(req) + offset; 780 data->args.pgbase = req->wb_pgbase + offset; 781 data->args.pages = data->pagevec; 782 data->args.count = count; 783 data->args.context = req->wb_context; 784 785 data->res.fattr = &data->fattr; 786 data->res.count = count; 787 data->res.verf = &data->verf; 788 nfs_fattr_init(&data->fattr); 789 790 /* Set up the initial task struct. */ 791 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC; 792 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data); 793 NFS_PROTO(inode)->write_setup(data, how); 794 795 data->task.tk_priority = flush_task_priority(how); 796 data->task.tk_cookie = (unsigned long)inode; 797 798 dprintk("NFS: %5u initiated write call " 799 "(req %s/%Ld, %u bytes @ offset %Lu)\n", 800 data->task.tk_pid, 801 inode->i_sb->s_id, 802 (long long)NFS_FILEID(inode), 803 count, 804 (unsigned long long)data->args.offset); 805 } 806 807 static void nfs_execute_write(struct nfs_write_data *data) 808 { 809 struct rpc_clnt *clnt = NFS_CLIENT(data->inode); 810 sigset_t oldset; 811 812 rpc_clnt_sigmask(clnt, &oldset); 813 rpc_execute(&data->task); 814 rpc_clnt_sigunmask(clnt, &oldset); 815 } 816 817 /* 818 * Generate multiple small requests to write out a single 819 * contiguous dirty area on one page. 820 */ 821 static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how) 822 { 823 struct nfs_page *req = nfs_list_entry(head->next); 824 struct page *page = req->wb_page; 825 struct nfs_write_data *data; 826 size_t wsize = NFS_SERVER(inode)->wsize, nbytes; 827 unsigned int offset; 828 int requests = 0; 829 LIST_HEAD(list); 830 831 nfs_list_remove_request(req); 832 833 nbytes = count; 834 do { 835 size_t len = min(nbytes, wsize); 836 837 data = nfs_writedata_alloc(1); 838 if (!data) 839 goto out_bad; 840 list_add(&data->pages, &list); 841 requests++; 842 nbytes -= len; 843 } while (nbytes != 0); 844 atomic_set(&req->wb_complete, requests); 845 846 ClearPageError(page); 847 offset = 0; 848 nbytes = count; 849 do { 850 data = list_entry(list.next, struct nfs_write_data, pages); 851 list_del_init(&data->pages); 852 853 data->pagevec[0] = page; 854 855 if (nbytes < wsize) 856 wsize = nbytes; 857 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops, 858 wsize, offset, how); 859 offset += wsize; 860 nbytes -= wsize; 861 nfs_execute_write(data); 862 } while (nbytes != 0); 863 864 return 0; 865 866 out_bad: 867 while (!list_empty(&list)) { 868 data = list_entry(list.next, struct nfs_write_data, pages); 869 list_del(&data->pages); 870 nfs_writedata_release(data); 871 } 872 nfs_redirty_request(req); 873 nfs_end_page_writeback(req->wb_page); 874 nfs_clear_page_tag_locked(req); 875 return -ENOMEM; 876 } 877 878 /* 879 * Create an RPC task for the given write request and kick it. 880 * The page must have been locked by the caller. 881 * 882 * It may happen that the page we're passed is not marked dirty. 883 * This is the case if nfs_updatepage detects a conflicting request 884 * that has been written but not committed. 885 */ 886 static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how) 887 { 888 struct nfs_page *req; 889 struct page **pages; 890 struct nfs_write_data *data; 891 892 data = nfs_writedata_alloc(npages); 893 if (!data) 894 goto out_bad; 895 896 pages = data->pagevec; 897 while (!list_empty(head)) { 898 req = nfs_list_entry(head->next); 899 nfs_list_remove_request(req); 900 nfs_list_add_request(req, &data->pages); 901 ClearPageError(req->wb_page); 902 *pages++ = req->wb_page; 903 } 904 req = nfs_list_entry(data->pages.next); 905 906 /* Set up the argument struct */ 907 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how); 908 909 nfs_execute_write(data); 910 return 0; 911 out_bad: 912 while (!list_empty(head)) { 913 req = nfs_list_entry(head->next); 914 nfs_list_remove_request(req); 915 nfs_redirty_request(req); 916 nfs_end_page_writeback(req->wb_page); 917 nfs_clear_page_tag_locked(req); 918 } 919 return -ENOMEM; 920 } 921 922 static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, 923 struct inode *inode, int ioflags) 924 { 925 int wsize = NFS_SERVER(inode)->wsize; 926 927 if (wsize < PAGE_CACHE_SIZE) 928 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags); 929 else 930 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags); 931 } 932 933 /* 934 * Handle a write reply that flushed part of a page. 935 */ 936 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata) 937 { 938 struct nfs_write_data *data = calldata; 939 struct nfs_page *req = data->req; 940 struct page *page = req->wb_page; 941 942 dprintk("NFS: write (%s/%Ld %d@%Ld)", 943 req->wb_context->path.dentry->d_inode->i_sb->s_id, 944 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), 945 req->wb_bytes, 946 (long long)req_offset(req)); 947 948 if (nfs_writeback_done(task, data) != 0) 949 return; 950 951 if (task->tk_status < 0) { 952 nfs_set_pageerror(page); 953 nfs_context_set_write_error(req->wb_context, task->tk_status); 954 dprintk(", error = %d\n", task->tk_status); 955 goto out; 956 } 957 958 if (nfs_write_need_commit(data)) { 959 struct inode *inode = page->mapping->host; 960 961 spin_lock(&inode->i_lock); 962 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) { 963 /* Do nothing we need to resend the writes */ 964 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) { 965 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); 966 dprintk(" defer commit\n"); 967 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) { 968 set_bit(PG_NEED_RESCHED, &req->wb_flags); 969 clear_bit(PG_NEED_COMMIT, &req->wb_flags); 970 dprintk(" server reboot detected\n"); 971 } 972 spin_unlock(&inode->i_lock); 973 } else 974 dprintk(" OK\n"); 975 976 out: 977 if (atomic_dec_and_test(&req->wb_complete)) 978 nfs_writepage_release(req); 979 } 980 981 static const struct rpc_call_ops nfs_write_partial_ops = { 982 .rpc_call_done = nfs_writeback_done_partial, 983 .rpc_release = nfs_writedata_release, 984 }; 985 986 /* 987 * Handle a write reply that flushes a whole page. 988 * 989 * FIXME: There is an inherent race with invalidate_inode_pages and 990 * writebacks since the page->count is kept > 1 for as long 991 * as the page has a write request pending. 992 */ 993 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata) 994 { 995 struct nfs_write_data *data = calldata; 996 struct nfs_page *req; 997 struct page *page; 998 999 if (nfs_writeback_done(task, data) != 0) 1000 return; 1001 1002 /* Update attributes as result of writeback. */ 1003 while (!list_empty(&data->pages)) { 1004 req = nfs_list_entry(data->pages.next); 1005 nfs_list_remove_request(req); 1006 page = req->wb_page; 1007 1008 dprintk("NFS: write (%s/%Ld %d@%Ld)", 1009 req->wb_context->path.dentry->d_inode->i_sb->s_id, 1010 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), 1011 req->wb_bytes, 1012 (long long)req_offset(req)); 1013 1014 if (task->tk_status < 0) { 1015 nfs_set_pageerror(page); 1016 nfs_context_set_write_error(req->wb_context, task->tk_status); 1017 dprintk(", error = %d\n", task->tk_status); 1018 goto remove_request; 1019 } 1020 1021 if (nfs_write_need_commit(data)) { 1022 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); 1023 nfs_mark_request_commit(req); 1024 nfs_end_page_writeback(page); 1025 dprintk(" marked for commit\n"); 1026 goto next; 1027 } 1028 /* Set the PG_uptodate flag? */ 1029 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes); 1030 dprintk(" OK\n"); 1031 remove_request: 1032 nfs_end_page_writeback(page); 1033 nfs_inode_remove_request(req); 1034 next: 1035 nfs_clear_page_tag_locked(req); 1036 } 1037 } 1038 1039 static const struct rpc_call_ops nfs_write_full_ops = { 1040 .rpc_call_done = nfs_writeback_done_full, 1041 .rpc_release = nfs_writedata_release, 1042 }; 1043 1044 1045 /* 1046 * This function is called when the WRITE call is complete. 1047 */ 1048 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data) 1049 { 1050 struct nfs_writeargs *argp = &data->args; 1051 struct nfs_writeres *resp = &data->res; 1052 int status; 1053 1054 dprintk("NFS: %5u nfs_writeback_done (status %d)\n", 1055 task->tk_pid, task->tk_status); 1056 1057 /* 1058 * ->write_done will attempt to use post-op attributes to detect 1059 * conflicting writes by other clients. A strict interpretation 1060 * of close-to-open would allow us to continue caching even if 1061 * another writer had changed the file, but some applications 1062 * depend on tighter cache coherency when writing. 1063 */ 1064 status = NFS_PROTO(data->inode)->write_done(task, data); 1065 if (status != 0) 1066 return status; 1067 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count); 1068 1069 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 1070 if (resp->verf->committed < argp->stable && task->tk_status >= 0) { 1071 /* We tried a write call, but the server did not 1072 * commit data to stable storage even though we 1073 * requested it. 1074 * Note: There is a known bug in Tru64 < 5.0 in which 1075 * the server reports NFS_DATA_SYNC, but performs 1076 * NFS_FILE_SYNC. We therefore implement this checking 1077 * as a dprintk() in order to avoid filling syslog. 1078 */ 1079 static unsigned long complain; 1080 1081 if (time_before(complain, jiffies)) { 1082 dprintk("NFS: faulty NFS server %s:" 1083 " (committed = %d) != (stable = %d)\n", 1084 NFS_SERVER(data->inode)->nfs_client->cl_hostname, 1085 resp->verf->committed, argp->stable); 1086 complain = jiffies + 300 * HZ; 1087 } 1088 } 1089 #endif 1090 /* Is this a short write? */ 1091 if (task->tk_status >= 0 && resp->count < argp->count) { 1092 static unsigned long complain; 1093 1094 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE); 1095 1096 /* Has the server at least made some progress? */ 1097 if (resp->count != 0) { 1098 /* Was this an NFSv2 write or an NFSv3 stable write? */ 1099 if (resp->verf->committed != NFS_UNSTABLE) { 1100 /* Resend from where the server left off */ 1101 argp->offset += resp->count; 1102 argp->pgbase += resp->count; 1103 argp->count -= resp->count; 1104 } else { 1105 /* Resend as a stable write in order to avoid 1106 * headaches in the case of a server crash. 1107 */ 1108 argp->stable = NFS_FILE_SYNC; 1109 } 1110 rpc_restart_call(task); 1111 return -EAGAIN; 1112 } 1113 if (time_before(complain, jiffies)) { 1114 printk(KERN_WARNING 1115 "NFS: Server wrote zero bytes, expected %u.\n", 1116 argp->count); 1117 complain = jiffies + 300 * HZ; 1118 } 1119 /* Can't do anything about it except throw an error. */ 1120 task->tk_status = -EIO; 1121 } 1122 return 0; 1123 } 1124 1125 1126 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 1127 void nfs_commit_release(void *wdata) 1128 { 1129 nfs_commit_free(wdata); 1130 } 1131 1132 /* 1133 * Set up the argument/result storage required for the RPC call. 1134 */ 1135 static void nfs_commit_rpcsetup(struct list_head *head, 1136 struct nfs_write_data *data, 1137 int how) 1138 { 1139 struct nfs_page *first; 1140 struct inode *inode; 1141 int flags; 1142 1143 /* Set up the RPC argument and reply structs 1144 * NB: take care not to mess about with data->commit et al. */ 1145 1146 list_splice_init(head, &data->pages); 1147 first = nfs_list_entry(data->pages.next); 1148 inode = first->wb_context->path.dentry->d_inode; 1149 1150 data->inode = inode; 1151 data->cred = first->wb_context->cred; 1152 1153 data->args.fh = NFS_FH(data->inode); 1154 /* Note: we always request a commit of the entire inode */ 1155 data->args.offset = 0; 1156 data->args.count = 0; 1157 data->res.count = 0; 1158 data->res.fattr = &data->fattr; 1159 data->res.verf = &data->verf; 1160 nfs_fattr_init(&data->fattr); 1161 1162 /* Set up the initial task struct. */ 1163 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC; 1164 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data); 1165 NFS_PROTO(inode)->commit_setup(data, how); 1166 1167 data->task.tk_priority = flush_task_priority(how); 1168 data->task.tk_cookie = (unsigned long)inode; 1169 1170 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); 1171 } 1172 1173 /* 1174 * Commit dirty pages 1175 */ 1176 static int 1177 nfs_commit_list(struct inode *inode, struct list_head *head, int how) 1178 { 1179 struct nfs_write_data *data; 1180 struct nfs_page *req; 1181 1182 data = nfs_commit_alloc(); 1183 1184 if (!data) 1185 goto out_bad; 1186 1187 /* Set up the argument struct */ 1188 nfs_commit_rpcsetup(head, data, how); 1189 1190 nfs_execute_write(data); 1191 return 0; 1192 out_bad: 1193 while (!list_empty(head)) { 1194 req = nfs_list_entry(head->next); 1195 nfs_list_remove_request(req); 1196 nfs_mark_request_commit(req); 1197 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 1198 nfs_clear_page_tag_locked(req); 1199 } 1200 return -ENOMEM; 1201 } 1202 1203 /* 1204 * COMMIT call returned 1205 */ 1206 static void nfs_commit_done(struct rpc_task *task, void *calldata) 1207 { 1208 struct nfs_write_data *data = calldata; 1209 struct nfs_page *req; 1210 1211 dprintk("NFS: %5u nfs_commit_done (status %d)\n", 1212 task->tk_pid, task->tk_status); 1213 1214 /* Call the NFS version-specific code */ 1215 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0) 1216 return; 1217 1218 while (!list_empty(&data->pages)) { 1219 req = nfs_list_entry(data->pages.next); 1220 nfs_list_remove_request(req); 1221 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags); 1222 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 1223 1224 dprintk("NFS: commit (%s/%Ld %d@%Ld)", 1225 req->wb_context->path.dentry->d_inode->i_sb->s_id, 1226 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), 1227 req->wb_bytes, 1228 (long long)req_offset(req)); 1229 if (task->tk_status < 0) { 1230 nfs_context_set_write_error(req->wb_context, task->tk_status); 1231 nfs_inode_remove_request(req); 1232 dprintk(", error = %d\n", task->tk_status); 1233 goto next; 1234 } 1235 1236 /* Okay, COMMIT succeeded, apparently. Check the verifier 1237 * returned by the server against all stored verfs. */ 1238 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) { 1239 /* We have a match */ 1240 /* Set the PG_uptodate flag */ 1241 nfs_mark_uptodate(req->wb_page, req->wb_pgbase, 1242 req->wb_bytes); 1243 nfs_inode_remove_request(req); 1244 dprintk(" OK\n"); 1245 goto next; 1246 } 1247 /* We have a mismatch. Write the page again */ 1248 dprintk(" mismatch\n"); 1249 nfs_redirty_request(req); 1250 next: 1251 nfs_clear_page_tag_locked(req); 1252 } 1253 } 1254 1255 static const struct rpc_call_ops nfs_commit_ops = { 1256 .rpc_call_done = nfs_commit_done, 1257 .rpc_release = nfs_commit_release, 1258 }; 1259 1260 int nfs_commit_inode(struct inode *inode, int how) 1261 { 1262 LIST_HEAD(head); 1263 int res; 1264 1265 spin_lock(&inode->i_lock); 1266 res = nfs_scan_commit(inode, &head, 0, 0); 1267 spin_unlock(&inode->i_lock); 1268 if (res) { 1269 int error = nfs_commit_list(inode, &head, how); 1270 if (error < 0) 1271 return error; 1272 } 1273 return res; 1274 } 1275 #else 1276 static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how) 1277 { 1278 return 0; 1279 } 1280 #endif 1281 1282 long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how) 1283 { 1284 struct inode *inode = mapping->host; 1285 pgoff_t idx_start, idx_end; 1286 unsigned int npages = 0; 1287 LIST_HEAD(head); 1288 int nocommit = how & FLUSH_NOCOMMIT; 1289 long pages, ret; 1290 1291 /* FIXME */ 1292 if (wbc->range_cyclic) 1293 idx_start = 0; 1294 else { 1295 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT; 1296 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT; 1297 if (idx_end > idx_start) { 1298 pgoff_t l_npages = 1 + idx_end - idx_start; 1299 npages = l_npages; 1300 if (sizeof(npages) != sizeof(l_npages) && 1301 (pgoff_t)npages != l_npages) 1302 npages = 0; 1303 } 1304 } 1305 how &= ~FLUSH_NOCOMMIT; 1306 spin_lock(&inode->i_lock); 1307 do { 1308 ret = nfs_wait_on_requests_locked(inode, idx_start, npages); 1309 if (ret != 0) 1310 continue; 1311 if (nocommit) 1312 break; 1313 pages = nfs_scan_commit(inode, &head, idx_start, npages); 1314 if (pages == 0) 1315 break; 1316 if (how & FLUSH_INVALIDATE) { 1317 spin_unlock(&inode->i_lock); 1318 nfs_cancel_commit_list(&head); 1319 ret = pages; 1320 spin_lock(&inode->i_lock); 1321 continue; 1322 } 1323 pages += nfs_scan_commit(inode, &head, 0, 0); 1324 spin_unlock(&inode->i_lock); 1325 ret = nfs_commit_list(inode, &head, how); 1326 spin_lock(&inode->i_lock); 1327 1328 } while (ret >= 0); 1329 spin_unlock(&inode->i_lock); 1330 return ret; 1331 } 1332 1333 static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how) 1334 { 1335 int ret; 1336 1337 ret = nfs_writepages(mapping, wbc); 1338 if (ret < 0) 1339 goto out; 1340 ret = nfs_sync_mapping_wait(mapping, wbc, how); 1341 if (ret < 0) 1342 goto out; 1343 return 0; 1344 out: 1345 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); 1346 return ret; 1347 } 1348 1349 /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */ 1350 static int nfs_write_mapping(struct address_space *mapping, int how) 1351 { 1352 struct writeback_control wbc = { 1353 .bdi = mapping->backing_dev_info, 1354 .sync_mode = WB_SYNC_NONE, 1355 .nr_to_write = LONG_MAX, 1356 .for_writepages = 1, 1357 .range_cyclic = 1, 1358 }; 1359 int ret; 1360 1361 ret = __nfs_write_mapping(mapping, &wbc, how); 1362 if (ret < 0) 1363 return ret; 1364 wbc.sync_mode = WB_SYNC_ALL; 1365 return __nfs_write_mapping(mapping, &wbc, how); 1366 } 1367 1368 /* 1369 * flush the inode to disk. 1370 */ 1371 int nfs_wb_all(struct inode *inode) 1372 { 1373 return nfs_write_mapping(inode->i_mapping, 0); 1374 } 1375 1376 int nfs_wb_nocommit(struct inode *inode) 1377 { 1378 return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT); 1379 } 1380 1381 int nfs_wb_page_cancel(struct inode *inode, struct page *page) 1382 { 1383 struct nfs_page *req; 1384 loff_t range_start = page_offset(page); 1385 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); 1386 struct writeback_control wbc = { 1387 .bdi = page->mapping->backing_dev_info, 1388 .sync_mode = WB_SYNC_ALL, 1389 .nr_to_write = LONG_MAX, 1390 .range_start = range_start, 1391 .range_end = range_end, 1392 }; 1393 int ret = 0; 1394 1395 BUG_ON(!PageLocked(page)); 1396 for (;;) { 1397 req = nfs_page_find_request(page); 1398 if (req == NULL) 1399 goto out; 1400 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) { 1401 nfs_release_request(req); 1402 break; 1403 } 1404 if (nfs_lock_request_dontget(req)) { 1405 nfs_inode_remove_request(req); 1406 /* 1407 * In case nfs_inode_remove_request has marked the 1408 * page as being dirty 1409 */ 1410 cancel_dirty_page(page, PAGE_CACHE_SIZE); 1411 nfs_unlock_request(req); 1412 break; 1413 } 1414 ret = nfs_wait_on_request(req); 1415 if (ret < 0) 1416 goto out; 1417 } 1418 if (!PagePrivate(page)) 1419 return 0; 1420 ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE); 1421 out: 1422 return ret; 1423 } 1424 1425 int nfs_wb_page_priority(struct inode *inode, struct page *page, int how) 1426 { 1427 loff_t range_start = page_offset(page); 1428 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); 1429 struct writeback_control wbc = { 1430 .bdi = page->mapping->backing_dev_info, 1431 .sync_mode = WB_SYNC_ALL, 1432 .nr_to_write = LONG_MAX, 1433 .range_start = range_start, 1434 .range_end = range_end, 1435 }; 1436 int ret; 1437 1438 BUG_ON(!PageLocked(page)); 1439 if (clear_page_dirty_for_io(page)) { 1440 ret = nfs_writepage_locked(page, &wbc); 1441 if (ret < 0) 1442 goto out; 1443 } 1444 if (!PagePrivate(page)) 1445 return 0; 1446 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how); 1447 if (ret >= 0) 1448 return 0; 1449 out: 1450 __mark_inode_dirty(inode, I_DIRTY_PAGES); 1451 return ret; 1452 } 1453 1454 /* 1455 * Write back all requests on one page - we do this before reading it. 1456 */ 1457 int nfs_wb_page(struct inode *inode, struct page* page) 1458 { 1459 return nfs_wb_page_priority(inode, page, FLUSH_STABLE); 1460 } 1461 1462 int __init nfs_init_writepagecache(void) 1463 { 1464 nfs_wdata_cachep = kmem_cache_create("nfs_write_data", 1465 sizeof(struct nfs_write_data), 1466 0, SLAB_HWCACHE_ALIGN, 1467 NULL); 1468 if (nfs_wdata_cachep == NULL) 1469 return -ENOMEM; 1470 1471 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, 1472 nfs_wdata_cachep); 1473 if (nfs_wdata_mempool == NULL) 1474 return -ENOMEM; 1475 1476 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, 1477 nfs_wdata_cachep); 1478 if (nfs_commit_mempool == NULL) 1479 return -ENOMEM; 1480 1481 /* 1482 * NFS congestion size, scale with available memory. 1483 * 1484 * 64MB: 8192k 1485 * 128MB: 11585k 1486 * 256MB: 16384k 1487 * 512MB: 23170k 1488 * 1GB: 32768k 1489 * 2GB: 46340k 1490 * 4GB: 65536k 1491 * 8GB: 92681k 1492 * 16GB: 131072k 1493 * 1494 * This allows larger machines to have larger/more transfers. 1495 * Limit the default to 256M 1496 */ 1497 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10); 1498 if (nfs_congestion_kb > 256*1024) 1499 nfs_congestion_kb = 256*1024; 1500 1501 return 0; 1502 } 1503 1504 void nfs_destroy_writepagecache(void) 1505 { 1506 mempool_destroy(nfs_commit_mempool); 1507 mempool_destroy(nfs_wdata_mempool); 1508 kmem_cache_destroy(nfs_wdata_cachep); 1509 } 1510 1511