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