]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/nfs/write.c
[PATCH] fs: Removing useless casts
[net-next-2.6.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>
54#include <linux/mpage.h>
55#include <linux/writeback.h>
56
57#include <linux/sunrpc/clnt.h>
58#include <linux/nfs_fs.h>
59#include <linux/nfs_mount.h>
60#include <linux/nfs_page.h>
61#include <asm/uaccess.h>
62#include <linux/smp_lock.h>
63
64#include "delegation.h"
91d5b470 65#include "iostat.h"
1da177e4
LT
66
67#define NFSDBG_FACILITY NFSDBG_PAGECACHE
68
69#define MIN_POOL_WRITE (32)
70#define MIN_POOL_COMMIT (4)
71
72/*
73 * Local function declarations
74 */
75static struct nfs_page * nfs_update_request(struct nfs_open_context*,
76 struct inode *,
77 struct page *,
78 unsigned int, unsigned int);
1da177e4
LT
79static int nfs_wait_on_write_congestion(struct address_space *, int);
80static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int);
81static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
82 unsigned int npages, 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
e17b1fc4 104void nfs_commit_free(struct nfs_write_data *p)
1da177e4 105{
40859d7e
CL
106 if (p && (p->pagevec != &p->page_array[0]))
107 kfree(p->pagevec);
1da177e4
LT
108 mempool_free(p, nfs_commit_mempool);
109}
110
e9f7bee1 111struct nfs_write_data *nfs_writedata_alloc(size_t len)
3feb2d49 112{
e9f7bee1 113 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3feb2d49
TM
114 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
115
116 if (p) {
117 memset(p, 0, sizeof(*p));
118 INIT_LIST_HEAD(&p->pages);
e9f7bee1 119 p->npages = pagecount;
0d0b5cb3
CL
120 if (pagecount <= ARRAY_SIZE(p->page_array))
121 p->pagevec = p->page_array;
3feb2d49 122 else {
0d0b5cb3
CL
123 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
124 if (!p->pagevec) {
3feb2d49
TM
125 mempool_free(p, nfs_wdata_mempool);
126 p = NULL;
127 }
128 }
129 }
130 return p;
131}
132
e4e20512 133static void nfs_writedata_free(struct nfs_write_data *p)
3feb2d49
TM
134{
135 if (p && (p->pagevec != &p->page_array[0]))
136 kfree(p->pagevec);
137 mempool_free(p, nfs_wdata_mempool);
138}
139
963d8fe5 140void nfs_writedata_release(void *wdata)
1da177e4 141{
1da177e4
LT
142 nfs_writedata_free(wdata);
143}
144
145/* Adjust the file length if we're writing beyond the end */
146static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
147{
148 struct inode *inode = page->mapping->host;
149 loff_t end, i_size = i_size_read(inode);
150 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
151
152 if (i_size > 0 && page->index < end_index)
153 return;
154 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
155 if (i_size >= end)
156 return;
91d5b470 157 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
1da177e4
LT
158 i_size_write(inode, end);
159}
160
161/* We can set the PG_uptodate flag if we see that a write request
162 * covers the full page.
163 */
164static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
165{
166 loff_t end_offs;
167
168 if (PageUptodate(page))
169 return;
170 if (base != 0)
171 return;
172 if (count == PAGE_CACHE_SIZE) {
173 SetPageUptodate(page);
174 return;
175 }
176
177 end_offs = i_size_read(page->mapping->host) - 1;
178 if (end_offs < 0)
179 return;
180 /* Is this the last page? */
181 if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT))
182 return;
183 /* This is the last page: set PG_uptodate if we cover the entire
184 * extent of the data, then zero the rest of the page.
185 */
186 if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) {
187 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
188 SetPageUptodate(page);
189 }
190}
191
192/*
193 * Write a page synchronously.
194 * Offset is the data offset within the page.
195 */
196static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
197 struct page *page, unsigned int offset, unsigned int count,
198 int how)
199{
200 unsigned int wsize = NFS_SERVER(inode)->wsize;
201 int result, written = 0;
202 struct nfs_write_data *wdata;
203
e9f7bee1 204 wdata = nfs_writedata_alloc(wsize);
1da177e4
LT
205 if (!wdata)
206 return -ENOMEM;
207
208 wdata->flags = how;
209 wdata->cred = ctx->cred;
210 wdata->inode = inode;
211 wdata->args.fh = NFS_FH(inode);
212 wdata->args.context = ctx;
213 wdata->args.pages = &page;
214 wdata->args.stable = NFS_FILE_SYNC;
215 wdata->args.pgbase = offset;
216 wdata->args.count = wsize;
217 wdata->res.fattr = &wdata->fattr;
218 wdata->res.verf = &wdata->verf;
219
220 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
221 inode->i_sb->s_id,
222 (long long)NFS_FILEID(inode),
223 count, (long long)(page_offset(page) + offset));
224
bb713d6d 225 set_page_writeback(page);
1da177e4
LT
226 nfs_begin_data_update(inode);
227 do {
228 if (count < wsize)
229 wdata->args.count = count;
230 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
231
232 result = NFS_PROTO(inode)->write(wdata);
233
234 if (result < 0) {
235 /* Must mark the page invalid after I/O error */
236 ClearPageUptodate(page);
237 goto io_error;
238 }
239 if (result < wdata->args.count)
240 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
241 wdata->args.count, result);
242
243 wdata->args.offset += result;
244 wdata->args.pgbase += result;
245 written += result;
246 count -= result;
91d5b470 247 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
1da177e4
LT
248 } while (count);
249 /* Update file length */
250 nfs_grow_file(page, offset, written);
251 /* Set the PG_uptodate flag? */
252 nfs_mark_uptodate(page, offset, written);
253
254 if (PageError(page))
255 ClearPageError(page);
256
257io_error:
951a143b 258 nfs_end_data_update(inode);
bb713d6d 259 end_page_writeback(page);
1da177e4
LT
260 nfs_writedata_free(wdata);
261 return written ? written : result;
262}
263
264static int nfs_writepage_async(struct nfs_open_context *ctx,
265 struct inode *inode, struct page *page,
266 unsigned int offset, unsigned int count)
267{
268 struct nfs_page *req;
1da177e4
LT
269
270 req = nfs_update_request(ctx, inode, page, offset, count);
abd3e641
TM
271 if (IS_ERR(req))
272 return PTR_ERR(req);
1da177e4
LT
273 /* Update file length */
274 nfs_grow_file(page, offset, count);
275 /* Set the PG_uptodate flag? */
276 nfs_mark_uptodate(page, offset, count);
277 nfs_unlock_request(req);
abd3e641 278 return 0;
1da177e4
LT
279}
280
281static int wb_priority(struct writeback_control *wbc)
282{
283 if (wbc->for_reclaim)
284 return FLUSH_HIGHPRI;
285 if (wbc->for_kupdate)
286 return FLUSH_LOWPRI;
287 return 0;
288}
289
290/*
291 * Write an mmapped page to the server.
292 */
293int nfs_writepage(struct page *page, struct writeback_control *wbc)
294{
295 struct nfs_open_context *ctx;
296 struct inode *inode = page->mapping->host;
297 unsigned long end_index;
298 unsigned offset = PAGE_CACHE_SIZE;
299 loff_t i_size = i_size_read(inode);
300 int inode_referenced = 0;
301 int priority = wb_priority(wbc);
302 int err;
303
91d5b470
CL
304 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
305 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
306
1da177e4
LT
307 /*
308 * Note: We need to ensure that we have a reference to the inode
309 * if we are to do asynchronous writes. If not, waiting
310 * in nfs_wait_on_request() may deadlock with clear_inode().
311 *
312 * If igrab() fails here, then it is in any case safe to
313 * call nfs_wb_page(), since there will be no pending writes.
314 */
315 if (igrab(inode) != 0)
316 inode_referenced = 1;
317 end_index = i_size >> PAGE_CACHE_SHIFT;
318
319 /* Ensure we've flushed out any previous writes */
320 nfs_wb_page_priority(inode, page, priority);
321
322 /* easy case */
323 if (page->index < end_index)
324 goto do_it;
325 /* things got complicated... */
326 offset = i_size & (PAGE_CACHE_SIZE-1);
327
328 /* OK, are we completely out? */
329 err = 0; /* potential race with truncate - ignore */
330 if (page->index >= end_index+1 || !offset)
331 goto out;
332do_it:
d530838b 333 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
1da177e4
LT
334 if (ctx == NULL) {
335 err = -EBADF;
336 goto out;
337 }
338 lock_kernel();
339 if (!IS_SYNC(inode) && inode_referenced) {
340 err = nfs_writepage_async(ctx, inode, page, 0, offset);
abd3e641
TM
341 if (!wbc->for_writepages)
342 nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
1da177e4
LT
343 } else {
344 err = nfs_writepage_sync(ctx, inode, page, 0,
345 offset, priority);
346 if (err >= 0) {
347 if (err != offset)
348 redirty_page_for_writepage(wbc, page);
349 err = 0;
350 }
351 }
352 unlock_kernel();
353 put_nfs_open_context(ctx);
354out:
355 unlock_page(page);
356 if (inode_referenced)
357 iput(inode);
358 return err;
359}
360
361/*
362 * Note: causes nfs_update_request() to block on the assumption
363 * that the writeback is generated due to memory pressure.
364 */
365int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
366{
367 struct backing_dev_info *bdi = mapping->backing_dev_info;
368 struct inode *inode = mapping->host;
369 int err;
370
91d5b470
CL
371 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
372
1da177e4
LT
373 err = generic_writepages(mapping, wbc);
374 if (err)
375 return err;
376 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
377 if (wbc->nonblocking)
378 return 0;
379 nfs_wait_on_write_congestion(mapping, 0);
380 }
381 err = nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
382 if (err < 0)
383 goto out;
91d5b470 384 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
1da177e4
LT
385 wbc->nr_to_write -= err;
386 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
387 err = nfs_wait_on_requests(inode, 0, 0);
388 if (err < 0)
389 goto out;
390 }
3da28eb1 391 err = nfs_commit_inode(inode, wb_priority(wbc));
1da177e4
LT
392 if (err > 0) {
393 wbc->nr_to_write -= err;
394 err = 0;
395 }
396out:
397 clear_bit(BDI_write_congested, &bdi->state);
398 wake_up_all(&nfs_write_congestion);
275a082f 399 writeback_congestion_end();
1da177e4
LT
400 return err;
401}
402
403/*
404 * Insert a write request into an inode
405 */
406static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
407{
408 struct nfs_inode *nfsi = NFS_I(inode);
409 int error;
410
411 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
412 BUG_ON(error == -EEXIST);
413 if (error)
414 return error;
415 if (!nfsi->npages) {
416 igrab(inode);
417 nfs_begin_data_update(inode);
418 if (nfs_have_delegation(inode, FMODE_WRITE))
419 nfsi->change_attr++;
420 }
deb7d638 421 SetPagePrivate(req->wb_page);
1da177e4
LT
422 nfsi->npages++;
423 atomic_inc(&req->wb_count);
424 return 0;
425}
426
427/*
428 * Insert a write request into an inode
429 */
430static void nfs_inode_remove_request(struct nfs_page *req)
431{
432 struct inode *inode = req->wb_context->dentry->d_inode;
433 struct nfs_inode *nfsi = NFS_I(inode);
434
435 BUG_ON (!NFS_WBACK_BUSY(req));
436
437 spin_lock(&nfsi->req_lock);
deb7d638 438 ClearPagePrivate(req->wb_page);
1da177e4
LT
439 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
440 nfsi->npages--;
441 if (!nfsi->npages) {
442 spin_unlock(&nfsi->req_lock);
951a143b 443 nfs_end_data_update(inode);
1da177e4
LT
444 iput(inode);
445 } else
446 spin_unlock(&nfsi->req_lock);
447 nfs_clear_request(req);
448 nfs_release_request(req);
449}
450
451/*
452 * Find a request
453 */
454static inline struct nfs_page *
455_nfs_find_request(struct inode *inode, unsigned long index)
456{
457 struct nfs_inode *nfsi = NFS_I(inode);
458 struct nfs_page *req;
459
460 req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index);
461 if (req)
462 atomic_inc(&req->wb_count);
463 return req;
464}
465
466static struct nfs_page *
467nfs_find_request(struct inode *inode, unsigned long index)
468{
469 struct nfs_page *req;
470 struct nfs_inode *nfsi = NFS_I(inode);
471
472 spin_lock(&nfsi->req_lock);
473 req = _nfs_find_request(inode, index);
474 spin_unlock(&nfsi->req_lock);
475 return req;
476}
477
478/*
479 * Add a request to the inode's dirty list.
480 */
481static void
482nfs_mark_request_dirty(struct nfs_page *req)
483{
484 struct inode *inode = req->wb_context->dentry->d_inode;
485 struct nfs_inode *nfsi = NFS_I(inode);
486
487 spin_lock(&nfsi->req_lock);
3da28eb1
TM
488 radix_tree_tag_set(&nfsi->nfs_page_tree,
489 req->wb_index, NFS_PAGE_TAG_DIRTY);
1da177e4
LT
490 nfs_list_add_request(req, &nfsi->dirty);
491 nfsi->ndirty++;
492 spin_unlock(&nfsi->req_lock);
b1e7a8fd 493 inc_zone_page_state(req->wb_page, NR_FILE_DIRTY);
1da177e4
LT
494 mark_inode_dirty(inode);
495}
496
497/*
498 * Check if a request is dirty
499 */
500static inline int
501nfs_dirty_request(struct nfs_page *req)
502{
503 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
504 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
505}
506
507#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
508/*
509 * Add a request to the inode's commit list.
510 */
511static void
512nfs_mark_request_commit(struct nfs_page *req)
513{
514 struct inode *inode = req->wb_context->dentry->d_inode;
515 struct nfs_inode *nfsi = NFS_I(inode);
516
517 spin_lock(&nfsi->req_lock);
518 nfs_list_add_request(req, &nfsi->commit);
519 nfsi->ncommit++;
520 spin_unlock(&nfsi->req_lock);
fd39fc85 521 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1da177e4
LT
522 mark_inode_dirty(inode);
523}
524#endif
525
526/*
527 * Wait for a request to complete.
528 *
529 * Interruptible by signals only if mounted with intr flag.
530 */
c42de9dd 531static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
1da177e4
LT
532{
533 struct nfs_inode *nfsi = NFS_I(inode);
534 struct nfs_page *req;
535 unsigned long idx_end, next;
536 unsigned int res = 0;
537 int error;
538
539 if (npages == 0)
540 idx_end = ~0;
541 else
542 idx_end = idx_start + npages - 1;
543
1da177e4 544 next = idx_start;
c6a556b8 545 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
1da177e4
LT
546 if (req->wb_index > idx_end)
547 break;
548
549 next = req->wb_index + 1;
c6a556b8 550 BUG_ON(!NFS_WBACK_BUSY(req));
1da177e4
LT
551
552 atomic_inc(&req->wb_count);
553 spin_unlock(&nfsi->req_lock);
554 error = nfs_wait_on_request(req);
555 nfs_release_request(req);
c42de9dd 556 spin_lock(&nfsi->req_lock);
1da177e4
LT
557 if (error < 0)
558 return error;
1da177e4
LT
559 res++;
560 }
1da177e4
LT
561 return res;
562}
563
c42de9dd
TM
564static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
565{
566 struct nfs_inode *nfsi = NFS_I(inode);
567 int ret;
568
569 spin_lock(&nfsi->req_lock);
570 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
571 spin_unlock(&nfsi->req_lock);
572 return ret;
573}
574
83715ad5 575static void nfs_cancel_dirty_list(struct list_head *head)
d2ccddf0
TM
576{
577 struct nfs_page *req;
578 while(!list_empty(head)) {
579 req = nfs_list_entry(head->next);
580 nfs_list_remove_request(req);
581 nfs_inode_remove_request(req);
582 nfs_clear_page_writeback(req);
583 }
584}
585
83715ad5
TM
586static void nfs_cancel_commit_list(struct list_head *head)
587{
588 struct nfs_page *req;
589
590 while(!list_empty(head)) {
591 req = nfs_list_entry(head->next);
592 nfs_list_remove_request(req);
593 nfs_inode_remove_request(req);
83715ad5 594 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
5c2d97cb 595 nfs_clear_page_writeback(req);
83715ad5
TM
596 }
597}
598
1da177e4
LT
599/*
600 * nfs_scan_dirty - Scan an inode for dirty requests
601 * @inode: NFS inode to scan
602 * @dst: destination list
603 * @idx_start: lower bound of page->index to scan.
604 * @npages: idx_start + npages sets the upper bound to scan.
605 *
606 * Moves requests from the inode's dirty page list.
607 * The requests are *not* checked to ensure that they form a contiguous set.
608 */
609static int
610nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
611{
612 struct nfs_inode *nfsi = NFS_I(inode);
3da28eb1
TM
613 int res = 0;
614
615 if (nfsi->ndirty != 0) {
616 res = nfs_scan_lock_dirty(nfsi, dst, idx_start, npages);
617 nfsi->ndirty -= res;
3da28eb1
TM
618 if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
619 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
620 }
1da177e4
LT
621 return res;
622}
623
624#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
625/*
626 * nfs_scan_commit - Scan an inode for commit requests
627 * @inode: NFS inode to scan
628 * @dst: destination list
629 * @idx_start: lower bound of page->index to scan.
630 * @npages: idx_start + npages sets the upper bound to scan.
631 *
632 * Moves requests from the inode's 'commit' request list.
633 * The requests are *not* checked to ensure that they form a contiguous set.
634 */
635static int
636nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
637{
638 struct nfs_inode *nfsi = NFS_I(inode);
3da28eb1
TM
639 int res = 0;
640
641 if (nfsi->ncommit != 0) {
d2ccddf0 642 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
3da28eb1
TM
643 nfsi->ncommit -= res;
644 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
645 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
646 }
1da177e4
LT
647 return res;
648}
c42de9dd
TM
649#else
650static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
651{
652 return 0;
653}
1da177e4
LT
654#endif
655
656static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
657{
658 struct backing_dev_info *bdi = mapping->backing_dev_info;
659 DEFINE_WAIT(wait);
660 int ret = 0;
661
662 might_sleep();
663
664 if (!bdi_write_congested(bdi))
665 return 0;
91d5b470
CL
666
667 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
668
1da177e4
LT
669 if (intr) {
670 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
671 sigset_t oldset;
672
673 rpc_clnt_sigmask(clnt, &oldset);
674 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
675 if (bdi_write_congested(bdi)) {
676 if (signalled())
677 ret = -ERESTARTSYS;
678 else
679 schedule();
680 }
681 rpc_clnt_sigunmask(clnt, &oldset);
682 } else {
683 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
684 if (bdi_write_congested(bdi))
685 schedule();
686 }
687 finish_wait(&nfs_write_congestion, &wait);
688 return ret;
689}
690
691
692/*
693 * Try to update any existing write request, or create one if there is none.
694 * In order to match, the request's credentials must match those of
695 * the calling process.
696 *
697 * Note: Should always be called with the Page Lock held!
698 */
699static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
700 struct inode *inode, struct page *page,
701 unsigned int offset, unsigned int bytes)
702{
703 struct nfs_server *server = NFS_SERVER(inode);
704 struct nfs_inode *nfsi = NFS_I(inode);
705 struct nfs_page *req, *new = NULL;
706 unsigned long rqend, end;
707
708 end = offset + bytes;
709
710 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
711 return ERR_PTR(-ERESTARTSYS);
712 for (;;) {
713 /* Loop over all inode entries and see if we find
714 * A request for the page we wish to update
715 */
716 spin_lock(&nfsi->req_lock);
717 req = _nfs_find_request(inode, page->index);
718 if (req) {
719 if (!nfs_lock_request_dontget(req)) {
720 int error;
721 spin_unlock(&nfsi->req_lock);
722 error = nfs_wait_on_request(req);
723 nfs_release_request(req);
1dd594b2
NB
724 if (error < 0) {
725 if (new)
726 nfs_release_request(new);
1da177e4 727 return ERR_PTR(error);
1dd594b2 728 }
1da177e4
LT
729 continue;
730 }
731 spin_unlock(&nfsi->req_lock);
732 if (new)
733 nfs_release_request(new);
734 break;
735 }
736
737 if (new) {
738 int error;
739 nfs_lock_request_dontget(new);
740 error = nfs_inode_add_request(inode, new);
741 if (error) {
742 spin_unlock(&nfsi->req_lock);
743 nfs_unlock_request(new);
744 return ERR_PTR(error);
745 }
746 spin_unlock(&nfsi->req_lock);
747 nfs_mark_request_dirty(new);
748 return new;
749 }
750 spin_unlock(&nfsi->req_lock);
751
752 new = nfs_create_request(ctx, inode, page, offset, bytes);
753 if (IS_ERR(new))
754 return new;
755 }
756
757 /* We have a request for our page.
758 * If the creds don't match, or the
759 * page addresses don't match,
760 * tell the caller to wait on the conflicting
761 * request.
762 */
763 rqend = req->wb_offset + req->wb_bytes;
764 if (req->wb_context != ctx
765 || req->wb_page != page
766 || !nfs_dirty_request(req)
767 || offset > rqend || end < req->wb_offset) {
768 nfs_unlock_request(req);
769 return ERR_PTR(-EBUSY);
770 }
771
772 /* Okay, the request matches. Update the region */
773 if (offset < req->wb_offset) {
774 req->wb_offset = offset;
775 req->wb_pgbase = offset;
776 req->wb_bytes = rqend - req->wb_offset;
777 }
778
779 if (end > rqend)
780 req->wb_bytes = end - req->wb_offset;
781
782 return req;
783}
784
785int nfs_flush_incompatible(struct file *file, struct page *page)
786{
787 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
788 struct inode *inode = page->mapping->host;
789 struct nfs_page *req;
790 int status = 0;
791 /*
792 * Look for a request corresponding to this page. If there
793 * is one, and it belongs to another file, we flush it out
794 * before we try to copy anything into the page. Do this
795 * due to the lack of an ACCESS-type call in NFSv2.
796 * Also do the same if we find a request from an existing
797 * dropped page.
798 */
799 req = nfs_find_request(inode, page->index);
800 if (req) {
801 if (req->wb_page != page || ctx != req->wb_context)
802 status = nfs_wb_page(inode, page);
803 nfs_release_request(req);
804 }
805 return (status < 0) ? status : 0;
806}
807
808/*
809 * Update and possibly write a cached page of an NFS file.
810 *
811 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
812 * things with a page scheduled for an RPC call (e.g. invalidate it).
813 */
814int nfs_updatepage(struct file *file, struct page *page,
815 unsigned int offset, unsigned int count)
816{
817 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
1da177e4
LT
818 struct inode *inode = page->mapping->host;
819 struct nfs_page *req;
820 int status = 0;
821
91d5b470
CL
822 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
823
1da177e4 824 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
0bbacc40
CL
825 file->f_dentry->d_parent->d_name.name,
826 file->f_dentry->d_name.name, count,
827 (long long)(page_offset(page) +offset));
1da177e4
LT
828
829 if (IS_SYNC(inode)) {
830 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
831 if (status > 0) {
832 if (offset == 0 && status == PAGE_CACHE_SIZE)
833 SetPageUptodate(page);
834 return 0;
835 }
836 return status;
837 }
838
839 /* If we're not using byte range locks, and we know the page
840 * is entirely in cache, it may be more efficient to avoid
841 * fragmenting write requests.
842 */
ab0a3dbe 843 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
1da177e4
LT
844 loff_t end_offs = i_size_read(inode) - 1;
845 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
846
847 count += offset;
848 offset = 0;
849 if (unlikely(end_offs < 0)) {
850 /* Do nothing */
851 } else if (page->index == end_index) {
852 unsigned int pglen;
853 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
854 if (count < pglen)
855 count = pglen;
856 } else if (page->index < end_index)
857 count = PAGE_CACHE_SIZE;
858 }
859
860 /*
861 * Try to find an NFS request corresponding to this page
862 * and update it.
863 * If the existing request cannot be updated, we must flush
864 * it out now.
865 */
866 do {
867 req = nfs_update_request(ctx, inode, page, offset, count);
868 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
869 if (status != -EBUSY)
870 break;
871 /* Request could not be updated. Flush it out and try again */
872 status = nfs_wb_page(inode, page);
873 } while (status >= 0);
874 if (status < 0)
875 goto done;
876
877 status = 0;
878
879 /* Update file length */
880 nfs_grow_file(page, offset, count);
881 /* Set the PG_uptodate flag? */
882 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
883 nfs_unlock_request(req);
884done:
885 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
886 status, (long long)i_size_read(inode));
887 if (status < 0)
888 ClearPageUptodate(page);
889 return status;
890}
891
892static void nfs_writepage_release(struct nfs_page *req)
893{
894 end_page_writeback(req->wb_page);
895
896#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
897 if (!PageError(req->wb_page)) {
898 if (NFS_NEED_RESCHED(req)) {
899 nfs_mark_request_dirty(req);
900 goto out;
901 } else if (NFS_NEED_COMMIT(req)) {
902 nfs_mark_request_commit(req);
903 goto out;
904 }
905 }
906 nfs_inode_remove_request(req);
907
908out:
909 nfs_clear_commit(req);
910 nfs_clear_reschedule(req);
911#else
912 nfs_inode_remove_request(req);
913#endif
c6a556b8 914 nfs_clear_page_writeback(req);
1da177e4
LT
915}
916
917static inline int flush_task_priority(int how)
918{
919 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
920 case FLUSH_HIGHPRI:
921 return RPC_PRIORITY_HIGH;
922 case FLUSH_LOWPRI:
923 return RPC_PRIORITY_LOW;
924 }
925 return RPC_PRIORITY_NORMAL;
926}
927
928/*
929 * Set up the argument/result storage required for the RPC call.
930 */
931static void nfs_write_rpcsetup(struct nfs_page *req,
932 struct nfs_write_data *data,
788e7a89 933 const struct rpc_call_ops *call_ops,
1da177e4
LT
934 unsigned int count, unsigned int offset,
935 int how)
936{
1da177e4 937 struct inode *inode;
788e7a89 938 int flags;
1da177e4
LT
939
940 /* Set up the RPC argument and reply structs
941 * NB: take care not to mess about with data->commit et al. */
942
943 data->req = req;
944 data->inode = inode = req->wb_context->dentry->d_inode;
945 data->cred = req->wb_context->cred;
946
947 data->args.fh = NFS_FH(inode);
948 data->args.offset = req_offset(req) + offset;
949 data->args.pgbase = req->wb_pgbase + offset;
950 data->args.pages = data->pagevec;
951 data->args.count = count;
952 data->args.context = req->wb_context;
953
954 data->res.fattr = &data->fattr;
955 data->res.count = count;
956 data->res.verf = &data->verf;
0e574af1 957 nfs_fattr_init(&data->fattr);
1da177e4 958
788e7a89
TM
959 /* Set up the initial task struct. */
960 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
961 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
1da177e4
LT
962 NFS_PROTO(inode)->write_setup(data, how);
963
964 data->task.tk_priority = flush_task_priority(how);
965 data->task.tk_cookie = (unsigned long)inode;
1da177e4
LT
966
967 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
0bbacc40 968 data->task.tk_pid,
1da177e4
LT
969 inode->i_sb->s_id,
970 (long long)NFS_FILEID(inode),
971 count,
972 (unsigned long long)data->args.offset);
973}
974
975static void nfs_execute_write(struct nfs_write_data *data)
976{
977 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
978 sigset_t oldset;
979
980 rpc_clnt_sigmask(clnt, &oldset);
981 lock_kernel();
982 rpc_execute(&data->task);
983 unlock_kernel();
984 rpc_clnt_sigunmask(clnt, &oldset);
985}
986
987/*
988 * Generate multiple small requests to write out a single
989 * contiguous dirty area on one page.
990 */
7d46a49f 991static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
1da177e4
LT
992{
993 struct nfs_page *req = nfs_list_entry(head->next);
994 struct page *page = req->wb_page;
995 struct nfs_write_data *data;
e9f7bee1
TM
996 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
997 unsigned int offset;
1da177e4
LT
998 int requests = 0;
999 LIST_HEAD(list);
1000
1001 nfs_list_remove_request(req);
1002
1003 nbytes = req->wb_bytes;
e9f7bee1
TM
1004 do {
1005 size_t len = min(nbytes, wsize);
1006
1007 data = nfs_writedata_alloc(len);
1da177e4
LT
1008 if (!data)
1009 goto out_bad;
1010 list_add(&data->pages, &list);
1011 requests++;
e9f7bee1
TM
1012 nbytes -= len;
1013 } while (nbytes != 0);
1da177e4
LT
1014 atomic_set(&req->wb_complete, requests);
1015
1016 ClearPageError(page);
bb713d6d 1017 set_page_writeback(page);
1da177e4
LT
1018 offset = 0;
1019 nbytes = req->wb_bytes;
1020 do {
1021 data = list_entry(list.next, struct nfs_write_data, pages);
1022 list_del_init(&data->pages);
1023
1024 data->pagevec[0] = page;
1da177e4
LT
1025
1026 if (nbytes > wsize) {
788e7a89
TM
1027 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1028 wsize, offset, how);
1da177e4
LT
1029 offset += wsize;
1030 nbytes -= wsize;
1031 } else {
788e7a89
TM
1032 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1033 nbytes, offset, how);
1da177e4
LT
1034 nbytes = 0;
1035 }
1036 nfs_execute_write(data);
1037 } while (nbytes != 0);
1038
1039 return 0;
1040
1041out_bad:
1042 while (!list_empty(&list)) {
1043 data = list_entry(list.next, struct nfs_write_data, pages);
1044 list_del(&data->pages);
1045 nfs_writedata_free(data);
1046 }
1047 nfs_mark_request_dirty(req);
c6a556b8 1048 nfs_clear_page_writeback(req);
1da177e4
LT
1049 return -ENOMEM;
1050}
1051
1052/*
1053 * Create an RPC task for the given write request and kick it.
1054 * The page must have been locked by the caller.
1055 *
1056 * It may happen that the page we're passed is not marked dirty.
1057 * This is the case if nfs_updatepage detects a conflicting request
1058 * that has been written but not committed.
1059 */
7d46a49f 1060static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
1da177e4
LT
1061{
1062 struct nfs_page *req;
1063 struct page **pages;
1064 struct nfs_write_data *data;
1065 unsigned int count;
1066
e9f7bee1 1067 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
1da177e4
LT
1068 if (!data)
1069 goto out_bad;
1070
1071 pages = data->pagevec;
1072 count = 0;
1073 while (!list_empty(head)) {
1074 req = nfs_list_entry(head->next);
1075 nfs_list_remove_request(req);
1076 nfs_list_add_request(req, &data->pages);
1077 ClearPageError(req->wb_page);
bb713d6d 1078 set_page_writeback(req->wb_page);
1da177e4
LT
1079 *pages++ = req->wb_page;
1080 count += req->wb_bytes;
1081 }
1082 req = nfs_list_entry(data->pages.next);
1083
1da177e4 1084 /* Set up the argument struct */
788e7a89 1085 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
1da177e4
LT
1086
1087 nfs_execute_write(data);
1088 return 0;
1089 out_bad:
1090 while (!list_empty(head)) {
1091 struct nfs_page *req = nfs_list_entry(head->next);
1092 nfs_list_remove_request(req);
1093 nfs_mark_request_dirty(req);
c6a556b8 1094 nfs_clear_page_writeback(req);
1da177e4
LT
1095 }
1096 return -ENOMEM;
1097}
1098
7d46a49f 1099static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
1da177e4
LT
1100{
1101 LIST_HEAD(one_request);
7d46a49f
TM
1102 int (*flush_one)(struct inode *, struct list_head *, int);
1103 struct nfs_page *req;
1104 int wpages = NFS_SERVER(inode)->wpages;
1105 int wsize = NFS_SERVER(inode)->wsize;
1106 int error;
1da177e4 1107
7d46a49f
TM
1108 flush_one = nfs_flush_one;
1109 if (wsize < PAGE_CACHE_SIZE)
1110 flush_one = nfs_flush_multi;
1111 /* For single writes, FLUSH_STABLE is more efficient */
1112 if (npages <= wpages && npages == NFS_I(inode)->npages
1113 && nfs_list_entry(head->next)->wb_bytes <= wsize)
1114 how |= FLUSH_STABLE;
1115
1116 do {
1117 nfs_coalesce_requests(head, &one_request, wpages);
1da177e4 1118 req = nfs_list_entry(one_request.next);
7d46a49f 1119 error = flush_one(inode, &one_request, how);
1da177e4 1120 if (error < 0)
7d46a49f
TM
1121 goto out_err;
1122 } while (!list_empty(head));
1123 return 0;
1124out_err:
1da177e4
LT
1125 while (!list_empty(head)) {
1126 req = nfs_list_entry(head->next);
1127 nfs_list_remove_request(req);
1128 nfs_mark_request_dirty(req);
c6a556b8 1129 nfs_clear_page_writeback(req);
1da177e4
LT
1130 }
1131 return error;
1132}
1133
1134/*
1135 * Handle a write reply that flushed part of a page.
1136 */
788e7a89 1137static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
1da177e4 1138{
788e7a89 1139 struct nfs_write_data *data = calldata;
1da177e4
LT
1140 struct nfs_page *req = data->req;
1141 struct page *page = req->wb_page;
1142
1143 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1144 req->wb_context->dentry->d_inode->i_sb->s_id,
1145 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1146 req->wb_bytes,
1147 (long long)req_offset(req));
1148
788e7a89
TM
1149 if (nfs_writeback_done(task, data) != 0)
1150 return;
1151
1152 if (task->tk_status < 0) {
1da177e4
LT
1153 ClearPageUptodate(page);
1154 SetPageError(page);
788e7a89
TM
1155 req->wb_context->error = task->tk_status;
1156 dprintk(", error = %d\n", task->tk_status);
1da177e4
LT
1157 } else {
1158#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1159 if (data->verf.committed < NFS_FILE_SYNC) {
1160 if (!NFS_NEED_COMMIT(req)) {
1161 nfs_defer_commit(req);
1162 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1163 dprintk(" defer commit\n");
1164 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1165 nfs_defer_reschedule(req);
1166 dprintk(" server reboot detected\n");
1167 }
1168 } else
1169#endif
1170 dprintk(" OK\n");
1171 }
1172
1173 if (atomic_dec_and_test(&req->wb_complete))
1174 nfs_writepage_release(req);
1175}
1176
788e7a89
TM
1177static const struct rpc_call_ops nfs_write_partial_ops = {
1178 .rpc_call_done = nfs_writeback_done_partial,
1179 .rpc_release = nfs_writedata_release,
1180};
1181
1da177e4
LT
1182/*
1183 * Handle a write reply that flushes a whole page.
1184 *
1185 * FIXME: There is an inherent race with invalidate_inode_pages and
1186 * writebacks since the page->count is kept > 1 for as long
1187 * as the page has a write request pending.
1188 */
788e7a89 1189static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1da177e4 1190{
788e7a89 1191 struct nfs_write_data *data = calldata;
1da177e4
LT
1192 struct nfs_page *req;
1193 struct page *page;
1194
788e7a89
TM
1195 if (nfs_writeback_done(task, data) != 0)
1196 return;
1197
1da177e4
LT
1198 /* Update attributes as result of writeback. */
1199 while (!list_empty(&data->pages)) {
1200 req = nfs_list_entry(data->pages.next);
1201 nfs_list_remove_request(req);
1202 page = req->wb_page;
1203
1204 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1205 req->wb_context->dentry->d_inode->i_sb->s_id,
1206 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1207 req->wb_bytes,
1208 (long long)req_offset(req));
1209
788e7a89 1210 if (task->tk_status < 0) {
1da177e4
LT
1211 ClearPageUptodate(page);
1212 SetPageError(page);
788e7a89 1213 req->wb_context->error = task->tk_status;
1da177e4
LT
1214 end_page_writeback(page);
1215 nfs_inode_remove_request(req);
788e7a89 1216 dprintk(", error = %d\n", task->tk_status);
1da177e4
LT
1217 goto next;
1218 }
1219 end_page_writeback(page);
1220
1221#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1222 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1223 nfs_inode_remove_request(req);
1224 dprintk(" OK\n");
1225 goto next;
1226 }
1227 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1228 nfs_mark_request_commit(req);
1229 dprintk(" marked for commit\n");
1230#else
1231 nfs_inode_remove_request(req);
1232#endif
1233 next:
c6a556b8 1234 nfs_clear_page_writeback(req);
1da177e4
LT
1235 }
1236}
1237
788e7a89
TM
1238static const struct rpc_call_ops nfs_write_full_ops = {
1239 .rpc_call_done = nfs_writeback_done_full,
1240 .rpc_release = nfs_writedata_release,
1241};
1242
1243
1da177e4
LT
1244/*
1245 * This function is called when the WRITE call is complete.
1246 */
462d5b32 1247int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1da177e4 1248{
1da177e4
LT
1249 struct nfs_writeargs *argp = &data->args;
1250 struct nfs_writeres *resp = &data->res;
788e7a89 1251 int status;
1da177e4
LT
1252
1253 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1254 task->tk_pid, task->tk_status);
1255
f551e44f
CL
1256 /*
1257 * ->write_done will attempt to use post-op attributes to detect
1258 * conflicting writes by other clients. A strict interpretation
1259 * of close-to-open would allow us to continue caching even if
1260 * another writer had changed the file, but some applications
1261 * depend on tighter cache coherency when writing.
1262 */
788e7a89
TM
1263 status = NFS_PROTO(data->inode)->write_done(task, data);
1264 if (status != 0)
1265 return status;
91d5b470
CL
1266 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1267
1da177e4
LT
1268#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1269 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1270 /* We tried a write call, but the server did not
1271 * commit data to stable storage even though we
1272 * requested it.
1273 * Note: There is a known bug in Tru64 < 5.0 in which
1274 * the server reports NFS_DATA_SYNC, but performs
1275 * NFS_FILE_SYNC. We therefore implement this checking
1276 * as a dprintk() in order to avoid filling syslog.
1277 */
1278 static unsigned long complain;
1279
1280 if (time_before(complain, jiffies)) {
1281 dprintk("NFS: faulty NFS server %s:"
1282 " (committed = %d) != (stable = %d)\n",
54ceac45 1283 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
1da177e4
LT
1284 resp->verf->committed, argp->stable);
1285 complain = jiffies + 300 * HZ;
1286 }
1287 }
1288#endif
1289 /* Is this a short write? */
1290 if (task->tk_status >= 0 && resp->count < argp->count) {
1291 static unsigned long complain;
1292
91d5b470
CL
1293 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1294
1da177e4
LT
1295 /* Has the server at least made some progress? */
1296 if (resp->count != 0) {
1297 /* Was this an NFSv2 write or an NFSv3 stable write? */
1298 if (resp->verf->committed != NFS_UNSTABLE) {
1299 /* Resend from where the server left off */
1300 argp->offset += resp->count;
1301 argp->pgbase += resp->count;
1302 argp->count -= resp->count;
1303 } else {
1304 /* Resend as a stable write in order to avoid
1305 * headaches in the case of a server crash.
1306 */
1307 argp->stable = NFS_FILE_SYNC;
1308 }
1309 rpc_restart_call(task);
788e7a89 1310 return -EAGAIN;
1da177e4
LT
1311 }
1312 if (time_before(complain, jiffies)) {
1313 printk(KERN_WARNING
1314 "NFS: Server wrote zero bytes, expected %u.\n",
1315 argp->count);
1316 complain = jiffies + 300 * HZ;
1317 }
1318 /* Can't do anything about it except throw an error. */
1319 task->tk_status = -EIO;
1320 }
788e7a89 1321 return 0;
1da177e4
LT
1322}
1323
1324
1325#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
963d8fe5 1326void nfs_commit_release(void *wdata)
1da177e4 1327{
1da177e4
LT
1328 nfs_commit_free(wdata);
1329}
1330
1331/*
1332 * Set up the argument/result storage required for the RPC call.
1333 */
1334static void nfs_commit_rpcsetup(struct list_head *head,
788e7a89
TM
1335 struct nfs_write_data *data,
1336 int how)
1da177e4 1337{
3da28eb1 1338 struct nfs_page *first;
1da177e4 1339 struct inode *inode;
788e7a89 1340 int flags;
1da177e4
LT
1341
1342 /* Set up the RPC argument and reply structs
1343 * NB: take care not to mess about with data->commit et al. */
1344
1345 list_splice_init(head, &data->pages);
1346 first = nfs_list_entry(data->pages.next);
1da177e4
LT
1347 inode = first->wb_context->dentry->d_inode;
1348
1da177e4
LT
1349 data->inode = inode;
1350 data->cred = first->wb_context->cred;
1351
1352 data->args.fh = NFS_FH(data->inode);
3da28eb1
TM
1353 /* Note: we always request a commit of the entire inode */
1354 data->args.offset = 0;
1355 data->args.count = 0;
1356 data->res.count = 0;
1da177e4
LT
1357 data->res.fattr = &data->fattr;
1358 data->res.verf = &data->verf;
0e574af1 1359 nfs_fattr_init(&data->fattr);
788e7a89
TM
1360
1361 /* Set up the initial task struct. */
1362 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1363 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
1da177e4
LT
1364 NFS_PROTO(inode)->commit_setup(data, how);
1365
1366 data->task.tk_priority = flush_task_priority(how);
1367 data->task.tk_cookie = (unsigned long)inode;
1da177e4 1368
0bbacc40 1369 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
1da177e4
LT
1370}
1371
1372/*
1373 * Commit dirty pages
1374 */
1375static int
40859d7e 1376nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1da177e4
LT
1377{
1378 struct nfs_write_data *data;
1379 struct nfs_page *req;
1380
e9f7bee1 1381 data = nfs_commit_alloc();
1da177e4
LT
1382
1383 if (!data)
1384 goto out_bad;
1385
1386 /* Set up the argument struct */
1387 nfs_commit_rpcsetup(head, data, how);
1388
1389 nfs_execute_write(data);
1390 return 0;
1391 out_bad:
1392 while (!list_empty(head)) {
1393 req = nfs_list_entry(head->next);
1394 nfs_list_remove_request(req);
1395 nfs_mark_request_commit(req);
83715ad5 1396 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
5c2d97cb 1397 nfs_clear_page_writeback(req);
1da177e4
LT
1398 }
1399 return -ENOMEM;
1400}
1401
1402/*
1403 * COMMIT call returned
1404 */
788e7a89 1405static void nfs_commit_done(struct rpc_task *task, void *calldata)
1da177e4 1406{
963d8fe5 1407 struct nfs_write_data *data = calldata;
1da177e4 1408 struct nfs_page *req;
1da177e4
LT
1409
1410 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1411 task->tk_pid, task->tk_status);
1412
788e7a89
TM
1413 /* Call the NFS version-specific code */
1414 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1415 return;
1416
1da177e4
LT
1417 while (!list_empty(&data->pages)) {
1418 req = nfs_list_entry(data->pages.next);
1419 nfs_list_remove_request(req);
fd39fc85 1420 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1da177e4
LT
1421
1422 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1423 req->wb_context->dentry->d_inode->i_sb->s_id,
1424 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1425 req->wb_bytes,
1426 (long long)req_offset(req));
1427 if (task->tk_status < 0) {
1428 req->wb_context->error = task->tk_status;
1429 nfs_inode_remove_request(req);
1430 dprintk(", error = %d\n", task->tk_status);
1431 goto next;
1432 }
1433
1434 /* Okay, COMMIT succeeded, apparently. Check the verifier
1435 * returned by the server against all stored verfs. */
1436 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1437 /* We have a match */
1438 nfs_inode_remove_request(req);
1439 dprintk(" OK\n");
1440 goto next;
1441 }
1442 /* We have a mismatch. Write the page again */
1443 dprintk(" mismatch\n");
1444 nfs_mark_request_dirty(req);
1445 next:
c6a556b8 1446 nfs_clear_page_writeback(req);
1da177e4 1447 }
1da177e4 1448}
788e7a89
TM
1449
1450static const struct rpc_call_ops nfs_commit_ops = {
1451 .rpc_call_done = nfs_commit_done,
1452 .rpc_release = nfs_commit_release,
1453};
c42de9dd
TM
1454#else
1455static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1456{
1457 return 0;
1458}
1da177e4
LT
1459#endif
1460
1461static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
1462 unsigned int npages, int how)
1463{
1464 struct nfs_inode *nfsi = NFS_I(inode);
1465 LIST_HEAD(head);
7d46a49f 1466 int res;
1da177e4
LT
1467
1468 spin_lock(&nfsi->req_lock);
1469 res = nfs_scan_dirty(inode, &head, idx_start, npages);
1470 spin_unlock(&nfsi->req_lock);
ab0a3dbe 1471 if (res) {
7d46a49f
TM
1472 int error = nfs_flush_list(inode, &head, res, how);
1473 if (error < 0)
1474 return error;
ab0a3dbe 1475 }
1da177e4
LT
1476 return res;
1477}
1478
1479#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
3da28eb1 1480int nfs_commit_inode(struct inode *inode, int how)
1da177e4
LT
1481{
1482 struct nfs_inode *nfsi = NFS_I(inode);
1483 LIST_HEAD(head);
7d46a49f 1484 int res;
1da177e4
LT
1485
1486 spin_lock(&nfsi->req_lock);
3da28eb1
TM
1487 res = nfs_scan_commit(inode, &head, 0, 0);
1488 spin_unlock(&nfsi->req_lock);
1da177e4 1489 if (res) {
7d46a49f 1490 int error = nfs_commit_list(inode, &head, how);
3da28eb1
TM
1491 if (error < 0)
1492 return error;
1493 }
1da177e4
LT
1494 return res;
1495}
1496#endif
1497
c42de9dd
TM
1498int nfs_sync_inode_wait(struct inode *inode, unsigned long idx_start,
1499 unsigned int npages, int how)
1da177e4 1500{
c42de9dd
TM
1501 struct nfs_inode *nfsi = NFS_I(inode);
1502 LIST_HEAD(head);
70b9ecbd 1503 int nocommit = how & FLUSH_NOCOMMIT;
c42de9dd 1504 int pages, ret;
1da177e4 1505
c42de9dd
TM
1506 how &= ~FLUSH_NOCOMMIT;
1507 spin_lock(&nfsi->req_lock);
1da177e4 1508 do {
c42de9dd
TM
1509 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1510 if (ret != 0)
70b9ecbd 1511 continue;
c42de9dd
TM
1512 pages = nfs_scan_dirty(inode, &head, idx_start, npages);
1513 if (pages != 0) {
1514 spin_unlock(&nfsi->req_lock);
d2ccddf0 1515 if (how & FLUSH_INVALIDATE)
83715ad5 1516 nfs_cancel_dirty_list(&head);
d2ccddf0
TM
1517 else
1518 ret = nfs_flush_list(inode, &head, pages, how);
c42de9dd
TM
1519 spin_lock(&nfsi->req_lock);
1520 continue;
1521 }
1522 if (nocommit)
1523 break;
d2ccddf0 1524 pages = nfs_scan_commit(inode, &head, idx_start, npages);
c42de9dd
TM
1525 if (pages == 0)
1526 break;
d2ccddf0
TM
1527 if (how & FLUSH_INVALIDATE) {
1528 spin_unlock(&nfsi->req_lock);
83715ad5 1529 nfs_cancel_commit_list(&head);
d2ccddf0
TM
1530 spin_lock(&nfsi->req_lock);
1531 continue;
1532 }
1533 pages += nfs_scan_commit(inode, &head, 0, 0);
c42de9dd
TM
1534 spin_unlock(&nfsi->req_lock);
1535 ret = nfs_commit_list(inode, &head, how);
1536 spin_lock(&nfsi->req_lock);
1537 } while (ret >= 0);
1538 spin_unlock(&nfsi->req_lock);
1539 return ret;
1da177e4
LT
1540}
1541
f7b422b1 1542int __init nfs_init_writepagecache(void)
1da177e4
LT
1543{
1544 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1545 sizeof(struct nfs_write_data),
1546 0, SLAB_HWCACHE_ALIGN,
1547 NULL, NULL);
1548 if (nfs_wdata_cachep == NULL)
1549 return -ENOMEM;
1550
93d2341c
MD
1551 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1552 nfs_wdata_cachep);
1da177e4
LT
1553 if (nfs_wdata_mempool == NULL)
1554 return -ENOMEM;
1555
93d2341c
MD
1556 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1557 nfs_wdata_cachep);
1da177e4
LT
1558 if (nfs_commit_mempool == NULL)
1559 return -ENOMEM;
1560
1561 return 0;
1562}
1563
266bee88 1564void nfs_destroy_writepagecache(void)
1da177e4
LT
1565{
1566 mempool_destroy(nfs_commit_mempool);
1567 mempool_destroy(nfs_wdata_mempool);
1568 if (kmem_cache_destroy(nfs_wdata_cachep))
1569 printk(KERN_INFO "nfs_write_data: not all structures were freed\n");
1570}
1571