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