blob: 2867e6b7096f006c174ca58a087c584f3cc561b2 [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
221static void nfs_set_page_writeback(struct page *page)
222{
223 if (!test_set_page_writeback(page)) {
224 struct inode *inode = page->mapping->host;
225 struct nfs_server *nfss = NFS_SERVER(inode);
226
227 if (atomic_inc_return(&nfss->writeback) >
228 NFS_CONGESTION_ON_THRESH)
229 set_bdi_congested(&nfss->backing_dev_info, WRITE);
230 }
231}
232
233static void nfs_end_page_writeback(struct page *page)
234{
235 struct inode *inode = page->mapping->host;
236 struct nfs_server *nfss = NFS_SERVER(inode);
237
238 end_page_writeback(page);
239 if (atomic_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) {
240 clear_bdi_congested(&nfss->backing_dev_info, WRITE);
241 congestion_end(WRITE);
242 }
243}
244
245/*
Trond Myklebuste261f512006-12-05 00:35:41 -0500246 * Find an associated nfs write request, and prepare to flush it out
247 * Returns 1 if there was no write request, or if the request was
248 * already tagged by nfs_set_page_dirty.Returns 0 if the request
249 * was not tagged.
250 * May also return an error if the user signalled nfs_wait_on_request().
251 */
252static int nfs_page_mark_flush(struct page *page)
253{
254 struct nfs_page *req;
255 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
256 int ret;
257
258 spin_lock(req_lock);
259 for(;;) {
260 req = nfs_page_find_request_locked(page);
261 if (req == NULL) {
262 spin_unlock(req_lock);
263 return 1;
264 }
265 if (nfs_lock_request_dontget(req))
266 break;
267 /* Note: If we hold the page lock, as is the case in nfs_writepage,
268 * then the call to nfs_lock_request_dontget() will always
269 * succeed provided that someone hasn't already marked the
270 * request as dirty (in which case we don't care).
271 */
272 spin_unlock(req_lock);
273 ret = nfs_wait_on_request(req);
274 nfs_release_request(req);
275 if (ret != 0)
276 return ret;
277 spin_lock(req_lock);
278 }
279 spin_unlock(req_lock);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500280 if (test_and_set_bit(PG_FLUSHING, &req->wb_flags) == 0) {
Trond Myklebuste261f512006-12-05 00:35:41 -0500281 nfs_mark_request_dirty(req);
Peter Zijlstra89a09142007-03-16 13:38:26 -0800282 nfs_set_page_writeback(page);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500283 }
Trond Myklebuste261f512006-12-05 00:35:41 -0500284 ret = test_bit(PG_NEED_FLUSH, &req->wb_flags);
285 nfs_unlock_request(req);
286 return ret;
287}
288
289/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 * Write an mmapped page to the server.
291 */
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500292static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 struct nfs_open_context *ctx;
295 struct inode *inode = page->mapping->host;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500296 unsigned offset;
Trond Myklebuste261f512006-12-05 00:35:41 -0500297 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Chuck Lever91d5b472006-03-20 13:44:14 -0500299 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
300 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
301
Trond Myklebuste261f512006-12-05 00:35:41 -0500302 err = nfs_page_mark_flush(page);
303 if (err <= 0)
304 goto out;
305 err = 0;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500306 offset = nfs_page_length(page);
307 if (!offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 goto out;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500309
Trond Myklebustd5308382005-11-04 15:33:38 -0500310 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (ctx == NULL) {
312 err = -EBADF;
313 goto out;
314 }
Trond Myklebust200baa22006-12-05 00:35:40 -0500315 err = nfs_writepage_setup(ctx, page, 0, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 put_nfs_open_context(ctx);
Trond Myklebuste261f512006-12-05 00:35:41 -0500317 if (err != 0)
318 goto out;
319 err = nfs_page_mark_flush(page);
320 if (err > 0)
321 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322out:
Trond Myklebust200baa22006-12-05 00:35:40 -0500323 if (!wbc->for_writepages)
Trond Myklebust02241bc2007-01-13 02:28:07 -0500324 nfs_flush_mapping(page->mapping, wbc, FLUSH_STABLE|wb_priority(wbc));
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500325 return err;
326}
327
328int nfs_writepage(struct page *page, struct writeback_control *wbc)
329{
330 int err;
331
332 err = nfs_writepage_locked(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return err;
335}
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
338{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 struct inode *inode = mapping->host;
340 int err;
341
Chuck Lever91d5b472006-03-20 13:44:14 -0500342 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 err = generic_writepages(mapping, wbc);
345 if (err)
346 return err;
Trond Myklebust28c69252006-09-16 13:04:50 -0400347 err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (err < 0)
349 goto out;
Chuck Lever91d5b472006-03-20 13:44:14 -0500350 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
Trond Myklebustd30c8342006-12-13 15:23:47 -0500351 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return err;
354}
355
356/*
357 * Insert a write request into an inode
358 */
359static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
360{
361 struct nfs_inode *nfsi = NFS_I(inode);
362 int error;
363
364 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
365 BUG_ON(error == -EEXIST);
366 if (error)
367 return error;
368 if (!nfsi->npages) {
369 igrab(inode);
370 nfs_begin_data_update(inode);
371 if (nfs_have_delegation(inode, FMODE_WRITE))
372 nfsi->change_attr++;
373 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500374 SetPagePrivate(req->wb_page);
Trond Myklebust277459d2006-12-05 00:35:35 -0500375 set_page_private(req->wb_page, (unsigned long)req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 nfsi->npages++;
377 atomic_inc(&req->wb_count);
378 return 0;
379}
380
381/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800382 * Remove a write request from an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 */
384static void nfs_inode_remove_request(struct nfs_page *req)
385{
386 struct inode *inode = req->wb_context->dentry->d_inode;
387 struct nfs_inode *nfsi = NFS_I(inode);
388
389 BUG_ON (!NFS_WBACK_BUSY(req));
390
391 spin_lock(&nfsi->req_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500392 set_page_private(req->wb_page, 0);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500393 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
395 nfsi->npages--;
396 if (!nfsi->npages) {
397 spin_unlock(&nfsi->req_lock);
Trond Myklebust951a1432005-06-22 17:16:30 +0000398 nfs_end_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 iput(inode);
400 } else
401 spin_unlock(&nfsi->req_lock);
402 nfs_clear_request(req);
403 nfs_release_request(req);
404}
405
406/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 * Add a request to the inode's dirty list.
408 */
409static void
410nfs_mark_request_dirty(struct nfs_page *req)
411{
412 struct inode *inode = req->wb_context->dentry->d_inode;
413 struct nfs_inode *nfsi = NFS_I(inode);
414
415 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000416 radix_tree_tag_set(&nfsi->nfs_page_tree,
417 req->wb_index, NFS_PAGE_TAG_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 nfs_list_add_request(req, &nfsi->dirty);
419 nfsi->ndirty++;
420 spin_unlock(&nfsi->req_lock);
Trond Myklebusta1803042006-12-05 00:45:19 -0500421 __mark_inode_dirty(inode, I_DIRTY_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Trond Myklebust61822ab2006-12-05 00:35:42 -0500424static void
425nfs_redirty_request(struct nfs_page *req)
426{
427 clear_bit(PG_FLUSHING, &req->wb_flags);
428 __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 Myklebuste261f512006-12-05 00:35:41 -0500437 return test_bit(PG_FLUSHING, &req->wb_flags) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
440#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
441/*
442 * Add a request to the inode's commit list.
443 */
444static void
445nfs_mark_request_commit(struct nfs_page *req)
446{
447 struct inode *inode = req->wb_context->dentry->d_inode;
448 struct nfs_inode *nfsi = NFS_I(inode);
449
450 spin_lock(&nfsi->req_lock);
451 nfs_list_add_request(req, &nfsi->commit);
452 nfsi->ncommit++;
453 spin_unlock(&nfsi->req_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700454 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebusta1803042006-12-05 00:45:19 -0500455 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457#endif
458
459/*
460 * Wait for a request to complete.
461 *
462 * Interruptible by signals only if mounted with intr flag.
463 */
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500464static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 struct nfs_inode *nfsi = NFS_I(inode);
467 struct nfs_page *req;
468 unsigned long idx_end, next;
469 unsigned int res = 0;
470 int error;
471
472 if (npages == 0)
473 idx_end = ~0;
474 else
475 idx_end = idx_start + npages - 1;
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 next = idx_start;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000478 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 -0700479 if (req->wb_index > idx_end)
480 break;
481
482 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000483 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 atomic_inc(&req->wb_count);
486 spin_unlock(&nfsi->req_lock);
487 error = nfs_wait_on_request(req);
488 nfs_release_request(req);
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500489 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (error < 0)
491 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 res++;
493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return res;
495}
496
Trond Myklebust83715ad2006-07-05 13:17:12 -0400497static void nfs_cancel_dirty_list(struct list_head *head)
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400498{
499 struct nfs_page *req;
500 while(!list_empty(head)) {
501 req = nfs_list_entry(head->next);
502 nfs_list_remove_request(req);
503 nfs_inode_remove_request(req);
504 nfs_clear_page_writeback(req);
505 }
506}
507
Trond Myklebust83715ad2006-07-05 13:17:12 -0400508static void nfs_cancel_commit_list(struct list_head *head)
509{
510 struct nfs_page *req;
511
512 while(!list_empty(head)) {
513 req = nfs_list_entry(head->next);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700514 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400515 nfs_list_remove_request(req);
516 nfs_inode_remove_request(req);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700517 nfs_unlock_request(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400518 }
519}
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
522/*
523 * nfs_scan_commit - Scan an inode for commit requests
524 * @inode: NFS inode to scan
525 * @dst: destination list
526 * @idx_start: lower bound of page->index to scan.
527 * @npages: idx_start + npages sets the upper bound to scan.
528 *
529 * Moves requests from the inode's 'commit' request list.
530 * The requests are *not* checked to ensure that they form a contiguous set.
531 */
532static int
533nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
534{
535 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000536 int res = 0;
537
538 if (nfsi->ncommit != 0) {
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400539 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000540 nfsi->ncommit -= res;
541 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
542 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return res;
545}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500546#else
547static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
548{
549 return 0;
550}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551#endif
552
Peter Zijlstra89a09142007-03-16 13:38:26 -0800553static int nfs_wait_on_write_congestion(struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Peter Zijlstra89a09142007-03-16 13:38:26 -0800555 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 struct backing_dev_info *bdi = mapping->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 int ret = 0;
558
559 might_sleep();
560
561 if (!bdi_write_congested(bdi))
562 return 0;
Chuck Lever91d5b472006-03-20 13:44:14 -0500563
Peter Zijlstra89a09142007-03-16 13:38:26 -0800564 nfs_inc_stats(inode, NFSIOS_CONGESTIONWAIT);
Chuck Lever91d5b472006-03-20 13:44:14 -0500565
Peter Zijlstra89a09142007-03-16 13:38:26 -0800566 do {
567 struct rpc_clnt *clnt = NFS_CLIENT(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 sigset_t oldset;
569
570 rpc_clnt_sigmask(clnt, &oldset);
Peter Zijlstra89a09142007-03-16 13:38:26 -0800571 ret = congestion_wait_interruptible(WRITE, HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 rpc_clnt_sigunmask(clnt, &oldset);
Peter Zijlstra89a09142007-03-16 13:38:26 -0800573 if (ret == -ERESTARTSYS)
574 break;
575 ret = 0;
576 } while (bdi_write_congested(bdi));
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return ret;
579}
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581/*
582 * Try to update any existing write request, or create one if there is none.
583 * In order to match, the request's credentials must match those of
584 * the calling process.
585 *
586 * Note: Should always be called with the Page Lock held!
587 */
588static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
Trond Myklebuste21195a2006-12-05 00:35:39 -0500589 struct page *page, unsigned int offset, unsigned int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Peter Zijlstra89a09142007-03-16 13:38:26 -0800591 struct address_space *mapping = page->mapping;
592 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 struct nfs_inode *nfsi = NFS_I(inode);
594 struct nfs_page *req, *new = NULL;
595 unsigned long rqend, end;
596
597 end = offset + bytes;
598
Peter Zijlstra89a09142007-03-16 13:38:26 -0800599 if (nfs_wait_on_write_congestion(mapping))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return ERR_PTR(-ERESTARTSYS);
601 for (;;) {
602 /* Loop over all inode entries and see if we find
603 * A request for the page we wish to update
604 */
605 spin_lock(&nfsi->req_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500606 req = nfs_page_find_request_locked(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (req) {
608 if (!nfs_lock_request_dontget(req)) {
609 int error;
Trond Myklebust277459d2006-12-05 00:35:35 -0500610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 spin_unlock(&nfsi->req_lock);
612 error = nfs_wait_on_request(req);
613 nfs_release_request(req);
Neil Brown1dd594b2006-03-20 13:44:04 -0500614 if (error < 0) {
615 if (new)
616 nfs_release_request(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return ERR_PTR(error);
Neil Brown1dd594b2006-03-20 13:44:04 -0500618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 continue;
620 }
621 spin_unlock(&nfsi->req_lock);
622 if (new)
623 nfs_release_request(new);
624 break;
625 }
626
627 if (new) {
628 int error;
629 nfs_lock_request_dontget(new);
630 error = nfs_inode_add_request(inode, new);
631 if (error) {
632 spin_unlock(&nfsi->req_lock);
633 nfs_unlock_request(new);
634 return ERR_PTR(error);
635 }
636 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return new;
638 }
639 spin_unlock(&nfsi->req_lock);
640
641 new = nfs_create_request(ctx, inode, page, offset, bytes);
642 if (IS_ERR(new))
643 return new;
644 }
645
646 /* We have a request for our page.
647 * If the creds don't match, or the
648 * page addresses don't match,
649 * tell the caller to wait on the conflicting
650 * request.
651 */
652 rqend = req->wb_offset + req->wb_bytes;
653 if (req->wb_context != ctx
654 || req->wb_page != page
655 || !nfs_dirty_request(req)
656 || offset > rqend || end < req->wb_offset) {
657 nfs_unlock_request(req);
658 return ERR_PTR(-EBUSY);
659 }
660
661 /* Okay, the request matches. Update the region */
662 if (offset < req->wb_offset) {
663 req->wb_offset = offset;
664 req->wb_pgbase = offset;
665 req->wb_bytes = rqend - req->wb_offset;
666 }
667
668 if (end > rqend)
669 req->wb_bytes = end - req->wb_offset;
670
671 return req;
672}
673
674int nfs_flush_incompatible(struct file *file, struct page *page)
675{
676 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 struct nfs_page *req;
Trond Myklebust1a545332006-12-05 00:35:40 -0500678 int do_flush, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 /*
680 * Look for a request corresponding to this page. If there
681 * is one, and it belongs to another file, we flush it out
682 * before we try to copy anything into the page. Do this
683 * due to the lack of an ACCESS-type call in NFSv2.
684 * Also do the same if we find a request from an existing
685 * dropped page.
686 */
Trond Myklebust1a545332006-12-05 00:35:40 -0500687 do {
688 req = nfs_page_find_request(page);
689 if (req == NULL)
690 return 0;
691 do_flush = req->wb_page != page || req->wb_context != ctx
Trond Myklebuste261f512006-12-05 00:35:41 -0500692 || !nfs_dirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 nfs_release_request(req);
Trond Myklebust1a545332006-12-05 00:35:40 -0500694 if (!do_flush)
695 return 0;
696 status = nfs_wb_page(page->mapping->host, page);
697 } while (status == 0);
698 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
701/*
702 * Update and possibly write a cached page of an NFS file.
703 *
704 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
705 * things with a page scheduled for an RPC call (e.g. invalidate it).
706 */
707int nfs_updatepage(struct file *file, struct page *page,
708 unsigned int offset, unsigned int count)
709{
710 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 int status = 0;
713
Chuck Lever91d5b472006-03-20 13:44:14 -0500714 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800717 file->f_path.dentry->d_parent->d_name.name,
718 file->f_path.dentry->d_name.name, count,
Chuck Lever0bbacc42005-11-01 16:53:32 -0500719 (long long)(page_offset(page) +offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 /* If we're not using byte range locks, and we know the page
722 * is entirely in cache, it may be more efficient to avoid
723 * fragmenting write requests.
724 */
Trond Myklebustab0a3db2005-06-22 17:16:30 +0000725 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
Trond Myklebust49a70f22006-12-05 00:35:38 -0500726 count = max(count + offset, nfs_page_length(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729
Trond Myklebuste21195a2006-12-05 00:35:39 -0500730 status = nfs_writepage_setup(ctx, page, offset, count);
Trond Myklebuste261f512006-12-05 00:35:41 -0500731 __set_page_dirty_nobuffers(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
734 status, (long long)i_size_read(inode));
735 if (status < 0)
Trond Myklebusta301b772007-02-06 11:07:15 -0800736 nfs_set_pageerror(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return status;
738}
739
740static void nfs_writepage_release(struct nfs_page *req)
741{
Peter Zijlstra89a09142007-03-16 13:38:26 -0800742 nfs_end_page_writeback(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
745 if (!PageError(req->wb_page)) {
746 if (NFS_NEED_RESCHED(req)) {
Trond Myklebust61822ab2006-12-05 00:35:42 -0500747 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 goto out;
749 } else if (NFS_NEED_COMMIT(req)) {
750 nfs_mark_request_commit(req);
751 goto out;
752 }
753 }
754 nfs_inode_remove_request(req);
755
756out:
757 nfs_clear_commit(req);
758 nfs_clear_reschedule(req);
759#else
760 nfs_inode_remove_request(req);
761#endif
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000762 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
765static inline int flush_task_priority(int how)
766{
767 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
768 case FLUSH_HIGHPRI:
769 return RPC_PRIORITY_HIGH;
770 case FLUSH_LOWPRI:
771 return RPC_PRIORITY_LOW;
772 }
773 return RPC_PRIORITY_NORMAL;
774}
775
776/*
777 * Set up the argument/result storage required for the RPC call.
778 */
779static void nfs_write_rpcsetup(struct nfs_page *req,
780 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500781 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 unsigned int count, unsigned int offset,
783 int how)
784{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -0500786 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 /* Set up the RPC argument and reply structs
789 * NB: take care not to mess about with data->commit et al. */
790
791 data->req = req;
792 data->inode = inode = req->wb_context->dentry->d_inode;
793 data->cred = req->wb_context->cred;
794
795 data->args.fh = NFS_FH(inode);
796 data->args.offset = req_offset(req) + offset;
797 data->args.pgbase = req->wb_pgbase + offset;
798 data->args.pages = data->pagevec;
799 data->args.count = count;
800 data->args.context = req->wb_context;
801
802 data->res.fattr = &data->fattr;
803 data->res.count = count;
804 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400805 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Trond Myklebust788e7a82006-03-20 13:44:27 -0500807 /* Set up the initial task struct. */
808 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
809 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 NFS_PROTO(inode)->write_setup(data, how);
811
812 data->task.tk_priority = flush_task_priority(how);
813 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Chuck Levera3f565b2007-01-31 12:14:01 -0500815 dprintk("NFS: %5u initiated write call "
816 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500817 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 inode->i_sb->s_id,
819 (long long)NFS_FILEID(inode),
820 count,
821 (unsigned long long)data->args.offset);
822}
823
824static void nfs_execute_write(struct nfs_write_data *data)
825{
826 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
827 sigset_t oldset;
828
829 rpc_clnt_sigmask(clnt, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 rpc_execute(&data->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 rpc_clnt_sigunmask(clnt, &oldset);
832}
833
834/*
835 * Generate multiple small requests to write out a single
836 * contiguous dirty area on one page.
837 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500838static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 struct nfs_page *req = nfs_list_entry(head->next);
841 struct page *page = req->wb_page;
842 struct nfs_write_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700843 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
844 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 int requests = 0;
846 LIST_HEAD(list);
847
848 nfs_list_remove_request(req);
849
850 nbytes = req->wb_bytes;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700851 do {
852 size_t len = min(nbytes, wsize);
853
854 data = nfs_writedata_alloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (!data)
856 goto out_bad;
857 list_add(&data->pages, &list);
858 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700859 nbytes -= len;
860 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 atomic_set(&req->wb_complete, requests);
862
863 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 offset = 0;
865 nbytes = req->wb_bytes;
866 do {
867 data = list_entry(list.next, struct nfs_write_data, pages);
868 list_del_init(&data->pages);
869
870 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 if (nbytes > wsize) {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500873 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
874 wsize, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 offset += wsize;
876 nbytes -= wsize;
877 } else {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500878 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
879 nbytes, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 nbytes = 0;
881 }
882 nfs_execute_write(data);
883 } while (nbytes != 0);
884
885 return 0;
886
887out_bad:
888 while (!list_empty(&list)) {
889 data = list_entry(list.next, struct nfs_write_data, pages);
890 list_del(&data->pages);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500891 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
Trond Myklebust61822ab2006-12-05 00:35:42 -0500893 nfs_redirty_request(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000894 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return -ENOMEM;
896}
897
898/*
899 * Create an RPC task for the given write request and kick it.
900 * The page must have been locked by the caller.
901 *
902 * It may happen that the page we're passed is not marked dirty.
903 * This is the case if nfs_updatepage detects a conflicting request
904 * that has been written but not committed.
905 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500906static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
908 struct nfs_page *req;
909 struct page **pages;
910 struct nfs_write_data *data;
911 unsigned int count;
912
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700913 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (!data)
915 goto out_bad;
916
917 pages = data->pagevec;
918 count = 0;
919 while (!list_empty(head)) {
920 req = nfs_list_entry(head->next);
921 nfs_list_remove_request(req);
922 nfs_list_add_request(req, &data->pages);
923 ClearPageError(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 *pages++ = req->wb_page;
925 count += req->wb_bytes;
926 }
927 req = nfs_list_entry(data->pages.next);
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 /* Set up the argument struct */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500930 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 nfs_execute_write(data);
933 return 0;
934 out_bad:
935 while (!list_empty(head)) {
936 struct nfs_page *req = nfs_list_entry(head->next);
937 nfs_list_remove_request(req);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500938 nfs_redirty_request(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000939 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
941 return -ENOMEM;
942}
943
Trond Myklebust7d46a492006-03-20 13:44:50 -0500944static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
946 LIST_HEAD(one_request);
Trond Myklebust7d46a492006-03-20 13:44:50 -0500947 int (*flush_one)(struct inode *, struct list_head *, int);
948 struct nfs_page *req;
949 int wpages = NFS_SERVER(inode)->wpages;
950 int wsize = NFS_SERVER(inode)->wsize;
951 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Trond Myklebust7d46a492006-03-20 13:44:50 -0500953 flush_one = nfs_flush_one;
954 if (wsize < PAGE_CACHE_SIZE)
955 flush_one = nfs_flush_multi;
956 /* For single writes, FLUSH_STABLE is more efficient */
957 if (npages <= wpages && npages == NFS_I(inode)->npages
958 && nfs_list_entry(head->next)->wb_bytes <= wsize)
959 how |= FLUSH_STABLE;
960
961 do {
962 nfs_coalesce_requests(head, &one_request, wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 req = nfs_list_entry(one_request.next);
Trond Myklebust7d46a492006-03-20 13:44:50 -0500964 error = flush_one(inode, &one_request, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (error < 0)
Trond Myklebust7d46a492006-03-20 13:44:50 -0500966 goto out_err;
967 } while (!list_empty(head));
968 return 0;
969out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 while (!list_empty(head)) {
971 req = nfs_list_entry(head->next);
972 nfs_list_remove_request(req);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500973 nfs_redirty_request(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000974 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976 return error;
977}
978
979/*
980 * Handle a write reply that flushed part of a page.
981 */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500982static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Trond Myklebust788e7a82006-03-20 13:44:27 -0500984 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 struct nfs_page *req = data->req;
986 struct page *page = req->wb_page;
987
988 dprintk("NFS: write (%s/%Ld %d@%Ld)",
989 req->wb_context->dentry->d_inode->i_sb->s_id,
990 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
991 req->wb_bytes,
992 (long long)req_offset(req));
993
Trond Myklebust788e7a82006-03-20 13:44:27 -0500994 if (nfs_writeback_done(task, data) != 0)
995 return;
996
997 if (task->tk_status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -0800998 nfs_set_pageerror(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500999 req->wb_context->error = task->tk_status;
1000 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 } else {
1002#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1003 if (data->verf.committed < NFS_FILE_SYNC) {
1004 if (!NFS_NEED_COMMIT(req)) {
1005 nfs_defer_commit(req);
1006 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1007 dprintk(" defer commit\n");
1008 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1009 nfs_defer_reschedule(req);
1010 dprintk(" server reboot detected\n");
1011 }
1012 } else
1013#endif
1014 dprintk(" OK\n");
1015 }
1016
1017 if (atomic_dec_and_test(&req->wb_complete))
1018 nfs_writepage_release(req);
1019}
1020
Trond Myklebust788e7a82006-03-20 13:44:27 -05001021static const struct rpc_call_ops nfs_write_partial_ops = {
1022 .rpc_call_done = nfs_writeback_done_partial,
1023 .rpc_release = nfs_writedata_release,
1024};
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026/*
1027 * Handle a write reply that flushes a whole page.
1028 *
1029 * FIXME: There is an inherent race with invalidate_inode_pages and
1030 * writebacks since the page->count is kept > 1 for as long
1031 * as the page has a write request pending.
1032 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001033static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001035 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 struct nfs_page *req;
1037 struct page *page;
1038
Trond Myklebust788e7a82006-03-20 13:44:27 -05001039 if (nfs_writeback_done(task, data) != 0)
1040 return;
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 /* Update attributes as result of writeback. */
1043 while (!list_empty(&data->pages)) {
1044 req = nfs_list_entry(data->pages.next);
1045 nfs_list_remove_request(req);
1046 page = req->wb_page;
1047
1048 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1049 req->wb_context->dentry->d_inode->i_sb->s_id,
1050 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1051 req->wb_bytes,
1052 (long long)req_offset(req));
1053
Trond Myklebust788e7a82006-03-20 13:44:27 -05001054 if (task->tk_status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -08001055 nfs_set_pageerror(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001056 req->wb_context->error = task->tk_status;
Peter Zijlstra89a09142007-03-16 13:38:26 -08001057 nfs_end_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 nfs_inode_remove_request(req);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001059 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 goto next;
1061 }
Peter Zijlstra89a09142007-03-16 13:38:26 -08001062 nfs_end_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1065 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1066 nfs_inode_remove_request(req);
1067 dprintk(" OK\n");
1068 goto next;
1069 }
1070 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1071 nfs_mark_request_commit(req);
1072 dprintk(" marked for commit\n");
1073#else
1074 nfs_inode_remove_request(req);
1075#endif
1076 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001077 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
1079}
1080
Trond Myklebust788e7a82006-03-20 13:44:27 -05001081static const struct rpc_call_ops nfs_write_full_ops = {
1082 .rpc_call_done = nfs_writeback_done_full,
1083 .rpc_release = nfs_writedata_release,
1084};
1085
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087/*
1088 * This function is called when the WRITE call is complete.
1089 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001090int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 struct nfs_writeargs *argp = &data->args;
1093 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001094 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Chuck Levera3f565b2007-01-31 12:14:01 -05001096 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 task->tk_pid, task->tk_status);
1098
Chuck Leverf551e442006-09-20 14:33:04 -04001099 /*
1100 * ->write_done will attempt to use post-op attributes to detect
1101 * conflicting writes by other clients. A strict interpretation
1102 * of close-to-open would allow us to continue caching even if
1103 * another writer had changed the file, but some applications
1104 * depend on tighter cache coherency when writing.
1105 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001106 status = NFS_PROTO(data->inode)->write_done(task, data);
1107 if (status != 0)
1108 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001109 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1112 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1113 /* We tried a write call, but the server did not
1114 * commit data to stable storage even though we
1115 * requested it.
1116 * Note: There is a known bug in Tru64 < 5.0 in which
1117 * the server reports NFS_DATA_SYNC, but performs
1118 * NFS_FILE_SYNC. We therefore implement this checking
1119 * as a dprintk() in order to avoid filling syslog.
1120 */
1121 static unsigned long complain;
1122
1123 if (time_before(complain, jiffies)) {
1124 dprintk("NFS: faulty NFS server %s:"
1125 " (committed = %d) != (stable = %d)\n",
David Howells54ceac42006-08-22 20:06:13 -04001126 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 resp->verf->committed, argp->stable);
1128 complain = jiffies + 300 * HZ;
1129 }
1130 }
1131#endif
1132 /* Is this a short write? */
1133 if (task->tk_status >= 0 && resp->count < argp->count) {
1134 static unsigned long complain;
1135
Chuck Lever91d5b472006-03-20 13:44:14 -05001136 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 /* Has the server at least made some progress? */
1139 if (resp->count != 0) {
1140 /* Was this an NFSv2 write or an NFSv3 stable write? */
1141 if (resp->verf->committed != NFS_UNSTABLE) {
1142 /* Resend from where the server left off */
1143 argp->offset += resp->count;
1144 argp->pgbase += resp->count;
1145 argp->count -= resp->count;
1146 } else {
1147 /* Resend as a stable write in order to avoid
1148 * headaches in the case of a server crash.
1149 */
1150 argp->stable = NFS_FILE_SYNC;
1151 }
1152 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001153 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
1155 if (time_before(complain, jiffies)) {
1156 printk(KERN_WARNING
1157 "NFS: Server wrote zero bytes, expected %u.\n",
1158 argp->count);
1159 complain = jiffies + 300 * HZ;
1160 }
1161 /* Can't do anything about it except throw an error. */
1162 task->tk_status = -EIO;
1163 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
1167
1168#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001169void nfs_commit_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 nfs_commit_free(wdata);
1172}
1173
1174/*
1175 * Set up the argument/result storage required for the RPC call.
1176 */
1177static void nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001178 struct nfs_write_data *data,
1179 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001181 struct nfs_page *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001183 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 /* Set up the RPC argument and reply structs
1186 * NB: take care not to mess about with data->commit et al. */
1187
1188 list_splice_init(head, &data->pages);
1189 first = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 inode = first->wb_context->dentry->d_inode;
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 data->inode = inode;
1193 data->cred = first->wb_context->cred;
1194
1195 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001196 /* Note: we always request a commit of the entire inode */
1197 data->args.offset = 0;
1198 data->args.count = 0;
1199 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 data->res.fattr = &data->fattr;
1201 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001202 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001203
1204 /* Set up the initial task struct. */
1205 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1206 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 NFS_PROTO(inode)->commit_setup(data, how);
1208
1209 data->task.tk_priority = flush_task_priority(how);
1210 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Chuck Levera3f565b2007-01-31 12:14:01 -05001212 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
1215/*
1216 * Commit dirty pages
1217 */
1218static int
Chuck Lever40859d72005-11-30 18:09:02 -05001219nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
1221 struct nfs_write_data *data;
1222 struct nfs_page *req;
1223
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001224 data = nfs_commit_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 if (!data)
1227 goto out_bad;
1228
1229 /* Set up the argument struct */
1230 nfs_commit_rpcsetup(head, data, how);
1231
1232 nfs_execute_write(data);
1233 return 0;
1234 out_bad:
1235 while (!list_empty(head)) {
1236 req = nfs_list_entry(head->next);
1237 nfs_list_remove_request(req);
1238 nfs_mark_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001239 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust5c2d97c2006-09-18 23:20:35 -04001240 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
1242 return -ENOMEM;
1243}
1244
1245/*
1246 * COMMIT call returned
1247 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001248static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001250 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Chuck Levera3f565b2007-01-31 12:14:01 -05001253 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 task->tk_pid, task->tk_status);
1255
Trond Myklebust788e7a82006-03-20 13:44:27 -05001256 /* Call the NFS version-specific code */
1257 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1258 return;
1259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 while (!list_empty(&data->pages)) {
1261 req = nfs_list_entry(data->pages.next);
1262 nfs_list_remove_request(req);
Christoph Lameterfd39fc82006-06-30 01:55:40 -07001263 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1266 req->wb_context->dentry->d_inode->i_sb->s_id,
1267 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1268 req->wb_bytes,
1269 (long long)req_offset(req));
1270 if (task->tk_status < 0) {
1271 req->wb_context->error = task->tk_status;
1272 nfs_inode_remove_request(req);
1273 dprintk(", error = %d\n", task->tk_status);
1274 goto next;
1275 }
1276
1277 /* Okay, COMMIT succeeded, apparently. Check the verifier
1278 * returned by the server against all stored verfs. */
1279 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1280 /* We have a match */
1281 nfs_inode_remove_request(req);
1282 dprintk(" OK\n");
1283 goto next;
1284 }
1285 /* We have a mismatch. Write the page again */
1286 dprintk(" mismatch\n");
Trond Myklebust61822ab2006-12-05 00:35:42 -05001287 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001289 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001292
1293static const struct rpc_call_ops nfs_commit_ops = {
1294 .rpc_call_done = nfs_commit_done,
1295 .rpc_release = nfs_commit_release,
1296};
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001297#else
1298static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1299{
1300 return 0;
1301}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302#endif
1303
Trond Myklebust3f442542006-09-17 14:46:44 -04001304static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
Trond Myklebust28c69252006-09-16 13:04:50 -04001306 struct nfs_inode *nfsi = NFS_I(mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 LIST_HEAD(head);
Trond Myklebust3f442542006-09-17 14:46:44 -04001308 long res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 spin_lock(&nfsi->req_lock);
Trond Myklebust3f442542006-09-17 14:46:44 -04001311 res = nfs_scan_dirty(mapping, wbc, &head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 spin_unlock(&nfsi->req_lock);
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001313 if (res) {
Trond Myklebust28c69252006-09-16 13:04:50 -04001314 int error = nfs_flush_list(mapping->host, &head, res, how);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001315 if (error < 0)
1316 return error;
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return res;
1319}
1320
1321#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001322int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
1324 struct nfs_inode *nfsi = NFS_I(inode);
1325 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001326 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001329 res = nfs_scan_commit(inode, &head, 0, 0);
1330 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001332 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001333 if (error < 0)
1334 return error;
1335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 return res;
1337}
1338#endif
1339
Trond Myklebust1c759502006-10-09 16:18:38 -04001340long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
Trond Myklebust1c759502006-10-09 16:18:38 -04001342 struct inode *inode = mapping->host;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001343 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c759502006-10-09 16:18:38 -04001344 unsigned long idx_start, idx_end;
1345 unsigned int npages = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001346 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001347 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebust3f442542006-09-17 14:46:44 -04001348 long pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Trond Myklebust1c759502006-10-09 16:18:38 -04001350 /* FIXME */
1351 if (wbc->range_cyclic)
1352 idx_start = 0;
1353 else {
1354 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1355 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1356 if (idx_end > idx_start) {
1357 unsigned long l_npages = 1 + idx_end - idx_start;
1358 npages = l_npages;
1359 if (sizeof(npages) != sizeof(l_npages) &&
1360 (unsigned long)npages != l_npages)
1361 npages = 0;
1362 }
1363 }
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001364 how &= ~FLUSH_NOCOMMIT;
1365 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 do {
Trond Myklebust1c759502006-10-09 16:18:38 -04001367 wbc->pages_skipped = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001368 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1369 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001370 continue;
Trond Myklebust1c759502006-10-09 16:18:38 -04001371 pages = nfs_scan_dirty(mapping, wbc, &head);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001372 if (pages != 0) {
1373 spin_unlock(&nfsi->req_lock);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001374 if (how & FLUSH_INVALIDATE) {
Trond Myklebust83715ad2006-07-05 13:17:12 -04001375 nfs_cancel_dirty_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001376 ret = pages;
1377 } else
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001378 ret = nfs_flush_list(inode, &head, pages, how);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001379 spin_lock(&nfsi->req_lock);
1380 continue;
1381 }
Trond Myklebust1c759502006-10-09 16:18:38 -04001382 if (wbc->pages_skipped != 0)
1383 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001384 if (nocommit)
1385 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001386 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebust1c759502006-10-09 16:18:38 -04001387 if (pages == 0) {
1388 if (wbc->pages_skipped != 0)
1389 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001390 break;
Trond Myklebust1c759502006-10-09 16:18:38 -04001391 }
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001392 if (how & FLUSH_INVALIDATE) {
1393 spin_unlock(&nfsi->req_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001394 nfs_cancel_commit_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001395 ret = pages;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001396 spin_lock(&nfsi->req_lock);
1397 continue;
1398 }
1399 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001400 spin_unlock(&nfsi->req_lock);
1401 ret = nfs_commit_list(inode, &head, how);
1402 spin_lock(&nfsi->req_lock);
1403 } while (ret >= 0);
1404 spin_unlock(&nfsi->req_lock);
1405 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406}
1407
Trond Myklebust1c759502006-10-09 16:18:38 -04001408/*
1409 * flush the inode to disk.
1410 */
1411int nfs_wb_all(struct inode *inode)
1412{
1413 struct address_space *mapping = inode->i_mapping;
1414 struct writeback_control wbc = {
1415 .bdi = mapping->backing_dev_info,
1416 .sync_mode = WB_SYNC_ALL,
1417 .nr_to_write = LONG_MAX,
Trond Myklebust61822ab2006-12-05 00:35:42 -05001418 .for_writepages = 1,
Trond Myklebust1c759502006-10-09 16:18:38 -04001419 .range_cyclic = 1,
1420 };
1421 int ret;
1422
Trond Myklebust61822ab2006-12-05 00:35:42 -05001423 ret = generic_writepages(mapping, &wbc);
1424 if (ret < 0)
1425 goto out;
Trond Myklebust1c759502006-10-09 16:18:38 -04001426 ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
1427 if (ret >= 0)
1428 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001429out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001430 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Trond Myklebust1c759502006-10-09 16:18:38 -04001431 return ret;
1432}
1433
1434int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
1435{
1436 struct writeback_control wbc = {
1437 .bdi = mapping->backing_dev_info,
1438 .sync_mode = WB_SYNC_ALL,
1439 .nr_to_write = LONG_MAX,
1440 .range_start = range_start,
1441 .range_end = range_end,
Trond Myklebust61822ab2006-12-05 00:35:42 -05001442 .for_writepages = 1,
Trond Myklebust1c759502006-10-09 16:18:38 -04001443 };
1444 int ret;
1445
Trond Myklebust61822ab2006-12-05 00:35:42 -05001446 if (!(how & FLUSH_NOWRITEPAGE)) {
1447 ret = generic_writepages(mapping, &wbc);
1448 if (ret < 0)
1449 goto out;
1450 }
Trond Myklebust1c759502006-10-09 16:18:38 -04001451 ret = nfs_sync_mapping_wait(mapping, &wbc, how);
1452 if (ret >= 0)
1453 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001454out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001455 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Trond Myklebust1c759502006-10-09 16:18:38 -04001456 return ret;
1457}
1458
Trond Myklebust61822ab2006-12-05 00:35:42 -05001459int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
Trond Myklebust1c759502006-10-09 16:18:38 -04001460{
1461 loff_t range_start = page_offset(page);
1462 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001463 struct writeback_control wbc = {
1464 .bdi = page->mapping->backing_dev_info,
1465 .sync_mode = WB_SYNC_ALL,
1466 .nr_to_write = LONG_MAX,
1467 .range_start = range_start,
1468 .range_end = range_end,
1469 };
1470 int ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001471
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001472 BUG_ON(!PageLocked(page));
1473 if (!(how & FLUSH_NOWRITEPAGE) && clear_page_dirty_for_io(page)) {
1474 ret = nfs_writepage_locked(page, &wbc);
1475 if (ret < 0)
1476 goto out;
1477 }
Trond Myklebustf40313a2007-01-13 02:28:08 -05001478 if (!PagePrivate(page))
1479 return 0;
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001480 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1481 if (ret >= 0)
1482 return 0;
1483out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001484 __mark_inode_dirty(inode, I_DIRTY_PAGES);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001485 return ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001486}
1487
1488/*
1489 * Write back all requests on one page - we do this before reading it.
1490 */
1491int nfs_wb_page(struct inode *inode, struct page* page)
1492{
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001493 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
Trond Myklebust1c759502006-10-09 16:18:38 -04001494}
1495
Trond Myklebust1a545332006-12-05 00:35:40 -05001496int nfs_set_page_dirty(struct page *page)
1497{
1498 struct nfs_page *req;
1499
1500 req = nfs_page_find_request(page);
1501 if (req != NULL) {
1502 /* Mark any existing write requests for flushing */
1503 set_bit(PG_NEED_FLUSH, &req->wb_flags);
1504 nfs_release_request(req);
1505 }
1506 return __set_page_dirty_nobuffers(page);
1507}
1508
Trond Myklebust1c759502006-10-09 16:18:38 -04001509
David Howellsf7b422b2006-06-09 09:34:33 -04001510int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
1512 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1513 sizeof(struct nfs_write_data),
1514 0, SLAB_HWCACHE_ALIGN,
1515 NULL, NULL);
1516 if (nfs_wdata_cachep == NULL)
1517 return -ENOMEM;
1518
Matthew Dobson93d23412006-03-26 01:37:50 -08001519 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1520 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (nfs_wdata_mempool == NULL)
1522 return -ENOMEM;
1523
Matthew Dobson93d23412006-03-26 01:37:50 -08001524 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1525 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (nfs_commit_mempool == NULL)
1527 return -ENOMEM;
1528
Peter Zijlstra89a09142007-03-16 13:38:26 -08001529 /*
1530 * NFS congestion size, scale with available memory.
1531 *
1532 * 64MB: 8192k
1533 * 128MB: 11585k
1534 * 256MB: 16384k
1535 * 512MB: 23170k
1536 * 1GB: 32768k
1537 * 2GB: 46340k
1538 * 4GB: 65536k
1539 * 8GB: 92681k
1540 * 16GB: 131072k
1541 *
1542 * This allows larger machines to have larger/more transfers.
1543 * Limit the default to 256M
1544 */
1545 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1546 if (nfs_congestion_kb > 256*1024)
1547 nfs_congestion_kb = 256*1024;
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return 0;
1550}
1551
David Brownell266bee82006-06-27 12:59:15 -07001552void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
1554 mempool_destroy(nfs_commit_mempool);
1555 mempool_destroy(nfs_wdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001556 kmem_cache_destroy(nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558