blob: 453d44666ea58250c2f3e55243c1781e75c9cb1a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/types.h>
50#include <linux/slab.h>
51#include <linux/mm.h>
52#include <linux/pagemap.h>
53#include <linux/file.h>
54#include <linux/mpage.h>
55#include <linux/writeback.h>
56
57#include <linux/sunrpc/clnt.h>
58#include <linux/nfs_fs.h>
59#include <linux/nfs_mount.h>
60#include <linux/nfs_page.h>
61#include <asm/uaccess.h>
62#include <linux/smp_lock.h>
63
64#include "delegation.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050065#include "iostat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
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 */
75static struct nfs_page * nfs_update_request(struct nfs_open_context*,
76 struct inode *,
77 struct page *,
78 unsigned int, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static int nfs_wait_on_write_congestion(struct address_space *, int);
80static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int);
81static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
82 unsigned int npages, int how);
Trond Myklebust788e7a82006-03-20 13:44:27 -050083static const struct rpc_call_ops nfs_write_partial_ops;
84static const struct rpc_call_ops nfs_write_full_ops;
85static const struct rpc_call_ops nfs_commit_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87static kmem_cache_t *nfs_wdata_cachep;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050088static mempool_t *nfs_wdata_mempool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static mempool_t *nfs_commit_mempool;
90
91static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
92
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070093struct nfs_write_data *nfs_commit_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS);
Chuck Lever40859d72005-11-30 18:09:02 -050096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (p) {
98 memset(p, 0, sizeof(*p));
99 INIT_LIST_HEAD(&p->pages);
100 }
101 return p;
102}
103
Trond Myklebuste17b1fc2006-03-20 13:44:35 -0500104void nfs_commit_free(struct nfs_write_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Chuck Lever40859d72005-11-30 18:09:02 -0500106 if (p && (p->pagevec != &p->page_array[0]))
107 kfree(p->pagevec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 mempool_free(p, nfs_commit_mempool);
109}
110
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700111struct nfs_write_data *nfs_writedata_alloc(size_t len)
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500112{
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700113 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500114 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
115
116 if (p) {
117 memset(p, 0, sizeof(*p));
118 INIT_LIST_HEAD(&p->pages);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700119 p->npages = pagecount;
Chuck Lever0d0b5cb2006-05-25 01:40:53 -0400120 if (pagecount <= ARRAY_SIZE(p->page_array))
121 p->pagevec = p->page_array;
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500122 else {
Chuck Lever0d0b5cb2006-05-25 01:40:53 -0400123 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
124 if (!p->pagevec) {
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500125 mempool_free(p, nfs_wdata_mempool);
126 p = NULL;
127 }
128 }
129 }
130 return p;
131}
132
Adrian Bunke4e20512006-08-03 15:07:47 -0400133static void nfs_writedata_free(struct nfs_write_data *p)
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500134{
135 if (p && (p->pagevec != &p->page_array[0]))
136 kfree(p->pagevec);
137 mempool_free(p, nfs_wdata_mempool);
138}
139
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100140void nfs_writedata_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 nfs_writedata_free(wdata);
143}
144
145/* Adjust the file length if we're writing beyond the end */
146static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
147{
148 struct inode *inode = page->mapping->host;
149 loff_t end, i_size = i_size_read(inode);
150 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
151
152 if (i_size > 0 && page->index < end_index)
153 return;
154 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
155 if (i_size >= end)
156 return;
Chuck Lever91d5b472006-03-20 13:44:14 -0500157 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 i_size_write(inode, end);
159}
160
161/* We can set the PG_uptodate flag if we see that a write request
162 * covers the full page.
163 */
164static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
165{
166 loff_t end_offs;
167
168 if (PageUptodate(page))
169 return;
170 if (base != 0)
171 return;
172 if (count == PAGE_CACHE_SIZE) {
173 SetPageUptodate(page);
174 return;
175 }
176
177 end_offs = i_size_read(page->mapping->host) - 1;
178 if (end_offs < 0)
179 return;
180 /* Is this the last page? */
181 if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT))
182 return;
183 /* This is the last page: set PG_uptodate if we cover the entire
184 * extent of the data, then zero the rest of the page.
185 */
186 if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) {
187 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
188 SetPageUptodate(page);
189 }
190}
191
192/*
193 * Write a page synchronously.
194 * Offset is the data offset within the page.
195 */
196static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
197 struct page *page, unsigned int offset, unsigned int count,
198 int how)
199{
200 unsigned int wsize = NFS_SERVER(inode)->wsize;
201 int result, written = 0;
202 struct nfs_write_data *wdata;
203
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700204 wdata = nfs_writedata_alloc(wsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (!wdata)
206 return -ENOMEM;
207
208 wdata->flags = how;
209 wdata->cred = ctx->cred;
210 wdata->inode = inode;
211 wdata->args.fh = NFS_FH(inode);
212 wdata->args.context = ctx;
213 wdata->args.pages = &page;
214 wdata->args.stable = NFS_FILE_SYNC;
215 wdata->args.pgbase = offset;
216 wdata->args.count = wsize;
217 wdata->res.fattr = &wdata->fattr;
218 wdata->res.verf = &wdata->verf;
219
220 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
221 inode->i_sb->s_id,
222 (long long)NFS_FILEID(inode),
223 count, (long long)(page_offset(page) + offset));
224
Trond Myklebustbb713d62005-12-03 15:20:14 -0500225 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 nfs_begin_data_update(inode);
227 do {
228 if (count < wsize)
229 wdata->args.count = count;
230 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
231
232 result = NFS_PROTO(inode)->write(wdata);
233
234 if (result < 0) {
235 /* Must mark the page invalid after I/O error */
236 ClearPageUptodate(page);
237 goto io_error;
238 }
239 if (result < wdata->args.count)
240 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
241 wdata->args.count, result);
242
243 wdata->args.offset += result;
244 wdata->args.pgbase += result;
245 written += result;
246 count -= result;
Chuck Lever91d5b472006-03-20 13:44:14 -0500247 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 } while (count);
249 /* Update file length */
250 nfs_grow_file(page, offset, written);
251 /* Set the PG_uptodate flag? */
252 nfs_mark_uptodate(page, offset, written);
253
254 if (PageError(page))
255 ClearPageError(page);
256
257io_error:
Trond Myklebust951a1432005-06-22 17:16:30 +0000258 nfs_end_data_update(inode);
Trond Myklebustbb713d62005-12-03 15:20:14 -0500259 end_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 nfs_writedata_free(wdata);
261 return written ? written : result;
262}
263
264static int nfs_writepage_async(struct nfs_open_context *ctx,
265 struct inode *inode, struct page *page,
266 unsigned int offset, unsigned int count)
267{
268 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 req = nfs_update_request(ctx, inode, page, offset, count);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100271 if (IS_ERR(req))
272 return PTR_ERR(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* Update file length */
274 nfs_grow_file(page, offset, count);
275 /* Set the PG_uptodate flag? */
276 nfs_mark_uptodate(page, offset, count);
277 nfs_unlock_request(req);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281static int wb_priority(struct writeback_control *wbc)
282{
283 if (wbc->for_reclaim)
284 return FLUSH_HIGHPRI;
285 if (wbc->for_kupdate)
286 return FLUSH_LOWPRI;
287 return 0;
288}
289
290/*
291 * Write an mmapped page to the server.
292 */
293int nfs_writepage(struct page *page, struct writeback_control *wbc)
294{
295 struct nfs_open_context *ctx;
296 struct inode *inode = page->mapping->host;
297 unsigned long end_index;
298 unsigned offset = PAGE_CACHE_SIZE;
299 loff_t i_size = i_size_read(inode);
300 int inode_referenced = 0;
301 int priority = wb_priority(wbc);
302 int err;
303
Chuck Lever91d5b472006-03-20 13:44:14 -0500304 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
305 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 /*
308 * Note: We need to ensure that we have a reference to the inode
309 * if we are to do asynchronous writes. If not, waiting
310 * in nfs_wait_on_request() may deadlock with clear_inode().
311 *
312 * If igrab() fails here, then it is in any case safe to
313 * call nfs_wb_page(), since there will be no pending writes.
314 */
315 if (igrab(inode) != 0)
316 inode_referenced = 1;
317 end_index = i_size >> PAGE_CACHE_SHIFT;
318
319 /* Ensure we've flushed out any previous writes */
320 nfs_wb_page_priority(inode, page, priority);
321
322 /* easy case */
323 if (page->index < end_index)
324 goto do_it;
325 /* things got complicated... */
326 offset = i_size & (PAGE_CACHE_SIZE-1);
327
328 /* OK, are we completely out? */
329 err = 0; /* potential race with truncate - ignore */
330 if (page->index >= end_index+1 || !offset)
331 goto out;
332do_it:
Trond Myklebustd5308382005-11-04 15:33:38 -0500333 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (ctx == NULL) {
335 err = -EBADF;
336 goto out;
337 }
338 lock_kernel();
339 if (!IS_SYNC(inode) && inode_referenced) {
340 err = nfs_writepage_async(ctx, inode, page, 0, offset);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100341 if (!wbc->for_writepages)
342 nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 } else {
344 err = nfs_writepage_sync(ctx, inode, page, 0,
345 offset, priority);
346 if (err >= 0) {
347 if (err != offset)
348 redirty_page_for_writepage(wbc, page);
349 err = 0;
350 }
351 }
352 unlock_kernel();
353 put_nfs_open_context(ctx);
354out:
355 unlock_page(page);
356 if (inode_referenced)
357 iput(inode);
358 return err;
359}
360
361/*
362 * Note: causes nfs_update_request() to block on the assumption
363 * that the writeback is generated due to memory pressure.
364 */
365int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
366{
367 struct backing_dev_info *bdi = mapping->backing_dev_info;
368 struct inode *inode = mapping->host;
369 int err;
370
Chuck Lever91d5b472006-03-20 13:44:14 -0500371 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 err = generic_writepages(mapping, wbc);
374 if (err)
375 return err;
376 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
377 if (wbc->nonblocking)
378 return 0;
379 nfs_wait_on_write_congestion(mapping, 0);
380 }
381 err = nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
382 if (err < 0)
383 goto out;
Chuck Lever91d5b472006-03-20 13:44:14 -0500384 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 wbc->nr_to_write -= err;
386 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
387 err = nfs_wait_on_requests(inode, 0, 0);
388 if (err < 0)
389 goto out;
390 }
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000391 err = nfs_commit_inode(inode, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (err > 0) {
393 wbc->nr_to_write -= err;
394 err = 0;
395 }
396out:
397 clear_bit(BDI_write_congested, &bdi->state);
398 wake_up_all(&nfs_write_congestion);
399 return err;
400}
401
402/*
403 * Insert a write request into an inode
404 */
405static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
406{
407 struct nfs_inode *nfsi = NFS_I(inode);
408 int error;
409
410 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
411 BUG_ON(error == -EEXIST);
412 if (error)
413 return error;
414 if (!nfsi->npages) {
415 igrab(inode);
416 nfs_begin_data_update(inode);
417 if (nfs_have_delegation(inode, FMODE_WRITE))
418 nfsi->change_attr++;
419 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500420 SetPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 nfsi->npages++;
422 atomic_inc(&req->wb_count);
423 return 0;
424}
425
426/*
427 * Insert a write request into an inode
428 */
429static void nfs_inode_remove_request(struct nfs_page *req)
430{
431 struct inode *inode = req->wb_context->dentry->d_inode;
432 struct nfs_inode *nfsi = NFS_I(inode);
433
434 BUG_ON (!NFS_WBACK_BUSY(req));
435
436 spin_lock(&nfsi->req_lock);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500437 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
439 nfsi->npages--;
440 if (!nfsi->npages) {
441 spin_unlock(&nfsi->req_lock);
Trond Myklebust951a1432005-06-22 17:16:30 +0000442 nfs_end_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 iput(inode);
444 } else
445 spin_unlock(&nfsi->req_lock);
446 nfs_clear_request(req);
447 nfs_release_request(req);
448}
449
450/*
451 * Find a request
452 */
453static inline struct nfs_page *
454_nfs_find_request(struct inode *inode, unsigned long index)
455{
456 struct nfs_inode *nfsi = NFS_I(inode);
457 struct nfs_page *req;
458
459 req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index);
460 if (req)
461 atomic_inc(&req->wb_count);
462 return req;
463}
464
465static struct nfs_page *
466nfs_find_request(struct inode *inode, unsigned long index)
467{
468 struct nfs_page *req;
469 struct nfs_inode *nfsi = NFS_I(inode);
470
471 spin_lock(&nfsi->req_lock);
472 req = _nfs_find_request(inode, index);
473 spin_unlock(&nfsi->req_lock);
474 return req;
475}
476
477/*
478 * Add a request to the inode's dirty list.
479 */
480static void
481nfs_mark_request_dirty(struct nfs_page *req)
482{
483 struct inode *inode = req->wb_context->dentry->d_inode;
484 struct nfs_inode *nfsi = NFS_I(inode);
485
486 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000487 radix_tree_tag_set(&nfsi->nfs_page_tree,
488 req->wb_index, NFS_PAGE_TAG_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 nfs_list_add_request(req, &nfsi->dirty);
490 nfsi->ndirty++;
491 spin_unlock(&nfsi->req_lock);
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700492 inc_zone_page_state(req->wb_page, NR_FILE_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 mark_inode_dirty(inode);
494}
495
496/*
497 * Check if a request is dirty
498 */
499static inline int
500nfs_dirty_request(struct nfs_page *req)
501{
502 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
503 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
504}
505
506#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
507/*
508 * Add a request to the inode's commit list.
509 */
510static void
511nfs_mark_request_commit(struct nfs_page *req)
512{
513 struct inode *inode = req->wb_context->dentry->d_inode;
514 struct nfs_inode *nfsi = NFS_I(inode);
515
516 spin_lock(&nfsi->req_lock);
517 nfs_list_add_request(req, &nfsi->commit);
518 nfsi->ncommit++;
519 spin_unlock(&nfsi->req_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700520 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 mark_inode_dirty(inode);
522}
523#endif
524
525/*
526 * Wait for a request to complete.
527 *
528 * Interruptible by signals only if mounted with intr flag.
529 */
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500530static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 struct nfs_inode *nfsi = NFS_I(inode);
533 struct nfs_page *req;
534 unsigned long idx_end, next;
535 unsigned int res = 0;
536 int error;
537
538 if (npages == 0)
539 idx_end = ~0;
540 else
541 idx_end = idx_start + npages - 1;
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 next = idx_start;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000544 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (req->wb_index > idx_end)
546 break;
547
548 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000549 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 atomic_inc(&req->wb_count);
552 spin_unlock(&nfsi->req_lock);
553 error = nfs_wait_on_request(req);
554 nfs_release_request(req);
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500555 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (error < 0)
557 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 res++;
559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return res;
561}
562
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500563static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
564{
565 struct nfs_inode *nfsi = NFS_I(inode);
566 int ret;
567
568 spin_lock(&nfsi->req_lock);
569 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
570 spin_unlock(&nfsi->req_lock);
571 return ret;
572}
573
Trond Myklebust83715ad2006-07-05 13:17:12 -0400574static void nfs_cancel_dirty_list(struct list_head *head)
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400575{
576 struct nfs_page *req;
577 while(!list_empty(head)) {
578 req = nfs_list_entry(head->next);
579 nfs_list_remove_request(req);
580 nfs_inode_remove_request(req);
581 nfs_clear_page_writeback(req);
582 }
583}
584
Trond Myklebust83715ad2006-07-05 13:17:12 -0400585static void nfs_cancel_commit_list(struct list_head *head)
586{
587 struct nfs_page *req;
588
589 while(!list_empty(head)) {
590 req = nfs_list_entry(head->next);
591 nfs_list_remove_request(req);
592 nfs_inode_remove_request(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400593 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust5c2d97c2006-09-18 23:20:35 -0400594 nfs_clear_page_writeback(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400595 }
596}
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598/*
599 * nfs_scan_dirty - Scan an inode for dirty requests
600 * @inode: NFS inode to scan
601 * @dst: destination list
602 * @idx_start: lower bound of page->index to scan.
603 * @npages: idx_start + npages sets the upper bound to scan.
604 *
605 * Moves requests from the inode's dirty page list.
606 * The requests are *not* checked to ensure that they form a contiguous set.
607 */
608static int
609nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
610{
611 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000612 int res = 0;
613
614 if (nfsi->ndirty != 0) {
615 res = nfs_scan_lock_dirty(nfsi, dst, idx_start, npages);
616 nfsi->ndirty -= res;
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000617 if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
618 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return res;
621}
622
623#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
624/*
625 * nfs_scan_commit - Scan an inode for commit requests
626 * @inode: NFS inode to scan
627 * @dst: destination list
628 * @idx_start: lower bound of page->index to scan.
629 * @npages: idx_start + npages sets the upper bound to scan.
630 *
631 * Moves requests from the inode's 'commit' request list.
632 * The requests are *not* checked to ensure that they form a contiguous set.
633 */
634static int
635nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
636{
637 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000638 int res = 0;
639
640 if (nfsi->ncommit != 0) {
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400641 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000642 nfsi->ncommit -= res;
643 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
644 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return res;
647}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500648#else
649static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
650{
651 return 0;
652}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653#endif
654
655static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
656{
657 struct backing_dev_info *bdi = mapping->backing_dev_info;
658 DEFINE_WAIT(wait);
659 int ret = 0;
660
661 might_sleep();
662
663 if (!bdi_write_congested(bdi))
664 return 0;
Chuck Lever91d5b472006-03-20 13:44:14 -0500665
666 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (intr) {
669 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
670 sigset_t oldset;
671
672 rpc_clnt_sigmask(clnt, &oldset);
673 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
674 if (bdi_write_congested(bdi)) {
675 if (signalled())
676 ret = -ERESTARTSYS;
677 else
678 schedule();
679 }
680 rpc_clnt_sigunmask(clnt, &oldset);
681 } else {
682 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
683 if (bdi_write_congested(bdi))
684 schedule();
685 }
686 finish_wait(&nfs_write_congestion, &wait);
687 return ret;
688}
689
690
691/*
692 * Try to update any existing write request, or create one if there is none.
693 * In order to match, the request's credentials must match those of
694 * the calling process.
695 *
696 * Note: Should always be called with the Page Lock held!
697 */
698static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
699 struct inode *inode, struct page *page,
700 unsigned int offset, unsigned int bytes)
701{
702 struct nfs_server *server = NFS_SERVER(inode);
703 struct nfs_inode *nfsi = NFS_I(inode);
704 struct nfs_page *req, *new = NULL;
705 unsigned long rqend, end;
706
707 end = offset + bytes;
708
709 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
710 return ERR_PTR(-ERESTARTSYS);
711 for (;;) {
712 /* Loop over all inode entries and see if we find
713 * A request for the page we wish to update
714 */
715 spin_lock(&nfsi->req_lock);
716 req = _nfs_find_request(inode, page->index);
717 if (req) {
718 if (!nfs_lock_request_dontget(req)) {
719 int error;
720 spin_unlock(&nfsi->req_lock);
721 error = nfs_wait_on_request(req);
722 nfs_release_request(req);
Neil Brown1dd594b2006-03-20 13:44:04 -0500723 if (error < 0) {
724 if (new)
725 nfs_release_request(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return ERR_PTR(error);
Neil Brown1dd594b2006-03-20 13:44:04 -0500727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 continue;
729 }
730 spin_unlock(&nfsi->req_lock);
731 if (new)
732 nfs_release_request(new);
733 break;
734 }
735
736 if (new) {
737 int error;
738 nfs_lock_request_dontget(new);
739 error = nfs_inode_add_request(inode, new);
740 if (error) {
741 spin_unlock(&nfsi->req_lock);
742 nfs_unlock_request(new);
743 return ERR_PTR(error);
744 }
745 spin_unlock(&nfsi->req_lock);
746 nfs_mark_request_dirty(new);
747 return new;
748 }
749 spin_unlock(&nfsi->req_lock);
750
751 new = nfs_create_request(ctx, inode, page, offset, bytes);
752 if (IS_ERR(new))
753 return new;
754 }
755
756 /* We have a request for our page.
757 * If the creds don't match, or the
758 * page addresses don't match,
759 * tell the caller to wait on the conflicting
760 * request.
761 */
762 rqend = req->wb_offset + req->wb_bytes;
763 if (req->wb_context != ctx
764 || req->wb_page != page
765 || !nfs_dirty_request(req)
766 || offset > rqend || end < req->wb_offset) {
767 nfs_unlock_request(req);
768 return ERR_PTR(-EBUSY);
769 }
770
771 /* Okay, the request matches. Update the region */
772 if (offset < req->wb_offset) {
773 req->wb_offset = offset;
774 req->wb_pgbase = offset;
775 req->wb_bytes = rqend - req->wb_offset;
776 }
777
778 if (end > rqend)
779 req->wb_bytes = end - req->wb_offset;
780
781 return req;
782}
783
784int nfs_flush_incompatible(struct file *file, struct page *page)
785{
786 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
787 struct inode *inode = page->mapping->host;
788 struct nfs_page *req;
789 int status = 0;
790 /*
791 * Look for a request corresponding to this page. If there
792 * is one, and it belongs to another file, we flush it out
793 * before we try to copy anything into the page. Do this
794 * due to the lack of an ACCESS-type call in NFSv2.
795 * Also do the same if we find a request from an existing
796 * dropped page.
797 */
798 req = nfs_find_request(inode, page->index);
799 if (req) {
800 if (req->wb_page != page || ctx != req->wb_context)
801 status = nfs_wb_page(inode, page);
802 nfs_release_request(req);
803 }
804 return (status < 0) ? status : 0;
805}
806
807/*
808 * Update and possibly write a cached page of an NFS file.
809 *
810 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
811 * things with a page scheduled for an RPC call (e.g. invalidate it).
812 */
813int nfs_updatepage(struct file *file, struct page *page,
814 unsigned int offset, unsigned int count)
815{
816 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 struct inode *inode = page->mapping->host;
818 struct nfs_page *req;
819 int status = 0;
820
Chuck Lever91d5b472006-03-20 13:44:14 -0500821 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500824 file->f_dentry->d_parent->d_name.name,
825 file->f_dentry->d_name.name, count,
826 (long long)(page_offset(page) +offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 if (IS_SYNC(inode)) {
829 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
830 if (status > 0) {
831 if (offset == 0 && status == PAGE_CACHE_SIZE)
832 SetPageUptodate(page);
833 return 0;
834 }
835 return status;
836 }
837
838 /* If we're not using byte range locks, and we know the page
839 * is entirely in cache, it may be more efficient to avoid
840 * fragmenting write requests.
841 */
Trond Myklebustab0a3db2005-06-22 17:16:30 +0000842 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 loff_t end_offs = i_size_read(inode) - 1;
844 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
845
846 count += offset;
847 offset = 0;
848 if (unlikely(end_offs < 0)) {
849 /* Do nothing */
850 } else if (page->index == end_index) {
851 unsigned int pglen;
852 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
853 if (count < pglen)
854 count = pglen;
855 } else if (page->index < end_index)
856 count = PAGE_CACHE_SIZE;
857 }
858
859 /*
860 * Try to find an NFS request corresponding to this page
861 * and update it.
862 * If the existing request cannot be updated, we must flush
863 * it out now.
864 */
865 do {
866 req = nfs_update_request(ctx, inode, page, offset, count);
867 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
868 if (status != -EBUSY)
869 break;
870 /* Request could not be updated. Flush it out and try again */
871 status = nfs_wb_page(inode, page);
872 } while (status >= 0);
873 if (status < 0)
874 goto done;
875
876 status = 0;
877
878 /* Update file length */
879 nfs_grow_file(page, offset, count);
880 /* Set the PG_uptodate flag? */
881 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
882 nfs_unlock_request(req);
883done:
884 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
885 status, (long long)i_size_read(inode));
886 if (status < 0)
887 ClearPageUptodate(page);
888 return status;
889}
890
891static void nfs_writepage_release(struct nfs_page *req)
892{
893 end_page_writeback(req->wb_page);
894
895#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
896 if (!PageError(req->wb_page)) {
897 if (NFS_NEED_RESCHED(req)) {
898 nfs_mark_request_dirty(req);
899 goto out;
900 } else if (NFS_NEED_COMMIT(req)) {
901 nfs_mark_request_commit(req);
902 goto out;
903 }
904 }
905 nfs_inode_remove_request(req);
906
907out:
908 nfs_clear_commit(req);
909 nfs_clear_reschedule(req);
910#else
911 nfs_inode_remove_request(req);
912#endif
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000913 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
916static inline int flush_task_priority(int how)
917{
918 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
919 case FLUSH_HIGHPRI:
920 return RPC_PRIORITY_HIGH;
921 case FLUSH_LOWPRI:
922 return RPC_PRIORITY_LOW;
923 }
924 return RPC_PRIORITY_NORMAL;
925}
926
927/*
928 * Set up the argument/result storage required for the RPC call.
929 */
930static void nfs_write_rpcsetup(struct nfs_page *req,
931 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500932 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 unsigned int count, unsigned int offset,
934 int how)
935{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -0500937 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 /* Set up the RPC argument and reply structs
940 * NB: take care not to mess about with data->commit et al. */
941
942 data->req = req;
943 data->inode = inode = req->wb_context->dentry->d_inode;
944 data->cred = req->wb_context->cred;
945
946 data->args.fh = NFS_FH(inode);
947 data->args.offset = req_offset(req) + offset;
948 data->args.pgbase = req->wb_pgbase + offset;
949 data->args.pages = data->pagevec;
950 data->args.count = count;
951 data->args.context = req->wb_context;
952
953 data->res.fattr = &data->fattr;
954 data->res.count = count;
955 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400956 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Trond Myklebust788e7a82006-03-20 13:44:27 -0500958 /* Set up the initial task struct. */
959 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
960 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 NFS_PROTO(inode)->write_setup(data, how);
962
963 data->task.tk_priority = flush_task_priority(how);
964 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500967 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 inode->i_sb->s_id,
969 (long long)NFS_FILEID(inode),
970 count,
971 (unsigned long long)data->args.offset);
972}
973
974static void nfs_execute_write(struct nfs_write_data *data)
975{
976 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
977 sigset_t oldset;
978
979 rpc_clnt_sigmask(clnt, &oldset);
980 lock_kernel();
981 rpc_execute(&data->task);
982 unlock_kernel();
983 rpc_clnt_sigunmask(clnt, &oldset);
984}
985
986/*
987 * Generate multiple small requests to write out a single
988 * contiguous dirty area on one page.
989 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500990static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
992 struct nfs_page *req = nfs_list_entry(head->next);
993 struct page *page = req->wb_page;
994 struct nfs_write_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700995 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
996 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 int requests = 0;
998 LIST_HEAD(list);
999
1000 nfs_list_remove_request(req);
1001
1002 nbytes = req->wb_bytes;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001003 do {
1004 size_t len = min(nbytes, wsize);
1005
1006 data = nfs_writedata_alloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (!data)
1008 goto out_bad;
1009 list_add(&data->pages, &list);
1010 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001011 nbytes -= len;
1012 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 atomic_set(&req->wb_complete, requests);
1014
1015 ClearPageError(page);
Trond Myklebustbb713d62005-12-03 15:20:14 -05001016 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 offset = 0;
1018 nbytes = req->wb_bytes;
1019 do {
1020 data = list_entry(list.next, struct nfs_write_data, pages);
1021 list_del_init(&data->pages);
1022
1023 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 if (nbytes > wsize) {
Trond Myklebust788e7a82006-03-20 13:44:27 -05001026 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1027 wsize, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 offset += wsize;
1029 nbytes -= wsize;
1030 } else {
Trond Myklebust788e7a82006-03-20 13:44:27 -05001031 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1032 nbytes, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 nbytes = 0;
1034 }
1035 nfs_execute_write(data);
1036 } while (nbytes != 0);
1037
1038 return 0;
1039
1040out_bad:
1041 while (!list_empty(&list)) {
1042 data = list_entry(list.next, struct nfs_write_data, pages);
1043 list_del(&data->pages);
1044 nfs_writedata_free(data);
1045 }
1046 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001047 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return -ENOMEM;
1049}
1050
1051/*
1052 * Create an RPC task for the given write request and kick it.
1053 * The page must have been locked by the caller.
1054 *
1055 * It may happen that the page we're passed is not marked dirty.
1056 * This is the case if nfs_updatepage detects a conflicting request
1057 * that has been written but not committed.
1058 */
Trond Myklebust7d46a492006-03-20 13:44:50 -05001059static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 struct nfs_page *req;
1062 struct page **pages;
1063 struct nfs_write_data *data;
1064 unsigned int count;
1065
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001066 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (!data)
1068 goto out_bad;
1069
1070 pages = data->pagevec;
1071 count = 0;
1072 while (!list_empty(head)) {
1073 req = nfs_list_entry(head->next);
1074 nfs_list_remove_request(req);
1075 nfs_list_add_request(req, &data->pages);
1076 ClearPageError(req->wb_page);
Trond Myklebustbb713d62005-12-03 15:20:14 -05001077 set_page_writeback(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 *pages++ = req->wb_page;
1079 count += req->wb_bytes;
1080 }
1081 req = nfs_list_entry(data->pages.next);
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 /* Set up the argument struct */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001084 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 nfs_execute_write(data);
1087 return 0;
1088 out_bad:
1089 while (!list_empty(head)) {
1090 struct nfs_page *req = nfs_list_entry(head->next);
1091 nfs_list_remove_request(req);
1092 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001093 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
1095 return -ENOMEM;
1096}
1097
Trond Myklebust7d46a492006-03-20 13:44:50 -05001098static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
1100 LIST_HEAD(one_request);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001101 int (*flush_one)(struct inode *, struct list_head *, int);
1102 struct nfs_page *req;
1103 int wpages = NFS_SERVER(inode)->wpages;
1104 int wsize = NFS_SERVER(inode)->wsize;
1105 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Trond Myklebust7d46a492006-03-20 13:44:50 -05001107 flush_one = nfs_flush_one;
1108 if (wsize < PAGE_CACHE_SIZE)
1109 flush_one = nfs_flush_multi;
1110 /* For single writes, FLUSH_STABLE is more efficient */
1111 if (npages <= wpages && npages == NFS_I(inode)->npages
1112 && nfs_list_entry(head->next)->wb_bytes <= wsize)
1113 how |= FLUSH_STABLE;
1114
1115 do {
1116 nfs_coalesce_requests(head, &one_request, wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 req = nfs_list_entry(one_request.next);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001118 error = flush_one(inode, &one_request, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (error < 0)
Trond Myklebust7d46a492006-03-20 13:44:50 -05001120 goto out_err;
1121 } while (!list_empty(head));
1122 return 0;
1123out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 while (!list_empty(head)) {
1125 req = nfs_list_entry(head->next);
1126 nfs_list_remove_request(req);
1127 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001128 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
1130 return error;
1131}
1132
1133/*
1134 * Handle a write reply that flushed part of a page.
1135 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001136static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001138 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 struct nfs_page *req = data->req;
1140 struct page *page = req->wb_page;
1141
1142 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1143 req->wb_context->dentry->d_inode->i_sb->s_id,
1144 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1145 req->wb_bytes,
1146 (long long)req_offset(req));
1147
Trond Myklebust788e7a82006-03-20 13:44:27 -05001148 if (nfs_writeback_done(task, data) != 0)
1149 return;
1150
1151 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 ClearPageUptodate(page);
1153 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001154 req->wb_context->error = task->tk_status;
1155 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 } else {
1157#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1158 if (data->verf.committed < NFS_FILE_SYNC) {
1159 if (!NFS_NEED_COMMIT(req)) {
1160 nfs_defer_commit(req);
1161 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1162 dprintk(" defer commit\n");
1163 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1164 nfs_defer_reschedule(req);
1165 dprintk(" server reboot detected\n");
1166 }
1167 } else
1168#endif
1169 dprintk(" OK\n");
1170 }
1171
1172 if (atomic_dec_and_test(&req->wb_complete))
1173 nfs_writepage_release(req);
1174}
1175
Trond Myklebust788e7a82006-03-20 13:44:27 -05001176static const struct rpc_call_ops nfs_write_partial_ops = {
1177 .rpc_call_done = nfs_writeback_done_partial,
1178 .rpc_release = nfs_writedata_release,
1179};
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181/*
1182 * Handle a write reply that flushes a whole page.
1183 *
1184 * FIXME: There is an inherent race with invalidate_inode_pages and
1185 * writebacks since the page->count is kept > 1 for as long
1186 * as the page has a write request pending.
1187 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001188static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001190 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 struct nfs_page *req;
1192 struct page *page;
1193
Trond Myklebust788e7a82006-03-20 13:44:27 -05001194 if (nfs_writeback_done(task, data) != 0)
1195 return;
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 /* Update attributes as result of writeback. */
1198 while (!list_empty(&data->pages)) {
1199 req = nfs_list_entry(data->pages.next);
1200 nfs_list_remove_request(req);
1201 page = req->wb_page;
1202
1203 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1204 req->wb_context->dentry->d_inode->i_sb->s_id,
1205 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1206 req->wb_bytes,
1207 (long long)req_offset(req));
1208
Trond Myklebust788e7a82006-03-20 13:44:27 -05001209 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 ClearPageUptodate(page);
1211 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001212 req->wb_context->error = task->tk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 end_page_writeback(page);
1214 nfs_inode_remove_request(req);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001215 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 goto next;
1217 }
1218 end_page_writeback(page);
1219
1220#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1221 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1222 nfs_inode_remove_request(req);
1223 dprintk(" OK\n");
1224 goto next;
1225 }
1226 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1227 nfs_mark_request_commit(req);
1228 dprintk(" marked for commit\n");
1229#else
1230 nfs_inode_remove_request(req);
1231#endif
1232 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001233 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235}
1236
Trond Myklebust788e7a82006-03-20 13:44:27 -05001237static const struct rpc_call_ops nfs_write_full_ops = {
1238 .rpc_call_done = nfs_writeback_done_full,
1239 .rpc_release = nfs_writedata_release,
1240};
1241
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243/*
1244 * This function is called when the WRITE call is complete.
1245 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001246int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 struct nfs_writeargs *argp = &data->args;
1249 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001250 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1253 task->tk_pid, task->tk_status);
1254
Trond Myklebust788e7a82006-03-20 13:44:27 -05001255 /* Call the NFS version-specific code */
1256 status = NFS_PROTO(data->inode)->write_done(task, data);
1257 if (status != 0)
1258 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001259 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1262 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1263 /* We tried a write call, but the server did not
1264 * commit data to stable storage even though we
1265 * requested it.
1266 * Note: There is a known bug in Tru64 < 5.0 in which
1267 * the server reports NFS_DATA_SYNC, but performs
1268 * NFS_FILE_SYNC. We therefore implement this checking
1269 * as a dprintk() in order to avoid filling syslog.
1270 */
1271 static unsigned long complain;
1272
1273 if (time_before(complain, jiffies)) {
1274 dprintk("NFS: faulty NFS server %s:"
1275 " (committed = %d) != (stable = %d)\n",
David Howells54ceac42006-08-22 20:06:13 -04001276 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 resp->verf->committed, argp->stable);
1278 complain = jiffies + 300 * HZ;
1279 }
1280 }
1281#endif
1282 /* Is this a short write? */
1283 if (task->tk_status >= 0 && resp->count < argp->count) {
1284 static unsigned long complain;
1285
Chuck Lever91d5b472006-03-20 13:44:14 -05001286 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 /* Has the server at least made some progress? */
1289 if (resp->count != 0) {
1290 /* Was this an NFSv2 write or an NFSv3 stable write? */
1291 if (resp->verf->committed != NFS_UNSTABLE) {
1292 /* Resend from where the server left off */
1293 argp->offset += resp->count;
1294 argp->pgbase += resp->count;
1295 argp->count -= resp->count;
1296 } else {
1297 /* Resend as a stable write in order to avoid
1298 * headaches in the case of a server crash.
1299 */
1300 argp->stable = NFS_FILE_SYNC;
1301 }
1302 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001303 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
1305 if (time_before(complain, jiffies)) {
1306 printk(KERN_WARNING
1307 "NFS: Server wrote zero bytes, expected %u.\n",
1308 argp->count);
1309 complain = jiffies + 300 * HZ;
1310 }
1311 /* Can't do anything about it except throw an error. */
1312 task->tk_status = -EIO;
1313 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
1317
1318#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001319void nfs_commit_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 nfs_commit_free(wdata);
1322}
1323
1324/*
1325 * Set up the argument/result storage required for the RPC call.
1326 */
1327static void nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001328 struct nfs_write_data *data,
1329 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001331 struct nfs_page *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001333 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 /* Set up the RPC argument and reply structs
1336 * NB: take care not to mess about with data->commit et al. */
1337
1338 list_splice_init(head, &data->pages);
1339 first = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 inode = first->wb_context->dentry->d_inode;
1341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 data->inode = inode;
1343 data->cred = first->wb_context->cred;
1344
1345 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001346 /* Note: we always request a commit of the entire inode */
1347 data->args.offset = 0;
1348 data->args.count = 0;
1349 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 data->res.fattr = &data->fattr;
1351 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001352 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001353
1354 /* Set up the initial task struct. */
1355 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1356 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 NFS_PROTO(inode)->commit_setup(data, how);
1358
1359 data->task.tk_priority = flush_task_priority(how);
1360 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Chuck Lever0bbacc42005-11-01 16:53:32 -05001362 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363}
1364
1365/*
1366 * Commit dirty pages
1367 */
1368static int
Chuck Lever40859d72005-11-30 18:09:02 -05001369nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
1371 struct nfs_write_data *data;
1372 struct nfs_page *req;
1373
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001374 data = nfs_commit_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 if (!data)
1377 goto out_bad;
1378
1379 /* Set up the argument struct */
1380 nfs_commit_rpcsetup(head, data, how);
1381
1382 nfs_execute_write(data);
1383 return 0;
1384 out_bad:
1385 while (!list_empty(head)) {
1386 req = nfs_list_entry(head->next);
1387 nfs_list_remove_request(req);
1388 nfs_mark_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001389 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust5c2d97c2006-09-18 23:20:35 -04001390 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392 return -ENOMEM;
1393}
1394
1395/*
1396 * COMMIT call returned
1397 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001398static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001400 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1404 task->tk_pid, task->tk_status);
1405
Trond Myklebust788e7a82006-03-20 13:44:27 -05001406 /* Call the NFS version-specific code */
1407 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1408 return;
1409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 while (!list_empty(&data->pages)) {
1411 req = nfs_list_entry(data->pages.next);
1412 nfs_list_remove_request(req);
Christoph Lameterfd39fc82006-06-30 01:55:40 -07001413 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1416 req->wb_context->dentry->d_inode->i_sb->s_id,
1417 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1418 req->wb_bytes,
1419 (long long)req_offset(req));
1420 if (task->tk_status < 0) {
1421 req->wb_context->error = task->tk_status;
1422 nfs_inode_remove_request(req);
1423 dprintk(", error = %d\n", task->tk_status);
1424 goto next;
1425 }
1426
1427 /* Okay, COMMIT succeeded, apparently. Check the verifier
1428 * returned by the server against all stored verfs. */
1429 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1430 /* We have a match */
1431 nfs_inode_remove_request(req);
1432 dprintk(" OK\n");
1433 goto next;
1434 }
1435 /* We have a mismatch. Write the page again */
1436 dprintk(" mismatch\n");
1437 nfs_mark_request_dirty(req);
1438 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001439 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001442
1443static const struct rpc_call_ops nfs_commit_ops = {
1444 .rpc_call_done = nfs_commit_done,
1445 .rpc_release = nfs_commit_release,
1446};
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001447#else
1448static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1449{
1450 return 0;
1451}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452#endif
1453
1454static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
1455 unsigned int npages, int how)
1456{
1457 struct nfs_inode *nfsi = NFS_I(inode);
1458 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001459 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 spin_lock(&nfsi->req_lock);
1462 res = nfs_scan_dirty(inode, &head, idx_start, npages);
1463 spin_unlock(&nfsi->req_lock);
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001464 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001465 int error = nfs_flush_list(inode, &head, res, how);
1466 if (error < 0)
1467 return error;
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return res;
1470}
1471
1472#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001473int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
1475 struct nfs_inode *nfsi = NFS_I(inode);
1476 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001477 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001480 res = nfs_scan_commit(inode, &head, 0, 0);
1481 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001483 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001484 if (error < 0)
1485 return error;
1486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return res;
1488}
1489#endif
1490
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001491int nfs_sync_inode_wait(struct inode *inode, unsigned long idx_start,
1492 unsigned int npages, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001494 struct nfs_inode *nfsi = NFS_I(inode);
1495 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001496 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001497 int pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001499 how &= ~FLUSH_NOCOMMIT;
1500 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 do {
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001502 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1503 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001504 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001505 pages = nfs_scan_dirty(inode, &head, idx_start, npages);
1506 if (pages != 0) {
1507 spin_unlock(&nfsi->req_lock);
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001508 if (how & FLUSH_INVALIDATE)
Trond Myklebust83715ad2006-07-05 13:17:12 -04001509 nfs_cancel_dirty_list(&head);
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001510 else
1511 ret = nfs_flush_list(inode, &head, pages, how);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001512 spin_lock(&nfsi->req_lock);
1513 continue;
1514 }
1515 if (nocommit)
1516 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001517 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001518 if (pages == 0)
1519 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001520 if (how & FLUSH_INVALIDATE) {
1521 spin_unlock(&nfsi->req_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001522 nfs_cancel_commit_list(&head);
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001523 spin_lock(&nfsi->req_lock);
1524 continue;
1525 }
1526 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001527 spin_unlock(&nfsi->req_lock);
1528 ret = nfs_commit_list(inode, &head, how);
1529 spin_lock(&nfsi->req_lock);
1530 } while (ret >= 0);
1531 spin_unlock(&nfsi->req_lock);
1532 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
David Howellsf7b422b2006-06-09 09:34:33 -04001535int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
1537 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1538 sizeof(struct nfs_write_data),
1539 0, SLAB_HWCACHE_ALIGN,
1540 NULL, NULL);
1541 if (nfs_wdata_cachep == NULL)
1542 return -ENOMEM;
1543
Matthew Dobson93d23412006-03-26 01:37:50 -08001544 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1545 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if (nfs_wdata_mempool == NULL)
1547 return -ENOMEM;
1548
Matthew Dobson93d23412006-03-26 01:37:50 -08001549 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1550 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (nfs_commit_mempool == NULL)
1552 return -ENOMEM;
1553
1554 return 0;
1555}
1556
David Brownell266bee82006-06-27 12:59:15 -07001557void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
1559 mempool_destroy(nfs_commit_mempool);
1560 mempool_destroy(nfs_wdata_mempool);
1561 if (kmem_cache_destroy(nfs_wdata_cachep))
1562 printk(KERN_INFO "nfs_write_data: not all structures were freed\n");
1563}
1564