blob: 130528d09a26fe68d59a45064bfd13488137a65e [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*,
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 struct page *,
79 unsigned int, unsigned int);
Trond Myklebuste261f512006-12-05 00:35:41 -050080static void nfs_mark_request_dirty(struct nfs_page *req);
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
Trond Myklebuste21195a2006-12-05 00:35:39 -0500214static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 unsigned int offset, unsigned int count)
216{
217 struct nfs_page *req;
Trond Myklebuste21195a2006-12-05 00:35:39 -0500218 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Trond Myklebuste21195a2006-12-05 00:35:39 -0500220 for (;;) {
221 req = nfs_update_request(ctx, page, offset, count);
222 if (!IS_ERR(req))
223 break;
224 ret = PTR_ERR(req);
225 if (ret != -EBUSY)
226 return ret;
227 ret = nfs_wb_page(page->mapping->host, page);
228 if (ret != 0)
229 return ret;
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /* Update file length */
232 nfs_grow_file(page, offset, count);
233 /* Set the PG_uptodate flag? */
234 nfs_mark_uptodate(page, offset, count);
235 nfs_unlock_request(req);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239static int wb_priority(struct writeback_control *wbc)
240{
241 if (wbc->for_reclaim)
242 return FLUSH_HIGHPRI;
243 if (wbc->for_kupdate)
244 return FLUSH_LOWPRI;
245 return 0;
246}
247
248/*
Trond Myklebuste261f512006-12-05 00:35:41 -0500249 * Find an associated nfs write request, and prepare to flush it out
250 * Returns 1 if there was no write request, or if the request was
251 * already tagged by nfs_set_page_dirty.Returns 0 if the request
252 * was not tagged.
253 * May also return an error if the user signalled nfs_wait_on_request().
254 */
255static int nfs_page_mark_flush(struct page *page)
256{
257 struct nfs_page *req;
258 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
259 int ret;
260
261 spin_lock(req_lock);
262 for(;;) {
263 req = nfs_page_find_request_locked(page);
264 if (req == NULL) {
265 spin_unlock(req_lock);
266 return 1;
267 }
268 if (nfs_lock_request_dontget(req))
269 break;
270 /* Note: If we hold the page lock, as is the case in nfs_writepage,
271 * then the call to nfs_lock_request_dontget() will always
272 * succeed provided that someone hasn't already marked the
273 * request as dirty (in which case we don't care).
274 */
275 spin_unlock(req_lock);
276 ret = nfs_wait_on_request(req);
277 nfs_release_request(req);
278 if (ret != 0)
279 return ret;
280 spin_lock(req_lock);
281 }
282 spin_unlock(req_lock);
283 if (test_and_set_bit(PG_FLUSHING, &req->wb_flags) == 0)
284 nfs_mark_request_dirty(req);
285 ret = test_bit(PG_NEED_FLUSH, &req->wb_flags);
286 nfs_unlock_request(req);
287 return ret;
288}
289
290/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 * Write an mmapped page to the server.
292 */
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500293static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 struct nfs_open_context *ctx;
296 struct inode *inode = page->mapping->host;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500297 unsigned offset;
Trond Myklebuste261f512006-12-05 00:35:41 -0500298 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Chuck Lever91d5b472006-03-20 13:44:14 -0500300 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
301 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
302
Trond Myklebuste261f512006-12-05 00:35:41 -0500303 err = nfs_page_mark_flush(page);
304 if (err <= 0)
305 goto out;
306 err = 0;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500307 offset = nfs_page_length(page);
308 if (!offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 goto out;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500310
Trond Myklebustd5308382005-11-04 15:33:38 -0500311 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (ctx == NULL) {
313 err = -EBADF;
314 goto out;
315 }
Trond Myklebust200baa22006-12-05 00:35:40 -0500316 err = nfs_writepage_setup(ctx, page, 0, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 put_nfs_open_context(ctx);
Trond Myklebuste261f512006-12-05 00:35:41 -0500318 if (err != 0)
319 goto out;
320 err = nfs_page_mark_flush(page);
321 if (err > 0)
322 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323out:
Trond Myklebust200baa22006-12-05 00:35:40 -0500324 if (!wbc->for_writepages)
325 nfs_flush_mapping(page->mapping, wbc, wb_priority(wbc));
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500326 return err;
327}
328
329int nfs_writepage(struct page *page, struct writeback_control *wbc)
330{
331 int err;
332
333 err = nfs_writepage_locked(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return err;
336}
337
338/*
339 * Note: causes nfs_update_request() to block on the assumption
340 * that the writeback is generated due to memory pressure.
341 */
342int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
343{
344 struct backing_dev_info *bdi = mapping->backing_dev_info;
345 struct inode *inode = mapping->host;
346 int err;
347
Chuck Lever91d5b472006-03-20 13:44:14 -0500348 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 err = generic_writepages(mapping, wbc);
351 if (err)
352 return err;
353 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
354 if (wbc->nonblocking)
355 return 0;
356 nfs_wait_on_write_congestion(mapping, 0);
357 }
Trond Myklebust28c69252006-09-16 13:04:50 -0400358 err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (err < 0)
360 goto out;
Chuck Lever91d5b472006-03-20 13:44:14 -0500361 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
363 err = nfs_wait_on_requests(inode, 0, 0);
364 if (err < 0)
365 goto out;
366 }
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000367 err = nfs_commit_inode(inode, wb_priority(wbc));
Trond Myklebust3f442542006-09-17 14:46:44 -0400368 if (err > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370out:
371 clear_bit(BDI_write_congested, &bdi->state);
372 wake_up_all(&nfs_write_congestion);
Andrew Morton3fcfab12006-10-19 23:28:16 -0700373 congestion_end(WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return err;
375}
376
377/*
378 * Insert a write request into an inode
379 */
380static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
381{
382 struct nfs_inode *nfsi = NFS_I(inode);
383 int error;
384
385 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
386 BUG_ON(error == -EEXIST);
387 if (error)
388 return error;
389 if (!nfsi->npages) {
390 igrab(inode);
391 nfs_begin_data_update(inode);
392 if (nfs_have_delegation(inode, FMODE_WRITE))
393 nfsi->change_attr++;
394 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500395 SetPagePrivate(req->wb_page);
Trond Myklebust277459d2006-12-05 00:35:35 -0500396 set_page_private(req->wb_page, (unsigned long)req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 nfsi->npages++;
398 atomic_inc(&req->wb_count);
399 return 0;
400}
401
402/*
403 * Insert a write request into an inode
404 */
405static void nfs_inode_remove_request(struct nfs_page *req)
406{
407 struct inode *inode = req->wb_context->dentry->d_inode;
408 struct nfs_inode *nfsi = NFS_I(inode);
409
410 BUG_ON (!NFS_WBACK_BUSY(req));
411
412 spin_lock(&nfsi->req_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500413 set_page_private(req->wb_page, 0);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500414 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
416 nfsi->npages--;
417 if (!nfsi->npages) {
418 spin_unlock(&nfsi->req_lock);
Trond Myklebust951a1432005-06-22 17:16:30 +0000419 nfs_end_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 iput(inode);
421 } else
422 spin_unlock(&nfsi->req_lock);
423 nfs_clear_request(req);
424 nfs_release_request(req);
425}
426
427/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 * Add a request to the inode's dirty list.
429 */
430static void
431nfs_mark_request_dirty(struct nfs_page *req)
432{
433 struct inode *inode = req->wb_context->dentry->d_inode;
434 struct nfs_inode *nfsi = NFS_I(inode);
435
436 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000437 radix_tree_tag_set(&nfsi->nfs_page_tree,
438 req->wb_index, NFS_PAGE_TAG_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 nfs_list_add_request(req, &nfsi->dirty);
440 nfsi->ndirty++;
441 spin_unlock(&nfsi->req_lock);
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700442 inc_zone_page_state(req->wb_page, NR_FILE_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 mark_inode_dirty(inode);
444}
445
446/*
447 * Check if a request is dirty
448 */
449static inline int
450nfs_dirty_request(struct nfs_page *req)
451{
Trond Myklebuste261f512006-12-05 00:35:41 -0500452 return test_bit(PG_FLUSHING, &req->wb_flags) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
455#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
456/*
457 * Add a request to the inode's commit list.
458 */
459static void
460nfs_mark_request_commit(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);
466 nfs_list_add_request(req, &nfsi->commit);
467 nfsi->ncommit++;
468 spin_unlock(&nfsi->req_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700469 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 mark_inode_dirty(inode);
471}
472#endif
473
474/*
475 * Wait for a request to complete.
476 *
477 * Interruptible by signals only if mounted with intr flag.
478 */
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500479static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 struct nfs_inode *nfsi = NFS_I(inode);
482 struct nfs_page *req;
483 unsigned long idx_end, next;
484 unsigned int res = 0;
485 int error;
486
487 if (npages == 0)
488 idx_end = ~0;
489 else
490 idx_end = idx_start + npages - 1;
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 next = idx_start;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000493 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 -0700494 if (req->wb_index > idx_end)
495 break;
496
497 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000498 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 atomic_inc(&req->wb_count);
501 spin_unlock(&nfsi->req_lock);
502 error = nfs_wait_on_request(req);
503 nfs_release_request(req);
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500504 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (error < 0)
506 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 res++;
508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return res;
510}
511
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500512static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
513{
514 struct nfs_inode *nfsi = NFS_I(inode);
515 int ret;
516
517 spin_lock(&nfsi->req_lock);
518 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
519 spin_unlock(&nfsi->req_lock);
520 return ret;
521}
522
Trond Myklebust83715ad2006-07-05 13:17:12 -0400523static void nfs_cancel_dirty_list(struct list_head *head)
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400524{
525 struct nfs_page *req;
526 while(!list_empty(head)) {
527 req = nfs_list_entry(head->next);
528 nfs_list_remove_request(req);
529 nfs_inode_remove_request(req);
530 nfs_clear_page_writeback(req);
531 }
532}
533
Trond Myklebust83715ad2006-07-05 13:17:12 -0400534static void nfs_cancel_commit_list(struct list_head *head)
535{
536 struct nfs_page *req;
537
538 while(!list_empty(head)) {
539 req = nfs_list_entry(head->next);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700540 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400541 nfs_list_remove_request(req);
542 nfs_inode_remove_request(req);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700543 nfs_unlock_request(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400544 }
545}
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
548/*
549 * nfs_scan_commit - Scan an inode for commit requests
550 * @inode: NFS inode to scan
551 * @dst: destination list
552 * @idx_start: lower bound of page->index to scan.
553 * @npages: idx_start + npages sets the upper bound to scan.
554 *
555 * Moves requests from the inode's 'commit' request list.
556 * The requests are *not* checked to ensure that they form a contiguous set.
557 */
558static int
559nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
560{
561 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000562 int res = 0;
563
564 if (nfsi->ncommit != 0) {
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400565 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000566 nfsi->ncommit -= res;
567 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
568 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return res;
571}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500572#else
573static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
574{
575 return 0;
576}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577#endif
578
579static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
580{
581 struct backing_dev_info *bdi = mapping->backing_dev_info;
582 DEFINE_WAIT(wait);
583 int ret = 0;
584
585 might_sleep();
586
587 if (!bdi_write_congested(bdi))
588 return 0;
Chuck Lever91d5b472006-03-20 13:44:14 -0500589
590 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (intr) {
593 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
594 sigset_t oldset;
595
596 rpc_clnt_sigmask(clnt, &oldset);
597 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
598 if (bdi_write_congested(bdi)) {
599 if (signalled())
600 ret = -ERESTARTSYS;
601 else
602 schedule();
603 }
604 rpc_clnt_sigunmask(clnt, &oldset);
605 } else {
606 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
607 if (bdi_write_congested(bdi))
608 schedule();
609 }
610 finish_wait(&nfs_write_congestion, &wait);
611 return ret;
612}
613
614
615/*
616 * Try to update any existing write request, or create one if there is none.
617 * In order to match, the request's credentials must match those of
618 * the calling process.
619 *
620 * Note: Should always be called with the Page Lock held!
621 */
622static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
Trond Myklebuste21195a2006-12-05 00:35:39 -0500623 struct page *page, unsigned int offset, unsigned int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Trond Myklebuste21195a2006-12-05 00:35:39 -0500625 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 struct nfs_inode *nfsi = NFS_I(inode);
627 struct nfs_page *req, *new = NULL;
628 unsigned long rqend, end;
629
630 end = offset + bytes;
631
Trond Myklebuste21195a2006-12-05 00:35:39 -0500632 if (nfs_wait_on_write_congestion(page->mapping, NFS_SERVER(inode)->flags & NFS_MOUNT_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return ERR_PTR(-ERESTARTSYS);
634 for (;;) {
635 /* Loop over all inode entries and see if we find
636 * A request for the page we wish to update
637 */
638 spin_lock(&nfsi->req_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500639 req = nfs_page_find_request_locked(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (req) {
641 if (!nfs_lock_request_dontget(req)) {
642 int error;
Trond Myklebust277459d2006-12-05 00:35:35 -0500643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 spin_unlock(&nfsi->req_lock);
645 error = nfs_wait_on_request(req);
646 nfs_release_request(req);
Neil Brown1dd594b2006-03-20 13:44:04 -0500647 if (error < 0) {
648 if (new)
649 nfs_release_request(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return ERR_PTR(error);
Neil Brown1dd594b2006-03-20 13:44:04 -0500651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 continue;
653 }
654 spin_unlock(&nfsi->req_lock);
655 if (new)
656 nfs_release_request(new);
657 break;
658 }
659
660 if (new) {
661 int error;
662 nfs_lock_request_dontget(new);
663 error = nfs_inode_add_request(inode, new);
664 if (error) {
665 spin_unlock(&nfsi->req_lock);
666 nfs_unlock_request(new);
667 return ERR_PTR(error);
668 }
669 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return new;
671 }
672 spin_unlock(&nfsi->req_lock);
673
674 new = nfs_create_request(ctx, inode, page, offset, bytes);
675 if (IS_ERR(new))
676 return new;
677 }
678
679 /* We have a request for our page.
680 * If the creds don't match, or the
681 * page addresses don't match,
682 * tell the caller to wait on the conflicting
683 * request.
684 */
685 rqend = req->wb_offset + req->wb_bytes;
686 if (req->wb_context != ctx
687 || req->wb_page != page
688 || !nfs_dirty_request(req)
689 || offset > rqend || end < req->wb_offset) {
690 nfs_unlock_request(req);
691 return ERR_PTR(-EBUSY);
692 }
693
694 /* Okay, the request matches. Update the region */
695 if (offset < req->wb_offset) {
696 req->wb_offset = offset;
697 req->wb_pgbase = offset;
698 req->wb_bytes = rqend - req->wb_offset;
699 }
700
701 if (end > rqend)
702 req->wb_bytes = end - req->wb_offset;
703
704 return req;
705}
706
707int nfs_flush_incompatible(struct file *file, struct page *page)
708{
709 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 struct nfs_page *req;
Trond Myklebust1a545332006-12-05 00:35:40 -0500711 int do_flush, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /*
713 * Look for a request corresponding to this page. If there
714 * is one, and it belongs to another file, we flush it out
715 * before we try to copy anything into the page. Do this
716 * due to the lack of an ACCESS-type call in NFSv2.
717 * Also do the same if we find a request from an existing
718 * dropped page.
719 */
Trond Myklebust1a545332006-12-05 00:35:40 -0500720 do {
721 req = nfs_page_find_request(page);
722 if (req == NULL)
723 return 0;
724 do_flush = req->wb_page != page || req->wb_context != ctx
Trond Myklebuste261f512006-12-05 00:35:41 -0500725 || !nfs_dirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 nfs_release_request(req);
Trond Myklebust1a545332006-12-05 00:35:40 -0500727 if (!do_flush)
728 return 0;
729 status = nfs_wb_page(page->mapping->host, page);
730 } while (status == 0);
731 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
734/*
735 * Update and possibly write a cached page of an NFS file.
736 *
737 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
738 * things with a page scheduled for an RPC call (e.g. invalidate it).
739 */
740int nfs_updatepage(struct file *file, struct page *page,
741 unsigned int offset, unsigned int count)
742{
743 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 int status = 0;
746
Chuck Lever91d5b472006-03-20 13:44:14 -0500747 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500750 file->f_dentry->d_parent->d_name.name,
751 file->f_dentry->d_name.name, count,
752 (long long)(page_offset(page) +offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 /* If we're not using byte range locks, and we know the page
755 * is entirely in cache, it may be more efficient to avoid
756 * fragmenting write requests.
757 */
Trond Myklebustab0a3db2005-06-22 17:16:30 +0000758 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
Trond Myklebust49a70f22006-12-05 00:35:38 -0500759 count = max(count + offset, nfs_page_length(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762
Trond Myklebuste21195a2006-12-05 00:35:39 -0500763 status = nfs_writepage_setup(ctx, page, offset, count);
Trond Myklebuste261f512006-12-05 00:35:41 -0500764 __set_page_dirty_nobuffers(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
767 status, (long long)i_size_read(inode));
768 if (status < 0)
769 ClearPageUptodate(page);
770 return status;
771}
772
773static void nfs_writepage_release(struct nfs_page *req)
774{
775 end_page_writeback(req->wb_page);
776
777#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
778 if (!PageError(req->wb_page)) {
779 if (NFS_NEED_RESCHED(req)) {
780 nfs_mark_request_dirty(req);
781 goto out;
782 } else if (NFS_NEED_COMMIT(req)) {
783 nfs_mark_request_commit(req);
784 goto out;
785 }
786 }
787 nfs_inode_remove_request(req);
788
789out:
790 nfs_clear_commit(req);
791 nfs_clear_reschedule(req);
792#else
793 nfs_inode_remove_request(req);
794#endif
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000795 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
798static inline int flush_task_priority(int how)
799{
800 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
801 case FLUSH_HIGHPRI:
802 return RPC_PRIORITY_HIGH;
803 case FLUSH_LOWPRI:
804 return RPC_PRIORITY_LOW;
805 }
806 return RPC_PRIORITY_NORMAL;
807}
808
809/*
810 * Set up the argument/result storage required for the RPC call.
811 */
812static void nfs_write_rpcsetup(struct nfs_page *req,
813 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500814 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 unsigned int count, unsigned int offset,
816 int how)
817{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -0500819 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 /* Set up the RPC argument and reply structs
822 * NB: take care not to mess about with data->commit et al. */
823
824 data->req = req;
825 data->inode = inode = req->wb_context->dentry->d_inode;
826 data->cred = req->wb_context->cred;
827
828 data->args.fh = NFS_FH(inode);
829 data->args.offset = req_offset(req) + offset;
830 data->args.pgbase = req->wb_pgbase + offset;
831 data->args.pages = data->pagevec;
832 data->args.count = count;
833 data->args.context = req->wb_context;
834
835 data->res.fattr = &data->fattr;
836 data->res.count = count;
837 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400838 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Trond Myklebust788e7a82006-03-20 13:44:27 -0500840 /* Set up the initial task struct. */
841 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
842 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 NFS_PROTO(inode)->write_setup(data, how);
844
845 data->task.tk_priority = flush_task_priority(how);
846 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500849 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 inode->i_sb->s_id,
851 (long long)NFS_FILEID(inode),
852 count,
853 (unsigned long long)data->args.offset);
854}
855
856static void nfs_execute_write(struct nfs_write_data *data)
857{
858 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
859 sigset_t oldset;
860
861 rpc_clnt_sigmask(clnt, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 rpc_execute(&data->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 rpc_clnt_sigunmask(clnt, &oldset);
864}
865
866/*
867 * Generate multiple small requests to write out a single
868 * contiguous dirty area on one page.
869 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500870static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 struct nfs_page *req = nfs_list_entry(head->next);
873 struct page *page = req->wb_page;
874 struct nfs_write_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700875 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
876 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 int requests = 0;
878 LIST_HEAD(list);
879
880 nfs_list_remove_request(req);
881
882 nbytes = req->wb_bytes;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700883 do {
884 size_t len = min(nbytes, wsize);
885
886 data = nfs_writedata_alloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (!data)
888 goto out_bad;
889 list_add(&data->pages, &list);
890 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700891 nbytes -= len;
892 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 atomic_set(&req->wb_complete, requests);
894
895 ClearPageError(page);
Trond Myklebustbb713d62005-12-03 15:20:14 -0500896 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 offset = 0;
898 nbytes = req->wb_bytes;
899 do {
900 data = list_entry(list.next, struct nfs_write_data, pages);
901 list_del_init(&data->pages);
902
903 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 if (nbytes > wsize) {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500906 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
907 wsize, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 offset += wsize;
909 nbytes -= wsize;
910 } else {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500911 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
912 nbytes, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 nbytes = 0;
914 }
915 nfs_execute_write(data);
916 } while (nbytes != 0);
917
918 return 0;
919
920out_bad:
921 while (!list_empty(&list)) {
922 data = list_entry(list.next, struct nfs_write_data, pages);
923 list_del(&data->pages);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500924 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000927 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return -ENOMEM;
929}
930
931/*
932 * Create an RPC task for the given write request and kick it.
933 * The page must have been locked by the caller.
934 *
935 * It may happen that the page we're passed is not marked dirty.
936 * This is the case if nfs_updatepage detects a conflicting request
937 * that has been written but not committed.
938 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500939static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
941 struct nfs_page *req;
942 struct page **pages;
943 struct nfs_write_data *data;
944 unsigned int count;
945
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700946 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (!data)
948 goto out_bad;
949
950 pages = data->pagevec;
951 count = 0;
952 while (!list_empty(head)) {
953 req = nfs_list_entry(head->next);
954 nfs_list_remove_request(req);
955 nfs_list_add_request(req, &data->pages);
956 ClearPageError(req->wb_page);
Trond Myklebustbb713d62005-12-03 15:20:14 -0500957 set_page_writeback(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 *pages++ = req->wb_page;
959 count += req->wb_bytes;
960 }
961 req = nfs_list_entry(data->pages.next);
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 /* Set up the argument struct */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500964 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 nfs_execute_write(data);
967 return 0;
968 out_bad:
969 while (!list_empty(head)) {
970 struct nfs_page *req = nfs_list_entry(head->next);
971 nfs_list_remove_request(req);
972 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000973 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
975 return -ENOMEM;
976}
977
Trond Myklebust7d46a492006-03-20 13:44:50 -0500978static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 LIST_HEAD(one_request);
Trond Myklebust7d46a492006-03-20 13:44:50 -0500981 int (*flush_one)(struct inode *, struct list_head *, int);
982 struct nfs_page *req;
983 int wpages = NFS_SERVER(inode)->wpages;
984 int wsize = NFS_SERVER(inode)->wsize;
985 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Trond Myklebust7d46a492006-03-20 13:44:50 -0500987 flush_one = nfs_flush_one;
988 if (wsize < PAGE_CACHE_SIZE)
989 flush_one = nfs_flush_multi;
990 /* For single writes, FLUSH_STABLE is more efficient */
991 if (npages <= wpages && npages == NFS_I(inode)->npages
992 && nfs_list_entry(head->next)->wb_bytes <= wsize)
993 how |= FLUSH_STABLE;
994
995 do {
996 nfs_coalesce_requests(head, &one_request, wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 req = nfs_list_entry(one_request.next);
Trond Myklebust7d46a492006-03-20 13:44:50 -0500998 error = flush_one(inode, &one_request, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (error < 0)
Trond Myklebust7d46a492006-03-20 13:44:50 -05001000 goto out_err;
1001 } while (!list_empty(head));
1002 return 0;
1003out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 while (!list_empty(head)) {
1005 req = nfs_list_entry(head->next);
1006 nfs_list_remove_request(req);
1007 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001008 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010 return error;
1011}
1012
1013/*
1014 * Handle a write reply that flushed part of a page.
1015 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001016static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001018 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 struct nfs_page *req = data->req;
1020 struct page *page = req->wb_page;
1021
1022 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1023 req->wb_context->dentry->d_inode->i_sb->s_id,
1024 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1025 req->wb_bytes,
1026 (long long)req_offset(req));
1027
Trond Myklebust788e7a82006-03-20 13:44:27 -05001028 if (nfs_writeback_done(task, data) != 0)
1029 return;
1030
1031 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 ClearPageUptodate(page);
1033 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001034 req->wb_context->error = task->tk_status;
1035 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 } else {
1037#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1038 if (data->verf.committed < NFS_FILE_SYNC) {
1039 if (!NFS_NEED_COMMIT(req)) {
1040 nfs_defer_commit(req);
1041 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1042 dprintk(" defer commit\n");
1043 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1044 nfs_defer_reschedule(req);
1045 dprintk(" server reboot detected\n");
1046 }
1047 } else
1048#endif
1049 dprintk(" OK\n");
1050 }
1051
1052 if (atomic_dec_and_test(&req->wb_complete))
1053 nfs_writepage_release(req);
1054}
1055
Trond Myklebust788e7a82006-03-20 13:44:27 -05001056static const struct rpc_call_ops nfs_write_partial_ops = {
1057 .rpc_call_done = nfs_writeback_done_partial,
1058 .rpc_release = nfs_writedata_release,
1059};
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061/*
1062 * Handle a write reply that flushes a whole page.
1063 *
1064 * FIXME: There is an inherent race with invalidate_inode_pages and
1065 * writebacks since the page->count is kept > 1 for as long
1066 * as the page has a write request pending.
1067 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001068static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001070 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 struct nfs_page *req;
1072 struct page *page;
1073
Trond Myklebust788e7a82006-03-20 13:44:27 -05001074 if (nfs_writeback_done(task, data) != 0)
1075 return;
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 /* Update attributes as result of writeback. */
1078 while (!list_empty(&data->pages)) {
1079 req = nfs_list_entry(data->pages.next);
1080 nfs_list_remove_request(req);
1081 page = req->wb_page;
1082
1083 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1084 req->wb_context->dentry->d_inode->i_sb->s_id,
1085 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1086 req->wb_bytes,
1087 (long long)req_offset(req));
1088
Trond Myklebust788e7a82006-03-20 13:44:27 -05001089 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 ClearPageUptodate(page);
1091 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001092 req->wb_context->error = task->tk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 end_page_writeback(page);
1094 nfs_inode_remove_request(req);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001095 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 goto next;
1097 }
1098 end_page_writeback(page);
1099
1100#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1101 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1102 nfs_inode_remove_request(req);
1103 dprintk(" OK\n");
1104 goto next;
1105 }
1106 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1107 nfs_mark_request_commit(req);
1108 dprintk(" marked for commit\n");
1109#else
1110 nfs_inode_remove_request(req);
1111#endif
1112 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001113 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
1115}
1116
Trond Myklebust788e7a82006-03-20 13:44:27 -05001117static const struct rpc_call_ops nfs_write_full_ops = {
1118 .rpc_call_done = nfs_writeback_done_full,
1119 .rpc_release = nfs_writedata_release,
1120};
1121
1122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123/*
1124 * This function is called when the WRITE call is complete.
1125 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001126int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 struct nfs_writeargs *argp = &data->args;
1129 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001130 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1133 task->tk_pid, task->tk_status);
1134
Chuck Leverf551e442006-09-20 14:33:04 -04001135 /*
1136 * ->write_done will attempt to use post-op attributes to detect
1137 * conflicting writes by other clients. A strict interpretation
1138 * of close-to-open would allow us to continue caching even if
1139 * another writer had changed the file, but some applications
1140 * depend on tighter cache coherency when writing.
1141 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001142 status = NFS_PROTO(data->inode)->write_done(task, data);
1143 if (status != 0)
1144 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001145 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1148 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1149 /* We tried a write call, but the server did not
1150 * commit data to stable storage even though we
1151 * requested it.
1152 * Note: There is a known bug in Tru64 < 5.0 in which
1153 * the server reports NFS_DATA_SYNC, but performs
1154 * NFS_FILE_SYNC. We therefore implement this checking
1155 * as a dprintk() in order to avoid filling syslog.
1156 */
1157 static unsigned long complain;
1158
1159 if (time_before(complain, jiffies)) {
1160 dprintk("NFS: faulty NFS server %s:"
1161 " (committed = %d) != (stable = %d)\n",
David Howells54ceac42006-08-22 20:06:13 -04001162 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 resp->verf->committed, argp->stable);
1164 complain = jiffies + 300 * HZ;
1165 }
1166 }
1167#endif
1168 /* Is this a short write? */
1169 if (task->tk_status >= 0 && resp->count < argp->count) {
1170 static unsigned long complain;
1171
Chuck Lever91d5b472006-03-20 13:44:14 -05001172 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 /* Has the server at least made some progress? */
1175 if (resp->count != 0) {
1176 /* Was this an NFSv2 write or an NFSv3 stable write? */
1177 if (resp->verf->committed != NFS_UNSTABLE) {
1178 /* Resend from where the server left off */
1179 argp->offset += resp->count;
1180 argp->pgbase += resp->count;
1181 argp->count -= resp->count;
1182 } else {
1183 /* Resend as a stable write in order to avoid
1184 * headaches in the case of a server crash.
1185 */
1186 argp->stable = NFS_FILE_SYNC;
1187 }
1188 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001189 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
1191 if (time_before(complain, jiffies)) {
1192 printk(KERN_WARNING
1193 "NFS: Server wrote zero bytes, expected %u.\n",
1194 argp->count);
1195 complain = jiffies + 300 * HZ;
1196 }
1197 /* Can't do anything about it except throw an error. */
1198 task->tk_status = -EIO;
1199 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001200 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
1203
1204#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001205void nfs_commit_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 nfs_commit_free(wdata);
1208}
1209
1210/*
1211 * Set up the argument/result storage required for the RPC call.
1212 */
1213static void nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001214 struct nfs_write_data *data,
1215 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001217 struct nfs_page *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001219 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 /* Set up the RPC argument and reply structs
1222 * NB: take care not to mess about with data->commit et al. */
1223
1224 list_splice_init(head, &data->pages);
1225 first = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 inode = first->wb_context->dentry->d_inode;
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 data->inode = inode;
1229 data->cred = first->wb_context->cred;
1230
1231 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001232 /* Note: we always request a commit of the entire inode */
1233 data->args.offset = 0;
1234 data->args.count = 0;
1235 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 data->res.fattr = &data->fattr;
1237 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001238 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001239
1240 /* Set up the initial task struct. */
1241 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1242 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 NFS_PROTO(inode)->commit_setup(data, how);
1244
1245 data->task.tk_priority = flush_task_priority(how);
1246 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Chuck Lever0bbacc42005-11-01 16:53:32 -05001248 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
1251/*
1252 * Commit dirty pages
1253 */
1254static int
Chuck Lever40859d72005-11-30 18:09:02 -05001255nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
1257 struct nfs_write_data *data;
1258 struct nfs_page *req;
1259
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001260 data = nfs_commit_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 if (!data)
1263 goto out_bad;
1264
1265 /* Set up the argument struct */
1266 nfs_commit_rpcsetup(head, data, how);
1267
1268 nfs_execute_write(data);
1269 return 0;
1270 out_bad:
1271 while (!list_empty(head)) {
1272 req = nfs_list_entry(head->next);
1273 nfs_list_remove_request(req);
1274 nfs_mark_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001275 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust5c2d97c2006-09-18 23:20:35 -04001276 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278 return -ENOMEM;
1279}
1280
1281/*
1282 * COMMIT call returned
1283 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001284static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001286 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1290 task->tk_pid, task->tk_status);
1291
Trond Myklebust788e7a82006-03-20 13:44:27 -05001292 /* Call the NFS version-specific code */
1293 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1294 return;
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 while (!list_empty(&data->pages)) {
1297 req = nfs_list_entry(data->pages.next);
1298 nfs_list_remove_request(req);
Christoph Lameterfd39fc82006-06-30 01:55:40 -07001299 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1302 req->wb_context->dentry->d_inode->i_sb->s_id,
1303 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1304 req->wb_bytes,
1305 (long long)req_offset(req));
1306 if (task->tk_status < 0) {
1307 req->wb_context->error = task->tk_status;
1308 nfs_inode_remove_request(req);
1309 dprintk(", error = %d\n", task->tk_status);
1310 goto next;
1311 }
1312
1313 /* Okay, COMMIT succeeded, apparently. Check the verifier
1314 * returned by the server against all stored verfs. */
1315 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1316 /* We have a match */
1317 nfs_inode_remove_request(req);
1318 dprintk(" OK\n");
1319 goto next;
1320 }
1321 /* We have a mismatch. Write the page again */
1322 dprintk(" mismatch\n");
1323 nfs_mark_request_dirty(req);
1324 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001325 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001328
1329static const struct rpc_call_ops nfs_commit_ops = {
1330 .rpc_call_done = nfs_commit_done,
1331 .rpc_release = nfs_commit_release,
1332};
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001333#else
1334static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1335{
1336 return 0;
1337}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338#endif
1339
Trond Myklebust3f442542006-09-17 14:46:44 -04001340static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
Trond Myklebust28c69252006-09-16 13:04:50 -04001342 struct nfs_inode *nfsi = NFS_I(mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 LIST_HEAD(head);
Trond Myklebust3f442542006-09-17 14:46:44 -04001344 long res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 spin_lock(&nfsi->req_lock);
Trond Myklebust3f442542006-09-17 14:46:44 -04001347 res = nfs_scan_dirty(mapping, wbc, &head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 spin_unlock(&nfsi->req_lock);
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001349 if (res) {
Trond Myklebust28c69252006-09-16 13:04:50 -04001350 int error = nfs_flush_list(mapping->host, &head, res, how);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001351 if (error < 0)
1352 return error;
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return res;
1355}
1356
1357#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001358int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
1360 struct nfs_inode *nfsi = NFS_I(inode);
1361 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001362 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001365 res = nfs_scan_commit(inode, &head, 0, 0);
1366 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001368 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001369 if (error < 0)
1370 return error;
1371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return res;
1373}
1374#endif
1375
Trond Myklebust1c759502006-10-09 16:18:38 -04001376long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Trond Myklebust1c759502006-10-09 16:18:38 -04001378 struct inode *inode = mapping->host;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001379 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c759502006-10-09 16:18:38 -04001380 unsigned long idx_start, idx_end;
1381 unsigned int npages = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001382 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001383 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebust3f442542006-09-17 14:46:44 -04001384 long pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Trond Myklebust1c759502006-10-09 16:18:38 -04001386 /* FIXME */
1387 if (wbc->range_cyclic)
1388 idx_start = 0;
1389 else {
1390 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1391 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1392 if (idx_end > idx_start) {
1393 unsigned long l_npages = 1 + idx_end - idx_start;
1394 npages = l_npages;
1395 if (sizeof(npages) != sizeof(l_npages) &&
1396 (unsigned long)npages != l_npages)
1397 npages = 0;
1398 }
1399 }
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001400 how &= ~FLUSH_NOCOMMIT;
1401 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 do {
Trond Myklebust1c759502006-10-09 16:18:38 -04001403 wbc->pages_skipped = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001404 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1405 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001406 continue;
Trond Myklebust1c759502006-10-09 16:18:38 -04001407 pages = nfs_scan_dirty(mapping, wbc, &head);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001408 if (pages != 0) {
1409 spin_unlock(&nfsi->req_lock);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001410 if (how & FLUSH_INVALIDATE) {
Trond Myklebust83715ad2006-07-05 13:17:12 -04001411 nfs_cancel_dirty_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001412 ret = pages;
1413 } else
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001414 ret = nfs_flush_list(inode, &head, pages, how);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001415 spin_lock(&nfsi->req_lock);
1416 continue;
1417 }
Trond Myklebust1c759502006-10-09 16:18:38 -04001418 if (wbc->pages_skipped != 0)
1419 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001420 if (nocommit)
1421 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001422 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebust1c759502006-10-09 16:18:38 -04001423 if (pages == 0) {
1424 if (wbc->pages_skipped != 0)
1425 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001426 break;
Trond Myklebust1c759502006-10-09 16:18:38 -04001427 }
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001428 if (how & FLUSH_INVALIDATE) {
1429 spin_unlock(&nfsi->req_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001430 nfs_cancel_commit_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001431 ret = pages;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001432 spin_lock(&nfsi->req_lock);
1433 continue;
1434 }
1435 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001436 spin_unlock(&nfsi->req_lock);
1437 ret = nfs_commit_list(inode, &head, how);
1438 spin_lock(&nfsi->req_lock);
1439 } while (ret >= 0);
1440 spin_unlock(&nfsi->req_lock);
1441 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
Trond Myklebust1c759502006-10-09 16:18:38 -04001444/*
1445 * flush the inode to disk.
1446 */
1447int nfs_wb_all(struct inode *inode)
1448{
1449 struct address_space *mapping = inode->i_mapping;
1450 struct writeback_control wbc = {
1451 .bdi = mapping->backing_dev_info,
1452 .sync_mode = WB_SYNC_ALL,
1453 .nr_to_write = LONG_MAX,
1454 .range_cyclic = 1,
1455 };
1456 int ret;
1457
1458 ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
1459 if (ret >= 0)
1460 return 0;
1461 return ret;
1462}
1463
1464int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
1465{
1466 struct writeback_control wbc = {
1467 .bdi = mapping->backing_dev_info,
1468 .sync_mode = WB_SYNC_ALL,
1469 .nr_to_write = LONG_MAX,
1470 .range_start = range_start,
1471 .range_end = range_end,
1472 };
1473 int ret;
1474
1475 ret = nfs_sync_mapping_wait(mapping, &wbc, how);
1476 if (ret >= 0)
1477 return 0;
1478 return ret;
1479}
1480
1481static int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
1482{
1483 loff_t range_start = page_offset(page);
1484 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001485 struct writeback_control wbc = {
1486 .bdi = page->mapping->backing_dev_info,
1487 .sync_mode = WB_SYNC_ALL,
1488 .nr_to_write = LONG_MAX,
1489 .range_start = range_start,
1490 .range_end = range_end,
1491 };
1492 int ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001493
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001494 BUG_ON(!PageLocked(page));
1495 if (!(how & FLUSH_NOWRITEPAGE) && clear_page_dirty_for_io(page)) {
1496 ret = nfs_writepage_locked(page, &wbc);
1497 if (ret < 0)
1498 goto out;
1499 }
1500 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1501 if (ret >= 0)
1502 return 0;
1503out:
1504 return ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001505}
1506
1507/*
1508 * Write back all requests on one page - we do this before reading it.
1509 */
1510int nfs_wb_page(struct inode *inode, struct page* page)
1511{
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001512 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
Trond Myklebust1c759502006-10-09 16:18:38 -04001513}
1514
Trond Myklebust1a545332006-12-05 00:35:40 -05001515int nfs_set_page_dirty(struct page *page)
1516{
1517 struct nfs_page *req;
1518
1519 req = nfs_page_find_request(page);
1520 if (req != NULL) {
1521 /* Mark any existing write requests for flushing */
1522 set_bit(PG_NEED_FLUSH, &req->wb_flags);
1523 nfs_release_request(req);
1524 }
1525 return __set_page_dirty_nobuffers(page);
1526}
1527
Trond Myklebust1c759502006-10-09 16:18:38 -04001528
David Howellsf7b422b2006-06-09 09:34:33 -04001529int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
1531 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1532 sizeof(struct nfs_write_data),
1533 0, SLAB_HWCACHE_ALIGN,
1534 NULL, NULL);
1535 if (nfs_wdata_cachep == NULL)
1536 return -ENOMEM;
1537
Matthew Dobson93d23412006-03-26 01:37:50 -08001538 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1539 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 if (nfs_wdata_mempool == NULL)
1541 return -ENOMEM;
1542
Matthew Dobson93d23412006-03-26 01:37:50 -08001543 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1544 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 if (nfs_commit_mempool == NULL)
1546 return -ENOMEM;
1547
1548 return 0;
1549}
1550
David Brownell266bee82006-06-27 12:59:15 -07001551void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
1553 mempool_destroy(nfs_commit_mempool);
1554 mempool_destroy(nfs_wdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001555 kmem_cache_destroy(nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557