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