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>
16 #include <linux/sunrpc/clnt.h>
17 #include <linux/nfs_fs.h>
18 #include <linux/nfs_mount.h>
19 #include <linux/nfs_page.h>
20 #include <linux/backing-dev.h>
22 #include <asm/uaccess.h>
23 #include <linux/smp_lock.h>
25 #include "delegation.h"
29 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
31 #define MIN_POOL_WRITE (32)
32 #define MIN_POOL_COMMIT (4)
35 * Local function declarations
37 static struct nfs_page
* nfs_update_request(struct nfs_open_context
*,
39 unsigned int, unsigned int);
40 static void nfs_mark_request_dirty(struct nfs_page
*req
);
41 static int nfs_wait_on_write_congestion(struct address_space
*, int);
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 static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion
);
53 struct nfs_write_data
*nfs_commit_alloc(void)
55 struct nfs_write_data
*p
= mempool_alloc(nfs_commit_mempool
, GFP_NOFS
);
58 memset(p
, 0, sizeof(*p
));
59 INIT_LIST_HEAD(&p
->pages
);
64 void nfs_commit_rcu_free(struct rcu_head
*head
)
66 struct nfs_write_data
*p
= container_of(head
, struct nfs_write_data
, task
.u
.tk_rcu
);
67 if (p
&& (p
->pagevec
!= &p
->page_array
[0]))
69 mempool_free(p
, nfs_commit_mempool
);
72 void nfs_commit_free(struct nfs_write_data
*wdata
)
74 call_rcu_bh(&wdata
->task
.u
.tk_rcu
, nfs_commit_rcu_free
);
77 struct nfs_write_data
*nfs_writedata_alloc(size_t len
)
79 unsigned int pagecount
= (len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
80 struct nfs_write_data
*p
= mempool_alloc(nfs_wdata_mempool
, GFP_NOFS
);
83 memset(p
, 0, sizeof(*p
));
84 INIT_LIST_HEAD(&p
->pages
);
85 p
->npages
= pagecount
;
86 if (pagecount
<= ARRAY_SIZE(p
->page_array
))
87 p
->pagevec
= p
->page_array
;
89 p
->pagevec
= kcalloc(pagecount
, sizeof(struct page
*), GFP_NOFS
);
91 mempool_free(p
, nfs_wdata_mempool
);
99 static void nfs_writedata_rcu_free(struct rcu_head
*head
)
101 struct nfs_write_data
*p
= container_of(head
, struct nfs_write_data
, task
.u
.tk_rcu
);
102 if (p
&& (p
->pagevec
!= &p
->page_array
[0]))
104 mempool_free(p
, nfs_wdata_mempool
);
107 static void nfs_writedata_free(struct nfs_write_data
*wdata
)
109 call_rcu_bh(&wdata
->task
.u
.tk_rcu
, nfs_writedata_rcu_free
);
112 void nfs_writedata_release(void *wdata
)
114 nfs_writedata_free(wdata
);
117 static struct nfs_page
*nfs_page_find_request_locked(struct page
*page
)
119 struct nfs_page
*req
= NULL
;
121 if (PagePrivate(page
)) {
122 req
= (struct nfs_page
*)page_private(page
);
124 atomic_inc(&req
->wb_count
);
129 static struct nfs_page
*nfs_page_find_request(struct page
*page
)
131 struct nfs_page
*req
= NULL
;
132 spinlock_t
*req_lock
= &NFS_I(page
->mapping
->host
)->req_lock
;
135 req
= nfs_page_find_request_locked(page
);
136 spin_unlock(req_lock
);
140 /* Adjust the file length if we're writing beyond the end */
141 static void nfs_grow_file(struct page
*page
, unsigned int offset
, unsigned int count
)
143 struct inode
*inode
= page
->mapping
->host
;
144 loff_t end
, i_size
= i_size_read(inode
);
145 unsigned long end_index
= (i_size
- 1) >> PAGE_CACHE_SHIFT
;
147 if (i_size
> 0 && page
->index
< end_index
)
149 end
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + ((loff_t
)offset
+count
);
152 nfs_inc_stats(inode
, NFSIOS_EXTENDWRITE
);
153 i_size_write(inode
, end
);
156 /* A writeback failed: mark the page as bad, and invalidate the page cache */
157 static void nfs_set_pageerror(struct page
*page
)
160 nfs_zap_mapping(page
->mapping
->host
, page
->mapping
);
163 /* We can set the PG_uptodate flag if we see that a write request
164 * covers the full page.
166 static void nfs_mark_uptodate(struct page
*page
, unsigned int base
, unsigned int count
)
168 if (PageUptodate(page
))
172 if (count
!= nfs_page_length(page
))
174 if (count
!= PAGE_CACHE_SIZE
)
175 memclear_highpage_flush(page
, count
, PAGE_CACHE_SIZE
- count
);
176 SetPageUptodate(page
);
179 static int nfs_writepage_setup(struct nfs_open_context
*ctx
, struct page
*page
,
180 unsigned int offset
, unsigned int count
)
182 struct nfs_page
*req
;
186 req
= nfs_update_request(ctx
, page
, offset
, count
);
192 ret
= nfs_wb_page(page
->mapping
->host
, page
);
196 /* Update file length */
197 nfs_grow_file(page
, offset
, count
);
198 /* Set the PG_uptodate flag? */
199 nfs_mark_uptodate(page
, offset
, count
);
200 nfs_unlock_request(req
);
204 static int wb_priority(struct writeback_control
*wbc
)
206 if (wbc
->for_reclaim
)
207 return FLUSH_HIGHPRI
;
208 if (wbc
->for_kupdate
)
214 * Find an associated nfs write request, and prepare to flush it out
215 * Returns 1 if there was no write request, or if the request was
216 * already tagged by nfs_set_page_dirty.Returns 0 if the request
218 * May also return an error if the user signalled nfs_wait_on_request().
220 static int nfs_page_mark_flush(struct page
*page
)
222 struct nfs_page
*req
;
223 spinlock_t
*req_lock
= &NFS_I(page
->mapping
->host
)->req_lock
;
228 req
= nfs_page_find_request_locked(page
);
230 spin_unlock(req_lock
);
233 if (nfs_lock_request_dontget(req
))
235 /* Note: If we hold the page lock, as is the case in nfs_writepage,
236 * then the call to nfs_lock_request_dontget() will always
237 * succeed provided that someone hasn't already marked the
238 * request as dirty (in which case we don't care).
240 spin_unlock(req_lock
);
241 ret
= nfs_wait_on_request(req
);
242 nfs_release_request(req
);
247 spin_unlock(req_lock
);
248 if (test_and_set_bit(PG_FLUSHING
, &req
->wb_flags
) == 0) {
249 nfs_mark_request_dirty(req
);
250 set_page_writeback(page
);
252 ret
= test_bit(PG_NEED_FLUSH
, &req
->wb_flags
);
253 nfs_unlock_request(req
);
258 * Write an mmapped page to the server.
260 static int nfs_writepage_locked(struct page
*page
, struct writeback_control
*wbc
)
262 struct nfs_open_context
*ctx
;
263 struct inode
*inode
= page
->mapping
->host
;
267 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGE
);
268 nfs_add_stats(inode
, NFSIOS_WRITEPAGES
, 1);
270 err
= nfs_page_mark_flush(page
);
274 offset
= nfs_page_length(page
);
278 ctx
= nfs_find_open_context(inode
, NULL
, FMODE_WRITE
);
283 err
= nfs_writepage_setup(ctx
, page
, 0, offset
);
284 put_nfs_open_context(ctx
);
287 err
= nfs_page_mark_flush(page
);
291 if (!wbc
->for_writepages
)
292 nfs_flush_mapping(page
->mapping
, wbc
, FLUSH_STABLE
|wb_priority(wbc
));
296 int nfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
300 err
= nfs_writepage_locked(page
, wbc
);
306 * Note: causes nfs_update_request() to block on the assumption
307 * that the writeback is generated due to memory pressure.
309 int nfs_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
311 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
312 struct inode
*inode
= mapping
->host
;
315 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGES
);
317 err
= generic_writepages(mapping
, wbc
);
320 while (test_and_set_bit(BDI_write_congested
, &bdi
->state
) != 0) {
321 if (wbc
->nonblocking
)
323 nfs_wait_on_write_congestion(mapping
, 0);
325 err
= nfs_flush_mapping(mapping
, wbc
, wb_priority(wbc
));
328 nfs_add_stats(inode
, NFSIOS_WRITEPAGES
, err
);
331 clear_bit(BDI_write_congested
, &bdi
->state
);
332 wake_up_all(&nfs_write_congestion
);
333 congestion_end(WRITE
);
338 * Insert a write request into an inode
340 static int nfs_inode_add_request(struct inode
*inode
, struct nfs_page
*req
)
342 struct nfs_inode
*nfsi
= NFS_I(inode
);
345 error
= radix_tree_insert(&nfsi
->nfs_page_tree
, req
->wb_index
, req
);
346 BUG_ON(error
== -EEXIST
);
351 nfs_begin_data_update(inode
);
352 if (nfs_have_delegation(inode
, FMODE_WRITE
))
355 SetPagePrivate(req
->wb_page
);
356 set_page_private(req
->wb_page
, (unsigned long)req
);
358 atomic_inc(&req
->wb_count
);
363 * Insert a write request into an inode
365 static void nfs_inode_remove_request(struct nfs_page
*req
)
367 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
368 struct nfs_inode
*nfsi
= NFS_I(inode
);
370 BUG_ON (!NFS_WBACK_BUSY(req
));
372 spin_lock(&nfsi
->req_lock
);
373 set_page_private(req
->wb_page
, 0);
374 ClearPagePrivate(req
->wb_page
);
375 radix_tree_delete(&nfsi
->nfs_page_tree
, req
->wb_index
);
378 spin_unlock(&nfsi
->req_lock
);
379 nfs_end_data_update(inode
);
382 spin_unlock(&nfsi
->req_lock
);
383 nfs_clear_request(req
);
384 nfs_release_request(req
);
388 * Add a request to the inode's dirty list.
391 nfs_mark_request_dirty(struct nfs_page
*req
)
393 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
394 struct nfs_inode
*nfsi
= NFS_I(inode
);
396 spin_lock(&nfsi
->req_lock
);
397 radix_tree_tag_set(&nfsi
->nfs_page_tree
,
398 req
->wb_index
, NFS_PAGE_TAG_DIRTY
);
399 nfs_list_add_request(req
, &nfsi
->dirty
);
401 spin_unlock(&nfsi
->req_lock
);
402 __mark_inode_dirty(inode
, I_DIRTY_PAGES
);
406 nfs_redirty_request(struct nfs_page
*req
)
408 clear_bit(PG_FLUSHING
, &req
->wb_flags
);
409 __set_page_dirty_nobuffers(req
->wb_page
);
413 * Check if a request is dirty
416 nfs_dirty_request(struct nfs_page
*req
)
418 return test_bit(PG_FLUSHING
, &req
->wb_flags
) == 0;
421 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
423 * Add a request to the inode's commit list.
426 nfs_mark_request_commit(struct nfs_page
*req
)
428 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
429 struct nfs_inode
*nfsi
= NFS_I(inode
);
431 spin_lock(&nfsi
->req_lock
);
432 nfs_list_add_request(req
, &nfsi
->commit
);
434 spin_unlock(&nfsi
->req_lock
);
435 inc_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
436 __mark_inode_dirty(inode
, I_DIRTY_DATASYNC
);
441 * Wait for a request to complete.
443 * Interruptible by signals only if mounted with intr flag.
445 static int nfs_wait_on_requests_locked(struct inode
*inode
, unsigned long idx_start
, unsigned int npages
)
447 struct nfs_inode
*nfsi
= NFS_I(inode
);
448 struct nfs_page
*req
;
449 unsigned long idx_end
, next
;
450 unsigned int res
= 0;
456 idx_end
= idx_start
+ npages
- 1;
459 while (radix_tree_gang_lookup_tag(&nfsi
->nfs_page_tree
, (void **)&req
, next
, 1, NFS_PAGE_TAG_WRITEBACK
)) {
460 if (req
->wb_index
> idx_end
)
463 next
= req
->wb_index
+ 1;
464 BUG_ON(!NFS_WBACK_BUSY(req
));
466 atomic_inc(&req
->wb_count
);
467 spin_unlock(&nfsi
->req_lock
);
468 error
= nfs_wait_on_request(req
);
469 nfs_release_request(req
);
470 spin_lock(&nfsi
->req_lock
);
478 static void nfs_cancel_dirty_list(struct list_head
*head
)
480 struct nfs_page
*req
;
481 while(!list_empty(head
)) {
482 req
= nfs_list_entry(head
->next
);
483 nfs_list_remove_request(req
);
484 nfs_inode_remove_request(req
);
485 nfs_clear_page_writeback(req
);
489 static void nfs_cancel_commit_list(struct list_head
*head
)
491 struct nfs_page
*req
;
493 while(!list_empty(head
)) {
494 req
= nfs_list_entry(head
->next
);
495 dec_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
496 nfs_list_remove_request(req
);
497 nfs_inode_remove_request(req
);
498 nfs_unlock_request(req
);
502 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
504 * nfs_scan_commit - Scan an inode for commit requests
505 * @inode: NFS inode to scan
506 * @dst: destination list
507 * @idx_start: lower bound of page->index to scan.
508 * @npages: idx_start + npages sets the upper bound to scan.
510 * Moves requests from the inode's 'commit' request list.
511 * The requests are *not* checked to ensure that they form a contiguous set.
514 nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
, unsigned long idx_start
, unsigned int npages
)
516 struct nfs_inode
*nfsi
= NFS_I(inode
);
519 if (nfsi
->ncommit
!= 0) {
520 res
= nfs_scan_list(nfsi
, &nfsi
->commit
, dst
, idx_start
, npages
);
521 nfsi
->ncommit
-= res
;
522 if ((nfsi
->ncommit
== 0) != list_empty(&nfsi
->commit
))
523 printk(KERN_ERR
"NFS: desynchronized value of nfs_i.ncommit.\n");
528 static inline int nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
, unsigned long idx_start
, unsigned int npages
)
534 static int nfs_wait_on_write_congestion(struct address_space
*mapping
, int intr
)
536 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
542 if (!bdi_write_congested(bdi
))
545 nfs_inc_stats(mapping
->host
, NFSIOS_CONGESTIONWAIT
);
548 struct rpc_clnt
*clnt
= NFS_CLIENT(mapping
->host
);
551 rpc_clnt_sigmask(clnt
, &oldset
);
552 prepare_to_wait(&nfs_write_congestion
, &wait
, TASK_INTERRUPTIBLE
);
553 if (bdi_write_congested(bdi
)) {
559 rpc_clnt_sigunmask(clnt
, &oldset
);
561 prepare_to_wait(&nfs_write_congestion
, &wait
, TASK_UNINTERRUPTIBLE
);
562 if (bdi_write_congested(bdi
))
565 finish_wait(&nfs_write_congestion
, &wait
);
571 * Try to update any existing write request, or create one if there is none.
572 * In order to match, the request's credentials must match those of
573 * the calling process.
575 * Note: Should always be called with the Page Lock held!
577 static struct nfs_page
* nfs_update_request(struct nfs_open_context
* ctx
,
578 struct page
*page
, unsigned int offset
, unsigned int bytes
)
580 struct inode
*inode
= page
->mapping
->host
;
581 struct nfs_inode
*nfsi
= NFS_I(inode
);
582 struct nfs_page
*req
, *new = NULL
;
583 unsigned long rqend
, end
;
585 end
= offset
+ bytes
;
587 if (nfs_wait_on_write_congestion(page
->mapping
, NFS_SERVER(inode
)->flags
& NFS_MOUNT_INTR
))
588 return ERR_PTR(-ERESTARTSYS
);
590 /* Loop over all inode entries and see if we find
591 * A request for the page we wish to update
593 spin_lock(&nfsi
->req_lock
);
594 req
= nfs_page_find_request_locked(page
);
596 if (!nfs_lock_request_dontget(req
)) {
599 spin_unlock(&nfsi
->req_lock
);
600 error
= nfs_wait_on_request(req
);
601 nfs_release_request(req
);
604 nfs_release_request(new);
605 return ERR_PTR(error
);
609 spin_unlock(&nfsi
->req_lock
);
611 nfs_release_request(new);
617 nfs_lock_request_dontget(new);
618 error
= nfs_inode_add_request(inode
, new);
620 spin_unlock(&nfsi
->req_lock
);
621 nfs_unlock_request(new);
622 return ERR_PTR(error
);
624 spin_unlock(&nfsi
->req_lock
);
627 spin_unlock(&nfsi
->req_lock
);
629 new = nfs_create_request(ctx
, inode
, page
, offset
, bytes
);
634 /* We have a request for our page.
635 * If the creds don't match, or the
636 * page addresses don't match,
637 * tell the caller to wait on the conflicting
640 rqend
= req
->wb_offset
+ req
->wb_bytes
;
641 if (req
->wb_context
!= ctx
642 || req
->wb_page
!= page
643 || !nfs_dirty_request(req
)
644 || offset
> rqend
|| end
< req
->wb_offset
) {
645 nfs_unlock_request(req
);
646 return ERR_PTR(-EBUSY
);
649 /* Okay, the request matches. Update the region */
650 if (offset
< req
->wb_offset
) {
651 req
->wb_offset
= offset
;
652 req
->wb_pgbase
= offset
;
653 req
->wb_bytes
= rqend
- req
->wb_offset
;
657 req
->wb_bytes
= end
- req
->wb_offset
;
662 int nfs_flush_incompatible(struct file
*file
, struct page
*page
)
664 struct nfs_open_context
*ctx
= (struct nfs_open_context
*)file
->private_data
;
665 struct nfs_page
*req
;
666 int do_flush
, status
;
668 * Look for a request corresponding to this page. If there
669 * is one, and it belongs to another file, we flush it out
670 * before we try to copy anything into the page. Do this
671 * due to the lack of an ACCESS-type call in NFSv2.
672 * Also do the same if we find a request from an existing
676 req
= nfs_page_find_request(page
);
679 do_flush
= req
->wb_page
!= page
|| req
->wb_context
!= ctx
680 || !nfs_dirty_request(req
);
681 nfs_release_request(req
);
684 status
= nfs_wb_page(page
->mapping
->host
, page
);
685 } while (status
== 0);
690 * Update and possibly write a cached page of an NFS file.
692 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
693 * things with a page scheduled for an RPC call (e.g. invalidate it).
695 int nfs_updatepage(struct file
*file
, struct page
*page
,
696 unsigned int offset
, unsigned int count
)
698 struct nfs_open_context
*ctx
= (struct nfs_open_context
*)file
->private_data
;
699 struct inode
*inode
= page
->mapping
->host
;
702 nfs_inc_stats(inode
, NFSIOS_VFSUPDATEPAGE
);
704 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
705 file
->f_path
.dentry
->d_parent
->d_name
.name
,
706 file
->f_path
.dentry
->d_name
.name
, count
,
707 (long long)(page_offset(page
) +offset
));
709 /* If we're not using byte range locks, and we know the page
710 * is entirely in cache, it may be more efficient to avoid
711 * fragmenting write requests.
713 if (PageUptodate(page
) && inode
->i_flock
== NULL
&& !(file
->f_mode
& O_SYNC
)) {
714 count
= max(count
+ offset
, nfs_page_length(page
));
718 status
= nfs_writepage_setup(ctx
, page
, offset
, count
);
719 __set_page_dirty_nobuffers(page
);
721 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
722 status
, (long long)i_size_read(inode
));
724 nfs_set_pageerror(page
);
728 static void nfs_writepage_release(struct nfs_page
*req
)
730 end_page_writeback(req
->wb_page
);
732 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
733 if (!PageError(req
->wb_page
)) {
734 if (NFS_NEED_RESCHED(req
)) {
735 nfs_redirty_request(req
);
737 } else if (NFS_NEED_COMMIT(req
)) {
738 nfs_mark_request_commit(req
);
742 nfs_inode_remove_request(req
);
745 nfs_clear_commit(req
);
746 nfs_clear_reschedule(req
);
748 nfs_inode_remove_request(req
);
750 nfs_clear_page_writeback(req
);
753 static inline int flush_task_priority(int how
)
755 switch (how
& (FLUSH_HIGHPRI
|FLUSH_LOWPRI
)) {
757 return RPC_PRIORITY_HIGH
;
759 return RPC_PRIORITY_LOW
;
761 return RPC_PRIORITY_NORMAL
;
765 * Set up the argument/result storage required for the RPC call.
767 static void nfs_write_rpcsetup(struct nfs_page
*req
,
768 struct nfs_write_data
*data
,
769 const struct rpc_call_ops
*call_ops
,
770 unsigned int count
, unsigned int offset
,
776 /* Set up the RPC argument and reply structs
777 * NB: take care not to mess about with data->commit et al. */
780 data
->inode
= inode
= req
->wb_context
->dentry
->d_inode
;
781 data
->cred
= req
->wb_context
->cred
;
783 data
->args
.fh
= NFS_FH(inode
);
784 data
->args
.offset
= req_offset(req
) + offset
;
785 data
->args
.pgbase
= req
->wb_pgbase
+ offset
;
786 data
->args
.pages
= data
->pagevec
;
787 data
->args
.count
= count
;
788 data
->args
.context
= req
->wb_context
;
790 data
->res
.fattr
= &data
->fattr
;
791 data
->res
.count
= count
;
792 data
->res
.verf
= &data
->verf
;
793 nfs_fattr_init(&data
->fattr
);
795 /* Set up the initial task struct. */
796 flags
= (how
& FLUSH_SYNC
) ? 0 : RPC_TASK_ASYNC
;
797 rpc_init_task(&data
->task
, NFS_CLIENT(inode
), flags
, call_ops
, data
);
798 NFS_PROTO(inode
)->write_setup(data
, how
);
800 data
->task
.tk_priority
= flush_task_priority(how
);
801 data
->task
.tk_cookie
= (unsigned long)inode
;
803 dprintk("NFS: %5u initiated write call "
804 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
807 (long long)NFS_FILEID(inode
),
809 (unsigned long long)data
->args
.offset
);
812 static void nfs_execute_write(struct nfs_write_data
*data
)
814 struct rpc_clnt
*clnt
= NFS_CLIENT(data
->inode
);
817 rpc_clnt_sigmask(clnt
, &oldset
);
818 rpc_execute(&data
->task
);
819 rpc_clnt_sigunmask(clnt
, &oldset
);
823 * Generate multiple small requests to write out a single
824 * contiguous dirty area on one page.
826 static int nfs_flush_multi(struct inode
*inode
, struct list_head
*head
, int how
)
828 struct nfs_page
*req
= nfs_list_entry(head
->next
);
829 struct page
*page
= req
->wb_page
;
830 struct nfs_write_data
*data
;
831 size_t wsize
= NFS_SERVER(inode
)->wsize
, nbytes
;
836 nfs_list_remove_request(req
);
838 nbytes
= req
->wb_bytes
;
840 size_t len
= min(nbytes
, wsize
);
842 data
= nfs_writedata_alloc(len
);
845 list_add(&data
->pages
, &list
);
848 } while (nbytes
!= 0);
849 atomic_set(&req
->wb_complete
, requests
);
851 ClearPageError(page
);
853 nbytes
= req
->wb_bytes
;
855 data
= list_entry(list
.next
, struct nfs_write_data
, pages
);
856 list_del_init(&data
->pages
);
858 data
->pagevec
[0] = page
;
860 if (nbytes
> wsize
) {
861 nfs_write_rpcsetup(req
, data
, &nfs_write_partial_ops
,
866 nfs_write_rpcsetup(req
, data
, &nfs_write_partial_ops
,
867 nbytes
, offset
, how
);
870 nfs_execute_write(data
);
871 } while (nbytes
!= 0);
876 while (!list_empty(&list
)) {
877 data
= list_entry(list
.next
, struct nfs_write_data
, pages
);
878 list_del(&data
->pages
);
879 nfs_writedata_release(data
);
881 nfs_redirty_request(req
);
882 nfs_clear_page_writeback(req
);
887 * Create an RPC task for the given write request and kick it.
888 * The page must have been locked by the caller.
890 * It may happen that the page we're passed is not marked dirty.
891 * This is the case if nfs_updatepage detects a conflicting request
892 * that has been written but not committed.
894 static int nfs_flush_one(struct inode
*inode
, struct list_head
*head
, int how
)
896 struct nfs_page
*req
;
898 struct nfs_write_data
*data
;
901 data
= nfs_writedata_alloc(NFS_SERVER(inode
)->wsize
);
905 pages
= data
->pagevec
;
907 while (!list_empty(head
)) {
908 req
= nfs_list_entry(head
->next
);
909 nfs_list_remove_request(req
);
910 nfs_list_add_request(req
, &data
->pages
);
911 ClearPageError(req
->wb_page
);
912 *pages
++ = req
->wb_page
;
913 count
+= req
->wb_bytes
;
915 req
= nfs_list_entry(data
->pages
.next
);
917 /* Set up the argument struct */
918 nfs_write_rpcsetup(req
, data
, &nfs_write_full_ops
, count
, 0, how
);
920 nfs_execute_write(data
);
923 while (!list_empty(head
)) {
924 struct nfs_page
*req
= nfs_list_entry(head
->next
);
925 nfs_list_remove_request(req
);
926 nfs_redirty_request(req
);
927 nfs_clear_page_writeback(req
);
932 static int nfs_flush_list(struct inode
*inode
, struct list_head
*head
, int npages
, int how
)
934 LIST_HEAD(one_request
);
935 int (*flush_one
)(struct inode
*, struct list_head
*, int);
936 struct nfs_page
*req
;
937 int wpages
= NFS_SERVER(inode
)->wpages
;
938 int wsize
= NFS_SERVER(inode
)->wsize
;
941 flush_one
= nfs_flush_one
;
942 if (wsize
< PAGE_CACHE_SIZE
)
943 flush_one
= nfs_flush_multi
;
944 /* For single writes, FLUSH_STABLE is more efficient */
945 if (npages
<= wpages
&& npages
== NFS_I(inode
)->npages
946 && nfs_list_entry(head
->next
)->wb_bytes
<= wsize
)
950 nfs_coalesce_requests(head
, &one_request
, wpages
);
951 req
= nfs_list_entry(one_request
.next
);
952 error
= flush_one(inode
, &one_request
, how
);
955 } while (!list_empty(head
));
958 while (!list_empty(head
)) {
959 req
= nfs_list_entry(head
->next
);
960 nfs_list_remove_request(req
);
961 nfs_redirty_request(req
);
962 nfs_clear_page_writeback(req
);
968 * Handle a write reply that flushed part of a page.
970 static void nfs_writeback_done_partial(struct rpc_task
*task
, void *calldata
)
972 struct nfs_write_data
*data
= calldata
;
973 struct nfs_page
*req
= data
->req
;
974 struct page
*page
= req
->wb_page
;
976 dprintk("NFS: write (%s/%Ld %d@%Ld)",
977 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
978 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
980 (long long)req_offset(req
));
982 if (nfs_writeback_done(task
, data
) != 0)
985 if (task
->tk_status
< 0) {
986 nfs_set_pageerror(page
);
987 req
->wb_context
->error
= task
->tk_status
;
988 dprintk(", error = %d\n", task
->tk_status
);
990 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
991 if (data
->verf
.committed
< NFS_FILE_SYNC
) {
992 if (!NFS_NEED_COMMIT(req
)) {
993 nfs_defer_commit(req
);
994 memcpy(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
));
995 dprintk(" defer commit\n");
996 } else if (memcmp(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
))) {
997 nfs_defer_reschedule(req
);
998 dprintk(" server reboot detected\n");
1005 if (atomic_dec_and_test(&req
->wb_complete
))
1006 nfs_writepage_release(req
);
1009 static const struct rpc_call_ops nfs_write_partial_ops
= {
1010 .rpc_call_done
= nfs_writeback_done_partial
,
1011 .rpc_release
= nfs_writedata_release
,
1015 * Handle a write reply that flushes a whole page.
1017 * FIXME: There is an inherent race with invalidate_inode_pages and
1018 * writebacks since the page->count is kept > 1 for as long
1019 * as the page has a write request pending.
1021 static void nfs_writeback_done_full(struct rpc_task
*task
, void *calldata
)
1023 struct nfs_write_data
*data
= calldata
;
1024 struct nfs_page
*req
;
1027 if (nfs_writeback_done(task
, data
) != 0)
1030 /* Update attributes as result of writeback. */
1031 while (!list_empty(&data
->pages
)) {
1032 req
= nfs_list_entry(data
->pages
.next
);
1033 nfs_list_remove_request(req
);
1034 page
= req
->wb_page
;
1036 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1037 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
1038 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
1040 (long long)req_offset(req
));
1042 if (task
->tk_status
< 0) {
1043 nfs_set_pageerror(page
);
1044 req
->wb_context
->error
= task
->tk_status
;
1045 end_page_writeback(page
);
1046 nfs_inode_remove_request(req
);
1047 dprintk(", error = %d\n", task
->tk_status
);
1050 end_page_writeback(page
);
1052 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1053 if (data
->args
.stable
!= NFS_UNSTABLE
|| data
->verf
.committed
== NFS_FILE_SYNC
) {
1054 nfs_inode_remove_request(req
);
1058 memcpy(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
));
1059 nfs_mark_request_commit(req
);
1060 dprintk(" marked for commit\n");
1062 nfs_inode_remove_request(req
);
1065 nfs_clear_page_writeback(req
);
1069 static const struct rpc_call_ops nfs_write_full_ops
= {
1070 .rpc_call_done
= nfs_writeback_done_full
,
1071 .rpc_release
= nfs_writedata_release
,
1076 * This function is called when the WRITE call is complete.
1078 int nfs_writeback_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
1080 struct nfs_writeargs
*argp
= &data
->args
;
1081 struct nfs_writeres
*resp
= &data
->res
;
1084 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1085 task
->tk_pid
, task
->tk_status
);
1088 * ->write_done will attempt to use post-op attributes to detect
1089 * conflicting writes by other clients. A strict interpretation
1090 * of close-to-open would allow us to continue caching even if
1091 * another writer had changed the file, but some applications
1092 * depend on tighter cache coherency when writing.
1094 status
= NFS_PROTO(data
->inode
)->write_done(task
, data
);
1097 nfs_add_stats(data
->inode
, NFSIOS_SERVERWRITTENBYTES
, resp
->count
);
1099 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1100 if (resp
->verf
->committed
< argp
->stable
&& task
->tk_status
>= 0) {
1101 /* We tried a write call, but the server did not
1102 * commit data to stable storage even though we
1104 * Note: There is a known bug in Tru64 < 5.0 in which
1105 * the server reports NFS_DATA_SYNC, but performs
1106 * NFS_FILE_SYNC. We therefore implement this checking
1107 * as a dprintk() in order to avoid filling syslog.
1109 static unsigned long complain
;
1111 if (time_before(complain
, jiffies
)) {
1112 dprintk("NFS: faulty NFS server %s:"
1113 " (committed = %d) != (stable = %d)\n",
1114 NFS_SERVER(data
->inode
)->nfs_client
->cl_hostname
,
1115 resp
->verf
->committed
, argp
->stable
);
1116 complain
= jiffies
+ 300 * HZ
;
1120 /* Is this a short write? */
1121 if (task
->tk_status
>= 0 && resp
->count
< argp
->count
) {
1122 static unsigned long complain
;
1124 nfs_inc_stats(data
->inode
, NFSIOS_SHORTWRITE
);
1126 /* Has the server at least made some progress? */
1127 if (resp
->count
!= 0) {
1128 /* Was this an NFSv2 write or an NFSv3 stable write? */
1129 if (resp
->verf
->committed
!= NFS_UNSTABLE
) {
1130 /* Resend from where the server left off */
1131 argp
->offset
+= resp
->count
;
1132 argp
->pgbase
+= resp
->count
;
1133 argp
->count
-= resp
->count
;
1135 /* Resend as a stable write in order to avoid
1136 * headaches in the case of a server crash.
1138 argp
->stable
= NFS_FILE_SYNC
;
1140 rpc_restart_call(task
);
1143 if (time_before(complain
, jiffies
)) {
1145 "NFS: Server wrote zero bytes, expected %u.\n",
1147 complain
= jiffies
+ 300 * HZ
;
1149 /* Can't do anything about it except throw an error. */
1150 task
->tk_status
= -EIO
;
1156 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1157 void nfs_commit_release(void *wdata
)
1159 nfs_commit_free(wdata
);
1163 * Set up the argument/result storage required for the RPC call.
1165 static void nfs_commit_rpcsetup(struct list_head
*head
,
1166 struct nfs_write_data
*data
,
1169 struct nfs_page
*first
;
1170 struct inode
*inode
;
1173 /* Set up the RPC argument and reply structs
1174 * NB: take care not to mess about with data->commit et al. */
1176 list_splice_init(head
, &data
->pages
);
1177 first
= nfs_list_entry(data
->pages
.next
);
1178 inode
= first
->wb_context
->dentry
->d_inode
;
1180 data
->inode
= inode
;
1181 data
->cred
= first
->wb_context
->cred
;
1183 data
->args
.fh
= NFS_FH(data
->inode
);
1184 /* Note: we always request a commit of the entire inode */
1185 data
->args
.offset
= 0;
1186 data
->args
.count
= 0;
1187 data
->res
.count
= 0;
1188 data
->res
.fattr
= &data
->fattr
;
1189 data
->res
.verf
= &data
->verf
;
1190 nfs_fattr_init(&data
->fattr
);
1192 /* Set up the initial task struct. */
1193 flags
= (how
& FLUSH_SYNC
) ? 0 : RPC_TASK_ASYNC
;
1194 rpc_init_task(&data
->task
, NFS_CLIENT(inode
), flags
, &nfs_commit_ops
, data
);
1195 NFS_PROTO(inode
)->commit_setup(data
, how
);
1197 data
->task
.tk_priority
= flush_task_priority(how
);
1198 data
->task
.tk_cookie
= (unsigned long)inode
;
1200 dprintk("NFS: %5u initiated commit call\n", data
->task
.tk_pid
);
1204 * Commit dirty pages
1207 nfs_commit_list(struct inode
*inode
, struct list_head
*head
, int how
)
1209 struct nfs_write_data
*data
;
1210 struct nfs_page
*req
;
1212 data
= nfs_commit_alloc();
1217 /* Set up the argument struct */
1218 nfs_commit_rpcsetup(head
, data
, how
);
1220 nfs_execute_write(data
);
1223 while (!list_empty(head
)) {
1224 req
= nfs_list_entry(head
->next
);
1225 nfs_list_remove_request(req
);
1226 nfs_mark_request_commit(req
);
1227 dec_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
1228 nfs_clear_page_writeback(req
);
1234 * COMMIT call returned
1236 static void nfs_commit_done(struct rpc_task
*task
, void *calldata
)
1238 struct nfs_write_data
*data
= calldata
;
1239 struct nfs_page
*req
;
1241 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1242 task
->tk_pid
, task
->tk_status
);
1244 /* Call the NFS version-specific code */
1245 if (NFS_PROTO(data
->inode
)->commit_done(task
, data
) != 0)
1248 while (!list_empty(&data
->pages
)) {
1249 req
= nfs_list_entry(data
->pages
.next
);
1250 nfs_list_remove_request(req
);
1251 dec_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
1253 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1254 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
1255 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
1257 (long long)req_offset(req
));
1258 if (task
->tk_status
< 0) {
1259 req
->wb_context
->error
= task
->tk_status
;
1260 nfs_inode_remove_request(req
);
1261 dprintk(", error = %d\n", task
->tk_status
);
1265 /* Okay, COMMIT succeeded, apparently. Check the verifier
1266 * returned by the server against all stored verfs. */
1267 if (!memcmp(req
->wb_verf
.verifier
, data
->verf
.verifier
, sizeof(data
->verf
.verifier
))) {
1268 /* We have a match */
1269 nfs_inode_remove_request(req
);
1273 /* We have a mismatch. Write the page again */
1274 dprintk(" mismatch\n");
1275 nfs_redirty_request(req
);
1277 nfs_clear_page_writeback(req
);
1281 static const struct rpc_call_ops nfs_commit_ops
= {
1282 .rpc_call_done
= nfs_commit_done
,
1283 .rpc_release
= nfs_commit_release
,
1286 static inline int nfs_commit_list(struct inode
*inode
, struct list_head
*head
, int how
)
1292 static long nfs_flush_mapping(struct address_space
*mapping
, struct writeback_control
*wbc
, int how
)
1294 struct nfs_inode
*nfsi
= NFS_I(mapping
->host
);
1298 spin_lock(&nfsi
->req_lock
);
1299 res
= nfs_scan_dirty(mapping
, wbc
, &head
);
1300 spin_unlock(&nfsi
->req_lock
);
1302 int error
= nfs_flush_list(mapping
->host
, &head
, res
, how
);
1309 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1310 int nfs_commit_inode(struct inode
*inode
, int how
)
1312 struct nfs_inode
*nfsi
= NFS_I(inode
);
1316 spin_lock(&nfsi
->req_lock
);
1317 res
= nfs_scan_commit(inode
, &head
, 0, 0);
1318 spin_unlock(&nfsi
->req_lock
);
1320 int error
= nfs_commit_list(inode
, &head
, how
);
1328 long nfs_sync_mapping_wait(struct address_space
*mapping
, struct writeback_control
*wbc
, int how
)
1330 struct inode
*inode
= mapping
->host
;
1331 struct nfs_inode
*nfsi
= NFS_I(inode
);
1332 unsigned long idx_start
, idx_end
;
1333 unsigned int npages
= 0;
1335 int nocommit
= how
& FLUSH_NOCOMMIT
;
1339 if (wbc
->range_cyclic
)
1342 idx_start
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
1343 idx_end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
1344 if (idx_end
> idx_start
) {
1345 unsigned long l_npages
= 1 + idx_end
- idx_start
;
1347 if (sizeof(npages
) != sizeof(l_npages
) &&
1348 (unsigned long)npages
!= l_npages
)
1352 how
&= ~FLUSH_NOCOMMIT
;
1353 spin_lock(&nfsi
->req_lock
);
1355 wbc
->pages_skipped
= 0;
1356 ret
= nfs_wait_on_requests_locked(inode
, idx_start
, npages
);
1359 pages
= nfs_scan_dirty(mapping
, wbc
, &head
);
1361 spin_unlock(&nfsi
->req_lock
);
1362 if (how
& FLUSH_INVALIDATE
) {
1363 nfs_cancel_dirty_list(&head
);
1366 ret
= nfs_flush_list(inode
, &head
, pages
, how
);
1367 spin_lock(&nfsi
->req_lock
);
1370 if (wbc
->pages_skipped
!= 0)
1374 pages
= nfs_scan_commit(inode
, &head
, idx_start
, npages
);
1376 if (wbc
->pages_skipped
!= 0)
1380 if (how
& FLUSH_INVALIDATE
) {
1381 spin_unlock(&nfsi
->req_lock
);
1382 nfs_cancel_commit_list(&head
);
1384 spin_lock(&nfsi
->req_lock
);
1387 pages
+= nfs_scan_commit(inode
, &head
, 0, 0);
1388 spin_unlock(&nfsi
->req_lock
);
1389 ret
= nfs_commit_list(inode
, &head
, how
);
1390 spin_lock(&nfsi
->req_lock
);
1392 spin_unlock(&nfsi
->req_lock
);
1397 * flush the inode to disk.
1399 int nfs_wb_all(struct inode
*inode
)
1401 struct address_space
*mapping
= inode
->i_mapping
;
1402 struct writeback_control wbc
= {
1403 .bdi
= mapping
->backing_dev_info
,
1404 .sync_mode
= WB_SYNC_ALL
,
1405 .nr_to_write
= LONG_MAX
,
1406 .for_writepages
= 1,
1411 ret
= generic_writepages(mapping
, &wbc
);
1414 ret
= nfs_sync_mapping_wait(mapping
, &wbc
, 0);
1418 __mark_inode_dirty(mapping
->host
, I_DIRTY_PAGES
);
1422 int nfs_sync_mapping_range(struct address_space
*mapping
, loff_t range_start
, loff_t range_end
, int how
)
1424 struct writeback_control wbc
= {
1425 .bdi
= mapping
->backing_dev_info
,
1426 .sync_mode
= WB_SYNC_ALL
,
1427 .nr_to_write
= LONG_MAX
,
1428 .range_start
= range_start
,
1429 .range_end
= range_end
,
1430 .for_writepages
= 1,
1434 if (!(how
& FLUSH_NOWRITEPAGE
)) {
1435 ret
= generic_writepages(mapping
, &wbc
);
1439 ret
= nfs_sync_mapping_wait(mapping
, &wbc
, how
);
1443 __mark_inode_dirty(mapping
->host
, I_DIRTY_PAGES
);
1447 int nfs_wb_page_priority(struct inode
*inode
, struct page
*page
, int how
)
1449 loff_t range_start
= page_offset(page
);
1450 loff_t range_end
= range_start
+ (loff_t
)(PAGE_CACHE_SIZE
- 1);
1451 struct writeback_control wbc
= {
1452 .bdi
= page
->mapping
->backing_dev_info
,
1453 .sync_mode
= WB_SYNC_ALL
,
1454 .nr_to_write
= LONG_MAX
,
1455 .range_start
= range_start
,
1456 .range_end
= range_end
,
1460 BUG_ON(!PageLocked(page
));
1461 if (!(how
& FLUSH_NOWRITEPAGE
) && clear_page_dirty_for_io(page
)) {
1462 ret
= nfs_writepage_locked(page
, &wbc
);
1466 if (!PagePrivate(page
))
1468 ret
= nfs_sync_mapping_wait(page
->mapping
, &wbc
, how
);
1472 __mark_inode_dirty(inode
, I_DIRTY_PAGES
);
1477 * Write back all requests on one page - we do this before reading it.
1479 int nfs_wb_page(struct inode
*inode
, struct page
* page
)
1481 return nfs_wb_page_priority(inode
, page
, FLUSH_STABLE
);
1484 int nfs_set_page_dirty(struct page
*page
)
1486 struct nfs_page
*req
;
1488 req
= nfs_page_find_request(page
);
1490 /* Mark any existing write requests for flushing */
1491 set_bit(PG_NEED_FLUSH
, &req
->wb_flags
);
1492 nfs_release_request(req
);
1494 return __set_page_dirty_nobuffers(page
);
1498 int __init
nfs_init_writepagecache(void)
1500 nfs_wdata_cachep
= kmem_cache_create("nfs_write_data",
1501 sizeof(struct nfs_write_data
),
1502 0, SLAB_HWCACHE_ALIGN
,
1504 if (nfs_wdata_cachep
== NULL
)
1507 nfs_wdata_mempool
= mempool_create_slab_pool(MIN_POOL_WRITE
,
1509 if (nfs_wdata_mempool
== NULL
)
1512 nfs_commit_mempool
= mempool_create_slab_pool(MIN_POOL_COMMIT
,
1514 if (nfs_commit_mempool
== NULL
)
1520 void nfs_destroy_writepagecache(void)
1522 mempool_destroy(nfs_commit_mempool
);
1523 mempool_destroy(nfs_wdata_mempool
);
1524 kmem_cache_destroy(nfs_wdata_cachep
);