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