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