blob: 2beb1268bb1b582844ac802fefef60959a707862 [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 Lever40859d72005-11-30 18:09:02 -0500101 if (pagecount < NFS_PAGEVEC_SIZE)
102 p->pagevec = &p->page_array[0];
103 else {
104 size_t size = ++pagecount * sizeof(struct page *);
Eric Sesterhennbd647542006-03-20 13:44:10 -0500105 p->pagevec = kzalloc(size, GFP_NOFS);
106 if (!p->pagevec) {
Chuck Lever40859d72005-11-30 18:09:02 -0500107 mempool_free(p, nfs_commit_mempool);
108 p = NULL;
109 }
110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
112 return p;
113}
114
Trond Myklebuste17b1fc2006-03-20 13:44:35 -0500115void nfs_commit_free(struct nfs_write_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Chuck Lever40859d72005-11-30 18:09:02 -0500117 if (p && (p->pagevec != &p->page_array[0]))
118 kfree(p->pagevec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 mempool_free(p, nfs_commit_mempool);
120}
121
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500122struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
123{
124 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
125
126 if (p) {
127 memset(p, 0, sizeof(*p));
128 INIT_LIST_HEAD(&p->pages);
129 if (pagecount < NFS_PAGEVEC_SIZE)
130 p->pagevec = &p->page_array[0];
131 else {
132 size_t size = ++pagecount * sizeof(struct page *);
133 p->pagevec = kmalloc(size, GFP_NOFS);
134 if (p->pagevec) {
135 memset(p->pagevec, 0, size);
136 } else {
137 mempool_free(p, nfs_wdata_mempool);
138 p = NULL;
139 }
140 }
141 }
142 return p;
143}
144
145void nfs_writedata_free(struct nfs_write_data *p)
146{
147 if (p && (p->pagevec != &p->page_array[0]))
148 kfree(p->pagevec);
149 mempool_free(p, nfs_wdata_mempool);
150}
151
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100152void nfs_writedata_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 nfs_writedata_free(wdata);
155}
156
157/* Adjust the file length if we're writing beyond the end */
158static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
159{
160 struct inode *inode = page->mapping->host;
161 loff_t end, i_size = i_size_read(inode);
162 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
163
164 if (i_size > 0 && page->index < end_index)
165 return;
166 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
167 if (i_size >= end)
168 return;
Chuck Lever91d5b472006-03-20 13:44:14 -0500169 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 i_size_write(inode, end);
171}
172
173/* We can set the PG_uptodate flag if we see that a write request
174 * covers the full page.
175 */
176static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
177{
178 loff_t end_offs;
179
180 if (PageUptodate(page))
181 return;
182 if (base != 0)
183 return;
184 if (count == PAGE_CACHE_SIZE) {
185 SetPageUptodate(page);
186 return;
187 }
188
189 end_offs = i_size_read(page->mapping->host) - 1;
190 if (end_offs < 0)
191 return;
192 /* Is this the last page? */
193 if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT))
194 return;
195 /* This is the last page: set PG_uptodate if we cover the entire
196 * extent of the data, then zero the rest of the page.
197 */
198 if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) {
199 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
200 SetPageUptodate(page);
201 }
202}
203
204/*
205 * Write a page synchronously.
206 * Offset is the data offset within the page.
207 */
208static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
209 struct page *page, unsigned int offset, unsigned int count,
210 int how)
211{
212 unsigned int wsize = NFS_SERVER(inode)->wsize;
213 int result, written = 0;
214 struct nfs_write_data *wdata;
215
Chuck Lever40859d72005-11-30 18:09:02 -0500216 wdata = nfs_writedata_alloc(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (!wdata)
218 return -ENOMEM;
219
220 wdata->flags = how;
221 wdata->cred = ctx->cred;
222 wdata->inode = inode;
223 wdata->args.fh = NFS_FH(inode);
224 wdata->args.context = ctx;
225 wdata->args.pages = &page;
226 wdata->args.stable = NFS_FILE_SYNC;
227 wdata->args.pgbase = offset;
228 wdata->args.count = wsize;
229 wdata->res.fattr = &wdata->fattr;
230 wdata->res.verf = &wdata->verf;
231
232 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
233 inode->i_sb->s_id,
234 (long long)NFS_FILEID(inode),
235 count, (long long)(page_offset(page) + offset));
236
Trond Myklebustbb713d62005-12-03 15:20:14 -0500237 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 nfs_begin_data_update(inode);
239 do {
240 if (count < wsize)
241 wdata->args.count = count;
242 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
243
244 result = NFS_PROTO(inode)->write(wdata);
245
246 if (result < 0) {
247 /* Must mark the page invalid after I/O error */
248 ClearPageUptodate(page);
249 goto io_error;
250 }
251 if (result < wdata->args.count)
252 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
253 wdata->args.count, result);
254
255 wdata->args.offset += result;
256 wdata->args.pgbase += result;
257 written += result;
258 count -= result;
Chuck Lever91d5b472006-03-20 13:44:14 -0500259 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 } while (count);
261 /* Update file length */
262 nfs_grow_file(page, offset, written);
263 /* Set the PG_uptodate flag? */
264 nfs_mark_uptodate(page, offset, written);
265
266 if (PageError(page))
267 ClearPageError(page);
268
269io_error:
Trond Myklebust951a1432005-06-22 17:16:30 +0000270 nfs_end_data_update(inode);
Trond Myklebustbb713d62005-12-03 15:20:14 -0500271 end_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 nfs_writedata_free(wdata);
273 return written ? written : result;
274}
275
276static int nfs_writepage_async(struct nfs_open_context *ctx,
277 struct inode *inode, struct page *page,
278 unsigned int offset, unsigned int count)
279{
280 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 req = nfs_update_request(ctx, inode, page, offset, count);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100283 if (IS_ERR(req))
284 return PTR_ERR(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 /* Update file length */
286 nfs_grow_file(page, offset, count);
287 /* Set the PG_uptodate flag? */
288 nfs_mark_uptodate(page, offset, count);
289 nfs_unlock_request(req);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100290 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293static int wb_priority(struct writeback_control *wbc)
294{
295 if (wbc->for_reclaim)
296 return FLUSH_HIGHPRI;
297 if (wbc->for_kupdate)
298 return FLUSH_LOWPRI;
299 return 0;
300}
301
302/*
303 * Write an mmapped page to the server.
304 */
305int nfs_writepage(struct page *page, struct writeback_control *wbc)
306{
307 struct nfs_open_context *ctx;
308 struct inode *inode = page->mapping->host;
309 unsigned long end_index;
310 unsigned offset = PAGE_CACHE_SIZE;
311 loff_t i_size = i_size_read(inode);
312 int inode_referenced = 0;
313 int priority = wb_priority(wbc);
314 int err;
315
Chuck Lever91d5b472006-03-20 13:44:14 -0500316 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
317 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /*
320 * Note: We need to ensure that we have a reference to the inode
321 * if we are to do asynchronous writes. If not, waiting
322 * in nfs_wait_on_request() may deadlock with clear_inode().
323 *
324 * If igrab() fails here, then it is in any case safe to
325 * call nfs_wb_page(), since there will be no pending writes.
326 */
327 if (igrab(inode) != 0)
328 inode_referenced = 1;
329 end_index = i_size >> PAGE_CACHE_SHIFT;
330
331 /* Ensure we've flushed out any previous writes */
332 nfs_wb_page_priority(inode, page, priority);
333
334 /* easy case */
335 if (page->index < end_index)
336 goto do_it;
337 /* things got complicated... */
338 offset = i_size & (PAGE_CACHE_SIZE-1);
339
340 /* OK, are we completely out? */
341 err = 0; /* potential race with truncate - ignore */
342 if (page->index >= end_index+1 || !offset)
343 goto out;
344do_it:
Trond Myklebustd5308382005-11-04 15:33:38 -0500345 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (ctx == NULL) {
347 err = -EBADF;
348 goto out;
349 }
350 lock_kernel();
351 if (!IS_SYNC(inode) && inode_referenced) {
352 err = nfs_writepage_async(ctx, inode, page, 0, offset);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100353 if (!wbc->for_writepages)
354 nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 } else {
356 err = nfs_writepage_sync(ctx, inode, page, 0,
357 offset, priority);
358 if (err >= 0) {
359 if (err != offset)
360 redirty_page_for_writepage(wbc, page);
361 err = 0;
362 }
363 }
364 unlock_kernel();
365 put_nfs_open_context(ctx);
366out:
367 unlock_page(page);
368 if (inode_referenced)
369 iput(inode);
370 return err;
371}
372
373/*
374 * Note: causes nfs_update_request() to block on the assumption
375 * that the writeback is generated due to memory pressure.
376 */
377int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
378{
379 struct backing_dev_info *bdi = mapping->backing_dev_info;
380 struct inode *inode = mapping->host;
381 int err;
382
Chuck Lever91d5b472006-03-20 13:44:14 -0500383 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 err = generic_writepages(mapping, wbc);
386 if (err)
387 return err;
388 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
389 if (wbc->nonblocking)
390 return 0;
391 nfs_wait_on_write_congestion(mapping, 0);
392 }
393 err = nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
394 if (err < 0)
395 goto out;
Chuck Lever91d5b472006-03-20 13:44:14 -0500396 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 wbc->nr_to_write -= err;
398 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
399 err = nfs_wait_on_requests(inode, 0, 0);
400 if (err < 0)
401 goto out;
402 }
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000403 err = nfs_commit_inode(inode, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (err > 0) {
405 wbc->nr_to_write -= err;
406 err = 0;
407 }
408out:
409 clear_bit(BDI_write_congested, &bdi->state);
410 wake_up_all(&nfs_write_congestion);
411 return err;
412}
413
414/*
415 * Insert a write request into an inode
416 */
417static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
418{
419 struct nfs_inode *nfsi = NFS_I(inode);
420 int error;
421
422 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
423 BUG_ON(error == -EEXIST);
424 if (error)
425 return error;
426 if (!nfsi->npages) {
427 igrab(inode);
428 nfs_begin_data_update(inode);
429 if (nfs_have_delegation(inode, FMODE_WRITE))
430 nfsi->change_attr++;
431 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500432 SetPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 nfsi->npages++;
434 atomic_inc(&req->wb_count);
435 return 0;
436}
437
438/*
439 * Insert a write request into an inode
440 */
441static void nfs_inode_remove_request(struct nfs_page *req)
442{
443 struct inode *inode = req->wb_context->dentry->d_inode;
444 struct nfs_inode *nfsi = NFS_I(inode);
445
446 BUG_ON (!NFS_WBACK_BUSY(req));
447
448 spin_lock(&nfsi->req_lock);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500449 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
451 nfsi->npages--;
452 if (!nfsi->npages) {
453 spin_unlock(&nfsi->req_lock);
Trond Myklebust951a1432005-06-22 17:16:30 +0000454 nfs_end_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 iput(inode);
456 } else
457 spin_unlock(&nfsi->req_lock);
458 nfs_clear_request(req);
459 nfs_release_request(req);
460}
461
462/*
463 * Find a request
464 */
465static inline struct nfs_page *
466_nfs_find_request(struct inode *inode, unsigned long index)
467{
468 struct nfs_inode *nfsi = NFS_I(inode);
469 struct nfs_page *req;
470
471 req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index);
472 if (req)
473 atomic_inc(&req->wb_count);
474 return req;
475}
476
477static struct nfs_page *
478nfs_find_request(struct inode *inode, unsigned long index)
479{
480 struct nfs_page *req;
481 struct nfs_inode *nfsi = NFS_I(inode);
482
483 spin_lock(&nfsi->req_lock);
484 req = _nfs_find_request(inode, index);
485 spin_unlock(&nfsi->req_lock);
486 return req;
487}
488
489/*
490 * Add a request to the inode's dirty list.
491 */
492static void
493nfs_mark_request_dirty(struct nfs_page *req)
494{
495 struct inode *inode = req->wb_context->dentry->d_inode;
496 struct nfs_inode *nfsi = NFS_I(inode);
497
498 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000499 radix_tree_tag_set(&nfsi->nfs_page_tree,
500 req->wb_index, NFS_PAGE_TAG_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 nfs_list_add_request(req, &nfsi->dirty);
502 nfsi->ndirty++;
503 spin_unlock(&nfsi->req_lock);
504 inc_page_state(nr_dirty);
505 mark_inode_dirty(inode);
506}
507
508/*
509 * Check if a request is dirty
510 */
511static inline int
512nfs_dirty_request(struct nfs_page *req)
513{
514 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
515 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
516}
517
518#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
519/*
520 * Add a request to the inode's commit list.
521 */
522static void
523nfs_mark_request_commit(struct nfs_page *req)
524{
525 struct inode *inode = req->wb_context->dentry->d_inode;
526 struct nfs_inode *nfsi = NFS_I(inode);
527
528 spin_lock(&nfsi->req_lock);
529 nfs_list_add_request(req, &nfsi->commit);
530 nfsi->ncommit++;
531 spin_unlock(&nfsi->req_lock);
532 inc_page_state(nr_unstable);
533 mark_inode_dirty(inode);
534}
535#endif
536
537/*
538 * Wait for a request to complete.
539 *
540 * Interruptible by signals only if mounted with intr flag.
541 */
542static int
543nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
544{
545 struct nfs_inode *nfsi = NFS_I(inode);
546 struct nfs_page *req;
547 unsigned long idx_end, next;
548 unsigned int res = 0;
549 int error;
550
551 if (npages == 0)
552 idx_end = ~0;
553 else
554 idx_end = idx_start + npages - 1;
555
556 spin_lock(&nfsi->req_lock);
557 next = idx_start;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000558 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (req->wb_index > idx_end)
560 break;
561
562 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000563 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 atomic_inc(&req->wb_count);
566 spin_unlock(&nfsi->req_lock);
567 error = nfs_wait_on_request(req);
568 nfs_release_request(req);
569 if (error < 0)
570 return error;
571 spin_lock(&nfsi->req_lock);
572 res++;
573 }
574 spin_unlock(&nfsi->req_lock);
575 return res;
576}
577
578/*
579 * nfs_scan_dirty - Scan an inode for dirty requests
580 * @inode: NFS inode to scan
581 * @dst: destination list
582 * @idx_start: lower bound of page->index to scan.
583 * @npages: idx_start + npages sets the upper bound to scan.
584 *
585 * Moves requests from the inode's dirty page list.
586 * The requests are *not* checked to ensure that they form a contiguous set.
587 */
588static int
589nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
590{
591 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000592 int res = 0;
593
594 if (nfsi->ndirty != 0) {
595 res = nfs_scan_lock_dirty(nfsi, dst, idx_start, npages);
596 nfsi->ndirty -= res;
597 sub_page_state(nr_dirty,res);
598 if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
599 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return res;
602}
603
604#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
605/*
606 * nfs_scan_commit - Scan an inode for commit requests
607 * @inode: NFS inode to scan
608 * @dst: destination list
609 * @idx_start: lower bound of page->index to scan.
610 * @npages: idx_start + npages sets the upper bound to scan.
611 *
612 * Moves requests from the inode's 'commit' request list.
613 * The requests are *not* checked to ensure that they form a contiguous set.
614 */
615static int
616nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
617{
618 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000619 int res = 0;
620
621 if (nfsi->ncommit != 0) {
622 res = nfs_scan_list(&nfsi->commit, dst, idx_start, npages);
623 nfsi->ncommit -= res;
624 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
625 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return res;
628}
629#endif
630
631static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
632{
633 struct backing_dev_info *bdi = mapping->backing_dev_info;
634 DEFINE_WAIT(wait);
635 int ret = 0;
636
637 might_sleep();
638
639 if (!bdi_write_congested(bdi))
640 return 0;
Chuck Lever91d5b472006-03-20 13:44:14 -0500641
642 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (intr) {
645 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
646 sigset_t oldset;
647
648 rpc_clnt_sigmask(clnt, &oldset);
649 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
650 if (bdi_write_congested(bdi)) {
651 if (signalled())
652 ret = -ERESTARTSYS;
653 else
654 schedule();
655 }
656 rpc_clnt_sigunmask(clnt, &oldset);
657 } else {
658 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
659 if (bdi_write_congested(bdi))
660 schedule();
661 }
662 finish_wait(&nfs_write_congestion, &wait);
663 return ret;
664}
665
666
667/*
668 * Try to update any existing write request, or create one if there is none.
669 * In order to match, the request's credentials must match those of
670 * the calling process.
671 *
672 * Note: Should always be called with the Page Lock held!
673 */
674static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
675 struct inode *inode, struct page *page,
676 unsigned int offset, unsigned int bytes)
677{
678 struct nfs_server *server = NFS_SERVER(inode);
679 struct nfs_inode *nfsi = NFS_I(inode);
680 struct nfs_page *req, *new = NULL;
681 unsigned long rqend, end;
682
683 end = offset + bytes;
684
685 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
686 return ERR_PTR(-ERESTARTSYS);
687 for (;;) {
688 /* Loop over all inode entries and see if we find
689 * A request for the page we wish to update
690 */
691 spin_lock(&nfsi->req_lock);
692 req = _nfs_find_request(inode, page->index);
693 if (req) {
694 if (!nfs_lock_request_dontget(req)) {
695 int error;
696 spin_unlock(&nfsi->req_lock);
697 error = nfs_wait_on_request(req);
698 nfs_release_request(req);
Neil Brown1dd594b2006-03-20 13:44:04 -0500699 if (error < 0) {
700 if (new)
701 nfs_release_request(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return ERR_PTR(error);
Neil Brown1dd594b2006-03-20 13:44:04 -0500703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 continue;
705 }
706 spin_unlock(&nfsi->req_lock);
707 if (new)
708 nfs_release_request(new);
709 break;
710 }
711
712 if (new) {
713 int error;
714 nfs_lock_request_dontget(new);
715 error = nfs_inode_add_request(inode, new);
716 if (error) {
717 spin_unlock(&nfsi->req_lock);
718 nfs_unlock_request(new);
719 return ERR_PTR(error);
720 }
721 spin_unlock(&nfsi->req_lock);
722 nfs_mark_request_dirty(new);
723 return new;
724 }
725 spin_unlock(&nfsi->req_lock);
726
727 new = nfs_create_request(ctx, inode, page, offset, bytes);
728 if (IS_ERR(new))
729 return new;
730 }
731
732 /* We have a request for our page.
733 * If the creds don't match, or the
734 * page addresses don't match,
735 * tell the caller to wait on the conflicting
736 * request.
737 */
738 rqend = req->wb_offset + req->wb_bytes;
739 if (req->wb_context != ctx
740 || req->wb_page != page
741 || !nfs_dirty_request(req)
742 || offset > rqend || end < req->wb_offset) {
743 nfs_unlock_request(req);
744 return ERR_PTR(-EBUSY);
745 }
746
747 /* Okay, the request matches. Update the region */
748 if (offset < req->wb_offset) {
749 req->wb_offset = offset;
750 req->wb_pgbase = offset;
751 req->wb_bytes = rqend - req->wb_offset;
752 }
753
754 if (end > rqend)
755 req->wb_bytes = end - req->wb_offset;
756
757 return req;
758}
759
760int nfs_flush_incompatible(struct file *file, struct page *page)
761{
762 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
763 struct inode *inode = page->mapping->host;
764 struct nfs_page *req;
765 int status = 0;
766 /*
767 * Look for a request corresponding to this page. If there
768 * is one, and it belongs to another file, we flush it out
769 * before we try to copy anything into the page. Do this
770 * due to the lack of an ACCESS-type call in NFSv2.
771 * Also do the same if we find a request from an existing
772 * dropped page.
773 */
774 req = nfs_find_request(inode, page->index);
775 if (req) {
776 if (req->wb_page != page || ctx != req->wb_context)
777 status = nfs_wb_page(inode, page);
778 nfs_release_request(req);
779 }
780 return (status < 0) ? status : 0;
781}
782
783/*
784 * Update and possibly write a cached page of an NFS file.
785 *
786 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
787 * things with a page scheduled for an RPC call (e.g. invalidate it).
788 */
789int nfs_updatepage(struct file *file, struct page *page,
790 unsigned int offset, unsigned int count)
791{
792 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 struct inode *inode = page->mapping->host;
794 struct nfs_page *req;
795 int status = 0;
796
Chuck Lever91d5b472006-03-20 13:44:14 -0500797 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500800 file->f_dentry->d_parent->d_name.name,
801 file->f_dentry->d_name.name, count,
802 (long long)(page_offset(page) +offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 if (IS_SYNC(inode)) {
805 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
806 if (status > 0) {
807 if (offset == 0 && status == PAGE_CACHE_SIZE)
808 SetPageUptodate(page);
809 return 0;
810 }
811 return status;
812 }
813
814 /* If we're not using byte range locks, and we know the page
815 * is entirely in cache, it may be more efficient to avoid
816 * fragmenting write requests.
817 */
Trond Myklebustab0a3db2005-06-22 17:16:30 +0000818 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 loff_t end_offs = i_size_read(inode) - 1;
820 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
821
822 count += offset;
823 offset = 0;
824 if (unlikely(end_offs < 0)) {
825 /* Do nothing */
826 } else if (page->index == end_index) {
827 unsigned int pglen;
828 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
829 if (count < pglen)
830 count = pglen;
831 } else if (page->index < end_index)
832 count = PAGE_CACHE_SIZE;
833 }
834
835 /*
836 * Try to find an NFS request corresponding to this page
837 * and update it.
838 * If the existing request cannot be updated, we must flush
839 * it out now.
840 */
841 do {
842 req = nfs_update_request(ctx, inode, page, offset, count);
843 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
844 if (status != -EBUSY)
845 break;
846 /* Request could not be updated. Flush it out and try again */
847 status = nfs_wb_page(inode, page);
848 } while (status >= 0);
849 if (status < 0)
850 goto done;
851
852 status = 0;
853
854 /* Update file length */
855 nfs_grow_file(page, offset, count);
856 /* Set the PG_uptodate flag? */
857 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
858 nfs_unlock_request(req);
859done:
860 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
861 status, (long long)i_size_read(inode));
862 if (status < 0)
863 ClearPageUptodate(page);
864 return status;
865}
866
867static void nfs_writepage_release(struct nfs_page *req)
868{
869 end_page_writeback(req->wb_page);
870
871#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
872 if (!PageError(req->wb_page)) {
873 if (NFS_NEED_RESCHED(req)) {
874 nfs_mark_request_dirty(req);
875 goto out;
876 } else if (NFS_NEED_COMMIT(req)) {
877 nfs_mark_request_commit(req);
878 goto out;
879 }
880 }
881 nfs_inode_remove_request(req);
882
883out:
884 nfs_clear_commit(req);
885 nfs_clear_reschedule(req);
886#else
887 nfs_inode_remove_request(req);
888#endif
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000889 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
892static inline int flush_task_priority(int how)
893{
894 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
895 case FLUSH_HIGHPRI:
896 return RPC_PRIORITY_HIGH;
897 case FLUSH_LOWPRI:
898 return RPC_PRIORITY_LOW;
899 }
900 return RPC_PRIORITY_NORMAL;
901}
902
903/*
904 * Set up the argument/result storage required for the RPC call.
905 */
906static void nfs_write_rpcsetup(struct nfs_page *req,
907 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500908 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 unsigned int count, unsigned int offset,
910 int how)
911{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -0500913 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 /* Set up the RPC argument and reply structs
916 * NB: take care not to mess about with data->commit et al. */
917
918 data->req = req;
919 data->inode = inode = req->wb_context->dentry->d_inode;
920 data->cred = req->wb_context->cred;
921
922 data->args.fh = NFS_FH(inode);
923 data->args.offset = req_offset(req) + offset;
924 data->args.pgbase = req->wb_pgbase + offset;
925 data->args.pages = data->pagevec;
926 data->args.count = count;
927 data->args.context = req->wb_context;
928
929 data->res.fattr = &data->fattr;
930 data->res.count = count;
931 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400932 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Trond Myklebust788e7a82006-03-20 13:44:27 -0500934 /* Set up the initial task struct. */
935 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
936 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 NFS_PROTO(inode)->write_setup(data, how);
938
939 data->task.tk_priority = flush_task_priority(how);
940 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500943 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 inode->i_sb->s_id,
945 (long long)NFS_FILEID(inode),
946 count,
947 (unsigned long long)data->args.offset);
948}
949
950static void nfs_execute_write(struct nfs_write_data *data)
951{
952 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
953 sigset_t oldset;
954
955 rpc_clnt_sigmask(clnt, &oldset);
956 lock_kernel();
957 rpc_execute(&data->task);
958 unlock_kernel();
959 rpc_clnt_sigunmask(clnt, &oldset);
960}
961
962/*
963 * Generate multiple small requests to write out a single
964 * contiguous dirty area on one page.
965 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500966static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
968 struct nfs_page *req = nfs_list_entry(head->next);
969 struct page *page = req->wb_page;
970 struct nfs_write_data *data;
971 unsigned int wsize = NFS_SERVER(inode)->wsize;
972 unsigned int nbytes, offset;
973 int requests = 0;
974 LIST_HEAD(list);
975
976 nfs_list_remove_request(req);
977
978 nbytes = req->wb_bytes;
979 for (;;) {
Chuck Lever40859d72005-11-30 18:09:02 -0500980 data = nfs_writedata_alloc(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (!data)
982 goto out_bad;
983 list_add(&data->pages, &list);
984 requests++;
985 if (nbytes <= wsize)
986 break;
987 nbytes -= wsize;
988 }
989 atomic_set(&req->wb_complete, requests);
990
991 ClearPageError(page);
Trond Myklebustbb713d62005-12-03 15:20:14 -0500992 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 offset = 0;
994 nbytes = req->wb_bytes;
995 do {
996 data = list_entry(list.next, struct nfs_write_data, pages);
997 list_del_init(&data->pages);
998
999 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 if (nbytes > wsize) {
Trond Myklebust788e7a82006-03-20 13:44:27 -05001002 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1003 wsize, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 offset += wsize;
1005 nbytes -= wsize;
1006 } else {
Trond Myklebust788e7a82006-03-20 13:44:27 -05001007 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1008 nbytes, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 nbytes = 0;
1010 }
1011 nfs_execute_write(data);
1012 } while (nbytes != 0);
1013
1014 return 0;
1015
1016out_bad:
1017 while (!list_empty(&list)) {
1018 data = list_entry(list.next, struct nfs_write_data, pages);
1019 list_del(&data->pages);
1020 nfs_writedata_free(data);
1021 }
1022 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001023 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return -ENOMEM;
1025}
1026
1027/*
1028 * Create an RPC task for the given write request and kick it.
1029 * The page must have been locked by the caller.
1030 *
1031 * It may happen that the page we're passed is not marked dirty.
1032 * This is the case if nfs_updatepage detects a conflicting request
1033 * that has been written but not committed.
1034 */
Trond Myklebust7d46a492006-03-20 13:44:50 -05001035static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
1037 struct nfs_page *req;
1038 struct page **pages;
1039 struct nfs_write_data *data;
1040 unsigned int count;
1041
Chuck Lever40859d72005-11-30 18:09:02 -05001042 data = nfs_writedata_alloc(NFS_SERVER(inode)->wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (!data)
1044 goto out_bad;
1045
1046 pages = data->pagevec;
1047 count = 0;
1048 while (!list_empty(head)) {
1049 req = nfs_list_entry(head->next);
1050 nfs_list_remove_request(req);
1051 nfs_list_add_request(req, &data->pages);
1052 ClearPageError(req->wb_page);
Trond Myklebustbb713d62005-12-03 15:20:14 -05001053 set_page_writeback(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 *pages++ = req->wb_page;
1055 count += req->wb_bytes;
1056 }
1057 req = nfs_list_entry(data->pages.next);
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 /* Set up the argument struct */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001060 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 nfs_execute_write(data);
1063 return 0;
1064 out_bad:
1065 while (!list_empty(head)) {
1066 struct nfs_page *req = nfs_list_entry(head->next);
1067 nfs_list_remove_request(req);
1068 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001069 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071 return -ENOMEM;
1072}
1073
Trond Myklebust7d46a492006-03-20 13:44:50 -05001074static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
1076 LIST_HEAD(one_request);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001077 int (*flush_one)(struct inode *, struct list_head *, int);
1078 struct nfs_page *req;
1079 int wpages = NFS_SERVER(inode)->wpages;
1080 int wsize = NFS_SERVER(inode)->wsize;
1081 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Trond Myklebust7d46a492006-03-20 13:44:50 -05001083 flush_one = nfs_flush_one;
1084 if (wsize < PAGE_CACHE_SIZE)
1085 flush_one = nfs_flush_multi;
1086 /* For single writes, FLUSH_STABLE is more efficient */
1087 if (npages <= wpages && npages == NFS_I(inode)->npages
1088 && nfs_list_entry(head->next)->wb_bytes <= wsize)
1089 how |= FLUSH_STABLE;
1090
1091 do {
1092 nfs_coalesce_requests(head, &one_request, wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 req = nfs_list_entry(one_request.next);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001094 error = flush_one(inode, &one_request, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (error < 0)
Trond Myklebust7d46a492006-03-20 13:44:50 -05001096 goto out_err;
1097 } while (!list_empty(head));
1098 return 0;
1099out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 while (!list_empty(head)) {
1101 req = nfs_list_entry(head->next);
1102 nfs_list_remove_request(req);
1103 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001104 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106 return error;
1107}
1108
1109/*
1110 * Handle a write reply that flushed part of a page.
1111 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001112static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001114 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 struct nfs_page *req = data->req;
1116 struct page *page = req->wb_page;
1117
1118 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1119 req->wb_context->dentry->d_inode->i_sb->s_id,
1120 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1121 req->wb_bytes,
1122 (long long)req_offset(req));
1123
Trond Myklebust788e7a82006-03-20 13:44:27 -05001124 if (nfs_writeback_done(task, data) != 0)
1125 return;
1126
1127 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 ClearPageUptodate(page);
1129 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001130 req->wb_context->error = task->tk_status;
1131 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 } else {
1133#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1134 if (data->verf.committed < NFS_FILE_SYNC) {
1135 if (!NFS_NEED_COMMIT(req)) {
1136 nfs_defer_commit(req);
1137 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1138 dprintk(" defer commit\n");
1139 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1140 nfs_defer_reschedule(req);
1141 dprintk(" server reboot detected\n");
1142 }
1143 } else
1144#endif
1145 dprintk(" OK\n");
1146 }
1147
1148 if (atomic_dec_and_test(&req->wb_complete))
1149 nfs_writepage_release(req);
1150}
1151
Trond Myklebust788e7a82006-03-20 13:44:27 -05001152static const struct rpc_call_ops nfs_write_partial_ops = {
1153 .rpc_call_done = nfs_writeback_done_partial,
1154 .rpc_release = nfs_writedata_release,
1155};
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157/*
1158 * Handle a write reply that flushes a whole page.
1159 *
1160 * FIXME: There is an inherent race with invalidate_inode_pages and
1161 * writebacks since the page->count is kept > 1 for as long
1162 * as the page has a write request pending.
1163 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001164static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001166 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 struct nfs_page *req;
1168 struct page *page;
1169
Trond Myklebust788e7a82006-03-20 13:44:27 -05001170 if (nfs_writeback_done(task, data) != 0)
1171 return;
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 /* Update attributes as result of writeback. */
1174 while (!list_empty(&data->pages)) {
1175 req = nfs_list_entry(data->pages.next);
1176 nfs_list_remove_request(req);
1177 page = req->wb_page;
1178
1179 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1180 req->wb_context->dentry->d_inode->i_sb->s_id,
1181 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1182 req->wb_bytes,
1183 (long long)req_offset(req));
1184
Trond Myklebust788e7a82006-03-20 13:44:27 -05001185 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 ClearPageUptodate(page);
1187 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001188 req->wb_context->error = task->tk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 end_page_writeback(page);
1190 nfs_inode_remove_request(req);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001191 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 goto next;
1193 }
1194 end_page_writeback(page);
1195
1196#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1197 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1198 nfs_inode_remove_request(req);
1199 dprintk(" OK\n");
1200 goto next;
1201 }
1202 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1203 nfs_mark_request_commit(req);
1204 dprintk(" marked for commit\n");
1205#else
1206 nfs_inode_remove_request(req);
1207#endif
1208 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001209 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
1211}
1212
Trond Myklebust788e7a82006-03-20 13:44:27 -05001213static const struct rpc_call_ops nfs_write_full_ops = {
1214 .rpc_call_done = nfs_writeback_done_full,
1215 .rpc_release = nfs_writedata_release,
1216};
1217
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219/*
1220 * This function is called when the WRITE call is complete.
1221 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001222int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 struct nfs_writeargs *argp = &data->args;
1225 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001226 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1229 task->tk_pid, task->tk_status);
1230
Trond Myklebust788e7a82006-03-20 13:44:27 -05001231 /* Call the NFS version-specific code */
1232 status = NFS_PROTO(data->inode)->write_done(task, data);
1233 if (status != 0)
1234 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001235 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1238 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1239 /* We tried a write call, but the server did not
1240 * commit data to stable storage even though we
1241 * requested it.
1242 * Note: There is a known bug in Tru64 < 5.0 in which
1243 * the server reports NFS_DATA_SYNC, but performs
1244 * NFS_FILE_SYNC. We therefore implement this checking
1245 * as a dprintk() in order to avoid filling syslog.
1246 */
1247 static unsigned long complain;
1248
1249 if (time_before(complain, jiffies)) {
1250 dprintk("NFS: faulty NFS server %s:"
1251 " (committed = %d) != (stable = %d)\n",
1252 NFS_SERVER(data->inode)->hostname,
1253 resp->verf->committed, argp->stable);
1254 complain = jiffies + 300 * HZ;
1255 }
1256 }
1257#endif
1258 /* Is this a short write? */
1259 if (task->tk_status >= 0 && resp->count < argp->count) {
1260 static unsigned long complain;
1261
Chuck Lever91d5b472006-03-20 13:44:14 -05001262 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 /* Has the server at least made some progress? */
1265 if (resp->count != 0) {
1266 /* Was this an NFSv2 write or an NFSv3 stable write? */
1267 if (resp->verf->committed != NFS_UNSTABLE) {
1268 /* Resend from where the server left off */
1269 argp->offset += resp->count;
1270 argp->pgbase += resp->count;
1271 argp->count -= resp->count;
1272 } else {
1273 /* Resend as a stable write in order to avoid
1274 * headaches in the case of a server crash.
1275 */
1276 argp->stable = NFS_FILE_SYNC;
1277 }
1278 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001279 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281 if (time_before(complain, jiffies)) {
1282 printk(KERN_WARNING
1283 "NFS: Server wrote zero bytes, expected %u.\n",
1284 argp->count);
1285 complain = jiffies + 300 * HZ;
1286 }
1287 /* Can't do anything about it except throw an error. */
1288 task->tk_status = -EIO;
1289 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001290 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
1292
1293
1294#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001295void nfs_commit_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 nfs_commit_free(wdata);
1298}
1299
1300/*
1301 * Set up the argument/result storage required for the RPC call.
1302 */
1303static void nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001304 struct nfs_write_data *data,
1305 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001307 struct nfs_page *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001309 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 /* Set up the RPC argument and reply structs
1312 * NB: take care not to mess about with data->commit et al. */
1313
1314 list_splice_init(head, &data->pages);
1315 first = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 inode = first->wb_context->dentry->d_inode;
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 data->inode = inode;
1319 data->cred = first->wb_context->cred;
1320
1321 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001322 /* Note: we always request a commit of the entire inode */
1323 data->args.offset = 0;
1324 data->args.count = 0;
1325 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 data->res.fattr = &data->fattr;
1327 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001328 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001329
1330 /* Set up the initial task struct. */
1331 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1332 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 NFS_PROTO(inode)->commit_setup(data, how);
1334
1335 data->task.tk_priority = flush_task_priority(how);
1336 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Chuck Lever0bbacc42005-11-01 16:53:32 -05001338 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
1341/*
1342 * Commit dirty pages
1343 */
1344static int
Chuck Lever40859d72005-11-30 18:09:02 -05001345nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
1347 struct nfs_write_data *data;
1348 struct nfs_page *req;
1349
Chuck Lever40859d72005-11-30 18:09:02 -05001350 data = nfs_commit_alloc(NFS_SERVER(inode)->wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 if (!data)
1353 goto out_bad;
1354
1355 /* Set up the argument struct */
1356 nfs_commit_rpcsetup(head, data, how);
1357
1358 nfs_execute_write(data);
1359 return 0;
1360 out_bad:
1361 while (!list_empty(head)) {
1362 req = nfs_list_entry(head->next);
1363 nfs_list_remove_request(req);
1364 nfs_mark_request_commit(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001365 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
1367 return -ENOMEM;
1368}
1369
1370/*
1371 * COMMIT call returned
1372 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001373static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001375 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 struct nfs_page *req;
1377 int res = 0;
1378
1379 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1380 task->tk_pid, task->tk_status);
1381
Trond Myklebust788e7a82006-03-20 13:44:27 -05001382 /* Call the NFS version-specific code */
1383 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1384 return;
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 while (!list_empty(&data->pages)) {
1387 req = nfs_list_entry(data->pages.next);
1388 nfs_list_remove_request(req);
1389
1390 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1391 req->wb_context->dentry->d_inode->i_sb->s_id,
1392 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1393 req->wb_bytes,
1394 (long long)req_offset(req));
1395 if (task->tk_status < 0) {
1396 req->wb_context->error = task->tk_status;
1397 nfs_inode_remove_request(req);
1398 dprintk(", error = %d\n", task->tk_status);
1399 goto next;
1400 }
1401
1402 /* Okay, COMMIT succeeded, apparently. Check the verifier
1403 * returned by the server against all stored verfs. */
1404 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1405 /* We have a match */
1406 nfs_inode_remove_request(req);
1407 dprintk(" OK\n");
1408 goto next;
1409 }
1410 /* We have a mismatch. Write the page again */
1411 dprintk(" mismatch\n");
1412 nfs_mark_request_dirty(req);
1413 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001414 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 res++;
1416 }
1417 sub_page_state(nr_unstable,res);
1418}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001419
1420static const struct rpc_call_ops nfs_commit_ops = {
1421 .rpc_call_done = nfs_commit_done,
1422 .rpc_release = nfs_commit_release,
1423};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424#endif
1425
1426static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
1427 unsigned int npages, int how)
1428{
1429 struct nfs_inode *nfsi = NFS_I(inode);
1430 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001431 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 spin_lock(&nfsi->req_lock);
1434 res = nfs_scan_dirty(inode, &head, idx_start, npages);
1435 spin_unlock(&nfsi->req_lock);
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001436 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001437 int error = nfs_flush_list(inode, &head, res, how);
1438 if (error < 0)
1439 return error;
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return res;
1442}
1443
1444#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001445int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
1447 struct nfs_inode *nfsi = NFS_I(inode);
1448 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001449 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001452 res = nfs_scan_commit(inode, &head, 0, 0);
1453 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001455 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001456 if (error < 0)
1457 return error;
1458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return res;
1460}
1461#endif
1462
1463int nfs_sync_inode(struct inode *inode, unsigned long idx_start,
1464 unsigned int npages, int how)
1465{
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001466 int nocommit = how & FLUSH_NOCOMMIT;
1467 int wait = how & FLUSH_WAIT;
1468 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001470 how &= ~(FLUSH_WAIT|FLUSH_NOCOMMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 do {
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001473 if (wait) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 error = nfs_wait_on_requests(inode, idx_start, npages);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001475 if (error != 0)
1476 continue;
1477 }
1478 error = nfs_flush_inode(inode, idx_start, npages, how);
1479 if (error != 0)
1480 continue;
1481 if (!nocommit)
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001482 error = nfs_commit_inode(inode, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 } while (error > 0);
1484 return error;
1485}
1486
1487int nfs_init_writepagecache(void)
1488{
1489 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1490 sizeof(struct nfs_write_data),
1491 0, SLAB_HWCACHE_ALIGN,
1492 NULL, NULL);
1493 if (nfs_wdata_cachep == NULL)
1494 return -ENOMEM;
1495
1496 nfs_wdata_mempool = mempool_create(MIN_POOL_WRITE,
1497 mempool_alloc_slab,
1498 mempool_free_slab,
1499 nfs_wdata_cachep);
1500 if (nfs_wdata_mempool == NULL)
1501 return -ENOMEM;
1502
1503 nfs_commit_mempool = mempool_create(MIN_POOL_COMMIT,
1504 mempool_alloc_slab,
1505 mempool_free_slab,
1506 nfs_wdata_cachep);
1507 if (nfs_commit_mempool == NULL)
1508 return -ENOMEM;
1509
1510 return 0;
1511}
1512
1513void nfs_destroy_writepagecache(void)
1514{
1515 mempool_destroy(nfs_commit_mempool);
1516 mempool_destroy(nfs_wdata_mempool);
1517 if (kmem_cache_destroy(nfs_wdata_cachep))
1518 printk(KERN_INFO "nfs_write_data: not all structures were freed\n");
1519}
1520