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>
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_pageio_init_write(struct nfs_pageio_descriptor
*desc
,
41 struct inode
*inode
, int ioflags
);
42 static void nfs_redirty_request(struct nfs_page
*req
);
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_commitdata_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_free(struct nfs_write_data
*p
)
64 if (p
&& (p
->pagevec
!= &p
->page_array
[0]))
66 mempool_free(p
, nfs_commit_mempool
);
69 struct nfs_write_data
*nfs_writedata_alloc(unsigned int pagecount
)
71 struct nfs_write_data
*p
= mempool_alloc(nfs_wdata_mempool
, GFP_NOFS
);
74 memset(p
, 0, sizeof(*p
));
75 INIT_LIST_HEAD(&p
->pages
);
76 p
->npages
= pagecount
;
77 if (pagecount
<= ARRAY_SIZE(p
->page_array
))
78 p
->pagevec
= p
->page_array
;
80 p
->pagevec
= kcalloc(pagecount
, sizeof(struct page
*), GFP_NOFS
);
82 mempool_free(p
, nfs_wdata_mempool
);
90 static void nfs_writedata_free(struct nfs_write_data
*p
)
92 if (p
&& (p
->pagevec
!= &p
->page_array
[0]))
94 mempool_free(p
, nfs_wdata_mempool
);
97 void nfs_writedata_release(void *data
)
99 struct nfs_write_data
*wdata
= data
;
101 put_nfs_open_context(wdata
->args
.context
);
102 nfs_writedata_free(wdata
);
105 static void nfs_context_set_write_error(struct nfs_open_context
*ctx
, int error
)
109 set_bit(NFS_CONTEXT_ERROR_WRITE
, &ctx
->flags
);
112 static struct nfs_page
*nfs_page_find_request_locked(struct page
*page
)
114 struct nfs_page
*req
= NULL
;
116 if (PagePrivate(page
)) {
117 req
= (struct nfs_page
*)page_private(page
);
119 kref_get(&req
->wb_kref
);
124 static struct nfs_page
*nfs_page_find_request(struct page
*page
)
126 struct inode
*inode
= page
->mapping
->host
;
127 struct nfs_page
*req
= NULL
;
129 spin_lock(&inode
->i_lock
);
130 req
= nfs_page_find_request_locked(page
);
131 spin_unlock(&inode
->i_lock
);
135 /* Adjust the file length if we're writing beyond the end */
136 static void nfs_grow_file(struct page
*page
, unsigned int offset
, unsigned int count
)
138 struct inode
*inode
= page
->mapping
->host
;
139 loff_t end
, i_size
= i_size_read(inode
);
140 pgoff_t end_index
= (i_size
- 1) >> PAGE_CACHE_SHIFT
;
142 if (i_size
> 0 && page
->index
< end_index
)
144 end
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + ((loff_t
)offset
+count
);
147 nfs_inc_stats(inode
, NFSIOS_EXTENDWRITE
);
148 i_size_write(inode
, end
);
151 /* A writeback failed: mark the page as bad, and invalidate the page cache */
152 static void nfs_set_pageerror(struct page
*page
)
155 nfs_zap_mapping(page
->mapping
->host
, page
->mapping
);
158 /* We can set the PG_uptodate flag if we see that a write request
159 * covers the full page.
161 static void nfs_mark_uptodate(struct page
*page
, unsigned int base
, unsigned int count
)
163 if (PageUptodate(page
))
167 if (count
!= nfs_page_length(page
))
169 SetPageUptodate(page
);
172 static int nfs_writepage_setup(struct nfs_open_context
*ctx
, struct page
*page
,
173 unsigned int offset
, unsigned int count
)
175 struct nfs_page
*req
;
179 req
= nfs_update_request(ctx
, page
, offset
, count
);
185 ret
= nfs_wb_page(page
->mapping
->host
, page
);
189 /* Update file length */
190 nfs_grow_file(page
, offset
, count
);
191 nfs_mark_uptodate(page
, req
->wb_pgbase
, req
->wb_bytes
);
192 nfs_clear_page_tag_locked(req
);
196 static int wb_priority(struct writeback_control
*wbc
)
198 if (wbc
->for_reclaim
)
199 return FLUSH_HIGHPRI
| FLUSH_STABLE
;
200 if (wbc
->for_kupdate
)
206 * NFS congestion control
209 int nfs_congestion_kb
;
211 #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
212 #define NFS_CONGESTION_OFF_THRESH \
213 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
215 static int nfs_set_page_writeback(struct page
*page
)
217 int ret
= test_set_page_writeback(page
);
220 struct inode
*inode
= page
->mapping
->host
;
221 struct nfs_server
*nfss
= NFS_SERVER(inode
);
223 if (atomic_long_inc_return(&nfss
->writeback
) >
224 NFS_CONGESTION_ON_THRESH
)
225 set_bdi_congested(&nfss
->backing_dev_info
, WRITE
);
230 static void nfs_end_page_writeback(struct page
*page
)
232 struct inode
*inode
= page
->mapping
->host
;
233 struct nfs_server
*nfss
= NFS_SERVER(inode
);
235 end_page_writeback(page
);
236 if (atomic_long_dec_return(&nfss
->writeback
) < NFS_CONGESTION_OFF_THRESH
)
237 clear_bdi_congested(&nfss
->backing_dev_info
, WRITE
);
241 * Find an associated nfs write request, and prepare to flush it out
242 * May return an error if the user signalled nfs_wait_on_request().
244 static int nfs_page_async_flush(struct nfs_pageio_descriptor
*pgio
,
247 struct inode
*inode
= page
->mapping
->host
;
248 struct nfs_page
*req
;
251 spin_lock(&inode
->i_lock
);
253 req
= nfs_page_find_request_locked(page
);
255 spin_unlock(&inode
->i_lock
);
258 if (nfs_set_page_tag_locked(req
))
260 /* Note: If we hold the page lock, as is the case in nfs_writepage,
261 * then the call to nfs_set_page_tag_locked() will always
262 * succeed provided that someone hasn't already marked the
263 * request as dirty (in which case we don't care).
265 spin_unlock(&inode
->i_lock
);
266 ret
= nfs_wait_on_request(req
);
267 nfs_release_request(req
);
270 spin_lock(&inode
->i_lock
);
272 if (test_bit(PG_NEED_COMMIT
, &req
->wb_flags
)) {
273 /* This request is marked for commit */
274 spin_unlock(&inode
->i_lock
);
275 nfs_clear_page_tag_locked(req
);
276 nfs_pageio_complete(pgio
);
279 if (nfs_set_page_writeback(page
) != 0) {
280 spin_unlock(&inode
->i_lock
);
283 spin_unlock(&inode
->i_lock
);
284 if (!nfs_pageio_add_request(pgio
, req
)) {
285 nfs_redirty_request(req
);
286 return pgio
->pg_error
;
291 static int nfs_do_writepage(struct page
*page
, struct writeback_control
*wbc
, struct nfs_pageio_descriptor
*pgio
)
293 struct inode
*inode
= page
->mapping
->host
;
295 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGE
);
296 nfs_add_stats(inode
, NFSIOS_WRITEPAGES
, 1);
298 nfs_pageio_cond_complete(pgio
, page
->index
);
299 return nfs_page_async_flush(pgio
, page
);
303 * Write an mmapped page to the server.
305 static int nfs_writepage_locked(struct page
*page
, struct writeback_control
*wbc
)
307 struct nfs_pageio_descriptor pgio
;
310 nfs_pageio_init_write(&pgio
, page
->mapping
->host
, wb_priority(wbc
));
311 err
= nfs_do_writepage(page
, wbc
, &pgio
);
312 nfs_pageio_complete(&pgio
);
315 if (pgio
.pg_error
< 0)
316 return pgio
.pg_error
;
320 int nfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
324 ret
= nfs_writepage_locked(page
, wbc
);
329 static int nfs_writepages_callback(struct page
*page
, struct writeback_control
*wbc
, void *data
)
333 ret
= nfs_do_writepage(page
, wbc
, data
);
338 int nfs_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
340 struct inode
*inode
= mapping
->host
;
341 struct nfs_pageio_descriptor pgio
;
344 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGES
);
346 nfs_pageio_init_write(&pgio
, inode
, wb_priority(wbc
));
347 err
= write_cache_pages(mapping
, wbc
, nfs_writepages_callback
, &pgio
);
348 nfs_pageio_complete(&pgio
);
351 if (pgio
.pg_error
< 0)
352 return pgio
.pg_error
;
357 * Insert a write request into an inode
359 static void nfs_inode_add_request(struct inode
*inode
, struct nfs_page
*req
)
361 struct nfs_inode
*nfsi
= NFS_I(inode
);
364 error
= radix_tree_insert(&nfsi
->nfs_page_tree
, req
->wb_index
, req
);
368 if (nfs_have_delegation(inode
, FMODE_WRITE
))
371 SetPagePrivate(req
->wb_page
);
372 set_page_private(req
->wb_page
, (unsigned long)req
);
374 kref_get(&req
->wb_kref
);
375 radix_tree_tag_set(&nfsi
->nfs_page_tree
, req
->wb_index
,
376 NFS_PAGE_TAG_LOCKED
);
380 * Remove a write request from an inode
382 static void nfs_inode_remove_request(struct nfs_page
*req
)
384 struct inode
*inode
= req
->wb_context
->path
.dentry
->d_inode
;
385 struct nfs_inode
*nfsi
= NFS_I(inode
);
387 BUG_ON (!NFS_WBACK_BUSY(req
));
389 spin_lock(&inode
->i_lock
);
390 set_page_private(req
->wb_page
, 0);
391 ClearPagePrivate(req
->wb_page
);
392 radix_tree_delete(&nfsi
->nfs_page_tree
, req
->wb_index
);
395 spin_unlock(&inode
->i_lock
);
398 spin_unlock(&inode
->i_lock
);
399 nfs_clear_request(req
);
400 nfs_release_request(req
);
404 nfs_mark_request_dirty(struct nfs_page
*req
)
406 __set_page_dirty_nobuffers(req
->wb_page
);
410 * Check if a request is dirty
413 nfs_dirty_request(struct nfs_page
*req
)
415 struct page
*page
= req
->wb_page
;
417 if (page
== NULL
|| test_bit(PG_NEED_COMMIT
, &req
->wb_flags
))
419 return !PageWriteback(page
);
422 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
424 * Add a request to the inode's commit list.
427 nfs_mark_request_commit(struct nfs_page
*req
)
429 struct inode
*inode
= req
->wb_context
->path
.dentry
->d_inode
;
430 struct nfs_inode
*nfsi
= NFS_I(inode
);
432 spin_lock(&inode
->i_lock
);
434 set_bit(PG_NEED_COMMIT
, &(req
)->wb_flags
);
435 radix_tree_tag_set(&nfsi
->nfs_page_tree
,
437 NFS_PAGE_TAG_COMMIT
);
438 spin_unlock(&inode
->i_lock
);
439 inc_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
440 inc_bdi_stat(req
->wb_page
->mapping
->backing_dev_info
, BDI_RECLAIMABLE
);
441 __mark_inode_dirty(inode
, I_DIRTY_DATASYNC
);
445 int nfs_write_need_commit(struct nfs_write_data
*data
)
447 return data
->verf
.committed
!= NFS_FILE_SYNC
;
451 int nfs_reschedule_unstable_write(struct nfs_page
*req
)
453 if (test_bit(PG_NEED_COMMIT
, &req
->wb_flags
)) {
454 nfs_mark_request_commit(req
);
457 if (test_and_clear_bit(PG_NEED_RESCHED
, &req
->wb_flags
)) {
458 nfs_mark_request_dirty(req
);
465 nfs_mark_request_commit(struct nfs_page
*req
)
470 int nfs_write_need_commit(struct nfs_write_data
*data
)
476 int nfs_reschedule_unstable_write(struct nfs_page
*req
)
483 * Wait for a request to complete.
485 * Interruptible by fatal signals only.
487 static int nfs_wait_on_requests_locked(struct inode
*inode
, pgoff_t idx_start
, unsigned int npages
)
489 struct nfs_inode
*nfsi
= NFS_I(inode
);
490 struct nfs_page
*req
;
491 pgoff_t idx_end
, next
;
492 unsigned int res
= 0;
498 idx_end
= idx_start
+ npages
- 1;
501 while (radix_tree_gang_lookup_tag(&nfsi
->nfs_page_tree
, (void **)&req
, next
, 1, NFS_PAGE_TAG_LOCKED
)) {
502 if (req
->wb_index
> idx_end
)
505 next
= req
->wb_index
+ 1;
506 BUG_ON(!NFS_WBACK_BUSY(req
));
508 kref_get(&req
->wb_kref
);
509 spin_unlock(&inode
->i_lock
);
510 error
= nfs_wait_on_request(req
);
511 nfs_release_request(req
);
512 spin_lock(&inode
->i_lock
);
520 static void nfs_cancel_commit_list(struct list_head
*head
)
522 struct nfs_page
*req
;
524 while(!list_empty(head
)) {
525 req
= nfs_list_entry(head
->next
);
526 dec_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
527 dec_bdi_stat(req
->wb_page
->mapping
->backing_dev_info
,
529 nfs_list_remove_request(req
);
530 clear_bit(PG_NEED_COMMIT
, &(req
)->wb_flags
);
531 nfs_inode_remove_request(req
);
532 nfs_unlock_request(req
);
536 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
538 * nfs_scan_commit - Scan an inode for commit requests
539 * @inode: NFS inode to scan
540 * @dst: destination list
541 * @idx_start: lower bound of page->index to scan.
542 * @npages: idx_start + npages sets the upper bound to scan.
544 * Moves requests from the inode's 'commit' request list.
545 * The requests are *not* checked to ensure that they form a contiguous set.
548 nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
, pgoff_t idx_start
, unsigned int npages
)
550 struct nfs_inode
*nfsi
= NFS_I(inode
);
553 if (nfsi
->ncommit
!= 0) {
554 res
= nfs_scan_list(nfsi
, dst
, idx_start
, npages
,
555 NFS_PAGE_TAG_COMMIT
);
556 nfsi
->ncommit
-= res
;
561 static inline int nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
, pgoff_t idx_start
, unsigned int npages
)
568 * Try to update any existing write request, or create one if there is none.
569 * In order to match, the request's credentials must match those of
570 * the calling process.
572 * Note: Should always be called with the Page Lock held!
574 static struct nfs_page
* nfs_update_request(struct nfs_open_context
* ctx
,
575 struct page
*page
, unsigned int offset
, unsigned int bytes
)
577 struct address_space
*mapping
= page
->mapping
;
578 struct inode
*inode
= mapping
->host
;
579 struct nfs_page
*req
, *new = NULL
;
582 end
= offset
+ bytes
;
585 /* Loop over all inode entries and see if we find
586 * A request for the page we wish to update
588 spin_lock(&inode
->i_lock
);
589 req
= nfs_page_find_request_locked(page
);
591 if (!nfs_set_page_tag_locked(req
)) {
594 spin_unlock(&inode
->i_lock
);
595 error
= nfs_wait_on_request(req
);
596 nfs_release_request(req
);
599 radix_tree_preload_end();
600 nfs_release_request(new);
602 return ERR_PTR(error
);
606 spin_unlock(&inode
->i_lock
);
608 radix_tree_preload_end();
609 nfs_release_request(new);
615 nfs_lock_request_dontget(new);
616 nfs_inode_add_request(inode
, new);
617 spin_unlock(&inode
->i_lock
);
618 radix_tree_preload_end();
622 spin_unlock(&inode
->i_lock
);
624 new = nfs_create_request(ctx
, inode
, page
, offset
, bytes
);
627 if (radix_tree_preload(GFP_NOFS
)) {
628 nfs_release_request(new);
629 return ERR_PTR(-ENOMEM
);
633 /* We have a request for our page.
634 * If the creds don't match, or the
635 * page addresses don't match,
636 * tell the caller to wait on the conflicting
639 rqend
= req
->wb_offset
+ req
->wb_bytes
;
640 if (req
->wb_context
!= ctx
641 || req
->wb_page
!= page
642 || !nfs_dirty_request(req
)
643 || offset
> rqend
|| end
< req
->wb_offset
) {
644 nfs_clear_page_tag_locked(req
);
645 return ERR_PTR(-EBUSY
);
648 /* Okay, the request matches. Update the region */
649 if (offset
< req
->wb_offset
) {
650 req
->wb_offset
= offset
;
651 req
->wb_pgbase
= offset
;
652 req
->wb_bytes
= max(end
, rqend
) - req
->wb_offset
;
657 req
->wb_bytes
= end
- req
->wb_offset
;
663 int nfs_flush_incompatible(struct file
*file
, struct page
*page
)
665 struct nfs_open_context
*ctx
= nfs_file_open_context(file
);
666 struct nfs_page
*req
;
667 int do_flush
, status
;
669 * Look for a request corresponding to this page. If there
670 * is one, and it belongs to another file, we flush it out
671 * before we try to copy anything into the page. Do this
672 * due to the lack of an ACCESS-type call in NFSv2.
673 * Also do the same if we find a request from an existing
677 req
= nfs_page_find_request(page
);
680 do_flush
= req
->wb_page
!= page
|| req
->wb_context
!= ctx
681 || !nfs_dirty_request(req
);
682 nfs_release_request(req
);
685 status
= nfs_wb_page(page
->mapping
->host
, page
);
686 } while (status
== 0);
691 * If the page cache is marked as unsafe or invalid, then we can't rely on
692 * the PageUptodate() flag. In this case, we will need to turn off
693 * write optimisations that depend on the page contents being correct.
695 static int nfs_write_pageuptodate(struct page
*page
, struct inode
*inode
)
697 return PageUptodate(page
) &&
698 !(NFS_I(inode
)->cache_validity
& (NFS_INO_REVAL_PAGECACHE
|NFS_INO_INVALID_DATA
));
702 * Update and possibly write a cached page of an NFS file.
704 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
705 * things with a page scheduled for an RPC call (e.g. invalidate it).
707 int nfs_updatepage(struct file
*file
, struct page
*page
,
708 unsigned int offset
, unsigned int count
)
710 struct nfs_open_context
*ctx
= nfs_file_open_context(file
);
711 struct inode
*inode
= page
->mapping
->host
;
714 nfs_inc_stats(inode
, NFSIOS_VFSUPDATEPAGE
);
716 dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n",
717 file
->f_path
.dentry
->d_parent
->d_name
.name
,
718 file
->f_path
.dentry
->d_name
.name
, count
,
719 (long long)(page_offset(page
) + offset
));
721 /* If we're not using byte range locks, and we know the page
722 * is up to date, it may be more efficient to extend the write
723 * to cover the entire page in order to avoid fragmentation
726 if (nfs_write_pageuptodate(page
, inode
) &&
727 inode
->i_flock
== NULL
&&
728 !(file
->f_flags
& O_SYNC
)) {
729 count
= max(count
+ offset
, nfs_page_length(page
));
733 status
= nfs_writepage_setup(ctx
, page
, offset
, count
);
735 nfs_set_pageerror(page
);
737 __set_page_dirty_nobuffers(page
);
739 dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
740 status
, (long long)i_size_read(inode
));
744 static void nfs_writepage_release(struct nfs_page
*req
)
747 if (PageError(req
->wb_page
) || !nfs_reschedule_unstable_write(req
)) {
748 nfs_end_page_writeback(req
->wb_page
);
749 nfs_inode_remove_request(req
);
751 nfs_end_page_writeback(req
->wb_page
);
752 nfs_clear_page_tag_locked(req
);
755 static int flush_task_priority(int how
)
757 switch (how
& (FLUSH_HIGHPRI
|FLUSH_LOWPRI
)) {
759 return RPC_PRIORITY_HIGH
;
761 return RPC_PRIORITY_LOW
;
763 return RPC_PRIORITY_NORMAL
;
767 * Set up the argument/result storage required for the RPC call.
769 static int nfs_write_rpcsetup(struct nfs_page
*req
,
770 struct nfs_write_data
*data
,
771 const struct rpc_call_ops
*call_ops
,
772 unsigned int count
, unsigned int offset
,
775 struct inode
*inode
= req
->wb_context
->path
.dentry
->d_inode
;
776 int flags
= (how
& FLUSH_SYNC
) ? 0 : RPC_TASK_ASYNC
;
777 int priority
= flush_task_priority(how
);
778 struct rpc_task
*task
;
779 struct rpc_message msg
= {
780 .rpc_argp
= &data
->args
,
781 .rpc_resp
= &data
->res
,
782 .rpc_cred
= req
->wb_context
->cred
,
784 struct rpc_task_setup task_setup_data
= {
785 .rpc_client
= NFS_CLIENT(inode
),
788 .callback_ops
= call_ops
,
789 .callback_data
= data
,
790 .workqueue
= nfsiod_workqueue
,
792 .priority
= priority
,
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
->path
.dentry
->d_inode
;
800 data
->cred
= msg
.rpc_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
= get_nfs_open_context(req
->wb_context
);
808 data
->args
.stable
= NFS_UNSTABLE
;
809 if (how
& FLUSH_STABLE
) {
810 data
->args
.stable
= NFS_DATA_SYNC
;
811 if (!NFS_I(inode
)->ncommit
)
812 data
->args
.stable
= NFS_FILE_SYNC
;
815 data
->res
.fattr
= &data
->fattr
;
816 data
->res
.count
= count
;
817 data
->res
.verf
= &data
->verf
;
818 nfs_fattr_init(&data
->fattr
);
820 /* Set up the initial task struct. */
821 NFS_PROTO(inode
)->write_setup(data
, &msg
);
823 dprintk("NFS: %5u initiated write call "
824 "(req %s/%lld, %u bytes @ offset %llu)\n",
827 (long long)NFS_FILEID(inode
),
829 (unsigned long long)data
->args
.offset
);
831 task
= rpc_run_task(&task_setup_data
);
833 return PTR_ERR(task
);
838 /* If a nfs_flush_* function fails, it should remove reqs from @head and
839 * call this on each, which will prepare them to be retried on next
840 * writeback using standard nfs.
842 static void nfs_redirty_request(struct nfs_page
*req
)
844 nfs_mark_request_dirty(req
);
845 nfs_end_page_writeback(req
->wb_page
);
846 nfs_clear_page_tag_locked(req
);
850 * Generate multiple small requests to write out a single
851 * contiguous dirty area on one page.
853 static int nfs_flush_multi(struct inode
*inode
, struct list_head
*head
, unsigned int npages
, size_t count
, int how
)
855 struct nfs_page
*req
= nfs_list_entry(head
->next
);
856 struct page
*page
= req
->wb_page
;
857 struct nfs_write_data
*data
;
858 size_t wsize
= NFS_SERVER(inode
)->wsize
, nbytes
;
864 nfs_list_remove_request(req
);
868 size_t len
= min(nbytes
, wsize
);
870 data
= nfs_writedata_alloc(1);
873 list_add(&data
->pages
, &list
);
876 } while (nbytes
!= 0);
877 atomic_set(&req
->wb_complete
, requests
);
879 ClearPageError(page
);
885 data
= list_entry(list
.next
, struct nfs_write_data
, pages
);
886 list_del_init(&data
->pages
);
888 data
->pagevec
[0] = page
;
892 ret2
= nfs_write_rpcsetup(req
, data
, &nfs_write_partial_ops
,
898 } while (nbytes
!= 0);
903 while (!list_empty(&list
)) {
904 data
= list_entry(list
.next
, struct nfs_write_data
, pages
);
905 list_del(&data
->pages
);
906 nfs_writedata_release(data
);
908 nfs_redirty_request(req
);
913 * Create an RPC task for the given write request and kick it.
914 * The page must have been locked by the caller.
916 * It may happen that the page we're passed is not marked dirty.
917 * This is the case if nfs_updatepage detects a conflicting request
918 * that has been written but not committed.
920 static int nfs_flush_one(struct inode
*inode
, struct list_head
*head
, unsigned int npages
, size_t count
, int how
)
922 struct nfs_page
*req
;
924 struct nfs_write_data
*data
;
926 data
= nfs_writedata_alloc(npages
);
930 pages
= data
->pagevec
;
931 while (!list_empty(head
)) {
932 req
= nfs_list_entry(head
->next
);
933 nfs_list_remove_request(req
);
934 nfs_list_add_request(req
, &data
->pages
);
935 ClearPageError(req
->wb_page
);
936 *pages
++ = req
->wb_page
;
938 req
= nfs_list_entry(data
->pages
.next
);
940 /* Set up the argument struct */
941 return nfs_write_rpcsetup(req
, data
, &nfs_write_full_ops
, count
, 0, how
);
943 while (!list_empty(head
)) {
944 req
= nfs_list_entry(head
->next
);
945 nfs_list_remove_request(req
);
946 nfs_redirty_request(req
);
951 static void nfs_pageio_init_write(struct nfs_pageio_descriptor
*pgio
,
952 struct inode
*inode
, int ioflags
)
954 size_t wsize
= NFS_SERVER(inode
)->wsize
;
956 if (wsize
< PAGE_CACHE_SIZE
)
957 nfs_pageio_init(pgio
, inode
, nfs_flush_multi
, wsize
, ioflags
);
959 nfs_pageio_init(pgio
, inode
, nfs_flush_one
, wsize
, ioflags
);
963 * Handle a write reply that flushed part of a page.
965 static void nfs_writeback_done_partial(struct rpc_task
*task
, void *calldata
)
967 struct nfs_write_data
*data
= calldata
;
969 dprintk("NFS: %5u write(%s/%lld %d@%lld)",
971 data
->req
->wb_context
->path
.dentry
->d_inode
->i_sb
->s_id
,
973 NFS_FILEID(data
->req
->wb_context
->path
.dentry
->d_inode
),
974 data
->req
->wb_bytes
, (long long)req_offset(data
->req
));
976 nfs_writeback_done(task
, data
);
979 static void nfs_writeback_release_partial(void *calldata
)
981 struct nfs_write_data
*data
= calldata
;
982 struct nfs_page
*req
= data
->req
;
983 struct page
*page
= req
->wb_page
;
984 int status
= data
->task
.tk_status
;
987 nfs_set_pageerror(page
);
988 nfs_context_set_write_error(req
->wb_context
, status
);
989 dprintk(", error = %d\n", status
);
993 if (nfs_write_need_commit(data
)) {
994 struct inode
*inode
= page
->mapping
->host
;
996 spin_lock(&inode
->i_lock
);
997 if (test_bit(PG_NEED_RESCHED
, &req
->wb_flags
)) {
998 /* Do nothing we need to resend the writes */
999 } else if (!test_and_set_bit(PG_NEED_COMMIT
, &req
->wb_flags
)) {
1000 memcpy(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
));
1001 dprintk(" defer commit\n");
1002 } else if (memcmp(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
))) {
1003 set_bit(PG_NEED_RESCHED
, &req
->wb_flags
);
1004 clear_bit(PG_NEED_COMMIT
, &req
->wb_flags
);
1005 dprintk(" server reboot detected\n");
1007 spin_unlock(&inode
->i_lock
);
1012 if (atomic_dec_and_test(&req
->wb_complete
))
1013 nfs_writepage_release(req
);
1014 nfs_writedata_release(calldata
);
1017 static const struct rpc_call_ops nfs_write_partial_ops
= {
1018 .rpc_call_done
= nfs_writeback_done_partial
,
1019 .rpc_release
= nfs_writeback_release_partial
,
1023 * Handle a write reply that flushes a whole page.
1025 * FIXME: There is an inherent race with invalidate_inode_pages and
1026 * writebacks since the page->count is kept > 1 for as long
1027 * as the page has a write request pending.
1029 static void nfs_writeback_done_full(struct rpc_task
*task
, void *calldata
)
1031 struct nfs_write_data
*data
= calldata
;
1033 nfs_writeback_done(task
, data
);
1036 static void nfs_writeback_release_full(void *calldata
)
1038 struct nfs_write_data
*data
= calldata
;
1039 int status
= data
->task
.tk_status
;
1041 /* Update attributes as result of writeback. */
1042 while (!list_empty(&data
->pages
)) {
1043 struct nfs_page
*req
= nfs_list_entry(data
->pages
.next
);
1044 struct page
*page
= req
->wb_page
;
1046 nfs_list_remove_request(req
);
1048 dprintk("NFS: %5u write (%s/%lld %d@%lld)",
1050 req
->wb_context
->path
.dentry
->d_inode
->i_sb
->s_id
,
1051 (long long)NFS_FILEID(req
->wb_context
->path
.dentry
->d_inode
),
1053 (long long)req_offset(req
));
1056 nfs_set_pageerror(page
);
1057 nfs_context_set_write_error(req
->wb_context
, status
);
1058 dprintk(", error = %d\n", status
);
1059 goto remove_request
;
1062 if (nfs_write_need_commit(data
)) {
1063 memcpy(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
));
1064 nfs_mark_request_commit(req
);
1065 nfs_end_page_writeback(page
);
1066 dprintk(" marked for commit\n");
1071 nfs_end_page_writeback(page
);
1072 nfs_inode_remove_request(req
);
1074 nfs_clear_page_tag_locked(req
);
1076 nfs_writedata_release(calldata
);
1079 static const struct rpc_call_ops nfs_write_full_ops
= {
1080 .rpc_call_done
= nfs_writeback_done_full
,
1081 .rpc_release
= nfs_writeback_release_full
,
1086 * This function is called when the WRITE call is complete.
1088 int nfs_writeback_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
1090 struct nfs_writeargs
*argp
= &data
->args
;
1091 struct nfs_writeres
*resp
= &data
->res
;
1094 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1095 task
->tk_pid
, task
->tk_status
);
1098 * ->write_done will attempt to use post-op attributes to detect
1099 * conflicting writes by other clients. A strict interpretation
1100 * of close-to-open would allow us to continue caching even if
1101 * another writer had changed the file, but some applications
1102 * depend on tighter cache coherency when writing.
1104 status
= NFS_PROTO(data
->inode
)->write_done(task
, data
);
1107 nfs_add_stats(data
->inode
, NFSIOS_SERVERWRITTENBYTES
, resp
->count
);
1109 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1110 if (resp
->verf
->committed
< argp
->stable
&& task
->tk_status
>= 0) {
1111 /* We tried a write call, but the server did not
1112 * commit data to stable storage even though we
1114 * Note: There is a known bug in Tru64 < 5.0 in which
1115 * the server reports NFS_DATA_SYNC, but performs
1116 * NFS_FILE_SYNC. We therefore implement this checking
1117 * as a dprintk() in order to avoid filling syslog.
1119 static unsigned long complain
;
1121 if (time_before(complain
, jiffies
)) {
1122 dprintk("NFS: faulty NFS server %s:"
1123 " (committed = %d) != (stable = %d)\n",
1124 NFS_SERVER(data
->inode
)->nfs_client
->cl_hostname
,
1125 resp
->verf
->committed
, argp
->stable
);
1126 complain
= jiffies
+ 300 * HZ
;
1130 /* Is this a short write? */
1131 if (task
->tk_status
>= 0 && resp
->count
< argp
->count
) {
1132 static unsigned long complain
;
1134 nfs_inc_stats(data
->inode
, NFSIOS_SHORTWRITE
);
1136 /* Has the server at least made some progress? */
1137 if (resp
->count
!= 0) {
1138 /* Was this an NFSv2 write or an NFSv3 stable write? */
1139 if (resp
->verf
->committed
!= NFS_UNSTABLE
) {
1140 /* Resend from where the server left off */
1141 argp
->offset
+= resp
->count
;
1142 argp
->pgbase
+= resp
->count
;
1143 argp
->count
-= resp
->count
;
1145 /* Resend as a stable write in order to avoid
1146 * headaches in the case of a server crash.
1148 argp
->stable
= NFS_FILE_SYNC
;
1150 rpc_restart_call(task
);
1153 if (time_before(complain
, jiffies
)) {
1155 "NFS: Server wrote zero bytes, expected %u.\n",
1157 complain
= jiffies
+ 300 * HZ
;
1159 /* Can't do anything about it except throw an error. */
1160 task
->tk_status
= -EIO
;
1166 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1167 void nfs_commitdata_release(void *data
)
1169 struct nfs_write_data
*wdata
= data
;
1171 put_nfs_open_context(wdata
->args
.context
);
1172 nfs_commit_free(wdata
);
1176 * Set up the argument/result storage required for the RPC call.
1178 static int nfs_commit_rpcsetup(struct list_head
*head
,
1179 struct nfs_write_data
*data
,
1182 struct nfs_page
*first
= nfs_list_entry(head
->next
);
1183 struct inode
*inode
= first
->wb_context
->path
.dentry
->d_inode
;
1184 int flags
= (how
& FLUSH_SYNC
) ? 0 : RPC_TASK_ASYNC
;
1185 int priority
= flush_task_priority(how
);
1186 struct rpc_task
*task
;
1187 struct rpc_message msg
= {
1188 .rpc_argp
= &data
->args
,
1189 .rpc_resp
= &data
->res
,
1190 .rpc_cred
= first
->wb_context
->cred
,
1192 struct rpc_task_setup task_setup_data
= {
1193 .task
= &data
->task
,
1194 .rpc_client
= NFS_CLIENT(inode
),
1195 .rpc_message
= &msg
,
1196 .callback_ops
= &nfs_commit_ops
,
1197 .callback_data
= data
,
1198 .workqueue
= nfsiod_workqueue
,
1200 .priority
= priority
,
1203 /* Set up the RPC argument and reply structs
1204 * NB: take care not to mess about with data->commit et al. */
1206 list_splice_init(head
, &data
->pages
);
1208 data
->inode
= inode
;
1209 data
->cred
= msg
.rpc_cred
;
1211 data
->args
.fh
= NFS_FH(data
->inode
);
1212 /* Note: we always request a commit of the entire inode */
1213 data
->args
.offset
= 0;
1214 data
->args
.count
= 0;
1215 data
->args
.context
= get_nfs_open_context(first
->wb_context
);
1216 data
->res
.count
= 0;
1217 data
->res
.fattr
= &data
->fattr
;
1218 data
->res
.verf
= &data
->verf
;
1219 nfs_fattr_init(&data
->fattr
);
1221 /* Set up the initial task struct. */
1222 NFS_PROTO(inode
)->commit_setup(data
, &msg
);
1224 dprintk("NFS: %5u initiated commit call\n", data
->task
.tk_pid
);
1226 task
= rpc_run_task(&task_setup_data
);
1228 return PTR_ERR(task
);
1234 * Commit dirty pages
1237 nfs_commit_list(struct inode
*inode
, struct list_head
*head
, int how
)
1239 struct nfs_write_data
*data
;
1240 struct nfs_page
*req
;
1242 data
= nfs_commitdata_alloc();
1247 /* Set up the argument struct */
1248 return nfs_commit_rpcsetup(head
, data
, how
);
1250 while (!list_empty(head
)) {
1251 req
= nfs_list_entry(head
->next
);
1252 nfs_list_remove_request(req
);
1253 nfs_mark_request_commit(req
);
1254 dec_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
1255 dec_bdi_stat(req
->wb_page
->mapping
->backing_dev_info
,
1257 nfs_clear_page_tag_locked(req
);
1263 * COMMIT call returned
1265 static void nfs_commit_done(struct rpc_task
*task
, void *calldata
)
1267 struct nfs_write_data
*data
= calldata
;
1269 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1270 task
->tk_pid
, task
->tk_status
);
1272 /* Call the NFS version-specific code */
1273 if (NFS_PROTO(data
->inode
)->commit_done(task
, data
) != 0)
1277 static void nfs_commit_release(void *calldata
)
1279 struct nfs_write_data
*data
= calldata
;
1280 struct nfs_page
*req
;
1281 int status
= data
->task
.tk_status
;
1283 while (!list_empty(&data
->pages
)) {
1284 req
= nfs_list_entry(data
->pages
.next
);
1285 nfs_list_remove_request(req
);
1286 clear_bit(PG_NEED_COMMIT
, &(req
)->wb_flags
);
1287 dec_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
1288 dec_bdi_stat(req
->wb_page
->mapping
->backing_dev_info
,
1291 dprintk("NFS: commit (%s/%lld %d@%lld)",
1292 req
->wb_context
->path
.dentry
->d_inode
->i_sb
->s_id
,
1293 (long long)NFS_FILEID(req
->wb_context
->path
.dentry
->d_inode
),
1295 (long long)req_offset(req
));
1297 nfs_context_set_write_error(req
->wb_context
, status
);
1298 nfs_inode_remove_request(req
);
1299 dprintk(", error = %d\n", status
);
1303 /* Okay, COMMIT succeeded, apparently. Check the verifier
1304 * returned by the server against all stored verfs. */
1305 if (!memcmp(req
->wb_verf
.verifier
, data
->verf
.verifier
, sizeof(data
->verf
.verifier
))) {
1306 /* We have a match */
1307 nfs_inode_remove_request(req
);
1311 /* We have a mismatch. Write the page again */
1312 dprintk(" mismatch\n");
1313 nfs_mark_request_dirty(req
);
1315 nfs_clear_page_tag_locked(req
);
1317 nfs_commitdata_release(calldata
);
1320 static const struct rpc_call_ops nfs_commit_ops
= {
1321 .rpc_call_done
= nfs_commit_done
,
1322 .rpc_release
= nfs_commit_release
,
1325 int nfs_commit_inode(struct inode
*inode
, int how
)
1330 spin_lock(&inode
->i_lock
);
1331 res
= nfs_scan_commit(inode
, &head
, 0, 0);
1332 spin_unlock(&inode
->i_lock
);
1334 int error
= nfs_commit_list(inode
, &head
, how
);
1341 static inline int nfs_commit_list(struct inode
*inode
, struct list_head
*head
, int how
)
1347 long nfs_sync_mapping_wait(struct address_space
*mapping
, struct writeback_control
*wbc
, int how
)
1349 struct inode
*inode
= mapping
->host
;
1350 pgoff_t idx_start
, idx_end
;
1351 unsigned int npages
= 0;
1353 int nocommit
= how
& FLUSH_NOCOMMIT
;
1357 if (wbc
->range_cyclic
)
1360 idx_start
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
1361 idx_end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
1362 if (idx_end
> idx_start
) {
1363 pgoff_t l_npages
= 1 + idx_end
- idx_start
;
1365 if (sizeof(npages
) != sizeof(l_npages
) &&
1366 (pgoff_t
)npages
!= l_npages
)
1370 how
&= ~FLUSH_NOCOMMIT
;
1371 spin_lock(&inode
->i_lock
);
1373 ret
= nfs_wait_on_requests_locked(inode
, idx_start
, npages
);
1378 pages
= nfs_scan_commit(inode
, &head
, idx_start
, npages
);
1381 if (how
& FLUSH_INVALIDATE
) {
1382 spin_unlock(&inode
->i_lock
);
1383 nfs_cancel_commit_list(&head
);
1385 spin_lock(&inode
->i_lock
);
1388 pages
+= nfs_scan_commit(inode
, &head
, 0, 0);
1389 spin_unlock(&inode
->i_lock
);
1390 ret
= nfs_commit_list(inode
, &head
, how
);
1391 spin_lock(&inode
->i_lock
);
1394 spin_unlock(&inode
->i_lock
);
1398 static int __nfs_write_mapping(struct address_space
*mapping
, struct writeback_control
*wbc
, int how
)
1402 ret
= nfs_writepages(mapping
, wbc
);
1405 ret
= nfs_sync_mapping_wait(mapping
, wbc
, how
);
1410 __mark_inode_dirty(mapping
->host
, I_DIRTY_PAGES
);
1414 /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
1415 static int nfs_write_mapping(struct address_space
*mapping
, int how
)
1417 struct writeback_control wbc
= {
1418 .bdi
= mapping
->backing_dev_info
,
1419 .sync_mode
= WB_SYNC_NONE
,
1420 .nr_to_write
= LONG_MAX
,
1421 .for_writepages
= 1,
1426 ret
= __nfs_write_mapping(mapping
, &wbc
, how
);
1429 wbc
.sync_mode
= WB_SYNC_ALL
;
1430 return __nfs_write_mapping(mapping
, &wbc
, how
);
1434 * flush the inode to disk.
1436 int nfs_wb_all(struct inode
*inode
)
1438 return nfs_write_mapping(inode
->i_mapping
, 0);
1441 int nfs_wb_nocommit(struct inode
*inode
)
1443 return nfs_write_mapping(inode
->i_mapping
, FLUSH_NOCOMMIT
);
1446 int nfs_wb_page_cancel(struct inode
*inode
, struct page
*page
)
1448 struct nfs_page
*req
;
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
));
1462 req
= nfs_page_find_request(page
);
1465 if (test_bit(PG_NEED_COMMIT
, &req
->wb_flags
)) {
1466 nfs_release_request(req
);
1469 if (nfs_lock_request_dontget(req
)) {
1470 nfs_inode_remove_request(req
);
1472 * In case nfs_inode_remove_request has marked the
1473 * page as being dirty
1475 cancel_dirty_page(page
, PAGE_CACHE_SIZE
);
1476 nfs_unlock_request(req
);
1479 ret
= nfs_wait_on_request(req
);
1483 if (!PagePrivate(page
))
1485 ret
= nfs_sync_mapping_wait(page
->mapping
, &wbc
, FLUSH_INVALIDATE
);
1490 static int nfs_wb_page_priority(struct inode
*inode
, struct page
*page
,
1493 loff_t range_start
= page_offset(page
);
1494 loff_t range_end
= range_start
+ (loff_t
)(PAGE_CACHE_SIZE
- 1);
1495 struct writeback_control wbc
= {
1496 .bdi
= page
->mapping
->backing_dev_info
,
1497 .sync_mode
= WB_SYNC_ALL
,
1498 .nr_to_write
= LONG_MAX
,
1499 .range_start
= range_start
,
1500 .range_end
= range_end
,
1505 if (clear_page_dirty_for_io(page
)) {
1506 ret
= nfs_writepage_locked(page
, &wbc
);
1509 } else if (!PagePrivate(page
))
1511 ret
= nfs_sync_mapping_wait(page
->mapping
, &wbc
, how
);
1514 } while (PagePrivate(page
));
1517 __mark_inode_dirty(inode
, I_DIRTY_PAGES
);
1522 * Write back all requests on one page - we do this before reading it.
1524 int nfs_wb_page(struct inode
*inode
, struct page
* page
)
1526 return nfs_wb_page_priority(inode
, page
, FLUSH_STABLE
);
1529 int __init
nfs_init_writepagecache(void)
1531 nfs_wdata_cachep
= kmem_cache_create("nfs_write_data",
1532 sizeof(struct nfs_write_data
),
1533 0, SLAB_HWCACHE_ALIGN
,
1535 if (nfs_wdata_cachep
== NULL
)
1538 nfs_wdata_mempool
= mempool_create_slab_pool(MIN_POOL_WRITE
,
1540 if (nfs_wdata_mempool
== NULL
)
1543 nfs_commit_mempool
= mempool_create_slab_pool(MIN_POOL_COMMIT
,
1545 if (nfs_commit_mempool
== NULL
)
1549 * NFS congestion size, scale with available memory.
1561 * This allows larger machines to have larger/more transfers.
1562 * Limit the default to 256M
1564 nfs_congestion_kb
= (16*int_sqrt(totalram_pages
)) << (PAGE_SHIFT
-10);
1565 if (nfs_congestion_kb
> 256*1024)
1566 nfs_congestion_kb
= 256*1024;
1571 void nfs_destroy_writepagecache(void)
1573 mempool_destroy(nfs_commit_mempool
);
1574 mempool_destroy(nfs_wdata_mempool
);
1575 kmem_cache_destroy(nfs_wdata_cachep
);