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>
64 #include <linux/mempool.h>
66 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
68 #define MIN_POOL_WRITE (32)
69 #define MIN_POOL_COMMIT (4)
72 * Local function declarations
74 static struct nfs_page
* nfs_update_request(struct file
*, struct inode
*,
76 unsigned int, unsigned int);
77 static void nfs_strategy(struct inode
*inode
);
79 static kmem_cache_t
*nfs_wdata_cachep
;
80 static mempool_t
*nfs_wdata_mempool
;
81 static mempool_t
*nfs_commit_mempool
;
83 static __inline__
struct nfs_write_data
*nfs_writedata_alloc(void)
85 struct nfs_write_data
*p
;
86 p
= (struct nfs_write_data
*)mempool_alloc(nfs_wdata_mempool
, SLAB_NOFS
);
88 memset(p
, 0, sizeof(*p
));
89 INIT_LIST_HEAD(&p
->pages
);
94 static __inline__
void nfs_writedata_free(struct nfs_write_data
*p
)
96 mempool_free(p
, nfs_wdata_mempool
);
99 void nfs_writedata_release(struct rpc_task
*task
)
101 struct nfs_write_data
*wdata
= (struct nfs_write_data
*)task
->tk_calldata
;
102 nfs_writedata_free(wdata
);
105 static __inline__
struct nfs_write_data
*nfs_commit_alloc(void)
107 struct nfs_write_data
*p
;
108 p
= (struct nfs_write_data
*)mempool_alloc(nfs_commit_mempool
, SLAB_NOFS
);
110 memset(p
, 0, sizeof(*p
));
111 INIT_LIST_HEAD(&p
->pages
);
116 static __inline__
void nfs_commit_free(struct nfs_write_data
*p
)
118 mempool_free(p
, nfs_commit_mempool
);
121 void nfs_commit_release(struct rpc_task
*task
)
123 struct nfs_write_data
*wdata
= (struct nfs_write_data
*)task
->tk_calldata
;
124 nfs_commit_free(wdata
);
128 * Write a page synchronously.
129 * Offset is the data offset within the page.
132 nfs_writepage_sync(struct file
*file
, struct inode
*inode
, struct page
*page
,
133 unsigned int offset
, unsigned int count
)
135 struct rpc_cred
*cred
= NULL
;
137 unsigned int wsize
= NFS_SERVER(inode
)->wsize
;
138 int result
, refresh
= 0, written
= 0, flags
;
140 struct nfs_fattr fattr
;
141 struct nfs_writeverf verf
;
145 cred
= get_rpccred(nfs_file_cred(file
));
147 cred
= get_rpccred(NFS_I(inode
)->mm_cred
);
149 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
151 (long long)NFS_FILEID(inode
),
152 count
, (long long)(page_offset(page
) + offset
));
154 base
= page_offset(page
) + offset
;
156 flags
= ((IS_SWAPFILE(inode
)) ? NFS_RW_SWAP
: 0) | NFS_RW_SYNC
;
159 if (count
< wsize
&& !IS_SWAPFILE(inode
))
162 result
= NFS_PROTO(inode
)->write(inode
, cred
, &fattr
, flags
,
163 offset
, wsize
, page
, &verf
);
166 /* Must mark the page invalid after I/O error */
167 ClearPageUptodate(page
);
171 printk("NFS: short write, wsize=%u, result=%d\n",
180 * If we've extended the file, update the inode
181 * now so we don't invalidate the cache.
183 if (base
> inode
->i_size
)
184 inode
->i_size
= base
;
188 ClearPageError(page
);
194 return written
? written
: result
;
198 nfs_writepage_async(struct file
*file
, struct inode
*inode
, struct page
*page
,
199 unsigned int offset
, unsigned int count
)
201 struct nfs_page
*req
;
205 req
= nfs_update_request(file
, inode
, page
, offset
, count
);
206 status
= (IS_ERR(req
)) ? PTR_ERR(req
) : 0;
210 req
->wb_cred
= get_rpccred(NFS_I(inode
)->mm_cred
);
211 nfs_unlock_request(req
);
213 end
= ((loff_t
)page
->index
<<PAGE_CACHE_SHIFT
) + (loff_t
)(offset
+ count
);
214 if (inode
->i_size
< end
)
222 * Write an mmapped page to the server.
225 nfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
227 struct inode
*inode
= page
->mapping
->host
;
228 unsigned long end_index
;
229 unsigned offset
= PAGE_CACHE_SIZE
;
232 end_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
234 /* Ensure we've flushed out any previous writes */
235 nfs_wb_page(inode
,page
);
238 if (page
->index
< end_index
)
240 /* things got complicated... */
241 offset
= inode
->i_size
& (PAGE_CACHE_SIZE
-1);
243 /* OK, are we completely out? */
245 if (page
->index
>= end_index
+1 || !offset
)
249 if (NFS_SERVER(inode
)->wsize
>= PAGE_CACHE_SIZE
&& !IS_SYNC(inode
)) {
250 err
= nfs_writepage_async(NULL
, inode
, page
, 0, offset
);
254 err
= nfs_writepage_sync(NULL
, inode
, page
, 0, offset
);
265 nfs_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
267 struct inode
*inode
= mapping
->host
;
268 int is_sync
= !wbc
->nonblocking
;
271 err
= generic_writepages(mapping
, wbc
);
274 err
= nfs_flush_file(inode
, NULL
, 0, 0, 0);
277 if (wbc
->sync_mode
== WB_SYNC_HOLD
)
279 if (is_sync
&& wbc
->sync_mode
== WB_SYNC_ALL
) {
280 err
= nfs_wb_all(inode
);
282 nfs_commit_file(inode
, NULL
, 0, 0, 0);
288 * Insert a write request into an inode
291 nfs_inode_add_request(struct inode
*inode
, struct nfs_page
*req
)
293 struct nfs_inode
*nfsi
= NFS_I(inode
);
296 error
= radix_tree_insert(&nfsi
->nfs_page_tree
, req
->wb_index
, req
);
297 BUG_ON(error
== -EEXIST
);
308 * Insert a write request into an inode
311 nfs_inode_remove_request(struct nfs_page
*req
)
313 struct nfs_inode
*nfsi
;
316 BUG_ON (!NFS_WBACK_BUSY(req
));
317 spin_lock(&nfs_wreq_lock
);
318 inode
= req
->wb_inode
;
320 radix_tree_delete(&nfsi
->nfs_page_tree
, req
->wb_index
);
323 spin_unlock(&nfs_wreq_lock
);
326 spin_unlock(&nfs_wreq_lock
);
327 nfs_clear_request(req
);
328 nfs_release_request(req
);
334 static inline struct nfs_page
*
335 _nfs_find_request(struct inode
*inode
, unsigned long index
)
337 struct nfs_inode
*nfsi
= NFS_I(inode
);
338 struct nfs_page
*req
;
340 req
= (struct nfs_page
*)radix_tree_lookup(&nfsi
->nfs_page_tree
, index
);
346 static struct nfs_page
*
347 nfs_find_request(struct inode
*inode
, unsigned long index
)
349 struct nfs_page
*req
;
351 spin_lock(&nfs_wreq_lock
);
352 req
= _nfs_find_request(inode
, index
);
353 spin_unlock(&nfs_wreq_lock
);
358 * Add a request to the inode's dirty list.
361 nfs_mark_request_dirty(struct nfs_page
*req
)
363 struct inode
*inode
= req
->wb_inode
;
364 struct nfs_inode
*nfsi
= NFS_I(inode
);
366 spin_lock(&nfs_wreq_lock
);
367 nfs_list_add_request(req
, &nfsi
->dirty
);
369 spin_unlock(&nfs_wreq_lock
);
370 inc_page_state(nr_dirty
);
371 mark_inode_dirty(inode
);
375 * Check if a request is dirty
378 nfs_dirty_request(struct nfs_page
*req
)
380 struct nfs_inode
*nfsi
= NFS_I(req
->wb_inode
);
381 return !list_empty(&req
->wb_list
) && req
->wb_list_head
== &nfsi
->dirty
;
384 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
386 * Add a request to the inode's commit list.
389 nfs_mark_request_commit(struct nfs_page
*req
)
391 struct inode
*inode
= req
->wb_inode
;
392 struct nfs_inode
*nfsi
= NFS_I(inode
);
394 spin_lock(&nfs_wreq_lock
);
395 nfs_list_add_request(req
, &nfsi
->commit
);
397 spin_unlock(&nfs_wreq_lock
);
398 inc_page_state(nr_unstable
);
399 mark_inode_dirty(inode
);
404 * Wait for a request to complete.
406 * Interruptible by signals only if mounted with intr flag.
409 nfs_wait_on_requests(struct inode
*inode
, struct file
*file
, unsigned long idx_start
, unsigned int npages
)
411 struct nfs_inode
*nfsi
= NFS_I(inode
);
412 struct nfs_page
*req
;
413 unsigned long idx_end
, next
;
414 unsigned int res
= 0;
420 idx_end
= idx_start
+ npages
- 1;
422 spin_lock(&nfs_wreq_lock
);
424 while (radix_tree_gang_lookup(&nfsi
->nfs_page_tree
, (void **)&req
, next
, 1)) {
425 if (req
->wb_index
> idx_end
)
428 next
= req
->wb_index
+ 1;
429 if (file
&& req
->wb_file
!= file
)
431 if (!NFS_WBACK_BUSY(req
))
435 spin_unlock(&nfs_wreq_lock
);
436 error
= nfs_wait_on_request(req
);
437 nfs_release_request(req
);
440 spin_lock(&nfs_wreq_lock
);
444 spin_unlock(&nfs_wreq_lock
);
449 * nfs_scan_dirty - Scan an inode for dirty requests
450 * @inode: NFS inode to scan
451 * @dst: destination list
452 * @file: if set, ensure we match requests from this file
453 * @idx_start: lower bound of page->index to scan.
454 * @npages: idx_start + npages sets the upper bound to scan.
456 * Moves requests from the inode's dirty page list.
457 * The requests are *not* checked to ensure that they form a contiguous set.
460 nfs_scan_dirty(struct inode
*inode
, struct list_head
*dst
, struct file
*file
, unsigned long idx_start
, unsigned int npages
)
462 struct nfs_inode
*nfsi
= NFS_I(inode
);
464 res
= nfs_scan_list(&nfsi
->dirty
, dst
, file
, idx_start
, npages
);
466 sub_page_state(nr_dirty
,res
);
467 if ((nfsi
->ndirty
== 0) != list_empty(&nfsi
->dirty
))
468 printk(KERN_ERR
"NFS: desynchronized value of nfs_i.ndirty.\n");
472 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
474 * nfs_scan_commit - Scan an inode for commit requests
475 * @inode: NFS inode to scan
476 * @dst: destination list
477 * @file: if set, ensure we collect requests from this file only.
478 * @idx_start: lower bound of page->index to scan.
479 * @npages: idx_start + npages sets the upper bound to scan.
481 * Moves requests from the inode's 'commit' request list.
482 * The requests are *not* checked to ensure that they form a contiguous set.
485 nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
, struct file
*file
, unsigned long idx_start
, unsigned int npages
)
487 struct nfs_inode
*nfsi
= NFS_I(inode
);
489 res
= nfs_scan_list(&nfsi
->commit
, dst
, file
, idx_start
, npages
);
490 nfsi
->ncommit
-= res
;
491 if ((nfsi
->ncommit
== 0) != list_empty(&nfsi
->commit
))
492 printk(KERN_ERR
"NFS: desynchronized value of nfs_i.ncommit.\n");
499 * Try to update any existing write request, or create one if there is none.
500 * In order to match, the request's credentials must match those of
501 * the calling process.
503 * Note: Should always be called with the Page Lock held!
505 static struct nfs_page
*
506 nfs_update_request(struct file
* file
, struct inode
*inode
, struct page
*page
,
507 unsigned int offset
, unsigned int bytes
)
509 struct nfs_page
*req
, *new = NULL
;
510 unsigned long rqend
, end
;
512 end
= offset
+ bytes
;
515 /* Loop over all inode entries and see if we find
516 * A request for the page we wish to update
518 spin_lock(&nfs_wreq_lock
);
519 req
= _nfs_find_request(inode
, page
->index
);
521 if (!nfs_lock_request_dontget(req
)) {
523 spin_unlock(&nfs_wreq_lock
);
524 error
= nfs_wait_on_request(req
);
525 nfs_release_request(req
);
527 return ERR_PTR(error
);
530 spin_unlock(&nfs_wreq_lock
);
532 nfs_release_request(new);
538 nfs_lock_request_dontget(new);
539 error
= nfs_inode_add_request(inode
, new);
541 spin_unlock(&nfs_wreq_lock
);
542 nfs_unlock_request(new);
543 return ERR_PTR(error
);
545 spin_unlock(&nfs_wreq_lock
);
546 nfs_mark_request_dirty(new);
549 spin_unlock(&nfs_wreq_lock
);
551 new = nfs_create_request(nfs_file_cred(file
), inode
, page
, offset
, bytes
);
560 /* We have a request for our page.
561 * If the creds don't match, or the
562 * page addresses don't match,
563 * tell the caller to wait on the conflicting
566 rqend
= req
->wb_offset
+ req
->wb_bytes
;
567 if (req
->wb_file
!= file
568 || req
->wb_page
!= page
569 || !nfs_dirty_request(req
)
570 || offset
> rqend
|| end
< req
->wb_offset
) {
571 nfs_unlock_request(req
);
572 return ERR_PTR(-EBUSY
);
575 /* Okay, the request matches. Update the region */
576 if (offset
< req
->wb_offset
) {
577 req
->wb_offset
= offset
;
578 req
->wb_pgbase
= offset
;
579 req
->wb_bytes
= rqend
- req
->wb_offset
;
583 req
->wb_bytes
= end
- req
->wb_offset
;
589 * This is the strategy routine for NFS.
590 * It is called by nfs_updatepage whenever the user wrote up to the end
593 * We always try to submit a set of requests in parallel so that the
594 * server's write code can gather writes. This is mainly for the benefit
597 * We never submit more requests than we think the remote can handle.
598 * For UDP sockets, we make sure we don't exceed the congestion window;
599 * for TCP, we limit the number of requests to 8.
601 * NFS_STRATEGY_PAGES gives the minimum number of requests for NFSv2 that
602 * should be sent out in one go. This is for the benefit of NFSv2 servers
603 * that perform write gathering.
605 * FIXME: Different servers may have different sweet spots.
606 * Record the average congestion window in server struct?
608 #define NFS_STRATEGY_PAGES 8
610 nfs_strategy(struct inode
*inode
)
612 unsigned int dirty
, wpages
;
614 dirty
= NFS_I(inode
)->ndirty
;
615 wpages
= NFS_SERVER(inode
)->wpages
;
616 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
617 if (NFS_PROTO(inode
)->version
== 2) {
618 if (dirty
>= NFS_STRATEGY_PAGES
* wpages
)
619 nfs_flush_file(inode
, NULL
, 0, 0, 0);
620 } else if (dirty
>= wpages
)
621 nfs_flush_file(inode
, NULL
, 0, 0, 0);
623 if (dirty
>= NFS_STRATEGY_PAGES
* wpages
)
624 nfs_flush_file(inode
, NULL
, 0, 0, 0);
629 nfs_flush_incompatible(struct file
*file
, struct page
*page
)
631 struct inode
*inode
= page
->mapping
->host
;
632 struct rpc_cred
*cred
= nfs_file_cred(file
);
633 struct nfs_page
*req
;
636 * Look for a request corresponding to this page. If there
637 * is one, and it belongs to another file, we flush it out
638 * before we try to copy anything into the page. Do this
639 * due to the lack of an ACCESS-type call in NFSv2.
640 * Also do the same if we find a request from an existing
643 req
= nfs_find_request(inode
, page
->index
);
645 if (req
->wb_file
!= file
|| req
->wb_cred
!= cred
|| req
->wb_page
!= page
)
646 status
= nfs_wb_page(inode
, page
);
647 nfs_release_request(req
);
649 return (status
< 0) ? status
: 0;
653 * Update and possibly write a cached page of an NFS file.
655 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
656 * things with a page scheduled for an RPC call (e.g. invalidate it).
659 nfs_updatepage(struct file
*file
, struct page
*page
, unsigned int offset
, unsigned int count
)
661 struct dentry
*dentry
= file
->f_dentry
;
662 struct inode
*inode
= page
->mapping
->host
;
663 struct nfs_page
*req
;
667 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
668 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
,
669 count
, (long long)(page_offset(page
) +offset
));
672 * If wsize is smaller than page size, update and write
673 * page synchronously.
675 if (NFS_SERVER(inode
)->wsize
< PAGE_CACHE_SIZE
|| IS_SYNC(inode
)) {
676 status
= nfs_writepage_sync(file
, inode
, page
, offset
, count
);
678 if (offset
== 0 && status
== PAGE_CACHE_SIZE
)
679 SetPageUptodate(page
);
686 * Try to find an NFS request corresponding to this page
688 * If the existing request cannot be updated, we must flush
692 req
= nfs_update_request(file
, inode
, page
, offset
, count
);
693 status
= (IS_ERR(req
)) ? PTR_ERR(req
) : 0;
694 if (status
!= -EBUSY
)
696 /* Request could not be updated. Flush it out and try again */
697 status
= nfs_wb_page(inode
, page
);
698 } while (status
>= 0);
703 end
= ((loff_t
)page
->index
<<PAGE_CACHE_SHIFT
) + (loff_t
)(offset
+ count
);
704 if (inode
->i_size
< end
)
707 /* If we wrote past the end of the page.
708 * Call the strategy routine so it can send out a bunch
711 if (req
->wb_pgbase
== 0 && req
->wb_bytes
== PAGE_CACHE_SIZE
) {
712 SetPageUptodate(page
);
713 nfs_unlock_request(req
);
716 nfs_unlock_request(req
);
718 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
719 status
, (long long)inode
->i_size
);
721 ClearPageUptodate(page
);
726 * Set up the argument/result storage required for the RPC call.
729 nfs_write_rpcsetup(struct list_head
*head
, struct nfs_write_data
*data
, int how
)
731 struct rpc_task
*task
= &data
->task
;
733 struct nfs_page
*req
;
737 /* Set up the RPC argument and reply structs
738 * NB: take care not to mess about with data->commit et al. */
740 pages
= data
->pagevec
;
742 while (!list_empty(head
)) {
743 req
= nfs_list_entry(head
->next
);
744 nfs_list_remove_request(req
);
745 nfs_list_add_request(req
, &data
->pages
);
746 SetPageWriteback(req
->wb_page
);
747 *pages
++ = req
->wb_page
;
748 count
+= req
->wb_bytes
;
750 req
= nfs_list_entry(data
->pages
.next
);
751 data
->inode
= inode
= req
->wb_inode
;
752 data
->cred
= req
->wb_cred
;
754 NFS_PROTO(inode
)->write_setup(data
, count
, how
);
756 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
759 (long long)NFS_FILEID(inode
),
761 (unsigned long long)req_offset(req
));
765 * Create an RPC task for the given write request and kick it.
766 * The page must have been locked by the caller.
768 * It may happen that the page we're passed is not marked dirty.
769 * This is the case if nfs_updatepage detects a conflicting request
770 * that has been written but not committed.
773 nfs_flush_one(struct list_head
*head
, struct inode
*inode
, int how
)
775 struct rpc_clnt
*clnt
= NFS_CLIENT(inode
);
776 struct nfs_write_data
*data
;
779 data
= nfs_writedata_alloc();
783 /* Set up the argument struct */
784 nfs_write_rpcsetup(head
, data
, how
);
786 rpc_clnt_sigmask(clnt
, &oldset
);
788 rpc_execute(&data
->task
);
790 rpc_clnt_sigunmask(clnt
, &oldset
);
793 while (!list_empty(head
)) {
794 struct nfs_page
*req
= nfs_list_entry(head
->next
);
795 nfs_list_remove_request(req
);
796 nfs_mark_request_dirty(req
);
797 nfs_unlock_request(req
);
803 nfs_flush_list(struct list_head
*head
, int wpages
, int how
)
805 LIST_HEAD(one_request
);
806 struct nfs_page
*req
;
808 unsigned int pages
= 0;
810 while (!list_empty(head
)) {
811 pages
+= nfs_coalesce_requests(head
, &one_request
, wpages
);
812 req
= nfs_list_entry(one_request
.next
);
813 error
= nfs_flush_one(&one_request
, req
->wb_inode
, how
);
820 while (!list_empty(head
)) {
821 req
= nfs_list_entry(head
->next
);
822 nfs_list_remove_request(req
);
823 nfs_mark_request_dirty(req
);
824 nfs_unlock_request(req
);
831 * This function is called when the WRITE call is complete.
834 nfs_writeback_done(struct rpc_task
*task
)
836 struct nfs_write_data
*data
= (struct nfs_write_data
*) task
->tk_calldata
;
837 struct nfs_writeargs
*argp
= &data
->args
;
838 struct nfs_writeres
*resp
= &data
->res
;
839 struct nfs_page
*req
;
842 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
843 task
->tk_pid
, task
->tk_status
);
845 /* We can't handle that yet but we check for it nevertheless */
846 if (resp
->count
< argp
->count
&& task
->tk_status
>= 0) {
847 static unsigned long complain
;
848 if (time_before(complain
, jiffies
)) {
850 "NFS: Server wrote less than requested.\n");
851 complain
= jiffies
+ 300 * HZ
;
853 /* Can't do anything about it right now except throw
855 task
->tk_status
= -EIO
;
857 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
858 if (data
->verf
.committed
< argp
->stable
&& task
->tk_status
>= 0) {
859 /* We tried a write call, but the server did not
860 * commit data to stable storage even though we
862 * Note: There is a known bug in Tru64 < 5.0 in which
863 * the server reports NFS_DATA_SYNC, but performs
864 * NFS_FILE_SYNC. We therefore implement this checking
865 * as a dprintk() in order to avoid filling syslog.
867 static unsigned long complain
;
869 if (time_before(complain
, jiffies
)) {
870 dprintk("NFS: faulty NFS server %s:"
871 " (committed = %d) != (stable = %d)\n",
872 NFS_SERVER(data
->inode
)->hostname
,
873 data
->verf
.committed
, argp
->stable
);
874 complain
= jiffies
+ 300 * HZ
;
880 * Update attributes as result of writeback.
881 * FIXME: There is an inherent race with invalidate_inode_pages and
882 * writebacks since the page->count is kept > 1 for as long
883 * as the page has a write request pending.
885 while (!list_empty(&data
->pages
)) {
886 req
= nfs_list_entry(data
->pages
.next
);
887 nfs_list_remove_request(req
);
890 dprintk("NFS: write (%s/%Ld %d@%Ld)",
891 req
->wb_inode
->i_sb
->s_id
,
892 (long long)NFS_FILEID(req
->wb_inode
),
894 (long long)req_offset(req
));
896 if (task
->tk_status
< 0) {
897 ClearPageUptodate(page
);
900 req
->wb_file
->f_error
= task
->tk_status
;
901 end_page_writeback(page
);
902 nfs_inode_remove_request(req
);
903 dprintk(", error = %d\n", task
->tk_status
);
906 end_page_writeback(page
);
908 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
909 if (argp
->stable
!= NFS_UNSTABLE
|| data
->verf
.committed
== NFS_FILE_SYNC
) {
910 nfs_inode_remove_request(req
);
914 memcpy(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
));
915 nfs_mark_request_commit(req
);
916 dprintk(" marked for commit\n");
918 nfs_inode_remove_request(req
);
921 nfs_unlock_request(req
);
926 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
928 * Set up the argument/result storage required for the RPC call.
931 nfs_commit_rpcsetup(struct list_head
*head
, struct nfs_write_data
*data
, int how
)
933 struct rpc_task
*task
= &data
->task
;
934 struct nfs_page
*first
, *last
;
936 loff_t start
, end
, len
;
938 /* Set up the RPC argument and reply structs
939 * NB: take care not to mess about with data->commit et al. */
941 list_splice_init(head
, &data
->pages
);
942 first
= nfs_list_entry(data
->pages
.next
);
943 last
= nfs_list_entry(data
->pages
.prev
);
944 inode
= first
->wb_inode
;
947 * Determine the offset range of requests in the COMMIT call.
948 * We rely on the fact that data->pages is an ordered list...
950 start
= req_offset(first
);
951 end
= req_offset(last
) + last
->wb_bytes
;
953 /* If 'len' is not a 32-bit quantity, pass '0' in the COMMIT call */
954 if (end
>= inode
->i_size
|| len
< 0 || len
> (~((u32
)0) >> 1))
958 data
->cred
= first
->wb_cred
;
960 NFS_PROTO(inode
)->commit_setup(data
, start
, len
, how
);
962 dprintk("NFS: %4d initiated commit call\n", task
->tk_pid
);
969 nfs_commit_list(struct list_head
*head
, int how
)
971 struct rpc_clnt
*clnt
;
972 struct nfs_write_data
*data
;
973 struct nfs_page
*req
;
976 data
= nfs_commit_alloc();
981 /* Set up the argument struct */
982 nfs_commit_rpcsetup(head
, data
, how
);
983 clnt
= NFS_CLIENT(data
->inode
);
985 rpc_clnt_sigmask(clnt
, &oldset
);
987 rpc_execute(&data
->task
);
989 rpc_clnt_sigunmask(clnt
, &oldset
);
992 while (!list_empty(head
)) {
993 req
= nfs_list_entry(head
->next
);
994 nfs_list_remove_request(req
);
995 nfs_mark_request_commit(req
);
996 nfs_unlock_request(req
);
1002 * COMMIT call returned
1005 nfs_commit_done(struct rpc_task
*task
)
1007 struct nfs_write_data
*data
= (struct nfs_write_data
*)task
->tk_calldata
;
1008 struct nfs_page
*req
;
1011 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1012 task
->tk_pid
, task
->tk_status
);
1014 while (!list_empty(&data
->pages
)) {
1015 req
= nfs_list_entry(data
->pages
.next
);
1016 nfs_list_remove_request(req
);
1018 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1019 req
->wb_inode
->i_sb
->s_id
,
1020 (long long)NFS_FILEID(req
->wb_inode
),
1022 (long long)req_offset(req
));
1023 if (task
->tk_status
< 0) {
1025 req
->wb_file
->f_error
= task
->tk_status
;
1026 nfs_inode_remove_request(req
);
1027 dprintk(", error = %d\n", task
->tk_status
);
1031 /* Okay, COMMIT succeeded, apparently. Check the verifier
1032 * returned by the server against all stored verfs. */
1033 if (!memcmp(req
->wb_verf
.verifier
, data
->verf
.verifier
, sizeof(data
->verf
.verifier
))) {
1034 /* We have a match */
1035 nfs_inode_remove_request(req
);
1039 /* We have a mismatch. Write the page again */
1040 dprintk(" mismatch\n");
1041 nfs_mark_request_dirty(req
);
1043 nfs_unlock_request(req
);
1046 sub_page_state(nr_unstable
,res
);
1050 int nfs_flush_file(struct inode
*inode
, struct file
*file
, unsigned long idx_start
,
1051 unsigned int npages
, int how
)
1057 spin_lock(&nfs_wreq_lock
);
1058 res
= nfs_scan_dirty(inode
, &head
, file
, idx_start
, npages
);
1059 spin_unlock(&nfs_wreq_lock
);
1061 error
= nfs_flush_list(&head
, NFS_SERVER(inode
)->wpages
, how
);
1067 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1068 int nfs_commit_file(struct inode
*inode
, struct file
*file
, unsigned long idx_start
,
1069 unsigned int npages
, int how
)
1075 spin_lock(&nfs_wreq_lock
);
1076 res
= nfs_scan_commit(inode
, &head
, file
, idx_start
, npages
);
1077 spin_unlock(&nfs_wreq_lock
);
1079 error
= nfs_commit_list(&head
, how
);
1086 int nfs_sync_file(struct inode
*inode
, struct file
*file
, unsigned long idx_start
,
1087 unsigned int npages
, int how
)
1092 wait
= how
& FLUSH_WAIT
;
1096 inode
= file
->f_dentry
->d_inode
;
1101 error
= nfs_wait_on_requests(inode
, file
, idx_start
, npages
);
1103 error
= nfs_flush_file(inode
, file
, idx_start
, npages
, how
);
1104 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1106 error
= nfs_commit_file(inode
, file
, idx_start
, npages
, how
);
1108 } while (error
> 0);
1112 int nfs_init_writepagecache(void)
1114 nfs_wdata_cachep
= kmem_cache_create("nfs_write_data",
1115 sizeof(struct nfs_write_data
),
1116 0, SLAB_HWCACHE_ALIGN
,
1118 if (nfs_wdata_cachep
== NULL
)
1121 nfs_wdata_mempool
= mempool_create(MIN_POOL_WRITE
,
1125 if (nfs_wdata_mempool
== NULL
)
1128 nfs_commit_mempool
= mempool_create(MIN_POOL_COMMIT
,
1132 if (nfs_commit_mempool
== NULL
)
1138 void nfs_destroy_writepagecache(void)
1140 mempool_destroy(nfs_commit_mempool
);
1141 mempool_destroy(nfs_wdata_mempool
);
1142 if (kmem_cache_destroy(nfs_wdata_cachep
))
1143 printk(KERN_INFO
"nfs_write_data: not all structures were freed\n");