Fix prototype of SMP version of synchronize_irq.
[linux-2.6/linux-mips.git] / fs / nfs / write.c
blobeff2765cee6e1778b8db14592e7953739ea9302c
1 /*
2 * linux/fs/nfs/write.c
4 * Writing file data over NFS.
6 * We do it like this: When a (user) process wishes to write data to an
7 * NFS file, a write request is allocated that contains the RPC task data
8 * plus some info on the page to be written, and added to the inode's
9 * write chain. If the process writes past the end of the page, an async
10 * RPC call to write the page is scheduled immediately; otherwise, the call
11 * is delayed for a few seconds.
13 * Just like readahead, no async I/O is performed if wsize < PAGE_SIZE.
15 * Write requests are kept on the inode's writeback list. Each entry in
16 * that list references the page (portion) to be written. When the
17 * cache timeout has expired, the RPC task is woken up, and tries to
18 * lock the page. As soon as it manages to do so, the request is moved
19 * from the writeback list to the writelock list.
21 * Note: we must make sure never to confuse the inode passed in the
22 * write_page request with the one in page->inode. As far as I understand
23 * it, these are different when doing a swap-out.
25 * To understand everything that goes on here and in the NFS read code,
26 * one should be aware that a page is locked in exactly one of the following
27 * cases:
29 * - A write request is in progress.
30 * - A user process is in generic_file_write/nfs_update_page
31 * - A user process is in generic_file_read
33 * Also note that because of the way pages are invalidated in
34 * nfs_revalidate_inode, the following assertions hold:
36 * - If a page is dirty, there will be no read requests (a page will
37 * not be re-read unless invalidated by nfs_revalidate_inode).
38 * - If the page is not uptodate, there will be no pending write
39 * requests, and no process will be in nfs_update_page.
41 * FIXME: Interaction with the vmscan routines is not optimal yet.
42 * Either vmscan must be made nfs-savvy, or we need a different page
43 * reclaim concept that supports something like FS-independent
44 * buffer_heads with a b_ops-> field.
46 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
49 #include <linux/config.h>
50 #include <linux/types.h>
51 #include <linux/slab.h>
52 #include <linux/mm.h>
53 #include <linux/pagemap.h>
54 #include <linux/file.h>
55 #include <linux/mpage.h>
56 #include <linux/writeback.h>
58 #include <linux/sunrpc/clnt.h>
59 #include <linux/nfs_fs.h>
60 #include <linux/nfs_mount.h>
61 #include <linux/nfs_page.h>
62 #include <asm/uaccess.h>
63 #include <linux/smp_lock.h>
64 #include <linux/mempool.h>
66 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
68 #define MIN_POOL_WRITE (32)
69 #define MIN_POOL_COMMIT (4)
72 * Local function declarations
74 static struct nfs_page * nfs_update_request(struct file*, struct inode *,
75 struct page *,
76 unsigned int, unsigned int);
77 static void nfs_strategy(struct inode *inode);
79 static kmem_cache_t *nfs_wdata_cachep;
80 static mempool_t *nfs_wdata_mempool;
81 static mempool_t *nfs_commit_mempool;
83 static __inline__ struct nfs_write_data *nfs_writedata_alloc(void)
85 struct nfs_write_data *p;
86 p = (struct nfs_write_data *)mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
87 if (p) {
88 memset(p, 0, sizeof(*p));
89 INIT_LIST_HEAD(&p->pages);
91 return p;
94 static __inline__ void nfs_writedata_free(struct nfs_write_data *p)
96 mempool_free(p, nfs_wdata_mempool);
99 void nfs_writedata_release(struct rpc_task *task)
101 struct nfs_write_data *wdata = (struct nfs_write_data *)task->tk_calldata;
102 nfs_writedata_free(wdata);
105 static __inline__ struct nfs_write_data *nfs_commit_alloc(void)
107 struct nfs_write_data *p;
108 p = (struct nfs_write_data *)mempool_alloc(nfs_commit_mempool, SLAB_NOFS);
109 if (p) {
110 memset(p, 0, sizeof(*p));
111 INIT_LIST_HEAD(&p->pages);
113 return p;
116 static __inline__ void nfs_commit_free(struct nfs_write_data *p)
118 mempool_free(p, nfs_commit_mempool);
121 void nfs_commit_release(struct rpc_task *task)
123 struct nfs_write_data *wdata = (struct nfs_write_data *)task->tk_calldata;
124 nfs_commit_free(wdata);
128 * Write a page synchronously.
129 * Offset is the data offset within the page.
131 static int
132 nfs_writepage_sync(struct file *file, struct inode *inode, struct page *page,
133 unsigned int offset, unsigned int count)
135 struct rpc_cred *cred = NULL;
136 loff_t base;
137 unsigned int wsize = NFS_SERVER(inode)->wsize;
138 int result, refresh = 0, written = 0, flags;
139 u8 *buffer;
140 struct nfs_fattr fattr;
141 struct nfs_writeverf verf;
144 if (file)
145 cred = get_rpccred(nfs_file_cred(file));
146 if (!cred)
147 cred = get_rpccred(NFS_I(inode)->mm_cred);
149 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
150 inode->i_sb->s_id,
151 (long long)NFS_FILEID(inode),
152 count, (long long)(page_offset(page) + offset));
154 base = page_offset(page) + offset;
156 flags = ((IS_SWAPFILE(inode)) ? NFS_RW_SWAP : 0) | NFS_RW_SYNC;
158 do {
159 if (count < wsize && !IS_SWAPFILE(inode))
160 wsize = count;
162 result = NFS_PROTO(inode)->write(inode, cred, &fattr, flags,
163 offset, wsize, page, &verf);
165 if (result < 0) {
166 /* Must mark the page invalid after I/O error */
167 ClearPageUptodate(page);
168 goto io_error;
170 if (result != wsize)
171 printk("NFS: short write, wsize=%u, result=%d\n",
172 wsize, result);
173 refresh = 1;
174 buffer += wsize;
175 base += wsize;
176 offset += wsize;
177 written += wsize;
178 count -= wsize;
180 * If we've extended the file, update the inode
181 * now so we don't invalidate the cache.
183 if (base > inode->i_size)
184 inode->i_size = base;
185 } while (count);
187 if (PageError(page))
188 ClearPageError(page);
190 io_error:
191 if (cred)
192 put_rpccred(cred);
194 return written? written : result;
197 static int
198 nfs_writepage_async(struct file *file, struct inode *inode, struct page *page,
199 unsigned int offset, unsigned int count)
201 struct nfs_page *req;
202 loff_t end;
203 int status;
205 req = nfs_update_request(file, inode, page, offset, count);
206 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
207 if (status < 0)
208 goto out;
209 if (!req->wb_cred)
210 req->wb_cred = get_rpccred(NFS_I(inode)->mm_cred);
211 nfs_unlock_request(req);
212 nfs_strategy(inode);
213 end = ((loff_t)page->index<<PAGE_CACHE_SHIFT) + (loff_t)(offset + count);
214 if (inode->i_size < end)
215 inode->i_size = end;
217 out:
218 return status;
222 * Write an mmapped page to the server.
225 nfs_writepage(struct page *page, struct writeback_control *wbc)
227 struct inode *inode = page->mapping->host;
228 unsigned long end_index;
229 unsigned offset = PAGE_CACHE_SIZE;
230 int err;
232 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
234 /* Ensure we've flushed out any previous writes */
235 nfs_wb_page(inode,page);
237 /* easy case */
238 if (page->index < end_index)
239 goto do_it;
240 /* things got complicated... */
241 offset = inode->i_size & (PAGE_CACHE_SIZE-1);
243 /* OK, are we completely out? */
244 err = -EIO;
245 if (page->index >= end_index+1 || !offset)
246 goto out;
247 do_it:
248 lock_kernel();
249 if (NFS_SERVER(inode)->wsize >= PAGE_CACHE_SIZE && !IS_SYNC(inode)) {
250 err = nfs_writepage_async(NULL, inode, page, 0, offset);
251 if (err >= 0)
252 err = 0;
253 } else {
254 err = nfs_writepage_sync(NULL, inode, page, 0, offset);
255 if (err == offset)
256 err = 0;
258 unlock_kernel();
259 out:
260 unlock_page(page);
261 return err;
265 nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
267 struct inode *inode = mapping->host;
268 int is_sync = !wbc->nonblocking;
269 int err;
271 err = generic_writepages(mapping, wbc);
272 if (err)
273 goto out;
274 err = nfs_flush_file(inode, NULL, 0, 0, 0);
275 if (err < 0)
276 goto out;
277 if (wbc->sync_mode == WB_SYNC_HOLD)
278 goto out;
279 if (is_sync && wbc->sync_mode == WB_SYNC_ALL) {
280 err = nfs_wb_all(inode);
281 } else
282 nfs_commit_file(inode, NULL, 0, 0, 0);
283 out:
284 return err;
288 * Insert a write request into an inode
290 static inline int
291 nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
293 struct nfs_inode *nfsi = NFS_I(inode);
294 int error;
296 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
297 BUG_ON(error == -EEXIST);
298 if (error)
299 return error;
300 if (!nfsi->npages)
301 igrab(inode);
302 nfsi->npages++;
303 req->wb_count++;
304 return 0;
308 * Insert a write request into an inode
310 static inline void
311 nfs_inode_remove_request(struct nfs_page *req)
313 struct nfs_inode *nfsi;
314 struct inode *inode;
316 BUG_ON (!NFS_WBACK_BUSY(req));
317 spin_lock(&nfs_wreq_lock);
318 inode = req->wb_inode;
319 nfsi = NFS_I(inode);
320 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
321 nfsi->npages--;
322 if (!nfsi->npages) {
323 spin_unlock(&nfs_wreq_lock);
324 iput(inode);
325 } else
326 spin_unlock(&nfs_wreq_lock);
327 nfs_clear_request(req);
328 nfs_release_request(req);
332 * Find a request
334 static inline struct nfs_page *
335 _nfs_find_request(struct inode *inode, unsigned long index)
337 struct nfs_inode *nfsi = NFS_I(inode);
338 struct nfs_page *req;
340 req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index);
341 if (req)
342 req->wb_count++;
343 return req;
346 static struct nfs_page *
347 nfs_find_request(struct inode *inode, unsigned long index)
349 struct nfs_page *req;
351 spin_lock(&nfs_wreq_lock);
352 req = _nfs_find_request(inode, index);
353 spin_unlock(&nfs_wreq_lock);
354 return req;
358 * Add a request to the inode's dirty list.
360 static inline void
361 nfs_mark_request_dirty(struct nfs_page *req)
363 struct inode *inode = req->wb_inode;
364 struct nfs_inode *nfsi = NFS_I(inode);
366 spin_lock(&nfs_wreq_lock);
367 nfs_list_add_request(req, &nfsi->dirty);
368 nfsi->ndirty++;
369 spin_unlock(&nfs_wreq_lock);
370 inc_page_state(nr_dirty);
371 mark_inode_dirty(inode);
375 * Check if a request is dirty
377 static inline int
378 nfs_dirty_request(struct nfs_page *req)
380 struct nfs_inode *nfsi = NFS_I(req->wb_inode);
381 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
384 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
386 * Add a request to the inode's commit list.
388 static inline void
389 nfs_mark_request_commit(struct nfs_page *req)
391 struct inode *inode = req->wb_inode;
392 struct nfs_inode *nfsi = NFS_I(inode);
394 spin_lock(&nfs_wreq_lock);
395 nfs_list_add_request(req, &nfsi->commit);
396 nfsi->ncommit++;
397 spin_unlock(&nfs_wreq_lock);
398 inc_page_state(nr_unstable);
399 mark_inode_dirty(inode);
401 #endif
404 * Wait for a request to complete.
406 * Interruptible by signals only if mounted with intr flag.
408 static int
409 nfs_wait_on_requests(struct inode *inode, struct file *file, unsigned long idx_start, unsigned int npages)
411 struct nfs_inode *nfsi = NFS_I(inode);
412 struct nfs_page *req;
413 unsigned long idx_end, next;
414 unsigned int res = 0;
415 int error;
417 if (npages == 0)
418 idx_end = ~0;
419 else
420 idx_end = idx_start + npages - 1;
422 spin_lock(&nfs_wreq_lock);
423 next = idx_start;
424 while (radix_tree_gang_lookup(&nfsi->nfs_page_tree, (void **)&req, next, 1)) {
425 if (req->wb_index > idx_end)
426 break;
428 next = req->wb_index + 1;
429 if (file && req->wb_file != file)
430 continue;
431 if (!NFS_WBACK_BUSY(req))
432 continue;
434 req->wb_count++;
435 spin_unlock(&nfs_wreq_lock);
436 error = nfs_wait_on_request(req);
437 nfs_release_request(req);
438 if (error < 0)
439 return error;
440 spin_lock(&nfs_wreq_lock);
441 next = idx_start;
442 res++;
444 spin_unlock(&nfs_wreq_lock);
445 return res;
449 * nfs_scan_dirty - Scan an inode for dirty requests
450 * @inode: NFS inode to scan
451 * @dst: destination list
452 * @file: if set, ensure we match requests from this file
453 * @idx_start: lower bound of page->index to scan.
454 * @npages: idx_start + npages sets the upper bound to scan.
456 * Moves requests from the inode's dirty page list.
457 * The requests are *not* checked to ensure that they form a contiguous set.
459 static int
460 nfs_scan_dirty(struct inode *inode, struct list_head *dst, struct file *file, unsigned long idx_start, unsigned int npages)
462 struct nfs_inode *nfsi = NFS_I(inode);
463 int res;
464 res = nfs_scan_list(&nfsi->dirty, dst, file, idx_start, npages);
465 nfsi->ndirty -= res;
466 sub_page_state(nr_dirty,res);
467 if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
468 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
469 return res;
472 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
474 * nfs_scan_commit - Scan an inode for commit requests
475 * @inode: NFS inode to scan
476 * @dst: destination list
477 * @file: if set, ensure we collect requests from this file only.
478 * @idx_start: lower bound of page->index to scan.
479 * @npages: idx_start + npages sets the upper bound to scan.
481 * Moves requests from the inode's 'commit' request list.
482 * The requests are *not* checked to ensure that they form a contiguous set.
484 static int
485 nfs_scan_commit(struct inode *inode, struct list_head *dst, struct file *file, unsigned long idx_start, unsigned int npages)
487 struct nfs_inode *nfsi = NFS_I(inode);
488 int res;
489 res = nfs_scan_list(&nfsi->commit, dst, file, idx_start, npages);
490 nfsi->ncommit -= res;
491 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
492 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
493 return res;
495 #endif
499 * Try to update any existing write request, or create one if there is none.
500 * In order to match, the request's credentials must match those of
501 * the calling process.
503 * Note: Should always be called with the Page Lock held!
505 static struct nfs_page *
506 nfs_update_request(struct file* file, struct inode *inode, struct page *page,
507 unsigned int offset, unsigned int bytes)
509 struct nfs_page *req, *new = NULL;
510 unsigned long rqend, end;
512 end = offset + bytes;
514 for (;;) {
515 /* Loop over all inode entries and see if we find
516 * A request for the page we wish to update
518 spin_lock(&nfs_wreq_lock);
519 req = _nfs_find_request(inode, page->index);
520 if (req) {
521 if (!nfs_lock_request_dontget(req)) {
522 int error;
523 spin_unlock(&nfs_wreq_lock);
524 error = nfs_wait_on_request(req);
525 nfs_release_request(req);
526 if (error < 0)
527 return ERR_PTR(error);
528 continue;
530 spin_unlock(&nfs_wreq_lock);
531 if (new)
532 nfs_release_request(new);
533 break;
536 if (new) {
537 int error;
538 nfs_lock_request_dontget(new);
539 error = nfs_inode_add_request(inode, new);
540 if (error) {
541 spin_unlock(&nfs_wreq_lock);
542 nfs_unlock_request(new);
543 return ERR_PTR(error);
545 spin_unlock(&nfs_wreq_lock);
546 nfs_mark_request_dirty(new);
547 return new;
549 spin_unlock(&nfs_wreq_lock);
551 new = nfs_create_request(nfs_file_cred(file), inode, page, offset, bytes);
552 if (IS_ERR(new))
553 return new;
554 if (file) {
555 new->wb_file = file;
556 get_file(file);
560 /* We have a request for our page.
561 * If the creds don't match, or the
562 * page addresses don't match,
563 * tell the caller to wait on the conflicting
564 * request.
566 rqend = req->wb_offset + req->wb_bytes;
567 if (req->wb_file != file
568 || req->wb_page != page
569 || !nfs_dirty_request(req)
570 || offset > rqend || end < req->wb_offset) {
571 nfs_unlock_request(req);
572 return ERR_PTR(-EBUSY);
575 /* Okay, the request matches. Update the region */
576 if (offset < req->wb_offset) {
577 req->wb_offset = offset;
578 req->wb_pgbase = offset;
579 req->wb_bytes = rqend - req->wb_offset;
582 if (end > rqend)
583 req->wb_bytes = end - req->wb_offset;
585 return req;
589 * This is the strategy routine for NFS.
590 * It is called by nfs_updatepage whenever the user wrote up to the end
591 * of a page.
593 * We always try to submit a set of requests in parallel so that the
594 * server's write code can gather writes. This is mainly for the benefit
595 * of NFSv2.
597 * We never submit more requests than we think the remote can handle.
598 * For UDP sockets, we make sure we don't exceed the congestion window;
599 * for TCP, we limit the number of requests to 8.
601 * NFS_STRATEGY_PAGES gives the minimum number of requests for NFSv2 that
602 * should be sent out in one go. This is for the benefit of NFSv2 servers
603 * that perform write gathering.
605 * FIXME: Different servers may have different sweet spots.
606 * Record the average congestion window in server struct?
608 #define NFS_STRATEGY_PAGES 8
609 static void
610 nfs_strategy(struct inode *inode)
612 unsigned int dirty, wpages;
614 dirty = NFS_I(inode)->ndirty;
615 wpages = NFS_SERVER(inode)->wpages;
616 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
617 if (NFS_PROTO(inode)->version == 2) {
618 if (dirty >= NFS_STRATEGY_PAGES * wpages)
619 nfs_flush_file(inode, NULL, 0, 0, 0);
620 } else if (dirty >= wpages)
621 nfs_flush_file(inode, NULL, 0, 0, 0);
622 #else
623 if (dirty >= NFS_STRATEGY_PAGES * wpages)
624 nfs_flush_file(inode, NULL, 0, 0, 0);
625 #endif
629 nfs_flush_incompatible(struct file *file, struct page *page)
631 struct inode *inode = page->mapping->host;
632 struct rpc_cred *cred = nfs_file_cred(file);
633 struct nfs_page *req;
634 int status = 0;
636 * Look for a request corresponding to this page. If there
637 * is one, and it belongs to another file, we flush it out
638 * before we try to copy anything into the page. Do this
639 * due to the lack of an ACCESS-type call in NFSv2.
640 * Also do the same if we find a request from an existing
641 * dropped page.
643 req = nfs_find_request(inode, page->index);
644 if (req) {
645 if (req->wb_file != file || req->wb_cred != cred || req->wb_page != page)
646 status = nfs_wb_page(inode, page);
647 nfs_release_request(req);
649 return (status < 0) ? status : 0;
653 * Update and possibly write a cached page of an NFS file.
655 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
656 * things with a page scheduled for an RPC call (e.g. invalidate it).
659 nfs_updatepage(struct file *file, struct page *page, unsigned int offset, unsigned int count)
661 struct dentry *dentry = file->f_dentry;
662 struct inode *inode = page->mapping->host;
663 struct nfs_page *req;
664 loff_t end;
665 int status = 0;
667 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
668 dentry->d_parent->d_name.name, dentry->d_name.name,
669 count, (long long)(page_offset(page) +offset));
672 * If wsize is smaller than page size, update and write
673 * page synchronously.
675 if (NFS_SERVER(inode)->wsize < PAGE_CACHE_SIZE || IS_SYNC(inode)) {
676 status = nfs_writepage_sync(file, inode, page, offset, count);
677 if (status > 0) {
678 if (offset == 0 && status == PAGE_CACHE_SIZE)
679 SetPageUptodate(page);
680 return 0;
682 return status;
686 * Try to find an NFS request corresponding to this page
687 * and update it.
688 * If the existing request cannot be updated, we must flush
689 * it out now.
691 do {
692 req = nfs_update_request(file, inode, page, offset, count);
693 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
694 if (status != -EBUSY)
695 break;
696 /* Request could not be updated. Flush it out and try again */
697 status = nfs_wb_page(inode, page);
698 } while (status >= 0);
699 if (status < 0)
700 goto done;
702 status = 0;
703 end = ((loff_t)page->index<<PAGE_CACHE_SHIFT) + (loff_t)(offset + count);
704 if (inode->i_size < end)
705 inode->i_size = end;
707 /* If we wrote past the end of the page.
708 * Call the strategy routine so it can send out a bunch
709 * of requests.
711 if (req->wb_pgbase == 0 && req->wb_bytes == PAGE_CACHE_SIZE) {
712 SetPageUptodate(page);
713 nfs_unlock_request(req);
714 nfs_strategy(inode);
715 } else
716 nfs_unlock_request(req);
717 done:
718 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
719 status, (long long)inode->i_size);
720 if (status < 0)
721 ClearPageUptodate(page);
722 return status;
726 * Set up the argument/result storage required for the RPC call.
728 static void
729 nfs_write_rpcsetup(struct list_head *head, struct nfs_write_data *data, int how)
731 struct rpc_task *task = &data->task;
732 struct inode *inode;
733 struct nfs_page *req;
734 struct page **pages;
735 unsigned int count;
737 /* Set up the RPC argument and reply structs
738 * NB: take care not to mess about with data->commit et al. */
740 pages = data->pagevec;
741 count = 0;
742 while (!list_empty(head)) {
743 req = nfs_list_entry(head->next);
744 nfs_list_remove_request(req);
745 nfs_list_add_request(req, &data->pages);
746 SetPageWriteback(req->wb_page);
747 *pages++ = req->wb_page;
748 count += req->wb_bytes;
750 req = nfs_list_entry(data->pages.next);
751 data->inode = inode = req->wb_inode;
752 data->cred = req->wb_cred;
754 NFS_PROTO(inode)->write_setup(data, count, how);
756 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
757 task->tk_pid,
758 inode->i_sb->s_id,
759 (long long)NFS_FILEID(inode),
760 count,
761 (unsigned long long)req_offset(req));
765 * Create an RPC task for the given write request and kick it.
766 * The page must have been locked by the caller.
768 * It may happen that the page we're passed is not marked dirty.
769 * This is the case if nfs_updatepage detects a conflicting request
770 * that has been written but not committed.
772 static int
773 nfs_flush_one(struct list_head *head, struct inode *inode, int how)
775 struct rpc_clnt *clnt = NFS_CLIENT(inode);
776 struct nfs_write_data *data;
777 sigset_t oldset;
779 data = nfs_writedata_alloc();
780 if (!data)
781 goto out_bad;
783 /* Set up the argument struct */
784 nfs_write_rpcsetup(head, data, how);
786 rpc_clnt_sigmask(clnt, &oldset);
787 lock_kernel();
788 rpc_execute(&data->task);
789 unlock_kernel();
790 rpc_clnt_sigunmask(clnt, &oldset);
791 return 0;
792 out_bad:
793 while (!list_empty(head)) {
794 struct nfs_page *req = nfs_list_entry(head->next);
795 nfs_list_remove_request(req);
796 nfs_mark_request_dirty(req);
797 nfs_unlock_request(req);
799 return -ENOMEM;
803 nfs_flush_list(struct list_head *head, int wpages, int how)
805 LIST_HEAD(one_request);
806 struct nfs_page *req;
807 int error = 0;
808 unsigned int pages = 0;
810 while (!list_empty(head)) {
811 pages += nfs_coalesce_requests(head, &one_request, wpages);
812 req = nfs_list_entry(one_request.next);
813 error = nfs_flush_one(&one_request, req->wb_inode, how);
814 if (error < 0)
815 break;
817 if (error >= 0)
818 return pages;
820 while (!list_empty(head)) {
821 req = nfs_list_entry(head->next);
822 nfs_list_remove_request(req);
823 nfs_mark_request_dirty(req);
824 nfs_unlock_request(req);
826 return error;
831 * This function is called when the WRITE call is complete.
833 void
834 nfs_writeback_done(struct rpc_task *task)
836 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
837 struct nfs_writeargs *argp = &data->args;
838 struct nfs_writeres *resp = &data->res;
839 struct nfs_page *req;
840 struct page *page;
842 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
843 task->tk_pid, task->tk_status);
845 /* We can't handle that yet but we check for it nevertheless */
846 if (resp->count < argp->count && task->tk_status >= 0) {
847 static unsigned long complain;
848 if (time_before(complain, jiffies)) {
849 printk(KERN_WARNING
850 "NFS: Server wrote less than requested.\n");
851 complain = jiffies + 300 * HZ;
853 /* Can't do anything about it right now except throw
854 * an error. */
855 task->tk_status = -EIO;
857 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
858 if (data->verf.committed < argp->stable && task->tk_status >= 0) {
859 /* We tried a write call, but the server did not
860 * commit data to stable storage even though we
861 * requested it.
862 * Note: There is a known bug in Tru64 < 5.0 in which
863 * the server reports NFS_DATA_SYNC, but performs
864 * NFS_FILE_SYNC. We therefore implement this checking
865 * as a dprintk() in order to avoid filling syslog.
867 static unsigned long complain;
869 if (time_before(complain, jiffies)) {
870 dprintk("NFS: faulty NFS server %s:"
871 " (committed = %d) != (stable = %d)\n",
872 NFS_SERVER(data->inode)->hostname,
873 data->verf.committed, argp->stable);
874 complain = jiffies + 300 * HZ;
877 #endif
880 * Update attributes as result of writeback.
881 * FIXME: There is an inherent race with invalidate_inode_pages and
882 * writebacks since the page->count is kept > 1 for as long
883 * as the page has a write request pending.
885 while (!list_empty(&data->pages)) {
886 req = nfs_list_entry(data->pages.next);
887 nfs_list_remove_request(req);
888 page = req->wb_page;
890 dprintk("NFS: write (%s/%Ld %d@%Ld)",
891 req->wb_inode->i_sb->s_id,
892 (long long)NFS_FILEID(req->wb_inode),
893 req->wb_bytes,
894 (long long)req_offset(req));
896 if (task->tk_status < 0) {
897 ClearPageUptodate(page);
898 SetPageError(page);
899 if (req->wb_file)
900 req->wb_file->f_error = task->tk_status;
901 end_page_writeback(page);
902 nfs_inode_remove_request(req);
903 dprintk(", error = %d\n", task->tk_status);
904 goto next;
906 end_page_writeback(page);
908 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
909 if (argp->stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
910 nfs_inode_remove_request(req);
911 dprintk(" OK\n");
912 goto next;
914 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
915 nfs_mark_request_commit(req);
916 dprintk(" marked for commit\n");
917 #else
918 nfs_inode_remove_request(req);
919 #endif
920 next:
921 nfs_unlock_request(req);
926 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
928 * Set up the argument/result storage required for the RPC call.
930 static void
931 nfs_commit_rpcsetup(struct list_head *head, struct nfs_write_data *data, int how)
933 struct rpc_task *task = &data->task;
934 struct nfs_page *first, *last;
935 struct inode *inode;
936 loff_t start, end, len;
938 /* Set up the RPC argument and reply structs
939 * NB: take care not to mess about with data->commit et al. */
941 list_splice_init(head, &data->pages);
942 first = nfs_list_entry(data->pages.next);
943 last = nfs_list_entry(data->pages.prev);
944 inode = first->wb_inode;
947 * Determine the offset range of requests in the COMMIT call.
948 * We rely on the fact that data->pages is an ordered list...
950 start = req_offset(first);
951 end = req_offset(last) + last->wb_bytes;
952 len = end - start;
953 /* If 'len' is not a 32-bit quantity, pass '0' in the COMMIT call */
954 if (end >= inode->i_size || len < 0 || len > (~((u32)0) >> 1))
955 len = 0;
957 data->inode = inode;
958 data->cred = first->wb_cred;
960 NFS_PROTO(inode)->commit_setup(data, start, len, how);
962 dprintk("NFS: %4d initiated commit call\n", task->tk_pid);
966 * Commit dirty pages
969 nfs_commit_list(struct list_head *head, int how)
971 struct rpc_clnt *clnt;
972 struct nfs_write_data *data;
973 struct nfs_page *req;
974 sigset_t oldset;
976 data = nfs_commit_alloc();
978 if (!data)
979 goto out_bad;
981 /* Set up the argument struct */
982 nfs_commit_rpcsetup(head, data, how);
983 clnt = NFS_CLIENT(data->inode);
985 rpc_clnt_sigmask(clnt, &oldset);
986 lock_kernel();
987 rpc_execute(&data->task);
988 unlock_kernel();
989 rpc_clnt_sigunmask(clnt, &oldset);
990 return 0;
991 out_bad:
992 while (!list_empty(head)) {
993 req = nfs_list_entry(head->next);
994 nfs_list_remove_request(req);
995 nfs_mark_request_commit(req);
996 nfs_unlock_request(req);
998 return -ENOMEM;
1002 * COMMIT call returned
1004 void
1005 nfs_commit_done(struct rpc_task *task)
1007 struct nfs_write_data *data = (struct nfs_write_data *)task->tk_calldata;
1008 struct nfs_page *req;
1009 int res = 0;
1011 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1012 task->tk_pid, task->tk_status);
1014 while (!list_empty(&data->pages)) {
1015 req = nfs_list_entry(data->pages.next);
1016 nfs_list_remove_request(req);
1018 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1019 req->wb_inode->i_sb->s_id,
1020 (long long)NFS_FILEID(req->wb_inode),
1021 req->wb_bytes,
1022 (long long)req_offset(req));
1023 if (task->tk_status < 0) {
1024 if (req->wb_file)
1025 req->wb_file->f_error = task->tk_status;
1026 nfs_inode_remove_request(req);
1027 dprintk(", error = %d\n", task->tk_status);
1028 goto next;
1031 /* Okay, COMMIT succeeded, apparently. Check the verifier
1032 * returned by the server against all stored verfs. */
1033 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1034 /* We have a match */
1035 nfs_inode_remove_request(req);
1036 dprintk(" OK\n");
1037 goto next;
1039 /* We have a mismatch. Write the page again */
1040 dprintk(" mismatch\n");
1041 nfs_mark_request_dirty(req);
1042 next:
1043 nfs_unlock_request(req);
1044 res++;
1046 sub_page_state(nr_unstable,res);
1048 #endif
1050 int nfs_flush_file(struct inode *inode, struct file *file, unsigned long idx_start,
1051 unsigned int npages, int how)
1053 LIST_HEAD(head);
1054 int res,
1055 error = 0;
1057 spin_lock(&nfs_wreq_lock);
1058 res = nfs_scan_dirty(inode, &head, file, idx_start, npages);
1059 spin_unlock(&nfs_wreq_lock);
1060 if (res)
1061 error = nfs_flush_list(&head, NFS_SERVER(inode)->wpages, how);
1062 if (error < 0)
1063 return error;
1064 return res;
1067 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1068 int nfs_commit_file(struct inode *inode, struct file *file, unsigned long idx_start,
1069 unsigned int npages, int how)
1071 LIST_HEAD(head);
1072 int res,
1073 error = 0;
1075 spin_lock(&nfs_wreq_lock);
1076 res = nfs_scan_commit(inode, &head, file, idx_start, npages);
1077 spin_unlock(&nfs_wreq_lock);
1078 if (res)
1079 error = nfs_commit_list(&head, how);
1080 if (error < 0)
1081 return error;
1082 return res;
1084 #endif
1086 int nfs_sync_file(struct inode *inode, struct file *file, unsigned long idx_start,
1087 unsigned int npages, int how)
1089 int error,
1090 wait;
1092 wait = how & FLUSH_WAIT;
1093 how &= ~FLUSH_WAIT;
1095 if (!inode && file)
1096 inode = file->f_dentry->d_inode;
1098 do {
1099 error = 0;
1100 if (wait)
1101 error = nfs_wait_on_requests(inode, file, idx_start, npages);
1102 if (error == 0)
1103 error = nfs_flush_file(inode, file, idx_start, npages, how);
1104 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1105 if (error == 0)
1106 error = nfs_commit_file(inode, file, idx_start, npages, how);
1107 #endif
1108 } while (error > 0);
1109 return error;
1112 int nfs_init_writepagecache(void)
1114 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1115 sizeof(struct nfs_write_data),
1116 0, SLAB_HWCACHE_ALIGN,
1117 NULL, NULL);
1118 if (nfs_wdata_cachep == NULL)
1119 return -ENOMEM;
1121 nfs_wdata_mempool = mempool_create(MIN_POOL_WRITE,
1122 mempool_alloc_slab,
1123 mempool_free_slab,
1124 nfs_wdata_cachep);
1125 if (nfs_wdata_mempool == NULL)
1126 return -ENOMEM;
1128 nfs_commit_mempool = mempool_create(MIN_POOL_COMMIT,
1129 mempool_alloc_slab,
1130 mempool_free_slab,
1131 nfs_wdata_cachep);
1132 if (nfs_commit_mempool == NULL)
1133 return -ENOMEM;
1135 return 0;
1138 void nfs_destroy_writepagecache(void)
1140 mempool_destroy(nfs_commit_mempool);
1141 mempool_destroy(nfs_wdata_mempool);
1142 if (kmem_cache_destroy(nfs_wdata_cachep))
1143 printk(KERN_INFO "nfs_write_data: not all structures were freed\n");