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 void nfs_writeback_done_partial(struct nfs_write_data
*, int);
81 static void nfs_writeback_done_full(struct nfs_write_data
*, int);
82 static int nfs_wait_on_write_congestion(struct address_space
*, int);
83 static int nfs_wait_on_requests(struct inode
*, unsigned long, unsigned int);
84 static int nfs_flush_inode(struct inode
*inode
, unsigned long idx_start
,
85 unsigned int npages
, int how
);
87 static kmem_cache_t
*nfs_wdata_cachep
;
88 mempool_t
*nfs_wdata_mempool
;
89 static mempool_t
*nfs_commit_mempool
;
91 static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion
);
93 static inline struct nfs_write_data
*nfs_commit_alloc(unsigned int pagecount
)
95 struct nfs_write_data
*p
= mempool_alloc(nfs_commit_mempool
, SLAB_NOFS
);
98 memset(p
, 0, sizeof(*p
));
99 INIT_LIST_HEAD(&p
->pages
);
100 if (pagecount
< NFS_PAGEVEC_SIZE
)
101 p
->pagevec
= &p
->page_array
[0];
103 size_t size
= ++pagecount
* sizeof(struct page
*);
104 p
->pagevec
= kzalloc(size
, GFP_NOFS
);
106 mempool_free(p
, nfs_commit_mempool
);
114 static inline void nfs_commit_free(struct nfs_write_data
*p
)
116 if (p
&& (p
->pagevec
!= &p
->page_array
[0]))
118 mempool_free(p
, nfs_commit_mempool
);
121 void nfs_writedata_release(void *wdata
)
123 nfs_writedata_free(wdata
);
126 /* Adjust the file length if we're writing beyond the end */
127 static void nfs_grow_file(struct page
*page
, unsigned int offset
, unsigned int count
)
129 struct inode
*inode
= page
->mapping
->host
;
130 loff_t end
, i_size
= i_size_read(inode
);
131 unsigned long end_index
= (i_size
- 1) >> PAGE_CACHE_SHIFT
;
133 if (i_size
> 0 && page
->index
< end_index
)
135 end
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + ((loff_t
)offset
+count
);
138 nfs_inc_stats(inode
, NFSIOS_EXTENDWRITE
);
139 i_size_write(inode
, end
);
142 /* We can set the PG_uptodate flag if we see that a write request
143 * covers the full page.
145 static void nfs_mark_uptodate(struct page
*page
, unsigned int base
, unsigned int count
)
149 if (PageUptodate(page
))
153 if (count
== PAGE_CACHE_SIZE
) {
154 SetPageUptodate(page
);
158 end_offs
= i_size_read(page
->mapping
->host
) - 1;
161 /* Is this the last page? */
162 if (page
->index
!= (unsigned long)(end_offs
>> PAGE_CACHE_SHIFT
))
164 /* This is the last page: set PG_uptodate if we cover the entire
165 * extent of the data, then zero the rest of the page.
167 if (count
== (unsigned int)(end_offs
& (PAGE_CACHE_SIZE
- 1)) + 1) {
168 memclear_highpage_flush(page
, count
, PAGE_CACHE_SIZE
- count
);
169 SetPageUptodate(page
);
174 * Write a page synchronously.
175 * Offset is the data offset within the page.
177 static int nfs_writepage_sync(struct nfs_open_context
*ctx
, struct inode
*inode
,
178 struct page
*page
, unsigned int offset
, unsigned int count
,
181 unsigned int wsize
= NFS_SERVER(inode
)->wsize
;
182 int result
, written
= 0;
183 struct nfs_write_data
*wdata
;
185 wdata
= nfs_writedata_alloc(1);
190 wdata
->cred
= ctx
->cred
;
191 wdata
->inode
= inode
;
192 wdata
->args
.fh
= NFS_FH(inode
);
193 wdata
->args
.context
= ctx
;
194 wdata
->args
.pages
= &page
;
195 wdata
->args
.stable
= NFS_FILE_SYNC
;
196 wdata
->args
.pgbase
= offset
;
197 wdata
->args
.count
= wsize
;
198 wdata
->res
.fattr
= &wdata
->fattr
;
199 wdata
->res
.verf
= &wdata
->verf
;
201 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
203 (long long)NFS_FILEID(inode
),
204 count
, (long long)(page_offset(page
) + offset
));
206 set_page_writeback(page
);
207 nfs_begin_data_update(inode
);
210 wdata
->args
.count
= count
;
211 wdata
->args
.offset
= page_offset(page
) + wdata
->args
.pgbase
;
213 result
= NFS_PROTO(inode
)->write(wdata
);
216 /* Must mark the page invalid after I/O error */
217 ClearPageUptodate(page
);
220 if (result
< wdata
->args
.count
)
221 printk(KERN_WARNING
"NFS: short write, count=%u, result=%d\n",
222 wdata
->args
.count
, result
);
224 wdata
->args
.offset
+= result
;
225 wdata
->args
.pgbase
+= result
;
228 nfs_add_stats(inode
, NFSIOS_SERVERWRITTENBYTES
, result
);
230 /* Update file length */
231 nfs_grow_file(page
, offset
, written
);
232 /* Set the PG_uptodate flag? */
233 nfs_mark_uptodate(page
, offset
, written
);
236 ClearPageError(page
);
239 nfs_end_data_update(inode
);
240 end_page_writeback(page
);
241 nfs_writedata_free(wdata
);
242 return written
? written
: result
;
245 static int nfs_writepage_async(struct nfs_open_context
*ctx
,
246 struct inode
*inode
, struct page
*page
,
247 unsigned int offset
, unsigned int count
)
249 struct nfs_page
*req
;
251 req
= nfs_update_request(ctx
, inode
, page
, offset
, count
);
254 /* Update file length */
255 nfs_grow_file(page
, offset
, count
);
256 /* Set the PG_uptodate flag? */
257 nfs_mark_uptodate(page
, offset
, count
);
258 nfs_unlock_request(req
);
262 static int wb_priority(struct writeback_control
*wbc
)
264 if (wbc
->for_reclaim
)
265 return FLUSH_HIGHPRI
;
266 if (wbc
->for_kupdate
)
272 * Write an mmapped page to the server.
274 int nfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
276 struct nfs_open_context
*ctx
;
277 struct inode
*inode
= page
->mapping
->host
;
278 unsigned long end_index
;
279 unsigned offset
= PAGE_CACHE_SIZE
;
280 loff_t i_size
= i_size_read(inode
);
281 int inode_referenced
= 0;
282 int priority
= wb_priority(wbc
);
285 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGE
);
286 nfs_add_stats(inode
, NFSIOS_WRITEPAGES
, 1);
289 * Note: We need to ensure that we have a reference to the inode
290 * if we are to do asynchronous writes. If not, waiting
291 * in nfs_wait_on_request() may deadlock with clear_inode().
293 * If igrab() fails here, then it is in any case safe to
294 * call nfs_wb_page(), since there will be no pending writes.
296 if (igrab(inode
) != 0)
297 inode_referenced
= 1;
298 end_index
= i_size
>> PAGE_CACHE_SHIFT
;
300 /* Ensure we've flushed out any previous writes */
301 nfs_wb_page_priority(inode
, page
, priority
);
304 if (page
->index
< end_index
)
306 /* things got complicated... */
307 offset
= i_size
& (PAGE_CACHE_SIZE
-1);
309 /* OK, are we completely out? */
310 err
= 0; /* potential race with truncate - ignore */
311 if (page
->index
>= end_index
+1 || !offset
)
314 ctx
= nfs_find_open_context(inode
, NULL
, FMODE_WRITE
);
320 if (!IS_SYNC(inode
) && inode_referenced
) {
321 err
= nfs_writepage_async(ctx
, inode
, page
, 0, offset
);
322 if (!wbc
->for_writepages
)
323 nfs_flush_inode(inode
, 0, 0, wb_priority(wbc
));
325 err
= nfs_writepage_sync(ctx
, inode
, page
, 0,
329 redirty_page_for_writepage(wbc
, page
);
334 put_nfs_open_context(ctx
);
337 if (inode_referenced
)
343 * Note: causes nfs_update_request() to block on the assumption
344 * that the writeback is generated due to memory pressure.
346 int nfs_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
348 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
349 struct inode
*inode
= mapping
->host
;
352 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGES
);
354 err
= generic_writepages(mapping
, wbc
);
357 while (test_and_set_bit(BDI_write_congested
, &bdi
->state
) != 0) {
358 if (wbc
->nonblocking
)
360 nfs_wait_on_write_congestion(mapping
, 0);
362 err
= nfs_flush_inode(inode
, 0, 0, wb_priority(wbc
));
365 nfs_add_stats(inode
, NFSIOS_WRITEPAGES
, err
);
366 wbc
->nr_to_write
-= err
;
367 if (!wbc
->nonblocking
&& wbc
->sync_mode
== WB_SYNC_ALL
) {
368 err
= nfs_wait_on_requests(inode
, 0, 0);
372 err
= nfs_commit_inode(inode
, wb_priority(wbc
));
374 wbc
->nr_to_write
-= err
;
378 clear_bit(BDI_write_congested
, &bdi
->state
);
379 wake_up_all(&nfs_write_congestion
);
384 * Insert a write request into an inode
386 static int nfs_inode_add_request(struct inode
*inode
, struct nfs_page
*req
)
388 struct nfs_inode
*nfsi
= NFS_I(inode
);
391 error
= radix_tree_insert(&nfsi
->nfs_page_tree
, req
->wb_index
, req
);
392 BUG_ON(error
== -EEXIST
);
397 nfs_begin_data_update(inode
);
398 if (nfs_have_delegation(inode
, FMODE_WRITE
))
402 atomic_inc(&req
->wb_count
);
407 * Insert a write request into an inode
409 static void nfs_inode_remove_request(struct nfs_page
*req
)
411 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
412 struct nfs_inode
*nfsi
= NFS_I(inode
);
414 BUG_ON (!NFS_WBACK_BUSY(req
));
416 spin_lock(&nfsi
->req_lock
);
417 radix_tree_delete(&nfsi
->nfs_page_tree
, req
->wb_index
);
420 spin_unlock(&nfsi
->req_lock
);
421 nfs_end_data_update(inode
);
424 spin_unlock(&nfsi
->req_lock
);
425 nfs_clear_request(req
);
426 nfs_release_request(req
);
432 static inline struct nfs_page
*
433 _nfs_find_request(struct inode
*inode
, unsigned long index
)
435 struct nfs_inode
*nfsi
= NFS_I(inode
);
436 struct nfs_page
*req
;
438 req
= (struct nfs_page
*)radix_tree_lookup(&nfsi
->nfs_page_tree
, index
);
440 atomic_inc(&req
->wb_count
);
444 static struct nfs_page
*
445 nfs_find_request(struct inode
*inode
, unsigned long index
)
447 struct nfs_page
*req
;
448 struct nfs_inode
*nfsi
= NFS_I(inode
);
450 spin_lock(&nfsi
->req_lock
);
451 req
= _nfs_find_request(inode
, index
);
452 spin_unlock(&nfsi
->req_lock
);
457 * Add a request to the inode's dirty list.
460 nfs_mark_request_dirty(struct nfs_page
*req
)
462 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
463 struct nfs_inode
*nfsi
= NFS_I(inode
);
465 spin_lock(&nfsi
->req_lock
);
466 radix_tree_tag_set(&nfsi
->nfs_page_tree
,
467 req
->wb_index
, NFS_PAGE_TAG_DIRTY
);
468 nfs_list_add_request(req
, &nfsi
->dirty
);
470 spin_unlock(&nfsi
->req_lock
);
471 inc_page_state(nr_dirty
);
472 mark_inode_dirty(inode
);
476 * Check if a request is dirty
479 nfs_dirty_request(struct nfs_page
*req
)
481 struct nfs_inode
*nfsi
= NFS_I(req
->wb_context
->dentry
->d_inode
);
482 return !list_empty(&req
->wb_list
) && req
->wb_list_head
== &nfsi
->dirty
;
485 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
487 * Add a request to the inode's commit list.
490 nfs_mark_request_commit(struct nfs_page
*req
)
492 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
493 struct nfs_inode
*nfsi
= NFS_I(inode
);
495 spin_lock(&nfsi
->req_lock
);
496 nfs_list_add_request(req
, &nfsi
->commit
);
498 spin_unlock(&nfsi
->req_lock
);
499 inc_page_state(nr_unstable
);
500 mark_inode_dirty(inode
);
505 * Wait for a request to complete.
507 * Interruptible by signals only if mounted with intr flag.
510 nfs_wait_on_requests(struct inode
*inode
, unsigned long idx_start
, unsigned int npages
)
512 struct nfs_inode
*nfsi
= NFS_I(inode
);
513 struct nfs_page
*req
;
514 unsigned long idx_end
, next
;
515 unsigned int res
= 0;
521 idx_end
= idx_start
+ npages
- 1;
523 spin_lock(&nfsi
->req_lock
);
525 while (radix_tree_gang_lookup_tag(&nfsi
->nfs_page_tree
, (void **)&req
, next
, 1, NFS_PAGE_TAG_WRITEBACK
)) {
526 if (req
->wb_index
> idx_end
)
529 next
= req
->wb_index
+ 1;
530 BUG_ON(!NFS_WBACK_BUSY(req
));
532 atomic_inc(&req
->wb_count
);
533 spin_unlock(&nfsi
->req_lock
);
534 error
= nfs_wait_on_request(req
);
535 nfs_release_request(req
);
538 spin_lock(&nfsi
->req_lock
);
541 spin_unlock(&nfsi
->req_lock
);
546 * nfs_scan_dirty - Scan an inode for dirty requests
547 * @inode: NFS inode to scan
548 * @dst: destination list
549 * @idx_start: lower bound of page->index to scan.
550 * @npages: idx_start + npages sets the upper bound to scan.
552 * Moves requests from the inode's dirty page list.
553 * The requests are *not* checked to ensure that they form a contiguous set.
556 nfs_scan_dirty(struct inode
*inode
, struct list_head
*dst
, unsigned long idx_start
, unsigned int npages
)
558 struct nfs_inode
*nfsi
= NFS_I(inode
);
561 if (nfsi
->ndirty
!= 0) {
562 res
= nfs_scan_lock_dirty(nfsi
, dst
, idx_start
, npages
);
564 sub_page_state(nr_dirty
,res
);
565 if ((nfsi
->ndirty
== 0) != list_empty(&nfsi
->dirty
))
566 printk(KERN_ERR
"NFS: desynchronized value of nfs_i.ndirty.\n");
571 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
573 * nfs_scan_commit - Scan an inode for commit requests
574 * @inode: NFS inode to scan
575 * @dst: destination list
576 * @idx_start: lower bound of page->index to scan.
577 * @npages: idx_start + npages sets the upper bound to scan.
579 * Moves requests from the inode's 'commit' request list.
580 * The requests are *not* checked to ensure that they form a contiguous set.
583 nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
, unsigned long idx_start
, unsigned int npages
)
585 struct nfs_inode
*nfsi
= NFS_I(inode
);
588 if (nfsi
->ncommit
!= 0) {
589 res
= nfs_scan_list(&nfsi
->commit
, dst
, idx_start
, npages
);
590 nfsi
->ncommit
-= res
;
591 if ((nfsi
->ncommit
== 0) != list_empty(&nfsi
->commit
))
592 printk(KERN_ERR
"NFS: desynchronized value of nfs_i.ncommit.\n");
598 static int nfs_wait_on_write_congestion(struct address_space
*mapping
, int intr
)
600 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
606 if (!bdi_write_congested(bdi
))
609 nfs_inc_stats(mapping
->host
, NFSIOS_CONGESTIONWAIT
);
612 struct rpc_clnt
*clnt
= NFS_CLIENT(mapping
->host
);
615 rpc_clnt_sigmask(clnt
, &oldset
);
616 prepare_to_wait(&nfs_write_congestion
, &wait
, TASK_INTERRUPTIBLE
);
617 if (bdi_write_congested(bdi
)) {
623 rpc_clnt_sigunmask(clnt
, &oldset
);
625 prepare_to_wait(&nfs_write_congestion
, &wait
, TASK_UNINTERRUPTIBLE
);
626 if (bdi_write_congested(bdi
))
629 finish_wait(&nfs_write_congestion
, &wait
);
635 * Try to update any existing write request, or create one if there is none.
636 * In order to match, the request's credentials must match those of
637 * the calling process.
639 * Note: Should always be called with the Page Lock held!
641 static struct nfs_page
* nfs_update_request(struct nfs_open_context
* ctx
,
642 struct inode
*inode
, struct page
*page
,
643 unsigned int offset
, unsigned int bytes
)
645 struct nfs_server
*server
= NFS_SERVER(inode
);
646 struct nfs_inode
*nfsi
= NFS_I(inode
);
647 struct nfs_page
*req
, *new = NULL
;
648 unsigned long rqend
, end
;
650 end
= offset
+ bytes
;
652 if (nfs_wait_on_write_congestion(page
->mapping
, server
->flags
& NFS_MOUNT_INTR
))
653 return ERR_PTR(-ERESTARTSYS
);
655 /* Loop over all inode entries and see if we find
656 * A request for the page we wish to update
658 spin_lock(&nfsi
->req_lock
);
659 req
= _nfs_find_request(inode
, page
->index
);
661 if (!nfs_lock_request_dontget(req
)) {
663 spin_unlock(&nfsi
->req_lock
);
664 error
= nfs_wait_on_request(req
);
665 nfs_release_request(req
);
668 nfs_release_request(new);
669 return ERR_PTR(error
);
673 spin_unlock(&nfsi
->req_lock
);
675 nfs_release_request(new);
681 nfs_lock_request_dontget(new);
682 error
= nfs_inode_add_request(inode
, new);
684 spin_unlock(&nfsi
->req_lock
);
685 nfs_unlock_request(new);
686 return ERR_PTR(error
);
688 spin_unlock(&nfsi
->req_lock
);
689 nfs_mark_request_dirty(new);
692 spin_unlock(&nfsi
->req_lock
);
694 new = nfs_create_request(ctx
, inode
, page
, offset
, bytes
);
699 /* We have a request for our page.
700 * If the creds don't match, or the
701 * page addresses don't match,
702 * tell the caller to wait on the conflicting
705 rqend
= req
->wb_offset
+ req
->wb_bytes
;
706 if (req
->wb_context
!= ctx
707 || req
->wb_page
!= page
708 || !nfs_dirty_request(req
)
709 || offset
> rqend
|| end
< req
->wb_offset
) {
710 nfs_unlock_request(req
);
711 return ERR_PTR(-EBUSY
);
714 /* Okay, the request matches. Update the region */
715 if (offset
< req
->wb_offset
) {
716 req
->wb_offset
= offset
;
717 req
->wb_pgbase
= offset
;
718 req
->wb_bytes
= rqend
- req
->wb_offset
;
722 req
->wb_bytes
= end
- req
->wb_offset
;
727 int nfs_flush_incompatible(struct file
*file
, struct page
*page
)
729 struct nfs_open_context
*ctx
= (struct nfs_open_context
*)file
->private_data
;
730 struct inode
*inode
= page
->mapping
->host
;
731 struct nfs_page
*req
;
734 * Look for a request corresponding to this page. If there
735 * is one, and it belongs to another file, we flush it out
736 * before we try to copy anything into the page. Do this
737 * due to the lack of an ACCESS-type call in NFSv2.
738 * Also do the same if we find a request from an existing
741 req
= nfs_find_request(inode
, page
->index
);
743 if (req
->wb_page
!= page
|| ctx
!= req
->wb_context
)
744 status
= nfs_wb_page(inode
, page
);
745 nfs_release_request(req
);
747 return (status
< 0) ? status
: 0;
751 * Update and possibly write a cached page of an NFS file.
753 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
754 * things with a page scheduled for an RPC call (e.g. invalidate it).
756 int nfs_updatepage(struct file
*file
, struct page
*page
,
757 unsigned int offset
, unsigned int count
)
759 struct nfs_open_context
*ctx
= (struct nfs_open_context
*)file
->private_data
;
760 struct inode
*inode
= page
->mapping
->host
;
761 struct nfs_page
*req
;
764 nfs_inc_stats(inode
, NFSIOS_VFSUPDATEPAGE
);
766 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
767 file
->f_dentry
->d_parent
->d_name
.name
,
768 file
->f_dentry
->d_name
.name
, count
,
769 (long long)(page_offset(page
) +offset
));
771 if (IS_SYNC(inode
)) {
772 status
= nfs_writepage_sync(ctx
, inode
, page
, offset
, count
, 0);
774 if (offset
== 0 && status
== PAGE_CACHE_SIZE
)
775 SetPageUptodate(page
);
781 /* If we're not using byte range locks, and we know the page
782 * is entirely in cache, it may be more efficient to avoid
783 * fragmenting write requests.
785 if (PageUptodate(page
) && inode
->i_flock
== NULL
&& !(file
->f_mode
& O_SYNC
)) {
786 loff_t end_offs
= i_size_read(inode
) - 1;
787 unsigned long end_index
= end_offs
>> PAGE_CACHE_SHIFT
;
791 if (unlikely(end_offs
< 0)) {
793 } else if (page
->index
== end_index
) {
795 pglen
= (unsigned int)(end_offs
& (PAGE_CACHE_SIZE
-1)) + 1;
798 } else if (page
->index
< end_index
)
799 count
= PAGE_CACHE_SIZE
;
803 * Try to find an NFS request corresponding to this page
805 * If the existing request cannot be updated, we must flush
809 req
= nfs_update_request(ctx
, inode
, page
, offset
, count
);
810 status
= (IS_ERR(req
)) ? PTR_ERR(req
) : 0;
811 if (status
!= -EBUSY
)
813 /* Request could not be updated. Flush it out and try again */
814 status
= nfs_wb_page(inode
, page
);
815 } while (status
>= 0);
821 /* Update file length */
822 nfs_grow_file(page
, offset
, count
);
823 /* Set the PG_uptodate flag? */
824 nfs_mark_uptodate(page
, req
->wb_pgbase
, req
->wb_bytes
);
825 nfs_unlock_request(req
);
827 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
828 status
, (long long)i_size_read(inode
));
830 ClearPageUptodate(page
);
834 static void nfs_writepage_release(struct nfs_page
*req
)
836 end_page_writeback(req
->wb_page
);
838 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
839 if (!PageError(req
->wb_page
)) {
840 if (NFS_NEED_RESCHED(req
)) {
841 nfs_mark_request_dirty(req
);
843 } else if (NFS_NEED_COMMIT(req
)) {
844 nfs_mark_request_commit(req
);
848 nfs_inode_remove_request(req
);
851 nfs_clear_commit(req
);
852 nfs_clear_reschedule(req
);
854 nfs_inode_remove_request(req
);
856 nfs_clear_page_writeback(req
);
859 static inline int flush_task_priority(int how
)
861 switch (how
& (FLUSH_HIGHPRI
|FLUSH_LOWPRI
)) {
863 return RPC_PRIORITY_HIGH
;
865 return RPC_PRIORITY_LOW
;
867 return RPC_PRIORITY_NORMAL
;
871 * Set up the argument/result storage required for the RPC call.
873 static void nfs_write_rpcsetup(struct nfs_page
*req
,
874 struct nfs_write_data
*data
,
875 unsigned int count
, unsigned int offset
,
880 /* Set up the RPC argument and reply structs
881 * NB: take care not to mess about with data->commit et al. */
884 data
->inode
= inode
= req
->wb_context
->dentry
->d_inode
;
885 data
->cred
= req
->wb_context
->cred
;
887 data
->args
.fh
= NFS_FH(inode
);
888 data
->args
.offset
= req_offset(req
) + offset
;
889 data
->args
.pgbase
= req
->wb_pgbase
+ offset
;
890 data
->args
.pages
= data
->pagevec
;
891 data
->args
.count
= count
;
892 data
->args
.context
= req
->wb_context
;
894 data
->res
.fattr
= &data
->fattr
;
895 data
->res
.count
= count
;
896 data
->res
.verf
= &data
->verf
;
897 nfs_fattr_init(&data
->fattr
);
899 NFS_PROTO(inode
)->write_setup(data
, how
);
901 data
->task
.tk_priority
= flush_task_priority(how
);
902 data
->task
.tk_cookie
= (unsigned long)inode
;
904 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
907 (long long)NFS_FILEID(inode
),
909 (unsigned long long)data
->args
.offset
);
912 static void nfs_execute_write(struct nfs_write_data
*data
)
914 struct rpc_clnt
*clnt
= NFS_CLIENT(data
->inode
);
917 rpc_clnt_sigmask(clnt
, &oldset
);
919 rpc_execute(&data
->task
);
921 rpc_clnt_sigunmask(clnt
, &oldset
);
925 * Generate multiple small requests to write out a single
926 * contiguous dirty area on one page.
928 static int nfs_flush_multi(struct list_head
*head
, struct inode
*inode
, int how
)
930 struct nfs_page
*req
= nfs_list_entry(head
->next
);
931 struct page
*page
= req
->wb_page
;
932 struct nfs_write_data
*data
;
933 unsigned int wsize
= NFS_SERVER(inode
)->wsize
;
934 unsigned int nbytes
, offset
;
938 nfs_list_remove_request(req
);
940 nbytes
= req
->wb_bytes
;
942 data
= nfs_writedata_alloc(1);
945 list_add(&data
->pages
, &list
);
951 atomic_set(&req
->wb_complete
, requests
);
953 ClearPageError(page
);
954 set_page_writeback(page
);
956 nbytes
= req
->wb_bytes
;
958 data
= list_entry(list
.next
, struct nfs_write_data
, pages
);
959 list_del_init(&data
->pages
);
961 data
->pagevec
[0] = page
;
962 data
->complete
= nfs_writeback_done_partial
;
964 if (nbytes
> wsize
) {
965 nfs_write_rpcsetup(req
, data
, wsize
, offset
, how
);
969 nfs_write_rpcsetup(req
, data
, nbytes
, offset
, how
);
972 nfs_execute_write(data
);
973 } while (nbytes
!= 0);
978 while (!list_empty(&list
)) {
979 data
= list_entry(list
.next
, struct nfs_write_data
, pages
);
980 list_del(&data
->pages
);
981 nfs_writedata_free(data
);
983 nfs_mark_request_dirty(req
);
984 nfs_clear_page_writeback(req
);
989 * Create an RPC task for the given write request and kick it.
990 * The page must have been locked by the caller.
992 * It may happen that the page we're passed is not marked dirty.
993 * This is the case if nfs_updatepage detects a conflicting request
994 * that has been written but not committed.
996 static int nfs_flush_one(struct list_head
*head
, struct inode
*inode
, int how
)
998 struct nfs_page
*req
;
1000 struct nfs_write_data
*data
;
1003 if (NFS_SERVER(inode
)->wsize
< PAGE_CACHE_SIZE
)
1004 return nfs_flush_multi(head
, inode
, how
);
1006 data
= nfs_writedata_alloc(NFS_SERVER(inode
)->wpages
);
1010 pages
= data
->pagevec
;
1012 while (!list_empty(head
)) {
1013 req
= nfs_list_entry(head
->next
);
1014 nfs_list_remove_request(req
);
1015 nfs_list_add_request(req
, &data
->pages
);
1016 ClearPageError(req
->wb_page
);
1017 set_page_writeback(req
->wb_page
);
1018 *pages
++ = req
->wb_page
;
1019 count
+= req
->wb_bytes
;
1021 req
= nfs_list_entry(data
->pages
.next
);
1023 data
->complete
= nfs_writeback_done_full
;
1024 /* Set up the argument struct */
1025 nfs_write_rpcsetup(req
, data
, count
, 0, how
);
1027 nfs_execute_write(data
);
1030 while (!list_empty(head
)) {
1031 struct nfs_page
*req
= nfs_list_entry(head
->next
);
1032 nfs_list_remove_request(req
);
1033 nfs_mark_request_dirty(req
);
1034 nfs_clear_page_writeback(req
);
1040 nfs_flush_list(struct list_head
*head
, int wpages
, int how
)
1042 LIST_HEAD(one_request
);
1043 struct nfs_page
*req
;
1045 unsigned int pages
= 0;
1047 while (!list_empty(head
)) {
1048 pages
+= nfs_coalesce_requests(head
, &one_request
, wpages
);
1049 req
= nfs_list_entry(one_request
.next
);
1050 error
= nfs_flush_one(&one_request
, req
->wb_context
->dentry
->d_inode
, how
);
1057 while (!list_empty(head
)) {
1058 req
= nfs_list_entry(head
->next
);
1059 nfs_list_remove_request(req
);
1060 nfs_mark_request_dirty(req
);
1061 nfs_clear_page_writeback(req
);
1067 * Handle a write reply that flushed part of a page.
1069 static void nfs_writeback_done_partial(struct nfs_write_data
*data
, int status
)
1071 struct nfs_page
*req
= data
->req
;
1072 struct page
*page
= req
->wb_page
;
1074 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1075 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
1076 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
1078 (long long)req_offset(req
));
1081 ClearPageUptodate(page
);
1083 req
->wb_context
->error
= status
;
1084 dprintk(", error = %d\n", status
);
1086 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1087 if (data
->verf
.committed
< NFS_FILE_SYNC
) {
1088 if (!NFS_NEED_COMMIT(req
)) {
1089 nfs_defer_commit(req
);
1090 memcpy(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
));
1091 dprintk(" defer commit\n");
1092 } else if (memcmp(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
))) {
1093 nfs_defer_reschedule(req
);
1094 dprintk(" server reboot detected\n");
1101 if (atomic_dec_and_test(&req
->wb_complete
))
1102 nfs_writepage_release(req
);
1106 * Handle a write reply that flushes a whole page.
1108 * FIXME: There is an inherent race with invalidate_inode_pages and
1109 * writebacks since the page->count is kept > 1 for as long
1110 * as the page has a write request pending.
1112 static void nfs_writeback_done_full(struct nfs_write_data
*data
, int status
)
1114 struct nfs_page
*req
;
1117 /* Update attributes as result of writeback. */
1118 while (!list_empty(&data
->pages
)) {
1119 req
= nfs_list_entry(data
->pages
.next
);
1120 nfs_list_remove_request(req
);
1121 page
= req
->wb_page
;
1123 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1124 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
1125 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
1127 (long long)req_offset(req
));
1130 ClearPageUptodate(page
);
1132 req
->wb_context
->error
= status
;
1133 end_page_writeback(page
);
1134 nfs_inode_remove_request(req
);
1135 dprintk(", error = %d\n", status
);
1138 end_page_writeback(page
);
1140 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1141 if (data
->args
.stable
!= NFS_UNSTABLE
|| data
->verf
.committed
== NFS_FILE_SYNC
) {
1142 nfs_inode_remove_request(req
);
1146 memcpy(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
));
1147 nfs_mark_request_commit(req
);
1148 dprintk(" marked for commit\n");
1150 nfs_inode_remove_request(req
);
1153 nfs_clear_page_writeback(req
);
1158 * This function is called when the WRITE call is complete.
1160 void nfs_writeback_done(struct rpc_task
*task
, void *calldata
)
1162 struct nfs_write_data
*data
= calldata
;
1163 struct nfs_writeargs
*argp
= &data
->args
;
1164 struct nfs_writeres
*resp
= &data
->res
;
1166 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1167 task
->tk_pid
, task
->tk_status
);
1169 nfs_add_stats(data
->inode
, NFSIOS_SERVERWRITTENBYTES
, resp
->count
);
1171 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1172 if (resp
->verf
->committed
< argp
->stable
&& task
->tk_status
>= 0) {
1173 /* We tried a write call, but the server did not
1174 * commit data to stable storage even though we
1176 * Note: There is a known bug in Tru64 < 5.0 in which
1177 * the server reports NFS_DATA_SYNC, but performs
1178 * NFS_FILE_SYNC. We therefore implement this checking
1179 * as a dprintk() in order to avoid filling syslog.
1181 static unsigned long complain
;
1183 if (time_before(complain
, jiffies
)) {
1184 dprintk("NFS: faulty NFS server %s:"
1185 " (committed = %d) != (stable = %d)\n",
1186 NFS_SERVER(data
->inode
)->hostname
,
1187 resp
->verf
->committed
, argp
->stable
);
1188 complain
= jiffies
+ 300 * HZ
;
1192 /* Is this a short write? */
1193 if (task
->tk_status
>= 0 && resp
->count
< argp
->count
) {
1194 static unsigned long complain
;
1196 nfs_inc_stats(data
->inode
, NFSIOS_SHORTWRITE
);
1198 /* Has the server at least made some progress? */
1199 if (resp
->count
!= 0) {
1200 /* Was this an NFSv2 write or an NFSv3 stable write? */
1201 if (resp
->verf
->committed
!= NFS_UNSTABLE
) {
1202 /* Resend from where the server left off */
1203 argp
->offset
+= resp
->count
;
1204 argp
->pgbase
+= resp
->count
;
1205 argp
->count
-= resp
->count
;
1207 /* Resend as a stable write in order to avoid
1208 * headaches in the case of a server crash.
1210 argp
->stable
= NFS_FILE_SYNC
;
1212 rpc_restart_call(task
);
1215 if (time_before(complain
, jiffies
)) {
1217 "NFS: Server wrote zero bytes, expected %u.\n",
1219 complain
= jiffies
+ 300 * HZ
;
1221 /* Can't do anything about it except throw an error. */
1222 task
->tk_status
= -EIO
;
1226 * Process the nfs_page list
1228 data
->complete(data
, task
->tk_status
);
1232 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1233 void nfs_commit_release(void *wdata
)
1235 nfs_commit_free(wdata
);
1239 * Set up the argument/result storage required for the RPC call.
1241 static void nfs_commit_rpcsetup(struct list_head
*head
,
1242 struct nfs_write_data
*data
, int how
)
1244 struct nfs_page
*first
;
1245 struct inode
*inode
;
1247 /* Set up the RPC argument and reply structs
1248 * NB: take care not to mess about with data->commit et al. */
1250 list_splice_init(head
, &data
->pages
);
1251 first
= nfs_list_entry(data
->pages
.next
);
1252 inode
= first
->wb_context
->dentry
->d_inode
;
1254 data
->inode
= inode
;
1255 data
->cred
= first
->wb_context
->cred
;
1257 data
->args
.fh
= NFS_FH(data
->inode
);
1258 /* Note: we always request a commit of the entire inode */
1259 data
->args
.offset
= 0;
1260 data
->args
.count
= 0;
1261 data
->res
.count
= 0;
1262 data
->res
.fattr
= &data
->fattr
;
1263 data
->res
.verf
= &data
->verf
;
1264 nfs_fattr_init(&data
->fattr
);
1266 NFS_PROTO(inode
)->commit_setup(data
, how
);
1268 data
->task
.tk_priority
= flush_task_priority(how
);
1269 data
->task
.tk_cookie
= (unsigned long)inode
;
1271 dprintk("NFS: %4d initiated commit call\n", data
->task
.tk_pid
);
1275 * Commit dirty pages
1278 nfs_commit_list(struct inode
*inode
, struct list_head
*head
, int how
)
1280 struct nfs_write_data
*data
;
1281 struct nfs_page
*req
;
1283 data
= nfs_commit_alloc(NFS_SERVER(inode
)->wpages
);
1288 /* Set up the argument struct */
1289 nfs_commit_rpcsetup(head
, data
, how
);
1291 nfs_execute_write(data
);
1294 while (!list_empty(head
)) {
1295 req
= nfs_list_entry(head
->next
);
1296 nfs_list_remove_request(req
);
1297 nfs_mark_request_commit(req
);
1298 nfs_clear_page_writeback(req
);
1304 * COMMIT call returned
1306 void nfs_commit_done(struct rpc_task
*task
, void *calldata
)
1308 struct nfs_write_data
*data
= calldata
;
1309 struct nfs_page
*req
;
1312 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1313 task
->tk_pid
, task
->tk_status
);
1315 while (!list_empty(&data
->pages
)) {
1316 req
= nfs_list_entry(data
->pages
.next
);
1317 nfs_list_remove_request(req
);
1319 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1320 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
1321 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
1323 (long long)req_offset(req
));
1324 if (task
->tk_status
< 0) {
1325 req
->wb_context
->error
= task
->tk_status
;
1326 nfs_inode_remove_request(req
);
1327 dprintk(", error = %d\n", task
->tk_status
);
1331 /* Okay, COMMIT succeeded, apparently. Check the verifier
1332 * returned by the server against all stored verfs. */
1333 if (!memcmp(req
->wb_verf
.verifier
, data
->verf
.verifier
, sizeof(data
->verf
.verifier
))) {
1334 /* We have a match */
1335 nfs_inode_remove_request(req
);
1339 /* We have a mismatch. Write the page again */
1340 dprintk(" mismatch\n");
1341 nfs_mark_request_dirty(req
);
1343 nfs_clear_page_writeback(req
);
1346 sub_page_state(nr_unstable
,res
);
1350 static int nfs_flush_inode(struct inode
*inode
, unsigned long idx_start
,
1351 unsigned int npages
, int how
)
1353 struct nfs_inode
*nfsi
= NFS_I(inode
);
1358 spin_lock(&nfsi
->req_lock
);
1359 res
= nfs_scan_dirty(inode
, &head
, idx_start
, npages
);
1360 spin_unlock(&nfsi
->req_lock
);
1362 struct nfs_server
*server
= NFS_SERVER(inode
);
1364 /* For single writes, FLUSH_STABLE is more efficient */
1365 if (res
== nfsi
->npages
&& nfsi
->npages
<= server
->wpages
) {
1366 if (res
> 1 || nfs_list_entry(head
.next
)->wb_bytes
<= server
->wsize
)
1367 how
|= FLUSH_STABLE
;
1369 error
= nfs_flush_list(&head
, server
->wpages
, how
);
1376 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1377 int nfs_commit_inode(struct inode
*inode
, int how
)
1379 struct nfs_inode
*nfsi
= NFS_I(inode
);
1384 spin_lock(&nfsi
->req_lock
);
1385 res
= nfs_scan_commit(inode
, &head
, 0, 0);
1386 spin_unlock(&nfsi
->req_lock
);
1388 error
= nfs_commit_list(inode
, &head
, how
);
1396 int nfs_sync_inode(struct inode
*inode
, unsigned long idx_start
,
1397 unsigned int npages
, int how
)
1399 int nocommit
= how
& FLUSH_NOCOMMIT
;
1400 int wait
= how
& FLUSH_WAIT
;
1403 how
&= ~(FLUSH_WAIT
|FLUSH_NOCOMMIT
);
1407 error
= nfs_wait_on_requests(inode
, idx_start
, npages
);
1411 error
= nfs_flush_inode(inode
, idx_start
, npages
, how
);
1415 error
= nfs_commit_inode(inode
, how
);
1416 } while (error
> 0);
1420 int nfs_init_writepagecache(void)
1422 nfs_wdata_cachep
= kmem_cache_create("nfs_write_data",
1423 sizeof(struct nfs_write_data
),
1424 0, SLAB_HWCACHE_ALIGN
,
1426 if (nfs_wdata_cachep
== NULL
)
1429 nfs_wdata_mempool
= mempool_create(MIN_POOL_WRITE
,
1433 if (nfs_wdata_mempool
== NULL
)
1436 nfs_commit_mempool
= mempool_create(MIN_POOL_COMMIT
,
1440 if (nfs_commit_mempool
== NULL
)
1446 void nfs_destroy_writepagecache(void)
1448 mempool_destroy(nfs_commit_mempool
);
1449 mempool_destroy(nfs_wdata_mempool
);
1450 if (kmem_cache_destroy(nfs_wdata_cachep
))
1451 printk(KERN_INFO
"nfs_write_data: not all structures were freed\n");