blob: ad2e91b4904fb5d2be8d594a828e12961ba65ec5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/write.c
3 *
Trond Myklebust7c85d902006-12-13 15:23:48 -05004 * Write file data over NFS.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/types.h>
10#include <linux/slab.h>
11#include <linux/mm.h>
12#include <linux/pagemap.h>
13#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/writeback.h>
Peter Zijlstra89a09142007-03-16 13:38:26 -080015#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#include <linux/sunrpc/clnt.h>
18#include <linux/nfs_fs.h>
19#include <linux/nfs_mount.h>
20#include <linux/nfs_page.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070021#include <linux/backing-dev.h>
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/uaccess.h>
24#include <linux/smp_lock.h>
25
26#include "delegation.h"
Trond Myklebust49a70f22006-12-05 00:35:38 -050027#include "internal.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050028#include "iostat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#define NFSDBG_FACILITY NFSDBG_PAGECACHE
31
32#define MIN_POOL_WRITE (32)
33#define MIN_POOL_COMMIT (4)
34
35/*
36 * Local function declarations
37 */
38static struct nfs_page * nfs_update_request(struct nfs_open_context*,
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct page *,
40 unsigned int, unsigned int);
Trond Myklebuste261f512006-12-05 00:35:41 -050041static void nfs_mark_request_dirty(struct nfs_page *req);
Trond Myklebust3f442542006-09-17 14:46:44 -040042static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how);
Trond Myklebust788e7a82006-03-20 13:44:27 -050043static const struct rpc_call_ops nfs_write_partial_ops;
44static const struct rpc_call_ops nfs_write_full_ops;
45static const struct rpc_call_ops nfs_commit_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Christoph Lametere18b8902006-12-06 20:33:20 -080047static struct kmem_cache *nfs_wdata_cachep;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050048static mempool_t *nfs_wdata_mempool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static mempool_t *nfs_commit_mempool;
50
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070051struct nfs_write_data *nfs_commit_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Christoph Lametere6b4f8d2006-12-06 20:33:14 -080053 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
Chuck Lever40859d72005-11-30 18:09:02 -050054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 if (p) {
56 memset(p, 0, sizeof(*p));
57 INIT_LIST_HEAD(&p->pages);
58 }
59 return p;
60}
61
Trond Myklebust8aca67f2006-11-13 16:23:44 -050062void nfs_commit_rcu_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Trond Myklebust8aca67f2006-11-13 16:23:44 -050064 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
Chuck Lever40859d72005-11-30 18:09:02 -050065 if (p && (p->pagevec != &p->page_array[0]))
66 kfree(p->pagevec);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 mempool_free(p, nfs_commit_mempool);
68}
69
Trond Myklebust8aca67f2006-11-13 16:23:44 -050070void nfs_commit_free(struct nfs_write_data *wdata)
71{
72 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
73}
74
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070075struct nfs_write_data *nfs_writedata_alloc(size_t len)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050076{
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070077 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Christoph Lametere6b4f8d2006-12-06 20:33:14 -080078 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050079
80 if (p) {
81 memset(p, 0, sizeof(*p));
82 INIT_LIST_HEAD(&p->pages);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070083 p->npages = pagecount;
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040084 if (pagecount <= ARRAY_SIZE(p->page_array))
85 p->pagevec = p->page_array;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050086 else {
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040087 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
88 if (!p->pagevec) {
Trond Myklebust3feb2d42006-03-20 13:44:37 -050089 mempool_free(p, nfs_wdata_mempool);
90 p = NULL;
91 }
92 }
93 }
94 return p;
95}
96
Trond Myklebust8aca67f2006-11-13 16:23:44 -050097static void nfs_writedata_rcu_free(struct rcu_head *head)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050098{
Trond Myklebust8aca67f2006-11-13 16:23:44 -050099 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
Trond Myklebust3feb2d42006-03-20 13:44:37 -0500100 if (p && (p->pagevec != &p->page_array[0]))
101 kfree(p->pagevec);
102 mempool_free(p, nfs_wdata_mempool);
103}
104
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500105static void nfs_writedata_free(struct nfs_write_data *wdata)
106{
107 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
108}
109
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100110void nfs_writedata_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 nfs_writedata_free(wdata);
113}
114
Trond Myklebust277459d2006-12-05 00:35:35 -0500115static struct nfs_page *nfs_page_find_request_locked(struct page *page)
116{
117 struct nfs_page *req = NULL;
118
119 if (PagePrivate(page)) {
120 req = (struct nfs_page *)page_private(page);
121 if (req != NULL)
122 atomic_inc(&req->wb_count);
123 }
124 return req;
125}
126
127static struct nfs_page *nfs_page_find_request(struct page *page)
128{
129 struct nfs_page *req = NULL;
130 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
131
132 spin_lock(req_lock);
133 req = nfs_page_find_request_locked(page);
134 spin_unlock(req_lock);
135 return req;
136}
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/* Adjust the file length if we're writing beyond the end */
139static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
140{
141 struct inode *inode = page->mapping->host;
142 loff_t end, i_size = i_size_read(inode);
143 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
144
145 if (i_size > 0 && page->index < end_index)
146 return;
147 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
148 if (i_size >= end)
149 return;
Chuck Lever91d5b472006-03-20 13:44:14 -0500150 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 i_size_write(inode, end);
152}
153
Trond Myklebusta301b772007-02-06 11:07:15 -0800154/* A writeback failed: mark the page as bad, and invalidate the page cache */
155static void nfs_set_pageerror(struct page *page)
156{
157 SetPageError(page);
158 nfs_zap_mapping(page->mapping->host, page->mapping);
159}
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/* We can set the PG_uptodate flag if we see that a write request
162 * covers the full page.
163 */
164static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
165{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (PageUptodate(page))
167 return;
168 if (base != 0)
169 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500170 if (count != nfs_page_length(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500172 if (count != PAGE_CACHE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
Trond Myklebust49a70f22006-12-05 00:35:38 -0500174 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Trond Myklebuste21195a2006-12-05 00:35:39 -0500177static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 unsigned int offset, unsigned int count)
179{
180 struct nfs_page *req;
Trond Myklebuste21195a2006-12-05 00:35:39 -0500181 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Trond Myklebuste21195a2006-12-05 00:35:39 -0500183 for (;;) {
184 req = nfs_update_request(ctx, page, offset, count);
185 if (!IS_ERR(req))
186 break;
187 ret = PTR_ERR(req);
188 if (ret != -EBUSY)
189 return ret;
190 ret = nfs_wb_page(page->mapping->host, page);
191 if (ret != 0)
192 return ret;
193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 /* Update file length */
195 nfs_grow_file(page, offset, count);
196 /* Set the PG_uptodate flag? */
197 nfs_mark_uptodate(page, offset, count);
198 nfs_unlock_request(req);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100199 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202static int wb_priority(struct writeback_control *wbc)
203{
204 if (wbc->for_reclaim)
205 return FLUSH_HIGHPRI;
206 if (wbc->for_kupdate)
207 return FLUSH_LOWPRI;
208 return 0;
209}
210
211/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800212 * NFS congestion control
213 */
214
215int nfs_congestion_kb;
216
217#define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
218#define NFS_CONGESTION_OFF_THRESH \
219 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
220
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 */
255static int nfs_page_mark_flush(struct page *page)
256{
257 struct nfs_page *req;
258 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
259 int ret;
260
261 spin_lock(req_lock);
262 for(;;) {
263 req = nfs_page_find_request_locked(page);
264 if (req == NULL) {
265 spin_unlock(req_lock);
266 return 1;
267 }
268 if (nfs_lock_request_dontget(req))
269 break;
270 /* Note: If we hold the page lock, as is the case in nfs_writepage,
271 * then the call to nfs_lock_request_dontget() will always
272 * succeed provided that someone hasn't already marked the
273 * request as dirty (in which case we don't care).
274 */
275 spin_unlock(req_lock);
276 ret = nfs_wait_on_request(req);
277 nfs_release_request(req);
278 if (ret != 0)
279 return ret;
280 spin_lock(req_lock);
281 }
282 spin_unlock(req_lock);
Trond Myklebusteb4cac12007-04-15 16:21:49 -0400283 if (nfs_set_page_writeback(page) == 0) {
284 nfs_list_remove_request(req);
Trond Myklebuste261f512006-12-05 00:35:41 -0500285 nfs_mark_request_dirty(req);
Trond Myklebusteb4cac12007-04-15 16:21:49 -0400286 }
Trond Myklebuste261f512006-12-05 00:35:41 -0500287 ret = test_bit(PG_NEED_FLUSH, &req->wb_flags);
288 nfs_unlock_request(req);
289 return ret;
290}
291
292/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 * Write an mmapped page to the server.
294 */
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500295static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 struct nfs_open_context *ctx;
298 struct inode *inode = page->mapping->host;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500299 unsigned offset;
Trond Myklebuste261f512006-12-05 00:35:41 -0500300 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Chuck Lever91d5b472006-03-20 13:44:14 -0500302 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
303 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
304
Trond Myklebuste261f512006-12-05 00:35:41 -0500305 err = nfs_page_mark_flush(page);
306 if (err <= 0)
307 goto out;
308 err = 0;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500309 offset = nfs_page_length(page);
310 if (!offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 goto out;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500312
Trond Myklebustd5308382005-11-04 15:33:38 -0500313 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (ctx == NULL) {
315 err = -EBADF;
316 goto out;
317 }
Trond Myklebust200baa22006-12-05 00:35:40 -0500318 err = nfs_writepage_setup(ctx, page, 0, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 put_nfs_open_context(ctx);
Trond Myklebuste261f512006-12-05 00:35:41 -0500320 if (err != 0)
321 goto out;
322 err = nfs_page_mark_flush(page);
323 if (err > 0)
324 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325out:
Trond Myklebust200baa22006-12-05 00:35:40 -0500326 if (!wbc->for_writepages)
Trond Myklebust02241bc2007-01-13 02:28:07 -0500327 nfs_flush_mapping(page->mapping, wbc, FLUSH_STABLE|wb_priority(wbc));
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500328 return err;
329}
330
331int nfs_writepage(struct page *page, struct writeback_control *wbc)
332{
333 int err;
334
335 err = nfs_writepage_locked(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return err;
338}
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
341{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 struct inode *inode = mapping->host;
343 int err;
344
Chuck Lever91d5b472006-03-20 13:44:14 -0500345 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 err = generic_writepages(mapping, wbc);
348 if (err)
349 return err;
Trond Myklebust28c69252006-09-16 13:04:50 -0400350 err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (err < 0)
352 goto out;
Chuck Lever91d5b472006-03-20 13:44:14 -0500353 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
Trond Myklebustd30c8342006-12-13 15:23:47 -0500354 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return err;
357}
358
359/*
360 * Insert a write request into an inode
361 */
362static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
363{
364 struct nfs_inode *nfsi = NFS_I(inode);
365 int error;
366
367 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
368 BUG_ON(error == -EEXIST);
369 if (error)
370 return error;
371 if (!nfsi->npages) {
372 igrab(inode);
373 nfs_begin_data_update(inode);
374 if (nfs_have_delegation(inode, FMODE_WRITE))
375 nfsi->change_attr++;
376 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500377 SetPagePrivate(req->wb_page);
Trond Myklebust277459d2006-12-05 00:35:35 -0500378 set_page_private(req->wb_page, (unsigned long)req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 nfsi->npages++;
380 atomic_inc(&req->wb_count);
381 return 0;
382}
383
384/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800385 * Remove a write request from an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 */
387static void nfs_inode_remove_request(struct nfs_page *req)
388{
389 struct inode *inode = req->wb_context->dentry->d_inode;
390 struct nfs_inode *nfsi = NFS_I(inode);
391
392 BUG_ON (!NFS_WBACK_BUSY(req));
393
394 spin_lock(&nfsi->req_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500395 set_page_private(req->wb_page, 0);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500396 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
398 nfsi->npages--;
399 if (!nfsi->npages) {
400 spin_unlock(&nfsi->req_lock);
Trond Myklebust951a1432005-06-22 17:16:30 +0000401 nfs_end_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 iput(inode);
403 } else
404 spin_unlock(&nfsi->req_lock);
405 nfs_clear_request(req);
406 nfs_release_request(req);
407}
408
409/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 * Add a request to the inode's dirty list.
411 */
412static void
413nfs_mark_request_dirty(struct nfs_page *req)
414{
415 struct inode *inode = req->wb_context->dentry->d_inode;
416 struct nfs_inode *nfsi = NFS_I(inode);
417
418 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000419 radix_tree_tag_set(&nfsi->nfs_page_tree,
420 req->wb_index, NFS_PAGE_TAG_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 nfs_list_add_request(req, &nfsi->dirty);
422 nfsi->ndirty++;
423 spin_unlock(&nfsi->req_lock);
Trond Myklebusta1803042006-12-05 00:45:19 -0500424 __mark_inode_dirty(inode, I_DIRTY_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Trond Myklebust61822ab2006-12-05 00:35:42 -0500427static void
428nfs_redirty_request(struct nfs_page *req)
429{
Trond Myklebust61822ab2006-12-05 00:35:42 -0500430 __set_page_dirty_nobuffers(req->wb_page);
431}
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433/*
434 * Check if a request is dirty
435 */
436static inline int
437nfs_dirty_request(struct nfs_page *req)
438{
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400439 struct page *page = req->wb_page;
440
441 if (page == NULL)
442 return 0;
443 return !PageWriteback(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
447/*
448 * Add a request to the inode's commit list.
449 */
450static void
451nfs_mark_request_commit(struct nfs_page *req)
452{
453 struct inode *inode = req->wb_context->dentry->d_inode;
454 struct nfs_inode *nfsi = NFS_I(inode);
455
456 spin_lock(&nfsi->req_lock);
457 nfs_list_add_request(req, &nfsi->commit);
458 nfsi->ncommit++;
459 spin_unlock(&nfsi->req_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700460 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebusta1803042006-12-05 00:45:19 -0500461 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463#endif
464
465/*
466 * Wait for a request to complete.
467 *
468 * Interruptible by signals only if mounted with intr flag.
469 */
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500470static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 struct nfs_inode *nfsi = NFS_I(inode);
473 struct nfs_page *req;
474 unsigned long idx_end, next;
475 unsigned int res = 0;
476 int error;
477
478 if (npages == 0)
479 idx_end = ~0;
480 else
481 idx_end = idx_start + npages - 1;
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 next = idx_start;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000484 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 -0700485 if (req->wb_index > idx_end)
486 break;
487
488 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000489 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 atomic_inc(&req->wb_count);
492 spin_unlock(&nfsi->req_lock);
493 error = nfs_wait_on_request(req);
494 nfs_release_request(req);
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500495 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (error < 0)
497 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 res++;
499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return res;
501}
502
Trond Myklebust83715ad2006-07-05 13:17:12 -0400503static void nfs_cancel_dirty_list(struct list_head *head)
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400504{
505 struct nfs_page *req;
506 while(!list_empty(head)) {
507 req = nfs_list_entry(head->next);
508 nfs_list_remove_request(req);
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400509 nfs_end_page_writeback(req->wb_page);
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400510 nfs_inode_remove_request(req);
511 nfs_clear_page_writeback(req);
512 }
513}
514
Trond Myklebust83715ad2006-07-05 13:17:12 -0400515static void nfs_cancel_commit_list(struct list_head *head)
516{
517 struct nfs_page *req;
518
519 while(!list_empty(head)) {
520 req = nfs_list_entry(head->next);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700521 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400522 nfs_list_remove_request(req);
523 nfs_inode_remove_request(req);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700524 nfs_unlock_request(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400525 }
526}
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
529/*
530 * nfs_scan_commit - Scan an inode for commit requests
531 * @inode: NFS inode to scan
532 * @dst: destination list
533 * @idx_start: lower bound of page->index to scan.
534 * @npages: idx_start + npages sets the upper bound to scan.
535 *
536 * Moves requests from the inode's 'commit' request list.
537 * The requests are *not* checked to ensure that they form a contiguous set.
538 */
539static int
540nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
541{
542 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000543 int res = 0;
544
545 if (nfsi->ncommit != 0) {
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400546 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000547 nfsi->ncommit -= res;
548 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
549 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return res;
552}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500553#else
554static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
555{
556 return 0;
557}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558#endif
559
Peter Zijlstra89a09142007-03-16 13:38:26 -0800560static int nfs_wait_on_write_congestion(struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Peter Zijlstra89a09142007-03-16 13:38:26 -0800562 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 struct backing_dev_info *bdi = mapping->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 int ret = 0;
565
566 might_sleep();
567
568 if (!bdi_write_congested(bdi))
569 return 0;
Chuck Lever91d5b472006-03-20 13:44:14 -0500570
Peter Zijlstra89a09142007-03-16 13:38:26 -0800571 nfs_inc_stats(inode, NFSIOS_CONGESTIONWAIT);
Chuck Lever91d5b472006-03-20 13:44:14 -0500572
Peter Zijlstra89a09142007-03-16 13:38:26 -0800573 do {
574 struct rpc_clnt *clnt = NFS_CLIENT(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 sigset_t oldset;
576
577 rpc_clnt_sigmask(clnt, &oldset);
Peter Zijlstra89a09142007-03-16 13:38:26 -0800578 ret = congestion_wait_interruptible(WRITE, HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 rpc_clnt_sigunmask(clnt, &oldset);
Peter Zijlstra89a09142007-03-16 13:38:26 -0800580 if (ret == -ERESTARTSYS)
581 break;
582 ret = 0;
583 } while (bdi_write_congested(bdi));
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return ret;
586}
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588/*
589 * Try to update any existing write request, or create one if there is none.
590 * In order to match, the request's credentials must match those of
591 * the calling process.
592 *
593 * Note: Should always be called with the Page Lock held!
594 */
595static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
Trond Myklebuste21195a2006-12-05 00:35:39 -0500596 struct page *page, unsigned int offset, unsigned int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Peter Zijlstra89a09142007-03-16 13:38:26 -0800598 struct address_space *mapping = page->mapping;
599 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 struct nfs_inode *nfsi = NFS_I(inode);
601 struct nfs_page *req, *new = NULL;
602 unsigned long rqend, end;
603
604 end = offset + bytes;
605
Peter Zijlstra89a09142007-03-16 13:38:26 -0800606 if (nfs_wait_on_write_congestion(mapping))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return ERR_PTR(-ERESTARTSYS);
608 for (;;) {
609 /* Loop over all inode entries and see if we find
610 * A request for the page we wish to update
611 */
612 spin_lock(&nfsi->req_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500613 req = nfs_page_find_request_locked(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (req) {
615 if (!nfs_lock_request_dontget(req)) {
616 int error;
Trond Myklebust277459d2006-12-05 00:35:35 -0500617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 spin_unlock(&nfsi->req_lock);
619 error = nfs_wait_on_request(req);
620 nfs_release_request(req);
Neil Brown1dd594b2006-03-20 13:44:04 -0500621 if (error < 0) {
622 if (new)
623 nfs_release_request(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return ERR_PTR(error);
Neil Brown1dd594b2006-03-20 13:44:04 -0500625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 continue;
627 }
628 spin_unlock(&nfsi->req_lock);
629 if (new)
630 nfs_release_request(new);
631 break;
632 }
633
634 if (new) {
635 int error;
636 nfs_lock_request_dontget(new);
637 error = nfs_inode_add_request(inode, new);
638 if (error) {
639 spin_unlock(&nfsi->req_lock);
640 nfs_unlock_request(new);
641 return ERR_PTR(error);
642 }
643 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return new;
645 }
646 spin_unlock(&nfsi->req_lock);
647
648 new = nfs_create_request(ctx, inode, page, offset, bytes);
649 if (IS_ERR(new))
650 return new;
651 }
652
653 /* We have a request for our page.
654 * If the creds don't match, or the
655 * page addresses don't match,
656 * tell the caller to wait on the conflicting
657 * request.
658 */
659 rqend = req->wb_offset + req->wb_bytes;
660 if (req->wb_context != ctx
661 || req->wb_page != page
662 || !nfs_dirty_request(req)
663 || offset > rqend || end < req->wb_offset) {
664 nfs_unlock_request(req);
665 return ERR_PTR(-EBUSY);
666 }
667
668 /* Okay, the request matches. Update the region */
669 if (offset < req->wb_offset) {
670 req->wb_offset = offset;
671 req->wb_pgbase = offset;
672 req->wb_bytes = rqend - req->wb_offset;
673 }
674
675 if (end > rqend)
676 req->wb_bytes = end - req->wb_offset;
677
678 return req;
679}
680
681int nfs_flush_incompatible(struct file *file, struct page *page)
682{
683 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 struct nfs_page *req;
Trond Myklebust1a545332006-12-05 00:35:40 -0500685 int do_flush, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 /*
687 * Look for a request corresponding to this page. If there
688 * is one, and it belongs to another file, we flush it out
689 * before we try to copy anything into the page. Do this
690 * due to the lack of an ACCESS-type call in NFSv2.
691 * Also do the same if we find a request from an existing
692 * dropped page.
693 */
Trond Myklebust1a545332006-12-05 00:35:40 -0500694 do {
695 req = nfs_page_find_request(page);
696 if (req == NULL)
697 return 0;
698 do_flush = req->wb_page != page || req->wb_context != ctx
Trond Myklebuste261f512006-12-05 00:35:41 -0500699 || !nfs_dirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 nfs_release_request(req);
Trond Myklebust1a545332006-12-05 00:35:40 -0500701 if (!do_flush)
702 return 0;
703 status = nfs_wb_page(page->mapping->host, page);
704 } while (status == 0);
705 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
708/*
709 * Update and possibly write a cached page of an NFS file.
710 *
711 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
712 * things with a page scheduled for an RPC call (e.g. invalidate it).
713 */
714int nfs_updatepage(struct file *file, struct page *page,
715 unsigned int offset, unsigned int count)
716{
717 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 int status = 0;
720
Chuck Lever91d5b472006-03-20 13:44:14 -0500721 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800724 file->f_path.dentry->d_parent->d_name.name,
725 file->f_path.dentry->d_name.name, count,
Chuck Lever0bbacc42005-11-01 16:53:32 -0500726 (long long)(page_offset(page) +offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 /* If we're not using byte range locks, and we know the page
729 * is entirely in cache, it may be more efficient to avoid
730 * fragmenting write requests.
731 */
Trond Myklebustab0a3db2005-06-22 17:16:30 +0000732 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
Trond Myklebust49a70f22006-12-05 00:35:38 -0500733 count = max(count + offset, nfs_page_length(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736
Trond Myklebuste21195a2006-12-05 00:35:39 -0500737 status = nfs_writepage_setup(ctx, page, offset, count);
Trond Myklebuste261f512006-12-05 00:35:41 -0500738 __set_page_dirty_nobuffers(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
741 status, (long long)i_size_read(inode));
742 if (status < 0)
Trond Myklebusta301b772007-02-06 11:07:15 -0800743 nfs_set_pageerror(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return status;
745}
746
747static void nfs_writepage_release(struct nfs_page *req)
748{
Peter Zijlstra89a09142007-03-16 13:38:26 -0800749 nfs_end_page_writeback(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
752 if (!PageError(req->wb_page)) {
753 if (NFS_NEED_RESCHED(req)) {
Trond Myklebust61822ab2006-12-05 00:35:42 -0500754 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 goto out;
756 } else if (NFS_NEED_COMMIT(req)) {
757 nfs_mark_request_commit(req);
758 goto out;
759 }
760 }
761 nfs_inode_remove_request(req);
762
763out:
764 nfs_clear_commit(req);
765 nfs_clear_reschedule(req);
766#else
767 nfs_inode_remove_request(req);
768#endif
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000769 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
772static inline int flush_task_priority(int how)
773{
774 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
775 case FLUSH_HIGHPRI:
776 return RPC_PRIORITY_HIGH;
777 case FLUSH_LOWPRI:
778 return RPC_PRIORITY_LOW;
779 }
780 return RPC_PRIORITY_NORMAL;
781}
782
783/*
784 * Set up the argument/result storage required for the RPC call.
785 */
786static void nfs_write_rpcsetup(struct nfs_page *req,
787 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500788 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 unsigned int count, unsigned int offset,
790 int how)
791{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -0500793 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 /* Set up the RPC argument and reply structs
796 * NB: take care not to mess about with data->commit et al. */
797
798 data->req = req;
799 data->inode = inode = req->wb_context->dentry->d_inode;
800 data->cred = req->wb_context->cred;
801
802 data->args.fh = NFS_FH(inode);
803 data->args.offset = req_offset(req) + offset;
804 data->args.pgbase = req->wb_pgbase + offset;
805 data->args.pages = data->pagevec;
806 data->args.count = count;
807 data->args.context = req->wb_context;
808
809 data->res.fattr = &data->fattr;
810 data->res.count = count;
811 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400812 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Trond Myklebust788e7a82006-03-20 13:44:27 -0500814 /* Set up the initial task struct. */
815 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
816 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 NFS_PROTO(inode)->write_setup(data, how);
818
819 data->task.tk_priority = flush_task_priority(how);
820 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Chuck Levera3f565b2007-01-31 12:14:01 -0500822 dprintk("NFS: %5u initiated write call "
823 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500824 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 inode->i_sb->s_id,
826 (long long)NFS_FILEID(inode),
827 count,
828 (unsigned long long)data->args.offset);
829}
830
831static void nfs_execute_write(struct nfs_write_data *data)
832{
833 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
834 sigset_t oldset;
835
836 rpc_clnt_sigmask(clnt, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 rpc_execute(&data->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 rpc_clnt_sigunmask(clnt, &oldset);
839}
840
841/*
842 * Generate multiple small requests to write out a single
843 * contiguous dirty area on one page.
844 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500845static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
847 struct nfs_page *req = nfs_list_entry(head->next);
848 struct page *page = req->wb_page;
849 struct nfs_write_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700850 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
851 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 int requests = 0;
853 LIST_HEAD(list);
854
855 nfs_list_remove_request(req);
856
857 nbytes = req->wb_bytes;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700858 do {
859 size_t len = min(nbytes, wsize);
860
861 data = nfs_writedata_alloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (!data)
863 goto out_bad;
864 list_add(&data->pages, &list);
865 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700866 nbytes -= len;
867 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 atomic_set(&req->wb_complete, requests);
869
870 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 offset = 0;
872 nbytes = req->wb_bytes;
873 do {
874 data = list_entry(list.next, struct nfs_write_data, pages);
875 list_del_init(&data->pages);
876
877 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 if (nbytes > wsize) {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500880 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
881 wsize, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 offset += wsize;
883 nbytes -= wsize;
884 } else {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500885 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
886 nbytes, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 nbytes = 0;
888 }
889 nfs_execute_write(data);
890 } while (nbytes != 0);
891
892 return 0;
893
894out_bad:
895 while (!list_empty(&list)) {
896 data = list_entry(list.next, struct nfs_write_data, pages);
897 list_del(&data->pages);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500898 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400900 nfs_end_page_writeback(req->wb_page);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500901 nfs_redirty_request(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000902 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return -ENOMEM;
904}
905
906/*
907 * Create an RPC task for the given write request and kick it.
908 * The page must have been locked by the caller.
909 *
910 * It may happen that the page we're passed is not marked dirty.
911 * This is the case if nfs_updatepage detects a conflicting request
912 * that has been written but not committed.
913 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500914static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916 struct nfs_page *req;
917 struct page **pages;
918 struct nfs_write_data *data;
919 unsigned int count;
920
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700921 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (!data)
923 goto out_bad;
924
925 pages = data->pagevec;
926 count = 0;
927 while (!list_empty(head)) {
928 req = nfs_list_entry(head->next);
929 nfs_list_remove_request(req);
930 nfs_list_add_request(req, &data->pages);
931 ClearPageError(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 *pages++ = req->wb_page;
933 count += req->wb_bytes;
934 }
935 req = nfs_list_entry(data->pages.next);
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 /* Set up the argument struct */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500938 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 nfs_execute_write(data);
941 return 0;
942 out_bad:
943 while (!list_empty(head)) {
944 struct nfs_page *req = nfs_list_entry(head->next);
945 nfs_list_remove_request(req);
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400946 nfs_end_page_writeback(req->wb_page);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500947 nfs_redirty_request(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000948 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950 return -ENOMEM;
951}
952
Trond Myklebust7d46a492006-03-20 13:44:50 -0500953static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 LIST_HEAD(one_request);
Trond Myklebust7d46a492006-03-20 13:44:50 -0500956 int (*flush_one)(struct inode *, struct list_head *, int);
957 struct nfs_page *req;
958 int wpages = NFS_SERVER(inode)->wpages;
959 int wsize = NFS_SERVER(inode)->wsize;
960 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Trond Myklebust7d46a492006-03-20 13:44:50 -0500962 flush_one = nfs_flush_one;
963 if (wsize < PAGE_CACHE_SIZE)
964 flush_one = nfs_flush_multi;
965 /* For single writes, FLUSH_STABLE is more efficient */
966 if (npages <= wpages && npages == NFS_I(inode)->npages
967 && nfs_list_entry(head->next)->wb_bytes <= wsize)
968 how |= FLUSH_STABLE;
969
970 do {
971 nfs_coalesce_requests(head, &one_request, wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 req = nfs_list_entry(one_request.next);
Trond Myklebust7d46a492006-03-20 13:44:50 -0500973 error = flush_one(inode, &one_request, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (error < 0)
Trond Myklebust7d46a492006-03-20 13:44:50 -0500975 goto out_err;
976 } while (!list_empty(head));
977 return 0;
978out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 while (!list_empty(head)) {
980 req = nfs_list_entry(head->next);
981 nfs_list_remove_request(req);
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400982 nfs_end_page_writeback(req->wb_page);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500983 nfs_redirty_request(req);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000984 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986 return error;
987}
988
989/*
990 * Handle a write reply that flushed part of a page.
991 */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500992static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Trond Myklebust788e7a82006-03-20 13:44:27 -0500994 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 struct nfs_page *req = data->req;
996 struct page *page = req->wb_page;
997
998 dprintk("NFS: write (%s/%Ld %d@%Ld)",
999 req->wb_context->dentry->d_inode->i_sb->s_id,
1000 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1001 req->wb_bytes,
1002 (long long)req_offset(req));
1003
Trond Myklebust788e7a82006-03-20 13:44:27 -05001004 if (nfs_writeback_done(task, data) != 0)
1005 return;
1006
1007 if (task->tk_status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -08001008 nfs_set_pageerror(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001009 req->wb_context->error = task->tk_status;
1010 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 } else {
1012#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1013 if (data->verf.committed < NFS_FILE_SYNC) {
1014 if (!NFS_NEED_COMMIT(req)) {
1015 nfs_defer_commit(req);
1016 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1017 dprintk(" defer commit\n");
1018 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1019 nfs_defer_reschedule(req);
1020 dprintk(" server reboot detected\n");
1021 }
1022 } else
1023#endif
1024 dprintk(" OK\n");
1025 }
1026
1027 if (atomic_dec_and_test(&req->wb_complete))
1028 nfs_writepage_release(req);
1029}
1030
Trond Myklebust788e7a82006-03-20 13:44:27 -05001031static const struct rpc_call_ops nfs_write_partial_ops = {
1032 .rpc_call_done = nfs_writeback_done_partial,
1033 .rpc_release = nfs_writedata_release,
1034};
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036/*
1037 * Handle a write reply that flushes a whole page.
1038 *
1039 * FIXME: There is an inherent race with invalidate_inode_pages and
1040 * writebacks since the page->count is kept > 1 for as long
1041 * as the page has a write request pending.
1042 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001043static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001045 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 struct nfs_page *req;
1047 struct page *page;
1048
Trond Myklebust788e7a82006-03-20 13:44:27 -05001049 if (nfs_writeback_done(task, data) != 0)
1050 return;
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 /* Update attributes as result of writeback. */
1053 while (!list_empty(&data->pages)) {
1054 req = nfs_list_entry(data->pages.next);
1055 nfs_list_remove_request(req);
1056 page = req->wb_page;
1057
1058 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1059 req->wb_context->dentry->d_inode->i_sb->s_id,
1060 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1061 req->wb_bytes,
1062 (long long)req_offset(req));
1063
Trond Myklebust788e7a82006-03-20 13:44:27 -05001064 if (task->tk_status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -08001065 nfs_set_pageerror(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001066 req->wb_context->error = task->tk_status;
Peter Zijlstra89a09142007-03-16 13:38:26 -08001067 nfs_end_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 nfs_inode_remove_request(req);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001069 dprintk(", error = %d\n", task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 goto next;
1071 }
Peter Zijlstra89a09142007-03-16 13:38:26 -08001072 nfs_end_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1075 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1076 nfs_inode_remove_request(req);
1077 dprintk(" OK\n");
1078 goto next;
1079 }
1080 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1081 nfs_mark_request_commit(req);
1082 dprintk(" marked for commit\n");
1083#else
1084 nfs_inode_remove_request(req);
1085#endif
1086 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001087 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
1089}
1090
Trond Myklebust788e7a82006-03-20 13:44:27 -05001091static const struct rpc_call_ops nfs_write_full_ops = {
1092 .rpc_call_done = nfs_writeback_done_full,
1093 .rpc_release = nfs_writedata_release,
1094};
1095
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097/*
1098 * This function is called when the WRITE call is complete.
1099 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001100int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 struct nfs_writeargs *argp = &data->args;
1103 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001104 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Chuck Levera3f565b2007-01-31 12:14:01 -05001106 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 task->tk_pid, task->tk_status);
1108
Chuck Leverf551e442006-09-20 14:33:04 -04001109 /*
1110 * ->write_done will attempt to use post-op attributes to detect
1111 * conflicting writes by other clients. A strict interpretation
1112 * of close-to-open would allow us to continue caching even if
1113 * another writer had changed the file, but some applications
1114 * depend on tighter cache coherency when writing.
1115 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001116 status = NFS_PROTO(data->inode)->write_done(task, data);
1117 if (status != 0)
1118 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001119 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1122 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1123 /* We tried a write call, but the server did not
1124 * commit data to stable storage even though we
1125 * requested it.
1126 * Note: There is a known bug in Tru64 < 5.0 in which
1127 * the server reports NFS_DATA_SYNC, but performs
1128 * NFS_FILE_SYNC. We therefore implement this checking
1129 * as a dprintk() in order to avoid filling syslog.
1130 */
1131 static unsigned long complain;
1132
1133 if (time_before(complain, jiffies)) {
1134 dprintk("NFS: faulty NFS server %s:"
1135 " (committed = %d) != (stable = %d)\n",
David Howells54ceac42006-08-22 20:06:13 -04001136 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 resp->verf->committed, argp->stable);
1138 complain = jiffies + 300 * HZ;
1139 }
1140 }
1141#endif
1142 /* Is this a short write? */
1143 if (task->tk_status >= 0 && resp->count < argp->count) {
1144 static unsigned long complain;
1145
Chuck Lever91d5b472006-03-20 13:44:14 -05001146 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 /* Has the server at least made some progress? */
1149 if (resp->count != 0) {
1150 /* Was this an NFSv2 write or an NFSv3 stable write? */
1151 if (resp->verf->committed != NFS_UNSTABLE) {
1152 /* Resend from where the server left off */
1153 argp->offset += resp->count;
1154 argp->pgbase += resp->count;
1155 argp->count -= resp->count;
1156 } else {
1157 /* Resend as a stable write in order to avoid
1158 * headaches in the case of a server crash.
1159 */
1160 argp->stable = NFS_FILE_SYNC;
1161 }
1162 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001163 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165 if (time_before(complain, jiffies)) {
1166 printk(KERN_WARNING
1167 "NFS: Server wrote zero bytes, expected %u.\n",
1168 argp->count);
1169 complain = jiffies + 300 * HZ;
1170 }
1171 /* Can't do anything about it except throw an error. */
1172 task->tk_status = -EIO;
1173 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
1177
1178#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001179void nfs_commit_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 nfs_commit_free(wdata);
1182}
1183
1184/*
1185 * Set up the argument/result storage required for the RPC call.
1186 */
1187static void nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001188 struct nfs_write_data *data,
1189 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001191 struct nfs_page *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001193 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 /* Set up the RPC argument and reply structs
1196 * NB: take care not to mess about with data->commit et al. */
1197
1198 list_splice_init(head, &data->pages);
1199 first = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 inode = first->wb_context->dentry->d_inode;
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 data->inode = inode;
1203 data->cred = first->wb_context->cred;
1204
1205 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001206 /* Note: we always request a commit of the entire inode */
1207 data->args.offset = 0;
1208 data->args.count = 0;
1209 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 data->res.fattr = &data->fattr;
1211 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001212 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001213
1214 /* Set up the initial task struct. */
1215 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1216 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 NFS_PROTO(inode)->commit_setup(data, how);
1218
1219 data->task.tk_priority = flush_task_priority(how);
1220 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Chuck Levera3f565b2007-01-31 12:14:01 -05001222 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
1225/*
1226 * Commit dirty pages
1227 */
1228static int
Chuck Lever40859d72005-11-30 18:09:02 -05001229nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
1231 struct nfs_write_data *data;
1232 struct nfs_page *req;
1233
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001234 data = nfs_commit_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 if (!data)
1237 goto out_bad;
1238
1239 /* Set up the argument struct */
1240 nfs_commit_rpcsetup(head, data, how);
1241
1242 nfs_execute_write(data);
1243 return 0;
1244 out_bad:
1245 while (!list_empty(head)) {
1246 req = nfs_list_entry(head->next);
1247 nfs_list_remove_request(req);
1248 nfs_mark_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001249 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust5c2d97c2006-09-18 23:20:35 -04001250 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
1252 return -ENOMEM;
1253}
1254
1255/*
1256 * COMMIT call returned
1257 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001258static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001260 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Chuck Levera3f565b2007-01-31 12:14:01 -05001263 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 task->tk_pid, task->tk_status);
1265
Trond Myklebust788e7a82006-03-20 13:44:27 -05001266 /* Call the NFS version-specific code */
1267 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1268 return;
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 while (!list_empty(&data->pages)) {
1271 req = nfs_list_entry(data->pages.next);
1272 nfs_list_remove_request(req);
Christoph Lameterfd39fc82006-06-30 01:55:40 -07001273 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1276 req->wb_context->dentry->d_inode->i_sb->s_id,
1277 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1278 req->wb_bytes,
1279 (long long)req_offset(req));
1280 if (task->tk_status < 0) {
1281 req->wb_context->error = task->tk_status;
1282 nfs_inode_remove_request(req);
1283 dprintk(", error = %d\n", task->tk_status);
1284 goto next;
1285 }
1286
1287 /* Okay, COMMIT succeeded, apparently. Check the verifier
1288 * returned by the server against all stored verfs. */
1289 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1290 /* We have a match */
1291 nfs_inode_remove_request(req);
1292 dprintk(" OK\n");
1293 goto next;
1294 }
1295 /* We have a mismatch. Write the page again */
1296 dprintk(" mismatch\n");
Trond Myklebust61822ab2006-12-05 00:35:42 -05001297 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001299 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001302
1303static const struct rpc_call_ops nfs_commit_ops = {
1304 .rpc_call_done = nfs_commit_done,
1305 .rpc_release = nfs_commit_release,
1306};
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001307#else
1308static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1309{
1310 return 0;
1311}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312#endif
1313
Trond Myklebust3f442542006-09-17 14:46:44 -04001314static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
Trond Myklebust28c69252006-09-16 13:04:50 -04001316 struct nfs_inode *nfsi = NFS_I(mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 LIST_HEAD(head);
Trond Myklebust3f442542006-09-17 14:46:44 -04001318 long res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 spin_lock(&nfsi->req_lock);
Trond Myklebust3f442542006-09-17 14:46:44 -04001321 res = nfs_scan_dirty(mapping, wbc, &head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 spin_unlock(&nfsi->req_lock);
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001323 if (res) {
Trond Myklebust28c69252006-09-16 13:04:50 -04001324 int error = nfs_flush_list(mapping->host, &head, res, how);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001325 if (error < 0)
1326 return error;
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return res;
1329}
1330
1331#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001332int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 struct nfs_inode *nfsi = NFS_I(inode);
1335 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001336 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001339 res = nfs_scan_commit(inode, &head, 0, 0);
1340 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001342 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001343 if (error < 0)
1344 return error;
1345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return res;
1347}
1348#endif
1349
Trond Myklebust1c759502006-10-09 16:18:38 -04001350long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Trond Myklebust1c759502006-10-09 16:18:38 -04001352 struct inode *inode = mapping->host;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001353 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c759502006-10-09 16:18:38 -04001354 unsigned long idx_start, idx_end;
1355 unsigned int npages = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001356 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001357 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebust3f442542006-09-17 14:46:44 -04001358 long pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Trond Myklebust1c759502006-10-09 16:18:38 -04001360 /* FIXME */
1361 if (wbc->range_cyclic)
1362 idx_start = 0;
1363 else {
1364 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1365 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1366 if (idx_end > idx_start) {
1367 unsigned long l_npages = 1 + idx_end - idx_start;
1368 npages = l_npages;
1369 if (sizeof(npages) != sizeof(l_npages) &&
1370 (unsigned long)npages != l_npages)
1371 npages = 0;
1372 }
1373 }
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001374 how &= ~FLUSH_NOCOMMIT;
1375 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 do {
Trond Myklebust1c759502006-10-09 16:18:38 -04001377 wbc->pages_skipped = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001378 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1379 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001380 continue;
Trond Myklebust1c759502006-10-09 16:18:38 -04001381 pages = nfs_scan_dirty(mapping, wbc, &head);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001382 if (pages != 0) {
1383 spin_unlock(&nfsi->req_lock);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001384 if (how & FLUSH_INVALIDATE) {
Trond Myklebust83715ad2006-07-05 13:17:12 -04001385 nfs_cancel_dirty_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001386 ret = pages;
1387 } else
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001388 ret = nfs_flush_list(inode, &head, pages, how);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001389 spin_lock(&nfsi->req_lock);
1390 continue;
1391 }
Trond Myklebust1c759502006-10-09 16:18:38 -04001392 if (wbc->pages_skipped != 0)
1393 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001394 if (nocommit)
1395 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001396 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebust1c759502006-10-09 16:18:38 -04001397 if (pages == 0) {
1398 if (wbc->pages_skipped != 0)
1399 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001400 break;
Trond Myklebust1c759502006-10-09 16:18:38 -04001401 }
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001402 if (how & FLUSH_INVALIDATE) {
1403 spin_unlock(&nfsi->req_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001404 nfs_cancel_commit_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001405 ret = pages;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001406 spin_lock(&nfsi->req_lock);
1407 continue;
1408 }
1409 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001410 spin_unlock(&nfsi->req_lock);
1411 ret = nfs_commit_list(inode, &head, how);
1412 spin_lock(&nfsi->req_lock);
1413 } while (ret >= 0);
1414 spin_unlock(&nfsi->req_lock);
1415 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
1417
Trond Myklebust1c759502006-10-09 16:18:38 -04001418/*
1419 * flush the inode to disk.
1420 */
1421int nfs_wb_all(struct inode *inode)
1422{
1423 struct address_space *mapping = inode->i_mapping;
1424 struct writeback_control wbc = {
1425 .bdi = mapping->backing_dev_info,
1426 .sync_mode = WB_SYNC_ALL,
1427 .nr_to_write = LONG_MAX,
Trond Myklebust61822ab2006-12-05 00:35:42 -05001428 .for_writepages = 1,
Trond Myklebust1c759502006-10-09 16:18:38 -04001429 .range_cyclic = 1,
1430 };
1431 int ret;
1432
Trond Myklebust61822ab2006-12-05 00:35:42 -05001433 ret = generic_writepages(mapping, &wbc);
1434 if (ret < 0)
1435 goto out;
Trond Myklebust1c759502006-10-09 16:18:38 -04001436 ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
1437 if (ret >= 0)
1438 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001439out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001440 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Trond Myklebust1c759502006-10-09 16:18:38 -04001441 return ret;
1442}
1443
1444int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
1445{
1446 struct writeback_control wbc = {
1447 .bdi = mapping->backing_dev_info,
1448 .sync_mode = WB_SYNC_ALL,
1449 .nr_to_write = LONG_MAX,
1450 .range_start = range_start,
1451 .range_end = range_end,
Trond Myklebust61822ab2006-12-05 00:35:42 -05001452 .for_writepages = 1,
Trond Myklebust1c759502006-10-09 16:18:38 -04001453 };
1454 int ret;
1455
Trond Myklebust61822ab2006-12-05 00:35:42 -05001456 if (!(how & FLUSH_NOWRITEPAGE)) {
1457 ret = generic_writepages(mapping, &wbc);
1458 if (ret < 0)
1459 goto out;
1460 }
Trond Myklebust1c759502006-10-09 16:18:38 -04001461 ret = nfs_sync_mapping_wait(mapping, &wbc, how);
1462 if (ret >= 0)
1463 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001464out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001465 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Trond Myklebust1c759502006-10-09 16:18:38 -04001466 return ret;
1467}
1468
Trond Myklebust61822ab2006-12-05 00:35:42 -05001469int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
Trond Myklebust1c759502006-10-09 16:18:38 -04001470{
1471 loff_t range_start = page_offset(page);
1472 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001473 struct writeback_control wbc = {
1474 .bdi = page->mapping->backing_dev_info,
1475 .sync_mode = WB_SYNC_ALL,
1476 .nr_to_write = LONG_MAX,
1477 .range_start = range_start,
1478 .range_end = range_end,
1479 };
1480 int ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001481
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001482 BUG_ON(!PageLocked(page));
1483 if (!(how & FLUSH_NOWRITEPAGE) && clear_page_dirty_for_io(page)) {
1484 ret = nfs_writepage_locked(page, &wbc);
1485 if (ret < 0)
1486 goto out;
1487 }
Trond Myklebustf40313a2007-01-13 02:28:08 -05001488 if (!PagePrivate(page))
1489 return 0;
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001490 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1491 if (ret >= 0)
1492 return 0;
1493out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001494 __mark_inode_dirty(inode, I_DIRTY_PAGES);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001495 return ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001496}
1497
1498/*
1499 * Write back all requests on one page - we do this before reading it.
1500 */
1501int nfs_wb_page(struct inode *inode, struct page* page)
1502{
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001503 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
Trond Myklebust1c759502006-10-09 16:18:38 -04001504}
1505
Trond Myklebust1a545332006-12-05 00:35:40 -05001506int nfs_set_page_dirty(struct page *page)
1507{
1508 struct nfs_page *req;
1509
1510 req = nfs_page_find_request(page);
1511 if (req != NULL) {
1512 /* Mark any existing write requests for flushing */
1513 set_bit(PG_NEED_FLUSH, &req->wb_flags);
1514 nfs_release_request(req);
1515 }
1516 return __set_page_dirty_nobuffers(page);
1517}
1518
Trond Myklebust1c759502006-10-09 16:18:38 -04001519
David Howellsf7b422b2006-06-09 09:34:33 -04001520int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1523 sizeof(struct nfs_write_data),
1524 0, SLAB_HWCACHE_ALIGN,
1525 NULL, NULL);
1526 if (nfs_wdata_cachep == NULL)
1527 return -ENOMEM;
1528
Matthew Dobson93d23412006-03-26 01:37:50 -08001529 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1530 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 if (nfs_wdata_mempool == NULL)
1532 return -ENOMEM;
1533
Matthew Dobson93d23412006-03-26 01:37:50 -08001534 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1535 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (nfs_commit_mempool == NULL)
1537 return -ENOMEM;
1538
Peter Zijlstra89a09142007-03-16 13:38:26 -08001539 /*
1540 * NFS congestion size, scale with available memory.
1541 *
1542 * 64MB: 8192k
1543 * 128MB: 11585k
1544 * 256MB: 16384k
1545 * 512MB: 23170k
1546 * 1GB: 32768k
1547 * 2GB: 46340k
1548 * 4GB: 65536k
1549 * 8GB: 92681k
1550 * 16GB: 131072k
1551 *
1552 * This allows larger machines to have larger/more transfers.
1553 * Limit the default to 256M
1554 */
1555 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1556 if (nfs_congestion_kb > 256*1024)
1557 nfs_congestion_kb = 256*1024;
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return 0;
1560}
1561
David Brownell266bee82006-06-27 12:59:15 -07001562void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
1564 mempool_destroy(nfs_commit_mempool);
1565 mempool_destroy(nfs_wdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001566 kmem_cache_destroy(nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
1568