blob: f21e268705c0998c48c4ae4adfeae0c0efbf146d [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
49#include <linux/config.h>
50#include <linux/types.h>
51#include <linux/slab.h>
52#include <linux/mm.h>
53#include <linux/pagemap.h>
54#include <linux/file.h>
55#include <linux/mpage.h>
56#include <linux/writeback.h>
57
58#include <linux/sunrpc/clnt.h>
59#include <linux/nfs_fs.h>
60#include <linux/nfs_mount.h>
61#include <linux/nfs_page.h>
62#include <asm/uaccess.h>
63#include <linux/smp_lock.h>
64
65#include "delegation.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050066#include "iostat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#define NFSDBG_FACILITY NFSDBG_PAGECACHE
69
70#define MIN_POOL_WRITE (32)
71#define MIN_POOL_COMMIT (4)
72
73/*
74 * Local function declarations
75 */
76static struct nfs_page * nfs_update_request(struct nfs_open_context*,
77 struct inode *,
78 struct page *,
79 unsigned int, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static int nfs_wait_on_write_congestion(struct address_space *, int);
81static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int);
82static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
83 unsigned int npages, int how);
Trond Myklebust788e7a82006-03-20 13:44:27 -050084static const struct rpc_call_ops nfs_write_partial_ops;
85static const struct rpc_call_ops nfs_write_full_ops;
86static const struct rpc_call_ops nfs_commit_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88static kmem_cache_t *nfs_wdata_cachep;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050089static mempool_t *nfs_wdata_mempool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static mempool_t *nfs_commit_mempool;
91
92static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
93
Trond Myklebuste17b1fc2006-03-20 13:44:35 -050094struct nfs_write_data *nfs_commit_alloc(unsigned int pagecount)
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);
Chuck Lever0d0b5cb2006-05-25 01:40:53 -0400101 if (pagecount <= ARRAY_SIZE(p->page_array))
102 p->pagevec = p->page_array;
Chuck Lever40859d72005-11-30 18:09:02 -0500103 else {
Chuck Lever0d0b5cb2006-05-25 01:40:53 -0400104 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
Eric Sesterhennbd647542006-03-20 13:44:10 -0500105 if (!p->pagevec) {
Chuck Lever40859d72005-11-30 18:09:02 -0500106 mempool_free(p, nfs_commit_mempool);
107 p = NULL;
108 }
109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
111 return p;
112}
113
Trond Myklebuste17b1fc2006-03-20 13:44:35 -0500114void nfs_commit_free(struct nfs_write_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Chuck Lever40859d72005-11-30 18:09:02 -0500116 if (p && (p->pagevec != &p->page_array[0]))
117 kfree(p->pagevec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 mempool_free(p, nfs_commit_mempool);
119}
120
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500121struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
122{
123 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
124
125 if (p) {
126 memset(p, 0, sizeof(*p));
127 INIT_LIST_HEAD(&p->pages);
Chuck Lever0d0b5cb2006-05-25 01:40:53 -0400128 if (pagecount <= ARRAY_SIZE(p->page_array))
129 p->pagevec = p->page_array;
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500130 else {
Chuck Lever0d0b5cb2006-05-25 01:40:53 -0400131 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
132 if (!p->pagevec) {
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500133 mempool_free(p, nfs_wdata_mempool);
134 p = NULL;
135 }
136 }
137 }
138 return p;
139}
140
141void nfs_writedata_free(struct nfs_write_data *p)
142{
143 if (p && (p->pagevec != &p->page_array[0]))
144 kfree(p->pagevec);
145 mempool_free(p, nfs_wdata_mempool);
146}
147
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100148void nfs_writedata_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 nfs_writedata_free(wdata);
151}
152
153/* Adjust the file length if we're writing beyond the end */
154static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
155{
156 struct inode *inode = page->mapping->host;
157 loff_t end, i_size = i_size_read(inode);
158 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
159
160 if (i_size > 0 && page->index < end_index)
161 return;
162 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
163 if (i_size >= end)
164 return;
Chuck Lever91d5b472006-03-20 13:44:14 -0500165 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 i_size_write(inode, end);
167}
168
169/* We can set the PG_uptodate flag if we see that a write request
170 * covers the full page.
171 */
172static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
173{
174 loff_t end_offs;
175
176 if (PageUptodate(page))
177 return;
178 if (base != 0)
179 return;
180 if (count == PAGE_CACHE_SIZE) {
181 SetPageUptodate(page);
182 return;
183 }
184
185 end_offs = i_size_read(page->mapping->host) - 1;
186 if (end_offs < 0)
187 return;
188 /* Is this the last page? */
189 if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT))
190 return;
191 /* This is the last page: set PG_uptodate if we cover the entire
192 * extent of the data, then zero the rest of the page.
193 */
194 if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) {
195 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
196 SetPageUptodate(page);
197 }
198}
199
200/*
201 * Write a page synchronously.
202 * Offset is the data offset within the page.
203 */
204static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
205 struct page *page, unsigned int offset, unsigned int count,
206 int how)
207{
208 unsigned int wsize = NFS_SERVER(inode)->wsize;
209 int result, written = 0;
210 struct nfs_write_data *wdata;
211
Chuck Lever40859d72005-11-30 18:09:02 -0500212 wdata = nfs_writedata_alloc(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (!wdata)
214 return -ENOMEM;
215
216 wdata->flags = how;
217 wdata->cred = ctx->cred;
218 wdata->inode = inode;
219 wdata->args.fh = NFS_FH(inode);
220 wdata->args.context = ctx;
221 wdata->args.pages = &page;
222 wdata->args.stable = NFS_FILE_SYNC;
223 wdata->args.pgbase = offset;
224 wdata->args.count = wsize;
225 wdata->res.fattr = &wdata->fattr;
226 wdata->res.verf = &wdata->verf;
227
228 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
229 inode->i_sb->s_id,
230 (long long)NFS_FILEID(inode),
231 count, (long long)(page_offset(page) + offset));
232
Trond Myklebustbb713d62005-12-03 15:20:14 -0500233 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 nfs_begin_data_update(inode);
235 do {
236 if (count < wsize)
237 wdata->args.count = count;
238 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
239
240 result = NFS_PROTO(inode)->write(wdata);
241
242 if (result < 0) {
243 /* Must mark the page invalid after I/O error */
244 ClearPageUptodate(page);
245 goto io_error;
246 }
247 if (result < wdata->args.count)
248 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
249 wdata->args.count, result);
250
251 wdata->args.offset += result;
252 wdata->args.pgbase += result;
253 written += result;
254 count -= result;
Chuck Lever91d5b472006-03-20 13:44:14 -0500255 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 } while (count);
257 /* Update file length */
258 nfs_grow_file(page, offset, written);
259 /* Set the PG_uptodate flag? */
260 nfs_mark_uptodate(page, offset, written);
261
262 if (PageError(page))
263 ClearPageError(page);
264
265io_error:
Trond Myklebust951a1432005-06-22 17:16:30 +0000266 nfs_end_data_update(inode);
Trond Myklebustbb713d62005-12-03 15:20:14 -0500267 end_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 nfs_writedata_free(wdata);
269 return written ? written : result;
270}
271
272static int nfs_writepage_async(struct nfs_open_context *ctx,
273 struct inode *inode, struct page *page,
274 unsigned int offset, unsigned int count)
275{
276 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 req = nfs_update_request(ctx, inode, page, offset, count);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100279 if (IS_ERR(req))
280 return PTR_ERR(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 /* Update file length */
282 nfs_grow_file(page, offset, count);
283 /* Set the PG_uptodate flag? */
284 nfs_mark_uptodate(page, offset, count);
285 nfs_unlock_request(req);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289static int wb_priority(struct writeback_control *wbc)
290{
291 if (wbc->for_reclaim)
292 return FLUSH_HIGHPRI;
293 if (wbc->for_kupdate)
294 return FLUSH_LOWPRI;
295 return 0;
296}
297
298/*
299 * Write an mmapped page to the server.
300 */
301int nfs_writepage(struct page *page, struct writeback_control *wbc)
302{
303 struct nfs_open_context *ctx;
304 struct inode *inode = page->mapping->host;
305 unsigned long end_index;
306 unsigned offset = PAGE_CACHE_SIZE;
307 loff_t i_size = i_size_read(inode);
308 int inode_referenced = 0;
309 int priority = wb_priority(wbc);
310 int err;
311
Chuck Lever91d5b472006-03-20 13:44:14 -0500312 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
313 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /*
316 * Note: We need to ensure that we have a reference to the inode
317 * if we are to do asynchronous writes. If not, waiting
318 * in nfs_wait_on_request() may deadlock with clear_inode().
319 *
320 * If igrab() fails here, then it is in any case safe to
321 * call nfs_wb_page(), since there will be no pending writes.
322 */
323 if (igrab(inode) != 0)
324 inode_referenced = 1;
325 end_index = i_size >> PAGE_CACHE_SHIFT;
326
327 /* Ensure we've flushed out any previous writes */
328 nfs_wb_page_priority(inode, page, priority);
329
330 /* easy case */
331 if (page->index < end_index)
332 goto do_it;
333 /* things got complicated... */
334 offset = i_size & (PAGE_CACHE_SIZE-1);
335
336 /* OK, are we completely out? */
337 err = 0; /* potential race with truncate - ignore */
338 if (page->index >= end_index+1 || !offset)
339 goto out;
340do_it:
Trond Myklebustd5308382005-11-04 15:33:38 -0500341 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (ctx == NULL) {
343 err = -EBADF;
344 goto out;
345 }
346 lock_kernel();
347 if (!IS_SYNC(inode) && inode_referenced) {
348 err = nfs_writepage_async(ctx, inode, page, 0, offset);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100349 if (!wbc->for_writepages)
350 nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 } else {
352 err = nfs_writepage_sync(ctx, inode, page, 0,
353 offset, priority);
354 if (err >= 0) {
355 if (err != offset)
356 redirty_page_for_writepage(wbc, page);
357 err = 0;
358 }
359 }
360 unlock_kernel();
361 put_nfs_open_context(ctx);
362out:
363 unlock_page(page);
364 if (inode_referenced)
365 iput(inode);
366 return err;
367}
368
369/*
370 * Note: causes nfs_update_request() to block on the assumption
371 * that the writeback is generated due to memory pressure.
372 */
373int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
374{
375 struct backing_dev_info *bdi = mapping->backing_dev_info;
376 struct inode *inode = mapping->host;
377 int err;
378
Chuck Lever91d5b472006-03-20 13:44:14 -0500379 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 err = generic_writepages(mapping, wbc);
382 if (err)
383 return err;
384 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
385 if (wbc->nonblocking)
386 return 0;
387 nfs_wait_on_write_congestion(mapping, 0);
388 }
389 err = nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
390 if (err < 0)
391 goto out;
Chuck Lever91d5b472006-03-20 13:44:14 -0500392 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 wbc->nr_to_write -= err;
394 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
395 err = nfs_wait_on_requests(inode, 0, 0);
396 if (err < 0)
397 goto out;
398 }
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000399 err = nfs_commit_inode(inode, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (err > 0) {
401 wbc->nr_to_write -= err;
402 err = 0;
403 }
404out:
405 clear_bit(BDI_write_congested, &bdi->state);
406 wake_up_all(&nfs_write_congestion);
407 return err;
408}
409
410/*
411 * Insert a write request into an inode
412 */
413static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
414{
415 struct nfs_inode *nfsi = NFS_I(inode);
416 int error;
417
418 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
419 BUG_ON(error == -EEXIST);
420 if (error)
421 return error;
422 if (!nfsi->npages) {
423 igrab(inode);
424 nfs_begin_data_update(inode);
425 if (nfs_have_delegation(inode, FMODE_WRITE))
426 nfsi->change_attr++;
427 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500428 SetPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 nfsi->npages++;
430 atomic_inc(&req->wb_count);
431 return 0;
432}
433
434/*
435 * Insert a write request into an inode
436 */
437static void nfs_inode_remove_request(struct nfs_page *req)
438{
439 struct inode *inode = req->wb_context->dentry->d_inode;
440 struct nfs_inode *nfsi = NFS_I(inode);
441
442 BUG_ON (!NFS_WBACK_BUSY(req));
443
444 spin_lock(&nfsi->req_lock);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500445 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
447 nfsi->npages--;
448 if (!nfsi->npages) {
449 spin_unlock(&nfsi->req_lock);
Trond Myklebust951a1432005-06-22 17:16:30 +0000450 nfs_end_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 iput(inode);
452 } else
453 spin_unlock(&nfsi->req_lock);
454 nfs_clear_request(req);
455 nfs_release_request(req);
456}
457
458/*
459 * Find a request
460 */
461static inline struct nfs_page *
462_nfs_find_request(struct inode *inode, unsigned long index)
463{
464 struct nfs_inode *nfsi = NFS_I(inode);
465 struct nfs_page *req;
466
467 req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index);
468 if (req)
469 atomic_inc(&req->wb_count);
470 return req;
471}
472
473static struct nfs_page *
474nfs_find_request(struct inode *inode, unsigned long index)
475{
476 struct nfs_page *req;
477 struct nfs_inode *nfsi = NFS_I(inode);
478
479 spin_lock(&nfsi->req_lock);
480 req = _nfs_find_request(inode, index);
481 spin_unlock(&nfsi->req_lock);
482 return req;
483}
484
485/*
486 * Add a request to the inode's dirty list.
487 */
488static void
489nfs_mark_request_dirty(struct nfs_page *req)
490{
491 struct inode *inode = req->wb_context->dentry->d_inode;
492 struct nfs_inode *nfsi = NFS_I(inode);
493
494 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000495 radix_tree_tag_set(&nfsi->nfs_page_tree,
496 req->wb_index, NFS_PAGE_TAG_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 nfs_list_add_request(req, &nfsi->dirty);
498 nfsi->ndirty++;
499 spin_unlock(&nfsi->req_lock);
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700500 inc_zone_page_state(req->wb_page, NR_FILE_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 mark_inode_dirty(inode);
502}
503
504/*
505 * Check if a request is dirty
506 */
507static inline int
508nfs_dirty_request(struct nfs_page *req)
509{
510 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
511 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
512}
513
514#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
515/*
516 * Add a request to the inode's commit list.
517 */
518static void
519nfs_mark_request_commit(struct nfs_page *req)
520{
521 struct inode *inode = req->wb_context->dentry->d_inode;
522 struct nfs_inode *nfsi = NFS_I(inode);
523
524 spin_lock(&nfsi->req_lock);
525 nfs_list_add_request(req, &nfsi->commit);
526 nfsi->ncommit++;
527 spin_unlock(&nfsi->req_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700528 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 mark_inode_dirty(inode);
530}
531#endif
532
533/*
534 * Wait for a request to complete.
535 *
536 * Interruptible by signals only if mounted with intr flag.
537 */
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500538static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 struct nfs_inode *nfsi = NFS_I(inode);
541 struct nfs_page *req;
542 unsigned long idx_end, next;
543 unsigned int res = 0;
544 int error;
545
546 if (npages == 0)
547 idx_end = ~0;
548 else
549 idx_end = idx_start + npages - 1;
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 next = idx_start;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000552 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 -0700553 if (req->wb_index > idx_end)
554 break;
555
556 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000557 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 atomic_inc(&req->wb_count);
560 spin_unlock(&nfsi->req_lock);
561 error = nfs_wait_on_request(req);
562 nfs_release_request(req);
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500563 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (error < 0)
565 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 res++;
567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return res;
569}
570
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500571static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
572{
573 struct nfs_inode *nfsi = NFS_I(inode);
574 int ret;
575
576 spin_lock(&nfsi->req_lock);
577 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
578 spin_unlock(&nfsi->req_lock);
579 return ret;
580}
581
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400582static void nfs_cancel_requests(struct list_head *head)
583{
584 struct nfs_page *req;
585 while(!list_empty(head)) {
586 req = nfs_list_entry(head->next);
587 nfs_list_remove_request(req);
588 nfs_inode_remove_request(req);
589 nfs_clear_page_writeback(req);
590 }
591}
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593/*
594 * nfs_scan_dirty - Scan an inode for dirty requests
595 * @inode: NFS inode to scan
596 * @dst: destination list
597 * @idx_start: lower bound of page->index to scan.
598 * @npages: idx_start + npages sets the upper bound to scan.
599 *
600 * Moves requests from the inode's dirty page list.
601 * The requests are *not* checked to ensure that they form a contiguous set.
602 */
603static int
604nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
605{
606 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000607 int res = 0;
608
609 if (nfsi->ndirty != 0) {
610 res = nfs_scan_lock_dirty(nfsi, dst, idx_start, npages);
611 nfsi->ndirty -= res;
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000612 if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
613 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return res;
616}
617
618#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
619/*
620 * nfs_scan_commit - Scan an inode for commit requests
621 * @inode: NFS inode to scan
622 * @dst: destination list
623 * @idx_start: lower bound of page->index to scan.
624 * @npages: idx_start + npages sets the upper bound to scan.
625 *
626 * Moves requests from the inode's 'commit' request list.
627 * The requests are *not* checked to ensure that they form a contiguous set.
628 */
629static int
630nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
631{
632 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000633 int res = 0;
634
635 if (nfsi->ncommit != 0) {
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400636 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000637 nfsi->ncommit -= res;
638 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
639 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return res;
642}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500643#else
644static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
645{
646 return 0;
647}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648#endif
649
650static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
651{
652 struct backing_dev_info *bdi = mapping->backing_dev_info;
653 DEFINE_WAIT(wait);
654 int ret = 0;
655
656 might_sleep();
657
658 if (!bdi_write_congested(bdi))
659 return 0;
Chuck Lever91d5b472006-03-20 13:44:14 -0500660
661 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (intr) {
664 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
665 sigset_t oldset;
666
667 rpc_clnt_sigmask(clnt, &oldset);
668 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
669 if (bdi_write_congested(bdi)) {
670 if (signalled())
671 ret = -ERESTARTSYS;
672 else
673 schedule();
674 }
675 rpc_clnt_sigunmask(clnt, &oldset);
676 } else {
677 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
678 if (bdi_write_congested(bdi))
679 schedule();
680 }
681 finish_wait(&nfs_write_congestion, &wait);
682 return ret;
683}
684
685
686/*
687 * Try to update any existing write request, or create one if there is none.
688 * In order to match, the request's credentials must match those of
689 * the calling process.
690 *
691 * Note: Should always be called with the Page Lock held!
692 */
693static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
694 struct inode *inode, struct page *page,
695 unsigned int offset, unsigned int bytes)
696{
697 struct nfs_server *server = NFS_SERVER(inode);
698 struct nfs_inode *nfsi = NFS_I(inode);
699 struct nfs_page *req, *new = NULL;
700 unsigned long rqend, end;
701
702 end = offset + bytes;
703
704 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
705 return ERR_PTR(-ERESTARTSYS);
706 for (;;) {
707 /* Loop over all inode entries and see if we find
708 * A request for the page we wish to update
709 */
710 spin_lock(&nfsi->req_lock);
711 req = _nfs_find_request(inode, page->index);
712 if (req) {
713 if (!nfs_lock_request_dontget(req)) {
714 int error;
715 spin_unlock(&nfsi->req_lock);
716 error = nfs_wait_on_request(req);
717 nfs_release_request(req);
Neil Brown1dd594b2006-03-20 13:44:04 -0500718 if (error < 0) {
719 if (new)
720 nfs_release_request(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return ERR_PTR(error);
Neil Brown1dd594b2006-03-20 13:44:04 -0500722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 continue;
724 }
725 spin_unlock(&nfsi->req_lock);
726 if (new)
727 nfs_release_request(new);
728 break;
729 }
730
731 if (new) {
732 int error;
733 nfs_lock_request_dontget(new);
734 error = nfs_inode_add_request(inode, new);
735 if (error) {
736 spin_unlock(&nfsi->req_lock);
737 nfs_unlock_request(new);
738 return ERR_PTR(error);
739 }
740 spin_unlock(&nfsi->req_lock);
741 nfs_mark_request_dirty(new);
742 return new;
743 }
744 spin_unlock(&nfsi->req_lock);
745
746 new = nfs_create_request(ctx, inode, page, offset, bytes);
747 if (IS_ERR(new))
748 return new;
749 }
750
751 /* We have a request for our page.
752 * If the creds don't match, or the
753 * page addresses don't match,
754 * tell the caller to wait on the conflicting
755 * request.
756 */
757 rqend = req->wb_offset + req->wb_bytes;
758 if (req->wb_context != ctx
759 || req->wb_page != page
760 || !nfs_dirty_request(req)
761 || offset > rqend || end < req->wb_offset) {
762 nfs_unlock_request(req);
763 return ERR_PTR(-EBUSY);
764 }
765
766 /* Okay, the request matches. Update the region */
767 if (offset < req->wb_offset) {
768 req->wb_offset = offset;
769 req->wb_pgbase = offset;
770 req->wb_bytes = rqend - req->wb_offset;
771 }
772
773 if (end > rqend)
774 req->wb_bytes = end - req->wb_offset;
775
776 return req;
777}
778
779int nfs_flush_incompatible(struct file *file, struct page *page)
780{
781 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
782 struct inode *inode = page->mapping->host;
783 struct nfs_page *req;
784 int status = 0;
785 /*
786 * Look for a request corresponding to this page. If there
787 * is one, and it belongs to another file, we flush it out
788 * before we try to copy anything into the page. Do this
789 * due to the lack of an ACCESS-type call in NFSv2.
790 * Also do the same if we find a request from an existing
791 * dropped page.
792 */
793 req = nfs_find_request(inode, page->index);
794 if (req) {
795 if (req->wb_page != page || ctx != req->wb_context)
796 status = nfs_wb_page(inode, page);
797 nfs_release_request(req);
798 }
799 return (status < 0) ? status : 0;
800}
801
802/*
803 * Update and possibly write a cached page of an NFS file.
804 *
805 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
806 * things with a page scheduled for an RPC call (e.g. invalidate it).
807 */
808int nfs_updatepage(struct file *file, struct page *page,
809 unsigned int offset, unsigned int count)
810{
811 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 struct inode *inode = page->mapping->host;
813 struct nfs_page *req;
814 int status = 0;
815
Chuck Lever91d5b472006-03-20 13:44:14 -0500816 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500819 file->f_dentry->d_parent->d_name.name,
820 file->f_dentry->d_name.name, count,
821 (long long)(page_offset(page) +offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 if (IS_SYNC(inode)) {
824 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
825 if (status > 0) {
826 if (offset == 0 && status == PAGE_CACHE_SIZE)
827 SetPageUptodate(page);
828 return 0;
829 }
830 return status;
831 }
832
833 /* If we're not using byte range locks, and we know the page
834 * is entirely in cache, it may be more efficient to avoid
835 * fragmenting write requests.
836 */
Trond Myklebustab0a3db2005-06-22 17:16:30 +0000837 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 loff_t end_offs = i_size_read(inode) - 1;
839 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
840
841 count += offset;
842 offset = 0;
843 if (unlikely(end_offs < 0)) {
844 /* Do nothing */
845 } else if (page->index == end_index) {
846 unsigned int pglen;
847 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
848 if (count < pglen)
849 count = pglen;
850 } else if (page->index < end_index)
851 count = PAGE_CACHE_SIZE;
852 }
853
854 /*
855 * Try to find an NFS request corresponding to this page
856 * and update it.
857 * If the existing request cannot be updated, we must flush
858 * it out now.
859 */
860 do {
861 req = nfs_update_request(ctx, inode, page, offset, count);
862 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
863 if (status != -EBUSY)
864 break;
865 /* Request could not be updated. Flush it out and try again */
866 status = nfs_wb_page(inode, page);
867 } while (status >= 0);
868 if (status < 0)
869 goto done;
870
871 status = 0;
872
873 /* Update file length */
874 nfs_grow_file(page, offset, count);
875 /* Set the PG_uptodate flag? */
876 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
877 nfs_unlock_request(req);
878done:
879 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
880 status, (long long)i_size_read(inode));
881 if (status < 0)
882 ClearPageUptodate(page);
883 return status;
884}
885
886static void nfs_writepage_release(struct nfs_page *req)
887{
888 end_page_writeback(req->wb_page);
889
890#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
891 if (!PageError(req->wb_page)) {
892 if (NFS_NEED_RESCHED(req)) {
893 nfs_mark_request_dirty(req);
894 goto out;
895 } else if (NFS_NEED_COMMIT(req)) {
896 nfs_mark_request_commit(req);
897 goto out;
898 }
899 }
900 nfs_inode_remove_request(req);
901
902out:
903 nfs_clear_commit(req);
904 nfs_clear_reschedule(req);
905#else
906 nfs_inode_remove_request(req);
907#endif
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000908 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
911static inline int flush_task_priority(int how)
912{
913 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
914 case FLUSH_HIGHPRI:
915 return RPC_PRIORITY_HIGH;
916 case FLUSH_LOWPRI:
917 return RPC_PRIORITY_LOW;
918 }
919 return RPC_PRIORITY_NORMAL;
920}
921
922/*
923 * Set up the argument/result storage required for the RPC call.
924 */
925static void nfs_write_rpcsetup(struct nfs_page *req,
926 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500927 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 unsigned int count, unsigned int offset,
929 int how)
930{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -0500932 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 /* Set up the RPC argument and reply structs
935 * NB: take care not to mess about with data->commit et al. */
936
937 data->req = req;
938 data->inode = inode = req->wb_context->dentry->d_inode;
939 data->cred = req->wb_context->cred;
940
941 data->args.fh = NFS_FH(inode);
942 data->args.offset = req_offset(req) + offset;
943 data->args.pgbase = req->wb_pgbase + offset;
944 data->args.pages = data->pagevec;
945 data->args.count = count;
946 data->args.context = req->wb_context;
947
948 data->res.fattr = &data->fattr;
949 data->res.count = count;
950 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400951 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Trond Myklebust788e7a82006-03-20 13:44:27 -0500953 /* Set up the initial task struct. */
954 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
955 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 NFS_PROTO(inode)->write_setup(data, how);
957
958 data->task.tk_priority = flush_task_priority(how);
959 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500962 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 inode->i_sb->s_id,
964 (long long)NFS_FILEID(inode),
965 count,
966 (unsigned long long)data->args.offset);
967}
968
969static void nfs_execute_write(struct nfs_write_data *data)
970{
971 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
972 sigset_t oldset;
973
974 rpc_clnt_sigmask(clnt, &oldset);
975 lock_kernel();
976 rpc_execute(&data->task);
977 unlock_kernel();
978 rpc_clnt_sigunmask(clnt, &oldset);
979}
980
981/*
982 * Generate multiple small requests to write out a single
983 * contiguous dirty area on one page.
984 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500985static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
987 struct nfs_page *req = nfs_list_entry(head->next);
988 struct page *page = req->wb_page;
989 struct nfs_write_data *data;
990 unsigned int wsize = NFS_SERVER(inode)->wsize;
991 unsigned int nbytes, offset;
992 int requests = 0;
993 LIST_HEAD(list);
994
995 nfs_list_remove_request(req);
996
997 nbytes = req->wb_bytes;
998 for (;;) {
Chuck Lever40859d72005-11-30 18:09:02 -0500999 data = nfs_writedata_alloc(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (!data)
1001 goto out_bad;
1002 list_add(&data->pages, &list);
1003 requests++;
1004 if (nbytes <= wsize)
1005 break;
1006 nbytes -= wsize;
1007 }
1008 atomic_set(&req->wb_complete, requests);
1009
1010 ClearPageError(page);
Trond Myklebustbb713d62005-12-03 15:20:14 -05001011 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 offset = 0;
1013 nbytes = req->wb_bytes;
1014 do {
1015 data = list_entry(list.next, struct nfs_write_data, pages);
1016 list_del_init(&data->pages);
1017
1018 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 if (nbytes > wsize) {
Trond Myklebust788e7a82006-03-20 13:44:27 -05001021 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1022 wsize, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 offset += wsize;
1024 nbytes -= wsize;
1025 } else {
Trond Myklebust788e7a82006-03-20 13:44:27 -05001026 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1027 nbytes, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 nbytes = 0;
1029 }
1030 nfs_execute_write(data);
1031 } while (nbytes != 0);
1032
1033 return 0;
1034
1035out_bad:
1036 while (!list_empty(&list)) {
1037 data = list_entry(list.next, struct nfs_write_data, pages);
1038 list_del(&data->pages);
1039 nfs_writedata_free(data);
1040 }
1041 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001042 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return -ENOMEM;
1044}
1045
1046/*
1047 * Create an RPC task for the given write request and kick it.
1048 * The page must have been locked by the caller.
1049 *
1050 * It may happen that the page we're passed is not marked dirty.
1051 * This is the case if nfs_updatepage detects a conflicting request
1052 * that has been written but not committed.
1053 */
Trond Myklebust7d46a492006-03-20 13:44:50 -05001054static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
1056 struct nfs_page *req;
1057 struct page **pages;
1058 struct nfs_write_data *data;
1059 unsigned int count;
1060
Chuck Lever40859d72005-11-30 18:09:02 -05001061 data = nfs_writedata_alloc(NFS_SERVER(inode)->wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (!data)
1063 goto out_bad;
1064
1065 pages = data->pagevec;
1066 count = 0;
1067 while (!list_empty(head)) {
1068 req = nfs_list_entry(head->next);
1069 nfs_list_remove_request(req);
1070 nfs_list_add_request(req, &data->pages);
1071 ClearPageError(req->wb_page);
Trond Myklebustbb713d62005-12-03 15:20:14 -05001072 set_page_writeback(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 *pages++ = req->wb_page;
1074 count += req->wb_bytes;
1075 }
1076 req = nfs_list_entry(data->pages.next);
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 /* Set up the argument struct */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001079 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 nfs_execute_write(data);
1082 return 0;
1083 out_bad:
1084 while (!list_empty(head)) {
1085 struct nfs_page *req = nfs_list_entry(head->next);
1086 nfs_list_remove_request(req);
1087 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001088 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090 return -ENOMEM;
1091}
1092
Trond Myklebust7d46a492006-03-20 13:44:50 -05001093static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
1095 LIST_HEAD(one_request);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001096 int (*flush_one)(struct inode *, struct list_head *, int);
1097 struct nfs_page *req;
1098 int wpages = NFS_SERVER(inode)->wpages;
1099 int wsize = NFS_SERVER(inode)->wsize;
1100 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Trond Myklebust7d46a492006-03-20 13:44:50 -05001102 flush_one = nfs_flush_one;
1103 if (wsize < PAGE_CACHE_SIZE)
1104 flush_one = nfs_flush_multi;
1105 /* For single writes, FLUSH_STABLE is more efficient */
1106 if (npages <= wpages && npages == NFS_I(inode)->npages
1107 && nfs_list_entry(head->next)->wb_bytes <= wsize)
1108 how |= FLUSH_STABLE;
1109
1110 do {
1111 nfs_coalesce_requests(head, &one_request, wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 req = nfs_list_entry(one_request.next);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001113 error = flush_one(inode, &one_request, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (error < 0)
Trond Myklebust7d46a492006-03-20 13:44:50 -05001115 goto out_err;
1116 } while (!list_empty(head));
1117 return 0;
1118out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 while (!list_empty(head)) {
1120 req = nfs_list_entry(head->next);
1121 nfs_list_remove_request(req);
1122 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001123 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
1125 return error;
1126}
1127
1128/*
1129 * Handle a write reply that flushed part of a page.
1130 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001131static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001133 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 struct nfs_page *req = data->req;
1135 struct page *page = req->wb_page;
1136
1137 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1138 req->wb_context->dentry->d_inode->i_sb->s_id,
1139 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1140 req->wb_bytes,
1141 (long long)req_offset(req));
1142
Trond Myklebust788e7a82006-03-20 13:44:27 -05001143 if (nfs_writeback_done(task, data) != 0)
1144 return;
1145
1146 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 ClearPageUptodate(page);
1148 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001149 req->wb_context->error = task->tk_status;
1150 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 } else {
1152#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1153 if (data->verf.committed < NFS_FILE_SYNC) {
1154 if (!NFS_NEED_COMMIT(req)) {
1155 nfs_defer_commit(req);
1156 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1157 dprintk(" defer commit\n");
1158 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1159 nfs_defer_reschedule(req);
1160 dprintk(" server reboot detected\n");
1161 }
1162 } else
1163#endif
1164 dprintk(" OK\n");
1165 }
1166
1167 if (atomic_dec_and_test(&req->wb_complete))
1168 nfs_writepage_release(req);
1169}
1170
Trond Myklebust788e7a82006-03-20 13:44:27 -05001171static const struct rpc_call_ops nfs_write_partial_ops = {
1172 .rpc_call_done = nfs_writeback_done_partial,
1173 .rpc_release = nfs_writedata_release,
1174};
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176/*
1177 * Handle a write reply that flushes a whole page.
1178 *
1179 * FIXME: There is an inherent race with invalidate_inode_pages and
1180 * writebacks since the page->count is kept > 1 for as long
1181 * as the page has a write request pending.
1182 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001183static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001185 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 struct nfs_page *req;
1187 struct page *page;
1188
Trond Myklebust788e7a82006-03-20 13:44:27 -05001189 if (nfs_writeback_done(task, data) != 0)
1190 return;
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 /* Update attributes as result of writeback. */
1193 while (!list_empty(&data->pages)) {
1194 req = nfs_list_entry(data->pages.next);
1195 nfs_list_remove_request(req);
1196 page = req->wb_page;
1197
1198 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1199 req->wb_context->dentry->d_inode->i_sb->s_id,
1200 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1201 req->wb_bytes,
1202 (long long)req_offset(req));
1203
Trond Myklebust788e7a82006-03-20 13:44:27 -05001204 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 ClearPageUptodate(page);
1206 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001207 req->wb_context->error = task->tk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 end_page_writeback(page);
1209 nfs_inode_remove_request(req);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001210 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 goto next;
1212 }
1213 end_page_writeback(page);
1214
1215#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1216 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1217 nfs_inode_remove_request(req);
1218 dprintk(" OK\n");
1219 goto next;
1220 }
1221 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1222 nfs_mark_request_commit(req);
1223 dprintk(" marked for commit\n");
1224#else
1225 nfs_inode_remove_request(req);
1226#endif
1227 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001228 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
1230}
1231
Trond Myklebust788e7a82006-03-20 13:44:27 -05001232static const struct rpc_call_ops nfs_write_full_ops = {
1233 .rpc_call_done = nfs_writeback_done_full,
1234 .rpc_release = nfs_writedata_release,
1235};
1236
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238/*
1239 * This function is called when the WRITE call is complete.
1240 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001241int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 struct nfs_writeargs *argp = &data->args;
1244 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001245 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1248 task->tk_pid, task->tk_status);
1249
Trond Myklebust788e7a82006-03-20 13:44:27 -05001250 /* Call the NFS version-specific code */
1251 status = NFS_PROTO(data->inode)->write_done(task, data);
1252 if (status != 0)
1253 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001254 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1257 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1258 /* We tried a write call, but the server did not
1259 * commit data to stable storage even though we
1260 * requested it.
1261 * Note: There is a known bug in Tru64 < 5.0 in which
1262 * the server reports NFS_DATA_SYNC, but performs
1263 * NFS_FILE_SYNC. We therefore implement this checking
1264 * as a dprintk() in order to avoid filling syslog.
1265 */
1266 static unsigned long complain;
1267
1268 if (time_before(complain, jiffies)) {
1269 dprintk("NFS: faulty NFS server %s:"
1270 " (committed = %d) != (stable = %d)\n",
1271 NFS_SERVER(data->inode)->hostname,
1272 resp->verf->committed, argp->stable);
1273 complain = jiffies + 300 * HZ;
1274 }
1275 }
1276#endif
1277 /* Is this a short write? */
1278 if (task->tk_status >= 0 && resp->count < argp->count) {
1279 static unsigned long complain;
1280
Chuck Lever91d5b472006-03-20 13:44:14 -05001281 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 /* Has the server at least made some progress? */
1284 if (resp->count != 0) {
1285 /* Was this an NFSv2 write or an NFSv3 stable write? */
1286 if (resp->verf->committed != NFS_UNSTABLE) {
1287 /* Resend from where the server left off */
1288 argp->offset += resp->count;
1289 argp->pgbase += resp->count;
1290 argp->count -= resp->count;
1291 } else {
1292 /* Resend as a stable write in order to avoid
1293 * headaches in the case of a server crash.
1294 */
1295 argp->stable = NFS_FILE_SYNC;
1296 }
1297 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001298 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
1300 if (time_before(complain, jiffies)) {
1301 printk(KERN_WARNING
1302 "NFS: Server wrote zero bytes, expected %u.\n",
1303 argp->count);
1304 complain = jiffies + 300 * HZ;
1305 }
1306 /* Can't do anything about it except throw an error. */
1307 task->tk_status = -EIO;
1308 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001309 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
1312
1313#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001314void nfs_commit_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 nfs_commit_free(wdata);
1317}
1318
1319/*
1320 * Set up the argument/result storage required for the RPC call.
1321 */
1322static void nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001323 struct nfs_write_data *data,
1324 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001326 struct nfs_page *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001328 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 /* Set up the RPC argument and reply structs
1331 * NB: take care not to mess about with data->commit et al. */
1332
1333 list_splice_init(head, &data->pages);
1334 first = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 inode = first->wb_context->dentry->d_inode;
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 data->inode = inode;
1338 data->cred = first->wb_context->cred;
1339
1340 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001341 /* Note: we always request a commit of the entire inode */
1342 data->args.offset = 0;
1343 data->args.count = 0;
1344 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 data->res.fattr = &data->fattr;
1346 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001347 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001348
1349 /* Set up the initial task struct. */
1350 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1351 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 NFS_PROTO(inode)->commit_setup(data, how);
1353
1354 data->task.tk_priority = flush_task_priority(how);
1355 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Chuck Lever0bbacc42005-11-01 16:53:32 -05001357 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
1359
1360/*
1361 * Commit dirty pages
1362 */
1363static int
Chuck Lever40859d72005-11-30 18:09:02 -05001364nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
1366 struct nfs_write_data *data;
1367 struct nfs_page *req;
1368
Chuck Lever40859d72005-11-30 18:09:02 -05001369 data = nfs_commit_alloc(NFS_SERVER(inode)->wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 if (!data)
1372 goto out_bad;
1373
1374 /* Set up the argument struct */
1375 nfs_commit_rpcsetup(head, data, how);
1376
1377 nfs_execute_write(data);
1378 return 0;
1379 out_bad:
1380 while (!list_empty(head)) {
1381 req = nfs_list_entry(head->next);
1382 nfs_list_remove_request(req);
1383 nfs_mark_request_commit(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001384 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
1386 return -ENOMEM;
1387}
1388
1389/*
1390 * COMMIT call returned
1391 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001392static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001394 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1398 task->tk_pid, task->tk_status);
1399
Trond Myklebust788e7a82006-03-20 13:44:27 -05001400 /* Call the NFS version-specific code */
1401 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1402 return;
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 while (!list_empty(&data->pages)) {
1405 req = nfs_list_entry(data->pages.next);
1406 nfs_list_remove_request(req);
Christoph Lameterfd39fc82006-06-30 01:55:40 -07001407 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1410 req->wb_context->dentry->d_inode->i_sb->s_id,
1411 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1412 req->wb_bytes,
1413 (long long)req_offset(req));
1414 if (task->tk_status < 0) {
1415 req->wb_context->error = task->tk_status;
1416 nfs_inode_remove_request(req);
1417 dprintk(", error = %d\n", task->tk_status);
1418 goto next;
1419 }
1420
1421 /* Okay, COMMIT succeeded, apparently. Check the verifier
1422 * returned by the server against all stored verfs. */
1423 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1424 /* We have a match */
1425 nfs_inode_remove_request(req);
1426 dprintk(" OK\n");
1427 goto next;
1428 }
1429 /* We have a mismatch. Write the page again */
1430 dprintk(" mismatch\n");
1431 nfs_mark_request_dirty(req);
1432 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001433 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001436
1437static const struct rpc_call_ops nfs_commit_ops = {
1438 .rpc_call_done = nfs_commit_done,
1439 .rpc_release = nfs_commit_release,
1440};
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001441#else
1442static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1443{
1444 return 0;
1445}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446#endif
1447
1448static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
1449 unsigned int npages, int how)
1450{
1451 struct nfs_inode *nfsi = NFS_I(inode);
1452 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001453 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 spin_lock(&nfsi->req_lock);
1456 res = nfs_scan_dirty(inode, &head, idx_start, npages);
1457 spin_unlock(&nfsi->req_lock);
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001458 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001459 int error = nfs_flush_list(inode, &head, res, how);
1460 if (error < 0)
1461 return error;
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return res;
1464}
1465
1466#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001467int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
1469 struct nfs_inode *nfsi = NFS_I(inode);
1470 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001471 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001474 res = nfs_scan_commit(inode, &head, 0, 0);
1475 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001477 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001478 if (error < 0)
1479 return error;
1480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return res;
1482}
1483#endif
1484
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001485int nfs_sync_inode_wait(struct inode *inode, unsigned long idx_start,
1486 unsigned int npages, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001488 struct nfs_inode *nfsi = NFS_I(inode);
1489 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001490 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001491 int pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001493 how &= ~FLUSH_NOCOMMIT;
1494 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 do {
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001496 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1497 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001498 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001499 pages = nfs_scan_dirty(inode, &head, idx_start, npages);
1500 if (pages != 0) {
1501 spin_unlock(&nfsi->req_lock);
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001502 if (how & FLUSH_INVALIDATE)
1503 nfs_cancel_requests(&head);
1504 else
1505 ret = nfs_flush_list(inode, &head, pages, how);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001506 spin_lock(&nfsi->req_lock);
1507 continue;
1508 }
1509 if (nocommit)
1510 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001511 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001512 if (pages == 0)
1513 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001514 if (how & FLUSH_INVALIDATE) {
1515 spin_unlock(&nfsi->req_lock);
1516 nfs_cancel_requests(&head);
1517 spin_lock(&nfsi->req_lock);
1518 continue;
1519 }
1520 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001521 spin_unlock(&nfsi->req_lock);
1522 ret = nfs_commit_list(inode, &head, how);
1523 spin_lock(&nfsi->req_lock);
1524 } while (ret >= 0);
1525 spin_unlock(&nfsi->req_lock);
1526 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
1528
David Howellsf7b422b2006-06-09 09:34:33 -04001529int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
1531 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1532 sizeof(struct nfs_write_data),
1533 0, SLAB_HWCACHE_ALIGN,
1534 NULL, NULL);
1535 if (nfs_wdata_cachep == NULL)
1536 return -ENOMEM;
1537
Matthew Dobson93d23412006-03-26 01:37:50 -08001538 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1539 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 if (nfs_wdata_mempool == NULL)
1541 return -ENOMEM;
1542
Matthew Dobson93d23412006-03-26 01:37:50 -08001543 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1544 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 if (nfs_commit_mempool == NULL)
1546 return -ENOMEM;
1547
1548 return 0;
1549}
1550
David Brownell266bee82006-06-27 12:59:15 -07001551void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
1553 mempool_destroy(nfs_commit_mempool);
1554 mempool_destroy(nfs_wdata_mempool);
1555 if (kmem_cache_destroy(nfs_wdata_cachep))
1556 printk(KERN_INFO "nfs_write_data: not all structures were freed\n");
1557}
1558