blob: febdade91670562b003a43fd3e0ee8ad900445eb [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
Trond Myklebusta301b772007-02-06 11:07:15 -0800156/* A writeback failed: mark the page as bad, and invalidate the page cache */
157static void nfs_set_pageerror(struct page *page)
158{
159 SetPageError(page);
160 nfs_zap_mapping(page->mapping->host, page->mapping);
161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/* We can set the PG_uptodate flag if we see that a write request
164 * covers the full page.
165 */
166static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
167{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (PageUptodate(page))
169 return;
170 if (base != 0)
171 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500172 if (count != nfs_page_length(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500174 if (count != PAGE_CACHE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
Trond Myklebust49a70f22006-12-05 00:35:38 -0500176 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Trond Myklebuste21195a2006-12-05 00:35:39 -0500179static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 unsigned int offset, unsigned int count)
181{
182 struct nfs_page *req;
Trond Myklebuste21195a2006-12-05 00:35:39 -0500183 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Trond Myklebuste21195a2006-12-05 00:35:39 -0500185 for (;;) {
186 req = nfs_update_request(ctx, page, offset, count);
187 if (!IS_ERR(req))
188 break;
189 ret = PTR_ERR(req);
190 if (ret != -EBUSY)
191 return ret;
192 ret = nfs_wb_page(page->mapping->host, page);
193 if (ret != 0)
194 return ret;
195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /* Update file length */
197 nfs_grow_file(page, offset, count);
198 /* Set the PG_uptodate flag? */
199 nfs_mark_uptodate(page, offset, count);
200 nfs_unlock_request(req);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
204static int wb_priority(struct writeback_control *wbc)
205{
206 if (wbc->for_reclaim)
207 return FLUSH_HIGHPRI;
208 if (wbc->for_kupdate)
209 return FLUSH_LOWPRI;
210 return 0;
211}
212
213/*
Trond Myklebuste261f512006-12-05 00:35:41 -0500214 * Find an associated nfs write request, and prepare to flush it out
215 * Returns 1 if there was no write request, or if the request was
216 * already tagged by nfs_set_page_dirty.Returns 0 if the request
217 * was not tagged.
218 * May also return an error if the user signalled nfs_wait_on_request().
219 */
220static int nfs_page_mark_flush(struct page *page)
221{
222 struct nfs_page *req;
223 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
224 int ret;
225
226 spin_lock(req_lock);
227 for(;;) {
228 req = nfs_page_find_request_locked(page);
229 if (req == NULL) {
230 spin_unlock(req_lock);
231 return 1;
232 }
233 if (nfs_lock_request_dontget(req))
234 break;
235 /* Note: If we hold the page lock, as is the case in nfs_writepage,
236 * then the call to nfs_lock_request_dontget() will always
237 * succeed provided that someone hasn't already marked the
238 * request as dirty (in which case we don't care).
239 */
240 spin_unlock(req_lock);
241 ret = nfs_wait_on_request(req);
242 nfs_release_request(req);
243 if (ret != 0)
244 return ret;
245 spin_lock(req_lock);
246 }
247 spin_unlock(req_lock);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500248 if (test_and_set_bit(PG_FLUSHING, &req->wb_flags) == 0) {
Trond Myklebuste261f512006-12-05 00:35:41 -0500249 nfs_mark_request_dirty(req);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500250 set_page_writeback(page);
251 }
Trond Myklebuste261f512006-12-05 00:35:41 -0500252 ret = test_bit(PG_NEED_FLUSH, &req->wb_flags);
253 nfs_unlock_request(req);
254 return ret;
255}
256
257/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 * Write an mmapped page to the server.
259 */
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500260static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 struct nfs_open_context *ctx;
263 struct inode *inode = page->mapping->host;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500264 unsigned offset;
Trond Myklebuste261f512006-12-05 00:35:41 -0500265 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Chuck Lever91d5b472006-03-20 13:44:14 -0500267 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
268 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
269
Trond Myklebuste261f512006-12-05 00:35:41 -0500270 err = nfs_page_mark_flush(page);
271 if (err <= 0)
272 goto out;
273 err = 0;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500274 offset = nfs_page_length(page);
275 if (!offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 goto out;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500277
Trond Myklebustd5308382005-11-04 15:33:38 -0500278 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (ctx == NULL) {
280 err = -EBADF;
281 goto out;
282 }
Trond Myklebust200baa22006-12-05 00:35:40 -0500283 err = nfs_writepage_setup(ctx, page, 0, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 put_nfs_open_context(ctx);
Trond Myklebuste261f512006-12-05 00:35:41 -0500285 if (err != 0)
286 goto out;
287 err = nfs_page_mark_flush(page);
288 if (err > 0)
289 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290out:
Trond Myklebust200baa22006-12-05 00:35:40 -0500291 if (!wbc->for_writepages)
Trond Myklebust02241bc2007-01-13 02:28:07 -0500292 nfs_flush_mapping(page->mapping, wbc, FLUSH_STABLE|wb_priority(wbc));
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500293 return err;
294}
295
296int nfs_writepage(struct page *page, struct writeback_control *wbc)
297{
298 int err;
299
300 err = nfs_writepage_locked(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return err;
303}
304
305/*
306 * Note: causes nfs_update_request() to block on the assumption
307 * that the writeback is generated due to memory pressure.
308 */
309int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
310{
311 struct backing_dev_info *bdi = mapping->backing_dev_info;
312 struct inode *inode = mapping->host;
313 int err;
314
Chuck Lever91d5b472006-03-20 13:44:14 -0500315 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 err = generic_writepages(mapping, wbc);
318 if (err)
319 return err;
320 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
321 if (wbc->nonblocking)
322 return 0;
323 nfs_wait_on_write_congestion(mapping, 0);
324 }
Trond Myklebust28c69252006-09-16 13:04:50 -0400325 err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (err < 0)
327 goto out;
Chuck Lever91d5b472006-03-20 13:44:14 -0500328 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
Trond Myklebustd30c8342006-12-13 15:23:47 -0500329 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330out:
331 clear_bit(BDI_write_congested, &bdi->state);
332 wake_up_all(&nfs_write_congestion);
Andrew Morton3fcfab12006-10-19 23:28:16 -0700333 congestion_end(WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return err;
335}
336
337/*
338 * Insert a write request into an inode
339 */
340static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
341{
342 struct nfs_inode *nfsi = NFS_I(inode);
343 int error;
344
345 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
346 BUG_ON(error == -EEXIST);
347 if (error)
348 return error;
349 if (!nfsi->npages) {
350 igrab(inode);
351 nfs_begin_data_update(inode);
352 if (nfs_have_delegation(inode, FMODE_WRITE))
353 nfsi->change_attr++;
354 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500355 SetPagePrivate(req->wb_page);
Trond Myklebust277459d2006-12-05 00:35:35 -0500356 set_page_private(req->wb_page, (unsigned long)req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 nfsi->npages++;
358 atomic_inc(&req->wb_count);
359 return 0;
360}
361
362/*
363 * Insert a write request into an inode
364 */
365static void nfs_inode_remove_request(struct nfs_page *req)
366{
367 struct inode *inode = req->wb_context->dentry->d_inode;
368 struct nfs_inode *nfsi = NFS_I(inode);
369
370 BUG_ON (!NFS_WBACK_BUSY(req));
371
372 spin_lock(&nfsi->req_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500373 set_page_private(req->wb_page, 0);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500374 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
376 nfsi->npages--;
377 if (!nfsi->npages) {
378 spin_unlock(&nfsi->req_lock);
Trond Myklebust951a1432005-06-22 17:16:30 +0000379 nfs_end_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 iput(inode);
381 } else
382 spin_unlock(&nfsi->req_lock);
383 nfs_clear_request(req);
384 nfs_release_request(req);
385}
386
387/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 * Add a request to the inode's dirty list.
389 */
390static void
391nfs_mark_request_dirty(struct nfs_page *req)
392{
393 struct inode *inode = req->wb_context->dentry->d_inode;
394 struct nfs_inode *nfsi = NFS_I(inode);
395
396 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000397 radix_tree_tag_set(&nfsi->nfs_page_tree,
398 req->wb_index, NFS_PAGE_TAG_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 nfs_list_add_request(req, &nfsi->dirty);
400 nfsi->ndirty++;
401 spin_unlock(&nfsi->req_lock);
Trond Myklebusta1803042006-12-05 00:45:19 -0500402 __mark_inode_dirty(inode, I_DIRTY_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Trond Myklebust61822ab2006-12-05 00:35:42 -0500405static void
406nfs_redirty_request(struct nfs_page *req)
407{
408 clear_bit(PG_FLUSHING, &req->wb_flags);
409 __set_page_dirty_nobuffers(req->wb_page);
410}
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412/*
413 * Check if a request is dirty
414 */
415static inline int
416nfs_dirty_request(struct nfs_page *req)
417{
Trond Myklebuste261f512006-12-05 00:35:41 -0500418 return test_bit(PG_FLUSHING, &req->wb_flags) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
421#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
422/*
423 * Add a request to the inode's commit list.
424 */
425static void
426nfs_mark_request_commit(struct nfs_page *req)
427{
428 struct inode *inode = req->wb_context->dentry->d_inode;
429 struct nfs_inode *nfsi = NFS_I(inode);
430
431 spin_lock(&nfsi->req_lock);
432 nfs_list_add_request(req, &nfsi->commit);
433 nfsi->ncommit++;
434 spin_unlock(&nfsi->req_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700435 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebusta1803042006-12-05 00:45:19 -0500436 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438#endif
439
440/*
441 * Wait for a request to complete.
442 *
443 * Interruptible by signals only if mounted with intr flag.
444 */
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500445static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 struct nfs_inode *nfsi = NFS_I(inode);
448 struct nfs_page *req;
449 unsigned long idx_end, next;
450 unsigned int res = 0;
451 int error;
452
453 if (npages == 0)
454 idx_end = ~0;
455 else
456 idx_end = idx_start + npages - 1;
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 next = idx_start;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000459 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 -0700460 if (req->wb_index > idx_end)
461 break;
462
463 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000464 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 atomic_inc(&req->wb_count);
467 spin_unlock(&nfsi->req_lock);
468 error = nfs_wait_on_request(req);
469 nfs_release_request(req);
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500470 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (error < 0)
472 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 res++;
474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return res;
476}
477
Trond Myklebust83715ad2006-07-05 13:17:12 -0400478static void nfs_cancel_dirty_list(struct list_head *head)
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400479{
480 struct nfs_page *req;
481 while(!list_empty(head)) {
482 req = nfs_list_entry(head->next);
483 nfs_list_remove_request(req);
484 nfs_inode_remove_request(req);
485 nfs_clear_page_writeback(req);
486 }
487}
488
Trond Myklebust83715ad2006-07-05 13:17:12 -0400489static void nfs_cancel_commit_list(struct list_head *head)
490{
491 struct nfs_page *req;
492
493 while(!list_empty(head)) {
494 req = nfs_list_entry(head->next);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700495 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400496 nfs_list_remove_request(req);
497 nfs_inode_remove_request(req);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700498 nfs_unlock_request(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400499 }
500}
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
503/*
504 * nfs_scan_commit - Scan an inode for commit requests
505 * @inode: NFS inode to scan
506 * @dst: destination list
507 * @idx_start: lower bound of page->index to scan.
508 * @npages: idx_start + npages sets the upper bound to scan.
509 *
510 * Moves requests from the inode's 'commit' request list.
511 * The requests are *not* checked to ensure that they form a contiguous set.
512 */
513static int
514nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
515{
516 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000517 int res = 0;
518
519 if (nfsi->ncommit != 0) {
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400520 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000521 nfsi->ncommit -= res;
522 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
523 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return res;
526}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500527#else
528static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
529{
530 return 0;
531}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532#endif
533
534static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
535{
536 struct backing_dev_info *bdi = mapping->backing_dev_info;
537 DEFINE_WAIT(wait);
538 int ret = 0;
539
540 might_sleep();
541
542 if (!bdi_write_congested(bdi))
543 return 0;
Chuck Lever91d5b472006-03-20 13:44:14 -0500544
545 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (intr) {
548 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
549 sigset_t oldset;
550
551 rpc_clnt_sigmask(clnt, &oldset);
552 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
553 if (bdi_write_congested(bdi)) {
554 if (signalled())
555 ret = -ERESTARTSYS;
556 else
557 schedule();
558 }
559 rpc_clnt_sigunmask(clnt, &oldset);
560 } else {
561 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
562 if (bdi_write_congested(bdi))
563 schedule();
564 }
565 finish_wait(&nfs_write_congestion, &wait);
566 return ret;
567}
568
569
570/*
571 * Try to update any existing write request, or create one if there is none.
572 * In order to match, the request's credentials must match those of
573 * the calling process.
574 *
575 * Note: Should always be called with the Page Lock held!
576 */
577static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
Trond Myklebuste21195a2006-12-05 00:35:39 -0500578 struct page *page, unsigned int offset, unsigned int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Trond Myklebuste21195a2006-12-05 00:35:39 -0500580 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 struct nfs_inode *nfsi = NFS_I(inode);
582 struct nfs_page *req, *new = NULL;
583 unsigned long rqend, end;
584
585 end = offset + bytes;
586
Trond Myklebuste21195a2006-12-05 00:35:39 -0500587 if (nfs_wait_on_write_congestion(page->mapping, NFS_SERVER(inode)->flags & NFS_MOUNT_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return ERR_PTR(-ERESTARTSYS);
589 for (;;) {
590 /* Loop over all inode entries and see if we find
591 * A request for the page we wish to update
592 */
593 spin_lock(&nfsi->req_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500594 req = nfs_page_find_request_locked(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (req) {
596 if (!nfs_lock_request_dontget(req)) {
597 int error;
Trond Myklebust277459d2006-12-05 00:35:35 -0500598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 spin_unlock(&nfsi->req_lock);
600 error = nfs_wait_on_request(req);
601 nfs_release_request(req);
Neil Brown1dd594b2006-03-20 13:44:04 -0500602 if (error < 0) {
603 if (new)
604 nfs_release_request(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return ERR_PTR(error);
Neil Brown1dd594b2006-03-20 13:44:04 -0500606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 continue;
608 }
609 spin_unlock(&nfsi->req_lock);
610 if (new)
611 nfs_release_request(new);
612 break;
613 }
614
615 if (new) {
616 int error;
617 nfs_lock_request_dontget(new);
618 error = nfs_inode_add_request(inode, new);
619 if (error) {
620 spin_unlock(&nfsi->req_lock);
621 nfs_unlock_request(new);
622 return ERR_PTR(error);
623 }
624 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return new;
626 }
627 spin_unlock(&nfsi->req_lock);
628
629 new = nfs_create_request(ctx, inode, page, offset, bytes);
630 if (IS_ERR(new))
631 return new;
632 }
633
634 /* We have a request for our page.
635 * If the creds don't match, or the
636 * page addresses don't match,
637 * tell the caller to wait on the conflicting
638 * request.
639 */
640 rqend = req->wb_offset + req->wb_bytes;
641 if (req->wb_context != ctx
642 || req->wb_page != page
643 || !nfs_dirty_request(req)
644 || offset > rqend || end < req->wb_offset) {
645 nfs_unlock_request(req);
646 return ERR_PTR(-EBUSY);
647 }
648
649 /* Okay, the request matches. Update the region */
650 if (offset < req->wb_offset) {
651 req->wb_offset = offset;
652 req->wb_pgbase = offset;
653 req->wb_bytes = rqend - req->wb_offset;
654 }
655
656 if (end > rqend)
657 req->wb_bytes = end - req->wb_offset;
658
659 return req;
660}
661
662int nfs_flush_incompatible(struct file *file, struct page *page)
663{
664 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 struct nfs_page *req;
Trond Myklebust1a545332006-12-05 00:35:40 -0500666 int do_flush, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /*
668 * Look for a request corresponding to this page. If there
669 * is one, and it belongs to another file, we flush it out
670 * before we try to copy anything into the page. Do this
671 * due to the lack of an ACCESS-type call in NFSv2.
672 * Also do the same if we find a request from an existing
673 * dropped page.
674 */
Trond Myklebust1a545332006-12-05 00:35:40 -0500675 do {
676 req = nfs_page_find_request(page);
677 if (req == NULL)
678 return 0;
679 do_flush = req->wb_page != page || req->wb_context != ctx
Trond Myklebuste261f512006-12-05 00:35:41 -0500680 || !nfs_dirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 nfs_release_request(req);
Trond Myklebust1a545332006-12-05 00:35:40 -0500682 if (!do_flush)
683 return 0;
684 status = nfs_wb_page(page->mapping->host, page);
685 } while (status == 0);
686 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
689/*
690 * Update and possibly write a cached page of an NFS file.
691 *
692 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
693 * things with a page scheduled for an RPC call (e.g. invalidate it).
694 */
695int nfs_updatepage(struct file *file, struct page *page,
696 unsigned int offset, unsigned int count)
697{
698 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 int status = 0;
701
Chuck Lever91d5b472006-03-20 13:44:14 -0500702 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800705 file->f_path.dentry->d_parent->d_name.name,
706 file->f_path.dentry->d_name.name, count,
Chuck Lever0bbacc42005-11-01 16:53:32 -0500707 (long long)(page_offset(page) +offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 /* If we're not using byte range locks, and we know the page
710 * is entirely in cache, it may be more efficient to avoid
711 * fragmenting write requests.
712 */
Trond Myklebustab0a3db2005-06-22 17:16:30 +0000713 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
Trond Myklebust49a70f22006-12-05 00:35:38 -0500714 count = max(count + offset, nfs_page_length(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717
Trond Myklebuste21195a2006-12-05 00:35:39 -0500718 status = nfs_writepage_setup(ctx, page, offset, count);
Trond Myklebuste261f512006-12-05 00:35:41 -0500719 __set_page_dirty_nobuffers(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
722 status, (long long)i_size_read(inode));
723 if (status < 0)
Trond Myklebusta301b772007-02-06 11:07:15 -0800724 nfs_set_pageerror(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return status;
726}
727
728static void nfs_writepage_release(struct nfs_page *req)
729{
730 end_page_writeback(req->wb_page);
731
732#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
733 if (!PageError(req->wb_page)) {
734 if (NFS_NEED_RESCHED(req)) {
Trond Myklebust61822ab2006-12-05 00:35:42 -0500735 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 goto out;
737 } else if (NFS_NEED_COMMIT(req)) {
738 nfs_mark_request_commit(req);
739 goto out;
740 }
741 }
742 nfs_inode_remove_request(req);
743
744out:
745 nfs_clear_commit(req);
746 nfs_clear_reschedule(req);
747#else
748 nfs_inode_remove_request(req);
749#endif
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000750 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
753static inline int flush_task_priority(int how)
754{
755 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
756 case FLUSH_HIGHPRI:
757 return RPC_PRIORITY_HIGH;
758 case FLUSH_LOWPRI:
759 return RPC_PRIORITY_LOW;
760 }
761 return RPC_PRIORITY_NORMAL;
762}
763
764/*
765 * Set up the argument/result storage required for the RPC call.
766 */
767static void nfs_write_rpcsetup(struct nfs_page *req,
768 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500769 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 unsigned int count, unsigned int offset,
771 int how)
772{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -0500774 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 /* Set up the RPC argument and reply structs
777 * NB: take care not to mess about with data->commit et al. */
778
779 data->req = req;
780 data->inode = inode = req->wb_context->dentry->d_inode;
781 data->cred = req->wb_context->cred;
782
783 data->args.fh = NFS_FH(inode);
784 data->args.offset = req_offset(req) + offset;
785 data->args.pgbase = req->wb_pgbase + offset;
786 data->args.pages = data->pagevec;
787 data->args.count = count;
788 data->args.context = req->wb_context;
789
790 data->res.fattr = &data->fattr;
791 data->res.count = count;
792 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400793 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Trond Myklebust788e7a82006-03-20 13:44:27 -0500795 /* Set up the initial task struct. */
796 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
797 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 NFS_PROTO(inode)->write_setup(data, how);
799
800 data->task.tk_priority = flush_task_priority(how);
801 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Chuck Levera3f565b2007-01-31 12:14:01 -0500803 dprintk("NFS: %5u initiated write call "
804 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500805 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 inode->i_sb->s_id,
807 (long long)NFS_FILEID(inode),
808 count,
809 (unsigned long long)data->args.offset);
810}
811
812static void nfs_execute_write(struct nfs_write_data *data)
813{
814 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
815 sigset_t oldset;
816
817 rpc_clnt_sigmask(clnt, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 rpc_execute(&data->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 rpc_clnt_sigunmask(clnt, &oldset);
820}
821
822/*
823 * Generate multiple small requests to write out a single
824 * contiguous dirty area on one page.
825 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500826static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 struct nfs_page *req = nfs_list_entry(head->next);
829 struct page *page = req->wb_page;
830 struct nfs_write_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700831 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
832 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 int requests = 0;
834 LIST_HEAD(list);
835
836 nfs_list_remove_request(req);
837
838 nbytes = req->wb_bytes;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700839 do {
840 size_t len = min(nbytes, wsize);
841
842 data = nfs_writedata_alloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (!data)
844 goto out_bad;
845 list_add(&data->pages, &list);
846 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700847 nbytes -= len;
848 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 atomic_set(&req->wb_complete, requests);
850
851 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 offset = 0;
853 nbytes = req->wb_bytes;
854 do {
855 data = list_entry(list.next, struct nfs_write_data, pages);
856 list_del_init(&data->pages);
857
858 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 if (nbytes > wsize) {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500861 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
862 wsize, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 offset += wsize;
864 nbytes -= wsize;
865 } else {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500866 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
867 nbytes, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 nbytes = 0;
869 }
870 nfs_execute_write(data);
871 } while (nbytes != 0);
872
873 return 0;
874
875out_bad:
876 while (!list_empty(&list)) {
877 data = list_entry(list.next, struct nfs_write_data, pages);
878 list_del(&data->pages);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500879 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
Trond Myklebust61822ab2006-12-05 00:35:42 -0500881 nfs_redirty_request(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000882 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return -ENOMEM;
884}
885
886/*
887 * Create an RPC task for the given write request and kick it.
888 * The page must have been locked by the caller.
889 *
890 * It may happen that the page we're passed is not marked dirty.
891 * This is the case if nfs_updatepage detects a conflicting request
892 * that has been written but not committed.
893 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500894static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896 struct nfs_page *req;
897 struct page **pages;
898 struct nfs_write_data *data;
899 unsigned int count;
900
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700901 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if (!data)
903 goto out_bad;
904
905 pages = data->pagevec;
906 count = 0;
907 while (!list_empty(head)) {
908 req = nfs_list_entry(head->next);
909 nfs_list_remove_request(req);
910 nfs_list_add_request(req, &data->pages);
911 ClearPageError(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 *pages++ = req->wb_page;
913 count += req->wb_bytes;
914 }
915 req = nfs_list_entry(data->pages.next);
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 /* Set up the argument struct */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500918 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 nfs_execute_write(data);
921 return 0;
922 out_bad:
923 while (!list_empty(head)) {
924 struct nfs_page *req = nfs_list_entry(head->next);
925 nfs_list_remove_request(req);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500926 nfs_redirty_request(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000927 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929 return -ENOMEM;
930}
931
Trond Myklebust7d46a492006-03-20 13:44:50 -0500932static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
934 LIST_HEAD(one_request);
Trond Myklebust7d46a492006-03-20 13:44:50 -0500935 int (*flush_one)(struct inode *, struct list_head *, int);
936 struct nfs_page *req;
937 int wpages = NFS_SERVER(inode)->wpages;
938 int wsize = NFS_SERVER(inode)->wsize;
939 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Trond Myklebust7d46a492006-03-20 13:44:50 -0500941 flush_one = nfs_flush_one;
942 if (wsize < PAGE_CACHE_SIZE)
943 flush_one = nfs_flush_multi;
944 /* For single writes, FLUSH_STABLE is more efficient */
945 if (npages <= wpages && npages == NFS_I(inode)->npages
946 && nfs_list_entry(head->next)->wb_bytes <= wsize)
947 how |= FLUSH_STABLE;
948
949 do {
950 nfs_coalesce_requests(head, &one_request, wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 req = nfs_list_entry(one_request.next);
Trond Myklebust7d46a492006-03-20 13:44:50 -0500952 error = flush_one(inode, &one_request, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (error < 0)
Trond Myklebust7d46a492006-03-20 13:44:50 -0500954 goto out_err;
955 } while (!list_empty(head));
956 return 0;
957out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 while (!list_empty(head)) {
959 req = nfs_list_entry(head->next);
960 nfs_list_remove_request(req);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500961 nfs_redirty_request(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000962 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
964 return error;
965}
966
967/*
968 * Handle a write reply that flushed part of a page.
969 */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500970static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Trond Myklebust788e7a82006-03-20 13:44:27 -0500972 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 struct nfs_page *req = data->req;
974 struct page *page = req->wb_page;
975
976 dprintk("NFS: write (%s/%Ld %d@%Ld)",
977 req->wb_context->dentry->d_inode->i_sb->s_id,
978 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
979 req->wb_bytes,
980 (long long)req_offset(req));
981
Trond Myklebust788e7a82006-03-20 13:44:27 -0500982 if (nfs_writeback_done(task, data) != 0)
983 return;
984
985 if (task->tk_status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -0800986 nfs_set_pageerror(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500987 req->wb_context->error = task->tk_status;
988 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 } else {
990#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
991 if (data->verf.committed < NFS_FILE_SYNC) {
992 if (!NFS_NEED_COMMIT(req)) {
993 nfs_defer_commit(req);
994 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
995 dprintk(" defer commit\n");
996 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
997 nfs_defer_reschedule(req);
998 dprintk(" server reboot detected\n");
999 }
1000 } else
1001#endif
1002 dprintk(" OK\n");
1003 }
1004
1005 if (atomic_dec_and_test(&req->wb_complete))
1006 nfs_writepage_release(req);
1007}
1008
Trond Myklebust788e7a82006-03-20 13:44:27 -05001009static const struct rpc_call_ops nfs_write_partial_ops = {
1010 .rpc_call_done = nfs_writeback_done_partial,
1011 .rpc_release = nfs_writedata_release,
1012};
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014/*
1015 * Handle a write reply that flushes a whole page.
1016 *
1017 * FIXME: There is an inherent race with invalidate_inode_pages and
1018 * writebacks since the page->count is kept > 1 for as long
1019 * as the page has a write request pending.
1020 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001021static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001023 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 struct nfs_page *req;
1025 struct page *page;
1026
Trond Myklebust788e7a82006-03-20 13:44:27 -05001027 if (nfs_writeback_done(task, data) != 0)
1028 return;
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 /* Update attributes as result of writeback. */
1031 while (!list_empty(&data->pages)) {
1032 req = nfs_list_entry(data->pages.next);
1033 nfs_list_remove_request(req);
1034 page = req->wb_page;
1035
1036 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1037 req->wb_context->dentry->d_inode->i_sb->s_id,
1038 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1039 req->wb_bytes,
1040 (long long)req_offset(req));
1041
Trond Myklebust788e7a82006-03-20 13:44:27 -05001042 if (task->tk_status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -08001043 nfs_set_pageerror(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001044 req->wb_context->error = task->tk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 end_page_writeback(page);
1046 nfs_inode_remove_request(req);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001047 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 goto next;
1049 }
1050 end_page_writeback(page);
1051
1052#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1053 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1054 nfs_inode_remove_request(req);
1055 dprintk(" OK\n");
1056 goto next;
1057 }
1058 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1059 nfs_mark_request_commit(req);
1060 dprintk(" marked for commit\n");
1061#else
1062 nfs_inode_remove_request(req);
1063#endif
1064 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001065 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067}
1068
Trond Myklebust788e7a82006-03-20 13:44:27 -05001069static const struct rpc_call_ops nfs_write_full_ops = {
1070 .rpc_call_done = nfs_writeback_done_full,
1071 .rpc_release = nfs_writedata_release,
1072};
1073
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075/*
1076 * This function is called when the WRITE call is complete.
1077 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001078int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 struct nfs_writeargs *argp = &data->args;
1081 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001082 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Chuck Levera3f565b2007-01-31 12:14:01 -05001084 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 task->tk_pid, task->tk_status);
1086
Chuck Leverf551e442006-09-20 14:33:04 -04001087 /*
1088 * ->write_done will attempt to use post-op attributes to detect
1089 * conflicting writes by other clients. A strict interpretation
1090 * of close-to-open would allow us to continue caching even if
1091 * another writer had changed the file, but some applications
1092 * depend on tighter cache coherency when writing.
1093 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001094 status = NFS_PROTO(data->inode)->write_done(task, data);
1095 if (status != 0)
1096 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001097 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1100 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1101 /* We tried a write call, but the server did not
1102 * commit data to stable storage even though we
1103 * requested it.
1104 * Note: There is a known bug in Tru64 < 5.0 in which
1105 * the server reports NFS_DATA_SYNC, but performs
1106 * NFS_FILE_SYNC. We therefore implement this checking
1107 * as a dprintk() in order to avoid filling syslog.
1108 */
1109 static unsigned long complain;
1110
1111 if (time_before(complain, jiffies)) {
1112 dprintk("NFS: faulty NFS server %s:"
1113 " (committed = %d) != (stable = %d)\n",
David Howells54ceac42006-08-22 20:06:13 -04001114 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 resp->verf->committed, argp->stable);
1116 complain = jiffies + 300 * HZ;
1117 }
1118 }
1119#endif
1120 /* Is this a short write? */
1121 if (task->tk_status >= 0 && resp->count < argp->count) {
1122 static unsigned long complain;
1123
Chuck Lever91d5b472006-03-20 13:44:14 -05001124 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 /* Has the server at least made some progress? */
1127 if (resp->count != 0) {
1128 /* Was this an NFSv2 write or an NFSv3 stable write? */
1129 if (resp->verf->committed != NFS_UNSTABLE) {
1130 /* Resend from where the server left off */
1131 argp->offset += resp->count;
1132 argp->pgbase += resp->count;
1133 argp->count -= resp->count;
1134 } else {
1135 /* Resend as a stable write in order to avoid
1136 * headaches in the case of a server crash.
1137 */
1138 argp->stable = NFS_FILE_SYNC;
1139 }
1140 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001141 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143 if (time_before(complain, jiffies)) {
1144 printk(KERN_WARNING
1145 "NFS: Server wrote zero bytes, expected %u.\n",
1146 argp->count);
1147 complain = jiffies + 300 * HZ;
1148 }
1149 /* Can't do anything about it except throw an error. */
1150 task->tk_status = -EIO;
1151 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001152 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
1155
1156#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001157void nfs_commit_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 nfs_commit_free(wdata);
1160}
1161
1162/*
1163 * Set up the argument/result storage required for the RPC call.
1164 */
1165static void nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001166 struct nfs_write_data *data,
1167 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001169 struct nfs_page *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001171 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 /* Set up the RPC argument and reply structs
1174 * NB: take care not to mess about with data->commit et al. */
1175
1176 list_splice_init(head, &data->pages);
1177 first = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 inode = first->wb_context->dentry->d_inode;
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 data->inode = inode;
1181 data->cred = first->wb_context->cred;
1182
1183 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001184 /* Note: we always request a commit of the entire inode */
1185 data->args.offset = 0;
1186 data->args.count = 0;
1187 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 data->res.fattr = &data->fattr;
1189 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001190 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001191
1192 /* Set up the initial task struct. */
1193 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1194 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 NFS_PROTO(inode)->commit_setup(data, how);
1196
1197 data->task.tk_priority = flush_task_priority(how);
1198 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Chuck Levera3f565b2007-01-31 12:14:01 -05001200 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
1203/*
1204 * Commit dirty pages
1205 */
1206static int
Chuck Lever40859d72005-11-30 18:09:02 -05001207nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
1209 struct nfs_write_data *data;
1210 struct nfs_page *req;
1211
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001212 data = nfs_commit_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 if (!data)
1215 goto out_bad;
1216
1217 /* Set up the argument struct */
1218 nfs_commit_rpcsetup(head, data, how);
1219
1220 nfs_execute_write(data);
1221 return 0;
1222 out_bad:
1223 while (!list_empty(head)) {
1224 req = nfs_list_entry(head->next);
1225 nfs_list_remove_request(req);
1226 nfs_mark_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001227 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust5c2d97c2006-09-18 23:20:35 -04001228 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
1230 return -ENOMEM;
1231}
1232
1233/*
1234 * COMMIT call returned
1235 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001236static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001238 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Chuck Levera3f565b2007-01-31 12:14:01 -05001241 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 task->tk_pid, task->tk_status);
1243
Trond Myklebust788e7a82006-03-20 13:44:27 -05001244 /* Call the NFS version-specific code */
1245 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1246 return;
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 while (!list_empty(&data->pages)) {
1249 req = nfs_list_entry(data->pages.next);
1250 nfs_list_remove_request(req);
Christoph Lameterfd39fc82006-06-30 01:55:40 -07001251 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1254 req->wb_context->dentry->d_inode->i_sb->s_id,
1255 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1256 req->wb_bytes,
1257 (long long)req_offset(req));
1258 if (task->tk_status < 0) {
1259 req->wb_context->error = task->tk_status;
1260 nfs_inode_remove_request(req);
1261 dprintk(", error = %d\n", task->tk_status);
1262 goto next;
1263 }
1264
1265 /* Okay, COMMIT succeeded, apparently. Check the verifier
1266 * returned by the server against all stored verfs. */
1267 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1268 /* We have a match */
1269 nfs_inode_remove_request(req);
1270 dprintk(" OK\n");
1271 goto next;
1272 }
1273 /* We have a mismatch. Write the page again */
1274 dprintk(" mismatch\n");
Trond Myklebust61822ab2006-12-05 00:35:42 -05001275 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001277 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001280
1281static const struct rpc_call_ops nfs_commit_ops = {
1282 .rpc_call_done = nfs_commit_done,
1283 .rpc_release = nfs_commit_release,
1284};
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001285#else
1286static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1287{
1288 return 0;
1289}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290#endif
1291
Trond Myklebust3f442542006-09-17 14:46:44 -04001292static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
Trond Myklebust28c69252006-09-16 13:04:50 -04001294 struct nfs_inode *nfsi = NFS_I(mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 LIST_HEAD(head);
Trond Myklebust3f442542006-09-17 14:46:44 -04001296 long res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 spin_lock(&nfsi->req_lock);
Trond Myklebust3f442542006-09-17 14:46:44 -04001299 res = nfs_scan_dirty(mapping, wbc, &head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 spin_unlock(&nfsi->req_lock);
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001301 if (res) {
Trond Myklebust28c69252006-09-16 13:04:50 -04001302 int error = nfs_flush_list(mapping->host, &head, res, how);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001303 if (error < 0)
1304 return error;
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return res;
1307}
1308
1309#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001310int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 struct nfs_inode *nfsi = NFS_I(inode);
1313 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001314 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001317 res = nfs_scan_commit(inode, &head, 0, 0);
1318 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001320 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001321 if (error < 0)
1322 return error;
1323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return res;
1325}
1326#endif
1327
Trond Myklebust1c759502006-10-09 16:18:38 -04001328long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Trond Myklebust1c759502006-10-09 16:18:38 -04001330 struct inode *inode = mapping->host;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001331 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c759502006-10-09 16:18:38 -04001332 unsigned long idx_start, idx_end;
1333 unsigned int npages = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001334 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001335 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebust3f442542006-09-17 14:46:44 -04001336 long pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Trond Myklebust1c759502006-10-09 16:18:38 -04001338 /* FIXME */
1339 if (wbc->range_cyclic)
1340 idx_start = 0;
1341 else {
1342 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1343 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1344 if (idx_end > idx_start) {
1345 unsigned long l_npages = 1 + idx_end - idx_start;
1346 npages = l_npages;
1347 if (sizeof(npages) != sizeof(l_npages) &&
1348 (unsigned long)npages != l_npages)
1349 npages = 0;
1350 }
1351 }
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001352 how &= ~FLUSH_NOCOMMIT;
1353 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 do {
Trond Myklebust1c759502006-10-09 16:18:38 -04001355 wbc->pages_skipped = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001356 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1357 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001358 continue;
Trond Myklebust1c759502006-10-09 16:18:38 -04001359 pages = nfs_scan_dirty(mapping, wbc, &head);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001360 if (pages != 0) {
1361 spin_unlock(&nfsi->req_lock);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001362 if (how & FLUSH_INVALIDATE) {
Trond Myklebust83715ad2006-07-05 13:17:12 -04001363 nfs_cancel_dirty_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001364 ret = pages;
1365 } else
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001366 ret = nfs_flush_list(inode, &head, pages, how);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001367 spin_lock(&nfsi->req_lock);
1368 continue;
1369 }
Trond Myklebust1c759502006-10-09 16:18:38 -04001370 if (wbc->pages_skipped != 0)
1371 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001372 if (nocommit)
1373 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001374 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebust1c759502006-10-09 16:18:38 -04001375 if (pages == 0) {
1376 if (wbc->pages_skipped != 0)
1377 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001378 break;
Trond Myklebust1c759502006-10-09 16:18:38 -04001379 }
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001380 if (how & FLUSH_INVALIDATE) {
1381 spin_unlock(&nfsi->req_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001382 nfs_cancel_commit_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001383 ret = pages;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001384 spin_lock(&nfsi->req_lock);
1385 continue;
1386 }
1387 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001388 spin_unlock(&nfsi->req_lock);
1389 ret = nfs_commit_list(inode, &head, how);
1390 spin_lock(&nfsi->req_lock);
1391 } while (ret >= 0);
1392 spin_unlock(&nfsi->req_lock);
1393 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
1395
Trond Myklebust1c759502006-10-09 16:18:38 -04001396/*
1397 * flush the inode to disk.
1398 */
1399int nfs_wb_all(struct inode *inode)
1400{
1401 struct address_space *mapping = inode->i_mapping;
1402 struct writeback_control wbc = {
1403 .bdi = mapping->backing_dev_info,
1404 .sync_mode = WB_SYNC_ALL,
1405 .nr_to_write = LONG_MAX,
Trond Myklebust61822ab2006-12-05 00:35:42 -05001406 .for_writepages = 1,
Trond Myklebust1c759502006-10-09 16:18:38 -04001407 .range_cyclic = 1,
1408 };
1409 int ret;
1410
Trond Myklebust61822ab2006-12-05 00:35:42 -05001411 ret = generic_writepages(mapping, &wbc);
1412 if (ret < 0)
1413 goto out;
Trond Myklebust1c759502006-10-09 16:18:38 -04001414 ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
1415 if (ret >= 0)
1416 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001417out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001418 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Trond Myklebust1c759502006-10-09 16:18:38 -04001419 return ret;
1420}
1421
1422int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
1423{
1424 struct writeback_control wbc = {
1425 .bdi = mapping->backing_dev_info,
1426 .sync_mode = WB_SYNC_ALL,
1427 .nr_to_write = LONG_MAX,
1428 .range_start = range_start,
1429 .range_end = range_end,
Trond Myklebust61822ab2006-12-05 00:35:42 -05001430 .for_writepages = 1,
Trond Myklebust1c759502006-10-09 16:18:38 -04001431 };
1432 int ret;
1433
Trond Myklebust61822ab2006-12-05 00:35:42 -05001434 if (!(how & FLUSH_NOWRITEPAGE)) {
1435 ret = generic_writepages(mapping, &wbc);
1436 if (ret < 0)
1437 goto out;
1438 }
Trond Myklebust1c759502006-10-09 16:18:38 -04001439 ret = nfs_sync_mapping_wait(mapping, &wbc, how);
1440 if (ret >= 0)
1441 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001442out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001443 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Trond Myklebust1c759502006-10-09 16:18:38 -04001444 return ret;
1445}
1446
Trond Myklebust61822ab2006-12-05 00:35:42 -05001447int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
Trond Myklebust1c759502006-10-09 16:18:38 -04001448{
1449 loff_t range_start = page_offset(page);
1450 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001451 struct writeback_control wbc = {
1452 .bdi = page->mapping->backing_dev_info,
1453 .sync_mode = WB_SYNC_ALL,
1454 .nr_to_write = LONG_MAX,
1455 .range_start = range_start,
1456 .range_end = range_end,
1457 };
1458 int ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001459
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001460 BUG_ON(!PageLocked(page));
1461 if (!(how & FLUSH_NOWRITEPAGE) && clear_page_dirty_for_io(page)) {
1462 ret = nfs_writepage_locked(page, &wbc);
1463 if (ret < 0)
1464 goto out;
1465 }
Trond Myklebustf40313a2007-01-13 02:28:08 -05001466 if (!PagePrivate(page))
1467 return 0;
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001468 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1469 if (ret >= 0)
1470 return 0;
1471out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001472 __mark_inode_dirty(inode, I_DIRTY_PAGES);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001473 return ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001474}
1475
1476/*
1477 * Write back all requests on one page - we do this before reading it.
1478 */
1479int nfs_wb_page(struct inode *inode, struct page* page)
1480{
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001481 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
Trond Myklebust1c759502006-10-09 16:18:38 -04001482}
1483
Trond Myklebust1a545332006-12-05 00:35:40 -05001484int nfs_set_page_dirty(struct page *page)
1485{
1486 struct nfs_page *req;
1487
1488 req = nfs_page_find_request(page);
1489 if (req != NULL) {
1490 /* Mark any existing write requests for flushing */
1491 set_bit(PG_NEED_FLUSH, &req->wb_flags);
1492 nfs_release_request(req);
1493 }
1494 return __set_page_dirty_nobuffers(page);
1495}
1496
Trond Myklebust1c759502006-10-09 16:18:38 -04001497
David Howellsf7b422b2006-06-09 09:34:33 -04001498int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
1500 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1501 sizeof(struct nfs_write_data),
1502 0, SLAB_HWCACHE_ALIGN,
1503 NULL, NULL);
1504 if (nfs_wdata_cachep == NULL)
1505 return -ENOMEM;
1506
Matthew Dobson93d23412006-03-26 01:37:50 -08001507 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1508 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (nfs_wdata_mempool == NULL)
1510 return -ENOMEM;
1511
Matthew Dobson93d23412006-03-26 01:37:50 -08001512 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1513 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (nfs_commit_mempool == NULL)
1515 return -ENOMEM;
1516
1517 return 0;
1518}
1519
David Brownell266bee82006-06-27 12:59:15 -07001520void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 mempool_destroy(nfs_commit_mempool);
1523 mempool_destroy(nfs_wdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001524 kmem_cache_destroy(nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
1526