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 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_defer(inode
);
224 nfs_writedata_free(wdata
);
225 return written
? written
: result
;
228 static int nfs_writepage_async(struct nfs_open_context
*ctx
,
229 struct inode
*inode
, struct page
*page
,
230 unsigned int offset
, unsigned int count
)
232 struct nfs_page
*req
;
235 req
= nfs_update_request(ctx
, inode
, page
, offset
, count
);
236 status
= (IS_ERR(req
)) ? PTR_ERR(req
) : 0;
239 /* Update file length */
240 nfs_grow_file(page
, offset
, count
);
241 /* Set the PG_uptodate flag? */
242 nfs_mark_uptodate(page
, offset
, count
);
243 nfs_unlock_request(req
);
248 static int wb_priority(struct writeback_control
*wbc
)
250 if (wbc
->for_reclaim
)
251 return FLUSH_HIGHPRI
;
252 if (wbc
->for_kupdate
)
258 * Write an mmapped page to the server.
260 int nfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
262 struct nfs_open_context
*ctx
;
263 struct inode
*inode
= page
->mapping
->host
;
264 unsigned long end_index
;
265 unsigned offset
= PAGE_CACHE_SIZE
;
266 loff_t i_size
= i_size_read(inode
);
267 int inode_referenced
= 0;
268 int priority
= wb_priority(wbc
);
272 * Note: We need to ensure that we have a reference to the inode
273 * if we are to do asynchronous writes. If not, waiting
274 * in nfs_wait_on_request() may deadlock with clear_inode().
276 * If igrab() fails here, then it is in any case safe to
277 * call nfs_wb_page(), since there will be no pending writes.
279 if (igrab(inode
) != 0)
280 inode_referenced
= 1;
281 end_index
= i_size
>> PAGE_CACHE_SHIFT
;
283 /* Ensure we've flushed out any previous writes */
284 nfs_wb_page_priority(inode
, page
, priority
);
287 if (page
->index
< end_index
)
289 /* things got complicated... */
290 offset
= i_size
& (PAGE_CACHE_SIZE
-1);
292 /* OK, are we completely out? */
293 err
= 0; /* potential race with truncate - ignore */
294 if (page
->index
>= end_index
+1 || !offset
)
297 ctx
= nfs_find_open_context(inode
, FMODE_WRITE
);
303 if (!IS_SYNC(inode
) && inode_referenced
) {
304 err
= nfs_writepage_async(ctx
, inode
, page
, 0, offset
);
307 if (wbc
->for_reclaim
)
308 nfs_flush_inode(inode
, 0, 0, FLUSH_STABLE
);
311 err
= nfs_writepage_sync(ctx
, inode
, page
, 0,
315 redirty_page_for_writepage(wbc
, page
);
320 put_nfs_open_context(ctx
);
323 if (inode_referenced
)
329 * Note: causes nfs_update_request() to block on the assumption
330 * that the writeback is generated due to memory pressure.
332 int nfs_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
334 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
335 struct inode
*inode
= mapping
->host
;
338 err
= generic_writepages(mapping
, wbc
);
341 while (test_and_set_bit(BDI_write_congested
, &bdi
->state
) != 0) {
342 if (wbc
->nonblocking
)
344 nfs_wait_on_write_congestion(mapping
, 0);
346 err
= nfs_flush_inode(inode
, 0, 0, wb_priority(wbc
));
349 wbc
->nr_to_write
-= err
;
350 if (!wbc
->nonblocking
&& wbc
->sync_mode
== WB_SYNC_ALL
) {
351 err
= nfs_wait_on_requests(inode
, 0, 0);
355 err
= nfs_commit_inode(inode
, 0, 0, wb_priority(wbc
));
357 wbc
->nr_to_write
-= err
;
361 clear_bit(BDI_write_congested
, &bdi
->state
);
362 wake_up_all(&nfs_write_congestion
);
367 * Insert a write request into an inode
369 static int nfs_inode_add_request(struct inode
*inode
, struct nfs_page
*req
)
371 struct nfs_inode
*nfsi
= NFS_I(inode
);
374 error
= radix_tree_insert(&nfsi
->nfs_page_tree
, req
->wb_index
, req
);
375 BUG_ON(error
== -EEXIST
);
380 nfs_begin_data_update(inode
);
381 if (nfs_have_delegation(inode
, FMODE_WRITE
))
385 atomic_inc(&req
->wb_count
);
390 * Insert a write request into an inode
392 static void nfs_inode_remove_request(struct nfs_page
*req
)
394 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
395 struct nfs_inode
*nfsi
= NFS_I(inode
);
397 BUG_ON (!NFS_WBACK_BUSY(req
));
399 spin_lock(&nfsi
->req_lock
);
400 radix_tree_delete(&nfsi
->nfs_page_tree
, req
->wb_index
);
403 spin_unlock(&nfsi
->req_lock
);
404 nfs_end_data_update_defer(inode
);
407 spin_unlock(&nfsi
->req_lock
);
408 nfs_clear_request(req
);
409 nfs_release_request(req
);
415 static inline struct nfs_page
*
416 _nfs_find_request(struct inode
*inode
, unsigned long index
)
418 struct nfs_inode
*nfsi
= NFS_I(inode
);
419 struct nfs_page
*req
;
421 req
= (struct nfs_page
*)radix_tree_lookup(&nfsi
->nfs_page_tree
, index
);
423 atomic_inc(&req
->wb_count
);
427 static struct nfs_page
*
428 nfs_find_request(struct inode
*inode
, unsigned long index
)
430 struct nfs_page
*req
;
431 struct nfs_inode
*nfsi
= NFS_I(inode
);
433 spin_lock(&nfsi
->req_lock
);
434 req
= _nfs_find_request(inode
, index
);
435 spin_unlock(&nfsi
->req_lock
);
440 * Add a request to the inode's dirty list.
443 nfs_mark_request_dirty(struct nfs_page
*req
)
445 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
446 struct nfs_inode
*nfsi
= NFS_I(inode
);
448 spin_lock(&nfsi
->req_lock
);
449 nfs_list_add_request(req
, &nfsi
->dirty
);
451 spin_unlock(&nfsi
->req_lock
);
452 inc_page_state(nr_dirty
);
453 mark_inode_dirty(inode
);
457 * Check if a request is dirty
460 nfs_dirty_request(struct nfs_page
*req
)
462 struct nfs_inode
*nfsi
= NFS_I(req
->wb_context
->dentry
->d_inode
);
463 return !list_empty(&req
->wb_list
) && req
->wb_list_head
== &nfsi
->dirty
;
466 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
468 * Add a request to the inode's commit list.
471 nfs_mark_request_commit(struct nfs_page
*req
)
473 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
474 struct nfs_inode
*nfsi
= NFS_I(inode
);
476 spin_lock(&nfsi
->req_lock
);
477 nfs_list_add_request(req
, &nfsi
->commit
);
479 spin_unlock(&nfsi
->req_lock
);
480 inc_page_state(nr_unstable
);
481 mark_inode_dirty(inode
);
486 * Wait for a request to complete.
488 * Interruptible by signals only if mounted with intr flag.
491 nfs_wait_on_requests(struct inode
*inode
, unsigned long idx_start
, unsigned int npages
)
493 struct nfs_inode
*nfsi
= NFS_I(inode
);
494 struct nfs_page
*req
;
495 unsigned long idx_end
, next
;
496 unsigned int res
= 0;
502 idx_end
= idx_start
+ npages
- 1;
504 spin_lock(&nfsi
->req_lock
);
506 while (radix_tree_gang_lookup(&nfsi
->nfs_page_tree
, (void **)&req
, next
, 1)) {
507 if (req
->wb_index
> idx_end
)
510 next
= req
->wb_index
+ 1;
511 if (!NFS_WBACK_BUSY(req
))
514 atomic_inc(&req
->wb_count
);
515 spin_unlock(&nfsi
->req_lock
);
516 error
= nfs_wait_on_request(req
);
517 nfs_release_request(req
);
520 spin_lock(&nfsi
->req_lock
);
523 spin_unlock(&nfsi
->req_lock
);
528 * nfs_scan_dirty - Scan an inode for dirty requests
529 * @inode: NFS inode to scan
530 * @dst: destination list
531 * @idx_start: lower bound of page->index to scan.
532 * @npages: idx_start + npages sets the upper bound to scan.
534 * Moves requests from the inode's dirty page list.
535 * The requests are *not* checked to ensure that they form a contiguous set.
538 nfs_scan_dirty(struct inode
*inode
, struct list_head
*dst
, unsigned long idx_start
, unsigned int npages
)
540 struct nfs_inode
*nfsi
= NFS_I(inode
);
542 res
= nfs_scan_list(&nfsi
->dirty
, dst
, idx_start
, npages
);
544 sub_page_state(nr_dirty
,res
);
545 if ((nfsi
->ndirty
== 0) != list_empty(&nfsi
->dirty
))
546 printk(KERN_ERR
"NFS: desynchronized value of nfs_i.ndirty.\n");
550 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
552 * nfs_scan_commit - Scan an inode for commit requests
553 * @inode: NFS inode to scan
554 * @dst: destination list
555 * @idx_start: lower bound of page->index to scan.
556 * @npages: idx_start + npages sets the upper bound to scan.
558 * Moves requests from the inode's 'commit' request list.
559 * The requests are *not* checked to ensure that they form a contiguous set.
562 nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
, unsigned long idx_start
, unsigned int npages
)
564 struct nfs_inode
*nfsi
= NFS_I(inode
);
566 res
= nfs_scan_list(&nfsi
->commit
, dst
, idx_start
, npages
);
567 nfsi
->ncommit
-= res
;
568 if ((nfsi
->ncommit
== 0) != list_empty(&nfsi
->commit
))
569 printk(KERN_ERR
"NFS: desynchronized value of nfs_i.ncommit.\n");
574 static int nfs_wait_on_write_congestion(struct address_space
*mapping
, int intr
)
576 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
582 if (!bdi_write_congested(bdi
))
585 struct rpc_clnt
*clnt
= NFS_CLIENT(mapping
->host
);
588 rpc_clnt_sigmask(clnt
, &oldset
);
589 prepare_to_wait(&nfs_write_congestion
, &wait
, TASK_INTERRUPTIBLE
);
590 if (bdi_write_congested(bdi
)) {
596 rpc_clnt_sigunmask(clnt
, &oldset
);
598 prepare_to_wait(&nfs_write_congestion
, &wait
, TASK_UNINTERRUPTIBLE
);
599 if (bdi_write_congested(bdi
))
602 finish_wait(&nfs_write_congestion
, &wait
);
608 * Try to update any existing write request, or create one if there is none.
609 * In order to match, the request's credentials must match those of
610 * the calling process.
612 * Note: Should always be called with the Page Lock held!
614 static struct nfs_page
* nfs_update_request(struct nfs_open_context
* ctx
,
615 struct inode
*inode
, struct page
*page
,
616 unsigned int offset
, unsigned int bytes
)
618 struct nfs_server
*server
= NFS_SERVER(inode
);
619 struct nfs_inode
*nfsi
= NFS_I(inode
);
620 struct nfs_page
*req
, *new = NULL
;
621 unsigned long rqend
, end
;
623 end
= offset
+ bytes
;
625 if (nfs_wait_on_write_congestion(page
->mapping
, server
->flags
& NFS_MOUNT_INTR
))
626 return ERR_PTR(-ERESTARTSYS
);
628 /* Loop over all inode entries and see if we find
629 * A request for the page we wish to update
631 spin_lock(&nfsi
->req_lock
);
632 req
= _nfs_find_request(inode
, page
->index
);
634 if (!nfs_lock_request_dontget(req
)) {
636 spin_unlock(&nfsi
->req_lock
);
637 error
= nfs_wait_on_request(req
);
638 nfs_release_request(req
);
640 return ERR_PTR(error
);
643 spin_unlock(&nfsi
->req_lock
);
645 nfs_release_request(new);
651 nfs_lock_request_dontget(new);
652 error
= nfs_inode_add_request(inode
, new);
654 spin_unlock(&nfsi
->req_lock
);
655 nfs_unlock_request(new);
656 return ERR_PTR(error
);
658 spin_unlock(&nfsi
->req_lock
);
659 nfs_mark_request_dirty(new);
662 spin_unlock(&nfsi
->req_lock
);
664 new = nfs_create_request(ctx
, inode
, page
, offset
, bytes
);
669 /* We have a request for our page.
670 * If the creds don't match, or the
671 * page addresses don't match,
672 * tell the caller to wait on the conflicting
675 rqend
= req
->wb_offset
+ req
->wb_bytes
;
676 if (req
->wb_context
!= ctx
677 || req
->wb_page
!= page
678 || !nfs_dirty_request(req
)
679 || offset
> rqend
|| end
< req
->wb_offset
) {
680 nfs_unlock_request(req
);
681 return ERR_PTR(-EBUSY
);
684 /* Okay, the request matches. Update the region */
685 if (offset
< req
->wb_offset
) {
686 req
->wb_offset
= offset
;
687 req
->wb_pgbase
= offset
;
688 req
->wb_bytes
= rqend
- req
->wb_offset
;
692 req
->wb_bytes
= end
- req
->wb_offset
;
697 int nfs_flush_incompatible(struct file
*file
, struct page
*page
)
699 struct nfs_open_context
*ctx
= (struct nfs_open_context
*)file
->private_data
;
700 struct inode
*inode
= page
->mapping
->host
;
701 struct nfs_page
*req
;
704 * Look for a request corresponding to this page. If there
705 * is one, and it belongs to another file, we flush it out
706 * before we try to copy anything into the page. Do this
707 * due to the lack of an ACCESS-type call in NFSv2.
708 * Also do the same if we find a request from an existing
711 req
= nfs_find_request(inode
, page
->index
);
713 if (req
->wb_page
!= page
|| ctx
!= req
->wb_context
)
714 status
= nfs_wb_page(inode
, page
);
715 nfs_release_request(req
);
717 return (status
< 0) ? status
: 0;
721 * Update and possibly write a cached page of an NFS file.
723 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
724 * things with a page scheduled for an RPC call (e.g. invalidate it).
726 int nfs_updatepage(struct file
*file
, struct page
*page
,
727 unsigned int offset
, unsigned int count
)
729 struct nfs_open_context
*ctx
= (struct nfs_open_context
*)file
->private_data
;
730 struct dentry
*dentry
= file
->f_dentry
;
731 struct inode
*inode
= page
->mapping
->host
;
732 struct nfs_page
*req
;
735 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
736 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
737 count
, (long long)(page_offset(page
) +offset
));
739 if (IS_SYNC(inode
)) {
740 status
= nfs_writepage_sync(ctx
, inode
, page
, offset
, count
, 0);
742 if (offset
== 0 && status
== PAGE_CACHE_SIZE
)
743 SetPageUptodate(page
);
749 /* If we're not using byte range locks, and we know the page
750 * is entirely in cache, it may be more efficient to avoid
751 * fragmenting write requests.
753 if (PageUptodate(page
) && inode
->i_flock
== NULL
) {
754 loff_t end_offs
= i_size_read(inode
) - 1;
755 unsigned long end_index
= end_offs
>> PAGE_CACHE_SHIFT
;
759 if (unlikely(end_offs
< 0)) {
761 } else if (page
->index
== end_index
) {
763 pglen
= (unsigned int)(end_offs
& (PAGE_CACHE_SIZE
-1)) + 1;
766 } else if (page
->index
< end_index
)
767 count
= PAGE_CACHE_SIZE
;
771 * Try to find an NFS request corresponding to this page
773 * If the existing request cannot be updated, we must flush
777 req
= nfs_update_request(ctx
, inode
, page
, offset
, count
);
778 status
= (IS_ERR(req
)) ? PTR_ERR(req
) : 0;
779 if (status
!= -EBUSY
)
781 /* Request could not be updated. Flush it out and try again */
782 status
= nfs_wb_page(inode
, page
);
783 } while (status
>= 0);
789 /* Update file length */
790 nfs_grow_file(page
, offset
, count
);
791 /* Set the PG_uptodate flag? */
792 nfs_mark_uptodate(page
, req
->wb_pgbase
, req
->wb_bytes
);
793 nfs_unlock_request(req
);
795 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
796 status
, (long long)i_size_read(inode
));
798 ClearPageUptodate(page
);
802 static void nfs_writepage_release(struct nfs_page
*req
)
804 end_page_writeback(req
->wb_page
);
806 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
807 if (!PageError(req
->wb_page
)) {
808 if (NFS_NEED_RESCHED(req
)) {
809 nfs_mark_request_dirty(req
);
811 } else if (NFS_NEED_COMMIT(req
)) {
812 nfs_mark_request_commit(req
);
816 nfs_inode_remove_request(req
);
819 nfs_clear_commit(req
);
820 nfs_clear_reschedule(req
);
822 nfs_inode_remove_request(req
);
824 nfs_unlock_request(req
);
827 static inline int flush_task_priority(int how
)
829 switch (how
& (FLUSH_HIGHPRI
|FLUSH_LOWPRI
)) {
831 return RPC_PRIORITY_HIGH
;
833 return RPC_PRIORITY_LOW
;
835 return RPC_PRIORITY_NORMAL
;
839 * Set up the argument/result storage required for the RPC call.
841 static void nfs_write_rpcsetup(struct nfs_page
*req
,
842 struct nfs_write_data
*data
,
843 unsigned int count
, unsigned int offset
,
846 struct rpc_task
*task
= &data
->task
;
849 /* Set up the RPC argument and reply structs
850 * NB: take care not to mess about with data->commit et al. */
853 data
->inode
= inode
= req
->wb_context
->dentry
->d_inode
;
854 data
->cred
= req
->wb_context
->cred
;
856 data
->args
.fh
= NFS_FH(inode
);
857 data
->args
.offset
= req_offset(req
) + offset
;
858 data
->args
.pgbase
= req
->wb_pgbase
+ offset
;
859 data
->args
.pages
= data
->pagevec
;
860 data
->args
.count
= count
;
861 data
->args
.context
= req
->wb_context
;
863 data
->res
.fattr
= &data
->fattr
;
864 data
->res
.count
= count
;
865 data
->res
.verf
= &data
->verf
;
867 NFS_PROTO(inode
)->write_setup(data
, how
);
869 data
->task
.tk_priority
= flush_task_priority(how
);
870 data
->task
.tk_cookie
= (unsigned long)inode
;
871 data
->task
.tk_calldata
= data
;
872 /* Release requests */
873 data
->task
.tk_release
= nfs_writedata_release
;
875 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
878 (long long)NFS_FILEID(inode
),
880 (unsigned long long)data
->args
.offset
);
883 static void nfs_execute_write(struct nfs_write_data
*data
)
885 struct rpc_clnt
*clnt
= NFS_CLIENT(data
->inode
);
888 rpc_clnt_sigmask(clnt
, &oldset
);
890 rpc_execute(&data
->task
);
892 rpc_clnt_sigunmask(clnt
, &oldset
);
896 * Generate multiple small requests to write out a single
897 * contiguous dirty area on one page.
899 static int nfs_flush_multi(struct list_head
*head
, struct inode
*inode
, int how
)
901 struct nfs_page
*req
= nfs_list_entry(head
->next
);
902 struct page
*page
= req
->wb_page
;
903 struct nfs_write_data
*data
;
904 unsigned int wsize
= NFS_SERVER(inode
)->wsize
;
905 unsigned int nbytes
, offset
;
909 nfs_list_remove_request(req
);
911 nbytes
= req
->wb_bytes
;
913 data
= nfs_writedata_alloc();
916 list_add(&data
->pages
, &list
);
922 atomic_set(&req
->wb_complete
, requests
);
924 ClearPageError(page
);
925 SetPageWriteback(page
);
927 nbytes
= req
->wb_bytes
;
929 data
= list_entry(list
.next
, struct nfs_write_data
, pages
);
930 list_del_init(&data
->pages
);
932 data
->pagevec
[0] = page
;
933 data
->complete
= nfs_writeback_done_partial
;
935 if (nbytes
> wsize
) {
936 nfs_write_rpcsetup(req
, data
, wsize
, offset
, how
);
940 nfs_write_rpcsetup(req
, data
, nbytes
, offset
, how
);
943 nfs_execute_write(data
);
944 } while (nbytes
!= 0);
949 while (!list_empty(&list
)) {
950 data
= list_entry(list
.next
, struct nfs_write_data
, pages
);
951 list_del(&data
->pages
);
952 nfs_writedata_free(data
);
954 nfs_mark_request_dirty(req
);
955 nfs_unlock_request(req
);
960 * Create an RPC task for the given write request and kick it.
961 * The page must have been locked by the caller.
963 * It may happen that the page we're passed is not marked dirty.
964 * This is the case if nfs_updatepage detects a conflicting request
965 * that has been written but not committed.
967 static int nfs_flush_one(struct list_head
*head
, struct inode
*inode
, int how
)
969 struct nfs_page
*req
;
971 struct nfs_write_data
*data
;
974 if (NFS_SERVER(inode
)->wsize
< PAGE_CACHE_SIZE
)
975 return nfs_flush_multi(head
, inode
, how
);
977 data
= nfs_writedata_alloc();
981 pages
= data
->pagevec
;
983 while (!list_empty(head
)) {
984 req
= nfs_list_entry(head
->next
);
985 nfs_list_remove_request(req
);
986 nfs_list_add_request(req
, &data
->pages
);
987 ClearPageError(req
->wb_page
);
988 SetPageWriteback(req
->wb_page
);
989 *pages
++ = req
->wb_page
;
990 count
+= req
->wb_bytes
;
992 req
= nfs_list_entry(data
->pages
.next
);
994 data
->complete
= nfs_writeback_done_full
;
995 /* Set up the argument struct */
996 nfs_write_rpcsetup(req
, data
, count
, 0, how
);
998 nfs_execute_write(data
);
1001 while (!list_empty(head
)) {
1002 struct nfs_page
*req
= nfs_list_entry(head
->next
);
1003 nfs_list_remove_request(req
);
1004 nfs_mark_request_dirty(req
);
1005 nfs_unlock_request(req
);
1011 nfs_flush_list(struct list_head
*head
, int wpages
, int how
)
1013 LIST_HEAD(one_request
);
1014 struct nfs_page
*req
;
1016 unsigned int pages
= 0;
1018 while (!list_empty(head
)) {
1019 pages
+= nfs_coalesce_requests(head
, &one_request
, wpages
);
1020 req
= nfs_list_entry(one_request
.next
);
1021 error
= nfs_flush_one(&one_request
, req
->wb_context
->dentry
->d_inode
, how
);
1028 while (!list_empty(head
)) {
1029 req
= nfs_list_entry(head
->next
);
1030 nfs_list_remove_request(req
);
1031 nfs_mark_request_dirty(req
);
1032 nfs_unlock_request(req
);
1038 * Handle a write reply that flushed part of a page.
1040 static void nfs_writeback_done_partial(struct nfs_write_data
*data
, int status
)
1042 struct nfs_page
*req
= data
->req
;
1043 struct page
*page
= req
->wb_page
;
1045 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1046 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
1047 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
1049 (long long)req_offset(req
));
1052 ClearPageUptodate(page
);
1054 req
->wb_context
->error
= status
;
1055 dprintk(", error = %d\n", status
);
1057 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1058 if (data
->verf
.committed
< NFS_FILE_SYNC
) {
1059 if (!NFS_NEED_COMMIT(req
)) {
1060 nfs_defer_commit(req
);
1061 memcpy(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
));
1062 dprintk(" defer commit\n");
1063 } else if (memcmp(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
))) {
1064 nfs_defer_reschedule(req
);
1065 dprintk(" server reboot detected\n");
1072 if (atomic_dec_and_test(&req
->wb_complete
))
1073 nfs_writepage_release(req
);
1077 * Handle a write reply that flushes a whole page.
1079 * FIXME: There is an inherent race with invalidate_inode_pages and
1080 * writebacks since the page->count is kept > 1 for as long
1081 * as the page has a write request pending.
1083 static void nfs_writeback_done_full(struct nfs_write_data
*data
, int status
)
1085 struct nfs_page
*req
;
1088 /* Update attributes as result of writeback. */
1089 while (!list_empty(&data
->pages
)) {
1090 req
= nfs_list_entry(data
->pages
.next
);
1091 nfs_list_remove_request(req
);
1092 page
= req
->wb_page
;
1094 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1095 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
1096 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
1098 (long long)req_offset(req
));
1101 ClearPageUptodate(page
);
1103 req
->wb_context
->error
= status
;
1104 end_page_writeback(page
);
1105 nfs_inode_remove_request(req
);
1106 dprintk(", error = %d\n", status
);
1109 end_page_writeback(page
);
1111 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1112 if (data
->args
.stable
!= NFS_UNSTABLE
|| data
->verf
.committed
== NFS_FILE_SYNC
) {
1113 nfs_inode_remove_request(req
);
1117 memcpy(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
));
1118 nfs_mark_request_commit(req
);
1119 dprintk(" marked for commit\n");
1121 nfs_inode_remove_request(req
);
1124 nfs_unlock_request(req
);
1129 * This function is called when the WRITE call is complete.
1131 void nfs_writeback_done(struct rpc_task
*task
)
1133 struct nfs_write_data
*data
= (struct nfs_write_data
*) task
->tk_calldata
;
1134 struct nfs_writeargs
*argp
= &data
->args
;
1135 struct nfs_writeres
*resp
= &data
->res
;
1137 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1138 task
->tk_pid
, task
->tk_status
);
1140 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1141 if (resp
->verf
->committed
< argp
->stable
&& task
->tk_status
>= 0) {
1142 /* We tried a write call, but the server did not
1143 * commit data to stable storage even though we
1145 * Note: There is a known bug in Tru64 < 5.0 in which
1146 * the server reports NFS_DATA_SYNC, but performs
1147 * NFS_FILE_SYNC. We therefore implement this checking
1148 * as a dprintk() in order to avoid filling syslog.
1150 static unsigned long complain
;
1152 if (time_before(complain
, jiffies
)) {
1153 dprintk("NFS: faulty NFS server %s:"
1154 " (committed = %d) != (stable = %d)\n",
1155 NFS_SERVER(data
->inode
)->hostname
,
1156 resp
->verf
->committed
, argp
->stable
);
1157 complain
= jiffies
+ 300 * HZ
;
1161 /* Is this a short write? */
1162 if (task
->tk_status
>= 0 && resp
->count
< argp
->count
) {
1163 static unsigned long complain
;
1165 /* Has the server at least made some progress? */
1166 if (resp
->count
!= 0) {
1167 /* Was this an NFSv2 write or an NFSv3 stable write? */
1168 if (resp
->verf
->committed
!= NFS_UNSTABLE
) {
1169 /* Resend from where the server left off */
1170 argp
->offset
+= resp
->count
;
1171 argp
->pgbase
+= resp
->count
;
1172 argp
->count
-= resp
->count
;
1174 /* Resend as a stable write in order to avoid
1175 * headaches in the case of a server crash.
1177 argp
->stable
= NFS_FILE_SYNC
;
1179 rpc_restart_call(task
);
1182 if (time_before(complain
, jiffies
)) {
1184 "NFS: Server wrote zero bytes, expected %u.\n",
1186 complain
= jiffies
+ 300 * HZ
;
1188 /* Can't do anything about it except throw an error. */
1189 task
->tk_status
= -EIO
;
1193 * Process the nfs_page list
1195 data
->complete(data
, task
->tk_status
);
1199 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1200 static void nfs_commit_release(struct rpc_task
*task
)
1202 struct nfs_write_data
*wdata
= (struct nfs_write_data
*)task
->tk_calldata
;
1203 nfs_commit_free(wdata
);
1207 * Set up the argument/result storage required for the RPC call.
1209 static void nfs_commit_rpcsetup(struct list_head
*head
,
1210 struct nfs_write_data
*data
, int how
)
1212 struct rpc_task
*task
= &data
->task
;
1213 struct nfs_page
*first
, *last
;
1214 struct inode
*inode
;
1215 loff_t start
, end
, len
;
1217 /* Set up the RPC argument and reply structs
1218 * NB: take care not to mess about with data->commit et al. */
1220 list_splice_init(head
, &data
->pages
);
1221 first
= nfs_list_entry(data
->pages
.next
);
1222 last
= nfs_list_entry(data
->pages
.prev
);
1223 inode
= first
->wb_context
->dentry
->d_inode
;
1226 * Determine the offset range of requests in the COMMIT call.
1227 * We rely on the fact that data->pages is an ordered list...
1229 start
= req_offset(first
);
1230 end
= req_offset(last
) + last
->wb_bytes
;
1232 /* If 'len' is not a 32-bit quantity, pass '0' in the COMMIT call */
1233 if (end
>= i_size_read(inode
) || len
< 0 || len
> (~((u32
)0) >> 1))
1236 data
->inode
= inode
;
1237 data
->cred
= first
->wb_context
->cred
;
1239 data
->args
.fh
= NFS_FH(data
->inode
);
1240 data
->args
.offset
= start
;
1241 data
->args
.count
= len
;
1242 data
->res
.count
= len
;
1243 data
->res
.fattr
= &data
->fattr
;
1244 data
->res
.verf
= &data
->verf
;
1246 NFS_PROTO(inode
)->commit_setup(data
, how
);
1248 data
->task
.tk_priority
= flush_task_priority(how
);
1249 data
->task
.tk_cookie
= (unsigned long)inode
;
1250 data
->task
.tk_calldata
= data
;
1251 /* Release requests */
1252 data
->task
.tk_release
= nfs_commit_release
;
1254 dprintk("NFS: %4d initiated commit call\n", task
->tk_pid
);
1258 * Commit dirty pages
1261 nfs_commit_list(struct list_head
*head
, int how
)
1263 struct nfs_write_data
*data
;
1264 struct nfs_page
*req
;
1266 data
= nfs_commit_alloc();
1271 /* Set up the argument struct */
1272 nfs_commit_rpcsetup(head
, data
, how
);
1274 nfs_execute_write(data
);
1277 while (!list_empty(head
)) {
1278 req
= nfs_list_entry(head
->next
);
1279 nfs_list_remove_request(req
);
1280 nfs_mark_request_commit(req
);
1281 nfs_unlock_request(req
);
1287 * COMMIT call returned
1290 nfs_commit_done(struct rpc_task
*task
)
1292 struct nfs_write_data
*data
= (struct nfs_write_data
*)task
->tk_calldata
;
1293 struct nfs_page
*req
;
1296 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1297 task
->tk_pid
, task
->tk_status
);
1299 while (!list_empty(&data
->pages
)) {
1300 req
= nfs_list_entry(data
->pages
.next
);
1301 nfs_list_remove_request(req
);
1303 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1304 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
1305 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
1307 (long long)req_offset(req
));
1308 if (task
->tk_status
< 0) {
1309 req
->wb_context
->error
= task
->tk_status
;
1310 nfs_inode_remove_request(req
);
1311 dprintk(", error = %d\n", task
->tk_status
);
1315 /* Okay, COMMIT succeeded, apparently. Check the verifier
1316 * returned by the server against all stored verfs. */
1317 if (!memcmp(req
->wb_verf
.verifier
, data
->verf
.verifier
, sizeof(data
->verf
.verifier
))) {
1318 /* We have a match */
1319 nfs_inode_remove_request(req
);
1323 /* We have a mismatch. Write the page again */
1324 dprintk(" mismatch\n");
1325 nfs_mark_request_dirty(req
);
1327 nfs_unlock_request(req
);
1330 sub_page_state(nr_unstable
,res
);
1334 static int nfs_flush_inode(struct inode
*inode
, unsigned long idx_start
,
1335 unsigned int npages
, int how
)
1337 struct nfs_inode
*nfsi
= NFS_I(inode
);
1342 spin_lock(&nfsi
->req_lock
);
1343 res
= nfs_scan_dirty(inode
, &head
, idx_start
, npages
);
1344 spin_unlock(&nfsi
->req_lock
);
1346 error
= nfs_flush_list(&head
, NFS_SERVER(inode
)->wpages
, how
);
1352 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1353 int nfs_commit_inode(struct inode
*inode
, unsigned long idx_start
,
1354 unsigned int npages
, int how
)
1356 struct nfs_inode
*nfsi
= NFS_I(inode
);
1361 spin_lock(&nfsi
->req_lock
);
1362 res
= nfs_scan_commit(inode
, &head
, idx_start
, npages
);
1364 res
+= nfs_scan_commit(inode
, &head
, 0, 0);
1365 spin_unlock(&nfsi
->req_lock
);
1366 error
= nfs_commit_list(&head
, how
);
1368 spin_unlock(&nfsi
->req_lock
);
1375 int nfs_sync_inode(struct inode
*inode
, unsigned long idx_start
,
1376 unsigned int npages
, int how
)
1381 wait
= how
& FLUSH_WAIT
;
1387 error
= nfs_wait_on_requests(inode
, idx_start
, npages
);
1389 error
= nfs_flush_inode(inode
, idx_start
, npages
, how
);
1390 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1392 error
= nfs_commit_inode(inode
, idx_start
, npages
, how
);
1394 } while (error
> 0);
1398 int nfs_init_writepagecache(void)
1400 nfs_wdata_cachep
= kmem_cache_create("nfs_write_data",
1401 sizeof(struct nfs_write_data
),
1402 0, SLAB_HWCACHE_ALIGN
,
1404 if (nfs_wdata_cachep
== NULL
)
1407 nfs_wdata_mempool
= mempool_create(MIN_POOL_WRITE
,
1411 if (nfs_wdata_mempool
== NULL
)
1414 nfs_commit_mempool
= mempool_create(MIN_POOL_COMMIT
,
1418 if (nfs_commit_mempool
== NULL
)
1424 void nfs_destroy_writepagecache(void)
1426 mempool_destroy(nfs_commit_mempool
);
1427 mempool_destroy(nfs_wdata_mempool
);
1428 if (kmem_cache_destroy(nfs_wdata_cachep
))
1429 printk(KERN_INFO
"nfs_write_data: not all structures were freed\n");