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