blob: 3f5225404c973d41154c214f0d241f478da7da9d [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"
Chuck Lever91d5b472006-03-20 13:44:14 -050066#include "iostat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static 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);
Trond Myklebust788e7a82006-03-20 13:44:27 -050084static 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88static kmem_cache_t *nfs_wdata_cachep;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050089static mempool_t *nfs_wdata_mempool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static mempool_t *nfs_commit_mempool;
91
92static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
93
Trond Myklebuste17b1fc2006-03-20 13:44:35 -050094struct nfs_write_data *nfs_commit_alloc(unsigned int pagecount)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS);
Chuck Lever40859d72005-11-30 18:09:02 -050097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (p) {
99 memset(p, 0, sizeof(*p));
100 INIT_LIST_HEAD(&p->pages);
Chuck Lever40859d72005-11-30 18:09:02 -0500101 if (pagecount < NFS_PAGEVEC_SIZE)
102 p->pagevec = &p->page_array[0];
103 else {
104 size_t size = ++pagecount * sizeof(struct page *);
Eric Sesterhennbd647542006-03-20 13:44:10 -0500105 p->pagevec = kzalloc(size, GFP_NOFS);
106 if (!p->pagevec) {
Chuck Lever40859d72005-11-30 18:09:02 -0500107 mempool_free(p, nfs_commit_mempool);
108 p = NULL;
109 }
110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
112 return p;
113}
114
Trond Myklebuste17b1fc2006-03-20 13:44:35 -0500115void nfs_commit_free(struct nfs_write_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Chuck Lever40859d72005-11-30 18:09:02 -0500117 if (p && (p->pagevec != &p->page_array[0]))
118 kfree(p->pagevec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 mempool_free(p, nfs_commit_mempool);
120}
121
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500122struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
123{
124 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
125
126 if (p) {
127 memset(p, 0, sizeof(*p));
128 INIT_LIST_HEAD(&p->pages);
129 if (pagecount < NFS_PAGEVEC_SIZE)
130 p->pagevec = &p->page_array[0];
131 else {
132 size_t size = ++pagecount * sizeof(struct page *);
133 p->pagevec = kmalloc(size, GFP_NOFS);
134 if (p->pagevec) {
135 memset(p->pagevec, 0, size);
136 } else {
137 mempool_free(p, nfs_wdata_mempool);
138 p = NULL;
139 }
140 }
141 }
142 return p;
143}
144
145void nfs_writedata_free(struct nfs_write_data *p)
146{
147 if (p && (p->pagevec != &p->page_array[0]))
148 kfree(p->pagevec);
149 mempool_free(p, nfs_wdata_mempool);
150}
151
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100152void nfs_writedata_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 nfs_writedata_free(wdata);
155}
156
157/* Adjust the file length if we're writing beyond the end */
158static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
159{
160 struct inode *inode = page->mapping->host;
161 loff_t end, i_size = i_size_read(inode);
162 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
163
164 if (i_size > 0 && page->index < end_index)
165 return;
166 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
167 if (i_size >= end)
168 return;
Chuck Lever91d5b472006-03-20 13:44:14 -0500169 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 i_size_write(inode, end);
171}
172
173/* We can set the PG_uptodate flag if we see that a write request
174 * covers the full page.
175 */
176static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
177{
178 loff_t end_offs;
179
180 if (PageUptodate(page))
181 return;
182 if (base != 0)
183 return;
184 if (count == PAGE_CACHE_SIZE) {
185 SetPageUptodate(page);
186 return;
187 }
188
189 end_offs = i_size_read(page->mapping->host) - 1;
190 if (end_offs < 0)
191 return;
192 /* Is this the last page? */
193 if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT))
194 return;
195 /* This is the last page: set PG_uptodate if we cover the entire
196 * extent of the data, then zero the rest of the page.
197 */
198 if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) {
199 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
200 SetPageUptodate(page);
201 }
202}
203
204/*
205 * Write a page synchronously.
206 * Offset is the data offset within the page.
207 */
208static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
209 struct page *page, unsigned int offset, unsigned int count,
210 int how)
211{
212 unsigned int wsize = NFS_SERVER(inode)->wsize;
213 int result, written = 0;
214 struct nfs_write_data *wdata;
215
Chuck Lever40859d72005-11-30 18:09:02 -0500216 wdata = nfs_writedata_alloc(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (!wdata)
218 return -ENOMEM;
219
220 wdata->flags = how;
221 wdata->cred = ctx->cred;
222 wdata->inode = inode;
223 wdata->args.fh = NFS_FH(inode);
224 wdata->args.context = ctx;
225 wdata->args.pages = &page;
226 wdata->args.stable = NFS_FILE_SYNC;
227 wdata->args.pgbase = offset;
228 wdata->args.count = wsize;
229 wdata->res.fattr = &wdata->fattr;
230 wdata->res.verf = &wdata->verf;
231
232 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
233 inode->i_sb->s_id,
234 (long long)NFS_FILEID(inode),
235 count, (long long)(page_offset(page) + offset));
236
Trond Myklebustbb713d62005-12-03 15:20:14 -0500237 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 nfs_begin_data_update(inode);
239 do {
240 if (count < wsize)
241 wdata->args.count = count;
242 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
243
244 result = NFS_PROTO(inode)->write(wdata);
245
246 if (result < 0) {
247 /* Must mark the page invalid after I/O error */
248 ClearPageUptodate(page);
249 goto io_error;
250 }
251 if (result < wdata->args.count)
252 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
253 wdata->args.count, result);
254
255 wdata->args.offset += result;
256 wdata->args.pgbase += result;
257 written += result;
258 count -= result;
Chuck Lever91d5b472006-03-20 13:44:14 -0500259 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 } while (count);
261 /* Update file length */
262 nfs_grow_file(page, offset, written);
263 /* Set the PG_uptodate flag? */
264 nfs_mark_uptodate(page, offset, written);
265
266 if (PageError(page))
267 ClearPageError(page);
268
269io_error:
Trond Myklebust951a1432005-06-22 17:16:30 +0000270 nfs_end_data_update(inode);
Trond Myklebustbb713d62005-12-03 15:20:14 -0500271 end_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 nfs_writedata_free(wdata);
273 return written ? written : result;
274}
275
276static int nfs_writepage_async(struct nfs_open_context *ctx,
277 struct inode *inode, struct page *page,
278 unsigned int offset, unsigned int count)
279{
280 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 req = nfs_update_request(ctx, inode, page, offset, count);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100283 if (IS_ERR(req))
284 return PTR_ERR(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 /* Update file length */
286 nfs_grow_file(page, offset, count);
287 /* Set the PG_uptodate flag? */
288 nfs_mark_uptodate(page, offset, count);
289 nfs_unlock_request(req);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100290 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293static int wb_priority(struct writeback_control *wbc)
294{
295 if (wbc->for_reclaim)
296 return FLUSH_HIGHPRI;
297 if (wbc->for_kupdate)
298 return FLUSH_LOWPRI;
299 return 0;
300}
301
302/*
303 * Write an mmapped page to the server.
304 */
305int nfs_writepage(struct page *page, struct writeback_control *wbc)
306{
307 struct nfs_open_context *ctx;
308 struct inode *inode = page->mapping->host;
309 unsigned long end_index;
310 unsigned offset = PAGE_CACHE_SIZE;
311 loff_t i_size = i_size_read(inode);
312 int inode_referenced = 0;
313 int priority = wb_priority(wbc);
314 int err;
315
Chuck Lever91d5b472006-03-20 13:44:14 -0500316 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
317 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /*
320 * Note: We need to ensure that we have a reference to the inode
321 * if we are to do asynchronous writes. If not, waiting
322 * in nfs_wait_on_request() may deadlock with clear_inode().
323 *
324 * If igrab() fails here, then it is in any case safe to
325 * call nfs_wb_page(), since there will be no pending writes.
326 */
327 if (igrab(inode) != 0)
328 inode_referenced = 1;
329 end_index = i_size >> PAGE_CACHE_SHIFT;
330
331 /* Ensure we've flushed out any previous writes */
332 nfs_wb_page_priority(inode, page, priority);
333
334 /* easy case */
335 if (page->index < end_index)
336 goto do_it;
337 /* things got complicated... */
338 offset = i_size & (PAGE_CACHE_SIZE-1);
339
340 /* OK, are we completely out? */
341 err = 0; /* potential race with truncate - ignore */
342 if (page->index >= end_index+1 || !offset)
343 goto out;
344do_it:
Trond Myklebustd5308382005-11-04 15:33:38 -0500345 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (ctx == NULL) {
347 err = -EBADF;
348 goto out;
349 }
350 lock_kernel();
351 if (!IS_SYNC(inode) && inode_referenced) {
352 err = nfs_writepage_async(ctx, inode, page, 0, offset);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100353 if (!wbc->for_writepages)
354 nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 } else {
356 err = nfs_writepage_sync(ctx, inode, page, 0,
357 offset, priority);
358 if (err >= 0) {
359 if (err != offset)
360 redirty_page_for_writepage(wbc, page);
361 err = 0;
362 }
363 }
364 unlock_kernel();
365 put_nfs_open_context(ctx);
366out:
367 unlock_page(page);
368 if (inode_referenced)
369 iput(inode);
370 return err;
371}
372
373/*
374 * Note: causes nfs_update_request() to block on the assumption
375 * that the writeback is generated due to memory pressure.
376 */
377int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
378{
379 struct backing_dev_info *bdi = mapping->backing_dev_info;
380 struct inode *inode = mapping->host;
381 int err;
382
Chuck Lever91d5b472006-03-20 13:44:14 -0500383 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 err = generic_writepages(mapping, wbc);
386 if (err)
387 return err;
388 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
389 if (wbc->nonblocking)
390 return 0;
391 nfs_wait_on_write_congestion(mapping, 0);
392 }
393 err = nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
394 if (err < 0)
395 goto out;
Chuck Lever91d5b472006-03-20 13:44:14 -0500396 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 wbc->nr_to_write -= err;
398 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
399 err = nfs_wait_on_requests(inode, 0, 0);
400 if (err < 0)
401 goto out;
402 }
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000403 err = nfs_commit_inode(inode, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (err > 0) {
405 wbc->nr_to_write -= err;
406 err = 0;
407 }
408out:
409 clear_bit(BDI_write_congested, &bdi->state);
410 wake_up_all(&nfs_write_congestion);
411 return err;
412}
413
414/*
415 * Insert a write request into an inode
416 */
417static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
418{
419 struct nfs_inode *nfsi = NFS_I(inode);
420 int error;
421
422 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
423 BUG_ON(error == -EEXIST);
424 if (error)
425 return error;
426 if (!nfsi->npages) {
427 igrab(inode);
428 nfs_begin_data_update(inode);
429 if (nfs_have_delegation(inode, FMODE_WRITE))
430 nfsi->change_attr++;
431 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500432 SetPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 nfsi->npages++;
434 atomic_inc(&req->wb_count);
435 return 0;
436}
437
438/*
439 * Insert a write request into an inode
440 */
441static void nfs_inode_remove_request(struct nfs_page *req)
442{
443 struct inode *inode = req->wb_context->dentry->d_inode;
444 struct nfs_inode *nfsi = NFS_I(inode);
445
446 BUG_ON (!NFS_WBACK_BUSY(req));
447
448 spin_lock(&nfsi->req_lock);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500449 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
451 nfsi->npages--;
452 if (!nfsi->npages) {
453 spin_unlock(&nfsi->req_lock);
Trond Myklebust951a1432005-06-22 17:16:30 +0000454 nfs_end_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 iput(inode);
456 } else
457 spin_unlock(&nfsi->req_lock);
458 nfs_clear_request(req);
459 nfs_release_request(req);
460}
461
462/*
463 * Find a request
464 */
465static inline struct nfs_page *
466_nfs_find_request(struct inode *inode, unsigned long index)
467{
468 struct nfs_inode *nfsi = NFS_I(inode);
469 struct nfs_page *req;
470
471 req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index);
472 if (req)
473 atomic_inc(&req->wb_count);
474 return req;
475}
476
477static struct nfs_page *
478nfs_find_request(struct inode *inode, unsigned long index)
479{
480 struct nfs_page *req;
481 struct nfs_inode *nfsi = NFS_I(inode);
482
483 spin_lock(&nfsi->req_lock);
484 req = _nfs_find_request(inode, index);
485 spin_unlock(&nfsi->req_lock);
486 return req;
487}
488
489/*
490 * Add a request to the inode's dirty list.
491 */
492static void
493nfs_mark_request_dirty(struct nfs_page *req)
494{
495 struct inode *inode = req->wb_context->dentry->d_inode;
496 struct nfs_inode *nfsi = NFS_I(inode);
497
498 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000499 radix_tree_tag_set(&nfsi->nfs_page_tree,
500 req->wb_index, NFS_PAGE_TAG_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 nfs_list_add_request(req, &nfsi->dirty);
502 nfsi->ndirty++;
503 spin_unlock(&nfsi->req_lock);
504 inc_page_state(nr_dirty);
505 mark_inode_dirty(inode);
506}
507
508/*
509 * Check if a request is dirty
510 */
511static inline int
512nfs_dirty_request(struct nfs_page *req)
513{
514 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
515 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
516}
517
518#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
519/*
520 * Add a request to the inode's commit list.
521 */
522static void
523nfs_mark_request_commit(struct nfs_page *req)
524{
525 struct inode *inode = req->wb_context->dentry->d_inode;
526 struct nfs_inode *nfsi = NFS_I(inode);
527
528 spin_lock(&nfsi->req_lock);
529 nfs_list_add_request(req, &nfsi->commit);
530 nfsi->ncommit++;
531 spin_unlock(&nfsi->req_lock);
532 inc_page_state(nr_unstable);
533 mark_inode_dirty(inode);
534}
535#endif
536
537/*
538 * Wait for a request to complete.
539 *
540 * Interruptible by signals only if mounted with intr flag.
541 */
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500542static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 struct nfs_inode *nfsi = NFS_I(inode);
545 struct nfs_page *req;
546 unsigned long idx_end, next;
547 unsigned int res = 0;
548 int error;
549
550 if (npages == 0)
551 idx_end = ~0;
552 else
553 idx_end = idx_start + npages - 1;
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 next = idx_start;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000556 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 -0700557 if (req->wb_index > idx_end)
558 break;
559
560 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000561 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 atomic_inc(&req->wb_count);
564 spin_unlock(&nfsi->req_lock);
565 error = nfs_wait_on_request(req);
566 nfs_release_request(req);
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500567 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (error < 0)
569 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 res++;
571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return res;
573}
574
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500575static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
576{
577 struct nfs_inode *nfsi = NFS_I(inode);
578 int ret;
579
580 spin_lock(&nfsi->req_lock);
581 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
582 spin_unlock(&nfsi->req_lock);
583 return ret;
584}
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/*
587 * nfs_scan_dirty - Scan an inode for dirty requests
588 * @inode: NFS inode to scan
589 * @dst: destination list
590 * @idx_start: lower bound of page->index to scan.
591 * @npages: idx_start + npages sets the upper bound to scan.
592 *
593 * Moves requests from the inode's dirty page list.
594 * The requests are *not* checked to ensure that they form a contiguous set.
595 */
596static int
597nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
598{
599 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000600 int res = 0;
601
602 if (nfsi->ndirty != 0) {
603 res = nfs_scan_lock_dirty(nfsi, dst, idx_start, npages);
604 nfsi->ndirty -= res;
605 sub_page_state(nr_dirty,res);
606 if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
607 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return res;
610}
611
612#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
613/*
614 * nfs_scan_commit - Scan an inode for commit requests
615 * @inode: NFS inode to scan
616 * @dst: destination list
617 * @idx_start: lower bound of page->index to scan.
618 * @npages: idx_start + npages sets the upper bound to scan.
619 *
620 * Moves requests from the inode's 'commit' request list.
621 * The requests are *not* checked to ensure that they form a contiguous set.
622 */
623static int
624nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
625{
626 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000627 int res = 0;
628
629 if (nfsi->ncommit != 0) {
630 res = nfs_scan_list(&nfsi->commit, dst, idx_start, npages);
631 nfsi->ncommit -= res;
632 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
633 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return res;
636}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500637#else
638static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
639{
640 return 0;
641}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642#endif
643
644static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
645{
646 struct backing_dev_info *bdi = mapping->backing_dev_info;
647 DEFINE_WAIT(wait);
648 int ret = 0;
649
650 might_sleep();
651
652 if (!bdi_write_congested(bdi))
653 return 0;
Chuck Lever91d5b472006-03-20 13:44:14 -0500654
655 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (intr) {
658 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
659 sigset_t oldset;
660
661 rpc_clnt_sigmask(clnt, &oldset);
662 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
663 if (bdi_write_congested(bdi)) {
664 if (signalled())
665 ret = -ERESTARTSYS;
666 else
667 schedule();
668 }
669 rpc_clnt_sigunmask(clnt, &oldset);
670 } else {
671 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
672 if (bdi_write_congested(bdi))
673 schedule();
674 }
675 finish_wait(&nfs_write_congestion, &wait);
676 return ret;
677}
678
679
680/*
681 * Try to update any existing write request, or create one if there is none.
682 * In order to match, the request's credentials must match those of
683 * the calling process.
684 *
685 * Note: Should always be called with the Page Lock held!
686 */
687static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
688 struct inode *inode, struct page *page,
689 unsigned int offset, unsigned int bytes)
690{
691 struct nfs_server *server = NFS_SERVER(inode);
692 struct nfs_inode *nfsi = NFS_I(inode);
693 struct nfs_page *req, *new = NULL;
694 unsigned long rqend, end;
695
696 end = offset + bytes;
697
698 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
699 return ERR_PTR(-ERESTARTSYS);
700 for (;;) {
701 /* Loop over all inode entries and see if we find
702 * A request for the page we wish to update
703 */
704 spin_lock(&nfsi->req_lock);
705 req = _nfs_find_request(inode, page->index);
706 if (req) {
707 if (!nfs_lock_request_dontget(req)) {
708 int error;
709 spin_unlock(&nfsi->req_lock);
710 error = nfs_wait_on_request(req);
711 nfs_release_request(req);
Neil Brown1dd594b2006-03-20 13:44:04 -0500712 if (error < 0) {
713 if (new)
714 nfs_release_request(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return ERR_PTR(error);
Neil Brown1dd594b2006-03-20 13:44:04 -0500716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 continue;
718 }
719 spin_unlock(&nfsi->req_lock);
720 if (new)
721 nfs_release_request(new);
722 break;
723 }
724
725 if (new) {
726 int error;
727 nfs_lock_request_dontget(new);
728 error = nfs_inode_add_request(inode, new);
729 if (error) {
730 spin_unlock(&nfsi->req_lock);
731 nfs_unlock_request(new);
732 return ERR_PTR(error);
733 }
734 spin_unlock(&nfsi->req_lock);
735 nfs_mark_request_dirty(new);
736 return new;
737 }
738 spin_unlock(&nfsi->req_lock);
739
740 new = nfs_create_request(ctx, inode, page, offset, bytes);
741 if (IS_ERR(new))
742 return new;
743 }
744
745 /* We have a request for our page.
746 * If the creds don't match, or the
747 * page addresses don't match,
748 * tell the caller to wait on the conflicting
749 * request.
750 */
751 rqend = req->wb_offset + req->wb_bytes;
752 if (req->wb_context != ctx
753 || req->wb_page != page
754 || !nfs_dirty_request(req)
755 || offset > rqend || end < req->wb_offset) {
756 nfs_unlock_request(req);
757 return ERR_PTR(-EBUSY);
758 }
759
760 /* Okay, the request matches. Update the region */
761 if (offset < req->wb_offset) {
762 req->wb_offset = offset;
763 req->wb_pgbase = offset;
764 req->wb_bytes = rqend - req->wb_offset;
765 }
766
767 if (end > rqend)
768 req->wb_bytes = end - req->wb_offset;
769
770 return req;
771}
772
773int nfs_flush_incompatible(struct file *file, struct page *page)
774{
775 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
776 struct inode *inode = page->mapping->host;
777 struct nfs_page *req;
778 int status = 0;
779 /*
780 * Look for a request corresponding to this page. If there
781 * is one, and it belongs to another file, we flush it out
782 * before we try to copy anything into the page. Do this
783 * due to the lack of an ACCESS-type call in NFSv2.
784 * Also do the same if we find a request from an existing
785 * dropped page.
786 */
787 req = nfs_find_request(inode, page->index);
788 if (req) {
789 if (req->wb_page != page || ctx != req->wb_context)
790 status = nfs_wb_page(inode, page);
791 nfs_release_request(req);
792 }
793 return (status < 0) ? status : 0;
794}
795
796/*
797 * Update and possibly write a cached page of an NFS file.
798 *
799 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
800 * things with a page scheduled for an RPC call (e.g. invalidate it).
801 */
802int nfs_updatepage(struct file *file, struct page *page,
803 unsigned int offset, unsigned int count)
804{
805 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 struct inode *inode = page->mapping->host;
807 struct nfs_page *req;
808 int status = 0;
809
Chuck Lever91d5b472006-03-20 13:44:14 -0500810 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500813 file->f_dentry->d_parent->d_name.name,
814 file->f_dentry->d_name.name, count,
815 (long long)(page_offset(page) +offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 if (IS_SYNC(inode)) {
818 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
819 if (status > 0) {
820 if (offset == 0 && status == PAGE_CACHE_SIZE)
821 SetPageUptodate(page);
822 return 0;
823 }
824 return status;
825 }
826
827 /* If we're not using byte range locks, and we know the page
828 * is entirely in cache, it may be more efficient to avoid
829 * fragmenting write requests.
830 */
Trond Myklebustab0a3db2005-06-22 17:16:30 +0000831 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 loff_t end_offs = i_size_read(inode) - 1;
833 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
834
835 count += offset;
836 offset = 0;
837 if (unlikely(end_offs < 0)) {
838 /* Do nothing */
839 } else if (page->index == end_index) {
840 unsigned int pglen;
841 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
842 if (count < pglen)
843 count = pglen;
844 } else if (page->index < end_index)
845 count = PAGE_CACHE_SIZE;
846 }
847
848 /*
849 * Try to find an NFS request corresponding to this page
850 * and update it.
851 * If the existing request cannot be updated, we must flush
852 * it out now.
853 */
854 do {
855 req = nfs_update_request(ctx, inode, page, offset, count);
856 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
857 if (status != -EBUSY)
858 break;
859 /* Request could not be updated. Flush it out and try again */
860 status = nfs_wb_page(inode, page);
861 } while (status >= 0);
862 if (status < 0)
863 goto done;
864
865 status = 0;
866
867 /* Update file length */
868 nfs_grow_file(page, offset, count);
869 /* Set the PG_uptodate flag? */
870 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
871 nfs_unlock_request(req);
872done:
873 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
874 status, (long long)i_size_read(inode));
875 if (status < 0)
876 ClearPageUptodate(page);
877 return status;
878}
879
880static void nfs_writepage_release(struct nfs_page *req)
881{
882 end_page_writeback(req->wb_page);
883
884#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
885 if (!PageError(req->wb_page)) {
886 if (NFS_NEED_RESCHED(req)) {
887 nfs_mark_request_dirty(req);
888 goto out;
889 } else if (NFS_NEED_COMMIT(req)) {
890 nfs_mark_request_commit(req);
891 goto out;
892 }
893 }
894 nfs_inode_remove_request(req);
895
896out:
897 nfs_clear_commit(req);
898 nfs_clear_reschedule(req);
899#else
900 nfs_inode_remove_request(req);
901#endif
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000902 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
905static inline int flush_task_priority(int how)
906{
907 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
908 case FLUSH_HIGHPRI:
909 return RPC_PRIORITY_HIGH;
910 case FLUSH_LOWPRI:
911 return RPC_PRIORITY_LOW;
912 }
913 return RPC_PRIORITY_NORMAL;
914}
915
916/*
917 * Set up the argument/result storage required for the RPC call.
918 */
919static void nfs_write_rpcsetup(struct nfs_page *req,
920 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500921 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 unsigned int count, unsigned int offset,
923 int how)
924{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -0500926 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 /* Set up the RPC argument and reply structs
929 * NB: take care not to mess about with data->commit et al. */
930
931 data->req = req;
932 data->inode = inode = req->wb_context->dentry->d_inode;
933 data->cred = req->wb_context->cred;
934
935 data->args.fh = NFS_FH(inode);
936 data->args.offset = req_offset(req) + offset;
937 data->args.pgbase = req->wb_pgbase + offset;
938 data->args.pages = data->pagevec;
939 data->args.count = count;
940 data->args.context = req->wb_context;
941
942 data->res.fattr = &data->fattr;
943 data->res.count = count;
944 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400945 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Trond Myklebust788e7a82006-03-20 13:44:27 -0500947 /* Set up the initial task struct. */
948 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
949 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 NFS_PROTO(inode)->write_setup(data, how);
951
952 data->task.tk_priority = flush_task_priority(how);
953 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500956 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 inode->i_sb->s_id,
958 (long long)NFS_FILEID(inode),
959 count,
960 (unsigned long long)data->args.offset);
961}
962
963static void nfs_execute_write(struct nfs_write_data *data)
964{
965 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
966 sigset_t oldset;
967
968 rpc_clnt_sigmask(clnt, &oldset);
969 lock_kernel();
970 rpc_execute(&data->task);
971 unlock_kernel();
972 rpc_clnt_sigunmask(clnt, &oldset);
973}
974
975/*
976 * Generate multiple small requests to write out a single
977 * contiguous dirty area on one page.
978 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500979static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 struct nfs_page *req = nfs_list_entry(head->next);
982 struct page *page = req->wb_page;
983 struct nfs_write_data *data;
984 unsigned int wsize = NFS_SERVER(inode)->wsize;
985 unsigned int nbytes, offset;
986 int requests = 0;
987 LIST_HEAD(list);
988
989 nfs_list_remove_request(req);
990
991 nbytes = req->wb_bytes;
992 for (;;) {
Chuck Lever40859d72005-11-30 18:09:02 -0500993 data = nfs_writedata_alloc(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (!data)
995 goto out_bad;
996 list_add(&data->pages, &list);
997 requests++;
998 if (nbytes <= wsize)
999 break;
1000 nbytes -= wsize;
1001 }
1002 atomic_set(&req->wb_complete, requests);
1003
1004 ClearPageError(page);
Trond Myklebustbb713d62005-12-03 15:20:14 -05001005 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 offset = 0;
1007 nbytes = req->wb_bytes;
1008 do {
1009 data = list_entry(list.next, struct nfs_write_data, pages);
1010 list_del_init(&data->pages);
1011
1012 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 if (nbytes > wsize) {
Trond Myklebust788e7a82006-03-20 13:44:27 -05001015 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1016 wsize, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 offset += wsize;
1018 nbytes -= wsize;
1019 } else {
Trond Myklebust788e7a82006-03-20 13:44:27 -05001020 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1021 nbytes, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 nbytes = 0;
1023 }
1024 nfs_execute_write(data);
1025 } while (nbytes != 0);
1026
1027 return 0;
1028
1029out_bad:
1030 while (!list_empty(&list)) {
1031 data = list_entry(list.next, struct nfs_write_data, pages);
1032 list_del(&data->pages);
1033 nfs_writedata_free(data);
1034 }
1035 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001036 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 return -ENOMEM;
1038}
1039
1040/*
1041 * Create an RPC task for the given write request and kick it.
1042 * The page must have been locked by the caller.
1043 *
1044 * It may happen that the page we're passed is not marked dirty.
1045 * This is the case if nfs_updatepage detects a conflicting request
1046 * that has been written but not committed.
1047 */
Trond Myklebust7d46a492006-03-20 13:44:50 -05001048static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
1050 struct nfs_page *req;
1051 struct page **pages;
1052 struct nfs_write_data *data;
1053 unsigned int count;
1054
Chuck Lever40859d72005-11-30 18:09:02 -05001055 data = nfs_writedata_alloc(NFS_SERVER(inode)->wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (!data)
1057 goto out_bad;
1058
1059 pages = data->pagevec;
1060 count = 0;
1061 while (!list_empty(head)) {
1062 req = nfs_list_entry(head->next);
1063 nfs_list_remove_request(req);
1064 nfs_list_add_request(req, &data->pages);
1065 ClearPageError(req->wb_page);
Trond Myklebustbb713d62005-12-03 15:20:14 -05001066 set_page_writeback(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 *pages++ = req->wb_page;
1068 count += req->wb_bytes;
1069 }
1070 req = nfs_list_entry(data->pages.next);
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 /* Set up the argument struct */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001073 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 nfs_execute_write(data);
1076 return 0;
1077 out_bad:
1078 while (!list_empty(head)) {
1079 struct nfs_page *req = nfs_list_entry(head->next);
1080 nfs_list_remove_request(req);
1081 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001082 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084 return -ENOMEM;
1085}
1086
Trond Myklebust7d46a492006-03-20 13:44:50 -05001087static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
1089 LIST_HEAD(one_request);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001090 int (*flush_one)(struct inode *, struct list_head *, int);
1091 struct nfs_page *req;
1092 int wpages = NFS_SERVER(inode)->wpages;
1093 int wsize = NFS_SERVER(inode)->wsize;
1094 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Trond Myklebust7d46a492006-03-20 13:44:50 -05001096 flush_one = nfs_flush_one;
1097 if (wsize < PAGE_CACHE_SIZE)
1098 flush_one = nfs_flush_multi;
1099 /* For single writes, FLUSH_STABLE is more efficient */
1100 if (npages <= wpages && npages == NFS_I(inode)->npages
1101 && nfs_list_entry(head->next)->wb_bytes <= wsize)
1102 how |= FLUSH_STABLE;
1103
1104 do {
1105 nfs_coalesce_requests(head, &one_request, wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 req = nfs_list_entry(one_request.next);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001107 error = flush_one(inode, &one_request, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (error < 0)
Trond Myklebust7d46a492006-03-20 13:44:50 -05001109 goto out_err;
1110 } while (!list_empty(head));
1111 return 0;
1112out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 while (!list_empty(head)) {
1114 req = nfs_list_entry(head->next);
1115 nfs_list_remove_request(req);
1116 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001117 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
1119 return error;
1120}
1121
1122/*
1123 * Handle a write reply that flushed part of a page.
1124 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001125static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001127 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 struct nfs_page *req = data->req;
1129 struct page *page = req->wb_page;
1130
1131 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1132 req->wb_context->dentry->d_inode->i_sb->s_id,
1133 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1134 req->wb_bytes,
1135 (long long)req_offset(req));
1136
Trond Myklebust788e7a82006-03-20 13:44:27 -05001137 if (nfs_writeback_done(task, data) != 0)
1138 return;
1139
1140 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 ClearPageUptodate(page);
1142 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001143 req->wb_context->error = task->tk_status;
1144 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 } else {
1146#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1147 if (data->verf.committed < NFS_FILE_SYNC) {
1148 if (!NFS_NEED_COMMIT(req)) {
1149 nfs_defer_commit(req);
1150 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1151 dprintk(" defer commit\n");
1152 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1153 nfs_defer_reschedule(req);
1154 dprintk(" server reboot detected\n");
1155 }
1156 } else
1157#endif
1158 dprintk(" OK\n");
1159 }
1160
1161 if (atomic_dec_and_test(&req->wb_complete))
1162 nfs_writepage_release(req);
1163}
1164
Trond Myklebust788e7a82006-03-20 13:44:27 -05001165static const struct rpc_call_ops nfs_write_partial_ops = {
1166 .rpc_call_done = nfs_writeback_done_partial,
1167 .rpc_release = nfs_writedata_release,
1168};
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170/*
1171 * Handle a write reply that flushes a whole page.
1172 *
1173 * FIXME: There is an inherent race with invalidate_inode_pages and
1174 * writebacks since the page->count is kept > 1 for as long
1175 * as the page has a write request pending.
1176 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001177static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001179 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 struct nfs_page *req;
1181 struct page *page;
1182
Trond Myklebust788e7a82006-03-20 13:44:27 -05001183 if (nfs_writeback_done(task, data) != 0)
1184 return;
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 /* Update attributes as result of writeback. */
1187 while (!list_empty(&data->pages)) {
1188 req = nfs_list_entry(data->pages.next);
1189 nfs_list_remove_request(req);
1190 page = req->wb_page;
1191
1192 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1193 req->wb_context->dentry->d_inode->i_sb->s_id,
1194 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1195 req->wb_bytes,
1196 (long long)req_offset(req));
1197
Trond Myklebust788e7a82006-03-20 13:44:27 -05001198 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 ClearPageUptodate(page);
1200 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001201 req->wb_context->error = task->tk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 end_page_writeback(page);
1203 nfs_inode_remove_request(req);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001204 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 goto next;
1206 }
1207 end_page_writeback(page);
1208
1209#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1210 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1211 nfs_inode_remove_request(req);
1212 dprintk(" OK\n");
1213 goto next;
1214 }
1215 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1216 nfs_mark_request_commit(req);
1217 dprintk(" marked for commit\n");
1218#else
1219 nfs_inode_remove_request(req);
1220#endif
1221 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001222 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224}
1225
Trond Myklebust788e7a82006-03-20 13:44:27 -05001226static const struct rpc_call_ops nfs_write_full_ops = {
1227 .rpc_call_done = nfs_writeback_done_full,
1228 .rpc_release = nfs_writedata_release,
1229};
1230
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232/*
1233 * This function is called when the WRITE call is complete.
1234 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001235int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 struct nfs_writeargs *argp = &data->args;
1238 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001239 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1242 task->tk_pid, task->tk_status);
1243
Trond Myklebust788e7a82006-03-20 13:44:27 -05001244 /* Call the NFS version-specific code */
1245 status = NFS_PROTO(data->inode)->write_done(task, data);
1246 if (status != 0)
1247 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001248 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1251 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1252 /* We tried a write call, but the server did not
1253 * commit data to stable storage even though we
1254 * requested it.
1255 * Note: There is a known bug in Tru64 < 5.0 in which
1256 * the server reports NFS_DATA_SYNC, but performs
1257 * NFS_FILE_SYNC. We therefore implement this checking
1258 * as a dprintk() in order to avoid filling syslog.
1259 */
1260 static unsigned long complain;
1261
1262 if (time_before(complain, jiffies)) {
1263 dprintk("NFS: faulty NFS server %s:"
1264 " (committed = %d) != (stable = %d)\n",
1265 NFS_SERVER(data->inode)->hostname,
1266 resp->verf->committed, argp->stable);
1267 complain = jiffies + 300 * HZ;
1268 }
1269 }
1270#endif
1271 /* Is this a short write? */
1272 if (task->tk_status >= 0 && resp->count < argp->count) {
1273 static unsigned long complain;
1274
Chuck Lever91d5b472006-03-20 13:44:14 -05001275 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /* Has the server at least made some progress? */
1278 if (resp->count != 0) {
1279 /* Was this an NFSv2 write or an NFSv3 stable write? */
1280 if (resp->verf->committed != NFS_UNSTABLE) {
1281 /* Resend from where the server left off */
1282 argp->offset += resp->count;
1283 argp->pgbase += resp->count;
1284 argp->count -= resp->count;
1285 } else {
1286 /* Resend as a stable write in order to avoid
1287 * headaches in the case of a server crash.
1288 */
1289 argp->stable = NFS_FILE_SYNC;
1290 }
1291 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001292 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294 if (time_before(complain, jiffies)) {
1295 printk(KERN_WARNING
1296 "NFS: Server wrote zero bytes, expected %u.\n",
1297 argp->count);
1298 complain = jiffies + 300 * HZ;
1299 }
1300 /* Can't do anything about it except throw an error. */
1301 task->tk_status = -EIO;
1302 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
1305
1306
1307#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001308void nfs_commit_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 nfs_commit_free(wdata);
1311}
1312
1313/*
1314 * Set up the argument/result storage required for the RPC call.
1315 */
1316static void nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001317 struct nfs_write_data *data,
1318 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001320 struct nfs_page *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001322 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 /* Set up the RPC argument and reply structs
1325 * NB: take care not to mess about with data->commit et al. */
1326
1327 list_splice_init(head, &data->pages);
1328 first = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 inode = first->wb_context->dentry->d_inode;
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 data->inode = inode;
1332 data->cred = first->wb_context->cred;
1333
1334 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001335 /* Note: we always request a commit of the entire inode */
1336 data->args.offset = 0;
1337 data->args.count = 0;
1338 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 data->res.fattr = &data->fattr;
1340 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001341 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001342
1343 /* Set up the initial task struct. */
1344 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1345 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 NFS_PROTO(inode)->commit_setup(data, how);
1347
1348 data->task.tk_priority = flush_task_priority(how);
1349 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Chuck Lever0bbacc42005-11-01 16:53:32 -05001351 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352}
1353
1354/*
1355 * Commit dirty pages
1356 */
1357static int
Chuck Lever40859d72005-11-30 18:09:02 -05001358nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
1360 struct nfs_write_data *data;
1361 struct nfs_page *req;
1362
Chuck Lever40859d72005-11-30 18:09:02 -05001363 data = nfs_commit_alloc(NFS_SERVER(inode)->wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 if (!data)
1366 goto out_bad;
1367
1368 /* Set up the argument struct */
1369 nfs_commit_rpcsetup(head, data, how);
1370
1371 nfs_execute_write(data);
1372 return 0;
1373 out_bad:
1374 while (!list_empty(head)) {
1375 req = nfs_list_entry(head->next);
1376 nfs_list_remove_request(req);
1377 nfs_mark_request_commit(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001378 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380 return -ENOMEM;
1381}
1382
1383/*
1384 * COMMIT call returned
1385 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001386static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001388 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 struct nfs_page *req;
1390 int res = 0;
1391
1392 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1393 task->tk_pid, task->tk_status);
1394
Trond Myklebust788e7a82006-03-20 13:44:27 -05001395 /* Call the NFS version-specific code */
1396 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1397 return;
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 while (!list_empty(&data->pages)) {
1400 req = nfs_list_entry(data->pages.next);
1401 nfs_list_remove_request(req);
1402
1403 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1404 req->wb_context->dentry->d_inode->i_sb->s_id,
1405 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1406 req->wb_bytes,
1407 (long long)req_offset(req));
1408 if (task->tk_status < 0) {
1409 req->wb_context->error = task->tk_status;
1410 nfs_inode_remove_request(req);
1411 dprintk(", error = %d\n", task->tk_status);
1412 goto next;
1413 }
1414
1415 /* Okay, COMMIT succeeded, apparently. Check the verifier
1416 * returned by the server against all stored verfs. */
1417 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1418 /* We have a match */
1419 nfs_inode_remove_request(req);
1420 dprintk(" OK\n");
1421 goto next;
1422 }
1423 /* We have a mismatch. Write the page again */
1424 dprintk(" mismatch\n");
1425 nfs_mark_request_dirty(req);
1426 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001427 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 res++;
1429 }
1430 sub_page_state(nr_unstable,res);
1431}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001432
1433static const struct rpc_call_ops nfs_commit_ops = {
1434 .rpc_call_done = nfs_commit_done,
1435 .rpc_release = nfs_commit_release,
1436};
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001437#else
1438static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1439{
1440 return 0;
1441}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442#endif
1443
1444static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
1445 unsigned int npages, int how)
1446{
1447 struct nfs_inode *nfsi = NFS_I(inode);
1448 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001449 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 spin_lock(&nfsi->req_lock);
1452 res = nfs_scan_dirty(inode, &head, idx_start, npages);
1453 spin_unlock(&nfsi->req_lock);
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001454 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001455 int error = nfs_flush_list(inode, &head, res, how);
1456 if (error < 0)
1457 return error;
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return res;
1460}
1461
1462#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001463int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
1465 struct nfs_inode *nfsi = NFS_I(inode);
1466 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001467 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001470 res = nfs_scan_commit(inode, &head, 0, 0);
1471 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001473 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001474 if (error < 0)
1475 return error;
1476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return res;
1478}
1479#endif
1480
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001481int nfs_sync_inode_wait(struct inode *inode, unsigned long idx_start,
1482 unsigned int npages, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001484 struct nfs_inode *nfsi = NFS_I(inode);
1485 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001486 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001487 int pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001489 how &= ~FLUSH_NOCOMMIT;
1490 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 do {
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001492 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1493 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001494 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001495 pages = nfs_scan_dirty(inode, &head, idx_start, npages);
1496 if (pages != 0) {
1497 spin_unlock(&nfsi->req_lock);
1498 ret = nfs_flush_list(inode, &head, pages, how);
1499 spin_lock(&nfsi->req_lock);
1500 continue;
1501 }
1502 if (nocommit)
1503 break;
1504 pages = nfs_scan_commit(inode, &head, 0, 0);
1505 if (pages == 0)
1506 break;
1507 spin_unlock(&nfsi->req_lock);
1508 ret = nfs_commit_list(inode, &head, how);
1509 spin_lock(&nfsi->req_lock);
1510 } while (ret >= 0);
1511 spin_unlock(&nfsi->req_lock);
1512 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
1515int nfs_init_writepagecache(void)
1516{
1517 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1518 sizeof(struct nfs_write_data),
1519 0, SLAB_HWCACHE_ALIGN,
1520 NULL, NULL);
1521 if (nfs_wdata_cachep == NULL)
1522 return -ENOMEM;
1523
1524 nfs_wdata_mempool = mempool_create(MIN_POOL_WRITE,
1525 mempool_alloc_slab,
1526 mempool_free_slab,
1527 nfs_wdata_cachep);
1528 if (nfs_wdata_mempool == NULL)
1529 return -ENOMEM;
1530
1531 nfs_commit_mempool = mempool_create(MIN_POOL_COMMIT,
1532 mempool_alloc_slab,
1533 mempool_free_slab,
1534 nfs_wdata_cachep);
1535 if (nfs_commit_mempool == NULL)
1536 return -ENOMEM;
1537
1538 return 0;
1539}
1540
1541void nfs_destroy_writepagecache(void)
1542{
1543 mempool_destroy(nfs_commit_mempool);
1544 mempool_destroy(nfs_wdata_mempool);
1545 if (kmem_cache_destroy(nfs_wdata_cachep))
1546 printk(KERN_INFO "nfs_write_data: not all structures were freed\n");
1547}
1548