blob: bed63416a55b3c51ab539666c4f3f5f5ca8321b0 [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);
Fred Isamanf8512ad2008-03-19 11:24:39 -040042static void nfs_redirty_request(struct nfs_page *req);
Trond Myklebust788e7a82006-03-20 13:44:27 -050043static const struct rpc_call_ops nfs_write_partial_ops;
44static const struct rpc_call_ops nfs_write_full_ops;
45static const struct rpc_call_ops nfs_commit_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Christoph Lametere18b8902006-12-06 20:33:20 -080047static struct kmem_cache *nfs_wdata_cachep;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050048static mempool_t *nfs_wdata_mempool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static mempool_t *nfs_commit_mempool;
50
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070051struct nfs_write_data *nfs_commit_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Christoph Lametere6b4f8d2006-12-06 20:33:14 -080053 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
Chuck Lever40859d72005-11-30 18:09:02 -050054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 if (p) {
56 memset(p, 0, sizeof(*p));
57 INIT_LIST_HEAD(&p->pages);
58 }
59 return p;
60}
61
Trond Myklebust10afec92007-05-14 17:16:04 -040062static void nfs_commit_rcu_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Trond Myklebust8aca67f2006-11-13 16:23:44 -050064 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
Chuck Lever40859d72005-11-30 18:09:02 -050065 if (p && (p->pagevec != &p->page_array[0]))
66 kfree(p->pagevec);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 mempool_free(p, nfs_commit_mempool);
68}
69
Trond Myklebust8aca67f2006-11-13 16:23:44 -050070void nfs_commit_free(struct nfs_write_data *wdata)
71{
72 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
73}
74
Trond Myklebust8d5658c2007-04-10 09:26:35 -040075struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050076{
Christoph Lametere6b4f8d2006-12-06 20:33:14 -080077 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050078
79 if (p) {
80 memset(p, 0, sizeof(*p));
81 INIT_LIST_HEAD(&p->pages);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070082 p->npages = pagecount;
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040083 if (pagecount <= ARRAY_SIZE(p->page_array))
84 p->pagevec = p->page_array;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050085 else {
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040086 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
87 if (!p->pagevec) {
Trond Myklebust3feb2d42006-03-20 13:44:37 -050088 mempool_free(p, nfs_wdata_mempool);
89 p = NULL;
90 }
91 }
92 }
93 return p;
94}
95
Trond Myklebust8aca67f2006-11-13 16:23:44 -050096static void nfs_writedata_rcu_free(struct rcu_head *head)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050097{
Trond Myklebust8aca67f2006-11-13 16:23:44 -050098 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050099 if (p && (p->pagevec != &p->page_array[0]))
100 kfree(p->pagevec);
101 mempool_free(p, nfs_wdata_mempool);
102}
103
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500104static void nfs_writedata_free(struct nfs_write_data *wdata)
105{
106 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
107}
108
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100109void nfs_writedata_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 nfs_writedata_free(wdata);
112}
113
Trond Myklebust7b159fc2007-07-25 14:09:54 -0400114static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
115{
116 ctx->error = error;
117 smp_wmb();
118 set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
119}
120
Trond Myklebust277459d2006-12-05 00:35:35 -0500121static struct nfs_page *nfs_page_find_request_locked(struct page *page)
122{
123 struct nfs_page *req = NULL;
124
125 if (PagePrivate(page)) {
126 req = (struct nfs_page *)page_private(page);
127 if (req != NULL)
Trond Myklebustc03b4022007-06-17 13:26:38 -0400128 kref_get(&req->wb_kref);
Trond Myklebust277459d2006-12-05 00:35:35 -0500129 }
130 return req;
131}
132
133static struct nfs_page *nfs_page_find_request(struct page *page)
134{
Trond Myklebust587142f2007-07-02 09:57:54 -0400135 struct inode *inode = page->mapping->host;
Trond Myklebust277459d2006-12-05 00:35:35 -0500136 struct nfs_page *req = NULL;
Trond Myklebust277459d2006-12-05 00:35:35 -0500137
Trond Myklebust587142f2007-07-02 09:57:54 -0400138 spin_lock(&inode->i_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500139 req = nfs_page_find_request_locked(page);
Trond Myklebust587142f2007-07-02 09:57:54 -0400140 spin_unlock(&inode->i_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500141 return req;
142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* Adjust the file length if we're writing beyond the end */
145static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
146{
147 struct inode *inode = page->mapping->host;
148 loff_t end, i_size = i_size_read(inode);
Trond Myklebustca52fec2007-04-17 17:22:13 -0400149 pgoff_t end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 if (i_size > 0 && page->index < end_index)
152 return;
153 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
154 if (i_size >= end)
155 return;
Chuck Lever91d5b472006-03-20 13:44:14 -0500156 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 i_size_write(inode, end);
158}
159
Trond Myklebusta301b772007-02-06 11:07:15 -0800160/* A writeback failed: mark the page as bad, and invalidate the page cache */
161static void nfs_set_pageerror(struct page *page)
162{
163 SetPageError(page);
164 nfs_zap_mapping(page->mapping->host, page->mapping);
165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/* We can set the PG_uptodate flag if we see that a write request
168 * covers the full page.
169 */
170static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
171{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (PageUptodate(page))
173 return;
174 if (base != 0)
175 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500176 if (count != nfs_page_length(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500178 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Trond Myklebuste21195a2006-12-05 00:35:39 -0500181static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 unsigned int offset, unsigned int count)
183{
184 struct nfs_page *req;
Trond Myklebuste21195a2006-12-05 00:35:39 -0500185 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Trond Myklebuste21195a2006-12-05 00:35:39 -0500187 for (;;) {
188 req = nfs_update_request(ctx, page, offset, count);
189 if (!IS_ERR(req))
190 break;
191 ret = PTR_ERR(req);
192 if (ret != -EBUSY)
193 return ret;
194 ret = nfs_wb_page(page->mapping->host, page);
195 if (ret != 0)
196 return ret;
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 /* Update file length */
199 nfs_grow_file(page, offset, count);
Trond Myklebustacee4782008-01-22 17:13:07 -0500200 nfs_clear_page_tag_locked(req);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
204static int wb_priority(struct writeback_control *wbc)
205{
206 if (wbc->for_reclaim)
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400207 return FLUSH_HIGHPRI | FLUSH_STABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (wbc->for_kupdate)
209 return FLUSH_LOWPRI;
210 return 0;
211}
212
213/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800214 * NFS congestion control
215 */
216
217int nfs_congestion_kb;
218
219#define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
220#define NFS_CONGESTION_OFF_THRESH \
221 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
222
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400223static int nfs_set_page_writeback(struct page *page)
Peter Zijlstra89a09142007-03-16 13:38:26 -0800224{
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400225 int ret = test_set_page_writeback(page);
226
227 if (!ret) {
Peter Zijlstra89a09142007-03-16 13:38:26 -0800228 struct inode *inode = page->mapping->host;
229 struct nfs_server *nfss = NFS_SERVER(inode);
230
Peter Zijlstra277866a2007-05-08 00:35:12 -0700231 if (atomic_long_inc_return(&nfss->writeback) >
Peter Zijlstra89a09142007-03-16 13:38:26 -0800232 NFS_CONGESTION_ON_THRESH)
233 set_bdi_congested(&nfss->backing_dev_info, WRITE);
234 }
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400235 return ret;
Peter Zijlstra89a09142007-03-16 13:38:26 -0800236}
237
238static void nfs_end_page_writeback(struct page *page)
239{
240 struct inode *inode = page->mapping->host;
241 struct nfs_server *nfss = NFS_SERVER(inode);
242
243 end_page_writeback(page);
Peter Zijlstrac4dc4be2007-10-16 23:25:41 -0700244 if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
Peter Zijlstra89a09142007-03-16 13:38:26 -0800245 clear_bdi_congested(&nfss->backing_dev_info, WRITE);
Peter Zijlstra89a09142007-03-16 13:38:26 -0800246}
247
248/*
Trond Myklebuste261f512006-12-05 00:35:41 -0500249 * Find an associated nfs write request, and prepare to flush it out
Trond Myklebust9cccef92007-07-22 17:09:05 -0400250 * May return an error if the user signalled nfs_wait_on_request().
Trond Myklebuste261f512006-12-05 00:35:41 -0500251 */
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400252static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
253 struct page *page)
Trond Myklebuste261f512006-12-05 00:35:41 -0500254{
Trond Myklebust587142f2007-07-02 09:57:54 -0400255 struct inode *inode = page->mapping->host;
Trond Myklebuste261f512006-12-05 00:35:41 -0500256 struct nfs_page *req;
Trond Myklebuste261f512006-12-05 00:35:41 -0500257 int ret;
258
Trond Myklebust587142f2007-07-02 09:57:54 -0400259 spin_lock(&inode->i_lock);
Trond Myklebuste261f512006-12-05 00:35:41 -0500260 for(;;) {
261 req = nfs_page_find_request_locked(page);
262 if (req == NULL) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400263 spin_unlock(&inode->i_lock);
Trond Myklebust9cccef92007-07-22 17:09:05 -0400264 return 0;
Trond Myklebuste261f512006-12-05 00:35:41 -0500265 }
Trond Myklebustacee4782008-01-22 17:13:07 -0500266 if (nfs_set_page_tag_locked(req))
Trond Myklebuste261f512006-12-05 00:35:41 -0500267 break;
268 /* Note: If we hold the page lock, as is the case in nfs_writepage,
Trond Myklebustacee4782008-01-22 17:13:07 -0500269 * then the call to nfs_set_page_tag_locked() will always
Trond Myklebuste261f512006-12-05 00:35:41 -0500270 * succeed provided that someone hasn't already marked the
271 * request as dirty (in which case we don't care).
272 */
Trond Myklebust587142f2007-07-02 09:57:54 -0400273 spin_unlock(&inode->i_lock);
Trond Myklebuste261f512006-12-05 00:35:41 -0500274 ret = nfs_wait_on_request(req);
275 nfs_release_request(req);
276 if (ret != 0)
277 return ret;
Trond Myklebust587142f2007-07-02 09:57:54 -0400278 spin_lock(&inode->i_lock);
Trond Myklebuste261f512006-12-05 00:35:41 -0500279 }
Trond Myklebust612c9382007-04-20 16:12:45 -0400280 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
281 /* This request is marked for commit */
Trond Myklebust587142f2007-07-02 09:57:54 -0400282 spin_unlock(&inode->i_lock);
Trond Myklebustacee4782008-01-22 17:13:07 -0500283 nfs_clear_page_tag_locked(req);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400284 nfs_pageio_complete(pgio);
Trond Myklebust9cccef92007-07-22 17:09:05 -0400285 return 0;
Trond Myklebust612c9382007-04-20 16:12:45 -0400286 }
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400287 if (nfs_set_page_writeback(page) != 0) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400288 spin_unlock(&inode->i_lock);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400289 BUG();
290 }
Trond Myklebust587142f2007-07-02 09:57:54 -0400291 spin_unlock(&inode->i_lock);
Fred Isamanf8512ad2008-03-19 11:24:39 -0400292 if (!nfs_pageio_add_request(pgio, req)) {
293 nfs_redirty_request(req);
294 nfs_end_page_writeback(page);
295 nfs_clear_page_tag_locked(req);
296 return pgio->pg_error;
297 }
Trond Myklebust9cccef92007-07-22 17:09:05 -0400298 return 0;
Trond Myklebuste261f512006-12-05 00:35:41 -0500299}
300
Trond Myklebustf758c8852007-07-22 19:27:32 -0400301static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
302{
303 struct inode *inode = page->mapping->host;
304
305 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
306 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
307
308 nfs_pageio_cond_complete(pgio, page->index);
309 return nfs_page_async_flush(pgio, page);
310}
311
Trond Myklebuste261f512006-12-05 00:35:41 -0500312/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * Write an mmapped page to the server.
314 */
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500315static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Trond Myklebustf758c8852007-07-22 19:27:32 -0400317 struct nfs_pageio_descriptor pgio;
Trond Myklebuste261f512006-12-05 00:35:41 -0500318 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Trond Myklebustf758c8852007-07-22 19:27:32 -0400320 nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
321 err = nfs_do_writepage(page, wbc, &pgio);
322 nfs_pageio_complete(&pgio);
323 if (err < 0)
324 return err;
325 if (pgio.pg_error < 0)
326 return pgio.pg_error;
327 return 0;
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500328}
329
330int nfs_writepage(struct page *page, struct writeback_control *wbc)
331{
Trond Myklebustf758c8852007-07-22 19:27:32 -0400332 int ret;
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500333
Trond Myklebustf758c8852007-07-22 19:27:32 -0400334 ret = nfs_writepage_locked(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 unlock_page(page);
Trond Myklebustf758c8852007-07-22 19:27:32 -0400336 return ret;
337}
338
339static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
340{
341 int ret;
342
343 ret = nfs_do_writepage(page, wbc, data);
344 unlock_page(page);
345 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
349{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct inode *inode = mapping->host;
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400351 struct nfs_pageio_descriptor pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 int err;
353
Chuck Lever91d5b472006-03-20 13:44:14 -0500354 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
355
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400356 nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
Trond Myklebustf758c8852007-07-22 19:27:32 -0400357 err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400358 nfs_pageio_complete(&pgio);
Trond Myklebustf758c8852007-07-22 19:27:32 -0400359 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return err;
Trond Myklebustf758c8852007-07-22 19:27:32 -0400361 if (pgio.pg_error < 0)
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400362 return pgio.pg_error;
363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
366/*
367 * Insert a write request into an inode
368 */
369static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
370{
371 struct nfs_inode *nfsi = NFS_I(inode);
372 int error;
373
374 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
375 BUG_ON(error == -EEXIST);
376 if (error)
377 return error;
378 if (!nfsi->npages) {
379 igrab(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (nfs_have_delegation(inode, FMODE_WRITE))
381 nfsi->change_attr++;
382 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500383 SetPagePrivate(req->wb_page);
Trond Myklebust277459d2006-12-05 00:35:35 -0500384 set_page_private(req->wb_page, (unsigned long)req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 nfsi->npages++;
Trond Myklebustc03b4022007-06-17 13:26:38 -0400386 kref_get(&req->wb_kref);
Trond Myklebustacee4782008-01-22 17:13:07 -0500387 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_LOCKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return 0;
389}
390
391/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800392 * Remove a write request from an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 */
394static void nfs_inode_remove_request(struct nfs_page *req)
395{
Trond Myklebust88be9f92007-06-05 10:42:27 -0400396 struct inode *inode = req->wb_context->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 struct nfs_inode *nfsi = NFS_I(inode);
398
399 BUG_ON (!NFS_WBACK_BUSY(req));
400
Trond Myklebust587142f2007-07-02 09:57:54 -0400401 spin_lock(&inode->i_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500402 set_page_private(req->wb_page, 0);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500403 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
405 nfsi->npages--;
406 if (!nfsi->npages) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400407 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 iput(inode);
409 } else
Trond Myklebust587142f2007-07-02 09:57:54 -0400410 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 nfs_clear_request(req);
412 nfs_release_request(req);
413}
414
Trond Myklebust61822ab2006-12-05 00:35:42 -0500415static void
416nfs_redirty_request(struct nfs_page *req)
417{
Trond Myklebust61822ab2006-12-05 00:35:42 -0500418 __set_page_dirty_nobuffers(req->wb_page);
419}
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421/*
422 * Check if a request is dirty
423 */
424static inline int
425nfs_dirty_request(struct nfs_page *req)
426{
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400427 struct page *page = req->wb_page;
428
Trond Myklebust612c9382007-04-20 16:12:45 -0400429 if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags))
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400430 return 0;
431 return !PageWriteback(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
435/*
436 * Add a request to the inode's commit list.
437 */
438static void
439nfs_mark_request_commit(struct nfs_page *req)
440{
Trond Myklebust88be9f92007-06-05 10:42:27 -0400441 struct inode *inode = req->wb_context->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 struct nfs_inode *nfsi = NFS_I(inode);
443
Trond Myklebust587142f2007-07-02 09:57:54 -0400444 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 nfsi->ncommit++;
Trond Myklebust612c9382007-04-20 16:12:45 -0400446 set_bit(PG_NEED_COMMIT, &(req)->wb_flags);
Trond Myklebust5c369682007-06-17 15:27:42 -0400447 radix_tree_tag_set(&nfsi->nfs_page_tree,
448 req->wb_index,
449 NFS_PAGE_TAG_COMMIT);
Trond Myklebust587142f2007-07-02 09:57:54 -0400450 spin_unlock(&inode->i_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700451 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -0700452 inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
Trond Myklebusta1803042006-12-05 00:45:19 -0500453 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400455
456static inline
457int nfs_write_need_commit(struct nfs_write_data *data)
458{
459 return data->verf.committed != NFS_FILE_SYNC;
460}
461
462static inline
463int nfs_reschedule_unstable_write(struct nfs_page *req)
464{
Trond Myklebust612c9382007-04-20 16:12:45 -0400465 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400466 nfs_mark_request_commit(req);
467 return 1;
468 }
469 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
470 nfs_redirty_request(req);
471 return 1;
472 }
473 return 0;
474}
475#else
476static inline void
477nfs_mark_request_commit(struct nfs_page *req)
478{
479}
480
481static inline
482int nfs_write_need_commit(struct nfs_write_data *data)
483{
484 return 0;
485}
486
487static inline
488int nfs_reschedule_unstable_write(struct nfs_page *req)
489{
490 return 0;
491}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492#endif
493
494/*
495 * Wait for a request to complete.
496 *
Matthew Wilcox150030b2007-12-06 16:24:39 -0500497 * Interruptible by fatal signals only.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 */
Trond Myklebustca52fec2007-04-17 17:22:13 -0400499static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 struct nfs_inode *nfsi = NFS_I(inode);
502 struct nfs_page *req;
Trond Myklebustca52fec2007-04-17 17:22:13 -0400503 pgoff_t idx_end, next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 unsigned int res = 0;
505 int error;
506
507 if (npages == 0)
508 idx_end = ~0;
509 else
510 idx_end = idx_start + npages - 1;
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 next = idx_start;
Trond Myklebust9fd367f2007-06-17 15:10:24 -0400513 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 -0700514 if (req->wb_index > idx_end)
515 break;
516
517 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000518 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Trond Myklebustc03b4022007-06-17 13:26:38 -0400520 kref_get(&req->wb_kref);
Trond Myklebust587142f2007-07-02 09:57:54 -0400521 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 error = nfs_wait_on_request(req);
523 nfs_release_request(req);
Trond Myklebust587142f2007-07-02 09:57:54 -0400524 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (error < 0)
526 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 res++;
528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return res;
530}
531
Trond Myklebust83715ad2006-07-05 13:17:12 -0400532static void nfs_cancel_commit_list(struct list_head *head)
533{
534 struct nfs_page *req;
535
536 while(!list_empty(head)) {
537 req = nfs_list_entry(head->next);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700538 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -0700539 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
540 BDI_RECLAIMABLE);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400541 nfs_list_remove_request(req);
Trond Myklebust612c9382007-04-20 16:12:45 -0400542 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400543 nfs_inode_remove_request(req);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700544 nfs_unlock_request(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400545 }
546}
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
549/*
550 * nfs_scan_commit - Scan an inode for commit requests
551 * @inode: NFS inode to scan
552 * @dst: destination list
553 * @idx_start: lower bound of page->index to scan.
554 * @npages: idx_start + npages sets the upper bound to scan.
555 *
556 * Moves requests from the inode's 'commit' request list.
557 * The requests are *not* checked to ensure that they form a contiguous set.
558 */
559static int
Trond Myklebustca52fec2007-04-17 17:22:13 -0400560nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000563 int res = 0;
564
565 if (nfsi->ncommit != 0) {
Trond Myklebust5c369682007-06-17 15:27:42 -0400566 res = nfs_scan_list(nfsi, dst, idx_start, npages,
567 NFS_PAGE_TAG_COMMIT);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000568 nfsi->ncommit -= res;
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return res;
571}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500572#else
Trond Myklebustca52fec2007-04-17 17:22:13 -0400573static 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 -0500574{
575 return 0;
576}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577#endif
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579/*
580 * Try to update any existing write request, or create one if there is none.
581 * In order to match, the request's credentials must match those of
582 * the calling process.
583 *
584 * Note: Should always be called with the Page Lock held!
585 */
586static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
Trond Myklebuste21195a2006-12-05 00:35:39 -0500587 struct page *page, unsigned int offset, unsigned int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Peter Zijlstra89a09142007-03-16 13:38:26 -0800589 struct address_space *mapping = page->mapping;
590 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 struct nfs_page *req, *new = NULL;
Trond Myklebustca52fec2007-04-17 17:22:13 -0400592 pgoff_t rqend, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 end = offset + bytes;
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 for (;;) {
597 /* Loop over all inode entries and see if we find
598 * A request for the page we wish to update
599 */
Trond Myklebust587142f2007-07-02 09:57:54 -0400600 spin_lock(&inode->i_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500601 req = nfs_page_find_request_locked(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (req) {
Trond Myklebustacee4782008-01-22 17:13:07 -0500603 if (!nfs_set_page_tag_locked(req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 int error;
Trond Myklebust277459d2006-12-05 00:35:35 -0500605
Trond Myklebust587142f2007-07-02 09:57:54 -0400606 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 error = nfs_wait_on_request(req);
608 nfs_release_request(req);
Neil Brown1dd594b2006-03-20 13:44:04 -0500609 if (error < 0) {
610 if (new)
611 nfs_release_request(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return ERR_PTR(error);
Neil Brown1dd594b2006-03-20 13:44:04 -0500613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 continue;
615 }
Trond Myklebust587142f2007-07-02 09:57:54 -0400616 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (new)
618 nfs_release_request(new);
619 break;
620 }
621
622 if (new) {
623 int error;
624 nfs_lock_request_dontget(new);
625 error = nfs_inode_add_request(inode, new);
626 if (error) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400627 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 nfs_unlock_request(new);
629 return ERR_PTR(error);
630 }
Trond Myklebust587142f2007-07-02 09:57:54 -0400631 spin_unlock(&inode->i_lock);
Trond Myklebust61e930a2007-10-18 17:08:05 -0400632 req = new;
633 goto zero_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
Trond Myklebust587142f2007-07-02 09:57:54 -0400635 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 new = nfs_create_request(ctx, inode, page, offset, bytes);
638 if (IS_ERR(new))
639 return new;
640 }
641
642 /* We have a request for our page.
643 * If the creds don't match, or the
644 * page addresses don't match,
645 * tell the caller to wait on the conflicting
646 * request.
647 */
648 rqend = req->wb_offset + req->wb_bytes;
649 if (req->wb_context != ctx
650 || req->wb_page != page
651 || !nfs_dirty_request(req)
652 || offset > rqend || end < req->wb_offset) {
Trond Myklebustacee4782008-01-22 17:13:07 -0500653 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return ERR_PTR(-EBUSY);
655 }
656
657 /* Okay, the request matches. Update the region */
658 if (offset < req->wb_offset) {
659 req->wb_offset = offset;
660 req->wb_pgbase = offset;
Trond Myklebust61e930a2007-10-18 17:08:05 -0400661 req->wb_bytes = max(end, rqend) - req->wb_offset;
662 goto zero_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664
665 if (end > rqend)
666 req->wb_bytes = end - req->wb_offset;
667
668 return req;
Trond Myklebust61e930a2007-10-18 17:08:05 -0400669zero_page:
670 /* If this page might potentially be marked as up to date,
671 * then we need to zero any uninitalised data. */
672 if (req->wb_pgbase == 0 && req->wb_bytes != PAGE_CACHE_SIZE
673 && !PageUptodate(req->wb_page))
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800674 zero_user_segment(req->wb_page, req->wb_bytes, PAGE_CACHE_SIZE);
Trond Myklebust61e930a2007-10-18 17:08:05 -0400675 return req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
678int nfs_flush_incompatible(struct file *file, struct page *page)
679{
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400680 struct nfs_open_context *ctx = nfs_file_open_context(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 struct nfs_page *req;
Trond Myklebust1a545332006-12-05 00:35:40 -0500682 int do_flush, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 /*
684 * Look for a request corresponding to this page. If there
685 * is one, and it belongs to another file, we flush it out
686 * before we try to copy anything into the page. Do this
687 * due to the lack of an ACCESS-type call in NFSv2.
688 * Also do the same if we find a request from an existing
689 * dropped page.
690 */
Trond Myklebust1a545332006-12-05 00:35:40 -0500691 do {
692 req = nfs_page_find_request(page);
693 if (req == NULL)
694 return 0;
695 do_flush = req->wb_page != page || req->wb_context != ctx
Trond Myklebuste261f512006-12-05 00:35:41 -0500696 || !nfs_dirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 nfs_release_request(req);
Trond Myklebust1a545332006-12-05 00:35:40 -0500698 if (!do_flush)
699 return 0;
700 status = nfs_wb_page(page->mapping->host, page);
701 } while (status == 0);
702 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
705/*
Trond Myklebust5d47a352008-02-07 17:24:07 -0500706 * If the page cache is marked as unsafe or invalid, then we can't rely on
707 * the PageUptodate() flag. In this case, we will need to turn off
708 * write optimisations that depend on the page contents being correct.
709 */
710static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
711{
712 return PageUptodate(page) &&
713 !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
714}
715
716/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 * Update and possibly write a cached page of an NFS file.
718 *
719 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
720 * things with a page scheduled for an RPC call (e.g. invalidate it).
721 */
722int nfs_updatepage(struct file *file, struct page *page,
723 unsigned int offset, unsigned int count)
724{
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400725 struct nfs_open_context *ctx = nfs_file_open_context(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 int status = 0;
728
Chuck Lever91d5b472006-03-20 13:44:14 -0500729 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800732 file->f_path.dentry->d_parent->d_name.name,
733 file->f_path.dentry->d_name.name, count,
Chuck Lever0bbacc42005-11-01 16:53:32 -0500734 (long long)(page_offset(page) +offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 /* If we're not using byte range locks, and we know the page
Trond Myklebust5d47a352008-02-07 17:24:07 -0500737 * is up to date, it may be more efficient to extend the write
738 * to cover the entire page in order to avoid fragmentation
739 * inefficiencies.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 */
Trond Myklebust5d47a352008-02-07 17:24:07 -0500741 if (nfs_write_pageuptodate(page, inode) &&
742 inode->i_flock == NULL &&
Trond Myklebustaf1b8c22008-02-25 15:56:29 -0800743 !(file->f_flags & O_SYNC)) {
Trond Myklebust49a70f22006-12-05 00:35:38 -0500744 count = max(count + offset, nfs_page_length(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747
Trond Myklebuste21195a2006-12-05 00:35:39 -0500748 status = nfs_writepage_setup(ctx, page, offset, count);
Trond Myklebuste261f512006-12-05 00:35:41 -0500749 __set_page_dirty_nobuffers(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
752 status, (long long)i_size_read(inode));
753 if (status < 0)
Trond Myklebusta301b772007-02-06 11:07:15 -0800754 nfs_set_pageerror(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return status;
756}
757
758static void nfs_writepage_release(struct nfs_page *req)
759{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Trond Myklebust44dd1512007-05-19 11:58:03 -0400761 if (PageError(req->wb_page)) {
762 nfs_end_page_writeback(req->wb_page);
763 nfs_inode_remove_request(req);
764 } else if (!nfs_reschedule_unstable_write(req)) {
765 /* Set the PG_uptodate flag */
766 nfs_mark_uptodate(req->wb_page, req->wb_pgbase, req->wb_bytes);
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400767 nfs_end_page_writeback(req->wb_page);
768 nfs_inode_remove_request(req);
769 } else
770 nfs_end_page_writeback(req->wb_page);
Trond Myklebust9fd367f2007-06-17 15:10:24 -0400771 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
Trond Myklebust3ff75762007-07-14 15:40:00 -0400774static int flush_task_priority(int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
777 case FLUSH_HIGHPRI:
778 return RPC_PRIORITY_HIGH;
779 case FLUSH_LOWPRI:
780 return RPC_PRIORITY_LOW;
781 }
782 return RPC_PRIORITY_NORMAL;
783}
784
785/*
786 * Set up the argument/result storage required for the RPC call.
787 */
788static void nfs_write_rpcsetup(struct nfs_page *req,
789 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500790 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 unsigned int count, unsigned int offset,
792 int how)
793{
Trond Myklebust84115e12007-07-14 15:39:59 -0400794 struct inode *inode = req->wb_context->path.dentry->d_inode;
795 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
Trond Myklebust3ff75762007-07-14 15:40:00 -0400796 int priority = flush_task_priority(how);
Trond Myklebust07737692007-10-25 18:42:54 -0400797 struct rpc_task *task;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400798 struct rpc_message msg = {
799 .rpc_argp = &data->args,
800 .rpc_resp = &data->res,
801 .rpc_cred = req->wb_context->cred,
802 };
Trond Myklebust84115e12007-07-14 15:39:59 -0400803 struct rpc_task_setup task_setup_data = {
804 .rpc_client = NFS_CLIENT(inode),
Trond Myklebust07737692007-10-25 18:42:54 -0400805 .task = &data->task,
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400806 .rpc_message = &msg,
Trond Myklebust84115e12007-07-14 15:39:59 -0400807 .callback_ops = call_ops,
808 .callback_data = data,
809 .flags = flags,
Trond Myklebust3ff75762007-07-14 15:40:00 -0400810 .priority = priority,
Trond Myklebust84115e12007-07-14 15:39:59 -0400811 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 /* Set up the RPC argument and reply structs
814 * NB: take care not to mess about with data->commit et al. */
815
816 data->req = req;
Trond Myklebust88be9f92007-06-05 10:42:27 -0400817 data->inode = inode = req->wb_context->path.dentry->d_inode;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400818 data->cred = msg.rpc_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 data->args.fh = NFS_FH(inode);
821 data->args.offset = req_offset(req) + offset;
822 data->args.pgbase = req->wb_pgbase + offset;
823 data->args.pages = data->pagevec;
824 data->args.count = count;
825 data->args.context = req->wb_context;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400826 data->args.stable = NFS_UNSTABLE;
827 if (how & FLUSH_STABLE) {
828 data->args.stable = NFS_DATA_SYNC;
829 if (!NFS_I(inode)->ncommit)
830 data->args.stable = NFS_FILE_SYNC;
831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 data->res.fattr = &data->fattr;
834 data->res.count = count;
835 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400836 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Trond Myklebust788e7a82006-03-20 13:44:27 -0500838 /* Set up the initial task struct. */
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400839 NFS_PROTO(inode)->write_setup(data, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Chuck Levera3f565b2007-01-31 12:14:01 -0500841 dprintk("NFS: %5u initiated write call "
842 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500843 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 inode->i_sb->s_id,
845 (long long)NFS_FILEID(inode),
846 count,
847 (unsigned long long)data->args.offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Trond Myklebust07737692007-10-25 18:42:54 -0400849 task = rpc_run_task(&task_setup_data);
850 if (!IS_ERR(task))
851 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
854/*
855 * Generate multiple small requests to write out a single
856 * contiguous dirty area on one page.
857 */
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400858static 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 -0700859{
860 struct nfs_page *req = nfs_list_entry(head->next);
861 struct page *page = req->wb_page;
862 struct nfs_write_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700863 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
864 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 int requests = 0;
866 LIST_HEAD(list);
867
868 nfs_list_remove_request(req);
869
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400870 nbytes = count;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700871 do {
872 size_t len = min(nbytes, wsize);
873
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400874 data = nfs_writedata_alloc(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (!data)
876 goto out_bad;
877 list_add(&data->pages, &list);
878 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700879 nbytes -= len;
880 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 atomic_set(&req->wb_complete, requests);
882
883 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 offset = 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400885 nbytes = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 do {
887 data = list_entry(list.next, struct nfs_write_data, pages);
888 list_del_init(&data->pages);
889
890 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400892 if (nbytes < wsize)
893 wsize = nbytes;
894 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
895 wsize, offset, how);
896 offset += wsize;
897 nbytes -= wsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 } while (nbytes != 0);
899
900 return 0;
901
902out_bad:
903 while (!list_empty(&list)) {
904 data = list_entry(list.next, struct nfs_write_data, pages);
905 list_del(&data->pages);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500906 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
Trond Myklebust61822ab2006-12-05 00:35:42 -0500908 nfs_redirty_request(req);
Trond Myklebust6d677e32007-04-20 16:12:40 -0400909 nfs_end_page_writeback(req->wb_page);
Trond Myklebust9fd367f2007-06-17 15:10:24 -0400910 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return -ENOMEM;
912}
913
914/*
915 * Create an RPC task for the given write request and kick it.
916 * The page must have been locked by the caller.
917 *
918 * It may happen that the page we're passed is not marked dirty.
919 * This is the case if nfs_updatepage detects a conflicting request
920 * that has been written but not committed.
921 */
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400922static 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 -0700923{
924 struct nfs_page *req;
925 struct page **pages;
926 struct nfs_write_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400928 data = nfs_writedata_alloc(npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (!data)
930 goto out_bad;
931
932 pages = data->pagevec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 while (!list_empty(head)) {
934 req = nfs_list_entry(head->next);
935 nfs_list_remove_request(req);
936 nfs_list_add_request(req, &data->pages);
937 ClearPageError(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 *pages++ = req->wb_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940 req = nfs_list_entry(data->pages.next);
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 /* Set up the argument struct */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500943 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return 0;
946 out_bad:
947 while (!list_empty(head)) {
Trond Myklebust10afec92007-05-14 17:16:04 -0400948 req = nfs_list_entry(head->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 nfs_list_remove_request(req);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500950 nfs_redirty_request(req);
Trond Myklebust6d677e32007-04-20 16:12:40 -0400951 nfs_end_page_writeback(req->wb_page);
Trond Myklebust9fd367f2007-06-17 15:10:24 -0400952 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
954 return -ENOMEM;
955}
956
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400957static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
958 struct inode *inode, int ioflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
Chuck Leverbf4285e2007-12-20 14:54:57 -0500960 size_t wsize = NFS_SERVER(inode)->wsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400962 if (wsize < PAGE_CACHE_SIZE)
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400963 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400964 else
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400965 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
968/*
969 * Handle a write reply that flushed part of a page.
970 */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500971static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Trond Myklebust788e7a82006-03-20 13:44:27 -0500973 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 struct nfs_page *req = data->req;
975 struct page *page = req->wb_page;
976
977 dprintk("NFS: write (%s/%Ld %d@%Ld)",
Trond Myklebust88be9f92007-06-05 10:42:27 -0400978 req->wb_context->path.dentry->d_inode->i_sb->s_id,
979 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 req->wb_bytes,
981 (long long)req_offset(req));
982
Trond Myklebust788e7a82006-03-20 13:44:27 -0500983 if (nfs_writeback_done(task, data) != 0)
984 return;
985
986 if (task->tk_status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -0800987 nfs_set_pageerror(page);
Trond Myklebust7b159fc2007-07-25 14:09:54 -0400988 nfs_context_set_write_error(req->wb_context, task->tk_status);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500989 dprintk(", error = %d\n", task->tk_status);
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400990 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400993 if (nfs_write_need_commit(data)) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400994 struct inode *inode = page->mapping->host;
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400995
Trond Myklebust587142f2007-07-02 09:57:54 -0400996 spin_lock(&inode->i_lock);
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400997 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
998 /* Do nothing we need to resend the writes */
999 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1000 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1001 dprintk(" defer commit\n");
1002 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1003 set_bit(PG_NEED_RESCHED, &req->wb_flags);
1004 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
1005 dprintk(" server reboot detected\n");
1006 }
Trond Myklebust587142f2007-07-02 09:57:54 -04001007 spin_unlock(&inode->i_lock);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001008 } else
1009 dprintk(" OK\n");
1010
1011out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (atomic_dec_and_test(&req->wb_complete))
1013 nfs_writepage_release(req);
1014}
1015
Trond Myklebust788e7a82006-03-20 13:44:27 -05001016static const struct rpc_call_ops nfs_write_partial_ops = {
1017 .rpc_call_done = nfs_writeback_done_partial,
1018 .rpc_release = nfs_writedata_release,
1019};
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021/*
1022 * Handle a write reply that flushes a whole page.
1023 *
1024 * FIXME: There is an inherent race with invalidate_inode_pages and
1025 * writebacks since the page->count is kept > 1 for as long
1026 * as the page has a write request pending.
1027 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001028static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001030 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 struct nfs_page *req;
1032 struct page *page;
1033
Trond Myklebust788e7a82006-03-20 13:44:27 -05001034 if (nfs_writeback_done(task, data) != 0)
1035 return;
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 /* Update attributes as result of writeback. */
1038 while (!list_empty(&data->pages)) {
1039 req = nfs_list_entry(data->pages.next);
1040 nfs_list_remove_request(req);
1041 page = req->wb_page;
1042
1043 dprintk("NFS: write (%s/%Ld %d@%Ld)",
Trond Myklebust88be9f92007-06-05 10:42:27 -04001044 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1045 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 req->wb_bytes,
1047 (long long)req_offset(req));
1048
Trond Myklebust788e7a82006-03-20 13:44:27 -05001049 if (task->tk_status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -08001050 nfs_set_pageerror(page);
Trond Myklebust7b159fc2007-07-25 14:09:54 -04001051 nfs_context_set_write_error(req->wb_context, task->tk_status);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001052 dprintk(", error = %d\n", task->tk_status);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001053 goto remove_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001056 if (nfs_write_need_commit(data)) {
1057 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1058 nfs_mark_request_commit(req);
1059 nfs_end_page_writeback(page);
1060 dprintk(" marked for commit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 goto next;
1062 }
Trond Myklebust44dd1512007-05-19 11:58:03 -04001063 /* Set the PG_uptodate flag? */
1064 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001065 dprintk(" OK\n");
1066remove_request:
1067 nfs_end_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 nfs_inode_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 next:
Trond Myklebust9fd367f2007-06-17 15:10:24 -04001070 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072}
1073
Trond Myklebust788e7a82006-03-20 13:44:27 -05001074static const struct rpc_call_ops nfs_write_full_ops = {
1075 .rpc_call_done = nfs_writeback_done_full,
1076 .rpc_release = nfs_writedata_release,
1077};
1078
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080/*
1081 * This function is called when the WRITE call is complete.
1082 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001083int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 struct nfs_writeargs *argp = &data->args;
1086 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001087 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Chuck Levera3f565b2007-01-31 12:14:01 -05001089 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 task->tk_pid, task->tk_status);
1091
Chuck Leverf551e442006-09-20 14:33:04 -04001092 /*
1093 * ->write_done will attempt to use post-op attributes to detect
1094 * conflicting writes by other clients. A strict interpretation
1095 * of close-to-open would allow us to continue caching even if
1096 * another writer had changed the file, but some applications
1097 * depend on tighter cache coherency when writing.
1098 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001099 status = NFS_PROTO(data->inode)->write_done(task, data);
1100 if (status != 0)
1101 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001102 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1105 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1106 /* We tried a write call, but the server did not
1107 * commit data to stable storage even though we
1108 * requested it.
1109 * Note: There is a known bug in Tru64 < 5.0 in which
1110 * the server reports NFS_DATA_SYNC, but performs
1111 * NFS_FILE_SYNC. We therefore implement this checking
1112 * as a dprintk() in order to avoid filling syslog.
1113 */
1114 static unsigned long complain;
1115
1116 if (time_before(complain, jiffies)) {
1117 dprintk("NFS: faulty NFS server %s:"
1118 " (committed = %d) != (stable = %d)\n",
David Howells54ceac42006-08-22 20:06:13 -04001119 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 resp->verf->committed, argp->stable);
1121 complain = jiffies + 300 * HZ;
1122 }
1123 }
1124#endif
1125 /* Is this a short write? */
1126 if (task->tk_status >= 0 && resp->count < argp->count) {
1127 static unsigned long complain;
1128
Chuck Lever91d5b472006-03-20 13:44:14 -05001129 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 /* Has the server at least made some progress? */
1132 if (resp->count != 0) {
1133 /* Was this an NFSv2 write or an NFSv3 stable write? */
1134 if (resp->verf->committed != NFS_UNSTABLE) {
1135 /* Resend from where the server left off */
1136 argp->offset += resp->count;
1137 argp->pgbase += resp->count;
1138 argp->count -= resp->count;
1139 } else {
1140 /* Resend as a stable write in order to avoid
1141 * headaches in the case of a server crash.
1142 */
1143 argp->stable = NFS_FILE_SYNC;
1144 }
1145 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001146 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148 if (time_before(complain, jiffies)) {
1149 printk(KERN_WARNING
1150 "NFS: Server wrote zero bytes, expected %u.\n",
1151 argp->count);
1152 complain = jiffies + 300 * HZ;
1153 }
1154 /* Can't do anything about it except throw an error. */
1155 task->tk_status = -EIO;
1156 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001157 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158}
1159
1160
1161#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001162void nfs_commit_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 nfs_commit_free(wdata);
1165}
1166
1167/*
1168 * Set up the argument/result storage required for the RPC call.
1169 */
1170static void nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001171 struct nfs_write_data *data,
1172 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Trond Myklebust84115e12007-07-14 15:39:59 -04001174 struct nfs_page *first = nfs_list_entry(head->next);
1175 struct inode *inode = first->wb_context->path.dentry->d_inode;
1176 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
Trond Myklebust3ff75762007-07-14 15:40:00 -04001177 int priority = flush_task_priority(how);
Trond Myklebust07737692007-10-25 18:42:54 -04001178 struct rpc_task *task;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001179 struct rpc_message msg = {
1180 .rpc_argp = &data->args,
1181 .rpc_resp = &data->res,
1182 .rpc_cred = first->wb_context->cred,
1183 };
Trond Myklebust84115e12007-07-14 15:39:59 -04001184 struct rpc_task_setup task_setup_data = {
Trond Myklebust07737692007-10-25 18:42:54 -04001185 .task = &data->task,
Trond Myklebust84115e12007-07-14 15:39:59 -04001186 .rpc_client = NFS_CLIENT(inode),
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001187 .rpc_message = &msg,
Trond Myklebust84115e12007-07-14 15:39:59 -04001188 .callback_ops = &nfs_commit_ops,
1189 .callback_data = data,
1190 .flags = flags,
Trond Myklebust3ff75762007-07-14 15:40:00 -04001191 .priority = priority,
Trond Myklebust84115e12007-07-14 15:39:59 -04001192 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 /* Set up the RPC argument and reply structs
1195 * NB: take care not to mess about with data->commit et al. */
1196
1197 list_splice_init(head, &data->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 data->inode = inode;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001200 data->cred = msg.rpc_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001203 /* Note: we always request a commit of the entire inode */
1204 data->args.offset = 0;
1205 data->args.count = 0;
1206 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 data->res.fattr = &data->fattr;
1208 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001209 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001210
1211 /* Set up the initial task struct. */
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001212 NFS_PROTO(inode)->commit_setup(data, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Chuck Levera3f565b2007-01-31 12:14:01 -05001214 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001215
Trond Myklebust07737692007-10-25 18:42:54 -04001216 task = rpc_run_task(&task_setup_data);
1217 if (!IS_ERR(task))
1218 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
1221/*
1222 * Commit dirty pages
1223 */
1224static int
Chuck Lever40859d72005-11-30 18:09:02 -05001225nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
1227 struct nfs_write_data *data;
1228 struct nfs_page *req;
1229
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001230 data = nfs_commit_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 if (!data)
1233 goto out_bad;
1234
1235 /* Set up the argument struct */
1236 nfs_commit_rpcsetup(head, data, how);
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return 0;
1239 out_bad:
1240 while (!list_empty(head)) {
1241 req = nfs_list_entry(head->next);
1242 nfs_list_remove_request(req);
1243 nfs_mark_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001244 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -07001245 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1246 BDI_RECLAIMABLE);
Trond Myklebust9fd367f2007-06-17 15:10:24 -04001247 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249 return -ENOMEM;
1250}
1251
1252/*
1253 * COMMIT call returned
1254 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001255static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001257 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Chuck Levera3f565b2007-01-31 12:14:01 -05001260 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 task->tk_pid, task->tk_status);
1262
Trond Myklebust788e7a82006-03-20 13:44:27 -05001263 /* Call the NFS version-specific code */
1264 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1265 return;
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 while (!list_empty(&data->pages)) {
1268 req = nfs_list_entry(data->pages.next);
1269 nfs_list_remove_request(req);
Trond Myklebust612c9382007-04-20 16:12:45 -04001270 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
Christoph Lameterfd39fc82006-06-30 01:55:40 -07001271 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -07001272 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1273 BDI_RECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
Trond Myklebust88be9f92007-06-05 10:42:27 -04001276 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1277 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 req->wb_bytes,
1279 (long long)req_offset(req));
1280 if (task->tk_status < 0) {
Trond Myklebust7b159fc2007-07-25 14:09:54 -04001281 nfs_context_set_write_error(req->wb_context, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 nfs_inode_remove_request(req);
1283 dprintk(", error = %d\n", task->tk_status);
1284 goto next;
1285 }
1286
1287 /* Okay, COMMIT succeeded, apparently. Check the verifier
1288 * returned by the server against all stored verfs. */
1289 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1290 /* We have a match */
Trond Myklebust44dd1512007-05-19 11:58:03 -04001291 /* Set the PG_uptodate flag */
1292 nfs_mark_uptodate(req->wb_page, req->wb_pgbase,
1293 req->wb_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 nfs_inode_remove_request(req);
1295 dprintk(" OK\n");
1296 goto next;
1297 }
1298 /* We have a mismatch. Write the page again */
1299 dprintk(" mismatch\n");
Trond Myklebust61822ab2006-12-05 00:35:42 -05001300 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 next:
Trond Myklebust9fd367f2007-06-17 15:10:24 -04001302 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001305
1306static const struct rpc_call_ops nfs_commit_ops = {
1307 .rpc_call_done = nfs_commit_done,
1308 .rpc_release = nfs_commit_release,
1309};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001311int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001314 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Trond Myklebust587142f2007-07-02 09:57:54 -04001316 spin_lock(&inode->i_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001317 res = nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebust587142f2007-07-02 09:57:54 -04001318 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001320 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001321 if (error < 0)
1322 return error;
1323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return res;
1325}
Trond Myklebustc63c7b02007-04-02 19:29:52 -04001326#else
1327static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1328{
1329 return 0;
1330}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331#endif
1332
Trond Myklebust1c759502006-10-09 16:18:38 -04001333long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
Trond Myklebust1c759502006-10-09 16:18:38 -04001335 struct inode *inode = mapping->host;
Trond Myklebustca52fec2007-04-17 17:22:13 -04001336 pgoff_t idx_start, idx_end;
Trond Myklebust1c759502006-10-09 16:18:38 -04001337 unsigned int npages = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001338 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001339 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebust3f442542006-09-17 14:46:44 -04001340 long pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Trond Myklebust1c759502006-10-09 16:18:38 -04001342 /* FIXME */
1343 if (wbc->range_cyclic)
1344 idx_start = 0;
1345 else {
1346 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1347 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1348 if (idx_end > idx_start) {
Trond Myklebustca52fec2007-04-17 17:22:13 -04001349 pgoff_t l_npages = 1 + idx_end - idx_start;
Trond Myklebust1c759502006-10-09 16:18:38 -04001350 npages = l_npages;
1351 if (sizeof(npages) != sizeof(l_npages) &&
Trond Myklebustca52fec2007-04-17 17:22:13 -04001352 (pgoff_t)npages != l_npages)
Trond Myklebust1c759502006-10-09 16:18:38 -04001353 npages = 0;
1354 }
1355 }
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001356 how &= ~FLUSH_NOCOMMIT;
Trond Myklebust587142f2007-07-02 09:57:54 -04001357 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 do {
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001359 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1360 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001361 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001362 if (nocommit)
1363 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001364 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebust724c4392007-04-17 17:22:13 -04001365 if (pages == 0)
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001366 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001367 if (how & FLUSH_INVALIDATE) {
Trond Myklebust587142f2007-07-02 09:57:54 -04001368 spin_unlock(&inode->i_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001369 nfs_cancel_commit_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001370 ret = pages;
Trond Myklebust587142f2007-07-02 09:57:54 -04001371 spin_lock(&inode->i_lock);
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001372 continue;
1373 }
1374 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebust587142f2007-07-02 09:57:54 -04001375 spin_unlock(&inode->i_lock);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001376 ret = nfs_commit_list(inode, &head, how);
Trond Myklebust587142f2007-07-02 09:57:54 -04001377 spin_lock(&inode->i_lock);
1378
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001379 } while (ret >= 0);
Trond Myklebust587142f2007-07-02 09:57:54 -04001380 spin_unlock(&inode->i_lock);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001381 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
1383
Trond Myklebust34901f72007-07-25 14:09:54 -04001384static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
Trond Myklebust1c759502006-10-09 16:18:38 -04001385{
Trond Myklebust1c759502006-10-09 16:18:38 -04001386 int ret;
1387
Trond Myklebust34901f72007-07-25 14:09:54 -04001388 ret = nfs_writepages(mapping, wbc);
Trond Myklebust61822ab2006-12-05 00:35:42 -05001389 if (ret < 0)
1390 goto out;
Trond Myklebust34901f72007-07-25 14:09:54 -04001391 ret = nfs_sync_mapping_wait(mapping, wbc, how);
Trond Myklebusted90ef52007-07-20 13:13:28 -04001392 if (ret < 0)
1393 goto out;
1394 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001395out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001396 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Trond Myklebust1c759502006-10-09 16:18:38 -04001397 return ret;
1398}
1399
Trond Myklebust34901f72007-07-25 14:09:54 -04001400/* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
1401static int nfs_write_mapping(struct address_space *mapping, int how)
1402{
1403 struct writeback_control wbc = {
1404 .bdi = mapping->backing_dev_info,
1405 .sync_mode = WB_SYNC_NONE,
1406 .nr_to_write = LONG_MAX,
1407 .for_writepages = 1,
1408 .range_cyclic = 1,
1409 };
1410 int ret;
1411
1412 ret = __nfs_write_mapping(mapping, &wbc, how);
1413 if (ret < 0)
1414 return ret;
1415 wbc.sync_mode = WB_SYNC_ALL;
1416 return __nfs_write_mapping(mapping, &wbc, how);
1417}
1418
Trond Myklebusted90ef52007-07-20 13:13:28 -04001419/*
1420 * flush the inode to disk.
1421 */
1422int nfs_wb_all(struct inode *inode)
Trond Myklebust1c759502006-10-09 16:18:38 -04001423{
Trond Myklebusted90ef52007-07-20 13:13:28 -04001424 return nfs_write_mapping(inode->i_mapping, 0);
1425}
Trond Myklebust1c759502006-10-09 16:18:38 -04001426
Trond Myklebusted90ef52007-07-20 13:13:28 -04001427int nfs_wb_nocommit(struct inode *inode)
1428{
1429 return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
Trond Myklebust1c759502006-10-09 16:18:38 -04001430}
1431
Trond Myklebust1b3b4a12007-08-28 10:29:36 -04001432int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1433{
1434 struct nfs_page *req;
1435 loff_t range_start = page_offset(page);
1436 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1437 struct writeback_control wbc = {
1438 .bdi = page->mapping->backing_dev_info,
1439 .sync_mode = WB_SYNC_ALL,
1440 .nr_to_write = LONG_MAX,
1441 .range_start = range_start,
1442 .range_end = range_end,
1443 };
1444 int ret = 0;
1445
1446 BUG_ON(!PageLocked(page));
1447 for (;;) {
1448 req = nfs_page_find_request(page);
1449 if (req == NULL)
1450 goto out;
1451 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1452 nfs_release_request(req);
1453 break;
1454 }
1455 if (nfs_lock_request_dontget(req)) {
1456 nfs_inode_remove_request(req);
1457 /*
1458 * In case nfs_inode_remove_request has marked the
1459 * page as being dirty
1460 */
1461 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1462 nfs_unlock_request(req);
1463 break;
1464 }
1465 ret = nfs_wait_on_request(req);
1466 if (ret < 0)
1467 goto out;
1468 }
1469 if (!PagePrivate(page))
1470 return 0;
1471 ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
1472out:
1473 return ret;
1474}
1475
Adrian Bunk5334eb12007-11-21 15:04:31 -08001476static int nfs_wb_page_priority(struct inode *inode, struct page *page,
1477 int how)
Trond Myklebust1c759502006-10-09 16:18:38 -04001478{
1479 loff_t range_start = page_offset(page);
1480 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001481 struct writeback_control wbc = {
1482 .bdi = page->mapping->backing_dev_info,
1483 .sync_mode = WB_SYNC_ALL,
1484 .nr_to_write = LONG_MAX,
1485 .range_start = range_start,
1486 .range_end = range_end,
1487 };
1488 int ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001489
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001490 BUG_ON(!PageLocked(page));
Trond Myklebustc63c7b02007-04-02 19:29:52 -04001491 if (clear_page_dirty_for_io(page)) {
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001492 ret = nfs_writepage_locked(page, &wbc);
1493 if (ret < 0)
1494 goto out;
1495 }
Trond Myklebustf40313a2007-01-13 02:28:08 -05001496 if (!PagePrivate(page))
1497 return 0;
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001498 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1499 if (ret >= 0)
1500 return 0;
1501out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001502 __mark_inode_dirty(inode, I_DIRTY_PAGES);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001503 return ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001504}
1505
1506/*
1507 * Write back all requests on one page - we do this before reading it.
1508 */
1509int nfs_wb_page(struct inode *inode, struct page* page)
1510{
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001511 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
Trond Myklebust1c759502006-10-09 16:18:38 -04001512}
1513
David Howellsf7b422b2006-06-09 09:34:33 -04001514int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1517 sizeof(struct nfs_write_data),
1518 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001519 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (nfs_wdata_cachep == NULL)
1521 return -ENOMEM;
1522
Matthew Dobson93d23412006-03-26 01:37:50 -08001523 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1524 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 if (nfs_wdata_mempool == NULL)
1526 return -ENOMEM;
1527
Matthew Dobson93d23412006-03-26 01:37:50 -08001528 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1529 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 if (nfs_commit_mempool == NULL)
1531 return -ENOMEM;
1532
Peter Zijlstra89a09142007-03-16 13:38:26 -08001533 /*
1534 * NFS congestion size, scale with available memory.
1535 *
1536 * 64MB: 8192k
1537 * 128MB: 11585k
1538 * 256MB: 16384k
1539 * 512MB: 23170k
1540 * 1GB: 32768k
1541 * 2GB: 46340k
1542 * 4GB: 65536k
1543 * 8GB: 92681k
1544 * 16GB: 131072k
1545 *
1546 * This allows larger machines to have larger/more transfers.
1547 * Limit the default to 256M
1548 */
1549 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1550 if (nfs_congestion_kb > 256*1024)
1551 nfs_congestion_kb = 256*1024;
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return 0;
1554}
1555
David Brownell266bee82006-06-27 12:59:15 -07001556void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
1558 mempool_destroy(nfs_commit_mempool);
1559 mempool_destroy(nfs_wdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001560 kmem_cache_destroy(nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
1562