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