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