blob: 74e86601d9788f4590ba6f7cee7998693c4b4c9e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/write.c
3 *
4 * Writing file data over NFS.
5 *
6 * We do it like this: When a (user) process wishes to write data to an
7 * NFS file, a write request is allocated that contains the RPC task data
8 * plus some info on the page to be written, and added to the inode's
9 * write chain. If the process writes past the end of the page, an async
10 * RPC call to write the page is scheduled immediately; otherwise, the call
11 * is delayed for a few seconds.
12 *
13 * Just like readahead, no async I/O is performed if wsize < PAGE_SIZE.
14 *
15 * Write requests are kept on the inode's writeback list. Each entry in
16 * that list references the page (portion) to be written. When the
17 * cache timeout has expired, the RPC task is woken up, and tries to
18 * lock the page. As soon as it manages to do so, the request is moved
19 * from the writeback list to the writelock list.
20 *
21 * Note: we must make sure never to confuse the inode passed in the
22 * write_page request with the one in page->inode. As far as I understand
23 * it, these are different when doing a swap-out.
24 *
25 * To understand everything that goes on here and in the NFS read code,
26 * one should be aware that a page is locked in exactly one of the following
27 * cases:
28 *
29 * - A write request is in progress.
30 * - A user process is in generic_file_write/nfs_update_page
31 * - A user process is in generic_file_read
32 *
33 * Also note that because of the way pages are invalidated in
34 * nfs_revalidate_inode, the following assertions hold:
35 *
36 * - If a page is dirty, there will be no read requests (a page will
37 * not be re-read unless invalidated by nfs_revalidate_inode).
38 * - If the page is not uptodate, there will be no pending write
39 * requests, and no process will be in nfs_update_page.
40 *
41 * FIXME: Interaction with the vmscan routines is not optimal yet.
42 * Either vmscan must be made nfs-savvy, or we need a different page
43 * reclaim concept that supports something like FS-independent
44 * buffer_heads with a b_ops-> field.
45 *
46 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
47 */
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/types.h>
50#include <linux/slab.h>
51#include <linux/mm.h>
52#include <linux/pagemap.h>
53#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/writeback.h>
55
56#include <linux/sunrpc/clnt.h>
57#include <linux/nfs_fs.h>
58#include <linux/nfs_mount.h>
59#include <linux/nfs_page.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070060#include <linux/backing-dev.h>
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <asm/uaccess.h>
63#include <linux/smp_lock.h>
64
65#include "delegation.h"
Trond Myklebust49a70f22006-12-05 00:35:38 -050066#include "internal.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050067#include "iostat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define NFSDBG_FACILITY NFSDBG_PAGECACHE
70
71#define MIN_POOL_WRITE (32)
72#define MIN_POOL_COMMIT (4)
73
74/*
75 * Local function declarations
76 */
77static struct nfs_page * nfs_update_request(struct nfs_open_context*,
78 struct inode *,
79 struct page *,
80 unsigned int, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static int nfs_wait_on_write_congestion(struct address_space *, int);
82static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int);
Trond Myklebust3f442542006-09-17 14:46:44 -040083static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how);
Trond Myklebust1c759502006-10-09 16:18:38 -040084static int nfs_wb_page_priority(struct inode *inode, struct page *page, int how);
Trond Myklebust788e7a82006-03-20 13:44:27 -050085static const struct rpc_call_ops nfs_write_partial_ops;
86static const struct rpc_call_ops nfs_write_full_ops;
87static const struct rpc_call_ops nfs_commit_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89static kmem_cache_t *nfs_wdata_cachep;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050090static mempool_t *nfs_wdata_mempool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static mempool_t *nfs_commit_mempool;
92
93static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
94
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070095struct nfs_write_data *nfs_commit_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS);
Chuck Lever40859d72005-11-30 18:09:02 -050098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (p) {
100 memset(p, 0, sizeof(*p));
101 INIT_LIST_HEAD(&p->pages);
102 }
103 return p;
104}
105
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500106void nfs_commit_rcu_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500108 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
Chuck Lever40859d72005-11-30 18:09:02 -0500109 if (p && (p->pagevec != &p->page_array[0]))
110 kfree(p->pagevec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 mempool_free(p, nfs_commit_mempool);
112}
113
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500114void nfs_commit_free(struct nfs_write_data *wdata)
115{
116 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
117}
118
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700119struct nfs_write_data *nfs_writedata_alloc(size_t len)
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500120{
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700121 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500122 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
123
124 if (p) {
125 memset(p, 0, sizeof(*p));
126 INIT_LIST_HEAD(&p->pages);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700127 p->npages = pagecount;
Chuck Lever0d0b5cb2006-05-25 01:40:53 -0400128 if (pagecount <= ARRAY_SIZE(p->page_array))
129 p->pagevec = p->page_array;
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500130 else {
Chuck Lever0d0b5cb2006-05-25 01:40:53 -0400131 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
132 if (!p->pagevec) {
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500133 mempool_free(p, nfs_wdata_mempool);
134 p = NULL;
135 }
136 }
137 }
138 return p;
139}
140
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500141static void nfs_writedata_rcu_free(struct rcu_head *head)
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500142{
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500143 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500144 if (p && (p->pagevec != &p->page_array[0]))
145 kfree(p->pagevec);
146 mempool_free(p, nfs_wdata_mempool);
147}
148
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500149static void nfs_writedata_free(struct nfs_write_data *wdata)
150{
151 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
152}
153
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100154void nfs_writedata_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 nfs_writedata_free(wdata);
157}
158
Trond Myklebust277459d2006-12-05 00:35:35 -0500159static struct nfs_page *nfs_page_find_request_locked(struct page *page)
160{
161 struct nfs_page *req = NULL;
162
163 if (PagePrivate(page)) {
164 req = (struct nfs_page *)page_private(page);
165 if (req != NULL)
166 atomic_inc(&req->wb_count);
167 }
168 return req;
169}
170
171static struct nfs_page *nfs_page_find_request(struct page *page)
172{
173 struct nfs_page *req = NULL;
174 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
175
176 spin_lock(req_lock);
177 req = nfs_page_find_request_locked(page);
178 spin_unlock(req_lock);
179 return req;
180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/* Adjust the file length if we're writing beyond the end */
183static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
184{
185 struct inode *inode = page->mapping->host;
186 loff_t end, i_size = i_size_read(inode);
187 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
188
189 if (i_size > 0 && page->index < end_index)
190 return;
191 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
192 if (i_size >= end)
193 return;
Chuck Lever91d5b472006-03-20 13:44:14 -0500194 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 i_size_write(inode, end);
196}
197
198/* We can set the PG_uptodate flag if we see that a write request
199 * covers the full page.
200 */
201static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
202{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (PageUptodate(page))
204 return;
205 if (base != 0)
206 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500207 if (count != nfs_page_length(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500209 if (count != PAGE_CACHE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
Trond Myklebust49a70f22006-12-05 00:35:38 -0500211 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214/*
215 * Write a page synchronously.
216 * Offset is the data offset within the page.
217 */
218static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
219 struct page *page, unsigned int offset, unsigned int count,
220 int how)
221{
222 unsigned int wsize = NFS_SERVER(inode)->wsize;
223 int result, written = 0;
224 struct nfs_write_data *wdata;
225
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700226 wdata = nfs_writedata_alloc(wsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (!wdata)
228 return -ENOMEM;
229
230 wdata->flags = how;
231 wdata->cred = ctx->cred;
232 wdata->inode = inode;
233 wdata->args.fh = NFS_FH(inode);
234 wdata->args.context = ctx;
235 wdata->args.pages = &page;
236 wdata->args.stable = NFS_FILE_SYNC;
237 wdata->args.pgbase = offset;
238 wdata->args.count = wsize;
239 wdata->res.fattr = &wdata->fattr;
240 wdata->res.verf = &wdata->verf;
241
242 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
243 inode->i_sb->s_id,
244 (long long)NFS_FILEID(inode),
245 count, (long long)(page_offset(page) + offset));
246
Trond Myklebustbb713d62005-12-03 15:20:14 -0500247 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 nfs_begin_data_update(inode);
249 do {
250 if (count < wsize)
251 wdata->args.count = count;
252 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
253
254 result = NFS_PROTO(inode)->write(wdata);
255
256 if (result < 0) {
257 /* Must mark the page invalid after I/O error */
258 ClearPageUptodate(page);
259 goto io_error;
260 }
261 if (result < wdata->args.count)
262 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
263 wdata->args.count, result);
264
265 wdata->args.offset += result;
266 wdata->args.pgbase += result;
267 written += result;
268 count -= result;
Chuck Lever91d5b472006-03-20 13:44:14 -0500269 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 } while (count);
271 /* Update file length */
272 nfs_grow_file(page, offset, written);
273 /* Set the PG_uptodate flag? */
274 nfs_mark_uptodate(page, offset, written);
275
276 if (PageError(page))
277 ClearPageError(page);
278
279io_error:
Trond Myklebust951a1432005-06-22 17:16:30 +0000280 nfs_end_data_update(inode);
Trond Myklebustbb713d62005-12-03 15:20:14 -0500281 end_page_writeback(page);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500282 nfs_writedata_release(wdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return written ? written : result;
284}
285
286static int nfs_writepage_async(struct nfs_open_context *ctx,
287 struct inode *inode, struct page *page,
288 unsigned int offset, unsigned int count)
289{
290 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 req = nfs_update_request(ctx, inode, page, offset, count);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100293 if (IS_ERR(req))
294 return PTR_ERR(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 /* Update file length */
296 nfs_grow_file(page, offset, count);
297 /* Set the PG_uptodate flag? */
298 nfs_mark_uptodate(page, offset, count);
299 nfs_unlock_request(req);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100300 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
303static int wb_priority(struct writeback_control *wbc)
304{
305 if (wbc->for_reclaim)
306 return FLUSH_HIGHPRI;
307 if (wbc->for_kupdate)
308 return FLUSH_LOWPRI;
309 return 0;
310}
311
312/*
313 * Write an mmapped page to the server.
314 */
315int nfs_writepage(struct page *page, struct writeback_control *wbc)
316{
317 struct nfs_open_context *ctx;
318 struct inode *inode = page->mapping->host;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500319 unsigned offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int priority = wb_priority(wbc);
321 int err;
322
Chuck Lever91d5b472006-03-20 13:44:14 -0500323 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
324 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 /* Ensure we've flushed out any previous writes */
327 nfs_wb_page_priority(inode, page, priority);
328
Trond Myklebust49a70f22006-12-05 00:35:38 -0500329 err = 0;
330 offset = nfs_page_length(page);
331 if (!offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 goto out;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500333
Trond Myklebustd5308382005-11-04 15:33:38 -0500334 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (ctx == NULL) {
336 err = -EBADF;
337 goto out;
338 }
339 lock_kernel();
Trond Myklebust87a4ce12006-12-05 00:35:39 -0500340 if (!IS_SYNC(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 err = nfs_writepage_async(ctx, inode, page, 0, offset);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100342 if (!wbc->for_writepages)
Trond Myklebust28c69252006-09-16 13:04:50 -0400343 nfs_flush_mapping(page->mapping, wbc, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 } else {
345 err = nfs_writepage_sync(ctx, inode, page, 0,
346 offset, priority);
347 if (err >= 0) {
348 if (err != offset)
349 redirty_page_for_writepage(wbc, page);
350 err = 0;
351 }
352 }
353 unlock_kernel();
354 put_nfs_open_context(ctx);
355out:
356 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return err;
358}
359
360/*
361 * Note: causes nfs_update_request() to block on the assumption
362 * that the writeback is generated due to memory pressure.
363 */
364int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
365{
366 struct backing_dev_info *bdi = mapping->backing_dev_info;
367 struct inode *inode = mapping->host;
368 int err;
369
Chuck Lever91d5b472006-03-20 13:44:14 -0500370 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 err = generic_writepages(mapping, wbc);
373 if (err)
374 return err;
375 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
376 if (wbc->nonblocking)
377 return 0;
378 nfs_wait_on_write_congestion(mapping, 0);
379 }
Trond Myklebust28c69252006-09-16 13:04:50 -0400380 err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (err < 0)
382 goto out;
Chuck Lever91d5b472006-03-20 13:44:14 -0500383 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
385 err = nfs_wait_on_requests(inode, 0, 0);
386 if (err < 0)
387 goto out;
388 }
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000389 err = nfs_commit_inode(inode, wb_priority(wbc));
Trond Myklebust3f442542006-09-17 14:46:44 -0400390 if (err > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392out:
393 clear_bit(BDI_write_congested, &bdi->state);
394 wake_up_all(&nfs_write_congestion);
Andrew Morton3fcfab12006-10-19 23:28:16 -0700395 congestion_end(WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return err;
397}
398
399/*
400 * Insert a write request into an inode
401 */
402static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
403{
404 struct nfs_inode *nfsi = NFS_I(inode);
405 int error;
406
407 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
408 BUG_ON(error == -EEXIST);
409 if (error)
410 return error;
411 if (!nfsi->npages) {
412 igrab(inode);
413 nfs_begin_data_update(inode);
414 if (nfs_have_delegation(inode, FMODE_WRITE))
415 nfsi->change_attr++;
416 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500417 SetPagePrivate(req->wb_page);
Trond Myklebust277459d2006-12-05 00:35:35 -0500418 set_page_private(req->wb_page, (unsigned long)req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 nfsi->npages++;
420 atomic_inc(&req->wb_count);
421 return 0;
422}
423
424/*
425 * Insert a write request into an inode
426 */
427static void nfs_inode_remove_request(struct nfs_page *req)
428{
429 struct inode *inode = req->wb_context->dentry->d_inode;
430 struct nfs_inode *nfsi = NFS_I(inode);
431
432 BUG_ON (!NFS_WBACK_BUSY(req));
433
434 spin_lock(&nfsi->req_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500435 set_page_private(req->wb_page, 0);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500436 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
438 nfsi->npages--;
439 if (!nfsi->npages) {
440 spin_unlock(&nfsi->req_lock);
Trond Myklebust951a1432005-06-22 17:16:30 +0000441 nfs_end_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 iput(inode);
443 } else
444 spin_unlock(&nfsi->req_lock);
445 nfs_clear_request(req);
446 nfs_release_request(req);
447}
448
449/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 * Add a request to the inode's dirty list.
451 */
452static void
453nfs_mark_request_dirty(struct nfs_page *req)
454{
455 struct inode *inode = req->wb_context->dentry->d_inode;
456 struct nfs_inode *nfsi = NFS_I(inode);
457
458 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000459 radix_tree_tag_set(&nfsi->nfs_page_tree,
460 req->wb_index, NFS_PAGE_TAG_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 nfs_list_add_request(req, &nfsi->dirty);
462 nfsi->ndirty++;
463 spin_unlock(&nfsi->req_lock);
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -0700464 inc_zone_page_state(req->wb_page, NR_FILE_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 mark_inode_dirty(inode);
466}
467
468/*
469 * Check if a request is dirty
470 */
471static inline int
472nfs_dirty_request(struct nfs_page *req)
473{
474 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
475 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
476}
477
478#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
479/*
480 * Add a request to the inode's commit list.
481 */
482static void
483nfs_mark_request_commit(struct nfs_page *req)
484{
485 struct inode *inode = req->wb_context->dentry->d_inode;
486 struct nfs_inode *nfsi = NFS_I(inode);
487
488 spin_lock(&nfsi->req_lock);
489 nfs_list_add_request(req, &nfsi->commit);
490 nfsi->ncommit++;
491 spin_unlock(&nfsi->req_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700492 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 mark_inode_dirty(inode);
494}
495#endif
496
497/*
498 * Wait for a request to complete.
499 *
500 * Interruptible by signals only if mounted with intr flag.
501 */
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500502static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 struct nfs_inode *nfsi = NFS_I(inode);
505 struct nfs_page *req;
506 unsigned long idx_end, next;
507 unsigned int res = 0;
508 int error;
509
510 if (npages == 0)
511 idx_end = ~0;
512 else
513 idx_end = idx_start + npages - 1;
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 next = idx_start;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000516 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 -0700517 if (req->wb_index > idx_end)
518 break;
519
520 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000521 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 atomic_inc(&req->wb_count);
524 spin_unlock(&nfsi->req_lock);
525 error = nfs_wait_on_request(req);
526 nfs_release_request(req);
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500527 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (error < 0)
529 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 res++;
531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return res;
533}
534
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500535static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
536{
537 struct nfs_inode *nfsi = NFS_I(inode);
538 int ret;
539
540 spin_lock(&nfsi->req_lock);
541 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
542 spin_unlock(&nfsi->req_lock);
543 return ret;
544}
545
Trond Myklebust83715ad2006-07-05 13:17:12 -0400546static void nfs_cancel_dirty_list(struct list_head *head)
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400547{
548 struct nfs_page *req;
549 while(!list_empty(head)) {
550 req = nfs_list_entry(head->next);
551 nfs_list_remove_request(req);
552 nfs_inode_remove_request(req);
553 nfs_clear_page_writeback(req);
554 }
555}
556
Trond Myklebust83715ad2006-07-05 13:17:12 -0400557static void nfs_cancel_commit_list(struct list_head *head)
558{
559 struct nfs_page *req;
560
561 while(!list_empty(head)) {
562 req = nfs_list_entry(head->next);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700563 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400564 nfs_list_remove_request(req);
565 nfs_inode_remove_request(req);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700566 nfs_unlock_request(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400567 }
568}
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
571/*
572 * nfs_scan_commit - Scan an inode for commit requests
573 * @inode: NFS inode to scan
574 * @dst: destination list
575 * @idx_start: lower bound of page->index to scan.
576 * @npages: idx_start + npages sets the upper bound to scan.
577 *
578 * Moves requests from the inode's 'commit' request list.
579 * The requests are *not* checked to ensure that they form a contiguous set.
580 */
581static int
582nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
583{
584 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000585 int res = 0;
586
587 if (nfsi->ncommit != 0) {
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400588 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000589 nfsi->ncommit -= res;
590 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
591 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return res;
594}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500595#else
596static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
597{
598 return 0;
599}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600#endif
601
602static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
603{
604 struct backing_dev_info *bdi = mapping->backing_dev_info;
605 DEFINE_WAIT(wait);
606 int ret = 0;
607
608 might_sleep();
609
610 if (!bdi_write_congested(bdi))
611 return 0;
Chuck Lever91d5b472006-03-20 13:44:14 -0500612
613 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (intr) {
616 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
617 sigset_t oldset;
618
619 rpc_clnt_sigmask(clnt, &oldset);
620 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
621 if (bdi_write_congested(bdi)) {
622 if (signalled())
623 ret = -ERESTARTSYS;
624 else
625 schedule();
626 }
627 rpc_clnt_sigunmask(clnt, &oldset);
628 } else {
629 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
630 if (bdi_write_congested(bdi))
631 schedule();
632 }
633 finish_wait(&nfs_write_congestion, &wait);
634 return ret;
635}
636
637
638/*
639 * Try to update any existing write request, or create one if there is none.
640 * In order to match, the request's credentials must match those of
641 * the calling process.
642 *
643 * Note: Should always be called with the Page Lock held!
644 */
645static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
646 struct inode *inode, struct page *page,
647 unsigned int offset, unsigned int bytes)
648{
649 struct nfs_server *server = NFS_SERVER(inode);
650 struct nfs_inode *nfsi = NFS_I(inode);
651 struct nfs_page *req, *new = NULL;
652 unsigned long rqend, end;
653
654 end = offset + bytes;
655
656 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
657 return ERR_PTR(-ERESTARTSYS);
658 for (;;) {
659 /* Loop over all inode entries and see if we find
660 * A request for the page we wish to update
661 */
662 spin_lock(&nfsi->req_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500663 req = nfs_page_find_request_locked(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (req) {
665 if (!nfs_lock_request_dontget(req)) {
666 int error;
Trond Myklebust277459d2006-12-05 00:35:35 -0500667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 spin_unlock(&nfsi->req_lock);
669 error = nfs_wait_on_request(req);
670 nfs_release_request(req);
Neil Brown1dd594b2006-03-20 13:44:04 -0500671 if (error < 0) {
672 if (new)
673 nfs_release_request(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return ERR_PTR(error);
Neil Brown1dd594b2006-03-20 13:44:04 -0500675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 continue;
677 }
678 spin_unlock(&nfsi->req_lock);
679 if (new)
680 nfs_release_request(new);
681 break;
682 }
683
684 if (new) {
685 int error;
686 nfs_lock_request_dontget(new);
687 error = nfs_inode_add_request(inode, new);
688 if (error) {
689 spin_unlock(&nfsi->req_lock);
690 nfs_unlock_request(new);
691 return ERR_PTR(error);
692 }
693 spin_unlock(&nfsi->req_lock);
694 nfs_mark_request_dirty(new);
695 return new;
696 }
697 spin_unlock(&nfsi->req_lock);
698
699 new = nfs_create_request(ctx, inode, page, offset, bytes);
700 if (IS_ERR(new))
701 return new;
702 }
703
704 /* We have a request for our page.
705 * If the creds don't match, or the
706 * page addresses don't match,
707 * tell the caller to wait on the conflicting
708 * request.
709 */
710 rqend = req->wb_offset + req->wb_bytes;
711 if (req->wb_context != ctx
712 || req->wb_page != page
713 || !nfs_dirty_request(req)
714 || offset > rqend || end < req->wb_offset) {
715 nfs_unlock_request(req);
716 return ERR_PTR(-EBUSY);
717 }
718
719 /* Okay, the request matches. Update the region */
720 if (offset < req->wb_offset) {
721 req->wb_offset = offset;
722 req->wb_pgbase = offset;
723 req->wb_bytes = rqend - req->wb_offset;
724 }
725
726 if (end > rqend)
727 req->wb_bytes = end - req->wb_offset;
728
729 return req;
730}
731
732int nfs_flush_incompatible(struct file *file, struct page *page)
733{
734 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 struct nfs_page *req;
736 int status = 0;
737 /*
738 * Look for a request corresponding to this page. If there
739 * is one, and it belongs to another file, we flush it out
740 * before we try to copy anything into the page. Do this
741 * due to the lack of an ACCESS-type call in NFSv2.
742 * Also do the same if we find a request from an existing
743 * dropped page.
744 */
Trond Myklebust277459d2006-12-05 00:35:35 -0500745 req = nfs_page_find_request(page);
746 if (req != NULL) {
747 int do_flush = req->wb_page != page || req->wb_context != ctx;
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 nfs_release_request(req);
Trond Myklebust277459d2006-12-05 00:35:35 -0500750 if (do_flush)
751 status = nfs_wb_page(page->mapping->host, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753 return (status < 0) ? status : 0;
754}
755
756/*
757 * Update and possibly write a cached page of an NFS file.
758 *
759 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
760 * things with a page scheduled for an RPC call (e.g. invalidate it).
761 */
762int nfs_updatepage(struct file *file, struct page *page,
763 unsigned int offset, unsigned int count)
764{
765 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 struct inode *inode = page->mapping->host;
767 struct nfs_page *req;
768 int status = 0;
769
Chuck Lever91d5b472006-03-20 13:44:14 -0500770 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500773 file->f_dentry->d_parent->d_name.name,
774 file->f_dentry->d_name.name, count,
775 (long long)(page_offset(page) +offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 if (IS_SYNC(inode)) {
778 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
779 if (status > 0) {
780 if (offset == 0 && status == PAGE_CACHE_SIZE)
781 SetPageUptodate(page);
782 return 0;
783 }
784 return status;
785 }
786
787 /* If we're not using byte range locks, and we know the page
788 * is entirely in cache, it may be more efficient to avoid
789 * fragmenting write requests.
790 */
Trond Myklebustab0a3db2005-06-22 17:16:30 +0000791 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
Trond Myklebust49a70f22006-12-05 00:35:38 -0500792 count = max(count + offset, nfs_page_length(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795
796 /*
797 * Try to find an NFS request corresponding to this page
798 * and update it.
799 * If the existing request cannot be updated, we must flush
800 * it out now.
801 */
802 do {
803 req = nfs_update_request(ctx, inode, page, offset, count);
804 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
805 if (status != -EBUSY)
806 break;
807 /* Request could not be updated. Flush it out and try again */
808 status = nfs_wb_page(inode, page);
809 } while (status >= 0);
810 if (status < 0)
811 goto done;
812
813 status = 0;
814
815 /* Update file length */
816 nfs_grow_file(page, offset, count);
817 /* Set the PG_uptodate flag? */
818 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
819 nfs_unlock_request(req);
820done:
821 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
822 status, (long long)i_size_read(inode));
823 if (status < 0)
824 ClearPageUptodate(page);
825 return status;
826}
827
828static void nfs_writepage_release(struct nfs_page *req)
829{
830 end_page_writeback(req->wb_page);
831
832#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
833 if (!PageError(req->wb_page)) {
834 if (NFS_NEED_RESCHED(req)) {
835 nfs_mark_request_dirty(req);
836 goto out;
837 } else if (NFS_NEED_COMMIT(req)) {
838 nfs_mark_request_commit(req);
839 goto out;
840 }
841 }
842 nfs_inode_remove_request(req);
843
844out:
845 nfs_clear_commit(req);
846 nfs_clear_reschedule(req);
847#else
848 nfs_inode_remove_request(req);
849#endif
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000850 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
853static inline int flush_task_priority(int how)
854{
855 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
856 case FLUSH_HIGHPRI:
857 return RPC_PRIORITY_HIGH;
858 case FLUSH_LOWPRI:
859 return RPC_PRIORITY_LOW;
860 }
861 return RPC_PRIORITY_NORMAL;
862}
863
864/*
865 * Set up the argument/result storage required for the RPC call.
866 */
867static void nfs_write_rpcsetup(struct nfs_page *req,
868 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500869 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 unsigned int count, unsigned int offset,
871 int how)
872{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -0500874 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 /* Set up the RPC argument and reply structs
877 * NB: take care not to mess about with data->commit et al. */
878
879 data->req = req;
880 data->inode = inode = req->wb_context->dentry->d_inode;
881 data->cred = req->wb_context->cred;
882
883 data->args.fh = NFS_FH(inode);
884 data->args.offset = req_offset(req) + offset;
885 data->args.pgbase = req->wb_pgbase + offset;
886 data->args.pages = data->pagevec;
887 data->args.count = count;
888 data->args.context = req->wb_context;
889
890 data->res.fattr = &data->fattr;
891 data->res.count = count;
892 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400893 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Trond Myklebust788e7a82006-03-20 13:44:27 -0500895 /* Set up the initial task struct. */
896 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
897 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 NFS_PROTO(inode)->write_setup(data, how);
899
900 data->task.tk_priority = flush_task_priority(how);
901 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500904 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 inode->i_sb->s_id,
906 (long long)NFS_FILEID(inode),
907 count,
908 (unsigned long long)data->args.offset);
909}
910
911static void nfs_execute_write(struct nfs_write_data *data)
912{
913 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
914 sigset_t oldset;
915
916 rpc_clnt_sigmask(clnt, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 rpc_execute(&data->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 rpc_clnt_sigunmask(clnt, &oldset);
919}
920
921/*
922 * Generate multiple small requests to write out a single
923 * contiguous dirty area on one page.
924 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500925static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
927 struct nfs_page *req = nfs_list_entry(head->next);
928 struct page *page = req->wb_page;
929 struct nfs_write_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700930 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
931 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 int requests = 0;
933 LIST_HEAD(list);
934
935 nfs_list_remove_request(req);
936
937 nbytes = req->wb_bytes;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700938 do {
939 size_t len = min(nbytes, wsize);
940
941 data = nfs_writedata_alloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (!data)
943 goto out_bad;
944 list_add(&data->pages, &list);
945 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700946 nbytes -= len;
947 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 atomic_set(&req->wb_complete, requests);
949
950 ClearPageError(page);
Trond Myklebustbb713d62005-12-03 15:20:14 -0500951 set_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 offset = 0;
953 nbytes = req->wb_bytes;
954 do {
955 data = list_entry(list.next, struct nfs_write_data, pages);
956 list_del_init(&data->pages);
957
958 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 if (nbytes > wsize) {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500961 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
962 wsize, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 offset += wsize;
964 nbytes -= wsize;
965 } else {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500966 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
967 nbytes, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 nbytes = 0;
969 }
970 nfs_execute_write(data);
971 } while (nbytes != 0);
972
973 return 0;
974
975out_bad:
976 while (!list_empty(&list)) {
977 data = list_entry(list.next, struct nfs_write_data, pages);
978 list_del(&data->pages);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500979 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
981 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000982 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return -ENOMEM;
984}
985
986/*
987 * Create an RPC task for the given write request and kick it.
988 * The page must have been locked by the caller.
989 *
990 * It may happen that the page we're passed is not marked dirty.
991 * This is the case if nfs_updatepage detects a conflicting request
992 * that has been written but not committed.
993 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500994static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 struct nfs_page *req;
997 struct page **pages;
998 struct nfs_write_data *data;
999 unsigned int count;
1000
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001001 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (!data)
1003 goto out_bad;
1004
1005 pages = data->pagevec;
1006 count = 0;
1007 while (!list_empty(head)) {
1008 req = nfs_list_entry(head->next);
1009 nfs_list_remove_request(req);
1010 nfs_list_add_request(req, &data->pages);
1011 ClearPageError(req->wb_page);
Trond Myklebustbb713d62005-12-03 15:20:14 -05001012 set_page_writeback(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 *pages++ = req->wb_page;
1014 count += req->wb_bytes;
1015 }
1016 req = nfs_list_entry(data->pages.next);
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 /* Set up the argument struct */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001019 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 nfs_execute_write(data);
1022 return 0;
1023 out_bad:
1024 while (!list_empty(head)) {
1025 struct nfs_page *req = nfs_list_entry(head->next);
1026 nfs_list_remove_request(req);
1027 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001028 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
1030 return -ENOMEM;
1031}
1032
Trond Myklebust7d46a492006-03-20 13:44:50 -05001033static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
1035 LIST_HEAD(one_request);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001036 int (*flush_one)(struct inode *, struct list_head *, int);
1037 struct nfs_page *req;
1038 int wpages = NFS_SERVER(inode)->wpages;
1039 int wsize = NFS_SERVER(inode)->wsize;
1040 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Trond Myklebust7d46a492006-03-20 13:44:50 -05001042 flush_one = nfs_flush_one;
1043 if (wsize < PAGE_CACHE_SIZE)
1044 flush_one = nfs_flush_multi;
1045 /* For single writes, FLUSH_STABLE is more efficient */
1046 if (npages <= wpages && npages == NFS_I(inode)->npages
1047 && nfs_list_entry(head->next)->wb_bytes <= wsize)
1048 how |= FLUSH_STABLE;
1049
1050 do {
1051 nfs_coalesce_requests(head, &one_request, wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 req = nfs_list_entry(one_request.next);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001053 error = flush_one(inode, &one_request, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (error < 0)
Trond Myklebust7d46a492006-03-20 13:44:50 -05001055 goto out_err;
1056 } while (!list_empty(head));
1057 return 0;
1058out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 while (!list_empty(head)) {
1060 req = nfs_list_entry(head->next);
1061 nfs_list_remove_request(req);
1062 nfs_mark_request_dirty(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001063 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065 return error;
1066}
1067
1068/*
1069 * Handle a write reply that flushed part of a page.
1070 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001071static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001073 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 struct nfs_page *req = data->req;
1075 struct page *page = req->wb_page;
1076
1077 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1078 req->wb_context->dentry->d_inode->i_sb->s_id,
1079 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1080 req->wb_bytes,
1081 (long long)req_offset(req));
1082
Trond Myklebust788e7a82006-03-20 13:44:27 -05001083 if (nfs_writeback_done(task, data) != 0)
1084 return;
1085
1086 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 ClearPageUptodate(page);
1088 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001089 req->wb_context->error = task->tk_status;
1090 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 } else {
1092#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1093 if (data->verf.committed < NFS_FILE_SYNC) {
1094 if (!NFS_NEED_COMMIT(req)) {
1095 nfs_defer_commit(req);
1096 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1097 dprintk(" defer commit\n");
1098 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1099 nfs_defer_reschedule(req);
1100 dprintk(" server reboot detected\n");
1101 }
1102 } else
1103#endif
1104 dprintk(" OK\n");
1105 }
1106
1107 if (atomic_dec_and_test(&req->wb_complete))
1108 nfs_writepage_release(req);
1109}
1110
Trond Myklebust788e7a82006-03-20 13:44:27 -05001111static const struct rpc_call_ops nfs_write_partial_ops = {
1112 .rpc_call_done = nfs_writeback_done_partial,
1113 .rpc_release = nfs_writedata_release,
1114};
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116/*
1117 * Handle a write reply that flushes a whole page.
1118 *
1119 * FIXME: There is an inherent race with invalidate_inode_pages and
1120 * writebacks since the page->count is kept > 1 for as long
1121 * as the page has a write request pending.
1122 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001123static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001125 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 struct nfs_page *req;
1127 struct page *page;
1128
Trond Myklebust788e7a82006-03-20 13:44:27 -05001129 if (nfs_writeback_done(task, data) != 0)
1130 return;
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 /* Update attributes as result of writeback. */
1133 while (!list_empty(&data->pages)) {
1134 req = nfs_list_entry(data->pages.next);
1135 nfs_list_remove_request(req);
1136 page = req->wb_page;
1137
1138 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1139 req->wb_context->dentry->d_inode->i_sb->s_id,
1140 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1141 req->wb_bytes,
1142 (long long)req_offset(req));
1143
Trond Myklebust788e7a82006-03-20 13:44:27 -05001144 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 ClearPageUptodate(page);
1146 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001147 req->wb_context->error = task->tk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 end_page_writeback(page);
1149 nfs_inode_remove_request(req);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001150 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 goto next;
1152 }
1153 end_page_writeback(page);
1154
1155#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1156 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1157 nfs_inode_remove_request(req);
1158 dprintk(" OK\n");
1159 goto next;
1160 }
1161 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1162 nfs_mark_request_commit(req);
1163 dprintk(" marked for commit\n");
1164#else
1165 nfs_inode_remove_request(req);
1166#endif
1167 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001168 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
1170}
1171
Trond Myklebust788e7a82006-03-20 13:44:27 -05001172static const struct rpc_call_ops nfs_write_full_ops = {
1173 .rpc_call_done = nfs_writeback_done_full,
1174 .rpc_release = nfs_writedata_release,
1175};
1176
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178/*
1179 * This function is called when the WRITE call is complete.
1180 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001181int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 struct nfs_writeargs *argp = &data->args;
1184 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001185 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1188 task->tk_pid, task->tk_status);
1189
Chuck Leverf551e442006-09-20 14:33:04 -04001190 /*
1191 * ->write_done will attempt to use post-op attributes to detect
1192 * conflicting writes by other clients. A strict interpretation
1193 * of close-to-open would allow us to continue caching even if
1194 * another writer had changed the file, but some applications
1195 * depend on tighter cache coherency when writing.
1196 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001197 status = NFS_PROTO(data->inode)->write_done(task, data);
1198 if (status != 0)
1199 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001200 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1203 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1204 /* We tried a write call, but the server did not
1205 * commit data to stable storage even though we
1206 * requested it.
1207 * Note: There is a known bug in Tru64 < 5.0 in which
1208 * the server reports NFS_DATA_SYNC, but performs
1209 * NFS_FILE_SYNC. We therefore implement this checking
1210 * as a dprintk() in order to avoid filling syslog.
1211 */
1212 static unsigned long complain;
1213
1214 if (time_before(complain, jiffies)) {
1215 dprintk("NFS: faulty NFS server %s:"
1216 " (committed = %d) != (stable = %d)\n",
David Howells54ceac42006-08-22 20:06:13 -04001217 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 resp->verf->committed, argp->stable);
1219 complain = jiffies + 300 * HZ;
1220 }
1221 }
1222#endif
1223 /* Is this a short write? */
1224 if (task->tk_status >= 0 && resp->count < argp->count) {
1225 static unsigned long complain;
1226
Chuck Lever91d5b472006-03-20 13:44:14 -05001227 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 /* Has the server at least made some progress? */
1230 if (resp->count != 0) {
1231 /* Was this an NFSv2 write or an NFSv3 stable write? */
1232 if (resp->verf->committed != NFS_UNSTABLE) {
1233 /* Resend from where the server left off */
1234 argp->offset += resp->count;
1235 argp->pgbase += resp->count;
1236 argp->count -= resp->count;
1237 } else {
1238 /* Resend as a stable write in order to avoid
1239 * headaches in the case of a server crash.
1240 */
1241 argp->stable = NFS_FILE_SYNC;
1242 }
1243 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001244 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
1246 if (time_before(complain, jiffies)) {
1247 printk(KERN_WARNING
1248 "NFS: Server wrote zero bytes, expected %u.\n",
1249 argp->count);
1250 complain = jiffies + 300 * HZ;
1251 }
1252 /* Can't do anything about it except throw an error. */
1253 task->tk_status = -EIO;
1254 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001255 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256}
1257
1258
1259#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001260void nfs_commit_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 nfs_commit_free(wdata);
1263}
1264
1265/*
1266 * Set up the argument/result storage required for the RPC call.
1267 */
1268static void nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001269 struct nfs_write_data *data,
1270 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001272 struct nfs_page *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001274 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 /* Set up the RPC argument and reply structs
1277 * NB: take care not to mess about with data->commit et al. */
1278
1279 list_splice_init(head, &data->pages);
1280 first = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 inode = first->wb_context->dentry->d_inode;
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 data->inode = inode;
1284 data->cred = first->wb_context->cred;
1285
1286 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001287 /* Note: we always request a commit of the entire inode */
1288 data->args.offset = 0;
1289 data->args.count = 0;
1290 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 data->res.fattr = &data->fattr;
1292 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001293 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001294
1295 /* Set up the initial task struct. */
1296 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1297 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 NFS_PROTO(inode)->commit_setup(data, how);
1299
1300 data->task.tk_priority = flush_task_priority(how);
1301 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Chuck Lever0bbacc42005-11-01 16:53:32 -05001303 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
1305
1306/*
1307 * Commit dirty pages
1308 */
1309static int
Chuck Lever40859d72005-11-30 18:09:02 -05001310nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 struct nfs_write_data *data;
1313 struct nfs_page *req;
1314
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001315 data = nfs_commit_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 if (!data)
1318 goto out_bad;
1319
1320 /* Set up the argument struct */
1321 nfs_commit_rpcsetup(head, data, how);
1322
1323 nfs_execute_write(data);
1324 return 0;
1325 out_bad:
1326 while (!list_empty(head)) {
1327 req = nfs_list_entry(head->next);
1328 nfs_list_remove_request(req);
1329 nfs_mark_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001330 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust5c2d97c2006-09-18 23:20:35 -04001331 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
1333 return -ENOMEM;
1334}
1335
1336/*
1337 * COMMIT call returned
1338 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001339static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001341 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1345 task->tk_pid, task->tk_status);
1346
Trond Myklebust788e7a82006-03-20 13:44:27 -05001347 /* Call the NFS version-specific code */
1348 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1349 return;
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 while (!list_empty(&data->pages)) {
1352 req = nfs_list_entry(data->pages.next);
1353 nfs_list_remove_request(req);
Christoph Lameterfd39fc82006-06-30 01:55:40 -07001354 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1357 req->wb_context->dentry->d_inode->i_sb->s_id,
1358 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1359 req->wb_bytes,
1360 (long long)req_offset(req));
1361 if (task->tk_status < 0) {
1362 req->wb_context->error = task->tk_status;
1363 nfs_inode_remove_request(req);
1364 dprintk(", error = %d\n", task->tk_status);
1365 goto next;
1366 }
1367
1368 /* Okay, COMMIT succeeded, apparently. Check the verifier
1369 * returned by the server against all stored verfs. */
1370 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1371 /* We have a match */
1372 nfs_inode_remove_request(req);
1373 dprintk(" OK\n");
1374 goto next;
1375 }
1376 /* We have a mismatch. Write the page again */
1377 dprintk(" mismatch\n");
1378 nfs_mark_request_dirty(req);
1379 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001380 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001383
1384static const struct rpc_call_ops nfs_commit_ops = {
1385 .rpc_call_done = nfs_commit_done,
1386 .rpc_release = nfs_commit_release,
1387};
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001388#else
1389static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1390{
1391 return 0;
1392}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393#endif
1394
Trond Myklebust3f442542006-09-17 14:46:44 -04001395static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
Trond Myklebust28c69252006-09-16 13:04:50 -04001397 struct nfs_inode *nfsi = NFS_I(mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 LIST_HEAD(head);
Trond Myklebust3f442542006-09-17 14:46:44 -04001399 long res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 spin_lock(&nfsi->req_lock);
Trond Myklebust3f442542006-09-17 14:46:44 -04001402 res = nfs_scan_dirty(mapping, wbc, &head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 spin_unlock(&nfsi->req_lock);
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001404 if (res) {
Trond Myklebust28c69252006-09-16 13:04:50 -04001405 int error = nfs_flush_list(mapping->host, &head, res, how);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001406 if (error < 0)
1407 return error;
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return res;
1410}
1411
1412#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001413int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
1415 struct nfs_inode *nfsi = NFS_I(inode);
1416 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001417 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001420 res = nfs_scan_commit(inode, &head, 0, 0);
1421 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001423 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001424 if (error < 0)
1425 return error;
1426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return res;
1428}
1429#endif
1430
Trond Myklebust1c759502006-10-09 16:18:38 -04001431long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
Trond Myklebust1c759502006-10-09 16:18:38 -04001433 struct inode *inode = mapping->host;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001434 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c759502006-10-09 16:18:38 -04001435 unsigned long idx_start, idx_end;
1436 unsigned int npages = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001437 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001438 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebust3f442542006-09-17 14:46:44 -04001439 long pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Trond Myklebust1c759502006-10-09 16:18:38 -04001441 /* FIXME */
1442 if (wbc->range_cyclic)
1443 idx_start = 0;
1444 else {
1445 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1446 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1447 if (idx_end > idx_start) {
1448 unsigned long l_npages = 1 + idx_end - idx_start;
1449 npages = l_npages;
1450 if (sizeof(npages) != sizeof(l_npages) &&
1451 (unsigned long)npages != l_npages)
1452 npages = 0;
1453 }
1454 }
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001455 how &= ~FLUSH_NOCOMMIT;
1456 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 do {
Trond Myklebust1c759502006-10-09 16:18:38 -04001458 wbc->pages_skipped = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001459 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1460 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001461 continue;
Trond Myklebust1c759502006-10-09 16:18:38 -04001462 pages = nfs_scan_dirty(mapping, wbc, &head);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001463 if (pages != 0) {
1464 spin_unlock(&nfsi->req_lock);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001465 if (how & FLUSH_INVALIDATE) {
Trond Myklebust83715ad2006-07-05 13:17:12 -04001466 nfs_cancel_dirty_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001467 ret = pages;
1468 } else
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001469 ret = nfs_flush_list(inode, &head, pages, how);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001470 spin_lock(&nfsi->req_lock);
1471 continue;
1472 }
Trond Myklebust1c759502006-10-09 16:18:38 -04001473 if (wbc->pages_skipped != 0)
1474 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001475 if (nocommit)
1476 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001477 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebust1c759502006-10-09 16:18:38 -04001478 if (pages == 0) {
1479 if (wbc->pages_skipped != 0)
1480 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001481 break;
Trond Myklebust1c759502006-10-09 16:18:38 -04001482 }
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001483 if (how & FLUSH_INVALIDATE) {
1484 spin_unlock(&nfsi->req_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001485 nfs_cancel_commit_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001486 ret = pages;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001487 spin_lock(&nfsi->req_lock);
1488 continue;
1489 }
1490 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001491 spin_unlock(&nfsi->req_lock);
1492 ret = nfs_commit_list(inode, &head, how);
1493 spin_lock(&nfsi->req_lock);
1494 } while (ret >= 0);
1495 spin_unlock(&nfsi->req_lock);
1496 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
1498
Trond Myklebust1c759502006-10-09 16:18:38 -04001499/*
1500 * flush the inode to disk.
1501 */
1502int nfs_wb_all(struct inode *inode)
1503{
1504 struct address_space *mapping = inode->i_mapping;
1505 struct writeback_control wbc = {
1506 .bdi = mapping->backing_dev_info,
1507 .sync_mode = WB_SYNC_ALL,
1508 .nr_to_write = LONG_MAX,
1509 .range_cyclic = 1,
1510 };
1511 int ret;
1512
1513 ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
1514 if (ret >= 0)
1515 return 0;
1516 return ret;
1517}
1518
1519int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
1520{
1521 struct writeback_control wbc = {
1522 .bdi = mapping->backing_dev_info,
1523 .sync_mode = WB_SYNC_ALL,
1524 .nr_to_write = LONG_MAX,
1525 .range_start = range_start,
1526 .range_end = range_end,
1527 };
1528 int ret;
1529
1530 ret = nfs_sync_mapping_wait(mapping, &wbc, how);
1531 if (ret >= 0)
1532 return 0;
1533 return ret;
1534}
1535
1536static int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
1537{
1538 loff_t range_start = page_offset(page);
1539 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1540
1541 return nfs_sync_mapping_range(inode->i_mapping, range_start, range_end, how | FLUSH_STABLE);
1542}
1543
1544/*
1545 * Write back all requests on one page - we do this before reading it.
1546 */
1547int nfs_wb_page(struct inode *inode, struct page* page)
1548{
1549 return nfs_wb_page_priority(inode, page, 0);
1550}
1551
1552
David Howellsf7b422b2006-06-09 09:34:33 -04001553int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
1555 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1556 sizeof(struct nfs_write_data),
1557 0, SLAB_HWCACHE_ALIGN,
1558 NULL, NULL);
1559 if (nfs_wdata_cachep == NULL)
1560 return -ENOMEM;
1561
Matthew Dobson93d23412006-03-26 01:37:50 -08001562 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1563 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 if (nfs_wdata_mempool == NULL)
1565 return -ENOMEM;
1566
Matthew Dobson93d23412006-03-26 01:37:50 -08001567 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1568 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (nfs_commit_mempool == NULL)
1570 return -ENOMEM;
1571
1572 return 0;
1573}
1574
David Brownell266bee82006-06-27 12:59:15 -07001575void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
1577 mempool_destroy(nfs_commit_mempool);
1578 mempool_destroy(nfs_wdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001579 kmem_cache_destroy(nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581