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