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