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