blob: c12effb46fe50df5ff959455de5fcb1f2c75533e [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);
Trond Myklebust275a0822006-08-22 20:06:24 -0400399 writeback_congestion_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return err;
401}
402
403/*
404 * Insert a write request into an inode
405 */
406static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
407{
408 struct nfs_inode *nfsi = NFS_I(inode);
409 int error;
410
411 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
412 BUG_ON(error == -EEXIST);
413 if (error)
414 return error;
415 if (!nfsi->npages) {
416 igrab(inode);
417 nfs_begin_data_update(inode);
418 if (nfs_have_delegation(inode, FMODE_WRITE))
419 nfsi->change_attr++;
420 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500421 SetPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 nfsi->npages++;
423 atomic_inc(&req->wb_count);
424 return 0;
425}
426
427/*
428 * Insert a write request into an inode
429 */
430static void nfs_inode_remove_request(struct nfs_page *req)
431{
432 struct inode *inode = req->wb_context->dentry->d_inode;
433 struct nfs_inode *nfsi = NFS_I(inode);
434
435 BUG_ON (!NFS_WBACK_BUSY(req));
436
437 spin_lock(&nfsi->req_lock);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500438 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
440 nfsi->npages--;
441 if (!nfsi->npages) {
442 spin_unlock(&nfsi->req_lock);
Trond Myklebust951a1432005-06-22 17:16:30 +0000443 nfs_end_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 iput(inode);
445 } else
446 spin_unlock(&nfsi->req_lock);
447 nfs_clear_request(req);
448 nfs_release_request(req);
449}
450
451/*
452 * Find a request
453 */
454static inline struct nfs_page *
455_nfs_find_request(struct inode *inode, unsigned long index)
456{
457 struct nfs_inode *nfsi = NFS_I(inode);
458 struct nfs_page *req;
459
460 req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index);
461 if (req)
462 atomic_inc(&req->wb_count);
463 return req;
464}
465
466static struct nfs_page *
467nfs_find_request(struct inode *inode, unsigned long index)
468{
469 struct nfs_page *req;
470 struct nfs_inode *nfsi = NFS_I(inode);
471
472 spin_lock(&nfsi->req_lock);
473 req = _nfs_find_request(inode, index);
474 spin_unlock(&nfsi->req_lock);
475 return req;
476}
477
478/*
479 * Add a request to the inode's dirty list.
480 */
481static void
482nfs_mark_request_dirty(struct nfs_page *req)
483{
484 struct inode *inode = req->wb_context->dentry->d_inode;
485 struct nfs_inode *nfsi = NFS_I(inode);
486
487 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000488 radix_tree_tag_set(&nfsi->nfs_page_tree,
489 req->wb_index, NFS_PAGE_TAG_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 nfs_list_add_request(req, &nfsi->dirty);
491 nfsi->ndirty++;
492 spin_unlock(&nfsi->req_lock);
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700493 inc_zone_page_state(req->wb_page, NR_FILE_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 mark_inode_dirty(inode);
495}
496
497/*
498 * Check if a request is dirty
499 */
500static inline int
501nfs_dirty_request(struct nfs_page *req)
502{
503 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
504 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
505}
506
507#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
508/*
509 * Add a request to the inode's commit list.
510 */
511static void
512nfs_mark_request_commit(struct nfs_page *req)
513{
514 struct inode *inode = req->wb_context->dentry->d_inode;
515 struct nfs_inode *nfsi = NFS_I(inode);
516
517 spin_lock(&nfsi->req_lock);
518 nfs_list_add_request(req, &nfsi->commit);
519 nfsi->ncommit++;
520 spin_unlock(&nfsi->req_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700521 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 mark_inode_dirty(inode);
523}
524#endif
525
526/*
527 * Wait for a request to complete.
528 *
529 * Interruptible by signals only if mounted with intr flag.
530 */
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500531static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 struct nfs_inode *nfsi = NFS_I(inode);
534 struct nfs_page *req;
535 unsigned long idx_end, next;
536 unsigned int res = 0;
537 int error;
538
539 if (npages == 0)
540 idx_end = ~0;
541 else
542 idx_end = idx_start + npages - 1;
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 next = idx_start;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000545 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 -0700546 if (req->wb_index > idx_end)
547 break;
548
549 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000550 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 atomic_inc(&req->wb_count);
553 spin_unlock(&nfsi->req_lock);
554 error = nfs_wait_on_request(req);
555 nfs_release_request(req);
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500556 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (error < 0)
558 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 res++;
560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return res;
562}
563
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500564static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
565{
566 struct nfs_inode *nfsi = NFS_I(inode);
567 int ret;
568
569 spin_lock(&nfsi->req_lock);
570 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
571 spin_unlock(&nfsi->req_lock);
572 return ret;
573}
574
Trond Myklebust83715ad2006-07-05 13:17:12 -0400575static void nfs_cancel_dirty_list(struct list_head *head)
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400576{
577 struct nfs_page *req;
578 while(!list_empty(head)) {
579 req = nfs_list_entry(head->next);
580 nfs_list_remove_request(req);
581 nfs_inode_remove_request(req);
582 nfs_clear_page_writeback(req);
583 }
584}
585
Trond Myklebust83715ad2006-07-05 13:17:12 -0400586static void nfs_cancel_commit_list(struct list_head *head)
587{
588 struct nfs_page *req;
589
590 while(!list_empty(head)) {
591 req = nfs_list_entry(head->next);
592 nfs_list_remove_request(req);
593 nfs_inode_remove_request(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400594 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust5c2d97c2006-09-18 23:20:35 -0400595 nfs_clear_page_writeback(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400596 }
597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599/*
600 * nfs_scan_dirty - Scan an inode for dirty requests
601 * @inode: NFS inode to scan
602 * @dst: destination list
603 * @idx_start: lower bound of page->index to scan.
604 * @npages: idx_start + npages sets the upper bound to scan.
605 *
606 * Moves requests from the inode's dirty page list.
607 * The requests are *not* checked to ensure that they form a contiguous set.
608 */
609static int
610nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
611{
612 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000613 int res = 0;
614
615 if (nfsi->ndirty != 0) {
616 res = nfs_scan_lock_dirty(nfsi, dst, idx_start, npages);
617 nfsi->ndirty -= res;
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000618 if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
619 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return res;
622}
623
624#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
625/*
626 * nfs_scan_commit - Scan an inode for commit requests
627 * @inode: NFS inode to scan
628 * @dst: destination list
629 * @idx_start: lower bound of page->index to scan.
630 * @npages: idx_start + npages sets the upper bound to scan.
631 *
632 * Moves requests from the inode's 'commit' request list.
633 * The requests are *not* checked to ensure that they form a contiguous set.
634 */
635static int
636nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
637{
638 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000639 int res = 0;
640
641 if (nfsi->ncommit != 0) {
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400642 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000643 nfsi->ncommit -= res;
644 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
645 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return res;
648}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500649#else
650static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
651{
652 return 0;
653}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654#endif
655
656static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
657{
658 struct backing_dev_info *bdi = mapping->backing_dev_info;
659 DEFINE_WAIT(wait);
660 int ret = 0;
661
662 might_sleep();
663
664 if (!bdi_write_congested(bdi))
665 return 0;
Chuck Lever91d5b472006-03-20 13:44:14 -0500666
667 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (intr) {
670 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
671 sigset_t oldset;
672
673 rpc_clnt_sigmask(clnt, &oldset);
674 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
675 if (bdi_write_congested(bdi)) {
676 if (signalled())
677 ret = -ERESTARTSYS;
678 else
679 schedule();
680 }
681 rpc_clnt_sigunmask(clnt, &oldset);
682 } else {
683 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
684 if (bdi_write_congested(bdi))
685 schedule();
686 }
687 finish_wait(&nfs_write_congestion, &wait);
688 return ret;
689}
690
691
692/*
693 * Try to update any existing write request, or create one if there is none.
694 * In order to match, the request's credentials must match those of
695 * the calling process.
696 *
697 * Note: Should always be called with the Page Lock held!
698 */
699static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
700 struct inode *inode, struct page *page,
701 unsigned int offset, unsigned int bytes)
702{
703 struct nfs_server *server = NFS_SERVER(inode);
704 struct nfs_inode *nfsi = NFS_I(inode);
705 struct nfs_page *req, *new = NULL;
706 unsigned long rqend, end;
707
708 end = offset + bytes;
709
710 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
711 return ERR_PTR(-ERESTARTSYS);
712 for (;;) {
713 /* Loop over all inode entries and see if we find
714 * A request for the page we wish to update
715 */
716 spin_lock(&nfsi->req_lock);
717 req = _nfs_find_request(inode, page->index);
718 if (req) {
719 if (!nfs_lock_request_dontget(req)) {
720 int error;
721 spin_unlock(&nfsi->req_lock);
722 error = nfs_wait_on_request(req);
723 nfs_release_request(req);
Neil Brown1dd594b2006-03-20 13:44:04 -0500724 if (error < 0) {
725 if (new)
726 nfs_release_request(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return ERR_PTR(error);
Neil Brown1dd594b2006-03-20 13:44:04 -0500728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 continue;
730 }
731 spin_unlock(&nfsi->req_lock);
732 if (new)
733 nfs_release_request(new);
734 break;
735 }
736
737 if (new) {
738 int error;
739 nfs_lock_request_dontget(new);
740 error = nfs_inode_add_request(inode, new);
741 if (error) {
742 spin_unlock(&nfsi->req_lock);
743 nfs_unlock_request(new);
744 return ERR_PTR(error);
745 }
746 spin_unlock(&nfsi->req_lock);
747 nfs_mark_request_dirty(new);
748 return new;
749 }
750 spin_unlock(&nfsi->req_lock);
751
752 new = nfs_create_request(ctx, inode, page, offset, bytes);
753 if (IS_ERR(new))
754 return new;
755 }
756
757 /* We have a request for our page.
758 * If the creds don't match, or the
759 * page addresses don't match,
760 * tell the caller to wait on the conflicting
761 * request.
762 */
763 rqend = req->wb_offset + req->wb_bytes;
764 if (req->wb_context != ctx
765 || req->wb_page != page
766 || !nfs_dirty_request(req)
767 || offset > rqend || end < req->wb_offset) {
768 nfs_unlock_request(req);
769 return ERR_PTR(-EBUSY);
770 }
771
772 /* Okay, the request matches. Update the region */
773 if (offset < req->wb_offset) {
774 req->wb_offset = offset;
775 req->wb_pgbase = offset;
776 req->wb_bytes = rqend - req->wb_offset;
777 }
778
779 if (end > rqend)
780 req->wb_bytes = end - req->wb_offset;
781
782 return req;
783}
784
785int nfs_flush_incompatible(struct file *file, struct page *page)
786{
787 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
788 struct inode *inode = page->mapping->host;
789 struct nfs_page *req;
790 int status = 0;
791 /*
792 * Look for a request corresponding to this page. If there
793 * is one, and it belongs to another file, we flush it out
794 * before we try to copy anything into the page. Do this
795 * due to the lack of an ACCESS-type call in NFSv2.
796 * Also do the same if we find a request from an existing
797 * dropped page.
798 */
799 req = nfs_find_request(inode, page->index);
800 if (req) {
801 if (req->wb_page != page || ctx != req->wb_context)
802 status = nfs_wb_page(inode, page);
803 nfs_release_request(req);
804 }
805 return (status < 0) ? status : 0;
806}
807
808/*
809 * Update and possibly write a cached page of an NFS file.
810 *
811 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
812 * things with a page scheduled for an RPC call (e.g. invalidate it).
813 */
814int nfs_updatepage(struct file *file, struct page *page,
815 unsigned int offset, unsigned int count)
816{
817 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 struct inode *inode = page->mapping->host;
819 struct nfs_page *req;
820 int status = 0;
821
Chuck Lever91d5b472006-03-20 13:44:14 -0500822 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500825 file->f_dentry->d_parent->d_name.name,
826 file->f_dentry->d_name.name, count,
827 (long long)(page_offset(page) +offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 if (IS_SYNC(inode)) {
830 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
831 if (status > 0) {
832 if (offset == 0 && status == PAGE_CACHE_SIZE)
833 SetPageUptodate(page);
834 return 0;
835 }
836 return status;
837 }
838
839 /* If we're not using byte range locks, and we know the page
840 * is entirely in cache, it may be more efficient to avoid
841 * fragmenting write requests.
842 */
Trond Myklebustab0a3db2005-06-22 17:16:30 +0000843 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 loff_t end_offs = i_size_read(inode) - 1;
845 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
846
847 count += offset;
848 offset = 0;
849 if (unlikely(end_offs < 0)) {
850 /* Do nothing */
851 } else if (page->index == end_index) {
852 unsigned int pglen;
853 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
854 if (count < pglen)
855 count = pglen;
856 } else if (page->index < end_index)
857 count = PAGE_CACHE_SIZE;
858 }
859
860 /*
861 * Try to find an NFS request corresponding to this page
862 * and update it.
863 * If the existing request cannot be updated, we must flush
864 * it out now.
865 */
866 do {
867 req = nfs_update_request(ctx, inode, page, offset, count);
868 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
869 if (status != -EBUSY)
870 break;
871 /* Request could not be updated. Flush it out and try again */
872 status = nfs_wb_page(inode, page);
873 } while (status >= 0);
874 if (status < 0)
875 goto done;
876
877 status = 0;
878
879 /* Update file length */
880 nfs_grow_file(page, offset, count);
881 /* Set the PG_uptodate flag? */
882 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
883 nfs_unlock_request(req);
884done:
885 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
886 status, (long long)i_size_read(inode));
887 if (status < 0)
888 ClearPageUptodate(page);
889 return status;
890}
891
892static void nfs_writepage_release(struct nfs_page *req)
893{
894 end_page_writeback(req->wb_page);
895
896#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
897 if (!PageError(req->wb_page)) {
898 if (NFS_NEED_RESCHED(req)) {
899 nfs_mark_request_dirty(req);
900 goto out;
901 } else if (NFS_NEED_COMMIT(req)) {
902 nfs_mark_request_commit(req);
903 goto out;
904 }
905 }
906 nfs_inode_remove_request(req);
907
908out:
909 nfs_clear_commit(req);
910 nfs_clear_reschedule(req);
911#else
912 nfs_inode_remove_request(req);
913#endif
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000914 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
917static inline int flush_task_priority(int how)
918{
919 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
920 case FLUSH_HIGHPRI:
921 return RPC_PRIORITY_HIGH;
922 case FLUSH_LOWPRI:
923 return RPC_PRIORITY_LOW;
924 }
925 return RPC_PRIORITY_NORMAL;
926}
927
928/*
929 * Set up the argument/result storage required for the RPC call.
930 */
931static void nfs_write_rpcsetup(struct nfs_page *req,
932 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500933 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 unsigned int count, unsigned int offset,
935 int how)
936{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -0500938 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 /* Set up the RPC argument and reply structs
941 * NB: take care not to mess about with data->commit et al. */
942
943 data->req = req;
944 data->inode = inode = req->wb_context->dentry->d_inode;
945 data->cred = req->wb_context->cred;
946
947 data->args.fh = NFS_FH(inode);
948 data->args.offset = req_offset(req) + offset;
949 data->args.pgbase = req->wb_pgbase + offset;
950 data->args.pages = data->pagevec;
951 data->args.count = count;
952 data->args.context = req->wb_context;
953
954 data->res.fattr = &data->fattr;
955 data->res.count = count;
956 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400957 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Trond Myklebust788e7a82006-03-20 13:44:27 -0500959 /* Set up the initial task struct. */
960 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
961 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 NFS_PROTO(inode)->write_setup(data, how);
963
964 data->task.tk_priority = flush_task_priority(how);
965 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500968 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 inode->i_sb->s_id,
970 (long long)NFS_FILEID(inode),
971 count,
972 (unsigned long long)data->args.offset);
973}
974
975static void nfs_execute_write(struct nfs_write_data *data)
976{
977 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
978 sigset_t oldset;
979
980 rpc_clnt_sigmask(clnt, &oldset);
981 lock_kernel();
982 rpc_execute(&data->task);
983 unlock_kernel();
984 rpc_clnt_sigunmask(clnt, &oldset);
985}
986
987/*
988 * Generate multiple small requests to write out a single
989 * contiguous dirty area on one page.
990 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500991static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
993 struct nfs_page *req = nfs_list_entry(head->next);
994 struct page *page = req->wb_page;
995 struct nfs_write_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700996 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
997 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 int requests = 0;
999 LIST_HEAD(list);
1000
1001 nfs_list_remove_request(req);
1002
1003 nbytes = req->wb_bytes;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001004 do {
1005 size_t len = min(nbytes, wsize);
1006
1007 data = nfs_writedata_alloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (!data)
1009 goto out_bad;
1010 list_add(&data->pages, &list);
1011 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001012 nbytes -= len;
1013 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 atomic_set(&req->wb_complete, requests);
1015
1016 ClearPageError(page);
Trond Myklebustbb713d62005-12-03 15:20:14 -05001017 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 offset = 0;
1019 nbytes = req->wb_bytes;
1020 do {
1021 data = list_entry(list.next, struct nfs_write_data, pages);
1022 list_del_init(&data->pages);
1023
1024 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 if (nbytes > wsize) {
Trond Myklebust788e7a82006-03-20 13:44:27 -05001027 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1028 wsize, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 offset += wsize;
1030 nbytes -= wsize;
1031 } else {
Trond Myklebust788e7a82006-03-20 13:44:27 -05001032 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1033 nbytes, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 nbytes = 0;
1035 }
1036 nfs_execute_write(data);
1037 } while (nbytes != 0);
1038
1039 return 0;
1040
1041out_bad:
1042 while (!list_empty(&list)) {
1043 data = list_entry(list.next, struct nfs_write_data, pages);
1044 list_del(&data->pages);
1045 nfs_writedata_free(data);
1046 }
1047 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001048 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return -ENOMEM;
1050}
1051
1052/*
1053 * Create an RPC task for the given write request and kick it.
1054 * The page must have been locked by the caller.
1055 *
1056 * It may happen that the page we're passed is not marked dirty.
1057 * This is the case if nfs_updatepage detects a conflicting request
1058 * that has been written but not committed.
1059 */
Trond Myklebust7d46a492006-03-20 13:44:50 -05001060static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
1062 struct nfs_page *req;
1063 struct page **pages;
1064 struct nfs_write_data *data;
1065 unsigned int count;
1066
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001067 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (!data)
1069 goto out_bad;
1070
1071 pages = data->pagevec;
1072 count = 0;
1073 while (!list_empty(head)) {
1074 req = nfs_list_entry(head->next);
1075 nfs_list_remove_request(req);
1076 nfs_list_add_request(req, &data->pages);
1077 ClearPageError(req->wb_page);
Trond Myklebustbb713d62005-12-03 15:20:14 -05001078 set_page_writeback(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 *pages++ = req->wb_page;
1080 count += req->wb_bytes;
1081 }
1082 req = nfs_list_entry(data->pages.next);
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 /* Set up the argument struct */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001085 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 nfs_execute_write(data);
1088 return 0;
1089 out_bad:
1090 while (!list_empty(head)) {
1091 struct nfs_page *req = nfs_list_entry(head->next);
1092 nfs_list_remove_request(req);
1093 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001094 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
1096 return -ENOMEM;
1097}
1098
Trond Myklebust7d46a492006-03-20 13:44:50 -05001099static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
1101 LIST_HEAD(one_request);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001102 int (*flush_one)(struct inode *, struct list_head *, int);
1103 struct nfs_page *req;
1104 int wpages = NFS_SERVER(inode)->wpages;
1105 int wsize = NFS_SERVER(inode)->wsize;
1106 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Trond Myklebust7d46a492006-03-20 13:44:50 -05001108 flush_one = nfs_flush_one;
1109 if (wsize < PAGE_CACHE_SIZE)
1110 flush_one = nfs_flush_multi;
1111 /* For single writes, FLUSH_STABLE is more efficient */
1112 if (npages <= wpages && npages == NFS_I(inode)->npages
1113 && nfs_list_entry(head->next)->wb_bytes <= wsize)
1114 how |= FLUSH_STABLE;
1115
1116 do {
1117 nfs_coalesce_requests(head, &one_request, wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 req = nfs_list_entry(one_request.next);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001119 error = flush_one(inode, &one_request, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (error < 0)
Trond Myklebust7d46a492006-03-20 13:44:50 -05001121 goto out_err;
1122 } while (!list_empty(head));
1123 return 0;
1124out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 while (!list_empty(head)) {
1126 req = nfs_list_entry(head->next);
1127 nfs_list_remove_request(req);
1128 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001129 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131 return error;
1132}
1133
1134/*
1135 * Handle a write reply that flushed part of a page.
1136 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001137static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001139 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 struct nfs_page *req = data->req;
1141 struct page *page = req->wb_page;
1142
1143 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1144 req->wb_context->dentry->d_inode->i_sb->s_id,
1145 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1146 req->wb_bytes,
1147 (long long)req_offset(req));
1148
Trond Myklebust788e7a82006-03-20 13:44:27 -05001149 if (nfs_writeback_done(task, data) != 0)
1150 return;
1151
1152 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 ClearPageUptodate(page);
1154 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001155 req->wb_context->error = task->tk_status;
1156 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 } else {
1158#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1159 if (data->verf.committed < NFS_FILE_SYNC) {
1160 if (!NFS_NEED_COMMIT(req)) {
1161 nfs_defer_commit(req);
1162 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1163 dprintk(" defer commit\n");
1164 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1165 nfs_defer_reschedule(req);
1166 dprintk(" server reboot detected\n");
1167 }
1168 } else
1169#endif
1170 dprintk(" OK\n");
1171 }
1172
1173 if (atomic_dec_and_test(&req->wb_complete))
1174 nfs_writepage_release(req);
1175}
1176
Trond Myklebust788e7a82006-03-20 13:44:27 -05001177static const struct rpc_call_ops nfs_write_partial_ops = {
1178 .rpc_call_done = nfs_writeback_done_partial,
1179 .rpc_release = nfs_writedata_release,
1180};
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182/*
1183 * Handle a write reply that flushes a whole page.
1184 *
1185 * FIXME: There is an inherent race with invalidate_inode_pages and
1186 * writebacks since the page->count is kept > 1 for as long
1187 * as the page has a write request pending.
1188 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001189static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001191 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 struct nfs_page *req;
1193 struct page *page;
1194
Trond Myklebust788e7a82006-03-20 13:44:27 -05001195 if (nfs_writeback_done(task, data) != 0)
1196 return;
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 /* Update attributes as result of writeback. */
1199 while (!list_empty(&data->pages)) {
1200 req = nfs_list_entry(data->pages.next);
1201 nfs_list_remove_request(req);
1202 page = req->wb_page;
1203
1204 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1205 req->wb_context->dentry->d_inode->i_sb->s_id,
1206 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1207 req->wb_bytes,
1208 (long long)req_offset(req));
1209
Trond Myklebust788e7a82006-03-20 13:44:27 -05001210 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 ClearPageUptodate(page);
1212 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001213 req->wb_context->error = task->tk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 end_page_writeback(page);
1215 nfs_inode_remove_request(req);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001216 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 goto next;
1218 }
1219 end_page_writeback(page);
1220
1221#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1222 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1223 nfs_inode_remove_request(req);
1224 dprintk(" OK\n");
1225 goto next;
1226 }
1227 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1228 nfs_mark_request_commit(req);
1229 dprintk(" marked for commit\n");
1230#else
1231 nfs_inode_remove_request(req);
1232#endif
1233 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001234 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
1236}
1237
Trond Myklebust788e7a82006-03-20 13:44:27 -05001238static const struct rpc_call_ops nfs_write_full_ops = {
1239 .rpc_call_done = nfs_writeback_done_full,
1240 .rpc_release = nfs_writedata_release,
1241};
1242
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244/*
1245 * This function is called when the WRITE call is complete.
1246 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001247int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 struct nfs_writeargs *argp = &data->args;
1250 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001251 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1254 task->tk_pid, task->tk_status);
1255
Chuck Leverf551e442006-09-20 14:33:04 -04001256 /*
1257 * ->write_done will attempt to use post-op attributes to detect
1258 * conflicting writes by other clients. A strict interpretation
1259 * of close-to-open would allow us to continue caching even if
1260 * another writer had changed the file, but some applications
1261 * depend on tighter cache coherency when writing.
1262 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001263 status = NFS_PROTO(data->inode)->write_done(task, data);
1264 if (status != 0)
1265 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001266 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1269 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1270 /* We tried a write call, but the server did not
1271 * commit data to stable storage even though we
1272 * requested it.
1273 * Note: There is a known bug in Tru64 < 5.0 in which
1274 * the server reports NFS_DATA_SYNC, but performs
1275 * NFS_FILE_SYNC. We therefore implement this checking
1276 * as a dprintk() in order to avoid filling syslog.
1277 */
1278 static unsigned long complain;
1279
1280 if (time_before(complain, jiffies)) {
1281 dprintk("NFS: faulty NFS server %s:"
1282 " (committed = %d) != (stable = %d)\n",
David Howells54ceac42006-08-22 20:06:13 -04001283 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 resp->verf->committed, argp->stable);
1285 complain = jiffies + 300 * HZ;
1286 }
1287 }
1288#endif
1289 /* Is this a short write? */
1290 if (task->tk_status >= 0 && resp->count < argp->count) {
1291 static unsigned long complain;
1292
Chuck Lever91d5b472006-03-20 13:44:14 -05001293 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 /* Has the server at least made some progress? */
1296 if (resp->count != 0) {
1297 /* Was this an NFSv2 write or an NFSv3 stable write? */
1298 if (resp->verf->committed != NFS_UNSTABLE) {
1299 /* Resend from where the server left off */
1300 argp->offset += resp->count;
1301 argp->pgbase += resp->count;
1302 argp->count -= resp->count;
1303 } else {
1304 /* Resend as a stable write in order to avoid
1305 * headaches in the case of a server crash.
1306 */
1307 argp->stable = NFS_FILE_SYNC;
1308 }
1309 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001310 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
1312 if (time_before(complain, jiffies)) {
1313 printk(KERN_WARNING
1314 "NFS: Server wrote zero bytes, expected %u.\n",
1315 argp->count);
1316 complain = jiffies + 300 * HZ;
1317 }
1318 /* Can't do anything about it except throw an error. */
1319 task->tk_status = -EIO;
1320 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001321 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322}
1323
1324
1325#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001326void nfs_commit_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 nfs_commit_free(wdata);
1329}
1330
1331/*
1332 * Set up the argument/result storage required for the RPC call.
1333 */
1334static void nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001335 struct nfs_write_data *data,
1336 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001338 struct nfs_page *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001340 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 /* Set up the RPC argument and reply structs
1343 * NB: take care not to mess about with data->commit et al. */
1344
1345 list_splice_init(head, &data->pages);
1346 first = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 inode = first->wb_context->dentry->d_inode;
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 data->inode = inode;
1350 data->cred = first->wb_context->cred;
1351
1352 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001353 /* Note: we always request a commit of the entire inode */
1354 data->args.offset = 0;
1355 data->args.count = 0;
1356 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 data->res.fattr = &data->fattr;
1358 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001359 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001360
1361 /* Set up the initial task struct. */
1362 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1363 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 NFS_PROTO(inode)->commit_setup(data, how);
1365
1366 data->task.tk_priority = flush_task_priority(how);
1367 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Chuck Lever0bbacc42005-11-01 16:53:32 -05001369 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370}
1371
1372/*
1373 * Commit dirty pages
1374 */
1375static int
Chuck Lever40859d72005-11-30 18:09:02 -05001376nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 struct nfs_write_data *data;
1379 struct nfs_page *req;
1380
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001381 data = nfs_commit_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 if (!data)
1384 goto out_bad;
1385
1386 /* Set up the argument struct */
1387 nfs_commit_rpcsetup(head, data, how);
1388
1389 nfs_execute_write(data);
1390 return 0;
1391 out_bad:
1392 while (!list_empty(head)) {
1393 req = nfs_list_entry(head->next);
1394 nfs_list_remove_request(req);
1395 nfs_mark_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001396 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust5c2d97c2006-09-18 23:20:35 -04001397 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399 return -ENOMEM;
1400}
1401
1402/*
1403 * COMMIT call returned
1404 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001405static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001407 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1411 task->tk_pid, task->tk_status);
1412
Trond Myklebust788e7a82006-03-20 13:44:27 -05001413 /* Call the NFS version-specific code */
1414 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1415 return;
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 while (!list_empty(&data->pages)) {
1418 req = nfs_list_entry(data->pages.next);
1419 nfs_list_remove_request(req);
Christoph Lameterfd39fc82006-06-30 01:55:40 -07001420 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1423 req->wb_context->dentry->d_inode->i_sb->s_id,
1424 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1425 req->wb_bytes,
1426 (long long)req_offset(req));
1427 if (task->tk_status < 0) {
1428 req->wb_context->error = task->tk_status;
1429 nfs_inode_remove_request(req);
1430 dprintk(", error = %d\n", task->tk_status);
1431 goto next;
1432 }
1433
1434 /* Okay, COMMIT succeeded, apparently. Check the verifier
1435 * returned by the server against all stored verfs. */
1436 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1437 /* We have a match */
1438 nfs_inode_remove_request(req);
1439 dprintk(" OK\n");
1440 goto next;
1441 }
1442 /* We have a mismatch. Write the page again */
1443 dprintk(" mismatch\n");
1444 nfs_mark_request_dirty(req);
1445 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001446 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001449
1450static const struct rpc_call_ops nfs_commit_ops = {
1451 .rpc_call_done = nfs_commit_done,
1452 .rpc_release = nfs_commit_release,
1453};
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001454#else
1455static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1456{
1457 return 0;
1458}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459#endif
1460
1461static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
1462 unsigned int npages, int how)
1463{
1464 struct nfs_inode *nfsi = NFS_I(inode);
1465 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001466 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 spin_lock(&nfsi->req_lock);
1469 res = nfs_scan_dirty(inode, &head, idx_start, npages);
1470 spin_unlock(&nfsi->req_lock);
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001471 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001472 int error = nfs_flush_list(inode, &head, res, how);
1473 if (error < 0)
1474 return error;
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return res;
1477}
1478
1479#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001480int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 struct nfs_inode *nfsi = NFS_I(inode);
1483 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001484 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001487 res = nfs_scan_commit(inode, &head, 0, 0);
1488 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001490 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001491 if (error < 0)
1492 return error;
1493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return res;
1495}
1496#endif
1497
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001498int nfs_sync_inode_wait(struct inode *inode, unsigned long idx_start,
1499 unsigned int npages, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001501 struct nfs_inode *nfsi = NFS_I(inode);
1502 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001503 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001504 int pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001506 how &= ~FLUSH_NOCOMMIT;
1507 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 do {
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001509 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1510 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001511 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001512 pages = nfs_scan_dirty(inode, &head, idx_start, npages);
1513 if (pages != 0) {
1514 spin_unlock(&nfsi->req_lock);
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001515 if (how & FLUSH_INVALIDATE)
Trond Myklebust83715ad2006-07-05 13:17:12 -04001516 nfs_cancel_dirty_list(&head);
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001517 else
1518 ret = nfs_flush_list(inode, &head, pages, how);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001519 spin_lock(&nfsi->req_lock);
1520 continue;
1521 }
1522 if (nocommit)
1523 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001524 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001525 if (pages == 0)
1526 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001527 if (how & FLUSH_INVALIDATE) {
1528 spin_unlock(&nfsi->req_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001529 nfs_cancel_commit_list(&head);
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001530 spin_lock(&nfsi->req_lock);
1531 continue;
1532 }
1533 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001534 spin_unlock(&nfsi->req_lock);
1535 ret = nfs_commit_list(inode, &head, how);
1536 spin_lock(&nfsi->req_lock);
1537 } while (ret >= 0);
1538 spin_unlock(&nfsi->req_lock);
1539 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
David Howellsf7b422b2006-06-09 09:34:33 -04001542int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
1544 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1545 sizeof(struct nfs_write_data),
1546 0, SLAB_HWCACHE_ALIGN,
1547 NULL, NULL);
1548 if (nfs_wdata_cachep == NULL)
1549 return -ENOMEM;
1550
Matthew Dobson93d23412006-03-26 01:37:50 -08001551 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1552 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (nfs_wdata_mempool == NULL)
1554 return -ENOMEM;
1555
Matthew Dobson93d23412006-03-26 01:37:50 -08001556 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1557 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if (nfs_commit_mempool == NULL)
1559 return -ENOMEM;
1560
1561 return 0;
1562}
1563
David Brownell266bee82006-06-27 12:59:15 -07001564void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
1566 mempool_destroy(nfs_commit_mempool);
1567 mempool_destroy(nfs_wdata_mempool);
1568 if (kmem_cache_destroy(nfs_wdata_cachep))
1569 printk(KERN_INFO "nfs_write_data: not all structures were freed\n");
1570}
1571