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