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