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