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