blob: 3f6ca52295629b8e5eeebdf757e2fb69da205a5c [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/types.h>
50#include <linux/slab.h>
51#include <linux/mm.h>
52#include <linux/pagemap.h>
53#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/writeback.h>
55
56#include <linux/sunrpc/clnt.h>
57#include <linux/nfs_fs.h>
58#include <linux/nfs_mount.h>
59#include <linux/nfs_page.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070060#include <linux/backing-dev.h>
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <asm/uaccess.h>
63#include <linux/smp_lock.h>
64
65#include "delegation.h"
Trond Myklebust49a70f22006-12-05 00:35:38 -050066#include "internal.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050067#include "iostat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define NFSDBG_FACILITY NFSDBG_PAGECACHE
70
71#define MIN_POOL_WRITE (32)
72#define MIN_POOL_COMMIT (4)
73
74/*
75 * Local function declarations
76 */
77static struct nfs_page * nfs_update_request(struct nfs_open_context*,
78 struct inode *,
79 struct page *,
80 unsigned int, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static int nfs_wait_on_write_congestion(struct address_space *, int);
82static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int);
Trond Myklebust3f442542006-09-17 14:46:44 -040083static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how);
Trond Myklebust1c759502006-10-09 16:18:38 -040084static int nfs_wb_page_priority(struct inode *inode, struct page *page, int how);
Trond Myklebust788e7a82006-03-20 13:44:27 -050085static const struct rpc_call_ops nfs_write_partial_ops;
86static const struct rpc_call_ops nfs_write_full_ops;
87static const struct rpc_call_ops nfs_commit_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89static kmem_cache_t *nfs_wdata_cachep;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050090static mempool_t *nfs_wdata_mempool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static mempool_t *nfs_commit_mempool;
92
93static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
94
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070095struct nfs_write_data *nfs_commit_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS);
Chuck Lever40859d72005-11-30 18:09:02 -050098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (p) {
100 memset(p, 0, sizeof(*p));
101 INIT_LIST_HEAD(&p->pages);
102 }
103 return p;
104}
105
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500106void nfs_commit_rcu_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500108 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
Chuck Lever40859d72005-11-30 18:09:02 -0500109 if (p && (p->pagevec != &p->page_array[0]))
110 kfree(p->pagevec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 mempool_free(p, nfs_commit_mempool);
112}
113
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500114void nfs_commit_free(struct nfs_write_data *wdata)
115{
116 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
117}
118
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700119struct nfs_write_data *nfs_writedata_alloc(size_t len)
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500120{
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700121 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500122 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
123
124 if (p) {
125 memset(p, 0, sizeof(*p));
126 INIT_LIST_HEAD(&p->pages);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700127 p->npages = pagecount;
Chuck Lever0d0b5cb2006-05-25 01:40:53 -0400128 if (pagecount <= ARRAY_SIZE(p->page_array))
129 p->pagevec = p->page_array;
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500130 else {
Chuck Lever0d0b5cb2006-05-25 01:40:53 -0400131 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
132 if (!p->pagevec) {
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500133 mempool_free(p, nfs_wdata_mempool);
134 p = NULL;
135 }
136 }
137 }
138 return p;
139}
140
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500141static void nfs_writedata_rcu_free(struct rcu_head *head)
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500142{
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500143 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500144 if (p && (p->pagevec != &p->page_array[0]))
145 kfree(p->pagevec);
146 mempool_free(p, nfs_wdata_mempool);
147}
148
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500149static void nfs_writedata_free(struct nfs_write_data *wdata)
150{
151 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
152}
153
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100154void nfs_writedata_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 nfs_writedata_free(wdata);
157}
158
Trond Myklebust277459d2006-12-05 00:35:35 -0500159static struct nfs_page *nfs_page_find_request_locked(struct page *page)
160{
161 struct nfs_page *req = NULL;
162
163 if (PagePrivate(page)) {
164 req = (struct nfs_page *)page_private(page);
165 if (req != NULL)
166 atomic_inc(&req->wb_count);
167 }
168 return req;
169}
170
171static struct nfs_page *nfs_page_find_request(struct page *page)
172{
173 struct nfs_page *req = NULL;
174 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
175
176 spin_lock(req_lock);
177 req = nfs_page_find_request_locked(page);
178 spin_unlock(req_lock);
179 return req;
180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/* Adjust the file length if we're writing beyond the end */
183static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
184{
185 struct inode *inode = page->mapping->host;
186 loff_t end, i_size = i_size_read(inode);
187 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
188
189 if (i_size > 0 && page->index < end_index)
190 return;
191 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
192 if (i_size >= end)
193 return;
Chuck Lever91d5b472006-03-20 13:44:14 -0500194 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 i_size_write(inode, end);
196}
197
198/* We can set the PG_uptodate flag if we see that a write request
199 * covers the full page.
200 */
201static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
202{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (PageUptodate(page))
204 return;
205 if (base != 0)
206 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500207 if (count != nfs_page_length(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500209 if (count != PAGE_CACHE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
Trond Myklebust49a70f22006-12-05 00:35:38 -0500211 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214/*
215 * Write a page synchronously.
216 * Offset is the data offset within the page.
217 */
218static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
219 struct page *page, unsigned int offset, unsigned int count,
220 int how)
221{
222 unsigned int wsize = NFS_SERVER(inode)->wsize;
223 int result, written = 0;
224 struct nfs_write_data *wdata;
225
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700226 wdata = nfs_writedata_alloc(wsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (!wdata)
228 return -ENOMEM;
229
230 wdata->flags = how;
231 wdata->cred = ctx->cred;
232 wdata->inode = inode;
233 wdata->args.fh = NFS_FH(inode);
234 wdata->args.context = ctx;
235 wdata->args.pages = &page;
236 wdata->args.stable = NFS_FILE_SYNC;
237 wdata->args.pgbase = offset;
238 wdata->args.count = wsize;
239 wdata->res.fattr = &wdata->fattr;
240 wdata->res.verf = &wdata->verf;
241
242 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
243 inode->i_sb->s_id,
244 (long long)NFS_FILEID(inode),
245 count, (long long)(page_offset(page) + offset));
246
Trond Myklebustbb713d62005-12-03 15:20:14 -0500247 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 nfs_begin_data_update(inode);
249 do {
250 if (count < wsize)
251 wdata->args.count = count;
252 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
253
254 result = NFS_PROTO(inode)->write(wdata);
255
256 if (result < 0) {
257 /* Must mark the page invalid after I/O error */
258 ClearPageUptodate(page);
259 goto io_error;
260 }
261 if (result < wdata->args.count)
262 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
263 wdata->args.count, result);
264
265 wdata->args.offset += result;
266 wdata->args.pgbase += result;
267 written += result;
268 count -= result;
Chuck Lever91d5b472006-03-20 13:44:14 -0500269 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 } while (count);
271 /* Update file length */
272 nfs_grow_file(page, offset, written);
273 /* Set the PG_uptodate flag? */
274 nfs_mark_uptodate(page, offset, written);
275
276 if (PageError(page))
277 ClearPageError(page);
278
279io_error:
Trond Myklebust951a1432005-06-22 17:16:30 +0000280 nfs_end_data_update(inode);
Trond Myklebustbb713d62005-12-03 15:20:14 -0500281 end_page_writeback(page);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500282 nfs_writedata_release(wdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return written ? written : result;
284}
285
286static int nfs_writepage_async(struct nfs_open_context *ctx,
287 struct inode *inode, struct page *page,
288 unsigned int offset, unsigned int count)
289{
290 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 req = nfs_update_request(ctx, inode, page, offset, count);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100293 if (IS_ERR(req))
294 return PTR_ERR(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 /* Update file length */
296 nfs_grow_file(page, offset, count);
297 /* Set the PG_uptodate flag? */
298 nfs_mark_uptodate(page, offset, count);
299 nfs_unlock_request(req);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100300 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
303static int wb_priority(struct writeback_control *wbc)
304{
305 if (wbc->for_reclaim)
306 return FLUSH_HIGHPRI;
307 if (wbc->for_kupdate)
308 return FLUSH_LOWPRI;
309 return 0;
310}
311
312/*
313 * Write an mmapped page to the server.
314 */
315int nfs_writepage(struct page *page, struct writeback_control *wbc)
316{
317 struct nfs_open_context *ctx;
318 struct inode *inode = page->mapping->host;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500319 unsigned offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int inode_referenced = 0;
321 int priority = wb_priority(wbc);
322 int err;
323
Chuck Lever91d5b472006-03-20 13:44:14 -0500324 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
325 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 /*
328 * Note: We need to ensure that we have a reference to the inode
329 * if we are to do asynchronous writes. If not, waiting
330 * in nfs_wait_on_request() may deadlock with clear_inode().
331 *
332 * If igrab() fails here, then it is in any case safe to
333 * call nfs_wb_page(), since there will be no pending writes.
334 */
335 if (igrab(inode) != 0)
336 inode_referenced = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 /* Ensure we've flushed out any previous writes */
339 nfs_wb_page_priority(inode, page, priority);
340
Trond Myklebust49a70f22006-12-05 00:35:38 -0500341 err = 0;
342 offset = nfs_page_length(page);
343 if (!offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 goto out;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500345
Trond Myklebustd5308382005-11-04 15:33:38 -0500346 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (ctx == NULL) {
348 err = -EBADF;
349 goto out;
350 }
351 lock_kernel();
352 if (!IS_SYNC(inode) && inode_referenced) {
353 err = nfs_writepage_async(ctx, inode, page, 0, offset);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100354 if (!wbc->for_writepages)
Trond Myklebust28c69252006-09-16 13:04:50 -0400355 nfs_flush_mapping(page->mapping, wbc, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 } else {
357 err = nfs_writepage_sync(ctx, inode, page, 0,
358 offset, priority);
359 if (err >= 0) {
360 if (err != offset)
361 redirty_page_for_writepage(wbc, page);
362 err = 0;
363 }
364 }
365 unlock_kernel();
366 put_nfs_open_context(ctx);
367out:
368 unlock_page(page);
369 if (inode_referenced)
370 iput(inode);
371 return err;
372}
373
374/*
375 * Note: causes nfs_update_request() to block on the assumption
376 * that the writeback is generated due to memory pressure.
377 */
378int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
379{
380 struct backing_dev_info *bdi = mapping->backing_dev_info;
381 struct inode *inode = mapping->host;
382 int err;
383
Chuck Lever91d5b472006-03-20 13:44:14 -0500384 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 err = generic_writepages(mapping, wbc);
387 if (err)
388 return err;
389 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
390 if (wbc->nonblocking)
391 return 0;
392 nfs_wait_on_write_congestion(mapping, 0);
393 }
Trond Myklebust28c69252006-09-16 13:04:50 -0400394 err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (err < 0)
396 goto out;
Chuck Lever91d5b472006-03-20 13:44:14 -0500397 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 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));
Trond Myklebust3f442542006-09-17 14:46:44 -0400404 if (err > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406out:
407 clear_bit(BDI_write_congested, &bdi->state);
408 wake_up_all(&nfs_write_congestion);
Andrew Morton3fcfab12006-10-19 23:28:16 -0700409 congestion_end(WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return err;
411}
412
413/*
414 * Insert a write request into an inode
415 */
416static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
417{
418 struct nfs_inode *nfsi = NFS_I(inode);
419 int error;
420
421 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
422 BUG_ON(error == -EEXIST);
423 if (error)
424 return error;
425 if (!nfsi->npages) {
426 igrab(inode);
427 nfs_begin_data_update(inode);
428 if (nfs_have_delegation(inode, FMODE_WRITE))
429 nfsi->change_attr++;
430 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500431 SetPagePrivate(req->wb_page);
Trond Myklebust277459d2006-12-05 00:35:35 -0500432 set_page_private(req->wb_page, (unsigned long)req);
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 Myklebust277459d2006-12-05 00:35:35 -0500449 set_page_private(req->wb_page, 0);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500450 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
452 nfsi->npages--;
453 if (!nfsi->npages) {
454 spin_unlock(&nfsi->req_lock);
Trond Myklebust951a1432005-06-22 17:16:30 +0000455 nfs_end_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 iput(inode);
457 } else
458 spin_unlock(&nfsi->req_lock);
459 nfs_clear_request(req);
460 nfs_release_request(req);
461}
462
463/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 * Add a request to the inode's dirty list.
465 */
466static void
467nfs_mark_request_dirty(struct nfs_page *req)
468{
469 struct inode *inode = req->wb_context->dentry->d_inode;
470 struct nfs_inode *nfsi = NFS_I(inode);
471
472 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000473 radix_tree_tag_set(&nfsi->nfs_page_tree,
474 req->wb_index, NFS_PAGE_TAG_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 nfs_list_add_request(req, &nfsi->dirty);
476 nfsi->ndirty++;
477 spin_unlock(&nfsi->req_lock);
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700478 inc_zone_page_state(req->wb_page, NR_FILE_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 mark_inode_dirty(inode);
480}
481
482/*
483 * Check if a request is dirty
484 */
485static inline int
486nfs_dirty_request(struct nfs_page *req)
487{
488 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
489 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
490}
491
492#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
493/*
494 * Add a request to the inode's commit list.
495 */
496static void
497nfs_mark_request_commit(struct nfs_page *req)
498{
499 struct inode *inode = req->wb_context->dentry->d_inode;
500 struct nfs_inode *nfsi = NFS_I(inode);
501
502 spin_lock(&nfsi->req_lock);
503 nfs_list_add_request(req, &nfsi->commit);
504 nfsi->ncommit++;
505 spin_unlock(&nfsi->req_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700506 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 mark_inode_dirty(inode);
508}
509#endif
510
511/*
512 * Wait for a request to complete.
513 *
514 * Interruptible by signals only if mounted with intr flag.
515 */
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500516static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 struct nfs_inode *nfsi = NFS_I(inode);
519 struct nfs_page *req;
520 unsigned long idx_end, next;
521 unsigned int res = 0;
522 int error;
523
524 if (npages == 0)
525 idx_end = ~0;
526 else
527 idx_end = idx_start + npages - 1;
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 next = idx_start;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000530 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 -0700531 if (req->wb_index > idx_end)
532 break;
533
534 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000535 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 atomic_inc(&req->wb_count);
538 spin_unlock(&nfsi->req_lock);
539 error = nfs_wait_on_request(req);
540 nfs_release_request(req);
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500541 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (error < 0)
543 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 res++;
545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return res;
547}
548
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500549static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
550{
551 struct nfs_inode *nfsi = NFS_I(inode);
552 int ret;
553
554 spin_lock(&nfsi->req_lock);
555 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
556 spin_unlock(&nfsi->req_lock);
557 return ret;
558}
559
Trond Myklebust83715ad2006-07-05 13:17:12 -0400560static void nfs_cancel_dirty_list(struct list_head *head)
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400561{
562 struct nfs_page *req;
563 while(!list_empty(head)) {
564 req = nfs_list_entry(head->next);
565 nfs_list_remove_request(req);
566 nfs_inode_remove_request(req);
567 nfs_clear_page_writeback(req);
568 }
569}
570
Trond Myklebust83715ad2006-07-05 13:17:12 -0400571static void nfs_cancel_commit_list(struct list_head *head)
572{
573 struct nfs_page *req;
574
575 while(!list_empty(head)) {
576 req = nfs_list_entry(head->next);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700577 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400578 nfs_list_remove_request(req);
579 nfs_inode_remove_request(req);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700580 nfs_unlock_request(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400581 }
582}
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
585/*
586 * nfs_scan_commit - Scan an inode for commit requests
587 * @inode: NFS inode to scan
588 * @dst: destination list
589 * @idx_start: lower bound of page->index to scan.
590 * @npages: idx_start + npages sets the upper bound to scan.
591 *
592 * Moves requests from the inode's 'commit' request list.
593 * The requests are *not* checked to ensure that they form a contiguous set.
594 */
595static int
596nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
597{
598 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000599 int res = 0;
600
601 if (nfsi->ncommit != 0) {
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400602 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000603 nfsi->ncommit -= res;
604 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
605 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return res;
608}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500609#else
610static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
611{
612 return 0;
613}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614#endif
615
616static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
617{
618 struct backing_dev_info *bdi = mapping->backing_dev_info;
619 DEFINE_WAIT(wait);
620 int ret = 0;
621
622 might_sleep();
623
624 if (!bdi_write_congested(bdi))
625 return 0;
Chuck Lever91d5b472006-03-20 13:44:14 -0500626
627 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (intr) {
630 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
631 sigset_t oldset;
632
633 rpc_clnt_sigmask(clnt, &oldset);
634 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
635 if (bdi_write_congested(bdi)) {
636 if (signalled())
637 ret = -ERESTARTSYS;
638 else
639 schedule();
640 }
641 rpc_clnt_sigunmask(clnt, &oldset);
642 } else {
643 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
644 if (bdi_write_congested(bdi))
645 schedule();
646 }
647 finish_wait(&nfs_write_congestion, &wait);
648 return ret;
649}
650
651
652/*
653 * Try to update any existing write request, or create one if there is none.
654 * In order to match, the request's credentials must match those of
655 * the calling process.
656 *
657 * Note: Should always be called with the Page Lock held!
658 */
659static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
660 struct inode *inode, struct page *page,
661 unsigned int offset, unsigned int bytes)
662{
663 struct nfs_server *server = NFS_SERVER(inode);
664 struct nfs_inode *nfsi = NFS_I(inode);
665 struct nfs_page *req, *new = NULL;
666 unsigned long rqend, end;
667
668 end = offset + bytes;
669
670 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
671 return ERR_PTR(-ERESTARTSYS);
672 for (;;) {
673 /* Loop over all inode entries and see if we find
674 * A request for the page we wish to update
675 */
676 spin_lock(&nfsi->req_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500677 req = nfs_page_find_request_locked(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (req) {
679 if (!nfs_lock_request_dontget(req)) {
680 int error;
Trond Myklebust277459d2006-12-05 00:35:35 -0500681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 spin_unlock(&nfsi->req_lock);
683 error = nfs_wait_on_request(req);
684 nfs_release_request(req);
Neil Brown1dd594b2006-03-20 13:44:04 -0500685 if (error < 0) {
686 if (new)
687 nfs_release_request(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return ERR_PTR(error);
Neil Brown1dd594b2006-03-20 13:44:04 -0500689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 continue;
691 }
692 spin_unlock(&nfsi->req_lock);
693 if (new)
694 nfs_release_request(new);
695 break;
696 }
697
698 if (new) {
699 int error;
700 nfs_lock_request_dontget(new);
701 error = nfs_inode_add_request(inode, new);
702 if (error) {
703 spin_unlock(&nfsi->req_lock);
704 nfs_unlock_request(new);
705 return ERR_PTR(error);
706 }
707 spin_unlock(&nfsi->req_lock);
708 nfs_mark_request_dirty(new);
709 return new;
710 }
711 spin_unlock(&nfsi->req_lock);
712
713 new = nfs_create_request(ctx, inode, page, offset, bytes);
714 if (IS_ERR(new))
715 return new;
716 }
717
718 /* We have a request for our page.
719 * If the creds don't match, or the
720 * page addresses don't match,
721 * tell the caller to wait on the conflicting
722 * request.
723 */
724 rqend = req->wb_offset + req->wb_bytes;
725 if (req->wb_context != ctx
726 || req->wb_page != page
727 || !nfs_dirty_request(req)
728 || offset > rqend || end < req->wb_offset) {
729 nfs_unlock_request(req);
730 return ERR_PTR(-EBUSY);
731 }
732
733 /* Okay, the request matches. Update the region */
734 if (offset < req->wb_offset) {
735 req->wb_offset = offset;
736 req->wb_pgbase = offset;
737 req->wb_bytes = rqend - req->wb_offset;
738 }
739
740 if (end > rqend)
741 req->wb_bytes = end - req->wb_offset;
742
743 return req;
744}
745
746int nfs_flush_incompatible(struct file *file, struct page *page)
747{
748 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 struct nfs_page *req;
750 int status = 0;
751 /*
752 * Look for a request corresponding to this page. If there
753 * is one, and it belongs to another file, we flush it out
754 * before we try to copy anything into the page. Do this
755 * due to the lack of an ACCESS-type call in NFSv2.
756 * Also do the same if we find a request from an existing
757 * dropped page.
758 */
Trond Myklebust277459d2006-12-05 00:35:35 -0500759 req = nfs_page_find_request(page);
760 if (req != NULL) {
761 int do_flush = req->wb_page != page || req->wb_context != ctx;
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 nfs_release_request(req);
Trond Myklebust277459d2006-12-05 00:35:35 -0500764 if (do_flush)
765 status = nfs_wb_page(page->mapping->host, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767 return (status < 0) ? status : 0;
768}
769
770/*
771 * Update and possibly write a cached page of an NFS file.
772 *
773 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
774 * things with a page scheduled for an RPC call (e.g. invalidate it).
775 */
776int nfs_updatepage(struct file *file, struct page *page,
777 unsigned int offset, unsigned int count)
778{
779 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 struct inode *inode = page->mapping->host;
781 struct nfs_page *req;
782 int status = 0;
783
Chuck Lever91d5b472006-03-20 13:44:14 -0500784 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500787 file->f_dentry->d_parent->d_name.name,
788 file->f_dentry->d_name.name, count,
789 (long long)(page_offset(page) +offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 if (IS_SYNC(inode)) {
792 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
793 if (status > 0) {
794 if (offset == 0 && status == PAGE_CACHE_SIZE)
795 SetPageUptodate(page);
796 return 0;
797 }
798 return status;
799 }
800
801 /* If we're not using byte range locks, and we know the page
802 * is entirely in cache, it may be more efficient to avoid
803 * fragmenting write requests.
804 */
Trond Myklebustab0a3db2005-06-22 17:16:30 +0000805 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
Trond Myklebust49a70f22006-12-05 00:35:38 -0500806 count = max(count + offset, nfs_page_length(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809
810 /*
811 * Try to find an NFS request corresponding to this page
812 * and update it.
813 * If the existing request cannot be updated, we must flush
814 * it out now.
815 */
816 do {
817 req = nfs_update_request(ctx, inode, page, offset, count);
818 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
819 if (status != -EBUSY)
820 break;
821 /* Request could not be updated. Flush it out and try again */
822 status = nfs_wb_page(inode, page);
823 } while (status >= 0);
824 if (status < 0)
825 goto done;
826
827 status = 0;
828
829 /* Update file length */
830 nfs_grow_file(page, offset, count);
831 /* Set the PG_uptodate flag? */
832 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
833 nfs_unlock_request(req);
834done:
835 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
836 status, (long long)i_size_read(inode));
837 if (status < 0)
838 ClearPageUptodate(page);
839 return status;
840}
841
842static void nfs_writepage_release(struct nfs_page *req)
843{
844 end_page_writeback(req->wb_page);
845
846#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
847 if (!PageError(req->wb_page)) {
848 if (NFS_NEED_RESCHED(req)) {
849 nfs_mark_request_dirty(req);
850 goto out;
851 } else if (NFS_NEED_COMMIT(req)) {
852 nfs_mark_request_commit(req);
853 goto out;
854 }
855 }
856 nfs_inode_remove_request(req);
857
858out:
859 nfs_clear_commit(req);
860 nfs_clear_reschedule(req);
861#else
862 nfs_inode_remove_request(req);
863#endif
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000864 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
867static inline int flush_task_priority(int how)
868{
869 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
870 case FLUSH_HIGHPRI:
871 return RPC_PRIORITY_HIGH;
872 case FLUSH_LOWPRI:
873 return RPC_PRIORITY_LOW;
874 }
875 return RPC_PRIORITY_NORMAL;
876}
877
878/*
879 * Set up the argument/result storage required for the RPC call.
880 */
881static void nfs_write_rpcsetup(struct nfs_page *req,
882 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500883 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 unsigned int count, unsigned int offset,
885 int how)
886{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -0500888 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 /* Set up the RPC argument and reply structs
891 * NB: take care not to mess about with data->commit et al. */
892
893 data->req = req;
894 data->inode = inode = req->wb_context->dentry->d_inode;
895 data->cred = req->wb_context->cred;
896
897 data->args.fh = NFS_FH(inode);
898 data->args.offset = req_offset(req) + offset;
899 data->args.pgbase = req->wb_pgbase + offset;
900 data->args.pages = data->pagevec;
901 data->args.count = count;
902 data->args.context = req->wb_context;
903
904 data->res.fattr = &data->fattr;
905 data->res.count = count;
906 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400907 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Trond Myklebust788e7a82006-03-20 13:44:27 -0500909 /* Set up the initial task struct. */
910 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
911 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 NFS_PROTO(inode)->write_setup(data, how);
913
914 data->task.tk_priority = flush_task_priority(how);
915 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500918 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 inode->i_sb->s_id,
920 (long long)NFS_FILEID(inode),
921 count,
922 (unsigned long long)data->args.offset);
923}
924
925static void nfs_execute_write(struct nfs_write_data *data)
926{
927 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
928 sigset_t oldset;
929
930 rpc_clnt_sigmask(clnt, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 rpc_execute(&data->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 rpc_clnt_sigunmask(clnt, &oldset);
933}
934
935/*
936 * Generate multiple small requests to write out a single
937 * contiguous dirty area on one page.
938 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500939static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
941 struct nfs_page *req = nfs_list_entry(head->next);
942 struct page *page = req->wb_page;
943 struct nfs_write_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700944 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
945 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 int requests = 0;
947 LIST_HEAD(list);
948
949 nfs_list_remove_request(req);
950
951 nbytes = req->wb_bytes;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700952 do {
953 size_t len = min(nbytes, wsize);
954
955 data = nfs_writedata_alloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (!data)
957 goto out_bad;
958 list_add(&data->pages, &list);
959 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700960 nbytes -= len;
961 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 atomic_set(&req->wb_complete, requests);
963
964 ClearPageError(page);
Trond Myklebustbb713d62005-12-03 15:20:14 -0500965 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 offset = 0;
967 nbytes = req->wb_bytes;
968 do {
969 data = list_entry(list.next, struct nfs_write_data, pages);
970 list_del_init(&data->pages);
971
972 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 if (nbytes > wsize) {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500975 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
976 wsize, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 offset += wsize;
978 nbytes -= wsize;
979 } else {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500980 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
981 nbytes, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 nbytes = 0;
983 }
984 nfs_execute_write(data);
985 } while (nbytes != 0);
986
987 return 0;
988
989out_bad:
990 while (!list_empty(&list)) {
991 data = list_entry(list.next, struct nfs_write_data, pages);
992 list_del(&data->pages);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500993 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
995 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000996 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return -ENOMEM;
998}
999
1000/*
1001 * Create an RPC task for the given write request and kick it.
1002 * The page must have been locked by the caller.
1003 *
1004 * It may happen that the page we're passed is not marked dirty.
1005 * This is the case if nfs_updatepage detects a conflicting request
1006 * that has been written but not committed.
1007 */
Trond Myklebust7d46a492006-03-20 13:44:50 -05001008static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 struct nfs_page *req;
1011 struct page **pages;
1012 struct nfs_write_data *data;
1013 unsigned int count;
1014
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001015 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (!data)
1017 goto out_bad;
1018
1019 pages = data->pagevec;
1020 count = 0;
1021 while (!list_empty(head)) {
1022 req = nfs_list_entry(head->next);
1023 nfs_list_remove_request(req);
1024 nfs_list_add_request(req, &data->pages);
1025 ClearPageError(req->wb_page);
Trond Myklebustbb713d62005-12-03 15:20:14 -05001026 set_page_writeback(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 *pages++ = req->wb_page;
1028 count += req->wb_bytes;
1029 }
1030 req = nfs_list_entry(data->pages.next);
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 /* Set up the argument struct */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001033 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 nfs_execute_write(data);
1036 return 0;
1037 out_bad:
1038 while (!list_empty(head)) {
1039 struct nfs_page *req = nfs_list_entry(head->next);
1040 nfs_list_remove_request(req);
1041 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001042 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
1044 return -ENOMEM;
1045}
1046
Trond Myklebust7d46a492006-03-20 13:44:50 -05001047static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
1049 LIST_HEAD(one_request);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001050 int (*flush_one)(struct inode *, struct list_head *, int);
1051 struct nfs_page *req;
1052 int wpages = NFS_SERVER(inode)->wpages;
1053 int wsize = NFS_SERVER(inode)->wsize;
1054 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Trond Myklebust7d46a492006-03-20 13:44:50 -05001056 flush_one = nfs_flush_one;
1057 if (wsize < PAGE_CACHE_SIZE)
1058 flush_one = nfs_flush_multi;
1059 /* For single writes, FLUSH_STABLE is more efficient */
1060 if (npages <= wpages && npages == NFS_I(inode)->npages
1061 && nfs_list_entry(head->next)->wb_bytes <= wsize)
1062 how |= FLUSH_STABLE;
1063
1064 do {
1065 nfs_coalesce_requests(head, &one_request, wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 req = nfs_list_entry(one_request.next);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001067 error = flush_one(inode, &one_request, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (error < 0)
Trond Myklebust7d46a492006-03-20 13:44:50 -05001069 goto out_err;
1070 } while (!list_empty(head));
1071 return 0;
1072out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 while (!list_empty(head)) {
1074 req = nfs_list_entry(head->next);
1075 nfs_list_remove_request(req);
1076 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001077 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
1079 return error;
1080}
1081
1082/*
1083 * Handle a write reply that flushed part of a page.
1084 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001085static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001087 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 struct nfs_page *req = data->req;
1089 struct page *page = req->wb_page;
1090
1091 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1092 req->wb_context->dentry->d_inode->i_sb->s_id,
1093 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1094 req->wb_bytes,
1095 (long long)req_offset(req));
1096
Trond Myklebust788e7a82006-03-20 13:44:27 -05001097 if (nfs_writeback_done(task, data) != 0)
1098 return;
1099
1100 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 ClearPageUptodate(page);
1102 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001103 req->wb_context->error = task->tk_status;
1104 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 } else {
1106#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1107 if (data->verf.committed < NFS_FILE_SYNC) {
1108 if (!NFS_NEED_COMMIT(req)) {
1109 nfs_defer_commit(req);
1110 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1111 dprintk(" defer commit\n");
1112 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1113 nfs_defer_reschedule(req);
1114 dprintk(" server reboot detected\n");
1115 }
1116 } else
1117#endif
1118 dprintk(" OK\n");
1119 }
1120
1121 if (atomic_dec_and_test(&req->wb_complete))
1122 nfs_writepage_release(req);
1123}
1124
Trond Myklebust788e7a82006-03-20 13:44:27 -05001125static const struct rpc_call_ops nfs_write_partial_ops = {
1126 .rpc_call_done = nfs_writeback_done_partial,
1127 .rpc_release = nfs_writedata_release,
1128};
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130/*
1131 * Handle a write reply that flushes a whole page.
1132 *
1133 * FIXME: There is an inherent race with invalidate_inode_pages and
1134 * writebacks since the page->count is kept > 1 for as long
1135 * as the page has a write request pending.
1136 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001137static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001139 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 struct nfs_page *req;
1141 struct page *page;
1142
Trond Myklebust788e7a82006-03-20 13:44:27 -05001143 if (nfs_writeback_done(task, data) != 0)
1144 return;
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 /* Update attributes as result of writeback. */
1147 while (!list_empty(&data->pages)) {
1148 req = nfs_list_entry(data->pages.next);
1149 nfs_list_remove_request(req);
1150 page = req->wb_page;
1151
1152 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1153 req->wb_context->dentry->d_inode->i_sb->s_id,
1154 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1155 req->wb_bytes,
1156 (long long)req_offset(req));
1157
Trond Myklebust788e7a82006-03-20 13:44:27 -05001158 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 ClearPageUptodate(page);
1160 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001161 req->wb_context->error = task->tk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 end_page_writeback(page);
1163 nfs_inode_remove_request(req);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001164 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 goto next;
1166 }
1167 end_page_writeback(page);
1168
1169#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1170 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1171 nfs_inode_remove_request(req);
1172 dprintk(" OK\n");
1173 goto next;
1174 }
1175 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1176 nfs_mark_request_commit(req);
1177 dprintk(" marked for commit\n");
1178#else
1179 nfs_inode_remove_request(req);
1180#endif
1181 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001182 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
1184}
1185
Trond Myklebust788e7a82006-03-20 13:44:27 -05001186static const struct rpc_call_ops nfs_write_full_ops = {
1187 .rpc_call_done = nfs_writeback_done_full,
1188 .rpc_release = nfs_writedata_release,
1189};
1190
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192/*
1193 * This function is called when the WRITE call is complete.
1194 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001195int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 struct nfs_writeargs *argp = &data->args;
1198 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001199 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1202 task->tk_pid, task->tk_status);
1203
Chuck Leverf551e442006-09-20 14:33:04 -04001204 /*
1205 * ->write_done will attempt to use post-op attributes to detect
1206 * conflicting writes by other clients. A strict interpretation
1207 * of close-to-open would allow us to continue caching even if
1208 * another writer had changed the file, but some applications
1209 * depend on tighter cache coherency when writing.
1210 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001211 status = NFS_PROTO(data->inode)->write_done(task, data);
1212 if (status != 0)
1213 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001214 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1217 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1218 /* We tried a write call, but the server did not
1219 * commit data to stable storage even though we
1220 * requested it.
1221 * Note: There is a known bug in Tru64 < 5.0 in which
1222 * the server reports NFS_DATA_SYNC, but performs
1223 * NFS_FILE_SYNC. We therefore implement this checking
1224 * as a dprintk() in order to avoid filling syslog.
1225 */
1226 static unsigned long complain;
1227
1228 if (time_before(complain, jiffies)) {
1229 dprintk("NFS: faulty NFS server %s:"
1230 " (committed = %d) != (stable = %d)\n",
David Howells54ceac42006-08-22 20:06:13 -04001231 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 resp->verf->committed, argp->stable);
1233 complain = jiffies + 300 * HZ;
1234 }
1235 }
1236#endif
1237 /* Is this a short write? */
1238 if (task->tk_status >= 0 && resp->count < argp->count) {
1239 static unsigned long complain;
1240
Chuck Lever91d5b472006-03-20 13:44:14 -05001241 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 /* Has the server at least made some progress? */
1244 if (resp->count != 0) {
1245 /* Was this an NFSv2 write or an NFSv3 stable write? */
1246 if (resp->verf->committed != NFS_UNSTABLE) {
1247 /* Resend from where the server left off */
1248 argp->offset += resp->count;
1249 argp->pgbase += resp->count;
1250 argp->count -= resp->count;
1251 } else {
1252 /* Resend as a stable write in order to avoid
1253 * headaches in the case of a server crash.
1254 */
1255 argp->stable = NFS_FILE_SYNC;
1256 }
1257 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001258 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260 if (time_before(complain, jiffies)) {
1261 printk(KERN_WARNING
1262 "NFS: Server wrote zero bytes, expected %u.\n",
1263 argp->count);
1264 complain = jiffies + 300 * HZ;
1265 }
1266 /* Can't do anything about it except throw an error. */
1267 task->tk_status = -EIO;
1268 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
1272
1273#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001274void nfs_commit_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 nfs_commit_free(wdata);
1277}
1278
1279/*
1280 * Set up the argument/result storage required for the RPC call.
1281 */
1282static void nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001283 struct nfs_write_data *data,
1284 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001286 struct nfs_page *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001288 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 /* Set up the RPC argument and reply structs
1291 * NB: take care not to mess about with data->commit et al. */
1292
1293 list_splice_init(head, &data->pages);
1294 first = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 inode = first->wb_context->dentry->d_inode;
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 data->inode = inode;
1298 data->cred = first->wb_context->cred;
1299
1300 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001301 /* Note: we always request a commit of the entire inode */
1302 data->args.offset = 0;
1303 data->args.count = 0;
1304 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 data->res.fattr = &data->fattr;
1306 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001307 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001308
1309 /* Set up the initial task struct. */
1310 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1311 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 NFS_PROTO(inode)->commit_setup(data, how);
1313
1314 data->task.tk_priority = flush_task_priority(how);
1315 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Chuck Lever0bbacc42005-11-01 16:53:32 -05001317 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318}
1319
1320/*
1321 * Commit dirty pages
1322 */
1323static int
Chuck Lever40859d72005-11-30 18:09:02 -05001324nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
1326 struct nfs_write_data *data;
1327 struct nfs_page *req;
1328
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001329 data = nfs_commit_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 if (!data)
1332 goto out_bad;
1333
1334 /* Set up the argument struct */
1335 nfs_commit_rpcsetup(head, data, how);
1336
1337 nfs_execute_write(data);
1338 return 0;
1339 out_bad:
1340 while (!list_empty(head)) {
1341 req = nfs_list_entry(head->next);
1342 nfs_list_remove_request(req);
1343 nfs_mark_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001344 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust5c2d97c2006-09-18 23:20:35 -04001345 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
1347 return -ENOMEM;
1348}
1349
1350/*
1351 * COMMIT call returned
1352 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001353static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001355 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1359 task->tk_pid, task->tk_status);
1360
Trond Myklebust788e7a82006-03-20 13:44:27 -05001361 /* Call the NFS version-specific code */
1362 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1363 return;
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 while (!list_empty(&data->pages)) {
1366 req = nfs_list_entry(data->pages.next);
1367 nfs_list_remove_request(req);
Christoph Lameterfd39fc82006-06-30 01:55:40 -07001368 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1371 req->wb_context->dentry->d_inode->i_sb->s_id,
1372 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1373 req->wb_bytes,
1374 (long long)req_offset(req));
1375 if (task->tk_status < 0) {
1376 req->wb_context->error = task->tk_status;
1377 nfs_inode_remove_request(req);
1378 dprintk(", error = %d\n", task->tk_status);
1379 goto next;
1380 }
1381
1382 /* Okay, COMMIT succeeded, apparently. Check the verifier
1383 * returned by the server against all stored verfs. */
1384 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1385 /* We have a match */
1386 nfs_inode_remove_request(req);
1387 dprintk(" OK\n");
1388 goto next;
1389 }
1390 /* We have a mismatch. Write the page again */
1391 dprintk(" mismatch\n");
1392 nfs_mark_request_dirty(req);
1393 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001394 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001397
1398static const struct rpc_call_ops nfs_commit_ops = {
1399 .rpc_call_done = nfs_commit_done,
1400 .rpc_release = nfs_commit_release,
1401};
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001402#else
1403static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1404{
1405 return 0;
1406}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407#endif
1408
Trond Myklebust3f442542006-09-17 14:46:44 -04001409static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Trond Myklebust28c69252006-09-16 13:04:50 -04001411 struct nfs_inode *nfsi = NFS_I(mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 LIST_HEAD(head);
Trond Myklebust3f442542006-09-17 14:46:44 -04001413 long res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 spin_lock(&nfsi->req_lock);
Trond Myklebust3f442542006-09-17 14:46:44 -04001416 res = nfs_scan_dirty(mapping, wbc, &head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 spin_unlock(&nfsi->req_lock);
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001418 if (res) {
Trond Myklebust28c69252006-09-16 13:04:50 -04001419 int error = nfs_flush_list(mapping->host, &head, res, how);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001420 if (error < 0)
1421 return error;
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return res;
1424}
1425
1426#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001427int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
1429 struct nfs_inode *nfsi = NFS_I(inode);
1430 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001431 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001434 res = nfs_scan_commit(inode, &head, 0, 0);
1435 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001437 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001438 if (error < 0)
1439 return error;
1440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return res;
1442}
1443#endif
1444
Trond Myklebust1c759502006-10-09 16:18:38 -04001445long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
Trond Myklebust1c759502006-10-09 16:18:38 -04001447 struct inode *inode = mapping->host;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001448 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c759502006-10-09 16:18:38 -04001449 unsigned long idx_start, idx_end;
1450 unsigned int npages = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001451 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001452 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebust3f442542006-09-17 14:46:44 -04001453 long pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Trond Myklebust1c759502006-10-09 16:18:38 -04001455 /* FIXME */
1456 if (wbc->range_cyclic)
1457 idx_start = 0;
1458 else {
1459 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1460 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1461 if (idx_end > idx_start) {
1462 unsigned long l_npages = 1 + idx_end - idx_start;
1463 npages = l_npages;
1464 if (sizeof(npages) != sizeof(l_npages) &&
1465 (unsigned long)npages != l_npages)
1466 npages = 0;
1467 }
1468 }
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001469 how &= ~FLUSH_NOCOMMIT;
1470 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 do {
Trond Myklebust1c759502006-10-09 16:18:38 -04001472 wbc->pages_skipped = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001473 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1474 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001475 continue;
Trond Myklebust1c759502006-10-09 16:18:38 -04001476 pages = nfs_scan_dirty(mapping, wbc, &head);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001477 if (pages != 0) {
1478 spin_unlock(&nfsi->req_lock);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001479 if (how & FLUSH_INVALIDATE) {
Trond Myklebust83715ad2006-07-05 13:17:12 -04001480 nfs_cancel_dirty_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001481 ret = pages;
1482 } else
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001483 ret = nfs_flush_list(inode, &head, pages, how);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001484 spin_lock(&nfsi->req_lock);
1485 continue;
1486 }
Trond Myklebust1c759502006-10-09 16:18:38 -04001487 if (wbc->pages_skipped != 0)
1488 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001489 if (nocommit)
1490 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001491 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebust1c759502006-10-09 16:18:38 -04001492 if (pages == 0) {
1493 if (wbc->pages_skipped != 0)
1494 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001495 break;
Trond Myklebust1c759502006-10-09 16:18:38 -04001496 }
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001497 if (how & FLUSH_INVALIDATE) {
1498 spin_unlock(&nfsi->req_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001499 nfs_cancel_commit_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001500 ret = pages;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001501 spin_lock(&nfsi->req_lock);
1502 continue;
1503 }
1504 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001505 spin_unlock(&nfsi->req_lock);
1506 ret = nfs_commit_list(inode, &head, how);
1507 spin_lock(&nfsi->req_lock);
1508 } while (ret >= 0);
1509 spin_unlock(&nfsi->req_lock);
1510 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
1512
Trond Myklebust1c759502006-10-09 16:18:38 -04001513/*
1514 * flush the inode to disk.
1515 */
1516int nfs_wb_all(struct inode *inode)
1517{
1518 struct address_space *mapping = inode->i_mapping;
1519 struct writeback_control wbc = {
1520 .bdi = mapping->backing_dev_info,
1521 .sync_mode = WB_SYNC_ALL,
1522 .nr_to_write = LONG_MAX,
1523 .range_cyclic = 1,
1524 };
1525 int ret;
1526
1527 ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
1528 if (ret >= 0)
1529 return 0;
1530 return ret;
1531}
1532
1533int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
1534{
1535 struct writeback_control wbc = {
1536 .bdi = mapping->backing_dev_info,
1537 .sync_mode = WB_SYNC_ALL,
1538 .nr_to_write = LONG_MAX,
1539 .range_start = range_start,
1540 .range_end = range_end,
1541 };
1542 int ret;
1543
1544 ret = nfs_sync_mapping_wait(mapping, &wbc, how);
1545 if (ret >= 0)
1546 return 0;
1547 return ret;
1548}
1549
1550static int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
1551{
1552 loff_t range_start = page_offset(page);
1553 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1554
1555 return nfs_sync_mapping_range(inode->i_mapping, range_start, range_end, how | FLUSH_STABLE);
1556}
1557
1558/*
1559 * Write back all requests on one page - we do this before reading it.
1560 */
1561int nfs_wb_page(struct inode *inode, struct page* page)
1562{
1563 return nfs_wb_page_priority(inode, page, 0);
1564}
1565
1566
David Howellsf7b422b2006-06-09 09:34:33 -04001567int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
1569 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1570 sizeof(struct nfs_write_data),
1571 0, SLAB_HWCACHE_ALIGN,
1572 NULL, NULL);
1573 if (nfs_wdata_cachep == NULL)
1574 return -ENOMEM;
1575
Matthew Dobson93d23412006-03-26 01:37:50 -08001576 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1577 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (nfs_wdata_mempool == NULL)
1579 return -ENOMEM;
1580
Matthew Dobson93d23412006-03-26 01:37:50 -08001581 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1582 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (nfs_commit_mempool == NULL)
1584 return -ENOMEM;
1585
1586 return 0;
1587}
1588
David Brownell266bee82006-06-27 12:59:15 -07001589void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
1591 mempool_destroy(nfs_commit_mempool);
1592 mempool_destroy(nfs_wdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001593 kmem_cache_destroy(nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
1595