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