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