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