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