NFS: Clean up nfs_flush_inode()
[linux-2.6-block.git] / fs / nfs / write.c
CommitLineData
1da177e4
LT
1/*
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
1da177e4
LT
49#include <linux/types.h>
50#include <linux/slab.h>
51#include <linux/mm.h>
52#include <linux/pagemap.h>
53#include <linux/file.h>
1da177e4
LT
54#include <linux/writeback.h>
55
56#include <linux/sunrpc/clnt.h>
57#include <linux/nfs_fs.h>
58#include <linux/nfs_mount.h>
59#include <linux/nfs_page.h>
3fcfab16
AM
60#include <linux/backing-dev.h>
61
1da177e4
LT
62#include <asm/uaccess.h>
63#include <linux/smp_lock.h>
64
65#include "delegation.h"
91d5b470 66#include "iostat.h"
1da177e4
LT
67
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);
1da177e4
LT
80static int nfs_wait_on_write_congestion(struct address_space *, int);
81static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int);
28c6925f 82static int nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how);
788e7a89
TM
83static const struct rpc_call_ops nfs_write_partial_ops;
84static const struct rpc_call_ops nfs_write_full_ops;
85static const struct rpc_call_ops nfs_commit_ops;
1da177e4
LT
86
87static kmem_cache_t *nfs_wdata_cachep;
3feb2d49 88static mempool_t *nfs_wdata_mempool;
1da177e4
LT
89static mempool_t *nfs_commit_mempool;
90
91static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
92
e9f7bee1 93struct nfs_write_data *nfs_commit_alloc(void)
1da177e4
LT
94{
95 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS);
40859d7e 96
1da177e4
LT
97 if (p) {
98 memset(p, 0, sizeof(*p));
99 INIT_LIST_HEAD(&p->pages);
100 }
101 return p;
102}
103
8aca67f0 104void nfs_commit_rcu_free(struct rcu_head *head)
1da177e4 105{
8aca67f0 106 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
40859d7e
CL
107 if (p && (p->pagevec != &p->page_array[0]))
108 kfree(p->pagevec);
1da177e4
LT
109 mempool_free(p, nfs_commit_mempool);
110}
111
8aca67f0
TM
112void nfs_commit_free(struct nfs_write_data *wdata)
113{
114 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
115}
116
e9f7bee1 117struct nfs_write_data *nfs_writedata_alloc(size_t len)
3feb2d49 118{
e9f7bee1 119 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3feb2d49
TM
120 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
121
122 if (p) {
123 memset(p, 0, sizeof(*p));
124 INIT_LIST_HEAD(&p->pages);
e9f7bee1 125 p->npages = pagecount;
0d0b5cb3
CL
126 if (pagecount <= ARRAY_SIZE(p->page_array))
127 p->pagevec = p->page_array;
3feb2d49 128 else {
0d0b5cb3
CL
129 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
130 if (!p->pagevec) {
3feb2d49
TM
131 mempool_free(p, nfs_wdata_mempool);
132 p = NULL;
133 }
134 }
135 }
136 return p;
137}
138
8aca67f0 139static void nfs_writedata_rcu_free(struct rcu_head *head)
3feb2d49 140{
8aca67f0 141 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
3feb2d49
TM
142 if (p && (p->pagevec != &p->page_array[0]))
143 kfree(p->pagevec);
144 mempool_free(p, nfs_wdata_mempool);
145}
146
8aca67f0
TM
147static void nfs_writedata_free(struct nfs_write_data *wdata)
148{
149 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
150}
151
963d8fe5 152void nfs_writedata_release(void *wdata)
1da177e4 153{
1da177e4
LT
154 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;
91d5b470 169 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
1da177e4
LT
170 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
e9f7bee1 216 wdata = nfs_writedata_alloc(wsize);
1da177e4
LT
217 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
bb713d6d 237 set_page_writeback(page);
1da177e4
LT
238 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;
91d5b470 259 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
1da177e4
LT
260 } 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:
951a143b 270 nfs_end_data_update(inode);
bb713d6d 271 end_page_writeback(page);
8aca67f0 272 nfs_writedata_release(wdata);
1da177e4
LT
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;
1da177e4
LT
281
282 req = nfs_update_request(ctx, inode, page, offset, count);
abd3e641
TM
283 if (IS_ERR(req))
284 return PTR_ERR(req);
1da177e4
LT
285 /* 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);
abd3e641 290 return 0;
1da177e4
LT
291}
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
91d5b470
CL
316 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
317 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
318
1da177e4
LT
319 /*
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:
d530838b 345 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
1da177e4
LT
346 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);
abd3e641 353 if (!wbc->for_writepages)
28c6925f 354 nfs_flush_mapping(page->mapping, wbc, wb_priority(wbc));
1da177e4
LT
355 } 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
91d5b470
CL
383 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
384
1da177e4
LT
385 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 }
28c6925f 393 err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
1da177e4
LT
394 if (err < 0)
395 goto out;
91d5b470 396 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
1da177e4
LT
397 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
398 err = nfs_wait_on_requests(inode, 0, 0);
399 if (err < 0)
400 goto out;
401 }
3da28eb1 402 err = nfs_commit_inode(inode, wb_priority(wbc));
1da177e4
LT
403 if (err > 0) {
404 wbc->nr_to_write -= err;
405 err = 0;
406 }
407out:
408 clear_bit(BDI_write_congested, &bdi->state);
409 wake_up_all(&nfs_write_congestion);
3fcfab16 410 congestion_end(WRITE);
1da177e4
LT
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 }
deb7d638 432 SetPagePrivate(req->wb_page);
1da177e4
LT
433 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);
deb7d638 449 ClearPagePrivate(req->wb_page);
1da177e4
LT
450 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
451 nfsi->npages--;
452 if (!nfsi->npages) {
453 spin_unlock(&nfsi->req_lock);
951a143b 454 nfs_end_data_update(inode);
1da177e4
LT
455 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);
3da28eb1
TM
499 radix_tree_tag_set(&nfsi->nfs_page_tree,
500 req->wb_index, NFS_PAGE_TAG_DIRTY);
1da177e4
LT
501 nfs_list_add_request(req, &nfsi->dirty);
502 nfsi->ndirty++;
503 spin_unlock(&nfsi->req_lock);
b1e7a8fd 504 inc_zone_page_state(req->wb_page, NR_FILE_DIRTY);
1da177e4
LT
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);
fd39fc85 532 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1da177e4
LT
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 */
c42de9dd 542static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
1da177e4
LT
543{
544 struct nfs_inode *nfsi = NFS_I(inode);
545 struct nfs_page *req;
546 unsigned long idx_end, next;
547 unsigned int res = 0;
548 int error;
549
550 if (npages == 0)
551 idx_end = ~0;
552 else
553 idx_end = idx_start + npages - 1;
554
1da177e4 555 next = idx_start;
c6a556b8 556 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
1da177e4
LT
557 if (req->wb_index > idx_end)
558 break;
559
560 next = req->wb_index + 1;
c6a556b8 561 BUG_ON(!NFS_WBACK_BUSY(req));
1da177e4
LT
562
563 atomic_inc(&req->wb_count);
564 spin_unlock(&nfsi->req_lock);
565 error = nfs_wait_on_request(req);
566 nfs_release_request(req);
c42de9dd 567 spin_lock(&nfsi->req_lock);
1da177e4
LT
568 if (error < 0)
569 return error;
1da177e4
LT
570 res++;
571 }
1da177e4
LT
572 return res;
573}
574
c42de9dd
TM
575static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
576{
577 struct nfs_inode *nfsi = NFS_I(inode);
578 int ret;
579
580 spin_lock(&nfsi->req_lock);
581 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
582 spin_unlock(&nfsi->req_lock);
583 return ret;
584}
585
83715ad5 586static void nfs_cancel_dirty_list(struct list_head *head)
d2ccddf0
TM
587{
588 struct nfs_page *req;
589 while(!list_empty(head)) {
590 req = nfs_list_entry(head->next);
591 nfs_list_remove_request(req);
592 nfs_inode_remove_request(req);
593 nfs_clear_page_writeback(req);
594 }
595}
596
83715ad5
TM
597static void nfs_cancel_commit_list(struct list_head *head)
598{
599 struct nfs_page *req;
600
601 while(!list_empty(head)) {
602 req = nfs_list_entry(head->next);
b6dff26a 603 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
83715ad5
TM
604 nfs_list_remove_request(req);
605 nfs_inode_remove_request(req);
b6dff26a 606 nfs_unlock_request(req);
83715ad5
TM
607 }
608}
609
1da177e4
LT
610/*
611 * nfs_scan_dirty - Scan an inode for dirty requests
612 * @inode: NFS inode to scan
613 * @dst: destination list
614 * @idx_start: lower bound of page->index to scan.
615 * @npages: idx_start + npages sets the upper bound to scan.
616 *
617 * Moves requests from the inode's dirty page list.
618 * The requests are *not* checked to ensure that they form a contiguous set.
619 */
620static int
621nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
622{
623 struct nfs_inode *nfsi = NFS_I(inode);
3da28eb1
TM
624 int res = 0;
625
626 if (nfsi->ndirty != 0) {
627 res = nfs_scan_lock_dirty(nfsi, dst, idx_start, npages);
628 nfsi->ndirty -= res;
3da28eb1
TM
629 if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
630 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
631 }
1da177e4
LT
632 return res;
633}
634
635#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
636/*
637 * nfs_scan_commit - Scan an inode for commit requests
638 * @inode: NFS inode to scan
639 * @dst: destination list
640 * @idx_start: lower bound of page->index to scan.
641 * @npages: idx_start + npages sets the upper bound to scan.
642 *
643 * Moves requests from the inode's 'commit' request list.
644 * The requests are *not* checked to ensure that they form a contiguous set.
645 */
646static int
647nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
648{
649 struct nfs_inode *nfsi = NFS_I(inode);
3da28eb1
TM
650 int res = 0;
651
652 if (nfsi->ncommit != 0) {
d2ccddf0 653 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
3da28eb1
TM
654 nfsi->ncommit -= res;
655 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
656 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
657 }
1da177e4
LT
658 return res;
659}
c42de9dd
TM
660#else
661static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
662{
663 return 0;
664}
1da177e4
LT
665#endif
666
667static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
668{
669 struct backing_dev_info *bdi = mapping->backing_dev_info;
670 DEFINE_WAIT(wait);
671 int ret = 0;
672
673 might_sleep();
674
675 if (!bdi_write_congested(bdi))
676 return 0;
91d5b470
CL
677
678 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
679
1da177e4
LT
680 if (intr) {
681 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
682 sigset_t oldset;
683
684 rpc_clnt_sigmask(clnt, &oldset);
685 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
686 if (bdi_write_congested(bdi)) {
687 if (signalled())
688 ret = -ERESTARTSYS;
689 else
690 schedule();
691 }
692 rpc_clnt_sigunmask(clnt, &oldset);
693 } else {
694 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
695 if (bdi_write_congested(bdi))
696 schedule();
697 }
698 finish_wait(&nfs_write_congestion, &wait);
699 return ret;
700}
701
702
703/*
704 * Try to update any existing write request, or create one if there is none.
705 * In order to match, the request's credentials must match those of
706 * the calling process.
707 *
708 * Note: Should always be called with the Page Lock held!
709 */
710static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
711 struct inode *inode, struct page *page,
712 unsigned int offset, unsigned int bytes)
713{
714 struct nfs_server *server = NFS_SERVER(inode);
715 struct nfs_inode *nfsi = NFS_I(inode);
716 struct nfs_page *req, *new = NULL;
717 unsigned long rqend, end;
718
719 end = offset + bytes;
720
721 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
722 return ERR_PTR(-ERESTARTSYS);
723 for (;;) {
724 /* Loop over all inode entries and see if we find
725 * A request for the page we wish to update
726 */
727 spin_lock(&nfsi->req_lock);
728 req = _nfs_find_request(inode, page->index);
729 if (req) {
730 if (!nfs_lock_request_dontget(req)) {
731 int error;
732 spin_unlock(&nfsi->req_lock);
733 error = nfs_wait_on_request(req);
734 nfs_release_request(req);
1dd594b2
NB
735 if (error < 0) {
736 if (new)
737 nfs_release_request(new);
1da177e4 738 return ERR_PTR(error);
1dd594b2 739 }
1da177e4
LT
740 continue;
741 }
742 spin_unlock(&nfsi->req_lock);
743 if (new)
744 nfs_release_request(new);
745 break;
746 }
747
748 if (new) {
749 int error;
750 nfs_lock_request_dontget(new);
751 error = nfs_inode_add_request(inode, new);
752 if (error) {
753 spin_unlock(&nfsi->req_lock);
754 nfs_unlock_request(new);
755 return ERR_PTR(error);
756 }
757 spin_unlock(&nfsi->req_lock);
758 nfs_mark_request_dirty(new);
759 return new;
760 }
761 spin_unlock(&nfsi->req_lock);
762
763 new = nfs_create_request(ctx, inode, page, offset, bytes);
764 if (IS_ERR(new))
765 return new;
766 }
767
768 /* We have a request for our page.
769 * If the creds don't match, or the
770 * page addresses don't match,
771 * tell the caller to wait on the conflicting
772 * request.
773 */
774 rqend = req->wb_offset + req->wb_bytes;
775 if (req->wb_context != ctx
776 || req->wb_page != page
777 || !nfs_dirty_request(req)
778 || offset > rqend || end < req->wb_offset) {
779 nfs_unlock_request(req);
780 return ERR_PTR(-EBUSY);
781 }
782
783 /* Okay, the request matches. Update the region */
784 if (offset < req->wb_offset) {
785 req->wb_offset = offset;
786 req->wb_pgbase = offset;
787 req->wb_bytes = rqend - req->wb_offset;
788 }
789
790 if (end > rqend)
791 req->wb_bytes = end - req->wb_offset;
792
793 return req;
794}
795
796int nfs_flush_incompatible(struct file *file, struct page *page)
797{
798 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
799 struct inode *inode = page->mapping->host;
800 struct nfs_page *req;
801 int status = 0;
802 /*
803 * Look for a request corresponding to this page. If there
804 * is one, and it belongs to another file, we flush it out
805 * before we try to copy anything into the page. Do this
806 * due to the lack of an ACCESS-type call in NFSv2.
807 * Also do the same if we find a request from an existing
808 * dropped page.
809 */
810 req = nfs_find_request(inode, page->index);
811 if (req) {
812 if (req->wb_page != page || ctx != req->wb_context)
813 status = nfs_wb_page(inode, page);
814 nfs_release_request(req);
815 }
816 return (status < 0) ? status : 0;
817}
818
819/*
820 * Update and possibly write a cached page of an NFS file.
821 *
822 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
823 * things with a page scheduled for an RPC call (e.g. invalidate it).
824 */
825int nfs_updatepage(struct file *file, struct page *page,
826 unsigned int offset, unsigned int count)
827{
828 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
1da177e4
LT
829 struct inode *inode = page->mapping->host;
830 struct nfs_page *req;
831 int status = 0;
832
91d5b470
CL
833 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
834
1da177e4 835 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
0bbacc40
CL
836 file->f_dentry->d_parent->d_name.name,
837 file->f_dentry->d_name.name, count,
838 (long long)(page_offset(page) +offset));
1da177e4
LT
839
840 if (IS_SYNC(inode)) {
841 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
842 if (status > 0) {
843 if (offset == 0 && status == PAGE_CACHE_SIZE)
844 SetPageUptodate(page);
845 return 0;
846 }
847 return status;
848 }
849
850 /* If we're not using byte range locks, and we know the page
851 * is entirely in cache, it may be more efficient to avoid
852 * fragmenting write requests.
853 */
ab0a3dbe 854 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
1da177e4
LT
855 loff_t end_offs = i_size_read(inode) - 1;
856 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
857
858 count += offset;
859 offset = 0;
860 if (unlikely(end_offs < 0)) {
861 /* Do nothing */
862 } else if (page->index == end_index) {
863 unsigned int pglen;
864 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
865 if (count < pglen)
866 count = pglen;
867 } else if (page->index < end_index)
868 count = PAGE_CACHE_SIZE;
869 }
870
871 /*
872 * Try to find an NFS request corresponding to this page
873 * and update it.
874 * If the existing request cannot be updated, we must flush
875 * it out now.
876 */
877 do {
878 req = nfs_update_request(ctx, inode, page, offset, count);
879 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
880 if (status != -EBUSY)
881 break;
882 /* Request could not be updated. Flush it out and try again */
883 status = nfs_wb_page(inode, page);
884 } while (status >= 0);
885 if (status < 0)
886 goto done;
887
888 status = 0;
889
890 /* Update file length */
891 nfs_grow_file(page, offset, count);
892 /* Set the PG_uptodate flag? */
893 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
894 nfs_unlock_request(req);
895done:
896 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
897 status, (long long)i_size_read(inode));
898 if (status < 0)
899 ClearPageUptodate(page);
900 return status;
901}
902
903static void nfs_writepage_release(struct nfs_page *req)
904{
905 end_page_writeback(req->wb_page);
906
907#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
908 if (!PageError(req->wb_page)) {
909 if (NFS_NEED_RESCHED(req)) {
910 nfs_mark_request_dirty(req);
911 goto out;
912 } else if (NFS_NEED_COMMIT(req)) {
913 nfs_mark_request_commit(req);
914 goto out;
915 }
916 }
917 nfs_inode_remove_request(req);
918
919out:
920 nfs_clear_commit(req);
921 nfs_clear_reschedule(req);
922#else
923 nfs_inode_remove_request(req);
924#endif
c6a556b8 925 nfs_clear_page_writeback(req);
1da177e4
LT
926}
927
928static inline int flush_task_priority(int how)
929{
930 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
931 case FLUSH_HIGHPRI:
932 return RPC_PRIORITY_HIGH;
933 case FLUSH_LOWPRI:
934 return RPC_PRIORITY_LOW;
935 }
936 return RPC_PRIORITY_NORMAL;
937}
938
939/*
940 * Set up the argument/result storage required for the RPC call.
941 */
942static void nfs_write_rpcsetup(struct nfs_page *req,
943 struct nfs_write_data *data,
788e7a89 944 const struct rpc_call_ops *call_ops,
1da177e4
LT
945 unsigned int count, unsigned int offset,
946 int how)
947{
1da177e4 948 struct inode *inode;
788e7a89 949 int flags;
1da177e4
LT
950
951 /* Set up the RPC argument and reply structs
952 * NB: take care not to mess about with data->commit et al. */
953
954 data->req = req;
955 data->inode = inode = req->wb_context->dentry->d_inode;
956 data->cred = req->wb_context->cred;
957
958 data->args.fh = NFS_FH(inode);
959 data->args.offset = req_offset(req) + offset;
960 data->args.pgbase = req->wb_pgbase + offset;
961 data->args.pages = data->pagevec;
962 data->args.count = count;
963 data->args.context = req->wb_context;
964
965 data->res.fattr = &data->fattr;
966 data->res.count = count;
967 data->res.verf = &data->verf;
0e574af1 968 nfs_fattr_init(&data->fattr);
1da177e4 969
788e7a89
TM
970 /* Set up the initial task struct. */
971 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
972 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
1da177e4
LT
973 NFS_PROTO(inode)->write_setup(data, how);
974
975 data->task.tk_priority = flush_task_priority(how);
976 data->task.tk_cookie = (unsigned long)inode;
1da177e4
LT
977
978 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
0bbacc40 979 data->task.tk_pid,
1da177e4
LT
980 inode->i_sb->s_id,
981 (long long)NFS_FILEID(inode),
982 count,
983 (unsigned long long)data->args.offset);
984}
985
986static void nfs_execute_write(struct nfs_write_data *data)
987{
988 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
989 sigset_t oldset;
990
991 rpc_clnt_sigmask(clnt, &oldset);
1da177e4 992 rpc_execute(&data->task);
1da177e4
LT
993 rpc_clnt_sigunmask(clnt, &oldset);
994}
995
996/*
997 * Generate multiple small requests to write out a single
998 * contiguous dirty area on one page.
999 */
7d46a49f 1000static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
1da177e4
LT
1001{
1002 struct nfs_page *req = nfs_list_entry(head->next);
1003 struct page *page = req->wb_page;
1004 struct nfs_write_data *data;
e9f7bee1
TM
1005 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
1006 unsigned int offset;
1da177e4
LT
1007 int requests = 0;
1008 LIST_HEAD(list);
1009
1010 nfs_list_remove_request(req);
1011
1012 nbytes = req->wb_bytes;
e9f7bee1
TM
1013 do {
1014 size_t len = min(nbytes, wsize);
1015
1016 data = nfs_writedata_alloc(len);
1da177e4
LT
1017 if (!data)
1018 goto out_bad;
1019 list_add(&data->pages, &list);
1020 requests++;
e9f7bee1
TM
1021 nbytes -= len;
1022 } while (nbytes != 0);
1da177e4
LT
1023 atomic_set(&req->wb_complete, requests);
1024
1025 ClearPageError(page);
bb713d6d 1026 set_page_writeback(page);
1da177e4
LT
1027 offset = 0;
1028 nbytes = req->wb_bytes;
1029 do {
1030 data = list_entry(list.next, struct nfs_write_data, pages);
1031 list_del_init(&data->pages);
1032
1033 data->pagevec[0] = page;
1da177e4
LT
1034
1035 if (nbytes > wsize) {
788e7a89
TM
1036 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1037 wsize, offset, how);
1da177e4
LT
1038 offset += wsize;
1039 nbytes -= wsize;
1040 } else {
788e7a89
TM
1041 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1042 nbytes, offset, how);
1da177e4
LT
1043 nbytes = 0;
1044 }
1045 nfs_execute_write(data);
1046 } while (nbytes != 0);
1047
1048 return 0;
1049
1050out_bad:
1051 while (!list_empty(&list)) {
1052 data = list_entry(list.next, struct nfs_write_data, pages);
1053 list_del(&data->pages);
8aca67f0 1054 nfs_writedata_release(data);
1da177e4
LT
1055 }
1056 nfs_mark_request_dirty(req);
c6a556b8 1057 nfs_clear_page_writeback(req);
1da177e4
LT
1058 return -ENOMEM;
1059}
1060
1061/*
1062 * Create an RPC task for the given write request and kick it.
1063 * The page must have been locked by the caller.
1064 *
1065 * It may happen that the page we're passed is not marked dirty.
1066 * This is the case if nfs_updatepage detects a conflicting request
1067 * that has been written but not committed.
1068 */
7d46a49f 1069static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
1da177e4
LT
1070{
1071 struct nfs_page *req;
1072 struct page **pages;
1073 struct nfs_write_data *data;
1074 unsigned int count;
1075
e9f7bee1 1076 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
1da177e4
LT
1077 if (!data)
1078 goto out_bad;
1079
1080 pages = data->pagevec;
1081 count = 0;
1082 while (!list_empty(head)) {
1083 req = nfs_list_entry(head->next);
1084 nfs_list_remove_request(req);
1085 nfs_list_add_request(req, &data->pages);
1086 ClearPageError(req->wb_page);
bb713d6d 1087 set_page_writeback(req->wb_page);
1da177e4
LT
1088 *pages++ = req->wb_page;
1089 count += req->wb_bytes;
1090 }
1091 req = nfs_list_entry(data->pages.next);
1092
1da177e4 1093 /* Set up the argument struct */
788e7a89 1094 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
1da177e4
LT
1095
1096 nfs_execute_write(data);
1097 return 0;
1098 out_bad:
1099 while (!list_empty(head)) {
1100 struct nfs_page *req = nfs_list_entry(head->next);
1101 nfs_list_remove_request(req);
1102 nfs_mark_request_dirty(req);
c6a556b8 1103 nfs_clear_page_writeback(req);
1da177e4
LT
1104 }
1105 return -ENOMEM;
1106}
1107
7d46a49f 1108static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
1da177e4
LT
1109{
1110 LIST_HEAD(one_request);
7d46a49f
TM
1111 int (*flush_one)(struct inode *, struct list_head *, int);
1112 struct nfs_page *req;
1113 int wpages = NFS_SERVER(inode)->wpages;
1114 int wsize = NFS_SERVER(inode)->wsize;
1115 int error;
1da177e4 1116
7d46a49f
TM
1117 flush_one = nfs_flush_one;
1118 if (wsize < PAGE_CACHE_SIZE)
1119 flush_one = nfs_flush_multi;
1120 /* For single writes, FLUSH_STABLE is more efficient */
1121 if (npages <= wpages && npages == NFS_I(inode)->npages
1122 && nfs_list_entry(head->next)->wb_bytes <= wsize)
1123 how |= FLUSH_STABLE;
1124
1125 do {
1126 nfs_coalesce_requests(head, &one_request, wpages);
1da177e4 1127 req = nfs_list_entry(one_request.next);
7d46a49f 1128 error = flush_one(inode, &one_request, how);
1da177e4 1129 if (error < 0)
7d46a49f
TM
1130 goto out_err;
1131 } while (!list_empty(head));
1132 return 0;
1133out_err:
1da177e4
LT
1134 while (!list_empty(head)) {
1135 req = nfs_list_entry(head->next);
1136 nfs_list_remove_request(req);
1137 nfs_mark_request_dirty(req);
c6a556b8 1138 nfs_clear_page_writeback(req);
1da177e4
LT
1139 }
1140 return error;
1141}
1142
1143/*
1144 * Handle a write reply that flushed part of a page.
1145 */
788e7a89 1146static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
1da177e4 1147{
788e7a89 1148 struct nfs_write_data *data = calldata;
1da177e4
LT
1149 struct nfs_page *req = data->req;
1150 struct page *page = req->wb_page;
1151
1152 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1153 req->wb_context->dentry->d_inode->i_sb->s_id,
1154 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1155 req->wb_bytes,
1156 (long long)req_offset(req));
1157
788e7a89
TM
1158 if (nfs_writeback_done(task, data) != 0)
1159 return;
1160
1161 if (task->tk_status < 0) {
1da177e4
LT
1162 ClearPageUptodate(page);
1163 SetPageError(page);
788e7a89
TM
1164 req->wb_context->error = task->tk_status;
1165 dprintk(", error = %d\n", task->tk_status);
1da177e4
LT
1166 } else {
1167#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1168 if (data->verf.committed < NFS_FILE_SYNC) {
1169 if (!NFS_NEED_COMMIT(req)) {
1170 nfs_defer_commit(req);
1171 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1172 dprintk(" defer commit\n");
1173 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1174 nfs_defer_reschedule(req);
1175 dprintk(" server reboot detected\n");
1176 }
1177 } else
1178#endif
1179 dprintk(" OK\n");
1180 }
1181
1182 if (atomic_dec_and_test(&req->wb_complete))
1183 nfs_writepage_release(req);
1184}
1185
788e7a89
TM
1186static const struct rpc_call_ops nfs_write_partial_ops = {
1187 .rpc_call_done = nfs_writeback_done_partial,
1188 .rpc_release = nfs_writedata_release,
1189};
1190
1da177e4
LT
1191/*
1192 * Handle a write reply that flushes a whole page.
1193 *
1194 * FIXME: There is an inherent race with invalidate_inode_pages and
1195 * writebacks since the page->count is kept > 1 for as long
1196 * as the page has a write request pending.
1197 */
788e7a89 1198static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1da177e4 1199{
788e7a89 1200 struct nfs_write_data *data = calldata;
1da177e4
LT
1201 struct nfs_page *req;
1202 struct page *page;
1203
788e7a89
TM
1204 if (nfs_writeback_done(task, data) != 0)
1205 return;
1206
1da177e4
LT
1207 /* Update attributes as result of writeback. */
1208 while (!list_empty(&data->pages)) {
1209 req = nfs_list_entry(data->pages.next);
1210 nfs_list_remove_request(req);
1211 page = req->wb_page;
1212
1213 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1214 req->wb_context->dentry->d_inode->i_sb->s_id,
1215 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1216 req->wb_bytes,
1217 (long long)req_offset(req));
1218
788e7a89 1219 if (task->tk_status < 0) {
1da177e4
LT
1220 ClearPageUptodate(page);
1221 SetPageError(page);
788e7a89 1222 req->wb_context->error = task->tk_status;
1da177e4
LT
1223 end_page_writeback(page);
1224 nfs_inode_remove_request(req);
788e7a89 1225 dprintk(", error = %d\n", task->tk_status);
1da177e4
LT
1226 goto next;
1227 }
1228 end_page_writeback(page);
1229
1230#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1231 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1232 nfs_inode_remove_request(req);
1233 dprintk(" OK\n");
1234 goto next;
1235 }
1236 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1237 nfs_mark_request_commit(req);
1238 dprintk(" marked for commit\n");
1239#else
1240 nfs_inode_remove_request(req);
1241#endif
1242 next:
c6a556b8 1243 nfs_clear_page_writeback(req);
1da177e4
LT
1244 }
1245}
1246
788e7a89
TM
1247static const struct rpc_call_ops nfs_write_full_ops = {
1248 .rpc_call_done = nfs_writeback_done_full,
1249 .rpc_release = nfs_writedata_release,
1250};
1251
1252
1da177e4
LT
1253/*
1254 * This function is called when the WRITE call is complete.
1255 */
462d5b32 1256int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1da177e4 1257{
1da177e4
LT
1258 struct nfs_writeargs *argp = &data->args;
1259 struct nfs_writeres *resp = &data->res;
788e7a89 1260 int status;
1da177e4
LT
1261
1262 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1263 task->tk_pid, task->tk_status);
1264
f551e44f
CL
1265 /*
1266 * ->write_done will attempt to use post-op attributes to detect
1267 * conflicting writes by other clients. A strict interpretation
1268 * of close-to-open would allow us to continue caching even if
1269 * another writer had changed the file, but some applications
1270 * depend on tighter cache coherency when writing.
1271 */
788e7a89
TM
1272 status = NFS_PROTO(data->inode)->write_done(task, data);
1273 if (status != 0)
1274 return status;
91d5b470
CL
1275 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1276
1da177e4
LT
1277#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1278 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1279 /* We tried a write call, but the server did not
1280 * commit data to stable storage even though we
1281 * requested it.
1282 * Note: There is a known bug in Tru64 < 5.0 in which
1283 * the server reports NFS_DATA_SYNC, but performs
1284 * NFS_FILE_SYNC. We therefore implement this checking
1285 * as a dprintk() in order to avoid filling syslog.
1286 */
1287 static unsigned long complain;
1288
1289 if (time_before(complain, jiffies)) {
1290 dprintk("NFS: faulty NFS server %s:"
1291 " (committed = %d) != (stable = %d)\n",
54ceac45 1292 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
1da177e4
LT
1293 resp->verf->committed, argp->stable);
1294 complain = jiffies + 300 * HZ;
1295 }
1296 }
1297#endif
1298 /* Is this a short write? */
1299 if (task->tk_status >= 0 && resp->count < argp->count) {
1300 static unsigned long complain;
1301
91d5b470
CL
1302 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1303
1da177e4
LT
1304 /* Has the server at least made some progress? */
1305 if (resp->count != 0) {
1306 /* Was this an NFSv2 write or an NFSv3 stable write? */
1307 if (resp->verf->committed != NFS_UNSTABLE) {
1308 /* Resend from where the server left off */
1309 argp->offset += resp->count;
1310 argp->pgbase += resp->count;
1311 argp->count -= resp->count;
1312 } else {
1313 /* Resend as a stable write in order to avoid
1314 * headaches in the case of a server crash.
1315 */
1316 argp->stable = NFS_FILE_SYNC;
1317 }
1318 rpc_restart_call(task);
788e7a89 1319 return -EAGAIN;
1da177e4
LT
1320 }
1321 if (time_before(complain, jiffies)) {
1322 printk(KERN_WARNING
1323 "NFS: Server wrote zero bytes, expected %u.\n",
1324 argp->count);
1325 complain = jiffies + 300 * HZ;
1326 }
1327 /* Can't do anything about it except throw an error. */
1328 task->tk_status = -EIO;
1329 }
788e7a89 1330 return 0;
1da177e4
LT
1331}
1332
1333
1334#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
963d8fe5 1335void nfs_commit_release(void *wdata)
1da177e4 1336{
1da177e4
LT
1337 nfs_commit_free(wdata);
1338}
1339
1340/*
1341 * Set up the argument/result storage required for the RPC call.
1342 */
1343static void nfs_commit_rpcsetup(struct list_head *head,
788e7a89
TM
1344 struct nfs_write_data *data,
1345 int how)
1da177e4 1346{
3da28eb1 1347 struct nfs_page *first;
1da177e4 1348 struct inode *inode;
788e7a89 1349 int flags;
1da177e4
LT
1350
1351 /* Set up the RPC argument and reply structs
1352 * NB: take care not to mess about with data->commit et al. */
1353
1354 list_splice_init(head, &data->pages);
1355 first = nfs_list_entry(data->pages.next);
1da177e4
LT
1356 inode = first->wb_context->dentry->d_inode;
1357
1da177e4
LT
1358 data->inode = inode;
1359 data->cred = first->wb_context->cred;
1360
1361 data->args.fh = NFS_FH(data->inode);
3da28eb1
TM
1362 /* Note: we always request a commit of the entire inode */
1363 data->args.offset = 0;
1364 data->args.count = 0;
1365 data->res.count = 0;
1da177e4
LT
1366 data->res.fattr = &data->fattr;
1367 data->res.verf = &data->verf;
0e574af1 1368 nfs_fattr_init(&data->fattr);
788e7a89
TM
1369
1370 /* Set up the initial task struct. */
1371 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1372 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
1da177e4
LT
1373 NFS_PROTO(inode)->commit_setup(data, how);
1374
1375 data->task.tk_priority = flush_task_priority(how);
1376 data->task.tk_cookie = (unsigned long)inode;
1da177e4 1377
0bbacc40 1378 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
1da177e4
LT
1379}
1380
1381/*
1382 * Commit dirty pages
1383 */
1384static int
40859d7e 1385nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1da177e4
LT
1386{
1387 struct nfs_write_data *data;
1388 struct nfs_page *req;
1389
e9f7bee1 1390 data = nfs_commit_alloc();
1da177e4
LT
1391
1392 if (!data)
1393 goto out_bad;
1394
1395 /* Set up the argument struct */
1396 nfs_commit_rpcsetup(head, data, how);
1397
1398 nfs_execute_write(data);
1399 return 0;
1400 out_bad:
1401 while (!list_empty(head)) {
1402 req = nfs_list_entry(head->next);
1403 nfs_list_remove_request(req);
1404 nfs_mark_request_commit(req);
83715ad5 1405 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
5c2d97cb 1406 nfs_clear_page_writeback(req);
1da177e4
LT
1407 }
1408 return -ENOMEM;
1409}
1410
1411/*
1412 * COMMIT call returned
1413 */
788e7a89 1414static void nfs_commit_done(struct rpc_task *task, void *calldata)
1da177e4 1415{
963d8fe5 1416 struct nfs_write_data *data = calldata;
1da177e4 1417 struct nfs_page *req;
1da177e4
LT
1418
1419 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1420 task->tk_pid, task->tk_status);
1421
788e7a89
TM
1422 /* Call the NFS version-specific code */
1423 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1424 return;
1425
1da177e4
LT
1426 while (!list_empty(&data->pages)) {
1427 req = nfs_list_entry(data->pages.next);
1428 nfs_list_remove_request(req);
fd39fc85 1429 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1da177e4
LT
1430
1431 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1432 req->wb_context->dentry->d_inode->i_sb->s_id,
1433 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1434 req->wb_bytes,
1435 (long long)req_offset(req));
1436 if (task->tk_status < 0) {
1437 req->wb_context->error = task->tk_status;
1438 nfs_inode_remove_request(req);
1439 dprintk(", error = %d\n", task->tk_status);
1440 goto next;
1441 }
1442
1443 /* Okay, COMMIT succeeded, apparently. Check the verifier
1444 * returned by the server against all stored verfs. */
1445 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1446 /* We have a match */
1447 nfs_inode_remove_request(req);
1448 dprintk(" OK\n");
1449 goto next;
1450 }
1451 /* We have a mismatch. Write the page again */
1452 dprintk(" mismatch\n");
1453 nfs_mark_request_dirty(req);
1454 next:
c6a556b8 1455 nfs_clear_page_writeback(req);
1da177e4 1456 }
1da177e4 1457}
788e7a89
TM
1458
1459static const struct rpc_call_ops nfs_commit_ops = {
1460 .rpc_call_done = nfs_commit_done,
1461 .rpc_release = nfs_commit_release,
1462};
c42de9dd
TM
1463#else
1464static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1465{
1466 return 0;
1467}
1da177e4
LT
1468#endif
1469
28c6925f 1470static int nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
1da177e4 1471{
28c6925f 1472 struct nfs_inode *nfsi = NFS_I(mapping->host);
1da177e4 1473 LIST_HEAD(head);
28c6925f
TM
1474 pgoff_t index = wbc->range_start >> PAGE_CACHE_SHIFT;
1475 unsigned long npages = 1 + (wbc->range_end >> PAGE_CACHE_SHIFT) - index;
7d46a49f 1476 int res;
1da177e4
LT
1477
1478 spin_lock(&nfsi->req_lock);
28c6925f 1479 res = nfs_scan_dirty(mapping->host, &head, index, npages);
1da177e4 1480 spin_unlock(&nfsi->req_lock);
ab0a3dbe 1481 if (res) {
28c6925f 1482 int error = nfs_flush_list(mapping->host, &head, res, how);
7d46a49f
TM
1483 if (error < 0)
1484 return error;
28c6925f 1485 wbc->nr_to_write -= res;
ab0a3dbe 1486 }
1da177e4
LT
1487 return res;
1488}
1489
1490#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
3da28eb1 1491int nfs_commit_inode(struct inode *inode, int how)
1da177e4
LT
1492{
1493 struct nfs_inode *nfsi = NFS_I(inode);
1494 LIST_HEAD(head);
7d46a49f 1495 int res;
1da177e4
LT
1496
1497 spin_lock(&nfsi->req_lock);
3da28eb1
TM
1498 res = nfs_scan_commit(inode, &head, 0, 0);
1499 spin_unlock(&nfsi->req_lock);
1da177e4 1500 if (res) {
7d46a49f 1501 int error = nfs_commit_list(inode, &head, how);
3da28eb1
TM
1502 if (error < 0)
1503 return error;
1504 }
1da177e4
LT
1505 return res;
1506}
1507#endif
1508
c42de9dd
TM
1509int nfs_sync_inode_wait(struct inode *inode, unsigned long idx_start,
1510 unsigned int npages, int how)
1da177e4 1511{
c42de9dd
TM
1512 struct nfs_inode *nfsi = NFS_I(inode);
1513 LIST_HEAD(head);
70b9ecbd 1514 int nocommit = how & FLUSH_NOCOMMIT;
c42de9dd 1515 int pages, ret;
1da177e4 1516
c42de9dd
TM
1517 how &= ~FLUSH_NOCOMMIT;
1518 spin_lock(&nfsi->req_lock);
1da177e4 1519 do {
c42de9dd
TM
1520 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1521 if (ret != 0)
70b9ecbd 1522 continue;
c42de9dd
TM
1523 pages = nfs_scan_dirty(inode, &head, idx_start, npages);
1524 if (pages != 0) {
1525 spin_unlock(&nfsi->req_lock);
e8e058e8 1526 if (how & FLUSH_INVALIDATE) {
83715ad5 1527 nfs_cancel_dirty_list(&head);
e8e058e8
TM
1528 ret = pages;
1529 } else
d2ccddf0 1530 ret = nfs_flush_list(inode, &head, pages, how);
c42de9dd
TM
1531 spin_lock(&nfsi->req_lock);
1532 continue;
1533 }
1534 if (nocommit)
1535 break;
d2ccddf0 1536 pages = nfs_scan_commit(inode, &head, idx_start, npages);
c42de9dd
TM
1537 if (pages == 0)
1538 break;
d2ccddf0
TM
1539 if (how & FLUSH_INVALIDATE) {
1540 spin_unlock(&nfsi->req_lock);
83715ad5 1541 nfs_cancel_commit_list(&head);
e8e058e8 1542 ret = pages;
d2ccddf0
TM
1543 spin_lock(&nfsi->req_lock);
1544 continue;
1545 }
1546 pages += nfs_scan_commit(inode, &head, 0, 0);
c42de9dd
TM
1547 spin_unlock(&nfsi->req_lock);
1548 ret = nfs_commit_list(inode, &head, how);
1549 spin_lock(&nfsi->req_lock);
1550 } while (ret >= 0);
1551 spin_unlock(&nfsi->req_lock);
1552 return ret;
1da177e4
LT
1553}
1554
f7b422b1 1555int __init nfs_init_writepagecache(void)
1da177e4
LT
1556{
1557 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1558 sizeof(struct nfs_write_data),
1559 0, SLAB_HWCACHE_ALIGN,
1560 NULL, NULL);
1561 if (nfs_wdata_cachep == NULL)
1562 return -ENOMEM;
1563
93d2341c
MD
1564 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1565 nfs_wdata_cachep);
1da177e4
LT
1566 if (nfs_wdata_mempool == NULL)
1567 return -ENOMEM;
1568
93d2341c
MD
1569 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1570 nfs_wdata_cachep);
1da177e4
LT
1571 if (nfs_commit_mempool == NULL)
1572 return -ENOMEM;
1573
1574 return 0;
1575}
1576
266bee88 1577void nfs_destroy_writepagecache(void)
1da177e4
LT
1578{
1579 mempool_destroy(nfs_commit_mempool);
1580 mempool_destroy(nfs_wdata_mempool);
1a1d92c1 1581 kmem_cache_destroy(nfs_wdata_cachep);
1da177e4
LT
1582}
1583