blob: bd4dff9dbd694b55a3e432483f16ccf73a3528f7 [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 Myklebust788e7a82006-03-20 13:44:27 -050084static const struct rpc_call_ops nfs_write_partial_ops;
85static const struct rpc_call_ops nfs_write_full_ops;
86static const struct rpc_call_ops nfs_commit_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88static kmem_cache_t *nfs_wdata_cachep;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050089static mempool_t *nfs_wdata_mempool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static mempool_t *nfs_commit_mempool;
91
92static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
93
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070094struct nfs_write_data *nfs_commit_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS);
Chuck Lever40859d72005-11-30 18:09:02 -050097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (p) {
99 memset(p, 0, sizeof(*p));
100 INIT_LIST_HEAD(&p->pages);
101 }
102 return p;
103}
104
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500105void nfs_commit_rcu_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500107 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
Chuck Lever40859d72005-11-30 18:09:02 -0500108 if (p && (p->pagevec != &p->page_array[0]))
109 kfree(p->pagevec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 mempool_free(p, nfs_commit_mempool);
111}
112
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500113void nfs_commit_free(struct nfs_write_data *wdata)
114{
115 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
116}
117
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700118struct nfs_write_data *nfs_writedata_alloc(size_t len)
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500119{
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700120 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500121 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
122
123 if (p) {
124 memset(p, 0, sizeof(*p));
125 INIT_LIST_HEAD(&p->pages);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700126 p->npages = pagecount;
Chuck Lever0d0b5cb2006-05-25 01:40:53 -0400127 if (pagecount <= ARRAY_SIZE(p->page_array))
128 p->pagevec = p->page_array;
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500129 else {
Chuck Lever0d0b5cb2006-05-25 01:40:53 -0400130 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
131 if (!p->pagevec) {
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500132 mempool_free(p, nfs_wdata_mempool);
133 p = NULL;
134 }
135 }
136 }
137 return p;
138}
139
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500140static void nfs_writedata_rcu_free(struct rcu_head *head)
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500141{
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500142 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500143 if (p && (p->pagevec != &p->page_array[0]))
144 kfree(p->pagevec);
145 mempool_free(p, nfs_wdata_mempool);
146}
147
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500148static void nfs_writedata_free(struct nfs_write_data *wdata)
149{
150 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
151}
152
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100153void nfs_writedata_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 nfs_writedata_free(wdata);
156}
157
Trond Myklebust277459d2006-12-05 00:35:35 -0500158static struct nfs_page *nfs_page_find_request_locked(struct page *page)
159{
160 struct nfs_page *req = NULL;
161
162 if (PagePrivate(page)) {
163 req = (struct nfs_page *)page_private(page);
164 if (req != NULL)
165 atomic_inc(&req->wb_count);
166 }
167 return req;
168}
169
170static struct nfs_page *nfs_page_find_request(struct page *page)
171{
172 struct nfs_page *req = NULL;
173 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
174
175 spin_lock(req_lock);
176 req = nfs_page_find_request_locked(page);
177 spin_unlock(req_lock);
178 return req;
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/* Adjust the file length if we're writing beyond the end */
182static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
183{
184 struct inode *inode = page->mapping->host;
185 loff_t end, i_size = i_size_read(inode);
186 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
187
188 if (i_size > 0 && page->index < end_index)
189 return;
190 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
191 if (i_size >= end)
192 return;
Chuck Lever91d5b472006-03-20 13:44:14 -0500193 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 i_size_write(inode, end);
195}
196
197/* We can set the PG_uptodate flag if we see that a write request
198 * covers the full page.
199 */
200static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
201{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (PageUptodate(page))
203 return;
204 if (base != 0)
205 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500206 if (count != nfs_page_length(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500208 if (count != PAGE_CACHE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
Trond Myklebust49a70f22006-12-05 00:35:38 -0500210 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
Trond Myklebuste21195a2006-12-05 00:35:39 -0500213static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 unsigned int offset, unsigned int count)
215{
216 struct nfs_page *req;
Trond Myklebuste21195a2006-12-05 00:35:39 -0500217 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Trond Myklebuste21195a2006-12-05 00:35:39 -0500219 for (;;) {
220 req = nfs_update_request(ctx, page, offset, count);
221 if (!IS_ERR(req))
222 break;
223 ret = PTR_ERR(req);
224 if (ret != -EBUSY)
225 return ret;
226 ret = nfs_wb_page(page->mapping->host, page);
227 if (ret != 0)
228 return ret;
229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /* Update file length */
231 nfs_grow_file(page, offset, count);
232 /* Set the PG_uptodate flag? */
233 nfs_mark_uptodate(page, offset, count);
234 nfs_unlock_request(req);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100235 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238static int wb_priority(struct writeback_control *wbc)
239{
240 if (wbc->for_reclaim)
241 return FLUSH_HIGHPRI;
242 if (wbc->for_kupdate)
243 return FLUSH_LOWPRI;
244 return 0;
245}
246
247/*
Trond Myklebuste261f512006-12-05 00:35:41 -0500248 * Find an associated nfs write request, and prepare to flush it out
249 * Returns 1 if there was no write request, or if the request was
250 * already tagged by nfs_set_page_dirty.Returns 0 if the request
251 * was not tagged.
252 * May also return an error if the user signalled nfs_wait_on_request().
253 */
254static int nfs_page_mark_flush(struct page *page)
255{
256 struct nfs_page *req;
257 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
258 int ret;
259
260 spin_lock(req_lock);
261 for(;;) {
262 req = nfs_page_find_request_locked(page);
263 if (req == NULL) {
264 spin_unlock(req_lock);
265 return 1;
266 }
267 if (nfs_lock_request_dontget(req))
268 break;
269 /* Note: If we hold the page lock, as is the case in nfs_writepage,
270 * then the call to nfs_lock_request_dontget() will always
271 * succeed provided that someone hasn't already marked the
272 * request as dirty (in which case we don't care).
273 */
274 spin_unlock(req_lock);
275 ret = nfs_wait_on_request(req);
276 nfs_release_request(req);
277 if (ret != 0)
278 return ret;
279 spin_lock(req_lock);
280 }
281 spin_unlock(req_lock);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500282 if (test_and_set_bit(PG_FLUSHING, &req->wb_flags) == 0) {
Trond Myklebuste261f512006-12-05 00:35:41 -0500283 nfs_mark_request_dirty(req);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500284 set_page_writeback(page);
285 }
Trond Myklebuste261f512006-12-05 00:35:41 -0500286 ret = test_bit(PG_NEED_FLUSH, &req->wb_flags);
287 nfs_unlock_request(req);
288 return ret;
289}
290
291/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 * Write an mmapped page to the server.
293 */
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500294static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 struct nfs_open_context *ctx;
297 struct inode *inode = page->mapping->host;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500298 unsigned offset;
Trond Myklebuste261f512006-12-05 00:35:41 -0500299 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Chuck Lever91d5b472006-03-20 13:44:14 -0500301 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
302 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
303
Trond Myklebuste261f512006-12-05 00:35:41 -0500304 err = nfs_page_mark_flush(page);
305 if (err <= 0)
306 goto out;
307 err = 0;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500308 offset = nfs_page_length(page);
309 if (!offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 goto out;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500311
Trond Myklebustd5308382005-11-04 15:33:38 -0500312 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (ctx == NULL) {
314 err = -EBADF;
315 goto out;
316 }
Trond Myklebust200baa22006-12-05 00:35:40 -0500317 err = nfs_writepage_setup(ctx, page, 0, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 put_nfs_open_context(ctx);
Trond Myklebuste261f512006-12-05 00:35:41 -0500319 if (err != 0)
320 goto out;
321 err = nfs_page_mark_flush(page);
322 if (err > 0)
323 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324out:
Trond Myklebust200baa22006-12-05 00:35:40 -0500325 if (!wbc->for_writepages)
326 nfs_flush_mapping(page->mapping, wbc, wb_priority(wbc));
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500327 return err;
328}
329
330int nfs_writepage(struct page *page, struct writeback_control *wbc)
331{
332 int err;
333
334 err = nfs_writepage_locked(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return err;
337}
338
339/*
340 * Note: causes nfs_update_request() to block on the assumption
341 * that the writeback is generated due to memory pressure.
342 */
343int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
344{
345 struct backing_dev_info *bdi = mapping->backing_dev_info;
346 struct inode *inode = mapping->host;
347 int err;
348
Chuck Lever91d5b472006-03-20 13:44:14 -0500349 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 err = generic_writepages(mapping, wbc);
352 if (err)
353 return err;
354 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
355 if (wbc->nonblocking)
356 return 0;
357 nfs_wait_on_write_congestion(mapping, 0);
358 }
Trond Myklebust28c69252006-09-16 13:04:50 -0400359 err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (err < 0)
361 goto out;
Chuck Lever91d5b472006-03-20 13:44:14 -0500362 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
364 err = nfs_wait_on_requests(inode, 0, 0);
365 if (err < 0)
366 goto out;
367 }
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000368 err = nfs_commit_inode(inode, wb_priority(wbc));
Trond Myklebust3f442542006-09-17 14:46:44 -0400369 if (err > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371out:
372 clear_bit(BDI_write_congested, &bdi->state);
373 wake_up_all(&nfs_write_congestion);
Andrew Morton3fcfab12006-10-19 23:28:16 -0700374 congestion_end(WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return err;
376}
377
378/*
379 * Insert a write request into an inode
380 */
381static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
382{
383 struct nfs_inode *nfsi = NFS_I(inode);
384 int error;
385
386 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
387 BUG_ON(error == -EEXIST);
388 if (error)
389 return error;
390 if (!nfsi->npages) {
391 igrab(inode);
392 nfs_begin_data_update(inode);
393 if (nfs_have_delegation(inode, FMODE_WRITE))
394 nfsi->change_attr++;
395 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500396 SetPagePrivate(req->wb_page);
Trond Myklebust277459d2006-12-05 00:35:35 -0500397 set_page_private(req->wb_page, (unsigned long)req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 nfsi->npages++;
399 atomic_inc(&req->wb_count);
400 return 0;
401}
402
403/*
404 * Insert a write request into an inode
405 */
406static void nfs_inode_remove_request(struct nfs_page *req)
407{
408 struct inode *inode = req->wb_context->dentry->d_inode;
409 struct nfs_inode *nfsi = NFS_I(inode);
410
411 BUG_ON (!NFS_WBACK_BUSY(req));
412
413 spin_lock(&nfsi->req_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500414 set_page_private(req->wb_page, 0);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500415 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
417 nfsi->npages--;
418 if (!nfsi->npages) {
419 spin_unlock(&nfsi->req_lock);
Trond Myklebust951a1432005-06-22 17:16:30 +0000420 nfs_end_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 iput(inode);
422 } else
423 spin_unlock(&nfsi->req_lock);
424 nfs_clear_request(req);
425 nfs_release_request(req);
426}
427
428/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 * Add a request to the inode's dirty list.
430 */
431static void
432nfs_mark_request_dirty(struct nfs_page *req)
433{
434 struct inode *inode = req->wb_context->dentry->d_inode;
435 struct nfs_inode *nfsi = NFS_I(inode);
436
437 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000438 radix_tree_tag_set(&nfsi->nfs_page_tree,
439 req->wb_index, NFS_PAGE_TAG_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 nfs_list_add_request(req, &nfsi->dirty);
441 nfsi->ndirty++;
442 spin_unlock(&nfsi->req_lock);
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700443 inc_zone_page_state(req->wb_page, NR_FILE_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 mark_inode_dirty(inode);
445}
446
Trond Myklebust61822ab2006-12-05 00:35:42 -0500447static void
448nfs_redirty_request(struct nfs_page *req)
449{
450 clear_bit(PG_FLUSHING, &req->wb_flags);
451 __set_page_dirty_nobuffers(req->wb_page);
452}
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454/*
455 * Check if a request is dirty
456 */
457static inline int
458nfs_dirty_request(struct nfs_page *req)
459{
Trond Myklebuste261f512006-12-05 00:35:41 -0500460 return test_bit(PG_FLUSHING, &req->wb_flags) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
463#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
464/*
465 * Add a request to the inode's commit list.
466 */
467static void
468nfs_mark_request_commit(struct nfs_page *req)
469{
470 struct inode *inode = req->wb_context->dentry->d_inode;
471 struct nfs_inode *nfsi = NFS_I(inode);
472
473 spin_lock(&nfsi->req_lock);
474 nfs_list_add_request(req, &nfsi->commit);
475 nfsi->ncommit++;
476 spin_unlock(&nfsi->req_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700477 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 mark_inode_dirty(inode);
479}
480#endif
481
482/*
483 * Wait for a request to complete.
484 *
485 * Interruptible by signals only if mounted with intr flag.
486 */
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500487static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct nfs_inode *nfsi = NFS_I(inode);
490 struct nfs_page *req;
491 unsigned long idx_end, next;
492 unsigned int res = 0;
493 int error;
494
495 if (npages == 0)
496 idx_end = ~0;
497 else
498 idx_end = idx_start + npages - 1;
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 next = idx_start;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000501 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 -0700502 if (req->wb_index > idx_end)
503 break;
504
505 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000506 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 atomic_inc(&req->wb_count);
509 spin_unlock(&nfsi->req_lock);
510 error = nfs_wait_on_request(req);
511 nfs_release_request(req);
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500512 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (error < 0)
514 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 res++;
516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return res;
518}
519
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500520static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
521{
522 struct nfs_inode *nfsi = NFS_I(inode);
523 int ret;
524
525 spin_lock(&nfsi->req_lock);
526 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
527 spin_unlock(&nfsi->req_lock);
528 return ret;
529}
530
Trond Myklebust83715ad2006-07-05 13:17:12 -0400531static void nfs_cancel_dirty_list(struct list_head *head)
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400532{
533 struct nfs_page *req;
534 while(!list_empty(head)) {
535 req = nfs_list_entry(head->next);
536 nfs_list_remove_request(req);
537 nfs_inode_remove_request(req);
538 nfs_clear_page_writeback(req);
539 }
540}
541
Trond Myklebust83715ad2006-07-05 13:17:12 -0400542static void nfs_cancel_commit_list(struct list_head *head)
543{
544 struct nfs_page *req;
545
546 while(!list_empty(head)) {
547 req = nfs_list_entry(head->next);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700548 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400549 nfs_list_remove_request(req);
550 nfs_inode_remove_request(req);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700551 nfs_unlock_request(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400552 }
553}
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
556/*
557 * nfs_scan_commit - Scan an inode for commit requests
558 * @inode: NFS inode to scan
559 * @dst: destination list
560 * @idx_start: lower bound of page->index to scan.
561 * @npages: idx_start + npages sets the upper bound to scan.
562 *
563 * Moves requests from the inode's 'commit' request list.
564 * The requests are *not* checked to ensure that they form a contiguous set.
565 */
566static int
567nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
568{
569 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000570 int res = 0;
571
572 if (nfsi->ncommit != 0) {
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400573 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000574 nfsi->ncommit -= res;
575 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
576 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return res;
579}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500580#else
581static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
582{
583 return 0;
584}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585#endif
586
587static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
588{
589 struct backing_dev_info *bdi = mapping->backing_dev_info;
590 DEFINE_WAIT(wait);
591 int ret = 0;
592
593 might_sleep();
594
595 if (!bdi_write_congested(bdi))
596 return 0;
Chuck Lever91d5b472006-03-20 13:44:14 -0500597
598 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (intr) {
601 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
602 sigset_t oldset;
603
604 rpc_clnt_sigmask(clnt, &oldset);
605 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
606 if (bdi_write_congested(bdi)) {
607 if (signalled())
608 ret = -ERESTARTSYS;
609 else
610 schedule();
611 }
612 rpc_clnt_sigunmask(clnt, &oldset);
613 } else {
614 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
615 if (bdi_write_congested(bdi))
616 schedule();
617 }
618 finish_wait(&nfs_write_congestion, &wait);
619 return ret;
620}
621
622
623/*
624 * Try to update any existing write request, or create one if there is none.
625 * In order to match, the request's credentials must match those of
626 * the calling process.
627 *
628 * Note: Should always be called with the Page Lock held!
629 */
630static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
Trond Myklebuste21195a2006-12-05 00:35:39 -0500631 struct page *page, unsigned int offset, unsigned int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Trond Myklebuste21195a2006-12-05 00:35:39 -0500633 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 struct nfs_inode *nfsi = NFS_I(inode);
635 struct nfs_page *req, *new = NULL;
636 unsigned long rqend, end;
637
638 end = offset + bytes;
639
Trond Myklebuste21195a2006-12-05 00:35:39 -0500640 if (nfs_wait_on_write_congestion(page->mapping, NFS_SERVER(inode)->flags & NFS_MOUNT_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return ERR_PTR(-ERESTARTSYS);
642 for (;;) {
643 /* Loop over all inode entries and see if we find
644 * A request for the page we wish to update
645 */
646 spin_lock(&nfsi->req_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500647 req = nfs_page_find_request_locked(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (req) {
649 if (!nfs_lock_request_dontget(req)) {
650 int error;
Trond Myklebust277459d2006-12-05 00:35:35 -0500651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 spin_unlock(&nfsi->req_lock);
653 error = nfs_wait_on_request(req);
654 nfs_release_request(req);
Neil Brown1dd594b2006-03-20 13:44:04 -0500655 if (error < 0) {
656 if (new)
657 nfs_release_request(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return ERR_PTR(error);
Neil Brown1dd594b2006-03-20 13:44:04 -0500659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 continue;
661 }
662 spin_unlock(&nfsi->req_lock);
663 if (new)
664 nfs_release_request(new);
665 break;
666 }
667
668 if (new) {
669 int error;
670 nfs_lock_request_dontget(new);
671 error = nfs_inode_add_request(inode, new);
672 if (error) {
673 spin_unlock(&nfsi->req_lock);
674 nfs_unlock_request(new);
675 return ERR_PTR(error);
676 }
677 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return new;
679 }
680 spin_unlock(&nfsi->req_lock);
681
682 new = nfs_create_request(ctx, inode, page, offset, bytes);
683 if (IS_ERR(new))
684 return new;
685 }
686
687 /* We have a request for our page.
688 * If the creds don't match, or the
689 * page addresses don't match,
690 * tell the caller to wait on the conflicting
691 * request.
692 */
693 rqend = req->wb_offset + req->wb_bytes;
694 if (req->wb_context != ctx
695 || req->wb_page != page
696 || !nfs_dirty_request(req)
697 || offset > rqend || end < req->wb_offset) {
698 nfs_unlock_request(req);
699 return ERR_PTR(-EBUSY);
700 }
701
702 /* Okay, the request matches. Update the region */
703 if (offset < req->wb_offset) {
704 req->wb_offset = offset;
705 req->wb_pgbase = offset;
706 req->wb_bytes = rqend - req->wb_offset;
707 }
708
709 if (end > rqend)
710 req->wb_bytes = end - req->wb_offset;
711
712 return req;
713}
714
715int nfs_flush_incompatible(struct file *file, struct page *page)
716{
717 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 struct nfs_page *req;
Trond Myklebust1a545332006-12-05 00:35:40 -0500719 int do_flush, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 /*
721 * Look for a request corresponding to this page. If there
722 * is one, and it belongs to another file, we flush it out
723 * before we try to copy anything into the page. Do this
724 * due to the lack of an ACCESS-type call in NFSv2.
725 * Also do the same if we find a request from an existing
726 * dropped page.
727 */
Trond Myklebust1a545332006-12-05 00:35:40 -0500728 do {
729 req = nfs_page_find_request(page);
730 if (req == NULL)
731 return 0;
732 do_flush = req->wb_page != page || req->wb_context != ctx
Trond Myklebuste261f512006-12-05 00:35:41 -0500733 || !nfs_dirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 nfs_release_request(req);
Trond Myklebust1a545332006-12-05 00:35:40 -0500735 if (!do_flush)
736 return 0;
737 status = nfs_wb_page(page->mapping->host, page);
738 } while (status == 0);
739 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
742/*
743 * Update and possibly write a cached page of an NFS file.
744 *
745 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
746 * things with a page scheduled for an RPC call (e.g. invalidate it).
747 */
748int nfs_updatepage(struct file *file, struct page *page,
749 unsigned int offset, unsigned int count)
750{
751 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 int status = 0;
754
Chuck Lever91d5b472006-03-20 13:44:14 -0500755 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500758 file->f_dentry->d_parent->d_name.name,
759 file->f_dentry->d_name.name, count,
760 (long long)(page_offset(page) +offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 /* If we're not using byte range locks, and we know the page
763 * is entirely in cache, it may be more efficient to avoid
764 * fragmenting write requests.
765 */
Trond Myklebustab0a3db2005-06-22 17:16:30 +0000766 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
Trond Myklebust49a70f22006-12-05 00:35:38 -0500767 count = max(count + offset, nfs_page_length(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770
Trond Myklebuste21195a2006-12-05 00:35:39 -0500771 status = nfs_writepage_setup(ctx, page, offset, count);
Trond Myklebuste261f512006-12-05 00:35:41 -0500772 __set_page_dirty_nobuffers(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
775 status, (long long)i_size_read(inode));
776 if (status < 0)
777 ClearPageUptodate(page);
778 return status;
779}
780
781static void nfs_writepage_release(struct nfs_page *req)
782{
783 end_page_writeback(req->wb_page);
784
785#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
786 if (!PageError(req->wb_page)) {
787 if (NFS_NEED_RESCHED(req)) {
Trond Myklebust61822ab2006-12-05 00:35:42 -0500788 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 goto out;
790 } else if (NFS_NEED_COMMIT(req)) {
791 nfs_mark_request_commit(req);
792 goto out;
793 }
794 }
795 nfs_inode_remove_request(req);
796
797out:
798 nfs_clear_commit(req);
799 nfs_clear_reschedule(req);
800#else
801 nfs_inode_remove_request(req);
802#endif
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000803 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
806static inline int flush_task_priority(int how)
807{
808 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
809 case FLUSH_HIGHPRI:
810 return RPC_PRIORITY_HIGH;
811 case FLUSH_LOWPRI:
812 return RPC_PRIORITY_LOW;
813 }
814 return RPC_PRIORITY_NORMAL;
815}
816
817/*
818 * Set up the argument/result storage required for the RPC call.
819 */
820static void nfs_write_rpcsetup(struct nfs_page *req,
821 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500822 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 unsigned int count, unsigned int offset,
824 int how)
825{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -0500827 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 /* Set up the RPC argument and reply structs
830 * NB: take care not to mess about with data->commit et al. */
831
832 data->req = req;
833 data->inode = inode = req->wb_context->dentry->d_inode;
834 data->cred = req->wb_context->cred;
835
836 data->args.fh = NFS_FH(inode);
837 data->args.offset = req_offset(req) + offset;
838 data->args.pgbase = req->wb_pgbase + offset;
839 data->args.pages = data->pagevec;
840 data->args.count = count;
841 data->args.context = req->wb_context;
842
843 data->res.fattr = &data->fattr;
844 data->res.count = count;
845 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400846 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Trond Myklebust788e7a82006-03-20 13:44:27 -0500848 /* Set up the initial task struct. */
849 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
850 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 NFS_PROTO(inode)->write_setup(data, how);
852
853 data->task.tk_priority = flush_task_priority(how);
854 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500857 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 inode->i_sb->s_id,
859 (long long)NFS_FILEID(inode),
860 count,
861 (unsigned long long)data->args.offset);
862}
863
864static void nfs_execute_write(struct nfs_write_data *data)
865{
866 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
867 sigset_t oldset;
868
869 rpc_clnt_sigmask(clnt, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 rpc_execute(&data->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 rpc_clnt_sigunmask(clnt, &oldset);
872}
873
874/*
875 * Generate multiple small requests to write out a single
876 * contiguous dirty area on one page.
877 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500878static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 struct nfs_page *req = nfs_list_entry(head->next);
881 struct page *page = req->wb_page;
882 struct nfs_write_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700883 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
884 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 int requests = 0;
886 LIST_HEAD(list);
887
888 nfs_list_remove_request(req);
889
890 nbytes = req->wb_bytes;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700891 do {
892 size_t len = min(nbytes, wsize);
893
894 data = nfs_writedata_alloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (!data)
896 goto out_bad;
897 list_add(&data->pages, &list);
898 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700899 nbytes -= len;
900 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 atomic_set(&req->wb_complete, requests);
902
903 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 offset = 0;
905 nbytes = req->wb_bytes;
906 do {
907 data = list_entry(list.next, struct nfs_write_data, pages);
908 list_del_init(&data->pages);
909
910 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 if (nbytes > wsize) {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500913 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
914 wsize, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 offset += wsize;
916 nbytes -= wsize;
917 } else {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500918 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
919 nbytes, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 nbytes = 0;
921 }
922 nfs_execute_write(data);
923 } while (nbytes != 0);
924
925 return 0;
926
927out_bad:
928 while (!list_empty(&list)) {
929 data = list_entry(list.next, struct nfs_write_data, pages);
930 list_del(&data->pages);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500931 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
Trond Myklebust61822ab2006-12-05 00:35:42 -0500933 nfs_redirty_request(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000934 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return -ENOMEM;
936}
937
938/*
939 * Create an RPC task for the given write request and kick it.
940 * The page must have been locked by the caller.
941 *
942 * It may happen that the page we're passed is not marked dirty.
943 * This is the case if nfs_updatepage detects a conflicting request
944 * that has been written but not committed.
945 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500946static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
948 struct nfs_page *req;
949 struct page **pages;
950 struct nfs_write_data *data;
951 unsigned int count;
952
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700953 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (!data)
955 goto out_bad;
956
957 pages = data->pagevec;
958 count = 0;
959 while (!list_empty(head)) {
960 req = nfs_list_entry(head->next);
961 nfs_list_remove_request(req);
962 nfs_list_add_request(req, &data->pages);
963 ClearPageError(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 *pages++ = req->wb_page;
965 count += req->wb_bytes;
966 }
967 req = nfs_list_entry(data->pages.next);
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 /* Set up the argument struct */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500970 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 nfs_execute_write(data);
973 return 0;
974 out_bad:
975 while (!list_empty(head)) {
976 struct nfs_page *req = nfs_list_entry(head->next);
977 nfs_list_remove_request(req);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500978 nfs_redirty_request(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000979 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
981 return -ENOMEM;
982}
983
Trond Myklebust7d46a492006-03-20 13:44:50 -0500984static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 LIST_HEAD(one_request);
Trond Myklebust7d46a492006-03-20 13:44:50 -0500987 int (*flush_one)(struct inode *, struct list_head *, int);
988 struct nfs_page *req;
989 int wpages = NFS_SERVER(inode)->wpages;
990 int wsize = NFS_SERVER(inode)->wsize;
991 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Trond Myklebust7d46a492006-03-20 13:44:50 -0500993 flush_one = nfs_flush_one;
994 if (wsize < PAGE_CACHE_SIZE)
995 flush_one = nfs_flush_multi;
996 /* For single writes, FLUSH_STABLE is more efficient */
997 if (npages <= wpages && npages == NFS_I(inode)->npages
998 && nfs_list_entry(head->next)->wb_bytes <= wsize)
999 how |= FLUSH_STABLE;
1000
1001 do {
1002 nfs_coalesce_requests(head, &one_request, wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 req = nfs_list_entry(one_request.next);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001004 error = flush_one(inode, &one_request, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (error < 0)
Trond Myklebust7d46a492006-03-20 13:44:50 -05001006 goto out_err;
1007 } while (!list_empty(head));
1008 return 0;
1009out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 while (!list_empty(head)) {
1011 req = nfs_list_entry(head->next);
1012 nfs_list_remove_request(req);
Trond Myklebust61822ab2006-12-05 00:35:42 -05001013 nfs_redirty_request(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001014 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016 return error;
1017}
1018
1019/*
1020 * Handle a write reply that flushed part of a page.
1021 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001022static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001024 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 struct nfs_page *req = data->req;
1026 struct page *page = req->wb_page;
1027
1028 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1029 req->wb_context->dentry->d_inode->i_sb->s_id,
1030 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1031 req->wb_bytes,
1032 (long long)req_offset(req));
1033
Trond Myklebust788e7a82006-03-20 13:44:27 -05001034 if (nfs_writeback_done(task, data) != 0)
1035 return;
1036
1037 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 ClearPageUptodate(page);
1039 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001040 req->wb_context->error = task->tk_status;
1041 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 } else {
1043#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1044 if (data->verf.committed < NFS_FILE_SYNC) {
1045 if (!NFS_NEED_COMMIT(req)) {
1046 nfs_defer_commit(req);
1047 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1048 dprintk(" defer commit\n");
1049 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1050 nfs_defer_reschedule(req);
1051 dprintk(" server reboot detected\n");
1052 }
1053 } else
1054#endif
1055 dprintk(" OK\n");
1056 }
1057
1058 if (atomic_dec_and_test(&req->wb_complete))
1059 nfs_writepage_release(req);
1060}
1061
Trond Myklebust788e7a82006-03-20 13:44:27 -05001062static const struct rpc_call_ops nfs_write_partial_ops = {
1063 .rpc_call_done = nfs_writeback_done_partial,
1064 .rpc_release = nfs_writedata_release,
1065};
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067/*
1068 * Handle a write reply that flushes a whole page.
1069 *
1070 * FIXME: There is an inherent race with invalidate_inode_pages and
1071 * writebacks since the page->count is kept > 1 for as long
1072 * as the page has a write request pending.
1073 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001074static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001076 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 struct nfs_page *req;
1078 struct page *page;
1079
Trond Myklebust788e7a82006-03-20 13:44:27 -05001080 if (nfs_writeback_done(task, data) != 0)
1081 return;
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 /* Update attributes as result of writeback. */
1084 while (!list_empty(&data->pages)) {
1085 req = nfs_list_entry(data->pages.next);
1086 nfs_list_remove_request(req);
1087 page = req->wb_page;
1088
1089 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1090 req->wb_context->dentry->d_inode->i_sb->s_id,
1091 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1092 req->wb_bytes,
1093 (long long)req_offset(req));
1094
Trond Myklebust788e7a82006-03-20 13:44:27 -05001095 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 ClearPageUptodate(page);
1097 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001098 req->wb_context->error = task->tk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 end_page_writeback(page);
1100 nfs_inode_remove_request(req);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001101 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 goto next;
1103 }
1104 end_page_writeback(page);
1105
1106#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1107 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1108 nfs_inode_remove_request(req);
1109 dprintk(" OK\n");
1110 goto next;
1111 }
1112 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1113 nfs_mark_request_commit(req);
1114 dprintk(" marked for commit\n");
1115#else
1116 nfs_inode_remove_request(req);
1117#endif
1118 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001119 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121}
1122
Trond Myklebust788e7a82006-03-20 13:44:27 -05001123static const struct rpc_call_ops nfs_write_full_ops = {
1124 .rpc_call_done = nfs_writeback_done_full,
1125 .rpc_release = nfs_writedata_release,
1126};
1127
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129/*
1130 * This function is called when the WRITE call is complete.
1131 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001132int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 struct nfs_writeargs *argp = &data->args;
1135 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001136 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1139 task->tk_pid, task->tk_status);
1140
Chuck Leverf551e442006-09-20 14:33:04 -04001141 /*
1142 * ->write_done will attempt to use post-op attributes to detect
1143 * conflicting writes by other clients. A strict interpretation
1144 * of close-to-open would allow us to continue caching even if
1145 * another writer had changed the file, but some applications
1146 * depend on tighter cache coherency when writing.
1147 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001148 status = NFS_PROTO(data->inode)->write_done(task, data);
1149 if (status != 0)
1150 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001151 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1154 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1155 /* We tried a write call, but the server did not
1156 * commit data to stable storage even though we
1157 * requested it.
1158 * Note: There is a known bug in Tru64 < 5.0 in which
1159 * the server reports NFS_DATA_SYNC, but performs
1160 * NFS_FILE_SYNC. We therefore implement this checking
1161 * as a dprintk() in order to avoid filling syslog.
1162 */
1163 static unsigned long complain;
1164
1165 if (time_before(complain, jiffies)) {
1166 dprintk("NFS: faulty NFS server %s:"
1167 " (committed = %d) != (stable = %d)\n",
David Howells54ceac42006-08-22 20:06:13 -04001168 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 resp->verf->committed, argp->stable);
1170 complain = jiffies + 300 * HZ;
1171 }
1172 }
1173#endif
1174 /* Is this a short write? */
1175 if (task->tk_status >= 0 && resp->count < argp->count) {
1176 static unsigned long complain;
1177
Chuck Lever91d5b472006-03-20 13:44:14 -05001178 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 /* Has the server at least made some progress? */
1181 if (resp->count != 0) {
1182 /* Was this an NFSv2 write or an NFSv3 stable write? */
1183 if (resp->verf->committed != NFS_UNSTABLE) {
1184 /* Resend from where the server left off */
1185 argp->offset += resp->count;
1186 argp->pgbase += resp->count;
1187 argp->count -= resp->count;
1188 } else {
1189 /* Resend as a stable write in order to avoid
1190 * headaches in the case of a server crash.
1191 */
1192 argp->stable = NFS_FILE_SYNC;
1193 }
1194 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001195 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
1197 if (time_before(complain, jiffies)) {
1198 printk(KERN_WARNING
1199 "NFS: Server wrote zero bytes, expected %u.\n",
1200 argp->count);
1201 complain = jiffies + 300 * HZ;
1202 }
1203 /* Can't do anything about it except throw an error. */
1204 task->tk_status = -EIO;
1205 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001206 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
1209
1210#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001211void nfs_commit_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 nfs_commit_free(wdata);
1214}
1215
1216/*
1217 * Set up the argument/result storage required for the RPC call.
1218 */
1219static void nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001220 struct nfs_write_data *data,
1221 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001223 struct nfs_page *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001225 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 /* Set up the RPC argument and reply structs
1228 * NB: take care not to mess about with data->commit et al. */
1229
1230 list_splice_init(head, &data->pages);
1231 first = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 inode = first->wb_context->dentry->d_inode;
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 data->inode = inode;
1235 data->cred = first->wb_context->cred;
1236
1237 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001238 /* Note: we always request a commit of the entire inode */
1239 data->args.offset = 0;
1240 data->args.count = 0;
1241 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 data->res.fattr = &data->fattr;
1243 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001244 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001245
1246 /* Set up the initial task struct. */
1247 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1248 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 NFS_PROTO(inode)->commit_setup(data, how);
1250
1251 data->task.tk_priority = flush_task_priority(how);
1252 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Chuck Lever0bbacc42005-11-01 16:53:32 -05001254 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
1257/*
1258 * Commit dirty pages
1259 */
1260static int
Chuck Lever40859d72005-11-30 18:09:02 -05001261nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
1263 struct nfs_write_data *data;
1264 struct nfs_page *req;
1265
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001266 data = nfs_commit_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 if (!data)
1269 goto out_bad;
1270
1271 /* Set up the argument struct */
1272 nfs_commit_rpcsetup(head, data, how);
1273
1274 nfs_execute_write(data);
1275 return 0;
1276 out_bad:
1277 while (!list_empty(head)) {
1278 req = nfs_list_entry(head->next);
1279 nfs_list_remove_request(req);
1280 nfs_mark_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001281 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust5c2d97c2006-09-18 23:20:35 -04001282 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284 return -ENOMEM;
1285}
1286
1287/*
1288 * COMMIT call returned
1289 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001290static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001292 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1296 task->tk_pid, task->tk_status);
1297
Trond Myklebust788e7a82006-03-20 13:44:27 -05001298 /* Call the NFS version-specific code */
1299 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1300 return;
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 while (!list_empty(&data->pages)) {
1303 req = nfs_list_entry(data->pages.next);
1304 nfs_list_remove_request(req);
Christoph Lameterfd39fc82006-06-30 01:55:40 -07001305 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1308 req->wb_context->dentry->d_inode->i_sb->s_id,
1309 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1310 req->wb_bytes,
1311 (long long)req_offset(req));
1312 if (task->tk_status < 0) {
1313 req->wb_context->error = task->tk_status;
1314 nfs_inode_remove_request(req);
1315 dprintk(", error = %d\n", task->tk_status);
1316 goto next;
1317 }
1318
1319 /* Okay, COMMIT succeeded, apparently. Check the verifier
1320 * returned by the server against all stored verfs. */
1321 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1322 /* We have a match */
1323 nfs_inode_remove_request(req);
1324 dprintk(" OK\n");
1325 goto next;
1326 }
1327 /* We have a mismatch. Write the page again */
1328 dprintk(" mismatch\n");
Trond Myklebust61822ab2006-12-05 00:35:42 -05001329 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001331 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001334
1335static const struct rpc_call_ops nfs_commit_ops = {
1336 .rpc_call_done = nfs_commit_done,
1337 .rpc_release = nfs_commit_release,
1338};
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001339#else
1340static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1341{
1342 return 0;
1343}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344#endif
1345
Trond Myklebust3f442542006-09-17 14:46:44 -04001346static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Trond Myklebust28c69252006-09-16 13:04:50 -04001348 struct nfs_inode *nfsi = NFS_I(mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 LIST_HEAD(head);
Trond Myklebust3f442542006-09-17 14:46:44 -04001350 long res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 spin_lock(&nfsi->req_lock);
Trond Myklebust3f442542006-09-17 14:46:44 -04001353 res = nfs_scan_dirty(mapping, wbc, &head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 spin_unlock(&nfsi->req_lock);
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001355 if (res) {
Trond Myklebust28c69252006-09-16 13:04:50 -04001356 int error = nfs_flush_list(mapping->host, &head, res, how);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001357 if (error < 0)
1358 return error;
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return res;
1361}
1362
1363#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001364int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
1366 struct nfs_inode *nfsi = NFS_I(inode);
1367 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001368 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001371 res = nfs_scan_commit(inode, &head, 0, 0);
1372 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001374 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001375 if (error < 0)
1376 return error;
1377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return res;
1379}
1380#endif
1381
Trond Myklebust1c759502006-10-09 16:18:38 -04001382long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
Trond Myklebust1c759502006-10-09 16:18:38 -04001384 struct inode *inode = mapping->host;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001385 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c759502006-10-09 16:18:38 -04001386 unsigned long idx_start, idx_end;
1387 unsigned int npages = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001388 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001389 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebust3f442542006-09-17 14:46:44 -04001390 long pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Trond Myklebust1c759502006-10-09 16:18:38 -04001392 /* FIXME */
1393 if (wbc->range_cyclic)
1394 idx_start = 0;
1395 else {
1396 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1397 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1398 if (idx_end > idx_start) {
1399 unsigned long l_npages = 1 + idx_end - idx_start;
1400 npages = l_npages;
1401 if (sizeof(npages) != sizeof(l_npages) &&
1402 (unsigned long)npages != l_npages)
1403 npages = 0;
1404 }
1405 }
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001406 how &= ~FLUSH_NOCOMMIT;
1407 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 do {
Trond Myklebust1c759502006-10-09 16:18:38 -04001409 wbc->pages_skipped = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001410 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1411 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001412 continue;
Trond Myklebust1c759502006-10-09 16:18:38 -04001413 pages = nfs_scan_dirty(mapping, wbc, &head);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001414 if (pages != 0) {
1415 spin_unlock(&nfsi->req_lock);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001416 if (how & FLUSH_INVALIDATE) {
Trond Myklebust83715ad2006-07-05 13:17:12 -04001417 nfs_cancel_dirty_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001418 ret = pages;
1419 } else
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001420 ret = nfs_flush_list(inode, &head, pages, how);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001421 spin_lock(&nfsi->req_lock);
1422 continue;
1423 }
Trond Myklebust1c759502006-10-09 16:18:38 -04001424 if (wbc->pages_skipped != 0)
1425 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001426 if (nocommit)
1427 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001428 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebust1c759502006-10-09 16:18:38 -04001429 if (pages == 0) {
1430 if (wbc->pages_skipped != 0)
1431 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001432 break;
Trond Myklebust1c759502006-10-09 16:18:38 -04001433 }
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001434 if (how & FLUSH_INVALIDATE) {
1435 spin_unlock(&nfsi->req_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001436 nfs_cancel_commit_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001437 ret = pages;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001438 spin_lock(&nfsi->req_lock);
1439 continue;
1440 }
1441 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001442 spin_unlock(&nfsi->req_lock);
1443 ret = nfs_commit_list(inode, &head, how);
1444 spin_lock(&nfsi->req_lock);
1445 } while (ret >= 0);
1446 spin_unlock(&nfsi->req_lock);
1447 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448}
1449
Trond Myklebust1c759502006-10-09 16:18:38 -04001450/*
1451 * flush the inode to disk.
1452 */
1453int nfs_wb_all(struct inode *inode)
1454{
1455 struct address_space *mapping = inode->i_mapping;
1456 struct writeback_control wbc = {
1457 .bdi = mapping->backing_dev_info,
1458 .sync_mode = WB_SYNC_ALL,
1459 .nr_to_write = LONG_MAX,
Trond Myklebust61822ab2006-12-05 00:35:42 -05001460 .for_writepages = 1,
Trond Myklebust1c759502006-10-09 16:18:38 -04001461 .range_cyclic = 1,
1462 };
1463 int ret;
1464
Trond Myklebust61822ab2006-12-05 00:35:42 -05001465 ret = generic_writepages(mapping, &wbc);
1466 if (ret < 0)
1467 goto out;
Trond Myklebust1c759502006-10-09 16:18:38 -04001468 ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
1469 if (ret >= 0)
1470 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001471out:
Trond Myklebust1c759502006-10-09 16:18:38 -04001472 return ret;
1473}
1474
1475int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
1476{
1477 struct writeback_control wbc = {
1478 .bdi = mapping->backing_dev_info,
1479 .sync_mode = WB_SYNC_ALL,
1480 .nr_to_write = LONG_MAX,
1481 .range_start = range_start,
1482 .range_end = range_end,
Trond Myklebust61822ab2006-12-05 00:35:42 -05001483 .for_writepages = 1,
Trond Myklebust1c759502006-10-09 16:18:38 -04001484 };
1485 int ret;
1486
Trond Myklebust61822ab2006-12-05 00:35:42 -05001487 if (!(how & FLUSH_NOWRITEPAGE)) {
1488 ret = generic_writepages(mapping, &wbc);
1489 if (ret < 0)
1490 goto out;
1491 }
Trond Myklebust1c759502006-10-09 16:18:38 -04001492 ret = nfs_sync_mapping_wait(mapping, &wbc, how);
1493 if (ret >= 0)
1494 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001495out:
Trond Myklebust1c759502006-10-09 16:18:38 -04001496 return ret;
1497}
1498
Trond Myklebust61822ab2006-12-05 00:35:42 -05001499int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
Trond Myklebust1c759502006-10-09 16:18:38 -04001500{
1501 loff_t range_start = page_offset(page);
1502 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001503 struct writeback_control wbc = {
1504 .bdi = page->mapping->backing_dev_info,
1505 .sync_mode = WB_SYNC_ALL,
1506 .nr_to_write = LONG_MAX,
1507 .range_start = range_start,
1508 .range_end = range_end,
1509 };
1510 int ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001511
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001512 BUG_ON(!PageLocked(page));
1513 if (!(how & FLUSH_NOWRITEPAGE) && clear_page_dirty_for_io(page)) {
1514 ret = nfs_writepage_locked(page, &wbc);
1515 if (ret < 0)
1516 goto out;
1517 }
1518 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1519 if (ret >= 0)
1520 return 0;
1521out:
1522 return ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001523}
1524
1525/*
1526 * Write back all requests on one page - we do this before reading it.
1527 */
1528int nfs_wb_page(struct inode *inode, struct page* page)
1529{
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001530 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
Trond Myklebust1c759502006-10-09 16:18:38 -04001531}
1532
Trond Myklebust1a545332006-12-05 00:35:40 -05001533int nfs_set_page_dirty(struct page *page)
1534{
1535 struct nfs_page *req;
1536
1537 req = nfs_page_find_request(page);
1538 if (req != NULL) {
1539 /* Mark any existing write requests for flushing */
1540 set_bit(PG_NEED_FLUSH, &req->wb_flags);
1541 nfs_release_request(req);
1542 }
1543 return __set_page_dirty_nobuffers(page);
1544}
1545
Trond Myklebust1c759502006-10-09 16:18:38 -04001546
David Howellsf7b422b2006-06-09 09:34:33 -04001547int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
1549 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1550 sizeof(struct nfs_write_data),
1551 0, SLAB_HWCACHE_ALIGN,
1552 NULL, NULL);
1553 if (nfs_wdata_cachep == NULL)
1554 return -ENOMEM;
1555
Matthew Dobson93d23412006-03-26 01:37:50 -08001556 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1557 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if (nfs_wdata_mempool == NULL)
1559 return -ENOMEM;
1560
Matthew Dobson93d23412006-03-26 01:37:50 -08001561 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1562 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 if (nfs_commit_mempool == NULL)
1564 return -ENOMEM;
1565
1566 return 0;
1567}
1568
David Brownell266bee82006-06-27 12:59:15 -07001569void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
1571 mempool_destroy(nfs_commit_mempool);
1572 mempool_destroy(nfs_wdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001573 kmem_cache_destroy(nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575