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