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