blob: 82d7ee98c675da31aec4161512711f2ec5b9e5b3 [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 Myklebust7b159fc2007-07-25 14:09:54 -0400113static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
114{
115 ctx->error = error;
116 smp_wmb();
117 set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
118}
119
Trond Myklebust277459d2006-12-05 00:35:35 -0500120static struct nfs_page *nfs_page_find_request_locked(struct page *page)
121{
122 struct nfs_page *req = NULL;
123
124 if (PagePrivate(page)) {
125 req = (struct nfs_page *)page_private(page);
126 if (req != NULL)
Trond Myklebustc03b4022007-06-17 13:26:38 -0400127 kref_get(&req->wb_kref);
Trond Myklebust277459d2006-12-05 00:35:35 -0500128 }
129 return req;
130}
131
132static struct nfs_page *nfs_page_find_request(struct page *page)
133{
Trond Myklebust587142f2007-07-02 09:57:54 -0400134 struct inode *inode = page->mapping->host;
Trond Myklebust277459d2006-12-05 00:35:35 -0500135 struct nfs_page *req = NULL;
Trond Myklebust277459d2006-12-05 00:35:35 -0500136
Trond Myklebust587142f2007-07-02 09:57:54 -0400137 spin_lock(&inode->i_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500138 req = nfs_page_find_request_locked(page);
Trond Myklebust587142f2007-07-02 09:57:54 -0400139 spin_unlock(&inode->i_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500140 return req;
141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/* Adjust the file length if we're writing beyond the end */
144static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
145{
146 struct inode *inode = page->mapping->host;
147 loff_t end, i_size = i_size_read(inode);
Trond Myklebustca52fec2007-04-17 17:22:13 -0400148 pgoff_t end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 if (i_size > 0 && page->index < end_index)
151 return;
152 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
153 if (i_size >= end)
154 return;
Chuck Lever91d5b472006-03-20 13:44:14 -0500155 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 i_size_write(inode, end);
157}
158
Trond Myklebusta301b772007-02-06 11:07:15 -0800159/* A writeback failed: mark the page as bad, and invalidate the page cache */
160static void nfs_set_pageerror(struct page *page)
161{
162 SetPageError(page);
163 nfs_zap_mapping(page->mapping->host, page->mapping);
164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/* We can set the PG_uptodate flag if we see that a write request
167 * covers the full page.
168 */
169static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
170{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (PageUptodate(page))
172 return;
173 if (base != 0)
174 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500175 if (count != nfs_page_length(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500177 if (count != PAGE_CACHE_SIZE)
Nate Diller60945cb2007-05-10 22:55:08 -0700178 zero_user_page(page, count, PAGE_CACHE_SIZE - count, KM_USER0);
Trond Myklebust49a70f22006-12-05 00:35:38 -0500179 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Trond Myklebuste21195a2006-12-05 00:35:39 -0500182static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 unsigned int offset, unsigned int count)
184{
185 struct nfs_page *req;
Trond Myklebuste21195a2006-12-05 00:35:39 -0500186 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Trond Myklebuste21195a2006-12-05 00:35:39 -0500188 for (;;) {
189 req = nfs_update_request(ctx, page, offset, count);
190 if (!IS_ERR(req))
191 break;
192 ret = PTR_ERR(req);
193 if (ret != -EBUSY)
194 return ret;
195 ret = nfs_wb_page(page->mapping->host, page);
196 if (ret != 0)
197 return ret;
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /* Update file length */
200 nfs_grow_file(page, offset, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 nfs_unlock_request(req);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205static int wb_priority(struct writeback_control *wbc)
206{
207 if (wbc->for_reclaim)
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400208 return FLUSH_HIGHPRI | FLUSH_STABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (wbc->for_kupdate)
210 return FLUSH_LOWPRI;
211 return 0;
212}
213
214/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800215 * NFS congestion control
216 */
217
218int nfs_congestion_kb;
219
220#define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
221#define NFS_CONGESTION_OFF_THRESH \
222 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
223
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400224static int nfs_set_page_writeback(struct page *page)
Peter Zijlstra89a09142007-03-16 13:38:26 -0800225{
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400226 int ret = test_set_page_writeback(page);
227
228 if (!ret) {
Peter Zijlstra89a09142007-03-16 13:38:26 -0800229 struct inode *inode = page->mapping->host;
230 struct nfs_server *nfss = NFS_SERVER(inode);
231
Peter Zijlstra277866a2007-05-08 00:35:12 -0700232 if (atomic_long_inc_return(&nfss->writeback) >
Peter Zijlstra89a09142007-03-16 13:38:26 -0800233 NFS_CONGESTION_ON_THRESH)
234 set_bdi_congested(&nfss->backing_dev_info, WRITE);
235 }
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400236 return ret;
Peter Zijlstra89a09142007-03-16 13:38:26 -0800237}
238
239static void nfs_end_page_writeback(struct page *page)
240{
241 struct inode *inode = page->mapping->host;
242 struct nfs_server *nfss = NFS_SERVER(inode);
243
244 end_page_writeback(page);
Peter Zijlstrac4dc4be2007-10-16 23:25:41 -0700245 if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
Peter Zijlstra89a09142007-03-16 13:38:26 -0800246 clear_bdi_congested(&nfss->backing_dev_info, WRITE);
Peter Zijlstra89a09142007-03-16 13:38:26 -0800247}
248
249/*
Trond Myklebuste261f512006-12-05 00:35:41 -0500250 * Find an associated nfs write request, and prepare to flush it out
Trond Myklebust9cccef92007-07-22 17:09:05 -0400251 * May return an error if the user signalled nfs_wait_on_request().
Trond Myklebuste261f512006-12-05 00:35:41 -0500252 */
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{
Trond Myklebust587142f2007-07-02 09:57:54 -0400256 struct inode *inode = page->mapping->host;
257 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebuste261f512006-12-05 00:35:41 -0500258 struct nfs_page *req;
Trond Myklebuste261f512006-12-05 00:35:41 -0500259 int ret;
260
Trond Myklebust587142f2007-07-02 09:57:54 -0400261 spin_lock(&inode->i_lock);
Trond Myklebuste261f512006-12-05 00:35:41 -0500262 for(;;) {
263 req = nfs_page_find_request_locked(page);
264 if (req == NULL) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400265 spin_unlock(&inode->i_lock);
Trond Myklebust9cccef92007-07-22 17:09:05 -0400266 return 0;
Trond Myklebuste261f512006-12-05 00:35:41 -0500267 }
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 */
Trond Myklebust587142f2007-07-02 09:57:54 -0400275 spin_unlock(&inode->i_lock);
Trond Myklebuste261f512006-12-05 00:35:41 -0500276 ret = nfs_wait_on_request(req);
277 nfs_release_request(req);
278 if (ret != 0)
279 return ret;
Trond Myklebust587142f2007-07-02 09:57:54 -0400280 spin_lock(&inode->i_lock);
Trond Myklebuste261f512006-12-05 00:35:41 -0500281 }
Trond Myklebust612c9382007-04-20 16:12:45 -0400282 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
283 /* This request is marked for commit */
Trond Myklebust587142f2007-07-02 09:57:54 -0400284 spin_unlock(&inode->i_lock);
Trond Myklebust612c9382007-04-20 16:12:45 -0400285 nfs_unlock_request(req);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400286 nfs_pageio_complete(pgio);
Trond Myklebust9cccef92007-07-22 17:09:05 -0400287 return 0;
Trond Myklebust612c9382007-04-20 16:12:45 -0400288 }
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400289 if (nfs_set_page_writeback(page) != 0) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400290 spin_unlock(&inode->i_lock);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400291 BUG();
292 }
293 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
Trond Myklebust9fd367f2007-06-17 15:10:24 -0400294 NFS_PAGE_TAG_LOCKED);
Trond Myklebust587142f2007-07-02 09:57:54 -0400295 spin_unlock(&inode->i_lock);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400296 nfs_pageio_add_request(pgio, req);
Trond Myklebust9cccef92007-07-22 17:09:05 -0400297 return 0;
Trond Myklebuste261f512006-12-05 00:35:41 -0500298}
299
Trond Myklebustf758c8852007-07-22 19:27:32 -0400300static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
301{
302 struct inode *inode = page->mapping->host;
303
304 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
305 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
306
307 nfs_pageio_cond_complete(pgio, page->index);
308 return nfs_page_async_flush(pgio, page);
309}
310
Trond Myklebuste261f512006-12-05 00:35:41 -0500311/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 * Write an mmapped page to the server.
313 */
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500314static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Trond Myklebustf758c8852007-07-22 19:27:32 -0400316 struct nfs_pageio_descriptor pgio;
Trond Myklebuste261f512006-12-05 00:35:41 -0500317 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Trond Myklebustf758c8852007-07-22 19:27:32 -0400319 nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
320 err = nfs_do_writepage(page, wbc, &pgio);
321 nfs_pageio_complete(&pgio);
322 if (err < 0)
323 return err;
324 if (pgio.pg_error < 0)
325 return pgio.pg_error;
326 return 0;
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500327}
328
329int nfs_writepage(struct page *page, struct writeback_control *wbc)
330{
Trond Myklebustf758c8852007-07-22 19:27:32 -0400331 int ret;
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500332
Trond Myklebustf758c8852007-07-22 19:27:32 -0400333 ret = nfs_writepage_locked(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 unlock_page(page);
Trond Myklebustf758c8852007-07-22 19:27:32 -0400335 return ret;
336}
337
338static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
339{
340 int ret;
341
342 ret = nfs_do_writepage(page, wbc, data);
343 unlock_page(page);
344 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
348{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 struct inode *inode = mapping->host;
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400350 struct nfs_pageio_descriptor pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 int err;
352
Chuck Lever91d5b472006-03-20 13:44:14 -0500353 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
354
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400355 nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
Trond Myklebustf758c8852007-07-22 19:27:32 -0400356 err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400357 nfs_pageio_complete(&pgio);
Trond Myklebustf758c8852007-07-22 19:27:32 -0400358 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return err;
Trond Myklebustf758c8852007-07-22 19:27:32 -0400360 if (pgio.pg_error < 0)
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400361 return pgio.pg_error;
362 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
365/*
366 * Insert a write request into an inode
367 */
368static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
369{
370 struct nfs_inode *nfsi = NFS_I(inode);
371 int error;
372
373 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
374 BUG_ON(error == -EEXIST);
375 if (error)
376 return error;
377 if (!nfsi->npages) {
378 igrab(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (nfs_have_delegation(inode, FMODE_WRITE))
380 nfsi->change_attr++;
381 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500382 SetPagePrivate(req->wb_page);
Trond Myklebust277459d2006-12-05 00:35:35 -0500383 set_page_private(req->wb_page, (unsigned long)req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 nfsi->npages++;
Trond Myklebustc03b4022007-06-17 13:26:38 -0400385 kref_get(&req->wb_kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return 0;
387}
388
389/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800390 * Remove a write request from an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 */
392static void nfs_inode_remove_request(struct nfs_page *req)
393{
Trond Myklebust88be9f92007-06-05 10:42:27 -0400394 struct inode *inode = req->wb_context->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 struct nfs_inode *nfsi = NFS_I(inode);
396
397 BUG_ON (!NFS_WBACK_BUSY(req));
398
Trond Myklebust587142f2007-07-02 09:57:54 -0400399 spin_lock(&inode->i_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500400 set_page_private(req->wb_page, 0);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500401 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
403 nfsi->npages--;
404 if (!nfsi->npages) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400405 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 iput(inode);
407 } else
Trond Myklebust587142f2007-07-02 09:57:54 -0400408 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 nfs_clear_request(req);
410 nfs_release_request(req);
411}
412
Trond Myklebust61822ab2006-12-05 00:35:42 -0500413static void
414nfs_redirty_request(struct nfs_page *req)
415{
Trond Myklebust61822ab2006-12-05 00:35:42 -0500416 __set_page_dirty_nobuffers(req->wb_page);
417}
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419/*
420 * Check if a request is dirty
421 */
422static inline int
423nfs_dirty_request(struct nfs_page *req)
424{
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400425 struct page *page = req->wb_page;
426
Trond Myklebust612c9382007-04-20 16:12:45 -0400427 if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags))
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400428 return 0;
429 return !PageWriteback(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
432#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
433/*
434 * Add a request to the inode's commit list.
435 */
436static void
437nfs_mark_request_commit(struct nfs_page *req)
438{
Trond Myklebust88be9f92007-06-05 10:42:27 -0400439 struct inode *inode = req->wb_context->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 struct nfs_inode *nfsi = NFS_I(inode);
441
Trond Myklebust587142f2007-07-02 09:57:54 -0400442 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 nfsi->ncommit++;
Trond Myklebust612c9382007-04-20 16:12:45 -0400444 set_bit(PG_NEED_COMMIT, &(req)->wb_flags);
Trond Myklebust5c369682007-06-17 15:27:42 -0400445 radix_tree_tag_set(&nfsi->nfs_page_tree,
446 req->wb_index,
447 NFS_PAGE_TAG_COMMIT);
Trond Myklebust587142f2007-07-02 09:57:54 -0400448 spin_unlock(&inode->i_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700449 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebusta1803042006-12-05 00:45:19 -0500450 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400452
453static inline
454int nfs_write_need_commit(struct nfs_write_data *data)
455{
456 return data->verf.committed != NFS_FILE_SYNC;
457}
458
459static inline
460int nfs_reschedule_unstable_write(struct nfs_page *req)
461{
Trond Myklebust612c9382007-04-20 16:12:45 -0400462 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400463 nfs_mark_request_commit(req);
464 return 1;
465 }
466 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
467 nfs_redirty_request(req);
468 return 1;
469 }
470 return 0;
471}
472#else
473static inline void
474nfs_mark_request_commit(struct nfs_page *req)
475{
476}
477
478static inline
479int nfs_write_need_commit(struct nfs_write_data *data)
480{
481 return 0;
482}
483
484static inline
485int nfs_reschedule_unstable_write(struct nfs_page *req)
486{
487 return 0;
488}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489#endif
490
491/*
492 * Wait for a request to complete.
493 *
494 * Interruptible by signals only if mounted with intr flag.
495 */
Trond Myklebustca52fec2007-04-17 17:22:13 -0400496static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct nfs_inode *nfsi = NFS_I(inode);
499 struct nfs_page *req;
Trond Myklebustca52fec2007-04-17 17:22:13 -0400500 pgoff_t idx_end, next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 unsigned int res = 0;
502 int error;
503
504 if (npages == 0)
505 idx_end = ~0;
506 else
507 idx_end = idx_start + npages - 1;
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 next = idx_start;
Trond Myklebust9fd367f2007-06-17 15:10:24 -0400510 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_LOCKED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (req->wb_index > idx_end)
512 break;
513
514 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000515 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Trond Myklebustc03b4022007-06-17 13:26:38 -0400517 kref_get(&req->wb_kref);
Trond Myklebust587142f2007-07-02 09:57:54 -0400518 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 error = nfs_wait_on_request(req);
520 nfs_release_request(req);
Trond Myklebust587142f2007-07-02 09:57:54 -0400521 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (error < 0)
523 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 res++;
525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return res;
527}
528
Trond Myklebust83715ad2006-07-05 13:17:12 -0400529static void nfs_cancel_commit_list(struct list_head *head)
530{
531 struct nfs_page *req;
532
533 while(!list_empty(head)) {
534 req = nfs_list_entry(head->next);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700535 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400536 nfs_list_remove_request(req);
Trond Myklebust612c9382007-04-20 16:12:45 -0400537 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400538 nfs_inode_remove_request(req);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700539 nfs_unlock_request(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400540 }
541}
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
544/*
545 * nfs_scan_commit - Scan an inode for commit requests
546 * @inode: NFS inode to scan
547 * @dst: destination list
548 * @idx_start: lower bound of page->index to scan.
549 * @npages: idx_start + npages sets the upper bound to scan.
550 *
551 * Moves requests from the inode's 'commit' request list.
552 * The requests are *not* checked to ensure that they form a contiguous set.
553 */
554static int
Trond Myklebustca52fec2007-04-17 17:22:13 -0400555nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000558 int res = 0;
559
560 if (nfsi->ncommit != 0) {
Trond Myklebust5c369682007-06-17 15:27:42 -0400561 res = nfs_scan_list(nfsi, dst, idx_start, npages,
562 NFS_PAGE_TAG_COMMIT);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000563 nfsi->ncommit -= res;
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return res;
566}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500567#else
Trond Myklebustca52fec2007-04-17 17:22:13 -0400568static 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 -0500569{
570 return 0;
571}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572#endif
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574/*
575 * Try to update any existing write request, or create one if there is none.
576 * In order to match, the request's credentials must match those of
577 * the calling process.
578 *
579 * Note: Should always be called with the Page Lock held!
580 */
581static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
Trond Myklebuste21195a2006-12-05 00:35:39 -0500582 struct page *page, unsigned int offset, unsigned int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Peter Zijlstra89a09142007-03-16 13:38:26 -0800584 struct address_space *mapping = page->mapping;
585 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 struct nfs_page *req, *new = NULL;
Trond Myklebustca52fec2007-04-17 17:22:13 -0400587 pgoff_t rqend, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 end = offset + bytes;
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 for (;;) {
592 /* Loop over all inode entries and see if we find
593 * A request for the page we wish to update
594 */
Trond Myklebust587142f2007-07-02 09:57:54 -0400595 spin_lock(&inode->i_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500596 req = nfs_page_find_request_locked(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (req) {
598 if (!nfs_lock_request_dontget(req)) {
599 int error;
Trond Myklebust277459d2006-12-05 00:35:35 -0500600
Trond Myklebust587142f2007-07-02 09:57:54 -0400601 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 error = nfs_wait_on_request(req);
603 nfs_release_request(req);
Neil Brown1dd594b2006-03-20 13:44:04 -0500604 if (error < 0) {
605 if (new)
606 nfs_release_request(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return ERR_PTR(error);
Neil Brown1dd594b2006-03-20 13:44:04 -0500608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 continue;
610 }
Trond Myklebust587142f2007-07-02 09:57:54 -0400611 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (new)
613 nfs_release_request(new);
614 break;
615 }
616
617 if (new) {
618 int error;
619 nfs_lock_request_dontget(new);
620 error = nfs_inode_add_request(inode, new);
621 if (error) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400622 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 nfs_unlock_request(new);
624 return ERR_PTR(error);
625 }
Trond Myklebust587142f2007-07-02 09:57:54 -0400626 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return new;
628 }
Trond Myklebust587142f2007-07-02 09:57:54 -0400629 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 new = nfs_create_request(ctx, inode, page, offset, bytes);
632 if (IS_ERR(new))
633 return new;
634 }
635
636 /* We have a request for our page.
637 * If the creds don't match, or the
638 * page addresses don't match,
639 * tell the caller to wait on the conflicting
640 * request.
641 */
642 rqend = req->wb_offset + req->wb_bytes;
643 if (req->wb_context != ctx
644 || req->wb_page != page
645 || !nfs_dirty_request(req)
646 || offset > rqend || end < req->wb_offset) {
647 nfs_unlock_request(req);
648 return ERR_PTR(-EBUSY);
649 }
650
651 /* Okay, the request matches. Update the region */
652 if (offset < req->wb_offset) {
653 req->wb_offset = offset;
654 req->wb_pgbase = offset;
655 req->wb_bytes = rqend - req->wb_offset;
656 }
657
658 if (end > rqend)
659 req->wb_bytes = end - req->wb_offset;
660
661 return req;
662}
663
664int nfs_flush_incompatible(struct file *file, struct page *page)
665{
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400666 struct nfs_open_context *ctx = nfs_file_open_context(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 struct nfs_page *req;
Trond Myklebust1a545332006-12-05 00:35:40 -0500668 int do_flush, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 /*
670 * Look for a request corresponding to this page. If there
671 * is one, and it belongs to another file, we flush it out
672 * before we try to copy anything into the page. Do this
673 * due to the lack of an ACCESS-type call in NFSv2.
674 * Also do the same if we find a request from an existing
675 * dropped page.
676 */
Trond Myklebust1a545332006-12-05 00:35:40 -0500677 do {
678 req = nfs_page_find_request(page);
679 if (req == NULL)
680 return 0;
681 do_flush = req->wb_page != page || req->wb_context != ctx
Trond Myklebuste261f512006-12-05 00:35:41 -0500682 || !nfs_dirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 nfs_release_request(req);
Trond Myklebust1a545332006-12-05 00:35:40 -0500684 if (!do_flush)
685 return 0;
686 status = nfs_wb_page(page->mapping->host, page);
687 } while (status == 0);
688 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
691/*
692 * Update and possibly write a cached page of an NFS file.
693 *
694 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
695 * things with a page scheduled for an RPC call (e.g. invalidate it).
696 */
697int nfs_updatepage(struct file *file, struct page *page,
698 unsigned int offset, unsigned int count)
699{
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400700 struct nfs_open_context *ctx = nfs_file_open_context(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 int status = 0;
703
Chuck Lever91d5b472006-03-20 13:44:14 -0500704 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800707 file->f_path.dentry->d_parent->d_name.name,
708 file->f_path.dentry->d_name.name, count,
Chuck Lever0bbacc42005-11-01 16:53:32 -0500709 (long long)(page_offset(page) +offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 /* If we're not using byte range locks, and we know the page
712 * is entirely in cache, it may be more efficient to avoid
713 * fragmenting write requests.
714 */
Trond Myklebustab0a3db2005-06-22 17:16:30 +0000715 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
Trond Myklebust49a70f22006-12-05 00:35:38 -0500716 count = max(count + offset, nfs_page_length(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719
Trond Myklebuste21195a2006-12-05 00:35:39 -0500720 status = nfs_writepage_setup(ctx, page, offset, count);
Trond Myklebuste261f512006-12-05 00:35:41 -0500721 __set_page_dirty_nobuffers(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
724 status, (long long)i_size_read(inode));
725 if (status < 0)
Trond Myklebusta301b772007-02-06 11:07:15 -0800726 nfs_set_pageerror(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return status;
728}
729
730static void nfs_writepage_release(struct nfs_page *req)
731{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Trond Myklebust44dd1512007-05-19 11:58:03 -0400733 if (PageError(req->wb_page)) {
734 nfs_end_page_writeback(req->wb_page);
735 nfs_inode_remove_request(req);
736 } else if (!nfs_reschedule_unstable_write(req)) {
737 /* Set the PG_uptodate flag */
738 nfs_mark_uptodate(req->wb_page, req->wb_pgbase, req->wb_bytes);
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400739 nfs_end_page_writeback(req->wb_page);
740 nfs_inode_remove_request(req);
741 } else
742 nfs_end_page_writeback(req->wb_page);
Trond Myklebust9fd367f2007-06-17 15:10:24 -0400743 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
746static inline int flush_task_priority(int how)
747{
748 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
749 case FLUSH_HIGHPRI:
750 return RPC_PRIORITY_HIGH;
751 case FLUSH_LOWPRI:
752 return RPC_PRIORITY_LOW;
753 }
754 return RPC_PRIORITY_NORMAL;
755}
756
757/*
758 * Set up the argument/result storage required for the RPC call.
759 */
760static void nfs_write_rpcsetup(struct nfs_page *req,
761 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500762 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 unsigned int count, unsigned int offset,
764 int how)
765{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -0500767 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 /* Set up the RPC argument and reply structs
770 * NB: take care not to mess about with data->commit et al. */
771
772 data->req = req;
Trond Myklebust88be9f92007-06-05 10:42:27 -0400773 data->inode = inode = req->wb_context->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 data->cred = req->wb_context->cred;
775
776 data->args.fh = NFS_FH(inode);
777 data->args.offset = req_offset(req) + offset;
778 data->args.pgbase = req->wb_pgbase + offset;
779 data->args.pages = data->pagevec;
780 data->args.count = count;
781 data->args.context = req->wb_context;
782
783 data->res.fattr = &data->fattr;
784 data->res.count = count;
785 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400786 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Trond Myklebust788e7a82006-03-20 13:44:27 -0500788 /* Set up the initial task struct. */
789 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
790 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 NFS_PROTO(inode)->write_setup(data, how);
792
793 data->task.tk_priority = flush_task_priority(how);
794 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Chuck Levera3f565b2007-01-31 12:14:01 -0500796 dprintk("NFS: %5u initiated write call "
797 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500798 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 inode->i_sb->s_id,
800 (long long)NFS_FILEID(inode),
801 count,
802 (unsigned long long)data->args.offset);
803}
804
805static void nfs_execute_write(struct nfs_write_data *data)
806{
807 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
808 sigset_t oldset;
809
810 rpc_clnt_sigmask(clnt, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 rpc_execute(&data->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 rpc_clnt_sigunmask(clnt, &oldset);
813}
814
815/*
816 * Generate multiple small requests to write out a single
817 * contiguous dirty area on one page.
818 */
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400819static 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 -0700820{
821 struct nfs_page *req = nfs_list_entry(head->next);
822 struct page *page = req->wb_page;
823 struct nfs_write_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700824 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
825 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 int requests = 0;
827 LIST_HEAD(list);
828
829 nfs_list_remove_request(req);
830
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400831 nbytes = count;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700832 do {
833 size_t len = min(nbytes, wsize);
834
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400835 data = nfs_writedata_alloc(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (!data)
837 goto out_bad;
838 list_add(&data->pages, &list);
839 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700840 nbytes -= len;
841 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 atomic_set(&req->wb_complete, requests);
843
844 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 offset = 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400846 nbytes = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 do {
848 data = list_entry(list.next, struct nfs_write_data, pages);
849 list_del_init(&data->pages);
850
851 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400853 if (nbytes < wsize)
854 wsize = nbytes;
855 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
856 wsize, offset, how);
857 offset += wsize;
858 nbytes -= wsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 nfs_execute_write(data);
860 } while (nbytes != 0);
861
862 return 0;
863
864out_bad:
865 while (!list_empty(&list)) {
866 data = list_entry(list.next, struct nfs_write_data, pages);
867 list_del(&data->pages);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500868 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
Trond Myklebust61822ab2006-12-05 00:35:42 -0500870 nfs_redirty_request(req);
Trond Myklebust6d677e32007-04-20 16:12:40 -0400871 nfs_end_page_writeback(req->wb_page);
Trond Myklebust9fd367f2007-06-17 15:10:24 -0400872 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return -ENOMEM;
874}
875
876/*
877 * Create an RPC task for the given write request and kick it.
878 * The page must have been locked by the caller.
879 *
880 * It may happen that the page we're passed is not marked dirty.
881 * This is the case if nfs_updatepage detects a conflicting request
882 * that has been written but not committed.
883 */
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400884static 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 -0700885{
886 struct nfs_page *req;
887 struct page **pages;
888 struct nfs_write_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400890 data = nfs_writedata_alloc(npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (!data)
892 goto out_bad;
893
894 pages = data->pagevec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 while (!list_empty(head)) {
896 req = nfs_list_entry(head->next);
897 nfs_list_remove_request(req);
898 nfs_list_add_request(req, &data->pages);
899 ClearPageError(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 *pages++ = req->wb_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902 req = nfs_list_entry(data->pages.next);
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 /* Set up the argument struct */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500905 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 nfs_execute_write(data);
908 return 0;
909 out_bad:
910 while (!list_empty(head)) {
Trond Myklebust10afec92007-05-14 17:16:04 -0400911 req = nfs_list_entry(head->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 nfs_list_remove_request(req);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500913 nfs_redirty_request(req);
Trond Myklebust6d677e32007-04-20 16:12:40 -0400914 nfs_end_page_writeback(req->wb_page);
Trond Myklebust9fd367f2007-06-17 15:10:24 -0400915 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917 return -ENOMEM;
918}
919
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400920static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
921 struct inode *inode, int ioflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Trond Myklebust7d46a492006-03-20 13:44:50 -0500923 int wsize = NFS_SERVER(inode)->wsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400925 if (wsize < PAGE_CACHE_SIZE)
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400926 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400927 else
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400928 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
931/*
932 * Handle a write reply that flushed part of a page.
933 */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500934static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Trond Myklebust788e7a82006-03-20 13:44:27 -0500936 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 struct nfs_page *req = data->req;
938 struct page *page = req->wb_page;
939
940 dprintk("NFS: write (%s/%Ld %d@%Ld)",
Trond Myklebust88be9f92007-06-05 10:42:27 -0400941 req->wb_context->path.dentry->d_inode->i_sb->s_id,
942 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 req->wb_bytes,
944 (long long)req_offset(req));
945
Trond Myklebust788e7a82006-03-20 13:44:27 -0500946 if (nfs_writeback_done(task, data) != 0)
947 return;
948
949 if (task->tk_status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -0800950 nfs_set_pageerror(page);
Trond Myklebust7b159fc2007-07-25 14:09:54 -0400951 nfs_context_set_write_error(req->wb_context, task->tk_status);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500952 dprintk(", error = %d\n", task->tk_status);
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400953 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
955
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400956 if (nfs_write_need_commit(data)) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400957 struct inode *inode = page->mapping->host;
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400958
Trond Myklebust587142f2007-07-02 09:57:54 -0400959 spin_lock(&inode->i_lock);
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400960 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
961 /* Do nothing we need to resend the writes */
962 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
963 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
964 dprintk(" defer commit\n");
965 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
966 set_bit(PG_NEED_RESCHED, &req->wb_flags);
967 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
968 dprintk(" server reboot detected\n");
969 }
Trond Myklebust587142f2007-07-02 09:57:54 -0400970 spin_unlock(&inode->i_lock);
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400971 } else
972 dprintk(" OK\n");
973
974out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (atomic_dec_and_test(&req->wb_complete))
976 nfs_writepage_release(req);
977}
978
Trond Myklebust788e7a82006-03-20 13:44:27 -0500979static const struct rpc_call_ops nfs_write_partial_ops = {
980 .rpc_call_done = nfs_writeback_done_partial,
981 .rpc_release = nfs_writedata_release,
982};
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984/*
985 * Handle a write reply that flushes a whole page.
986 *
987 * FIXME: There is an inherent race with invalidate_inode_pages and
988 * writebacks since the page->count is kept > 1 for as long
989 * as the page has a write request pending.
990 */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500991static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Trond Myklebust788e7a82006-03-20 13:44:27 -0500993 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 struct nfs_page *req;
995 struct page *page;
996
Trond Myklebust788e7a82006-03-20 13:44:27 -0500997 if (nfs_writeback_done(task, data) != 0)
998 return;
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 /* Update attributes as result of writeback. */
1001 while (!list_empty(&data->pages)) {
1002 req = nfs_list_entry(data->pages.next);
1003 nfs_list_remove_request(req);
1004 page = req->wb_page;
1005
1006 dprintk("NFS: write (%s/%Ld %d@%Ld)",
Trond Myklebust88be9f92007-06-05 10:42:27 -04001007 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1008 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 req->wb_bytes,
1010 (long long)req_offset(req));
1011
Trond Myklebust788e7a82006-03-20 13:44:27 -05001012 if (task->tk_status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -08001013 nfs_set_pageerror(page);
Trond Myklebust7b159fc2007-07-25 14:09:54 -04001014 nfs_context_set_write_error(req->wb_context, task->tk_status);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001015 dprintk(", error = %d\n", task->tk_status);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001016 goto remove_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001019 if (nfs_write_need_commit(data)) {
1020 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1021 nfs_mark_request_commit(req);
1022 nfs_end_page_writeback(page);
1023 dprintk(" marked for commit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 goto next;
1025 }
Trond Myklebust44dd1512007-05-19 11:58:03 -04001026 /* Set the PG_uptodate flag? */
1027 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001028 dprintk(" OK\n");
1029remove_request:
1030 nfs_end_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 nfs_inode_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 next:
Trond Myklebust9fd367f2007-06-17 15:10:24 -04001033 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035}
1036
Trond Myklebust788e7a82006-03-20 13:44:27 -05001037static const struct rpc_call_ops nfs_write_full_ops = {
1038 .rpc_call_done = nfs_writeback_done_full,
1039 .rpc_release = nfs_writedata_release,
1040};
1041
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043/*
1044 * This function is called when the WRITE call is complete.
1045 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001046int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 struct nfs_writeargs *argp = &data->args;
1049 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001050 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Chuck Levera3f565b2007-01-31 12:14:01 -05001052 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 task->tk_pid, task->tk_status);
1054
Chuck Leverf551e442006-09-20 14:33:04 -04001055 /*
1056 * ->write_done will attempt to use post-op attributes to detect
1057 * conflicting writes by other clients. A strict interpretation
1058 * of close-to-open would allow us to continue caching even if
1059 * another writer had changed the file, but some applications
1060 * depend on tighter cache coherency when writing.
1061 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001062 status = NFS_PROTO(data->inode)->write_done(task, data);
1063 if (status != 0)
1064 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001065 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1068 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1069 /* We tried a write call, but the server did not
1070 * commit data to stable storage even though we
1071 * requested it.
1072 * Note: There is a known bug in Tru64 < 5.0 in which
1073 * the server reports NFS_DATA_SYNC, but performs
1074 * NFS_FILE_SYNC. We therefore implement this checking
1075 * as a dprintk() in order to avoid filling syslog.
1076 */
1077 static unsigned long complain;
1078
1079 if (time_before(complain, jiffies)) {
1080 dprintk("NFS: faulty NFS server %s:"
1081 " (committed = %d) != (stable = %d)\n",
David Howells54ceac42006-08-22 20:06:13 -04001082 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 resp->verf->committed, argp->stable);
1084 complain = jiffies + 300 * HZ;
1085 }
1086 }
1087#endif
1088 /* Is this a short write? */
1089 if (task->tk_status >= 0 && resp->count < argp->count) {
1090 static unsigned long complain;
1091
Chuck Lever91d5b472006-03-20 13:44:14 -05001092 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 /* Has the server at least made some progress? */
1095 if (resp->count != 0) {
1096 /* Was this an NFSv2 write or an NFSv3 stable write? */
1097 if (resp->verf->committed != NFS_UNSTABLE) {
1098 /* Resend from where the server left off */
1099 argp->offset += resp->count;
1100 argp->pgbase += resp->count;
1101 argp->count -= resp->count;
1102 } else {
1103 /* Resend as a stable write in order to avoid
1104 * headaches in the case of a server crash.
1105 */
1106 argp->stable = NFS_FILE_SYNC;
1107 }
1108 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001109 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
1111 if (time_before(complain, jiffies)) {
1112 printk(KERN_WARNING
1113 "NFS: Server wrote zero bytes, expected %u.\n",
1114 argp->count);
1115 complain = jiffies + 300 * HZ;
1116 }
1117 /* Can't do anything about it except throw an error. */
1118 task->tk_status = -EIO;
1119 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
1123
1124#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001125void nfs_commit_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 nfs_commit_free(wdata);
1128}
1129
1130/*
1131 * Set up the argument/result storage required for the RPC call.
1132 */
1133static void nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001134 struct nfs_write_data *data,
1135 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001137 struct nfs_page *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001139 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 /* Set up the RPC argument and reply structs
1142 * NB: take care not to mess about with data->commit et al. */
1143
1144 list_splice_init(head, &data->pages);
1145 first = nfs_list_entry(data->pages.next);
Trond Myklebust88be9f92007-06-05 10:42:27 -04001146 inode = first->wb_context->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 data->inode = inode;
1149 data->cred = first->wb_context->cred;
1150
1151 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001152 /* Note: we always request a commit of the entire inode */
1153 data->args.offset = 0;
1154 data->args.count = 0;
1155 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 data->res.fattr = &data->fattr;
1157 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001158 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001159
1160 /* Set up the initial task struct. */
1161 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1162 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 NFS_PROTO(inode)->commit_setup(data, how);
1164
1165 data->task.tk_priority = flush_task_priority(how);
1166 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Chuck Levera3f565b2007-01-31 12:14:01 -05001168 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
1171/*
1172 * Commit dirty pages
1173 */
1174static int
Chuck Lever40859d72005-11-30 18:09:02 -05001175nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
1177 struct nfs_write_data *data;
1178 struct nfs_page *req;
1179
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001180 data = nfs_commit_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 if (!data)
1183 goto out_bad;
1184
1185 /* Set up the argument struct */
1186 nfs_commit_rpcsetup(head, data, how);
1187
1188 nfs_execute_write(data);
1189 return 0;
1190 out_bad:
1191 while (!list_empty(head)) {
1192 req = nfs_list_entry(head->next);
1193 nfs_list_remove_request(req);
1194 nfs_mark_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001195 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust9fd367f2007-06-17 15:10:24 -04001196 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
1198 return -ENOMEM;
1199}
1200
1201/*
1202 * COMMIT call returned
1203 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001204static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001206 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Chuck Levera3f565b2007-01-31 12:14:01 -05001209 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 task->tk_pid, task->tk_status);
1211
Trond Myklebust788e7a82006-03-20 13:44:27 -05001212 /* Call the NFS version-specific code */
1213 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1214 return;
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 while (!list_empty(&data->pages)) {
1217 req = nfs_list_entry(data->pages.next);
1218 nfs_list_remove_request(req);
Trond Myklebust612c9382007-04-20 16:12:45 -04001219 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
Christoph Lameterfd39fc82006-06-30 01:55:40 -07001220 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
Trond Myklebust88be9f92007-06-05 10:42:27 -04001223 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1224 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 req->wb_bytes,
1226 (long long)req_offset(req));
1227 if (task->tk_status < 0) {
Trond Myklebust7b159fc2007-07-25 14:09:54 -04001228 nfs_context_set_write_error(req->wb_context, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 nfs_inode_remove_request(req);
1230 dprintk(", error = %d\n", task->tk_status);
1231 goto next;
1232 }
1233
1234 /* Okay, COMMIT succeeded, apparently. Check the verifier
1235 * returned by the server against all stored verfs. */
1236 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1237 /* We have a match */
Trond Myklebust44dd1512007-05-19 11:58:03 -04001238 /* Set the PG_uptodate flag */
1239 nfs_mark_uptodate(req->wb_page, req->wb_pgbase,
1240 req->wb_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 nfs_inode_remove_request(req);
1242 dprintk(" OK\n");
1243 goto next;
1244 }
1245 /* We have a mismatch. Write the page again */
1246 dprintk(" mismatch\n");
Trond Myklebust61822ab2006-12-05 00:35:42 -05001247 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 next:
Trond Myklebust9fd367f2007-06-17 15:10:24 -04001249 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001252
1253static const struct rpc_call_ops nfs_commit_ops = {
1254 .rpc_call_done = nfs_commit_done,
1255 .rpc_release = nfs_commit_release,
1256};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001258int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001261 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Trond Myklebust587142f2007-07-02 09:57:54 -04001263 spin_lock(&inode->i_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001264 res = nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebust587142f2007-07-02 09:57:54 -04001265 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001267 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001268 if (error < 0)
1269 return error;
1270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 return res;
1272}
Trond Myklebustc63c7b02007-04-02 19:29:52 -04001273#else
1274static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1275{
1276 return 0;
1277}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278#endif
1279
Trond Myklebust1c759502006-10-09 16:18:38 -04001280long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Trond Myklebust1c759502006-10-09 16:18:38 -04001282 struct inode *inode = mapping->host;
Trond Myklebustca52fec2007-04-17 17:22:13 -04001283 pgoff_t idx_start, idx_end;
Trond Myklebust1c759502006-10-09 16:18:38 -04001284 unsigned int npages = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001285 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001286 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebust3f442542006-09-17 14:46:44 -04001287 long pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Trond Myklebust1c759502006-10-09 16:18:38 -04001289 /* FIXME */
1290 if (wbc->range_cyclic)
1291 idx_start = 0;
1292 else {
1293 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1294 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1295 if (idx_end > idx_start) {
Trond Myklebustca52fec2007-04-17 17:22:13 -04001296 pgoff_t l_npages = 1 + idx_end - idx_start;
Trond Myklebust1c759502006-10-09 16:18:38 -04001297 npages = l_npages;
1298 if (sizeof(npages) != sizeof(l_npages) &&
Trond Myklebustca52fec2007-04-17 17:22:13 -04001299 (pgoff_t)npages != l_npages)
Trond Myklebust1c759502006-10-09 16:18:38 -04001300 npages = 0;
1301 }
1302 }
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001303 how &= ~FLUSH_NOCOMMIT;
Trond Myklebust587142f2007-07-02 09:57:54 -04001304 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 do {
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001306 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1307 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001308 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001309 if (nocommit)
1310 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001311 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebust724c4392007-04-17 17:22:13 -04001312 if (pages == 0)
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001313 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001314 if (how & FLUSH_INVALIDATE) {
Trond Myklebust587142f2007-07-02 09:57:54 -04001315 spin_unlock(&inode->i_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001316 nfs_cancel_commit_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001317 ret = pages;
Trond Myklebust587142f2007-07-02 09:57:54 -04001318 spin_lock(&inode->i_lock);
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001319 continue;
1320 }
1321 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebust587142f2007-07-02 09:57:54 -04001322 spin_unlock(&inode->i_lock);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001323 ret = nfs_commit_list(inode, &head, how);
Trond Myklebust587142f2007-07-02 09:57:54 -04001324 spin_lock(&inode->i_lock);
1325
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001326 } while (ret >= 0);
Trond Myklebust587142f2007-07-02 09:57:54 -04001327 spin_unlock(&inode->i_lock);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001328 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
Trond Myklebust34901f72007-07-25 14:09:54 -04001331static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
Trond Myklebust1c759502006-10-09 16:18:38 -04001332{
Trond Myklebust1c759502006-10-09 16:18:38 -04001333 int ret;
1334
Trond Myklebust34901f72007-07-25 14:09:54 -04001335 ret = nfs_writepages(mapping, wbc);
Trond Myklebust61822ab2006-12-05 00:35:42 -05001336 if (ret < 0)
1337 goto out;
Trond Myklebust34901f72007-07-25 14:09:54 -04001338 ret = nfs_sync_mapping_wait(mapping, wbc, how);
Trond Myklebusted90ef52007-07-20 13:13:28 -04001339 if (ret < 0)
1340 goto out;
1341 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001342out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001343 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Trond Myklebust1c759502006-10-09 16:18:38 -04001344 return ret;
1345}
1346
Trond Myklebust34901f72007-07-25 14:09:54 -04001347/* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
1348static int nfs_write_mapping(struct address_space *mapping, int how)
1349{
1350 struct writeback_control wbc = {
1351 .bdi = mapping->backing_dev_info,
1352 .sync_mode = WB_SYNC_NONE,
1353 .nr_to_write = LONG_MAX,
1354 .for_writepages = 1,
1355 .range_cyclic = 1,
1356 };
1357 int ret;
1358
1359 ret = __nfs_write_mapping(mapping, &wbc, how);
1360 if (ret < 0)
1361 return ret;
1362 wbc.sync_mode = WB_SYNC_ALL;
1363 return __nfs_write_mapping(mapping, &wbc, how);
1364}
1365
Trond Myklebusted90ef52007-07-20 13:13:28 -04001366/*
1367 * flush the inode to disk.
1368 */
1369int nfs_wb_all(struct inode *inode)
Trond Myklebust1c759502006-10-09 16:18:38 -04001370{
Trond Myklebusted90ef52007-07-20 13:13:28 -04001371 return nfs_write_mapping(inode->i_mapping, 0);
1372}
Trond Myklebust1c759502006-10-09 16:18:38 -04001373
Trond Myklebusted90ef52007-07-20 13:13:28 -04001374int nfs_wb_nocommit(struct inode *inode)
1375{
1376 return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
Trond Myklebust1c759502006-10-09 16:18:38 -04001377}
1378
Trond Myklebust1b3b4a12007-08-28 10:29:36 -04001379int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1380{
1381 struct nfs_page *req;
1382 loff_t range_start = page_offset(page);
1383 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1384 struct writeback_control wbc = {
1385 .bdi = page->mapping->backing_dev_info,
1386 .sync_mode = WB_SYNC_ALL,
1387 .nr_to_write = LONG_MAX,
1388 .range_start = range_start,
1389 .range_end = range_end,
1390 };
1391 int ret = 0;
1392
1393 BUG_ON(!PageLocked(page));
1394 for (;;) {
1395 req = nfs_page_find_request(page);
1396 if (req == NULL)
1397 goto out;
1398 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1399 nfs_release_request(req);
1400 break;
1401 }
1402 if (nfs_lock_request_dontget(req)) {
1403 nfs_inode_remove_request(req);
1404 /*
1405 * In case nfs_inode_remove_request has marked the
1406 * page as being dirty
1407 */
1408 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1409 nfs_unlock_request(req);
1410 break;
1411 }
1412 ret = nfs_wait_on_request(req);
1413 if (ret < 0)
1414 goto out;
1415 }
1416 if (!PagePrivate(page))
1417 return 0;
1418 ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
1419out:
1420 return ret;
1421}
1422
Trond Myklebust61822ab2006-12-05 00:35:42 -05001423int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
Trond Myklebust1c759502006-10-09 16:18:38 -04001424{
1425 loff_t range_start = page_offset(page);
1426 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001427 struct writeback_control wbc = {
1428 .bdi = page->mapping->backing_dev_info,
1429 .sync_mode = WB_SYNC_ALL,
1430 .nr_to_write = LONG_MAX,
1431 .range_start = range_start,
1432 .range_end = range_end,
1433 };
1434 int ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001435
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001436 BUG_ON(!PageLocked(page));
Trond Myklebustc63c7b02007-04-02 19:29:52 -04001437 if (clear_page_dirty_for_io(page)) {
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001438 ret = nfs_writepage_locked(page, &wbc);
1439 if (ret < 0)
1440 goto out;
1441 }
Trond Myklebustf40313a2007-01-13 02:28:08 -05001442 if (!PagePrivate(page))
1443 return 0;
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001444 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1445 if (ret >= 0)
1446 return 0;
1447out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001448 __mark_inode_dirty(inode, I_DIRTY_PAGES);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001449 return ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001450}
1451
1452/*
1453 * Write back all requests on one page - we do this before reading it.
1454 */
1455int nfs_wb_page(struct inode *inode, struct page* page)
1456{
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001457 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
Trond Myklebust1c759502006-10-09 16:18:38 -04001458}
1459
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,
Paul Mundt20c2df82007-07-20 10:11:58 +09001465 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 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