aboutsummaryrefslogtreecommitdiffstats
path: root/fs/nfs/write.c
diff options
context:
space:
mode:
authorDavid Woodhouse <dwmw2@infradead.org>2007-01-17 18:34:51 -0500
committerDavid Woodhouse <dwmw2@infradead.org>2007-01-17 18:34:51 -0500
commit9cdf083f981b8d37b3212400a359368661385099 (patch)
treeaa15a6a08ad87e650dea40fb59b3180bef0d345b /fs/nfs/write.c
parente499e01d234a31d59679b7b1e1cf628d917ba49a (diff)
parenta8b3485287731978899ced11f24628c927890e78 (diff)
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
Diffstat (limited to 'fs/nfs/write.c')
-rw-r--r--fs/nfs/write.c608
1 files changed, 307 insertions, 301 deletions
diff --git a/fs/nfs/write.c b/fs/nfs/write.c
index 883dd4a1c157..345492e78643 100644
--- a/fs/nfs/write.c
+++ b/fs/nfs/write.c
@@ -63,6 +63,7 @@
63#include <linux/smp_lock.h> 63#include <linux/smp_lock.h>
64 64
65#include "delegation.h" 65#include "delegation.h"
66#include "internal.h"
66#include "iostat.h" 67#include "iostat.h"
67 68
68#define NFSDBG_FACILITY NFSDBG_PAGECACHE 69#define NFSDBG_FACILITY NFSDBG_PAGECACHE
@@ -74,18 +75,17 @@
74 * Local function declarations 75 * Local function declarations
75 */ 76 */
76static struct nfs_page * nfs_update_request(struct nfs_open_context*, 77static struct nfs_page * nfs_update_request(struct nfs_open_context*,
77 struct inode *,
78 struct page *, 78 struct page *,
79 unsigned int, unsigned int); 79 unsigned int, unsigned int);
80static void nfs_mark_request_dirty(struct nfs_page *req);
80static int nfs_wait_on_write_congestion(struct address_space *, int); 81static int nfs_wait_on_write_congestion(struct address_space *, int);
81static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int); 82static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int);
82static int nfs_flush_inode(struct inode *inode, unsigned long idx_start, 83static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how);
83 unsigned int npages, int how);
84static const struct rpc_call_ops nfs_write_partial_ops; 84static const struct rpc_call_ops nfs_write_partial_ops;
85static const struct rpc_call_ops nfs_write_full_ops; 85static const struct rpc_call_ops nfs_write_full_ops;
86static const struct rpc_call_ops nfs_commit_ops; 86static const struct rpc_call_ops nfs_commit_ops;
87 87
88static kmem_cache_t *nfs_wdata_cachep; 88static struct kmem_cache *nfs_wdata_cachep;
89static mempool_t *nfs_wdata_mempool; 89static mempool_t *nfs_wdata_mempool;
90static mempool_t *nfs_commit_mempool; 90static mempool_t *nfs_commit_mempool;
91 91
@@ -93,7 +93,7 @@ static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
93 93
94struct nfs_write_data *nfs_commit_alloc(void) 94struct nfs_write_data *nfs_commit_alloc(void)
95{ 95{
96 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS); 96 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
97 97
98 if (p) { 98 if (p) {
99 memset(p, 0, sizeof(*p)); 99 memset(p, 0, sizeof(*p));
@@ -102,17 +102,23 @@ struct nfs_write_data *nfs_commit_alloc(void)
102 return p; 102 return p;
103} 103}
104 104
105void nfs_commit_free(struct nfs_write_data *p) 105void nfs_commit_rcu_free(struct rcu_head *head)
106{ 106{
107 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
107 if (p && (p->pagevec != &p->page_array[0])) 108 if (p && (p->pagevec != &p->page_array[0]))
108 kfree(p->pagevec); 109 kfree(p->pagevec);
109 mempool_free(p, nfs_commit_mempool); 110 mempool_free(p, nfs_commit_mempool);
110} 111}
111 112
113void nfs_commit_free(struct nfs_write_data *wdata)
114{
115 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
116}
117
112struct nfs_write_data *nfs_writedata_alloc(size_t len) 118struct nfs_write_data *nfs_writedata_alloc(size_t len)
113{ 119{
114 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; 120 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
115 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS); 121 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
116 122
117 if (p) { 123 if (p) {
118 memset(p, 0, sizeof(*p)); 124 memset(p, 0, sizeof(*p));
@@ -131,18 +137,47 @@ struct nfs_write_data *nfs_writedata_alloc(size_t len)
131 return p; 137 return p;
132} 138}
133 139
134static void nfs_writedata_free(struct nfs_write_data *p) 140static void nfs_writedata_rcu_free(struct rcu_head *head)
135{ 141{
142 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
136 if (p && (p->pagevec != &p->page_array[0])) 143 if (p && (p->pagevec != &p->page_array[0]))
137 kfree(p->pagevec); 144 kfree(p->pagevec);
138 mempool_free(p, nfs_wdata_mempool); 145 mempool_free(p, nfs_wdata_mempool);
139} 146}
140 147
148static void nfs_writedata_free(struct nfs_write_data *wdata)
149{
150 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
151}
152
141void nfs_writedata_release(void *wdata) 153void nfs_writedata_release(void *wdata)
142{ 154{
143 nfs_writedata_free(wdata); 155 nfs_writedata_free(wdata);
144} 156}
145 157
158static struct nfs_page *nfs_page_find_request_locked(struct page *page)
159{
160 struct nfs_page *req = NULL;
161
162 if (PagePrivate(page)) {
163 req = (struct nfs_page *)page_private(page);
164 if (req != NULL)
165 atomic_inc(&req->wb_count);
166 }
167 return req;
168}
169
170static struct nfs_page *nfs_page_find_request(struct page *page)
171{
172 struct nfs_page *req = NULL;
173 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
174
175 spin_lock(req_lock);
176 req = nfs_page_find_request_locked(page);
177 spin_unlock(req_lock);
178 return req;
179}
180
146/* Adjust the file length if we're writing beyond the end */ 181/* Adjust the file length if we're writing beyond the end */
147static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count) 182static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
148{ 183{
@@ -164,113 +199,34 @@ static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int c
164 */ 199 */
165static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count) 200static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
166{ 201{
167 loff_t end_offs;
168
169 if (PageUptodate(page)) 202 if (PageUptodate(page))
170 return; 203 return;
171 if (base != 0) 204 if (base != 0)
172 return; 205 return;
173 if (count == PAGE_CACHE_SIZE) { 206 if (count != nfs_page_length(page))
174 SetPageUptodate(page);
175 return;
176 }
177
178 end_offs = i_size_read(page->mapping->host) - 1;
179 if (end_offs < 0)
180 return; 207 return;
181 /* Is this the last page? */ 208 if (count != PAGE_CACHE_SIZE)
182 if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT))
183 return;
184 /* This is the last page: set PG_uptodate if we cover the entire
185 * extent of the data, then zero the rest of the page.
186 */
187 if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) {
188 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count); 209 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
189 SetPageUptodate(page); 210 SetPageUptodate(page);
190 }
191} 211}
192 212
193/* 213static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
194 * Write a page synchronously.
195 * Offset is the data offset within the page.
196 */
197static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
198 struct page *page, unsigned int offset, unsigned int count,
199 int how)
200{
201 unsigned int wsize = NFS_SERVER(inode)->wsize;
202 int result, written = 0;
203 struct nfs_write_data *wdata;
204
205 wdata = nfs_writedata_alloc(wsize);
206 if (!wdata)
207 return -ENOMEM;
208
209 wdata->flags = how;
210 wdata->cred = ctx->cred;
211 wdata->inode = inode;
212 wdata->args.fh = NFS_FH(inode);
213 wdata->args.context = ctx;
214 wdata->args.pages = &page;
215 wdata->args.stable = NFS_FILE_SYNC;
216 wdata->args.pgbase = offset;
217 wdata->args.count = wsize;
218 wdata->res.fattr = &wdata->fattr;
219 wdata->res.verf = &wdata->verf;
220
221 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
222 inode->i_sb->s_id,
223 (long long)NFS_FILEID(inode),
224 count, (long long)(page_offset(page) + offset));
225
226 set_page_writeback(page);
227 nfs_begin_data_update(inode);
228 do {
229 if (count < wsize)
230 wdata->args.count = count;
231 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
232
233 result = NFS_PROTO(inode)->write(wdata);
234
235 if (result < 0) {
236 /* Must mark the page invalid after I/O error */
237 ClearPageUptodate(page);
238 goto io_error;
239 }
240 if (result < wdata->args.count)
241 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
242 wdata->args.count, result);
243
244 wdata->args.offset += result;
245 wdata->args.pgbase += result;
246 written += result;
247 count -= result;
248 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
249 } while (count);
250 /* Update file length */
251 nfs_grow_file(page, offset, written);
252 /* Set the PG_uptodate flag? */
253 nfs_mark_uptodate(page, offset, written);
254
255 if (PageError(page))
256 ClearPageError(page);
257
258io_error:
259 nfs_end_data_update(inode);
260 end_page_writeback(page);
261 nfs_writedata_free(wdata);
262 return written ? written : result;
263}
264
265static int nfs_writepage_async(struct nfs_open_context *ctx,
266 struct inode *inode, struct page *page,
267 unsigned int offset, unsigned int count) 214 unsigned int offset, unsigned int count)
268{ 215{
269 struct nfs_page *req; 216 struct nfs_page *req;
217 int ret;
270 218
271 req = nfs_update_request(ctx, inode, page, offset, count); 219 for (;;) {
272 if (IS_ERR(req)) 220 req = nfs_update_request(ctx, page, offset, count);
273 return PTR_ERR(req); 221 if (!IS_ERR(req))
222 break;
223 ret = PTR_ERR(req);
224 if (ret != -EBUSY)
225 return ret;
226 ret = nfs_wb_page(page->mapping->host, page);
227 if (ret != 0)
228 return ret;
229 }
274 /* Update file length */ 230 /* Update file length */
275 nfs_grow_file(page, offset, count); 231 nfs_grow_file(page, offset, count);
276 /* Set the PG_uptodate flag? */ 232 /* Set the PG_uptodate flag? */
@@ -289,73 +245,94 @@ static int wb_priority(struct writeback_control *wbc)
289} 245}
290 246
291/* 247/*
248 * Find an associated nfs write request, and prepare to flush it out
249 * Returns 1 if there was no write request, or if the request was
250 * already tagged by nfs_set_page_dirty.Returns 0 if the request
251 * was not tagged.
252 * May also return an error if the user signalled nfs_wait_on_request().
253 */
254static int nfs_page_mark_flush(struct page *page)
255{
256 struct nfs_page *req;
257 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
258 int ret;
259
260 spin_lock(req_lock);
261 for(;;) {
262 req = nfs_page_find_request_locked(page);
263 if (req == NULL) {
264 spin_unlock(req_lock);
265 return 1;
266 }
267 if (nfs_lock_request_dontget(req))
268 break;
269 /* Note: If we hold the page lock, as is the case in nfs_writepage,
270 * then the call to nfs_lock_request_dontget() will always
271 * succeed provided that someone hasn't already marked the
272 * request as dirty (in which case we don't care).
273 */
274 spin_unlock(req_lock);
275 ret = nfs_wait_on_request(req);
276 nfs_release_request(req);
277 if (ret != 0)
278 return ret;
279 spin_lock(req_lock);
280 }
281 spin_unlock(req_lock);
282 if (test_and_set_bit(PG_FLUSHING, &req->wb_flags) == 0) {
283 nfs_mark_request_dirty(req);
284 set_page_writeback(page);
285 }
286 ret = test_bit(PG_NEED_FLUSH, &req->wb_flags);
287 nfs_unlock_request(req);
288 return ret;
289}
290
291/*
292 * Write an mmapped page to the server. 292 * Write an mmapped page to the server.
293 */ 293 */
294int nfs_writepage(struct page *page, struct writeback_control *wbc) 294static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
295{ 295{
296 struct nfs_open_context *ctx; 296 struct nfs_open_context *ctx;
297 struct inode *inode = page->mapping->host; 297 struct inode *inode = page->mapping->host;
298 unsigned long end_index; 298 unsigned offset;
299 unsigned offset = PAGE_CACHE_SIZE;
300 loff_t i_size = i_size_read(inode);
301 int inode_referenced = 0;
302 int priority = wb_priority(wbc);
303 int err; 299 int err;
304 300
305 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); 301 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
306 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1); 302 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
307 303
308 /* 304 err = nfs_page_mark_flush(page);
309 * Note: We need to ensure that we have a reference to the inode 305 if (err <= 0)
310 * if we are to do asynchronous writes. If not, waiting 306 goto out;
311 * in nfs_wait_on_request() may deadlock with clear_inode(). 307 err = 0;
312 * 308 offset = nfs_page_length(page);
313 * If igrab() fails here, then it is in any case safe to 309 if (!offset)
314 * call nfs_wb_page(), since there will be no pending writes.
315 */
316 if (igrab(inode) != 0)
317 inode_referenced = 1;
318 end_index = i_size >> PAGE_CACHE_SHIFT;
319
320 /* Ensure we've flushed out any previous writes */
321 nfs_wb_page_priority(inode, page, priority);
322
323 /* easy case */
324 if (page->index < end_index)
325 goto do_it;
326 /* things got complicated... */
327 offset = i_size & (PAGE_CACHE_SIZE-1);
328
329 /* OK, are we completely out? */
330 err = 0; /* potential race with truncate - ignore */
331 if (page->index >= end_index+1 || !offset)
332 goto out; 310 goto out;
333do_it: 311
334 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE); 312 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
335 if (ctx == NULL) { 313 if (ctx == NULL) {
336 err = -EBADF; 314 err = -EBADF;
337 goto out; 315 goto out;
338 } 316 }
339 lock_kernel(); 317 err = nfs_writepage_setup(ctx, page, 0, offset);
340 if (!IS_SYNC(inode) && inode_referenced) {
341 err = nfs_writepage_async(ctx, inode, page, 0, offset);
342 if (!wbc->for_writepages)
343 nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
344 } 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); 318 put_nfs_open_context(ctx);
319 if (err != 0)
320 goto out;
321 err = nfs_page_mark_flush(page);
322 if (err > 0)
323 err = 0;
355out: 324out:
325 if (!wbc->for_writepages)
326 nfs_flush_mapping(page->mapping, wbc, wb_priority(wbc));
327 return err;
328}
329
330int nfs_writepage(struct page *page, struct writeback_control *wbc)
331{
332 int err;
333
334 err = nfs_writepage_locked(page, wbc);
356 unlock_page(page); 335 unlock_page(page);
357 if (inode_referenced)
358 iput(inode);
359 return err; 336 return err;
360} 337}
361 338
@@ -379,21 +356,18 @@ int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
379 return 0; 356 return 0;
380 nfs_wait_on_write_congestion(mapping, 0); 357 nfs_wait_on_write_congestion(mapping, 0);
381 } 358 }
382 err = nfs_flush_inode(inode, 0, 0, wb_priority(wbc)); 359 err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
383 if (err < 0) 360 if (err < 0)
384 goto out; 361 goto out;
385 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err); 362 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
386 wbc->nr_to_write -= err;
387 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) { 363 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
388 err = nfs_wait_on_requests(inode, 0, 0); 364 err = nfs_wait_on_requests(inode, 0, 0);
389 if (err < 0) 365 if (err < 0)
390 goto out; 366 goto out;
391 } 367 }
392 err = nfs_commit_inode(inode, wb_priority(wbc)); 368 err = nfs_commit_inode(inode, wb_priority(wbc));
393 if (err > 0) { 369 if (err > 0)
394 wbc->nr_to_write -= err;
395 err = 0; 370 err = 0;
396 }
397out: 371out:
398 clear_bit(BDI_write_congested, &bdi->state); 372 clear_bit(BDI_write_congested, &bdi->state);
399 wake_up_all(&nfs_write_congestion); 373 wake_up_all(&nfs_write_congestion);
@@ -420,6 +394,7 @@ static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
420 nfsi->change_attr++; 394 nfsi->change_attr++;
421 } 395 }
422 SetPagePrivate(req->wb_page); 396 SetPagePrivate(req->wb_page);
397 set_page_private(req->wb_page, (unsigned long)req);
423 nfsi->npages++; 398 nfsi->npages++;
424 atomic_inc(&req->wb_count); 399 atomic_inc(&req->wb_count);
425 return 0; 400 return 0;
@@ -436,6 +411,7 @@ static void nfs_inode_remove_request(struct nfs_page *req)
436 BUG_ON (!NFS_WBACK_BUSY(req)); 411 BUG_ON (!NFS_WBACK_BUSY(req));
437 412
438 spin_lock(&nfsi->req_lock); 413 spin_lock(&nfsi->req_lock);
414 set_page_private(req->wb_page, 0);
439 ClearPagePrivate(req->wb_page); 415 ClearPagePrivate(req->wb_page);
440 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index); 416 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
441 nfsi->npages--; 417 nfsi->npages--;
@@ -450,33 +426,6 @@ static void nfs_inode_remove_request(struct nfs_page *req)
450} 426}
451 427
452/* 428/*
453 * Find a request
454 */
455static inline struct nfs_page *
456_nfs_find_request(struct inode *inode, unsigned long index)
457{
458 struct nfs_inode *nfsi = NFS_I(inode);
459 struct nfs_page *req;
460
461 req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index);
462 if (req)
463 atomic_inc(&req->wb_count);
464 return req;
465}
466
467static struct nfs_page *
468nfs_find_request(struct inode *inode, unsigned long index)
469{
470 struct nfs_page *req;
471 struct nfs_inode *nfsi = NFS_I(inode);
472
473 spin_lock(&nfsi->req_lock);
474 req = _nfs_find_request(inode, index);
475 spin_unlock(&nfsi->req_lock);
476 return req;
477}
478
479/*
480 * Add a request to the inode's dirty list. 429 * Add a request to the inode's dirty list.
481 */ 430 */
482static void 431static void
@@ -491,8 +440,14 @@ nfs_mark_request_dirty(struct nfs_page *req)
491 nfs_list_add_request(req, &nfsi->dirty); 440 nfs_list_add_request(req, &nfsi->dirty);
492 nfsi->ndirty++; 441 nfsi->ndirty++;
493 spin_unlock(&nfsi->req_lock); 442 spin_unlock(&nfsi->req_lock);
494 inc_zone_page_state(req->wb_page, NR_FILE_DIRTY); 443 __mark_inode_dirty(inode, I_DIRTY_PAGES);
495 mark_inode_dirty(inode); 444}
445
446static void
447nfs_redirty_request(struct nfs_page *req)
448{
449 clear_bit(PG_FLUSHING, &req->wb_flags);
450 __set_page_dirty_nobuffers(req->wb_page);
496} 451}
497 452
498/* 453/*
@@ -501,8 +456,7 @@ nfs_mark_request_dirty(struct nfs_page *req)
501static inline int 456static inline int
502nfs_dirty_request(struct nfs_page *req) 457nfs_dirty_request(struct nfs_page *req)
503{ 458{
504 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode); 459 return test_bit(PG_FLUSHING, &req->wb_flags) == 0;
505 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
506} 460}
507 461
508#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 462#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
@@ -520,7 +474,7 @@ nfs_mark_request_commit(struct nfs_page *req)
520 nfsi->ncommit++; 474 nfsi->ncommit++;
521 spin_unlock(&nfsi->req_lock); 475 spin_unlock(&nfsi->req_lock);
522 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 476 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
523 mark_inode_dirty(inode); 477 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
524} 478}
525#endif 479#endif
526 480
@@ -597,31 +551,6 @@ static void nfs_cancel_commit_list(struct list_head *head)
597 } 551 }
598} 552}
599 553
600/*
601 * nfs_scan_dirty - Scan an inode for dirty requests
602 * @inode: NFS inode to scan
603 * @dst: destination list
604 * @idx_start: lower bound of page->index to scan.
605 * @npages: idx_start + npages sets the upper bound to scan.
606 *
607 * Moves requests from the inode's dirty page list.
608 * The requests are *not* checked to ensure that they form a contiguous set.
609 */
610static int
611nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
612{
613 struct nfs_inode *nfsi = NFS_I(inode);
614 int res = 0;
615
616 if (nfsi->ndirty != 0) {
617 res = nfs_scan_lock_dirty(nfsi, dst, idx_start, npages);
618 nfsi->ndirty -= res;
619 if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
620 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
621 }
622 return res;
623}
624
625#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 554#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
626/* 555/*
627 * nfs_scan_commit - Scan an inode for commit requests 556 * nfs_scan_commit - Scan an inode for commit requests
@@ -698,27 +627,27 @@ static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
698 * Note: Should always be called with the Page Lock held! 627 * Note: Should always be called with the Page Lock held!
699 */ 628 */
700static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx, 629static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
701 struct inode *inode, struct page *page, 630 struct page *page, unsigned int offset, unsigned int bytes)
702 unsigned int offset, unsigned int bytes)
703{ 631{
704 struct nfs_server *server = NFS_SERVER(inode); 632 struct inode *inode = page->mapping->host;
705 struct nfs_inode *nfsi = NFS_I(inode); 633 struct nfs_inode *nfsi = NFS_I(inode);
706 struct nfs_page *req, *new = NULL; 634 struct nfs_page *req, *new = NULL;
707 unsigned long rqend, end; 635 unsigned long rqend, end;
708 636
709 end = offset + bytes; 637 end = offset + bytes;
710 638
711 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR)) 639 if (nfs_wait_on_write_congestion(page->mapping, NFS_SERVER(inode)->flags & NFS_MOUNT_INTR))
712 return ERR_PTR(-ERESTARTSYS); 640 return ERR_PTR(-ERESTARTSYS);
713 for (;;) { 641 for (;;) {
714 /* Loop over all inode entries and see if we find 642 /* Loop over all inode entries and see if we find
715 * A request for the page we wish to update 643 * A request for the page we wish to update
716 */ 644 */
717 spin_lock(&nfsi->req_lock); 645 spin_lock(&nfsi->req_lock);
718 req = _nfs_find_request(inode, page->index); 646 req = nfs_page_find_request_locked(page);
719 if (req) { 647 if (req) {
720 if (!nfs_lock_request_dontget(req)) { 648 if (!nfs_lock_request_dontget(req)) {
721 int error; 649 int error;
650
722 spin_unlock(&nfsi->req_lock); 651 spin_unlock(&nfsi->req_lock);
723 error = nfs_wait_on_request(req); 652 error = nfs_wait_on_request(req);
724 nfs_release_request(req); 653 nfs_release_request(req);
@@ -745,7 +674,6 @@ static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
745 return ERR_PTR(error); 674 return ERR_PTR(error);
746 } 675 }
747 spin_unlock(&nfsi->req_lock); 676 spin_unlock(&nfsi->req_lock);
748 nfs_mark_request_dirty(new);
749 return new; 677 return new;
750 } 678 }
751 spin_unlock(&nfsi->req_lock); 679 spin_unlock(&nfsi->req_lock);
@@ -786,9 +714,8 @@ static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
786int nfs_flush_incompatible(struct file *file, struct page *page) 714int nfs_flush_incompatible(struct file *file, struct page *page)
787{ 715{
788 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data; 716 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
789 struct inode *inode = page->mapping->host;
790 struct nfs_page *req; 717 struct nfs_page *req;
791 int status = 0; 718 int do_flush, status;
792 /* 719 /*
793 * Look for a request corresponding to this page. If there 720 * Look for a request corresponding to this page. If there
794 * is one, and it belongs to another file, we flush it out 721 * is one, and it belongs to another file, we flush it out
@@ -797,13 +724,18 @@ int nfs_flush_incompatible(struct file *file, struct page *page)
797 * Also do the same if we find a request from an existing 724 * Also do the same if we find a request from an existing
798 * dropped page. 725 * dropped page.
799 */ 726 */
800 req = nfs_find_request(inode, page->index); 727 do {
801 if (req) { 728 req = nfs_page_find_request(page);
802 if (req->wb_page != page || ctx != req->wb_context) 729 if (req == NULL)
803 status = nfs_wb_page(inode, page); 730 return 0;
731 do_flush = req->wb_page != page || req->wb_context != ctx
732 || !nfs_dirty_request(req);
804 nfs_release_request(req); 733 nfs_release_request(req);
805 } 734 if (!do_flush)
806 return (status < 0) ? status : 0; 735 return 0;
736 status = nfs_wb_page(page->mapping->host, page);
737 } while (status == 0);
738 return status;
807} 739}
808 740
809/* 741/*
@@ -817,72 +749,27 @@ int nfs_updatepage(struct file *file, struct page *page,
817{ 749{
818 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data; 750 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
819 struct inode *inode = page->mapping->host; 751 struct inode *inode = page->mapping->host;
820 struct nfs_page *req;
821 int status = 0; 752 int status = 0;
822 753
823 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); 754 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
824 755
825 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n", 756 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
826 file->f_dentry->d_parent->d_name.name, 757 file->f_path.dentry->d_parent->d_name.name,
827 file->f_dentry->d_name.name, count, 758 file->f_path.dentry->d_name.name, count,
828 (long long)(page_offset(page) +offset)); 759 (long long)(page_offset(page) +offset));
829 760
830 if (IS_SYNC(inode)) {
831 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
832 if (status > 0) {
833 if (offset == 0 && status == PAGE_CACHE_SIZE)
834 SetPageUptodate(page);
835 return 0;
836 }
837 return status;
838 }
839
840 /* If we're not using byte range locks, and we know the page 761 /* If we're not using byte range locks, and we know the page
841 * is entirely in cache, it may be more efficient to avoid 762 * is entirely in cache, it may be more efficient to avoid
842 * fragmenting write requests. 763 * fragmenting write requests.
843 */ 764 */
844 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) { 765 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
845 loff_t end_offs = i_size_read(inode) - 1; 766 count = max(count + offset, nfs_page_length(page));
846 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
847
848 count += offset;
849 offset = 0; 767 offset = 0;
850 if (unlikely(end_offs < 0)) {
851 /* Do nothing */
852 } else if (page->index == end_index) {
853 unsigned int pglen;
854 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
855 if (count < pglen)
856 count = pglen;
857 } else if (page->index < end_index)
858 count = PAGE_CACHE_SIZE;
859 } 768 }
860 769
861 /* 770 status = nfs_writepage_setup(ctx, page, offset, count);
862 * Try to find an NFS request corresponding to this page 771 __set_page_dirty_nobuffers(page);
863 * and update it.
864 * If the existing request cannot be updated, we must flush
865 * it out now.
866 */
867 do {
868 req = nfs_update_request(ctx, inode, page, offset, count);
869 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
870 if (status != -EBUSY)
871 break;
872 /* Request could not be updated. Flush it out and try again */
873 status = nfs_wb_page(inode, page);
874 } while (status >= 0);
875 if (status < 0)
876 goto done;
877
878 status = 0;
879 772
880 /* Update file length */
881 nfs_grow_file(page, offset, count);
882 /* Set the PG_uptodate flag? */
883 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
884 nfs_unlock_request(req);
885done:
886 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n", 773 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
887 status, (long long)i_size_read(inode)); 774 status, (long long)i_size_read(inode));
888 if (status < 0) 775 if (status < 0)
@@ -897,7 +784,7 @@ static void nfs_writepage_release(struct nfs_page *req)
897#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 784#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
898 if (!PageError(req->wb_page)) { 785 if (!PageError(req->wb_page)) {
899 if (NFS_NEED_RESCHED(req)) { 786 if (NFS_NEED_RESCHED(req)) {
900 nfs_mark_request_dirty(req); 787 nfs_redirty_request(req);
901 goto out; 788 goto out;
902 } else if (NFS_NEED_COMMIT(req)) { 789 } else if (NFS_NEED_COMMIT(req)) {
903 nfs_mark_request_commit(req); 790 nfs_mark_request_commit(req);
@@ -979,9 +866,7 @@ static void nfs_execute_write(struct nfs_write_data *data)
979 sigset_t oldset; 866 sigset_t oldset;
980 867
981 rpc_clnt_sigmask(clnt, &oldset); 868 rpc_clnt_sigmask(clnt, &oldset);
982 lock_kernel();
983 rpc_execute(&data->task); 869 rpc_execute(&data->task);
984 unlock_kernel();
985 rpc_clnt_sigunmask(clnt, &oldset); 870 rpc_clnt_sigunmask(clnt, &oldset);
986} 871}
987 872
@@ -1015,7 +900,6 @@ static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
1015 atomic_set(&req->wb_complete, requests); 900 atomic_set(&req->wb_complete, requests);
1016 901
1017 ClearPageError(page); 902 ClearPageError(page);
1018 set_page_writeback(page);
1019 offset = 0; 903 offset = 0;
1020 nbytes = req->wb_bytes; 904 nbytes = req->wb_bytes;
1021 do { 905 do {
@@ -1043,9 +927,9 @@ out_bad:
1043 while (!list_empty(&list)) { 927 while (!list_empty(&list)) {
1044 data = list_entry(list.next, struct nfs_write_data, pages); 928 data = list_entry(list.next, struct nfs_write_data, pages);
1045 list_del(&data->pages); 929 list_del(&data->pages);
1046 nfs_writedata_free(data); 930 nfs_writedata_release(data);
1047 } 931 }
1048 nfs_mark_request_dirty(req); 932 nfs_redirty_request(req);
1049 nfs_clear_page_writeback(req); 933 nfs_clear_page_writeback(req);
1050 return -ENOMEM; 934 return -ENOMEM;
1051} 935}
@@ -1076,7 +960,6 @@ static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
1076 nfs_list_remove_request(req); 960 nfs_list_remove_request(req);
1077 nfs_list_add_request(req, &data->pages); 961 nfs_list_add_request(req, &data->pages);
1078 ClearPageError(req->wb_page); 962 ClearPageError(req->wb_page);
1079 set_page_writeback(req->wb_page);
1080 *pages++ = req->wb_page; 963 *pages++ = req->wb_page;
1081 count += req->wb_bytes; 964 count += req->wb_bytes;
1082 } 965 }
@@ -1091,7 +974,7 @@ static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
1091 while (!list_empty(head)) { 974 while (!list_empty(head)) {
1092 struct nfs_page *req = nfs_list_entry(head->next); 975 struct nfs_page *req = nfs_list_entry(head->next);
1093 nfs_list_remove_request(req); 976 nfs_list_remove_request(req);
1094 nfs_mark_request_dirty(req); 977 nfs_redirty_request(req);
1095 nfs_clear_page_writeback(req); 978 nfs_clear_page_writeback(req);
1096 } 979 }
1097 return -ENOMEM; 980 return -ENOMEM;
@@ -1126,7 +1009,7 @@ out_err:
1126 while (!list_empty(head)) { 1009 while (!list_empty(head)) {
1127 req = nfs_list_entry(head->next); 1010 req = nfs_list_entry(head->next);
1128 nfs_list_remove_request(req); 1011 nfs_list_remove_request(req);
1129 nfs_mark_request_dirty(req); 1012 nfs_redirty_request(req);
1130 nfs_clear_page_writeback(req); 1013 nfs_clear_page_writeback(req);
1131 } 1014 }
1132 return error; 1015 return error;
@@ -1442,7 +1325,7 @@ static void nfs_commit_done(struct rpc_task *task, void *calldata)
1442 } 1325 }
1443 /* We have a mismatch. Write the page again */ 1326 /* We have a mismatch. Write the page again */
1444 dprintk(" mismatch\n"); 1327 dprintk(" mismatch\n");
1445 nfs_mark_request_dirty(req); 1328 nfs_redirty_request(req);
1446 next: 1329 next:
1447 nfs_clear_page_writeback(req); 1330 nfs_clear_page_writeback(req);
1448 } 1331 }
@@ -1459,18 +1342,17 @@ static inline int nfs_commit_list(struct inode *inode, struct list_head *head, i
1459} 1342}
1460#endif 1343#endif
1461 1344
1462static int nfs_flush_inode(struct inode *inode, unsigned long idx_start, 1345static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
1463 unsigned int npages, int how)
1464{ 1346{
1465 struct nfs_inode *nfsi = NFS_I(inode); 1347 struct nfs_inode *nfsi = NFS_I(mapping->host);
1466 LIST_HEAD(head); 1348 LIST_HEAD(head);
1467 int res; 1349 long res;
1468 1350
1469 spin_lock(&nfsi->req_lock); 1351 spin_lock(&nfsi->req_lock);
1470 res = nfs_scan_dirty(inode, &head, idx_start, npages); 1352 res = nfs_scan_dirty(mapping, wbc, &head);
1471 spin_unlock(&nfsi->req_lock); 1353 spin_unlock(&nfsi->req_lock);
1472 if (res) { 1354 if (res) {
1473 int error = nfs_flush_list(inode, &head, res, how); 1355 int error = nfs_flush_list(mapping->host, &head, res, how);
1474 if (error < 0) 1356 if (error < 0)
1475 return error; 1357 return error;
1476 } 1358 }
@@ -1496,38 +1378,62 @@ int nfs_commit_inode(struct inode *inode, int how)
1496} 1378}
1497#endif 1379#endif
1498 1380
1499int nfs_sync_inode_wait(struct inode *inode, unsigned long idx_start, 1381long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
1500 unsigned int npages, int how)
1501{ 1382{
1383 struct inode *inode = mapping->host;
1502 struct nfs_inode *nfsi = NFS_I(inode); 1384 struct nfs_inode *nfsi = NFS_I(inode);
1385 unsigned long idx_start, idx_end;
1386 unsigned int npages = 0;
1503 LIST_HEAD(head); 1387 LIST_HEAD(head);
1504 int nocommit = how & FLUSH_NOCOMMIT; 1388 int nocommit = how & FLUSH_NOCOMMIT;
1505 int pages, ret; 1389 long pages, ret;
1506 1390
1391 /* FIXME */
1392 if (wbc->range_cyclic)
1393 idx_start = 0;
1394 else {
1395 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1396 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1397 if (idx_end > idx_start) {
1398 unsigned long l_npages = 1 + idx_end - idx_start;
1399 npages = l_npages;
1400 if (sizeof(npages) != sizeof(l_npages) &&
1401 (unsigned long)npages != l_npages)
1402 npages = 0;
1403 }
1404 }
1507 how &= ~FLUSH_NOCOMMIT; 1405 how &= ~FLUSH_NOCOMMIT;
1508 spin_lock(&nfsi->req_lock); 1406 spin_lock(&nfsi->req_lock);
1509 do { 1407 do {
1408 wbc->pages_skipped = 0;
1510 ret = nfs_wait_on_requests_locked(inode, idx_start, npages); 1409 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1511 if (ret != 0) 1410 if (ret != 0)
1512 continue; 1411 continue;
1513 pages = nfs_scan_dirty(inode, &head, idx_start, npages); 1412 pages = nfs_scan_dirty(mapping, wbc, &head);
1514 if (pages != 0) { 1413 if (pages != 0) {
1515 spin_unlock(&nfsi->req_lock); 1414 spin_unlock(&nfsi->req_lock);
1516 if (how & FLUSH_INVALIDATE) 1415 if (how & FLUSH_INVALIDATE) {
1517 nfs_cancel_dirty_list(&head); 1416 nfs_cancel_dirty_list(&head);
1518 else 1417 ret = pages;
1418 } else
1519 ret = nfs_flush_list(inode, &head, pages, how); 1419 ret = nfs_flush_list(inode, &head, pages, how);
1520 spin_lock(&nfsi->req_lock); 1420 spin_lock(&nfsi->req_lock);
1521 continue; 1421 continue;
1522 } 1422 }
1423 if (wbc->pages_skipped != 0)
1424 continue;
1523 if (nocommit) 1425 if (nocommit)
1524 break; 1426 break;
1525 pages = nfs_scan_commit(inode, &head, idx_start, npages); 1427 pages = nfs_scan_commit(inode, &head, idx_start, npages);
1526 if (pages == 0) 1428 if (pages == 0) {
1429 if (wbc->pages_skipped != 0)
1430 continue;
1527 break; 1431 break;
1432 }
1528 if (how & FLUSH_INVALIDATE) { 1433 if (how & FLUSH_INVALIDATE) {
1529 spin_unlock(&nfsi->req_lock); 1434 spin_unlock(&nfsi->req_lock);
1530 nfs_cancel_commit_list(&head); 1435 nfs_cancel_commit_list(&head);
1436 ret = pages;
1531 spin_lock(&nfsi->req_lock); 1437 spin_lock(&nfsi->req_lock);
1532 continue; 1438 continue;
1533 } 1439 }
@@ -1540,6 +1446,106 @@ int nfs_sync_inode_wait(struct inode *inode, unsigned long idx_start,
1540 return ret; 1446 return ret;
1541} 1447}
1542 1448
1449/*
1450 * flush the inode to disk.
1451 */
1452int nfs_wb_all(struct inode *inode)
1453{
1454 struct address_space *mapping = inode->i_mapping;
1455 struct writeback_control wbc = {
1456 .bdi = mapping->backing_dev_info,
1457 .sync_mode = WB_SYNC_ALL,
1458 .nr_to_write = LONG_MAX,
1459 .for_writepages = 1,
1460 .range_cyclic = 1,
1461 };
1462 int ret;
1463
1464 ret = generic_writepages(mapping, &wbc);
1465 if (ret < 0)
1466 goto out;
1467 ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
1468 if (ret >= 0)
1469 return 0;
1470out:
1471 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1472 return ret;
1473}
1474
1475int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
1476{
1477 struct writeback_control wbc = {
1478 .bdi = mapping->backing_dev_info,
1479 .sync_mode = WB_SYNC_ALL,
1480 .nr_to_write = LONG_MAX,
1481 .range_start = range_start,
1482 .range_end = range_end,
1483 .for_writepages = 1,
1484 };
1485 int ret;
1486
1487 if (!(how & FLUSH_NOWRITEPAGE)) {
1488 ret = generic_writepages(mapping, &wbc);
1489 if (ret < 0)
1490 goto out;
1491 }
1492 ret = nfs_sync_mapping_wait(mapping, &wbc, how);
1493 if (ret >= 0)
1494 return 0;
1495out:
1496 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1497 return ret;
1498}
1499
1500int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
1501{
1502 loff_t range_start = page_offset(page);
1503 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1504 struct writeback_control wbc = {
1505 .bdi = page->mapping->backing_dev_info,
1506 .sync_mode = WB_SYNC_ALL,
1507 .nr_to_write = LONG_MAX,
1508 .range_start = range_start,
1509 .range_end = range_end,
1510 };
1511 int ret;
1512
1513 BUG_ON(!PageLocked(page));
1514 if (!(how & FLUSH_NOWRITEPAGE) && clear_page_dirty_for_io(page)) {
1515 ret = nfs_writepage_locked(page, &wbc);
1516 if (ret < 0)
1517 goto out;
1518 }
1519 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1520 if (ret >= 0)
1521 return 0;
1522out:
1523 __mark_inode_dirty(inode, I_DIRTY_PAGES);
1524 return ret;
1525}
1526
1527/*
1528 * Write back all requests on one page - we do this before reading it.
1529 */
1530int nfs_wb_page(struct inode *inode, struct page* page)
1531{
1532 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
1533}
1534
1535int nfs_set_page_dirty(struct page *page)
1536{
1537 struct nfs_page *req;
1538
1539 req = nfs_page_find_request(page);
1540 if (req != NULL) {
1541 /* Mark any existing write requests for flushing */
1542 set_bit(PG_NEED_FLUSH, &req->wb_flags);
1543 nfs_release_request(req);
1544 }
1545 return __set_page_dirty_nobuffers(page);
1546}
1547
1548
1543int __init nfs_init_writepagecache(void) 1549int __init nfs_init_writepagecache(void)
1544{ 1550{
1545 nfs_wdata_cachep = kmem_cache_create("nfs_write_data", 1551 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",