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