blob: dbc89fa7e9d5340815c4ce834c4804d67c07baf6 [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);
Trond Myklebust3f442542006-09-17 14:46:44 -040082static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, 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 Myklebust8aca67f2006-11-13 16:23:44 -0500104void nfs_commit_rcu_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500106 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
Chuck Lever40859d72005-11-30 18:09:02 -0500107 if (p && (p->pagevec != &p->page_array[0]))
108 kfree(p->pagevec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 mempool_free(p, nfs_commit_mempool);
110}
111
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500112void nfs_commit_free(struct nfs_write_data *wdata)
113{
114 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
115}
116
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700117struct nfs_write_data *nfs_writedata_alloc(size_t len)
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500118{
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700119 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500120 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
121
122 if (p) {
123 memset(p, 0, sizeof(*p));
124 INIT_LIST_HEAD(&p->pages);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700125 p->npages = pagecount;
Chuck Lever0d0b5cb2006-05-25 01:40:53 -0400126 if (pagecount <= ARRAY_SIZE(p->page_array))
127 p->pagevec = p->page_array;
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500128 else {
Chuck Lever0d0b5cb2006-05-25 01:40:53 -0400129 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
130 if (!p->pagevec) {
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500131 mempool_free(p, nfs_wdata_mempool);
132 p = NULL;
133 }
134 }
135 }
136 return p;
137}
138
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500139static void nfs_writedata_rcu_free(struct rcu_head *head)
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500140{
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500141 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500142 if (p && (p->pagevec != &p->page_array[0]))
143 kfree(p->pagevec);
144 mempool_free(p, nfs_wdata_mempool);
145}
146
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500147static void nfs_writedata_free(struct nfs_write_data *wdata)
148{
149 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
150}
151
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100152void nfs_writedata_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 nfs_writedata_free(wdata);
155}
156
157/* Adjust the file length if we're writing beyond the end */
158static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
159{
160 struct inode *inode = page->mapping->host;
161 loff_t end, i_size = i_size_read(inode);
162 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
163
164 if (i_size > 0 && page->index < end_index)
165 return;
166 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
167 if (i_size >= end)
168 return;
Chuck Lever91d5b472006-03-20 13:44:14 -0500169 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 i_size_write(inode, end);
171}
172
173/* We can set the PG_uptodate flag if we see that a write request
174 * covers the full page.
175 */
176static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
177{
178 loff_t end_offs;
179
180 if (PageUptodate(page))
181 return;
182 if (base != 0)
183 return;
184 if (count == PAGE_CACHE_SIZE) {
185 SetPageUptodate(page);
186 return;
187 }
188
189 end_offs = i_size_read(page->mapping->host) - 1;
190 if (end_offs < 0)
191 return;
192 /* Is this the last page? */
193 if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT))
194 return;
195 /* This is the last page: set PG_uptodate if we cover the entire
196 * extent of the data, then zero the rest of the page.
197 */
198 if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) {
199 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
200 SetPageUptodate(page);
201 }
202}
203
204/*
205 * Write a page synchronously.
206 * Offset is the data offset within the page.
207 */
208static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
209 struct page *page, unsigned int offset, unsigned int count,
210 int how)
211{
212 unsigned int wsize = NFS_SERVER(inode)->wsize;
213 int result, written = 0;
214 struct nfs_write_data *wdata;
215
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700216 wdata = nfs_writedata_alloc(wsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (!wdata)
218 return -ENOMEM;
219
220 wdata->flags = how;
221 wdata->cred = ctx->cred;
222 wdata->inode = inode;
223 wdata->args.fh = NFS_FH(inode);
224 wdata->args.context = ctx;
225 wdata->args.pages = &page;
226 wdata->args.stable = NFS_FILE_SYNC;
227 wdata->args.pgbase = offset;
228 wdata->args.count = wsize;
229 wdata->res.fattr = &wdata->fattr;
230 wdata->res.verf = &wdata->verf;
231
232 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
233 inode->i_sb->s_id,
234 (long long)NFS_FILEID(inode),
235 count, (long long)(page_offset(page) + offset));
236
Trond Myklebustbb713d62005-12-03 15:20:14 -0500237 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 nfs_begin_data_update(inode);
239 do {
240 if (count < wsize)
241 wdata->args.count = count;
242 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
243
244 result = NFS_PROTO(inode)->write(wdata);
245
246 if (result < 0) {
247 /* Must mark the page invalid after I/O error */
248 ClearPageUptodate(page);
249 goto io_error;
250 }
251 if (result < wdata->args.count)
252 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
253 wdata->args.count, result);
254
255 wdata->args.offset += result;
256 wdata->args.pgbase += result;
257 written += result;
258 count -= result;
Chuck Lever91d5b472006-03-20 13:44:14 -0500259 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 } while (count);
261 /* Update file length */
262 nfs_grow_file(page, offset, written);
263 /* Set the PG_uptodate flag? */
264 nfs_mark_uptodate(page, offset, written);
265
266 if (PageError(page))
267 ClearPageError(page);
268
269io_error:
Trond Myklebust951a1432005-06-22 17:16:30 +0000270 nfs_end_data_update(inode);
Trond Myklebustbb713d62005-12-03 15:20:14 -0500271 end_page_writeback(page);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500272 nfs_writedata_release(wdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return written ? written : result;
274}
275
276static int nfs_writepage_async(struct nfs_open_context *ctx,
277 struct inode *inode, struct page *page,
278 unsigned int offset, unsigned int count)
279{
280 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 req = nfs_update_request(ctx, inode, page, offset, count);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100283 if (IS_ERR(req))
284 return PTR_ERR(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 /* Update file length */
286 nfs_grow_file(page, offset, count);
287 /* Set the PG_uptodate flag? */
288 nfs_mark_uptodate(page, offset, count);
289 nfs_unlock_request(req);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100290 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293static int wb_priority(struct writeback_control *wbc)
294{
295 if (wbc->for_reclaim)
296 return FLUSH_HIGHPRI;
297 if (wbc->for_kupdate)
298 return FLUSH_LOWPRI;
299 return 0;
300}
301
302/*
303 * Write an mmapped page to the server.
304 */
305int nfs_writepage(struct page *page, struct writeback_control *wbc)
306{
307 struct nfs_open_context *ctx;
308 struct inode *inode = page->mapping->host;
309 unsigned long end_index;
310 unsigned offset = PAGE_CACHE_SIZE;
311 loff_t i_size = i_size_read(inode);
312 int inode_referenced = 0;
313 int priority = wb_priority(wbc);
314 int err;
315
Chuck Lever91d5b472006-03-20 13:44:14 -0500316 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
317 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /*
320 * Note: We need to ensure that we have a reference to the inode
321 * if we are to do asynchronous writes. If not, waiting
322 * in nfs_wait_on_request() may deadlock with clear_inode().
323 *
324 * If igrab() fails here, then it is in any case safe to
325 * call nfs_wb_page(), since there will be no pending writes.
326 */
327 if (igrab(inode) != 0)
328 inode_referenced = 1;
329 end_index = i_size >> PAGE_CACHE_SHIFT;
330
331 /* Ensure we've flushed out any previous writes */
332 nfs_wb_page_priority(inode, page, priority);
333
334 /* easy case */
335 if (page->index < end_index)
336 goto do_it;
337 /* things got complicated... */
338 offset = i_size & (PAGE_CACHE_SIZE-1);
339
340 /* OK, are we completely out? */
341 err = 0; /* potential race with truncate - ignore */
342 if (page->index >= end_index+1 || !offset)
343 goto out;
344do_it:
Trond Myklebustd5308382005-11-04 15:33:38 -0500345 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (ctx == NULL) {
347 err = -EBADF;
348 goto out;
349 }
350 lock_kernel();
351 if (!IS_SYNC(inode) && inode_referenced) {
352 err = nfs_writepage_async(ctx, inode, page, 0, offset);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100353 if (!wbc->for_writepages)
Trond Myklebust28c69252006-09-16 13:04:50 -0400354 nfs_flush_mapping(page->mapping, wbc, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 } else {
356 err = nfs_writepage_sync(ctx, inode, page, 0,
357 offset, priority);
358 if (err >= 0) {
359 if (err != offset)
360 redirty_page_for_writepage(wbc, page);
361 err = 0;
362 }
363 }
364 unlock_kernel();
365 put_nfs_open_context(ctx);
366out:
367 unlock_page(page);
368 if (inode_referenced)
369 iput(inode);
370 return err;
371}
372
373/*
374 * Note: causes nfs_update_request() to block on the assumption
375 * that the writeback is generated due to memory pressure.
376 */
377int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
378{
379 struct backing_dev_info *bdi = mapping->backing_dev_info;
380 struct inode *inode = mapping->host;
381 int err;
382
Chuck Lever91d5b472006-03-20 13:44:14 -0500383 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 err = generic_writepages(mapping, wbc);
386 if (err)
387 return err;
388 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
389 if (wbc->nonblocking)
390 return 0;
391 nfs_wait_on_write_congestion(mapping, 0);
392 }
Trond Myklebust28c69252006-09-16 13:04:50 -0400393 err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (err < 0)
395 goto out;
Chuck Lever91d5b472006-03-20 13:44:14 -0500396 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
398 err = nfs_wait_on_requests(inode, 0, 0);
399 if (err < 0)
400 goto out;
401 }
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000402 err = nfs_commit_inode(inode, wb_priority(wbc));
Trond Myklebust3f442542006-09-17 14:46:44 -0400403 if (err > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405out:
406 clear_bit(BDI_write_congested, &bdi->state);
407 wake_up_all(&nfs_write_congestion);
Andrew Morton3fcfab12006-10-19 23:28:16 -0700408 congestion_end(WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return err;
410}
411
412/*
413 * Insert a write request into an inode
414 */
415static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
416{
417 struct nfs_inode *nfsi = NFS_I(inode);
418 int error;
419
420 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
421 BUG_ON(error == -EEXIST);
422 if (error)
423 return error;
424 if (!nfsi->npages) {
425 igrab(inode);
426 nfs_begin_data_update(inode);
427 if (nfs_have_delegation(inode, FMODE_WRITE))
428 nfsi->change_attr++;
429 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500430 SetPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 nfsi->npages++;
432 atomic_inc(&req->wb_count);
433 return 0;
434}
435
436/*
437 * Insert a write request into an inode
438 */
439static void nfs_inode_remove_request(struct nfs_page *req)
440{
441 struct inode *inode = req->wb_context->dentry->d_inode;
442 struct nfs_inode *nfsi = NFS_I(inode);
443
444 BUG_ON (!NFS_WBACK_BUSY(req));
445
446 spin_lock(&nfsi->req_lock);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500447 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
449 nfsi->npages--;
450 if (!nfsi->npages) {
451 spin_unlock(&nfsi->req_lock);
Trond Myklebust951a1432005-06-22 17:16:30 +0000452 nfs_end_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 iput(inode);
454 } else
455 spin_unlock(&nfsi->req_lock);
456 nfs_clear_request(req);
457 nfs_release_request(req);
458}
459
460/*
461 * Find a request
462 */
463static inline struct nfs_page *
464_nfs_find_request(struct inode *inode, unsigned long index)
465{
466 struct nfs_inode *nfsi = NFS_I(inode);
467 struct nfs_page *req;
468
469 req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index);
470 if (req)
471 atomic_inc(&req->wb_count);
472 return req;
473}
474
475static struct nfs_page *
476nfs_find_request(struct inode *inode, unsigned long index)
477{
478 struct nfs_page *req;
479 struct nfs_inode *nfsi = NFS_I(inode);
480
481 spin_lock(&nfsi->req_lock);
482 req = _nfs_find_request(inode, index);
483 spin_unlock(&nfsi->req_lock);
484 return req;
485}
486
487/*
488 * Add a request to the inode's dirty list.
489 */
490static void
491nfs_mark_request_dirty(struct nfs_page *req)
492{
493 struct inode *inode = req->wb_context->dentry->d_inode;
494 struct nfs_inode *nfsi = NFS_I(inode);
495
496 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000497 radix_tree_tag_set(&nfsi->nfs_page_tree,
498 req->wb_index, NFS_PAGE_TAG_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 nfs_list_add_request(req, &nfsi->dirty);
500 nfsi->ndirty++;
501 spin_unlock(&nfsi->req_lock);
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700502 inc_zone_page_state(req->wb_page, NR_FILE_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 mark_inode_dirty(inode);
504}
505
506/*
507 * Check if a request is dirty
508 */
509static inline int
510nfs_dirty_request(struct nfs_page *req)
511{
512 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
513 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
514}
515
516#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
517/*
518 * Add a request to the inode's commit list.
519 */
520static void
521nfs_mark_request_commit(struct nfs_page *req)
522{
523 struct inode *inode = req->wb_context->dentry->d_inode;
524 struct nfs_inode *nfsi = NFS_I(inode);
525
526 spin_lock(&nfsi->req_lock);
527 nfs_list_add_request(req, &nfsi->commit);
528 nfsi->ncommit++;
529 spin_unlock(&nfsi->req_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700530 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 mark_inode_dirty(inode);
532}
533#endif
534
535/*
536 * Wait for a request to complete.
537 *
538 * Interruptible by signals only if mounted with intr flag.
539 */
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500540static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 struct nfs_inode *nfsi = NFS_I(inode);
543 struct nfs_page *req;
544 unsigned long idx_end, next;
545 unsigned int res = 0;
546 int error;
547
548 if (npages == 0)
549 idx_end = ~0;
550 else
551 idx_end = idx_start + npages - 1;
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 next = idx_start;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000554 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 -0700555 if (req->wb_index > idx_end)
556 break;
557
558 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000559 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 atomic_inc(&req->wb_count);
562 spin_unlock(&nfsi->req_lock);
563 error = nfs_wait_on_request(req);
564 nfs_release_request(req);
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500565 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (error < 0)
567 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 res++;
569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return res;
571}
572
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500573static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
574{
575 struct nfs_inode *nfsi = NFS_I(inode);
576 int ret;
577
578 spin_lock(&nfsi->req_lock);
579 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
580 spin_unlock(&nfsi->req_lock);
581 return ret;
582}
583
Trond Myklebust83715ad2006-07-05 13:17:12 -0400584static void nfs_cancel_dirty_list(struct list_head *head)
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400585{
586 struct nfs_page *req;
587 while(!list_empty(head)) {
588 req = nfs_list_entry(head->next);
589 nfs_list_remove_request(req);
590 nfs_inode_remove_request(req);
591 nfs_clear_page_writeback(req);
592 }
593}
594
Trond Myklebust83715ad2006-07-05 13:17:12 -0400595static void nfs_cancel_commit_list(struct list_head *head)
596{
597 struct nfs_page *req;
598
599 while(!list_empty(head)) {
600 req = nfs_list_entry(head->next);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700601 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400602 nfs_list_remove_request(req);
603 nfs_inode_remove_request(req);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700604 nfs_unlock_request(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400605 }
606}
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
609/*
610 * nfs_scan_commit - Scan an inode for commit requests
611 * @inode: NFS inode to scan
612 * @dst: destination list
613 * @idx_start: lower bound of page->index to scan.
614 * @npages: idx_start + npages sets the upper bound to scan.
615 *
616 * Moves requests from the inode's 'commit' request list.
617 * The requests are *not* checked to ensure that they form a contiguous set.
618 */
619static int
620nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
621{
622 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000623 int res = 0;
624
625 if (nfsi->ncommit != 0) {
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400626 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000627 nfsi->ncommit -= res;
628 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
629 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return res;
632}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500633#else
634static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
635{
636 return 0;
637}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638#endif
639
640static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
641{
642 struct backing_dev_info *bdi = mapping->backing_dev_info;
643 DEFINE_WAIT(wait);
644 int ret = 0;
645
646 might_sleep();
647
648 if (!bdi_write_congested(bdi))
649 return 0;
Chuck Lever91d5b472006-03-20 13:44:14 -0500650
651 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (intr) {
654 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
655 sigset_t oldset;
656
657 rpc_clnt_sigmask(clnt, &oldset);
658 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
659 if (bdi_write_congested(bdi)) {
660 if (signalled())
661 ret = -ERESTARTSYS;
662 else
663 schedule();
664 }
665 rpc_clnt_sigunmask(clnt, &oldset);
666 } else {
667 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
668 if (bdi_write_congested(bdi))
669 schedule();
670 }
671 finish_wait(&nfs_write_congestion, &wait);
672 return ret;
673}
674
675
676/*
677 * Try to update any existing write request, or create one if there is none.
678 * In order to match, the request's credentials must match those of
679 * the calling process.
680 *
681 * Note: Should always be called with the Page Lock held!
682 */
683static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
684 struct inode *inode, struct page *page,
685 unsigned int offset, unsigned int bytes)
686{
687 struct nfs_server *server = NFS_SERVER(inode);
688 struct nfs_inode *nfsi = NFS_I(inode);
689 struct nfs_page *req, *new = NULL;
690 unsigned long rqend, end;
691
692 end = offset + bytes;
693
694 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
695 return ERR_PTR(-ERESTARTSYS);
696 for (;;) {
697 /* Loop over all inode entries and see if we find
698 * A request for the page we wish to update
699 */
700 spin_lock(&nfsi->req_lock);
701 req = _nfs_find_request(inode, page->index);
702 if (req) {
703 if (!nfs_lock_request_dontget(req)) {
704 int error;
705 spin_unlock(&nfsi->req_lock);
706 error = nfs_wait_on_request(req);
707 nfs_release_request(req);
Neil Brown1dd594b2006-03-20 13:44:04 -0500708 if (error < 0) {
709 if (new)
710 nfs_release_request(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return ERR_PTR(error);
Neil Brown1dd594b2006-03-20 13:44:04 -0500712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 continue;
714 }
715 spin_unlock(&nfsi->req_lock);
716 if (new)
717 nfs_release_request(new);
718 break;
719 }
720
721 if (new) {
722 int error;
723 nfs_lock_request_dontget(new);
724 error = nfs_inode_add_request(inode, new);
725 if (error) {
726 spin_unlock(&nfsi->req_lock);
727 nfs_unlock_request(new);
728 return ERR_PTR(error);
729 }
730 spin_unlock(&nfsi->req_lock);
731 nfs_mark_request_dirty(new);
732 return new;
733 }
734 spin_unlock(&nfsi->req_lock);
735
736 new = nfs_create_request(ctx, inode, page, offset, bytes);
737 if (IS_ERR(new))
738 return new;
739 }
740
741 /* We have a request for our page.
742 * If the creds don't match, or the
743 * page addresses don't match,
744 * tell the caller to wait on the conflicting
745 * request.
746 */
747 rqend = req->wb_offset + req->wb_bytes;
748 if (req->wb_context != ctx
749 || req->wb_page != page
750 || !nfs_dirty_request(req)
751 || offset > rqend || end < req->wb_offset) {
752 nfs_unlock_request(req);
753 return ERR_PTR(-EBUSY);
754 }
755
756 /* Okay, the request matches. Update the region */
757 if (offset < req->wb_offset) {
758 req->wb_offset = offset;
759 req->wb_pgbase = offset;
760 req->wb_bytes = rqend - req->wb_offset;
761 }
762
763 if (end > rqend)
764 req->wb_bytes = end - req->wb_offset;
765
766 return req;
767}
768
769int nfs_flush_incompatible(struct file *file, struct page *page)
770{
771 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
772 struct inode *inode = page->mapping->host;
773 struct nfs_page *req;
774 int status = 0;
775 /*
776 * Look for a request corresponding to this page. If there
777 * is one, and it belongs to another file, we flush it out
778 * before we try to copy anything into the page. Do this
779 * due to the lack of an ACCESS-type call in NFSv2.
780 * Also do the same if we find a request from an existing
781 * dropped page.
782 */
783 req = nfs_find_request(inode, page->index);
784 if (req) {
785 if (req->wb_page != page || ctx != req->wb_context)
786 status = nfs_wb_page(inode, page);
787 nfs_release_request(req);
788 }
789 return (status < 0) ? status : 0;
790}
791
792/*
793 * Update and possibly write a cached page of an NFS file.
794 *
795 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
796 * things with a page scheduled for an RPC call (e.g. invalidate it).
797 */
798int nfs_updatepage(struct file *file, struct page *page,
799 unsigned int offset, unsigned int count)
800{
801 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 struct inode *inode = page->mapping->host;
803 struct nfs_page *req;
804 int status = 0;
805
Chuck Lever91d5b472006-03-20 13:44:14 -0500806 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500809 file->f_dentry->d_parent->d_name.name,
810 file->f_dentry->d_name.name, count,
811 (long long)(page_offset(page) +offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 if (IS_SYNC(inode)) {
814 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
815 if (status > 0) {
816 if (offset == 0 && status == PAGE_CACHE_SIZE)
817 SetPageUptodate(page);
818 return 0;
819 }
820 return status;
821 }
822
823 /* If we're not using byte range locks, and we know the page
824 * is entirely in cache, it may be more efficient to avoid
825 * fragmenting write requests.
826 */
Trond Myklebustab0a3db2005-06-22 17:16:30 +0000827 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 loff_t end_offs = i_size_read(inode) - 1;
829 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
830
831 count += offset;
832 offset = 0;
833 if (unlikely(end_offs < 0)) {
834 /* Do nothing */
835 } else if (page->index == end_index) {
836 unsigned int pglen;
837 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
838 if (count < pglen)
839 count = pglen;
840 } else if (page->index < end_index)
841 count = PAGE_CACHE_SIZE;
842 }
843
844 /*
845 * Try to find an NFS request corresponding to this page
846 * and update it.
847 * If the existing request cannot be updated, we must flush
848 * it out now.
849 */
850 do {
851 req = nfs_update_request(ctx, inode, page, offset, count);
852 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
853 if (status != -EBUSY)
854 break;
855 /* Request could not be updated. Flush it out and try again */
856 status = nfs_wb_page(inode, page);
857 } while (status >= 0);
858 if (status < 0)
859 goto done;
860
861 status = 0;
862
863 /* Update file length */
864 nfs_grow_file(page, offset, count);
865 /* Set the PG_uptodate flag? */
866 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
867 nfs_unlock_request(req);
868done:
869 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
870 status, (long long)i_size_read(inode));
871 if (status < 0)
872 ClearPageUptodate(page);
873 return status;
874}
875
876static void nfs_writepage_release(struct nfs_page *req)
877{
878 end_page_writeback(req->wb_page);
879
880#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
881 if (!PageError(req->wb_page)) {
882 if (NFS_NEED_RESCHED(req)) {
883 nfs_mark_request_dirty(req);
884 goto out;
885 } else if (NFS_NEED_COMMIT(req)) {
886 nfs_mark_request_commit(req);
887 goto out;
888 }
889 }
890 nfs_inode_remove_request(req);
891
892out:
893 nfs_clear_commit(req);
894 nfs_clear_reschedule(req);
895#else
896 nfs_inode_remove_request(req);
897#endif
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000898 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
901static inline int flush_task_priority(int how)
902{
903 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
904 case FLUSH_HIGHPRI:
905 return RPC_PRIORITY_HIGH;
906 case FLUSH_LOWPRI:
907 return RPC_PRIORITY_LOW;
908 }
909 return RPC_PRIORITY_NORMAL;
910}
911
912/*
913 * Set up the argument/result storage required for the RPC call.
914 */
915static void nfs_write_rpcsetup(struct nfs_page *req,
916 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500917 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 unsigned int count, unsigned int offset,
919 int how)
920{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -0500922 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 /* Set up the RPC argument and reply structs
925 * NB: take care not to mess about with data->commit et al. */
926
927 data->req = req;
928 data->inode = inode = req->wb_context->dentry->d_inode;
929 data->cred = req->wb_context->cred;
930
931 data->args.fh = NFS_FH(inode);
932 data->args.offset = req_offset(req) + offset;
933 data->args.pgbase = req->wb_pgbase + offset;
934 data->args.pages = data->pagevec;
935 data->args.count = count;
936 data->args.context = req->wb_context;
937
938 data->res.fattr = &data->fattr;
939 data->res.count = count;
940 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400941 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Trond Myklebust788e7a82006-03-20 13:44:27 -0500943 /* Set up the initial task struct. */
944 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
945 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 NFS_PROTO(inode)->write_setup(data, how);
947
948 data->task.tk_priority = flush_task_priority(how);
949 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500952 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 inode->i_sb->s_id,
954 (long long)NFS_FILEID(inode),
955 count,
956 (unsigned long long)data->args.offset);
957}
958
959static void nfs_execute_write(struct nfs_write_data *data)
960{
961 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
962 sigset_t oldset;
963
964 rpc_clnt_sigmask(clnt, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 rpc_execute(&data->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 rpc_clnt_sigunmask(clnt, &oldset);
967}
968
969/*
970 * Generate multiple small requests to write out a single
971 * contiguous dirty area on one page.
972 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500973static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
975 struct nfs_page *req = nfs_list_entry(head->next);
976 struct page *page = req->wb_page;
977 struct nfs_write_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700978 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
979 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 int requests = 0;
981 LIST_HEAD(list);
982
983 nfs_list_remove_request(req);
984
985 nbytes = req->wb_bytes;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700986 do {
987 size_t len = min(nbytes, wsize);
988
989 data = nfs_writedata_alloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (!data)
991 goto out_bad;
992 list_add(&data->pages, &list);
993 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700994 nbytes -= len;
995 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 atomic_set(&req->wb_complete, requests);
997
998 ClearPageError(page);
Trond Myklebustbb713d62005-12-03 15:20:14 -0500999 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 offset = 0;
1001 nbytes = req->wb_bytes;
1002 do {
1003 data = list_entry(list.next, struct nfs_write_data, pages);
1004 list_del_init(&data->pages);
1005
1006 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 if (nbytes > wsize) {
Trond Myklebust788e7a82006-03-20 13:44:27 -05001009 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1010 wsize, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 offset += wsize;
1012 nbytes -= wsize;
1013 } else {
Trond Myklebust788e7a82006-03-20 13:44:27 -05001014 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1015 nbytes, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 nbytes = 0;
1017 }
1018 nfs_execute_write(data);
1019 } while (nbytes != 0);
1020
1021 return 0;
1022
1023out_bad:
1024 while (!list_empty(&list)) {
1025 data = list_entry(list.next, struct nfs_write_data, pages);
1026 list_del(&data->pages);
Trond Myklebust8aca67f2006-11-13 16:23:44 -05001027 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001030 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return -ENOMEM;
1032}
1033
1034/*
1035 * Create an RPC task for the given write request and kick it.
1036 * The page must have been locked by the caller.
1037 *
1038 * It may happen that the page we're passed is not marked dirty.
1039 * This is the case if nfs_updatepage detects a conflicting request
1040 * that has been written but not committed.
1041 */
Trond Myklebust7d46a492006-03-20 13:44:50 -05001042static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 struct nfs_page *req;
1045 struct page **pages;
1046 struct nfs_write_data *data;
1047 unsigned int count;
1048
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001049 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (!data)
1051 goto out_bad;
1052
1053 pages = data->pagevec;
1054 count = 0;
1055 while (!list_empty(head)) {
1056 req = nfs_list_entry(head->next);
1057 nfs_list_remove_request(req);
1058 nfs_list_add_request(req, &data->pages);
1059 ClearPageError(req->wb_page);
Trond Myklebustbb713d62005-12-03 15:20:14 -05001060 set_page_writeback(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 *pages++ = req->wb_page;
1062 count += req->wb_bytes;
1063 }
1064 req = nfs_list_entry(data->pages.next);
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 /* Set up the argument struct */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001067 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 nfs_execute_write(data);
1070 return 0;
1071 out_bad:
1072 while (!list_empty(head)) {
1073 struct nfs_page *req = nfs_list_entry(head->next);
1074 nfs_list_remove_request(req);
1075 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001076 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078 return -ENOMEM;
1079}
1080
Trond Myklebust7d46a492006-03-20 13:44:50 -05001081static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 LIST_HEAD(one_request);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001084 int (*flush_one)(struct inode *, struct list_head *, int);
1085 struct nfs_page *req;
1086 int wpages = NFS_SERVER(inode)->wpages;
1087 int wsize = NFS_SERVER(inode)->wsize;
1088 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Trond Myklebust7d46a492006-03-20 13:44:50 -05001090 flush_one = nfs_flush_one;
1091 if (wsize < PAGE_CACHE_SIZE)
1092 flush_one = nfs_flush_multi;
1093 /* For single writes, FLUSH_STABLE is more efficient */
1094 if (npages <= wpages && npages == NFS_I(inode)->npages
1095 && nfs_list_entry(head->next)->wb_bytes <= wsize)
1096 how |= FLUSH_STABLE;
1097
1098 do {
1099 nfs_coalesce_requests(head, &one_request, wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 req = nfs_list_entry(one_request.next);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001101 error = flush_one(inode, &one_request, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (error < 0)
Trond Myklebust7d46a492006-03-20 13:44:50 -05001103 goto out_err;
1104 } while (!list_empty(head));
1105 return 0;
1106out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 while (!list_empty(head)) {
1108 req = nfs_list_entry(head->next);
1109 nfs_list_remove_request(req);
1110 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001111 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 }
1113 return error;
1114}
1115
1116/*
1117 * Handle a write reply that flushed part of a page.
1118 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001119static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001121 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 struct nfs_page *req = data->req;
1123 struct page *page = req->wb_page;
1124
1125 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1126 req->wb_context->dentry->d_inode->i_sb->s_id,
1127 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1128 req->wb_bytes,
1129 (long long)req_offset(req));
1130
Trond Myklebust788e7a82006-03-20 13:44:27 -05001131 if (nfs_writeback_done(task, data) != 0)
1132 return;
1133
1134 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 ClearPageUptodate(page);
1136 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001137 req->wb_context->error = task->tk_status;
1138 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 } else {
1140#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1141 if (data->verf.committed < NFS_FILE_SYNC) {
1142 if (!NFS_NEED_COMMIT(req)) {
1143 nfs_defer_commit(req);
1144 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1145 dprintk(" defer commit\n");
1146 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1147 nfs_defer_reschedule(req);
1148 dprintk(" server reboot detected\n");
1149 }
1150 } else
1151#endif
1152 dprintk(" OK\n");
1153 }
1154
1155 if (atomic_dec_and_test(&req->wb_complete))
1156 nfs_writepage_release(req);
1157}
1158
Trond Myklebust788e7a82006-03-20 13:44:27 -05001159static const struct rpc_call_ops nfs_write_partial_ops = {
1160 .rpc_call_done = nfs_writeback_done_partial,
1161 .rpc_release = nfs_writedata_release,
1162};
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164/*
1165 * Handle a write reply that flushes a whole page.
1166 *
1167 * FIXME: There is an inherent race with invalidate_inode_pages and
1168 * writebacks since the page->count is kept > 1 for as long
1169 * as the page has a write request pending.
1170 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001171static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001173 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 struct nfs_page *req;
1175 struct page *page;
1176
Trond Myklebust788e7a82006-03-20 13:44:27 -05001177 if (nfs_writeback_done(task, data) != 0)
1178 return;
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 /* Update attributes as result of writeback. */
1181 while (!list_empty(&data->pages)) {
1182 req = nfs_list_entry(data->pages.next);
1183 nfs_list_remove_request(req);
1184 page = req->wb_page;
1185
1186 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1187 req->wb_context->dentry->d_inode->i_sb->s_id,
1188 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1189 req->wb_bytes,
1190 (long long)req_offset(req));
1191
Trond Myklebust788e7a82006-03-20 13:44:27 -05001192 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 ClearPageUptodate(page);
1194 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001195 req->wb_context->error = task->tk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 end_page_writeback(page);
1197 nfs_inode_remove_request(req);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001198 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 goto next;
1200 }
1201 end_page_writeback(page);
1202
1203#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1204 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1205 nfs_inode_remove_request(req);
1206 dprintk(" OK\n");
1207 goto next;
1208 }
1209 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1210 nfs_mark_request_commit(req);
1211 dprintk(" marked for commit\n");
1212#else
1213 nfs_inode_remove_request(req);
1214#endif
1215 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001216 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 }
1218}
1219
Trond Myklebust788e7a82006-03-20 13:44:27 -05001220static const struct rpc_call_ops nfs_write_full_ops = {
1221 .rpc_call_done = nfs_writeback_done_full,
1222 .rpc_release = nfs_writedata_release,
1223};
1224
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226/*
1227 * This function is called when the WRITE call is complete.
1228 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001229int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 struct nfs_writeargs *argp = &data->args;
1232 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001233 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1236 task->tk_pid, task->tk_status);
1237
Chuck Leverf551e442006-09-20 14:33:04 -04001238 /*
1239 * ->write_done will attempt to use post-op attributes to detect
1240 * conflicting writes by other clients. A strict interpretation
1241 * of close-to-open would allow us to continue caching even if
1242 * another writer had changed the file, but some applications
1243 * depend on tighter cache coherency when writing.
1244 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001245 status = NFS_PROTO(data->inode)->write_done(task, data);
1246 if (status != 0)
1247 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001248 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1251 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1252 /* We tried a write call, but the server did not
1253 * commit data to stable storage even though we
1254 * requested it.
1255 * Note: There is a known bug in Tru64 < 5.0 in which
1256 * the server reports NFS_DATA_SYNC, but performs
1257 * NFS_FILE_SYNC. We therefore implement this checking
1258 * as a dprintk() in order to avoid filling syslog.
1259 */
1260 static unsigned long complain;
1261
1262 if (time_before(complain, jiffies)) {
1263 dprintk("NFS: faulty NFS server %s:"
1264 " (committed = %d) != (stable = %d)\n",
David Howells54ceac42006-08-22 20:06:13 -04001265 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 resp->verf->committed, argp->stable);
1267 complain = jiffies + 300 * HZ;
1268 }
1269 }
1270#endif
1271 /* Is this a short write? */
1272 if (task->tk_status >= 0 && resp->count < argp->count) {
1273 static unsigned long complain;
1274
Chuck Lever91d5b472006-03-20 13:44:14 -05001275 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /* Has the server at least made some progress? */
1278 if (resp->count != 0) {
1279 /* Was this an NFSv2 write or an NFSv3 stable write? */
1280 if (resp->verf->committed != NFS_UNSTABLE) {
1281 /* Resend from where the server left off */
1282 argp->offset += resp->count;
1283 argp->pgbase += resp->count;
1284 argp->count -= resp->count;
1285 } else {
1286 /* Resend as a stable write in order to avoid
1287 * headaches in the case of a server crash.
1288 */
1289 argp->stable = NFS_FILE_SYNC;
1290 }
1291 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001292 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294 if (time_before(complain, jiffies)) {
1295 printk(KERN_WARNING
1296 "NFS: Server wrote zero bytes, expected %u.\n",
1297 argp->count);
1298 complain = jiffies + 300 * HZ;
1299 }
1300 /* Can't do anything about it except throw an error. */
1301 task->tk_status = -EIO;
1302 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
1305
1306
1307#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001308void nfs_commit_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 nfs_commit_free(wdata);
1311}
1312
1313/*
1314 * Set up the argument/result storage required for the RPC call.
1315 */
1316static void nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001317 struct nfs_write_data *data,
1318 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001320 struct nfs_page *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001322 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 /* Set up the RPC argument and reply structs
1325 * NB: take care not to mess about with data->commit et al. */
1326
1327 list_splice_init(head, &data->pages);
1328 first = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 inode = first->wb_context->dentry->d_inode;
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 data->inode = inode;
1332 data->cred = first->wb_context->cred;
1333
1334 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001335 /* Note: we always request a commit of the entire inode */
1336 data->args.offset = 0;
1337 data->args.count = 0;
1338 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 data->res.fattr = &data->fattr;
1340 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001341 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001342
1343 /* Set up the initial task struct. */
1344 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1345 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 NFS_PROTO(inode)->commit_setup(data, how);
1347
1348 data->task.tk_priority = flush_task_priority(how);
1349 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Chuck Lever0bbacc42005-11-01 16:53:32 -05001351 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352}
1353
1354/*
1355 * Commit dirty pages
1356 */
1357static int
Chuck Lever40859d72005-11-30 18:09:02 -05001358nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
1360 struct nfs_write_data *data;
1361 struct nfs_page *req;
1362
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001363 data = nfs_commit_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 if (!data)
1366 goto out_bad;
1367
1368 /* Set up the argument struct */
1369 nfs_commit_rpcsetup(head, data, how);
1370
1371 nfs_execute_write(data);
1372 return 0;
1373 out_bad:
1374 while (!list_empty(head)) {
1375 req = nfs_list_entry(head->next);
1376 nfs_list_remove_request(req);
1377 nfs_mark_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001378 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust5c2d97c2006-09-18 23:20:35 -04001379 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
1381 return -ENOMEM;
1382}
1383
1384/*
1385 * COMMIT call returned
1386 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001387static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001389 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1393 task->tk_pid, task->tk_status);
1394
Trond Myklebust788e7a82006-03-20 13:44:27 -05001395 /* Call the NFS version-specific code */
1396 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1397 return;
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 while (!list_empty(&data->pages)) {
1400 req = nfs_list_entry(data->pages.next);
1401 nfs_list_remove_request(req);
Christoph Lameterfd39fc82006-06-30 01:55:40 -07001402 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1405 req->wb_context->dentry->d_inode->i_sb->s_id,
1406 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1407 req->wb_bytes,
1408 (long long)req_offset(req));
1409 if (task->tk_status < 0) {
1410 req->wb_context->error = task->tk_status;
1411 nfs_inode_remove_request(req);
1412 dprintk(", error = %d\n", task->tk_status);
1413 goto next;
1414 }
1415
1416 /* Okay, COMMIT succeeded, apparently. Check the verifier
1417 * returned by the server against all stored verfs. */
1418 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1419 /* We have a match */
1420 nfs_inode_remove_request(req);
1421 dprintk(" OK\n");
1422 goto next;
1423 }
1424 /* We have a mismatch. Write the page again */
1425 dprintk(" mismatch\n");
1426 nfs_mark_request_dirty(req);
1427 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001428 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001431
1432static const struct rpc_call_ops nfs_commit_ops = {
1433 .rpc_call_done = nfs_commit_done,
1434 .rpc_release = nfs_commit_release,
1435};
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001436#else
1437static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1438{
1439 return 0;
1440}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441#endif
1442
Trond Myklebust3f442542006-09-17 14:46:44 -04001443static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
Trond Myklebust28c69252006-09-16 13:04:50 -04001445 struct nfs_inode *nfsi = NFS_I(mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 LIST_HEAD(head);
Trond Myklebust3f442542006-09-17 14:46:44 -04001447 long res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 spin_lock(&nfsi->req_lock);
Trond Myklebust3f442542006-09-17 14:46:44 -04001450 res = nfs_scan_dirty(mapping, wbc, &head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 spin_unlock(&nfsi->req_lock);
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001452 if (res) {
Trond Myklebust28c69252006-09-16 13:04:50 -04001453 int error = nfs_flush_list(mapping->host, &head, res, how);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001454 if (error < 0)
1455 return error;
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return res;
1458}
1459
1460#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001461int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
1463 struct nfs_inode *nfsi = NFS_I(inode);
1464 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001465 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001468 res = nfs_scan_commit(inode, &head, 0, 0);
1469 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001471 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001472 if (error < 0)
1473 return error;
1474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return res;
1476}
1477#endif
1478
Trond Myklebust3f442542006-09-17 14:46:44 -04001479long nfs_sync_inode_wait(struct inode *inode, unsigned long idx_start,
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001480 unsigned int npages, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001482 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3f442542006-09-17 14:46:44 -04001483 struct address_space *mapping = inode->i_mapping;
1484 struct writeback_control wbc = {
1485 .bdi = mapping->backing_dev_info,
1486 .sync_mode = WB_SYNC_ALL,
1487 .nr_to_write = LONG_MAX,
1488 .range_start = ((loff_t)idx_start) << PAGE_CACHE_SHIFT,
1489 .range_end = ((loff_t)(idx_start + npages - 1)) << PAGE_CACHE_SHIFT,
1490 };
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001491 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001492 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebust3f442542006-09-17 14:46:44 -04001493 long pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001495 how &= ~FLUSH_NOCOMMIT;
1496 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 do {
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001498 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1499 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001500 continue;
Trond Myklebust3f442542006-09-17 14:46:44 -04001501 pages = nfs_scan_dirty(mapping, &wbc, &head);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001502 if (pages != 0) {
1503 spin_unlock(&nfsi->req_lock);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001504 if (how & FLUSH_INVALIDATE) {
Trond Myklebust83715ad2006-07-05 13:17:12 -04001505 nfs_cancel_dirty_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001506 ret = pages;
1507 } else
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001508 ret = nfs_flush_list(inode, &head, pages, how);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001509 spin_lock(&nfsi->req_lock);
1510 continue;
1511 }
1512 if (nocommit)
1513 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001514 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001515 if (pages == 0)
1516 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001517 if (how & FLUSH_INVALIDATE) {
1518 spin_unlock(&nfsi->req_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001519 nfs_cancel_commit_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001520 ret = pages;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001521 spin_lock(&nfsi->req_lock);
1522 continue;
1523 }
1524 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001525 spin_unlock(&nfsi->req_lock);
1526 ret = nfs_commit_list(inode, &head, how);
1527 spin_lock(&nfsi->req_lock);
1528 } while (ret >= 0);
1529 spin_unlock(&nfsi->req_lock);
1530 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
1532
David Howellsf7b422b2006-06-09 09:34:33 -04001533int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
1535 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1536 sizeof(struct nfs_write_data),
1537 0, SLAB_HWCACHE_ALIGN,
1538 NULL, NULL);
1539 if (nfs_wdata_cachep == NULL)
1540 return -ENOMEM;
1541
Matthew Dobson93d23412006-03-26 01:37:50 -08001542 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1543 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if (nfs_wdata_mempool == NULL)
1545 return -ENOMEM;
1546
Matthew Dobson93d23412006-03-26 01:37:50 -08001547 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1548 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 if (nfs_commit_mempool == NULL)
1550 return -ENOMEM;
1551
1552 return 0;
1553}
1554
David Brownell266bee82006-06-27 12:59:15 -07001555void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
1557 mempool_destroy(nfs_commit_mempool);
1558 mempool_destroy(nfs_wdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001559 kmem_cache_destroy(nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561