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