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"
68 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
70 #define MIN_POOL_WRITE (32)
71 #define MIN_POOL_COMMIT (4)
74 * Local function declarations
76 static struct nfs_page
* nfs_update_request(struct nfs_open_context
*,
79 unsigned int, unsigned int);
80 static int nfs_writeback_done(struct rpc_task
*, struct nfs_write_data
*);
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
);
85 static const struct rpc_call_ops nfs_write_partial_ops
;
86 static const struct rpc_call_ops nfs_write_full_ops
;
87 static const struct rpc_call_ops nfs_commit_ops
;
89 static kmem_cache_t
*nfs_wdata_cachep
;
90 mempool_t
*nfs_wdata_mempool
;
91 static mempool_t
*nfs_commit_mempool
;
93 static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion
);
95 static inline struct nfs_write_data
*nfs_commit_alloc(unsigned int pagecount
)
97 struct nfs_write_data
*p
= mempool_alloc(nfs_commit_mempool
, SLAB_NOFS
);
100 memset(p
, 0, sizeof(*p
));
101 INIT_LIST_HEAD(&p
->pages
);
102 if (pagecount
< NFS_PAGEVEC_SIZE
)
103 p
->pagevec
= &p
->page_array
[0];
105 size_t size
= ++pagecount
* sizeof(struct page
*);
106 p
->pagevec
= kzalloc(size
, GFP_NOFS
);
108 mempool_free(p
, nfs_commit_mempool
);
116 static inline void nfs_commit_free(struct nfs_write_data
*p
)
118 if (p
&& (p
->pagevec
!= &p
->page_array
[0]))
120 mempool_free(p
, nfs_commit_mempool
);
123 void nfs_writedata_release(void *wdata
)
125 nfs_writedata_free(wdata
);
128 /* Adjust the file length if we're writing beyond the end */
129 static void nfs_grow_file(struct page
*page
, unsigned int offset
, unsigned int count
)
131 struct inode
*inode
= page
->mapping
->host
;
132 loff_t end
, i_size
= i_size_read(inode
);
133 unsigned long end_index
= (i_size
- 1) >> PAGE_CACHE_SHIFT
;
135 if (i_size
> 0 && page
->index
< end_index
)
137 end
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + ((loff_t
)offset
+count
);
140 nfs_inc_stats(inode
, NFSIOS_EXTENDWRITE
);
141 i_size_write(inode
, end
);
144 /* We can set the PG_uptodate flag if we see that a write request
145 * covers the full page.
147 static void nfs_mark_uptodate(struct page
*page
, unsigned int base
, unsigned int count
)
151 if (PageUptodate(page
))
155 if (count
== PAGE_CACHE_SIZE
) {
156 SetPageUptodate(page
);
160 end_offs
= i_size_read(page
->mapping
->host
) - 1;
163 /* Is this the last page? */
164 if (page
->index
!= (unsigned long)(end_offs
>> PAGE_CACHE_SHIFT
))
166 /* This is the last page: set PG_uptodate if we cover the entire
167 * extent of the data, then zero the rest of the page.
169 if (count
== (unsigned int)(end_offs
& (PAGE_CACHE_SIZE
- 1)) + 1) {
170 memclear_highpage_flush(page
, count
, PAGE_CACHE_SIZE
- count
);
171 SetPageUptodate(page
);
176 * Write a page synchronously.
177 * Offset is the data offset within the page.
179 static int nfs_writepage_sync(struct nfs_open_context
*ctx
, struct inode
*inode
,
180 struct page
*page
, unsigned int offset
, unsigned int count
,
183 unsigned int wsize
= NFS_SERVER(inode
)->wsize
;
184 int result
, written
= 0;
185 struct nfs_write_data
*wdata
;
187 wdata
= nfs_writedata_alloc(1);
192 wdata
->cred
= ctx
->cred
;
193 wdata
->inode
= inode
;
194 wdata
->args
.fh
= NFS_FH(inode
);
195 wdata
->args
.context
= ctx
;
196 wdata
->args
.pages
= &page
;
197 wdata
->args
.stable
= NFS_FILE_SYNC
;
198 wdata
->args
.pgbase
= offset
;
199 wdata
->args
.count
= wsize
;
200 wdata
->res
.fattr
= &wdata
->fattr
;
201 wdata
->res
.verf
= &wdata
->verf
;
203 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
205 (long long)NFS_FILEID(inode
),
206 count
, (long long)(page_offset(page
) + offset
));
208 set_page_writeback(page
);
209 nfs_begin_data_update(inode
);
212 wdata
->args
.count
= count
;
213 wdata
->args
.offset
= page_offset(page
) + wdata
->args
.pgbase
;
215 result
= NFS_PROTO(inode
)->write(wdata
);
218 /* Must mark the page invalid after I/O error */
219 ClearPageUptodate(page
);
222 if (result
< wdata
->args
.count
)
223 printk(KERN_WARNING
"NFS: short write, count=%u, result=%d\n",
224 wdata
->args
.count
, result
);
226 wdata
->args
.offset
+= result
;
227 wdata
->args
.pgbase
+= result
;
230 nfs_add_stats(inode
, NFSIOS_SERVERWRITTENBYTES
, result
);
232 /* Update file length */
233 nfs_grow_file(page
, offset
, written
);
234 /* Set the PG_uptodate flag? */
235 nfs_mark_uptodate(page
, offset
, written
);
238 ClearPageError(page
);
241 nfs_end_data_update(inode
);
242 end_page_writeback(page
);
243 nfs_writedata_free(wdata
);
244 return written
? written
: result
;
247 static int nfs_writepage_async(struct nfs_open_context
*ctx
,
248 struct inode
*inode
, struct page
*page
,
249 unsigned int offset
, unsigned int count
)
251 struct nfs_page
*req
;
253 req
= nfs_update_request(ctx
, inode
, page
, offset
, count
);
256 /* Update file length */
257 nfs_grow_file(page
, offset
, count
);
258 /* Set the PG_uptodate flag? */
259 nfs_mark_uptodate(page
, offset
, count
);
260 nfs_unlock_request(req
);
264 static int wb_priority(struct writeback_control
*wbc
)
266 if (wbc
->for_reclaim
)
267 return FLUSH_HIGHPRI
;
268 if (wbc
->for_kupdate
)
274 * Write an mmapped page to the server.
276 int nfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
278 struct nfs_open_context
*ctx
;
279 struct inode
*inode
= page
->mapping
->host
;
280 unsigned long end_index
;
281 unsigned offset
= PAGE_CACHE_SIZE
;
282 loff_t i_size
= i_size_read(inode
);
283 int inode_referenced
= 0;
284 int priority
= wb_priority(wbc
);
287 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGE
);
288 nfs_add_stats(inode
, NFSIOS_WRITEPAGES
, 1);
291 * Note: We need to ensure that we have a reference to the inode
292 * if we are to do asynchronous writes. If not, waiting
293 * in nfs_wait_on_request() may deadlock with clear_inode().
295 * If igrab() fails here, then it is in any case safe to
296 * call nfs_wb_page(), since there will be no pending writes.
298 if (igrab(inode
) != 0)
299 inode_referenced
= 1;
300 end_index
= i_size
>> PAGE_CACHE_SHIFT
;
302 /* Ensure we've flushed out any previous writes */
303 nfs_wb_page_priority(inode
, page
, priority
);
306 if (page
->index
< end_index
)
308 /* things got complicated... */
309 offset
= i_size
& (PAGE_CACHE_SIZE
-1);
311 /* OK, are we completely out? */
312 err
= 0; /* potential race with truncate - ignore */
313 if (page
->index
>= end_index
+1 || !offset
)
316 ctx
= nfs_find_open_context(inode
, NULL
, FMODE_WRITE
);
322 if (!IS_SYNC(inode
) && inode_referenced
) {
323 err
= nfs_writepage_async(ctx
, inode
, page
, 0, offset
);
324 if (!wbc
->for_writepages
)
325 nfs_flush_inode(inode
, 0, 0, wb_priority(wbc
));
327 err
= nfs_writepage_sync(ctx
, inode
, page
, 0,
331 redirty_page_for_writepage(wbc
, page
);
336 put_nfs_open_context(ctx
);
339 if (inode_referenced
)
345 * Note: causes nfs_update_request() to block on the assumption
346 * that the writeback is generated due to memory pressure.
348 int nfs_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
350 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
351 struct inode
*inode
= mapping
->host
;
354 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGES
);
356 err
= generic_writepages(mapping
, wbc
);
359 while (test_and_set_bit(BDI_write_congested
, &bdi
->state
) != 0) {
360 if (wbc
->nonblocking
)
362 nfs_wait_on_write_congestion(mapping
, 0);
364 err
= nfs_flush_inode(inode
, 0, 0, wb_priority(wbc
));
367 nfs_add_stats(inode
, NFSIOS_WRITEPAGES
, err
);
368 wbc
->nr_to_write
-= err
;
369 if (!wbc
->nonblocking
&& wbc
->sync_mode
== WB_SYNC_ALL
) {
370 err
= nfs_wait_on_requests(inode
, 0, 0);
374 err
= nfs_commit_inode(inode
, wb_priority(wbc
));
376 wbc
->nr_to_write
-= err
;
380 clear_bit(BDI_write_congested
, &bdi
->state
);
381 wake_up_all(&nfs_write_congestion
);
386 * Insert a write request into an inode
388 static int nfs_inode_add_request(struct inode
*inode
, struct nfs_page
*req
)
390 struct nfs_inode
*nfsi
= NFS_I(inode
);
393 error
= radix_tree_insert(&nfsi
->nfs_page_tree
, req
->wb_index
, req
);
394 BUG_ON(error
== -EEXIST
);
399 nfs_begin_data_update(inode
);
400 if (nfs_have_delegation(inode
, FMODE_WRITE
))
404 atomic_inc(&req
->wb_count
);
409 * Insert a write request into an inode
411 static void nfs_inode_remove_request(struct nfs_page
*req
)
413 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
414 struct nfs_inode
*nfsi
= NFS_I(inode
);
416 BUG_ON (!NFS_WBACK_BUSY(req
));
418 spin_lock(&nfsi
->req_lock
);
419 radix_tree_delete(&nfsi
->nfs_page_tree
, req
->wb_index
);
422 spin_unlock(&nfsi
->req_lock
);
423 nfs_end_data_update(inode
);
426 spin_unlock(&nfsi
->req_lock
);
427 nfs_clear_request(req
);
428 nfs_release_request(req
);
434 static inline struct nfs_page
*
435 _nfs_find_request(struct inode
*inode
, unsigned long index
)
437 struct nfs_inode
*nfsi
= NFS_I(inode
);
438 struct nfs_page
*req
;
440 req
= (struct nfs_page
*)radix_tree_lookup(&nfsi
->nfs_page_tree
, index
);
442 atomic_inc(&req
->wb_count
);
446 static struct nfs_page
*
447 nfs_find_request(struct inode
*inode
, unsigned long index
)
449 struct nfs_page
*req
;
450 struct nfs_inode
*nfsi
= NFS_I(inode
);
452 spin_lock(&nfsi
->req_lock
);
453 req
= _nfs_find_request(inode
, index
);
454 spin_unlock(&nfsi
->req_lock
);
459 * Add a request to the inode's dirty list.
462 nfs_mark_request_dirty(struct nfs_page
*req
)
464 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
465 struct nfs_inode
*nfsi
= NFS_I(inode
);
467 spin_lock(&nfsi
->req_lock
);
468 radix_tree_tag_set(&nfsi
->nfs_page_tree
,
469 req
->wb_index
, NFS_PAGE_TAG_DIRTY
);
470 nfs_list_add_request(req
, &nfsi
->dirty
);
472 spin_unlock(&nfsi
->req_lock
);
473 inc_page_state(nr_dirty
);
474 mark_inode_dirty(inode
);
478 * Check if a request is dirty
481 nfs_dirty_request(struct nfs_page
*req
)
483 struct nfs_inode
*nfsi
= NFS_I(req
->wb_context
->dentry
->d_inode
);
484 return !list_empty(&req
->wb_list
) && req
->wb_list_head
== &nfsi
->dirty
;
487 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
489 * Add a request to the inode's commit list.
492 nfs_mark_request_commit(struct nfs_page
*req
)
494 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
495 struct nfs_inode
*nfsi
= NFS_I(inode
);
497 spin_lock(&nfsi
->req_lock
);
498 nfs_list_add_request(req
, &nfsi
->commit
);
500 spin_unlock(&nfsi
->req_lock
);
501 inc_page_state(nr_unstable
);
502 mark_inode_dirty(inode
);
507 * Wait for a request to complete.
509 * Interruptible by signals only if mounted with intr flag.
512 nfs_wait_on_requests(struct inode
*inode
, unsigned long idx_start
, unsigned int npages
)
514 struct nfs_inode
*nfsi
= NFS_I(inode
);
515 struct nfs_page
*req
;
516 unsigned long idx_end
, next
;
517 unsigned int res
= 0;
523 idx_end
= idx_start
+ npages
- 1;
525 spin_lock(&nfsi
->req_lock
);
527 while (radix_tree_gang_lookup_tag(&nfsi
->nfs_page_tree
, (void **)&req
, next
, 1, NFS_PAGE_TAG_WRITEBACK
)) {
528 if (req
->wb_index
> idx_end
)
531 next
= req
->wb_index
+ 1;
532 BUG_ON(!NFS_WBACK_BUSY(req
));
534 atomic_inc(&req
->wb_count
);
535 spin_unlock(&nfsi
->req_lock
);
536 error
= nfs_wait_on_request(req
);
537 nfs_release_request(req
);
540 spin_lock(&nfsi
->req_lock
);
543 spin_unlock(&nfsi
->req_lock
);
548 * nfs_scan_dirty - Scan an inode for dirty requests
549 * @inode: NFS inode to scan
550 * @dst: destination list
551 * @idx_start: lower bound of page->index to scan.
552 * @npages: idx_start + npages sets the upper bound to scan.
554 * Moves requests from the inode's dirty page list.
555 * The requests are *not* checked to ensure that they form a contiguous set.
558 nfs_scan_dirty(struct inode
*inode
, struct list_head
*dst
, unsigned long idx_start
, unsigned int npages
)
560 struct nfs_inode
*nfsi
= NFS_I(inode
);
563 if (nfsi
->ndirty
!= 0) {
564 res
= nfs_scan_lock_dirty(nfsi
, dst
, idx_start
, npages
);
566 sub_page_state(nr_dirty
,res
);
567 if ((nfsi
->ndirty
== 0) != list_empty(&nfsi
->dirty
))
568 printk(KERN_ERR
"NFS: desynchronized value of nfs_i.ndirty.\n");
573 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
575 * nfs_scan_commit - Scan an inode for commit requests
576 * @inode: NFS inode to scan
577 * @dst: destination list
578 * @idx_start: lower bound of page->index to scan.
579 * @npages: idx_start + npages sets the upper bound to scan.
581 * Moves requests from the inode's 'commit' request list.
582 * The requests are *not* checked to ensure that they form a contiguous set.
585 nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
, unsigned long idx_start
, unsigned int npages
)
587 struct nfs_inode
*nfsi
= NFS_I(inode
);
590 if (nfsi
->ncommit
!= 0) {
591 res
= nfs_scan_list(&nfsi
->commit
, dst
, idx_start
, npages
);
592 nfsi
->ncommit
-= res
;
593 if ((nfsi
->ncommit
== 0) != list_empty(&nfsi
->commit
))
594 printk(KERN_ERR
"NFS: desynchronized value of nfs_i.ncommit.\n");
600 static int nfs_wait_on_write_congestion(struct address_space
*mapping
, int intr
)
602 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
608 if (!bdi_write_congested(bdi
))
611 nfs_inc_stats(mapping
->host
, NFSIOS_CONGESTIONWAIT
);
614 struct rpc_clnt
*clnt
= NFS_CLIENT(mapping
->host
);
617 rpc_clnt_sigmask(clnt
, &oldset
);
618 prepare_to_wait(&nfs_write_congestion
, &wait
, TASK_INTERRUPTIBLE
);
619 if (bdi_write_congested(bdi
)) {
625 rpc_clnt_sigunmask(clnt
, &oldset
);
627 prepare_to_wait(&nfs_write_congestion
, &wait
, TASK_UNINTERRUPTIBLE
);
628 if (bdi_write_congested(bdi
))
631 finish_wait(&nfs_write_congestion
, &wait
);
637 * Try to update any existing write request, or create one if there is none.
638 * In order to match, the request's credentials must match those of
639 * the calling process.
641 * Note: Should always be called with the Page Lock held!
643 static struct nfs_page
* nfs_update_request(struct nfs_open_context
* ctx
,
644 struct inode
*inode
, struct page
*page
,
645 unsigned int offset
, unsigned int bytes
)
647 struct nfs_server
*server
= NFS_SERVER(inode
);
648 struct nfs_inode
*nfsi
= NFS_I(inode
);
649 struct nfs_page
*req
, *new = NULL
;
650 unsigned long rqend
, end
;
652 end
= offset
+ bytes
;
654 if (nfs_wait_on_write_congestion(page
->mapping
, server
->flags
& NFS_MOUNT_INTR
))
655 return ERR_PTR(-ERESTARTSYS
);
657 /* Loop over all inode entries and see if we find
658 * A request for the page we wish to update
660 spin_lock(&nfsi
->req_lock
);
661 req
= _nfs_find_request(inode
, page
->index
);
663 if (!nfs_lock_request_dontget(req
)) {
665 spin_unlock(&nfsi
->req_lock
);
666 error
= nfs_wait_on_request(req
);
667 nfs_release_request(req
);
670 nfs_release_request(new);
671 return ERR_PTR(error
);
675 spin_unlock(&nfsi
->req_lock
);
677 nfs_release_request(new);
683 nfs_lock_request_dontget(new);
684 error
= nfs_inode_add_request(inode
, new);
686 spin_unlock(&nfsi
->req_lock
);
687 nfs_unlock_request(new);
688 return ERR_PTR(error
);
690 spin_unlock(&nfsi
->req_lock
);
691 nfs_mark_request_dirty(new);
694 spin_unlock(&nfsi
->req_lock
);
696 new = nfs_create_request(ctx
, inode
, page
, offset
, bytes
);
701 /* We have a request for our page.
702 * If the creds don't match, or the
703 * page addresses don't match,
704 * tell the caller to wait on the conflicting
707 rqend
= req
->wb_offset
+ req
->wb_bytes
;
708 if (req
->wb_context
!= ctx
709 || req
->wb_page
!= page
710 || !nfs_dirty_request(req
)
711 || offset
> rqend
|| end
< req
->wb_offset
) {
712 nfs_unlock_request(req
);
713 return ERR_PTR(-EBUSY
);
716 /* Okay, the request matches. Update the region */
717 if (offset
< req
->wb_offset
) {
718 req
->wb_offset
= offset
;
719 req
->wb_pgbase
= offset
;
720 req
->wb_bytes
= rqend
- req
->wb_offset
;
724 req
->wb_bytes
= end
- req
->wb_offset
;
729 int nfs_flush_incompatible(struct file
*file
, struct page
*page
)
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 * Look for a request corresponding to this page. If there
737 * is one, and it belongs to another file, we flush it out
738 * before we try to copy anything into the page. Do this
739 * due to the lack of an ACCESS-type call in NFSv2.
740 * Also do the same if we find a request from an existing
743 req
= nfs_find_request(inode
, page
->index
);
745 if (req
->wb_page
!= page
|| ctx
!= req
->wb_context
)
746 status
= nfs_wb_page(inode
, page
);
747 nfs_release_request(req
);
749 return (status
< 0) ? status
: 0;
753 * Update and possibly write a cached page of an NFS file.
755 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
756 * things with a page scheduled for an RPC call (e.g. invalidate it).
758 int nfs_updatepage(struct file
*file
, struct page
*page
,
759 unsigned int offset
, unsigned int count
)
761 struct nfs_open_context
*ctx
= (struct nfs_open_context
*)file
->private_data
;
762 struct inode
*inode
= page
->mapping
->host
;
763 struct nfs_page
*req
;
766 nfs_inc_stats(inode
, NFSIOS_VFSUPDATEPAGE
);
768 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
769 file
->f_dentry
->d_parent
->d_name
.name
,
770 file
->f_dentry
->d_name
.name
, count
,
771 (long long)(page_offset(page
) +offset
));
773 if (IS_SYNC(inode
)) {
774 status
= nfs_writepage_sync(ctx
, inode
, page
, offset
, count
, 0);
776 if (offset
== 0 && status
== PAGE_CACHE_SIZE
)
777 SetPageUptodate(page
);
783 /* If we're not using byte range locks, and we know the page
784 * is entirely in cache, it may be more efficient to avoid
785 * fragmenting write requests.
787 if (PageUptodate(page
) && inode
->i_flock
== NULL
&& !(file
->f_mode
& O_SYNC
)) {
788 loff_t end_offs
= i_size_read(inode
) - 1;
789 unsigned long end_index
= end_offs
>> PAGE_CACHE_SHIFT
;
793 if (unlikely(end_offs
< 0)) {
795 } else if (page
->index
== end_index
) {
797 pglen
= (unsigned int)(end_offs
& (PAGE_CACHE_SIZE
-1)) + 1;
800 } else if (page
->index
< end_index
)
801 count
= PAGE_CACHE_SIZE
;
805 * Try to find an NFS request corresponding to this page
807 * If the existing request cannot be updated, we must flush
811 req
= nfs_update_request(ctx
, inode
, page
, offset
, count
);
812 status
= (IS_ERR(req
)) ? PTR_ERR(req
) : 0;
813 if (status
!= -EBUSY
)
815 /* Request could not be updated. Flush it out and try again */
816 status
= nfs_wb_page(inode
, page
);
817 } while (status
>= 0);
823 /* Update file length */
824 nfs_grow_file(page
, offset
, count
);
825 /* Set the PG_uptodate flag? */
826 nfs_mark_uptodate(page
, req
->wb_pgbase
, req
->wb_bytes
);
827 nfs_unlock_request(req
);
829 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
830 status
, (long long)i_size_read(inode
));
832 ClearPageUptodate(page
);
836 static void nfs_writepage_release(struct nfs_page
*req
)
838 end_page_writeback(req
->wb_page
);
840 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
841 if (!PageError(req
->wb_page
)) {
842 if (NFS_NEED_RESCHED(req
)) {
843 nfs_mark_request_dirty(req
);
845 } else if (NFS_NEED_COMMIT(req
)) {
846 nfs_mark_request_commit(req
);
850 nfs_inode_remove_request(req
);
853 nfs_clear_commit(req
);
854 nfs_clear_reschedule(req
);
856 nfs_inode_remove_request(req
);
858 nfs_clear_page_writeback(req
);
861 static inline int flush_task_priority(int how
)
863 switch (how
& (FLUSH_HIGHPRI
|FLUSH_LOWPRI
)) {
865 return RPC_PRIORITY_HIGH
;
867 return RPC_PRIORITY_LOW
;
869 return RPC_PRIORITY_NORMAL
;
873 * Set up the argument/result storage required for the RPC call.
875 static void nfs_write_rpcsetup(struct nfs_page
*req
,
876 struct nfs_write_data
*data
,
877 const struct rpc_call_ops
*call_ops
,
878 unsigned int count
, unsigned int offset
,
884 /* Set up the RPC argument and reply structs
885 * NB: take care not to mess about with data->commit et al. */
888 data
->inode
= inode
= req
->wb_context
->dentry
->d_inode
;
889 data
->cred
= req
->wb_context
->cred
;
891 data
->args
.fh
= NFS_FH(inode
);
892 data
->args
.offset
= req_offset(req
) + offset
;
893 data
->args
.pgbase
= req
->wb_pgbase
+ offset
;
894 data
->args
.pages
= data
->pagevec
;
895 data
->args
.count
= count
;
896 data
->args
.context
= req
->wb_context
;
898 data
->res
.fattr
= &data
->fattr
;
899 data
->res
.count
= count
;
900 data
->res
.verf
= &data
->verf
;
901 nfs_fattr_init(&data
->fattr
);
903 /* Set up the initial task struct. */
904 flags
= (how
& FLUSH_SYNC
) ? 0 : RPC_TASK_ASYNC
;
905 rpc_init_task(&data
->task
, NFS_CLIENT(inode
), flags
, call_ops
, data
);
906 NFS_PROTO(inode
)->write_setup(data
, how
);
908 data
->task
.tk_priority
= flush_task_priority(how
);
909 data
->task
.tk_cookie
= (unsigned long)inode
;
911 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
914 (long long)NFS_FILEID(inode
),
916 (unsigned long long)data
->args
.offset
);
919 static void nfs_execute_write(struct nfs_write_data
*data
)
921 struct rpc_clnt
*clnt
= NFS_CLIENT(data
->inode
);
924 rpc_clnt_sigmask(clnt
, &oldset
);
926 rpc_execute(&data
->task
);
928 rpc_clnt_sigunmask(clnt
, &oldset
);
932 * Generate multiple small requests to write out a single
933 * contiguous dirty area on one page.
935 static int nfs_flush_multi(struct list_head
*head
, struct inode
*inode
, int how
)
937 struct nfs_page
*req
= nfs_list_entry(head
->next
);
938 struct page
*page
= req
->wb_page
;
939 struct nfs_write_data
*data
;
940 unsigned int wsize
= NFS_SERVER(inode
)->wsize
;
941 unsigned int nbytes
, offset
;
945 nfs_list_remove_request(req
);
947 nbytes
= req
->wb_bytes
;
949 data
= nfs_writedata_alloc(1);
952 list_add(&data
->pages
, &list
);
958 atomic_set(&req
->wb_complete
, requests
);
960 ClearPageError(page
);
961 set_page_writeback(page
);
963 nbytes
= req
->wb_bytes
;
965 data
= list_entry(list
.next
, struct nfs_write_data
, pages
);
966 list_del_init(&data
->pages
);
968 data
->pagevec
[0] = page
;
970 if (nbytes
> wsize
) {
971 nfs_write_rpcsetup(req
, data
, &nfs_write_partial_ops
,
976 nfs_write_rpcsetup(req
, data
, &nfs_write_partial_ops
,
977 nbytes
, offset
, how
);
980 nfs_execute_write(data
);
981 } while (nbytes
!= 0);
986 while (!list_empty(&list
)) {
987 data
= list_entry(list
.next
, struct nfs_write_data
, pages
);
988 list_del(&data
->pages
);
989 nfs_writedata_free(data
);
991 nfs_mark_request_dirty(req
);
992 nfs_clear_page_writeback(req
);
997 * Create an RPC task for the given write request and kick it.
998 * The page must have been locked by the caller.
1000 * It may happen that the page we're passed is not marked dirty.
1001 * This is the case if nfs_updatepage detects a conflicting request
1002 * that has been written but not committed.
1004 static int nfs_flush_one(struct list_head
*head
, struct inode
*inode
, int how
)
1006 struct nfs_page
*req
;
1007 struct page
**pages
;
1008 struct nfs_write_data
*data
;
1011 if (NFS_SERVER(inode
)->wsize
< PAGE_CACHE_SIZE
)
1012 return nfs_flush_multi(head
, inode
, how
);
1014 data
= nfs_writedata_alloc(NFS_SERVER(inode
)->wpages
);
1018 pages
= data
->pagevec
;
1020 while (!list_empty(head
)) {
1021 req
= nfs_list_entry(head
->next
);
1022 nfs_list_remove_request(req
);
1023 nfs_list_add_request(req
, &data
->pages
);
1024 ClearPageError(req
->wb_page
);
1025 set_page_writeback(req
->wb_page
);
1026 *pages
++ = req
->wb_page
;
1027 count
+= req
->wb_bytes
;
1029 req
= nfs_list_entry(data
->pages
.next
);
1031 /* Set up the argument struct */
1032 nfs_write_rpcsetup(req
, data
, &nfs_write_full_ops
, count
, 0, how
);
1034 nfs_execute_write(data
);
1037 while (!list_empty(head
)) {
1038 struct nfs_page
*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 nfs_flush_list(struct list_head
*head
, int wpages
, int how
)
1049 LIST_HEAD(one_request
);
1050 struct nfs_page
*req
;
1052 unsigned int pages
= 0;
1054 while (!list_empty(head
)) {
1055 pages
+= nfs_coalesce_requests(head
, &one_request
, wpages
);
1056 req
= nfs_list_entry(one_request
.next
);
1057 error
= nfs_flush_one(&one_request
, req
->wb_context
->dentry
->d_inode
, how
);
1064 while (!list_empty(head
)) {
1065 req
= nfs_list_entry(head
->next
);
1066 nfs_list_remove_request(req
);
1067 nfs_mark_request_dirty(req
);
1068 nfs_clear_page_writeback(req
);
1074 * Handle a write reply that flushed part of a page.
1076 static void nfs_writeback_done_partial(struct rpc_task
*task
, void *calldata
)
1078 struct nfs_write_data
*data
= calldata
;
1079 struct nfs_page
*req
= data
->req
;
1080 struct page
*page
= req
->wb_page
;
1082 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1083 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
1084 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
1086 (long long)req_offset(req
));
1088 if (nfs_writeback_done(task
, data
) != 0)
1091 if (task
->tk_status
< 0) {
1092 ClearPageUptodate(page
);
1094 req
->wb_context
->error
= task
->tk_status
;
1095 dprintk(", error = %d\n", task
->tk_status
);
1097 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1098 if (data
->verf
.committed
< NFS_FILE_SYNC
) {
1099 if (!NFS_NEED_COMMIT(req
)) {
1100 nfs_defer_commit(req
);
1101 memcpy(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
));
1102 dprintk(" defer commit\n");
1103 } else if (memcmp(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
))) {
1104 nfs_defer_reschedule(req
);
1105 dprintk(" server reboot detected\n");
1112 if (atomic_dec_and_test(&req
->wb_complete
))
1113 nfs_writepage_release(req
);
1116 static const struct rpc_call_ops nfs_write_partial_ops
= {
1117 .rpc_call_done
= nfs_writeback_done_partial
,
1118 .rpc_release
= nfs_writedata_release
,
1122 * Handle a write reply that flushes a whole page.
1124 * FIXME: There is an inherent race with invalidate_inode_pages and
1125 * writebacks since the page->count is kept > 1 for as long
1126 * as the page has a write request pending.
1128 static void nfs_writeback_done_full(struct rpc_task
*task
, void *calldata
)
1130 struct nfs_write_data
*data
= calldata
;
1131 struct nfs_page
*req
;
1134 if (nfs_writeback_done(task
, data
) != 0)
1137 /* Update attributes as result of writeback. */
1138 while (!list_empty(&data
->pages
)) {
1139 req
= nfs_list_entry(data
->pages
.next
);
1140 nfs_list_remove_request(req
);
1141 page
= req
->wb_page
;
1143 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1144 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
1145 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
1147 (long long)req_offset(req
));
1149 if (task
->tk_status
< 0) {
1150 ClearPageUptodate(page
);
1152 req
->wb_context
->error
= task
->tk_status
;
1153 end_page_writeback(page
);
1154 nfs_inode_remove_request(req
);
1155 dprintk(", error = %d\n", task
->tk_status
);
1158 end_page_writeback(page
);
1160 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1161 if (data
->args
.stable
!= NFS_UNSTABLE
|| data
->verf
.committed
== NFS_FILE_SYNC
) {
1162 nfs_inode_remove_request(req
);
1166 memcpy(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
));
1167 nfs_mark_request_commit(req
);
1168 dprintk(" marked for commit\n");
1170 nfs_inode_remove_request(req
);
1173 nfs_clear_page_writeback(req
);
1177 static const struct rpc_call_ops nfs_write_full_ops
= {
1178 .rpc_call_done
= nfs_writeback_done_full
,
1179 .rpc_release
= nfs_writedata_release
,
1184 * This function is called when the WRITE call is complete.
1186 static int nfs_writeback_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
1188 struct nfs_writeargs
*argp
= &data
->args
;
1189 struct nfs_writeres
*resp
= &data
->res
;
1192 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1193 task
->tk_pid
, task
->tk_status
);
1195 /* Call the NFS version-specific code */
1196 status
= NFS_PROTO(data
->inode
)->write_done(task
, data
);
1199 nfs_add_stats(data
->inode
, NFSIOS_SERVERWRITTENBYTES
, resp
->count
);
1201 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1202 if (resp
->verf
->committed
< argp
->stable
&& task
->tk_status
>= 0) {
1203 /* We tried a write call, but the server did not
1204 * commit data to stable storage even though we
1206 * Note: There is a known bug in Tru64 < 5.0 in which
1207 * the server reports NFS_DATA_SYNC, but performs
1208 * NFS_FILE_SYNC. We therefore implement this checking
1209 * as a dprintk() in order to avoid filling syslog.
1211 static unsigned long complain
;
1213 if (time_before(complain
, jiffies
)) {
1214 dprintk("NFS: faulty NFS server %s:"
1215 " (committed = %d) != (stable = %d)\n",
1216 NFS_SERVER(data
->inode
)->hostname
,
1217 resp
->verf
->committed
, argp
->stable
);
1218 complain
= jiffies
+ 300 * HZ
;
1222 /* Is this a short write? */
1223 if (task
->tk_status
>= 0 && resp
->count
< argp
->count
) {
1224 static unsigned long complain
;
1226 nfs_inc_stats(data
->inode
, NFSIOS_SHORTWRITE
);
1228 /* Has the server at least made some progress? */
1229 if (resp
->count
!= 0) {
1230 /* Was this an NFSv2 write or an NFSv3 stable write? */
1231 if (resp
->verf
->committed
!= NFS_UNSTABLE
) {
1232 /* Resend from where the server left off */
1233 argp
->offset
+= resp
->count
;
1234 argp
->pgbase
+= resp
->count
;
1235 argp
->count
-= resp
->count
;
1237 /* Resend as a stable write in order to avoid
1238 * headaches in the case of a server crash.
1240 argp
->stable
= NFS_FILE_SYNC
;
1242 rpc_restart_call(task
);
1245 if (time_before(complain
, jiffies
)) {
1247 "NFS: Server wrote zero bytes, expected %u.\n",
1249 complain
= jiffies
+ 300 * HZ
;
1251 /* Can't do anything about it except throw an error. */
1252 task
->tk_status
= -EIO
;
1258 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1259 void nfs_commit_release(void *wdata
)
1261 nfs_commit_free(wdata
);
1265 * Set up the argument/result storage required for the RPC call.
1267 static void nfs_commit_rpcsetup(struct list_head
*head
,
1268 struct nfs_write_data
*data
,
1271 struct nfs_page
*first
;
1272 struct inode
*inode
;
1275 /* Set up the RPC argument and reply structs
1276 * NB: take care not to mess about with data->commit et al. */
1278 list_splice_init(head
, &data
->pages
);
1279 first
= nfs_list_entry(data
->pages
.next
);
1280 inode
= first
->wb_context
->dentry
->d_inode
;
1282 data
->inode
= inode
;
1283 data
->cred
= first
->wb_context
->cred
;
1285 data
->args
.fh
= NFS_FH(data
->inode
);
1286 /* Note: we always request a commit of the entire inode */
1287 data
->args
.offset
= 0;
1288 data
->args
.count
= 0;
1289 data
->res
.count
= 0;
1290 data
->res
.fattr
= &data
->fattr
;
1291 data
->res
.verf
= &data
->verf
;
1292 nfs_fattr_init(&data
->fattr
);
1294 /* Set up the initial task struct. */
1295 flags
= (how
& FLUSH_SYNC
) ? 0 : RPC_TASK_ASYNC
;
1296 rpc_init_task(&data
->task
, NFS_CLIENT(inode
), flags
, &nfs_commit_ops
, data
);
1297 NFS_PROTO(inode
)->commit_setup(data
, how
);
1299 data
->task
.tk_priority
= flush_task_priority(how
);
1300 data
->task
.tk_cookie
= (unsigned long)inode
;
1302 dprintk("NFS: %4d initiated commit call\n", data
->task
.tk_pid
);
1306 * Commit dirty pages
1309 nfs_commit_list(struct inode
*inode
, struct list_head
*head
, int how
)
1311 struct nfs_write_data
*data
;
1312 struct nfs_page
*req
;
1314 data
= nfs_commit_alloc(NFS_SERVER(inode
)->wpages
);
1319 /* Set up the argument struct */
1320 nfs_commit_rpcsetup(head
, data
, how
);
1322 nfs_execute_write(data
);
1325 while (!list_empty(head
)) {
1326 req
= nfs_list_entry(head
->next
);
1327 nfs_list_remove_request(req
);
1328 nfs_mark_request_commit(req
);
1329 nfs_clear_page_writeback(req
);
1335 * COMMIT call returned
1337 static void nfs_commit_done(struct rpc_task
*task
, void *calldata
)
1339 struct nfs_write_data
*data
= calldata
;
1340 struct nfs_page
*req
;
1343 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1344 task
->tk_pid
, task
->tk_status
);
1346 /* Call the NFS version-specific code */
1347 if (NFS_PROTO(data
->inode
)->commit_done(task
, data
) != 0)
1350 while (!list_empty(&data
->pages
)) {
1351 req
= nfs_list_entry(data
->pages
.next
);
1352 nfs_list_remove_request(req
);
1354 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1355 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
1356 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
1358 (long long)req_offset(req
));
1359 if (task
->tk_status
< 0) {
1360 req
->wb_context
->error
= task
->tk_status
;
1361 nfs_inode_remove_request(req
);
1362 dprintk(", error = %d\n", task
->tk_status
);
1366 /* Okay, COMMIT succeeded, apparently. Check the verifier
1367 * returned by the server against all stored verfs. */
1368 if (!memcmp(req
->wb_verf
.verifier
, data
->verf
.verifier
, sizeof(data
->verf
.verifier
))) {
1369 /* We have a match */
1370 nfs_inode_remove_request(req
);
1374 /* We have a mismatch. Write the page again */
1375 dprintk(" mismatch\n");
1376 nfs_mark_request_dirty(req
);
1378 nfs_clear_page_writeback(req
);
1381 sub_page_state(nr_unstable
,res
);
1384 static const struct rpc_call_ops nfs_commit_ops
= {
1385 .rpc_call_done
= nfs_commit_done
,
1386 .rpc_release
= nfs_commit_release
,
1390 static int nfs_flush_inode(struct inode
*inode
, unsigned long idx_start
,
1391 unsigned int npages
, int how
)
1393 struct nfs_inode
*nfsi
= NFS_I(inode
);
1398 spin_lock(&nfsi
->req_lock
);
1399 res
= nfs_scan_dirty(inode
, &head
, idx_start
, npages
);
1400 spin_unlock(&nfsi
->req_lock
);
1402 struct nfs_server
*server
= NFS_SERVER(inode
);
1404 /* For single writes, FLUSH_STABLE is more efficient */
1405 if (res
== nfsi
->npages
&& nfsi
->npages
<= server
->wpages
) {
1406 if (res
> 1 || nfs_list_entry(head
.next
)->wb_bytes
<= server
->wsize
)
1407 how
|= FLUSH_STABLE
;
1409 error
= nfs_flush_list(&head
, server
->wpages
, how
);
1416 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1417 int nfs_commit_inode(struct inode
*inode
, int how
)
1419 struct nfs_inode
*nfsi
= NFS_I(inode
);
1424 spin_lock(&nfsi
->req_lock
);
1425 res
= nfs_scan_commit(inode
, &head
, 0, 0);
1426 spin_unlock(&nfsi
->req_lock
);
1428 error
= nfs_commit_list(inode
, &head
, how
);
1436 int nfs_sync_inode(struct inode
*inode
, unsigned long idx_start
,
1437 unsigned int npages
, int how
)
1439 int nocommit
= how
& FLUSH_NOCOMMIT
;
1440 int wait
= how
& FLUSH_WAIT
;
1443 how
&= ~(FLUSH_WAIT
|FLUSH_NOCOMMIT
);
1447 error
= nfs_wait_on_requests(inode
, idx_start
, npages
);
1451 error
= nfs_flush_inode(inode
, idx_start
, npages
, how
);
1455 error
= nfs_commit_inode(inode
, how
);
1456 } while (error
> 0);
1460 int nfs_init_writepagecache(void)
1462 nfs_wdata_cachep
= kmem_cache_create("nfs_write_data",
1463 sizeof(struct nfs_write_data
),
1464 0, SLAB_HWCACHE_ALIGN
,
1466 if (nfs_wdata_cachep
== NULL
)
1469 nfs_wdata_mempool
= mempool_create(MIN_POOL_WRITE
,
1473 if (nfs_wdata_mempool
== NULL
)
1476 nfs_commit_mempool
= mempool_create(MIN_POOL_COMMIT
,
1480 if (nfs_commit_mempool
== NULL
)
1486 void nfs_destroy_writepagecache(void)
1488 mempool_destroy(nfs_commit_mempool
);
1489 mempool_destroy(nfs_wdata_mempool
);
1490 if (kmem_cache_destroy(nfs_wdata_cachep
))
1491 printk(KERN_INFO
"nfs_write_data: not all structures were freed\n");