blob: e7c8361cf201b1277d465b76fd76d22450a96dda [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);
80static void nfs_writeback_done_partial(struct nfs_write_data *, int);
81static void nfs_writeback_done_full(struct nfs_write_data *, int);
82static int nfs_wait_on_write_congestion(struct address_space *, int);
83static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int);
84static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
85 unsigned int npages, int how);
86
87static kmem_cache_t *nfs_wdata_cachep;
88mempool_t *nfs_wdata_mempool;
89static mempool_t *nfs_commit_mempool;
90
91static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
92
Chuck Lever40859d72005-11-30 18:09:02 -050093static inline struct nfs_write_data *nfs_commit_alloc(unsigned int pagecount)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS);
Chuck Lever40859d72005-11-30 18:09:02 -050096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (p) {
98 memset(p, 0, sizeof(*p));
99 INIT_LIST_HEAD(&p->pages);
Chuck Lever40859d72005-11-30 18:09:02 -0500100 if (pagecount < NFS_PAGEVEC_SIZE)
101 p->pagevec = &p->page_array[0];
102 else {
103 size_t size = ++pagecount * sizeof(struct page *);
Eric Sesterhennbd647542006-03-20 13:44:10 -0500104 p->pagevec = kzalloc(size, GFP_NOFS);
105 if (!p->pagevec) {
Chuck Lever40859d72005-11-30 18:09:02 -0500106 mempool_free(p, nfs_commit_mempool);
107 p = NULL;
108 }
109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
111 return p;
112}
113
114static inline void nfs_commit_free(struct nfs_write_data *p)
115{
Chuck Lever40859d72005-11-30 18:09:02 -0500116 if (p && (p->pagevec != &p->page_array[0]))
117 kfree(p->pagevec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 mempool_free(p, nfs_commit_mempool);
119}
120
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100121void nfs_writedata_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 nfs_writedata_free(wdata);
124}
125
126/* Adjust the file length if we're writing beyond the end */
127static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
128{
129 struct inode *inode = page->mapping->host;
130 loff_t end, i_size = i_size_read(inode);
131 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
132
133 if (i_size > 0 && page->index < end_index)
134 return;
135 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
136 if (i_size >= end)
137 return;
Chuck Lever91d5b472006-03-20 13:44:14 -0500138 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 i_size_write(inode, end);
140}
141
142/* We can set the PG_uptodate flag if we see that a write request
143 * covers the full page.
144 */
145static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
146{
147 loff_t end_offs;
148
149 if (PageUptodate(page))
150 return;
151 if (base != 0)
152 return;
153 if (count == PAGE_CACHE_SIZE) {
154 SetPageUptodate(page);
155 return;
156 }
157
158 end_offs = i_size_read(page->mapping->host) - 1;
159 if (end_offs < 0)
160 return;
161 /* Is this the last page? */
162 if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT))
163 return;
164 /* This is the last page: set PG_uptodate if we cover the entire
165 * extent of the data, then zero the rest of the page.
166 */
167 if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) {
168 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
169 SetPageUptodate(page);
170 }
171}
172
173/*
174 * Write a page synchronously.
175 * Offset is the data offset within the page.
176 */
177static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
178 struct page *page, unsigned int offset, unsigned int count,
179 int how)
180{
181 unsigned int wsize = NFS_SERVER(inode)->wsize;
182 int result, written = 0;
183 struct nfs_write_data *wdata;
184
Chuck Lever40859d72005-11-30 18:09:02 -0500185 wdata = nfs_writedata_alloc(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (!wdata)
187 return -ENOMEM;
188
189 wdata->flags = how;
190 wdata->cred = ctx->cred;
191 wdata->inode = inode;
192 wdata->args.fh = NFS_FH(inode);
193 wdata->args.context = ctx;
194 wdata->args.pages = &page;
195 wdata->args.stable = NFS_FILE_SYNC;
196 wdata->args.pgbase = offset;
197 wdata->args.count = wsize;
198 wdata->res.fattr = &wdata->fattr;
199 wdata->res.verf = &wdata->verf;
200
201 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
202 inode->i_sb->s_id,
203 (long long)NFS_FILEID(inode),
204 count, (long long)(page_offset(page) + offset));
205
Trond Myklebustbb713d62005-12-03 15:20:14 -0500206 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 nfs_begin_data_update(inode);
208 do {
209 if (count < wsize)
210 wdata->args.count = count;
211 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
212
213 result = NFS_PROTO(inode)->write(wdata);
214
215 if (result < 0) {
216 /* Must mark the page invalid after I/O error */
217 ClearPageUptodate(page);
218 goto io_error;
219 }
220 if (result < wdata->args.count)
221 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
222 wdata->args.count, result);
223
224 wdata->args.offset += result;
225 wdata->args.pgbase += result;
226 written += result;
227 count -= result;
Chuck Lever91d5b472006-03-20 13:44:14 -0500228 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 } while (count);
230 /* Update file length */
231 nfs_grow_file(page, offset, written);
232 /* Set the PG_uptodate flag? */
233 nfs_mark_uptodate(page, offset, written);
234
235 if (PageError(page))
236 ClearPageError(page);
237
238io_error:
Trond Myklebust951a1432005-06-22 17:16:30 +0000239 nfs_end_data_update(inode);
Trond Myklebustbb713d62005-12-03 15:20:14 -0500240 end_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 nfs_writedata_free(wdata);
242 return written ? written : result;
243}
244
245static int nfs_writepage_async(struct nfs_open_context *ctx,
246 struct inode *inode, struct page *page,
247 unsigned int offset, unsigned int count)
248{
249 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 req = nfs_update_request(ctx, inode, page, offset, count);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100252 if (IS_ERR(req))
253 return PTR_ERR(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 /* Update file length */
255 nfs_grow_file(page, offset, count);
256 /* Set the PG_uptodate flag? */
257 nfs_mark_uptodate(page, offset, count);
258 nfs_unlock_request(req);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
262static int wb_priority(struct writeback_control *wbc)
263{
264 if (wbc->for_reclaim)
265 return FLUSH_HIGHPRI;
266 if (wbc->for_kupdate)
267 return FLUSH_LOWPRI;
268 return 0;
269}
270
271/*
272 * Write an mmapped page to the server.
273 */
274int nfs_writepage(struct page *page, struct writeback_control *wbc)
275{
276 struct nfs_open_context *ctx;
277 struct inode *inode = page->mapping->host;
278 unsigned long end_index;
279 unsigned offset = PAGE_CACHE_SIZE;
280 loff_t i_size = i_size_read(inode);
281 int inode_referenced = 0;
282 int priority = wb_priority(wbc);
283 int err;
284
Chuck Lever91d5b472006-03-20 13:44:14 -0500285 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
286 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 /*
289 * Note: We need to ensure that we have a reference to the inode
290 * if we are to do asynchronous writes. If not, waiting
291 * in nfs_wait_on_request() may deadlock with clear_inode().
292 *
293 * If igrab() fails here, then it is in any case safe to
294 * call nfs_wb_page(), since there will be no pending writes.
295 */
296 if (igrab(inode) != 0)
297 inode_referenced = 1;
298 end_index = i_size >> PAGE_CACHE_SHIFT;
299
300 /* Ensure we've flushed out any previous writes */
301 nfs_wb_page_priority(inode, page, priority);
302
303 /* easy case */
304 if (page->index < end_index)
305 goto do_it;
306 /* things got complicated... */
307 offset = i_size & (PAGE_CACHE_SIZE-1);
308
309 /* OK, are we completely out? */
310 err = 0; /* potential race with truncate - ignore */
311 if (page->index >= end_index+1 || !offset)
312 goto out;
313do_it:
Trond Myklebustd5308382005-11-04 15:33:38 -0500314 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (ctx == NULL) {
316 err = -EBADF;
317 goto out;
318 }
319 lock_kernel();
320 if (!IS_SYNC(inode) && inode_referenced) {
321 err = nfs_writepage_async(ctx, inode, page, 0, offset);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100322 if (!wbc->for_writepages)
323 nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 } else {
325 err = nfs_writepage_sync(ctx, inode, page, 0,
326 offset, priority);
327 if (err >= 0) {
328 if (err != offset)
329 redirty_page_for_writepage(wbc, page);
330 err = 0;
331 }
332 }
333 unlock_kernel();
334 put_nfs_open_context(ctx);
335out:
336 unlock_page(page);
337 if (inode_referenced)
338 iput(inode);
339 return err;
340}
341
342/*
343 * Note: causes nfs_update_request() to block on the assumption
344 * that the writeback is generated due to memory pressure.
345 */
346int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
347{
348 struct backing_dev_info *bdi = mapping->backing_dev_info;
349 struct inode *inode = mapping->host;
350 int err;
351
Chuck Lever91d5b472006-03-20 13:44:14 -0500352 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 err = generic_writepages(mapping, wbc);
355 if (err)
356 return err;
357 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
358 if (wbc->nonblocking)
359 return 0;
360 nfs_wait_on_write_congestion(mapping, 0);
361 }
362 err = nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
363 if (err < 0)
364 goto out;
Chuck Lever91d5b472006-03-20 13:44:14 -0500365 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 wbc->nr_to_write -= err;
367 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
368 err = nfs_wait_on_requests(inode, 0, 0);
369 if (err < 0)
370 goto out;
371 }
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000372 err = nfs_commit_inode(inode, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (err > 0) {
374 wbc->nr_to_write -= err;
375 err = 0;
376 }
377out:
378 clear_bit(BDI_write_congested, &bdi->state);
379 wake_up_all(&nfs_write_congestion);
380 return err;
381}
382
383/*
384 * Insert a write request into an inode
385 */
386static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
387{
388 struct nfs_inode *nfsi = NFS_I(inode);
389 int error;
390
391 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
392 BUG_ON(error == -EEXIST);
393 if (error)
394 return error;
395 if (!nfsi->npages) {
396 igrab(inode);
397 nfs_begin_data_update(inode);
398 if (nfs_have_delegation(inode, FMODE_WRITE))
399 nfsi->change_attr++;
400 }
401 nfsi->npages++;
402 atomic_inc(&req->wb_count);
403 return 0;
404}
405
406/*
407 * Insert a write request into an inode
408 */
409static void nfs_inode_remove_request(struct nfs_page *req)
410{
411 struct inode *inode = req->wb_context->dentry->d_inode;
412 struct nfs_inode *nfsi = NFS_I(inode);
413
414 BUG_ON (!NFS_WBACK_BUSY(req));
415
416 spin_lock(&nfsi->req_lock);
417 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
418 nfsi->npages--;
419 if (!nfsi->npages) {
420 spin_unlock(&nfsi->req_lock);
Trond Myklebust951a1432005-06-22 17:16:30 +0000421 nfs_end_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 iput(inode);
423 } else
424 spin_unlock(&nfsi->req_lock);
425 nfs_clear_request(req);
426 nfs_release_request(req);
427}
428
429/*
430 * Find a request
431 */
432static inline struct nfs_page *
433_nfs_find_request(struct inode *inode, unsigned long index)
434{
435 struct nfs_inode *nfsi = NFS_I(inode);
436 struct nfs_page *req;
437
438 req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index);
439 if (req)
440 atomic_inc(&req->wb_count);
441 return req;
442}
443
444static struct nfs_page *
445nfs_find_request(struct inode *inode, unsigned long index)
446{
447 struct nfs_page *req;
448 struct nfs_inode *nfsi = NFS_I(inode);
449
450 spin_lock(&nfsi->req_lock);
451 req = _nfs_find_request(inode, index);
452 spin_unlock(&nfsi->req_lock);
453 return req;
454}
455
456/*
457 * Add a request to the inode's dirty list.
458 */
459static void
460nfs_mark_request_dirty(struct nfs_page *req)
461{
462 struct inode *inode = req->wb_context->dentry->d_inode;
463 struct nfs_inode *nfsi = NFS_I(inode);
464
465 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000466 radix_tree_tag_set(&nfsi->nfs_page_tree,
467 req->wb_index, NFS_PAGE_TAG_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 nfs_list_add_request(req, &nfsi->dirty);
469 nfsi->ndirty++;
470 spin_unlock(&nfsi->req_lock);
471 inc_page_state(nr_dirty);
472 mark_inode_dirty(inode);
473}
474
475/*
476 * Check if a request is dirty
477 */
478static inline int
479nfs_dirty_request(struct nfs_page *req)
480{
481 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
482 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
483}
484
485#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
486/*
487 * Add a request to the inode's commit list.
488 */
489static void
490nfs_mark_request_commit(struct nfs_page *req)
491{
492 struct inode *inode = req->wb_context->dentry->d_inode;
493 struct nfs_inode *nfsi = NFS_I(inode);
494
495 spin_lock(&nfsi->req_lock);
496 nfs_list_add_request(req, &nfsi->commit);
497 nfsi->ncommit++;
498 spin_unlock(&nfsi->req_lock);
499 inc_page_state(nr_unstable);
500 mark_inode_dirty(inode);
501}
502#endif
503
504/*
505 * Wait for a request to complete.
506 *
507 * Interruptible by signals only if mounted with intr flag.
508 */
509static int
510nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
511{
512 struct nfs_inode *nfsi = NFS_I(inode);
513 struct nfs_page *req;
514 unsigned long idx_end, next;
515 unsigned int res = 0;
516 int error;
517
518 if (npages == 0)
519 idx_end = ~0;
520 else
521 idx_end = idx_start + npages - 1;
522
523 spin_lock(&nfsi->req_lock);
524 next = idx_start;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000525 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 -0700526 if (req->wb_index > idx_end)
527 break;
528
529 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000530 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 atomic_inc(&req->wb_count);
533 spin_unlock(&nfsi->req_lock);
534 error = nfs_wait_on_request(req);
535 nfs_release_request(req);
536 if (error < 0)
537 return error;
538 spin_lock(&nfsi->req_lock);
539 res++;
540 }
541 spin_unlock(&nfsi->req_lock);
542 return res;
543}
544
545/*
546 * nfs_scan_dirty - Scan an inode for dirty requests
547 * @inode: NFS inode to scan
548 * @dst: destination list
549 * @idx_start: lower bound of page->index to scan.
550 * @npages: idx_start + npages sets the upper bound to scan.
551 *
552 * Moves requests from the inode's dirty page list.
553 * The requests are *not* checked to ensure that they form a contiguous set.
554 */
555static int
556nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
557{
558 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000559 int res = 0;
560
561 if (nfsi->ndirty != 0) {
562 res = nfs_scan_lock_dirty(nfsi, dst, idx_start, npages);
563 nfsi->ndirty -= res;
564 sub_page_state(nr_dirty,res);
565 if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
566 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return res;
569}
570
571#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
572/*
573 * nfs_scan_commit - Scan an inode for commit requests
574 * @inode: NFS inode to scan
575 * @dst: destination list
576 * @idx_start: lower bound of page->index to scan.
577 * @npages: idx_start + npages sets the upper bound to scan.
578 *
579 * Moves requests from the inode's 'commit' request list.
580 * The requests are *not* checked to ensure that they form a contiguous set.
581 */
582static int
583nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
584{
585 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000586 int res = 0;
587
588 if (nfsi->ncommit != 0) {
589 res = nfs_scan_list(&nfsi->commit, dst, idx_start, npages);
590 nfsi->ncommit -= res;
591 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
592 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return res;
595}
596#endif
597
598static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
599{
600 struct backing_dev_info *bdi = mapping->backing_dev_info;
601 DEFINE_WAIT(wait);
602 int ret = 0;
603
604 might_sleep();
605
606 if (!bdi_write_congested(bdi))
607 return 0;
Chuck Lever91d5b472006-03-20 13:44:14 -0500608
609 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (intr) {
612 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
613 sigset_t oldset;
614
615 rpc_clnt_sigmask(clnt, &oldset);
616 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
617 if (bdi_write_congested(bdi)) {
618 if (signalled())
619 ret = -ERESTARTSYS;
620 else
621 schedule();
622 }
623 rpc_clnt_sigunmask(clnt, &oldset);
624 } else {
625 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
626 if (bdi_write_congested(bdi))
627 schedule();
628 }
629 finish_wait(&nfs_write_congestion, &wait);
630 return ret;
631}
632
633
634/*
635 * Try to update any existing write request, or create one if there is none.
636 * In order to match, the request's credentials must match those of
637 * the calling process.
638 *
639 * Note: Should always be called with the Page Lock held!
640 */
641static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
642 struct inode *inode, struct page *page,
643 unsigned int offset, unsigned int bytes)
644{
645 struct nfs_server *server = NFS_SERVER(inode);
646 struct nfs_inode *nfsi = NFS_I(inode);
647 struct nfs_page *req, *new = NULL;
648 unsigned long rqend, end;
649
650 end = offset + bytes;
651
652 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
653 return ERR_PTR(-ERESTARTSYS);
654 for (;;) {
655 /* Loop over all inode entries and see if we find
656 * A request for the page we wish to update
657 */
658 spin_lock(&nfsi->req_lock);
659 req = _nfs_find_request(inode, page->index);
660 if (req) {
661 if (!nfs_lock_request_dontget(req)) {
662 int error;
663 spin_unlock(&nfsi->req_lock);
664 error = nfs_wait_on_request(req);
665 nfs_release_request(req);
Neil Brown1dd594b2006-03-20 13:44:04 -0500666 if (error < 0) {
667 if (new)
668 nfs_release_request(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return ERR_PTR(error);
Neil Brown1dd594b2006-03-20 13:44:04 -0500670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 continue;
672 }
673 spin_unlock(&nfsi->req_lock);
674 if (new)
675 nfs_release_request(new);
676 break;
677 }
678
679 if (new) {
680 int error;
681 nfs_lock_request_dontget(new);
682 error = nfs_inode_add_request(inode, new);
683 if (error) {
684 spin_unlock(&nfsi->req_lock);
685 nfs_unlock_request(new);
686 return ERR_PTR(error);
687 }
688 spin_unlock(&nfsi->req_lock);
689 nfs_mark_request_dirty(new);
690 return new;
691 }
692 spin_unlock(&nfsi->req_lock);
693
694 new = nfs_create_request(ctx, inode, page, offset, bytes);
695 if (IS_ERR(new))
696 return new;
697 }
698
699 /* We have a request for our page.
700 * If the creds don't match, or the
701 * page addresses don't match,
702 * tell the caller to wait on the conflicting
703 * request.
704 */
705 rqend = req->wb_offset + req->wb_bytes;
706 if (req->wb_context != ctx
707 || req->wb_page != page
708 || !nfs_dirty_request(req)
709 || offset > rqend || end < req->wb_offset) {
710 nfs_unlock_request(req);
711 return ERR_PTR(-EBUSY);
712 }
713
714 /* Okay, the request matches. Update the region */
715 if (offset < req->wb_offset) {
716 req->wb_offset = offset;
717 req->wb_pgbase = offset;
718 req->wb_bytes = rqend - req->wb_offset;
719 }
720
721 if (end > rqend)
722 req->wb_bytes = end - req->wb_offset;
723
724 return req;
725}
726
727int nfs_flush_incompatible(struct file *file, struct page *page)
728{
729 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
730 struct inode *inode = page->mapping->host;
731 struct nfs_page *req;
732 int status = 0;
733 /*
734 * Look for a request corresponding to this page. If there
735 * is one, and it belongs to another file, we flush it out
736 * before we try to copy anything into the page. Do this
737 * due to the lack of an ACCESS-type call in NFSv2.
738 * Also do the same if we find a request from an existing
739 * dropped page.
740 */
741 req = nfs_find_request(inode, page->index);
742 if (req) {
743 if (req->wb_page != page || ctx != req->wb_context)
744 status = nfs_wb_page(inode, page);
745 nfs_release_request(req);
746 }
747 return (status < 0) ? status : 0;
748}
749
750/*
751 * Update and possibly write a cached page of an NFS file.
752 *
753 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
754 * things with a page scheduled for an RPC call (e.g. invalidate it).
755 */
756int nfs_updatepage(struct file *file, struct page *page,
757 unsigned int offset, unsigned int count)
758{
759 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 struct inode *inode = page->mapping->host;
761 struct nfs_page *req;
762 int status = 0;
763
Chuck Lever91d5b472006-03-20 13:44:14 -0500764 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500767 file->f_dentry->d_parent->d_name.name,
768 file->f_dentry->d_name.name, count,
769 (long long)(page_offset(page) +offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 if (IS_SYNC(inode)) {
772 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
773 if (status > 0) {
774 if (offset == 0 && status == PAGE_CACHE_SIZE)
775 SetPageUptodate(page);
776 return 0;
777 }
778 return status;
779 }
780
781 /* If we're not using byte range locks, and we know the page
782 * is entirely in cache, it may be more efficient to avoid
783 * fragmenting write requests.
784 */
Trond Myklebustab0a3db2005-06-22 17:16:30 +0000785 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 loff_t end_offs = i_size_read(inode) - 1;
787 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
788
789 count += offset;
790 offset = 0;
791 if (unlikely(end_offs < 0)) {
792 /* Do nothing */
793 } else if (page->index == end_index) {
794 unsigned int pglen;
795 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
796 if (count < pglen)
797 count = pglen;
798 } else if (page->index < end_index)
799 count = PAGE_CACHE_SIZE;
800 }
801
802 /*
803 * Try to find an NFS request corresponding to this page
804 * and update it.
805 * If the existing request cannot be updated, we must flush
806 * it out now.
807 */
808 do {
809 req = nfs_update_request(ctx, inode, page, offset, count);
810 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
811 if (status != -EBUSY)
812 break;
813 /* Request could not be updated. Flush it out and try again */
814 status = nfs_wb_page(inode, page);
815 } while (status >= 0);
816 if (status < 0)
817 goto done;
818
819 status = 0;
820
821 /* Update file length */
822 nfs_grow_file(page, offset, count);
823 /* Set the PG_uptodate flag? */
824 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
825 nfs_unlock_request(req);
826done:
827 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
828 status, (long long)i_size_read(inode));
829 if (status < 0)
830 ClearPageUptodate(page);
831 return status;
832}
833
834static void nfs_writepage_release(struct nfs_page *req)
835{
836 end_page_writeback(req->wb_page);
837
838#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
839 if (!PageError(req->wb_page)) {
840 if (NFS_NEED_RESCHED(req)) {
841 nfs_mark_request_dirty(req);
842 goto out;
843 } else if (NFS_NEED_COMMIT(req)) {
844 nfs_mark_request_commit(req);
845 goto out;
846 }
847 }
848 nfs_inode_remove_request(req);
849
850out:
851 nfs_clear_commit(req);
852 nfs_clear_reschedule(req);
853#else
854 nfs_inode_remove_request(req);
855#endif
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000856 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
859static inline int flush_task_priority(int how)
860{
861 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
862 case FLUSH_HIGHPRI:
863 return RPC_PRIORITY_HIGH;
864 case FLUSH_LOWPRI:
865 return RPC_PRIORITY_LOW;
866 }
867 return RPC_PRIORITY_NORMAL;
868}
869
870/*
871 * Set up the argument/result storage required for the RPC call.
872 */
873static void nfs_write_rpcsetup(struct nfs_page *req,
874 struct nfs_write_data *data,
875 unsigned int count, unsigned int offset,
876 int how)
877{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 struct inode *inode;
879
880 /* Set up the RPC argument and reply structs
881 * NB: take care not to mess about with data->commit et al. */
882
883 data->req = req;
884 data->inode = inode = req->wb_context->dentry->d_inode;
885 data->cred = req->wb_context->cred;
886
887 data->args.fh = NFS_FH(inode);
888 data->args.offset = req_offset(req) + offset;
889 data->args.pgbase = req->wb_pgbase + offset;
890 data->args.pages = data->pagevec;
891 data->args.count = count;
892 data->args.context = req->wb_context;
893
894 data->res.fattr = &data->fattr;
895 data->res.count = count;
896 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400897 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 NFS_PROTO(inode)->write_setup(data, how);
900
901 data->task.tk_priority = flush_task_priority(how);
902 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500905 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 inode->i_sb->s_id,
907 (long long)NFS_FILEID(inode),
908 count,
909 (unsigned long long)data->args.offset);
910}
911
912static void nfs_execute_write(struct nfs_write_data *data)
913{
914 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
915 sigset_t oldset;
916
917 rpc_clnt_sigmask(clnt, &oldset);
918 lock_kernel();
919 rpc_execute(&data->task);
920 unlock_kernel();
921 rpc_clnt_sigunmask(clnt, &oldset);
922}
923
924/*
925 * Generate multiple small requests to write out a single
926 * contiguous dirty area on one page.
927 */
928static int nfs_flush_multi(struct list_head *head, struct inode *inode, int how)
929{
930 struct nfs_page *req = nfs_list_entry(head->next);
931 struct page *page = req->wb_page;
932 struct nfs_write_data *data;
933 unsigned int wsize = NFS_SERVER(inode)->wsize;
934 unsigned int nbytes, offset;
935 int requests = 0;
936 LIST_HEAD(list);
937
938 nfs_list_remove_request(req);
939
940 nbytes = req->wb_bytes;
941 for (;;) {
Chuck Lever40859d72005-11-30 18:09:02 -0500942 data = nfs_writedata_alloc(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (!data)
944 goto out_bad;
945 list_add(&data->pages, &list);
946 requests++;
947 if (nbytes <= wsize)
948 break;
949 nbytes -= wsize;
950 }
951 atomic_set(&req->wb_complete, requests);
952
953 ClearPageError(page);
Trond Myklebustbb713d62005-12-03 15:20:14 -0500954 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 offset = 0;
956 nbytes = req->wb_bytes;
957 do {
958 data = list_entry(list.next, struct nfs_write_data, pages);
959 list_del_init(&data->pages);
960
961 data->pagevec[0] = page;
962 data->complete = nfs_writeback_done_partial;
963
964 if (nbytes > wsize) {
965 nfs_write_rpcsetup(req, data, wsize, offset, how);
966 offset += wsize;
967 nbytes -= wsize;
968 } else {
969 nfs_write_rpcsetup(req, data, nbytes, offset, how);
970 nbytes = 0;
971 }
972 nfs_execute_write(data);
973 } while (nbytes != 0);
974
975 return 0;
976
977out_bad:
978 while (!list_empty(&list)) {
979 data = list_entry(list.next, struct nfs_write_data, pages);
980 list_del(&data->pages);
981 nfs_writedata_free(data);
982 }
983 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000984 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return -ENOMEM;
986}
987
988/*
989 * Create an RPC task for the given write request and kick it.
990 * The page must have been locked by the caller.
991 *
992 * It may happen that the page we're passed is not marked dirty.
993 * This is the case if nfs_updatepage detects a conflicting request
994 * that has been written but not committed.
995 */
996static int nfs_flush_one(struct list_head *head, struct inode *inode, int how)
997{
998 struct nfs_page *req;
999 struct page **pages;
1000 struct nfs_write_data *data;
1001 unsigned int count;
1002
1003 if (NFS_SERVER(inode)->wsize < PAGE_CACHE_SIZE)
1004 return nfs_flush_multi(head, inode, how);
1005
Chuck Lever40859d72005-11-30 18:09:02 -05001006 data = nfs_writedata_alloc(NFS_SERVER(inode)->wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (!data)
1008 goto out_bad;
1009
1010 pages = data->pagevec;
1011 count = 0;
1012 while (!list_empty(head)) {
1013 req = nfs_list_entry(head->next);
1014 nfs_list_remove_request(req);
1015 nfs_list_add_request(req, &data->pages);
1016 ClearPageError(req->wb_page);
Trond Myklebustbb713d62005-12-03 15:20:14 -05001017 set_page_writeback(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 *pages++ = req->wb_page;
1019 count += req->wb_bytes;
1020 }
1021 req = nfs_list_entry(data->pages.next);
1022
1023 data->complete = nfs_writeback_done_full;
1024 /* Set up the argument struct */
1025 nfs_write_rpcsetup(req, data, count, 0, how);
1026
1027 nfs_execute_write(data);
1028 return 0;
1029 out_bad:
1030 while (!list_empty(head)) {
1031 struct nfs_page *req = nfs_list_entry(head->next);
1032 nfs_list_remove_request(req);
1033 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001034 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036 return -ENOMEM;
1037}
1038
1039static int
1040nfs_flush_list(struct list_head *head, int wpages, int how)
1041{
1042 LIST_HEAD(one_request);
1043 struct nfs_page *req;
1044 int error = 0;
1045 unsigned int pages = 0;
1046
1047 while (!list_empty(head)) {
1048 pages += nfs_coalesce_requests(head, &one_request, wpages);
1049 req = nfs_list_entry(one_request.next);
1050 error = nfs_flush_one(&one_request, req->wb_context->dentry->d_inode, how);
1051 if (error < 0)
1052 break;
1053 }
1054 if (error >= 0)
1055 return pages;
1056
1057 while (!list_empty(head)) {
1058 req = nfs_list_entry(head->next);
1059 nfs_list_remove_request(req);
1060 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001061 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063 return error;
1064}
1065
1066/*
1067 * Handle a write reply that flushed part of a page.
1068 */
1069static void nfs_writeback_done_partial(struct nfs_write_data *data, int status)
1070{
1071 struct nfs_page *req = data->req;
1072 struct page *page = req->wb_page;
1073
1074 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1075 req->wb_context->dentry->d_inode->i_sb->s_id,
1076 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1077 req->wb_bytes,
1078 (long long)req_offset(req));
1079
1080 if (status < 0) {
1081 ClearPageUptodate(page);
1082 SetPageError(page);
1083 req->wb_context->error = status;
1084 dprintk(", error = %d\n", status);
1085 } else {
1086#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1087 if (data->verf.committed < NFS_FILE_SYNC) {
1088 if (!NFS_NEED_COMMIT(req)) {
1089 nfs_defer_commit(req);
1090 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1091 dprintk(" defer commit\n");
1092 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1093 nfs_defer_reschedule(req);
1094 dprintk(" server reboot detected\n");
1095 }
1096 } else
1097#endif
1098 dprintk(" OK\n");
1099 }
1100
1101 if (atomic_dec_and_test(&req->wb_complete))
1102 nfs_writepage_release(req);
1103}
1104
1105/*
1106 * Handle a write reply that flushes a whole page.
1107 *
1108 * FIXME: There is an inherent race with invalidate_inode_pages and
1109 * writebacks since the page->count is kept > 1 for as long
1110 * as the page has a write request pending.
1111 */
1112static void nfs_writeback_done_full(struct nfs_write_data *data, int status)
1113{
1114 struct nfs_page *req;
1115 struct page *page;
1116
1117 /* Update attributes as result of writeback. */
1118 while (!list_empty(&data->pages)) {
1119 req = nfs_list_entry(data->pages.next);
1120 nfs_list_remove_request(req);
1121 page = req->wb_page;
1122
1123 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1124 req->wb_context->dentry->d_inode->i_sb->s_id,
1125 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1126 req->wb_bytes,
1127 (long long)req_offset(req));
1128
1129 if (status < 0) {
1130 ClearPageUptodate(page);
1131 SetPageError(page);
1132 req->wb_context->error = status;
1133 end_page_writeback(page);
1134 nfs_inode_remove_request(req);
1135 dprintk(", error = %d\n", status);
1136 goto next;
1137 }
1138 end_page_writeback(page);
1139
1140#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1141 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1142 nfs_inode_remove_request(req);
1143 dprintk(" OK\n");
1144 goto next;
1145 }
1146 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1147 nfs_mark_request_commit(req);
1148 dprintk(" marked for commit\n");
1149#else
1150 nfs_inode_remove_request(req);
1151#endif
1152 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001153 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
1155}
1156
1157/*
1158 * This function is called when the WRITE call is complete.
1159 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001160void nfs_writeback_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001162 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 struct nfs_writeargs *argp = &data->args;
1164 struct nfs_writeres *resp = &data->res;
1165
1166 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1167 task->tk_pid, task->tk_status);
1168
Chuck Lever91d5b472006-03-20 13:44:14 -05001169 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1172 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1173 /* We tried a write call, but the server did not
1174 * commit data to stable storage even though we
1175 * requested it.
1176 * Note: There is a known bug in Tru64 < 5.0 in which
1177 * the server reports NFS_DATA_SYNC, but performs
1178 * NFS_FILE_SYNC. We therefore implement this checking
1179 * as a dprintk() in order to avoid filling syslog.
1180 */
1181 static unsigned long complain;
1182
1183 if (time_before(complain, jiffies)) {
1184 dprintk("NFS: faulty NFS server %s:"
1185 " (committed = %d) != (stable = %d)\n",
1186 NFS_SERVER(data->inode)->hostname,
1187 resp->verf->committed, argp->stable);
1188 complain = jiffies + 300 * HZ;
1189 }
1190 }
1191#endif
1192 /* Is this a short write? */
1193 if (task->tk_status >= 0 && resp->count < argp->count) {
1194 static unsigned long complain;
1195
Chuck Lever91d5b472006-03-20 13:44:14 -05001196 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 /* Has the server at least made some progress? */
1199 if (resp->count != 0) {
1200 /* Was this an NFSv2 write or an NFSv3 stable write? */
1201 if (resp->verf->committed != NFS_UNSTABLE) {
1202 /* Resend from where the server left off */
1203 argp->offset += resp->count;
1204 argp->pgbase += resp->count;
1205 argp->count -= resp->count;
1206 } else {
1207 /* Resend as a stable write in order to avoid
1208 * headaches in the case of a server crash.
1209 */
1210 argp->stable = NFS_FILE_SYNC;
1211 }
1212 rpc_restart_call(task);
1213 return;
1214 }
1215 if (time_before(complain, jiffies)) {
1216 printk(KERN_WARNING
1217 "NFS: Server wrote zero bytes, expected %u.\n",
1218 argp->count);
1219 complain = jiffies + 300 * HZ;
1220 }
1221 /* Can't do anything about it except throw an error. */
1222 task->tk_status = -EIO;
1223 }
1224
1225 /*
1226 * Process the nfs_page list
1227 */
1228 data->complete(data, task->tk_status);
1229}
1230
1231
1232#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001233void nfs_commit_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 nfs_commit_free(wdata);
1236}
1237
1238/*
1239 * Set up the argument/result storage required for the RPC call.
1240 */
1241static void nfs_commit_rpcsetup(struct list_head *head,
1242 struct nfs_write_data *data, int how)
1243{
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001244 struct nfs_page *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 /* Set up the RPC argument and reply structs
1248 * NB: take care not to mess about with data->commit et al. */
1249
1250 list_splice_init(head, &data->pages);
1251 first = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 inode = first->wb_context->dentry->d_inode;
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 data->inode = inode;
1255 data->cred = first->wb_context->cred;
1256
1257 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001258 /* Note: we always request a commit of the entire inode */
1259 data->args.offset = 0;
1260 data->args.count = 0;
1261 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 data->res.fattr = &data->fattr;
1263 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001264 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 NFS_PROTO(inode)->commit_setup(data, how);
1267
1268 data->task.tk_priority = flush_task_priority(how);
1269 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Chuck Lever0bbacc42005-11-01 16:53:32 -05001271 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
1274/*
1275 * Commit dirty pages
1276 */
1277static int
Chuck Lever40859d72005-11-30 18:09:02 -05001278nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 struct nfs_write_data *data;
1281 struct nfs_page *req;
1282
Chuck Lever40859d72005-11-30 18:09:02 -05001283 data = nfs_commit_alloc(NFS_SERVER(inode)->wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 if (!data)
1286 goto out_bad;
1287
1288 /* Set up the argument struct */
1289 nfs_commit_rpcsetup(head, data, how);
1290
1291 nfs_execute_write(data);
1292 return 0;
1293 out_bad:
1294 while (!list_empty(head)) {
1295 req = nfs_list_entry(head->next);
1296 nfs_list_remove_request(req);
1297 nfs_mark_request_commit(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001298 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
1300 return -ENOMEM;
1301}
1302
1303/*
1304 * COMMIT call returned
1305 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001306void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001308 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 struct nfs_page *req;
1310 int res = 0;
1311
1312 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1313 task->tk_pid, task->tk_status);
1314
1315 while (!list_empty(&data->pages)) {
1316 req = nfs_list_entry(data->pages.next);
1317 nfs_list_remove_request(req);
1318
1319 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1320 req->wb_context->dentry->d_inode->i_sb->s_id,
1321 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1322 req->wb_bytes,
1323 (long long)req_offset(req));
1324 if (task->tk_status < 0) {
1325 req->wb_context->error = task->tk_status;
1326 nfs_inode_remove_request(req);
1327 dprintk(", error = %d\n", task->tk_status);
1328 goto next;
1329 }
1330
1331 /* Okay, COMMIT succeeded, apparently. Check the verifier
1332 * returned by the server against all stored verfs. */
1333 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1334 /* We have a match */
1335 nfs_inode_remove_request(req);
1336 dprintk(" OK\n");
1337 goto next;
1338 }
1339 /* We have a mismatch. Write the page again */
1340 dprintk(" mismatch\n");
1341 nfs_mark_request_dirty(req);
1342 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001343 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 res++;
1345 }
1346 sub_page_state(nr_unstable,res);
1347}
1348#endif
1349
1350static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
1351 unsigned int npages, int how)
1352{
1353 struct nfs_inode *nfsi = NFS_I(inode);
1354 LIST_HEAD(head);
1355 int res,
1356 error = 0;
1357
1358 spin_lock(&nfsi->req_lock);
1359 res = nfs_scan_dirty(inode, &head, idx_start, npages);
1360 spin_unlock(&nfsi->req_lock);
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001361 if (res) {
1362 struct nfs_server *server = NFS_SERVER(inode);
1363
1364 /* For single writes, FLUSH_STABLE is more efficient */
1365 if (res == nfsi->npages && nfsi->npages <= server->wpages) {
1366 if (res > 1 || nfs_list_entry(head.next)->wb_bytes <= server->wsize)
1367 how |= FLUSH_STABLE;
1368 }
1369 error = nfs_flush_list(&head, server->wpages, how);
1370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (error < 0)
1372 return error;
1373 return res;
1374}
1375
1376#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001377int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 struct nfs_inode *nfsi = NFS_I(inode);
1380 LIST_HEAD(head);
1381 int res,
1382 error = 0;
1383
1384 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001385 res = nfs_scan_commit(inode, &head, 0, 0);
1386 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 if (res) {
Chuck Lever40859d72005-11-30 18:09:02 -05001388 error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001389 if (error < 0)
1390 return error;
1391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return res;
1393}
1394#endif
1395
1396int nfs_sync_inode(struct inode *inode, unsigned long idx_start,
1397 unsigned int npages, int how)
1398{
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001399 int nocommit = how & FLUSH_NOCOMMIT;
1400 int wait = how & FLUSH_WAIT;
1401 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001403 how &= ~(FLUSH_WAIT|FLUSH_NOCOMMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 do {
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001406 if (wait) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 error = nfs_wait_on_requests(inode, idx_start, npages);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001408 if (error != 0)
1409 continue;
1410 }
1411 error = nfs_flush_inode(inode, idx_start, npages, how);
1412 if (error != 0)
1413 continue;
1414 if (!nocommit)
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001415 error = nfs_commit_inode(inode, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 } while (error > 0);
1417 return error;
1418}
1419
1420int nfs_init_writepagecache(void)
1421{
1422 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1423 sizeof(struct nfs_write_data),
1424 0, SLAB_HWCACHE_ALIGN,
1425 NULL, NULL);
1426 if (nfs_wdata_cachep == NULL)
1427 return -ENOMEM;
1428
1429 nfs_wdata_mempool = mempool_create(MIN_POOL_WRITE,
1430 mempool_alloc_slab,
1431 mempool_free_slab,
1432 nfs_wdata_cachep);
1433 if (nfs_wdata_mempool == NULL)
1434 return -ENOMEM;
1435
1436 nfs_commit_mempool = mempool_create(MIN_POOL_COMMIT,
1437 mempool_alloc_slab,
1438 mempool_free_slab,
1439 nfs_wdata_cachep);
1440 if (nfs_commit_mempool == NULL)
1441 return -ENOMEM;
1442
1443 return 0;
1444}
1445
1446void nfs_destroy_writepagecache(void)
1447{
1448 mempool_destroy(nfs_commit_mempool);
1449 mempool_destroy(nfs_wdata_mempool);
1450 if (kmem_cache_destroy(nfs_wdata_cachep))
1451 printk(KERN_INFO "nfs_write_data: not all structures were freed\n");
1452}
1453