blob: 6df8319de060f7ba020f24c21b6dc485cbd3948d [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 Myklebust1c759502006-10-09 16:18:38 -040083static int nfs_wb_page_priority(struct inode *inode, struct page *page, 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
Trond Myklebust277459d2006-12-05 00:35:35 -0500158static struct nfs_page *nfs_page_find_request_locked(struct page *page)
159{
160 struct nfs_page *req = NULL;
161
162 if (PagePrivate(page)) {
163 req = (struct nfs_page *)page_private(page);
164 if (req != NULL)
165 atomic_inc(&req->wb_count);
166 }
167 return req;
168}
169
170static struct nfs_page *nfs_page_find_request(struct page *page)
171{
172 struct nfs_page *req = NULL;
173 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
174
175 spin_lock(req_lock);
176 req = nfs_page_find_request_locked(page);
177 spin_unlock(req_lock);
178 return req;
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/* Adjust the file length if we're writing beyond the end */
182static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
183{
184 struct inode *inode = page->mapping->host;
185 loff_t end, i_size = i_size_read(inode);
186 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
187
188 if (i_size > 0 && page->index < end_index)
189 return;
190 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
191 if (i_size >= end)
192 return;
Chuck Lever91d5b472006-03-20 13:44:14 -0500193 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 i_size_write(inode, end);
195}
196
197/* We can set the PG_uptodate flag if we see that a write request
198 * covers the full page.
199 */
200static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
201{
202 loff_t end_offs;
203
204 if (PageUptodate(page))
205 return;
206 if (base != 0)
207 return;
208 if (count == PAGE_CACHE_SIZE) {
209 SetPageUptodate(page);
210 return;
211 }
212
213 end_offs = i_size_read(page->mapping->host) - 1;
214 if (end_offs < 0)
215 return;
216 /* Is this the last page? */
217 if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT))
218 return;
219 /* This is the last page: set PG_uptodate if we cover the entire
220 * extent of the data, then zero the rest of the page.
221 */
222 if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) {
223 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
224 SetPageUptodate(page);
225 }
226}
227
228/*
229 * Write a page synchronously.
230 * Offset is the data offset within the page.
231 */
232static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
233 struct page *page, unsigned int offset, unsigned int count,
234 int how)
235{
236 unsigned int wsize = NFS_SERVER(inode)->wsize;
237 int result, written = 0;
238 struct nfs_write_data *wdata;
239
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700240 wdata = nfs_writedata_alloc(wsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (!wdata)
242 return -ENOMEM;
243
244 wdata->flags = how;
245 wdata->cred = ctx->cred;
246 wdata->inode = inode;
247 wdata->args.fh = NFS_FH(inode);
248 wdata->args.context = ctx;
249 wdata->args.pages = &page;
250 wdata->args.stable = NFS_FILE_SYNC;
251 wdata->args.pgbase = offset;
252 wdata->args.count = wsize;
253 wdata->res.fattr = &wdata->fattr;
254 wdata->res.verf = &wdata->verf;
255
256 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
257 inode->i_sb->s_id,
258 (long long)NFS_FILEID(inode),
259 count, (long long)(page_offset(page) + offset));
260
Trond Myklebustbb713d62005-12-03 15:20:14 -0500261 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 nfs_begin_data_update(inode);
263 do {
264 if (count < wsize)
265 wdata->args.count = count;
266 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
267
268 result = NFS_PROTO(inode)->write(wdata);
269
270 if (result < 0) {
271 /* Must mark the page invalid after I/O error */
272 ClearPageUptodate(page);
273 goto io_error;
274 }
275 if (result < wdata->args.count)
276 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
277 wdata->args.count, result);
278
279 wdata->args.offset += result;
280 wdata->args.pgbase += result;
281 written += result;
282 count -= result;
Chuck Lever91d5b472006-03-20 13:44:14 -0500283 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 } while (count);
285 /* Update file length */
286 nfs_grow_file(page, offset, written);
287 /* Set the PG_uptodate flag? */
288 nfs_mark_uptodate(page, offset, written);
289
290 if (PageError(page))
291 ClearPageError(page);
292
293io_error:
Trond Myklebust951a1432005-06-22 17:16:30 +0000294 nfs_end_data_update(inode);
Trond Myklebustbb713d62005-12-03 15:20:14 -0500295 end_page_writeback(page);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500296 nfs_writedata_release(wdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return written ? written : result;
298}
299
300static int nfs_writepage_async(struct nfs_open_context *ctx,
301 struct inode *inode, struct page *page,
302 unsigned int offset, unsigned int count)
303{
304 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 req = nfs_update_request(ctx, inode, page, offset, count);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100307 if (IS_ERR(req))
308 return PTR_ERR(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /* Update file length */
310 nfs_grow_file(page, offset, count);
311 /* Set the PG_uptodate flag? */
312 nfs_mark_uptodate(page, offset, count);
313 nfs_unlock_request(req);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
317static int wb_priority(struct writeback_control *wbc)
318{
319 if (wbc->for_reclaim)
320 return FLUSH_HIGHPRI;
321 if (wbc->for_kupdate)
322 return FLUSH_LOWPRI;
323 return 0;
324}
325
326/*
327 * Write an mmapped page to the server.
328 */
329int nfs_writepage(struct page *page, struct writeback_control *wbc)
330{
331 struct nfs_open_context *ctx;
332 struct inode *inode = page->mapping->host;
333 unsigned long end_index;
334 unsigned offset = PAGE_CACHE_SIZE;
335 loff_t i_size = i_size_read(inode);
336 int inode_referenced = 0;
337 int priority = wb_priority(wbc);
338 int err;
339
Chuck Lever91d5b472006-03-20 13:44:14 -0500340 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
341 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 /*
344 * Note: We need to ensure that we have a reference to the inode
345 * if we are to do asynchronous writes. If not, waiting
346 * in nfs_wait_on_request() may deadlock with clear_inode().
347 *
348 * If igrab() fails here, then it is in any case safe to
349 * call nfs_wb_page(), since there will be no pending writes.
350 */
351 if (igrab(inode) != 0)
352 inode_referenced = 1;
353 end_index = i_size >> PAGE_CACHE_SHIFT;
354
355 /* Ensure we've flushed out any previous writes */
356 nfs_wb_page_priority(inode, page, priority);
357
358 /* easy case */
359 if (page->index < end_index)
360 goto do_it;
361 /* things got complicated... */
362 offset = i_size & (PAGE_CACHE_SIZE-1);
363
364 /* OK, are we completely out? */
365 err = 0; /* potential race with truncate - ignore */
366 if (page->index >= end_index+1 || !offset)
367 goto out;
368do_it:
Trond Myklebustd5308382005-11-04 15:33:38 -0500369 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (ctx == NULL) {
371 err = -EBADF;
372 goto out;
373 }
374 lock_kernel();
375 if (!IS_SYNC(inode) && inode_referenced) {
376 err = nfs_writepage_async(ctx, inode, page, 0, offset);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100377 if (!wbc->for_writepages)
Trond Myklebust28c69252006-09-16 13:04:50 -0400378 nfs_flush_mapping(page->mapping, wbc, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 } else {
380 err = nfs_writepage_sync(ctx, inode, page, 0,
381 offset, priority);
382 if (err >= 0) {
383 if (err != offset)
384 redirty_page_for_writepage(wbc, page);
385 err = 0;
386 }
387 }
388 unlock_kernel();
389 put_nfs_open_context(ctx);
390out:
391 unlock_page(page);
392 if (inode_referenced)
393 iput(inode);
394 return err;
395}
396
397/*
398 * Note: causes nfs_update_request() to block on the assumption
399 * that the writeback is generated due to memory pressure.
400 */
401int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
402{
403 struct backing_dev_info *bdi = mapping->backing_dev_info;
404 struct inode *inode = mapping->host;
405 int err;
406
Chuck Lever91d5b472006-03-20 13:44:14 -0500407 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 err = generic_writepages(mapping, wbc);
410 if (err)
411 return err;
412 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
413 if (wbc->nonblocking)
414 return 0;
415 nfs_wait_on_write_congestion(mapping, 0);
416 }
Trond Myklebust28c69252006-09-16 13:04:50 -0400417 err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (err < 0)
419 goto out;
Chuck Lever91d5b472006-03-20 13:44:14 -0500420 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
422 err = nfs_wait_on_requests(inode, 0, 0);
423 if (err < 0)
424 goto out;
425 }
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000426 err = nfs_commit_inode(inode, wb_priority(wbc));
Trond Myklebust3f442542006-09-17 14:46:44 -0400427 if (err > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429out:
430 clear_bit(BDI_write_congested, &bdi->state);
431 wake_up_all(&nfs_write_congestion);
Andrew Morton3fcfab12006-10-19 23:28:16 -0700432 congestion_end(WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return err;
434}
435
436/*
437 * Insert a write request into an inode
438 */
439static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
440{
441 struct nfs_inode *nfsi = NFS_I(inode);
442 int error;
443
444 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
445 BUG_ON(error == -EEXIST);
446 if (error)
447 return error;
448 if (!nfsi->npages) {
449 igrab(inode);
450 nfs_begin_data_update(inode);
451 if (nfs_have_delegation(inode, FMODE_WRITE))
452 nfsi->change_attr++;
453 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500454 SetPagePrivate(req->wb_page);
Trond Myklebust277459d2006-12-05 00:35:35 -0500455 set_page_private(req->wb_page, (unsigned long)req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 nfsi->npages++;
457 atomic_inc(&req->wb_count);
458 return 0;
459}
460
461/*
462 * Insert a write request into an inode
463 */
464static void nfs_inode_remove_request(struct nfs_page *req)
465{
466 struct inode *inode = req->wb_context->dentry->d_inode;
467 struct nfs_inode *nfsi = NFS_I(inode);
468
469 BUG_ON (!NFS_WBACK_BUSY(req));
470
471 spin_lock(&nfsi->req_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500472 set_page_private(req->wb_page, 0);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500473 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
475 nfsi->npages--;
476 if (!nfsi->npages) {
477 spin_unlock(&nfsi->req_lock);
Trond Myklebust951a1432005-06-22 17:16:30 +0000478 nfs_end_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 iput(inode);
480 } else
481 spin_unlock(&nfsi->req_lock);
482 nfs_clear_request(req);
483 nfs_release_request(req);
484}
485
486/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 * Add a request to the inode's dirty list.
488 */
489static void
490nfs_mark_request_dirty(struct nfs_page *req)
491{
492 struct inode *inode = req->wb_context->dentry->d_inode;
493 struct nfs_inode *nfsi = NFS_I(inode);
494
495 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000496 radix_tree_tag_set(&nfsi->nfs_page_tree,
497 req->wb_index, NFS_PAGE_TAG_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 nfs_list_add_request(req, &nfsi->dirty);
499 nfsi->ndirty++;
500 spin_unlock(&nfsi->req_lock);
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700501 inc_zone_page_state(req->wb_page, NR_FILE_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 mark_inode_dirty(inode);
503}
504
505/*
506 * Check if a request is dirty
507 */
508static inline int
509nfs_dirty_request(struct nfs_page *req)
510{
511 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
512 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
513}
514
515#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
516/*
517 * Add a request to the inode's commit list.
518 */
519static void
520nfs_mark_request_commit(struct nfs_page *req)
521{
522 struct inode *inode = req->wb_context->dentry->d_inode;
523 struct nfs_inode *nfsi = NFS_I(inode);
524
525 spin_lock(&nfsi->req_lock);
526 nfs_list_add_request(req, &nfsi->commit);
527 nfsi->ncommit++;
528 spin_unlock(&nfsi->req_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700529 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 mark_inode_dirty(inode);
531}
532#endif
533
534/*
535 * Wait for a request to complete.
536 *
537 * Interruptible by signals only if mounted with intr flag.
538 */
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500539static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 struct nfs_inode *nfsi = NFS_I(inode);
542 struct nfs_page *req;
543 unsigned long idx_end, next;
544 unsigned int res = 0;
545 int error;
546
547 if (npages == 0)
548 idx_end = ~0;
549 else
550 idx_end = idx_start + npages - 1;
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 next = idx_start;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000553 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 -0700554 if (req->wb_index > idx_end)
555 break;
556
557 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000558 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 atomic_inc(&req->wb_count);
561 spin_unlock(&nfsi->req_lock);
562 error = nfs_wait_on_request(req);
563 nfs_release_request(req);
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500564 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (error < 0)
566 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 res++;
568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return res;
570}
571
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500572static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
573{
574 struct nfs_inode *nfsi = NFS_I(inode);
575 int ret;
576
577 spin_lock(&nfsi->req_lock);
578 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
579 spin_unlock(&nfsi->req_lock);
580 return ret;
581}
582
Trond Myklebust83715ad2006-07-05 13:17:12 -0400583static void nfs_cancel_dirty_list(struct list_head *head)
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400584{
585 struct nfs_page *req;
586 while(!list_empty(head)) {
587 req = nfs_list_entry(head->next);
588 nfs_list_remove_request(req);
589 nfs_inode_remove_request(req);
590 nfs_clear_page_writeback(req);
591 }
592}
593
Trond Myklebust83715ad2006-07-05 13:17:12 -0400594static void nfs_cancel_commit_list(struct list_head *head)
595{
596 struct nfs_page *req;
597
598 while(!list_empty(head)) {
599 req = nfs_list_entry(head->next);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700600 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400601 nfs_list_remove_request(req);
602 nfs_inode_remove_request(req);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700603 nfs_unlock_request(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400604 }
605}
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
608/*
609 * nfs_scan_commit - Scan an inode for commit requests
610 * @inode: NFS inode to scan
611 * @dst: destination list
612 * @idx_start: lower bound of page->index to scan.
613 * @npages: idx_start + npages sets the upper bound to scan.
614 *
615 * Moves requests from the inode's 'commit' request list.
616 * The requests are *not* checked to ensure that they form a contiguous set.
617 */
618static int
619nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
620{
621 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000622 int res = 0;
623
624 if (nfsi->ncommit != 0) {
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400625 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000626 nfsi->ncommit -= res;
627 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
628 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return res;
631}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500632#else
633static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
634{
635 return 0;
636}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637#endif
638
639static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
640{
641 struct backing_dev_info *bdi = mapping->backing_dev_info;
642 DEFINE_WAIT(wait);
643 int ret = 0;
644
645 might_sleep();
646
647 if (!bdi_write_congested(bdi))
648 return 0;
Chuck Lever91d5b472006-03-20 13:44:14 -0500649
650 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (intr) {
653 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
654 sigset_t oldset;
655
656 rpc_clnt_sigmask(clnt, &oldset);
657 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
658 if (bdi_write_congested(bdi)) {
659 if (signalled())
660 ret = -ERESTARTSYS;
661 else
662 schedule();
663 }
664 rpc_clnt_sigunmask(clnt, &oldset);
665 } else {
666 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
667 if (bdi_write_congested(bdi))
668 schedule();
669 }
670 finish_wait(&nfs_write_congestion, &wait);
671 return ret;
672}
673
674
675/*
676 * Try to update any existing write request, or create one if there is none.
677 * In order to match, the request's credentials must match those of
678 * the calling process.
679 *
680 * Note: Should always be called with the Page Lock held!
681 */
682static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
683 struct inode *inode, struct page *page,
684 unsigned int offset, unsigned int bytes)
685{
686 struct nfs_server *server = NFS_SERVER(inode);
687 struct nfs_inode *nfsi = NFS_I(inode);
688 struct nfs_page *req, *new = NULL;
689 unsigned long rqend, end;
690
691 end = offset + bytes;
692
693 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
694 return ERR_PTR(-ERESTARTSYS);
695 for (;;) {
696 /* Loop over all inode entries and see if we find
697 * A request for the page we wish to update
698 */
699 spin_lock(&nfsi->req_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500700 req = nfs_page_find_request_locked(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (req) {
702 if (!nfs_lock_request_dontget(req)) {
703 int error;
Trond Myklebust277459d2006-12-05 00:35:35 -0500704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 struct nfs_page *req;
773 int status = 0;
774 /*
775 * Look for a request corresponding to this page. If there
776 * is one, and it belongs to another file, we flush it out
777 * before we try to copy anything into the page. Do this
778 * due to the lack of an ACCESS-type call in NFSv2.
779 * Also do the same if we find a request from an existing
780 * dropped page.
781 */
Trond Myklebust277459d2006-12-05 00:35:35 -0500782 req = nfs_page_find_request(page);
783 if (req != NULL) {
784 int do_flush = req->wb_page != page || req->wb_context != ctx;
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 nfs_release_request(req);
Trond Myklebust277459d2006-12-05 00:35:35 -0500787 if (do_flush)
788 status = nfs_wb_page(page->mapping->host, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790 return (status < 0) ? status : 0;
791}
792
793/*
794 * Update and possibly write a cached page of an NFS file.
795 *
796 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
797 * things with a page scheduled for an RPC call (e.g. invalidate it).
798 */
799int nfs_updatepage(struct file *file, struct page *page,
800 unsigned int offset, unsigned int count)
801{
802 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 struct inode *inode = page->mapping->host;
804 struct nfs_page *req;
805 int status = 0;
806
Chuck Lever91d5b472006-03-20 13:44:14 -0500807 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500810 file->f_dentry->d_parent->d_name.name,
811 file->f_dentry->d_name.name, count,
812 (long long)(page_offset(page) +offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 if (IS_SYNC(inode)) {
815 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
816 if (status > 0) {
817 if (offset == 0 && status == PAGE_CACHE_SIZE)
818 SetPageUptodate(page);
819 return 0;
820 }
821 return status;
822 }
823
824 /* If we're not using byte range locks, and we know the page
825 * is entirely in cache, it may be more efficient to avoid
826 * fragmenting write requests.
827 */
Trond Myklebustab0a3db2005-06-22 17:16:30 +0000828 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 loff_t end_offs = i_size_read(inode) - 1;
830 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
831
832 count += offset;
833 offset = 0;
834 if (unlikely(end_offs < 0)) {
835 /* Do nothing */
836 } else if (page->index == end_index) {
837 unsigned int pglen;
838 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
839 if (count < pglen)
840 count = pglen;
841 } else if (page->index < end_index)
842 count = PAGE_CACHE_SIZE;
843 }
844
845 /*
846 * Try to find an NFS request corresponding to this page
847 * and update it.
848 * If the existing request cannot be updated, we must flush
849 * it out now.
850 */
851 do {
852 req = nfs_update_request(ctx, inode, page, offset, count);
853 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
854 if (status != -EBUSY)
855 break;
856 /* Request could not be updated. Flush it out and try again */
857 status = nfs_wb_page(inode, page);
858 } while (status >= 0);
859 if (status < 0)
860 goto done;
861
862 status = 0;
863
864 /* Update file length */
865 nfs_grow_file(page, offset, count);
866 /* Set the PG_uptodate flag? */
867 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
868 nfs_unlock_request(req);
869done:
870 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
871 status, (long long)i_size_read(inode));
872 if (status < 0)
873 ClearPageUptodate(page);
874 return status;
875}
876
877static void nfs_writepage_release(struct nfs_page *req)
878{
879 end_page_writeback(req->wb_page);
880
881#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
882 if (!PageError(req->wb_page)) {
883 if (NFS_NEED_RESCHED(req)) {
884 nfs_mark_request_dirty(req);
885 goto out;
886 } else if (NFS_NEED_COMMIT(req)) {
887 nfs_mark_request_commit(req);
888 goto out;
889 }
890 }
891 nfs_inode_remove_request(req);
892
893out:
894 nfs_clear_commit(req);
895 nfs_clear_reschedule(req);
896#else
897 nfs_inode_remove_request(req);
898#endif
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000899 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
902static inline int flush_task_priority(int how)
903{
904 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
905 case FLUSH_HIGHPRI:
906 return RPC_PRIORITY_HIGH;
907 case FLUSH_LOWPRI:
908 return RPC_PRIORITY_LOW;
909 }
910 return RPC_PRIORITY_NORMAL;
911}
912
913/*
914 * Set up the argument/result storage required for the RPC call.
915 */
916static void nfs_write_rpcsetup(struct nfs_page *req,
917 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500918 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 unsigned int count, unsigned int offset,
920 int how)
921{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -0500923 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 /* Set up the RPC argument and reply structs
926 * NB: take care not to mess about with data->commit et al. */
927
928 data->req = req;
929 data->inode = inode = req->wb_context->dentry->d_inode;
930 data->cred = req->wb_context->cred;
931
932 data->args.fh = NFS_FH(inode);
933 data->args.offset = req_offset(req) + offset;
934 data->args.pgbase = req->wb_pgbase + offset;
935 data->args.pages = data->pagevec;
936 data->args.count = count;
937 data->args.context = req->wb_context;
938
939 data->res.fattr = &data->fattr;
940 data->res.count = count;
941 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400942 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Trond Myklebust788e7a82006-03-20 13:44:27 -0500944 /* Set up the initial task struct. */
945 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
946 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 NFS_PROTO(inode)->write_setup(data, how);
948
949 data->task.tk_priority = flush_task_priority(how);
950 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500953 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 inode->i_sb->s_id,
955 (long long)NFS_FILEID(inode),
956 count,
957 (unsigned long long)data->args.offset);
958}
959
960static void nfs_execute_write(struct nfs_write_data *data)
961{
962 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
963 sigset_t oldset;
964
965 rpc_clnt_sigmask(clnt, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 rpc_execute(&data->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 rpc_clnt_sigunmask(clnt, &oldset);
968}
969
970/*
971 * Generate multiple small requests to write out a single
972 * contiguous dirty area on one page.
973 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500974static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 struct nfs_page *req = nfs_list_entry(head->next);
977 struct page *page = req->wb_page;
978 struct nfs_write_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700979 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
980 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 int requests = 0;
982 LIST_HEAD(list);
983
984 nfs_list_remove_request(req);
985
986 nbytes = req->wb_bytes;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700987 do {
988 size_t len = min(nbytes, wsize);
989
990 data = nfs_writedata_alloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (!data)
992 goto out_bad;
993 list_add(&data->pages, &list);
994 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700995 nbytes -= len;
996 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 atomic_set(&req->wb_complete, requests);
998
999 ClearPageError(page);
Trond Myklebustbb713d62005-12-03 15:20:14 -05001000 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 offset = 0;
1002 nbytes = req->wb_bytes;
1003 do {
1004 data = list_entry(list.next, struct nfs_write_data, pages);
1005 list_del_init(&data->pages);
1006
1007 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 if (nbytes > wsize) {
Trond Myklebust788e7a82006-03-20 13:44:27 -05001010 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1011 wsize, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 offset += wsize;
1013 nbytes -= wsize;
1014 } else {
Trond Myklebust788e7a82006-03-20 13:44:27 -05001015 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1016 nbytes, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 nbytes = 0;
1018 }
1019 nfs_execute_write(data);
1020 } while (nbytes != 0);
1021
1022 return 0;
1023
1024out_bad:
1025 while (!list_empty(&list)) {
1026 data = list_entry(list.next, struct nfs_write_data, pages);
1027 list_del(&data->pages);
Trond Myklebust8aca67f2006-11-13 16:23:44 -05001028 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
1030 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001031 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return -ENOMEM;
1033}
1034
1035/*
1036 * Create an RPC task for the given write request and kick it.
1037 * The page must have been locked by the caller.
1038 *
1039 * It may happen that the page we're passed is not marked dirty.
1040 * This is the case if nfs_updatepage detects a conflicting request
1041 * that has been written but not committed.
1042 */
Trond Myklebust7d46a492006-03-20 13:44:50 -05001043static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 struct nfs_page *req;
1046 struct page **pages;
1047 struct nfs_write_data *data;
1048 unsigned int count;
1049
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001050 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (!data)
1052 goto out_bad;
1053
1054 pages = data->pagevec;
1055 count = 0;
1056 while (!list_empty(head)) {
1057 req = nfs_list_entry(head->next);
1058 nfs_list_remove_request(req);
1059 nfs_list_add_request(req, &data->pages);
1060 ClearPageError(req->wb_page);
Trond Myklebustbb713d62005-12-03 15:20:14 -05001061 set_page_writeback(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 *pages++ = req->wb_page;
1063 count += req->wb_bytes;
1064 }
1065 req = nfs_list_entry(data->pages.next);
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 /* Set up the argument struct */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001068 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 nfs_execute_write(data);
1071 return 0;
1072 out_bad:
1073 while (!list_empty(head)) {
1074 struct nfs_page *req = nfs_list_entry(head->next);
1075 nfs_list_remove_request(req);
1076 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001077 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
1079 return -ENOMEM;
1080}
1081
Trond Myklebust7d46a492006-03-20 13:44:50 -05001082static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
1084 LIST_HEAD(one_request);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001085 int (*flush_one)(struct inode *, struct list_head *, int);
1086 struct nfs_page *req;
1087 int wpages = NFS_SERVER(inode)->wpages;
1088 int wsize = NFS_SERVER(inode)->wsize;
1089 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Trond Myklebust7d46a492006-03-20 13:44:50 -05001091 flush_one = nfs_flush_one;
1092 if (wsize < PAGE_CACHE_SIZE)
1093 flush_one = nfs_flush_multi;
1094 /* For single writes, FLUSH_STABLE is more efficient */
1095 if (npages <= wpages && npages == NFS_I(inode)->npages
1096 && nfs_list_entry(head->next)->wb_bytes <= wsize)
1097 how |= FLUSH_STABLE;
1098
1099 do {
1100 nfs_coalesce_requests(head, &one_request, wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 req = nfs_list_entry(one_request.next);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001102 error = flush_one(inode, &one_request, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (error < 0)
Trond Myklebust7d46a492006-03-20 13:44:50 -05001104 goto out_err;
1105 } while (!list_empty(head));
1106 return 0;
1107out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 while (!list_empty(head)) {
1109 req = nfs_list_entry(head->next);
1110 nfs_list_remove_request(req);
1111 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001112 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
1114 return error;
1115}
1116
1117/*
1118 * Handle a write reply that flushed part of a page.
1119 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001120static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001122 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 struct nfs_page *req = data->req;
1124 struct page *page = req->wb_page;
1125
1126 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1127 req->wb_context->dentry->d_inode->i_sb->s_id,
1128 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1129 req->wb_bytes,
1130 (long long)req_offset(req));
1131
Trond Myklebust788e7a82006-03-20 13:44:27 -05001132 if (nfs_writeback_done(task, data) != 0)
1133 return;
1134
1135 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 ClearPageUptodate(page);
1137 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001138 req->wb_context->error = task->tk_status;
1139 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 } else {
1141#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1142 if (data->verf.committed < NFS_FILE_SYNC) {
1143 if (!NFS_NEED_COMMIT(req)) {
1144 nfs_defer_commit(req);
1145 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1146 dprintk(" defer commit\n");
1147 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1148 nfs_defer_reschedule(req);
1149 dprintk(" server reboot detected\n");
1150 }
1151 } else
1152#endif
1153 dprintk(" OK\n");
1154 }
1155
1156 if (atomic_dec_and_test(&req->wb_complete))
1157 nfs_writepage_release(req);
1158}
1159
Trond Myklebust788e7a82006-03-20 13:44:27 -05001160static const struct rpc_call_ops nfs_write_partial_ops = {
1161 .rpc_call_done = nfs_writeback_done_partial,
1162 .rpc_release = nfs_writedata_release,
1163};
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165/*
1166 * Handle a write reply that flushes a whole page.
1167 *
1168 * FIXME: There is an inherent race with invalidate_inode_pages and
1169 * writebacks since the page->count is kept > 1 for as long
1170 * as the page has a write request pending.
1171 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001172static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001174 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 struct nfs_page *req;
1176 struct page *page;
1177
Trond Myklebust788e7a82006-03-20 13:44:27 -05001178 if (nfs_writeback_done(task, data) != 0)
1179 return;
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /* Update attributes as result of writeback. */
1182 while (!list_empty(&data->pages)) {
1183 req = nfs_list_entry(data->pages.next);
1184 nfs_list_remove_request(req);
1185 page = req->wb_page;
1186
1187 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1188 req->wb_context->dentry->d_inode->i_sb->s_id,
1189 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1190 req->wb_bytes,
1191 (long long)req_offset(req));
1192
Trond Myklebust788e7a82006-03-20 13:44:27 -05001193 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 ClearPageUptodate(page);
1195 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001196 req->wb_context->error = task->tk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 end_page_writeback(page);
1198 nfs_inode_remove_request(req);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001199 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 goto next;
1201 }
1202 end_page_writeback(page);
1203
1204#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1205 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1206 nfs_inode_remove_request(req);
1207 dprintk(" OK\n");
1208 goto next;
1209 }
1210 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1211 nfs_mark_request_commit(req);
1212 dprintk(" marked for commit\n");
1213#else
1214 nfs_inode_remove_request(req);
1215#endif
1216 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001217 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219}
1220
Trond Myklebust788e7a82006-03-20 13:44:27 -05001221static const struct rpc_call_ops nfs_write_full_ops = {
1222 .rpc_call_done = nfs_writeback_done_full,
1223 .rpc_release = nfs_writedata_release,
1224};
1225
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227/*
1228 * This function is called when the WRITE call is complete.
1229 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001230int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 struct nfs_writeargs *argp = &data->args;
1233 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001234 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1237 task->tk_pid, task->tk_status);
1238
Chuck Leverf551e442006-09-20 14:33:04 -04001239 /*
1240 * ->write_done will attempt to use post-op attributes to detect
1241 * conflicting writes by other clients. A strict interpretation
1242 * of close-to-open would allow us to continue caching even if
1243 * another writer had changed the file, but some applications
1244 * depend on tighter cache coherency when writing.
1245 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001246 status = NFS_PROTO(data->inode)->write_done(task, data);
1247 if (status != 0)
1248 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001249 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1252 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1253 /* We tried a write call, but the server did not
1254 * commit data to stable storage even though we
1255 * requested it.
1256 * Note: There is a known bug in Tru64 < 5.0 in which
1257 * the server reports NFS_DATA_SYNC, but performs
1258 * NFS_FILE_SYNC. We therefore implement this checking
1259 * as a dprintk() in order to avoid filling syslog.
1260 */
1261 static unsigned long complain;
1262
1263 if (time_before(complain, jiffies)) {
1264 dprintk("NFS: faulty NFS server %s:"
1265 " (committed = %d) != (stable = %d)\n",
David Howells54ceac42006-08-22 20:06:13 -04001266 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 resp->verf->committed, argp->stable);
1268 complain = jiffies + 300 * HZ;
1269 }
1270 }
1271#endif
1272 /* Is this a short write? */
1273 if (task->tk_status >= 0 && resp->count < argp->count) {
1274 static unsigned long complain;
1275
Chuck Lever91d5b472006-03-20 13:44:14 -05001276 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 /* Has the server at least made some progress? */
1279 if (resp->count != 0) {
1280 /* Was this an NFSv2 write or an NFSv3 stable write? */
1281 if (resp->verf->committed != NFS_UNSTABLE) {
1282 /* Resend from where the server left off */
1283 argp->offset += resp->count;
1284 argp->pgbase += resp->count;
1285 argp->count -= resp->count;
1286 } else {
1287 /* Resend as a stable write in order to avoid
1288 * headaches in the case of a server crash.
1289 */
1290 argp->stable = NFS_FILE_SYNC;
1291 }
1292 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001293 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
1295 if (time_before(complain, jiffies)) {
1296 printk(KERN_WARNING
1297 "NFS: Server wrote zero bytes, expected %u.\n",
1298 argp->count);
1299 complain = jiffies + 300 * HZ;
1300 }
1301 /* Can't do anything about it except throw an error. */
1302 task->tk_status = -EIO;
1303 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001304 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306
1307
1308#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001309void nfs_commit_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 nfs_commit_free(wdata);
1312}
1313
1314/*
1315 * Set up the argument/result storage required for the RPC call.
1316 */
1317static void nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001318 struct nfs_write_data *data,
1319 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001321 struct nfs_page *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001323 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 /* Set up the RPC argument and reply structs
1326 * NB: take care not to mess about with data->commit et al. */
1327
1328 list_splice_init(head, &data->pages);
1329 first = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 inode = first->wb_context->dentry->d_inode;
1331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 data->inode = inode;
1333 data->cred = first->wb_context->cred;
1334
1335 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001336 /* Note: we always request a commit of the entire inode */
1337 data->args.offset = 0;
1338 data->args.count = 0;
1339 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 data->res.fattr = &data->fattr;
1341 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001342 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001343
1344 /* Set up the initial task struct. */
1345 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1346 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 NFS_PROTO(inode)->commit_setup(data, how);
1348
1349 data->task.tk_priority = flush_task_priority(how);
1350 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Chuck Lever0bbacc42005-11-01 16:53:32 -05001352 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
1355/*
1356 * Commit dirty pages
1357 */
1358static int
Chuck Lever40859d72005-11-30 18:09:02 -05001359nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 struct nfs_write_data *data;
1362 struct nfs_page *req;
1363
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001364 data = nfs_commit_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 if (!data)
1367 goto out_bad;
1368
1369 /* Set up the argument struct */
1370 nfs_commit_rpcsetup(head, data, how);
1371
1372 nfs_execute_write(data);
1373 return 0;
1374 out_bad:
1375 while (!list_empty(head)) {
1376 req = nfs_list_entry(head->next);
1377 nfs_list_remove_request(req);
1378 nfs_mark_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001379 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust5c2d97c2006-09-18 23:20:35 -04001380 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382 return -ENOMEM;
1383}
1384
1385/*
1386 * COMMIT call returned
1387 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001388static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001390 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1394 task->tk_pid, task->tk_status);
1395
Trond Myklebust788e7a82006-03-20 13:44:27 -05001396 /* Call the NFS version-specific code */
1397 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1398 return;
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 while (!list_empty(&data->pages)) {
1401 req = nfs_list_entry(data->pages.next);
1402 nfs_list_remove_request(req);
Christoph Lameterfd39fc82006-06-30 01:55:40 -07001403 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1406 req->wb_context->dentry->d_inode->i_sb->s_id,
1407 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1408 req->wb_bytes,
1409 (long long)req_offset(req));
1410 if (task->tk_status < 0) {
1411 req->wb_context->error = task->tk_status;
1412 nfs_inode_remove_request(req);
1413 dprintk(", error = %d\n", task->tk_status);
1414 goto next;
1415 }
1416
1417 /* Okay, COMMIT succeeded, apparently. Check the verifier
1418 * returned by the server against all stored verfs. */
1419 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1420 /* We have a match */
1421 nfs_inode_remove_request(req);
1422 dprintk(" OK\n");
1423 goto next;
1424 }
1425 /* We have a mismatch. Write the page again */
1426 dprintk(" mismatch\n");
1427 nfs_mark_request_dirty(req);
1428 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001429 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001432
1433static const struct rpc_call_ops nfs_commit_ops = {
1434 .rpc_call_done = nfs_commit_done,
1435 .rpc_release = nfs_commit_release,
1436};
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001437#else
1438static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1439{
1440 return 0;
1441}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442#endif
1443
Trond Myklebust3f442542006-09-17 14:46:44 -04001444static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Trond Myklebust28c69252006-09-16 13:04:50 -04001446 struct nfs_inode *nfsi = NFS_I(mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 LIST_HEAD(head);
Trond Myklebust3f442542006-09-17 14:46:44 -04001448 long res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 spin_lock(&nfsi->req_lock);
Trond Myklebust3f442542006-09-17 14:46:44 -04001451 res = nfs_scan_dirty(mapping, wbc, &head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 spin_unlock(&nfsi->req_lock);
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001453 if (res) {
Trond Myklebust28c69252006-09-16 13:04:50 -04001454 int error = nfs_flush_list(mapping->host, &head, res, how);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001455 if (error < 0)
1456 return error;
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return res;
1459}
1460
1461#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001462int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
1464 struct nfs_inode *nfsi = NFS_I(inode);
1465 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001466 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001469 res = nfs_scan_commit(inode, &head, 0, 0);
1470 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001472 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001473 if (error < 0)
1474 return error;
1475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return res;
1477}
1478#endif
1479
Trond Myklebust1c759502006-10-09 16:18:38 -04001480long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
Trond Myklebust1c759502006-10-09 16:18:38 -04001482 struct inode *inode = mapping->host;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001483 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c759502006-10-09 16:18:38 -04001484 unsigned long idx_start, idx_end;
1485 unsigned int npages = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001486 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001487 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebust3f442542006-09-17 14:46:44 -04001488 long pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Trond Myklebust1c759502006-10-09 16:18:38 -04001490 /* FIXME */
1491 if (wbc->range_cyclic)
1492 idx_start = 0;
1493 else {
1494 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1495 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1496 if (idx_end > idx_start) {
1497 unsigned long l_npages = 1 + idx_end - idx_start;
1498 npages = l_npages;
1499 if (sizeof(npages) != sizeof(l_npages) &&
1500 (unsigned long)npages != l_npages)
1501 npages = 0;
1502 }
1503 }
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001504 how &= ~FLUSH_NOCOMMIT;
1505 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 do {
Trond Myklebust1c759502006-10-09 16:18:38 -04001507 wbc->pages_skipped = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001508 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1509 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001510 continue;
Trond Myklebust1c759502006-10-09 16:18:38 -04001511 pages = nfs_scan_dirty(mapping, wbc, &head);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001512 if (pages != 0) {
1513 spin_unlock(&nfsi->req_lock);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001514 if (how & FLUSH_INVALIDATE) {
Trond Myklebust83715ad2006-07-05 13:17:12 -04001515 nfs_cancel_dirty_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001516 ret = pages;
1517 } else
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001518 ret = nfs_flush_list(inode, &head, pages, how);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001519 spin_lock(&nfsi->req_lock);
1520 continue;
1521 }
Trond Myklebust1c759502006-10-09 16:18:38 -04001522 if (wbc->pages_skipped != 0)
1523 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001524 if (nocommit)
1525 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001526 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebust1c759502006-10-09 16:18:38 -04001527 if (pages == 0) {
1528 if (wbc->pages_skipped != 0)
1529 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001530 break;
Trond Myklebust1c759502006-10-09 16:18:38 -04001531 }
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001532 if (how & FLUSH_INVALIDATE) {
1533 spin_unlock(&nfsi->req_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001534 nfs_cancel_commit_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001535 ret = pages;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001536 spin_lock(&nfsi->req_lock);
1537 continue;
1538 }
1539 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001540 spin_unlock(&nfsi->req_lock);
1541 ret = nfs_commit_list(inode, &head, how);
1542 spin_lock(&nfsi->req_lock);
1543 } while (ret >= 0);
1544 spin_unlock(&nfsi->req_lock);
1545 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
Trond Myklebust1c759502006-10-09 16:18:38 -04001548/*
1549 * flush the inode to disk.
1550 */
1551int nfs_wb_all(struct inode *inode)
1552{
1553 struct address_space *mapping = inode->i_mapping;
1554 struct writeback_control wbc = {
1555 .bdi = mapping->backing_dev_info,
1556 .sync_mode = WB_SYNC_ALL,
1557 .nr_to_write = LONG_MAX,
1558 .range_cyclic = 1,
1559 };
1560 int ret;
1561
1562 ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
1563 if (ret >= 0)
1564 return 0;
1565 return ret;
1566}
1567
1568int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
1569{
1570 struct writeback_control wbc = {
1571 .bdi = mapping->backing_dev_info,
1572 .sync_mode = WB_SYNC_ALL,
1573 .nr_to_write = LONG_MAX,
1574 .range_start = range_start,
1575 .range_end = range_end,
1576 };
1577 int ret;
1578
1579 ret = nfs_sync_mapping_wait(mapping, &wbc, how);
1580 if (ret >= 0)
1581 return 0;
1582 return ret;
1583}
1584
1585static int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
1586{
1587 loff_t range_start = page_offset(page);
1588 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1589
1590 return nfs_sync_mapping_range(inode->i_mapping, range_start, range_end, how | FLUSH_STABLE);
1591}
1592
1593/*
1594 * Write back all requests on one page - we do this before reading it.
1595 */
1596int nfs_wb_page(struct inode *inode, struct page* page)
1597{
1598 return nfs_wb_page_priority(inode, page, 0);
1599}
1600
1601
David Howellsf7b422b2006-06-09 09:34:33 -04001602int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
1604 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1605 sizeof(struct nfs_write_data),
1606 0, SLAB_HWCACHE_ALIGN,
1607 NULL, NULL);
1608 if (nfs_wdata_cachep == NULL)
1609 return -ENOMEM;
1610
Matthew Dobson93d23412006-03-26 01:37:50 -08001611 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1612 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (nfs_wdata_mempool == NULL)
1614 return -ENOMEM;
1615
Matthew Dobson93d23412006-03-26 01:37:50 -08001616 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1617 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if (nfs_commit_mempool == NULL)
1619 return -ENOMEM;
1620
1621 return 0;
1622}
1623
David Brownell266bee82006-06-27 12:59:15 -07001624void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
1626 mempool_destroy(nfs_commit_mempool);
1627 mempool_destroy(nfs_wdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001628 kmem_cache_destroy(nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630