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