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