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