4 * Write file data over NFS.
6 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
9 #include <linux/types.h>
10 #include <linux/slab.h>
12 #include <linux/pagemap.h>
13 #include <linux/file.h>
14 #include <linux/writeback.h>
15 #include <linux/swap.h>
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>
23 #include <asm/uaccess.h>
25 #include "delegation.h"
29 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
31 #define MIN_POOL_WRITE (32)
32 #define MIN_POOL_COMMIT (4)
35 * Local function declarations
37 static struct nfs_page
* nfs_update_request(struct nfs_open_context
*,
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
;
46 static struct kmem_cache
*nfs_wdata_cachep
;
47 static mempool_t
*nfs_wdata_mempool
;
48 static mempool_t
*nfs_commit_mempool
;
50 struct nfs_write_data
*nfs_commit_alloc(void)
52 struct nfs_write_data
*p
= mempool_alloc(nfs_commit_mempool
, GFP_NOFS
);
55 memset(p
, 0, sizeof(*p
));
56 INIT_LIST_HEAD(&p
->pages
);
61 static void nfs_commit_rcu_free(struct rcu_head
*head
)
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]))
66 mempool_free(p
, nfs_commit_mempool
);
69 void nfs_commit_free(struct nfs_write_data
*wdata
)
71 call_rcu_bh(&wdata
->task
.u
.tk_rcu
, nfs_commit_rcu_free
);
74 struct nfs_write_data
*nfs_writedata_alloc(unsigned int pagecount
)
76 struct nfs_write_data
*p
= mempool_alloc(nfs_wdata_mempool
, GFP_NOFS
);
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
;
85 p
->pagevec
= kcalloc(pagecount
, sizeof(struct page
*), GFP_NOFS
);
87 mempool_free(p
, nfs_wdata_mempool
);
95 static void nfs_writedata_rcu_free(struct rcu_head
*head
)
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]))
100 mempool_free(p
, nfs_wdata_mempool
);
103 static void nfs_writedata_free(struct nfs_write_data
*wdata
)
105 call_rcu_bh(&wdata
->task
.u
.tk_rcu
, nfs_writedata_rcu_free
);
108 void nfs_writedata_release(void *wdata
)
110 nfs_writedata_free(wdata
);
113 static struct nfs_page
*nfs_page_find_request_locked(struct page
*page
)
115 struct nfs_page
*req
= NULL
;
117 if (PagePrivate(page
)) {
118 req
= (struct nfs_page
*)page_private(page
);
120 kref_get(&req
->wb_kref
);
125 static struct nfs_page
*nfs_page_find_request(struct page
*page
)
127 struct inode
*inode
= page
->mapping
->host
;
128 struct nfs_page
*req
= NULL
;
130 spin_lock(&inode
->i_lock
);
131 req
= nfs_page_find_request_locked(page
);
132 spin_unlock(&inode
->i_lock
);
136 /* Adjust the file length if we're writing beyond the end */
137 static void nfs_grow_file(struct page
*page
, unsigned int offset
, unsigned int count
)
139 struct inode
*inode
= page
->mapping
->host
;
140 loff_t end
, i_size
= i_size_read(inode
);
141 pgoff_t end_index
= (i_size
- 1) >> PAGE_CACHE_SHIFT
;
143 if (i_size
> 0 && page
->index
< end_index
)
145 end
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + ((loff_t
)offset
+count
);
148 nfs_inc_stats(inode
, NFSIOS_EXTENDWRITE
);
149 i_size_write(inode
, end
);
152 /* A writeback failed: mark the page as bad, and invalidate the page cache */
153 static void nfs_set_pageerror(struct page
*page
)
156 nfs_zap_mapping(page
->mapping
->host
, page
->mapping
);
159 /* We can set the PG_uptodate flag if we see that a write request
160 * covers the full page.
162 static void nfs_mark_uptodate(struct page
*page
, unsigned int base
, unsigned int count
)
164 if (PageUptodate(page
))
168 if (count
!= nfs_page_length(page
))
170 if (count
!= PAGE_CACHE_SIZE
)
171 zero_user_page(page
, count
, PAGE_CACHE_SIZE
- count
, KM_USER0
);
172 SetPageUptodate(page
);
175 static int nfs_writepage_setup(struct nfs_open_context
*ctx
, struct page
*page
,
176 unsigned int offset
, unsigned int count
)
178 struct nfs_page
*req
;
182 req
= nfs_update_request(ctx
, page
, offset
, count
);
188 ret
= nfs_wb_page(page
->mapping
->host
, page
);
192 /* Update file length */
193 nfs_grow_file(page
, offset
, count
);
194 nfs_unlock_request(req
);
198 static int wb_priority(struct writeback_control
*wbc
)
200 if (wbc
->for_reclaim
)
201 return FLUSH_HIGHPRI
| FLUSH_STABLE
;
202 if (wbc
->for_kupdate
)
208 * NFS congestion control
211 int nfs_congestion_kb
;
213 #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
214 #define NFS_CONGESTION_OFF_THRESH \
215 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
217 static int nfs_set_page_writeback(struct page
*page
)
219 int ret
= test_set_page_writeback(page
);
222 struct inode
*inode
= page
->mapping
->host
;
223 struct nfs_server
*nfss
= NFS_SERVER(inode
);
225 if (atomic_long_inc_return(&nfss
->writeback
) >
226 NFS_CONGESTION_ON_THRESH
)
227 set_bdi_congested(&nfss
->backing_dev_info
, WRITE
);
232 static void nfs_end_page_writeback(struct page
*page
)
234 struct inode
*inode
= page
->mapping
->host
;
235 struct nfs_server
*nfss
= NFS_SERVER(inode
);
237 end_page_writeback(page
);
238 if (atomic_long_dec_return(&nfss
->writeback
) < NFS_CONGESTION_OFF_THRESH
)
239 clear_bdi_congested(&nfss
->backing_dev_info
, WRITE
);
243 * Find an associated nfs write request, and prepare to flush it out
244 * Returns 1 if there was no write request, or if the request was
245 * already tagged by nfs_set_page_dirty.Returns 0 if the request
247 * May also return an error if the user signalled nfs_wait_on_request().
249 static int nfs_page_async_flush(struct nfs_pageio_descriptor
*pgio
,
252 struct inode
*inode
= page
->mapping
->host
;
253 struct nfs_inode
*nfsi
= NFS_I(inode
);
254 struct nfs_page
*req
;
257 spin_lock(&inode
->i_lock
);
259 req
= nfs_page_find_request_locked(page
);
261 spin_unlock(&inode
->i_lock
);
264 if (nfs_lock_request_dontget(req
))
266 /* Note: If we hold the page lock, as is the case in nfs_writepage,
267 * then the call to nfs_lock_request_dontget() will always
268 * succeed provided that someone hasn't already marked the
269 * request as dirty (in which case we don't care).
271 spin_unlock(&inode
->i_lock
);
272 ret
= nfs_wait_on_request(req
);
273 nfs_release_request(req
);
276 spin_lock(&inode
->i_lock
);
278 if (test_bit(PG_NEED_COMMIT
, &req
->wb_flags
)) {
279 /* This request is marked for commit */
280 spin_unlock(&inode
->i_lock
);
281 nfs_unlock_request(req
);
282 nfs_pageio_complete(pgio
);
285 if (nfs_set_page_writeback(page
) != 0) {
286 spin_unlock(&inode
->i_lock
);
289 radix_tree_tag_set(&nfsi
->nfs_page_tree
, req
->wb_index
,
290 NFS_PAGE_TAG_LOCKED
);
291 ret
= test_bit(PG_NEED_FLUSH
, &req
->wb_flags
);
292 spin_unlock(&inode
->i_lock
);
293 nfs_pageio_add_request(pgio
, req
);
298 * Write an mmapped page to the server.
300 static int nfs_writepage_locked(struct page
*page
, struct writeback_control
*wbc
)
302 struct nfs_pageio_descriptor mypgio
, *pgio
;
303 struct nfs_open_context
*ctx
;
304 struct inode
*inode
= page
->mapping
->host
;
308 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGE
);
309 nfs_add_stats(inode
, NFSIOS_WRITEPAGES
, 1);
311 if (wbc
->for_writepages
)
312 pgio
= wbc
->fs_private
;
314 nfs_pageio_init_write(&mypgio
, inode
, wb_priority(wbc
));
318 nfs_pageio_cond_complete(pgio
, page
->index
);
320 err
= nfs_page_async_flush(pgio
, page
);
324 offset
= nfs_page_length(page
);
328 nfs_pageio_cond_complete(pgio
, page
->index
);
330 ctx
= nfs_find_open_context(inode
, NULL
, FMODE_WRITE
);
335 err
= nfs_writepage_setup(ctx
, page
, 0, offset
);
336 put_nfs_open_context(ctx
);
339 err
= nfs_page_async_flush(pgio
, page
);
343 if (!wbc
->for_writepages
)
344 nfs_pageio_complete(pgio
);
348 int nfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
352 err
= nfs_writepage_locked(page
, wbc
);
357 int nfs_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
359 struct inode
*inode
= mapping
->host
;
360 struct nfs_pageio_descriptor pgio
;
363 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGES
);
365 nfs_pageio_init_write(&pgio
, inode
, wb_priority(wbc
));
366 wbc
->fs_private
= &pgio
;
367 err
= generic_writepages(mapping
, wbc
);
368 nfs_pageio_complete(&pgio
);
372 return pgio
.pg_error
;
377 * Insert a write request into an inode
379 static void nfs_inode_add_request(struct inode
*inode
, struct nfs_page
*req
)
381 struct nfs_inode
*nfsi
= NFS_I(inode
);
384 error
= radix_tree_insert(&nfsi
->nfs_page_tree
, req
->wb_index
, req
);
388 nfs_begin_data_update(inode
);
389 if (nfs_have_delegation(inode
, FMODE_WRITE
))
392 SetPagePrivate(req
->wb_page
);
393 set_page_private(req
->wb_page
, (unsigned long)req
);
394 if (PageDirty(req
->wb_page
))
395 set_bit(PG_NEED_FLUSH
, &req
->wb_flags
);
397 kref_get(&req
->wb_kref
);
398 radix_tree_tag_set(&nfsi
->nfs_page_tree
, req
->wb_index
,
399 NFS_PAGE_TAG_LOCKED
);
403 * Remove a write request from an inode
405 static void nfs_inode_remove_request(struct nfs_page
*req
)
407 struct inode
*inode
= req
->wb_context
->path
.dentry
->d_inode
;
408 struct nfs_inode
*nfsi
= NFS_I(inode
);
410 BUG_ON (!NFS_WBACK_BUSY(req
));
412 spin_lock(&inode
->i_lock
);
413 set_page_private(req
->wb_page
, 0);
414 ClearPagePrivate(req
->wb_page
);
415 radix_tree_delete(&nfsi
->nfs_page_tree
, req
->wb_index
);
416 if (test_and_clear_bit(PG_NEED_FLUSH
, &req
->wb_flags
))
417 __set_page_dirty_nobuffers(req
->wb_page
);
420 spin_unlock(&inode
->i_lock
);
421 nfs_end_data_update(inode
);
424 spin_unlock(&inode
->i_lock
);
425 nfs_clear_request(req
);
426 nfs_release_request(req
);
430 nfs_redirty_request(struct nfs_page
*req
)
432 __set_page_dirty_nobuffers(req
->wb_page
);
436 * Check if a request is dirty
439 nfs_dirty_request(struct nfs_page
*req
)
441 struct page
*page
= req
->wb_page
;
443 if (page
== NULL
|| test_bit(PG_NEED_COMMIT
, &req
->wb_flags
))
445 return !PageWriteback(req
->wb_page
);
448 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
450 * Add a request to the inode's commit list.
453 nfs_mark_request_commit(struct nfs_page
*req
)
455 struct inode
*inode
= req
->wb_context
->path
.dentry
->d_inode
;
456 struct nfs_inode
*nfsi
= NFS_I(inode
);
458 spin_lock(&inode
->i_lock
);
460 set_bit(PG_NEED_COMMIT
, &(req
)->wb_flags
);
461 radix_tree_tag_set(&nfsi
->nfs_page_tree
,
463 NFS_PAGE_TAG_COMMIT
);
464 spin_unlock(&inode
->i_lock
);
465 inc_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
466 inc_bdi_stat(req
->wb_page
->mapping
->backing_dev_info
, BDI_RECLAIMABLE
);
467 __mark_inode_dirty(inode
, I_DIRTY_DATASYNC
);
471 int nfs_write_need_commit(struct nfs_write_data
*data
)
473 return data
->verf
.committed
!= NFS_FILE_SYNC
;
477 int nfs_reschedule_unstable_write(struct nfs_page
*req
)
479 if (test_bit(PG_NEED_COMMIT
, &req
->wb_flags
)) {
480 nfs_mark_request_commit(req
);
483 if (test_and_clear_bit(PG_NEED_RESCHED
, &req
->wb_flags
)) {
484 nfs_redirty_request(req
);
491 nfs_mark_request_commit(struct nfs_page
*req
)
496 int nfs_write_need_commit(struct nfs_write_data
*data
)
502 int nfs_reschedule_unstable_write(struct nfs_page
*req
)
509 * Wait for a request to complete.
511 * Interruptible by signals only if mounted with intr flag.
513 static int nfs_wait_on_requests_locked(struct inode
*inode
, pgoff_t idx_start
, unsigned int npages
)
515 struct nfs_inode
*nfsi
= NFS_I(inode
);
516 struct nfs_page
*req
;
517 pgoff_t idx_end
, next
;
518 unsigned int res
= 0;
524 idx_end
= idx_start
+ npages
- 1;
527 while (radix_tree_gang_lookup_tag(&nfsi
->nfs_page_tree
, (void **)&req
, next
, 1, NFS_PAGE_TAG_LOCKED
)) {
528 if (req
->wb_index
> idx_end
)
531 next
= req
->wb_index
+ 1;
532 BUG_ON(!NFS_WBACK_BUSY(req
));
534 kref_get(&req
->wb_kref
);
535 spin_unlock(&inode
->i_lock
);
536 error
= nfs_wait_on_request(req
);
537 nfs_release_request(req
);
538 spin_lock(&inode
->i_lock
);
546 static void nfs_cancel_commit_list(struct list_head
*head
)
548 struct nfs_page
*req
;
550 while(!list_empty(head
)) {
551 req
= nfs_list_entry(head
->next
);
552 dec_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
553 dec_bdi_stat(req
->wb_page
->mapping
->backing_dev_info
,
555 nfs_list_remove_request(req
);
556 clear_bit(PG_NEED_COMMIT
, &(req
)->wb_flags
);
557 nfs_inode_remove_request(req
);
558 nfs_unlock_request(req
);
562 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
564 * nfs_scan_commit - Scan an inode for commit requests
565 * @inode: NFS inode to scan
566 * @dst: destination list
567 * @idx_start: lower bound of page->index to scan.
568 * @npages: idx_start + npages sets the upper bound to scan.
570 * Moves requests from the inode's 'commit' request list.
571 * The requests are *not* checked to ensure that they form a contiguous set.
574 nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
, pgoff_t idx_start
, unsigned int npages
)
576 struct nfs_inode
*nfsi
= NFS_I(inode
);
579 if (nfsi
->ncommit
!= 0) {
580 res
= nfs_scan_list(nfsi
, dst
, idx_start
, npages
,
581 NFS_PAGE_TAG_COMMIT
);
582 nfsi
->ncommit
-= res
;
587 static inline int nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
, pgoff_t idx_start
, unsigned int npages
)
594 * Try to update any existing write request, or create one if there is none.
595 * In order to match, the request's credentials must match those of
596 * the calling process.
598 * Note: Should always be called with the Page Lock held!
600 static struct nfs_page
* nfs_update_request(struct nfs_open_context
* ctx
,
601 struct page
*page
, unsigned int offset
, unsigned int bytes
)
603 struct address_space
*mapping
= page
->mapping
;
604 struct inode
*inode
= mapping
->host
;
605 struct nfs_page
*req
, *new = NULL
;
608 end
= offset
+ bytes
;
611 /* Loop over all inode entries and see if we find
612 * A request for the page we wish to update
614 spin_lock(&inode
->i_lock
);
615 req
= nfs_page_find_request_locked(page
);
617 if (!nfs_lock_request_dontget(req
)) {
620 spin_unlock(&inode
->i_lock
);
621 error
= nfs_wait_on_request(req
);
622 nfs_release_request(req
);
625 radix_tree_preload_end();
626 nfs_release_request(new);
628 return ERR_PTR(error
);
632 spin_unlock(&inode
->i_lock
);
634 radix_tree_preload_end();
635 nfs_release_request(new);
641 nfs_lock_request_dontget(new);
642 nfs_inode_add_request(inode
, new);
643 spin_unlock(&inode
->i_lock
);
644 radix_tree_preload_end();
647 spin_unlock(&inode
->i_lock
);
649 new = nfs_create_request(ctx
, inode
, page
, offset
, bytes
);
652 if (radix_tree_preload(GFP_NOFS
)) {
653 nfs_release_request(new);
654 return ERR_PTR(-ENOMEM
);
658 /* We have a request for our page.
659 * If the creds don't match, or the
660 * page addresses don't match,
661 * tell the caller to wait on the conflicting
664 rqend
= req
->wb_offset
+ req
->wb_bytes
;
665 if (req
->wb_context
!= ctx
666 || req
->wb_page
!= page
667 || !nfs_dirty_request(req
)
668 || offset
> rqend
|| end
< req
->wb_offset
) {
669 nfs_unlock_request(req
);
670 return ERR_PTR(-EBUSY
);
673 /* Okay, the request matches. Update the region */
674 if (offset
< req
->wb_offset
) {
675 req
->wb_offset
= offset
;
676 req
->wb_pgbase
= offset
;
677 req
->wb_bytes
= rqend
- req
->wb_offset
;
681 req
->wb_bytes
= end
- req
->wb_offset
;
686 int nfs_flush_incompatible(struct file
*file
, struct page
*page
)
688 struct nfs_open_context
*ctx
= (struct nfs_open_context
*)file
->private_data
;
689 struct nfs_page
*req
;
690 int do_flush
, status
;
692 * Look for a request corresponding to this page. If there
693 * is one, and it belongs to another file, we flush it out
694 * before we try to copy anything into the page. Do this
695 * due to the lack of an ACCESS-type call in NFSv2.
696 * Also do the same if we find a request from an existing
700 req
= nfs_page_find_request(page
);
703 do_flush
= req
->wb_page
!= page
|| req
->wb_context
!= ctx
704 || !nfs_dirty_request(req
);
705 nfs_release_request(req
);
708 status
= nfs_wb_page(page
->mapping
->host
, page
);
709 } while (status
== 0);
714 * If the page cache is marked as unsafe or invalid, then we can't rely on
715 * the PageUptodate() flag. In this case, we will need to turn off
716 * write optimisations that depend on the page contents being correct.
718 static int nfs_write_pageuptodate(struct page
*page
, struct inode
*inode
)
720 return PageUptodate(page
) &&
721 !(NFS_I(inode
)->cache_validity
& (NFS_INO_REVAL_PAGECACHE
|NFS_INO_INVALID_DATA
));
725 * Update and possibly write a cached page of an NFS file.
727 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
728 * things with a page scheduled for an RPC call (e.g. invalidate it).
730 int nfs_updatepage(struct file
*file
, struct page
*page
,
731 unsigned int offset
, unsigned int count
)
733 struct nfs_open_context
*ctx
= (struct nfs_open_context
*)file
->private_data
;
734 struct inode
*inode
= page
->mapping
->host
;
737 nfs_inc_stats(inode
, NFSIOS_VFSUPDATEPAGE
);
739 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
740 file
->f_path
.dentry
->d_parent
->d_name
.name
,
741 file
->f_path
.dentry
->d_name
.name
, count
,
742 (long long)(page_offset(page
) +offset
));
744 /* If we're not using byte range locks, and we know the page
745 * is up to date, it may be more efficient to extend the write
746 * to cover the entire page in order to avoid fragmentation
749 if (nfs_write_pageuptodate(page
, inode
) &&
750 inode
->i_flock
== NULL
&&
751 !(file
->f_flags
& O_SYNC
)) {
752 count
= max(count
+ offset
, nfs_page_length(page
));
756 status
= nfs_writepage_setup(ctx
, page
, offset
, count
);
757 __set_page_dirty_nobuffers(page
);
759 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
760 status
, (long long)i_size_read(inode
));
762 nfs_set_pageerror(page
);
766 static void nfs_writepage_release(struct nfs_page
*req
)
769 if (PageError(req
->wb_page
)) {
770 nfs_end_page_writeback(req
->wb_page
);
771 nfs_inode_remove_request(req
);
772 } else if (!nfs_reschedule_unstable_write(req
)) {
773 /* Set the PG_uptodate flag */
774 nfs_mark_uptodate(req
->wb_page
, req
->wb_pgbase
, req
->wb_bytes
);
775 nfs_end_page_writeback(req
->wb_page
);
776 nfs_inode_remove_request(req
);
778 nfs_end_page_writeback(req
->wb_page
);
779 nfs_clear_page_tag_locked(req
);
782 static inline int flush_task_priority(int how
)
784 switch (how
& (FLUSH_HIGHPRI
|FLUSH_LOWPRI
)) {
786 return RPC_PRIORITY_HIGH
;
788 return RPC_PRIORITY_LOW
;
790 return RPC_PRIORITY_NORMAL
;
794 * Set up the argument/result storage required for the RPC call.
796 static void nfs_write_rpcsetup(struct nfs_page
*req
,
797 struct nfs_write_data
*data
,
798 const struct rpc_call_ops
*call_ops
,
799 unsigned int count
, unsigned int offset
,
805 /* Set up the RPC argument and reply structs
806 * NB: take care not to mess about with data->commit et al. */
809 data
->inode
= inode
= req
->wb_context
->path
.dentry
->d_inode
;
810 data
->cred
= req
->wb_context
->cred
;
812 data
->args
.fh
= NFS_FH(inode
);
813 data
->args
.offset
= req_offset(req
) + offset
;
814 data
->args
.pgbase
= req
->wb_pgbase
+ offset
;
815 data
->args
.pages
= data
->pagevec
;
816 data
->args
.count
= count
;
817 data
->args
.context
= req
->wb_context
;
819 data
->res
.fattr
= &data
->fattr
;
820 data
->res
.count
= count
;
821 data
->res
.verf
= &data
->verf
;
822 nfs_fattr_init(&data
->fattr
);
824 /* Set up the initial task struct. */
825 flags
= (how
& FLUSH_SYNC
) ? 0 : RPC_TASK_ASYNC
;
826 rpc_init_task(&data
->task
, NFS_CLIENT(inode
), flags
, call_ops
, data
);
827 NFS_PROTO(inode
)->write_setup(data
, how
);
829 data
->task
.tk_priority
= flush_task_priority(how
);
830 data
->task
.tk_cookie
= (unsigned long)inode
;
832 dprintk("NFS: %5u initiated write call "
833 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
836 (long long)NFS_FILEID(inode
),
838 (unsigned long long)data
->args
.offset
);
841 static void nfs_execute_write(struct nfs_write_data
*data
)
843 struct rpc_clnt
*clnt
= NFS_CLIENT(data
->inode
);
846 rpc_clnt_sigmask(clnt
, &oldset
);
847 rpc_execute(&data
->task
);
848 rpc_clnt_sigunmask(clnt
, &oldset
);
852 * Generate multiple small requests to write out a single
853 * contiguous dirty area on one page.
855 static int nfs_flush_multi(struct inode
*inode
, struct list_head
*head
, unsigned int npages
, size_t count
, int how
)
857 struct nfs_page
*req
= nfs_list_entry(head
->next
);
858 struct page
*page
= req
->wb_page
;
859 struct nfs_write_data
*data
;
860 size_t wsize
= NFS_SERVER(inode
)->wsize
, nbytes
;
865 nfs_list_remove_request(req
);
869 size_t len
= min(nbytes
, wsize
);
871 data
= nfs_writedata_alloc(1);
874 list_add(&data
->pages
, &list
);
877 } while (nbytes
!= 0);
878 atomic_set(&req
->wb_complete
, requests
);
880 ClearPageError(page
);
884 data
= list_entry(list
.next
, struct nfs_write_data
, pages
);
885 list_del_init(&data
->pages
);
887 data
->pagevec
[0] = page
;
891 nfs_write_rpcsetup(req
, data
, &nfs_write_partial_ops
,
895 nfs_execute_write(data
);
896 } while (nbytes
!= 0);
901 while (!list_empty(&list
)) {
902 data
= list_entry(list
.next
, struct nfs_write_data
, pages
);
903 list_del(&data
->pages
);
904 nfs_writedata_release(data
);
906 nfs_redirty_request(req
);
907 nfs_end_page_writeback(req
->wb_page
);
908 nfs_clear_page_tag_locked(req
);
913 * Create an RPC task for the given write request and kick it.
914 * The page must have been locked by the caller.
916 * It may happen that the page we're passed is not marked dirty.
917 * This is the case if nfs_updatepage detects a conflicting request
918 * that has been written but not committed.
920 static int nfs_flush_one(struct inode
*inode
, struct list_head
*head
, unsigned int npages
, size_t count
, int how
)
922 struct nfs_page
*req
;
924 struct nfs_write_data
*data
;
926 data
= nfs_writedata_alloc(npages
);
930 pages
= data
->pagevec
;
931 while (!list_empty(head
)) {
932 req
= nfs_list_entry(head
->next
);
933 nfs_list_remove_request(req
);
934 nfs_list_add_request(req
, &data
->pages
);
935 ClearPageError(req
->wb_page
);
936 *pages
++ = req
->wb_page
;
938 req
= nfs_list_entry(data
->pages
.next
);
940 /* Set up the argument struct */
941 nfs_write_rpcsetup(req
, data
, &nfs_write_full_ops
, count
, 0, how
);
943 nfs_execute_write(data
);
946 while (!list_empty(head
)) {
947 req
= nfs_list_entry(head
->next
);
948 nfs_list_remove_request(req
);
949 nfs_redirty_request(req
);
950 nfs_end_page_writeback(req
->wb_page
);
951 nfs_clear_page_tag_locked(req
);
956 static void nfs_pageio_init_write(struct nfs_pageio_descriptor
*pgio
,
957 struct inode
*inode
, int ioflags
)
959 int wsize
= NFS_SERVER(inode
)->wsize
;
961 if (wsize
< PAGE_CACHE_SIZE
)
962 nfs_pageio_init(pgio
, inode
, nfs_flush_multi
, wsize
, ioflags
);
964 nfs_pageio_init(pgio
, inode
, nfs_flush_one
, wsize
, ioflags
);
968 * Handle a write reply that flushed part of a page.
970 static void nfs_writeback_done_partial(struct rpc_task
*task
, void *calldata
)
972 struct nfs_write_data
*data
= calldata
;
973 struct nfs_page
*req
= data
->req
;
974 struct page
*page
= req
->wb_page
;
976 dprintk("NFS: write (%s/%Ld %d@%Ld)",
977 req
->wb_context
->path
.dentry
->d_inode
->i_sb
->s_id
,
978 (long long)NFS_FILEID(req
->wb_context
->path
.dentry
->d_inode
),
980 (long long)req_offset(req
));
982 if (nfs_writeback_done(task
, data
) != 0)
985 if (task
->tk_status
< 0) {
986 nfs_set_pageerror(page
);
987 req
->wb_context
->error
= task
->tk_status
;
988 dprintk(", error = %d\n", task
->tk_status
);
992 if (nfs_write_need_commit(data
)) {
993 struct inode
*inode
= page
->mapping
->host
;
995 spin_lock(&inode
->i_lock
);
996 if (test_bit(PG_NEED_RESCHED
, &req
->wb_flags
)) {
997 /* Do nothing we need to resend the writes */
998 } else if (!test_and_set_bit(PG_NEED_COMMIT
, &req
->wb_flags
)) {
999 memcpy(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
));
1000 dprintk(" defer commit\n");
1001 } else if (memcmp(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
))) {
1002 set_bit(PG_NEED_RESCHED
, &req
->wb_flags
);
1003 clear_bit(PG_NEED_COMMIT
, &req
->wb_flags
);
1004 dprintk(" server reboot detected\n");
1006 spin_unlock(&inode
->i_lock
);
1011 if (atomic_dec_and_test(&req
->wb_complete
))
1012 nfs_writepage_release(req
);
1015 static const struct rpc_call_ops nfs_write_partial_ops
= {
1016 .rpc_call_done
= nfs_writeback_done_partial
,
1017 .rpc_release
= nfs_writedata_release
,
1021 * Handle a write reply that flushes a whole page.
1023 * FIXME: There is an inherent race with invalidate_inode_pages and
1024 * writebacks since the page->count is kept > 1 for as long
1025 * as the page has a write request pending.
1027 static void nfs_writeback_done_full(struct rpc_task
*task
, void *calldata
)
1029 struct nfs_write_data
*data
= calldata
;
1030 struct nfs_page
*req
;
1033 if (nfs_writeback_done(task
, data
) != 0)
1036 /* Update attributes as result of writeback. */
1037 while (!list_empty(&data
->pages
)) {
1038 req
= nfs_list_entry(data
->pages
.next
);
1039 nfs_list_remove_request(req
);
1040 page
= req
->wb_page
;
1042 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1043 req
->wb_context
->path
.dentry
->d_inode
->i_sb
->s_id
,
1044 (long long)NFS_FILEID(req
->wb_context
->path
.dentry
->d_inode
),
1046 (long long)req_offset(req
));
1048 if (task
->tk_status
< 0) {
1049 nfs_set_pageerror(page
);
1050 req
->wb_context
->error
= task
->tk_status
;
1051 dprintk(", error = %d\n", task
->tk_status
);
1052 goto remove_request
;
1055 if (nfs_write_need_commit(data
)) {
1056 memcpy(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
));
1057 nfs_mark_request_commit(req
);
1058 nfs_end_page_writeback(page
);
1059 dprintk(" marked for commit\n");
1062 /* Set the PG_uptodate flag? */
1063 nfs_mark_uptodate(page
, req
->wb_pgbase
, req
->wb_bytes
);
1066 nfs_end_page_writeback(page
);
1067 nfs_inode_remove_request(req
);
1069 nfs_clear_page_tag_locked(req
);
1073 static const struct rpc_call_ops nfs_write_full_ops
= {
1074 .rpc_call_done
= nfs_writeback_done_full
,
1075 .rpc_release
= nfs_writedata_release
,
1080 * This function is called when the WRITE call is complete.
1082 int nfs_writeback_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
1084 struct nfs_writeargs
*argp
= &data
->args
;
1085 struct nfs_writeres
*resp
= &data
->res
;
1088 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1089 task
->tk_pid
, task
->tk_status
);
1092 * ->write_done will attempt to use post-op attributes to detect
1093 * conflicting writes by other clients. A strict interpretation
1094 * of close-to-open would allow us to continue caching even if
1095 * another writer had changed the file, but some applications
1096 * depend on tighter cache coherency when writing.
1098 status
= NFS_PROTO(data
->inode
)->write_done(task
, data
);
1101 nfs_add_stats(data
->inode
, NFSIOS_SERVERWRITTENBYTES
, resp
->count
);
1103 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1104 if (resp
->verf
->committed
< argp
->stable
&& task
->tk_status
>= 0) {
1105 /* We tried a write call, but the server did not
1106 * commit data to stable storage even though we
1108 * Note: There is a known bug in Tru64 < 5.0 in which
1109 * the server reports NFS_DATA_SYNC, but performs
1110 * NFS_FILE_SYNC. We therefore implement this checking
1111 * as a dprintk() in order to avoid filling syslog.
1113 static unsigned long complain
;
1115 if (time_before(complain
, jiffies
)) {
1116 dprintk("NFS: faulty NFS server %s:"
1117 " (committed = %d) != (stable = %d)\n",
1118 NFS_SERVER(data
->inode
)->nfs_client
->cl_hostname
,
1119 resp
->verf
->committed
, argp
->stable
);
1120 complain
= jiffies
+ 300 * HZ
;
1124 /* Is this a short write? */
1125 if (task
->tk_status
>= 0 && resp
->count
< argp
->count
) {
1126 static unsigned long complain
;
1128 nfs_inc_stats(data
->inode
, NFSIOS_SHORTWRITE
);
1130 /* Has the server at least made some progress? */
1131 if (resp
->count
!= 0) {
1132 /* Was this an NFSv2 write or an NFSv3 stable write? */
1133 if (resp
->verf
->committed
!= NFS_UNSTABLE
) {
1134 /* Resend from where the server left off */
1135 argp
->offset
+= resp
->count
;
1136 argp
->pgbase
+= resp
->count
;
1137 argp
->count
-= resp
->count
;
1139 /* Resend as a stable write in order to avoid
1140 * headaches in the case of a server crash.
1142 argp
->stable
= NFS_FILE_SYNC
;
1144 rpc_restart_call(task
);
1147 if (time_before(complain
, jiffies
)) {
1149 "NFS: Server wrote zero bytes, expected %u.\n",
1151 complain
= jiffies
+ 300 * HZ
;
1153 /* Can't do anything about it except throw an error. */
1154 task
->tk_status
= -EIO
;
1160 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1161 void nfs_commit_release(void *wdata
)
1163 nfs_commit_free(wdata
);
1167 * Set up the argument/result storage required for the RPC call.
1169 static void nfs_commit_rpcsetup(struct list_head
*head
,
1170 struct nfs_write_data
*data
,
1173 struct nfs_page
*first
;
1174 struct inode
*inode
;
1177 /* Set up the RPC argument and reply structs
1178 * NB: take care not to mess about with data->commit et al. */
1180 list_splice_init(head
, &data
->pages
);
1181 first
= nfs_list_entry(data
->pages
.next
);
1182 inode
= first
->wb_context
->path
.dentry
->d_inode
;
1184 data
->inode
= inode
;
1185 data
->cred
= first
->wb_context
->cred
;
1187 data
->args
.fh
= NFS_FH(data
->inode
);
1188 /* Note: we always request a commit of the entire inode */
1189 data
->args
.offset
= 0;
1190 data
->args
.count
= 0;
1191 data
->res
.count
= 0;
1192 data
->res
.fattr
= &data
->fattr
;
1193 data
->res
.verf
= &data
->verf
;
1194 nfs_fattr_init(&data
->fattr
);
1196 /* Set up the initial task struct. */
1197 flags
= (how
& FLUSH_SYNC
) ? 0 : RPC_TASK_ASYNC
;
1198 rpc_init_task(&data
->task
, NFS_CLIENT(inode
), flags
, &nfs_commit_ops
, data
);
1199 NFS_PROTO(inode
)->commit_setup(data
, how
);
1201 data
->task
.tk_priority
= flush_task_priority(how
);
1202 data
->task
.tk_cookie
= (unsigned long)inode
;
1204 dprintk("NFS: %5u initiated commit call\n", data
->task
.tk_pid
);
1208 * Commit dirty pages
1211 nfs_commit_list(struct inode
*inode
, struct list_head
*head
, int how
)
1213 struct nfs_write_data
*data
;
1214 struct nfs_page
*req
;
1216 data
= nfs_commit_alloc();
1221 /* Set up the argument struct */
1222 nfs_commit_rpcsetup(head
, data
, how
);
1224 nfs_execute_write(data
);
1227 while (!list_empty(head
)) {
1228 req
= nfs_list_entry(head
->next
);
1229 nfs_list_remove_request(req
);
1230 nfs_mark_request_commit(req
);
1231 dec_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
1232 dec_bdi_stat(req
->wb_page
->mapping
->backing_dev_info
,
1234 nfs_clear_page_tag_locked(req
);
1240 * COMMIT call returned
1242 static void nfs_commit_done(struct rpc_task
*task
, void *calldata
)
1244 struct nfs_write_data
*data
= calldata
;
1245 struct nfs_page
*req
;
1247 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1248 task
->tk_pid
, task
->tk_status
);
1250 /* Call the NFS version-specific code */
1251 if (NFS_PROTO(data
->inode
)->commit_done(task
, data
) != 0)
1254 while (!list_empty(&data
->pages
)) {
1255 req
= nfs_list_entry(data
->pages
.next
);
1256 nfs_list_remove_request(req
);
1257 clear_bit(PG_NEED_COMMIT
, &(req
)->wb_flags
);
1258 dec_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
1259 dec_bdi_stat(req
->wb_page
->mapping
->backing_dev_info
,
1262 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1263 req
->wb_context
->path
.dentry
->d_inode
->i_sb
->s_id
,
1264 (long long)NFS_FILEID(req
->wb_context
->path
.dentry
->d_inode
),
1266 (long long)req_offset(req
));
1267 if (task
->tk_status
< 0) {
1268 req
->wb_context
->error
= task
->tk_status
;
1269 nfs_inode_remove_request(req
);
1270 dprintk(", error = %d\n", task
->tk_status
);
1274 /* Okay, COMMIT succeeded, apparently. Check the verifier
1275 * returned by the server against all stored verfs. */
1276 if (!memcmp(req
->wb_verf
.verifier
, data
->verf
.verifier
, sizeof(data
->verf
.verifier
))) {
1277 /* We have a match */
1278 /* Set the PG_uptodate flag */
1279 nfs_mark_uptodate(req
->wb_page
, req
->wb_pgbase
,
1281 nfs_inode_remove_request(req
);
1285 /* We have a mismatch. Write the page again */
1286 dprintk(" mismatch\n");
1287 nfs_redirty_request(req
);
1289 nfs_clear_page_tag_locked(req
);
1293 static const struct rpc_call_ops nfs_commit_ops
= {
1294 .rpc_call_done
= nfs_commit_done
,
1295 .rpc_release
= nfs_commit_release
,
1298 int nfs_commit_inode(struct inode
*inode
, int how
)
1303 spin_lock(&inode
->i_lock
);
1304 res
= nfs_scan_commit(inode
, &head
, 0, 0);
1305 spin_unlock(&inode
->i_lock
);
1307 int error
= nfs_commit_list(inode
, &head
, how
);
1314 static inline int nfs_commit_list(struct inode
*inode
, struct list_head
*head
, int how
)
1320 long nfs_sync_mapping_wait(struct address_space
*mapping
, struct writeback_control
*wbc
, int how
)
1322 struct inode
*inode
= mapping
->host
;
1323 pgoff_t idx_start
, idx_end
;
1324 unsigned int npages
= 0;
1326 int nocommit
= how
& FLUSH_NOCOMMIT
;
1330 if (wbc
->range_cyclic
)
1333 idx_start
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
1334 idx_end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
1335 if (idx_end
> idx_start
) {
1336 pgoff_t l_npages
= 1 + idx_end
- idx_start
;
1338 if (sizeof(npages
) != sizeof(l_npages
) &&
1339 (pgoff_t
)npages
!= l_npages
)
1343 how
&= ~FLUSH_NOCOMMIT
;
1344 spin_lock(&inode
->i_lock
);
1346 ret
= nfs_wait_on_requests_locked(inode
, idx_start
, npages
);
1351 pages
= nfs_scan_commit(inode
, &head
, idx_start
, npages
);
1354 if (how
& FLUSH_INVALIDATE
) {
1355 spin_unlock(&inode
->i_lock
);
1356 nfs_cancel_commit_list(&head
);
1358 spin_lock(&inode
->i_lock
);
1361 pages
+= nfs_scan_commit(inode
, &head
, 0, 0);
1362 spin_unlock(&inode
->i_lock
);
1363 ret
= nfs_commit_list(inode
, &head
, how
);
1364 spin_lock(&inode
->i_lock
);
1367 spin_unlock(&inode
->i_lock
);
1372 * flush the inode to disk.
1374 int nfs_wb_all(struct inode
*inode
)
1376 struct address_space
*mapping
= inode
->i_mapping
;
1377 struct writeback_control wbc
= {
1378 .bdi
= mapping
->backing_dev_info
,
1379 .sync_mode
= WB_SYNC_ALL
,
1380 .nr_to_write
= LONG_MAX
,
1381 .for_writepages
= 1,
1386 ret
= nfs_writepages(mapping
, &wbc
);
1389 ret
= nfs_sync_mapping_wait(mapping
, &wbc
, 0);
1393 __mark_inode_dirty(mapping
->host
, I_DIRTY_PAGES
);
1397 int nfs_sync_mapping_range(struct address_space
*mapping
, loff_t range_start
, loff_t range_end
, int how
)
1399 struct writeback_control wbc
= {
1400 .bdi
= mapping
->backing_dev_info
,
1401 .sync_mode
= WB_SYNC_ALL
,
1402 .nr_to_write
= LONG_MAX
,
1403 .range_start
= range_start
,
1404 .range_end
= range_end
,
1405 .for_writepages
= 1,
1409 ret
= nfs_writepages(mapping
, &wbc
);
1412 ret
= nfs_sync_mapping_wait(mapping
, &wbc
, how
);
1416 __mark_inode_dirty(mapping
->host
, I_DIRTY_PAGES
);
1420 int nfs_wb_page_priority(struct inode
*inode
, struct page
*page
, int how
)
1422 loff_t range_start
= page_offset(page
);
1423 loff_t range_end
= range_start
+ (loff_t
)(PAGE_CACHE_SIZE
- 1);
1424 struct writeback_control wbc
= {
1425 .bdi
= page
->mapping
->backing_dev_info
,
1426 .sync_mode
= WB_SYNC_ALL
,
1427 .nr_to_write
= LONG_MAX
,
1428 .range_start
= range_start
,
1429 .range_end
= range_end
,
1433 BUG_ON(!PageLocked(page
));
1434 if (clear_page_dirty_for_io(page
)) {
1435 ret
= nfs_writepage_locked(page
, &wbc
);
1439 if (!PagePrivate(page
))
1441 ret
= nfs_sync_mapping_wait(page
->mapping
, &wbc
, how
);
1445 __mark_inode_dirty(inode
, I_DIRTY_PAGES
);
1450 * Write back all requests on one page - we do this before reading it.
1452 int nfs_wb_page(struct inode
*inode
, struct page
* page
)
1454 return nfs_wb_page_priority(inode
, page
, FLUSH_STABLE
);
1457 int nfs_set_page_dirty(struct page
*page
)
1459 struct address_space
*mapping
= page
->mapping
;
1460 struct inode
*inode
;
1461 struct nfs_page
*req
;
1466 inode
= mapping
->host
;
1469 spin_lock(&inode
->i_lock
);
1470 req
= nfs_page_find_request_locked(page
);
1472 /* Mark any existing write requests for flushing */
1473 ret
= !test_and_set_bit(PG_NEED_FLUSH
, &req
->wb_flags
);
1474 spin_unlock(&inode
->i_lock
);
1475 nfs_release_request(req
);
1478 ret
= __set_page_dirty_nobuffers(page
);
1479 spin_unlock(&inode
->i_lock
);
1482 return !TestSetPageDirty(page
);
1486 int __init
nfs_init_writepagecache(void)
1488 nfs_wdata_cachep
= kmem_cache_create("nfs_write_data",
1489 sizeof(struct nfs_write_data
),
1490 0, SLAB_HWCACHE_ALIGN
,
1492 if (nfs_wdata_cachep
== NULL
)
1495 nfs_wdata_mempool
= mempool_create_slab_pool(MIN_POOL_WRITE
,
1497 if (nfs_wdata_mempool
== NULL
)
1500 nfs_commit_mempool
= mempool_create_slab_pool(MIN_POOL_COMMIT
,
1502 if (nfs_commit_mempool
== NULL
)
1506 * NFS congestion size, scale with available memory.
1518 * This allows larger machines to have larger/more transfers.
1519 * Limit the default to 256M
1521 nfs_congestion_kb
= (16*int_sqrt(totalram_pages
)) << (PAGE_SHIFT
-10);
1522 if (nfs_congestion_kb
> 256*1024)
1523 nfs_congestion_kb
= 256*1024;
1528 void nfs_destroy_writepagecache(void)
1530 mempool_destroy(nfs_commit_mempool
);
1531 mempool_destroy(nfs_wdata_mempool
);
1532 kmem_cache_destroy(nfs_wdata_cachep
);