blob: 29d88209199d0944f7a1ba3cdc88f1df328ee407 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/writeback.h>
55
56#include <linux/sunrpc/clnt.h>
57#include <linux/nfs_fs.h>
58#include <linux/nfs_mount.h>
59#include <linux/nfs_page.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070060#include <linux/backing-dev.h>
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <asm/uaccess.h>
63#include <linux/smp_lock.h>
64
65#include "delegation.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050066#include "iostat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#define NFSDBG_FACILITY NFSDBG_PAGECACHE
69
70#define MIN_POOL_WRITE (32)
71#define MIN_POOL_COMMIT (4)
72
73/*
74 * Local function declarations
75 */
76static struct nfs_page * nfs_update_request(struct nfs_open_context*,
77 struct inode *,
78 struct page *,
79 unsigned int, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static int nfs_wait_on_write_congestion(struct address_space *, int);
81static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int);
82static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
83 unsigned int npages, int how);
Trond Myklebust788e7a82006-03-20 13:44:27 -050084static const struct rpc_call_ops nfs_write_partial_ops;
85static const struct rpc_call_ops nfs_write_full_ops;
86static const struct rpc_call_ops nfs_commit_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88static kmem_cache_t *nfs_wdata_cachep;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050089static mempool_t *nfs_wdata_mempool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static mempool_t *nfs_commit_mempool;
91
92static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
93
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070094struct nfs_write_data *nfs_commit_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS);
Chuck Lever40859d72005-11-30 18:09:02 -050097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (p) {
99 memset(p, 0, sizeof(*p));
100 INIT_LIST_HEAD(&p->pages);
101 }
102 return p;
103}
104
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500105void nfs_commit_rcu_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500107 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
Chuck Lever40859d72005-11-30 18:09:02 -0500108 if (p && (p->pagevec != &p->page_array[0]))
109 kfree(p->pagevec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 mempool_free(p, nfs_commit_mempool);
111}
112
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500113void nfs_commit_free(struct nfs_write_data *wdata)
114{
115 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
116}
117
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700118struct nfs_write_data *nfs_writedata_alloc(size_t len)
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500119{
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700120 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500121 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
122
123 if (p) {
124 memset(p, 0, sizeof(*p));
125 INIT_LIST_HEAD(&p->pages);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700126 p->npages = pagecount;
Chuck Lever0d0b5cb2006-05-25 01:40:53 -0400127 if (pagecount <= ARRAY_SIZE(p->page_array))
128 p->pagevec = p->page_array;
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500129 else {
Chuck Lever0d0b5cb2006-05-25 01:40:53 -0400130 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
131 if (!p->pagevec) {
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500132 mempool_free(p, nfs_wdata_mempool);
133 p = NULL;
134 }
135 }
136 }
137 return p;
138}
139
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500140static void nfs_writedata_rcu_free(struct rcu_head *head)
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500141{
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500142 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500143 if (p && (p->pagevec != &p->page_array[0]))
144 kfree(p->pagevec);
145 mempool_free(p, nfs_wdata_mempool);
146}
147
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500148static void nfs_writedata_free(struct nfs_write_data *wdata)
149{
150 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
151}
152
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100153void nfs_writedata_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 nfs_writedata_free(wdata);
156}
157
158/* Adjust the file length if we're writing beyond the end */
159static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
160{
161 struct inode *inode = page->mapping->host;
162 loff_t end, i_size = i_size_read(inode);
163 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
164
165 if (i_size > 0 && page->index < end_index)
166 return;
167 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
168 if (i_size >= end)
169 return;
Chuck Lever91d5b472006-03-20 13:44:14 -0500170 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 i_size_write(inode, end);
172}
173
174/* We can set the PG_uptodate flag if we see that a write request
175 * covers the full page.
176 */
177static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
178{
179 loff_t end_offs;
180
181 if (PageUptodate(page))
182 return;
183 if (base != 0)
184 return;
185 if (count == PAGE_CACHE_SIZE) {
186 SetPageUptodate(page);
187 return;
188 }
189
190 end_offs = i_size_read(page->mapping->host) - 1;
191 if (end_offs < 0)
192 return;
193 /* Is this the last page? */
194 if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT))
195 return;
196 /* This is the last page: set PG_uptodate if we cover the entire
197 * extent of the data, then zero the rest of the page.
198 */
199 if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) {
200 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
201 SetPageUptodate(page);
202 }
203}
204
205/*
206 * Write a page synchronously.
207 * Offset is the data offset within the page.
208 */
209static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
210 struct page *page, unsigned int offset, unsigned int count,
211 int how)
212{
213 unsigned int wsize = NFS_SERVER(inode)->wsize;
214 int result, written = 0;
215 struct nfs_write_data *wdata;
216
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700217 wdata = nfs_writedata_alloc(wsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (!wdata)
219 return -ENOMEM;
220
221 wdata->flags = how;
222 wdata->cred = ctx->cred;
223 wdata->inode = inode;
224 wdata->args.fh = NFS_FH(inode);
225 wdata->args.context = ctx;
226 wdata->args.pages = &page;
227 wdata->args.stable = NFS_FILE_SYNC;
228 wdata->args.pgbase = offset;
229 wdata->args.count = wsize;
230 wdata->res.fattr = &wdata->fattr;
231 wdata->res.verf = &wdata->verf;
232
233 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
234 inode->i_sb->s_id,
235 (long long)NFS_FILEID(inode),
236 count, (long long)(page_offset(page) + offset));
237
Trond Myklebustbb713d62005-12-03 15:20:14 -0500238 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 nfs_begin_data_update(inode);
240 do {
241 if (count < wsize)
242 wdata->args.count = count;
243 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
244
245 result = NFS_PROTO(inode)->write(wdata);
246
247 if (result < 0) {
248 /* Must mark the page invalid after I/O error */
249 ClearPageUptodate(page);
250 goto io_error;
251 }
252 if (result < wdata->args.count)
253 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
254 wdata->args.count, result);
255
256 wdata->args.offset += result;
257 wdata->args.pgbase += result;
258 written += result;
259 count -= result;
Chuck Lever91d5b472006-03-20 13:44:14 -0500260 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 } while (count);
262 /* Update file length */
263 nfs_grow_file(page, offset, written);
264 /* Set the PG_uptodate flag? */
265 nfs_mark_uptodate(page, offset, written);
266
267 if (PageError(page))
268 ClearPageError(page);
269
270io_error:
Trond Myklebust951a1432005-06-22 17:16:30 +0000271 nfs_end_data_update(inode);
Trond Myklebustbb713d62005-12-03 15:20:14 -0500272 end_page_writeback(page);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500273 nfs_writedata_release(wdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return written ? written : result;
275}
276
277static int nfs_writepage_async(struct nfs_open_context *ctx,
278 struct inode *inode, struct page *page,
279 unsigned int offset, unsigned int count)
280{
281 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 req = nfs_update_request(ctx, inode, page, offset, count);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100284 if (IS_ERR(req))
285 return PTR_ERR(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 /* Update file length */
287 nfs_grow_file(page, offset, count);
288 /* Set the PG_uptodate flag? */
289 nfs_mark_uptodate(page, offset, count);
290 nfs_unlock_request(req);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
294static int wb_priority(struct writeback_control *wbc)
295{
296 if (wbc->for_reclaim)
297 return FLUSH_HIGHPRI;
298 if (wbc->for_kupdate)
299 return FLUSH_LOWPRI;
300 return 0;
301}
302
303/*
304 * Write an mmapped page to the server.
305 */
306int nfs_writepage(struct page *page, struct writeback_control *wbc)
307{
308 struct nfs_open_context *ctx;
309 struct inode *inode = page->mapping->host;
310 unsigned long end_index;
311 unsigned offset = PAGE_CACHE_SIZE;
312 loff_t i_size = i_size_read(inode);
313 int inode_referenced = 0;
314 int priority = wb_priority(wbc);
315 int err;
316
Chuck Lever91d5b472006-03-20 13:44:14 -0500317 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
318 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 /*
321 * Note: We need to ensure that we have a reference to the inode
322 * if we are to do asynchronous writes. If not, waiting
323 * in nfs_wait_on_request() may deadlock with clear_inode().
324 *
325 * If igrab() fails here, then it is in any case safe to
326 * call nfs_wb_page(), since there will be no pending writes.
327 */
328 if (igrab(inode) != 0)
329 inode_referenced = 1;
330 end_index = i_size >> PAGE_CACHE_SHIFT;
331
332 /* Ensure we've flushed out any previous writes */
333 nfs_wb_page_priority(inode, page, priority);
334
335 /* easy case */
336 if (page->index < end_index)
337 goto do_it;
338 /* things got complicated... */
339 offset = i_size & (PAGE_CACHE_SIZE-1);
340
341 /* OK, are we completely out? */
342 err = 0; /* potential race with truncate - ignore */
343 if (page->index >= end_index+1 || !offset)
344 goto out;
345do_it:
Trond Myklebustd5308382005-11-04 15:33:38 -0500346 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (ctx == NULL) {
348 err = -EBADF;
349 goto out;
350 }
351 lock_kernel();
352 if (!IS_SYNC(inode) && inode_referenced) {
353 err = nfs_writepage_async(ctx, inode, page, 0, offset);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100354 if (!wbc->for_writepages)
355 nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 } else {
357 err = nfs_writepage_sync(ctx, inode, page, 0,
358 offset, priority);
359 if (err >= 0) {
360 if (err != offset)
361 redirty_page_for_writepage(wbc, page);
362 err = 0;
363 }
364 }
365 unlock_kernel();
366 put_nfs_open_context(ctx);
367out:
368 unlock_page(page);
369 if (inode_referenced)
370 iput(inode);
371 return err;
372}
373
374/*
375 * Note: causes nfs_update_request() to block on the assumption
376 * that the writeback is generated due to memory pressure.
377 */
378int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
379{
380 struct backing_dev_info *bdi = mapping->backing_dev_info;
381 struct inode *inode = mapping->host;
382 int err;
383
Chuck Lever91d5b472006-03-20 13:44:14 -0500384 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 err = generic_writepages(mapping, wbc);
387 if (err)
388 return err;
389 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
390 if (wbc->nonblocking)
391 return 0;
392 nfs_wait_on_write_congestion(mapping, 0);
393 }
394 err = nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
395 if (err < 0)
396 goto out;
Chuck Lever91d5b472006-03-20 13:44:14 -0500397 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 wbc->nr_to_write -= err;
399 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
400 err = nfs_wait_on_requests(inode, 0, 0);
401 if (err < 0)
402 goto out;
403 }
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000404 err = nfs_commit_inode(inode, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (err > 0) {
406 wbc->nr_to_write -= err;
407 err = 0;
408 }
409out:
410 clear_bit(BDI_write_congested, &bdi->state);
411 wake_up_all(&nfs_write_congestion);
Andrew Morton3fcfab12006-10-19 23:28:16 -0700412 congestion_end(WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return err;
414}
415
416/*
417 * Insert a write request into an inode
418 */
419static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
420{
421 struct nfs_inode *nfsi = NFS_I(inode);
422 int error;
423
424 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
425 BUG_ON(error == -EEXIST);
426 if (error)
427 return error;
428 if (!nfsi->npages) {
429 igrab(inode);
430 nfs_begin_data_update(inode);
431 if (nfs_have_delegation(inode, FMODE_WRITE))
432 nfsi->change_attr++;
433 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500434 SetPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 nfsi->npages++;
436 atomic_inc(&req->wb_count);
437 return 0;
438}
439
440/*
441 * Insert a write request into an inode
442 */
443static void nfs_inode_remove_request(struct nfs_page *req)
444{
445 struct inode *inode = req->wb_context->dentry->d_inode;
446 struct nfs_inode *nfsi = NFS_I(inode);
447
448 BUG_ON (!NFS_WBACK_BUSY(req));
449
450 spin_lock(&nfsi->req_lock);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500451 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
453 nfsi->npages--;
454 if (!nfsi->npages) {
455 spin_unlock(&nfsi->req_lock);
Trond Myklebust951a1432005-06-22 17:16:30 +0000456 nfs_end_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 iput(inode);
458 } else
459 spin_unlock(&nfsi->req_lock);
460 nfs_clear_request(req);
461 nfs_release_request(req);
462}
463
464/*
465 * Find a request
466 */
467static inline struct nfs_page *
468_nfs_find_request(struct inode *inode, unsigned long index)
469{
470 struct nfs_inode *nfsi = NFS_I(inode);
471 struct nfs_page *req;
472
473 req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index);
474 if (req)
475 atomic_inc(&req->wb_count);
476 return req;
477}
478
479static struct nfs_page *
480nfs_find_request(struct inode *inode, unsigned long index)
481{
482 struct nfs_page *req;
483 struct nfs_inode *nfsi = NFS_I(inode);
484
485 spin_lock(&nfsi->req_lock);
486 req = _nfs_find_request(inode, index);
487 spin_unlock(&nfsi->req_lock);
488 return req;
489}
490
491/*
492 * Add a request to the inode's dirty list.
493 */
494static void
495nfs_mark_request_dirty(struct nfs_page *req)
496{
497 struct inode *inode = req->wb_context->dentry->d_inode;
498 struct nfs_inode *nfsi = NFS_I(inode);
499
500 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000501 radix_tree_tag_set(&nfsi->nfs_page_tree,
502 req->wb_index, NFS_PAGE_TAG_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 nfs_list_add_request(req, &nfsi->dirty);
504 nfsi->ndirty++;
505 spin_unlock(&nfsi->req_lock);
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700506 inc_zone_page_state(req->wb_page, NR_FILE_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 mark_inode_dirty(inode);
508}
509
510/*
511 * Check if a request is dirty
512 */
513static inline int
514nfs_dirty_request(struct nfs_page *req)
515{
516 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
517 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
518}
519
520#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
521/*
522 * Add a request to the inode's commit list.
523 */
524static void
525nfs_mark_request_commit(struct nfs_page *req)
526{
527 struct inode *inode = req->wb_context->dentry->d_inode;
528 struct nfs_inode *nfsi = NFS_I(inode);
529
530 spin_lock(&nfsi->req_lock);
531 nfs_list_add_request(req, &nfsi->commit);
532 nfsi->ncommit++;
533 spin_unlock(&nfsi->req_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700534 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 mark_inode_dirty(inode);
536}
537#endif
538
539/*
540 * Wait for a request to complete.
541 *
542 * Interruptible by signals only if mounted with intr flag.
543 */
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500544static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 struct nfs_inode *nfsi = NFS_I(inode);
547 struct nfs_page *req;
548 unsigned long idx_end, next;
549 unsigned int res = 0;
550 int error;
551
552 if (npages == 0)
553 idx_end = ~0;
554 else
555 idx_end = idx_start + npages - 1;
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 next = idx_start;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000558 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 -0700559 if (req->wb_index > idx_end)
560 break;
561
562 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000563 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 atomic_inc(&req->wb_count);
566 spin_unlock(&nfsi->req_lock);
567 error = nfs_wait_on_request(req);
568 nfs_release_request(req);
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500569 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (error < 0)
571 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 res++;
573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return res;
575}
576
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500577static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
578{
579 struct nfs_inode *nfsi = NFS_I(inode);
580 int ret;
581
582 spin_lock(&nfsi->req_lock);
583 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
584 spin_unlock(&nfsi->req_lock);
585 return ret;
586}
587
Trond Myklebust83715ad2006-07-05 13:17:12 -0400588static void nfs_cancel_dirty_list(struct list_head *head)
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400589{
590 struct nfs_page *req;
591 while(!list_empty(head)) {
592 req = nfs_list_entry(head->next);
593 nfs_list_remove_request(req);
594 nfs_inode_remove_request(req);
595 nfs_clear_page_writeback(req);
596 }
597}
598
Trond Myklebust83715ad2006-07-05 13:17:12 -0400599static void nfs_cancel_commit_list(struct list_head *head)
600{
601 struct nfs_page *req;
602
603 while(!list_empty(head)) {
604 req = nfs_list_entry(head->next);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700605 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400606 nfs_list_remove_request(req);
607 nfs_inode_remove_request(req);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700608 nfs_unlock_request(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400609 }
610}
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612/*
613 * nfs_scan_dirty - Scan an inode for dirty requests
614 * @inode: NFS inode to scan
615 * @dst: destination list
616 * @idx_start: lower bound of page->index to scan.
617 * @npages: idx_start + npages sets the upper bound to scan.
618 *
619 * Moves requests from the inode's dirty page list.
620 * The requests are *not* checked to ensure that they form a contiguous set.
621 */
622static int
623nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
624{
625 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000626 int res = 0;
627
628 if (nfsi->ndirty != 0) {
629 res = nfs_scan_lock_dirty(nfsi, dst, idx_start, npages);
630 nfsi->ndirty -= res;
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000631 if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
632 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return res;
635}
636
637#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
638/*
639 * nfs_scan_commit - Scan an inode for commit requests
640 * @inode: NFS inode to scan
641 * @dst: destination list
642 * @idx_start: lower bound of page->index to scan.
643 * @npages: idx_start + npages sets the upper bound to scan.
644 *
645 * Moves requests from the inode's 'commit' request list.
646 * The requests are *not* checked to ensure that they form a contiguous set.
647 */
648static int
649nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
650{
651 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000652 int res = 0;
653
654 if (nfsi->ncommit != 0) {
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400655 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000656 nfsi->ncommit -= res;
657 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
658 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return res;
661}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500662#else
663static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
664{
665 return 0;
666}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667#endif
668
669static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
670{
671 struct backing_dev_info *bdi = mapping->backing_dev_info;
672 DEFINE_WAIT(wait);
673 int ret = 0;
674
675 might_sleep();
676
677 if (!bdi_write_congested(bdi))
678 return 0;
Chuck Lever91d5b472006-03-20 13:44:14 -0500679
680 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (intr) {
683 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
684 sigset_t oldset;
685
686 rpc_clnt_sigmask(clnt, &oldset);
687 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
688 if (bdi_write_congested(bdi)) {
689 if (signalled())
690 ret = -ERESTARTSYS;
691 else
692 schedule();
693 }
694 rpc_clnt_sigunmask(clnt, &oldset);
695 } else {
696 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
697 if (bdi_write_congested(bdi))
698 schedule();
699 }
700 finish_wait(&nfs_write_congestion, &wait);
701 return ret;
702}
703
704
705/*
706 * Try to update any existing write request, or create one if there is none.
707 * In order to match, the request's credentials must match those of
708 * the calling process.
709 *
710 * Note: Should always be called with the Page Lock held!
711 */
712static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
713 struct inode *inode, struct page *page,
714 unsigned int offset, unsigned int bytes)
715{
716 struct nfs_server *server = NFS_SERVER(inode);
717 struct nfs_inode *nfsi = NFS_I(inode);
718 struct nfs_page *req, *new = NULL;
719 unsigned long rqend, end;
720
721 end = offset + bytes;
722
723 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
724 return ERR_PTR(-ERESTARTSYS);
725 for (;;) {
726 /* Loop over all inode entries and see if we find
727 * A request for the page we wish to update
728 */
729 spin_lock(&nfsi->req_lock);
730 req = _nfs_find_request(inode, page->index);
731 if (req) {
732 if (!nfs_lock_request_dontget(req)) {
733 int error;
734 spin_unlock(&nfsi->req_lock);
735 error = nfs_wait_on_request(req);
736 nfs_release_request(req);
Neil Brown1dd594b2006-03-20 13:44:04 -0500737 if (error < 0) {
738 if (new)
739 nfs_release_request(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return ERR_PTR(error);
Neil Brown1dd594b2006-03-20 13:44:04 -0500741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 continue;
743 }
744 spin_unlock(&nfsi->req_lock);
745 if (new)
746 nfs_release_request(new);
747 break;
748 }
749
750 if (new) {
751 int error;
752 nfs_lock_request_dontget(new);
753 error = nfs_inode_add_request(inode, new);
754 if (error) {
755 spin_unlock(&nfsi->req_lock);
756 nfs_unlock_request(new);
757 return ERR_PTR(error);
758 }
759 spin_unlock(&nfsi->req_lock);
760 nfs_mark_request_dirty(new);
761 return new;
762 }
763 spin_unlock(&nfsi->req_lock);
764
765 new = nfs_create_request(ctx, inode, page, offset, bytes);
766 if (IS_ERR(new))
767 return new;
768 }
769
770 /* We have a request for our page.
771 * If the creds don't match, or the
772 * page addresses don't match,
773 * tell the caller to wait on the conflicting
774 * request.
775 */
776 rqend = req->wb_offset + req->wb_bytes;
777 if (req->wb_context != ctx
778 || req->wb_page != page
779 || !nfs_dirty_request(req)
780 || offset > rqend || end < req->wb_offset) {
781 nfs_unlock_request(req);
782 return ERR_PTR(-EBUSY);
783 }
784
785 /* Okay, the request matches. Update the region */
786 if (offset < req->wb_offset) {
787 req->wb_offset = offset;
788 req->wb_pgbase = offset;
789 req->wb_bytes = rqend - req->wb_offset;
790 }
791
792 if (end > rqend)
793 req->wb_bytes = end - req->wb_offset;
794
795 return req;
796}
797
798int nfs_flush_incompatible(struct file *file, struct page *page)
799{
800 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
801 struct inode *inode = page->mapping->host;
802 struct nfs_page *req;
803 int status = 0;
804 /*
805 * Look for a request corresponding to this page. If there
806 * is one, and it belongs to another file, we flush it out
807 * before we try to copy anything into the page. Do this
808 * due to the lack of an ACCESS-type call in NFSv2.
809 * Also do the same if we find a request from an existing
810 * dropped page.
811 */
812 req = nfs_find_request(inode, page->index);
813 if (req) {
814 if (req->wb_page != page || ctx != req->wb_context)
815 status = nfs_wb_page(inode, page);
816 nfs_release_request(req);
817 }
818 return (status < 0) ? status : 0;
819}
820
821/*
822 * Update and possibly write a cached page of an NFS file.
823 *
824 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
825 * things with a page scheduled for an RPC call (e.g. invalidate it).
826 */
827int nfs_updatepage(struct file *file, struct page *page,
828 unsigned int offset, unsigned int count)
829{
830 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 struct inode *inode = page->mapping->host;
832 struct nfs_page *req;
833 int status = 0;
834
Chuck Lever91d5b472006-03-20 13:44:14 -0500835 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500838 file->f_dentry->d_parent->d_name.name,
839 file->f_dentry->d_name.name, count,
840 (long long)(page_offset(page) +offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 if (IS_SYNC(inode)) {
843 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
844 if (status > 0) {
845 if (offset == 0 && status == PAGE_CACHE_SIZE)
846 SetPageUptodate(page);
847 return 0;
848 }
849 return status;
850 }
851
852 /* If we're not using byte range locks, and we know the page
853 * is entirely in cache, it may be more efficient to avoid
854 * fragmenting write requests.
855 */
Trond Myklebustab0a3db2005-06-22 17:16:30 +0000856 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 loff_t end_offs = i_size_read(inode) - 1;
858 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
859
860 count += offset;
861 offset = 0;
862 if (unlikely(end_offs < 0)) {
863 /* Do nothing */
864 } else if (page->index == end_index) {
865 unsigned int pglen;
866 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
867 if (count < pglen)
868 count = pglen;
869 } else if (page->index < end_index)
870 count = PAGE_CACHE_SIZE;
871 }
872
873 /*
874 * Try to find an NFS request corresponding to this page
875 * and update it.
876 * If the existing request cannot be updated, we must flush
877 * it out now.
878 */
879 do {
880 req = nfs_update_request(ctx, inode, page, offset, count);
881 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
882 if (status != -EBUSY)
883 break;
884 /* Request could not be updated. Flush it out and try again */
885 status = nfs_wb_page(inode, page);
886 } while (status >= 0);
887 if (status < 0)
888 goto done;
889
890 status = 0;
891
892 /* Update file length */
893 nfs_grow_file(page, offset, count);
894 /* Set the PG_uptodate flag? */
895 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
896 nfs_unlock_request(req);
897done:
898 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
899 status, (long long)i_size_read(inode));
900 if (status < 0)
901 ClearPageUptodate(page);
902 return status;
903}
904
905static void nfs_writepage_release(struct nfs_page *req)
906{
907 end_page_writeback(req->wb_page);
908
909#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
910 if (!PageError(req->wb_page)) {
911 if (NFS_NEED_RESCHED(req)) {
912 nfs_mark_request_dirty(req);
913 goto out;
914 } else if (NFS_NEED_COMMIT(req)) {
915 nfs_mark_request_commit(req);
916 goto out;
917 }
918 }
919 nfs_inode_remove_request(req);
920
921out:
922 nfs_clear_commit(req);
923 nfs_clear_reschedule(req);
924#else
925 nfs_inode_remove_request(req);
926#endif
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000927 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
930static inline int flush_task_priority(int how)
931{
932 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
933 case FLUSH_HIGHPRI:
934 return RPC_PRIORITY_HIGH;
935 case FLUSH_LOWPRI:
936 return RPC_PRIORITY_LOW;
937 }
938 return RPC_PRIORITY_NORMAL;
939}
940
941/*
942 * Set up the argument/result storage required for the RPC call.
943 */
944static void nfs_write_rpcsetup(struct nfs_page *req,
945 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500946 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 unsigned int count, unsigned int offset,
948 int how)
949{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -0500951 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 /* Set up the RPC argument and reply structs
954 * NB: take care not to mess about with data->commit et al. */
955
956 data->req = req;
957 data->inode = inode = req->wb_context->dentry->d_inode;
958 data->cred = req->wb_context->cred;
959
960 data->args.fh = NFS_FH(inode);
961 data->args.offset = req_offset(req) + offset;
962 data->args.pgbase = req->wb_pgbase + offset;
963 data->args.pages = data->pagevec;
964 data->args.count = count;
965 data->args.context = req->wb_context;
966
967 data->res.fattr = &data->fattr;
968 data->res.count = count;
969 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400970 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Trond Myklebust788e7a82006-03-20 13:44:27 -0500972 /* Set up the initial task struct. */
973 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
974 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 NFS_PROTO(inode)->write_setup(data, how);
976
977 data->task.tk_priority = flush_task_priority(how);
978 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500981 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 inode->i_sb->s_id,
983 (long long)NFS_FILEID(inode),
984 count,
985 (unsigned long long)data->args.offset);
986}
987
988static void nfs_execute_write(struct nfs_write_data *data)
989{
990 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
991 sigset_t oldset;
992
993 rpc_clnt_sigmask(clnt, &oldset);
994 lock_kernel();
995 rpc_execute(&data->task);
996 unlock_kernel();
997 rpc_clnt_sigunmask(clnt, &oldset);
998}
999
1000/*
1001 * Generate multiple small requests to write out a single
1002 * contiguous dirty area on one page.
1003 */
Trond Myklebust7d46a492006-03-20 13:44:50 -05001004static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
1006 struct nfs_page *req = nfs_list_entry(head->next);
1007 struct page *page = req->wb_page;
1008 struct nfs_write_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001009 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
1010 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 int requests = 0;
1012 LIST_HEAD(list);
1013
1014 nfs_list_remove_request(req);
1015
1016 nbytes = req->wb_bytes;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001017 do {
1018 size_t len = min(nbytes, wsize);
1019
1020 data = nfs_writedata_alloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (!data)
1022 goto out_bad;
1023 list_add(&data->pages, &list);
1024 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001025 nbytes -= len;
1026 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 atomic_set(&req->wb_complete, requests);
1028
1029 ClearPageError(page);
Trond Myklebustbb713d62005-12-03 15:20:14 -05001030 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 offset = 0;
1032 nbytes = req->wb_bytes;
1033 do {
1034 data = list_entry(list.next, struct nfs_write_data, pages);
1035 list_del_init(&data->pages);
1036
1037 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 if (nbytes > wsize) {
Trond Myklebust788e7a82006-03-20 13:44:27 -05001040 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1041 wsize, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 offset += wsize;
1043 nbytes -= wsize;
1044 } else {
Trond Myklebust788e7a82006-03-20 13:44:27 -05001045 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1046 nbytes, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 nbytes = 0;
1048 }
1049 nfs_execute_write(data);
1050 } while (nbytes != 0);
1051
1052 return 0;
1053
1054out_bad:
1055 while (!list_empty(&list)) {
1056 data = list_entry(list.next, struct nfs_write_data, pages);
1057 list_del(&data->pages);
Trond Myklebust8aca67f2006-11-13 16:23:44 -05001058 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001061 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return -ENOMEM;
1063}
1064
1065/*
1066 * Create an RPC task for the given write request and kick it.
1067 * The page must have been locked by the caller.
1068 *
1069 * It may happen that the page we're passed is not marked dirty.
1070 * This is the case if nfs_updatepage detects a conflicting request
1071 * that has been written but not committed.
1072 */
Trond Myklebust7d46a492006-03-20 13:44:50 -05001073static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 struct nfs_page *req;
1076 struct page **pages;
1077 struct nfs_write_data *data;
1078 unsigned int count;
1079
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001080 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (!data)
1082 goto out_bad;
1083
1084 pages = data->pagevec;
1085 count = 0;
1086 while (!list_empty(head)) {
1087 req = nfs_list_entry(head->next);
1088 nfs_list_remove_request(req);
1089 nfs_list_add_request(req, &data->pages);
1090 ClearPageError(req->wb_page);
Trond Myklebustbb713d62005-12-03 15:20:14 -05001091 set_page_writeback(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 *pages++ = req->wb_page;
1093 count += req->wb_bytes;
1094 }
1095 req = nfs_list_entry(data->pages.next);
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 /* Set up the argument struct */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001098 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 nfs_execute_write(data);
1101 return 0;
1102 out_bad:
1103 while (!list_empty(head)) {
1104 struct nfs_page *req = nfs_list_entry(head->next);
1105 nfs_list_remove_request(req);
1106 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001107 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
1109 return -ENOMEM;
1110}
1111
Trond Myklebust7d46a492006-03-20 13:44:50 -05001112static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
1114 LIST_HEAD(one_request);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001115 int (*flush_one)(struct inode *, struct list_head *, int);
1116 struct nfs_page *req;
1117 int wpages = NFS_SERVER(inode)->wpages;
1118 int wsize = NFS_SERVER(inode)->wsize;
1119 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Trond Myklebust7d46a492006-03-20 13:44:50 -05001121 flush_one = nfs_flush_one;
1122 if (wsize < PAGE_CACHE_SIZE)
1123 flush_one = nfs_flush_multi;
1124 /* For single writes, FLUSH_STABLE is more efficient */
1125 if (npages <= wpages && npages == NFS_I(inode)->npages
1126 && nfs_list_entry(head->next)->wb_bytes <= wsize)
1127 how |= FLUSH_STABLE;
1128
1129 do {
1130 nfs_coalesce_requests(head, &one_request, wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 req = nfs_list_entry(one_request.next);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001132 error = flush_one(inode, &one_request, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (error < 0)
Trond Myklebust7d46a492006-03-20 13:44:50 -05001134 goto out_err;
1135 } while (!list_empty(head));
1136 return 0;
1137out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 while (!list_empty(head)) {
1139 req = nfs_list_entry(head->next);
1140 nfs_list_remove_request(req);
1141 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001142 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144 return error;
1145}
1146
1147/*
1148 * Handle a write reply that flushed part of a page.
1149 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001150static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001152 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 struct nfs_page *req = data->req;
1154 struct page *page = req->wb_page;
1155
1156 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1157 req->wb_context->dentry->d_inode->i_sb->s_id,
1158 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1159 req->wb_bytes,
1160 (long long)req_offset(req));
1161
Trond Myklebust788e7a82006-03-20 13:44:27 -05001162 if (nfs_writeback_done(task, data) != 0)
1163 return;
1164
1165 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 ClearPageUptodate(page);
1167 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001168 req->wb_context->error = task->tk_status;
1169 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 } else {
1171#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1172 if (data->verf.committed < NFS_FILE_SYNC) {
1173 if (!NFS_NEED_COMMIT(req)) {
1174 nfs_defer_commit(req);
1175 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1176 dprintk(" defer commit\n");
1177 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1178 nfs_defer_reschedule(req);
1179 dprintk(" server reboot detected\n");
1180 }
1181 } else
1182#endif
1183 dprintk(" OK\n");
1184 }
1185
1186 if (atomic_dec_and_test(&req->wb_complete))
1187 nfs_writepage_release(req);
1188}
1189
Trond Myklebust788e7a82006-03-20 13:44:27 -05001190static const struct rpc_call_ops nfs_write_partial_ops = {
1191 .rpc_call_done = nfs_writeback_done_partial,
1192 .rpc_release = nfs_writedata_release,
1193};
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195/*
1196 * Handle a write reply that flushes a whole page.
1197 *
1198 * FIXME: There is an inherent race with invalidate_inode_pages and
1199 * writebacks since the page->count is kept > 1 for as long
1200 * as the page has a write request pending.
1201 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001202static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001204 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 struct nfs_page *req;
1206 struct page *page;
1207
Trond Myklebust788e7a82006-03-20 13:44:27 -05001208 if (nfs_writeback_done(task, data) != 0)
1209 return;
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 /* Update attributes as result of writeback. */
1212 while (!list_empty(&data->pages)) {
1213 req = nfs_list_entry(data->pages.next);
1214 nfs_list_remove_request(req);
1215 page = req->wb_page;
1216
1217 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1218 req->wb_context->dentry->d_inode->i_sb->s_id,
1219 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1220 req->wb_bytes,
1221 (long long)req_offset(req));
1222
Trond Myklebust788e7a82006-03-20 13:44:27 -05001223 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 ClearPageUptodate(page);
1225 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001226 req->wb_context->error = task->tk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 end_page_writeback(page);
1228 nfs_inode_remove_request(req);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001229 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 goto next;
1231 }
1232 end_page_writeback(page);
1233
1234#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1235 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1236 nfs_inode_remove_request(req);
1237 dprintk(" OK\n");
1238 goto next;
1239 }
1240 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1241 nfs_mark_request_commit(req);
1242 dprintk(" marked for commit\n");
1243#else
1244 nfs_inode_remove_request(req);
1245#endif
1246 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001247 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249}
1250
Trond Myklebust788e7a82006-03-20 13:44:27 -05001251static const struct rpc_call_ops nfs_write_full_ops = {
1252 .rpc_call_done = nfs_writeback_done_full,
1253 .rpc_release = nfs_writedata_release,
1254};
1255
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257/*
1258 * This function is called when the WRITE call is complete.
1259 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001260int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 struct nfs_writeargs *argp = &data->args;
1263 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001264 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1267 task->tk_pid, task->tk_status);
1268
Chuck Leverf551e442006-09-20 14:33:04 -04001269 /*
1270 * ->write_done will attempt to use post-op attributes to detect
1271 * conflicting writes by other clients. A strict interpretation
1272 * of close-to-open would allow us to continue caching even if
1273 * another writer had changed the file, but some applications
1274 * depend on tighter cache coherency when writing.
1275 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001276 status = NFS_PROTO(data->inode)->write_done(task, data);
1277 if (status != 0)
1278 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001279 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1282 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1283 /* We tried a write call, but the server did not
1284 * commit data to stable storage even though we
1285 * requested it.
1286 * Note: There is a known bug in Tru64 < 5.0 in which
1287 * the server reports NFS_DATA_SYNC, but performs
1288 * NFS_FILE_SYNC. We therefore implement this checking
1289 * as a dprintk() in order to avoid filling syslog.
1290 */
1291 static unsigned long complain;
1292
1293 if (time_before(complain, jiffies)) {
1294 dprintk("NFS: faulty NFS server %s:"
1295 " (committed = %d) != (stable = %d)\n",
David Howells54ceac42006-08-22 20:06:13 -04001296 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 resp->verf->committed, argp->stable);
1298 complain = jiffies + 300 * HZ;
1299 }
1300 }
1301#endif
1302 /* Is this a short write? */
1303 if (task->tk_status >= 0 && resp->count < argp->count) {
1304 static unsigned long complain;
1305
Chuck Lever91d5b472006-03-20 13:44:14 -05001306 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 /* Has the server at least made some progress? */
1309 if (resp->count != 0) {
1310 /* Was this an NFSv2 write or an NFSv3 stable write? */
1311 if (resp->verf->committed != NFS_UNSTABLE) {
1312 /* Resend from where the server left off */
1313 argp->offset += resp->count;
1314 argp->pgbase += resp->count;
1315 argp->count -= resp->count;
1316 } else {
1317 /* Resend as a stable write in order to avoid
1318 * headaches in the case of a server crash.
1319 */
1320 argp->stable = NFS_FILE_SYNC;
1321 }
1322 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001323 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325 if (time_before(complain, jiffies)) {
1326 printk(KERN_WARNING
1327 "NFS: Server wrote zero bytes, expected %u.\n",
1328 argp->count);
1329 complain = jiffies + 300 * HZ;
1330 }
1331 /* Can't do anything about it except throw an error. */
1332 task->tk_status = -EIO;
1333 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001334 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
1337
1338#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001339void nfs_commit_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 nfs_commit_free(wdata);
1342}
1343
1344/*
1345 * Set up the argument/result storage required for the RPC call.
1346 */
1347static void nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001348 struct nfs_write_data *data,
1349 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001351 struct nfs_page *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001353 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 /* Set up the RPC argument and reply structs
1356 * NB: take care not to mess about with data->commit et al. */
1357
1358 list_splice_init(head, &data->pages);
1359 first = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 inode = first->wb_context->dentry->d_inode;
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 data->inode = inode;
1363 data->cred = first->wb_context->cred;
1364
1365 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001366 /* Note: we always request a commit of the entire inode */
1367 data->args.offset = 0;
1368 data->args.count = 0;
1369 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 data->res.fattr = &data->fattr;
1371 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001372 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001373
1374 /* Set up the initial task struct. */
1375 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1376 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 NFS_PROTO(inode)->commit_setup(data, how);
1378
1379 data->task.tk_priority = flush_task_priority(how);
1380 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Chuck Lever0bbacc42005-11-01 16:53:32 -05001382 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
1384
1385/*
1386 * Commit dirty pages
1387 */
1388static int
Chuck Lever40859d72005-11-30 18:09:02 -05001389nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 struct nfs_write_data *data;
1392 struct nfs_page *req;
1393
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001394 data = nfs_commit_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 if (!data)
1397 goto out_bad;
1398
1399 /* Set up the argument struct */
1400 nfs_commit_rpcsetup(head, data, how);
1401
1402 nfs_execute_write(data);
1403 return 0;
1404 out_bad:
1405 while (!list_empty(head)) {
1406 req = nfs_list_entry(head->next);
1407 nfs_list_remove_request(req);
1408 nfs_mark_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001409 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust5c2d97c2006-09-18 23:20:35 -04001410 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412 return -ENOMEM;
1413}
1414
1415/*
1416 * COMMIT call returned
1417 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001418static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001420 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1424 task->tk_pid, task->tk_status);
1425
Trond Myklebust788e7a82006-03-20 13:44:27 -05001426 /* Call the NFS version-specific code */
1427 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1428 return;
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 while (!list_empty(&data->pages)) {
1431 req = nfs_list_entry(data->pages.next);
1432 nfs_list_remove_request(req);
Christoph Lameterfd39fc82006-06-30 01:55:40 -07001433 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1436 req->wb_context->dentry->d_inode->i_sb->s_id,
1437 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1438 req->wb_bytes,
1439 (long long)req_offset(req));
1440 if (task->tk_status < 0) {
1441 req->wb_context->error = task->tk_status;
1442 nfs_inode_remove_request(req);
1443 dprintk(", error = %d\n", task->tk_status);
1444 goto next;
1445 }
1446
1447 /* Okay, COMMIT succeeded, apparently. Check the verifier
1448 * returned by the server against all stored verfs. */
1449 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1450 /* We have a match */
1451 nfs_inode_remove_request(req);
1452 dprintk(" OK\n");
1453 goto next;
1454 }
1455 /* We have a mismatch. Write the page again */
1456 dprintk(" mismatch\n");
1457 nfs_mark_request_dirty(req);
1458 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001459 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001462
1463static const struct rpc_call_ops nfs_commit_ops = {
1464 .rpc_call_done = nfs_commit_done,
1465 .rpc_release = nfs_commit_release,
1466};
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001467#else
1468static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1469{
1470 return 0;
1471}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472#endif
1473
1474static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
1475 unsigned int npages, int how)
1476{
1477 struct nfs_inode *nfsi = NFS_I(inode);
1478 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001479 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 spin_lock(&nfsi->req_lock);
1482 res = nfs_scan_dirty(inode, &head, idx_start, npages);
1483 spin_unlock(&nfsi->req_lock);
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001484 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001485 int error = nfs_flush_list(inode, &head, res, how);
1486 if (error < 0)
1487 return error;
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return res;
1490}
1491
1492#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001493int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
1495 struct nfs_inode *nfsi = NFS_I(inode);
1496 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001497 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001500 res = nfs_scan_commit(inode, &head, 0, 0);
1501 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001503 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001504 if (error < 0)
1505 return error;
1506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return res;
1508}
1509#endif
1510
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001511int nfs_sync_inode_wait(struct inode *inode, unsigned long idx_start,
1512 unsigned int npages, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001514 struct nfs_inode *nfsi = NFS_I(inode);
1515 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001516 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001517 int pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001519 how &= ~FLUSH_NOCOMMIT;
1520 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 do {
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001522 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1523 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001524 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001525 pages = nfs_scan_dirty(inode, &head, idx_start, npages);
1526 if (pages != 0) {
1527 spin_unlock(&nfsi->req_lock);
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001528 if (how & FLUSH_INVALIDATE)
Trond Myklebust83715ad2006-07-05 13:17:12 -04001529 nfs_cancel_dirty_list(&head);
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001530 else
1531 ret = nfs_flush_list(inode, &head, pages, how);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001532 spin_lock(&nfsi->req_lock);
1533 continue;
1534 }
1535 if (nocommit)
1536 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001537 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001538 if (pages == 0)
1539 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001540 if (how & FLUSH_INVALIDATE) {
1541 spin_unlock(&nfsi->req_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001542 nfs_cancel_commit_list(&head);
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001543 spin_lock(&nfsi->req_lock);
1544 continue;
1545 }
1546 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001547 spin_unlock(&nfsi->req_lock);
1548 ret = nfs_commit_list(inode, &head, how);
1549 spin_lock(&nfsi->req_lock);
1550 } while (ret >= 0);
1551 spin_unlock(&nfsi->req_lock);
1552 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
1554
David Howellsf7b422b2006-06-09 09:34:33 -04001555int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
1557 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1558 sizeof(struct nfs_write_data),
1559 0, SLAB_HWCACHE_ALIGN,
1560 NULL, NULL);
1561 if (nfs_wdata_cachep == NULL)
1562 return -ENOMEM;
1563
Matthew Dobson93d23412006-03-26 01:37:50 -08001564 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1565 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 if (nfs_wdata_mempool == NULL)
1567 return -ENOMEM;
1568
Matthew Dobson93d23412006-03-26 01:37:50 -08001569 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1570 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (nfs_commit_mempool == NULL)
1572 return -ENOMEM;
1573
1574 return 0;
1575}
1576
David Brownell266bee82006-06-27 12:59:15 -07001577void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
1579 mempool_destroy(nfs_commit_mempool);
1580 mempool_destroy(nfs_wdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001581 kmem_cache_destroy(nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583