[POWERPC] Fix ohare IDE irq workaround on old powermacs
[pandora-kernel.git] / fs / nfs / write.c
1 /*
2  * linux/fs/nfs/write.c
3  *
4  * Writing file data over NFS.
5  *
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.
12  *
13  * Just like readahead, no async I/O is performed if wsize < PAGE_SIZE.
14  *
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.
20  *
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.
24  *
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:
28  *
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
32  *
33  * Also note that because of the way pages are invalidated in
34  * nfs_revalidate_inode, the following assertions hold:
35  *
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.
40  *
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.
45  *
46  * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
47  */
48
49 #include <linux/types.h>
50 #include <linux/slab.h>
51 #include <linux/mm.h>
52 #include <linux/pagemap.h>
53 #include <linux/file.h>
54 #include <linux/mpage.h>
55 #include <linux/writeback.h>
56
57 #include <linux/sunrpc/clnt.h>
58 #include <linux/nfs_fs.h>
59 #include <linux/nfs_mount.h>
60 #include <linux/nfs_page.h>
61 #include <asm/uaccess.h>
62 #include <linux/smp_lock.h>
63
64 #include "delegation.h"
65 #include "iostat.h"
66
67 #define NFSDBG_FACILITY         NFSDBG_PAGECACHE
68
69 #define MIN_POOL_WRITE          (32)
70 #define MIN_POOL_COMMIT         (4)
71
72 /*
73  * Local function declarations
74  */
75 static struct nfs_page * nfs_update_request(struct nfs_open_context*,
76                                             struct inode *,
77                                             struct page *,
78                                             unsigned int, unsigned int);
79 static int nfs_wait_on_write_congestion(struct address_space *, int);
80 static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int);
81 static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
82                            unsigned int npages, int how);
83 static const struct rpc_call_ops nfs_write_partial_ops;
84 static const struct rpc_call_ops nfs_write_full_ops;
85 static const struct rpc_call_ops nfs_commit_ops;
86
87 static kmem_cache_t *nfs_wdata_cachep;
88 static mempool_t *nfs_wdata_mempool;
89 static mempool_t *nfs_commit_mempool;
90
91 static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
92
93 struct nfs_write_data *nfs_commit_alloc(void)
94 {
95         struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS);
96
97         if (p) {
98                 memset(p, 0, sizeof(*p));
99                 INIT_LIST_HEAD(&p->pages);
100         }
101         return p;
102 }
103
104 void nfs_commit_free(struct nfs_write_data *p)
105 {
106         if (p && (p->pagevec != &p->page_array[0]))
107                 kfree(p->pagevec);
108         mempool_free(p, nfs_commit_mempool);
109 }
110
111 struct nfs_write_data *nfs_writedata_alloc(size_t len)
112 {
113         unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
114         struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
115
116         if (p) {
117                 memset(p, 0, sizeof(*p));
118                 INIT_LIST_HEAD(&p->pages);
119                 p->npages = pagecount;
120                 if (pagecount <= ARRAY_SIZE(p->page_array))
121                         p->pagevec = p->page_array;
122                 else {
123                         p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
124                         if (!p->pagevec) {
125                                 mempool_free(p, nfs_wdata_mempool);
126                                 p = NULL;
127                         }
128                 }
129         }
130         return p;
131 }
132
133 static void nfs_writedata_free(struct nfs_write_data *p)
134 {
135         if (p && (p->pagevec != &p->page_array[0]))
136                 kfree(p->pagevec);
137         mempool_free(p, nfs_wdata_mempool);
138 }
139
140 void nfs_writedata_release(void *wdata)
141 {
142         nfs_writedata_free(wdata);
143 }
144
145 /* Adjust the file length if we're writing beyond the end */
146 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
147 {
148         struct inode *inode = page->mapping->host;
149         loff_t end, i_size = i_size_read(inode);
150         unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
151
152         if (i_size > 0 && page->index < end_index)
153                 return;
154         end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
155         if (i_size >= end)
156                 return;
157         nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
158         i_size_write(inode, end);
159 }
160
161 /* We can set the PG_uptodate flag if we see that a write request
162  * covers the full page.
163  */
164 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
165 {
166         loff_t end_offs;
167
168         if (PageUptodate(page))
169                 return;
170         if (base != 0)
171                 return;
172         if (count == PAGE_CACHE_SIZE) {
173                 SetPageUptodate(page);
174                 return;
175         }
176
177         end_offs = i_size_read(page->mapping->host) - 1;
178         if (end_offs < 0)
179                 return;
180         /* Is this the last page? */
181         if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT))
182                 return;
183         /* This is the last page: set PG_uptodate if we cover the entire
184          * extent of the data, then zero the rest of the page.
185          */
186         if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) {
187                 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
188                 SetPageUptodate(page);
189         }
190 }
191
192 /*
193  * Write a page synchronously.
194  * Offset is the data offset within the page.
195  */
196 static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
197                 struct page *page, unsigned int offset, unsigned int count,
198                 int how)
199 {
200         unsigned int    wsize = NFS_SERVER(inode)->wsize;
201         int             result, written = 0;
202         struct nfs_write_data *wdata;
203
204         wdata = nfs_writedata_alloc(wsize);
205         if (!wdata)
206                 return -ENOMEM;
207
208         wdata->flags = how;
209         wdata->cred = ctx->cred;
210         wdata->inode = inode;
211         wdata->args.fh = NFS_FH(inode);
212         wdata->args.context = ctx;
213         wdata->args.pages = &page;
214         wdata->args.stable = NFS_FILE_SYNC;
215         wdata->args.pgbase = offset;
216         wdata->args.count = wsize;
217         wdata->res.fattr = &wdata->fattr;
218         wdata->res.verf = &wdata->verf;
219
220         dprintk("NFS:      nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
221                 inode->i_sb->s_id,
222                 (long long)NFS_FILEID(inode),
223                 count, (long long)(page_offset(page) + offset));
224
225         set_page_writeback(page);
226         nfs_begin_data_update(inode);
227         do {
228                 if (count < wsize)
229                         wdata->args.count = count;
230                 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
231
232                 result = NFS_PROTO(inode)->write(wdata);
233
234                 if (result < 0) {
235                         /* Must mark the page invalid after I/O error */
236                         ClearPageUptodate(page);
237                         goto io_error;
238                 }
239                 if (result < wdata->args.count)
240                         printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
241                                         wdata->args.count, result);
242
243                 wdata->args.offset += result;
244                 wdata->args.pgbase += result;
245                 written += result;
246                 count -= result;
247                 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
248         } while (count);
249         /* Update file length */
250         nfs_grow_file(page, offset, written);
251         /* Set the PG_uptodate flag? */
252         nfs_mark_uptodate(page, offset, written);
253
254         if (PageError(page))
255                 ClearPageError(page);
256
257 io_error:
258         nfs_end_data_update(inode);
259         end_page_writeback(page);
260         nfs_writedata_free(wdata);
261         return written ? written : result;
262 }
263
264 static int nfs_writepage_async(struct nfs_open_context *ctx,
265                 struct inode *inode, struct page *page,
266                 unsigned int offset, unsigned int count)
267 {
268         struct nfs_page *req;
269
270         req = nfs_update_request(ctx, inode, page, offset, count);
271         if (IS_ERR(req))
272                 return PTR_ERR(req);
273         /* Update file length */
274         nfs_grow_file(page, offset, count);
275         /* Set the PG_uptodate flag? */
276         nfs_mark_uptodate(page, offset, count);
277         nfs_unlock_request(req);
278         return 0;
279 }
280
281 static int wb_priority(struct writeback_control *wbc)
282 {
283         if (wbc->for_reclaim)
284                 return FLUSH_HIGHPRI;
285         if (wbc->for_kupdate)
286                 return FLUSH_LOWPRI;
287         return 0;
288 }
289
290 /*
291  * Write an mmapped page to the server.
292  */
293 int nfs_writepage(struct page *page, struct writeback_control *wbc)
294 {
295         struct nfs_open_context *ctx;
296         struct inode *inode = page->mapping->host;
297         unsigned long end_index;
298         unsigned offset = PAGE_CACHE_SIZE;
299         loff_t i_size = i_size_read(inode);
300         int inode_referenced = 0;
301         int priority = wb_priority(wbc);
302         int err;
303
304         nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
305         nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
306
307         /*
308          * Note: We need to ensure that we have a reference to the inode
309          *       if we are to do asynchronous writes. If not, waiting
310          *       in nfs_wait_on_request() may deadlock with clear_inode().
311          *
312          *       If igrab() fails here, then it is in any case safe to
313          *       call nfs_wb_page(), since there will be no pending writes.
314          */
315         if (igrab(inode) != 0)
316                 inode_referenced = 1;
317         end_index = i_size >> PAGE_CACHE_SHIFT;
318
319         /* Ensure we've flushed out any previous writes */
320         nfs_wb_page_priority(inode, page, priority);
321
322         /* easy case */
323         if (page->index < end_index)
324                 goto do_it;
325         /* things got complicated... */
326         offset = i_size & (PAGE_CACHE_SIZE-1);
327
328         /* OK, are we completely out? */
329         err = 0; /* potential race with truncate - ignore */
330         if (page->index >= end_index+1 || !offset)
331                 goto out;
332 do_it:
333         ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
334         if (ctx == NULL) {
335                 err = -EBADF;
336                 goto out;
337         }
338         lock_kernel();
339         if (!IS_SYNC(inode) && inode_referenced) {
340                 err = nfs_writepage_async(ctx, inode, page, 0, offset);
341                 if (!wbc->for_writepages)
342                         nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
343         } else {
344                 err = nfs_writepage_sync(ctx, inode, page, 0,
345                                                 offset, priority);
346                 if (err >= 0) {
347                         if (err != offset)
348                                 redirty_page_for_writepage(wbc, page);
349                         err = 0;
350                 }
351         }
352         unlock_kernel();
353         put_nfs_open_context(ctx);
354 out:
355         unlock_page(page);
356         if (inode_referenced)
357                 iput(inode);
358         return err; 
359 }
360
361 /*
362  * Note: causes nfs_update_request() to block on the assumption
363  *       that the writeback is generated due to memory pressure.
364  */
365 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
366 {
367         struct backing_dev_info *bdi = mapping->backing_dev_info;
368         struct inode *inode = mapping->host;
369         int err;
370
371         nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
372
373         err = generic_writepages(mapping, wbc);
374         if (err)
375                 return err;
376         while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
377                 if (wbc->nonblocking)
378                         return 0;
379                 nfs_wait_on_write_congestion(mapping, 0);
380         }
381         err = nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
382         if (err < 0)
383                 goto out;
384         nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
385         wbc->nr_to_write -= err;
386         if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
387                 err = nfs_wait_on_requests(inode, 0, 0);
388                 if (err < 0)
389                         goto out;
390         }
391         err = nfs_commit_inode(inode, wb_priority(wbc));
392         if (err > 0) {
393                 wbc->nr_to_write -= err;
394                 err = 0;
395         }
396 out:
397         clear_bit(BDI_write_congested, &bdi->state);
398         wake_up_all(&nfs_write_congestion);
399         return err;
400 }
401
402 /*
403  * Insert a write request into an inode
404  */
405 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
406 {
407         struct nfs_inode *nfsi = NFS_I(inode);
408         int error;
409
410         error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
411         BUG_ON(error == -EEXIST);
412         if (error)
413                 return error;
414         if (!nfsi->npages) {
415                 igrab(inode);
416                 nfs_begin_data_update(inode);
417                 if (nfs_have_delegation(inode, FMODE_WRITE))
418                         nfsi->change_attr++;
419         }
420         SetPagePrivate(req->wb_page);
421         nfsi->npages++;
422         atomic_inc(&req->wb_count);
423         return 0;
424 }
425
426 /*
427  * Insert a write request into an inode
428  */
429 static void nfs_inode_remove_request(struct nfs_page *req)
430 {
431         struct inode *inode = req->wb_context->dentry->d_inode;
432         struct nfs_inode *nfsi = NFS_I(inode);
433
434         BUG_ON (!NFS_WBACK_BUSY(req));
435
436         spin_lock(&nfsi->req_lock);
437         ClearPagePrivate(req->wb_page);
438         radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
439         nfsi->npages--;
440         if (!nfsi->npages) {
441                 spin_unlock(&nfsi->req_lock);
442                 nfs_end_data_update(inode);
443                 iput(inode);
444         } else
445                 spin_unlock(&nfsi->req_lock);
446         nfs_clear_request(req);
447         nfs_release_request(req);
448 }
449
450 /*
451  * Find a request
452  */
453 static inline struct nfs_page *
454 _nfs_find_request(struct inode *inode, unsigned long index)
455 {
456         struct nfs_inode *nfsi = NFS_I(inode);
457         struct nfs_page *req;
458
459         req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index);
460         if (req)
461                 atomic_inc(&req->wb_count);
462         return req;
463 }
464
465 static struct nfs_page *
466 nfs_find_request(struct inode *inode, unsigned long index)
467 {
468         struct nfs_page         *req;
469         struct nfs_inode        *nfsi = NFS_I(inode);
470
471         spin_lock(&nfsi->req_lock);
472         req = _nfs_find_request(inode, index);
473         spin_unlock(&nfsi->req_lock);
474         return req;
475 }
476
477 /*
478  * Add a request to the inode's dirty list.
479  */
480 static void
481 nfs_mark_request_dirty(struct nfs_page *req)
482 {
483         struct inode *inode = req->wb_context->dentry->d_inode;
484         struct nfs_inode *nfsi = NFS_I(inode);
485
486         spin_lock(&nfsi->req_lock);
487         radix_tree_tag_set(&nfsi->nfs_page_tree,
488                         req->wb_index, NFS_PAGE_TAG_DIRTY);
489         nfs_list_add_request(req, &nfsi->dirty);
490         nfsi->ndirty++;
491         spin_unlock(&nfsi->req_lock);
492         inc_zone_page_state(req->wb_page, NR_FILE_DIRTY);
493         mark_inode_dirty(inode);
494 }
495
496 /*
497  * Check if a request is dirty
498  */
499 static inline int
500 nfs_dirty_request(struct nfs_page *req)
501 {
502         struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
503         return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
504 }
505
506 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
507 /*
508  * Add a request to the inode's commit list.
509  */
510 static void
511 nfs_mark_request_commit(struct nfs_page *req)
512 {
513         struct inode *inode = req->wb_context->dentry->d_inode;
514         struct nfs_inode *nfsi = NFS_I(inode);
515
516         spin_lock(&nfsi->req_lock);
517         nfs_list_add_request(req, &nfsi->commit);
518         nfsi->ncommit++;
519         spin_unlock(&nfsi->req_lock);
520         inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
521         mark_inode_dirty(inode);
522 }
523 #endif
524
525 /*
526  * Wait for a request to complete.
527  *
528  * Interruptible by signals only if mounted with intr flag.
529  */
530 static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
531 {
532         struct nfs_inode *nfsi = NFS_I(inode);
533         struct nfs_page *req;
534         unsigned long           idx_end, next;
535         unsigned int            res = 0;
536         int                     error;
537
538         if (npages == 0)
539                 idx_end = ~0;
540         else
541                 idx_end = idx_start + npages - 1;
542
543         next = idx_start;
544         while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
545                 if (req->wb_index > idx_end)
546                         break;
547
548                 next = req->wb_index + 1;
549                 BUG_ON(!NFS_WBACK_BUSY(req));
550
551                 atomic_inc(&req->wb_count);
552                 spin_unlock(&nfsi->req_lock);
553                 error = nfs_wait_on_request(req);
554                 nfs_release_request(req);
555                 spin_lock(&nfsi->req_lock);
556                 if (error < 0)
557                         return error;
558                 res++;
559         }
560         return res;
561 }
562
563 static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
564 {
565         struct nfs_inode *nfsi = NFS_I(inode);
566         int ret;
567
568         spin_lock(&nfsi->req_lock);
569         ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
570         spin_unlock(&nfsi->req_lock);
571         return ret;
572 }
573
574 static void nfs_cancel_dirty_list(struct list_head *head)
575 {
576         struct nfs_page *req;
577         while(!list_empty(head)) {
578                 req = nfs_list_entry(head->next);
579                 nfs_list_remove_request(req);
580                 nfs_inode_remove_request(req);
581                 nfs_clear_page_writeback(req);
582         }
583 }
584
585 static void nfs_cancel_commit_list(struct list_head *head)
586 {
587         struct nfs_page *req;
588
589         while(!list_empty(head)) {
590                 req = nfs_list_entry(head->next);
591                 nfs_list_remove_request(req);
592                 nfs_inode_remove_request(req);
593                 nfs_clear_page_writeback(req);
594                 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
595         }
596 }
597
598 /*
599  * nfs_scan_dirty - Scan an inode for dirty requests
600  * @inode: NFS inode to scan
601  * @dst: destination list
602  * @idx_start: lower bound of page->index to scan.
603  * @npages: idx_start + npages sets the upper bound to scan.
604  *
605  * Moves requests from the inode's dirty page list.
606  * The requests are *not* checked to ensure that they form a contiguous set.
607  */
608 static int
609 nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
610 {
611         struct nfs_inode *nfsi = NFS_I(inode);
612         int res = 0;
613
614         if (nfsi->ndirty != 0) {
615                 res = nfs_scan_lock_dirty(nfsi, dst, idx_start, npages);
616                 nfsi->ndirty -= res;
617                 if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
618                         printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
619         }
620         return res;
621 }
622
623 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
624 /*
625  * nfs_scan_commit - Scan an inode for commit requests
626  * @inode: NFS inode to scan
627  * @dst: destination list
628  * @idx_start: lower bound of page->index to scan.
629  * @npages: idx_start + npages sets the upper bound to scan.
630  *
631  * Moves requests from the inode's 'commit' request list.
632  * The requests are *not* checked to ensure that they form a contiguous set.
633  */
634 static int
635 nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
636 {
637         struct nfs_inode *nfsi = NFS_I(inode);
638         int res = 0;
639
640         if (nfsi->ncommit != 0) {
641                 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
642                 nfsi->ncommit -= res;
643                 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
644                         printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
645         }
646         return res;
647 }
648 #else
649 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
650 {
651         return 0;
652 }
653 #endif
654
655 static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
656 {
657         struct backing_dev_info *bdi = mapping->backing_dev_info;
658         DEFINE_WAIT(wait);
659         int ret = 0;
660
661         might_sleep();
662
663         if (!bdi_write_congested(bdi))
664                 return 0;
665
666         nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
667
668         if (intr) {
669                 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
670                 sigset_t oldset;
671
672                 rpc_clnt_sigmask(clnt, &oldset);
673                 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
674                 if (bdi_write_congested(bdi)) {
675                         if (signalled())
676                                 ret = -ERESTARTSYS;
677                         else
678                                 schedule();
679                 }
680                 rpc_clnt_sigunmask(clnt, &oldset);
681         } else {
682                 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
683                 if (bdi_write_congested(bdi))
684                         schedule();
685         }
686         finish_wait(&nfs_write_congestion, &wait);
687         return ret;
688 }
689
690
691 /*
692  * Try to update any existing write request, or create one if there is none.
693  * In order to match, the request's credentials must match those of
694  * the calling process.
695  *
696  * Note: Should always be called with the Page Lock held!
697  */
698 static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
699                 struct inode *inode, struct page *page,
700                 unsigned int offset, unsigned int bytes)
701 {
702         struct nfs_server *server = NFS_SERVER(inode);
703         struct nfs_inode *nfsi = NFS_I(inode);
704         struct nfs_page         *req, *new = NULL;
705         unsigned long           rqend, end;
706
707         end = offset + bytes;
708
709         if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
710                 return ERR_PTR(-ERESTARTSYS);
711         for (;;) {
712                 /* Loop over all inode entries and see if we find
713                  * A request for the page we wish to update
714                  */
715                 spin_lock(&nfsi->req_lock);
716                 req = _nfs_find_request(inode, page->index);
717                 if (req) {
718                         if (!nfs_lock_request_dontget(req)) {
719                                 int error;
720                                 spin_unlock(&nfsi->req_lock);
721                                 error = nfs_wait_on_request(req);
722                                 nfs_release_request(req);
723                                 if (error < 0) {
724                                         if (new)
725                                                 nfs_release_request(new);
726                                         return ERR_PTR(error);
727                                 }
728                                 continue;
729                         }
730                         spin_unlock(&nfsi->req_lock);
731                         if (new)
732                                 nfs_release_request(new);
733                         break;
734                 }
735
736                 if (new) {
737                         int error;
738                         nfs_lock_request_dontget(new);
739                         error = nfs_inode_add_request(inode, new);
740                         if (error) {
741                                 spin_unlock(&nfsi->req_lock);
742                                 nfs_unlock_request(new);
743                                 return ERR_PTR(error);
744                         }
745                         spin_unlock(&nfsi->req_lock);
746                         nfs_mark_request_dirty(new);
747                         return new;
748                 }
749                 spin_unlock(&nfsi->req_lock);
750
751                 new = nfs_create_request(ctx, inode, page, offset, bytes);
752                 if (IS_ERR(new))
753                         return new;
754         }
755
756         /* We have a request for our page.
757          * If the creds don't match, or the
758          * page addresses don't match,
759          * tell the caller to wait on the conflicting
760          * request.
761          */
762         rqend = req->wb_offset + req->wb_bytes;
763         if (req->wb_context != ctx
764             || req->wb_page != page
765             || !nfs_dirty_request(req)
766             || offset > rqend || end < req->wb_offset) {
767                 nfs_unlock_request(req);
768                 return ERR_PTR(-EBUSY);
769         }
770
771         /* Okay, the request matches. Update the region */
772         if (offset < req->wb_offset) {
773                 req->wb_offset = offset;
774                 req->wb_pgbase = offset;
775                 req->wb_bytes = rqend - req->wb_offset;
776         }
777
778         if (end > rqend)
779                 req->wb_bytes = end - req->wb_offset;
780
781         return req;
782 }
783
784 int nfs_flush_incompatible(struct file *file, struct page *page)
785 {
786         struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
787         struct inode    *inode = page->mapping->host;
788         struct nfs_page *req;
789         int             status = 0;
790         /*
791          * Look for a request corresponding to this page. If there
792          * is one, and it belongs to another file, we flush it out
793          * before we try to copy anything into the page. Do this
794          * due to the lack of an ACCESS-type call in NFSv2.
795          * Also do the same if we find a request from an existing
796          * dropped page.
797          */
798         req = nfs_find_request(inode, page->index);
799         if (req) {
800                 if (req->wb_page != page || ctx != req->wb_context)
801                         status = nfs_wb_page(inode, page);
802                 nfs_release_request(req);
803         }
804         return (status < 0) ? status : 0;
805 }
806
807 /*
808  * Update and possibly write a cached page of an NFS file.
809  *
810  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
811  * things with a page scheduled for an RPC call (e.g. invalidate it).
812  */
813 int nfs_updatepage(struct file *file, struct page *page,
814                 unsigned int offset, unsigned int count)
815 {
816         struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
817         struct inode    *inode = page->mapping->host;
818         struct nfs_page *req;
819         int             status = 0;
820
821         nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
822
823         dprintk("NFS:      nfs_updatepage(%s/%s %d@%Ld)\n",
824                 file->f_dentry->d_parent->d_name.name,
825                 file->f_dentry->d_name.name, count,
826                 (long long)(page_offset(page) +offset));
827
828         if (IS_SYNC(inode)) {
829                 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
830                 if (status > 0) {
831                         if (offset == 0 && status == PAGE_CACHE_SIZE)
832                                 SetPageUptodate(page);
833                         return 0;
834                 }
835                 return status;
836         }
837
838         /* If we're not using byte range locks, and we know the page
839          * is entirely in cache, it may be more efficient to avoid
840          * fragmenting write requests.
841          */
842         if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
843                 loff_t end_offs = i_size_read(inode) - 1;
844                 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
845
846                 count += offset;
847                 offset = 0;
848                 if (unlikely(end_offs < 0)) {
849                         /* Do nothing */
850                 } else if (page->index == end_index) {
851                         unsigned int pglen;
852                         pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
853                         if (count < pglen)
854                                 count = pglen;
855                 } else if (page->index < end_index)
856                         count = PAGE_CACHE_SIZE;
857         }
858
859         /*
860          * Try to find an NFS request corresponding to this page
861          * and update it.
862          * If the existing request cannot be updated, we must flush
863          * it out now.
864          */
865         do {
866                 req = nfs_update_request(ctx, inode, page, offset, count);
867                 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
868                 if (status != -EBUSY)
869                         break;
870                 /* Request could not be updated. Flush it out and try again */
871                 status = nfs_wb_page(inode, page);
872         } while (status >= 0);
873         if (status < 0)
874                 goto done;
875
876         status = 0;
877
878         /* Update file length */
879         nfs_grow_file(page, offset, count);
880         /* Set the PG_uptodate flag? */
881         nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
882         nfs_unlock_request(req);
883 done:
884         dprintk("NFS:      nfs_updatepage returns %d (isize %Ld)\n",
885                         status, (long long)i_size_read(inode));
886         if (status < 0)
887                 ClearPageUptodate(page);
888         return status;
889 }
890
891 static void nfs_writepage_release(struct nfs_page *req)
892 {
893         end_page_writeback(req->wb_page);
894
895 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
896         if (!PageError(req->wb_page)) {
897                 if (NFS_NEED_RESCHED(req)) {
898                         nfs_mark_request_dirty(req);
899                         goto out;
900                 } else if (NFS_NEED_COMMIT(req)) {
901                         nfs_mark_request_commit(req);
902                         goto out;
903                 }
904         }
905         nfs_inode_remove_request(req);
906
907 out:
908         nfs_clear_commit(req);
909         nfs_clear_reschedule(req);
910 #else
911         nfs_inode_remove_request(req);
912 #endif
913         nfs_clear_page_writeback(req);
914 }
915
916 static inline int flush_task_priority(int how)
917 {
918         switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
919                 case FLUSH_HIGHPRI:
920                         return RPC_PRIORITY_HIGH;
921                 case FLUSH_LOWPRI:
922                         return RPC_PRIORITY_LOW;
923         }
924         return RPC_PRIORITY_NORMAL;
925 }
926
927 /*
928  * Set up the argument/result storage required for the RPC call.
929  */
930 static void nfs_write_rpcsetup(struct nfs_page *req,
931                 struct nfs_write_data *data,
932                 const struct rpc_call_ops *call_ops,
933                 unsigned int count, unsigned int offset,
934                 int how)
935 {
936         struct inode            *inode;
937         int flags;
938
939         /* Set up the RPC argument and reply structs
940          * NB: take care not to mess about with data->commit et al. */
941
942         data->req = req;
943         data->inode = inode = req->wb_context->dentry->d_inode;
944         data->cred = req->wb_context->cred;
945
946         data->args.fh     = NFS_FH(inode);
947         data->args.offset = req_offset(req) + offset;
948         data->args.pgbase = req->wb_pgbase + offset;
949         data->args.pages  = data->pagevec;
950         data->args.count  = count;
951         data->args.context = req->wb_context;
952
953         data->res.fattr   = &data->fattr;
954         data->res.count   = count;
955         data->res.verf    = &data->verf;
956         nfs_fattr_init(&data->fattr);
957
958         /* Set up the initial task struct.  */
959         flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
960         rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
961         NFS_PROTO(inode)->write_setup(data, how);
962
963         data->task.tk_priority = flush_task_priority(how);
964         data->task.tk_cookie = (unsigned long)inode;
965
966         dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
967                 data->task.tk_pid,
968                 inode->i_sb->s_id,
969                 (long long)NFS_FILEID(inode),
970                 count,
971                 (unsigned long long)data->args.offset);
972 }
973
974 static void nfs_execute_write(struct nfs_write_data *data)
975 {
976         struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
977         sigset_t oldset;
978
979         rpc_clnt_sigmask(clnt, &oldset);
980         lock_kernel();
981         rpc_execute(&data->task);
982         unlock_kernel();
983         rpc_clnt_sigunmask(clnt, &oldset);
984 }
985
986 /*
987  * Generate multiple small requests to write out a single
988  * contiguous dirty area on one page.
989  */
990 static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
991 {
992         struct nfs_page *req = nfs_list_entry(head->next);
993         struct page *page = req->wb_page;
994         struct nfs_write_data *data;
995         size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
996         unsigned int offset;
997         int requests = 0;
998         LIST_HEAD(list);
999
1000         nfs_list_remove_request(req);
1001
1002         nbytes = req->wb_bytes;
1003         do {
1004                 size_t len = min(nbytes, wsize);
1005
1006                 data = nfs_writedata_alloc(len);
1007                 if (!data)
1008                         goto out_bad;
1009                 list_add(&data->pages, &list);
1010                 requests++;
1011                 nbytes -= len;
1012         } while (nbytes != 0);
1013         atomic_set(&req->wb_complete, requests);
1014
1015         ClearPageError(page);
1016         set_page_writeback(page);
1017         offset = 0;
1018         nbytes = req->wb_bytes;
1019         do {
1020                 data = list_entry(list.next, struct nfs_write_data, pages);
1021                 list_del_init(&data->pages);
1022
1023                 data->pagevec[0] = page;
1024
1025                 if (nbytes > wsize) {
1026                         nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1027                                         wsize, offset, how);
1028                         offset += wsize;
1029                         nbytes -= wsize;
1030                 } else {
1031                         nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1032                                         nbytes, offset, how);
1033                         nbytes = 0;
1034                 }
1035                 nfs_execute_write(data);
1036         } while (nbytes != 0);
1037
1038         return 0;
1039
1040 out_bad:
1041         while (!list_empty(&list)) {
1042                 data = list_entry(list.next, struct nfs_write_data, pages);
1043                 list_del(&data->pages);
1044                 nfs_writedata_free(data);
1045         }
1046         nfs_mark_request_dirty(req);
1047         nfs_clear_page_writeback(req);
1048         return -ENOMEM;
1049 }
1050
1051 /*
1052  * Create an RPC task for the given write request and kick it.
1053  * The page must have been locked by the caller.
1054  *
1055  * It may happen that the page we're passed is not marked dirty.
1056  * This is the case if nfs_updatepage detects a conflicting request
1057  * that has been written but not committed.
1058  */
1059 static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
1060 {
1061         struct nfs_page         *req;
1062         struct page             **pages;
1063         struct nfs_write_data   *data;
1064         unsigned int            count;
1065
1066         data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
1067         if (!data)
1068                 goto out_bad;
1069
1070         pages = data->pagevec;
1071         count = 0;
1072         while (!list_empty(head)) {
1073                 req = nfs_list_entry(head->next);
1074                 nfs_list_remove_request(req);
1075                 nfs_list_add_request(req, &data->pages);
1076                 ClearPageError(req->wb_page);
1077                 set_page_writeback(req->wb_page);
1078                 *pages++ = req->wb_page;
1079                 count += req->wb_bytes;
1080         }
1081         req = nfs_list_entry(data->pages.next);
1082
1083         /* Set up the argument struct */
1084         nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
1085
1086         nfs_execute_write(data);
1087         return 0;
1088  out_bad:
1089         while (!list_empty(head)) {
1090                 struct nfs_page *req = nfs_list_entry(head->next);
1091                 nfs_list_remove_request(req);
1092                 nfs_mark_request_dirty(req);
1093                 nfs_clear_page_writeback(req);
1094         }
1095         return -ENOMEM;
1096 }
1097
1098 static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
1099 {
1100         LIST_HEAD(one_request);
1101         int (*flush_one)(struct inode *, struct list_head *, int);
1102         struct nfs_page *req;
1103         int wpages = NFS_SERVER(inode)->wpages;
1104         int wsize = NFS_SERVER(inode)->wsize;
1105         int error;
1106
1107         flush_one = nfs_flush_one;
1108         if (wsize < PAGE_CACHE_SIZE)
1109                 flush_one = nfs_flush_multi;
1110         /* For single writes, FLUSH_STABLE is more efficient */
1111         if (npages <= wpages && npages == NFS_I(inode)->npages
1112                         && nfs_list_entry(head->next)->wb_bytes <= wsize)
1113                 how |= FLUSH_STABLE;
1114
1115         do {
1116                 nfs_coalesce_requests(head, &one_request, wpages);
1117                 req = nfs_list_entry(one_request.next);
1118                 error = flush_one(inode, &one_request, how);
1119                 if (error < 0)
1120                         goto out_err;
1121         } while (!list_empty(head));
1122         return 0;
1123 out_err:
1124         while (!list_empty(head)) {
1125                 req = nfs_list_entry(head->next);
1126                 nfs_list_remove_request(req);
1127                 nfs_mark_request_dirty(req);
1128                 nfs_clear_page_writeback(req);
1129         }
1130         return error;
1131 }
1132
1133 /*
1134  * Handle a write reply that flushed part of a page.
1135  */
1136 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
1137 {
1138         struct nfs_write_data   *data = calldata;
1139         struct nfs_page         *req = data->req;
1140         struct page             *page = req->wb_page;
1141
1142         dprintk("NFS: write (%s/%Ld %d@%Ld)",
1143                 req->wb_context->dentry->d_inode->i_sb->s_id,
1144                 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1145                 req->wb_bytes,
1146                 (long long)req_offset(req));
1147
1148         if (nfs_writeback_done(task, data) != 0)
1149                 return;
1150
1151         if (task->tk_status < 0) {
1152                 ClearPageUptodate(page);
1153                 SetPageError(page);
1154                 req->wb_context->error = task->tk_status;
1155                 dprintk(", error = %d\n", task->tk_status);
1156         } else {
1157 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1158                 if (data->verf.committed < NFS_FILE_SYNC) {
1159                         if (!NFS_NEED_COMMIT(req)) {
1160                                 nfs_defer_commit(req);
1161                                 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1162                                 dprintk(" defer commit\n");
1163                         } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1164                                 nfs_defer_reschedule(req);
1165                                 dprintk(" server reboot detected\n");
1166                         }
1167                 } else
1168 #endif
1169                         dprintk(" OK\n");
1170         }
1171
1172         if (atomic_dec_and_test(&req->wb_complete))
1173                 nfs_writepage_release(req);
1174 }
1175
1176 static const struct rpc_call_ops nfs_write_partial_ops = {
1177         .rpc_call_done = nfs_writeback_done_partial,
1178         .rpc_release = nfs_writedata_release,
1179 };
1180
1181 /*
1182  * Handle a write reply that flushes a whole page.
1183  *
1184  * FIXME: There is an inherent race with invalidate_inode_pages and
1185  *        writebacks since the page->count is kept > 1 for as long
1186  *        as the page has a write request pending.
1187  */
1188 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1189 {
1190         struct nfs_write_data   *data = calldata;
1191         struct nfs_page         *req;
1192         struct page             *page;
1193
1194         if (nfs_writeback_done(task, data) != 0)
1195                 return;
1196
1197         /* Update attributes as result of writeback. */
1198         while (!list_empty(&data->pages)) {
1199                 req = nfs_list_entry(data->pages.next);
1200                 nfs_list_remove_request(req);
1201                 page = req->wb_page;
1202
1203                 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1204                         req->wb_context->dentry->d_inode->i_sb->s_id,
1205                         (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1206                         req->wb_bytes,
1207                         (long long)req_offset(req));
1208
1209                 if (task->tk_status < 0) {
1210                         ClearPageUptodate(page);
1211                         SetPageError(page);
1212                         req->wb_context->error = task->tk_status;
1213                         end_page_writeback(page);
1214                         nfs_inode_remove_request(req);
1215                         dprintk(", error = %d\n", task->tk_status);
1216                         goto next;
1217                 }
1218                 end_page_writeback(page);
1219
1220 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1221                 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1222                         nfs_inode_remove_request(req);
1223                         dprintk(" OK\n");
1224                         goto next;
1225                 }
1226                 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1227                 nfs_mark_request_commit(req);
1228                 dprintk(" marked for commit\n");
1229 #else
1230                 nfs_inode_remove_request(req);
1231 #endif
1232         next:
1233                 nfs_clear_page_writeback(req);
1234         }
1235 }
1236
1237 static const struct rpc_call_ops nfs_write_full_ops = {
1238         .rpc_call_done = nfs_writeback_done_full,
1239         .rpc_release = nfs_writedata_release,
1240 };
1241
1242
1243 /*
1244  * This function is called when the WRITE call is complete.
1245  */
1246 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1247 {
1248         struct nfs_writeargs    *argp = &data->args;
1249         struct nfs_writeres     *resp = &data->res;
1250         int status;
1251
1252         dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1253                 task->tk_pid, task->tk_status);
1254
1255         /* Call the NFS version-specific code */
1256         status = NFS_PROTO(data->inode)->write_done(task, data);
1257         if (status != 0)
1258                 return status;
1259         nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1260
1261 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1262         if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1263                 /* We tried a write call, but the server did not
1264                  * commit data to stable storage even though we
1265                  * requested it.
1266                  * Note: There is a known bug in Tru64 < 5.0 in which
1267                  *       the server reports NFS_DATA_SYNC, but performs
1268                  *       NFS_FILE_SYNC. We therefore implement this checking
1269                  *       as a dprintk() in order to avoid filling syslog.
1270                  */
1271                 static unsigned long    complain;
1272
1273                 if (time_before(complain, jiffies)) {
1274                         dprintk("NFS: faulty NFS server %s:"
1275                                 " (committed = %d) != (stable = %d)\n",
1276                                 NFS_SERVER(data->inode)->hostname,
1277                                 resp->verf->committed, argp->stable);
1278                         complain = jiffies + 300 * HZ;
1279                 }
1280         }
1281 #endif
1282         /* Is this a short write? */
1283         if (task->tk_status >= 0 && resp->count < argp->count) {
1284                 static unsigned long    complain;
1285
1286                 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1287
1288                 /* Has the server at least made some progress? */
1289                 if (resp->count != 0) {
1290                         /* Was this an NFSv2 write or an NFSv3 stable write? */
1291                         if (resp->verf->committed != NFS_UNSTABLE) {
1292                                 /* Resend from where the server left off */
1293                                 argp->offset += resp->count;
1294                                 argp->pgbase += resp->count;
1295                                 argp->count -= resp->count;
1296                         } else {
1297                                 /* Resend as a stable write in order to avoid
1298                                  * headaches in the case of a server crash.
1299                                  */
1300                                 argp->stable = NFS_FILE_SYNC;
1301                         }
1302                         rpc_restart_call(task);
1303                         return -EAGAIN;
1304                 }
1305                 if (time_before(complain, jiffies)) {
1306                         printk(KERN_WARNING
1307                                "NFS: Server wrote zero bytes, expected %u.\n",
1308                                         argp->count);
1309                         complain = jiffies + 300 * HZ;
1310                 }
1311                 /* Can't do anything about it except throw an error. */
1312                 task->tk_status = -EIO;
1313         }
1314         return 0;
1315 }
1316
1317
1318 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1319 void nfs_commit_release(void *wdata)
1320 {
1321         nfs_commit_free(wdata);
1322 }
1323
1324 /*
1325  * Set up the argument/result storage required for the RPC call.
1326  */
1327 static void nfs_commit_rpcsetup(struct list_head *head,
1328                 struct nfs_write_data *data,
1329                 int how)
1330 {
1331         struct nfs_page         *first;
1332         struct inode            *inode;
1333         int flags;
1334
1335         /* Set up the RPC argument and reply structs
1336          * NB: take care not to mess about with data->commit et al. */
1337
1338         list_splice_init(head, &data->pages);
1339         first = nfs_list_entry(data->pages.next);
1340         inode = first->wb_context->dentry->d_inode;
1341
1342         data->inode       = inode;
1343         data->cred        = first->wb_context->cred;
1344
1345         data->args.fh     = NFS_FH(data->inode);
1346         /* Note: we always request a commit of the entire inode */
1347         data->args.offset = 0;
1348         data->args.count  = 0;
1349         data->res.count   = 0;
1350         data->res.fattr   = &data->fattr;
1351         data->res.verf    = &data->verf;
1352         nfs_fattr_init(&data->fattr);
1353
1354         /* Set up the initial task struct.  */
1355         flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1356         rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
1357         NFS_PROTO(inode)->commit_setup(data, how);
1358
1359         data->task.tk_priority = flush_task_priority(how);
1360         data->task.tk_cookie = (unsigned long)inode;
1361         
1362         dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
1363 }
1364
1365 /*
1366  * Commit dirty pages
1367  */
1368 static int
1369 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1370 {
1371         struct nfs_write_data   *data;
1372         struct nfs_page         *req;
1373
1374         data = nfs_commit_alloc();
1375
1376         if (!data)
1377                 goto out_bad;
1378
1379         /* Set up the argument struct */
1380         nfs_commit_rpcsetup(head, data, how);
1381
1382         nfs_execute_write(data);
1383         return 0;
1384  out_bad:
1385         while (!list_empty(head)) {
1386                 req = nfs_list_entry(head->next);
1387                 nfs_list_remove_request(req);
1388                 nfs_mark_request_commit(req);
1389                 nfs_clear_page_writeback(req);
1390                 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1391         }
1392         return -ENOMEM;
1393 }
1394
1395 /*
1396  * COMMIT call returned
1397  */
1398 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1399 {
1400         struct nfs_write_data   *data = calldata;
1401         struct nfs_page         *req;
1402
1403         dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1404                                 task->tk_pid, task->tk_status);
1405
1406         /* Call the NFS version-specific code */
1407         if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1408                 return;
1409
1410         while (!list_empty(&data->pages)) {
1411                 req = nfs_list_entry(data->pages.next);
1412                 nfs_list_remove_request(req);
1413                 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1414
1415                 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1416                         req->wb_context->dentry->d_inode->i_sb->s_id,
1417                         (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1418                         req->wb_bytes,
1419                         (long long)req_offset(req));
1420                 if (task->tk_status < 0) {
1421                         req->wb_context->error = task->tk_status;
1422                         nfs_inode_remove_request(req);
1423                         dprintk(", error = %d\n", task->tk_status);
1424                         goto next;
1425                 }
1426
1427                 /* Okay, COMMIT succeeded, apparently. Check the verifier
1428                  * returned by the server against all stored verfs. */
1429                 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1430                         /* We have a match */
1431                         nfs_inode_remove_request(req);
1432                         dprintk(" OK\n");
1433                         goto next;
1434                 }
1435                 /* We have a mismatch. Write the page again */
1436                 dprintk(" mismatch\n");
1437                 nfs_mark_request_dirty(req);
1438         next:
1439                 nfs_clear_page_writeback(req);
1440         }
1441 }
1442
1443 static const struct rpc_call_ops nfs_commit_ops = {
1444         .rpc_call_done = nfs_commit_done,
1445         .rpc_release = nfs_commit_release,
1446 };
1447 #else
1448 static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1449 {
1450         return 0;
1451 }
1452 #endif
1453
1454 static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
1455                            unsigned int npages, int how)
1456 {
1457         struct nfs_inode *nfsi = NFS_I(inode);
1458         LIST_HEAD(head);
1459         int res;
1460
1461         spin_lock(&nfsi->req_lock);
1462         res = nfs_scan_dirty(inode, &head, idx_start, npages);
1463         spin_unlock(&nfsi->req_lock);
1464         if (res) {
1465                 int error = nfs_flush_list(inode, &head, res, how);
1466                 if (error < 0)
1467                         return error;
1468         }
1469         return res;
1470 }
1471
1472 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1473 int nfs_commit_inode(struct inode *inode, int how)
1474 {
1475         struct nfs_inode *nfsi = NFS_I(inode);
1476         LIST_HEAD(head);
1477         int res;
1478
1479         spin_lock(&nfsi->req_lock);
1480         res = nfs_scan_commit(inode, &head, 0, 0);
1481         spin_unlock(&nfsi->req_lock);
1482         if (res) {
1483                 int error = nfs_commit_list(inode, &head, how);
1484                 if (error < 0)
1485                         return error;
1486         }
1487         return res;
1488 }
1489 #endif
1490
1491 int nfs_sync_inode_wait(struct inode *inode, unsigned long idx_start,
1492                 unsigned int npages, int how)
1493 {
1494         struct nfs_inode *nfsi = NFS_I(inode);
1495         LIST_HEAD(head);
1496         int nocommit = how & FLUSH_NOCOMMIT;
1497         int pages, ret;
1498
1499         how &= ~FLUSH_NOCOMMIT;
1500         spin_lock(&nfsi->req_lock);
1501         do {
1502                 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1503                 if (ret != 0)
1504                         continue;
1505                 pages = nfs_scan_dirty(inode, &head, idx_start, npages);
1506                 if (pages != 0) {
1507                         spin_unlock(&nfsi->req_lock);
1508                         if (how & FLUSH_INVALIDATE)
1509                                 nfs_cancel_dirty_list(&head);
1510                         else
1511                                 ret = nfs_flush_list(inode, &head, pages, how);
1512                         spin_lock(&nfsi->req_lock);
1513                         continue;
1514                 }
1515                 if (nocommit)
1516                         break;
1517                 pages = nfs_scan_commit(inode, &head, idx_start, npages);
1518                 if (pages == 0)
1519                         break;
1520                 if (how & FLUSH_INVALIDATE) {
1521                         spin_unlock(&nfsi->req_lock);
1522                         nfs_cancel_commit_list(&head);
1523                         spin_lock(&nfsi->req_lock);
1524                         continue;
1525                 }
1526                 pages += nfs_scan_commit(inode, &head, 0, 0);
1527                 spin_unlock(&nfsi->req_lock);
1528                 ret = nfs_commit_list(inode, &head, how);
1529                 spin_lock(&nfsi->req_lock);
1530         } while (ret >= 0);
1531         spin_unlock(&nfsi->req_lock);
1532         return ret;
1533 }
1534
1535 int __init nfs_init_writepagecache(void)
1536 {
1537         nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1538                                              sizeof(struct nfs_write_data),
1539                                              0, SLAB_HWCACHE_ALIGN,
1540                                              NULL, NULL);
1541         if (nfs_wdata_cachep == NULL)
1542                 return -ENOMEM;
1543
1544         nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1545                                                      nfs_wdata_cachep);
1546         if (nfs_wdata_mempool == NULL)
1547                 return -ENOMEM;
1548
1549         nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1550                                                       nfs_wdata_cachep);
1551         if (nfs_commit_mempool == NULL)
1552                 return -ENOMEM;
1553
1554         return 0;
1555 }
1556
1557 void nfs_destroy_writepagecache(void)
1558 {
1559         mempool_destroy(nfs_commit_mempool);
1560         mempool_destroy(nfs_wdata_mempool);
1561         if (kmem_cache_destroy(nfs_wdata_cachep))
1562                 printk(KERN_INFO "nfs_write_data: not all structures were freed\n");
1563 }
1564