blob: de9a16a8f7e4bdcf0ea6d4ec4ece01c0e7ff57f2 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static int nfs_wait_on_write_congestion(struct address_space *, int);
81static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int);
Trond Myklebust3f442542006-09-17 14:46:44 -040082static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how);
Trond Myklebust1c759502006-10-09 16:18:38 -040083static int nfs_wb_page_priority(struct inode *inode, struct page *page, 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
213/*
214 * Write a page synchronously.
215 * Offset is the data offset within the page.
216 */
Trond Myklebuste21195a2006-12-05 00:35:39 -0500217static int nfs_writepage_sync(struct nfs_open_context *ctx, struct page *page,
218 unsigned int offset, unsigned int count, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Trond Myklebuste21195a2006-12-05 00:35:39 -0500220 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 unsigned int wsize = NFS_SERVER(inode)->wsize;
222 int result, written = 0;
223 struct nfs_write_data *wdata;
224
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700225 wdata = nfs_writedata_alloc(wsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (!wdata)
227 return -ENOMEM;
228
229 wdata->flags = how;
230 wdata->cred = ctx->cred;
231 wdata->inode = inode;
232 wdata->args.fh = NFS_FH(inode);
233 wdata->args.context = ctx;
234 wdata->args.pages = &page;
235 wdata->args.stable = NFS_FILE_SYNC;
236 wdata->args.pgbase = offset;
237 wdata->args.count = wsize;
238 wdata->res.fattr = &wdata->fattr;
239 wdata->res.verf = &wdata->verf;
240
241 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
242 inode->i_sb->s_id,
243 (long long)NFS_FILEID(inode),
244 count, (long long)(page_offset(page) + offset));
245
Trond Myklebustbb713d62005-12-03 15:20:14 -0500246 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 nfs_begin_data_update(inode);
248 do {
249 if (count < wsize)
250 wdata->args.count = count;
251 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
252
253 result = NFS_PROTO(inode)->write(wdata);
254
255 if (result < 0) {
256 /* Must mark the page invalid after I/O error */
257 ClearPageUptodate(page);
258 goto io_error;
259 }
260 if (result < wdata->args.count)
261 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
262 wdata->args.count, result);
263
264 wdata->args.offset += result;
265 wdata->args.pgbase += result;
266 written += result;
267 count -= result;
Chuck Lever91d5b472006-03-20 13:44:14 -0500268 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 } while (count);
270 /* Update file length */
271 nfs_grow_file(page, offset, written);
272 /* Set the PG_uptodate flag? */
273 nfs_mark_uptodate(page, offset, written);
274
275 if (PageError(page))
276 ClearPageError(page);
277
278io_error:
Trond Myklebust951a1432005-06-22 17:16:30 +0000279 nfs_end_data_update(inode);
Trond Myklebustbb713d62005-12-03 15:20:14 -0500280 end_page_writeback(page);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500281 nfs_writedata_release(wdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return written ? written : result;
283}
284
Trond Myklebuste21195a2006-12-05 00:35:39 -0500285static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 unsigned int offset, unsigned int count)
287{
288 struct nfs_page *req;
Trond Myklebuste21195a2006-12-05 00:35:39 -0500289 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Trond Myklebuste21195a2006-12-05 00:35:39 -0500291 for (;;) {
292 req = nfs_update_request(ctx, page, offset, count);
293 if (!IS_ERR(req))
294 break;
295 ret = PTR_ERR(req);
296 if (ret != -EBUSY)
297 return ret;
298 ret = nfs_wb_page(page->mapping->host, page);
299 if (ret != 0)
300 return ret;
301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 /* Update file length */
303 nfs_grow_file(page, offset, count);
304 /* Set the PG_uptodate flag? */
305 nfs_mark_uptodate(page, offset, count);
306 nfs_unlock_request(req);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100307 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
310static int wb_priority(struct writeback_control *wbc)
311{
312 if (wbc->for_reclaim)
313 return FLUSH_HIGHPRI;
314 if (wbc->for_kupdate)
315 return FLUSH_LOWPRI;
316 return 0;
317}
318
319/*
320 * Write an mmapped page to the server.
321 */
322int nfs_writepage(struct page *page, struct writeback_control *wbc)
323{
324 struct nfs_open_context *ctx;
325 struct inode *inode = page->mapping->host;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500326 unsigned offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 int err;
328
Chuck Lever91d5b472006-03-20 13:44:14 -0500329 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
330 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 /* Ensure we've flushed out any previous writes */
Trond Myklebuste21195a2006-12-05 00:35:39 -0500333 nfs_wb_page_priority(inode, page, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Trond Myklebust49a70f22006-12-05 00:35:38 -0500335 err = 0;
336 offset = nfs_page_length(page);
337 if (!offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 goto out;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500339
Trond Myklebustd5308382005-11-04 15:33:38 -0500340 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (ctx == NULL) {
342 err = -EBADF;
343 goto out;
344 }
345 lock_kernel();
Trond Myklebust87a4ce12006-12-05 00:35:39 -0500346 if (!IS_SYNC(inode)) {
Trond Myklebuste21195a2006-12-05 00:35:39 -0500347 err = nfs_writepage_setup(ctx, page, 0, offset);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100348 if (!wbc->for_writepages)
Trond Myklebust28c69252006-09-16 13:04:50 -0400349 nfs_flush_mapping(page->mapping, wbc, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 } else {
Trond Myklebuste21195a2006-12-05 00:35:39 -0500351 err = nfs_writepage_sync(ctx, page, 0, offset, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (err >= 0) {
353 if (err != offset)
354 redirty_page_for_writepage(wbc, page);
355 err = 0;
356 }
357 }
358 unlock_kernel();
359 put_nfs_open_context(ctx);
360out:
361 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return err;
363}
364
365/*
366 * Note: causes nfs_update_request() to block on the assumption
367 * that the writeback is generated due to memory pressure.
368 */
369int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
370{
371 struct backing_dev_info *bdi = mapping->backing_dev_info;
372 struct inode *inode = mapping->host;
373 int err;
374
Chuck Lever91d5b472006-03-20 13:44:14 -0500375 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 err = generic_writepages(mapping, wbc);
378 if (err)
379 return err;
380 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
381 if (wbc->nonblocking)
382 return 0;
383 nfs_wait_on_write_congestion(mapping, 0);
384 }
Trond Myklebust28c69252006-09-16 13:04:50 -0400385 err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (err < 0)
387 goto out;
Chuck Lever91d5b472006-03-20 13:44:14 -0500388 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
390 err = nfs_wait_on_requests(inode, 0, 0);
391 if (err < 0)
392 goto out;
393 }
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000394 err = nfs_commit_inode(inode, wb_priority(wbc));
Trond Myklebust3f442542006-09-17 14:46:44 -0400395 if (err > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397out:
398 clear_bit(BDI_write_congested, &bdi->state);
399 wake_up_all(&nfs_write_congestion);
Andrew Morton3fcfab12006-10-19 23:28:16 -0700400 congestion_end(WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return err;
402}
403
404/*
405 * Insert a write request into an inode
406 */
407static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
408{
409 struct nfs_inode *nfsi = NFS_I(inode);
410 int error;
411
412 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
413 BUG_ON(error == -EEXIST);
414 if (error)
415 return error;
416 if (!nfsi->npages) {
417 igrab(inode);
418 nfs_begin_data_update(inode);
419 if (nfs_have_delegation(inode, FMODE_WRITE))
420 nfsi->change_attr++;
421 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500422 SetPagePrivate(req->wb_page);
Trond Myklebust277459d2006-12-05 00:35:35 -0500423 set_page_private(req->wb_page, (unsigned long)req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 nfsi->npages++;
425 atomic_inc(&req->wb_count);
426 return 0;
427}
428
429/*
430 * Insert a write request into an inode
431 */
432static void nfs_inode_remove_request(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 BUG_ON (!NFS_WBACK_BUSY(req));
438
439 spin_lock(&nfsi->req_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500440 set_page_private(req->wb_page, 0);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500441 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
443 nfsi->npages--;
444 if (!nfsi->npages) {
445 spin_unlock(&nfsi->req_lock);
Trond Myklebust951a1432005-06-22 17:16:30 +0000446 nfs_end_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 iput(inode);
448 } else
449 spin_unlock(&nfsi->req_lock);
450 nfs_clear_request(req);
451 nfs_release_request(req);
452}
453
454/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 * Add a request to the inode's dirty list.
456 */
457static void
458nfs_mark_request_dirty(struct nfs_page *req)
459{
460 struct inode *inode = req->wb_context->dentry->d_inode;
461 struct nfs_inode *nfsi = NFS_I(inode);
462
463 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000464 radix_tree_tag_set(&nfsi->nfs_page_tree,
465 req->wb_index, NFS_PAGE_TAG_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 nfs_list_add_request(req, &nfsi->dirty);
467 nfsi->ndirty++;
468 spin_unlock(&nfsi->req_lock);
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700469 inc_zone_page_state(req->wb_page, NR_FILE_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 mark_inode_dirty(inode);
471}
472
473/*
474 * Check if a request is dirty
475 */
476static inline int
477nfs_dirty_request(struct nfs_page *req)
478{
479 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
480 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
481}
482
483#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
484/*
485 * Add a request to the inode's commit list.
486 */
487static void
488nfs_mark_request_commit(struct nfs_page *req)
489{
490 struct inode *inode = req->wb_context->dentry->d_inode;
491 struct nfs_inode *nfsi = NFS_I(inode);
492
493 spin_lock(&nfsi->req_lock);
494 nfs_list_add_request(req, &nfsi->commit);
495 nfsi->ncommit++;
496 spin_unlock(&nfsi->req_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700497 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 mark_inode_dirty(inode);
499}
500#endif
501
502/*
503 * Wait for a request to complete.
504 *
505 * Interruptible by signals only if mounted with intr flag.
506 */
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500507static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 struct nfs_inode *nfsi = NFS_I(inode);
510 struct nfs_page *req;
511 unsigned long idx_end, next;
512 unsigned int res = 0;
513 int error;
514
515 if (npages == 0)
516 idx_end = ~0;
517 else
518 idx_end = idx_start + npages - 1;
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 next = idx_start;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000521 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 -0700522 if (req->wb_index > idx_end)
523 break;
524
525 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000526 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 atomic_inc(&req->wb_count);
529 spin_unlock(&nfsi->req_lock);
530 error = nfs_wait_on_request(req);
531 nfs_release_request(req);
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500532 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (error < 0)
534 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 res++;
536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return res;
538}
539
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500540static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
541{
542 struct nfs_inode *nfsi = NFS_I(inode);
543 int ret;
544
545 spin_lock(&nfsi->req_lock);
546 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
547 spin_unlock(&nfsi->req_lock);
548 return ret;
549}
550
Trond Myklebust83715ad2006-07-05 13:17:12 -0400551static void nfs_cancel_dirty_list(struct list_head *head)
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400552{
553 struct nfs_page *req;
554 while(!list_empty(head)) {
555 req = nfs_list_entry(head->next);
556 nfs_list_remove_request(req);
557 nfs_inode_remove_request(req);
558 nfs_clear_page_writeback(req);
559 }
560}
561
Trond Myklebust83715ad2006-07-05 13:17:12 -0400562static void nfs_cancel_commit_list(struct list_head *head)
563{
564 struct nfs_page *req;
565
566 while(!list_empty(head)) {
567 req = nfs_list_entry(head->next);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700568 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400569 nfs_list_remove_request(req);
570 nfs_inode_remove_request(req);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700571 nfs_unlock_request(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400572 }
573}
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
576/*
577 * nfs_scan_commit - Scan an inode for commit requests
578 * @inode: NFS inode to scan
579 * @dst: destination list
580 * @idx_start: lower bound of page->index to scan.
581 * @npages: idx_start + npages sets the upper bound to scan.
582 *
583 * Moves requests from the inode's 'commit' request list.
584 * The requests are *not* checked to ensure that they form a contiguous set.
585 */
586static int
587nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
588{
589 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000590 int res = 0;
591
592 if (nfsi->ncommit != 0) {
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400593 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000594 nfsi->ncommit -= res;
595 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
596 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return res;
599}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500600#else
601static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
602{
603 return 0;
604}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605#endif
606
607static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
608{
609 struct backing_dev_info *bdi = mapping->backing_dev_info;
610 DEFINE_WAIT(wait);
611 int ret = 0;
612
613 might_sleep();
614
615 if (!bdi_write_congested(bdi))
616 return 0;
Chuck Lever91d5b472006-03-20 13:44:14 -0500617
618 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (intr) {
621 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
622 sigset_t oldset;
623
624 rpc_clnt_sigmask(clnt, &oldset);
625 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
626 if (bdi_write_congested(bdi)) {
627 if (signalled())
628 ret = -ERESTARTSYS;
629 else
630 schedule();
631 }
632 rpc_clnt_sigunmask(clnt, &oldset);
633 } else {
634 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
635 if (bdi_write_congested(bdi))
636 schedule();
637 }
638 finish_wait(&nfs_write_congestion, &wait);
639 return ret;
640}
641
642
643/*
644 * Try to update any existing write request, or create one if there is none.
645 * In order to match, the request's credentials must match those of
646 * the calling process.
647 *
648 * Note: Should always be called with the Page Lock held!
649 */
650static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
Trond Myklebuste21195a2006-12-05 00:35:39 -0500651 struct page *page, unsigned int offset, unsigned int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Trond Myklebuste21195a2006-12-05 00:35:39 -0500653 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 struct nfs_inode *nfsi = NFS_I(inode);
655 struct nfs_page *req, *new = NULL;
656 unsigned long rqend, end;
657
658 end = offset + bytes;
659
Trond Myklebuste21195a2006-12-05 00:35:39 -0500660 if (nfs_wait_on_write_congestion(page->mapping, NFS_SERVER(inode)->flags & NFS_MOUNT_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return ERR_PTR(-ERESTARTSYS);
662 for (;;) {
663 /* Loop over all inode entries and see if we find
664 * A request for the page we wish to update
665 */
666 spin_lock(&nfsi->req_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500667 req = nfs_page_find_request_locked(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (req) {
669 if (!nfs_lock_request_dontget(req)) {
670 int error;
Trond Myklebust277459d2006-12-05 00:35:35 -0500671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 spin_unlock(&nfsi->req_lock);
673 error = nfs_wait_on_request(req);
674 nfs_release_request(req);
Neil Brown1dd594b2006-03-20 13:44:04 -0500675 if (error < 0) {
676 if (new)
677 nfs_release_request(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return ERR_PTR(error);
Neil Brown1dd594b2006-03-20 13:44:04 -0500679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 continue;
681 }
682 spin_unlock(&nfsi->req_lock);
683 if (new)
684 nfs_release_request(new);
685 break;
686 }
687
688 if (new) {
689 int error;
690 nfs_lock_request_dontget(new);
691 error = nfs_inode_add_request(inode, new);
692 if (error) {
693 spin_unlock(&nfsi->req_lock);
694 nfs_unlock_request(new);
695 return ERR_PTR(error);
696 }
697 spin_unlock(&nfsi->req_lock);
698 nfs_mark_request_dirty(new);
699 return new;
700 }
701 spin_unlock(&nfsi->req_lock);
702
703 new = nfs_create_request(ctx, inode, page, offset, bytes);
704 if (IS_ERR(new))
705 return new;
706 }
707
708 /* We have a request for our page.
709 * If the creds don't match, or the
710 * page addresses don't match,
711 * tell the caller to wait on the conflicting
712 * request.
713 */
714 rqend = req->wb_offset + req->wb_bytes;
715 if (req->wb_context != ctx
716 || req->wb_page != page
717 || !nfs_dirty_request(req)
718 || offset > rqend || end < req->wb_offset) {
719 nfs_unlock_request(req);
720 return ERR_PTR(-EBUSY);
721 }
722
723 /* Okay, the request matches. Update the region */
724 if (offset < req->wb_offset) {
725 req->wb_offset = offset;
726 req->wb_pgbase = offset;
727 req->wb_bytes = rqend - req->wb_offset;
728 }
729
730 if (end > rqend)
731 req->wb_bytes = end - req->wb_offset;
732
733 return req;
734}
735
736int nfs_flush_incompatible(struct file *file, struct page *page)
737{
738 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 struct nfs_page *req;
740 int status = 0;
741 /*
742 * Look for a request corresponding to this page. If there
743 * is one, and it belongs to another file, we flush it out
744 * before we try to copy anything into the page. Do this
745 * due to the lack of an ACCESS-type call in NFSv2.
746 * Also do the same if we find a request from an existing
747 * dropped page.
748 */
Trond Myklebust277459d2006-12-05 00:35:35 -0500749 req = nfs_page_find_request(page);
750 if (req != NULL) {
751 int do_flush = req->wb_page != page || req->wb_context != ctx;
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 nfs_release_request(req);
Trond Myklebust277459d2006-12-05 00:35:35 -0500754 if (do_flush)
755 status = nfs_wb_page(page->mapping->host, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
757 return (status < 0) ? status : 0;
758}
759
760/*
761 * Update and possibly write a cached page of an NFS file.
762 *
763 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
764 * things with a page scheduled for an RPC call (e.g. invalidate it).
765 */
766int nfs_updatepage(struct file *file, struct page *page,
767 unsigned int offset, unsigned int count)
768{
769 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 int status = 0;
772
Chuck Lever91d5b472006-03-20 13:44:14 -0500773 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500776 file->f_dentry->d_parent->d_name.name,
777 file->f_dentry->d_name.name, count,
778 (long long)(page_offset(page) +offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 if (IS_SYNC(inode)) {
Trond Myklebuste21195a2006-12-05 00:35:39 -0500781 status = nfs_writepage_sync(ctx, page, offset, count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (status > 0) {
783 if (offset == 0 && status == PAGE_CACHE_SIZE)
784 SetPageUptodate(page);
785 return 0;
786 }
787 return status;
788 }
789
790 /* If we're not using byte range locks, and we know the page
791 * is entirely in cache, it may be more efficient to avoid
792 * fragmenting write requests.
793 */
Trond Myklebustab0a3db2005-06-22 17:16:30 +0000794 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
Trond Myklebust49a70f22006-12-05 00:35:38 -0500795 count = max(count + offset, nfs_page_length(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798
Trond Myklebuste21195a2006-12-05 00:35:39 -0500799 status = nfs_writepage_setup(ctx, page, offset, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
802 status, (long long)i_size_read(inode));
803 if (status < 0)
804 ClearPageUptodate(page);
805 return status;
806}
807
808static void nfs_writepage_release(struct nfs_page *req)
809{
810 end_page_writeback(req->wb_page);
811
812#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
813 if (!PageError(req->wb_page)) {
814 if (NFS_NEED_RESCHED(req)) {
815 nfs_mark_request_dirty(req);
816 goto out;
817 } else if (NFS_NEED_COMMIT(req)) {
818 nfs_mark_request_commit(req);
819 goto out;
820 }
821 }
822 nfs_inode_remove_request(req);
823
824out:
825 nfs_clear_commit(req);
826 nfs_clear_reschedule(req);
827#else
828 nfs_inode_remove_request(req);
829#endif
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000830 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
833static inline int flush_task_priority(int how)
834{
835 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
836 case FLUSH_HIGHPRI:
837 return RPC_PRIORITY_HIGH;
838 case FLUSH_LOWPRI:
839 return RPC_PRIORITY_LOW;
840 }
841 return RPC_PRIORITY_NORMAL;
842}
843
844/*
845 * Set up the argument/result storage required for the RPC call.
846 */
847static void nfs_write_rpcsetup(struct nfs_page *req,
848 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500849 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 unsigned int count, unsigned int offset,
851 int how)
852{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -0500854 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 /* Set up the RPC argument and reply structs
857 * NB: take care not to mess about with data->commit et al. */
858
859 data->req = req;
860 data->inode = inode = req->wb_context->dentry->d_inode;
861 data->cred = req->wb_context->cred;
862
863 data->args.fh = NFS_FH(inode);
864 data->args.offset = req_offset(req) + offset;
865 data->args.pgbase = req->wb_pgbase + offset;
866 data->args.pages = data->pagevec;
867 data->args.count = count;
868 data->args.context = req->wb_context;
869
870 data->res.fattr = &data->fattr;
871 data->res.count = count;
872 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400873 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Trond Myklebust788e7a82006-03-20 13:44:27 -0500875 /* Set up the initial task struct. */
876 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
877 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 NFS_PROTO(inode)->write_setup(data, how);
879
880 data->task.tk_priority = flush_task_priority(how);
881 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500884 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 inode->i_sb->s_id,
886 (long long)NFS_FILEID(inode),
887 count,
888 (unsigned long long)data->args.offset);
889}
890
891static void nfs_execute_write(struct nfs_write_data *data)
892{
893 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
894 sigset_t oldset;
895
896 rpc_clnt_sigmask(clnt, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 rpc_execute(&data->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 rpc_clnt_sigunmask(clnt, &oldset);
899}
900
901/*
902 * Generate multiple small requests to write out a single
903 * contiguous dirty area on one page.
904 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500905static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 struct nfs_page *req = nfs_list_entry(head->next);
908 struct page *page = req->wb_page;
909 struct nfs_write_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700910 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
911 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 int requests = 0;
913 LIST_HEAD(list);
914
915 nfs_list_remove_request(req);
916
917 nbytes = req->wb_bytes;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700918 do {
919 size_t len = min(nbytes, wsize);
920
921 data = nfs_writedata_alloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (!data)
923 goto out_bad;
924 list_add(&data->pages, &list);
925 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700926 nbytes -= len;
927 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 atomic_set(&req->wb_complete, requests);
929
930 ClearPageError(page);
Trond Myklebustbb713d62005-12-03 15:20:14 -0500931 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 offset = 0;
933 nbytes = req->wb_bytes;
934 do {
935 data = list_entry(list.next, struct nfs_write_data, pages);
936 list_del_init(&data->pages);
937
938 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 if (nbytes > wsize) {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500941 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
942 wsize, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 offset += wsize;
944 nbytes -= wsize;
945 } else {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500946 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
947 nbytes, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 nbytes = 0;
949 }
950 nfs_execute_write(data);
951 } while (nbytes != 0);
952
953 return 0;
954
955out_bad:
956 while (!list_empty(&list)) {
957 data = list_entry(list.next, struct nfs_write_data, pages);
958 list_del(&data->pages);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500959 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000962 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return -ENOMEM;
964}
965
966/*
967 * Create an RPC task for the given write request and kick it.
968 * The page must have been locked by the caller.
969 *
970 * It may happen that the page we're passed is not marked dirty.
971 * This is the case if nfs_updatepage detects a conflicting request
972 * that has been written but not committed.
973 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500974static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 struct nfs_page *req;
977 struct page **pages;
978 struct nfs_write_data *data;
979 unsigned int count;
980
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700981 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (!data)
983 goto out_bad;
984
985 pages = data->pagevec;
986 count = 0;
987 while (!list_empty(head)) {
988 req = nfs_list_entry(head->next);
989 nfs_list_remove_request(req);
990 nfs_list_add_request(req, &data->pages);
991 ClearPageError(req->wb_page);
Trond Myklebustbb713d62005-12-03 15:20:14 -0500992 set_page_writeback(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 *pages++ = req->wb_page;
994 count += req->wb_bytes;
995 }
996 req = nfs_list_entry(data->pages.next);
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 /* Set up the argument struct */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500999 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 nfs_execute_write(data);
1002 return 0;
1003 out_bad:
1004 while (!list_empty(head)) {
1005 struct nfs_page *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 -ENOMEM;
1011}
1012
Trond Myklebust7d46a492006-03-20 13:44:50 -05001013static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
1015 LIST_HEAD(one_request);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001016 int (*flush_one)(struct inode *, struct list_head *, int);
1017 struct nfs_page *req;
1018 int wpages = NFS_SERVER(inode)->wpages;
1019 int wsize = NFS_SERVER(inode)->wsize;
1020 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Trond Myklebust7d46a492006-03-20 13:44:50 -05001022 flush_one = nfs_flush_one;
1023 if (wsize < PAGE_CACHE_SIZE)
1024 flush_one = nfs_flush_multi;
1025 /* For single writes, FLUSH_STABLE is more efficient */
1026 if (npages <= wpages && npages == NFS_I(inode)->npages
1027 && nfs_list_entry(head->next)->wb_bytes <= wsize)
1028 how |= FLUSH_STABLE;
1029
1030 do {
1031 nfs_coalesce_requests(head, &one_request, wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 req = nfs_list_entry(one_request.next);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001033 error = flush_one(inode, &one_request, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (error < 0)
Trond Myklebust7d46a492006-03-20 13:44:50 -05001035 goto out_err;
1036 } while (!list_empty(head));
1037 return 0;
1038out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 while (!list_empty(head)) {
1040 req = nfs_list_entry(head->next);
1041 nfs_list_remove_request(req);
1042 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001043 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045 return error;
1046}
1047
1048/*
1049 * Handle a write reply that flushed part of a page.
1050 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001051static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001053 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 struct nfs_page *req = data->req;
1055 struct page *page = req->wb_page;
1056
1057 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1058 req->wb_context->dentry->d_inode->i_sb->s_id,
1059 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1060 req->wb_bytes,
1061 (long long)req_offset(req));
1062
Trond Myklebust788e7a82006-03-20 13:44:27 -05001063 if (nfs_writeback_done(task, data) != 0)
1064 return;
1065
1066 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 ClearPageUptodate(page);
1068 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001069 req->wb_context->error = task->tk_status;
1070 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 } else {
1072#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1073 if (data->verf.committed < NFS_FILE_SYNC) {
1074 if (!NFS_NEED_COMMIT(req)) {
1075 nfs_defer_commit(req);
1076 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1077 dprintk(" defer commit\n");
1078 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1079 nfs_defer_reschedule(req);
1080 dprintk(" server reboot detected\n");
1081 }
1082 } else
1083#endif
1084 dprintk(" OK\n");
1085 }
1086
1087 if (atomic_dec_and_test(&req->wb_complete))
1088 nfs_writepage_release(req);
1089}
1090
Trond Myklebust788e7a82006-03-20 13:44:27 -05001091static const struct rpc_call_ops nfs_write_partial_ops = {
1092 .rpc_call_done = nfs_writeback_done_partial,
1093 .rpc_release = nfs_writedata_release,
1094};
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096/*
1097 * Handle a write reply that flushes a whole page.
1098 *
1099 * FIXME: There is an inherent race with invalidate_inode_pages and
1100 * writebacks since the page->count is kept > 1 for as long
1101 * as the page has a write request pending.
1102 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001103static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001105 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 struct nfs_page *req;
1107 struct page *page;
1108
Trond Myklebust788e7a82006-03-20 13:44:27 -05001109 if (nfs_writeback_done(task, data) != 0)
1110 return;
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 /* Update attributes as result of writeback. */
1113 while (!list_empty(&data->pages)) {
1114 req = nfs_list_entry(data->pages.next);
1115 nfs_list_remove_request(req);
1116 page = req->wb_page;
1117
1118 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1119 req->wb_context->dentry->d_inode->i_sb->s_id,
1120 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1121 req->wb_bytes,
1122 (long long)req_offset(req));
1123
Trond Myklebust788e7a82006-03-20 13:44:27 -05001124 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 ClearPageUptodate(page);
1126 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001127 req->wb_context->error = task->tk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 end_page_writeback(page);
1129 nfs_inode_remove_request(req);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001130 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 goto next;
1132 }
1133 end_page_writeback(page);
1134
1135#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1136 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1137 nfs_inode_remove_request(req);
1138 dprintk(" OK\n");
1139 goto next;
1140 }
1141 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1142 nfs_mark_request_commit(req);
1143 dprintk(" marked for commit\n");
1144#else
1145 nfs_inode_remove_request(req);
1146#endif
1147 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001148 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
1150}
1151
Trond Myklebust788e7a82006-03-20 13:44:27 -05001152static const struct rpc_call_ops nfs_write_full_ops = {
1153 .rpc_call_done = nfs_writeback_done_full,
1154 .rpc_release = nfs_writedata_release,
1155};
1156
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158/*
1159 * This function is called when the WRITE call is complete.
1160 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001161int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 struct nfs_writeargs *argp = &data->args;
1164 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001165 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1168 task->tk_pid, task->tk_status);
1169
Chuck Leverf551e442006-09-20 14:33:04 -04001170 /*
1171 * ->write_done will attempt to use post-op attributes to detect
1172 * conflicting writes by other clients. A strict interpretation
1173 * of close-to-open would allow us to continue caching even if
1174 * another writer had changed the file, but some applications
1175 * depend on tighter cache coherency when writing.
1176 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001177 status = NFS_PROTO(data->inode)->write_done(task, data);
1178 if (status != 0)
1179 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001180 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1183 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1184 /* We tried a write call, but the server did not
1185 * commit data to stable storage even though we
1186 * requested it.
1187 * Note: There is a known bug in Tru64 < 5.0 in which
1188 * the server reports NFS_DATA_SYNC, but performs
1189 * NFS_FILE_SYNC. We therefore implement this checking
1190 * as a dprintk() in order to avoid filling syslog.
1191 */
1192 static unsigned long complain;
1193
1194 if (time_before(complain, jiffies)) {
1195 dprintk("NFS: faulty NFS server %s:"
1196 " (committed = %d) != (stable = %d)\n",
David Howells54ceac42006-08-22 20:06:13 -04001197 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 resp->verf->committed, argp->stable);
1199 complain = jiffies + 300 * HZ;
1200 }
1201 }
1202#endif
1203 /* Is this a short write? */
1204 if (task->tk_status >= 0 && resp->count < argp->count) {
1205 static unsigned long complain;
1206
Chuck Lever91d5b472006-03-20 13:44:14 -05001207 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 /* Has the server at least made some progress? */
1210 if (resp->count != 0) {
1211 /* Was this an NFSv2 write or an NFSv3 stable write? */
1212 if (resp->verf->committed != NFS_UNSTABLE) {
1213 /* Resend from where the server left off */
1214 argp->offset += resp->count;
1215 argp->pgbase += resp->count;
1216 argp->count -= resp->count;
1217 } else {
1218 /* Resend as a stable write in order to avoid
1219 * headaches in the case of a server crash.
1220 */
1221 argp->stable = NFS_FILE_SYNC;
1222 }
1223 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001224 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
1226 if (time_before(complain, jiffies)) {
1227 printk(KERN_WARNING
1228 "NFS: Server wrote zero bytes, expected %u.\n",
1229 argp->count);
1230 complain = jiffies + 300 * HZ;
1231 }
1232 /* Can't do anything about it except throw an error. */
1233 task->tk_status = -EIO;
1234 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001235 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
1238
1239#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001240void nfs_commit_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 nfs_commit_free(wdata);
1243}
1244
1245/*
1246 * Set up the argument/result storage required for the RPC call.
1247 */
1248static void nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001249 struct nfs_write_data *data,
1250 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001252 struct nfs_page *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001254 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 /* Set up the RPC argument and reply structs
1257 * NB: take care not to mess about with data->commit et al. */
1258
1259 list_splice_init(head, &data->pages);
1260 first = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 inode = first->wb_context->dentry->d_inode;
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 data->inode = inode;
1264 data->cred = first->wb_context->cred;
1265
1266 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001267 /* Note: we always request a commit of the entire inode */
1268 data->args.offset = 0;
1269 data->args.count = 0;
1270 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 data->res.fattr = &data->fattr;
1272 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001273 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001274
1275 /* Set up the initial task struct. */
1276 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1277 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 NFS_PROTO(inode)->commit_setup(data, how);
1279
1280 data->task.tk_priority = flush_task_priority(how);
1281 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Chuck Lever0bbacc42005-11-01 16:53:32 -05001283 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
1286/*
1287 * Commit dirty pages
1288 */
1289static int
Chuck Lever40859d72005-11-30 18:09:02 -05001290nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
1292 struct nfs_write_data *data;
1293 struct nfs_page *req;
1294
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001295 data = nfs_commit_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 if (!data)
1298 goto out_bad;
1299
1300 /* Set up the argument struct */
1301 nfs_commit_rpcsetup(head, data, how);
1302
1303 nfs_execute_write(data);
1304 return 0;
1305 out_bad:
1306 while (!list_empty(head)) {
1307 req = nfs_list_entry(head->next);
1308 nfs_list_remove_request(req);
1309 nfs_mark_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001310 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust5c2d97c2006-09-18 23:20:35 -04001311 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
1313 return -ENOMEM;
1314}
1315
1316/*
1317 * COMMIT call returned
1318 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001319static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001321 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1325 task->tk_pid, task->tk_status);
1326
Trond Myklebust788e7a82006-03-20 13:44:27 -05001327 /* Call the NFS version-specific code */
1328 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1329 return;
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 while (!list_empty(&data->pages)) {
1332 req = nfs_list_entry(data->pages.next);
1333 nfs_list_remove_request(req);
Christoph Lameterfd39fc82006-06-30 01:55:40 -07001334 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1337 req->wb_context->dentry->d_inode->i_sb->s_id,
1338 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1339 req->wb_bytes,
1340 (long long)req_offset(req));
1341 if (task->tk_status < 0) {
1342 req->wb_context->error = task->tk_status;
1343 nfs_inode_remove_request(req);
1344 dprintk(", error = %d\n", task->tk_status);
1345 goto next;
1346 }
1347
1348 /* Okay, COMMIT succeeded, apparently. Check the verifier
1349 * returned by the server against all stored verfs. */
1350 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1351 /* We have a match */
1352 nfs_inode_remove_request(req);
1353 dprintk(" OK\n");
1354 goto next;
1355 }
1356 /* We have a mismatch. Write the page again */
1357 dprintk(" mismatch\n");
1358 nfs_mark_request_dirty(req);
1359 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001360 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001363
1364static const struct rpc_call_ops nfs_commit_ops = {
1365 .rpc_call_done = nfs_commit_done,
1366 .rpc_release = nfs_commit_release,
1367};
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001368#else
1369static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1370{
1371 return 0;
1372}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373#endif
1374
Trond Myklebust3f442542006-09-17 14:46:44 -04001375static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
Trond Myklebust28c69252006-09-16 13:04:50 -04001377 struct nfs_inode *nfsi = NFS_I(mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 LIST_HEAD(head);
Trond Myklebust3f442542006-09-17 14:46:44 -04001379 long res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 spin_lock(&nfsi->req_lock);
Trond Myklebust3f442542006-09-17 14:46:44 -04001382 res = nfs_scan_dirty(mapping, wbc, &head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 spin_unlock(&nfsi->req_lock);
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001384 if (res) {
Trond Myklebust28c69252006-09-16 13:04:50 -04001385 int error = nfs_flush_list(mapping->host, &head, res, how);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001386 if (error < 0)
1387 return error;
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 return res;
1390}
1391
1392#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001393int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
1395 struct nfs_inode *nfsi = NFS_I(inode);
1396 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001397 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001400 res = nfs_scan_commit(inode, &head, 0, 0);
1401 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001403 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001404 if (error < 0)
1405 return error;
1406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return res;
1408}
1409#endif
1410
Trond Myklebust1c759502006-10-09 16:18:38 -04001411long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
Trond Myklebust1c759502006-10-09 16:18:38 -04001413 struct inode *inode = mapping->host;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001414 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c759502006-10-09 16:18:38 -04001415 unsigned long idx_start, idx_end;
1416 unsigned int npages = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001417 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001418 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebust3f442542006-09-17 14:46:44 -04001419 long pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Trond Myklebust1c759502006-10-09 16:18:38 -04001421 /* FIXME */
1422 if (wbc->range_cyclic)
1423 idx_start = 0;
1424 else {
1425 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1426 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1427 if (idx_end > idx_start) {
1428 unsigned long l_npages = 1 + idx_end - idx_start;
1429 npages = l_npages;
1430 if (sizeof(npages) != sizeof(l_npages) &&
1431 (unsigned long)npages != l_npages)
1432 npages = 0;
1433 }
1434 }
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001435 how &= ~FLUSH_NOCOMMIT;
1436 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 do {
Trond Myklebust1c759502006-10-09 16:18:38 -04001438 wbc->pages_skipped = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001439 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1440 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001441 continue;
Trond Myklebust1c759502006-10-09 16:18:38 -04001442 pages = nfs_scan_dirty(mapping, wbc, &head);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001443 if (pages != 0) {
1444 spin_unlock(&nfsi->req_lock);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001445 if (how & FLUSH_INVALIDATE) {
Trond Myklebust83715ad2006-07-05 13:17:12 -04001446 nfs_cancel_dirty_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001447 ret = pages;
1448 } else
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001449 ret = nfs_flush_list(inode, &head, pages, how);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001450 spin_lock(&nfsi->req_lock);
1451 continue;
1452 }
Trond Myklebust1c759502006-10-09 16:18:38 -04001453 if (wbc->pages_skipped != 0)
1454 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001455 if (nocommit)
1456 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001457 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebust1c759502006-10-09 16:18:38 -04001458 if (pages == 0) {
1459 if (wbc->pages_skipped != 0)
1460 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001461 break;
Trond Myklebust1c759502006-10-09 16:18:38 -04001462 }
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001463 if (how & FLUSH_INVALIDATE) {
1464 spin_unlock(&nfsi->req_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001465 nfs_cancel_commit_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001466 ret = pages;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001467 spin_lock(&nfsi->req_lock);
1468 continue;
1469 }
1470 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001471 spin_unlock(&nfsi->req_lock);
1472 ret = nfs_commit_list(inode, &head, how);
1473 spin_lock(&nfsi->req_lock);
1474 } while (ret >= 0);
1475 spin_unlock(&nfsi->req_lock);
1476 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
Trond Myklebust1c759502006-10-09 16:18:38 -04001479/*
1480 * flush the inode to disk.
1481 */
1482int nfs_wb_all(struct inode *inode)
1483{
1484 struct address_space *mapping = inode->i_mapping;
1485 struct writeback_control wbc = {
1486 .bdi = mapping->backing_dev_info,
1487 .sync_mode = WB_SYNC_ALL,
1488 .nr_to_write = LONG_MAX,
1489 .range_cyclic = 1,
1490 };
1491 int ret;
1492
1493 ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
1494 if (ret >= 0)
1495 return 0;
1496 return ret;
1497}
1498
1499int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
1500{
1501 struct writeback_control wbc = {
1502 .bdi = mapping->backing_dev_info,
1503 .sync_mode = WB_SYNC_ALL,
1504 .nr_to_write = LONG_MAX,
1505 .range_start = range_start,
1506 .range_end = range_end,
1507 };
1508 int ret;
1509
1510 ret = nfs_sync_mapping_wait(mapping, &wbc, how);
1511 if (ret >= 0)
1512 return 0;
1513 return ret;
1514}
1515
1516static int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
1517{
1518 loff_t range_start = page_offset(page);
1519 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1520
1521 return nfs_sync_mapping_range(inode->i_mapping, range_start, range_end, how | FLUSH_STABLE);
1522}
1523
1524/*
1525 * Write back all requests on one page - we do this before reading it.
1526 */
1527int nfs_wb_page(struct inode *inode, struct page* page)
1528{
1529 return nfs_wb_page_priority(inode, page, 0);
1530}
1531
1532
David Howellsf7b422b2006-06-09 09:34:33 -04001533int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
1535 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1536 sizeof(struct nfs_write_data),
1537 0, SLAB_HWCACHE_ALIGN,
1538 NULL, NULL);
1539 if (nfs_wdata_cachep == NULL)
1540 return -ENOMEM;
1541
Matthew Dobson93d23412006-03-26 01:37:50 -08001542 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1543 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if (nfs_wdata_mempool == NULL)
1545 return -ENOMEM;
1546
Matthew Dobson93d23412006-03-26 01:37:50 -08001547 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1548 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 if (nfs_commit_mempool == NULL)
1550 return -ENOMEM;
1551
1552 return 0;
1553}
1554
David Brownell266bee82006-06-27 12:59:15 -07001555void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
1557 mempool_destroy(nfs_commit_mempool);
1558 mempool_destroy(nfs_wdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001559 kmem_cache_destroy(nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561