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 const struct rpc_call_ops nfs_write_partial_ops
;
43 static const struct rpc_call_ops nfs_write_full_ops
;
44 static const struct rpc_call_ops nfs_commit_ops
;
46 static struct kmem_cache
*nfs_wdata_cachep
;
47 static mempool_t
*nfs_wdata_mempool
;
48 static mempool_t
*nfs_commit_mempool
;
50 struct nfs_write_data
*nfs_commit_alloc(void)
52 struct nfs_write_data
*p
= mempool_alloc(nfs_commit_mempool
, GFP_NOFS
);
55 memset(p
, 0, sizeof(*p
));
56 INIT_LIST_HEAD(&p
->pages
);
61 static void nfs_commit_rcu_free(struct rcu_head
*head
)
63 struct nfs_write_data
*p
= container_of(head
, struct nfs_write_data
, task
.u
.tk_rcu
);
64 if (p
&& (p
->pagevec
!= &p
->page_array
[0]))
66 mempool_free(p
, nfs_commit_mempool
);
69 void nfs_commit_free(struct nfs_write_data
*wdata
)
71 call_rcu_bh(&wdata
->task
.u
.tk_rcu
, nfs_commit_rcu_free
);
74 struct nfs_write_data
*nfs_writedata_alloc(unsigned int pagecount
)
76 struct nfs_write_data
*p
= mempool_alloc(nfs_wdata_mempool
, GFP_NOFS
);
79 memset(p
, 0, sizeof(*p
));
80 INIT_LIST_HEAD(&p
->pages
);
81 p
->npages
= pagecount
;
82 if (pagecount
<= ARRAY_SIZE(p
->page_array
))
83 p
->pagevec
= p
->page_array
;
85 p
->pagevec
= kcalloc(pagecount
, sizeof(struct page
*), GFP_NOFS
);
87 mempool_free(p
, nfs_wdata_mempool
);
95 static void nfs_writedata_rcu_free(struct rcu_head
*head
)
97 struct nfs_write_data
*p
= container_of(head
, struct nfs_write_data
, task
.u
.tk_rcu
);
98 if (p
&& (p
->pagevec
!= &p
->page_array
[0]))
100 mempool_free(p
, nfs_wdata_mempool
);
103 static void nfs_writedata_free(struct nfs_write_data
*wdata
)
105 call_rcu_bh(&wdata
->task
.u
.tk_rcu
, nfs_writedata_rcu_free
);
108 void nfs_writedata_release(void *wdata
)
110 nfs_writedata_free(wdata
);
113 static void nfs_context_set_write_error(struct nfs_open_context
*ctx
, int error
)
117 set_bit(NFS_CONTEXT_ERROR_WRITE
, &ctx
->flags
);
120 static struct nfs_page
*nfs_page_find_request_locked(struct page
*page
)
122 struct nfs_page
*req
= NULL
;
124 if (PagePrivate(page
)) {
125 req
= (struct nfs_page
*)page_private(page
);
127 kref_get(&req
->wb_kref
);
132 static struct nfs_page
*nfs_page_find_request(struct page
*page
)
134 struct inode
*inode
= page
->mapping
->host
;
135 struct nfs_page
*req
= NULL
;
137 spin_lock(&inode
->i_lock
);
138 req
= nfs_page_find_request_locked(page
);
139 spin_unlock(&inode
->i_lock
);
143 /* Adjust the file length if we're writing beyond the end */
144 static void nfs_grow_file(struct page
*page
, unsigned int offset
, unsigned int count
)
146 struct inode
*inode
= page
->mapping
->host
;
147 loff_t end
, i_size
= i_size_read(inode
);
148 pgoff_t end_index
= (i_size
- 1) >> PAGE_CACHE_SHIFT
;
150 if (i_size
> 0 && page
->index
< end_index
)
152 end
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + ((loff_t
)offset
+count
);
155 nfs_inc_stats(inode
, NFSIOS_EXTENDWRITE
);
156 i_size_write(inode
, end
);
159 /* A writeback failed: mark the page as bad, and invalidate the page cache */
160 static void nfs_set_pageerror(struct page
*page
)
163 nfs_zap_mapping(page
->mapping
->host
, page
->mapping
);
166 /* We can set the PG_uptodate flag if we see that a write request
167 * covers the full page.
169 static void nfs_mark_uptodate(struct page
*page
, unsigned int base
, unsigned int count
)
171 if (PageUptodate(page
))
175 if (count
!= nfs_page_length(page
))
177 SetPageUptodate(page
);
180 static int nfs_writepage_setup(struct nfs_open_context
*ctx
, struct page
*page
,
181 unsigned int offset
, unsigned int count
)
183 struct nfs_page
*req
;
187 req
= nfs_update_request(ctx
, page
, offset
, count
);
193 ret
= nfs_wb_page(page
->mapping
->host
, page
);
197 /* Update file length */
198 nfs_grow_file(page
, offset
, count
);
199 nfs_unlock_request(req
);
203 static int wb_priority(struct writeback_control
*wbc
)
205 if (wbc
->for_reclaim
)
206 return FLUSH_HIGHPRI
| FLUSH_STABLE
;
207 if (wbc
->for_kupdate
)
213 * NFS congestion control
216 int nfs_congestion_kb
;
218 #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
219 #define NFS_CONGESTION_OFF_THRESH \
220 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
222 static int nfs_set_page_writeback(struct page
*page
)
224 int ret
= test_set_page_writeback(page
);
227 struct inode
*inode
= page
->mapping
->host
;
228 struct nfs_server
*nfss
= NFS_SERVER(inode
);
230 if (atomic_long_inc_return(&nfss
->writeback
) >
231 NFS_CONGESTION_ON_THRESH
)
232 set_bdi_congested(&nfss
->backing_dev_info
, WRITE
);
237 static void nfs_end_page_writeback(struct page
*page
)
239 struct inode
*inode
= page
->mapping
->host
;
240 struct nfs_server
*nfss
= NFS_SERVER(inode
);
242 end_page_writeback(page
);
243 if (atomic_long_dec_return(&nfss
->writeback
) < NFS_CONGESTION_OFF_THRESH
)
244 clear_bdi_congested(&nfss
->backing_dev_info
, WRITE
);
248 * Find an associated nfs write request, and prepare to flush it out
249 * May return an error if the user signalled nfs_wait_on_request().
251 static int nfs_page_async_flush(struct nfs_pageio_descriptor
*pgio
,
254 struct inode
*inode
= page
->mapping
->host
;
255 struct nfs_inode
*nfsi
= NFS_I(inode
);
256 struct nfs_page
*req
;
259 spin_lock(&inode
->i_lock
);
261 req
= nfs_page_find_request_locked(page
);
263 spin_unlock(&inode
->i_lock
);
266 if (nfs_lock_request_dontget(req
))
268 /* Note: If we hold the page lock, as is the case in nfs_writepage,
269 * then the call to nfs_lock_request_dontget() will always
270 * succeed provided that someone hasn't already marked the
271 * request as dirty (in which case we don't care).
273 spin_unlock(&inode
->i_lock
);
274 ret
= nfs_wait_on_request(req
);
275 nfs_release_request(req
);
278 spin_lock(&inode
->i_lock
);
280 if (test_bit(PG_NEED_COMMIT
, &req
->wb_flags
)) {
281 /* This request is marked for commit */
282 spin_unlock(&inode
->i_lock
);
283 nfs_unlock_request(req
);
284 nfs_pageio_complete(pgio
);
287 if (nfs_set_page_writeback(page
) != 0) {
288 spin_unlock(&inode
->i_lock
);
291 radix_tree_tag_set(&nfsi
->nfs_page_tree
, req
->wb_index
,
292 NFS_PAGE_TAG_LOCKED
);
293 spin_unlock(&inode
->i_lock
);
294 nfs_pageio_add_request(pgio
, req
);
298 static int nfs_do_writepage(struct page
*page
, struct writeback_control
*wbc
, struct nfs_pageio_descriptor
*pgio
)
300 struct inode
*inode
= page
->mapping
->host
;
302 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGE
);
303 nfs_add_stats(inode
, NFSIOS_WRITEPAGES
, 1);
305 nfs_pageio_cond_complete(pgio
, page
->index
);
306 return nfs_page_async_flush(pgio
, page
);
310 * Write an mmapped page to the server.
312 static int nfs_writepage_locked(struct page
*page
, struct writeback_control
*wbc
)
314 struct nfs_pageio_descriptor pgio
;
317 nfs_pageio_init_write(&pgio
, page
->mapping
->host
, wb_priority(wbc
));
318 err
= nfs_do_writepage(page
, wbc
, &pgio
);
319 nfs_pageio_complete(&pgio
);
322 if (pgio
.pg_error
< 0)
323 return pgio
.pg_error
;
327 int nfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
331 ret
= nfs_writepage_locked(page
, wbc
);
336 static int nfs_writepages_callback(struct page
*page
, struct writeback_control
*wbc
, void *data
)
340 ret
= nfs_do_writepage(page
, wbc
, data
);
345 int nfs_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
347 struct inode
*inode
= mapping
->host
;
348 struct nfs_pageio_descriptor pgio
;
351 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGES
);
353 nfs_pageio_init_write(&pgio
, inode
, wb_priority(wbc
));
354 err
= write_cache_pages(mapping
, wbc
, nfs_writepages_callback
, &pgio
);
355 nfs_pageio_complete(&pgio
);
358 if (pgio
.pg_error
< 0)
359 return pgio
.pg_error
;
364 * Insert a write request into an inode
366 static void nfs_inode_add_request(struct inode
*inode
, struct nfs_page
*req
)
368 struct nfs_inode
*nfsi
= NFS_I(inode
);
371 error
= radix_tree_insert(&nfsi
->nfs_page_tree
, req
->wb_index
, req
);
375 if (nfs_have_delegation(inode
, FMODE_WRITE
))
378 SetPagePrivate(req
->wb_page
);
379 set_page_private(req
->wb_page
, (unsigned long)req
);
381 kref_get(&req
->wb_kref
);
385 * Remove a write request from an inode
387 static void nfs_inode_remove_request(struct nfs_page
*req
)
389 struct inode
*inode
= req
->wb_context
->path
.dentry
->d_inode
;
390 struct nfs_inode
*nfsi
= NFS_I(inode
);
392 BUG_ON (!NFS_WBACK_BUSY(req
));
394 spin_lock(&inode
->i_lock
);
395 set_page_private(req
->wb_page
, 0);
396 ClearPagePrivate(req
->wb_page
);
397 radix_tree_delete(&nfsi
->nfs_page_tree
, req
->wb_index
);
400 spin_unlock(&inode
->i_lock
);
403 spin_unlock(&inode
->i_lock
);
404 nfs_clear_request(req
);
405 nfs_release_request(req
);
409 nfs_redirty_request(struct nfs_page
*req
)
411 __set_page_dirty_nobuffers(req
->wb_page
);
415 * Check if a request is dirty
418 nfs_dirty_request(struct nfs_page
*req
)
420 struct page
*page
= req
->wb_page
;
422 if (page
== NULL
|| test_bit(PG_NEED_COMMIT
, &req
->wb_flags
))
424 return !PageWriteback(req
->wb_page
);
427 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
429 * Add a request to the inode's commit list.
432 nfs_mark_request_commit(struct nfs_page
*req
)
434 struct inode
*inode
= req
->wb_context
->path
.dentry
->d_inode
;
435 struct nfs_inode
*nfsi
= NFS_I(inode
);
437 spin_lock(&inode
->i_lock
);
439 set_bit(PG_NEED_COMMIT
, &(req
)->wb_flags
);
440 radix_tree_tag_set(&nfsi
->nfs_page_tree
,
442 NFS_PAGE_TAG_COMMIT
);
443 spin_unlock(&inode
->i_lock
);
444 inc_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
445 inc_bdi_stat(req
->wb_page
->mapping
->backing_dev_info
, BDI_RECLAIMABLE
);
446 __mark_inode_dirty(inode
, I_DIRTY_DATASYNC
);
450 int nfs_write_need_commit(struct nfs_write_data
*data
)
452 return data
->verf
.committed
!= NFS_FILE_SYNC
;
456 int nfs_reschedule_unstable_write(struct nfs_page
*req
)
458 if (test_bit(PG_NEED_COMMIT
, &req
->wb_flags
)) {
459 nfs_mark_request_commit(req
);
462 if (test_and_clear_bit(PG_NEED_RESCHED
, &req
->wb_flags
)) {
463 nfs_redirty_request(req
);
470 nfs_mark_request_commit(struct nfs_page
*req
)
475 int nfs_write_need_commit(struct nfs_write_data
*data
)
481 int nfs_reschedule_unstable_write(struct nfs_page
*req
)
488 * Wait for a request to complete.
490 * Interruptible by signals only if mounted with intr flag.
492 static int nfs_wait_on_requests_locked(struct inode
*inode
, pgoff_t idx_start
, unsigned int npages
)
494 struct nfs_inode
*nfsi
= NFS_I(inode
);
495 struct nfs_page
*req
;
496 pgoff_t idx_end
, next
;
497 unsigned int res
= 0;
503 idx_end
= idx_start
+ npages
- 1;
506 while (radix_tree_gang_lookup_tag(&nfsi
->nfs_page_tree
, (void **)&req
, next
, 1, NFS_PAGE_TAG_LOCKED
)) {
507 if (req
->wb_index
> idx_end
)
510 next
= req
->wb_index
+ 1;
511 BUG_ON(!NFS_WBACK_BUSY(req
));
513 kref_get(&req
->wb_kref
);
514 spin_unlock(&inode
->i_lock
);
515 error
= nfs_wait_on_request(req
);
516 nfs_release_request(req
);
517 spin_lock(&inode
->i_lock
);
525 static void nfs_cancel_commit_list(struct list_head
*head
)
527 struct nfs_page
*req
;
529 while(!list_empty(head
)) {
530 req
= nfs_list_entry(head
->next
);
531 dec_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
532 dec_bdi_stat(req
->wb_page
->mapping
->backing_dev_info
,
534 nfs_list_remove_request(req
);
535 clear_bit(PG_NEED_COMMIT
, &(req
)->wb_flags
);
536 nfs_inode_remove_request(req
);
537 nfs_unlock_request(req
);
541 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
543 * nfs_scan_commit - Scan an inode for commit requests
544 * @inode: NFS inode to scan
545 * @dst: destination list
546 * @idx_start: lower bound of page->index to scan.
547 * @npages: idx_start + npages sets the upper bound to scan.
549 * Moves requests from the inode's 'commit' request list.
550 * The requests are *not* checked to ensure that they form a contiguous set.
553 nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
, pgoff_t idx_start
, unsigned int npages
)
555 struct nfs_inode
*nfsi
= NFS_I(inode
);
558 if (nfsi
->ncommit
!= 0) {
559 res
= nfs_scan_list(nfsi
, dst
, idx_start
, npages
,
560 NFS_PAGE_TAG_COMMIT
);
561 nfsi
->ncommit
-= res
;
566 static inline int nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
, pgoff_t idx_start
, unsigned int npages
)
573 * Try to update any existing write request, or create one if there is none.
574 * In order to match, the request's credentials must match those of
575 * the calling process.
577 * Note: Should always be called with the Page Lock held!
579 static struct nfs_page
* nfs_update_request(struct nfs_open_context
* ctx
,
580 struct page
*page
, unsigned int offset
, unsigned int bytes
)
582 struct address_space
*mapping
= page
->mapping
;
583 struct inode
*inode
= mapping
->host
;
584 struct nfs_page
*req
, *new = NULL
;
587 end
= offset
+ bytes
;
590 /* Loop over all inode entries and see if we find
591 * A request for the page we wish to update
594 if (radix_tree_preload(GFP_NOFS
)) {
595 nfs_release_request(new);
596 return ERR_PTR(-ENOMEM
);
600 spin_lock(&inode
->i_lock
);
601 req
= nfs_page_find_request_locked(page
);
603 if (!nfs_lock_request_dontget(req
)) {
606 spin_unlock(&inode
->i_lock
);
607 error
= nfs_wait_on_request(req
);
608 nfs_release_request(req
);
611 radix_tree_preload_end();
612 nfs_release_request(new);
614 return ERR_PTR(error
);
618 spin_unlock(&inode
->i_lock
);
620 radix_tree_preload_end();
621 nfs_release_request(new);
627 nfs_lock_request_dontget(new);
628 nfs_inode_add_request(inode
, new);
629 spin_unlock(&inode
->i_lock
);
630 radix_tree_preload_end();
634 spin_unlock(&inode
->i_lock
);
636 new = nfs_create_request(ctx
, inode
, page
, offset
, bytes
);
641 /* We have a request for our page.
642 * If the creds don't match, or the
643 * page addresses don't match,
644 * tell the caller to wait on the conflicting
647 rqend
= req
->wb_offset
+ req
->wb_bytes
;
648 if (req
->wb_context
!= ctx
649 || req
->wb_page
!= page
650 || !nfs_dirty_request(req
)
651 || offset
> rqend
|| end
< req
->wb_offset
) {
652 nfs_unlock_request(req
);
653 return ERR_PTR(-EBUSY
);
656 /* Okay, the request matches. Update the region */
657 if (offset
< req
->wb_offset
) {
658 req
->wb_offset
= offset
;
659 req
->wb_pgbase
= offset
;
660 req
->wb_bytes
= max(end
, rqend
) - req
->wb_offset
;
665 req
->wb_bytes
= end
- req
->wb_offset
;
669 /* If this page might potentially be marked as up to date,
670 * then we need to zero any uninitalised data. */
671 if (req
->wb_pgbase
== 0 && req
->wb_bytes
!= PAGE_CACHE_SIZE
672 && !PageUptodate(req
->wb_page
))
673 zero_user_segment(req
->wb_page
, req
->wb_bytes
, PAGE_CACHE_SIZE
);
677 int nfs_flush_incompatible(struct file
*file
, struct page
*page
)
679 struct nfs_open_context
*ctx
= nfs_file_open_context(file
);
680 struct nfs_page
*req
;
681 int do_flush
, status
;
683 * Look for a request corresponding to this page. If there
684 * is one, and it belongs to another file, we flush it out
685 * before we try to copy anything into the page. Do this
686 * due to the lack of an ACCESS-type call in NFSv2.
687 * Also do the same if we find a request from an existing
691 req
= nfs_page_find_request(page
);
694 do_flush
= req
->wb_page
!= page
|| req
->wb_context
!= ctx
695 || !nfs_dirty_request(req
);
696 nfs_release_request(req
);
699 status
= nfs_wb_page(page
->mapping
->host
, page
);
700 } while (status
== 0);
705 * Update and possibly write a cached page of an NFS file.
707 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
708 * things with a page scheduled for an RPC call (e.g. invalidate it).
710 int nfs_updatepage(struct file
*file
, struct page
*page
,
711 unsigned int offset
, unsigned int count
)
713 struct nfs_open_context
*ctx
= nfs_file_open_context(file
);
714 struct inode
*inode
= page
->mapping
->host
;
717 nfs_inc_stats(inode
, NFSIOS_VFSUPDATEPAGE
);
719 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
720 file
->f_path
.dentry
->d_parent
->d_name
.name
,
721 file
->f_path
.dentry
->d_name
.name
, count
,
722 (long long)(page_offset(page
) +offset
));
724 /* If we're not using byte range locks, and we know the page
725 * is entirely in cache, it may be more efficient to avoid
726 * fragmenting write requests.
728 if (PageUptodate(page
) && inode
->i_flock
== NULL
&& !(file
->f_mode
& O_SYNC
)) {
729 count
= max(count
+ offset
, nfs_page_length(page
));
733 status
= nfs_writepage_setup(ctx
, page
, offset
, count
);
734 __set_page_dirty_nobuffers(page
);
736 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
737 status
, (long long)i_size_read(inode
));
739 nfs_set_pageerror(page
);
743 static void nfs_writepage_release(struct nfs_page
*req
)
746 if (PageError(req
->wb_page
)) {
747 nfs_end_page_writeback(req
->wb_page
);
748 nfs_inode_remove_request(req
);
749 } else if (!nfs_reschedule_unstable_write(req
)) {
750 /* Set the PG_uptodate flag */
751 nfs_mark_uptodate(req
->wb_page
, req
->wb_pgbase
, req
->wb_bytes
);
752 nfs_end_page_writeback(req
->wb_page
);
753 nfs_inode_remove_request(req
);
755 nfs_end_page_writeback(req
->wb_page
);
756 nfs_clear_page_tag_locked(req
);
759 static inline int flush_task_priority(int how
)
761 switch (how
& (FLUSH_HIGHPRI
|FLUSH_LOWPRI
)) {
763 return RPC_PRIORITY_HIGH
;
765 return RPC_PRIORITY_LOW
;
767 return RPC_PRIORITY_NORMAL
;
771 * Set up the argument/result storage required for the RPC call.
773 static void nfs_write_rpcsetup(struct nfs_page
*req
,
774 struct nfs_write_data
*data
,
775 const struct rpc_call_ops
*call_ops
,
776 unsigned int count
, unsigned int offset
,
782 /* Set up the RPC argument and reply structs
783 * NB: take care not to mess about with data->commit et al. */
786 data
->inode
= inode
= req
->wb_context
->path
.dentry
->d_inode
;
787 data
->cred
= req
->wb_context
->cred
;
789 data
->args
.fh
= NFS_FH(inode
);
790 data
->args
.offset
= req_offset(req
) + offset
;
791 data
->args
.pgbase
= req
->wb_pgbase
+ offset
;
792 data
->args
.pages
= data
->pagevec
;
793 data
->args
.count
= count
;
794 data
->args
.context
= req
->wb_context
;
796 data
->res
.fattr
= &data
->fattr
;
797 data
->res
.count
= count
;
798 data
->res
.verf
= &data
->verf
;
799 nfs_fattr_init(&data
->fattr
);
801 /* Set up the initial task struct. */
802 flags
= (how
& FLUSH_SYNC
) ? 0 : RPC_TASK_ASYNC
;
803 rpc_init_task(&data
->task
, NFS_CLIENT(inode
), flags
, call_ops
, data
);
804 NFS_PROTO(inode
)->write_setup(data
, how
);
806 data
->task
.tk_priority
= flush_task_priority(how
);
807 data
->task
.tk_cookie
= (unsigned long)inode
;
809 dprintk("NFS: %5u initiated write call "
810 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
813 (long long)NFS_FILEID(inode
),
815 (unsigned long long)data
->args
.offset
);
818 static void nfs_execute_write(struct nfs_write_data
*data
)
820 struct rpc_clnt
*clnt
= NFS_CLIENT(data
->inode
);
823 rpc_clnt_sigmask(clnt
, &oldset
);
824 rpc_execute(&data
->task
);
825 rpc_clnt_sigunmask(clnt
, &oldset
);
829 * Generate multiple small requests to write out a single
830 * contiguous dirty area on one page.
832 static int nfs_flush_multi(struct inode
*inode
, struct list_head
*head
, unsigned int npages
, size_t count
, int how
)
834 struct nfs_page
*req
= nfs_list_entry(head
->next
);
835 struct page
*page
= req
->wb_page
;
836 struct nfs_write_data
*data
;
837 size_t wsize
= NFS_SERVER(inode
)->wsize
, nbytes
;
842 nfs_list_remove_request(req
);
846 size_t len
= min(nbytes
, wsize
);
848 data
= nfs_writedata_alloc(1);
851 list_add(&data
->pages
, &list
);
854 } while (nbytes
!= 0);
855 atomic_set(&req
->wb_complete
, requests
);
857 ClearPageError(page
);
861 data
= list_entry(list
.next
, struct nfs_write_data
, pages
);
862 list_del_init(&data
->pages
);
864 data
->pagevec
[0] = page
;
868 nfs_write_rpcsetup(req
, data
, &nfs_write_partial_ops
,
872 nfs_execute_write(data
);
873 } while (nbytes
!= 0);
878 while (!list_empty(&list
)) {
879 data
= list_entry(list
.next
, struct nfs_write_data
, pages
);
880 list_del(&data
->pages
);
881 nfs_writedata_release(data
);
883 nfs_redirty_request(req
);
884 nfs_end_page_writeback(req
->wb_page
);
885 nfs_clear_page_tag_locked(req
);
890 * Create an RPC task for the given write request and kick it.
891 * The page must have been locked by the caller.
893 * It may happen that the page we're passed is not marked dirty.
894 * This is the case if nfs_updatepage detects a conflicting request
895 * that has been written but not committed.
897 static int nfs_flush_one(struct inode
*inode
, struct list_head
*head
, unsigned int npages
, size_t count
, int how
)
899 struct nfs_page
*req
;
901 struct nfs_write_data
*data
;
903 data
= nfs_writedata_alloc(npages
);
907 pages
= data
->pagevec
;
908 while (!list_empty(head
)) {
909 req
= nfs_list_entry(head
->next
);
910 nfs_list_remove_request(req
);
911 nfs_list_add_request(req
, &data
->pages
);
912 ClearPageError(req
->wb_page
);
913 *pages
++ = req
->wb_page
;
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 req
= nfs_list_entry(head
->next
);
925 nfs_list_remove_request(req
);
926 nfs_redirty_request(req
);
927 nfs_end_page_writeback(req
->wb_page
);
928 nfs_clear_page_tag_locked(req
);
933 static void nfs_pageio_init_write(struct nfs_pageio_descriptor
*pgio
,
934 struct inode
*inode
, int ioflags
)
936 int wsize
= NFS_SERVER(inode
)->wsize
;
938 if (wsize
< PAGE_CACHE_SIZE
)
939 nfs_pageio_init(pgio
, inode
, nfs_flush_multi
, wsize
, ioflags
);
941 nfs_pageio_init(pgio
, inode
, nfs_flush_one
, wsize
, ioflags
);
945 * Handle a write reply that flushed part of a page.
947 static void nfs_writeback_done_partial(struct rpc_task
*task
, void *calldata
)
949 struct nfs_write_data
*data
= calldata
;
950 struct nfs_page
*req
= data
->req
;
951 struct page
*page
= req
->wb_page
;
953 dprintk("NFS: write (%s/%Ld %d@%Ld)",
954 req
->wb_context
->path
.dentry
->d_inode
->i_sb
->s_id
,
955 (long long)NFS_FILEID(req
->wb_context
->path
.dentry
->d_inode
),
957 (long long)req_offset(req
));
959 if (nfs_writeback_done(task
, data
) != 0)
962 if (task
->tk_status
< 0) {
963 nfs_set_pageerror(page
);
964 nfs_context_set_write_error(req
->wb_context
, task
->tk_status
);
965 dprintk(", error = %d\n", task
->tk_status
);
969 if (nfs_write_need_commit(data
)) {
970 struct inode
*inode
= page
->mapping
->host
;
972 spin_lock(&inode
->i_lock
);
973 if (test_bit(PG_NEED_RESCHED
, &req
->wb_flags
)) {
974 /* Do nothing we need to resend the writes */
975 } else if (!test_and_set_bit(PG_NEED_COMMIT
, &req
->wb_flags
)) {
976 memcpy(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
));
977 dprintk(" defer commit\n");
978 } else if (memcmp(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
))) {
979 set_bit(PG_NEED_RESCHED
, &req
->wb_flags
);
980 clear_bit(PG_NEED_COMMIT
, &req
->wb_flags
);
981 dprintk(" server reboot detected\n");
983 spin_unlock(&inode
->i_lock
);
988 if (atomic_dec_and_test(&req
->wb_complete
))
989 nfs_writepage_release(req
);
992 static const struct rpc_call_ops nfs_write_partial_ops
= {
993 .rpc_call_done
= nfs_writeback_done_partial
,
994 .rpc_release
= nfs_writedata_release
,
998 * Handle a write reply that flushes a whole page.
1000 * FIXME: There is an inherent race with invalidate_inode_pages and
1001 * writebacks since the page->count is kept > 1 for as long
1002 * as the page has a write request pending.
1004 static void nfs_writeback_done_full(struct rpc_task
*task
, void *calldata
)
1006 struct nfs_write_data
*data
= calldata
;
1007 struct nfs_page
*req
;
1010 if (nfs_writeback_done(task
, data
) != 0)
1013 /* Update attributes as result of writeback. */
1014 while (!list_empty(&data
->pages
)) {
1015 req
= nfs_list_entry(data
->pages
.next
);
1016 nfs_list_remove_request(req
);
1017 page
= req
->wb_page
;
1019 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1020 req
->wb_context
->path
.dentry
->d_inode
->i_sb
->s_id
,
1021 (long long)NFS_FILEID(req
->wb_context
->path
.dentry
->d_inode
),
1023 (long long)req_offset(req
));
1025 if (task
->tk_status
< 0) {
1026 nfs_set_pageerror(page
);
1027 nfs_context_set_write_error(req
->wb_context
, task
->tk_status
);
1028 dprintk(", error = %d\n", task
->tk_status
);
1029 goto remove_request
;
1032 if (nfs_write_need_commit(data
)) {
1033 memcpy(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
));
1034 nfs_mark_request_commit(req
);
1035 nfs_end_page_writeback(page
);
1036 dprintk(" marked for commit\n");
1039 /* Set the PG_uptodate flag? */
1040 nfs_mark_uptodate(page
, req
->wb_pgbase
, req
->wb_bytes
);
1043 nfs_end_page_writeback(page
);
1044 nfs_inode_remove_request(req
);
1046 nfs_clear_page_tag_locked(req
);
1050 static const struct rpc_call_ops nfs_write_full_ops
= {
1051 .rpc_call_done
= nfs_writeback_done_full
,
1052 .rpc_release
= nfs_writedata_release
,
1057 * This function is called when the WRITE call is complete.
1059 int nfs_writeback_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
1061 struct nfs_writeargs
*argp
= &data
->args
;
1062 struct nfs_writeres
*resp
= &data
->res
;
1065 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1066 task
->tk_pid
, task
->tk_status
);
1069 * ->write_done will attempt to use post-op attributes to detect
1070 * conflicting writes by other clients. A strict interpretation
1071 * of close-to-open would allow us to continue caching even if
1072 * another writer had changed the file, but some applications
1073 * depend on tighter cache coherency when writing.
1075 status
= NFS_PROTO(data
->inode
)->write_done(task
, data
);
1078 nfs_add_stats(data
->inode
, NFSIOS_SERVERWRITTENBYTES
, resp
->count
);
1080 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1081 if (resp
->verf
->committed
< argp
->stable
&& task
->tk_status
>= 0) {
1082 /* We tried a write call, but the server did not
1083 * commit data to stable storage even though we
1085 * Note: There is a known bug in Tru64 < 5.0 in which
1086 * the server reports NFS_DATA_SYNC, but performs
1087 * NFS_FILE_SYNC. We therefore implement this checking
1088 * as a dprintk() in order to avoid filling syslog.
1090 static unsigned long complain
;
1092 if (time_before(complain
, jiffies
)) {
1093 dprintk("NFS: faulty NFS server %s:"
1094 " (committed = %d) != (stable = %d)\n",
1095 NFS_SERVER(data
->inode
)->nfs_client
->cl_hostname
,
1096 resp
->verf
->committed
, argp
->stable
);
1097 complain
= jiffies
+ 300 * HZ
;
1101 /* Is this a short write? */
1102 if (task
->tk_status
>= 0 && resp
->count
< argp
->count
) {
1103 static unsigned long complain
;
1105 nfs_inc_stats(data
->inode
, NFSIOS_SHORTWRITE
);
1107 /* Has the server at least made some progress? */
1108 if (resp
->count
!= 0) {
1109 /* Was this an NFSv2 write or an NFSv3 stable write? */
1110 if (resp
->verf
->committed
!= NFS_UNSTABLE
) {
1111 /* Resend from where the server left off */
1112 argp
->offset
+= resp
->count
;
1113 argp
->pgbase
+= resp
->count
;
1114 argp
->count
-= resp
->count
;
1116 /* Resend as a stable write in order to avoid
1117 * headaches in the case of a server crash.
1119 argp
->stable
= NFS_FILE_SYNC
;
1121 rpc_restart_call(task
);
1124 if (time_before(complain
, jiffies
)) {
1126 "NFS: Server wrote zero bytes, expected %u.\n",
1128 complain
= jiffies
+ 300 * HZ
;
1130 /* Can't do anything about it except throw an error. */
1131 task
->tk_status
= -EIO
;
1137 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1138 void nfs_commit_release(void *wdata
)
1140 nfs_commit_free(wdata
);
1144 * Set up the argument/result storage required for the RPC call.
1146 static void nfs_commit_rpcsetup(struct list_head
*head
,
1147 struct nfs_write_data
*data
,
1150 struct nfs_page
*first
;
1151 struct inode
*inode
;
1154 /* Set up the RPC argument and reply structs
1155 * NB: take care not to mess about with data->commit et al. */
1157 list_splice_init(head
, &data
->pages
);
1158 first
= nfs_list_entry(data
->pages
.next
);
1159 inode
= first
->wb_context
->path
.dentry
->d_inode
;
1161 data
->inode
= inode
;
1162 data
->cred
= first
->wb_context
->cred
;
1164 data
->args
.fh
= NFS_FH(data
->inode
);
1165 /* Note: we always request a commit of the entire inode */
1166 data
->args
.offset
= 0;
1167 data
->args
.count
= 0;
1168 data
->res
.count
= 0;
1169 data
->res
.fattr
= &data
->fattr
;
1170 data
->res
.verf
= &data
->verf
;
1171 nfs_fattr_init(&data
->fattr
);
1173 /* Set up the initial task struct. */
1174 flags
= (how
& FLUSH_SYNC
) ? 0 : RPC_TASK_ASYNC
;
1175 rpc_init_task(&data
->task
, NFS_CLIENT(inode
), flags
, &nfs_commit_ops
, data
);
1176 NFS_PROTO(inode
)->commit_setup(data
, how
);
1178 data
->task
.tk_priority
= flush_task_priority(how
);
1179 data
->task
.tk_cookie
= (unsigned long)inode
;
1181 dprintk("NFS: %5u initiated commit call\n", data
->task
.tk_pid
);
1185 * Commit dirty pages
1188 nfs_commit_list(struct inode
*inode
, struct list_head
*head
, int how
)
1190 struct nfs_write_data
*data
;
1191 struct nfs_page
*req
;
1193 data
= nfs_commit_alloc();
1198 /* Set up the argument struct */
1199 nfs_commit_rpcsetup(head
, data
, how
);
1201 nfs_execute_write(data
);
1204 while (!list_empty(head
)) {
1205 req
= nfs_list_entry(head
->next
);
1206 nfs_list_remove_request(req
);
1207 nfs_mark_request_commit(req
);
1208 dec_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
1209 dec_bdi_stat(req
->wb_page
->mapping
->backing_dev_info
,
1211 nfs_clear_page_tag_locked(req
);
1217 * COMMIT call returned
1219 static void nfs_commit_done(struct rpc_task
*task
, void *calldata
)
1221 struct nfs_write_data
*data
= calldata
;
1222 struct nfs_page
*req
;
1224 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1225 task
->tk_pid
, task
->tk_status
);
1227 /* Call the NFS version-specific code */
1228 if (NFS_PROTO(data
->inode
)->commit_done(task
, data
) != 0)
1231 while (!list_empty(&data
->pages
)) {
1232 req
= nfs_list_entry(data
->pages
.next
);
1233 nfs_list_remove_request(req
);
1234 clear_bit(PG_NEED_COMMIT
, &(req
)->wb_flags
);
1235 dec_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
1236 dec_bdi_stat(req
->wb_page
->mapping
->backing_dev_info
,
1239 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1240 req
->wb_context
->path
.dentry
->d_inode
->i_sb
->s_id
,
1241 (long long)NFS_FILEID(req
->wb_context
->path
.dentry
->d_inode
),
1243 (long long)req_offset(req
));
1244 if (task
->tk_status
< 0) {
1245 nfs_context_set_write_error(req
->wb_context
, task
->tk_status
);
1246 nfs_inode_remove_request(req
);
1247 dprintk(", error = %d\n", task
->tk_status
);
1251 /* Okay, COMMIT succeeded, apparently. Check the verifier
1252 * returned by the server against all stored verfs. */
1253 if (!memcmp(req
->wb_verf
.verifier
, data
->verf
.verifier
, sizeof(data
->verf
.verifier
))) {
1254 /* We have a match */
1255 /* Set the PG_uptodate flag */
1256 nfs_mark_uptodate(req
->wb_page
, req
->wb_pgbase
,
1258 nfs_inode_remove_request(req
);
1262 /* We have a mismatch. Write the page again */
1263 dprintk(" mismatch\n");
1264 nfs_redirty_request(req
);
1266 nfs_clear_page_tag_locked(req
);
1270 static const struct rpc_call_ops nfs_commit_ops
= {
1271 .rpc_call_done
= nfs_commit_done
,
1272 .rpc_release
= nfs_commit_release
,
1275 int nfs_commit_inode(struct inode
*inode
, int how
)
1280 spin_lock(&inode
->i_lock
);
1281 res
= nfs_scan_commit(inode
, &head
, 0, 0);
1282 spin_unlock(&inode
->i_lock
);
1284 int error
= nfs_commit_list(inode
, &head
, how
);
1291 static inline int nfs_commit_list(struct inode
*inode
, struct list_head
*head
, int how
)
1297 long nfs_sync_mapping_wait(struct address_space
*mapping
, struct writeback_control
*wbc
, int how
)
1299 struct inode
*inode
= mapping
->host
;
1300 pgoff_t idx_start
, idx_end
;
1301 unsigned int npages
= 0;
1303 int nocommit
= how
& FLUSH_NOCOMMIT
;
1307 if (wbc
->range_cyclic
)
1310 idx_start
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
1311 idx_end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
1312 if (idx_end
> idx_start
) {
1313 pgoff_t l_npages
= 1 + idx_end
- idx_start
;
1315 if (sizeof(npages
) != sizeof(l_npages
) &&
1316 (pgoff_t
)npages
!= l_npages
)
1320 how
&= ~FLUSH_NOCOMMIT
;
1321 spin_lock(&inode
->i_lock
);
1323 ret
= nfs_wait_on_requests_locked(inode
, idx_start
, npages
);
1328 pages
= nfs_scan_commit(inode
, &head
, idx_start
, npages
);
1331 if (how
& FLUSH_INVALIDATE
) {
1332 spin_unlock(&inode
->i_lock
);
1333 nfs_cancel_commit_list(&head
);
1335 spin_lock(&inode
->i_lock
);
1338 pages
+= nfs_scan_commit(inode
, &head
, 0, 0);
1339 spin_unlock(&inode
->i_lock
);
1340 ret
= nfs_commit_list(inode
, &head
, how
);
1341 spin_lock(&inode
->i_lock
);
1344 spin_unlock(&inode
->i_lock
);
1348 static int __nfs_write_mapping(struct address_space
*mapping
, struct writeback_control
*wbc
, int how
)
1352 ret
= nfs_writepages(mapping
, wbc
);
1355 ret
= nfs_sync_mapping_wait(mapping
, wbc
, how
);
1360 __mark_inode_dirty(mapping
->host
, I_DIRTY_PAGES
);
1364 /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
1365 static int nfs_write_mapping(struct address_space
*mapping
, int how
)
1367 struct writeback_control wbc
= {
1368 .bdi
= mapping
->backing_dev_info
,
1369 .sync_mode
= WB_SYNC_NONE
,
1370 .nr_to_write
= LONG_MAX
,
1371 .for_writepages
= 1,
1376 ret
= __nfs_write_mapping(mapping
, &wbc
, how
);
1379 wbc
.sync_mode
= WB_SYNC_ALL
;
1380 return __nfs_write_mapping(mapping
, &wbc
, how
);
1384 * flush the inode to disk.
1386 int nfs_wb_all(struct inode
*inode
)
1388 return nfs_write_mapping(inode
->i_mapping
, 0);
1391 int nfs_wb_nocommit(struct inode
*inode
)
1393 return nfs_write_mapping(inode
->i_mapping
, FLUSH_NOCOMMIT
);
1396 int nfs_wb_page_cancel(struct inode
*inode
, struct page
*page
)
1398 struct nfs_page
*req
;
1399 loff_t range_start
= page_offset(page
);
1400 loff_t range_end
= range_start
+ (loff_t
)(PAGE_CACHE_SIZE
- 1);
1401 struct writeback_control wbc
= {
1402 .bdi
= page
->mapping
->backing_dev_info
,
1403 .sync_mode
= WB_SYNC_ALL
,
1404 .nr_to_write
= LONG_MAX
,
1405 .range_start
= range_start
,
1406 .range_end
= range_end
,
1410 BUG_ON(!PageLocked(page
));
1412 req
= nfs_page_find_request(page
);
1415 if (test_bit(PG_NEED_COMMIT
, &req
->wb_flags
)) {
1416 nfs_release_request(req
);
1419 if (nfs_lock_request_dontget(req
)) {
1420 nfs_inode_remove_request(req
);
1422 * In case nfs_inode_remove_request has marked the
1423 * page as being dirty
1425 cancel_dirty_page(page
, PAGE_CACHE_SIZE
);
1426 nfs_unlock_request(req
);
1429 ret
= nfs_wait_on_request(req
);
1433 if (!PagePrivate(page
))
1435 ret
= nfs_sync_mapping_wait(page
->mapping
, &wbc
, FLUSH_INVALIDATE
);
1440 static int nfs_wb_page_priority(struct inode
*inode
, struct page
*page
,
1443 loff_t range_start
= page_offset(page
);
1444 loff_t range_end
= range_start
+ (loff_t
)(PAGE_CACHE_SIZE
- 1);
1445 struct writeback_control wbc
= {
1446 .bdi
= page
->mapping
->backing_dev_info
,
1447 .sync_mode
= WB_SYNC_ALL
,
1448 .nr_to_write
= LONG_MAX
,
1449 .range_start
= range_start
,
1450 .range_end
= range_end
,
1454 BUG_ON(!PageLocked(page
));
1455 if (clear_page_dirty_for_io(page
)) {
1456 ret
= nfs_writepage_locked(page
, &wbc
);
1460 if (!PagePrivate(page
))
1462 ret
= nfs_sync_mapping_wait(page
->mapping
, &wbc
, how
);
1466 __mark_inode_dirty(inode
, I_DIRTY_PAGES
);
1471 * Write back all requests on one page - we do this before reading it.
1473 int nfs_wb_page(struct inode
*inode
, struct page
* page
)
1475 return nfs_wb_page_priority(inode
, page
, FLUSH_STABLE
);
1478 int __init
nfs_init_writepagecache(void)
1480 nfs_wdata_cachep
= kmem_cache_create("nfs_write_data",
1481 sizeof(struct nfs_write_data
),
1482 0, SLAB_HWCACHE_ALIGN
,
1484 if (nfs_wdata_cachep
== NULL
)
1487 nfs_wdata_mempool
= mempool_create_slab_pool(MIN_POOL_WRITE
,
1489 if (nfs_wdata_mempool
== NULL
)
1492 nfs_commit_mempool
= mempool_create_slab_pool(MIN_POOL_COMMIT
,
1494 if (nfs_commit_mempool
== NULL
)
1498 * NFS congestion size, scale with available memory.
1510 * This allows larger machines to have larger/more transfers.
1511 * Limit the default to 256M
1513 nfs_congestion_kb
= (16*int_sqrt(totalram_pages
)) << (PAGE_SHIFT
-10);
1514 if (nfs_congestion_kb
> 256*1024)
1515 nfs_congestion_kb
= 256*1024;
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
);