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