4 * Write file data over NFS.
6 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
9 #include <linux/types.h>
10 #include <linux/slab.h>
12 #include <linux/pagemap.h>
13 #include <linux/file.h>
14 #include <linux/writeback.h>
15 #include <linux/swap.h>
17 #include <linux/sunrpc/clnt.h>
18 #include <linux/nfs_fs.h>
19 #include <linux/nfs_mount.h>
20 #include <linux/nfs_page.h>
21 #include <linux/backing-dev.h>
23 #include <asm/uaccess.h>
24 #include <linux/smp_lock.h>
26 #include "delegation.h"
30 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
32 #define MIN_POOL_WRITE (32)
33 #define MIN_POOL_COMMIT (4)
36 * Local function declarations
38 static struct nfs_page
* nfs_update_request(struct nfs_open_context
*,
40 unsigned int, unsigned int);
41 static void nfs_mark_request_dirty(struct nfs_page
*req
);
42 static long nfs_flush_mapping(struct address_space
*mapping
, struct writeback_control
*wbc
, int how
);
43 static const struct rpc_call_ops nfs_write_partial_ops
;
44 static const struct rpc_call_ops nfs_write_full_ops
;
45 static const struct rpc_call_ops nfs_commit_ops
;
47 static struct kmem_cache
*nfs_wdata_cachep
;
48 static mempool_t
*nfs_wdata_mempool
;
49 static mempool_t
*nfs_commit_mempool
;
51 struct nfs_write_data
*nfs_commit_alloc(void)
53 struct nfs_write_data
*p
= mempool_alloc(nfs_commit_mempool
, GFP_NOFS
);
56 memset(p
, 0, sizeof(*p
));
57 INIT_LIST_HEAD(&p
->pages
);
62 void nfs_commit_rcu_free(struct rcu_head
*head
)
64 struct nfs_write_data
*p
= container_of(head
, struct nfs_write_data
, task
.u
.tk_rcu
);
65 if (p
&& (p
->pagevec
!= &p
->page_array
[0]))
67 mempool_free(p
, nfs_commit_mempool
);
70 void nfs_commit_free(struct nfs_write_data
*wdata
)
72 call_rcu_bh(&wdata
->task
.u
.tk_rcu
, nfs_commit_rcu_free
);
75 struct nfs_write_data
*nfs_writedata_alloc(size_t len
)
77 unsigned int pagecount
= (len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
78 struct nfs_write_data
*p
= mempool_alloc(nfs_wdata_mempool
, GFP_NOFS
);
81 memset(p
, 0, sizeof(*p
));
82 INIT_LIST_HEAD(&p
->pages
);
83 p
->npages
= pagecount
;
84 if (pagecount
<= ARRAY_SIZE(p
->page_array
))
85 p
->pagevec
= p
->page_array
;
87 p
->pagevec
= kcalloc(pagecount
, sizeof(struct page
*), GFP_NOFS
);
89 mempool_free(p
, nfs_wdata_mempool
);
97 static void nfs_writedata_rcu_free(struct rcu_head
*head
)
99 struct nfs_write_data
*p
= container_of(head
, struct nfs_write_data
, task
.u
.tk_rcu
);
100 if (p
&& (p
->pagevec
!= &p
->page_array
[0]))
102 mempool_free(p
, nfs_wdata_mempool
);
105 static void nfs_writedata_free(struct nfs_write_data
*wdata
)
107 call_rcu_bh(&wdata
->task
.u
.tk_rcu
, nfs_writedata_rcu_free
);
110 void nfs_writedata_release(void *wdata
)
112 nfs_writedata_free(wdata
);
115 static struct nfs_page
*nfs_page_find_request_locked(struct page
*page
)
117 struct nfs_page
*req
= NULL
;
119 if (PagePrivate(page
)) {
120 req
= (struct nfs_page
*)page_private(page
);
122 atomic_inc(&req
->wb_count
);
127 static struct nfs_page
*nfs_page_find_request(struct page
*page
)
129 struct nfs_page
*req
= NULL
;
130 spinlock_t
*req_lock
= &NFS_I(page
->mapping
->host
)->req_lock
;
133 req
= nfs_page_find_request_locked(page
);
134 spin_unlock(req_lock
);
138 /* Adjust the file length if we're writing beyond the end */
139 static void nfs_grow_file(struct page
*page
, unsigned int offset
, unsigned int count
)
141 struct inode
*inode
= page
->mapping
->host
;
142 loff_t end
, i_size
= i_size_read(inode
);
143 unsigned long end_index
= (i_size
- 1) >> PAGE_CACHE_SHIFT
;
145 if (i_size
> 0 && page
->index
< end_index
)
147 end
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + ((loff_t
)offset
+count
);
150 nfs_inc_stats(inode
, NFSIOS_EXTENDWRITE
);
151 i_size_write(inode
, end
);
154 /* A writeback failed: mark the page as bad, and invalidate the page cache */
155 static void nfs_set_pageerror(struct page
*page
)
158 nfs_zap_mapping(page
->mapping
->host
, page
->mapping
);
161 /* We can set the PG_uptodate flag if we see that a write request
162 * covers the full page.
164 static void nfs_mark_uptodate(struct page
*page
, unsigned int base
, unsigned int count
)
166 if (PageUptodate(page
))
170 if (count
!= nfs_page_length(page
))
172 if (count
!= PAGE_CACHE_SIZE
)
173 memclear_highpage_flush(page
, count
, PAGE_CACHE_SIZE
- count
);
174 SetPageUptodate(page
);
177 static int nfs_writepage_setup(struct nfs_open_context
*ctx
, struct page
*page
,
178 unsigned int offset
, unsigned int count
)
180 struct nfs_page
*req
;
184 req
= nfs_update_request(ctx
, page
, offset
, count
);
190 ret
= nfs_wb_page(page
->mapping
->host
, page
);
194 /* Update file length */
195 nfs_grow_file(page
, offset
, count
);
196 /* Set the PG_uptodate flag? */
197 nfs_mark_uptodate(page
, offset
, count
);
198 nfs_unlock_request(req
);
202 static int wb_priority(struct writeback_control
*wbc
)
204 if (wbc
->for_reclaim
)
205 return FLUSH_HIGHPRI
;
206 if (wbc
->for_kupdate
)
212 * NFS congestion control
215 int nfs_congestion_kb
;
217 #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
218 #define NFS_CONGESTION_OFF_THRESH \
219 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
221 static int nfs_set_page_writeback(struct page
*page
)
223 int ret
= test_set_page_writeback(page
);
226 struct inode
*inode
= page
->mapping
->host
;
227 struct nfs_server
*nfss
= NFS_SERVER(inode
);
229 if (atomic_inc_return(&nfss
->writeback
) >
230 NFS_CONGESTION_ON_THRESH
)
231 set_bdi_congested(&nfss
->backing_dev_info
, WRITE
);
236 static void nfs_end_page_writeback(struct page
*page
)
238 struct inode
*inode
= page
->mapping
->host
;
239 struct nfs_server
*nfss
= NFS_SERVER(inode
);
241 end_page_writeback(page
);
242 if (atomic_dec_return(&nfss
->writeback
) < NFS_CONGESTION_OFF_THRESH
) {
243 clear_bdi_congested(&nfss
->backing_dev_info
, WRITE
);
244 congestion_end(WRITE
);
249 * Find an associated nfs write request, and prepare to flush it out
250 * Returns 1 if there was no write request, or if the request was
251 * already tagged by nfs_set_page_dirty.Returns 0 if the request
253 * May also return an error if the user signalled nfs_wait_on_request().
255 static int nfs_page_mark_flush(struct page
*page
)
257 struct nfs_page
*req
;
258 spinlock_t
*req_lock
= &NFS_I(page
->mapping
->host
)->req_lock
;
263 req
= nfs_page_find_request_locked(page
);
265 spin_unlock(req_lock
);
268 if (nfs_lock_request_dontget(req
))
270 /* Note: If we hold the page lock, as is the case in nfs_writepage,
271 * then the call to nfs_lock_request_dontget() will always
272 * succeed provided that someone hasn't already marked the
273 * request as dirty (in which case we don't care).
275 spin_unlock(req_lock
);
276 ret
= nfs_wait_on_request(req
);
277 nfs_release_request(req
);
282 spin_unlock(req_lock
);
283 if (nfs_set_page_writeback(page
) == 0) {
284 nfs_list_remove_request(req
);
285 nfs_mark_request_dirty(req
);
287 ret
= test_bit(PG_NEED_FLUSH
, &req
->wb_flags
);
288 nfs_unlock_request(req
);
293 * Write an mmapped page to the server.
295 static int nfs_writepage_locked(struct page
*page
, struct writeback_control
*wbc
)
297 struct nfs_open_context
*ctx
;
298 struct inode
*inode
= page
->mapping
->host
;
302 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGE
);
303 nfs_add_stats(inode
, NFSIOS_WRITEPAGES
, 1);
305 err
= nfs_page_mark_flush(page
);
309 offset
= nfs_page_length(page
);
313 ctx
= nfs_find_open_context(inode
, NULL
, FMODE_WRITE
);
318 err
= nfs_writepage_setup(ctx
, page
, 0, offset
);
319 put_nfs_open_context(ctx
);
322 err
= nfs_page_mark_flush(page
);
326 if (!wbc
->for_writepages
)
327 nfs_flush_mapping(page
->mapping
, wbc
, FLUSH_STABLE
|wb_priority(wbc
));
331 int nfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
335 err
= nfs_writepage_locked(page
, wbc
);
340 int nfs_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
342 struct inode
*inode
= mapping
->host
;
345 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGES
);
347 err
= generic_writepages(mapping
, wbc
);
350 err
= nfs_flush_mapping(mapping
, wbc
, wb_priority(wbc
));
353 nfs_add_stats(inode
, NFSIOS_WRITEPAGES
, err
);
360 * Insert a write request into an inode
362 static int nfs_inode_add_request(struct inode
*inode
, struct nfs_page
*req
)
364 struct nfs_inode
*nfsi
= NFS_I(inode
);
367 error
= radix_tree_insert(&nfsi
->nfs_page_tree
, req
->wb_index
, req
);
368 BUG_ON(error
== -EEXIST
);
373 nfs_begin_data_update(inode
);
374 if (nfs_have_delegation(inode
, FMODE_WRITE
))
377 SetPagePrivate(req
->wb_page
);
378 set_page_private(req
->wb_page
, (unsigned long)req
);
380 atomic_inc(&req
->wb_count
);
385 * Remove a write request from an inode
387 static void nfs_inode_remove_request(struct nfs_page
*req
)
389 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
390 struct nfs_inode
*nfsi
= NFS_I(inode
);
392 BUG_ON (!NFS_WBACK_BUSY(req
));
394 spin_lock(&nfsi
->req_lock
);
395 set_page_private(req
->wb_page
, 0);
396 ClearPagePrivate(req
->wb_page
);
397 radix_tree_delete(&nfsi
->nfs_page_tree
, req
->wb_index
);
400 spin_unlock(&nfsi
->req_lock
);
401 nfs_end_data_update(inode
);
404 spin_unlock(&nfsi
->req_lock
);
405 nfs_clear_request(req
);
406 nfs_release_request(req
);
410 * Add a request to the inode's dirty list.
413 nfs_mark_request_dirty(struct nfs_page
*req
)
415 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
416 struct nfs_inode
*nfsi
= NFS_I(inode
);
418 spin_lock(&nfsi
->req_lock
);
419 radix_tree_tag_set(&nfsi
->nfs_page_tree
,
420 req
->wb_index
, NFS_PAGE_TAG_DIRTY
);
421 nfs_list_add_request(req
, &nfsi
->dirty
);
423 spin_unlock(&nfsi
->req_lock
);
424 __mark_inode_dirty(inode
, I_DIRTY_PAGES
);
428 nfs_redirty_request(struct nfs_page
*req
)
430 __set_page_dirty_nobuffers(req
->wb_page
);
434 * Check if a request is dirty
437 nfs_dirty_request(struct nfs_page
*req
)
439 struct page
*page
= req
->wb_page
;
443 return !PageWriteback(req
->wb_page
);
446 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
448 * Add a request to the inode's commit list.
451 nfs_mark_request_commit(struct nfs_page
*req
)
453 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
454 struct nfs_inode
*nfsi
= NFS_I(inode
);
456 spin_lock(&nfsi
->req_lock
);
457 nfs_list_add_request(req
, &nfsi
->commit
);
459 spin_unlock(&nfsi
->req_lock
);
460 inc_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
461 __mark_inode_dirty(inode
, I_DIRTY_DATASYNC
);
466 * Wait for a request to complete.
468 * Interruptible by signals only if mounted with intr flag.
470 static int nfs_wait_on_requests_locked(struct inode
*inode
, unsigned long idx_start
, unsigned int npages
)
472 struct nfs_inode
*nfsi
= NFS_I(inode
);
473 struct nfs_page
*req
;
474 unsigned long idx_end
, next
;
475 unsigned int res
= 0;
481 idx_end
= idx_start
+ npages
- 1;
484 while (radix_tree_gang_lookup_tag(&nfsi
->nfs_page_tree
, (void **)&req
, next
, 1, NFS_PAGE_TAG_WRITEBACK
)) {
485 if (req
->wb_index
> idx_end
)
488 next
= req
->wb_index
+ 1;
489 BUG_ON(!NFS_WBACK_BUSY(req
));
491 atomic_inc(&req
->wb_count
);
492 spin_unlock(&nfsi
->req_lock
);
493 error
= nfs_wait_on_request(req
);
494 nfs_release_request(req
);
495 spin_lock(&nfsi
->req_lock
);
503 static void nfs_cancel_dirty_list(struct list_head
*head
)
505 struct nfs_page
*req
;
506 while(!list_empty(head
)) {
507 req
= nfs_list_entry(head
->next
);
508 nfs_list_remove_request(req
);
509 nfs_end_page_writeback(req
->wb_page
);
510 nfs_inode_remove_request(req
);
511 nfs_clear_page_writeback(req
);
515 static void nfs_cancel_commit_list(struct list_head
*head
)
517 struct nfs_page
*req
;
519 while(!list_empty(head
)) {
520 req
= nfs_list_entry(head
->next
);
521 dec_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
522 nfs_list_remove_request(req
);
523 nfs_inode_remove_request(req
);
524 nfs_unlock_request(req
);
528 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
530 * nfs_scan_commit - Scan an inode for commit requests
531 * @inode: NFS inode to scan
532 * @dst: destination list
533 * @idx_start: lower bound of page->index to scan.
534 * @npages: idx_start + npages sets the upper bound to scan.
536 * Moves requests from the inode's 'commit' request list.
537 * The requests are *not* checked to ensure that they form a contiguous set.
540 nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
, unsigned long idx_start
, unsigned int npages
)
542 struct nfs_inode
*nfsi
= NFS_I(inode
);
545 if (nfsi
->ncommit
!= 0) {
546 res
= nfs_scan_list(nfsi
, &nfsi
->commit
, dst
, idx_start
, npages
);
547 nfsi
->ncommit
-= res
;
548 if ((nfsi
->ncommit
== 0) != list_empty(&nfsi
->commit
))
549 printk(KERN_ERR
"NFS: desynchronized value of nfs_i.ncommit.\n");
554 static inline int nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
, unsigned long idx_start
, unsigned int npages
)
560 static int nfs_wait_on_write_congestion(struct address_space
*mapping
)
562 struct inode
*inode
= mapping
->host
;
563 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
568 if (!bdi_write_congested(bdi
))
571 nfs_inc_stats(inode
, NFSIOS_CONGESTIONWAIT
);
574 struct rpc_clnt
*clnt
= NFS_CLIENT(inode
);
577 rpc_clnt_sigmask(clnt
, &oldset
);
578 ret
= congestion_wait_interruptible(WRITE
, HZ
/10);
579 rpc_clnt_sigunmask(clnt
, &oldset
);
580 if (ret
== -ERESTARTSYS
)
583 } while (bdi_write_congested(bdi
));
589 * Try to update any existing write request, or create one if there is none.
590 * In order to match, the request's credentials must match those of
591 * the calling process.
593 * Note: Should always be called with the Page Lock held!
595 static struct nfs_page
* nfs_update_request(struct nfs_open_context
* ctx
,
596 struct page
*page
, unsigned int offset
, unsigned int bytes
)
598 struct address_space
*mapping
= page
->mapping
;
599 struct inode
*inode
= mapping
->host
;
600 struct nfs_inode
*nfsi
= NFS_I(inode
);
601 struct nfs_page
*req
, *new = NULL
;
602 unsigned long rqend
, end
;
604 end
= offset
+ bytes
;
606 if (nfs_wait_on_write_congestion(mapping
))
607 return ERR_PTR(-ERESTARTSYS
);
609 /* Loop over all inode entries and see if we find
610 * A request for the page we wish to update
612 spin_lock(&nfsi
->req_lock
);
613 req
= nfs_page_find_request_locked(page
);
615 if (!nfs_lock_request_dontget(req
)) {
618 spin_unlock(&nfsi
->req_lock
);
619 error
= nfs_wait_on_request(req
);
620 nfs_release_request(req
);
623 nfs_release_request(new);
624 return ERR_PTR(error
);
628 spin_unlock(&nfsi
->req_lock
);
630 nfs_release_request(new);
636 nfs_lock_request_dontget(new);
637 error
= nfs_inode_add_request(inode
, new);
639 spin_unlock(&nfsi
->req_lock
);
640 nfs_unlock_request(new);
641 return ERR_PTR(error
);
643 spin_unlock(&nfsi
->req_lock
);
646 spin_unlock(&nfsi
->req_lock
);
648 new = nfs_create_request(ctx
, inode
, page
, offset
, bytes
);
653 /* We have a request for our page.
654 * If the creds don't match, or the
655 * page addresses don't match,
656 * tell the caller to wait on the conflicting
659 rqend
= req
->wb_offset
+ req
->wb_bytes
;
660 if (req
->wb_context
!= ctx
661 || req
->wb_page
!= page
662 || !nfs_dirty_request(req
)
663 || offset
> rqend
|| end
< req
->wb_offset
) {
664 nfs_unlock_request(req
);
665 return ERR_PTR(-EBUSY
);
668 /* Okay, the request matches. Update the region */
669 if (offset
< req
->wb_offset
) {
670 req
->wb_offset
= offset
;
671 req
->wb_pgbase
= offset
;
672 req
->wb_bytes
= rqend
- req
->wb_offset
;
676 req
->wb_bytes
= end
- req
->wb_offset
;
681 int nfs_flush_incompatible(struct file
*file
, struct page
*page
)
683 struct nfs_open_context
*ctx
= (struct nfs_open_context
*)file
->private_data
;
684 struct nfs_page
*req
;
685 int do_flush
, status
;
687 * Look for a request corresponding to this page. If there
688 * is one, and it belongs to another file, we flush it out
689 * before we try to copy anything into the page. Do this
690 * due to the lack of an ACCESS-type call in NFSv2.
691 * Also do the same if we find a request from an existing
695 req
= nfs_page_find_request(page
);
698 do_flush
= req
->wb_page
!= page
|| req
->wb_context
!= ctx
699 || !nfs_dirty_request(req
);
700 nfs_release_request(req
);
703 status
= nfs_wb_page(page
->mapping
->host
, page
);
704 } while (status
== 0);
709 * Update and possibly write a cached page of an NFS file.
711 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
712 * things with a page scheduled for an RPC call (e.g. invalidate it).
714 int nfs_updatepage(struct file
*file
, struct page
*page
,
715 unsigned int offset
, unsigned int count
)
717 struct nfs_open_context
*ctx
= (struct nfs_open_context
*)file
->private_data
;
718 struct inode
*inode
= page
->mapping
->host
;
721 nfs_inc_stats(inode
, NFSIOS_VFSUPDATEPAGE
);
723 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
724 file
->f_path
.dentry
->d_parent
->d_name
.name
,
725 file
->f_path
.dentry
->d_name
.name
, count
,
726 (long long)(page_offset(page
) +offset
));
728 /* If we're not using byte range locks, and we know the page
729 * is entirely in cache, it may be more efficient to avoid
730 * fragmenting write requests.
732 if (PageUptodate(page
) && inode
->i_flock
== NULL
&& !(file
->f_mode
& O_SYNC
)) {
733 count
= max(count
+ offset
, nfs_page_length(page
));
737 status
= nfs_writepage_setup(ctx
, page
, offset
, count
);
738 __set_page_dirty_nobuffers(page
);
740 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
741 status
, (long long)i_size_read(inode
));
743 nfs_set_pageerror(page
);
747 static void nfs_writepage_release(struct nfs_page
*req
)
749 nfs_end_page_writeback(req
->wb_page
);
751 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
752 if (!PageError(req
->wb_page
)) {
753 if (NFS_NEED_RESCHED(req
)) {
754 nfs_redirty_request(req
);
756 } else if (NFS_NEED_COMMIT(req
)) {
757 nfs_mark_request_commit(req
);
761 nfs_inode_remove_request(req
);
764 nfs_clear_commit(req
);
765 nfs_clear_reschedule(req
);
767 nfs_inode_remove_request(req
);
769 nfs_clear_page_writeback(req
);
772 static inline int flush_task_priority(int how
)
774 switch (how
& (FLUSH_HIGHPRI
|FLUSH_LOWPRI
)) {
776 return RPC_PRIORITY_HIGH
;
778 return RPC_PRIORITY_LOW
;
780 return RPC_PRIORITY_NORMAL
;
784 * Set up the argument/result storage required for the RPC call.
786 static void nfs_write_rpcsetup(struct nfs_page
*req
,
787 struct nfs_write_data
*data
,
788 const struct rpc_call_ops
*call_ops
,
789 unsigned int count
, unsigned int offset
,
795 /* Set up the RPC argument and reply structs
796 * NB: take care not to mess about with data->commit et al. */
799 data
->inode
= inode
= req
->wb_context
->dentry
->d_inode
;
800 data
->cred
= req
->wb_context
->cred
;
802 data
->args
.fh
= NFS_FH(inode
);
803 data
->args
.offset
= req_offset(req
) + offset
;
804 data
->args
.pgbase
= req
->wb_pgbase
+ offset
;
805 data
->args
.pages
= data
->pagevec
;
806 data
->args
.count
= count
;
807 data
->args
.context
= req
->wb_context
;
809 data
->res
.fattr
= &data
->fattr
;
810 data
->res
.count
= count
;
811 data
->res
.verf
= &data
->verf
;
812 nfs_fattr_init(&data
->fattr
);
814 /* Set up the initial task struct. */
815 flags
= (how
& FLUSH_SYNC
) ? 0 : RPC_TASK_ASYNC
;
816 rpc_init_task(&data
->task
, NFS_CLIENT(inode
), flags
, call_ops
, data
);
817 NFS_PROTO(inode
)->write_setup(data
, how
);
819 data
->task
.tk_priority
= flush_task_priority(how
);
820 data
->task
.tk_cookie
= (unsigned long)inode
;
822 dprintk("NFS: %5u initiated write call "
823 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
826 (long long)NFS_FILEID(inode
),
828 (unsigned long long)data
->args
.offset
);
831 static void nfs_execute_write(struct nfs_write_data
*data
)
833 struct rpc_clnt
*clnt
= NFS_CLIENT(data
->inode
);
836 rpc_clnt_sigmask(clnt
, &oldset
);
837 rpc_execute(&data
->task
);
838 rpc_clnt_sigunmask(clnt
, &oldset
);
842 * Generate multiple small requests to write out a single
843 * contiguous dirty area on one page.
845 static int nfs_flush_multi(struct inode
*inode
, struct list_head
*head
, int how
)
847 struct nfs_page
*req
= nfs_list_entry(head
->next
);
848 struct page
*page
= req
->wb_page
;
849 struct nfs_write_data
*data
;
850 size_t wsize
= NFS_SERVER(inode
)->wsize
, nbytes
;
855 nfs_list_remove_request(req
);
857 nbytes
= req
->wb_bytes
;
859 size_t len
= min(nbytes
, wsize
);
861 data
= nfs_writedata_alloc(len
);
864 list_add(&data
->pages
, &list
);
867 } while (nbytes
!= 0);
868 atomic_set(&req
->wb_complete
, requests
);
870 ClearPageError(page
);
872 nbytes
= req
->wb_bytes
;
874 data
= list_entry(list
.next
, struct nfs_write_data
, pages
);
875 list_del_init(&data
->pages
);
877 data
->pagevec
[0] = page
;
879 if (nbytes
> wsize
) {
880 nfs_write_rpcsetup(req
, data
, &nfs_write_partial_ops
,
885 nfs_write_rpcsetup(req
, data
, &nfs_write_partial_ops
,
886 nbytes
, offset
, how
);
889 nfs_execute_write(data
);
890 } while (nbytes
!= 0);
895 while (!list_empty(&list
)) {
896 data
= list_entry(list
.next
, struct nfs_write_data
, pages
);
897 list_del(&data
->pages
);
898 nfs_writedata_release(data
);
900 nfs_end_page_writeback(req
->wb_page
);
901 nfs_redirty_request(req
);
902 nfs_clear_page_writeback(req
);
907 * Create an RPC task for the given write request and kick it.
908 * The page must have been locked by the caller.
910 * It may happen that the page we're passed is not marked dirty.
911 * This is the case if nfs_updatepage detects a conflicting request
912 * that has been written but not committed.
914 static int nfs_flush_one(struct inode
*inode
, struct list_head
*head
, int how
)
916 struct nfs_page
*req
;
918 struct nfs_write_data
*data
;
921 data
= nfs_writedata_alloc(NFS_SERVER(inode
)->wsize
);
925 pages
= data
->pagevec
;
927 while (!list_empty(head
)) {
928 req
= nfs_list_entry(head
->next
);
929 nfs_list_remove_request(req
);
930 nfs_list_add_request(req
, &data
->pages
);
931 ClearPageError(req
->wb_page
);
932 *pages
++ = req
->wb_page
;
933 count
+= req
->wb_bytes
;
935 req
= nfs_list_entry(data
->pages
.next
);
937 /* Set up the argument struct */
938 nfs_write_rpcsetup(req
, data
, &nfs_write_full_ops
, count
, 0, how
);
940 nfs_execute_write(data
);
943 while (!list_empty(head
)) {
944 struct nfs_page
*req
= nfs_list_entry(head
->next
);
945 nfs_list_remove_request(req
);
946 nfs_end_page_writeback(req
->wb_page
);
947 nfs_redirty_request(req
);
948 nfs_clear_page_writeback(req
);
953 static int nfs_flush_list(struct inode
*inode
, struct list_head
*head
, int npages
, int how
)
955 LIST_HEAD(one_request
);
956 int (*flush_one
)(struct inode
*, struct list_head
*, int);
957 struct nfs_page
*req
;
958 int wpages
= NFS_SERVER(inode
)->wpages
;
959 int wsize
= NFS_SERVER(inode
)->wsize
;
962 flush_one
= nfs_flush_one
;
963 if (wsize
< PAGE_CACHE_SIZE
)
964 flush_one
= nfs_flush_multi
;
965 /* For single writes, FLUSH_STABLE is more efficient */
966 if (npages
<= wpages
&& npages
== NFS_I(inode
)->npages
967 && nfs_list_entry(head
->next
)->wb_bytes
<= wsize
)
971 nfs_coalesce_requests(head
, &one_request
, wpages
);
972 req
= nfs_list_entry(one_request
.next
);
973 error
= flush_one(inode
, &one_request
, how
);
976 } while (!list_empty(head
));
979 while (!list_empty(head
)) {
980 req
= nfs_list_entry(head
->next
);
981 nfs_list_remove_request(req
);
982 nfs_end_page_writeback(req
->wb_page
);
983 nfs_redirty_request(req
);
984 nfs_clear_page_writeback(req
);
990 * Handle a write reply that flushed part of a page.
992 static void nfs_writeback_done_partial(struct rpc_task
*task
, void *calldata
)
994 struct nfs_write_data
*data
= calldata
;
995 struct nfs_page
*req
= data
->req
;
996 struct page
*page
= req
->wb_page
;
998 dprintk("NFS: write (%s/%Ld %d@%Ld)",
999 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
1000 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
1002 (long long)req_offset(req
));
1004 if (nfs_writeback_done(task
, data
) != 0)
1007 if (task
->tk_status
< 0) {
1008 nfs_set_pageerror(page
);
1009 req
->wb_context
->error
= task
->tk_status
;
1010 dprintk(", error = %d\n", task
->tk_status
);
1012 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1013 if (data
->verf
.committed
< NFS_FILE_SYNC
) {
1014 if (!NFS_NEED_COMMIT(req
)) {
1015 nfs_defer_commit(req
);
1016 memcpy(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
));
1017 dprintk(" defer commit\n");
1018 } else if (memcmp(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
))) {
1019 nfs_defer_reschedule(req
);
1020 dprintk(" server reboot detected\n");
1027 if (atomic_dec_and_test(&req
->wb_complete
))
1028 nfs_writepage_release(req
);
1031 static const struct rpc_call_ops nfs_write_partial_ops
= {
1032 .rpc_call_done
= nfs_writeback_done_partial
,
1033 .rpc_release
= nfs_writedata_release
,
1037 * Handle a write reply that flushes a whole page.
1039 * FIXME: There is an inherent race with invalidate_inode_pages and
1040 * writebacks since the page->count is kept > 1 for as long
1041 * as the page has a write request pending.
1043 static void nfs_writeback_done_full(struct rpc_task
*task
, void *calldata
)
1045 struct nfs_write_data
*data
= calldata
;
1046 struct nfs_page
*req
;
1049 if (nfs_writeback_done(task
, data
) != 0)
1052 /* Update attributes as result of writeback. */
1053 while (!list_empty(&data
->pages
)) {
1054 req
= nfs_list_entry(data
->pages
.next
);
1055 nfs_list_remove_request(req
);
1056 page
= req
->wb_page
;
1058 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1059 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
1060 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
1062 (long long)req_offset(req
));
1064 if (task
->tk_status
< 0) {
1065 nfs_set_pageerror(page
);
1066 req
->wb_context
->error
= task
->tk_status
;
1067 nfs_end_page_writeback(page
);
1068 nfs_inode_remove_request(req
);
1069 dprintk(", error = %d\n", task
->tk_status
);
1072 nfs_end_page_writeback(page
);
1074 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1075 if (data
->args
.stable
!= NFS_UNSTABLE
|| data
->verf
.committed
== NFS_FILE_SYNC
) {
1076 nfs_inode_remove_request(req
);
1080 memcpy(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
));
1081 nfs_mark_request_commit(req
);
1082 dprintk(" marked for commit\n");
1084 nfs_inode_remove_request(req
);
1087 nfs_clear_page_writeback(req
);
1091 static const struct rpc_call_ops nfs_write_full_ops
= {
1092 .rpc_call_done
= nfs_writeback_done_full
,
1093 .rpc_release
= nfs_writedata_release
,
1098 * This function is called when the WRITE call is complete.
1100 int nfs_writeback_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
1102 struct nfs_writeargs
*argp
= &data
->args
;
1103 struct nfs_writeres
*resp
= &data
->res
;
1106 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1107 task
->tk_pid
, task
->tk_status
);
1110 * ->write_done will attempt to use post-op attributes to detect
1111 * conflicting writes by other clients. A strict interpretation
1112 * of close-to-open would allow us to continue caching even if
1113 * another writer had changed the file, but some applications
1114 * depend on tighter cache coherency when writing.
1116 status
= NFS_PROTO(data
->inode
)->write_done(task
, data
);
1119 nfs_add_stats(data
->inode
, NFSIOS_SERVERWRITTENBYTES
, resp
->count
);
1121 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1122 if (resp
->verf
->committed
< argp
->stable
&& task
->tk_status
>= 0) {
1123 /* We tried a write call, but the server did not
1124 * commit data to stable storage even though we
1126 * Note: There is a known bug in Tru64 < 5.0 in which
1127 * the server reports NFS_DATA_SYNC, but performs
1128 * NFS_FILE_SYNC. We therefore implement this checking
1129 * as a dprintk() in order to avoid filling syslog.
1131 static unsigned long complain
;
1133 if (time_before(complain
, jiffies
)) {
1134 dprintk("NFS: faulty NFS server %s:"
1135 " (committed = %d) != (stable = %d)\n",
1136 NFS_SERVER(data
->inode
)->nfs_client
->cl_hostname
,
1137 resp
->verf
->committed
, argp
->stable
);
1138 complain
= jiffies
+ 300 * HZ
;
1142 /* Is this a short write? */
1143 if (task
->tk_status
>= 0 && resp
->count
< argp
->count
) {
1144 static unsigned long complain
;
1146 nfs_inc_stats(data
->inode
, NFSIOS_SHORTWRITE
);
1148 /* Has the server at least made some progress? */
1149 if (resp
->count
!= 0) {
1150 /* Was this an NFSv2 write or an NFSv3 stable write? */
1151 if (resp
->verf
->committed
!= NFS_UNSTABLE
) {
1152 /* Resend from where the server left off */
1153 argp
->offset
+= resp
->count
;
1154 argp
->pgbase
+= resp
->count
;
1155 argp
->count
-= resp
->count
;
1157 /* Resend as a stable write in order to avoid
1158 * headaches in the case of a server crash.
1160 argp
->stable
= NFS_FILE_SYNC
;
1162 rpc_restart_call(task
);
1165 if (time_before(complain
, jiffies
)) {
1167 "NFS: Server wrote zero bytes, expected %u.\n",
1169 complain
= jiffies
+ 300 * HZ
;
1171 /* Can't do anything about it except throw an error. */
1172 task
->tk_status
= -EIO
;
1178 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1179 void nfs_commit_release(void *wdata
)
1181 nfs_commit_free(wdata
);
1185 * Set up the argument/result storage required for the RPC call.
1187 static void nfs_commit_rpcsetup(struct list_head
*head
,
1188 struct nfs_write_data
*data
,
1191 struct nfs_page
*first
;
1192 struct inode
*inode
;
1195 /* Set up the RPC argument and reply structs
1196 * NB: take care not to mess about with data->commit et al. */
1198 list_splice_init(head
, &data
->pages
);
1199 first
= nfs_list_entry(data
->pages
.next
);
1200 inode
= first
->wb_context
->dentry
->d_inode
;
1202 data
->inode
= inode
;
1203 data
->cred
= first
->wb_context
->cred
;
1205 data
->args
.fh
= NFS_FH(data
->inode
);
1206 /* Note: we always request a commit of the entire inode */
1207 data
->args
.offset
= 0;
1208 data
->args
.count
= 0;
1209 data
->res
.count
= 0;
1210 data
->res
.fattr
= &data
->fattr
;
1211 data
->res
.verf
= &data
->verf
;
1212 nfs_fattr_init(&data
->fattr
);
1214 /* Set up the initial task struct. */
1215 flags
= (how
& FLUSH_SYNC
) ? 0 : RPC_TASK_ASYNC
;
1216 rpc_init_task(&data
->task
, NFS_CLIENT(inode
), flags
, &nfs_commit_ops
, data
);
1217 NFS_PROTO(inode
)->commit_setup(data
, how
);
1219 data
->task
.tk_priority
= flush_task_priority(how
);
1220 data
->task
.tk_cookie
= (unsigned long)inode
;
1222 dprintk("NFS: %5u initiated commit call\n", data
->task
.tk_pid
);
1226 * Commit dirty pages
1229 nfs_commit_list(struct inode
*inode
, struct list_head
*head
, int how
)
1231 struct nfs_write_data
*data
;
1232 struct nfs_page
*req
;
1234 data
= nfs_commit_alloc();
1239 /* Set up the argument struct */
1240 nfs_commit_rpcsetup(head
, data
, how
);
1242 nfs_execute_write(data
);
1245 while (!list_empty(head
)) {
1246 req
= nfs_list_entry(head
->next
);
1247 nfs_list_remove_request(req
);
1248 nfs_mark_request_commit(req
);
1249 dec_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
1250 nfs_clear_page_writeback(req
);
1256 * COMMIT call returned
1258 static void nfs_commit_done(struct rpc_task
*task
, void *calldata
)
1260 struct nfs_write_data
*data
= calldata
;
1261 struct nfs_page
*req
;
1263 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1264 task
->tk_pid
, task
->tk_status
);
1266 /* Call the NFS version-specific code */
1267 if (NFS_PROTO(data
->inode
)->commit_done(task
, data
) != 0)
1270 while (!list_empty(&data
->pages
)) {
1271 req
= nfs_list_entry(data
->pages
.next
);
1272 nfs_list_remove_request(req
);
1273 dec_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
1275 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1276 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
1277 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
1279 (long long)req_offset(req
));
1280 if (task
->tk_status
< 0) {
1281 req
->wb_context
->error
= task
->tk_status
;
1282 nfs_inode_remove_request(req
);
1283 dprintk(", error = %d\n", task
->tk_status
);
1287 /* Okay, COMMIT succeeded, apparently. Check the verifier
1288 * returned by the server against all stored verfs. */
1289 if (!memcmp(req
->wb_verf
.verifier
, data
->verf
.verifier
, sizeof(data
->verf
.verifier
))) {
1290 /* We have a match */
1291 nfs_inode_remove_request(req
);
1295 /* We have a mismatch. Write the page again */
1296 dprintk(" mismatch\n");
1297 nfs_redirty_request(req
);
1299 nfs_clear_page_writeback(req
);
1303 static const struct rpc_call_ops nfs_commit_ops
= {
1304 .rpc_call_done
= nfs_commit_done
,
1305 .rpc_release
= nfs_commit_release
,
1308 static inline int nfs_commit_list(struct inode
*inode
, struct list_head
*head
, int how
)
1314 static long nfs_flush_mapping(struct address_space
*mapping
, struct writeback_control
*wbc
, int how
)
1316 struct nfs_inode
*nfsi
= NFS_I(mapping
->host
);
1320 spin_lock(&nfsi
->req_lock
);
1321 res
= nfs_scan_dirty(mapping
, wbc
, &head
);
1322 spin_unlock(&nfsi
->req_lock
);
1324 int error
= nfs_flush_list(mapping
->host
, &head
, res
, how
);
1331 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1332 int nfs_commit_inode(struct inode
*inode
, int how
)
1334 struct nfs_inode
*nfsi
= NFS_I(inode
);
1338 spin_lock(&nfsi
->req_lock
);
1339 res
= nfs_scan_commit(inode
, &head
, 0, 0);
1340 spin_unlock(&nfsi
->req_lock
);
1342 int error
= nfs_commit_list(inode
, &head
, how
);
1350 long nfs_sync_mapping_wait(struct address_space
*mapping
, struct writeback_control
*wbc
, int how
)
1352 struct inode
*inode
= mapping
->host
;
1353 struct nfs_inode
*nfsi
= NFS_I(inode
);
1354 unsigned long idx_start
, idx_end
;
1355 unsigned int npages
= 0;
1357 int nocommit
= how
& FLUSH_NOCOMMIT
;
1361 if (wbc
->range_cyclic
)
1364 idx_start
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
1365 idx_end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
1366 if (idx_end
> idx_start
) {
1367 unsigned long l_npages
= 1 + idx_end
- idx_start
;
1369 if (sizeof(npages
) != sizeof(l_npages
) &&
1370 (unsigned long)npages
!= l_npages
)
1374 how
&= ~FLUSH_NOCOMMIT
;
1375 spin_lock(&nfsi
->req_lock
);
1377 wbc
->pages_skipped
= 0;
1378 ret
= nfs_wait_on_requests_locked(inode
, idx_start
, npages
);
1381 pages
= nfs_scan_dirty(mapping
, wbc
, &head
);
1383 spin_unlock(&nfsi
->req_lock
);
1384 if (how
& FLUSH_INVALIDATE
) {
1385 nfs_cancel_dirty_list(&head
);
1388 ret
= nfs_flush_list(inode
, &head
, pages
, how
);
1389 spin_lock(&nfsi
->req_lock
);
1392 if (wbc
->pages_skipped
!= 0)
1396 pages
= nfs_scan_commit(inode
, &head
, idx_start
, npages
);
1398 if (wbc
->pages_skipped
!= 0)
1402 if (how
& FLUSH_INVALIDATE
) {
1403 spin_unlock(&nfsi
->req_lock
);
1404 nfs_cancel_commit_list(&head
);
1406 spin_lock(&nfsi
->req_lock
);
1409 pages
+= nfs_scan_commit(inode
, &head
, 0, 0);
1410 spin_unlock(&nfsi
->req_lock
);
1411 ret
= nfs_commit_list(inode
, &head
, how
);
1412 spin_lock(&nfsi
->req_lock
);
1414 spin_unlock(&nfsi
->req_lock
);
1419 * flush the inode to disk.
1421 int nfs_wb_all(struct inode
*inode
)
1423 struct address_space
*mapping
= inode
->i_mapping
;
1424 struct writeback_control wbc
= {
1425 .bdi
= mapping
->backing_dev_info
,
1426 .sync_mode
= WB_SYNC_ALL
,
1427 .nr_to_write
= LONG_MAX
,
1428 .for_writepages
= 1,
1433 ret
= generic_writepages(mapping
, &wbc
);
1436 ret
= nfs_sync_mapping_wait(mapping
, &wbc
, 0);
1440 __mark_inode_dirty(mapping
->host
, I_DIRTY_PAGES
);
1444 int nfs_sync_mapping_range(struct address_space
*mapping
, loff_t range_start
, loff_t range_end
, int how
)
1446 struct writeback_control wbc
= {
1447 .bdi
= mapping
->backing_dev_info
,
1448 .sync_mode
= WB_SYNC_ALL
,
1449 .nr_to_write
= LONG_MAX
,
1450 .range_start
= range_start
,
1451 .range_end
= range_end
,
1452 .for_writepages
= 1,
1456 if (!(how
& FLUSH_NOWRITEPAGE
)) {
1457 ret
= generic_writepages(mapping
, &wbc
);
1461 ret
= nfs_sync_mapping_wait(mapping
, &wbc
, how
);
1465 __mark_inode_dirty(mapping
->host
, I_DIRTY_PAGES
);
1469 int nfs_wb_page_priority(struct inode
*inode
, struct page
*page
, int how
)
1471 loff_t range_start
= page_offset(page
);
1472 loff_t range_end
= range_start
+ (loff_t
)(PAGE_CACHE_SIZE
- 1);
1473 struct writeback_control wbc
= {
1474 .bdi
= page
->mapping
->backing_dev_info
,
1475 .sync_mode
= WB_SYNC_ALL
,
1476 .nr_to_write
= LONG_MAX
,
1477 .range_start
= range_start
,
1478 .range_end
= range_end
,
1482 BUG_ON(!PageLocked(page
));
1483 if (!(how
& FLUSH_NOWRITEPAGE
) && clear_page_dirty_for_io(page
)) {
1484 ret
= nfs_writepage_locked(page
, &wbc
);
1488 if (!PagePrivate(page
))
1490 ret
= nfs_sync_mapping_wait(page
->mapping
, &wbc
, how
);
1494 __mark_inode_dirty(inode
, I_DIRTY_PAGES
);
1499 * Write back all requests on one page - we do this before reading it.
1501 int nfs_wb_page(struct inode
*inode
, struct page
* page
)
1503 return nfs_wb_page_priority(inode
, page
, FLUSH_STABLE
);
1506 int nfs_set_page_dirty(struct page
*page
)
1508 struct nfs_page
*req
;
1510 req
= nfs_page_find_request(page
);
1512 /* Mark any existing write requests for flushing */
1513 set_bit(PG_NEED_FLUSH
, &req
->wb_flags
);
1514 nfs_release_request(req
);
1516 return __set_page_dirty_nobuffers(page
);
1520 int __init
nfs_init_writepagecache(void)
1522 nfs_wdata_cachep
= kmem_cache_create("nfs_write_data",
1523 sizeof(struct nfs_write_data
),
1524 0, SLAB_HWCACHE_ALIGN
,
1526 if (nfs_wdata_cachep
== NULL
)
1529 nfs_wdata_mempool
= mempool_create_slab_pool(MIN_POOL_WRITE
,
1531 if (nfs_wdata_mempool
== NULL
)
1534 nfs_commit_mempool
= mempool_create_slab_pool(MIN_POOL_COMMIT
,
1536 if (nfs_commit_mempool
== NULL
)
1540 * NFS congestion size, scale with available memory.
1552 * This allows larger machines to have larger/more transfers.
1553 * Limit the default to 256M
1555 nfs_congestion_kb
= (16*int_sqrt(totalram_pages
)) << (PAGE_SHIFT
-10);
1556 if (nfs_congestion_kb
> 256*1024)
1557 nfs_congestion_kb
= 256*1024;
1562 void nfs_destroy_writepagecache(void)
1564 mempool_destroy(nfs_commit_mempool
);
1565 mempool_destroy(nfs_wdata_mempool
);
1566 kmem_cache_destroy(nfs_wdata_cachep
);