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