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