blob: af344a158e0103465ab632274e88b9583b69c5aa [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include "delegation.h"
Trond Myklebust49a70f22006-12-05 00:35:38 -050026#include "internal.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050027#include "iostat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#define NFSDBG_FACILITY NFSDBG_PAGECACHE
30
31#define MIN_POOL_WRITE (32)
32#define MIN_POOL_COMMIT (4)
33
34/*
35 * Local function declarations
36 */
37static struct nfs_page * nfs_update_request(struct nfs_open_context*,
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 struct page *,
39 unsigned int, unsigned int);
Trond Myklebustc63c7b02007-04-02 19:29:52 -040040static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc,
41 struct inode *inode, int ioflags);
Trond Myklebust788e7a82006-03-20 13:44:27 -050042static const struct rpc_call_ops nfs_write_partial_ops;
43static const struct rpc_call_ops nfs_write_full_ops;
44static const struct rpc_call_ops nfs_commit_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Christoph Lametere18b8902006-12-06 20:33:20 -080046static struct kmem_cache *nfs_wdata_cachep;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050047static mempool_t *nfs_wdata_mempool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static mempool_t *nfs_commit_mempool;
49
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070050struct nfs_write_data *nfs_commit_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Christoph Lametere6b4f8d2006-12-06 20:33:14 -080052 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
Chuck Lever40859d72005-11-30 18:09:02 -050053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 if (p) {
55 memset(p, 0, sizeof(*p));
56 INIT_LIST_HEAD(&p->pages);
57 }
58 return p;
59}
60
Trond Myklebust10afec92007-05-14 17:16:04 -040061static void nfs_commit_rcu_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Trond Myklebust8aca67f2006-11-13 16:23:44 -050063 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
Chuck Lever40859d72005-11-30 18:09:02 -050064 if (p && (p->pagevec != &p->page_array[0]))
65 kfree(p->pagevec);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 mempool_free(p, nfs_commit_mempool);
67}
68
Trond Myklebust8aca67f2006-11-13 16:23:44 -050069void nfs_commit_free(struct nfs_write_data *wdata)
70{
71 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
72}
73
Trond Myklebust8d5658c2007-04-10 09:26:35 -040074struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050075{
Christoph Lametere6b4f8d2006-12-06 20:33:14 -080076 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050077
78 if (p) {
79 memset(p, 0, sizeof(*p));
80 INIT_LIST_HEAD(&p->pages);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070081 p->npages = pagecount;
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040082 if (pagecount <= ARRAY_SIZE(p->page_array))
83 p->pagevec = p->page_array;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050084 else {
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040085 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
86 if (!p->pagevec) {
Trond Myklebust3feb2d42006-03-20 13:44:37 -050087 mempool_free(p, nfs_wdata_mempool);
88 p = NULL;
89 }
90 }
91 }
92 return p;
93}
94
Trond Myklebust8aca67f2006-11-13 16:23:44 -050095static void nfs_writedata_rcu_free(struct rcu_head *head)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050096{
Trond Myklebust8aca67f2006-11-13 16:23:44 -050097 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050098 if (p && (p->pagevec != &p->page_array[0]))
99 kfree(p->pagevec);
100 mempool_free(p, nfs_wdata_mempool);
101}
102
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500103static void nfs_writedata_free(struct nfs_write_data *wdata)
104{
105 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
106}
107
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100108void nfs_writedata_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 nfs_writedata_free(wdata);
111}
112
Trond Myklebust277459d2006-12-05 00:35:35 -0500113static struct nfs_page *nfs_page_find_request_locked(struct page *page)
114{
115 struct nfs_page *req = NULL;
116
117 if (PagePrivate(page)) {
118 req = (struct nfs_page *)page_private(page);
119 if (req != NULL)
120 atomic_inc(&req->wb_count);
121 }
122 return req;
123}
124
125static struct nfs_page *nfs_page_find_request(struct page *page)
126{
127 struct nfs_page *req = NULL;
128 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
129
130 spin_lock(req_lock);
131 req = nfs_page_find_request_locked(page);
132 spin_unlock(req_lock);
133 return req;
134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/* Adjust the file length if we're writing beyond the end */
137static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
138{
139 struct inode *inode = page->mapping->host;
140 loff_t end, i_size = i_size_read(inode);
Trond Myklebustca52fec2007-04-17 17:22:13 -0400141 pgoff_t end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 if (i_size > 0 && page->index < end_index)
144 return;
145 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
146 if (i_size >= end)
147 return;
Chuck Lever91d5b472006-03-20 13:44:14 -0500148 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 i_size_write(inode, end);
150}
151
Trond Myklebusta301b772007-02-06 11:07:15 -0800152/* A writeback failed: mark the page as bad, and invalidate the page cache */
153static void nfs_set_pageerror(struct page *page)
154{
155 SetPageError(page);
156 nfs_zap_mapping(page->mapping->host, page->mapping);
157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/* We can set the PG_uptodate flag if we see that a write request
160 * covers the full page.
161 */
162static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
163{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (PageUptodate(page))
165 return;
166 if (base != 0)
167 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500168 if (count != nfs_page_length(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500170 if (count != PAGE_CACHE_SIZE)
Nate Diller60945cb2007-05-10 22:55:08 -0700171 zero_user_page(page, count, PAGE_CACHE_SIZE - count, KM_USER0);
Trond Myklebust49a70f22006-12-05 00:35:38 -0500172 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Trond Myklebuste21195a2006-12-05 00:35:39 -0500175static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 unsigned int offset, unsigned int count)
177{
178 struct nfs_page *req;
Trond Myklebuste21195a2006-12-05 00:35:39 -0500179 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Trond Myklebuste21195a2006-12-05 00:35:39 -0500181 for (;;) {
182 req = nfs_update_request(ctx, page, offset, count);
183 if (!IS_ERR(req))
184 break;
185 ret = PTR_ERR(req);
186 if (ret != -EBUSY)
187 return ret;
188 ret = nfs_wb_page(page->mapping->host, page);
189 if (ret != 0)
190 return ret;
191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /* Update file length */
193 nfs_grow_file(page, offset, count);
194 /* Set the PG_uptodate flag? */
195 nfs_mark_uptodate(page, offset, count);
196 nfs_unlock_request(req);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100197 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200static int wb_priority(struct writeback_control *wbc)
201{
202 if (wbc->for_reclaim)
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400203 return FLUSH_HIGHPRI | FLUSH_STABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (wbc->for_kupdate)
205 return FLUSH_LOWPRI;
206 return 0;
207}
208
209/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800210 * NFS congestion control
211 */
212
213int nfs_congestion_kb;
214
215#define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
216#define NFS_CONGESTION_OFF_THRESH \
217 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
218
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400219static int nfs_set_page_writeback(struct page *page)
Peter Zijlstra89a09142007-03-16 13:38:26 -0800220{
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400221 int ret = test_set_page_writeback(page);
222
223 if (!ret) {
Peter Zijlstra89a09142007-03-16 13:38:26 -0800224 struct inode *inode = page->mapping->host;
225 struct nfs_server *nfss = NFS_SERVER(inode);
226
Peter Zijlstra277866a2007-05-08 00:35:12 -0700227 if (atomic_long_inc_return(&nfss->writeback) >
Peter Zijlstra89a09142007-03-16 13:38:26 -0800228 NFS_CONGESTION_ON_THRESH)
229 set_bdi_congested(&nfss->backing_dev_info, WRITE);
230 }
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400231 return ret;
Peter Zijlstra89a09142007-03-16 13:38:26 -0800232}
233
234static void nfs_end_page_writeback(struct page *page)
235{
236 struct inode *inode = page->mapping->host;
237 struct nfs_server *nfss = NFS_SERVER(inode);
238
239 end_page_writeback(page);
Peter Zijlstra277866a2007-05-08 00:35:12 -0700240 if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) {
Peter Zijlstra89a09142007-03-16 13:38:26 -0800241 clear_bdi_congested(&nfss->backing_dev_info, WRITE);
242 congestion_end(WRITE);
243 }
244}
245
246/*
Trond Myklebuste261f512006-12-05 00:35:41 -0500247 * Find an associated nfs write request, and prepare to flush it out
248 * Returns 1 if there was no write request, or if the request was
249 * already tagged by nfs_set_page_dirty.Returns 0 if the request
250 * was not tagged.
251 * May also return an error if the user signalled nfs_wait_on_request().
252 */
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400253static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
254 struct page *page)
Trond Myklebuste261f512006-12-05 00:35:41 -0500255{
256 struct nfs_page *req;
Trond Myklebust612c9382007-04-20 16:12:45 -0400257 struct nfs_inode *nfsi = NFS_I(page->mapping->host);
258 spinlock_t *req_lock = &nfsi->req_lock;
Trond Myklebuste261f512006-12-05 00:35:41 -0500259 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 }
Trond Myklebust612c9382007-04-20 16:12:45 -0400282 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
283 /* This request is marked for commit */
284 spin_unlock(req_lock);
285 nfs_unlock_request(req);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400286 nfs_pageio_complete(pgio);
Trond Myklebust612c9382007-04-20 16:12:45 -0400287 return 1;
288 }
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400289 if (nfs_set_page_writeback(page) != 0) {
Trond Myklebust612c9382007-04-20 16:12:45 -0400290 spin_unlock(req_lock);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400291 BUG();
292 }
293 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
294 NFS_PAGE_TAG_WRITEBACK);
Trond Myklebuste261f512006-12-05 00:35:41 -0500295 ret = test_bit(PG_NEED_FLUSH, &req->wb_flags);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400296 spin_unlock(req_lock);
297 nfs_pageio_add_request(pgio, req);
Trond Myklebuste261f512006-12-05 00:35:41 -0500298 return ret;
299}
300
301/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 * Write an mmapped page to the server.
303 */
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500304static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400306 struct nfs_pageio_descriptor mypgio, *pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 struct nfs_open_context *ctx;
308 struct inode *inode = page->mapping->host;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500309 unsigned offset;
Trond Myklebuste261f512006-12-05 00:35:41 -0500310 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Chuck Lever91d5b472006-03-20 13:44:14 -0500312 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
313 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
314
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400315 if (wbc->for_writepages)
316 pgio = wbc->fs_private;
317 else {
318 nfs_pageio_init_write(&mypgio, inode, wb_priority(wbc));
319 pgio = &mypgio;
320 }
321
Trond Myklebust7fe7f842007-05-20 10:18:27 -0400322 nfs_pageio_cond_complete(pgio, page->index);
323
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400324 err = nfs_page_async_flush(pgio, page);
Trond Myklebuste261f512006-12-05 00:35:41 -0500325 if (err <= 0)
326 goto out;
327 err = 0;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500328 offset = nfs_page_length(page);
329 if (!offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 goto out;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500331
Trond Myklebust7fe7f842007-05-20 10:18:27 -0400332 nfs_pageio_cond_complete(pgio, page->index);
333
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 Myklebustca52fec2007-04-17 17:22:13 -0400515static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t 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;
Trond Myklebustca52fec2007-04-17 17:22:13 -0400519 pgoff_t idx_end, next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 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
Trond Myklebustca52fec2007-04-17 17:22:13 -0400574nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
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
Trond Myklebustca52fec2007-04-17 17:22:13 -0400588static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500589{
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;
Trond Myklebustca52fec2007-04-17 17:22:13 -0400608 pgoff_t rqend, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
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 Myklebust8d5658c2007-04-10 09:26:35 -0400835static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, 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
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400851 data = nfs_writedata_alloc(1);
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 Myklebust8d5658c2007-04-10 09:26:35 -0400900static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, 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 Myklebust8d5658c2007-04-10 09:26:35 -0400906 data = nfs_writedata_alloc(npages);
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)) {
Trond Myklebust10afec92007-05-14 17:16:04 -0400927 req = nfs_list_entry(head->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 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 Myklebustca52fec2007-04-17 17:22:13 -04001296 pgoff_t idx_start, idx_end;
Trond Myklebust1c759502006-10-09 16:18:38 -04001297 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) {
Trond Myklebustca52fec2007-04-17 17:22:13 -04001309 pgoff_t l_npages = 1 + idx_end - idx_start;
Trond Myklebust1c759502006-10-09 16:18:38 -04001310 npages = l_npages;
1311 if (sizeof(npages) != sizeof(l_npages) &&
Trond Myklebustca52fec2007-04-17 17:22:13 -04001312 (pgoff_t)npages != l_npages)
Trond Myklebust1c759502006-10-09 16:18:38 -04001313 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 Myklebustc42de9d2006-03-20 13:44:51 -05001319 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1320 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001321 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001322 if (nocommit)
1323 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001324 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebust724c4392007-04-17 17:22:13 -04001325 if (pages == 0)
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001326 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001327 if (how & FLUSH_INVALIDATE) {
1328 spin_unlock(&nfsi->req_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001329 nfs_cancel_commit_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001330 ret = pages;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001331 spin_lock(&nfsi->req_lock);
1332 continue;
1333 }
1334 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001335 spin_unlock(&nfsi->req_lock);
1336 ret = nfs_commit_list(inode, &head, how);
1337 spin_lock(&nfsi->req_lock);
1338 } while (ret >= 0);
1339 spin_unlock(&nfsi->req_lock);
1340 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
1342
Trond Myklebust1c759502006-10-09 16:18:38 -04001343/*
1344 * flush the inode to disk.
1345 */
1346int nfs_wb_all(struct inode *inode)
1347{
1348 struct address_space *mapping = inode->i_mapping;
1349 struct writeback_control wbc = {
1350 .bdi = mapping->backing_dev_info,
1351 .sync_mode = WB_SYNC_ALL,
1352 .nr_to_write = LONG_MAX,
Trond Myklebust61822ab2006-12-05 00:35:42 -05001353 .for_writepages = 1,
Trond Myklebust1c759502006-10-09 16:18:38 -04001354 .range_cyclic = 1,
1355 };
1356 int ret;
1357
Trond Myklebustc63c7b02007-04-02 19:29:52 -04001358 ret = nfs_writepages(mapping, &wbc);
Trond Myklebust61822ab2006-12-05 00:35:42 -05001359 if (ret < 0)
1360 goto out;
Trond Myklebust1c759502006-10-09 16:18:38 -04001361 ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
1362 if (ret >= 0)
1363 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001364out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001365 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Trond Myklebust1c759502006-10-09 16:18:38 -04001366 return ret;
1367}
1368
1369int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
1370{
1371 struct writeback_control wbc = {
1372 .bdi = mapping->backing_dev_info,
1373 .sync_mode = WB_SYNC_ALL,
1374 .nr_to_write = LONG_MAX,
1375 .range_start = range_start,
1376 .range_end = range_end,
Trond Myklebust61822ab2006-12-05 00:35:42 -05001377 .for_writepages = 1,
Trond Myklebust1c759502006-10-09 16:18:38 -04001378 };
1379 int ret;
1380
Trond Myklebustc63c7b02007-04-02 19:29:52 -04001381 ret = nfs_writepages(mapping, &wbc);
1382 if (ret < 0)
1383 goto out;
Trond Myklebust1c759502006-10-09 16:18:38 -04001384 ret = nfs_sync_mapping_wait(mapping, &wbc, how);
1385 if (ret >= 0)
1386 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001387out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001388 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Trond Myklebust1c759502006-10-09 16:18:38 -04001389 return ret;
1390}
1391
Trond Myklebust61822ab2006-12-05 00:35:42 -05001392int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
Trond Myklebust1c759502006-10-09 16:18:38 -04001393{
1394 loff_t range_start = page_offset(page);
1395 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001396 struct writeback_control wbc = {
1397 .bdi = page->mapping->backing_dev_info,
1398 .sync_mode = WB_SYNC_ALL,
1399 .nr_to_write = LONG_MAX,
1400 .range_start = range_start,
1401 .range_end = range_end,
1402 };
1403 int ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001404
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001405 BUG_ON(!PageLocked(page));
Trond Myklebustc63c7b02007-04-02 19:29:52 -04001406 if (clear_page_dirty_for_io(page)) {
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001407 ret = nfs_writepage_locked(page, &wbc);
1408 if (ret < 0)
1409 goto out;
1410 }
Trond Myklebustf40313a2007-01-13 02:28:08 -05001411 if (!PagePrivate(page))
1412 return 0;
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001413 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1414 if (ret >= 0)
1415 return 0;
1416out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001417 __mark_inode_dirty(inode, I_DIRTY_PAGES);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001418 return ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001419}
1420
1421/*
1422 * Write back all requests on one page - we do this before reading it.
1423 */
1424int nfs_wb_page(struct inode *inode, struct page* page)
1425{
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001426 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
Trond Myklebust1c759502006-10-09 16:18:38 -04001427}
1428
Trond Myklebust1a545332006-12-05 00:35:40 -05001429int nfs_set_page_dirty(struct page *page)
1430{
Trond Myklebustd5851582007-04-30 22:17:02 -07001431 struct address_space *mapping = page->mapping;
1432 struct inode *inode;
1433 spinlock_t *req_lock;
Trond Myklebust1a545332006-12-05 00:35:40 -05001434 struct nfs_page *req;
Trond Myklebust2b82f192007-04-20 16:12:50 -04001435 int ret;
Trond Myklebust1a545332006-12-05 00:35:40 -05001436
Trond Myklebustd5851582007-04-30 22:17:02 -07001437 if (!mapping)
1438 goto out_raced;
1439 inode = mapping->host;
1440 if (!inode)
1441 goto out_raced;
1442 req_lock = &NFS_I(inode)->req_lock;
Trond Myklebust2b82f192007-04-20 16:12:50 -04001443 spin_lock(req_lock);
1444 req = nfs_page_find_request_locked(page);
Trond Myklebust1a545332006-12-05 00:35:40 -05001445 if (req != NULL) {
1446 /* Mark any existing write requests for flushing */
Trond Myklebust2b82f192007-04-20 16:12:50 -04001447 ret = !test_and_set_bit(PG_NEED_FLUSH, &req->wb_flags);
1448 spin_unlock(req_lock);
Trond Myklebust1a545332006-12-05 00:35:40 -05001449 nfs_release_request(req);
Trond Myklebust2b82f192007-04-20 16:12:50 -04001450 return ret;
Trond Myklebust1a545332006-12-05 00:35:40 -05001451 }
Trond Myklebust2b82f192007-04-20 16:12:50 -04001452 ret = __set_page_dirty_nobuffers(page);
1453 spin_unlock(req_lock);
1454 return ret;
Trond Myklebustd5851582007-04-30 22:17:02 -07001455out_raced:
1456 return !TestSetPageDirty(page);
Trond Myklebust1a545332006-12-05 00:35:40 -05001457}
1458
Trond Myklebust1c759502006-10-09 16:18:38 -04001459
David Howellsf7b422b2006-06-09 09:34:33 -04001460int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
1462 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1463 sizeof(struct nfs_write_data),
1464 0, SLAB_HWCACHE_ALIGN,
1465 NULL, NULL);
1466 if (nfs_wdata_cachep == NULL)
1467 return -ENOMEM;
1468
Matthew Dobson93d23412006-03-26 01:37:50 -08001469 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1470 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 if (nfs_wdata_mempool == NULL)
1472 return -ENOMEM;
1473
Matthew Dobson93d23412006-03-26 01:37:50 -08001474 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1475 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (nfs_commit_mempool == NULL)
1477 return -ENOMEM;
1478
Peter Zijlstra89a09142007-03-16 13:38:26 -08001479 /*
1480 * NFS congestion size, scale with available memory.
1481 *
1482 * 64MB: 8192k
1483 * 128MB: 11585k
1484 * 256MB: 16384k
1485 * 512MB: 23170k
1486 * 1GB: 32768k
1487 * 2GB: 46340k
1488 * 4GB: 65536k
1489 * 8GB: 92681k
1490 * 16GB: 131072k
1491 *
1492 * This allows larger machines to have larger/more transfers.
1493 * Limit the default to 256M
1494 */
1495 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1496 if (nfs_congestion_kb > 256*1024)
1497 nfs_congestion_kb = 256*1024;
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 return 0;
1500}
1501
David Brownell266bee82006-06-27 12:59:15 -07001502void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
1504 mempool_destroy(nfs_commit_mempool);
1505 mempool_destroy(nfs_wdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001506 kmem_cache_destroy(nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508