blob: e5d7cac569aa4ba6ba699d57e673b08427493b83 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/write.c
3 *
Trond Myklebust7c85d902006-12-13 15:23:48 -05004 * Write file data over NFS.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/types.h>
10#include <linux/slab.h>
11#include <linux/mm.h>
12#include <linux/pagemap.h>
13#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/writeback.h>
Peter Zijlstra89a09142007-03-16 13:38:26 -080015#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#include <linux/sunrpc/clnt.h>
18#include <linux/nfs_fs.h>
19#include <linux/nfs_mount.h>
20#include <linux/nfs_page.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070021#include <linux/backing-dev.h>
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/uaccess.h>
24#include <linux/smp_lock.h>
25
26#include "delegation.h"
Trond Myklebust49a70f22006-12-05 00:35:38 -050027#include "internal.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050028#include "iostat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#define NFSDBG_FACILITY NFSDBG_PAGECACHE
31
32#define MIN_POOL_WRITE (32)
33#define MIN_POOL_COMMIT (4)
34
35/*
36 * Local function declarations
37 */
38static struct nfs_page * nfs_update_request(struct nfs_open_context*,
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct page *,
40 unsigned int, unsigned int);
Trond Myklebuste261f512006-12-05 00:35:41 -050041static void nfs_mark_request_dirty(struct nfs_page *req);
Trond Myklebust3f442542006-09-17 14:46:44 -040042static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how);
Trond Myklebust788e7a82006-03-20 13:44:27 -050043static const struct rpc_call_ops nfs_write_partial_ops;
44static const struct rpc_call_ops nfs_write_full_ops;
45static const struct rpc_call_ops nfs_commit_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Christoph Lametere18b8902006-12-06 20:33:20 -080047static struct kmem_cache *nfs_wdata_cachep;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050048static mempool_t *nfs_wdata_mempool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static mempool_t *nfs_commit_mempool;
50
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070051struct nfs_write_data *nfs_commit_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Christoph Lametere6b4f8d2006-12-06 20:33:14 -080053 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
Chuck Lever40859d72005-11-30 18:09:02 -050054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 if (p) {
56 memset(p, 0, sizeof(*p));
57 INIT_LIST_HEAD(&p->pages);
58 }
59 return p;
60}
61
Trond Myklebust8aca67f2006-11-13 16:23:44 -050062void nfs_commit_rcu_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Trond Myklebust8aca67f2006-11-13 16:23:44 -050064 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
Chuck Lever40859d72005-11-30 18:09:02 -050065 if (p && (p->pagevec != &p->page_array[0]))
66 kfree(p->pagevec);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 mempool_free(p, nfs_commit_mempool);
68}
69
Trond Myklebust8aca67f2006-11-13 16:23:44 -050070void nfs_commit_free(struct nfs_write_data *wdata)
71{
72 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
73}
74
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070075struct nfs_write_data *nfs_writedata_alloc(size_t len)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050076{
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070077 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Christoph Lametere6b4f8d2006-12-06 20:33:14 -080078 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050079
80 if (p) {
81 memset(p, 0, sizeof(*p));
82 INIT_LIST_HEAD(&p->pages);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070083 p->npages = pagecount;
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040084 if (pagecount <= ARRAY_SIZE(p->page_array))
85 p->pagevec = p->page_array;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050086 else {
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040087 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
88 if (!p->pagevec) {
Trond Myklebust3feb2d42006-03-20 13:44:37 -050089 mempool_free(p, nfs_wdata_mempool);
90 p = NULL;
91 }
92 }
93 }
94 return p;
95}
96
Trond Myklebust8aca67f2006-11-13 16:23:44 -050097static void nfs_writedata_rcu_free(struct rcu_head *head)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050098{
Trond Myklebust8aca67f2006-11-13 16:23:44 -050099 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500100 if (p && (p->pagevec != &p->page_array[0]))
101 kfree(p->pagevec);
102 mempool_free(p, nfs_wdata_mempool);
103}
104
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500105static void nfs_writedata_free(struct nfs_write_data *wdata)
106{
107 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
108}
109
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100110void nfs_writedata_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 nfs_writedata_free(wdata);
113}
114
Trond Myklebust277459d2006-12-05 00:35:35 -0500115static struct nfs_page *nfs_page_find_request_locked(struct page *page)
116{
117 struct nfs_page *req = NULL;
118
119 if (PagePrivate(page)) {
120 req = (struct nfs_page *)page_private(page);
121 if (req != NULL)
122 atomic_inc(&req->wb_count);
123 }
124 return req;
125}
126
127static struct nfs_page *nfs_page_find_request(struct page *page)
128{
129 struct nfs_page *req = NULL;
130 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
131
132 spin_lock(req_lock);
133 req = nfs_page_find_request_locked(page);
134 spin_unlock(req_lock);
135 return req;
136}
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/* Adjust the file length if we're writing beyond the end */
139static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
140{
141 struct inode *inode = page->mapping->host;
142 loff_t end, i_size = i_size_read(inode);
143 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
144
145 if (i_size > 0 && page->index < end_index)
146 return;
147 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
148 if (i_size >= end)
149 return;
Chuck Lever91d5b472006-03-20 13:44:14 -0500150 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 i_size_write(inode, end);
152}
153
Trond Myklebusta301b772007-02-06 11:07:15 -0800154/* A writeback failed: mark the page as bad, and invalidate the page cache */
155static void nfs_set_pageerror(struct page *page)
156{
157 SetPageError(page);
158 nfs_zap_mapping(page->mapping->host, page->mapping);
159}
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/* We can set the PG_uptodate flag if we see that a write request
162 * covers the full page.
163 */
164static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
165{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (PageUptodate(page))
167 return;
168 if (base != 0)
169 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500170 if (count != nfs_page_length(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500172 if (count != PAGE_CACHE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
Trond Myklebust49a70f22006-12-05 00:35:38 -0500174 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Trond Myklebuste21195a2006-12-05 00:35:39 -0500177static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 unsigned int offset, unsigned int count)
179{
180 struct nfs_page *req;
Trond Myklebuste21195a2006-12-05 00:35:39 -0500181 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Trond Myklebuste21195a2006-12-05 00:35:39 -0500183 for (;;) {
184 req = nfs_update_request(ctx, page, offset, count);
185 if (!IS_ERR(req))
186 break;
187 ret = PTR_ERR(req);
188 if (ret != -EBUSY)
189 return ret;
190 ret = nfs_wb_page(page->mapping->host, page);
191 if (ret != 0)
192 return ret;
193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 /* Update file length */
195 nfs_grow_file(page, offset, count);
196 /* Set the PG_uptodate flag? */
197 nfs_mark_uptodate(page, offset, count);
198 nfs_unlock_request(req);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100199 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202static int wb_priority(struct writeback_control *wbc)
203{
204 if (wbc->for_reclaim)
205 return FLUSH_HIGHPRI;
206 if (wbc->for_kupdate)
207 return FLUSH_LOWPRI;
208 return 0;
209}
210
211/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800212 * NFS congestion control
213 */
214
215int nfs_congestion_kb;
216
217#define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
218#define NFS_CONGESTION_OFF_THRESH \
219 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
220
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400221static int nfs_set_page_writeback(struct page *page)
Peter Zijlstra89a09142007-03-16 13:38:26 -0800222{
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400223 int ret = test_set_page_writeback(page);
224
225 if (!ret) {
Peter Zijlstra89a09142007-03-16 13:38:26 -0800226 struct inode *inode = page->mapping->host;
227 struct nfs_server *nfss = NFS_SERVER(inode);
228
229 if (atomic_inc_return(&nfss->writeback) >
230 NFS_CONGESTION_ON_THRESH)
231 set_bdi_congested(&nfss->backing_dev_info, WRITE);
232 }
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400233 return ret;
Peter Zijlstra89a09142007-03-16 13:38:26 -0800234}
235
236static void nfs_end_page_writeback(struct page *page)
237{
238 struct inode *inode = page->mapping->host;
239 struct nfs_server *nfss = NFS_SERVER(inode);
240
241 end_page_writeback(page);
242 if (atomic_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) {
243 clear_bdi_congested(&nfss->backing_dev_info, WRITE);
244 congestion_end(WRITE);
245 }
246}
247
248/*
Trond Myklebuste261f512006-12-05 00:35:41 -0500249 * Find an associated nfs write request, and prepare to flush it out
250 * Returns 1 if there was no write request, or if the request was
251 * already tagged by nfs_set_page_dirty.Returns 0 if the request
252 * was not tagged.
253 * May also return an error if the user signalled nfs_wait_on_request().
254 */
255static int nfs_page_mark_flush(struct page *page)
256{
257 struct nfs_page *req;
258 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
259 int ret;
260
261 spin_lock(req_lock);
262 for(;;) {
263 req = nfs_page_find_request_locked(page);
264 if (req == NULL) {
265 spin_unlock(req_lock);
266 return 1;
267 }
268 if (nfs_lock_request_dontget(req))
269 break;
270 /* Note: If we hold the page lock, as is the case in nfs_writepage,
271 * then the call to nfs_lock_request_dontget() will always
272 * succeed provided that someone hasn't already marked the
273 * request as dirty (in which case we don't care).
274 */
275 spin_unlock(req_lock);
276 ret = nfs_wait_on_request(req);
277 nfs_release_request(req);
278 if (ret != 0)
279 return ret;
280 spin_lock(req_lock);
281 }
282 spin_unlock(req_lock);
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400283 if (nfs_set_page_writeback(page) == 0)
Trond Myklebuste261f512006-12-05 00:35:41 -0500284 nfs_mark_request_dirty(req);
285 ret = test_bit(PG_NEED_FLUSH, &req->wb_flags);
286 nfs_unlock_request(req);
287 return ret;
288}
289
290/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 * Write an mmapped page to the server.
292 */
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500293static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 struct nfs_open_context *ctx;
296 struct inode *inode = page->mapping->host;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500297 unsigned offset;
Trond Myklebuste261f512006-12-05 00:35:41 -0500298 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Chuck Lever91d5b472006-03-20 13:44:14 -0500300 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
301 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
302
Trond Myklebuste261f512006-12-05 00:35:41 -0500303 err = nfs_page_mark_flush(page);
304 if (err <= 0)
305 goto out;
306 err = 0;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500307 offset = nfs_page_length(page);
308 if (!offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 goto out;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500310
Trond Myklebustd5308382005-11-04 15:33:38 -0500311 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (ctx == NULL) {
313 err = -EBADF;
314 goto out;
315 }
Trond Myklebust200baa22006-12-05 00:35:40 -0500316 err = nfs_writepage_setup(ctx, page, 0, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 put_nfs_open_context(ctx);
Trond Myklebuste261f512006-12-05 00:35:41 -0500318 if (err != 0)
319 goto out;
320 err = nfs_page_mark_flush(page);
321 if (err > 0)
322 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323out:
Trond Myklebust200baa22006-12-05 00:35:40 -0500324 if (!wbc->for_writepages)
Trond Myklebust02241bc2007-01-13 02:28:07 -0500325 nfs_flush_mapping(page->mapping, wbc, FLUSH_STABLE|wb_priority(wbc));
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500326 return err;
327}
328
329int nfs_writepage(struct page *page, struct writeback_control *wbc)
330{
331 int err;
332
333 err = nfs_writepage_locked(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return err;
336}
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
339{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct inode *inode = mapping->host;
341 int err;
342
Chuck Lever91d5b472006-03-20 13:44:14 -0500343 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 err = generic_writepages(mapping, wbc);
346 if (err)
347 return err;
Trond Myklebust28c69252006-09-16 13:04:50 -0400348 err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (err < 0)
350 goto out;
Chuck Lever91d5b472006-03-20 13:44:14 -0500351 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
Trond Myklebustd30c8342006-12-13 15:23:47 -0500352 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return err;
355}
356
357/*
358 * Insert a write request into an inode
359 */
360static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
361{
362 struct nfs_inode *nfsi = NFS_I(inode);
363 int error;
364
365 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
366 BUG_ON(error == -EEXIST);
367 if (error)
368 return error;
369 if (!nfsi->npages) {
370 igrab(inode);
371 nfs_begin_data_update(inode);
372 if (nfs_have_delegation(inode, FMODE_WRITE))
373 nfsi->change_attr++;
374 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500375 SetPagePrivate(req->wb_page);
Trond Myklebust277459d2006-12-05 00:35:35 -0500376 set_page_private(req->wb_page, (unsigned long)req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 nfsi->npages++;
378 atomic_inc(&req->wb_count);
379 return 0;
380}
381
382/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800383 * Remove a write request from an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 */
385static void nfs_inode_remove_request(struct nfs_page *req)
386{
387 struct inode *inode = req->wb_context->dentry->d_inode;
388 struct nfs_inode *nfsi = NFS_I(inode);
389
390 BUG_ON (!NFS_WBACK_BUSY(req));
391
392 spin_lock(&nfsi->req_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500393 set_page_private(req->wb_page, 0);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500394 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
396 nfsi->npages--;
397 if (!nfsi->npages) {
398 spin_unlock(&nfsi->req_lock);
Trond Myklebust951a1432005-06-22 17:16:30 +0000399 nfs_end_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 iput(inode);
401 } else
402 spin_unlock(&nfsi->req_lock);
403 nfs_clear_request(req);
404 nfs_release_request(req);
405}
406
407/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 * Add a request to the inode's dirty list.
409 */
410static void
411nfs_mark_request_dirty(struct nfs_page *req)
412{
413 struct inode *inode = req->wb_context->dentry->d_inode;
414 struct nfs_inode *nfsi = NFS_I(inode);
415
416 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000417 radix_tree_tag_set(&nfsi->nfs_page_tree,
418 req->wb_index, NFS_PAGE_TAG_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 nfs_list_add_request(req, &nfsi->dirty);
420 nfsi->ndirty++;
421 spin_unlock(&nfsi->req_lock);
Trond Myklebusta1803042006-12-05 00:45:19 -0500422 __mark_inode_dirty(inode, I_DIRTY_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Trond Myklebust61822ab2006-12-05 00:35:42 -0500425static void
426nfs_redirty_request(struct nfs_page *req)
427{
Trond Myklebust61822ab2006-12-05 00:35:42 -0500428 __set_page_dirty_nobuffers(req->wb_page);
429}
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431/*
432 * Check if a request is dirty
433 */
434static inline int
435nfs_dirty_request(struct nfs_page *req)
436{
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400437 struct page *page = req->wb_page;
438
439 if (page == NULL)
440 return 0;
441 return !PageWriteback(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
445/*
446 * Add a request to the inode's commit list.
447 */
448static void
449nfs_mark_request_commit(struct nfs_page *req)
450{
451 struct inode *inode = req->wb_context->dentry->d_inode;
452 struct nfs_inode *nfsi = NFS_I(inode);
453
454 spin_lock(&nfsi->req_lock);
455 nfs_list_add_request(req, &nfsi->commit);
456 nfsi->ncommit++;
457 spin_unlock(&nfsi->req_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700458 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebusta1803042006-12-05 00:45:19 -0500459 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461#endif
462
463/*
464 * Wait for a request to complete.
465 *
466 * Interruptible by signals only if mounted with intr flag.
467 */
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500468static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 struct nfs_inode *nfsi = NFS_I(inode);
471 struct nfs_page *req;
472 unsigned long idx_end, next;
473 unsigned int res = 0;
474 int error;
475
476 if (npages == 0)
477 idx_end = ~0;
478 else
479 idx_end = idx_start + npages - 1;
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 next = idx_start;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000482 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 -0700483 if (req->wb_index > idx_end)
484 break;
485
486 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000487 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 atomic_inc(&req->wb_count);
490 spin_unlock(&nfsi->req_lock);
491 error = nfs_wait_on_request(req);
492 nfs_release_request(req);
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500493 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (error < 0)
495 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 res++;
497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return res;
499}
500
Trond Myklebust83715ad2006-07-05 13:17:12 -0400501static void nfs_cancel_dirty_list(struct list_head *head)
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400502{
503 struct nfs_page *req;
504 while(!list_empty(head)) {
505 req = nfs_list_entry(head->next);
506 nfs_list_remove_request(req);
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400507 nfs_end_page_writeback(req->wb_page);
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400508 nfs_inode_remove_request(req);
509 nfs_clear_page_writeback(req);
510 }
511}
512
Trond Myklebust83715ad2006-07-05 13:17:12 -0400513static void nfs_cancel_commit_list(struct list_head *head)
514{
515 struct nfs_page *req;
516
517 while(!list_empty(head)) {
518 req = nfs_list_entry(head->next);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700519 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400520 nfs_list_remove_request(req);
521 nfs_inode_remove_request(req);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700522 nfs_unlock_request(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400523 }
524}
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
527/*
528 * nfs_scan_commit - Scan an inode for commit requests
529 * @inode: NFS inode to scan
530 * @dst: destination list
531 * @idx_start: lower bound of page->index to scan.
532 * @npages: idx_start + npages sets the upper bound to scan.
533 *
534 * Moves requests from the inode's 'commit' request list.
535 * The requests are *not* checked to ensure that they form a contiguous set.
536 */
537static int
538nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
539{
540 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000541 int res = 0;
542
543 if (nfsi->ncommit != 0) {
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400544 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000545 nfsi->ncommit -= res;
546 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
547 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return res;
550}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500551#else
552static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
553{
554 return 0;
555}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556#endif
557
Peter Zijlstra89a09142007-03-16 13:38:26 -0800558static int nfs_wait_on_write_congestion(struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Peter Zijlstra89a09142007-03-16 13:38:26 -0800560 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 struct backing_dev_info *bdi = mapping->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 int ret = 0;
563
564 might_sleep();
565
566 if (!bdi_write_congested(bdi))
567 return 0;
Chuck Lever91d5b472006-03-20 13:44:14 -0500568
Peter Zijlstra89a09142007-03-16 13:38:26 -0800569 nfs_inc_stats(inode, NFSIOS_CONGESTIONWAIT);
Chuck Lever91d5b472006-03-20 13:44:14 -0500570
Peter Zijlstra89a09142007-03-16 13:38:26 -0800571 do {
572 struct rpc_clnt *clnt = NFS_CLIENT(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 sigset_t oldset;
574
575 rpc_clnt_sigmask(clnt, &oldset);
Peter Zijlstra89a09142007-03-16 13:38:26 -0800576 ret = congestion_wait_interruptible(WRITE, HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 rpc_clnt_sigunmask(clnt, &oldset);
Peter Zijlstra89a09142007-03-16 13:38:26 -0800578 if (ret == -ERESTARTSYS)
579 break;
580 ret = 0;
581 } while (bdi_write_congested(bdi));
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return ret;
584}
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/*
587 * Try to update any existing write request, or create one if there is none.
588 * In order to match, the request's credentials must match those of
589 * the calling process.
590 *
591 * Note: Should always be called with the Page Lock held!
592 */
593static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
Trond Myklebuste21195a2006-12-05 00:35:39 -0500594 struct page *page, unsigned int offset, unsigned int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Peter Zijlstra89a09142007-03-16 13:38:26 -0800596 struct address_space *mapping = page->mapping;
597 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 struct nfs_inode *nfsi = NFS_I(inode);
599 struct nfs_page *req, *new = NULL;
600 unsigned long rqend, end;
601
602 end = offset + bytes;
603
Peter Zijlstra89a09142007-03-16 13:38:26 -0800604 if (nfs_wait_on_write_congestion(mapping))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return ERR_PTR(-ERESTARTSYS);
606 for (;;) {
607 /* Loop over all inode entries and see if we find
608 * A request for the page we wish to update
609 */
610 spin_lock(&nfsi->req_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500611 req = nfs_page_find_request_locked(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (req) {
613 if (!nfs_lock_request_dontget(req)) {
614 int error;
Trond Myklebust277459d2006-12-05 00:35:35 -0500615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 spin_unlock(&nfsi->req_lock);
617 error = nfs_wait_on_request(req);
618 nfs_release_request(req);
Neil Brown1dd594b2006-03-20 13:44:04 -0500619 if (error < 0) {
620 if (new)
621 nfs_release_request(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return ERR_PTR(error);
Neil Brown1dd594b2006-03-20 13:44:04 -0500623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 continue;
625 }
626 spin_unlock(&nfsi->req_lock);
627 if (new)
628 nfs_release_request(new);
629 break;
630 }
631
632 if (new) {
633 int error;
634 nfs_lock_request_dontget(new);
635 error = nfs_inode_add_request(inode, new);
636 if (error) {
637 spin_unlock(&nfsi->req_lock);
638 nfs_unlock_request(new);
639 return ERR_PTR(error);
640 }
641 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return new;
643 }
644 spin_unlock(&nfsi->req_lock);
645
646 new = nfs_create_request(ctx, inode, page, offset, bytes);
647 if (IS_ERR(new))
648 return new;
649 }
650
651 /* We have a request for our page.
652 * If the creds don't match, or the
653 * page addresses don't match,
654 * tell the caller to wait on the conflicting
655 * request.
656 */
657 rqend = req->wb_offset + req->wb_bytes;
658 if (req->wb_context != ctx
659 || req->wb_page != page
660 || !nfs_dirty_request(req)
661 || offset > rqend || end < req->wb_offset) {
662 nfs_unlock_request(req);
663 return ERR_PTR(-EBUSY);
664 }
665
666 /* Okay, the request matches. Update the region */
667 if (offset < req->wb_offset) {
668 req->wb_offset = offset;
669 req->wb_pgbase = offset;
670 req->wb_bytes = rqend - req->wb_offset;
671 }
672
673 if (end > rqend)
674 req->wb_bytes = end - req->wb_offset;
675
676 return req;
677}
678
679int nfs_flush_incompatible(struct file *file, struct page *page)
680{
681 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 struct nfs_page *req;
Trond Myklebust1a545332006-12-05 00:35:40 -0500683 int do_flush, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 /*
685 * Look for a request corresponding to this page. If there
686 * is one, and it belongs to another file, we flush it out
687 * before we try to copy anything into the page. Do this
688 * due to the lack of an ACCESS-type call in NFSv2.
689 * Also do the same if we find a request from an existing
690 * dropped page.
691 */
Trond Myklebust1a545332006-12-05 00:35:40 -0500692 do {
693 req = nfs_page_find_request(page);
694 if (req == NULL)
695 return 0;
696 do_flush = req->wb_page != page || req->wb_context != ctx
Trond Myklebuste261f512006-12-05 00:35:41 -0500697 || !nfs_dirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 nfs_release_request(req);
Trond Myklebust1a545332006-12-05 00:35:40 -0500699 if (!do_flush)
700 return 0;
701 status = nfs_wb_page(page->mapping->host, page);
702 } while (status == 0);
703 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
706/*
707 * Update and possibly write a cached page of an NFS file.
708 *
709 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
710 * things with a page scheduled for an RPC call (e.g. invalidate it).
711 */
712int nfs_updatepage(struct file *file, struct page *page,
713 unsigned int offset, unsigned int count)
714{
715 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 int status = 0;
718
Chuck Lever91d5b472006-03-20 13:44:14 -0500719 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800722 file->f_path.dentry->d_parent->d_name.name,
723 file->f_path.dentry->d_name.name, count,
Chuck Lever0bbacc42005-11-01 16:53:32 -0500724 (long long)(page_offset(page) +offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /* If we're not using byte range locks, and we know the page
727 * is entirely in cache, it may be more efficient to avoid
728 * fragmenting write requests.
729 */
Trond Myklebustab0a3db2005-06-22 17:16:30 +0000730 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
Trond Myklebust49a70f22006-12-05 00:35:38 -0500731 count = max(count + offset, nfs_page_length(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734
Trond Myklebuste21195a2006-12-05 00:35:39 -0500735 status = nfs_writepage_setup(ctx, page, offset, count);
Trond Myklebuste261f512006-12-05 00:35:41 -0500736 __set_page_dirty_nobuffers(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
739 status, (long long)i_size_read(inode));
740 if (status < 0)
Trond Myklebusta301b772007-02-06 11:07:15 -0800741 nfs_set_pageerror(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return status;
743}
744
745static void nfs_writepage_release(struct nfs_page *req)
746{
Peter Zijlstra89a09142007-03-16 13:38:26 -0800747 nfs_end_page_writeback(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
750 if (!PageError(req->wb_page)) {
751 if (NFS_NEED_RESCHED(req)) {
Trond Myklebust61822ab2006-12-05 00:35:42 -0500752 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 goto out;
754 } else if (NFS_NEED_COMMIT(req)) {
755 nfs_mark_request_commit(req);
756 goto out;
757 }
758 }
759 nfs_inode_remove_request(req);
760
761out:
762 nfs_clear_commit(req);
763 nfs_clear_reschedule(req);
764#else
765 nfs_inode_remove_request(req);
766#endif
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000767 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
770static inline int flush_task_priority(int how)
771{
772 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
773 case FLUSH_HIGHPRI:
774 return RPC_PRIORITY_HIGH;
775 case FLUSH_LOWPRI:
776 return RPC_PRIORITY_LOW;
777 }
778 return RPC_PRIORITY_NORMAL;
779}
780
781/*
782 * Set up the argument/result storage required for the RPC call.
783 */
784static void nfs_write_rpcsetup(struct nfs_page *req,
785 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500786 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 unsigned int count, unsigned int offset,
788 int how)
789{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -0500791 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 /* Set up the RPC argument and reply structs
794 * NB: take care not to mess about with data->commit et al. */
795
796 data->req = req;
797 data->inode = inode = req->wb_context->dentry->d_inode;
798 data->cred = req->wb_context->cred;
799
800 data->args.fh = NFS_FH(inode);
801 data->args.offset = req_offset(req) + offset;
802 data->args.pgbase = req->wb_pgbase + offset;
803 data->args.pages = data->pagevec;
804 data->args.count = count;
805 data->args.context = req->wb_context;
806
807 data->res.fattr = &data->fattr;
808 data->res.count = count;
809 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400810 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Trond Myklebust788e7a82006-03-20 13:44:27 -0500812 /* Set up the initial task struct. */
813 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
814 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 NFS_PROTO(inode)->write_setup(data, how);
816
817 data->task.tk_priority = flush_task_priority(how);
818 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Chuck Levera3f565b2007-01-31 12:14:01 -0500820 dprintk("NFS: %5u initiated write call "
821 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500822 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 inode->i_sb->s_id,
824 (long long)NFS_FILEID(inode),
825 count,
826 (unsigned long long)data->args.offset);
827}
828
829static void nfs_execute_write(struct nfs_write_data *data)
830{
831 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
832 sigset_t oldset;
833
834 rpc_clnt_sigmask(clnt, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 rpc_execute(&data->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 rpc_clnt_sigunmask(clnt, &oldset);
837}
838
839/*
840 * Generate multiple small requests to write out a single
841 * contiguous dirty area on one page.
842 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500843static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
845 struct nfs_page *req = nfs_list_entry(head->next);
846 struct page *page = req->wb_page;
847 struct nfs_write_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700848 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
849 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 int requests = 0;
851 LIST_HEAD(list);
852
853 nfs_list_remove_request(req);
854
855 nbytes = req->wb_bytes;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700856 do {
857 size_t len = min(nbytes, wsize);
858
859 data = nfs_writedata_alloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (!data)
861 goto out_bad;
862 list_add(&data->pages, &list);
863 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700864 nbytes -= len;
865 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 atomic_set(&req->wb_complete, requests);
867
868 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 offset = 0;
870 nbytes = req->wb_bytes;
871 do {
872 data = list_entry(list.next, struct nfs_write_data, pages);
873 list_del_init(&data->pages);
874
875 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 if (nbytes > wsize) {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500878 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
879 wsize, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 offset += wsize;
881 nbytes -= wsize;
882 } else {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500883 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
884 nbytes, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 nbytes = 0;
886 }
887 nfs_execute_write(data);
888 } while (nbytes != 0);
889
890 return 0;
891
892out_bad:
893 while (!list_empty(&list)) {
894 data = list_entry(list.next, struct nfs_write_data, pages);
895 list_del(&data->pages);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500896 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400898 nfs_end_page_writeback(req->wb_page);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500899 nfs_redirty_request(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000900 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return -ENOMEM;
902}
903
904/*
905 * Create an RPC task for the given write request and kick it.
906 * The page must have been locked by the caller.
907 *
908 * It may happen that the page we're passed is not marked dirty.
909 * This is the case if nfs_updatepage detects a conflicting request
910 * that has been written but not committed.
911 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500912static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
914 struct nfs_page *req;
915 struct page **pages;
916 struct nfs_write_data *data;
917 unsigned int count;
918
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700919 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (!data)
921 goto out_bad;
922
923 pages = data->pagevec;
924 count = 0;
925 while (!list_empty(head)) {
926 req = nfs_list_entry(head->next);
927 nfs_list_remove_request(req);
928 nfs_list_add_request(req, &data->pages);
929 ClearPageError(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 *pages++ = req->wb_page;
931 count += req->wb_bytes;
932 }
933 req = nfs_list_entry(data->pages.next);
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 /* Set up the argument struct */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500936 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 nfs_execute_write(data);
939 return 0;
940 out_bad:
941 while (!list_empty(head)) {
942 struct nfs_page *req = nfs_list_entry(head->next);
943 nfs_list_remove_request(req);
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400944 nfs_end_page_writeback(req->wb_page);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500945 nfs_redirty_request(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000946 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948 return -ENOMEM;
949}
950
Trond Myklebust7d46a492006-03-20 13:44:50 -0500951static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
953 LIST_HEAD(one_request);
Trond Myklebust7d46a492006-03-20 13:44:50 -0500954 int (*flush_one)(struct inode *, struct list_head *, int);
955 struct nfs_page *req;
956 int wpages = NFS_SERVER(inode)->wpages;
957 int wsize = NFS_SERVER(inode)->wsize;
958 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Trond Myklebust7d46a492006-03-20 13:44:50 -0500960 flush_one = nfs_flush_one;
961 if (wsize < PAGE_CACHE_SIZE)
962 flush_one = nfs_flush_multi;
963 /* For single writes, FLUSH_STABLE is more efficient */
964 if (npages <= wpages && npages == NFS_I(inode)->npages
965 && nfs_list_entry(head->next)->wb_bytes <= wsize)
966 how |= FLUSH_STABLE;
967
968 do {
969 nfs_coalesce_requests(head, &one_request, wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 req = nfs_list_entry(one_request.next);
Trond Myklebust7d46a492006-03-20 13:44:50 -0500971 error = flush_one(inode, &one_request, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (error < 0)
Trond Myklebust7d46a492006-03-20 13:44:50 -0500973 goto out_err;
974 } while (!list_empty(head));
975 return 0;
976out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 while (!list_empty(head)) {
978 req = nfs_list_entry(head->next);
979 nfs_list_remove_request(req);
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400980 nfs_end_page_writeback(req->wb_page);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500981 nfs_redirty_request(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000982 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984 return error;
985}
986
987/*
988 * Handle a write reply that flushed part of a page.
989 */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500990static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Trond Myklebust788e7a82006-03-20 13:44:27 -0500992 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 struct nfs_page *req = data->req;
994 struct page *page = req->wb_page;
995
996 dprintk("NFS: write (%s/%Ld %d@%Ld)",
997 req->wb_context->dentry->d_inode->i_sb->s_id,
998 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
999 req->wb_bytes,
1000 (long long)req_offset(req));
1001
Trond Myklebust788e7a82006-03-20 13:44:27 -05001002 if (nfs_writeback_done(task, data) != 0)
1003 return;
1004
1005 if (task->tk_status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -08001006 nfs_set_pageerror(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001007 req->wb_context->error = task->tk_status;
1008 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 } else {
1010#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1011 if (data->verf.committed < NFS_FILE_SYNC) {
1012 if (!NFS_NEED_COMMIT(req)) {
1013 nfs_defer_commit(req);
1014 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1015 dprintk(" defer commit\n");
1016 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1017 nfs_defer_reschedule(req);
1018 dprintk(" server reboot detected\n");
1019 }
1020 } else
1021#endif
1022 dprintk(" OK\n");
1023 }
1024
1025 if (atomic_dec_and_test(&req->wb_complete))
1026 nfs_writepage_release(req);
1027}
1028
Trond Myklebust788e7a82006-03-20 13:44:27 -05001029static const struct rpc_call_ops nfs_write_partial_ops = {
1030 .rpc_call_done = nfs_writeback_done_partial,
1031 .rpc_release = nfs_writedata_release,
1032};
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034/*
1035 * Handle a write reply that flushes a whole page.
1036 *
1037 * FIXME: There is an inherent race with invalidate_inode_pages and
1038 * writebacks since the page->count is kept > 1 for as long
1039 * as the page has a write request pending.
1040 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001041static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001043 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 struct nfs_page *req;
1045 struct page *page;
1046
Trond Myklebust788e7a82006-03-20 13:44:27 -05001047 if (nfs_writeback_done(task, data) != 0)
1048 return;
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 /* Update attributes as result of writeback. */
1051 while (!list_empty(&data->pages)) {
1052 req = nfs_list_entry(data->pages.next);
1053 nfs_list_remove_request(req);
1054 page = req->wb_page;
1055
1056 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1057 req->wb_context->dentry->d_inode->i_sb->s_id,
1058 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1059 req->wb_bytes,
1060 (long long)req_offset(req));
1061
Trond Myklebust788e7a82006-03-20 13:44:27 -05001062 if (task->tk_status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -08001063 nfs_set_pageerror(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001064 req->wb_context->error = task->tk_status;
Peter Zijlstra89a09142007-03-16 13:38:26 -08001065 nfs_end_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 nfs_inode_remove_request(req);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001067 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 goto next;
1069 }
Peter Zijlstra89a09142007-03-16 13:38:26 -08001070 nfs_end_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1073 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1074 nfs_inode_remove_request(req);
1075 dprintk(" OK\n");
1076 goto next;
1077 }
1078 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1079 nfs_mark_request_commit(req);
1080 dprintk(" marked for commit\n");
1081#else
1082 nfs_inode_remove_request(req);
1083#endif
1084 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001085 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 }
1087}
1088
Trond Myklebust788e7a82006-03-20 13:44:27 -05001089static const struct rpc_call_ops nfs_write_full_ops = {
1090 .rpc_call_done = nfs_writeback_done_full,
1091 .rpc_release = nfs_writedata_release,
1092};
1093
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095/*
1096 * This function is called when the WRITE call is complete.
1097 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001098int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 struct nfs_writeargs *argp = &data->args;
1101 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001102 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Chuck Levera3f565b2007-01-31 12:14:01 -05001104 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 task->tk_pid, task->tk_status);
1106
Chuck Leverf551e442006-09-20 14:33:04 -04001107 /*
1108 * ->write_done will attempt to use post-op attributes to detect
1109 * conflicting writes by other clients. A strict interpretation
1110 * of close-to-open would allow us to continue caching even if
1111 * another writer had changed the file, but some applications
1112 * depend on tighter cache coherency when writing.
1113 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001114 status = NFS_PROTO(data->inode)->write_done(task, data);
1115 if (status != 0)
1116 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001117 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1120 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1121 /* We tried a write call, but the server did not
1122 * commit data to stable storage even though we
1123 * requested it.
1124 * Note: There is a known bug in Tru64 < 5.0 in which
1125 * the server reports NFS_DATA_SYNC, but performs
1126 * NFS_FILE_SYNC. We therefore implement this checking
1127 * as a dprintk() in order to avoid filling syslog.
1128 */
1129 static unsigned long complain;
1130
1131 if (time_before(complain, jiffies)) {
1132 dprintk("NFS: faulty NFS server %s:"
1133 " (committed = %d) != (stable = %d)\n",
David Howells54ceac42006-08-22 20:06:13 -04001134 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 resp->verf->committed, argp->stable);
1136 complain = jiffies + 300 * HZ;
1137 }
1138 }
1139#endif
1140 /* Is this a short write? */
1141 if (task->tk_status >= 0 && resp->count < argp->count) {
1142 static unsigned long complain;
1143
Chuck Lever91d5b472006-03-20 13:44:14 -05001144 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 /* Has the server at least made some progress? */
1147 if (resp->count != 0) {
1148 /* Was this an NFSv2 write or an NFSv3 stable write? */
1149 if (resp->verf->committed != NFS_UNSTABLE) {
1150 /* Resend from where the server left off */
1151 argp->offset += resp->count;
1152 argp->pgbase += resp->count;
1153 argp->count -= resp->count;
1154 } else {
1155 /* Resend as a stable write in order to avoid
1156 * headaches in the case of a server crash.
1157 */
1158 argp->stable = NFS_FILE_SYNC;
1159 }
1160 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001161 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
1163 if (time_before(complain, jiffies)) {
1164 printk(KERN_WARNING
1165 "NFS: Server wrote zero bytes, expected %u.\n",
1166 argp->count);
1167 complain = jiffies + 300 * HZ;
1168 }
1169 /* Can't do anything about it except throw an error. */
1170 task->tk_status = -EIO;
1171 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001172 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
1175
1176#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001177void nfs_commit_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 nfs_commit_free(wdata);
1180}
1181
1182/*
1183 * Set up the argument/result storage required for the RPC call.
1184 */
1185static void nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001186 struct nfs_write_data *data,
1187 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001189 struct nfs_page *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001191 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 /* Set up the RPC argument and reply structs
1194 * NB: take care not to mess about with data->commit et al. */
1195
1196 list_splice_init(head, &data->pages);
1197 first = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 inode = first->wb_context->dentry->d_inode;
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 data->inode = inode;
1201 data->cred = first->wb_context->cred;
1202
1203 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001204 /* Note: we always request a commit of the entire inode */
1205 data->args.offset = 0;
1206 data->args.count = 0;
1207 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 data->res.fattr = &data->fattr;
1209 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001210 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001211
1212 /* Set up the initial task struct. */
1213 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1214 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 NFS_PROTO(inode)->commit_setup(data, how);
1216
1217 data->task.tk_priority = flush_task_priority(how);
1218 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Chuck Levera3f565b2007-01-31 12:14:01 -05001220 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
1223/*
1224 * Commit dirty pages
1225 */
1226static int
Chuck Lever40859d72005-11-30 18:09:02 -05001227nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 struct nfs_write_data *data;
1230 struct nfs_page *req;
1231
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001232 data = nfs_commit_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 if (!data)
1235 goto out_bad;
1236
1237 /* Set up the argument struct */
1238 nfs_commit_rpcsetup(head, data, how);
1239
1240 nfs_execute_write(data);
1241 return 0;
1242 out_bad:
1243 while (!list_empty(head)) {
1244 req = nfs_list_entry(head->next);
1245 nfs_list_remove_request(req);
1246 nfs_mark_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001247 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust5c2d97c2006-09-18 23:20:35 -04001248 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
1250 return -ENOMEM;
1251}
1252
1253/*
1254 * COMMIT call returned
1255 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001256static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001258 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Chuck Levera3f565b2007-01-31 12:14:01 -05001261 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 task->tk_pid, task->tk_status);
1263
Trond Myklebust788e7a82006-03-20 13:44:27 -05001264 /* Call the NFS version-specific code */
1265 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1266 return;
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 while (!list_empty(&data->pages)) {
1269 req = nfs_list_entry(data->pages.next);
1270 nfs_list_remove_request(req);
Christoph Lameterfd39fc82006-06-30 01:55:40 -07001271 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1274 req->wb_context->dentry->d_inode->i_sb->s_id,
1275 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1276 req->wb_bytes,
1277 (long long)req_offset(req));
1278 if (task->tk_status < 0) {
1279 req->wb_context->error = task->tk_status;
1280 nfs_inode_remove_request(req);
1281 dprintk(", error = %d\n", task->tk_status);
1282 goto next;
1283 }
1284
1285 /* Okay, COMMIT succeeded, apparently. Check the verifier
1286 * returned by the server against all stored verfs. */
1287 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1288 /* We have a match */
1289 nfs_inode_remove_request(req);
1290 dprintk(" OK\n");
1291 goto next;
1292 }
1293 /* We have a mismatch. Write the page again */
1294 dprintk(" mismatch\n");
Trond Myklebust61822ab2006-12-05 00:35:42 -05001295 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001297 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001300
1301static const struct rpc_call_ops nfs_commit_ops = {
1302 .rpc_call_done = nfs_commit_done,
1303 .rpc_release = nfs_commit_release,
1304};
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001305#else
1306static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1307{
1308 return 0;
1309}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310#endif
1311
Trond Myklebust3f442542006-09-17 14:46:44 -04001312static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
Trond Myklebust28c69252006-09-16 13:04:50 -04001314 struct nfs_inode *nfsi = NFS_I(mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 LIST_HEAD(head);
Trond Myklebust3f442542006-09-17 14:46:44 -04001316 long res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 spin_lock(&nfsi->req_lock);
Trond Myklebust3f442542006-09-17 14:46:44 -04001319 res = nfs_scan_dirty(mapping, wbc, &head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 spin_unlock(&nfsi->req_lock);
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001321 if (res) {
Trond Myklebust28c69252006-09-16 13:04:50 -04001322 int error = nfs_flush_list(mapping->host, &head, res, how);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001323 if (error < 0)
1324 return error;
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return res;
1327}
1328
1329#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001330int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
1332 struct nfs_inode *nfsi = NFS_I(inode);
1333 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001334 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001337 res = nfs_scan_commit(inode, &head, 0, 0);
1338 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001340 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001341 if (error < 0)
1342 return error;
1343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return res;
1345}
1346#endif
1347
Trond Myklebust1c759502006-10-09 16:18:38 -04001348long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Trond Myklebust1c759502006-10-09 16:18:38 -04001350 struct inode *inode = mapping->host;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001351 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c759502006-10-09 16:18:38 -04001352 unsigned long idx_start, idx_end;
1353 unsigned int npages = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001354 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001355 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebust3f442542006-09-17 14:46:44 -04001356 long pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Trond Myklebust1c759502006-10-09 16:18:38 -04001358 /* FIXME */
1359 if (wbc->range_cyclic)
1360 idx_start = 0;
1361 else {
1362 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1363 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1364 if (idx_end > idx_start) {
1365 unsigned long l_npages = 1 + idx_end - idx_start;
1366 npages = l_npages;
1367 if (sizeof(npages) != sizeof(l_npages) &&
1368 (unsigned long)npages != l_npages)
1369 npages = 0;
1370 }
1371 }
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001372 how &= ~FLUSH_NOCOMMIT;
1373 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 do {
Trond Myklebust1c759502006-10-09 16:18:38 -04001375 wbc->pages_skipped = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001376 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1377 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001378 continue;
Trond Myklebust1c759502006-10-09 16:18:38 -04001379 pages = nfs_scan_dirty(mapping, wbc, &head);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001380 if (pages != 0) {
1381 spin_unlock(&nfsi->req_lock);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001382 if (how & FLUSH_INVALIDATE) {
Trond Myklebust83715ad2006-07-05 13:17:12 -04001383 nfs_cancel_dirty_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001384 ret = pages;
1385 } else
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001386 ret = nfs_flush_list(inode, &head, pages, how);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001387 spin_lock(&nfsi->req_lock);
1388 continue;
1389 }
Trond Myklebust1c759502006-10-09 16:18:38 -04001390 if (wbc->pages_skipped != 0)
1391 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001392 if (nocommit)
1393 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001394 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebust1c759502006-10-09 16:18:38 -04001395 if (pages == 0) {
1396 if (wbc->pages_skipped != 0)
1397 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001398 break;
Trond Myklebust1c759502006-10-09 16:18:38 -04001399 }
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001400 if (how & FLUSH_INVALIDATE) {
1401 spin_unlock(&nfsi->req_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001402 nfs_cancel_commit_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001403 ret = pages;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001404 spin_lock(&nfsi->req_lock);
1405 continue;
1406 }
1407 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001408 spin_unlock(&nfsi->req_lock);
1409 ret = nfs_commit_list(inode, &head, how);
1410 spin_lock(&nfsi->req_lock);
1411 } while (ret >= 0);
1412 spin_unlock(&nfsi->req_lock);
1413 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
Trond Myklebust1c759502006-10-09 16:18:38 -04001416/*
1417 * flush the inode to disk.
1418 */
1419int nfs_wb_all(struct inode *inode)
1420{
1421 struct address_space *mapping = inode->i_mapping;
1422 struct writeback_control wbc = {
1423 .bdi = mapping->backing_dev_info,
1424 .sync_mode = WB_SYNC_ALL,
1425 .nr_to_write = LONG_MAX,
Trond Myklebust61822ab2006-12-05 00:35:42 -05001426 .for_writepages = 1,
Trond Myklebust1c759502006-10-09 16:18:38 -04001427 .range_cyclic = 1,
1428 };
1429 int ret;
1430
Trond Myklebust61822ab2006-12-05 00:35:42 -05001431 ret = generic_writepages(mapping, &wbc);
1432 if (ret < 0)
1433 goto out;
Trond Myklebust1c759502006-10-09 16:18:38 -04001434 ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
1435 if (ret >= 0)
1436 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001437out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001438 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Trond Myklebust1c759502006-10-09 16:18:38 -04001439 return ret;
1440}
1441
1442int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
1443{
1444 struct writeback_control wbc = {
1445 .bdi = mapping->backing_dev_info,
1446 .sync_mode = WB_SYNC_ALL,
1447 .nr_to_write = LONG_MAX,
1448 .range_start = range_start,
1449 .range_end = range_end,
Trond Myklebust61822ab2006-12-05 00:35:42 -05001450 .for_writepages = 1,
Trond Myklebust1c759502006-10-09 16:18:38 -04001451 };
1452 int ret;
1453
Trond Myklebust61822ab2006-12-05 00:35:42 -05001454 if (!(how & FLUSH_NOWRITEPAGE)) {
1455 ret = generic_writepages(mapping, &wbc);
1456 if (ret < 0)
1457 goto out;
1458 }
Trond Myklebust1c759502006-10-09 16:18:38 -04001459 ret = nfs_sync_mapping_wait(mapping, &wbc, how);
1460 if (ret >= 0)
1461 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001462out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001463 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Trond Myklebust1c759502006-10-09 16:18:38 -04001464 return ret;
1465}
1466
Trond Myklebust61822ab2006-12-05 00:35:42 -05001467int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
Trond Myklebust1c759502006-10-09 16:18:38 -04001468{
1469 loff_t range_start = page_offset(page);
1470 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001471 struct writeback_control wbc = {
1472 .bdi = page->mapping->backing_dev_info,
1473 .sync_mode = WB_SYNC_ALL,
1474 .nr_to_write = LONG_MAX,
1475 .range_start = range_start,
1476 .range_end = range_end,
1477 };
1478 int ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001479
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001480 BUG_ON(!PageLocked(page));
1481 if (!(how & FLUSH_NOWRITEPAGE) && clear_page_dirty_for_io(page)) {
1482 ret = nfs_writepage_locked(page, &wbc);
1483 if (ret < 0)
1484 goto out;
1485 }
Trond Myklebustf40313a2007-01-13 02:28:08 -05001486 if (!PagePrivate(page))
1487 return 0;
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001488 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1489 if (ret >= 0)
1490 return 0;
1491out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001492 __mark_inode_dirty(inode, I_DIRTY_PAGES);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001493 return ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001494}
1495
1496/*
1497 * Write back all requests on one page - we do this before reading it.
1498 */
1499int nfs_wb_page(struct inode *inode, struct page* page)
1500{
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001501 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
Trond Myklebust1c759502006-10-09 16:18:38 -04001502}
1503
Trond Myklebust1a545332006-12-05 00:35:40 -05001504int nfs_set_page_dirty(struct page *page)
1505{
1506 struct nfs_page *req;
1507
1508 req = nfs_page_find_request(page);
1509 if (req != NULL) {
1510 /* Mark any existing write requests for flushing */
1511 set_bit(PG_NEED_FLUSH, &req->wb_flags);
1512 nfs_release_request(req);
1513 }
1514 return __set_page_dirty_nobuffers(page);
1515}
1516
Trond Myklebust1c759502006-10-09 16:18:38 -04001517
David Howellsf7b422b2006-06-09 09:34:33 -04001518int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
1520 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1521 sizeof(struct nfs_write_data),
1522 0, SLAB_HWCACHE_ALIGN,
1523 NULL, NULL);
1524 if (nfs_wdata_cachep == NULL)
1525 return -ENOMEM;
1526
Matthew Dobson93d23412006-03-26 01:37:50 -08001527 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1528 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if (nfs_wdata_mempool == NULL)
1530 return -ENOMEM;
1531
Matthew Dobson93d23412006-03-26 01:37:50 -08001532 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1533 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if (nfs_commit_mempool == NULL)
1535 return -ENOMEM;
1536
Peter Zijlstra89a09142007-03-16 13:38:26 -08001537 /*
1538 * NFS congestion size, scale with available memory.
1539 *
1540 * 64MB: 8192k
1541 * 128MB: 11585k
1542 * 256MB: 16384k
1543 * 512MB: 23170k
1544 * 1GB: 32768k
1545 * 2GB: 46340k
1546 * 4GB: 65536k
1547 * 8GB: 92681k
1548 * 16GB: 131072k
1549 *
1550 * This allows larger machines to have larger/more transfers.
1551 * Limit the default to 256M
1552 */
1553 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1554 if (nfs_congestion_kb > 256*1024)
1555 nfs_congestion_kb = 256*1024;
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 return 0;
1558}
1559
David Brownell266bee82006-06-27 12:59:15 -07001560void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
1562 mempool_destroy(nfs_commit_mempool);
1563 mempool_destroy(nfs_wdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001564 kmem_cache_destroy(nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566