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 static void nfs_writedata_release(struct rpc_task
*task
)
109 struct nfs_write_data
*wdata
= (struct nfs_write_data
*)task
->tk_calldata
;
110 nfs_writedata_free(wdata
);
113 /* Adjust the file length if we're writing beyond the end */
114 static void nfs_grow_file(struct page
*page
, unsigned int offset
, unsigned int count
)
116 struct inode
*inode
= page
->mapping
->host
;
117 loff_t end
, i_size
= i_size_read(inode
);
118 unsigned long end_index
= (i_size
- 1) >> PAGE_CACHE_SHIFT
;
120 if (i_size
> 0 && page
->index
< end_index
)
122 end
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + ((loff_t
)offset
+count
);
125 i_size_write(inode
, end
);
128 /* We can set the PG_uptodate flag if we see that a write request
129 * covers the full page.
131 static void nfs_mark_uptodate(struct page
*page
, unsigned int base
, unsigned int count
)
135 if (PageUptodate(page
))
139 if (count
== PAGE_CACHE_SIZE
) {
140 SetPageUptodate(page
);
144 end_offs
= i_size_read(page
->mapping
->host
) - 1;
147 /* Is this the last page? */
148 if (page
->index
!= (unsigned long)(end_offs
>> PAGE_CACHE_SHIFT
))
150 /* This is the last page: set PG_uptodate if we cover the entire
151 * extent of the data, then zero the rest of the page.
153 if (count
== (unsigned int)(end_offs
& (PAGE_CACHE_SIZE
- 1)) + 1) {
154 memclear_highpage_flush(page
, count
, PAGE_CACHE_SIZE
- count
);
155 SetPageUptodate(page
);
160 * Write a page synchronously.
161 * Offset is the data offset within the page.
163 static int nfs_writepage_sync(struct nfs_open_context
*ctx
, struct inode
*inode
,
164 struct page
*page
, unsigned int offset
, unsigned int count
,
167 unsigned int wsize
= NFS_SERVER(inode
)->wsize
;
168 int result
, written
= 0;
169 struct nfs_write_data
*wdata
;
171 wdata
= nfs_writedata_alloc();
176 wdata
->cred
= ctx
->cred
;
177 wdata
->inode
= inode
;
178 wdata
->args
.fh
= NFS_FH(inode
);
179 wdata
->args
.context
= ctx
;
180 wdata
->args
.pages
= &page
;
181 wdata
->args
.stable
= NFS_FILE_SYNC
;
182 wdata
->args
.pgbase
= offset
;
183 wdata
->args
.count
= wsize
;
184 wdata
->res
.fattr
= &wdata
->fattr
;
185 wdata
->res
.verf
= &wdata
->verf
;
187 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
189 (long long)NFS_FILEID(inode
),
190 count
, (long long)(page_offset(page
) + offset
));
192 set_page_writeback(page
);
193 nfs_begin_data_update(inode
);
196 wdata
->args
.count
= count
;
197 wdata
->args
.offset
= page_offset(page
) + wdata
->args
.pgbase
;
199 result
= NFS_PROTO(inode
)->write(wdata
);
202 /* Must mark the page invalid after I/O error */
203 ClearPageUptodate(page
);
206 if (result
< wdata
->args
.count
)
207 printk(KERN_WARNING
"NFS: short write, count=%u, result=%d\n",
208 wdata
->args
.count
, result
);
210 wdata
->args
.offset
+= result
;
211 wdata
->args
.pgbase
+= result
;
215 /* Update file length */
216 nfs_grow_file(page
, offset
, written
);
217 /* Set the PG_uptodate flag? */
218 nfs_mark_uptodate(page
, offset
, written
);
221 ClearPageError(page
);
224 nfs_end_data_update(inode
);
225 end_page_writeback(page
);
226 nfs_writedata_free(wdata
);
227 return written
? written
: result
;
230 static int nfs_writepage_async(struct nfs_open_context
*ctx
,
231 struct inode
*inode
, struct page
*page
,
232 unsigned int offset
, unsigned int count
)
234 struct nfs_page
*req
;
237 req
= nfs_update_request(ctx
, inode
, page
, offset
, count
);
238 status
= (IS_ERR(req
)) ? PTR_ERR(req
) : 0;
241 /* Update file length */
242 nfs_grow_file(page
, offset
, count
);
243 /* Set the PG_uptodate flag? */
244 nfs_mark_uptodate(page
, offset
, count
);
245 nfs_unlock_request(req
);
250 static int wb_priority(struct writeback_control
*wbc
)
252 if (wbc
->for_reclaim
)
253 return FLUSH_HIGHPRI
;
254 if (wbc
->for_kupdate
)
260 * Write an mmapped page to the server.
262 int nfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
264 struct nfs_open_context
*ctx
;
265 struct inode
*inode
= page
->mapping
->host
;
266 unsigned long end_index
;
267 unsigned offset
= PAGE_CACHE_SIZE
;
268 loff_t i_size
= i_size_read(inode
);
269 int inode_referenced
= 0;
270 int priority
= wb_priority(wbc
);
274 * Note: We need to ensure that we have a reference to the inode
275 * if we are to do asynchronous writes. If not, waiting
276 * in nfs_wait_on_request() may deadlock with clear_inode().
278 * If igrab() fails here, then it is in any case safe to
279 * call nfs_wb_page(), since there will be no pending writes.
281 if (igrab(inode
) != 0)
282 inode_referenced
= 1;
283 end_index
= i_size
>> PAGE_CACHE_SHIFT
;
285 /* Ensure we've flushed out any previous writes */
286 nfs_wb_page_priority(inode
, page
, priority
);
289 if (page
->index
< end_index
)
291 /* things got complicated... */
292 offset
= i_size
& (PAGE_CACHE_SIZE
-1);
294 /* OK, are we completely out? */
295 err
= 0; /* potential race with truncate - ignore */
296 if (page
->index
>= end_index
+1 || !offset
)
299 ctx
= nfs_find_open_context(inode
, NULL
, FMODE_WRITE
);
305 if (!IS_SYNC(inode
) && inode_referenced
) {
306 err
= nfs_writepage_async(ctx
, inode
, page
, 0, offset
);
309 if (wbc
->for_reclaim
)
310 nfs_flush_inode(inode
, 0, 0, FLUSH_STABLE
);
313 err
= nfs_writepage_sync(ctx
, inode
, page
, 0,
317 redirty_page_for_writepage(wbc
, page
);
322 put_nfs_open_context(ctx
);
325 if (inode_referenced
)
331 * Note: causes nfs_update_request() to block on the assumption
332 * that the writeback is generated due to memory pressure.
334 int nfs_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
336 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
337 struct inode
*inode
= mapping
->host
;
340 err
= generic_writepages(mapping
, wbc
);
343 while (test_and_set_bit(BDI_write_congested
, &bdi
->state
) != 0) {
344 if (wbc
->nonblocking
)
346 nfs_wait_on_write_congestion(mapping
, 0);
348 err
= nfs_flush_inode(inode
, 0, 0, wb_priority(wbc
));
351 wbc
->nr_to_write
-= err
;
352 if (!wbc
->nonblocking
&& wbc
->sync_mode
== WB_SYNC_ALL
) {
353 err
= nfs_wait_on_requests(inode
, 0, 0);
357 err
= nfs_commit_inode(inode
, wb_priority(wbc
));
359 wbc
->nr_to_write
-= err
;
363 clear_bit(BDI_write_congested
, &bdi
->state
);
364 wake_up_all(&nfs_write_congestion
);
369 * Insert a write request into an inode
371 static int nfs_inode_add_request(struct inode
*inode
, struct nfs_page
*req
)
373 struct nfs_inode
*nfsi
= NFS_I(inode
);
376 error
= radix_tree_insert(&nfsi
->nfs_page_tree
, req
->wb_index
, req
);
377 BUG_ON(error
== -EEXIST
);
382 nfs_begin_data_update(inode
);
383 if (nfs_have_delegation(inode
, FMODE_WRITE
))
387 atomic_inc(&req
->wb_count
);
392 * Insert a write request into an inode
394 static void nfs_inode_remove_request(struct nfs_page
*req
)
396 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
397 struct nfs_inode
*nfsi
= NFS_I(inode
);
399 BUG_ON (!NFS_WBACK_BUSY(req
));
401 spin_lock(&nfsi
->req_lock
);
402 radix_tree_delete(&nfsi
->nfs_page_tree
, req
->wb_index
);
405 spin_unlock(&nfsi
->req_lock
);
406 nfs_end_data_update(inode
);
409 spin_unlock(&nfsi
->req_lock
);
410 nfs_clear_request(req
);
411 nfs_release_request(req
);
417 static inline struct nfs_page
*
418 _nfs_find_request(struct inode
*inode
, unsigned long index
)
420 struct nfs_inode
*nfsi
= NFS_I(inode
);
421 struct nfs_page
*req
;
423 req
= (struct nfs_page
*)radix_tree_lookup(&nfsi
->nfs_page_tree
, index
);
425 atomic_inc(&req
->wb_count
);
429 static struct nfs_page
*
430 nfs_find_request(struct inode
*inode
, unsigned long index
)
432 struct nfs_page
*req
;
433 struct nfs_inode
*nfsi
= NFS_I(inode
);
435 spin_lock(&nfsi
->req_lock
);
436 req
= _nfs_find_request(inode
, index
);
437 spin_unlock(&nfsi
->req_lock
);
442 * Add a request to the inode's dirty list.
445 nfs_mark_request_dirty(struct nfs_page
*req
)
447 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
448 struct nfs_inode
*nfsi
= NFS_I(inode
);
450 spin_lock(&nfsi
->req_lock
);
451 radix_tree_tag_set(&nfsi
->nfs_page_tree
,
452 req
->wb_index
, NFS_PAGE_TAG_DIRTY
);
453 nfs_list_add_request(req
, &nfsi
->dirty
);
455 spin_unlock(&nfsi
->req_lock
);
456 inc_page_state(nr_dirty
);
457 mark_inode_dirty(inode
);
461 * Check if a request is dirty
464 nfs_dirty_request(struct nfs_page
*req
)
466 struct nfs_inode
*nfsi
= NFS_I(req
->wb_context
->dentry
->d_inode
);
467 return !list_empty(&req
->wb_list
) && req
->wb_list_head
== &nfsi
->dirty
;
470 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
472 * Add a request to the inode's commit list.
475 nfs_mark_request_commit(struct nfs_page
*req
)
477 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
478 struct nfs_inode
*nfsi
= NFS_I(inode
);
480 spin_lock(&nfsi
->req_lock
);
481 nfs_list_add_request(req
, &nfsi
->commit
);
483 spin_unlock(&nfsi
->req_lock
);
484 inc_page_state(nr_unstable
);
485 mark_inode_dirty(inode
);
490 * Wait for a request to complete.
492 * Interruptible by signals only if mounted with intr flag.
495 nfs_wait_on_requests(struct inode
*inode
, unsigned long idx_start
, unsigned int npages
)
497 struct nfs_inode
*nfsi
= NFS_I(inode
);
498 struct nfs_page
*req
;
499 unsigned long idx_end
, next
;
500 unsigned int res
= 0;
506 idx_end
= idx_start
+ npages
- 1;
508 spin_lock(&nfsi
->req_lock
);
510 while (radix_tree_gang_lookup_tag(&nfsi
->nfs_page_tree
, (void **)&req
, next
, 1, NFS_PAGE_TAG_WRITEBACK
)) {
511 if (req
->wb_index
> idx_end
)
514 next
= req
->wb_index
+ 1;
515 BUG_ON(!NFS_WBACK_BUSY(req
));
517 atomic_inc(&req
->wb_count
);
518 spin_unlock(&nfsi
->req_lock
);
519 error
= nfs_wait_on_request(req
);
520 nfs_release_request(req
);
523 spin_lock(&nfsi
->req_lock
);
526 spin_unlock(&nfsi
->req_lock
);
531 * nfs_scan_dirty - Scan an inode for dirty requests
532 * @inode: NFS inode to scan
533 * @dst: destination list
534 * @idx_start: lower bound of page->index to scan.
535 * @npages: idx_start + npages sets the upper bound to scan.
537 * Moves requests from the inode's dirty page list.
538 * The requests are *not* checked to ensure that they form a contiguous set.
541 nfs_scan_dirty(struct inode
*inode
, struct list_head
*dst
, unsigned long idx_start
, unsigned int npages
)
543 struct nfs_inode
*nfsi
= NFS_I(inode
);
546 if (nfsi
->ndirty
!= 0) {
547 res
= nfs_scan_lock_dirty(nfsi
, dst
, idx_start
, npages
);
549 sub_page_state(nr_dirty
,res
);
550 if ((nfsi
->ndirty
== 0) != list_empty(&nfsi
->dirty
))
551 printk(KERN_ERR
"NFS: desynchronized value of nfs_i.ndirty.\n");
556 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
558 * nfs_scan_commit - Scan an inode for commit requests
559 * @inode: NFS inode to scan
560 * @dst: destination list
561 * @idx_start: lower bound of page->index to scan.
562 * @npages: idx_start + npages sets the upper bound to scan.
564 * Moves requests from the inode's 'commit' request list.
565 * The requests are *not* checked to ensure that they form a contiguous set.
568 nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
, unsigned long idx_start
, unsigned int npages
)
570 struct nfs_inode
*nfsi
= NFS_I(inode
);
573 if (nfsi
->ncommit
!= 0) {
574 res
= nfs_scan_list(&nfsi
->commit
, dst
, idx_start
, npages
);
575 nfsi
->ncommit
-= res
;
576 if ((nfsi
->ncommit
== 0) != list_empty(&nfsi
->commit
))
577 printk(KERN_ERR
"NFS: desynchronized value of nfs_i.ncommit.\n");
583 static int nfs_wait_on_write_congestion(struct address_space
*mapping
, int intr
)
585 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
591 if (!bdi_write_congested(bdi
))
594 struct rpc_clnt
*clnt
= NFS_CLIENT(mapping
->host
);
597 rpc_clnt_sigmask(clnt
, &oldset
);
598 prepare_to_wait(&nfs_write_congestion
, &wait
, TASK_INTERRUPTIBLE
);
599 if (bdi_write_congested(bdi
)) {
605 rpc_clnt_sigunmask(clnt
, &oldset
);
607 prepare_to_wait(&nfs_write_congestion
, &wait
, TASK_UNINTERRUPTIBLE
);
608 if (bdi_write_congested(bdi
))
611 finish_wait(&nfs_write_congestion
, &wait
);
617 * Try to update any existing write request, or create one if there is none.
618 * In order to match, the request's credentials must match those of
619 * the calling process.
621 * Note: Should always be called with the Page Lock held!
623 static struct nfs_page
* nfs_update_request(struct nfs_open_context
* ctx
,
624 struct inode
*inode
, struct page
*page
,
625 unsigned int offset
, unsigned int bytes
)
627 struct nfs_server
*server
= NFS_SERVER(inode
);
628 struct nfs_inode
*nfsi
= NFS_I(inode
);
629 struct nfs_page
*req
, *new = NULL
;
630 unsigned long rqend
, end
;
632 end
= offset
+ bytes
;
634 if (nfs_wait_on_write_congestion(page
->mapping
, server
->flags
& NFS_MOUNT_INTR
))
635 return ERR_PTR(-ERESTARTSYS
);
637 /* Loop over all inode entries and see if we find
638 * A request for the page we wish to update
640 spin_lock(&nfsi
->req_lock
);
641 req
= _nfs_find_request(inode
, page
->index
);
643 if (!nfs_lock_request_dontget(req
)) {
645 spin_unlock(&nfsi
->req_lock
);
646 error
= nfs_wait_on_request(req
);
647 nfs_release_request(req
);
649 return ERR_PTR(error
);
652 spin_unlock(&nfsi
->req_lock
);
654 nfs_release_request(new);
660 nfs_lock_request_dontget(new);
661 error
= nfs_inode_add_request(inode
, new);
663 spin_unlock(&nfsi
->req_lock
);
664 nfs_unlock_request(new);
665 return ERR_PTR(error
);
667 spin_unlock(&nfsi
->req_lock
);
668 nfs_mark_request_dirty(new);
671 spin_unlock(&nfsi
->req_lock
);
673 new = nfs_create_request(ctx
, inode
, page
, offset
, bytes
);
678 /* We have a request for our page.
679 * If the creds don't match, or the
680 * page addresses don't match,
681 * tell the caller to wait on the conflicting
684 rqend
= req
->wb_offset
+ req
->wb_bytes
;
685 if (req
->wb_context
!= ctx
686 || req
->wb_page
!= page
687 || !nfs_dirty_request(req
)
688 || offset
> rqend
|| end
< req
->wb_offset
) {
689 nfs_unlock_request(req
);
690 return ERR_PTR(-EBUSY
);
693 /* Okay, the request matches. Update the region */
694 if (offset
< req
->wb_offset
) {
695 req
->wb_offset
= offset
;
696 req
->wb_pgbase
= offset
;
697 req
->wb_bytes
= rqend
- req
->wb_offset
;
701 req
->wb_bytes
= end
- req
->wb_offset
;
706 int nfs_flush_incompatible(struct file
*file
, struct page
*page
)
708 struct nfs_open_context
*ctx
= (struct nfs_open_context
*)file
->private_data
;
709 struct inode
*inode
= page
->mapping
->host
;
710 struct nfs_page
*req
;
713 * Look for a request corresponding to this page. If there
714 * is one, and it belongs to another file, we flush it out
715 * before we try to copy anything into the page. Do this
716 * due to the lack of an ACCESS-type call in NFSv2.
717 * Also do the same if we find a request from an existing
720 req
= nfs_find_request(inode
, page
->index
);
722 if (req
->wb_page
!= page
|| ctx
!= req
->wb_context
)
723 status
= nfs_wb_page(inode
, page
);
724 nfs_release_request(req
);
726 return (status
< 0) ? status
: 0;
730 * Update and possibly write a cached page of an NFS file.
732 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
733 * things with a page scheduled for an RPC call (e.g. invalidate it).
735 int nfs_updatepage(struct file
*file
, struct page
*page
,
736 unsigned int offset
, unsigned int count
)
738 struct nfs_open_context
*ctx
= (struct nfs_open_context
*)file
->private_data
;
739 struct inode
*inode
= page
->mapping
->host
;
740 struct nfs_page
*req
;
743 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
744 file
->f_dentry
->d_parent
->d_name
.name
,
745 file
->f_dentry
->d_name
.name
, count
,
746 (long long)(page_offset(page
) +offset
));
748 if (IS_SYNC(inode
)) {
749 status
= nfs_writepage_sync(ctx
, inode
, page
, offset
, count
, 0);
751 if (offset
== 0 && status
== PAGE_CACHE_SIZE
)
752 SetPageUptodate(page
);
758 /* If we're not using byte range locks, and we know the page
759 * is entirely in cache, it may be more efficient to avoid
760 * fragmenting write requests.
762 if (PageUptodate(page
) && inode
->i_flock
== NULL
&& !(file
->f_mode
& O_SYNC
)) {
763 loff_t end_offs
= i_size_read(inode
) - 1;
764 unsigned long end_index
= end_offs
>> PAGE_CACHE_SHIFT
;
768 if (unlikely(end_offs
< 0)) {
770 } else if (page
->index
== end_index
) {
772 pglen
= (unsigned int)(end_offs
& (PAGE_CACHE_SIZE
-1)) + 1;
775 } else if (page
->index
< end_index
)
776 count
= PAGE_CACHE_SIZE
;
780 * Try to find an NFS request corresponding to this page
782 * If the existing request cannot be updated, we must flush
786 req
= nfs_update_request(ctx
, inode
, page
, offset
, count
);
787 status
= (IS_ERR(req
)) ? PTR_ERR(req
) : 0;
788 if (status
!= -EBUSY
)
790 /* Request could not be updated. Flush it out and try again */
791 status
= nfs_wb_page(inode
, page
);
792 } while (status
>= 0);
798 /* Update file length */
799 nfs_grow_file(page
, offset
, count
);
800 /* Set the PG_uptodate flag? */
801 nfs_mark_uptodate(page
, req
->wb_pgbase
, req
->wb_bytes
);
802 nfs_unlock_request(req
);
804 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
805 status
, (long long)i_size_read(inode
));
807 ClearPageUptodate(page
);
811 static void nfs_writepage_release(struct nfs_page
*req
)
813 end_page_writeback(req
->wb_page
);
815 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
816 if (!PageError(req
->wb_page
)) {
817 if (NFS_NEED_RESCHED(req
)) {
818 nfs_mark_request_dirty(req
);
820 } else if (NFS_NEED_COMMIT(req
)) {
821 nfs_mark_request_commit(req
);
825 nfs_inode_remove_request(req
);
828 nfs_clear_commit(req
);
829 nfs_clear_reschedule(req
);
831 nfs_inode_remove_request(req
);
833 nfs_clear_page_writeback(req
);
836 static inline int flush_task_priority(int how
)
838 switch (how
& (FLUSH_HIGHPRI
|FLUSH_LOWPRI
)) {
840 return RPC_PRIORITY_HIGH
;
842 return RPC_PRIORITY_LOW
;
844 return RPC_PRIORITY_NORMAL
;
848 * Set up the argument/result storage required for the RPC call.
850 static void nfs_write_rpcsetup(struct nfs_page
*req
,
851 struct nfs_write_data
*data
,
852 unsigned int count
, unsigned int offset
,
857 /* Set up the RPC argument and reply structs
858 * NB: take care not to mess about with data->commit et al. */
861 data
->inode
= inode
= req
->wb_context
->dentry
->d_inode
;
862 data
->cred
= req
->wb_context
->cred
;
864 data
->args
.fh
= NFS_FH(inode
);
865 data
->args
.offset
= req_offset(req
) + offset
;
866 data
->args
.pgbase
= req
->wb_pgbase
+ offset
;
867 data
->args
.pages
= data
->pagevec
;
868 data
->args
.count
= count
;
869 data
->args
.context
= req
->wb_context
;
871 data
->res
.fattr
= &data
->fattr
;
872 data
->res
.count
= count
;
873 data
->res
.verf
= &data
->verf
;
874 nfs_fattr_init(&data
->fattr
);
876 NFS_PROTO(inode
)->write_setup(data
, how
);
878 data
->task
.tk_priority
= flush_task_priority(how
);
879 data
->task
.tk_cookie
= (unsigned long)inode
;
880 data
->task
.tk_calldata
= data
;
881 /* Release requests */
882 data
->task
.tk_release
= nfs_writedata_release
;
884 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
887 (long long)NFS_FILEID(inode
),
889 (unsigned long long)data
->args
.offset
);
892 static void nfs_execute_write(struct nfs_write_data
*data
)
894 struct rpc_clnt
*clnt
= NFS_CLIENT(data
->inode
);
897 rpc_clnt_sigmask(clnt
, &oldset
);
899 rpc_execute(&data
->task
);
901 rpc_clnt_sigunmask(clnt
, &oldset
);
905 * Generate multiple small requests to write out a single
906 * contiguous dirty area on one page.
908 static int nfs_flush_multi(struct list_head
*head
, struct inode
*inode
, int how
)
910 struct nfs_page
*req
= nfs_list_entry(head
->next
);
911 struct page
*page
= req
->wb_page
;
912 struct nfs_write_data
*data
;
913 unsigned int wsize
= NFS_SERVER(inode
)->wsize
;
914 unsigned int nbytes
, offset
;
918 nfs_list_remove_request(req
);
920 nbytes
= req
->wb_bytes
;
922 data
= nfs_writedata_alloc();
925 list_add(&data
->pages
, &list
);
931 atomic_set(&req
->wb_complete
, requests
);
933 ClearPageError(page
);
934 set_page_writeback(page
);
936 nbytes
= req
->wb_bytes
;
938 data
= list_entry(list
.next
, struct nfs_write_data
, pages
);
939 list_del_init(&data
->pages
);
941 data
->pagevec
[0] = page
;
942 data
->complete
= nfs_writeback_done_partial
;
944 if (nbytes
> wsize
) {
945 nfs_write_rpcsetup(req
, data
, wsize
, offset
, how
);
949 nfs_write_rpcsetup(req
, data
, nbytes
, offset
, how
);
952 nfs_execute_write(data
);
953 } while (nbytes
!= 0);
958 while (!list_empty(&list
)) {
959 data
= list_entry(list
.next
, struct nfs_write_data
, pages
);
960 list_del(&data
->pages
);
961 nfs_writedata_free(data
);
963 nfs_mark_request_dirty(req
);
964 nfs_clear_page_writeback(req
);
969 * Create an RPC task for the given write request and kick it.
970 * The page must have been locked by the caller.
972 * It may happen that the page we're passed is not marked dirty.
973 * This is the case if nfs_updatepage detects a conflicting request
974 * that has been written but not committed.
976 static int nfs_flush_one(struct list_head
*head
, struct inode
*inode
, int how
)
978 struct nfs_page
*req
;
980 struct nfs_write_data
*data
;
983 if (NFS_SERVER(inode
)->wsize
< PAGE_CACHE_SIZE
)
984 return nfs_flush_multi(head
, inode
, how
);
986 data
= nfs_writedata_alloc();
990 pages
= data
->pagevec
;
992 while (!list_empty(head
)) {
993 req
= nfs_list_entry(head
->next
);
994 nfs_list_remove_request(req
);
995 nfs_list_add_request(req
, &data
->pages
);
996 ClearPageError(req
->wb_page
);
997 set_page_writeback(req
->wb_page
);
998 *pages
++ = req
->wb_page
;
999 count
+= req
->wb_bytes
;
1001 req
= nfs_list_entry(data
->pages
.next
);
1003 data
->complete
= nfs_writeback_done_full
;
1004 /* Set up the argument struct */
1005 nfs_write_rpcsetup(req
, data
, count
, 0, how
);
1007 nfs_execute_write(data
);
1010 while (!list_empty(head
)) {
1011 struct nfs_page
*req
= nfs_list_entry(head
->next
);
1012 nfs_list_remove_request(req
);
1013 nfs_mark_request_dirty(req
);
1014 nfs_clear_page_writeback(req
);
1020 nfs_flush_list(struct list_head
*head
, int wpages
, int how
)
1022 LIST_HEAD(one_request
);
1023 struct nfs_page
*req
;
1025 unsigned int pages
= 0;
1027 while (!list_empty(head
)) {
1028 pages
+= nfs_coalesce_requests(head
, &one_request
, wpages
);
1029 req
= nfs_list_entry(one_request
.next
);
1030 error
= nfs_flush_one(&one_request
, req
->wb_context
->dentry
->d_inode
, how
);
1037 while (!list_empty(head
)) {
1038 req
= nfs_list_entry(head
->next
);
1039 nfs_list_remove_request(req
);
1040 nfs_mark_request_dirty(req
);
1041 nfs_clear_page_writeback(req
);
1047 * Handle a write reply that flushed part of a page.
1049 static void nfs_writeback_done_partial(struct nfs_write_data
*data
, int status
)
1051 struct nfs_page
*req
= data
->req
;
1052 struct page
*page
= req
->wb_page
;
1054 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1055 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
1056 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
1058 (long long)req_offset(req
));
1061 ClearPageUptodate(page
);
1063 req
->wb_context
->error
= status
;
1064 dprintk(", error = %d\n", status
);
1066 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1067 if (data
->verf
.committed
< NFS_FILE_SYNC
) {
1068 if (!NFS_NEED_COMMIT(req
)) {
1069 nfs_defer_commit(req
);
1070 memcpy(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
));
1071 dprintk(" defer commit\n");
1072 } else if (memcmp(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
))) {
1073 nfs_defer_reschedule(req
);
1074 dprintk(" server reboot detected\n");
1081 if (atomic_dec_and_test(&req
->wb_complete
))
1082 nfs_writepage_release(req
);
1086 * Handle a write reply that flushes a whole page.
1088 * FIXME: There is an inherent race with invalidate_inode_pages and
1089 * writebacks since the page->count is kept > 1 for as long
1090 * as the page has a write request pending.
1092 static void nfs_writeback_done_full(struct nfs_write_data
*data
, int status
)
1094 struct nfs_page
*req
;
1097 /* Update attributes as result of writeback. */
1098 while (!list_empty(&data
->pages
)) {
1099 req
= nfs_list_entry(data
->pages
.next
);
1100 nfs_list_remove_request(req
);
1101 page
= req
->wb_page
;
1103 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1104 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
1105 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
1107 (long long)req_offset(req
));
1110 ClearPageUptodate(page
);
1112 req
->wb_context
->error
= status
;
1113 end_page_writeback(page
);
1114 nfs_inode_remove_request(req
);
1115 dprintk(", error = %d\n", status
);
1118 end_page_writeback(page
);
1120 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1121 if (data
->args
.stable
!= NFS_UNSTABLE
|| data
->verf
.committed
== NFS_FILE_SYNC
) {
1122 nfs_inode_remove_request(req
);
1126 memcpy(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
));
1127 nfs_mark_request_commit(req
);
1128 dprintk(" marked for commit\n");
1130 nfs_inode_remove_request(req
);
1133 nfs_clear_page_writeback(req
);
1138 * This function is called when the WRITE call is complete.
1140 void nfs_writeback_done(struct rpc_task
*task
)
1142 struct nfs_write_data
*data
= (struct nfs_write_data
*) task
->tk_calldata
;
1143 struct nfs_writeargs
*argp
= &data
->args
;
1144 struct nfs_writeres
*resp
= &data
->res
;
1146 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1147 task
->tk_pid
, task
->tk_status
);
1149 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1150 if (resp
->verf
->committed
< argp
->stable
&& task
->tk_status
>= 0) {
1151 /* We tried a write call, but the server did not
1152 * commit data to stable storage even though we
1154 * Note: There is a known bug in Tru64 < 5.0 in which
1155 * the server reports NFS_DATA_SYNC, but performs
1156 * NFS_FILE_SYNC. We therefore implement this checking
1157 * as a dprintk() in order to avoid filling syslog.
1159 static unsigned long complain
;
1161 if (time_before(complain
, jiffies
)) {
1162 dprintk("NFS: faulty NFS server %s:"
1163 " (committed = %d) != (stable = %d)\n",
1164 NFS_SERVER(data
->inode
)->hostname
,
1165 resp
->verf
->committed
, argp
->stable
);
1166 complain
= jiffies
+ 300 * HZ
;
1170 /* Is this a short write? */
1171 if (task
->tk_status
>= 0 && resp
->count
< argp
->count
) {
1172 static unsigned long complain
;
1174 /* Has the server at least made some progress? */
1175 if (resp
->count
!= 0) {
1176 /* Was this an NFSv2 write or an NFSv3 stable write? */
1177 if (resp
->verf
->committed
!= NFS_UNSTABLE
) {
1178 /* Resend from where the server left off */
1179 argp
->offset
+= resp
->count
;
1180 argp
->pgbase
+= resp
->count
;
1181 argp
->count
-= resp
->count
;
1183 /* Resend as a stable write in order to avoid
1184 * headaches in the case of a server crash.
1186 argp
->stable
= NFS_FILE_SYNC
;
1188 rpc_restart_call(task
);
1191 if (time_before(complain
, jiffies
)) {
1193 "NFS: Server wrote zero bytes, expected %u.\n",
1195 complain
= jiffies
+ 300 * HZ
;
1197 /* Can't do anything about it except throw an error. */
1198 task
->tk_status
= -EIO
;
1202 * Process the nfs_page list
1204 data
->complete(data
, task
->tk_status
);
1208 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1209 static void nfs_commit_release(struct rpc_task
*task
)
1211 struct nfs_write_data
*wdata
= (struct nfs_write_data
*)task
->tk_calldata
;
1212 nfs_commit_free(wdata
);
1216 * Set up the argument/result storage required for the RPC call.
1218 static void nfs_commit_rpcsetup(struct list_head
*head
,
1219 struct nfs_write_data
*data
, int how
)
1221 struct nfs_page
*first
;
1222 struct inode
*inode
;
1224 /* Set up the RPC argument and reply structs
1225 * NB: take care not to mess about with data->commit et al. */
1227 list_splice_init(head
, &data
->pages
);
1228 first
= nfs_list_entry(data
->pages
.next
);
1229 inode
= first
->wb_context
->dentry
->d_inode
;
1231 data
->inode
= inode
;
1232 data
->cred
= first
->wb_context
->cred
;
1234 data
->args
.fh
= NFS_FH(data
->inode
);
1235 /* Note: we always request a commit of the entire inode */
1236 data
->args
.offset
= 0;
1237 data
->args
.count
= 0;
1238 data
->res
.count
= 0;
1239 data
->res
.fattr
= &data
->fattr
;
1240 data
->res
.verf
= &data
->verf
;
1241 nfs_fattr_init(&data
->fattr
);
1243 NFS_PROTO(inode
)->commit_setup(data
, how
);
1245 data
->task
.tk_priority
= flush_task_priority(how
);
1246 data
->task
.tk_cookie
= (unsigned long)inode
;
1247 data
->task
.tk_calldata
= data
;
1248 /* Release requests */
1249 data
->task
.tk_release
= nfs_commit_release
;
1251 dprintk("NFS: %4d initiated commit call\n", data
->task
.tk_pid
);
1255 * Commit dirty pages
1258 nfs_commit_list(struct list_head
*head
, int how
)
1260 struct nfs_write_data
*data
;
1261 struct nfs_page
*req
;
1263 data
= nfs_commit_alloc();
1268 /* Set up the argument struct */
1269 nfs_commit_rpcsetup(head
, data
, how
);
1271 nfs_execute_write(data
);
1274 while (!list_empty(head
)) {
1275 req
= nfs_list_entry(head
->next
);
1276 nfs_list_remove_request(req
);
1277 nfs_mark_request_commit(req
);
1278 nfs_clear_page_writeback(req
);
1284 * COMMIT call returned
1287 nfs_commit_done(struct rpc_task
*task
)
1289 struct nfs_write_data
*data
= (struct nfs_write_data
*)task
->tk_calldata
;
1290 struct nfs_page
*req
;
1293 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1294 task
->tk_pid
, task
->tk_status
);
1296 while (!list_empty(&data
->pages
)) {
1297 req
= nfs_list_entry(data
->pages
.next
);
1298 nfs_list_remove_request(req
);
1300 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1301 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
1302 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
1304 (long long)req_offset(req
));
1305 if (task
->tk_status
< 0) {
1306 req
->wb_context
->error
= task
->tk_status
;
1307 nfs_inode_remove_request(req
);
1308 dprintk(", error = %d\n", task
->tk_status
);
1312 /* Okay, COMMIT succeeded, apparently. Check the verifier
1313 * returned by the server against all stored verfs. */
1314 if (!memcmp(req
->wb_verf
.verifier
, data
->verf
.verifier
, sizeof(data
->verf
.verifier
))) {
1315 /* We have a match */
1316 nfs_inode_remove_request(req
);
1320 /* We have a mismatch. Write the page again */
1321 dprintk(" mismatch\n");
1322 nfs_mark_request_dirty(req
);
1324 nfs_clear_page_writeback(req
);
1327 sub_page_state(nr_unstable
,res
);
1331 static int nfs_flush_inode(struct inode
*inode
, unsigned long idx_start
,
1332 unsigned int npages
, int how
)
1334 struct nfs_inode
*nfsi
= NFS_I(inode
);
1339 spin_lock(&nfsi
->req_lock
);
1340 res
= nfs_scan_dirty(inode
, &head
, idx_start
, npages
);
1341 spin_unlock(&nfsi
->req_lock
);
1343 struct nfs_server
*server
= NFS_SERVER(inode
);
1345 /* For single writes, FLUSH_STABLE is more efficient */
1346 if (res
== nfsi
->npages
&& nfsi
->npages
<= server
->wpages
) {
1347 if (res
> 1 || nfs_list_entry(head
.next
)->wb_bytes
<= server
->wsize
)
1348 how
|= FLUSH_STABLE
;
1350 error
= nfs_flush_list(&head
, server
->wpages
, how
);
1357 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1358 int nfs_commit_inode(struct inode
*inode
, int how
)
1360 struct nfs_inode
*nfsi
= NFS_I(inode
);
1365 spin_lock(&nfsi
->req_lock
);
1366 res
= nfs_scan_commit(inode
, &head
, 0, 0);
1367 spin_unlock(&nfsi
->req_lock
);
1369 error
= nfs_commit_list(&head
, how
);
1377 int nfs_sync_inode(struct inode
*inode
, unsigned long idx_start
,
1378 unsigned int npages
, int how
)
1383 wait
= how
& FLUSH_WAIT
;
1389 error
= nfs_wait_on_requests(inode
, idx_start
, npages
);
1391 error
= nfs_flush_inode(inode
, idx_start
, npages
, how
);
1392 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1394 error
= nfs_commit_inode(inode
, how
);
1396 } while (error
> 0);
1400 int nfs_init_writepagecache(void)
1402 nfs_wdata_cachep
= kmem_cache_create("nfs_write_data",
1403 sizeof(struct nfs_write_data
),
1404 0, SLAB_HWCACHE_ALIGN
,
1406 if (nfs_wdata_cachep
== NULL
)
1409 nfs_wdata_mempool
= mempool_create(MIN_POOL_WRITE
,
1413 if (nfs_wdata_mempool
== NULL
)
1416 nfs_commit_mempool
= mempool_create(MIN_POOL_COMMIT
,
1420 if (nfs_commit_mempool
== NULL
)
1426 void nfs_destroy_writepagecache(void)
1428 mempool_destroy(nfs_commit_mempool
);
1429 mempool_destroy(nfs_wdata_mempool
);
1430 if (kmem_cache_destroy(nfs_wdata_cachep
))
1431 printk(KERN_INFO
"nfs_write_data: not all structures were freed\n");