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