blob: 6ce2d94e7b3f39f7c30d9dbdfca7c69e261a177e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/write.c
3 *
Trond Myklebust7c85d902006-12-13 15:23:48 -05004 * Write file data over NFS.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/types.h>
10#include <linux/slab.h>
11#include <linux/mm.h>
12#include <linux/pagemap.h>
13#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/writeback.h>
Peter Zijlstra89a09142007-03-16 13:38:26 -080015#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#include <linux/sunrpc/clnt.h>
18#include <linux/nfs_fs.h>
19#include <linux/nfs_mount.h>
20#include <linux/nfs_page.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070021#include <linux/backing-dev.h>
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/uaccess.h>
24#include <linux/smp_lock.h>
25
26#include "delegation.h"
Trond Myklebust49a70f22006-12-05 00:35:38 -050027#include "internal.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050028#include "iostat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#define NFSDBG_FACILITY NFSDBG_PAGECACHE
31
32#define MIN_POOL_WRITE (32)
33#define MIN_POOL_COMMIT (4)
34
35/*
36 * Local function declarations
37 */
38static struct nfs_page * nfs_update_request(struct nfs_open_context*,
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct page *,
40 unsigned int, unsigned int);
Trond Myklebustc63c7b02007-04-02 19:29:52 -040041static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc,
42 struct inode *inode, int ioflags);
Trond Myklebust788e7a82006-03-20 13:44:27 -050043static const struct rpc_call_ops nfs_write_partial_ops;
44static const struct rpc_call_ops nfs_write_full_ops;
45static const struct rpc_call_ops nfs_commit_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Christoph Lametere18b8902006-12-06 20:33:20 -080047static struct kmem_cache *nfs_wdata_cachep;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050048static mempool_t *nfs_wdata_mempool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static mempool_t *nfs_commit_mempool;
50
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070051struct nfs_write_data *nfs_commit_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Christoph Lametere6b4f8d2006-12-06 20:33:14 -080053 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
Chuck Lever40859d72005-11-30 18:09:02 -050054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 if (p) {
56 memset(p, 0, sizeof(*p));
57 INIT_LIST_HEAD(&p->pages);
58 }
59 return p;
60}
61
Trond Myklebust8aca67f2006-11-13 16:23:44 -050062void nfs_commit_rcu_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Trond Myklebust8aca67f2006-11-13 16:23:44 -050064 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
Chuck Lever40859d72005-11-30 18:09:02 -050065 if (p && (p->pagevec != &p->page_array[0]))
66 kfree(p->pagevec);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 mempool_free(p, nfs_commit_mempool);
68}
69
Trond Myklebust8aca67f2006-11-13 16:23:44 -050070void nfs_commit_free(struct nfs_write_data *wdata)
71{
72 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
73}
74
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070075struct nfs_write_data *nfs_writedata_alloc(size_t len)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050076{
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070077 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Christoph Lametere6b4f8d2006-12-06 20:33:14 -080078 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050079
80 if (p) {
81 memset(p, 0, sizeof(*p));
82 INIT_LIST_HEAD(&p->pages);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070083 p->npages = pagecount;
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040084 if (pagecount <= ARRAY_SIZE(p->page_array))
85 p->pagevec = p->page_array;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050086 else {
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040087 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
88 if (!p->pagevec) {
Trond Myklebust3feb2d42006-03-20 13:44:37 -050089 mempool_free(p, nfs_wdata_mempool);
90 p = NULL;
91 }
92 }
93 }
94 return p;
95}
96
Trond Myklebust8aca67f2006-11-13 16:23:44 -050097static void nfs_writedata_rcu_free(struct rcu_head *head)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050098{
Trond Myklebust8aca67f2006-11-13 16:23:44 -050099 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500100 if (p && (p->pagevec != &p->page_array[0]))
101 kfree(p->pagevec);
102 mempool_free(p, nfs_wdata_mempool);
103}
104
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500105static void nfs_writedata_free(struct nfs_write_data *wdata)
106{
107 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
108}
109
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100110void nfs_writedata_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 nfs_writedata_free(wdata);
113}
114
Trond Myklebust277459d2006-12-05 00:35:35 -0500115static struct nfs_page *nfs_page_find_request_locked(struct page *page)
116{
117 struct nfs_page *req = NULL;
118
119 if (PagePrivate(page)) {
120 req = (struct nfs_page *)page_private(page);
121 if (req != NULL)
122 atomic_inc(&req->wb_count);
123 }
124 return req;
125}
126
127static struct nfs_page *nfs_page_find_request(struct page *page)
128{
129 struct nfs_page *req = NULL;
130 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
131
132 spin_lock(req_lock);
133 req = nfs_page_find_request_locked(page);
134 spin_unlock(req_lock);
135 return req;
136}
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/* Adjust the file length if we're writing beyond the end */
139static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
140{
141 struct inode *inode = page->mapping->host;
142 loff_t end, i_size = i_size_read(inode);
143 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
144
145 if (i_size > 0 && page->index < end_index)
146 return;
147 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
148 if (i_size >= end)
149 return;
Chuck Lever91d5b472006-03-20 13:44:14 -0500150 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 i_size_write(inode, end);
152}
153
Trond Myklebusta301b772007-02-06 11:07:15 -0800154/* A writeback failed: mark the page as bad, and invalidate the page cache */
155static void nfs_set_pageerror(struct page *page)
156{
157 SetPageError(page);
158 nfs_zap_mapping(page->mapping->host, page->mapping);
159}
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/* We can set the PG_uptodate flag if we see that a write request
162 * covers the full page.
163 */
164static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
165{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (PageUptodate(page))
167 return;
168 if (base != 0)
169 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500170 if (count != nfs_page_length(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500172 if (count != PAGE_CACHE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
Trond Myklebust49a70f22006-12-05 00:35:38 -0500174 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Trond Myklebuste21195a2006-12-05 00:35:39 -0500177static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 unsigned int offset, unsigned int count)
179{
180 struct nfs_page *req;
Trond Myklebuste21195a2006-12-05 00:35:39 -0500181 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Trond Myklebuste21195a2006-12-05 00:35:39 -0500183 for (;;) {
184 req = nfs_update_request(ctx, page, offset, count);
185 if (!IS_ERR(req))
186 break;
187 ret = PTR_ERR(req);
188 if (ret != -EBUSY)
189 return ret;
190 ret = nfs_wb_page(page->mapping->host, page);
191 if (ret != 0)
192 return ret;
193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 /* Update file length */
195 nfs_grow_file(page, offset, count);
196 /* Set the PG_uptodate flag? */
197 nfs_mark_uptodate(page, offset, count);
198 nfs_unlock_request(req);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100199 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202static int wb_priority(struct writeback_control *wbc)
203{
204 if (wbc->for_reclaim)
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400205 return FLUSH_HIGHPRI | FLUSH_STABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (wbc->for_kupdate)
207 return FLUSH_LOWPRI;
208 return 0;
209}
210
211/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800212 * NFS congestion control
213 */
214
215int nfs_congestion_kb;
216
217#define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
218#define NFS_CONGESTION_OFF_THRESH \
219 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
220
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400221static int nfs_set_page_writeback(struct page *page)
Peter Zijlstra89a09142007-03-16 13:38:26 -0800222{
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400223 int ret = test_set_page_writeback(page);
224
225 if (!ret) {
Peter Zijlstra89a09142007-03-16 13:38:26 -0800226 struct inode *inode = page->mapping->host;
227 struct nfs_server *nfss = NFS_SERVER(inode);
228
229 if (atomic_inc_return(&nfss->writeback) >
230 NFS_CONGESTION_ON_THRESH)
231 set_bdi_congested(&nfss->backing_dev_info, WRITE);
232 }
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400233 return ret;
Peter Zijlstra89a09142007-03-16 13:38:26 -0800234}
235
236static void nfs_end_page_writeback(struct page *page)
237{
238 struct inode *inode = page->mapping->host;
239 struct nfs_server *nfss = NFS_SERVER(inode);
240
241 end_page_writeback(page);
242 if (atomic_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) {
243 clear_bdi_congested(&nfss->backing_dev_info, WRITE);
244 congestion_end(WRITE);
245 }
246}
247
248/*
Trond Myklebuste261f512006-12-05 00:35:41 -0500249 * Find an associated nfs write request, and prepare to flush it out
250 * Returns 1 if there was no write request, or if the request was
251 * already tagged by nfs_set_page_dirty.Returns 0 if the request
252 * was not tagged.
253 * May also return an error if the user signalled nfs_wait_on_request().
254 */
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400255static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
256 struct page *page)
Trond Myklebuste261f512006-12-05 00:35:41 -0500257{
258 struct nfs_page *req;
Trond Myklebust612c9382007-04-20 16:12:45 -0400259 struct nfs_inode *nfsi = NFS_I(page->mapping->host);
260 spinlock_t *req_lock = &nfsi->req_lock;
Trond Myklebuste261f512006-12-05 00:35:41 -0500261 int ret;
262
263 spin_lock(req_lock);
264 for(;;) {
265 req = nfs_page_find_request_locked(page);
266 if (req == NULL) {
267 spin_unlock(req_lock);
268 return 1;
269 }
270 if (nfs_lock_request_dontget(req))
271 break;
272 /* Note: If we hold the page lock, as is the case in nfs_writepage,
273 * then the call to nfs_lock_request_dontget() will always
274 * succeed provided that someone hasn't already marked the
275 * request as dirty (in which case we don't care).
276 */
277 spin_unlock(req_lock);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400278 /* Prevent deadlock! */
279 nfs_pageio_complete(pgio);
Trond Myklebuste261f512006-12-05 00:35:41 -0500280 ret = nfs_wait_on_request(req);
281 nfs_release_request(req);
282 if (ret != 0)
283 return ret;
284 spin_lock(req_lock);
285 }
Trond Myklebust612c9382007-04-20 16:12:45 -0400286 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
287 /* This request is marked for commit */
288 spin_unlock(req_lock);
289 nfs_unlock_request(req);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400290 nfs_pageio_complete(pgio);
Trond Myklebust612c9382007-04-20 16:12:45 -0400291 return 1;
292 }
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400293 if (nfs_set_page_writeback(page) != 0) {
Trond Myklebust612c9382007-04-20 16:12:45 -0400294 spin_unlock(req_lock);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400295 BUG();
296 }
297 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
298 NFS_PAGE_TAG_WRITEBACK);
Trond Myklebuste261f512006-12-05 00:35:41 -0500299 ret = test_bit(PG_NEED_FLUSH, &req->wb_flags);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400300 spin_unlock(req_lock);
301 nfs_pageio_add_request(pgio, req);
Trond Myklebuste261f512006-12-05 00:35:41 -0500302 return ret;
303}
304
305/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 * Write an mmapped page to the server.
307 */
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500308static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400310 struct nfs_pageio_descriptor mypgio, *pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 struct nfs_open_context *ctx;
312 struct inode *inode = page->mapping->host;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500313 unsigned offset;
Trond Myklebuste261f512006-12-05 00:35:41 -0500314 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Chuck Lever91d5b472006-03-20 13:44:14 -0500316 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
317 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
318
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400319 if (wbc->for_writepages)
320 pgio = wbc->fs_private;
321 else {
322 nfs_pageio_init_write(&mypgio, inode, wb_priority(wbc));
323 pgio = &mypgio;
324 }
325
326 err = nfs_page_async_flush(pgio, page);
Trond Myklebuste261f512006-12-05 00:35:41 -0500327 if (err <= 0)
328 goto out;
329 err = 0;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500330 offset = nfs_page_length(page);
331 if (!offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 goto out;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500333
Trond Myklebustd5308382005-11-04 15:33:38 -0500334 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (ctx == NULL) {
336 err = -EBADF;
337 goto out;
338 }
Trond Myklebust200baa22006-12-05 00:35:40 -0500339 err = nfs_writepage_setup(ctx, page, 0, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 put_nfs_open_context(ctx);
Trond Myklebuste261f512006-12-05 00:35:41 -0500341 if (err != 0)
342 goto out;
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400343 err = nfs_page_async_flush(pgio, page);
Trond Myklebuste261f512006-12-05 00:35:41 -0500344 if (err > 0)
345 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346out:
Trond Myklebust200baa22006-12-05 00:35:40 -0500347 if (!wbc->for_writepages)
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400348 nfs_pageio_complete(pgio);
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500349 return err;
350}
351
352int nfs_writepage(struct page *page, struct writeback_control *wbc)
353{
354 int err;
355
356 err = nfs_writepage_locked(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return err;
359}
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
362{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 struct inode *inode = mapping->host;
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400364 struct nfs_pageio_descriptor pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 int err;
366
Chuck Lever91d5b472006-03-20 13:44:14 -0500367 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
368
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400369 nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
370 wbc->fs_private = &pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 err = generic_writepages(mapping, wbc);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400372 nfs_pageio_complete(&pgio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (err)
374 return err;
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400375 if (pgio.pg_error)
376 return pgio.pg_error;
377 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
380/*
381 * Insert a write request into an inode
382 */
383static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
384{
385 struct nfs_inode *nfsi = NFS_I(inode);
386 int error;
387
388 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
389 BUG_ON(error == -EEXIST);
390 if (error)
391 return error;
392 if (!nfsi->npages) {
393 igrab(inode);
394 nfs_begin_data_update(inode);
395 if (nfs_have_delegation(inode, FMODE_WRITE))
396 nfsi->change_attr++;
397 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500398 SetPagePrivate(req->wb_page);
Trond Myklebust277459d2006-12-05 00:35:35 -0500399 set_page_private(req->wb_page, (unsigned long)req);
Trond Myklebust2b82f192007-04-20 16:12:50 -0400400 if (PageDirty(req->wb_page))
401 set_bit(PG_NEED_FLUSH, &req->wb_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 nfsi->npages++;
403 atomic_inc(&req->wb_count);
404 return 0;
405}
406
407/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800408 * Remove a write request from an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 */
410static void nfs_inode_remove_request(struct nfs_page *req)
411{
412 struct inode *inode = req->wb_context->dentry->d_inode;
413 struct nfs_inode *nfsi = NFS_I(inode);
414
415 BUG_ON (!NFS_WBACK_BUSY(req));
416
417 spin_lock(&nfsi->req_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500418 set_page_private(req->wb_page, 0);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500419 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
Trond Myklebust2b82f192007-04-20 16:12:50 -0400421 if (test_and_clear_bit(PG_NEED_FLUSH, &req->wb_flags))
422 __set_page_dirty_nobuffers(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 nfsi->npages--;
424 if (!nfsi->npages) {
425 spin_unlock(&nfsi->req_lock);
Trond Myklebust951a1432005-06-22 17:16:30 +0000426 nfs_end_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 iput(inode);
428 } else
429 spin_unlock(&nfsi->req_lock);
430 nfs_clear_request(req);
431 nfs_release_request(req);
432}
433
Trond Myklebust61822ab2006-12-05 00:35:42 -0500434static void
435nfs_redirty_request(struct nfs_page *req)
436{
Trond Myklebust61822ab2006-12-05 00:35:42 -0500437 __set_page_dirty_nobuffers(req->wb_page);
438}
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440/*
441 * Check if a request is dirty
442 */
443static inline int
444nfs_dirty_request(struct nfs_page *req)
445{
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400446 struct page *page = req->wb_page;
447
Trond Myklebust612c9382007-04-20 16:12:45 -0400448 if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags))
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400449 return 0;
450 return !PageWriteback(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
453#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
454/*
455 * Add a request to the inode's commit list.
456 */
457static void
458nfs_mark_request_commit(struct nfs_page *req)
459{
460 struct inode *inode = req->wb_context->dentry->d_inode;
461 struct nfs_inode *nfsi = NFS_I(inode);
462
463 spin_lock(&nfsi->req_lock);
464 nfs_list_add_request(req, &nfsi->commit);
465 nfsi->ncommit++;
Trond Myklebust612c9382007-04-20 16:12:45 -0400466 set_bit(PG_NEED_COMMIT, &(req)->wb_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 spin_unlock(&nfsi->req_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700468 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebusta1803042006-12-05 00:45:19 -0500469 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400471
472static inline
473int nfs_write_need_commit(struct nfs_write_data *data)
474{
475 return data->verf.committed != NFS_FILE_SYNC;
476}
477
478static inline
479int nfs_reschedule_unstable_write(struct nfs_page *req)
480{
Trond Myklebust612c9382007-04-20 16:12:45 -0400481 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400482 nfs_mark_request_commit(req);
483 return 1;
484 }
485 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
486 nfs_redirty_request(req);
487 return 1;
488 }
489 return 0;
490}
491#else
492static inline void
493nfs_mark_request_commit(struct nfs_page *req)
494{
495}
496
497static inline
498int nfs_write_need_commit(struct nfs_write_data *data)
499{
500 return 0;
501}
502
503static inline
504int nfs_reschedule_unstable_write(struct nfs_page *req)
505{
506 return 0;
507}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508#endif
509
510/*
511 * Wait for a request to complete.
512 *
513 * Interruptible by signals only if mounted with intr flag.
514 */
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500515static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
517 struct nfs_inode *nfsi = NFS_I(inode);
518 struct nfs_page *req;
519 unsigned long idx_end, next;
520 unsigned int res = 0;
521 int error;
522
523 if (npages == 0)
524 idx_end = ~0;
525 else
526 idx_end = idx_start + npages - 1;
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 next = idx_start;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000529 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 -0700530 if (req->wb_index > idx_end)
531 break;
532
533 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000534 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 atomic_inc(&req->wb_count);
537 spin_unlock(&nfsi->req_lock);
538 error = nfs_wait_on_request(req);
539 nfs_release_request(req);
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500540 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (error < 0)
542 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 res++;
544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return res;
546}
547
Trond Myklebust83715ad2006-07-05 13:17:12 -0400548static void nfs_cancel_commit_list(struct list_head *head)
549{
550 struct nfs_page *req;
551
552 while(!list_empty(head)) {
553 req = nfs_list_entry(head->next);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700554 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400555 nfs_list_remove_request(req);
Trond Myklebust612c9382007-04-20 16:12:45 -0400556 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400557 nfs_inode_remove_request(req);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700558 nfs_unlock_request(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400559 }
560}
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
563/*
564 * nfs_scan_commit - Scan an inode for commit requests
565 * @inode: NFS inode to scan
566 * @dst: destination list
567 * @idx_start: lower bound of page->index to scan.
568 * @npages: idx_start + npages sets the upper bound to scan.
569 *
570 * Moves requests from the inode's 'commit' request list.
571 * The requests are *not* checked to ensure that they form a contiguous set.
572 */
573static int
574nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
575{
576 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000577 int res = 0;
578
579 if (nfsi->ncommit != 0) {
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400580 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000581 nfsi->ncommit -= res;
582 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
583 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return res;
586}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500587#else
588static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
589{
590 return 0;
591}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592#endif
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594/*
595 * Try to update any existing write request, or create one if there is none.
596 * In order to match, the request's credentials must match those of
597 * the calling process.
598 *
599 * Note: Should always be called with the Page Lock held!
600 */
601static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
Trond Myklebuste21195a2006-12-05 00:35:39 -0500602 struct page *page, unsigned int offset, unsigned int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Peter Zijlstra89a09142007-03-16 13:38:26 -0800604 struct address_space *mapping = page->mapping;
605 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 struct nfs_inode *nfsi = NFS_I(inode);
607 struct nfs_page *req, *new = NULL;
608 unsigned long rqend, end;
609
610 end = offset + bytes;
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 for (;;) {
613 /* Loop over all inode entries and see if we find
614 * A request for the page we wish to update
615 */
616 spin_lock(&nfsi->req_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500617 req = nfs_page_find_request_locked(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (req) {
619 if (!nfs_lock_request_dontget(req)) {
620 int error;
Trond Myklebust277459d2006-12-05 00:35:35 -0500621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 spin_unlock(&nfsi->req_lock);
623 error = nfs_wait_on_request(req);
624 nfs_release_request(req);
Neil Brown1dd594b2006-03-20 13:44:04 -0500625 if (error < 0) {
626 if (new)
627 nfs_release_request(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return ERR_PTR(error);
Neil Brown1dd594b2006-03-20 13:44:04 -0500629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 continue;
631 }
632 spin_unlock(&nfsi->req_lock);
633 if (new)
634 nfs_release_request(new);
635 break;
636 }
637
638 if (new) {
639 int error;
640 nfs_lock_request_dontget(new);
641 error = nfs_inode_add_request(inode, new);
642 if (error) {
643 spin_unlock(&nfsi->req_lock);
644 nfs_unlock_request(new);
645 return ERR_PTR(error);
646 }
647 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return new;
649 }
650 spin_unlock(&nfsi->req_lock);
651
652 new = nfs_create_request(ctx, inode, page, offset, bytes);
653 if (IS_ERR(new))
654 return new;
655 }
656
657 /* We have a request for our page.
658 * If the creds don't match, or the
659 * page addresses don't match,
660 * tell the caller to wait on the conflicting
661 * request.
662 */
663 rqend = req->wb_offset + req->wb_bytes;
664 if (req->wb_context != ctx
665 || req->wb_page != page
666 || !nfs_dirty_request(req)
667 || offset > rqend || end < req->wb_offset) {
668 nfs_unlock_request(req);
669 return ERR_PTR(-EBUSY);
670 }
671
672 /* Okay, the request matches. Update the region */
673 if (offset < req->wb_offset) {
674 req->wb_offset = offset;
675 req->wb_pgbase = offset;
676 req->wb_bytes = rqend - req->wb_offset;
677 }
678
679 if (end > rqend)
680 req->wb_bytes = end - req->wb_offset;
681
682 return req;
683}
684
685int nfs_flush_incompatible(struct file *file, struct page *page)
686{
687 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 struct nfs_page *req;
Trond Myklebust1a545332006-12-05 00:35:40 -0500689 int do_flush, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /*
691 * Look for a request corresponding to this page. If there
692 * is one, and it belongs to another file, we flush it out
693 * before we try to copy anything into the page. Do this
694 * due to the lack of an ACCESS-type call in NFSv2.
695 * Also do the same if we find a request from an existing
696 * dropped page.
697 */
Trond Myklebust1a545332006-12-05 00:35:40 -0500698 do {
699 req = nfs_page_find_request(page);
700 if (req == NULL)
701 return 0;
702 do_flush = req->wb_page != page || req->wb_context != ctx
Trond Myklebuste261f512006-12-05 00:35:41 -0500703 || !nfs_dirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 nfs_release_request(req);
Trond Myklebust1a545332006-12-05 00:35:40 -0500705 if (!do_flush)
706 return 0;
707 status = nfs_wb_page(page->mapping->host, page);
708 } while (status == 0);
709 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
712/*
713 * Update and possibly write a cached page of an NFS file.
714 *
715 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
716 * things with a page scheduled for an RPC call (e.g. invalidate it).
717 */
718int nfs_updatepage(struct file *file, struct page *page,
719 unsigned int offset, unsigned int count)
720{
721 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 int status = 0;
724
Chuck Lever91d5b472006-03-20 13:44:14 -0500725 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800728 file->f_path.dentry->d_parent->d_name.name,
729 file->f_path.dentry->d_name.name, count,
Chuck Lever0bbacc42005-11-01 16:53:32 -0500730 (long long)(page_offset(page) +offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 /* If we're not using byte range locks, and we know the page
733 * is entirely in cache, it may be more efficient to avoid
734 * fragmenting write requests.
735 */
Trond Myklebustab0a3db2005-06-22 17:16:30 +0000736 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
Trond Myklebust49a70f22006-12-05 00:35:38 -0500737 count = max(count + offset, nfs_page_length(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740
Trond Myklebuste21195a2006-12-05 00:35:39 -0500741 status = nfs_writepage_setup(ctx, page, offset, count);
Trond Myklebuste261f512006-12-05 00:35:41 -0500742 __set_page_dirty_nobuffers(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
745 status, (long long)i_size_read(inode));
746 if (status < 0)
Trond Myklebusta301b772007-02-06 11:07:15 -0800747 nfs_set_pageerror(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return status;
749}
750
751static void nfs_writepage_release(struct nfs_page *req)
752{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400754 if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req)) {
755 nfs_end_page_writeback(req->wb_page);
756 nfs_inode_remove_request(req);
757 } else
758 nfs_end_page_writeback(req->wb_page);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000759 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
762static inline int flush_task_priority(int how)
763{
764 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
765 case FLUSH_HIGHPRI:
766 return RPC_PRIORITY_HIGH;
767 case FLUSH_LOWPRI:
768 return RPC_PRIORITY_LOW;
769 }
770 return RPC_PRIORITY_NORMAL;
771}
772
773/*
774 * Set up the argument/result storage required for the RPC call.
775 */
776static void nfs_write_rpcsetup(struct nfs_page *req,
777 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500778 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 unsigned int count, unsigned int offset,
780 int how)
781{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -0500783 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 /* Set up the RPC argument and reply structs
786 * NB: take care not to mess about with data->commit et al. */
787
788 data->req = req;
789 data->inode = inode = req->wb_context->dentry->d_inode;
790 data->cred = req->wb_context->cred;
791
792 data->args.fh = NFS_FH(inode);
793 data->args.offset = req_offset(req) + offset;
794 data->args.pgbase = req->wb_pgbase + offset;
795 data->args.pages = data->pagevec;
796 data->args.count = count;
797 data->args.context = req->wb_context;
798
799 data->res.fattr = &data->fattr;
800 data->res.count = count;
801 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400802 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Trond Myklebust788e7a82006-03-20 13:44:27 -0500804 /* Set up the initial task struct. */
805 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
806 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 NFS_PROTO(inode)->write_setup(data, how);
808
809 data->task.tk_priority = flush_task_priority(how);
810 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Chuck Levera3f565b2007-01-31 12:14:01 -0500812 dprintk("NFS: %5u initiated write call "
813 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500814 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 inode->i_sb->s_id,
816 (long long)NFS_FILEID(inode),
817 count,
818 (unsigned long long)data->args.offset);
819}
820
821static void nfs_execute_write(struct nfs_write_data *data)
822{
823 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
824 sigset_t oldset;
825
826 rpc_clnt_sigmask(clnt, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 rpc_execute(&data->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 rpc_clnt_sigunmask(clnt, &oldset);
829}
830
831/*
832 * Generate multiple small requests to write out a single
833 * contiguous dirty area on one page.
834 */
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400835static int nfs_flush_multi(struct inode *inode, struct list_head *head, size_t count, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
837 struct nfs_page *req = nfs_list_entry(head->next);
838 struct page *page = req->wb_page;
839 struct nfs_write_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700840 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
841 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 int requests = 0;
843 LIST_HEAD(list);
844
845 nfs_list_remove_request(req);
846
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400847 nbytes = count;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700848 do {
849 size_t len = min(nbytes, wsize);
850
851 data = nfs_writedata_alloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (!data)
853 goto out_bad;
854 list_add(&data->pages, &list);
855 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700856 nbytes -= len;
857 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 atomic_set(&req->wb_complete, requests);
859
860 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 offset = 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400862 nbytes = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 do {
864 data = list_entry(list.next, struct nfs_write_data, pages);
865 list_del_init(&data->pages);
866
867 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400869 if (nbytes < wsize)
870 wsize = nbytes;
871 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
872 wsize, offset, how);
873 offset += wsize;
874 nbytes -= wsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 nfs_execute_write(data);
876 } while (nbytes != 0);
877
878 return 0;
879
880out_bad:
881 while (!list_empty(&list)) {
882 data = list_entry(list.next, struct nfs_write_data, pages);
883 list_del(&data->pages);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500884 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
Trond Myklebust61822ab2006-12-05 00:35:42 -0500886 nfs_redirty_request(req);
Trond Myklebust6d677e32007-04-20 16:12:40 -0400887 nfs_end_page_writeback(req->wb_page);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000888 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return -ENOMEM;
890}
891
892/*
893 * Create an RPC task for the given write request and kick it.
894 * The page must have been locked by the caller.
895 *
896 * It may happen that the page we're passed is not marked dirty.
897 * This is the case if nfs_updatepage detects a conflicting request
898 * that has been written but not committed.
899 */
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400900static int nfs_flush_one(struct inode *inode, struct list_head *head, size_t count, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
902 struct nfs_page *req;
903 struct page **pages;
904 struct nfs_write_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400906 data = nfs_writedata_alloc(count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (!data)
908 goto out_bad;
909
910 pages = data->pagevec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 while (!list_empty(head)) {
912 req = nfs_list_entry(head->next);
913 nfs_list_remove_request(req);
914 nfs_list_add_request(req, &data->pages);
915 ClearPageError(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 *pages++ = req->wb_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918 req = nfs_list_entry(data->pages.next);
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 /* Set up the argument struct */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500921 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 nfs_execute_write(data);
924 return 0;
925 out_bad:
926 while (!list_empty(head)) {
927 struct nfs_page *req = nfs_list_entry(head->next);
928 nfs_list_remove_request(req);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500929 nfs_redirty_request(req);
Trond Myklebust6d677e32007-04-20 16:12:40 -0400930 nfs_end_page_writeback(req->wb_page);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000931 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933 return -ENOMEM;
934}
935
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400936static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
937 struct inode *inode, int ioflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Trond Myklebust7d46a492006-03-20 13:44:50 -0500939 int wsize = NFS_SERVER(inode)->wsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400941 if (wsize < PAGE_CACHE_SIZE)
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400942 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400943 else
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400944 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
947/*
948 * Handle a write reply that flushed part of a page.
949 */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500950static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Trond Myklebust788e7a82006-03-20 13:44:27 -0500952 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 struct nfs_page *req = data->req;
954 struct page *page = req->wb_page;
955
956 dprintk("NFS: write (%s/%Ld %d@%Ld)",
957 req->wb_context->dentry->d_inode->i_sb->s_id,
958 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
959 req->wb_bytes,
960 (long long)req_offset(req));
961
Trond Myklebust788e7a82006-03-20 13:44:27 -0500962 if (nfs_writeback_done(task, data) != 0)
963 return;
964
965 if (task->tk_status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -0800966 nfs_set_pageerror(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500967 req->wb_context->error = task->tk_status;
968 dprintk(", error = %d\n", task->tk_status);
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400969 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400972 if (nfs_write_need_commit(data)) {
973 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
974
975 spin_lock(req_lock);
976 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
977 /* Do nothing we need to resend the writes */
978 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
979 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
980 dprintk(" defer commit\n");
981 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
982 set_bit(PG_NEED_RESCHED, &req->wb_flags);
983 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
984 dprintk(" server reboot detected\n");
985 }
986 spin_unlock(req_lock);
987 } else
988 dprintk(" OK\n");
989
990out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (atomic_dec_and_test(&req->wb_complete))
992 nfs_writepage_release(req);
993}
994
Trond Myklebust788e7a82006-03-20 13:44:27 -0500995static const struct rpc_call_ops nfs_write_partial_ops = {
996 .rpc_call_done = nfs_writeback_done_partial,
997 .rpc_release = nfs_writedata_release,
998};
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000/*
1001 * Handle a write reply that flushes a whole page.
1002 *
1003 * FIXME: There is an inherent race with invalidate_inode_pages and
1004 * writebacks since the page->count is kept > 1 for as long
1005 * as the page has a write request pending.
1006 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001007static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001009 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 struct nfs_page *req;
1011 struct page *page;
1012
Trond Myklebust788e7a82006-03-20 13:44:27 -05001013 if (nfs_writeback_done(task, data) != 0)
1014 return;
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 /* Update attributes as result of writeback. */
1017 while (!list_empty(&data->pages)) {
1018 req = nfs_list_entry(data->pages.next);
1019 nfs_list_remove_request(req);
1020 page = req->wb_page;
1021
1022 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1023 req->wb_context->dentry->d_inode->i_sb->s_id,
1024 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1025 req->wb_bytes,
1026 (long long)req_offset(req));
1027
Trond Myklebust788e7a82006-03-20 13:44:27 -05001028 if (task->tk_status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -08001029 nfs_set_pageerror(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001030 req->wb_context->error = task->tk_status;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001031 dprintk(", error = %d\n", task->tk_status);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001032 goto remove_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001035 if (nfs_write_need_commit(data)) {
1036 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1037 nfs_mark_request_commit(req);
1038 nfs_end_page_writeback(page);
1039 dprintk(" marked for commit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 goto next;
1041 }
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001042 dprintk(" OK\n");
1043remove_request:
1044 nfs_end_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 nfs_inode_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001047 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049}
1050
Trond Myklebust788e7a82006-03-20 13:44:27 -05001051static const struct rpc_call_ops nfs_write_full_ops = {
1052 .rpc_call_done = nfs_writeback_done_full,
1053 .rpc_release = nfs_writedata_release,
1054};
1055
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057/*
1058 * This function is called when the WRITE call is complete.
1059 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001060int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 struct nfs_writeargs *argp = &data->args;
1063 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001064 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Chuck Levera3f565b2007-01-31 12:14:01 -05001066 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 task->tk_pid, task->tk_status);
1068
Chuck Leverf551e442006-09-20 14:33:04 -04001069 /*
1070 * ->write_done will attempt to use post-op attributes to detect
1071 * conflicting writes by other clients. A strict interpretation
1072 * of close-to-open would allow us to continue caching even if
1073 * another writer had changed the file, but some applications
1074 * depend on tighter cache coherency when writing.
1075 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001076 status = NFS_PROTO(data->inode)->write_done(task, data);
1077 if (status != 0)
1078 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001079 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1082 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1083 /* We tried a write call, but the server did not
1084 * commit data to stable storage even though we
1085 * requested it.
1086 * Note: There is a known bug in Tru64 < 5.0 in which
1087 * the server reports NFS_DATA_SYNC, but performs
1088 * NFS_FILE_SYNC. We therefore implement this checking
1089 * as a dprintk() in order to avoid filling syslog.
1090 */
1091 static unsigned long complain;
1092
1093 if (time_before(complain, jiffies)) {
1094 dprintk("NFS: faulty NFS server %s:"
1095 " (committed = %d) != (stable = %d)\n",
David Howells54ceac42006-08-22 20:06:13 -04001096 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 resp->verf->committed, argp->stable);
1098 complain = jiffies + 300 * HZ;
1099 }
1100 }
1101#endif
1102 /* Is this a short write? */
1103 if (task->tk_status >= 0 && resp->count < argp->count) {
1104 static unsigned long complain;
1105
Chuck Lever91d5b472006-03-20 13:44:14 -05001106 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 /* Has the server at least made some progress? */
1109 if (resp->count != 0) {
1110 /* Was this an NFSv2 write or an NFSv3 stable write? */
1111 if (resp->verf->committed != NFS_UNSTABLE) {
1112 /* Resend from where the server left off */
1113 argp->offset += resp->count;
1114 argp->pgbase += resp->count;
1115 argp->count -= resp->count;
1116 } else {
1117 /* Resend as a stable write in order to avoid
1118 * headaches in the case of a server crash.
1119 */
1120 argp->stable = NFS_FILE_SYNC;
1121 }
1122 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001123 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
1125 if (time_before(complain, jiffies)) {
1126 printk(KERN_WARNING
1127 "NFS: Server wrote zero bytes, expected %u.\n",
1128 argp->count);
1129 complain = jiffies + 300 * HZ;
1130 }
1131 /* Can't do anything about it except throw an error. */
1132 task->tk_status = -EIO;
1133 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
1137
1138#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001139void nfs_commit_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 nfs_commit_free(wdata);
1142}
1143
1144/*
1145 * Set up the argument/result storage required for the RPC call.
1146 */
1147static void nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001148 struct nfs_write_data *data,
1149 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001151 struct nfs_page *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001153 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155 /* Set up the RPC argument and reply structs
1156 * NB: take care not to mess about with data->commit et al. */
1157
1158 list_splice_init(head, &data->pages);
1159 first = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 inode = first->wb_context->dentry->d_inode;
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 data->inode = inode;
1163 data->cred = first->wb_context->cred;
1164
1165 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001166 /* Note: we always request a commit of the entire inode */
1167 data->args.offset = 0;
1168 data->args.count = 0;
1169 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 data->res.fattr = &data->fattr;
1171 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001172 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001173
1174 /* Set up the initial task struct. */
1175 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1176 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 NFS_PROTO(inode)->commit_setup(data, how);
1178
1179 data->task.tk_priority = flush_task_priority(how);
1180 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Chuck Levera3f565b2007-01-31 12:14:01 -05001182 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184
1185/*
1186 * Commit dirty pages
1187 */
1188static int
Chuck Lever40859d72005-11-30 18:09:02 -05001189nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 struct nfs_write_data *data;
1192 struct nfs_page *req;
1193
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001194 data = nfs_commit_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 if (!data)
1197 goto out_bad;
1198
1199 /* Set up the argument struct */
1200 nfs_commit_rpcsetup(head, data, how);
1201
1202 nfs_execute_write(data);
1203 return 0;
1204 out_bad:
1205 while (!list_empty(head)) {
1206 req = nfs_list_entry(head->next);
1207 nfs_list_remove_request(req);
1208 nfs_mark_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001209 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust5c2d97c2006-09-18 23:20:35 -04001210 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
1212 return -ENOMEM;
1213}
1214
1215/*
1216 * COMMIT call returned
1217 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001218static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001220 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Chuck Levera3f565b2007-01-31 12:14:01 -05001223 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 task->tk_pid, task->tk_status);
1225
Trond Myklebust788e7a82006-03-20 13:44:27 -05001226 /* Call the NFS version-specific code */
1227 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1228 return;
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 while (!list_empty(&data->pages)) {
1231 req = nfs_list_entry(data->pages.next);
1232 nfs_list_remove_request(req);
Trond Myklebust612c9382007-04-20 16:12:45 -04001233 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
Christoph Lameterfd39fc82006-06-30 01:55:40 -07001234 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1237 req->wb_context->dentry->d_inode->i_sb->s_id,
1238 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1239 req->wb_bytes,
1240 (long long)req_offset(req));
1241 if (task->tk_status < 0) {
1242 req->wb_context->error = task->tk_status;
1243 nfs_inode_remove_request(req);
1244 dprintk(", error = %d\n", task->tk_status);
1245 goto next;
1246 }
1247
1248 /* Okay, COMMIT succeeded, apparently. Check the verifier
1249 * returned by the server against all stored verfs. */
1250 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1251 /* We have a match */
1252 nfs_inode_remove_request(req);
1253 dprintk(" OK\n");
1254 goto next;
1255 }
1256 /* We have a mismatch. Write the page again */
1257 dprintk(" mismatch\n");
Trond Myklebust61822ab2006-12-05 00:35:42 -05001258 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001260 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001263
1264static const struct rpc_call_ops nfs_commit_ops = {
1265 .rpc_call_done = nfs_commit_done,
1266 .rpc_release = nfs_commit_release,
1267};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001269int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
1271 struct nfs_inode *nfsi = NFS_I(inode);
1272 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001273 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001276 res = nfs_scan_commit(inode, &head, 0, 0);
1277 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001279 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001280 if (error < 0)
1281 return error;
1282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return res;
1284}
Trond Myklebustc63c7b02007-04-02 19:29:52 -04001285#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 Myklebust1c759502006-10-09 16:18:38 -04001292long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
Trond Myklebust1c759502006-10-09 16:18:38 -04001294 struct inode *inode = mapping->host;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001295 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c759502006-10-09 16:18:38 -04001296 unsigned long idx_start, idx_end;
1297 unsigned int npages = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001298 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001299 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebust3f442542006-09-17 14:46:44 -04001300 long pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Trond Myklebust1c759502006-10-09 16:18:38 -04001302 /* FIXME */
1303 if (wbc->range_cyclic)
1304 idx_start = 0;
1305 else {
1306 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1307 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1308 if (idx_end > idx_start) {
1309 unsigned long l_npages = 1 + idx_end - idx_start;
1310 npages = l_npages;
1311 if (sizeof(npages) != sizeof(l_npages) &&
1312 (unsigned long)npages != l_npages)
1313 npages = 0;
1314 }
1315 }
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001316 how &= ~FLUSH_NOCOMMIT;
1317 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 do {
Trond Myklebust1c759502006-10-09 16:18:38 -04001319 wbc->pages_skipped = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001320 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1321 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001322 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001323 if (nocommit)
1324 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001325 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebust1c759502006-10-09 16:18:38 -04001326 if (pages == 0) {
1327 if (wbc->pages_skipped != 0)
1328 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001329 break;
Trond Myklebust1c759502006-10-09 16:18:38 -04001330 }
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001331 if (how & FLUSH_INVALIDATE) {
1332 spin_unlock(&nfsi->req_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001333 nfs_cancel_commit_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001334 ret = pages;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001335 spin_lock(&nfsi->req_lock);
1336 continue;
1337 }
1338 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001339 spin_unlock(&nfsi->req_lock);
1340 ret = nfs_commit_list(inode, &head, how);
1341 spin_lock(&nfsi->req_lock);
1342 } while (ret >= 0);
1343 spin_unlock(&nfsi->req_lock);
1344 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
1346
Trond Myklebust1c759502006-10-09 16:18:38 -04001347/*
1348 * flush the inode to disk.
1349 */
1350int nfs_wb_all(struct inode *inode)
1351{
1352 struct address_space *mapping = inode->i_mapping;
1353 struct writeback_control wbc = {
1354 .bdi = mapping->backing_dev_info,
1355 .sync_mode = WB_SYNC_ALL,
1356 .nr_to_write = LONG_MAX,
Trond Myklebust61822ab2006-12-05 00:35:42 -05001357 .for_writepages = 1,
Trond Myklebust1c759502006-10-09 16:18:38 -04001358 .range_cyclic = 1,
1359 };
1360 int ret;
1361
Trond Myklebustc63c7b02007-04-02 19:29:52 -04001362 ret = nfs_writepages(mapping, &wbc);
Trond Myklebust61822ab2006-12-05 00:35:42 -05001363 if (ret < 0)
1364 goto out;
Trond Myklebust1c759502006-10-09 16:18:38 -04001365 ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
1366 if (ret >= 0)
1367 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001368out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001369 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Trond Myklebust1c759502006-10-09 16:18:38 -04001370 return ret;
1371}
1372
1373int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
1374{
1375 struct writeback_control wbc = {
1376 .bdi = mapping->backing_dev_info,
1377 .sync_mode = WB_SYNC_ALL,
1378 .nr_to_write = LONG_MAX,
1379 .range_start = range_start,
1380 .range_end = range_end,
Trond Myklebust61822ab2006-12-05 00:35:42 -05001381 .for_writepages = 1,
Trond Myklebust1c759502006-10-09 16:18:38 -04001382 };
1383 int ret;
1384
Trond Myklebustc63c7b02007-04-02 19:29:52 -04001385 ret = nfs_writepages(mapping, &wbc);
1386 if (ret < 0)
1387 goto out;
Trond Myklebust1c759502006-10-09 16:18:38 -04001388 ret = nfs_sync_mapping_wait(mapping, &wbc, how);
1389 if (ret >= 0)
1390 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001391out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001392 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Trond Myklebust1c759502006-10-09 16:18:38 -04001393 return ret;
1394}
1395
Trond Myklebust61822ab2006-12-05 00:35:42 -05001396int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
Trond Myklebust1c759502006-10-09 16:18:38 -04001397{
1398 loff_t range_start = page_offset(page);
1399 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001400 struct writeback_control wbc = {
1401 .bdi = page->mapping->backing_dev_info,
1402 .sync_mode = WB_SYNC_ALL,
1403 .nr_to_write = LONG_MAX,
1404 .range_start = range_start,
1405 .range_end = range_end,
1406 };
1407 int ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001408
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001409 BUG_ON(!PageLocked(page));
Trond Myklebustc63c7b02007-04-02 19:29:52 -04001410 if (clear_page_dirty_for_io(page)) {
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001411 ret = nfs_writepage_locked(page, &wbc);
1412 if (ret < 0)
1413 goto out;
1414 }
Trond Myklebustf40313a2007-01-13 02:28:08 -05001415 if (!PagePrivate(page))
1416 return 0;
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001417 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1418 if (ret >= 0)
1419 return 0;
1420out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001421 __mark_inode_dirty(inode, I_DIRTY_PAGES);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001422 return ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001423}
1424
1425/*
1426 * Write back all requests on one page - we do this before reading it.
1427 */
1428int nfs_wb_page(struct inode *inode, struct page* page)
1429{
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001430 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
Trond Myklebust1c759502006-10-09 16:18:38 -04001431}
1432
Trond Myklebust1a545332006-12-05 00:35:40 -05001433int nfs_set_page_dirty(struct page *page)
1434{
Trond Myklebustd5851582007-04-30 22:17:02 -07001435 struct address_space *mapping = page->mapping;
1436 struct inode *inode;
1437 spinlock_t *req_lock;
Trond Myklebust1a545332006-12-05 00:35:40 -05001438 struct nfs_page *req;
Trond Myklebust2b82f192007-04-20 16:12:50 -04001439 int ret;
Trond Myklebust1a545332006-12-05 00:35:40 -05001440
Trond Myklebustd5851582007-04-30 22:17:02 -07001441 if (!mapping)
1442 goto out_raced;
1443 inode = mapping->host;
1444 if (!inode)
1445 goto out_raced;
1446 req_lock = &NFS_I(inode)->req_lock;
Trond Myklebust2b82f192007-04-20 16:12:50 -04001447 spin_lock(req_lock);
1448 req = nfs_page_find_request_locked(page);
Trond Myklebust1a545332006-12-05 00:35:40 -05001449 if (req != NULL) {
1450 /* Mark any existing write requests for flushing */
Trond Myklebust2b82f192007-04-20 16:12:50 -04001451 ret = !test_and_set_bit(PG_NEED_FLUSH, &req->wb_flags);
1452 spin_unlock(req_lock);
Trond Myklebust1a545332006-12-05 00:35:40 -05001453 nfs_release_request(req);
Trond Myklebust2b82f192007-04-20 16:12:50 -04001454 return ret;
Trond Myklebust1a545332006-12-05 00:35:40 -05001455 }
Trond Myklebust2b82f192007-04-20 16:12:50 -04001456 ret = __set_page_dirty_nobuffers(page);
1457 spin_unlock(req_lock);
1458 return ret;
Trond Myklebustd5851582007-04-30 22:17:02 -07001459out_raced:
1460 return !TestSetPageDirty(page);
Trond Myklebust1a545332006-12-05 00:35:40 -05001461}
1462
Trond Myklebust1c759502006-10-09 16:18:38 -04001463
David Howellsf7b422b2006-06-09 09:34:33 -04001464int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
1466 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1467 sizeof(struct nfs_write_data),
1468 0, SLAB_HWCACHE_ALIGN,
1469 NULL, NULL);
1470 if (nfs_wdata_cachep == NULL)
1471 return -ENOMEM;
1472
Matthew Dobson93d23412006-03-26 01:37:50 -08001473 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1474 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 if (nfs_wdata_mempool == NULL)
1476 return -ENOMEM;
1477
Matthew Dobson93d23412006-03-26 01:37:50 -08001478 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1479 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 if (nfs_commit_mempool == NULL)
1481 return -ENOMEM;
1482
Peter Zijlstra89a09142007-03-16 13:38:26 -08001483 /*
1484 * NFS congestion size, scale with available memory.
1485 *
1486 * 64MB: 8192k
1487 * 128MB: 11585k
1488 * 256MB: 16384k
1489 * 512MB: 23170k
1490 * 1GB: 32768k
1491 * 2GB: 46340k
1492 * 4GB: 65536k
1493 * 8GB: 92681k
1494 * 16GB: 131072k
1495 *
1496 * This allows larger machines to have larger/more transfers.
1497 * Limit the default to 256M
1498 */
1499 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1500 if (nfs_congestion_kb > 256*1024)
1501 nfs_congestion_kb = 256*1024;
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return 0;
1504}
1505
David Brownell266bee82006-06-27 12:59:15 -07001506void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
1508 mempool_destroy(nfs_commit_mempool);
1509 mempool_destroy(nfs_wdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001510 kmem_cache_destroy(nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
1512