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_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_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_clear_page_tag_locked(req
);
195 static int wb_priority(struct writeback_control
*wbc
)
197 if (wbc
->for_reclaim
)
198 return FLUSH_HIGHPRI
| FLUSH_STABLE
;
199 if (wbc
->for_kupdate
)
205 * NFS congestion control
208 int nfs_congestion_kb
;
210 #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
211 #define NFS_CONGESTION_OFF_THRESH \
212 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
214 static int nfs_set_page_writeback(struct page
*page
)
216 int ret
= test_set_page_writeback(page
);
219 struct inode
*inode
= page
->mapping
->host
;
220 struct nfs_server
*nfss
= NFS_SERVER(inode
);
222 if (atomic_long_inc_return(&nfss
->writeback
) >
223 NFS_CONGESTION_ON_THRESH
)
224 set_bdi_congested(&nfss
->backing_dev_info
, WRITE
);
229 static void nfs_end_page_writeback(struct page
*page
)
231 struct inode
*inode
= page
->mapping
->host
;
232 struct nfs_server
*nfss
= NFS_SERVER(inode
);
234 end_page_writeback(page
);
235 if (atomic_long_dec_return(&nfss
->writeback
) < NFS_CONGESTION_OFF_THRESH
)
236 clear_bdi_congested(&nfss
->backing_dev_info
, WRITE
);
240 * Find an associated nfs write request, and prepare to flush it out
241 * May return an error if the user signalled nfs_wait_on_request().
243 static int nfs_page_async_flush(struct nfs_pageio_descriptor
*pgio
,
246 struct inode
*inode
= page
->mapping
->host
;
247 struct nfs_page
*req
;
250 spin_lock(&inode
->i_lock
);
252 req
= nfs_page_find_request_locked(page
);
254 spin_unlock(&inode
->i_lock
);
257 if (nfs_set_page_tag_locked(req
))
259 /* Note: If we hold the page lock, as is the case in nfs_writepage,
260 * then the call to nfs_set_page_tag_locked() will always
261 * succeed provided that someone hasn't already marked the
262 * request as dirty (in which case we don't care).
264 spin_unlock(&inode
->i_lock
);
265 ret
= nfs_wait_on_request(req
);
266 nfs_release_request(req
);
269 spin_lock(&inode
->i_lock
);
271 if (test_bit(PG_NEED_COMMIT
, &req
->wb_flags
)) {
272 /* This request is marked for commit */
273 spin_unlock(&inode
->i_lock
);
274 nfs_clear_page_tag_locked(req
);
275 nfs_pageio_complete(pgio
);
278 if (nfs_set_page_writeback(page
) != 0) {
279 spin_unlock(&inode
->i_lock
);
282 spin_unlock(&inode
->i_lock
);
283 if (!nfs_pageio_add_request(pgio
, req
)) {
284 nfs_redirty_request(req
);
285 return pgio
->pg_error
;
290 static int nfs_do_writepage(struct page
*page
, struct writeback_control
*wbc
, struct nfs_pageio_descriptor
*pgio
)
292 struct inode
*inode
= page
->mapping
->host
;
294 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGE
);
295 nfs_add_stats(inode
, NFSIOS_WRITEPAGES
, 1);
297 nfs_pageio_cond_complete(pgio
, page
->index
);
298 return nfs_page_async_flush(pgio
, page
);
302 * Write an mmapped page to the server.
304 static int nfs_writepage_locked(struct page
*page
, struct writeback_control
*wbc
)
306 struct nfs_pageio_descriptor pgio
;
309 nfs_pageio_init_write(&pgio
, page
->mapping
->host
, wb_priority(wbc
));
310 err
= nfs_do_writepage(page
, wbc
, &pgio
);
311 nfs_pageio_complete(&pgio
);
314 if (pgio
.pg_error
< 0)
315 return pgio
.pg_error
;
319 int nfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
323 ret
= nfs_writepage_locked(page
, wbc
);
328 static int nfs_writepages_callback(struct page
*page
, struct writeback_control
*wbc
, void *data
)
332 ret
= nfs_do_writepage(page
, wbc
, data
);
337 int nfs_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
339 struct inode
*inode
= mapping
->host
;
340 struct nfs_pageio_descriptor pgio
;
343 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGES
);
345 nfs_pageio_init_write(&pgio
, inode
, wb_priority(wbc
));
346 err
= write_cache_pages(mapping
, wbc
, nfs_writepages_callback
, &pgio
);
347 nfs_pageio_complete(&pgio
);
350 if (pgio
.pg_error
< 0)
351 return pgio
.pg_error
;
356 * Insert a write request into an inode
358 static void nfs_inode_add_request(struct inode
*inode
, struct nfs_page
*req
)
360 struct nfs_inode
*nfsi
= NFS_I(inode
);
363 error
= radix_tree_insert(&nfsi
->nfs_page_tree
, req
->wb_index
, req
);
367 if (nfs_have_delegation(inode
, FMODE_WRITE
))
370 SetPagePrivate(req
->wb_page
);
371 set_page_private(req
->wb_page
, (unsigned long)req
);
373 kref_get(&req
->wb_kref
);
374 radix_tree_tag_set(&nfsi
->nfs_page_tree
, req
->wb_index
,
375 NFS_PAGE_TAG_LOCKED
);
379 * Remove a write request from an inode
381 static void nfs_inode_remove_request(struct nfs_page
*req
)
383 struct inode
*inode
= req
->wb_context
->path
.dentry
->d_inode
;
384 struct nfs_inode
*nfsi
= NFS_I(inode
);
386 BUG_ON (!NFS_WBACK_BUSY(req
));
388 spin_lock(&inode
->i_lock
);
389 set_page_private(req
->wb_page
, 0);
390 ClearPagePrivate(req
->wb_page
);
391 radix_tree_delete(&nfsi
->nfs_page_tree
, req
->wb_index
);
394 spin_unlock(&inode
->i_lock
);
397 spin_unlock(&inode
->i_lock
);
398 nfs_clear_request(req
);
399 nfs_release_request(req
);
403 nfs_mark_request_dirty(struct nfs_page
*req
)
405 __set_page_dirty_nobuffers(req
->wb_page
);
409 * Check if a request is dirty
412 nfs_dirty_request(struct nfs_page
*req
)
414 struct page
*page
= req
->wb_page
;
416 if (page
== NULL
|| test_bit(PG_NEED_COMMIT
, &req
->wb_flags
))
418 return !PageWriteback(req
->wb_page
);
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
->path
.dentry
->d_inode
;
429 struct nfs_inode
*nfsi
= NFS_I(inode
);
431 spin_lock(&inode
->i_lock
);
433 set_bit(PG_NEED_COMMIT
, &(req
)->wb_flags
);
434 radix_tree_tag_set(&nfsi
->nfs_page_tree
,
436 NFS_PAGE_TAG_COMMIT
);
437 spin_unlock(&inode
->i_lock
);
438 inc_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
439 inc_bdi_stat(req
->wb_page
->mapping
->backing_dev_info
, BDI_RECLAIMABLE
);
440 __mark_inode_dirty(inode
, I_DIRTY_DATASYNC
);
444 int nfs_write_need_commit(struct nfs_write_data
*data
)
446 return data
->verf
.committed
!= NFS_FILE_SYNC
;
450 int nfs_reschedule_unstable_write(struct nfs_page
*req
)
452 if (test_bit(PG_NEED_COMMIT
, &req
->wb_flags
)) {
453 nfs_mark_request_commit(req
);
456 if (test_and_clear_bit(PG_NEED_RESCHED
, &req
->wb_flags
)) {
457 nfs_mark_request_dirty(req
);
464 nfs_mark_request_commit(struct nfs_page
*req
)
469 int nfs_write_need_commit(struct nfs_write_data
*data
)
475 int nfs_reschedule_unstable_write(struct nfs_page
*req
)
482 * Wait for a request to complete.
484 * Interruptible by fatal signals only.
486 static int nfs_wait_on_requests_locked(struct inode
*inode
, pgoff_t idx_start
, unsigned int npages
)
488 struct nfs_inode
*nfsi
= NFS_I(inode
);
489 struct nfs_page
*req
;
490 pgoff_t idx_end
, next
;
491 unsigned int res
= 0;
497 idx_end
= idx_start
+ npages
- 1;
500 while (radix_tree_gang_lookup_tag(&nfsi
->nfs_page_tree
, (void **)&req
, next
, 1, NFS_PAGE_TAG_LOCKED
)) {
501 if (req
->wb_index
> idx_end
)
504 next
= req
->wb_index
+ 1;
505 BUG_ON(!NFS_WBACK_BUSY(req
));
507 kref_get(&req
->wb_kref
);
508 spin_unlock(&inode
->i_lock
);
509 error
= nfs_wait_on_request(req
);
510 nfs_release_request(req
);
511 spin_lock(&inode
->i_lock
);
519 static void nfs_cancel_commit_list(struct list_head
*head
)
521 struct nfs_page
*req
;
523 while(!list_empty(head
)) {
524 req
= nfs_list_entry(head
->next
);
525 dec_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
526 dec_bdi_stat(req
->wb_page
->mapping
->backing_dev_info
,
528 nfs_list_remove_request(req
);
529 clear_bit(PG_NEED_COMMIT
, &(req
)->wb_flags
);
530 nfs_inode_remove_request(req
);
531 nfs_unlock_request(req
);
535 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
537 * nfs_scan_commit - Scan an inode for commit requests
538 * @inode: NFS inode to scan
539 * @dst: destination list
540 * @idx_start: lower bound of page->index to scan.
541 * @npages: idx_start + npages sets the upper bound to scan.
543 * Moves requests from the inode's 'commit' request list.
544 * The requests are *not* checked to ensure that they form a contiguous set.
547 nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
, pgoff_t idx_start
, unsigned int npages
)
549 struct nfs_inode
*nfsi
= NFS_I(inode
);
552 if (nfsi
->ncommit
!= 0) {
553 res
= nfs_scan_list(nfsi
, dst
, idx_start
, npages
,
554 NFS_PAGE_TAG_COMMIT
);
555 nfsi
->ncommit
-= res
;
560 static inline int nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
, pgoff_t idx_start
, unsigned int npages
)
567 * Try to update any existing write request, or create one if there is none.
568 * In order to match, the request's credentials must match those of
569 * the calling process.
571 * Note: Should always be called with the Page Lock held!
573 static struct nfs_page
* nfs_update_request(struct nfs_open_context
* ctx
,
574 struct page
*page
, unsigned int offset
, unsigned int bytes
)
576 struct address_space
*mapping
= page
->mapping
;
577 struct inode
*inode
= mapping
->host
;
578 struct nfs_page
*req
, *new = NULL
;
581 end
= offset
+ bytes
;
584 /* Loop over all inode entries and see if we find
585 * A request for the page we wish to update
588 if (radix_tree_preload(GFP_NOFS
)) {
589 nfs_release_request(new);
590 return ERR_PTR(-ENOMEM
);
594 spin_lock(&inode
->i_lock
);
595 req
= nfs_page_find_request_locked(page
);
597 if (!nfs_set_page_tag_locked(req
)) {
600 spin_unlock(&inode
->i_lock
);
601 error
= nfs_wait_on_request(req
);
602 nfs_release_request(req
);
605 radix_tree_preload_end();
606 nfs_release_request(new);
608 return ERR_PTR(error
);
612 spin_unlock(&inode
->i_lock
);
614 radix_tree_preload_end();
615 nfs_release_request(new);
621 nfs_lock_request_dontget(new);
622 nfs_inode_add_request(inode
, new);
623 spin_unlock(&inode
->i_lock
);
624 radix_tree_preload_end();
628 spin_unlock(&inode
->i_lock
);
630 new = nfs_create_request(ctx
, inode
, page
, offset
, bytes
);
635 /* We have a request for our page.
636 * If the creds don't match, or the
637 * page addresses don't match,
638 * tell the caller to wait on the conflicting
641 rqend
= req
->wb_offset
+ req
->wb_bytes
;
642 if (req
->wb_context
!= ctx
643 || req
->wb_page
!= page
644 || !nfs_dirty_request(req
)
645 || offset
> rqend
|| end
< req
->wb_offset
) {
646 nfs_clear_page_tag_locked(req
);
647 return ERR_PTR(-EBUSY
);
650 /* Okay, the request matches. Update the region */
651 if (offset
< req
->wb_offset
) {
652 req
->wb_offset
= offset
;
653 req
->wb_pgbase
= offset
;
654 req
->wb_bytes
= max(end
, rqend
) - req
->wb_offset
;
659 req
->wb_bytes
= end
- req
->wb_offset
;
663 /* If this page might potentially be marked as up to date,
664 * then we need to zero any uninitalised data. */
665 if (req
->wb_pgbase
== 0 && req
->wb_bytes
!= PAGE_CACHE_SIZE
666 && !PageUptodate(req
->wb_page
))
667 zero_user_segment(req
->wb_page
, req
->wb_bytes
, PAGE_CACHE_SIZE
);
671 int nfs_flush_incompatible(struct file
*file
, struct page
*page
)
673 struct nfs_open_context
*ctx
= nfs_file_open_context(file
);
674 struct nfs_page
*req
;
675 int do_flush
, status
;
677 * Look for a request corresponding to this page. If there
678 * is one, and it belongs to another file, we flush it out
679 * before we try to copy anything into the page. Do this
680 * due to the lack of an ACCESS-type call in NFSv2.
681 * Also do the same if we find a request from an existing
685 req
= nfs_page_find_request(page
);
688 do_flush
= req
->wb_page
!= page
|| req
->wb_context
!= ctx
689 || !nfs_dirty_request(req
);
690 nfs_release_request(req
);
693 status
= nfs_wb_page(page
->mapping
->host
, page
);
694 } while (status
== 0);
699 * If the page cache is marked as unsafe or invalid, then we can't rely on
700 * the PageUptodate() flag. In this case, we will need to turn off
701 * write optimisations that depend on the page contents being correct.
703 static int nfs_write_pageuptodate(struct page
*page
, struct inode
*inode
)
705 return PageUptodate(page
) &&
706 !(NFS_I(inode
)->cache_validity
& (NFS_INO_REVAL_PAGECACHE
|NFS_INO_INVALID_DATA
));
710 * Update and possibly write a cached page of an NFS file.
712 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
713 * things with a page scheduled for an RPC call (e.g. invalidate it).
715 int nfs_updatepage(struct file
*file
, struct page
*page
,
716 unsigned int offset
, unsigned int count
)
718 struct nfs_open_context
*ctx
= nfs_file_open_context(file
);
719 struct inode
*inode
= page
->mapping
->host
;
722 nfs_inc_stats(inode
, NFSIOS_VFSUPDATEPAGE
);
724 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
725 file
->f_path
.dentry
->d_parent
->d_name
.name
,
726 file
->f_path
.dentry
->d_name
.name
, count
,
727 (long long)(page_offset(page
) +offset
));
729 /* If we're not using byte range locks, and we know the page
730 * is up to date, it may be more efficient to extend the write
731 * to cover the entire page in order to avoid fragmentation
734 if (nfs_write_pageuptodate(page
, inode
) &&
735 inode
->i_flock
== NULL
&&
736 !(file
->f_flags
& O_SYNC
)) {
737 count
= max(count
+ offset
, nfs_page_length(page
));
741 status
= nfs_writepage_setup(ctx
, page
, offset
, count
);
742 __set_page_dirty_nobuffers(page
);
744 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
745 status
, (long long)i_size_read(inode
));
747 nfs_set_pageerror(page
);
751 static void nfs_writepage_release(struct nfs_page
*req
)
754 if (PageError(req
->wb_page
)) {
755 nfs_end_page_writeback(req
->wb_page
);
756 nfs_inode_remove_request(req
);
757 } else if (!nfs_reschedule_unstable_write(req
)) {
758 /* Set the PG_uptodate flag */
759 nfs_mark_uptodate(req
->wb_page
, req
->wb_pgbase
, req
->wb_bytes
);
760 nfs_end_page_writeback(req
->wb_page
);
761 nfs_inode_remove_request(req
);
763 nfs_end_page_writeback(req
->wb_page
);
764 nfs_clear_page_tag_locked(req
);
767 static int flush_task_priority(int how
)
769 switch (how
& (FLUSH_HIGHPRI
|FLUSH_LOWPRI
)) {
771 return RPC_PRIORITY_HIGH
;
773 return RPC_PRIORITY_LOW
;
775 return RPC_PRIORITY_NORMAL
;
779 * Set up the argument/result storage required for the RPC call.
781 static void nfs_write_rpcsetup(struct nfs_page
*req
,
782 struct nfs_write_data
*data
,
783 const struct rpc_call_ops
*call_ops
,
784 unsigned int count
, unsigned int offset
,
787 struct inode
*inode
= req
->wb_context
->path
.dentry
->d_inode
;
788 int flags
= (how
& FLUSH_SYNC
) ? 0 : RPC_TASK_ASYNC
;
789 int priority
= flush_task_priority(how
);
790 struct rpc_task
*task
;
791 struct rpc_message msg
= {
792 .rpc_argp
= &data
->args
,
793 .rpc_resp
= &data
->res
,
794 .rpc_cred
= req
->wb_context
->cred
,
796 struct rpc_task_setup task_setup_data
= {
797 .rpc_client
= NFS_CLIENT(inode
),
800 .callback_ops
= call_ops
,
801 .callback_data
= data
,
802 .workqueue
= nfsiod_workqueue
,
804 .priority
= priority
,
807 /* Set up the RPC argument and reply structs
808 * NB: take care not to mess about with data->commit et al. */
811 data
->inode
= inode
= req
->wb_context
->path
.dentry
->d_inode
;
812 data
->cred
= msg
.rpc_cred
;
814 data
->args
.fh
= NFS_FH(inode
);
815 data
->args
.offset
= req_offset(req
) + offset
;
816 data
->args
.pgbase
= req
->wb_pgbase
+ offset
;
817 data
->args
.pages
= data
->pagevec
;
818 data
->args
.count
= count
;
819 data
->args
.context
= get_nfs_open_context(req
->wb_context
);
820 data
->args
.stable
= NFS_UNSTABLE
;
821 if (how
& FLUSH_STABLE
) {
822 data
->args
.stable
= NFS_DATA_SYNC
;
823 if (!NFS_I(inode
)->ncommit
)
824 data
->args
.stable
= NFS_FILE_SYNC
;
827 data
->res
.fattr
= &data
->fattr
;
828 data
->res
.count
= count
;
829 data
->res
.verf
= &data
->verf
;
830 nfs_fattr_init(&data
->fattr
);
832 /* Set up the initial task struct. */
833 NFS_PROTO(inode
)->write_setup(data
, &msg
);
835 dprintk("NFS: %5u initiated write call "
836 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
839 (long long)NFS_FILEID(inode
),
841 (unsigned long long)data
->args
.offset
);
843 task
= rpc_run_task(&task_setup_data
);
848 /* If a nfs_flush_* function fails, it should remove reqs from @head and
849 * call this on each, which will prepare them to be retried on next
850 * writeback using standard nfs.
852 static void nfs_redirty_request(struct nfs_page
*req
)
854 nfs_mark_request_dirty(req
);
855 nfs_end_page_writeback(req
->wb_page
);
856 nfs_clear_page_tag_locked(req
);
860 * Generate multiple small requests to write out a single
861 * contiguous dirty area on one page.
863 static int nfs_flush_multi(struct inode
*inode
, struct list_head
*head
, unsigned int npages
, size_t count
, int how
)
865 struct nfs_page
*req
= nfs_list_entry(head
->next
);
866 struct page
*page
= req
->wb_page
;
867 struct nfs_write_data
*data
;
868 size_t wsize
= NFS_SERVER(inode
)->wsize
, nbytes
;
873 nfs_list_remove_request(req
);
877 size_t len
= min(nbytes
, wsize
);
879 data
= nfs_writedata_alloc(1);
882 list_add(&data
->pages
, &list
);
885 } while (nbytes
!= 0);
886 atomic_set(&req
->wb_complete
, requests
);
888 ClearPageError(page
);
892 data
= list_entry(list
.next
, struct nfs_write_data
, pages
);
893 list_del_init(&data
->pages
);
895 data
->pagevec
[0] = page
;
899 nfs_write_rpcsetup(req
, data
, &nfs_write_partial_ops
,
903 } while (nbytes
!= 0);
908 while (!list_empty(&list
)) {
909 data
= list_entry(list
.next
, struct nfs_write_data
, pages
);
910 list_del(&data
->pages
);
911 nfs_writedata_release(data
);
913 nfs_redirty_request(req
);
918 * Create an RPC task for the given write request and kick it.
919 * The page must have been locked by the caller.
921 * It may happen that the page we're passed is not marked dirty.
922 * This is the case if nfs_updatepage detects a conflicting request
923 * that has been written but not committed.
925 static int nfs_flush_one(struct inode
*inode
, struct list_head
*head
, unsigned int npages
, size_t count
, int how
)
927 struct nfs_page
*req
;
929 struct nfs_write_data
*data
;
931 data
= nfs_writedata_alloc(npages
);
935 pages
= data
->pagevec
;
936 while (!list_empty(head
)) {
937 req
= nfs_list_entry(head
->next
);
938 nfs_list_remove_request(req
);
939 nfs_list_add_request(req
, &data
->pages
);
940 ClearPageError(req
->wb_page
);
941 *pages
++ = req
->wb_page
;
943 req
= nfs_list_entry(data
->pages
.next
);
945 /* Set up the argument struct */
946 nfs_write_rpcsetup(req
, data
, &nfs_write_full_ops
, count
, 0, how
);
950 while (!list_empty(head
)) {
951 req
= nfs_list_entry(head
->next
);
952 nfs_list_remove_request(req
);
953 nfs_redirty_request(req
);
958 static void nfs_pageio_init_write(struct nfs_pageio_descriptor
*pgio
,
959 struct inode
*inode
, int ioflags
)
961 size_t wsize
= NFS_SERVER(inode
)->wsize
;
963 if (wsize
< PAGE_CACHE_SIZE
)
964 nfs_pageio_init(pgio
, inode
, nfs_flush_multi
, wsize
, ioflags
);
966 nfs_pageio_init(pgio
, inode
, nfs_flush_one
, wsize
, ioflags
);
970 * Handle a write reply that flushed part of a page.
972 static void nfs_writeback_done_partial(struct rpc_task
*task
, void *calldata
)
974 struct nfs_write_data
*data
= calldata
;
975 struct nfs_page
*req
= data
->req
;
976 struct page
*page
= req
->wb_page
;
978 dprintk("NFS: write (%s/%Ld %d@%Ld)",
979 req
->wb_context
->path
.dentry
->d_inode
->i_sb
->s_id
,
980 (long long)NFS_FILEID(req
->wb_context
->path
.dentry
->d_inode
),
982 (long long)req_offset(req
));
984 if (nfs_writeback_done(task
, data
) != 0)
987 if (task
->tk_status
< 0) {
988 nfs_set_pageerror(page
);
989 nfs_context_set_write_error(req
->wb_context
, task
->tk_status
);
990 dprintk(", error = %d\n", task
->tk_status
);
994 if (nfs_write_need_commit(data
)) {
995 struct inode
*inode
= page
->mapping
->host
;
997 spin_lock(&inode
->i_lock
);
998 if (test_bit(PG_NEED_RESCHED
, &req
->wb_flags
)) {
999 /* Do nothing we need to resend the writes */
1000 } else if (!test_and_set_bit(PG_NEED_COMMIT
, &req
->wb_flags
)) {
1001 memcpy(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
));
1002 dprintk(" defer commit\n");
1003 } else if (memcmp(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
))) {
1004 set_bit(PG_NEED_RESCHED
, &req
->wb_flags
);
1005 clear_bit(PG_NEED_COMMIT
, &req
->wb_flags
);
1006 dprintk(" server reboot detected\n");
1008 spin_unlock(&inode
->i_lock
);
1013 if (atomic_dec_and_test(&req
->wb_complete
))
1014 nfs_writepage_release(req
);
1017 static const struct rpc_call_ops nfs_write_partial_ops
= {
1018 .rpc_call_done
= nfs_writeback_done_partial
,
1019 .rpc_release
= nfs_writedata_release
,
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
;
1032 struct nfs_page
*req
;
1035 if (nfs_writeback_done(task
, data
) != 0)
1038 /* Update attributes as result of writeback. */
1039 while (!list_empty(&data
->pages
)) {
1040 req
= nfs_list_entry(data
->pages
.next
);
1041 nfs_list_remove_request(req
);
1042 page
= req
->wb_page
;
1044 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1045 req
->wb_context
->path
.dentry
->d_inode
->i_sb
->s_id
,
1046 (long long)NFS_FILEID(req
->wb_context
->path
.dentry
->d_inode
),
1048 (long long)req_offset(req
));
1050 if (task
->tk_status
< 0) {
1051 nfs_set_pageerror(page
);
1052 nfs_context_set_write_error(req
->wb_context
, task
->tk_status
);
1053 dprintk(", error = %d\n", task
->tk_status
);
1054 goto remove_request
;
1057 if (nfs_write_need_commit(data
)) {
1058 memcpy(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
));
1059 nfs_mark_request_commit(req
);
1060 nfs_end_page_writeback(page
);
1061 dprintk(" marked for commit\n");
1064 /* Set the PG_uptodate flag? */
1065 nfs_mark_uptodate(page
, req
->wb_pgbase
, req
->wb_bytes
);
1068 nfs_end_page_writeback(page
);
1069 nfs_inode_remove_request(req
);
1071 nfs_clear_page_tag_locked(req
);
1075 static const struct rpc_call_ops nfs_write_full_ops
= {
1076 .rpc_call_done
= nfs_writeback_done_full
,
1077 .rpc_release
= nfs_writedata_release
,
1082 * This function is called when the WRITE call is complete.
1084 int nfs_writeback_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
1086 struct nfs_writeargs
*argp
= &data
->args
;
1087 struct nfs_writeres
*resp
= &data
->res
;
1090 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1091 task
->tk_pid
, task
->tk_status
);
1094 * ->write_done will attempt to use post-op attributes to detect
1095 * conflicting writes by other clients. A strict interpretation
1096 * of close-to-open would allow us to continue caching even if
1097 * another writer had changed the file, but some applications
1098 * depend on tighter cache coherency when writing.
1100 status
= NFS_PROTO(data
->inode
)->write_done(task
, data
);
1103 nfs_add_stats(data
->inode
, NFSIOS_SERVERWRITTENBYTES
, resp
->count
);
1105 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1106 if (resp
->verf
->committed
< argp
->stable
&& task
->tk_status
>= 0) {
1107 /* We tried a write call, but the server did not
1108 * commit data to stable storage even though we
1110 * Note: There is a known bug in Tru64 < 5.0 in which
1111 * the server reports NFS_DATA_SYNC, but performs
1112 * NFS_FILE_SYNC. We therefore implement this checking
1113 * as a dprintk() in order to avoid filling syslog.
1115 static unsigned long complain
;
1117 if (time_before(complain
, jiffies
)) {
1118 dprintk("NFS: faulty NFS server %s:"
1119 " (committed = %d) != (stable = %d)\n",
1120 NFS_SERVER(data
->inode
)->nfs_client
->cl_hostname
,
1121 resp
->verf
->committed
, argp
->stable
);
1122 complain
= jiffies
+ 300 * HZ
;
1126 /* Is this a short write? */
1127 if (task
->tk_status
>= 0 && resp
->count
< argp
->count
) {
1128 static unsigned long complain
;
1130 nfs_inc_stats(data
->inode
, NFSIOS_SHORTWRITE
);
1132 /* Has the server at least made some progress? */
1133 if (resp
->count
!= 0) {
1134 /* Was this an NFSv2 write or an NFSv3 stable write? */
1135 if (resp
->verf
->committed
!= NFS_UNSTABLE
) {
1136 /* Resend from where the server left off */
1137 argp
->offset
+= resp
->count
;
1138 argp
->pgbase
+= resp
->count
;
1139 argp
->count
-= resp
->count
;
1141 /* Resend as a stable write in order to avoid
1142 * headaches in the case of a server crash.
1144 argp
->stable
= NFS_FILE_SYNC
;
1146 rpc_restart_call(task
);
1149 if (time_before(complain
, jiffies
)) {
1151 "NFS: Server wrote zero bytes, expected %u.\n",
1153 complain
= jiffies
+ 300 * HZ
;
1155 /* Can't do anything about it except throw an error. */
1156 task
->tk_status
= -EIO
;
1162 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1163 void nfs_commit_release(void *data
)
1165 struct nfs_write_data
*wdata
= data
;
1167 put_nfs_open_context(wdata
->args
.context
);
1168 nfs_commit_free(wdata
);
1172 * Set up the argument/result storage required for the RPC call.
1174 static void nfs_commit_rpcsetup(struct list_head
*head
,
1175 struct nfs_write_data
*data
,
1178 struct nfs_page
*first
= nfs_list_entry(head
->next
);
1179 struct inode
*inode
= first
->wb_context
->path
.dentry
->d_inode
;
1180 int flags
= (how
& FLUSH_SYNC
) ? 0 : RPC_TASK_ASYNC
;
1181 int priority
= flush_task_priority(how
);
1182 struct rpc_task
*task
;
1183 struct rpc_message msg
= {
1184 .rpc_argp
= &data
->args
,
1185 .rpc_resp
= &data
->res
,
1186 .rpc_cred
= first
->wb_context
->cred
,
1188 struct rpc_task_setup task_setup_data
= {
1189 .task
= &data
->task
,
1190 .rpc_client
= NFS_CLIENT(inode
),
1191 .rpc_message
= &msg
,
1192 .callback_ops
= &nfs_commit_ops
,
1193 .callback_data
= data
,
1194 .workqueue
= nfsiod_workqueue
,
1196 .priority
= priority
,
1199 /* Set up the RPC argument and reply structs
1200 * NB: take care not to mess about with data->commit et al. */
1202 list_splice_init(head
, &data
->pages
);
1204 data
->inode
= inode
;
1205 data
->cred
= msg
.rpc_cred
;
1207 data
->args
.fh
= NFS_FH(data
->inode
);
1208 /* Note: we always request a commit of the entire inode */
1209 data
->args
.offset
= 0;
1210 data
->args
.count
= 0;
1211 data
->args
.context
= get_nfs_open_context(first
->wb_context
);
1212 data
->res
.count
= 0;
1213 data
->res
.fattr
= &data
->fattr
;
1214 data
->res
.verf
= &data
->verf
;
1215 nfs_fattr_init(&data
->fattr
);
1217 /* Set up the initial task struct. */
1218 NFS_PROTO(inode
)->commit_setup(data
, &msg
);
1220 dprintk("NFS: %5u initiated commit call\n", data
->task
.tk_pid
);
1222 task
= rpc_run_task(&task_setup_data
);
1228 * Commit dirty pages
1231 nfs_commit_list(struct inode
*inode
, struct list_head
*head
, int how
)
1233 struct nfs_write_data
*data
;
1234 struct nfs_page
*req
;
1236 data
= nfs_commit_alloc();
1241 /* Set up the argument struct */
1242 nfs_commit_rpcsetup(head
, data
, how
);
1246 while (!list_empty(head
)) {
1247 req
= nfs_list_entry(head
->next
);
1248 nfs_list_remove_request(req
);
1249 nfs_mark_request_commit(req
);
1250 dec_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
1251 dec_bdi_stat(req
->wb_page
->mapping
->backing_dev_info
,
1253 nfs_clear_page_tag_locked(req
);
1259 * COMMIT call returned
1261 static void nfs_commit_done(struct rpc_task
*task
, void *calldata
)
1263 struct nfs_write_data
*data
= calldata
;
1264 struct nfs_page
*req
;
1266 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1267 task
->tk_pid
, task
->tk_status
);
1269 /* Call the NFS version-specific code */
1270 if (NFS_PROTO(data
->inode
)->commit_done(task
, data
) != 0)
1273 while (!list_empty(&data
->pages
)) {
1274 req
= nfs_list_entry(data
->pages
.next
);
1275 nfs_list_remove_request(req
);
1276 clear_bit(PG_NEED_COMMIT
, &(req
)->wb_flags
);
1277 dec_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
1278 dec_bdi_stat(req
->wb_page
->mapping
->backing_dev_info
,
1281 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1282 req
->wb_context
->path
.dentry
->d_inode
->i_sb
->s_id
,
1283 (long long)NFS_FILEID(req
->wb_context
->path
.dentry
->d_inode
),
1285 (long long)req_offset(req
));
1286 if (task
->tk_status
< 0) {
1287 nfs_context_set_write_error(req
->wb_context
, task
->tk_status
);
1288 nfs_inode_remove_request(req
);
1289 dprintk(", error = %d\n", task
->tk_status
);
1293 /* Okay, COMMIT succeeded, apparently. Check the verifier
1294 * returned by the server against all stored verfs. */
1295 if (!memcmp(req
->wb_verf
.verifier
, data
->verf
.verifier
, sizeof(data
->verf
.verifier
))) {
1296 /* We have a match */
1297 /* Set the PG_uptodate flag */
1298 nfs_mark_uptodate(req
->wb_page
, req
->wb_pgbase
,
1300 nfs_inode_remove_request(req
);
1304 /* We have a mismatch. Write the page again */
1305 dprintk(" mismatch\n");
1306 nfs_mark_request_dirty(req
);
1308 nfs_clear_page_tag_locked(req
);
1312 static const struct rpc_call_ops nfs_commit_ops
= {
1313 .rpc_call_done
= nfs_commit_done
,
1314 .rpc_release
= nfs_commit_release
,
1317 int nfs_commit_inode(struct inode
*inode
, int how
)
1322 spin_lock(&inode
->i_lock
);
1323 res
= nfs_scan_commit(inode
, &head
, 0, 0);
1324 spin_unlock(&inode
->i_lock
);
1326 int error
= nfs_commit_list(inode
, &head
, how
);
1333 static inline int nfs_commit_list(struct inode
*inode
, struct list_head
*head
, int how
)
1339 long nfs_sync_mapping_wait(struct address_space
*mapping
, struct writeback_control
*wbc
, int how
)
1341 struct inode
*inode
= mapping
->host
;
1342 pgoff_t idx_start
, idx_end
;
1343 unsigned int npages
= 0;
1345 int nocommit
= how
& FLUSH_NOCOMMIT
;
1349 if (wbc
->range_cyclic
)
1352 idx_start
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
1353 idx_end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
1354 if (idx_end
> idx_start
) {
1355 pgoff_t l_npages
= 1 + idx_end
- idx_start
;
1357 if (sizeof(npages
) != sizeof(l_npages
) &&
1358 (pgoff_t
)npages
!= l_npages
)
1362 how
&= ~FLUSH_NOCOMMIT
;
1363 spin_lock(&inode
->i_lock
);
1365 ret
= nfs_wait_on_requests_locked(inode
, idx_start
, npages
);
1370 pages
= nfs_scan_commit(inode
, &head
, idx_start
, npages
);
1373 if (how
& FLUSH_INVALIDATE
) {
1374 spin_unlock(&inode
->i_lock
);
1375 nfs_cancel_commit_list(&head
);
1377 spin_lock(&inode
->i_lock
);
1380 pages
+= nfs_scan_commit(inode
, &head
, 0, 0);
1381 spin_unlock(&inode
->i_lock
);
1382 ret
= nfs_commit_list(inode
, &head
, how
);
1383 spin_lock(&inode
->i_lock
);
1386 spin_unlock(&inode
->i_lock
);
1390 static int __nfs_write_mapping(struct address_space
*mapping
, struct writeback_control
*wbc
, int how
)
1394 ret
= nfs_writepages(mapping
, wbc
);
1397 ret
= nfs_sync_mapping_wait(mapping
, wbc
, how
);
1402 __mark_inode_dirty(mapping
->host
, I_DIRTY_PAGES
);
1406 /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
1407 static int nfs_write_mapping(struct address_space
*mapping
, int how
)
1409 struct writeback_control wbc
= {
1410 .bdi
= mapping
->backing_dev_info
,
1411 .sync_mode
= WB_SYNC_NONE
,
1412 .nr_to_write
= LONG_MAX
,
1413 .for_writepages
= 1,
1418 ret
= __nfs_write_mapping(mapping
, &wbc
, how
);
1421 wbc
.sync_mode
= WB_SYNC_ALL
;
1422 return __nfs_write_mapping(mapping
, &wbc
, how
);
1426 * flush the inode to disk.
1428 int nfs_wb_all(struct inode
*inode
)
1430 return nfs_write_mapping(inode
->i_mapping
, 0);
1433 int nfs_wb_nocommit(struct inode
*inode
)
1435 return nfs_write_mapping(inode
->i_mapping
, FLUSH_NOCOMMIT
);
1438 int nfs_wb_page_cancel(struct inode
*inode
, struct page
*page
)
1440 struct nfs_page
*req
;
1441 loff_t range_start
= page_offset(page
);
1442 loff_t range_end
= range_start
+ (loff_t
)(PAGE_CACHE_SIZE
- 1);
1443 struct writeback_control wbc
= {
1444 .bdi
= page
->mapping
->backing_dev_info
,
1445 .sync_mode
= WB_SYNC_ALL
,
1446 .nr_to_write
= LONG_MAX
,
1447 .range_start
= range_start
,
1448 .range_end
= range_end
,
1452 BUG_ON(!PageLocked(page
));
1454 req
= nfs_page_find_request(page
);
1457 if (test_bit(PG_NEED_COMMIT
, &req
->wb_flags
)) {
1458 nfs_release_request(req
);
1461 if (nfs_lock_request_dontget(req
)) {
1462 nfs_inode_remove_request(req
);
1464 * In case nfs_inode_remove_request has marked the
1465 * page as being dirty
1467 cancel_dirty_page(page
, PAGE_CACHE_SIZE
);
1468 nfs_unlock_request(req
);
1471 ret
= nfs_wait_on_request(req
);
1475 if (!PagePrivate(page
))
1477 ret
= nfs_sync_mapping_wait(page
->mapping
, &wbc
, FLUSH_INVALIDATE
);
1482 static int nfs_wb_page_priority(struct inode
*inode
, struct page
*page
,
1485 loff_t range_start
= page_offset(page
);
1486 loff_t range_end
= range_start
+ (loff_t
)(PAGE_CACHE_SIZE
- 1);
1487 struct writeback_control wbc
= {
1488 .bdi
= page
->mapping
->backing_dev_info
,
1489 .sync_mode
= WB_SYNC_ALL
,
1490 .nr_to_write
= LONG_MAX
,
1491 .range_start
= range_start
,
1492 .range_end
= range_end
,
1496 BUG_ON(!PageLocked(page
));
1497 if (clear_page_dirty_for_io(page
)) {
1498 ret
= nfs_writepage_locked(page
, &wbc
);
1502 if (!PagePrivate(page
))
1504 ret
= nfs_sync_mapping_wait(page
->mapping
, &wbc
, how
);
1508 __mark_inode_dirty(inode
, I_DIRTY_PAGES
);
1513 * Write back all requests on one page - we do this before reading it.
1515 int nfs_wb_page(struct inode
*inode
, struct page
* page
)
1517 return nfs_wb_page_priority(inode
, page
, FLUSH_STABLE
);
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
);