4 * Writing file data over NFS.
6 * We do it like this: When a (user) process wishes to write data to an
7 * NFS file, a write request is allocated that contains the RPC task data
8 * plus some info on the page to be written, and added to the inode's
9 * write chain. If the process writes past the end of the page, an async
10 * RPC call to write the page is scheduled immediately; otherwise, the call
11 * is delayed for a few seconds.
13 * Just like readahead, no async I/O is performed if wsize < PAGE_SIZE.
15 * Write requests are kept on the inode's writeback list. Each entry in
16 * that list references the page (portion) to be written. When the
17 * cache timeout has expired, the RPC task is woken up, and tries to
18 * lock the page. As soon as it manages to do so, the request is moved
19 * from the writeback list to the writelock list.
21 * Note: we must make sure never to confuse the inode passed in the
22 * write_page request with the one in page->inode. As far as I understand
23 * it, these are different when doing a swap-out.
25 * To understand everything that goes on here and in the NFS read code,
26 * one should be aware that a page is locked in exactly one of the following
29 * - A write request is in progress.
30 * - A user process is in generic_file_write/nfs_update_page
31 * - A user process is in generic_file_read
33 * Also note that because of the way pages are invalidated in
34 * nfs_revalidate_inode, the following assertions hold:
36 * - If a page is dirty, there will be no read requests (a page will
37 * not be re-read unless invalidated by nfs_revalidate_inode).
38 * - If the page is not uptodate, there will be no pending write
39 * requests, and no process will be in nfs_update_page.
41 * FIXME: Interaction with the vmscan routines is not optimal yet.
42 * Either vmscan must be made nfs-savvy, or we need a different page
43 * reclaim concept that supports something like FS-independent
44 * buffer_heads with a b_ops-> field.
46 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
49 #include <linux/config.h>
50 #include <linux/types.h>
51 #include <linux/slab.h>
53 #include <linux/pagemap.h>
54 #include <linux/file.h>
55 #include <linux/mpage.h>
56 #include <linux/writeback.h>
58 #include <linux/sunrpc/clnt.h>
59 #include <linux/nfs_fs.h>
60 #include <linux/nfs_mount.h>
61 #include <linux/nfs_page.h>
62 #include <asm/uaccess.h>
63 #include <linux/smp_lock.h>
65 #include "delegation.h"
67 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
69 #define MIN_POOL_WRITE (32)
70 #define MIN_POOL_COMMIT (4)
73 * Local function declarations
75 static struct nfs_page
* nfs_update_request(struct nfs_open_context
*,
78 unsigned int, unsigned int);
79 static void nfs_writeback_done_partial(struct nfs_write_data
*, int);
80 static void nfs_writeback_done_full(struct nfs_write_data
*, int);
81 static int nfs_wait_on_write_congestion(struct address_space
*, int);
82 static int nfs_wait_on_requests(struct inode
*, unsigned long, unsigned int);
83 static int nfs_flush_inode(struct inode
*inode
, unsigned long idx_start
,
84 unsigned int npages
, int how
);
86 static kmem_cache_t
*nfs_wdata_cachep
;
87 mempool_t
*nfs_wdata_mempool
;
88 static mempool_t
*nfs_commit_mempool
;
90 static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion
);
92 static inline struct nfs_write_data
*nfs_commit_alloc(void)
94 struct nfs_write_data
*p
= mempool_alloc(nfs_commit_mempool
, SLAB_NOFS
);
96 memset(p
, 0, sizeof(*p
));
97 INIT_LIST_HEAD(&p
->pages
);
102 static inline void nfs_commit_free(struct nfs_write_data
*p
)
104 mempool_free(p
, nfs_commit_mempool
);
107 void nfs_writedata_release(void *wdata
)
109 nfs_writedata_free(wdata
);
112 /* Adjust the file length if we're writing beyond the end */
113 static void nfs_grow_file(struct page
*page
, unsigned int offset
, unsigned int count
)
115 struct inode
*inode
= page
->mapping
->host
;
116 loff_t end
, i_size
= i_size_read(inode
);
117 unsigned long end_index
= (i_size
- 1) >> PAGE_CACHE_SHIFT
;
119 if (i_size
> 0 && page
->index
< end_index
)
121 end
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + ((loff_t
)offset
+count
);
124 i_size_write(inode
, end
);
127 /* We can set the PG_uptodate flag if we see that a write request
128 * covers the full page.
130 static void nfs_mark_uptodate(struct page
*page
, unsigned int base
, unsigned int count
)
134 if (PageUptodate(page
))
138 if (count
== PAGE_CACHE_SIZE
) {
139 SetPageUptodate(page
);
143 end_offs
= i_size_read(page
->mapping
->host
) - 1;
146 /* Is this the last page? */
147 if (page
->index
!= (unsigned long)(end_offs
>> PAGE_CACHE_SHIFT
))
149 /* This is the last page: set PG_uptodate if we cover the entire
150 * extent of the data, then zero the rest of the page.
152 if (count
== (unsigned int)(end_offs
& (PAGE_CACHE_SIZE
- 1)) + 1) {
153 memclear_highpage_flush(page
, count
, PAGE_CACHE_SIZE
- count
);
154 SetPageUptodate(page
);
159 * Write a page synchronously.
160 * Offset is the data offset within the page.
162 static int nfs_writepage_sync(struct nfs_open_context
*ctx
, struct inode
*inode
,
163 struct page
*page
, unsigned int offset
, unsigned int count
,
166 unsigned int wsize
= NFS_SERVER(inode
)->wsize
;
167 int result
, written
= 0;
168 struct nfs_write_data
*wdata
;
170 wdata
= nfs_writedata_alloc();
175 wdata
->cred
= ctx
->cred
;
176 wdata
->inode
= inode
;
177 wdata
->args
.fh
= NFS_FH(inode
);
178 wdata
->args
.context
= ctx
;
179 wdata
->args
.pages
= &page
;
180 wdata
->args
.stable
= NFS_FILE_SYNC
;
181 wdata
->args
.pgbase
= offset
;
182 wdata
->args
.count
= wsize
;
183 wdata
->res
.fattr
= &wdata
->fattr
;
184 wdata
->res
.verf
= &wdata
->verf
;
186 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
188 (long long)NFS_FILEID(inode
),
189 count
, (long long)(page_offset(page
) + offset
));
191 set_page_writeback(page
);
192 nfs_begin_data_update(inode
);
195 wdata
->args
.count
= count
;
196 wdata
->args
.offset
= page_offset(page
) + wdata
->args
.pgbase
;
198 result
= NFS_PROTO(inode
)->write(wdata
);
201 /* Must mark the page invalid after I/O error */
202 ClearPageUptodate(page
);
205 if (result
< wdata
->args
.count
)
206 printk(KERN_WARNING
"NFS: short write, count=%u, result=%d\n",
207 wdata
->args
.count
, result
);
209 wdata
->args
.offset
+= result
;
210 wdata
->args
.pgbase
+= result
;
214 /* Update file length */
215 nfs_grow_file(page
, offset
, written
);
216 /* Set the PG_uptodate flag? */
217 nfs_mark_uptodate(page
, offset
, written
);
220 ClearPageError(page
);
223 nfs_end_data_update(inode
);
224 end_page_writeback(page
);
225 nfs_writedata_free(wdata
);
226 return written
? written
: result
;
229 static int nfs_writepage_async(struct nfs_open_context
*ctx
,
230 struct inode
*inode
, struct page
*page
,
231 unsigned int offset
, unsigned int count
)
233 struct nfs_page
*req
;
235 req
= nfs_update_request(ctx
, inode
, page
, offset
, count
);
238 /* Update file length */
239 nfs_grow_file(page
, offset
, count
);
240 /* Set the PG_uptodate flag? */
241 nfs_mark_uptodate(page
, offset
, count
);
242 nfs_unlock_request(req
);
246 static int wb_priority(struct writeback_control
*wbc
)
248 if (wbc
->for_reclaim
)
249 return FLUSH_HIGHPRI
;
250 if (wbc
->for_kupdate
)
256 * Write an mmapped page to the server.
258 int nfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
260 struct nfs_open_context
*ctx
;
261 struct inode
*inode
= page
->mapping
->host
;
262 unsigned long end_index
;
263 unsigned offset
= PAGE_CACHE_SIZE
;
264 loff_t i_size
= i_size_read(inode
);
265 int inode_referenced
= 0;
266 int priority
= wb_priority(wbc
);
270 * Note: We need to ensure that we have a reference to the inode
271 * if we are to do asynchronous writes. If not, waiting
272 * in nfs_wait_on_request() may deadlock with clear_inode().
274 * If igrab() fails here, then it is in any case safe to
275 * call nfs_wb_page(), since there will be no pending writes.
277 if (igrab(inode
) != 0)
278 inode_referenced
= 1;
279 end_index
= i_size
>> PAGE_CACHE_SHIFT
;
281 /* Ensure we've flushed out any previous writes */
282 nfs_wb_page_priority(inode
, page
, priority
);
285 if (page
->index
< end_index
)
287 /* things got complicated... */
288 offset
= i_size
& (PAGE_CACHE_SIZE
-1);
290 /* OK, are we completely out? */
291 err
= 0; /* potential race with truncate - ignore */
292 if (page
->index
>= end_index
+1 || !offset
)
295 ctx
= nfs_find_open_context(inode
, NULL
, FMODE_WRITE
);
301 if (!IS_SYNC(inode
) && inode_referenced
) {
302 err
= nfs_writepage_async(ctx
, inode
, page
, 0, offset
);
303 if (!wbc
->for_writepages
)
304 nfs_flush_inode(inode
, 0, 0, wb_priority(wbc
));
306 err
= nfs_writepage_sync(ctx
, inode
, page
, 0,
310 redirty_page_for_writepage(wbc
, page
);
315 put_nfs_open_context(ctx
);
318 if (inode_referenced
)
324 * Note: causes nfs_update_request() to block on the assumption
325 * that the writeback is generated due to memory pressure.
327 int nfs_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
329 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
330 struct inode
*inode
= mapping
->host
;
333 err
= generic_writepages(mapping
, wbc
);
336 while (test_and_set_bit(BDI_write_congested
, &bdi
->state
) != 0) {
337 if (wbc
->nonblocking
)
339 nfs_wait_on_write_congestion(mapping
, 0);
341 err
= nfs_flush_inode(inode
, 0, 0, wb_priority(wbc
));
344 wbc
->nr_to_write
-= err
;
345 if (!wbc
->nonblocking
&& wbc
->sync_mode
== WB_SYNC_ALL
) {
346 err
= nfs_wait_on_requests(inode
, 0, 0);
350 err
= nfs_commit_inode(inode
, wb_priority(wbc
));
352 wbc
->nr_to_write
-= err
;
356 clear_bit(BDI_write_congested
, &bdi
->state
);
357 wake_up_all(&nfs_write_congestion
);
362 * Insert a write request into an inode
364 static int nfs_inode_add_request(struct inode
*inode
, struct nfs_page
*req
)
366 struct nfs_inode
*nfsi
= NFS_I(inode
);
369 error
= radix_tree_insert(&nfsi
->nfs_page_tree
, req
->wb_index
, req
);
370 BUG_ON(error
== -EEXIST
);
375 nfs_begin_data_update(inode
);
376 if (nfs_have_delegation(inode
, FMODE_WRITE
))
380 atomic_inc(&req
->wb_count
);
385 * Insert a write request into an inode
387 static void nfs_inode_remove_request(struct nfs_page
*req
)
389 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
390 struct nfs_inode
*nfsi
= NFS_I(inode
);
392 BUG_ON (!NFS_WBACK_BUSY(req
));
394 spin_lock(&nfsi
->req_lock
);
395 radix_tree_delete(&nfsi
->nfs_page_tree
, req
->wb_index
);
398 spin_unlock(&nfsi
->req_lock
);
399 nfs_end_data_update(inode
);
402 spin_unlock(&nfsi
->req_lock
);
403 nfs_clear_request(req
);
404 nfs_release_request(req
);
410 static inline struct nfs_page
*
411 _nfs_find_request(struct inode
*inode
, unsigned long index
)
413 struct nfs_inode
*nfsi
= NFS_I(inode
);
414 struct nfs_page
*req
;
416 req
= (struct nfs_page
*)radix_tree_lookup(&nfsi
->nfs_page_tree
, index
);
418 atomic_inc(&req
->wb_count
);
422 static struct nfs_page
*
423 nfs_find_request(struct inode
*inode
, unsigned long index
)
425 struct nfs_page
*req
;
426 struct nfs_inode
*nfsi
= NFS_I(inode
);
428 spin_lock(&nfsi
->req_lock
);
429 req
= _nfs_find_request(inode
, index
);
430 spin_unlock(&nfsi
->req_lock
);
435 * Add a request to the inode's dirty list.
438 nfs_mark_request_dirty(struct nfs_page
*req
)
440 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
441 struct nfs_inode
*nfsi
= NFS_I(inode
);
443 spin_lock(&nfsi
->req_lock
);
444 radix_tree_tag_set(&nfsi
->nfs_page_tree
,
445 req
->wb_index
, NFS_PAGE_TAG_DIRTY
);
446 nfs_list_add_request(req
, &nfsi
->dirty
);
448 spin_unlock(&nfsi
->req_lock
);
449 inc_page_state(nr_dirty
);
450 mark_inode_dirty(inode
);
454 * Check if a request is dirty
457 nfs_dirty_request(struct nfs_page
*req
)
459 struct nfs_inode
*nfsi
= NFS_I(req
->wb_context
->dentry
->d_inode
);
460 return !list_empty(&req
->wb_list
) && req
->wb_list_head
== &nfsi
->dirty
;
463 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
465 * Add a request to the inode's commit list.
468 nfs_mark_request_commit(struct nfs_page
*req
)
470 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
471 struct nfs_inode
*nfsi
= NFS_I(inode
);
473 spin_lock(&nfsi
->req_lock
);
474 nfs_list_add_request(req
, &nfsi
->commit
);
476 spin_unlock(&nfsi
->req_lock
);
477 inc_page_state(nr_unstable
);
478 mark_inode_dirty(inode
);
483 * Wait for a request to complete.
485 * Interruptible by signals only if mounted with intr flag.
488 nfs_wait_on_requests(struct inode
*inode
, unsigned long idx_start
, unsigned int npages
)
490 struct nfs_inode
*nfsi
= NFS_I(inode
);
491 struct nfs_page
*req
;
492 unsigned long idx_end
, next
;
493 unsigned int res
= 0;
499 idx_end
= idx_start
+ npages
- 1;
501 spin_lock(&nfsi
->req_lock
);
503 while (radix_tree_gang_lookup_tag(&nfsi
->nfs_page_tree
, (void **)&req
, next
, 1, NFS_PAGE_TAG_WRITEBACK
)) {
504 if (req
->wb_index
> idx_end
)
507 next
= req
->wb_index
+ 1;
508 BUG_ON(!NFS_WBACK_BUSY(req
));
510 atomic_inc(&req
->wb_count
);
511 spin_unlock(&nfsi
->req_lock
);
512 error
= nfs_wait_on_request(req
);
513 nfs_release_request(req
);
516 spin_lock(&nfsi
->req_lock
);
519 spin_unlock(&nfsi
->req_lock
);
524 * nfs_scan_dirty - Scan an inode for dirty requests
525 * @inode: NFS inode to scan
526 * @dst: destination list
527 * @idx_start: lower bound of page->index to scan.
528 * @npages: idx_start + npages sets the upper bound to scan.
530 * Moves requests from the inode's dirty page list.
531 * The requests are *not* checked to ensure that they form a contiguous set.
534 nfs_scan_dirty(struct inode
*inode
, struct list_head
*dst
, unsigned long idx_start
, unsigned int npages
)
536 struct nfs_inode
*nfsi
= NFS_I(inode
);
539 if (nfsi
->ndirty
!= 0) {
540 res
= nfs_scan_lock_dirty(nfsi
, dst
, idx_start
, npages
);
542 sub_page_state(nr_dirty
,res
);
543 if ((nfsi
->ndirty
== 0) != list_empty(&nfsi
->dirty
))
544 printk(KERN_ERR
"NFS: desynchronized value of nfs_i.ndirty.\n");
549 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
551 * nfs_scan_commit - Scan an inode for commit requests
552 * @inode: NFS inode to scan
553 * @dst: destination list
554 * @idx_start: lower bound of page->index to scan.
555 * @npages: idx_start + npages sets the upper bound to scan.
557 * Moves requests from the inode's 'commit' request list.
558 * The requests are *not* checked to ensure that they form a contiguous set.
561 nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
, unsigned long idx_start
, unsigned int npages
)
563 struct nfs_inode
*nfsi
= NFS_I(inode
);
566 if (nfsi
->ncommit
!= 0) {
567 res
= nfs_scan_list(&nfsi
->commit
, dst
, idx_start
, npages
);
568 nfsi
->ncommit
-= res
;
569 if ((nfsi
->ncommit
== 0) != list_empty(&nfsi
->commit
))
570 printk(KERN_ERR
"NFS: desynchronized value of nfs_i.ncommit.\n");
576 static int nfs_wait_on_write_congestion(struct address_space
*mapping
, int intr
)
578 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
584 if (!bdi_write_congested(bdi
))
587 struct rpc_clnt
*clnt
= NFS_CLIENT(mapping
->host
);
590 rpc_clnt_sigmask(clnt
, &oldset
);
591 prepare_to_wait(&nfs_write_congestion
, &wait
, TASK_INTERRUPTIBLE
);
592 if (bdi_write_congested(bdi
)) {
598 rpc_clnt_sigunmask(clnt
, &oldset
);
600 prepare_to_wait(&nfs_write_congestion
, &wait
, TASK_UNINTERRUPTIBLE
);
601 if (bdi_write_congested(bdi
))
604 finish_wait(&nfs_write_congestion
, &wait
);
610 * Try to update any existing write request, or create one if there is none.
611 * In order to match, the request's credentials must match those of
612 * the calling process.
614 * Note: Should always be called with the Page Lock held!
616 static struct nfs_page
* nfs_update_request(struct nfs_open_context
* ctx
,
617 struct inode
*inode
, struct page
*page
,
618 unsigned int offset
, unsigned int bytes
)
620 struct nfs_server
*server
= NFS_SERVER(inode
);
621 struct nfs_inode
*nfsi
= NFS_I(inode
);
622 struct nfs_page
*req
, *new = NULL
;
623 unsigned long rqend
, end
;
625 end
= offset
+ bytes
;
627 if (nfs_wait_on_write_congestion(page
->mapping
, server
->flags
& NFS_MOUNT_INTR
))
628 return ERR_PTR(-ERESTARTSYS
);
630 /* Loop over all inode entries and see if we find
631 * A request for the page we wish to update
633 spin_lock(&nfsi
->req_lock
);
634 req
= _nfs_find_request(inode
, page
->index
);
636 if (!nfs_lock_request_dontget(req
)) {
638 spin_unlock(&nfsi
->req_lock
);
639 error
= nfs_wait_on_request(req
);
640 nfs_release_request(req
);
642 return ERR_PTR(error
);
645 spin_unlock(&nfsi
->req_lock
);
647 nfs_release_request(new);
653 nfs_lock_request_dontget(new);
654 error
= nfs_inode_add_request(inode
, new);
656 spin_unlock(&nfsi
->req_lock
);
657 nfs_unlock_request(new);
658 return ERR_PTR(error
);
660 spin_unlock(&nfsi
->req_lock
);
661 nfs_mark_request_dirty(new);
664 spin_unlock(&nfsi
->req_lock
);
666 new = nfs_create_request(ctx
, inode
, page
, offset
, bytes
);
671 /* We have a request for our page.
672 * If the creds don't match, or the
673 * page addresses don't match,
674 * tell the caller to wait on the conflicting
677 rqend
= req
->wb_offset
+ req
->wb_bytes
;
678 if (req
->wb_context
!= ctx
679 || req
->wb_page
!= page
680 || !nfs_dirty_request(req
)
681 || offset
> rqend
|| end
< req
->wb_offset
) {
682 nfs_unlock_request(req
);
683 return ERR_PTR(-EBUSY
);
686 /* Okay, the request matches. Update the region */
687 if (offset
< req
->wb_offset
) {
688 req
->wb_offset
= offset
;
689 req
->wb_pgbase
= offset
;
690 req
->wb_bytes
= rqend
- req
->wb_offset
;
694 req
->wb_bytes
= end
- req
->wb_offset
;
699 int nfs_flush_incompatible(struct file
*file
, struct page
*page
)
701 struct nfs_open_context
*ctx
= (struct nfs_open_context
*)file
->private_data
;
702 struct inode
*inode
= page
->mapping
->host
;
703 struct nfs_page
*req
;
706 * Look for a request corresponding to this page. If there
707 * is one, and it belongs to another file, we flush it out
708 * before we try to copy anything into the page. Do this
709 * due to the lack of an ACCESS-type call in NFSv2.
710 * Also do the same if we find a request from an existing
713 req
= nfs_find_request(inode
, page
->index
);
715 if (req
->wb_page
!= page
|| ctx
!= req
->wb_context
)
716 status
= nfs_wb_page(inode
, page
);
717 nfs_release_request(req
);
719 return (status
< 0) ? status
: 0;
723 * Update and possibly write a cached page of an NFS file.
725 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
726 * things with a page scheduled for an RPC call (e.g. invalidate it).
728 int nfs_updatepage(struct file
*file
, struct page
*page
,
729 unsigned int offset
, unsigned int count
)
731 struct nfs_open_context
*ctx
= (struct nfs_open_context
*)file
->private_data
;
732 struct inode
*inode
= page
->mapping
->host
;
733 struct nfs_page
*req
;
736 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
737 file
->f_dentry
->d_parent
->d_name
.name
,
738 file
->f_dentry
->d_name
.name
, count
,
739 (long long)(page_offset(page
) +offset
));
741 if (IS_SYNC(inode
)) {
742 status
= nfs_writepage_sync(ctx
, inode
, page
, offset
, count
, 0);
744 if (offset
== 0 && status
== PAGE_CACHE_SIZE
)
745 SetPageUptodate(page
);
751 /* If we're not using byte range locks, and we know the page
752 * is entirely in cache, it may be more efficient to avoid
753 * fragmenting write requests.
755 if (PageUptodate(page
) && inode
->i_flock
== NULL
&& !(file
->f_mode
& O_SYNC
)) {
756 loff_t end_offs
= i_size_read(inode
) - 1;
757 unsigned long end_index
= end_offs
>> PAGE_CACHE_SHIFT
;
761 if (unlikely(end_offs
< 0)) {
763 } else if (page
->index
== end_index
) {
765 pglen
= (unsigned int)(end_offs
& (PAGE_CACHE_SIZE
-1)) + 1;
768 } else if (page
->index
< end_index
)
769 count
= PAGE_CACHE_SIZE
;
773 * Try to find an NFS request corresponding to this page
775 * If the existing request cannot be updated, we must flush
779 req
= nfs_update_request(ctx
, inode
, page
, offset
, count
);
780 status
= (IS_ERR(req
)) ? PTR_ERR(req
) : 0;
781 if (status
!= -EBUSY
)
783 /* Request could not be updated. Flush it out and try again */
784 status
= nfs_wb_page(inode
, page
);
785 } while (status
>= 0);
791 /* Update file length */
792 nfs_grow_file(page
, offset
, count
);
793 /* Set the PG_uptodate flag? */
794 nfs_mark_uptodate(page
, req
->wb_pgbase
, req
->wb_bytes
);
795 nfs_unlock_request(req
);
797 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
798 status
, (long long)i_size_read(inode
));
800 ClearPageUptodate(page
);
804 static void nfs_writepage_release(struct nfs_page
*req
)
806 end_page_writeback(req
->wb_page
);
808 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
809 if (!PageError(req
->wb_page
)) {
810 if (NFS_NEED_RESCHED(req
)) {
811 nfs_mark_request_dirty(req
);
813 } else if (NFS_NEED_COMMIT(req
)) {
814 nfs_mark_request_commit(req
);
818 nfs_inode_remove_request(req
);
821 nfs_clear_commit(req
);
822 nfs_clear_reschedule(req
);
824 nfs_inode_remove_request(req
);
826 nfs_clear_page_writeback(req
);
829 static inline int flush_task_priority(int how
)
831 switch (how
& (FLUSH_HIGHPRI
|FLUSH_LOWPRI
)) {
833 return RPC_PRIORITY_HIGH
;
835 return RPC_PRIORITY_LOW
;
837 return RPC_PRIORITY_NORMAL
;
841 * Set up the argument/result storage required for the RPC call.
843 static void nfs_write_rpcsetup(struct nfs_page
*req
,
844 struct nfs_write_data
*data
,
845 unsigned int count
, unsigned int offset
,
850 /* Set up the RPC argument and reply structs
851 * NB: take care not to mess about with data->commit et al. */
854 data
->inode
= inode
= req
->wb_context
->dentry
->d_inode
;
855 data
->cred
= req
->wb_context
->cred
;
857 data
->args
.fh
= NFS_FH(inode
);
858 data
->args
.offset
= req_offset(req
) + offset
;
859 data
->args
.pgbase
= req
->wb_pgbase
+ offset
;
860 data
->args
.pages
= data
->pagevec
;
861 data
->args
.count
= count
;
862 data
->args
.context
= req
->wb_context
;
864 data
->res
.fattr
= &data
->fattr
;
865 data
->res
.count
= count
;
866 data
->res
.verf
= &data
->verf
;
867 nfs_fattr_init(&data
->fattr
);
869 NFS_PROTO(inode
)->write_setup(data
, how
);
871 data
->task
.tk_priority
= flush_task_priority(how
);
872 data
->task
.tk_cookie
= (unsigned long)inode
;
874 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
877 (long long)NFS_FILEID(inode
),
879 (unsigned long long)data
->args
.offset
);
882 static void nfs_execute_write(struct nfs_write_data
*data
)
884 struct rpc_clnt
*clnt
= NFS_CLIENT(data
->inode
);
887 rpc_clnt_sigmask(clnt
, &oldset
);
889 rpc_execute(&data
->task
);
891 rpc_clnt_sigunmask(clnt
, &oldset
);
895 * Generate multiple small requests to write out a single
896 * contiguous dirty area on one page.
898 static int nfs_flush_multi(struct list_head
*head
, struct inode
*inode
, int how
)
900 struct nfs_page
*req
= nfs_list_entry(head
->next
);
901 struct page
*page
= req
->wb_page
;
902 struct nfs_write_data
*data
;
903 unsigned int wsize
= NFS_SERVER(inode
)->wsize
;
904 unsigned int nbytes
, offset
;
908 nfs_list_remove_request(req
);
910 nbytes
= req
->wb_bytes
;
912 data
= nfs_writedata_alloc();
915 list_add(&data
->pages
, &list
);
921 atomic_set(&req
->wb_complete
, requests
);
923 ClearPageError(page
);
924 set_page_writeback(page
);
926 nbytes
= req
->wb_bytes
;
928 data
= list_entry(list
.next
, struct nfs_write_data
, pages
);
929 list_del_init(&data
->pages
);
931 data
->pagevec
[0] = page
;
932 data
->complete
= nfs_writeback_done_partial
;
934 if (nbytes
> wsize
) {
935 nfs_write_rpcsetup(req
, data
, wsize
, offset
, how
);
939 nfs_write_rpcsetup(req
, data
, nbytes
, offset
, how
);
942 nfs_execute_write(data
);
943 } while (nbytes
!= 0);
948 while (!list_empty(&list
)) {
949 data
= list_entry(list
.next
, struct nfs_write_data
, pages
);
950 list_del(&data
->pages
);
951 nfs_writedata_free(data
);
953 nfs_mark_request_dirty(req
);
954 nfs_clear_page_writeback(req
);
959 * Create an RPC task for the given write request and kick it.
960 * The page must have been locked by the caller.
962 * It may happen that the page we're passed is not marked dirty.
963 * This is the case if nfs_updatepage detects a conflicting request
964 * that has been written but not committed.
966 static int nfs_flush_one(struct list_head
*head
, struct inode
*inode
, int how
)
968 struct nfs_page
*req
;
970 struct nfs_write_data
*data
;
973 if (NFS_SERVER(inode
)->wsize
< PAGE_CACHE_SIZE
)
974 return nfs_flush_multi(head
, inode
, how
);
976 data
= nfs_writedata_alloc();
980 pages
= data
->pagevec
;
982 while (!list_empty(head
)) {
983 req
= nfs_list_entry(head
->next
);
984 nfs_list_remove_request(req
);
985 nfs_list_add_request(req
, &data
->pages
);
986 ClearPageError(req
->wb_page
);
987 set_page_writeback(req
->wb_page
);
988 *pages
++ = req
->wb_page
;
989 count
+= req
->wb_bytes
;
991 req
= nfs_list_entry(data
->pages
.next
);
993 data
->complete
= nfs_writeback_done_full
;
994 /* Set up the argument struct */
995 nfs_write_rpcsetup(req
, data
, count
, 0, how
);
997 nfs_execute_write(data
);
1000 while (!list_empty(head
)) {
1001 struct nfs_page
*req
= nfs_list_entry(head
->next
);
1002 nfs_list_remove_request(req
);
1003 nfs_mark_request_dirty(req
);
1004 nfs_clear_page_writeback(req
);
1010 nfs_flush_list(struct list_head
*head
, int wpages
, int how
)
1012 LIST_HEAD(one_request
);
1013 struct nfs_page
*req
;
1015 unsigned int pages
= 0;
1017 while (!list_empty(head
)) {
1018 pages
+= nfs_coalesce_requests(head
, &one_request
, wpages
);
1019 req
= nfs_list_entry(one_request
.next
);
1020 error
= nfs_flush_one(&one_request
, req
->wb_context
->dentry
->d_inode
, how
);
1027 while (!list_empty(head
)) {
1028 req
= nfs_list_entry(head
->next
);
1029 nfs_list_remove_request(req
);
1030 nfs_mark_request_dirty(req
);
1031 nfs_clear_page_writeback(req
);
1037 * Handle a write reply that flushed part of a page.
1039 static void nfs_writeback_done_partial(struct nfs_write_data
*data
, int status
)
1041 struct nfs_page
*req
= data
->req
;
1042 struct page
*page
= req
->wb_page
;
1044 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1045 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
1046 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
1048 (long long)req_offset(req
));
1051 ClearPageUptodate(page
);
1053 req
->wb_context
->error
= status
;
1054 dprintk(", error = %d\n", status
);
1056 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1057 if (data
->verf
.committed
< NFS_FILE_SYNC
) {
1058 if (!NFS_NEED_COMMIT(req
)) {
1059 nfs_defer_commit(req
);
1060 memcpy(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
));
1061 dprintk(" defer commit\n");
1062 } else if (memcmp(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
))) {
1063 nfs_defer_reschedule(req
);
1064 dprintk(" server reboot detected\n");
1071 if (atomic_dec_and_test(&req
->wb_complete
))
1072 nfs_writepage_release(req
);
1076 * Handle a write reply that flushes a whole page.
1078 * FIXME: There is an inherent race with invalidate_inode_pages and
1079 * writebacks since the page->count is kept > 1 for as long
1080 * as the page has a write request pending.
1082 static void nfs_writeback_done_full(struct nfs_write_data
*data
, int status
)
1084 struct nfs_page
*req
;
1087 /* Update attributes as result of writeback. */
1088 while (!list_empty(&data
->pages
)) {
1089 req
= nfs_list_entry(data
->pages
.next
);
1090 nfs_list_remove_request(req
);
1091 page
= req
->wb_page
;
1093 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1094 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
1095 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
1097 (long long)req_offset(req
));
1100 ClearPageUptodate(page
);
1102 req
->wb_context
->error
= status
;
1103 end_page_writeback(page
);
1104 nfs_inode_remove_request(req
);
1105 dprintk(", error = %d\n", status
);
1108 end_page_writeback(page
);
1110 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1111 if (data
->args
.stable
!= NFS_UNSTABLE
|| data
->verf
.committed
== NFS_FILE_SYNC
) {
1112 nfs_inode_remove_request(req
);
1116 memcpy(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
));
1117 nfs_mark_request_commit(req
);
1118 dprintk(" marked for commit\n");
1120 nfs_inode_remove_request(req
);
1123 nfs_clear_page_writeback(req
);
1128 * This function is called when the WRITE call is complete.
1130 void nfs_writeback_done(struct rpc_task
*task
, void *calldata
)
1132 struct nfs_write_data
*data
= calldata
;
1133 struct nfs_writeargs
*argp
= &data
->args
;
1134 struct nfs_writeres
*resp
= &data
->res
;
1136 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1137 task
->tk_pid
, task
->tk_status
);
1139 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1140 if (resp
->verf
->committed
< argp
->stable
&& task
->tk_status
>= 0) {
1141 /* We tried a write call, but the server did not
1142 * commit data to stable storage even though we
1144 * Note: There is a known bug in Tru64 < 5.0 in which
1145 * the server reports NFS_DATA_SYNC, but performs
1146 * NFS_FILE_SYNC. We therefore implement this checking
1147 * as a dprintk() in order to avoid filling syslog.
1149 static unsigned long complain
;
1151 if (time_before(complain
, jiffies
)) {
1152 dprintk("NFS: faulty NFS server %s:"
1153 " (committed = %d) != (stable = %d)\n",
1154 NFS_SERVER(data
->inode
)->hostname
,
1155 resp
->verf
->committed
, argp
->stable
);
1156 complain
= jiffies
+ 300 * HZ
;
1160 /* Is this a short write? */
1161 if (task
->tk_status
>= 0 && resp
->count
< argp
->count
) {
1162 static unsigned long complain
;
1164 /* Has the server at least made some progress? */
1165 if (resp
->count
!= 0) {
1166 /* Was this an NFSv2 write or an NFSv3 stable write? */
1167 if (resp
->verf
->committed
!= NFS_UNSTABLE
) {
1168 /* Resend from where the server left off */
1169 argp
->offset
+= resp
->count
;
1170 argp
->pgbase
+= resp
->count
;
1171 argp
->count
-= resp
->count
;
1173 /* Resend as a stable write in order to avoid
1174 * headaches in the case of a server crash.
1176 argp
->stable
= NFS_FILE_SYNC
;
1178 rpc_restart_call(task
);
1181 if (time_before(complain
, jiffies
)) {
1183 "NFS: Server wrote zero bytes, expected %u.\n",
1185 complain
= jiffies
+ 300 * HZ
;
1187 /* Can't do anything about it except throw an error. */
1188 task
->tk_status
= -EIO
;
1192 * Process the nfs_page list
1194 data
->complete(data
, task
->tk_status
);
1198 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1199 void nfs_commit_release(void *wdata
)
1201 nfs_commit_free(wdata
);
1205 * Set up the argument/result storage required for the RPC call.
1207 static void nfs_commit_rpcsetup(struct list_head
*head
,
1208 struct nfs_write_data
*data
, int how
)
1210 struct nfs_page
*first
;
1211 struct inode
*inode
;
1213 /* Set up the RPC argument and reply structs
1214 * NB: take care not to mess about with data->commit et al. */
1216 list_splice_init(head
, &data
->pages
);
1217 first
= nfs_list_entry(data
->pages
.next
);
1218 inode
= first
->wb_context
->dentry
->d_inode
;
1220 data
->inode
= inode
;
1221 data
->cred
= first
->wb_context
->cred
;
1223 data
->args
.fh
= NFS_FH(data
->inode
);
1224 /* Note: we always request a commit of the entire inode */
1225 data
->args
.offset
= 0;
1226 data
->args
.count
= 0;
1227 data
->res
.count
= 0;
1228 data
->res
.fattr
= &data
->fattr
;
1229 data
->res
.verf
= &data
->verf
;
1230 nfs_fattr_init(&data
->fattr
);
1232 NFS_PROTO(inode
)->commit_setup(data
, how
);
1234 data
->task
.tk_priority
= flush_task_priority(how
);
1235 data
->task
.tk_cookie
= (unsigned long)inode
;
1237 dprintk("NFS: %4d initiated commit call\n", data
->task
.tk_pid
);
1241 * Commit dirty pages
1244 nfs_commit_list(struct list_head
*head
, int how
)
1246 struct nfs_write_data
*data
;
1247 struct nfs_page
*req
;
1249 data
= nfs_commit_alloc();
1254 /* Set up the argument struct */
1255 nfs_commit_rpcsetup(head
, data
, how
);
1257 nfs_execute_write(data
);
1260 while (!list_empty(head
)) {
1261 req
= nfs_list_entry(head
->next
);
1262 nfs_list_remove_request(req
);
1263 nfs_mark_request_commit(req
);
1264 nfs_clear_page_writeback(req
);
1270 * COMMIT call returned
1272 void nfs_commit_done(struct rpc_task
*task
, void *calldata
)
1274 struct nfs_write_data
*data
= calldata
;
1275 struct nfs_page
*req
;
1278 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1279 task
->tk_pid
, task
->tk_status
);
1281 while (!list_empty(&data
->pages
)) {
1282 req
= nfs_list_entry(data
->pages
.next
);
1283 nfs_list_remove_request(req
);
1285 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1286 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
1287 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
1289 (long long)req_offset(req
));
1290 if (task
->tk_status
< 0) {
1291 req
->wb_context
->error
= task
->tk_status
;
1292 nfs_inode_remove_request(req
);
1293 dprintk(", error = %d\n", task
->tk_status
);
1297 /* Okay, COMMIT succeeded, apparently. Check the verifier
1298 * returned by the server against all stored verfs. */
1299 if (!memcmp(req
->wb_verf
.verifier
, data
->verf
.verifier
, sizeof(data
->verf
.verifier
))) {
1300 /* We have a match */
1301 nfs_inode_remove_request(req
);
1305 /* We have a mismatch. Write the page again */
1306 dprintk(" mismatch\n");
1307 nfs_mark_request_dirty(req
);
1309 nfs_clear_page_writeback(req
);
1312 sub_page_state(nr_unstable
,res
);
1316 static int nfs_flush_inode(struct inode
*inode
, unsigned long idx_start
,
1317 unsigned int npages
, int how
)
1319 struct nfs_inode
*nfsi
= NFS_I(inode
);
1324 spin_lock(&nfsi
->req_lock
);
1325 res
= nfs_scan_dirty(inode
, &head
, idx_start
, npages
);
1326 spin_unlock(&nfsi
->req_lock
);
1328 struct nfs_server
*server
= NFS_SERVER(inode
);
1330 /* For single writes, FLUSH_STABLE is more efficient */
1331 if (res
== nfsi
->npages
&& nfsi
->npages
<= server
->wpages
) {
1332 if (res
> 1 || nfs_list_entry(head
.next
)->wb_bytes
<= server
->wsize
)
1333 how
|= FLUSH_STABLE
;
1335 error
= nfs_flush_list(&head
, server
->wpages
, how
);
1342 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1343 int nfs_commit_inode(struct inode
*inode
, int how
)
1345 struct nfs_inode
*nfsi
= NFS_I(inode
);
1350 spin_lock(&nfsi
->req_lock
);
1351 res
= nfs_scan_commit(inode
, &head
, 0, 0);
1352 spin_unlock(&nfsi
->req_lock
);
1354 error
= nfs_commit_list(&head
, how
);
1362 int nfs_sync_inode(struct inode
*inode
, unsigned long idx_start
,
1363 unsigned int npages
, int how
)
1368 wait
= how
& FLUSH_WAIT
;
1374 error
= nfs_wait_on_requests(inode
, idx_start
, npages
);
1376 error
= nfs_flush_inode(inode
, idx_start
, npages
, how
);
1377 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1379 error
= nfs_commit_inode(inode
, how
);
1381 } while (error
> 0);
1385 int nfs_init_writepagecache(void)
1387 nfs_wdata_cachep
= kmem_cache_create("nfs_write_data",
1388 sizeof(struct nfs_write_data
),
1389 0, SLAB_HWCACHE_ALIGN
,
1391 if (nfs_wdata_cachep
== NULL
)
1394 nfs_wdata_mempool
= mempool_create(MIN_POOL_WRITE
,
1398 if (nfs_wdata_mempool
== NULL
)
1401 nfs_commit_mempool
= mempool_create(MIN_POOL_COMMIT
,
1405 if (nfs_commit_mempool
== NULL
)
1411 void nfs_destroy_writepagecache(void)
1413 mempool_destroy(nfs_commit_mempool
);
1414 mempool_destroy(nfs_wdata_mempool
);
1415 if (kmem_cache_destroy(nfs_wdata_cachep
))
1416 printk(KERN_INFO
"nfs_write_data: not all structures were freed\n");