blob: e9eff9376dc0710188a3ba1b15563cb593715592 [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>
15
16#include <linux/sunrpc/clnt.h>
17#include <linux/nfs_fs.h>
18#include <linux/nfs_mount.h>
19#include <linux/nfs_page.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070020#include <linux/backing-dev.h>
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/uaccess.h>
23#include <linux/smp_lock.h>
24
25#include "delegation.h"
Trond Myklebust49a70f22006-12-05 00:35:38 -050026#include "internal.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050027#include "iostat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#define NFSDBG_FACILITY NFSDBG_PAGECACHE
30
31#define MIN_POOL_WRITE (32)
32#define MIN_POOL_COMMIT (4)
33
34/*
35 * Local function declarations
36 */
37static struct nfs_page * nfs_update_request(struct nfs_open_context*,
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 struct page *,
39 unsigned int, unsigned int);
Trond Myklebuste261f512006-12-05 00:35:41 -050040static void nfs_mark_request_dirty(struct nfs_page *req);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static int nfs_wait_on_write_congestion(struct address_space *, int);
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
51static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
52
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070053struct nfs_write_data *nfs_commit_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Christoph Lametere6b4f8d2006-12-06 20:33:14 -080055 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
Chuck Lever40859d72005-11-30 18:09:02 -050056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 if (p) {
58 memset(p, 0, sizeof(*p));
59 INIT_LIST_HEAD(&p->pages);
60 }
61 return p;
62}
63
Trond Myklebust8aca67f2006-11-13 16:23:44 -050064void nfs_commit_rcu_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Trond Myklebust8aca67f2006-11-13 16:23:44 -050066 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
Chuck Lever40859d72005-11-30 18:09:02 -050067 if (p && (p->pagevec != &p->page_array[0]))
68 kfree(p->pagevec);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 mempool_free(p, nfs_commit_mempool);
70}
71
Trond Myklebust8aca67f2006-11-13 16:23:44 -050072void nfs_commit_free(struct nfs_write_data *wdata)
73{
74 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
75}
76
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070077struct nfs_write_data *nfs_writedata_alloc(size_t len)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050078{
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070079 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Christoph Lametere6b4f8d2006-12-06 20:33:14 -080080 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050081
82 if (p) {
83 memset(p, 0, sizeof(*p));
84 INIT_LIST_HEAD(&p->pages);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070085 p->npages = pagecount;
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040086 if (pagecount <= ARRAY_SIZE(p->page_array))
87 p->pagevec = p->page_array;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050088 else {
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040089 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
90 if (!p->pagevec) {
Trond Myklebust3feb2d42006-03-20 13:44:37 -050091 mempool_free(p, nfs_wdata_mempool);
92 p = NULL;
93 }
94 }
95 }
96 return p;
97}
98
Trond Myklebust8aca67f2006-11-13 16:23:44 -050099static void nfs_writedata_rcu_free(struct rcu_head *head)
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500100{
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500101 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500102 if (p && (p->pagevec != &p->page_array[0]))
103 kfree(p->pagevec);
104 mempool_free(p, nfs_wdata_mempool);
105}
106
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500107static void nfs_writedata_free(struct nfs_write_data *wdata)
108{
109 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
110}
111
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100112void nfs_writedata_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 nfs_writedata_free(wdata);
115}
116
Trond Myklebust277459d2006-12-05 00:35:35 -0500117static struct nfs_page *nfs_page_find_request_locked(struct page *page)
118{
119 struct nfs_page *req = NULL;
120
121 if (PagePrivate(page)) {
122 req = (struct nfs_page *)page_private(page);
123 if (req != NULL)
124 atomic_inc(&req->wb_count);
125 }
126 return req;
127}
128
129static struct nfs_page *nfs_page_find_request(struct page *page)
130{
131 struct nfs_page *req = NULL;
132 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
133
134 spin_lock(req_lock);
135 req = nfs_page_find_request_locked(page);
136 spin_unlock(req_lock);
137 return req;
138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/* Adjust the file length if we're writing beyond the end */
141static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
142{
143 struct inode *inode = page->mapping->host;
144 loff_t end, i_size = i_size_read(inode);
145 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
146
147 if (i_size > 0 && page->index < end_index)
148 return;
149 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
150 if (i_size >= end)
151 return;
Chuck Lever91d5b472006-03-20 13:44:14 -0500152 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 i_size_write(inode, end);
154}
155
156/* We can set the PG_uptodate flag if we see that a write request
157 * covers the full page.
158 */
159static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
160{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (PageUptodate(page))
162 return;
163 if (base != 0)
164 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500165 if (count != nfs_page_length(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500167 if (count != PAGE_CACHE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
Trond Myklebust49a70f22006-12-05 00:35:38 -0500169 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Trond Myklebuste21195a2006-12-05 00:35:39 -0500172static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 unsigned int offset, unsigned int count)
174{
175 struct nfs_page *req;
Trond Myklebuste21195a2006-12-05 00:35:39 -0500176 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Trond Myklebuste21195a2006-12-05 00:35:39 -0500178 for (;;) {
179 req = nfs_update_request(ctx, page, offset, count);
180 if (!IS_ERR(req))
181 break;
182 ret = PTR_ERR(req);
183 if (ret != -EBUSY)
184 return ret;
185 ret = nfs_wb_page(page->mapping->host, page);
186 if (ret != 0)
187 return ret;
188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 /* Update file length */
190 nfs_grow_file(page, offset, count);
191 /* Set the PG_uptodate flag? */
192 nfs_mark_uptodate(page, offset, count);
193 nfs_unlock_request(req);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
197static int wb_priority(struct writeback_control *wbc)
198{
199 if (wbc->for_reclaim)
200 return FLUSH_HIGHPRI;
201 if (wbc->for_kupdate)
202 return FLUSH_LOWPRI;
203 return 0;
204}
205
206/*
Trond Myklebuste261f512006-12-05 00:35:41 -0500207 * Find an associated nfs write request, and prepare to flush it out
208 * Returns 1 if there was no write request, or if the request was
209 * already tagged by nfs_set_page_dirty.Returns 0 if the request
210 * was not tagged.
211 * May also return an error if the user signalled nfs_wait_on_request().
212 */
213static int nfs_page_mark_flush(struct page *page)
214{
215 struct nfs_page *req;
216 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
217 int ret;
218
219 spin_lock(req_lock);
220 for(;;) {
221 req = nfs_page_find_request_locked(page);
222 if (req == NULL) {
223 spin_unlock(req_lock);
224 return 1;
225 }
226 if (nfs_lock_request_dontget(req))
227 break;
228 /* Note: If we hold the page lock, as is the case in nfs_writepage,
229 * then the call to nfs_lock_request_dontget() will always
230 * succeed provided that someone hasn't already marked the
231 * request as dirty (in which case we don't care).
232 */
233 spin_unlock(req_lock);
234 ret = nfs_wait_on_request(req);
235 nfs_release_request(req);
236 if (ret != 0)
237 return ret;
238 spin_lock(req_lock);
239 }
240 spin_unlock(req_lock);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500241 if (test_and_set_bit(PG_FLUSHING, &req->wb_flags) == 0) {
Trond Myklebuste261f512006-12-05 00:35:41 -0500242 nfs_mark_request_dirty(req);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500243 set_page_writeback(page);
244 }
Trond Myklebuste261f512006-12-05 00:35:41 -0500245 ret = test_bit(PG_NEED_FLUSH, &req->wb_flags);
246 nfs_unlock_request(req);
247 return ret;
248}
249
250/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 * Write an mmapped page to the server.
252 */
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500253static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 struct nfs_open_context *ctx;
256 struct inode *inode = page->mapping->host;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500257 unsigned offset;
Trond Myklebuste261f512006-12-05 00:35:41 -0500258 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Chuck Lever91d5b472006-03-20 13:44:14 -0500260 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
261 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
262
Trond Myklebuste261f512006-12-05 00:35:41 -0500263 err = nfs_page_mark_flush(page);
264 if (err <= 0)
265 goto out;
266 err = 0;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500267 offset = nfs_page_length(page);
268 if (!offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 goto out;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500270
Trond Myklebustd5308382005-11-04 15:33:38 -0500271 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (ctx == NULL) {
273 err = -EBADF;
274 goto out;
275 }
Trond Myklebust200baa22006-12-05 00:35:40 -0500276 err = nfs_writepage_setup(ctx, page, 0, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 put_nfs_open_context(ctx);
Trond Myklebuste261f512006-12-05 00:35:41 -0500278 if (err != 0)
279 goto out;
280 err = nfs_page_mark_flush(page);
281 if (err > 0)
282 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283out:
Trond Myklebust200baa22006-12-05 00:35:40 -0500284 if (!wbc->for_writepages)
Trond Myklebust02241bc2007-01-13 02:28:07 -0500285 nfs_flush_mapping(page->mapping, wbc, FLUSH_STABLE|wb_priority(wbc));
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500286 return err;
287}
288
289int nfs_writepage(struct page *page, struct writeback_control *wbc)
290{
291 int err;
292
293 err = nfs_writepage_locked(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return err;
296}
297
298/*
299 * Note: causes nfs_update_request() to block on the assumption
300 * that the writeback is generated due to memory pressure.
301 */
302int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
303{
304 struct backing_dev_info *bdi = mapping->backing_dev_info;
305 struct inode *inode = mapping->host;
306 int err;
307
Chuck Lever91d5b472006-03-20 13:44:14 -0500308 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 err = generic_writepages(mapping, wbc);
311 if (err)
312 return err;
313 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
314 if (wbc->nonblocking)
315 return 0;
316 nfs_wait_on_write_congestion(mapping, 0);
317 }
Trond Myklebust28c69252006-09-16 13:04:50 -0400318 err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (err < 0)
320 goto out;
Chuck Lever91d5b472006-03-20 13:44:14 -0500321 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
Trond Myklebustd30c8342006-12-13 15:23:47 -0500322 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323out:
324 clear_bit(BDI_write_congested, &bdi->state);
325 wake_up_all(&nfs_write_congestion);
Andrew Morton3fcfab12006-10-19 23:28:16 -0700326 congestion_end(WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return err;
328}
329
330/*
331 * Insert a write request into an inode
332 */
333static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
334{
335 struct nfs_inode *nfsi = NFS_I(inode);
336 int error;
337
338 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
339 BUG_ON(error == -EEXIST);
340 if (error)
341 return error;
342 if (!nfsi->npages) {
343 igrab(inode);
344 nfs_begin_data_update(inode);
345 if (nfs_have_delegation(inode, FMODE_WRITE))
346 nfsi->change_attr++;
347 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500348 SetPagePrivate(req->wb_page);
Trond Myklebust277459d2006-12-05 00:35:35 -0500349 set_page_private(req->wb_page, (unsigned long)req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 nfsi->npages++;
351 atomic_inc(&req->wb_count);
352 return 0;
353}
354
355/*
356 * Insert a write request into an inode
357 */
358static void nfs_inode_remove_request(struct nfs_page *req)
359{
360 struct inode *inode = req->wb_context->dentry->d_inode;
361 struct nfs_inode *nfsi = NFS_I(inode);
362
363 BUG_ON (!NFS_WBACK_BUSY(req));
364
365 spin_lock(&nfsi->req_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500366 set_page_private(req->wb_page, 0);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500367 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
369 nfsi->npages--;
370 if (!nfsi->npages) {
371 spin_unlock(&nfsi->req_lock);
Trond Myklebust951a1432005-06-22 17:16:30 +0000372 nfs_end_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 iput(inode);
374 } else
375 spin_unlock(&nfsi->req_lock);
376 nfs_clear_request(req);
377 nfs_release_request(req);
378}
379
380/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 * Add a request to the inode's dirty list.
382 */
383static void
384nfs_mark_request_dirty(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 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000390 radix_tree_tag_set(&nfsi->nfs_page_tree,
391 req->wb_index, NFS_PAGE_TAG_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 nfs_list_add_request(req, &nfsi->dirty);
393 nfsi->ndirty++;
394 spin_unlock(&nfsi->req_lock);
Trond Myklebusta1803042006-12-05 00:45:19 -0500395 __mark_inode_dirty(inode, I_DIRTY_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Trond Myklebust61822ab2006-12-05 00:35:42 -0500398static void
399nfs_redirty_request(struct nfs_page *req)
400{
401 clear_bit(PG_FLUSHING, &req->wb_flags);
402 __set_page_dirty_nobuffers(req->wb_page);
403}
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405/*
406 * Check if a request is dirty
407 */
408static inline int
409nfs_dirty_request(struct nfs_page *req)
410{
Trond Myklebuste261f512006-12-05 00:35:41 -0500411 return test_bit(PG_FLUSHING, &req->wb_flags) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
414#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
415/*
416 * Add a request to the inode's commit list.
417 */
418static void
419nfs_mark_request_commit(struct nfs_page *req)
420{
421 struct inode *inode = req->wb_context->dentry->d_inode;
422 struct nfs_inode *nfsi = NFS_I(inode);
423
424 spin_lock(&nfsi->req_lock);
425 nfs_list_add_request(req, &nfsi->commit);
426 nfsi->ncommit++;
427 spin_unlock(&nfsi->req_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700428 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebusta1803042006-12-05 00:45:19 -0500429 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431#endif
432
433/*
434 * Wait for a request to complete.
435 *
436 * Interruptible by signals only if mounted with intr flag.
437 */
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500438static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 struct nfs_inode *nfsi = NFS_I(inode);
441 struct nfs_page *req;
442 unsigned long idx_end, next;
443 unsigned int res = 0;
444 int error;
445
446 if (npages == 0)
447 idx_end = ~0;
448 else
449 idx_end = idx_start + npages - 1;
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 next = idx_start;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000452 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 -0700453 if (req->wb_index > idx_end)
454 break;
455
456 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000457 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 atomic_inc(&req->wb_count);
460 spin_unlock(&nfsi->req_lock);
461 error = nfs_wait_on_request(req);
462 nfs_release_request(req);
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500463 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (error < 0)
465 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 res++;
467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return res;
469}
470
Trond Myklebust83715ad2006-07-05 13:17:12 -0400471static void nfs_cancel_dirty_list(struct list_head *head)
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400472{
473 struct nfs_page *req;
474 while(!list_empty(head)) {
475 req = nfs_list_entry(head->next);
476 nfs_list_remove_request(req);
477 nfs_inode_remove_request(req);
478 nfs_clear_page_writeback(req);
479 }
480}
481
Trond Myklebust83715ad2006-07-05 13:17:12 -0400482static void nfs_cancel_commit_list(struct list_head *head)
483{
484 struct nfs_page *req;
485
486 while(!list_empty(head)) {
487 req = nfs_list_entry(head->next);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700488 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400489 nfs_list_remove_request(req);
490 nfs_inode_remove_request(req);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700491 nfs_unlock_request(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400492 }
493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
496/*
497 * nfs_scan_commit - Scan an inode for commit requests
498 * @inode: NFS inode to scan
499 * @dst: destination list
500 * @idx_start: lower bound of page->index to scan.
501 * @npages: idx_start + npages sets the upper bound to scan.
502 *
503 * Moves requests from the inode's 'commit' request list.
504 * The requests are *not* checked to ensure that they form a contiguous set.
505 */
506static int
507nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
508{
509 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000510 int res = 0;
511
512 if (nfsi->ncommit != 0) {
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400513 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000514 nfsi->ncommit -= res;
515 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
516 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return res;
519}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500520#else
521static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
522{
523 return 0;
524}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525#endif
526
527static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
528{
529 struct backing_dev_info *bdi = mapping->backing_dev_info;
530 DEFINE_WAIT(wait);
531 int ret = 0;
532
533 might_sleep();
534
535 if (!bdi_write_congested(bdi))
536 return 0;
Chuck Lever91d5b472006-03-20 13:44:14 -0500537
538 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (intr) {
541 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
542 sigset_t oldset;
543
544 rpc_clnt_sigmask(clnt, &oldset);
545 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
546 if (bdi_write_congested(bdi)) {
547 if (signalled())
548 ret = -ERESTARTSYS;
549 else
550 schedule();
551 }
552 rpc_clnt_sigunmask(clnt, &oldset);
553 } else {
554 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
555 if (bdi_write_congested(bdi))
556 schedule();
557 }
558 finish_wait(&nfs_write_congestion, &wait);
559 return ret;
560}
561
562
563/*
564 * Try to update any existing write request, or create one if there is none.
565 * In order to match, the request's credentials must match those of
566 * the calling process.
567 *
568 * Note: Should always be called with the Page Lock held!
569 */
570static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
Trond Myklebuste21195a2006-12-05 00:35:39 -0500571 struct page *page, unsigned int offset, unsigned int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Trond Myklebuste21195a2006-12-05 00:35:39 -0500573 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 struct nfs_inode *nfsi = NFS_I(inode);
575 struct nfs_page *req, *new = NULL;
576 unsigned long rqend, end;
577
578 end = offset + bytes;
579
Trond Myklebuste21195a2006-12-05 00:35:39 -0500580 if (nfs_wait_on_write_congestion(page->mapping, NFS_SERVER(inode)->flags & NFS_MOUNT_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return ERR_PTR(-ERESTARTSYS);
582 for (;;) {
583 /* Loop over all inode entries and see if we find
584 * A request for the page we wish to update
585 */
586 spin_lock(&nfsi->req_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500587 req = nfs_page_find_request_locked(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (req) {
589 if (!nfs_lock_request_dontget(req)) {
590 int error;
Trond Myklebust277459d2006-12-05 00:35:35 -0500591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 spin_unlock(&nfsi->req_lock);
593 error = nfs_wait_on_request(req);
594 nfs_release_request(req);
Neil Brown1dd594b2006-03-20 13:44:04 -0500595 if (error < 0) {
596 if (new)
597 nfs_release_request(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return ERR_PTR(error);
Neil Brown1dd594b2006-03-20 13:44:04 -0500599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 continue;
601 }
602 spin_unlock(&nfsi->req_lock);
603 if (new)
604 nfs_release_request(new);
605 break;
606 }
607
608 if (new) {
609 int error;
610 nfs_lock_request_dontget(new);
611 error = nfs_inode_add_request(inode, new);
612 if (error) {
613 spin_unlock(&nfsi->req_lock);
614 nfs_unlock_request(new);
615 return ERR_PTR(error);
616 }
617 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return new;
619 }
620 spin_unlock(&nfsi->req_lock);
621
622 new = nfs_create_request(ctx, inode, page, offset, bytes);
623 if (IS_ERR(new))
624 return new;
625 }
626
627 /* We have a request for our page.
628 * If the creds don't match, or the
629 * page addresses don't match,
630 * tell the caller to wait on the conflicting
631 * request.
632 */
633 rqend = req->wb_offset + req->wb_bytes;
634 if (req->wb_context != ctx
635 || req->wb_page != page
636 || !nfs_dirty_request(req)
637 || offset > rqend || end < req->wb_offset) {
638 nfs_unlock_request(req);
639 return ERR_PTR(-EBUSY);
640 }
641
642 /* Okay, the request matches. Update the region */
643 if (offset < req->wb_offset) {
644 req->wb_offset = offset;
645 req->wb_pgbase = offset;
646 req->wb_bytes = rqend - req->wb_offset;
647 }
648
649 if (end > rqend)
650 req->wb_bytes = end - req->wb_offset;
651
652 return req;
653}
654
655int nfs_flush_incompatible(struct file *file, struct page *page)
656{
657 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 struct nfs_page *req;
Trond Myklebust1a545332006-12-05 00:35:40 -0500659 int do_flush, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 /*
661 * Look for a request corresponding to this page. If there
662 * is one, and it belongs to another file, we flush it out
663 * before we try to copy anything into the page. Do this
664 * due to the lack of an ACCESS-type call in NFSv2.
665 * Also do the same if we find a request from an existing
666 * dropped page.
667 */
Trond Myklebust1a545332006-12-05 00:35:40 -0500668 do {
669 req = nfs_page_find_request(page);
670 if (req == NULL)
671 return 0;
672 do_flush = req->wb_page != page || req->wb_context != ctx
Trond Myklebuste261f512006-12-05 00:35:41 -0500673 || !nfs_dirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 nfs_release_request(req);
Trond Myklebust1a545332006-12-05 00:35:40 -0500675 if (!do_flush)
676 return 0;
677 status = nfs_wb_page(page->mapping->host, page);
678 } while (status == 0);
679 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
682/*
683 * Update and possibly write a cached page of an NFS file.
684 *
685 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
686 * things with a page scheduled for an RPC call (e.g. invalidate it).
687 */
688int nfs_updatepage(struct file *file, struct page *page,
689 unsigned int offset, unsigned int count)
690{
691 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 int status = 0;
694
Chuck Lever91d5b472006-03-20 13:44:14 -0500695 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800698 file->f_path.dentry->d_parent->d_name.name,
699 file->f_path.dentry->d_name.name, count,
Chuck Lever0bbacc42005-11-01 16:53:32 -0500700 (long long)(page_offset(page) +offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /* If we're not using byte range locks, and we know the page
703 * is entirely in cache, it may be more efficient to avoid
704 * fragmenting write requests.
705 */
Trond Myklebustab0a3db2005-06-22 17:16:30 +0000706 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
Trond Myklebust49a70f22006-12-05 00:35:38 -0500707 count = max(count + offset, nfs_page_length(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710
Trond Myklebuste21195a2006-12-05 00:35:39 -0500711 status = nfs_writepage_setup(ctx, page, offset, count);
Trond Myklebuste261f512006-12-05 00:35:41 -0500712 __set_page_dirty_nobuffers(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
715 status, (long long)i_size_read(inode));
716 if (status < 0)
717 ClearPageUptodate(page);
718 return status;
719}
720
721static void nfs_writepage_release(struct nfs_page *req)
722{
723 end_page_writeback(req->wb_page);
724
725#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
726 if (!PageError(req->wb_page)) {
727 if (NFS_NEED_RESCHED(req)) {
Trond Myklebust61822ab2006-12-05 00:35:42 -0500728 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 goto out;
730 } else if (NFS_NEED_COMMIT(req)) {
731 nfs_mark_request_commit(req);
732 goto out;
733 }
734 }
735 nfs_inode_remove_request(req);
736
737out:
738 nfs_clear_commit(req);
739 nfs_clear_reschedule(req);
740#else
741 nfs_inode_remove_request(req);
742#endif
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000743 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
746static inline int flush_task_priority(int how)
747{
748 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
749 case FLUSH_HIGHPRI:
750 return RPC_PRIORITY_HIGH;
751 case FLUSH_LOWPRI:
752 return RPC_PRIORITY_LOW;
753 }
754 return RPC_PRIORITY_NORMAL;
755}
756
757/*
758 * Set up the argument/result storage required for the RPC call.
759 */
760static void nfs_write_rpcsetup(struct nfs_page *req,
761 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500762 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 unsigned int count, unsigned int offset,
764 int how)
765{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -0500767 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 /* Set up the RPC argument and reply structs
770 * NB: take care not to mess about with data->commit et al. */
771
772 data->req = req;
773 data->inode = inode = req->wb_context->dentry->d_inode;
774 data->cred = req->wb_context->cred;
775
776 data->args.fh = NFS_FH(inode);
777 data->args.offset = req_offset(req) + offset;
778 data->args.pgbase = req->wb_pgbase + offset;
779 data->args.pages = data->pagevec;
780 data->args.count = count;
781 data->args.context = req->wb_context;
782
783 data->res.fattr = &data->fattr;
784 data->res.count = count;
785 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400786 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Trond Myklebust788e7a82006-03-20 13:44:27 -0500788 /* Set up the initial task struct. */
789 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
790 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 NFS_PROTO(inode)->write_setup(data, how);
792
793 data->task.tk_priority = flush_task_priority(how);
794 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500797 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 inode->i_sb->s_id,
799 (long long)NFS_FILEID(inode),
800 count,
801 (unsigned long long)data->args.offset);
802}
803
804static void nfs_execute_write(struct nfs_write_data *data)
805{
806 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
807 sigset_t oldset;
808
809 rpc_clnt_sigmask(clnt, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 rpc_execute(&data->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 rpc_clnt_sigunmask(clnt, &oldset);
812}
813
814/*
815 * Generate multiple small requests to write out a single
816 * contiguous dirty area on one page.
817 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500818static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
820 struct nfs_page *req = nfs_list_entry(head->next);
821 struct page *page = req->wb_page;
822 struct nfs_write_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700823 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
824 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 int requests = 0;
826 LIST_HEAD(list);
827
828 nfs_list_remove_request(req);
829
830 nbytes = req->wb_bytes;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700831 do {
832 size_t len = min(nbytes, wsize);
833
834 data = nfs_writedata_alloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (!data)
836 goto out_bad;
837 list_add(&data->pages, &list);
838 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700839 nbytes -= len;
840 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 atomic_set(&req->wb_complete, requests);
842
843 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 offset = 0;
845 nbytes = req->wb_bytes;
846 do {
847 data = list_entry(list.next, struct nfs_write_data, pages);
848 list_del_init(&data->pages);
849
850 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 if (nbytes > wsize) {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500853 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
854 wsize, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 offset += wsize;
856 nbytes -= wsize;
857 } else {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500858 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
859 nbytes, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 nbytes = 0;
861 }
862 nfs_execute_write(data);
863 } while (nbytes != 0);
864
865 return 0;
866
867out_bad:
868 while (!list_empty(&list)) {
869 data = list_entry(list.next, struct nfs_write_data, pages);
870 list_del(&data->pages);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500871 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
Trond Myklebust61822ab2006-12-05 00:35:42 -0500873 nfs_redirty_request(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000874 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return -ENOMEM;
876}
877
878/*
879 * Create an RPC task for the given write request and kick it.
880 * The page must have been locked by the caller.
881 *
882 * It may happen that the page we're passed is not marked dirty.
883 * This is the case if nfs_updatepage detects a conflicting request
884 * that has been written but not committed.
885 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500886static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888 struct nfs_page *req;
889 struct page **pages;
890 struct nfs_write_data *data;
891 unsigned int count;
892
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700893 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (!data)
895 goto out_bad;
896
897 pages = data->pagevec;
898 count = 0;
899 while (!list_empty(head)) {
900 req = nfs_list_entry(head->next);
901 nfs_list_remove_request(req);
902 nfs_list_add_request(req, &data->pages);
903 ClearPageError(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 *pages++ = req->wb_page;
905 count += req->wb_bytes;
906 }
907 req = nfs_list_entry(data->pages.next);
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 /* Set up the argument struct */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500910 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 nfs_execute_write(data);
913 return 0;
914 out_bad:
915 while (!list_empty(head)) {
916 struct nfs_page *req = nfs_list_entry(head->next);
917 nfs_list_remove_request(req);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500918 nfs_redirty_request(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000919 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921 return -ENOMEM;
922}
923
Trond Myklebust7d46a492006-03-20 13:44:50 -0500924static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
926 LIST_HEAD(one_request);
Trond Myklebust7d46a492006-03-20 13:44:50 -0500927 int (*flush_one)(struct inode *, struct list_head *, int);
928 struct nfs_page *req;
929 int wpages = NFS_SERVER(inode)->wpages;
930 int wsize = NFS_SERVER(inode)->wsize;
931 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Trond Myklebust7d46a492006-03-20 13:44:50 -0500933 flush_one = nfs_flush_one;
934 if (wsize < PAGE_CACHE_SIZE)
935 flush_one = nfs_flush_multi;
936 /* For single writes, FLUSH_STABLE is more efficient */
937 if (npages <= wpages && npages == NFS_I(inode)->npages
938 && nfs_list_entry(head->next)->wb_bytes <= wsize)
939 how |= FLUSH_STABLE;
940
941 do {
942 nfs_coalesce_requests(head, &one_request, wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 req = nfs_list_entry(one_request.next);
Trond Myklebust7d46a492006-03-20 13:44:50 -0500944 error = flush_one(inode, &one_request, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (error < 0)
Trond Myklebust7d46a492006-03-20 13:44:50 -0500946 goto out_err;
947 } while (!list_empty(head));
948 return 0;
949out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 while (!list_empty(head)) {
951 req = nfs_list_entry(head->next);
952 nfs_list_remove_request(req);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500953 nfs_redirty_request(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000954 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
956 return error;
957}
958
959/*
960 * Handle a write reply that flushed part of a page.
961 */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500962static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Trond Myklebust788e7a82006-03-20 13:44:27 -0500964 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 struct nfs_page *req = data->req;
966 struct page *page = req->wb_page;
967
968 dprintk("NFS: write (%s/%Ld %d@%Ld)",
969 req->wb_context->dentry->d_inode->i_sb->s_id,
970 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
971 req->wb_bytes,
972 (long long)req_offset(req));
973
Trond Myklebust788e7a82006-03-20 13:44:27 -0500974 if (nfs_writeback_done(task, data) != 0)
975 return;
976
977 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 ClearPageUptodate(page);
979 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500980 req->wb_context->error = task->tk_status;
981 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 } else {
983#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
984 if (data->verf.committed < NFS_FILE_SYNC) {
985 if (!NFS_NEED_COMMIT(req)) {
986 nfs_defer_commit(req);
987 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
988 dprintk(" defer commit\n");
989 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
990 nfs_defer_reschedule(req);
991 dprintk(" server reboot detected\n");
992 }
993 } else
994#endif
995 dprintk(" OK\n");
996 }
997
998 if (atomic_dec_and_test(&req->wb_complete))
999 nfs_writepage_release(req);
1000}
1001
Trond Myklebust788e7a82006-03-20 13:44:27 -05001002static const struct rpc_call_ops nfs_write_partial_ops = {
1003 .rpc_call_done = nfs_writeback_done_partial,
1004 .rpc_release = nfs_writedata_release,
1005};
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007/*
1008 * Handle a write reply that flushes a whole page.
1009 *
1010 * FIXME: There is an inherent race with invalidate_inode_pages and
1011 * writebacks since the page->count is kept > 1 for as long
1012 * as the page has a write request pending.
1013 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001014static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001016 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 struct nfs_page *req;
1018 struct page *page;
1019
Trond Myklebust788e7a82006-03-20 13:44:27 -05001020 if (nfs_writeback_done(task, data) != 0)
1021 return;
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 /* Update attributes as result of writeback. */
1024 while (!list_empty(&data->pages)) {
1025 req = nfs_list_entry(data->pages.next);
1026 nfs_list_remove_request(req);
1027 page = req->wb_page;
1028
1029 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1030 req->wb_context->dentry->d_inode->i_sb->s_id,
1031 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1032 req->wb_bytes,
1033 (long long)req_offset(req));
1034
Trond Myklebust788e7a82006-03-20 13:44:27 -05001035 if (task->tk_status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 ClearPageUptodate(page);
1037 SetPageError(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001038 req->wb_context->error = task->tk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 end_page_writeback(page);
1040 nfs_inode_remove_request(req);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001041 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 goto next;
1043 }
1044 end_page_writeback(page);
1045
1046#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1047 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1048 nfs_inode_remove_request(req);
1049 dprintk(" OK\n");
1050 goto next;
1051 }
1052 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1053 nfs_mark_request_commit(req);
1054 dprintk(" marked for commit\n");
1055#else
1056 nfs_inode_remove_request(req);
1057#endif
1058 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001059 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
1061}
1062
Trond Myklebust788e7a82006-03-20 13:44:27 -05001063static const struct rpc_call_ops nfs_write_full_ops = {
1064 .rpc_call_done = nfs_writeback_done_full,
1065 .rpc_release = nfs_writedata_release,
1066};
1067
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069/*
1070 * This function is called when the WRITE call is complete.
1071 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001072int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 struct nfs_writeargs *argp = &data->args;
1075 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001076 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1079 task->tk_pid, task->tk_status);
1080
Chuck Leverf551e442006-09-20 14:33:04 -04001081 /*
1082 * ->write_done will attempt to use post-op attributes to detect
1083 * conflicting writes by other clients. A strict interpretation
1084 * of close-to-open would allow us to continue caching even if
1085 * another writer had changed the file, but some applications
1086 * depend on tighter cache coherency when writing.
1087 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001088 status = NFS_PROTO(data->inode)->write_done(task, data);
1089 if (status != 0)
1090 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001091 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1094 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1095 /* We tried a write call, but the server did not
1096 * commit data to stable storage even though we
1097 * requested it.
1098 * Note: There is a known bug in Tru64 < 5.0 in which
1099 * the server reports NFS_DATA_SYNC, but performs
1100 * NFS_FILE_SYNC. We therefore implement this checking
1101 * as a dprintk() in order to avoid filling syslog.
1102 */
1103 static unsigned long complain;
1104
1105 if (time_before(complain, jiffies)) {
1106 dprintk("NFS: faulty NFS server %s:"
1107 " (committed = %d) != (stable = %d)\n",
David Howells54ceac42006-08-22 20:06:13 -04001108 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 resp->verf->committed, argp->stable);
1110 complain = jiffies + 300 * HZ;
1111 }
1112 }
1113#endif
1114 /* Is this a short write? */
1115 if (task->tk_status >= 0 && resp->count < argp->count) {
1116 static unsigned long complain;
1117
Chuck Lever91d5b472006-03-20 13:44:14 -05001118 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 /* Has the server at least made some progress? */
1121 if (resp->count != 0) {
1122 /* Was this an NFSv2 write or an NFSv3 stable write? */
1123 if (resp->verf->committed != NFS_UNSTABLE) {
1124 /* Resend from where the server left off */
1125 argp->offset += resp->count;
1126 argp->pgbase += resp->count;
1127 argp->count -= resp->count;
1128 } else {
1129 /* Resend as a stable write in order to avoid
1130 * headaches in the case of a server crash.
1131 */
1132 argp->stable = NFS_FILE_SYNC;
1133 }
1134 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001135 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
1137 if (time_before(complain, jiffies)) {
1138 printk(KERN_WARNING
1139 "NFS: Server wrote zero bytes, expected %u.\n",
1140 argp->count);
1141 complain = jiffies + 300 * HZ;
1142 }
1143 /* Can't do anything about it except throw an error. */
1144 task->tk_status = -EIO;
1145 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001146 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
1148
1149
1150#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001151void nfs_commit_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 nfs_commit_free(wdata);
1154}
1155
1156/*
1157 * Set up the argument/result storage required for the RPC call.
1158 */
1159static void nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001160 struct nfs_write_data *data,
1161 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001163 struct nfs_page *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001165 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 /* Set up the RPC argument and reply structs
1168 * NB: take care not to mess about with data->commit et al. */
1169
1170 list_splice_init(head, &data->pages);
1171 first = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 inode = first->wb_context->dentry->d_inode;
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 data->inode = inode;
1175 data->cred = first->wb_context->cred;
1176
1177 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001178 /* Note: we always request a commit of the entire inode */
1179 data->args.offset = 0;
1180 data->args.count = 0;
1181 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 data->res.fattr = &data->fattr;
1183 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001184 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001185
1186 /* Set up the initial task struct. */
1187 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1188 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 NFS_PROTO(inode)->commit_setup(data, how);
1190
1191 data->task.tk_priority = flush_task_priority(how);
1192 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Chuck Lever0bbacc42005-11-01 16:53:32 -05001194 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
1197/*
1198 * Commit dirty pages
1199 */
1200static int
Chuck Lever40859d72005-11-30 18:09:02 -05001201nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
1203 struct nfs_write_data *data;
1204 struct nfs_page *req;
1205
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001206 data = nfs_commit_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 if (!data)
1209 goto out_bad;
1210
1211 /* Set up the argument struct */
1212 nfs_commit_rpcsetup(head, data, how);
1213
1214 nfs_execute_write(data);
1215 return 0;
1216 out_bad:
1217 while (!list_empty(head)) {
1218 req = nfs_list_entry(head->next);
1219 nfs_list_remove_request(req);
1220 nfs_mark_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001221 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust5c2d97c2006-09-18 23:20:35 -04001222 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224 return -ENOMEM;
1225}
1226
1227/*
1228 * COMMIT call returned
1229 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001230static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001232 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1236 task->tk_pid, task->tk_status);
1237
Trond Myklebust788e7a82006-03-20 13:44:27 -05001238 /* Call the NFS version-specific code */
1239 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1240 return;
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 while (!list_empty(&data->pages)) {
1243 req = nfs_list_entry(data->pages.next);
1244 nfs_list_remove_request(req);
Christoph Lameterfd39fc82006-06-30 01:55:40 -07001245 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1248 req->wb_context->dentry->d_inode->i_sb->s_id,
1249 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1250 req->wb_bytes,
1251 (long long)req_offset(req));
1252 if (task->tk_status < 0) {
1253 req->wb_context->error = task->tk_status;
1254 nfs_inode_remove_request(req);
1255 dprintk(", error = %d\n", task->tk_status);
1256 goto next;
1257 }
1258
1259 /* Okay, COMMIT succeeded, apparently. Check the verifier
1260 * returned by the server against all stored verfs. */
1261 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1262 /* We have a match */
1263 nfs_inode_remove_request(req);
1264 dprintk(" OK\n");
1265 goto next;
1266 }
1267 /* We have a mismatch. Write the page again */
1268 dprintk(" mismatch\n");
Trond Myklebust61822ab2006-12-05 00:35:42 -05001269 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001271 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001274
1275static const struct rpc_call_ops nfs_commit_ops = {
1276 .rpc_call_done = nfs_commit_done,
1277 .rpc_release = nfs_commit_release,
1278};
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001279#else
1280static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1281{
1282 return 0;
1283}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284#endif
1285
Trond Myklebust3f442542006-09-17 14:46:44 -04001286static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Trond Myklebust28c69252006-09-16 13:04:50 -04001288 struct nfs_inode *nfsi = NFS_I(mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 LIST_HEAD(head);
Trond Myklebust3f442542006-09-17 14:46:44 -04001290 long res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 spin_lock(&nfsi->req_lock);
Trond Myklebust3f442542006-09-17 14:46:44 -04001293 res = nfs_scan_dirty(mapping, wbc, &head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 spin_unlock(&nfsi->req_lock);
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001295 if (res) {
Trond Myklebust28c69252006-09-16 13:04:50 -04001296 int error = nfs_flush_list(mapping->host, &head, res, how);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001297 if (error < 0)
1298 return error;
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return res;
1301}
1302
1303#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001304int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
1306 struct nfs_inode *nfsi = NFS_I(inode);
1307 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001308 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001311 res = nfs_scan_commit(inode, &head, 0, 0);
1312 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001314 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001315 if (error < 0)
1316 return error;
1317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return res;
1319}
1320#endif
1321
Trond Myklebust1c759502006-10-09 16:18:38 -04001322long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
Trond Myklebust1c759502006-10-09 16:18:38 -04001324 struct inode *inode = mapping->host;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001325 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c759502006-10-09 16:18:38 -04001326 unsigned long idx_start, idx_end;
1327 unsigned int npages = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001328 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001329 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebust3f442542006-09-17 14:46:44 -04001330 long pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Trond Myklebust1c759502006-10-09 16:18:38 -04001332 /* FIXME */
1333 if (wbc->range_cyclic)
1334 idx_start = 0;
1335 else {
1336 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1337 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1338 if (idx_end > idx_start) {
1339 unsigned long l_npages = 1 + idx_end - idx_start;
1340 npages = l_npages;
1341 if (sizeof(npages) != sizeof(l_npages) &&
1342 (unsigned long)npages != l_npages)
1343 npages = 0;
1344 }
1345 }
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001346 how &= ~FLUSH_NOCOMMIT;
1347 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 do {
Trond Myklebust1c759502006-10-09 16:18:38 -04001349 wbc->pages_skipped = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001350 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1351 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001352 continue;
Trond Myklebust1c759502006-10-09 16:18:38 -04001353 pages = nfs_scan_dirty(mapping, wbc, &head);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001354 if (pages != 0) {
1355 spin_unlock(&nfsi->req_lock);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001356 if (how & FLUSH_INVALIDATE) {
Trond Myklebust83715ad2006-07-05 13:17:12 -04001357 nfs_cancel_dirty_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001358 ret = pages;
1359 } else
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001360 ret = nfs_flush_list(inode, &head, pages, how);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001361 spin_lock(&nfsi->req_lock);
1362 continue;
1363 }
Trond Myklebust1c759502006-10-09 16:18:38 -04001364 if (wbc->pages_skipped != 0)
1365 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001366 if (nocommit)
1367 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001368 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebust1c759502006-10-09 16:18:38 -04001369 if (pages == 0) {
1370 if (wbc->pages_skipped != 0)
1371 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001372 break;
Trond Myklebust1c759502006-10-09 16:18:38 -04001373 }
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001374 if (how & FLUSH_INVALIDATE) {
1375 spin_unlock(&nfsi->req_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001376 nfs_cancel_commit_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001377 ret = pages;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001378 spin_lock(&nfsi->req_lock);
1379 continue;
1380 }
1381 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001382 spin_unlock(&nfsi->req_lock);
1383 ret = nfs_commit_list(inode, &head, how);
1384 spin_lock(&nfsi->req_lock);
1385 } while (ret >= 0);
1386 spin_unlock(&nfsi->req_lock);
1387 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388}
1389
Trond Myklebust1c759502006-10-09 16:18:38 -04001390/*
1391 * flush the inode to disk.
1392 */
1393int nfs_wb_all(struct inode *inode)
1394{
1395 struct address_space *mapping = inode->i_mapping;
1396 struct writeback_control wbc = {
1397 .bdi = mapping->backing_dev_info,
1398 .sync_mode = WB_SYNC_ALL,
1399 .nr_to_write = LONG_MAX,
Trond Myklebust61822ab2006-12-05 00:35:42 -05001400 .for_writepages = 1,
Trond Myklebust1c759502006-10-09 16:18:38 -04001401 .range_cyclic = 1,
1402 };
1403 int ret;
1404
Trond Myklebust61822ab2006-12-05 00:35:42 -05001405 ret = generic_writepages(mapping, &wbc);
1406 if (ret < 0)
1407 goto out;
Trond Myklebust1c759502006-10-09 16:18:38 -04001408 ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
1409 if (ret >= 0)
1410 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001411out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001412 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Trond Myklebust1c759502006-10-09 16:18:38 -04001413 return ret;
1414}
1415
1416int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
1417{
1418 struct writeback_control wbc = {
1419 .bdi = mapping->backing_dev_info,
1420 .sync_mode = WB_SYNC_ALL,
1421 .nr_to_write = LONG_MAX,
1422 .range_start = range_start,
1423 .range_end = range_end,
Trond Myklebust61822ab2006-12-05 00:35:42 -05001424 .for_writepages = 1,
Trond Myklebust1c759502006-10-09 16:18:38 -04001425 };
1426 int ret;
1427
Trond Myklebust61822ab2006-12-05 00:35:42 -05001428 if (!(how & FLUSH_NOWRITEPAGE)) {
1429 ret = generic_writepages(mapping, &wbc);
1430 if (ret < 0)
1431 goto out;
1432 }
Trond Myklebust1c759502006-10-09 16:18:38 -04001433 ret = nfs_sync_mapping_wait(mapping, &wbc, how);
1434 if (ret >= 0)
1435 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001436out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001437 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Trond Myklebust1c759502006-10-09 16:18:38 -04001438 return ret;
1439}
1440
Trond Myklebust61822ab2006-12-05 00:35:42 -05001441int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
Trond Myklebust1c759502006-10-09 16:18:38 -04001442{
1443 loff_t range_start = page_offset(page);
1444 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001445 struct writeback_control wbc = {
1446 .bdi = page->mapping->backing_dev_info,
1447 .sync_mode = WB_SYNC_ALL,
1448 .nr_to_write = LONG_MAX,
1449 .range_start = range_start,
1450 .range_end = range_end,
1451 };
1452 int ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001453
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001454 BUG_ON(!PageLocked(page));
1455 if (!(how & FLUSH_NOWRITEPAGE) && clear_page_dirty_for_io(page)) {
1456 ret = nfs_writepage_locked(page, &wbc);
1457 if (ret < 0)
1458 goto out;
1459 }
Trond Myklebustf40313a2007-01-13 02:28:08 -05001460 if (!PagePrivate(page))
1461 return 0;
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001462 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1463 if (ret >= 0)
1464 return 0;
1465out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001466 __mark_inode_dirty(inode, I_DIRTY_PAGES);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001467 return ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001468}
1469
1470/*
1471 * Write back all requests on one page - we do this before reading it.
1472 */
1473int nfs_wb_page(struct inode *inode, struct page* page)
1474{
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001475 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
Trond Myklebust1c759502006-10-09 16:18:38 -04001476}
1477
Trond Myklebust1a545332006-12-05 00:35:40 -05001478int nfs_set_page_dirty(struct page *page)
1479{
1480 struct nfs_page *req;
1481
1482 req = nfs_page_find_request(page);
1483 if (req != NULL) {
1484 /* Mark any existing write requests for flushing */
1485 set_bit(PG_NEED_FLUSH, &req->wb_flags);
1486 nfs_release_request(req);
1487 }
1488 return __set_page_dirty_nobuffers(page);
1489}
1490
Trond Myklebust1c759502006-10-09 16:18:38 -04001491
David Howellsf7b422b2006-06-09 09:34:33 -04001492int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
1494 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1495 sizeof(struct nfs_write_data),
1496 0, SLAB_HWCACHE_ALIGN,
1497 NULL, NULL);
1498 if (nfs_wdata_cachep == NULL)
1499 return -ENOMEM;
1500
Matthew Dobson93d23412006-03-26 01:37:50 -08001501 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1502 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (nfs_wdata_mempool == NULL)
1504 return -ENOMEM;
1505
Matthew Dobson93d23412006-03-26 01:37:50 -08001506 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1507 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 if (nfs_commit_mempool == NULL)
1509 return -ENOMEM;
1510
1511 return 0;
1512}
1513
David Brownell266bee82006-06-27 12:59:15 -07001514void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 mempool_destroy(nfs_commit_mempool);
1517 mempool_destroy(nfs_wdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001518 kmem_cache_destroy(nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519}
1520