blob: 3229e217c773a357cd7735f9cd72351c075dbee1 [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 */
Trond Myklebustc63c7b02007-04-02 19:29:52 -040037static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc,
38 struct inode *inode, int ioflags);
Fred Isamanf8512ad2008-03-19 11:24:39 -040039static void nfs_redirty_request(struct nfs_page *req);
Trond Myklebust788e7a82006-03-20 13:44:27 -050040static const struct rpc_call_ops nfs_write_partial_ops;
41static const struct rpc_call_ops nfs_write_full_ops;
42static const struct rpc_call_ops nfs_commit_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Christoph Lametere18b8902006-12-06 20:33:20 -080044static struct kmem_cache *nfs_wdata_cachep;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050045static mempool_t *nfs_wdata_mempool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static mempool_t *nfs_commit_mempool;
47
Trond Myklebustc9d8f892008-04-15 16:56:39 -040048struct nfs_write_data *nfs_commitdata_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Christoph Lametere6b4f8d2006-12-06 20:33:14 -080050 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
Chuck Lever40859d72005-11-30 18:09:02 -050051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 if (p) {
53 memset(p, 0, sizeof(*p));
54 INIT_LIST_HEAD(&p->pages);
55 }
56 return p;
57}
58
Trond Myklebust5e4424a2008-02-25 21:53:49 -080059void nfs_commit_free(struct nfs_write_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Chuck Lever40859d72005-11-30 18:09:02 -050061 if (p && (p->pagevec != &p->page_array[0]))
62 kfree(p->pagevec);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 mempool_free(p, nfs_commit_mempool);
64}
65
Trond Myklebust8d5658c2007-04-10 09:26:35 -040066struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050067{
Christoph Lametere6b4f8d2006-12-06 20:33:14 -080068 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050069
70 if (p) {
71 memset(p, 0, sizeof(*p));
72 INIT_LIST_HEAD(&p->pages);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070073 p->npages = pagecount;
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040074 if (pagecount <= ARRAY_SIZE(p->page_array))
75 p->pagevec = p->page_array;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050076 else {
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040077 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
78 if (!p->pagevec) {
Trond Myklebust3feb2d42006-03-20 13:44:37 -050079 mempool_free(p, nfs_wdata_mempool);
80 p = NULL;
81 }
82 }
83 }
84 return p;
85}
86
Trond Myklebust5e4424a2008-02-25 21:53:49 -080087static void nfs_writedata_free(struct nfs_write_data *p)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050088{
89 if (p && (p->pagevec != &p->page_array[0]))
90 kfree(p->pagevec);
91 mempool_free(p, nfs_wdata_mempool);
92}
93
Trond Myklebust383ba712008-02-19 20:04:20 -050094void nfs_writedata_release(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Trond Myklebust383ba712008-02-19 20:04:20 -050096 struct nfs_write_data *wdata = data;
97
98 put_nfs_open_context(wdata->args.context);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 nfs_writedata_free(wdata);
100}
101
Trond Myklebust7b159fc2007-07-25 14:09:54 -0400102static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
103{
104 ctx->error = error;
105 smp_wmb();
106 set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
107}
108
Trond Myklebust277459d2006-12-05 00:35:35 -0500109static struct nfs_page *nfs_page_find_request_locked(struct page *page)
110{
111 struct nfs_page *req = NULL;
112
113 if (PagePrivate(page)) {
114 req = (struct nfs_page *)page_private(page);
115 if (req != NULL)
Trond Myklebustc03b4022007-06-17 13:26:38 -0400116 kref_get(&req->wb_kref);
Trond Myklebust277459d2006-12-05 00:35:35 -0500117 }
118 return req;
119}
120
121static struct nfs_page *nfs_page_find_request(struct page *page)
122{
Trond Myklebust587142f2007-07-02 09:57:54 -0400123 struct inode *inode = page->mapping->host;
Trond Myklebust277459d2006-12-05 00:35:35 -0500124 struct nfs_page *req = NULL;
Trond Myklebust277459d2006-12-05 00:35:35 -0500125
Trond Myklebust587142f2007-07-02 09:57:54 -0400126 spin_lock(&inode->i_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500127 req = nfs_page_find_request_locked(page);
Trond Myklebust587142f2007-07-02 09:57:54 -0400128 spin_unlock(&inode->i_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500129 return req;
130}
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/* Adjust the file length if we're writing beyond the end */
133static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
134{
135 struct inode *inode = page->mapping->host;
Trond Myklebusta3d01452008-06-11 12:21:19 -0400136 loff_t end, i_size;
137 pgoff_t end_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Trond Myklebusta3d01452008-06-11 12:21:19 -0400139 spin_lock(&inode->i_lock);
140 i_size = i_size_read(inode);
141 end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (i_size > 0 && page->index < end_index)
Trond Myklebusta3d01452008-06-11 12:21:19 -0400143 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
145 if (i_size >= end)
Trond Myklebusta3d01452008-06-11 12:21:19 -0400146 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 i_size_write(inode, end);
Trond Myklebusta3d01452008-06-11 12:21:19 -0400148 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
149out:
150 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Trond Myklebusta301b772007-02-06 11:07:15 -0800153/* A writeback failed: mark the page as bad, and invalidate the page cache */
154static void nfs_set_pageerror(struct page *page)
155{
156 SetPageError(page);
157 nfs_zap_mapping(page->mapping->host, page->mapping);
158}
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/* We can set the PG_uptodate flag if we see that a write request
161 * covers the full page.
162 */
163static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
164{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (PageUptodate(page))
166 return;
167 if (base != 0)
168 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500169 if (count != nfs_page_length(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500171 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static int wb_priority(struct writeback_control *wbc)
175{
176 if (wbc->for_reclaim)
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400177 return FLUSH_HIGHPRI | FLUSH_STABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (wbc->for_kupdate)
179 return FLUSH_LOWPRI;
180 return 0;
181}
182
183/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800184 * NFS congestion control
185 */
186
187int nfs_congestion_kb;
188
189#define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
190#define NFS_CONGESTION_OFF_THRESH \
191 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
192
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400193static int nfs_set_page_writeback(struct page *page)
Peter Zijlstra89a09142007-03-16 13:38:26 -0800194{
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400195 int ret = test_set_page_writeback(page);
196
197 if (!ret) {
Peter Zijlstra89a09142007-03-16 13:38:26 -0800198 struct inode *inode = page->mapping->host;
199 struct nfs_server *nfss = NFS_SERVER(inode);
200
Peter Zijlstra277866a2007-05-08 00:35:12 -0700201 if (atomic_long_inc_return(&nfss->writeback) >
Peter Zijlstra89a09142007-03-16 13:38:26 -0800202 NFS_CONGESTION_ON_THRESH)
203 set_bdi_congested(&nfss->backing_dev_info, WRITE);
204 }
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400205 return ret;
Peter Zijlstra89a09142007-03-16 13:38:26 -0800206}
207
208static void nfs_end_page_writeback(struct page *page)
209{
210 struct inode *inode = page->mapping->host;
211 struct nfs_server *nfss = NFS_SERVER(inode);
212
213 end_page_writeback(page);
Peter Zijlstrac4dc4be2007-10-16 23:25:41 -0700214 if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
Peter Zijlstra89a09142007-03-16 13:38:26 -0800215 clear_bdi_congested(&nfss->backing_dev_info, WRITE);
Peter Zijlstra89a09142007-03-16 13:38:26 -0800216}
217
218/*
Trond Myklebuste261f512006-12-05 00:35:41 -0500219 * Find an associated nfs write request, and prepare to flush it out
Trond Myklebust9cccef92007-07-22 17:09:05 -0400220 * May return an error if the user signalled nfs_wait_on_request().
Trond Myklebuste261f512006-12-05 00:35:41 -0500221 */
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400222static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
223 struct page *page)
Trond Myklebuste261f512006-12-05 00:35:41 -0500224{
Trond Myklebust587142f2007-07-02 09:57:54 -0400225 struct inode *inode = page->mapping->host;
Trond Myklebuste261f512006-12-05 00:35:41 -0500226 struct nfs_page *req;
Trond Myklebuste261f512006-12-05 00:35:41 -0500227 int ret;
228
Trond Myklebust587142f2007-07-02 09:57:54 -0400229 spin_lock(&inode->i_lock);
Trond Myklebuste261f512006-12-05 00:35:41 -0500230 for(;;) {
231 req = nfs_page_find_request_locked(page);
232 if (req == NULL) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400233 spin_unlock(&inode->i_lock);
Trond Myklebust9cccef92007-07-22 17:09:05 -0400234 return 0;
Trond Myklebuste261f512006-12-05 00:35:41 -0500235 }
Trond Myklebustacee4782008-01-22 17:13:07 -0500236 if (nfs_set_page_tag_locked(req))
Trond Myklebuste261f512006-12-05 00:35:41 -0500237 break;
238 /* Note: If we hold the page lock, as is the case in nfs_writepage,
Trond Myklebustacee4782008-01-22 17:13:07 -0500239 * then the call to nfs_set_page_tag_locked() will always
Trond Myklebuste261f512006-12-05 00:35:41 -0500240 * succeed provided that someone hasn't already marked the
241 * request as dirty (in which case we don't care).
242 */
Trond Myklebust587142f2007-07-02 09:57:54 -0400243 spin_unlock(&inode->i_lock);
Trond Myklebuste261f512006-12-05 00:35:41 -0500244 ret = nfs_wait_on_request(req);
245 nfs_release_request(req);
246 if (ret != 0)
247 return ret;
Trond Myklebust587142f2007-07-02 09:57:54 -0400248 spin_lock(&inode->i_lock);
Trond Myklebuste261f512006-12-05 00:35:41 -0500249 }
Trond Myklebuste468bae2008-06-13 13:25:22 -0400250 if (test_bit(PG_CLEAN, &req->wb_flags)) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400251 spin_unlock(&inode->i_lock);
Trond Myklebuste468bae2008-06-13 13:25:22 -0400252 BUG();
Trond Myklebust612c9382007-04-20 16:12:45 -0400253 }
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400254 if (nfs_set_page_writeback(page) != 0) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400255 spin_unlock(&inode->i_lock);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400256 BUG();
257 }
Trond Myklebust587142f2007-07-02 09:57:54 -0400258 spin_unlock(&inode->i_lock);
Fred Isamanf8512ad2008-03-19 11:24:39 -0400259 if (!nfs_pageio_add_request(pgio, req)) {
260 nfs_redirty_request(req);
Fred Isamanf8512ad2008-03-19 11:24:39 -0400261 return pgio->pg_error;
262 }
Trond Myklebust9cccef92007-07-22 17:09:05 -0400263 return 0;
Trond Myklebuste261f512006-12-05 00:35:41 -0500264}
265
Trond Myklebustf758c8852007-07-22 19:27:32 -0400266static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
267{
268 struct inode *inode = page->mapping->host;
269
270 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
271 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
272
273 nfs_pageio_cond_complete(pgio, page->index);
274 return nfs_page_async_flush(pgio, page);
275}
276
Trond Myklebuste261f512006-12-05 00:35:41 -0500277/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 * Write an mmapped page to the server.
279 */
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500280static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Trond Myklebustf758c8852007-07-22 19:27:32 -0400282 struct nfs_pageio_descriptor pgio;
Trond Myklebuste261f512006-12-05 00:35:41 -0500283 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Trond Myklebustf758c8852007-07-22 19:27:32 -0400285 nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
286 err = nfs_do_writepage(page, wbc, &pgio);
287 nfs_pageio_complete(&pgio);
288 if (err < 0)
289 return err;
290 if (pgio.pg_error < 0)
291 return pgio.pg_error;
292 return 0;
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500293}
294
295int nfs_writepage(struct page *page, struct writeback_control *wbc)
296{
Trond Myklebustf758c8852007-07-22 19:27:32 -0400297 int ret;
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500298
Trond Myklebustf758c8852007-07-22 19:27:32 -0400299 ret = nfs_writepage_locked(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 unlock_page(page);
Trond Myklebustf758c8852007-07-22 19:27:32 -0400301 return ret;
302}
303
304static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
305{
306 int ret;
307
308 ret = nfs_do_writepage(page, wbc, data);
309 unlock_page(page);
310 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
314{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 struct inode *inode = mapping->host;
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400316 struct nfs_pageio_descriptor pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 int err;
318
Chuck Lever91d5b472006-03-20 13:44:14 -0500319 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
320
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400321 nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
Trond Myklebustf758c8852007-07-22 19:27:32 -0400322 err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400323 nfs_pageio_complete(&pgio);
Trond Myklebustf758c8852007-07-22 19:27:32 -0400324 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return err;
Trond Myklebustf758c8852007-07-22 19:27:32 -0400326 if (pgio.pg_error < 0)
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400327 return pgio.pg_error;
328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331/*
332 * Insert a write request into an inode
333 */
Trond Myklebuste7d39062008-06-13 12:12:32 -0400334static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 struct nfs_inode *nfsi = NFS_I(inode);
337 int error;
338
Trond Myklebuste7d39062008-06-13 12:12:32 -0400339 error = radix_tree_preload(GFP_NOFS);
340 if (error != 0)
341 goto out;
342
343 /* Lock the request! */
344 nfs_lock_request_dontget(req);
345
346 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
Nick Piggin27852592008-02-04 23:48:37 -0800348 BUG_ON(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (!nfsi->npages) {
350 igrab(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (nfs_have_delegation(inode, FMODE_WRITE))
352 nfsi->change_attr++;
353 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500354 SetPagePrivate(req->wb_page);
Trond Myklebust277459d2006-12-05 00:35:35 -0500355 set_page_private(req->wb_page, (unsigned long)req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 nfsi->npages++;
Trond Myklebustc03b4022007-06-17 13:26:38 -0400357 kref_get(&req->wb_kref);
Nick Piggin27852592008-02-04 23:48:37 -0800358 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
359 NFS_PAGE_TAG_LOCKED);
Trond Myklebuste7d39062008-06-13 12:12:32 -0400360 spin_unlock(&inode->i_lock);
361 radix_tree_preload_end();
362out:
363 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
366/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800367 * Remove a write request from an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 */
369static void nfs_inode_remove_request(struct nfs_page *req)
370{
Trond Myklebust88be9f92007-06-05 10:42:27 -0400371 struct inode *inode = req->wb_context->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 struct nfs_inode *nfsi = NFS_I(inode);
373
374 BUG_ON (!NFS_WBACK_BUSY(req));
375
Trond Myklebust587142f2007-07-02 09:57:54 -0400376 spin_lock(&inode->i_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500377 set_page_private(req->wb_page, 0);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500378 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
380 nfsi->npages--;
381 if (!nfsi->npages) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400382 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 iput(inode);
384 } else
Trond Myklebust587142f2007-07-02 09:57:54 -0400385 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 nfs_clear_request(req);
387 nfs_release_request(req);
388}
389
Trond Myklebust61822ab2006-12-05 00:35:42 -0500390static void
Fred6d884e82008-03-19 11:24:38 -0400391nfs_mark_request_dirty(struct nfs_page *req)
Trond Myklebust61822ab2006-12-05 00:35:42 -0500392{
Trond Myklebust61822ab2006-12-05 00:35:42 -0500393 __set_page_dirty_nobuffers(req->wb_page);
394}
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
397/*
398 * Add a request to the inode's commit list.
399 */
400static void
401nfs_mark_request_commit(struct nfs_page *req)
402{
Trond Myklebust88be9f92007-06-05 10:42:27 -0400403 struct inode *inode = req->wb_context->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 struct nfs_inode *nfsi = NFS_I(inode);
405
Trond Myklebust587142f2007-07-02 09:57:54 -0400406 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 nfsi->ncommit++;
Trond Myklebuste468bae2008-06-13 13:25:22 -0400408 set_bit(PG_CLEAN, &(req)->wb_flags);
Trond Myklebust5c369682007-06-17 15:27:42 -0400409 radix_tree_tag_set(&nfsi->nfs_page_tree,
410 req->wb_index,
411 NFS_PAGE_TAG_COMMIT);
Trond Myklebust587142f2007-07-02 09:57:54 -0400412 spin_unlock(&inode->i_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700413 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -0700414 inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
Trond Myklebusta1803042006-12-05 00:45:19 -0500415 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400417
Trond Myklebuste468bae2008-06-13 13:25:22 -0400418static int
419nfs_clear_request_commit(struct nfs_page *req)
420{
421 struct page *page = req->wb_page;
422
423 if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) {
424 dec_zone_page_state(page, NR_UNSTABLE_NFS);
425 dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE);
426 return 1;
427 }
428 return 0;
429}
430
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400431static inline
432int nfs_write_need_commit(struct nfs_write_data *data)
433{
434 return data->verf.committed != NFS_FILE_SYNC;
435}
436
437static inline
438int nfs_reschedule_unstable_write(struct nfs_page *req)
439{
Trond Myklebuste468bae2008-06-13 13:25:22 -0400440 if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) {
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400441 nfs_mark_request_commit(req);
442 return 1;
443 }
444 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
Fred6d884e82008-03-19 11:24:38 -0400445 nfs_mark_request_dirty(req);
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400446 return 1;
447 }
448 return 0;
449}
450#else
451static inline void
452nfs_mark_request_commit(struct nfs_page *req)
453{
454}
455
Trond Myklebuste468bae2008-06-13 13:25:22 -0400456static inline int
457nfs_clear_request_commit(struct nfs_page *req)
458{
459 return 0;
460}
461
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400462static inline
463int nfs_write_need_commit(struct nfs_write_data *data)
464{
465 return 0;
466}
467
468static inline
469int nfs_reschedule_unstable_write(struct nfs_page *req)
470{
471 return 0;
472}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473#endif
474
475/*
476 * Wait for a request to complete.
477 *
Matthew Wilcox150030b2007-12-06 16:24:39 -0500478 * Interruptible by fatal signals only.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 */
Trond Myklebustca52fec2007-04-17 17:22:13 -0400480static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 struct nfs_inode *nfsi = NFS_I(inode);
483 struct nfs_page *req;
Trond Myklebustca52fec2007-04-17 17:22:13 -0400484 pgoff_t idx_end, next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 unsigned int res = 0;
486 int error;
487
488 if (npages == 0)
489 idx_end = ~0;
490 else
491 idx_end = idx_start + npages - 1;
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 next = idx_start;
Trond Myklebust9fd367f2007-06-17 15:10:24 -0400494 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 -0700495 if (req->wb_index > idx_end)
496 break;
497
498 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000499 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Trond Myklebustc03b4022007-06-17 13:26:38 -0400501 kref_get(&req->wb_kref);
Trond Myklebust587142f2007-07-02 09:57:54 -0400502 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 error = nfs_wait_on_request(req);
504 nfs_release_request(req);
Trond Myklebust587142f2007-07-02 09:57:54 -0400505 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (error < 0)
507 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 res++;
509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return res;
511}
512
Trond Myklebust83715ad2006-07-05 13:17:12 -0400513static void nfs_cancel_commit_list(struct list_head *head)
514{
515 struct nfs_page *req;
516
517 while(!list_empty(head)) {
518 req = nfs_list_entry(head->next);
519 nfs_list_remove_request(req);
Trond Myklebuste468bae2008-06-13 13:25:22 -0400520 nfs_clear_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400521 nfs_inode_remove_request(req);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700522 nfs_unlock_request(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400523 }
524}
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
527/*
528 * nfs_scan_commit - Scan an inode for commit requests
529 * @inode: NFS inode to scan
530 * @dst: destination list
531 * @idx_start: lower bound of page->index to scan.
532 * @npages: idx_start + npages sets the upper bound to scan.
533 *
534 * Moves requests from the inode's 'commit' request list.
535 * The requests are *not* checked to ensure that they form a contiguous set.
536 */
537static int
Trond Myklebustca52fec2007-04-17 17:22:13 -0400538nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000541 int res = 0;
542
543 if (nfsi->ncommit != 0) {
Trond Myklebust5c369682007-06-17 15:27:42 -0400544 res = nfs_scan_list(nfsi, dst, idx_start, npages,
545 NFS_PAGE_TAG_COMMIT);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000546 nfsi->ncommit -= res;
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return res;
549}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500550#else
Trond Myklebustca52fec2007-04-17 17:22:13 -0400551static 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 -0500552{
553 return 0;
554}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555#endif
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557/*
Trond Myklebuste7d39062008-06-13 12:12:32 -0400558 * Search for an existing write request, and attempt to update
559 * it to reflect a new dirty region on a given page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 *
Trond Myklebuste7d39062008-06-13 12:12:32 -0400561 * If the attempt fails, then the existing request is flushed out
562 * to disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 */
Trond Myklebuste7d39062008-06-13 12:12:32 -0400564static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
565 struct page *page,
566 unsigned int offset,
567 unsigned int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Trond Myklebuste7d39062008-06-13 12:12:32 -0400569 struct nfs_page *req;
570 unsigned int rqend;
571 unsigned int end;
572 int error;
573
574 if (!PagePrivate(page))
575 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 end = offset + bytes;
Trond Myklebuste7d39062008-06-13 12:12:32 -0400578 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 for (;;) {
Trond Myklebust277459d2006-12-05 00:35:35 -0500581 req = nfs_page_find_request_locked(page);
Trond Myklebuste7d39062008-06-13 12:12:32 -0400582 if (req == NULL)
583 goto out_unlock;
Trond Myklebust277459d2006-12-05 00:35:35 -0500584
Trond Myklebuste7d39062008-06-13 12:12:32 -0400585 rqend = req->wb_offset + req->wb_bytes;
586 /*
587 * Tell the caller to flush out the request if
588 * the offsets are non-contiguous.
589 * Note: nfs_flush_incompatible() will already
590 * have flushed out requests having wrong owners.
591 */
Trond Myklebuste468bae2008-06-13 13:25:22 -0400592 if (offset > rqend
Trond Myklebuste7d39062008-06-13 12:12:32 -0400593 || end < req->wb_offset)
594 goto out_flushme;
595
596 if (nfs_set_page_tag_locked(req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Trond Myklebuste7d39062008-06-13 12:12:32 -0400599 /* The request is locked, so wait and then retry */
Trond Myklebust587142f2007-07-02 09:57:54 -0400600 spin_unlock(&inode->i_lock);
Trond Myklebuste7d39062008-06-13 12:12:32 -0400601 error = nfs_wait_on_request(req);
602 nfs_release_request(req);
603 if (error != 0)
604 goto out_err;
605 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607
Trond Myklebuste468bae2008-06-13 13:25:22 -0400608 if (nfs_clear_request_commit(req))
609 radix_tree_tag_clear(&NFS_I(inode)->nfs_page_tree,
610 req->wb_index, NFS_PAGE_TAG_COMMIT);
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /* Okay, the request matches. Update the region */
613 if (offset < req->wb_offset) {
614 req->wb_offset = offset;
615 req->wb_pgbase = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (end > rqend)
618 req->wb_bytes = end - req->wb_offset;
Trond Myklebuste7d39062008-06-13 12:12:32 -0400619 else
620 req->wb_bytes = rqend - req->wb_offset;
621out_unlock:
622 spin_unlock(&inode->i_lock);
623 return req;
624out_flushme:
625 spin_unlock(&inode->i_lock);
626 nfs_release_request(req);
627 error = nfs_wb_page(inode, page);
628out_err:
629 return ERR_PTR(error);
630}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Trond Myklebuste7d39062008-06-13 12:12:32 -0400632/*
633 * Try to update an existing write request, or create one if there is none.
634 *
635 * Note: Should always be called with the Page Lock held to prevent races
636 * if we have to add a new request. Also assumes that the caller has
637 * already called nfs_flush_incompatible() if necessary.
638 */
639static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
640 struct page *page, unsigned int offset, unsigned int bytes)
641{
642 struct inode *inode = page->mapping->host;
643 struct nfs_page *req;
644 int error;
645
646 req = nfs_try_to_update_request(inode, page, offset, bytes);
647 if (req != NULL)
648 goto out;
649 req = nfs_create_request(ctx, inode, page, offset, bytes);
650 if (IS_ERR(req))
651 goto out;
652 error = nfs_inode_add_request(inode, req);
653 if (error != 0) {
654 nfs_release_request(req);
655 req = ERR_PTR(error);
656 }
Trond Myklebustefc91ed2008-06-10 18:31:00 -0400657out:
Trond Myklebust61e930a2007-10-18 17:08:05 -0400658 return req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
Trond Myklebuste7d39062008-06-13 12:12:32 -0400661static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
662 unsigned int offset, unsigned int count)
663{
664 struct nfs_page *req;
665
666 req = nfs_setup_write_request(ctx, page, offset, count);
667 if (IS_ERR(req))
668 return PTR_ERR(req);
669 /* Update file length */
670 nfs_grow_file(page, offset, count);
671 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
672 nfs_clear_page_tag_locked(req);
673 return 0;
674}
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676int nfs_flush_incompatible(struct file *file, struct page *page)
677{
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400678 struct nfs_open_context *ctx = nfs_file_open_context(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 struct nfs_page *req;
Trond Myklebust1a545332006-12-05 00:35:40 -0500680 int do_flush, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 /*
682 * Look for a request corresponding to this page. If there
683 * is one, and it belongs to another file, we flush it out
684 * before we try to copy anything into the page. Do this
685 * due to the lack of an ACCESS-type call in NFSv2.
686 * Also do the same if we find a request from an existing
687 * dropped page.
688 */
Trond Myklebust1a545332006-12-05 00:35:40 -0500689 do {
690 req = nfs_page_find_request(page);
691 if (req == NULL)
692 return 0;
Trond Myklebuste468bae2008-06-13 13:25:22 -0400693 do_flush = req->wb_page != page || req->wb_context != ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 nfs_release_request(req);
Trond Myklebust1a545332006-12-05 00:35:40 -0500695 if (!do_flush)
696 return 0;
697 status = nfs_wb_page(page->mapping->host, page);
698 } while (status == 0);
699 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
702/*
Trond Myklebust5d47a352008-02-07 17:24:07 -0500703 * If the page cache is marked as unsafe or invalid, then we can't rely on
704 * the PageUptodate() flag. In this case, we will need to turn off
705 * write optimisations that depend on the page contents being correct.
706 */
707static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
708{
709 return PageUptodate(page) &&
710 !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
711}
712
713/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 * Update and possibly write a cached page of an NFS file.
715 *
716 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
717 * things with a page scheduled for an RPC call (e.g. invalidate it).
718 */
719int nfs_updatepage(struct file *file, struct page *page,
720 unsigned int offset, unsigned int count)
721{
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400722 struct nfs_open_context *ctx = nfs_file_open_context(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 int status = 0;
725
Chuck Lever91d5b472006-03-20 13:44:14 -0500726 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
727
Chuck Lever48186c72008-06-11 17:56:05 -0400728 dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n",
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800729 file->f_path.dentry->d_parent->d_name.name,
730 file->f_path.dentry->d_name.name, count,
Chuck Lever48186c72008-06-11 17:56:05 -0400731 (long long)(page_offset(page) + offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /* If we're not using byte range locks, and we know the page
Trond Myklebust5d47a352008-02-07 17:24:07 -0500734 * is up to date, it may be more efficient to extend the write
735 * to cover the entire page in order to avoid fragmentation
736 * inefficiencies.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 */
Trond Myklebust5d47a352008-02-07 17:24:07 -0500738 if (nfs_write_pageuptodate(page, inode) &&
739 inode->i_flock == NULL &&
Trond Myklebust4b5621f2008-02-25 15:56:29 -0800740 !(file->f_flags & O_SYNC)) {
Trond Myklebust49a70f22006-12-05 00:35:38 -0500741 count = max(count + offset, nfs_page_length(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744
Trond Myklebuste21195a2006-12-05 00:35:39 -0500745 status = nfs_writepage_setup(ctx, page, offset, count);
Trond Myklebust03fa9e82008-06-05 16:02:35 -0400746 if (status < 0)
747 nfs_set_pageerror(page);
748 else
749 __set_page_dirty_nobuffers(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Chuck Lever48186c72008-06-11 17:56:05 -0400751 dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 status, (long long)i_size_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return status;
754}
755
756static void nfs_writepage_release(struct nfs_page *req)
757{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Trond Myklebust7e5f6142008-05-25 12:59:19 -0400759 if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req)) {
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400760 nfs_end_page_writeback(req->wb_page);
761 nfs_inode_remove_request(req);
762 } else
763 nfs_end_page_writeback(req->wb_page);
Trond Myklebust9fd367f2007-06-17 15:10:24 -0400764 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Trond Myklebust3ff75762007-07-14 15:40:00 -0400767static int flush_task_priority(int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
770 case FLUSH_HIGHPRI:
771 return RPC_PRIORITY_HIGH;
772 case FLUSH_LOWPRI:
773 return RPC_PRIORITY_LOW;
774 }
775 return RPC_PRIORITY_NORMAL;
776}
777
778/*
779 * Set up the argument/result storage required for the RPC call.
780 */
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400781static int nfs_write_rpcsetup(struct nfs_page *req,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500783 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 unsigned int count, unsigned int offset,
785 int how)
786{
Trond Myklebust84115e12007-07-14 15:39:59 -0400787 struct inode *inode = req->wb_context->path.dentry->d_inode;
788 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
Trond Myklebust3ff75762007-07-14 15:40:00 -0400789 int priority = flush_task_priority(how);
Trond Myklebust07737692007-10-25 18:42:54 -0400790 struct rpc_task *task;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400791 struct rpc_message msg = {
792 .rpc_argp = &data->args,
793 .rpc_resp = &data->res,
794 .rpc_cred = req->wb_context->cred,
795 };
Trond Myklebust84115e12007-07-14 15:39:59 -0400796 struct rpc_task_setup task_setup_data = {
797 .rpc_client = NFS_CLIENT(inode),
Trond Myklebust07737692007-10-25 18:42:54 -0400798 .task = &data->task,
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400799 .rpc_message = &msg,
Trond Myklebust84115e12007-07-14 15:39:59 -0400800 .callback_ops = call_ops,
801 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -0500802 .workqueue = nfsiod_workqueue,
Trond Myklebust84115e12007-07-14 15:39:59 -0400803 .flags = flags,
Trond Myklebust3ff75762007-07-14 15:40:00 -0400804 .priority = priority,
Trond Myklebust84115e12007-07-14 15:39:59 -0400805 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 /* Set up the RPC argument and reply structs
808 * NB: take care not to mess about with data->commit et al. */
809
810 data->req = req;
Trond Myklebust88be9f92007-06-05 10:42:27 -0400811 data->inode = inode = req->wb_context->path.dentry->d_inode;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400812 data->cred = msg.rpc_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 data->args.fh = NFS_FH(inode);
815 data->args.offset = req_offset(req) + offset;
816 data->args.pgbase = req->wb_pgbase + offset;
817 data->args.pages = data->pagevec;
818 data->args.count = count;
Trond Myklebust383ba712008-02-19 20:04:20 -0500819 data->args.context = get_nfs_open_context(req->wb_context);
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400820 data->args.stable = NFS_UNSTABLE;
821 if (how & FLUSH_STABLE) {
822 data->args.stable = NFS_DATA_SYNC;
823 if (!NFS_I(inode)->ncommit)
824 data->args.stable = NFS_FILE_SYNC;
825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 data->res.fattr = &data->fattr;
828 data->res.count = count;
829 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400830 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Trond Myklebust788e7a82006-03-20 13:44:27 -0500832 /* Set up the initial task struct. */
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400833 NFS_PROTO(inode)->write_setup(data, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Chuck Levera3f565b2007-01-31 12:14:01 -0500835 dprintk("NFS: %5u initiated write call "
Chuck Lever48186c72008-06-11 17:56:05 -0400836 "(req %s/%lld, %u bytes @ offset %llu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500837 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 inode->i_sb->s_id,
839 (long long)NFS_FILEID(inode),
840 count,
841 (unsigned long long)data->args.offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Trond Myklebust07737692007-10-25 18:42:54 -0400843 task = rpc_run_task(&task_setup_data);
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400844 if (IS_ERR(task))
845 return PTR_ERR(task);
846 rpc_put_task(task);
847 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Fred6d884e82008-03-19 11:24:38 -0400850/* If a nfs_flush_* function fails, it should remove reqs from @head and
851 * call this on each, which will prepare them to be retried on next
852 * writeback using standard nfs.
853 */
854static void nfs_redirty_request(struct nfs_page *req)
855{
856 nfs_mark_request_dirty(req);
857 nfs_end_page_writeback(req->wb_page);
858 nfs_clear_page_tag_locked(req);
859}
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861/*
862 * Generate multiple small requests to write out a single
863 * contiguous dirty area on one page.
864 */
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400865static 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 -0700866{
867 struct nfs_page *req = nfs_list_entry(head->next);
868 struct page *page = req->wb_page;
869 struct nfs_write_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700870 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
871 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 int requests = 0;
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400873 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 LIST_HEAD(list);
875
876 nfs_list_remove_request(req);
877
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400878 nbytes = count;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700879 do {
880 size_t len = min(nbytes, wsize);
881
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400882 data = nfs_writedata_alloc(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (!data)
884 goto out_bad;
885 list_add(&data->pages, &list);
886 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700887 nbytes -= len;
888 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 atomic_set(&req->wb_complete, requests);
890
891 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 offset = 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400893 nbytes = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 do {
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400895 int ret2;
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 data = list_entry(list.next, struct nfs_write_data, pages);
898 list_del_init(&data->pages);
899
900 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400902 if (nbytes < wsize)
903 wsize = nbytes;
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400904 ret2 = nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400905 wsize, offset, how);
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400906 if (ret == 0)
907 ret = ret2;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400908 offset += wsize;
909 nbytes -= wsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 } while (nbytes != 0);
911
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400912 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914out_bad:
915 while (!list_empty(&list)) {
916 data = list_entry(list.next, struct nfs_write_data, pages);
917 list_del(&data->pages);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500918 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
Trond Myklebust61822ab2006-12-05 00:35:42 -0500920 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return -ENOMEM;
922}
923
924/*
925 * Create an RPC task for the given write request and kick it.
926 * The page must have been locked by the caller.
927 *
928 * It may happen that the page we're passed is not marked dirty.
929 * This is the case if nfs_updatepage detects a conflicting request
930 * that has been written but not committed.
931 */
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400932static 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 -0700933{
934 struct nfs_page *req;
935 struct page **pages;
936 struct nfs_write_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400938 data = nfs_writedata_alloc(npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (!data)
940 goto out_bad;
941
942 pages = data->pagevec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 while (!list_empty(head)) {
944 req = nfs_list_entry(head->next);
945 nfs_list_remove_request(req);
946 nfs_list_add_request(req, &data->pages);
947 ClearPageError(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 *pages++ = req->wb_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950 req = nfs_list_entry(data->pages.next);
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 /* Set up the argument struct */
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400953 return nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 out_bad:
955 while (!list_empty(head)) {
Trond Myklebust10afec92007-05-14 17:16:04 -0400956 req = nfs_list_entry(head->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 nfs_list_remove_request(req);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500958 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
960 return -ENOMEM;
961}
962
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400963static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
964 struct inode *inode, int ioflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Chuck Leverbf4285e2007-12-20 14:54:57 -0500966 size_t wsize = NFS_SERVER(inode)->wsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400968 if (wsize < PAGE_CACHE_SIZE)
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400969 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400970 else
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400971 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
974/*
975 * Handle a write reply that flushed part of a page.
976 */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500977static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Trond Myklebust788e7a82006-03-20 13:44:27 -0500979 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Chuck Lever48186c72008-06-11 17:56:05 -0400981 dprintk("NFS: %5u write(%s/%lld %d@%lld)",
982 task->tk_pid,
983 data->req->wb_context->path.dentry->d_inode->i_sb->s_id,
984 (long long)
985 NFS_FILEID(data->req->wb_context->path.dentry->d_inode),
986 data->req->wb_bytes, (long long)req_offset(data->req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Trond Myklebustc9d8f892008-04-15 16:56:39 -0400988 nfs_writeback_done(task, data);
989}
Trond Myklebust788e7a82006-03-20 13:44:27 -0500990
Trond Myklebustc9d8f892008-04-15 16:56:39 -0400991static void nfs_writeback_release_partial(void *calldata)
992{
993 struct nfs_write_data *data = calldata;
994 struct nfs_page *req = data->req;
995 struct page *page = req->wb_page;
996 int status = data->task.tk_status;
997
998 if (status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -0800999 nfs_set_pageerror(page);
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001000 nfs_context_set_write_error(req->wb_context, status);
1001 dprintk(", error = %d\n", status);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001002 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
1004
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001005 if (nfs_write_need_commit(data)) {
Trond Myklebust587142f2007-07-02 09:57:54 -04001006 struct inode *inode = page->mapping->host;
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001007
Trond Myklebust587142f2007-07-02 09:57:54 -04001008 spin_lock(&inode->i_lock);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001009 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
1010 /* Do nothing we need to resend the writes */
1011 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1012 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1013 dprintk(" defer commit\n");
1014 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1015 set_bit(PG_NEED_RESCHED, &req->wb_flags);
1016 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
1017 dprintk(" server reboot detected\n");
1018 }
Trond Myklebust587142f2007-07-02 09:57:54 -04001019 spin_unlock(&inode->i_lock);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001020 } else
1021 dprintk(" OK\n");
1022
1023out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (atomic_dec_and_test(&req->wb_complete))
1025 nfs_writepage_release(req);
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001026 nfs_writedata_release(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
Trond Myklebust788e7a82006-03-20 13:44:27 -05001029static const struct rpc_call_ops nfs_write_partial_ops = {
1030 .rpc_call_done = nfs_writeback_done_partial,
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001031 .rpc_release = nfs_writeback_release_partial,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001032};
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034/*
1035 * Handle a write reply that flushes a whole page.
1036 *
1037 * FIXME: There is an inherent race with invalidate_inode_pages and
1038 * writebacks since the page->count is kept > 1 for as long
1039 * as the page has a write request pending.
1040 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001041static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001043 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001045 nfs_writeback_done(task, data);
1046}
1047
1048static void nfs_writeback_release_full(void *calldata)
1049{
1050 struct nfs_write_data *data = calldata;
1051 int status = data->task.tk_status;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /* Update attributes as result of writeback. */
1054 while (!list_empty(&data->pages)) {
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001055 struct nfs_page *req = nfs_list_entry(data->pages.next);
1056 struct page *page = req->wb_page;
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 nfs_list_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Chuck Lever48186c72008-06-11 17:56:05 -04001060 dprintk("NFS: %5u write (%s/%lld %d@%lld)",
1061 data->task.tk_pid,
Trond Myklebust88be9f92007-06-05 10:42:27 -04001062 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1063 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 req->wb_bytes,
1065 (long long)req_offset(req));
1066
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001067 if (status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -08001068 nfs_set_pageerror(page);
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001069 nfs_context_set_write_error(req->wb_context, status);
1070 dprintk(", error = %d\n", status);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001071 goto remove_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001074 if (nfs_write_need_commit(data)) {
1075 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1076 nfs_mark_request_commit(req);
1077 nfs_end_page_writeback(page);
1078 dprintk(" marked for commit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 goto next;
1080 }
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001081 dprintk(" OK\n");
1082remove_request:
1083 nfs_end_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 nfs_inode_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 next:
Trond Myklebust9fd367f2007-06-17 15:10:24 -04001086 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001088 nfs_writedata_release(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
Trond Myklebust788e7a82006-03-20 13:44:27 -05001091static const struct rpc_call_ops nfs_write_full_ops = {
1092 .rpc_call_done = nfs_writeback_done_full,
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001093 .rpc_release = nfs_writeback_release_full,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001094};
1095
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097/*
1098 * This function is called when the WRITE call is complete.
1099 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001100int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 struct nfs_writeargs *argp = &data->args;
1103 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001104 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Chuck Levera3f565b2007-01-31 12:14:01 -05001106 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 task->tk_pid, task->tk_status);
1108
Chuck Leverf551e442006-09-20 14:33:04 -04001109 /*
1110 * ->write_done will attempt to use post-op attributes to detect
1111 * conflicting writes by other clients. A strict interpretation
1112 * of close-to-open would allow us to continue caching even if
1113 * another writer had changed the file, but some applications
1114 * depend on tighter cache coherency when writing.
1115 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001116 status = NFS_PROTO(data->inode)->write_done(task, data);
1117 if (status != 0)
1118 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001119 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1122 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1123 /* We tried a write call, but the server did not
1124 * commit data to stable storage even though we
1125 * requested it.
1126 * Note: There is a known bug in Tru64 < 5.0 in which
1127 * the server reports NFS_DATA_SYNC, but performs
1128 * NFS_FILE_SYNC. We therefore implement this checking
1129 * as a dprintk() in order to avoid filling syslog.
1130 */
1131 static unsigned long complain;
1132
1133 if (time_before(complain, jiffies)) {
Chuck Lever48186c72008-06-11 17:56:05 -04001134 dprintk("NFS: faulty NFS server %s:"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 " (committed = %d) != (stable = %d)\n",
David Howells54ceac42006-08-22 20:06:13 -04001136 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 resp->verf->committed, argp->stable);
1138 complain = jiffies + 300 * HZ;
1139 }
1140 }
1141#endif
1142 /* Is this a short write? */
1143 if (task->tk_status >= 0 && resp->count < argp->count) {
1144 static unsigned long complain;
1145
Chuck Lever91d5b472006-03-20 13:44:14 -05001146 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 /* Has the server at least made some progress? */
1149 if (resp->count != 0) {
1150 /* Was this an NFSv2 write or an NFSv3 stable write? */
1151 if (resp->verf->committed != NFS_UNSTABLE) {
1152 /* Resend from where the server left off */
1153 argp->offset += resp->count;
1154 argp->pgbase += resp->count;
1155 argp->count -= resp->count;
1156 } else {
1157 /* Resend as a stable write in order to avoid
1158 * headaches in the case of a server crash.
1159 */
1160 argp->stable = NFS_FILE_SYNC;
1161 }
1162 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001163 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165 if (time_before(complain, jiffies)) {
1166 printk(KERN_WARNING
1167 "NFS: Server wrote zero bytes, expected %u.\n",
1168 argp->count);
1169 complain = jiffies + 300 * HZ;
1170 }
1171 /* Can't do anything about it except throw an error. */
1172 task->tk_status = -EIO;
1173 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
1177
1178#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001179void nfs_commitdata_release(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Trond Myklebust383ba712008-02-19 20:04:20 -05001181 struct nfs_write_data *wdata = data;
1182
1183 put_nfs_open_context(wdata->args.context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 nfs_commit_free(wdata);
1185}
1186
1187/*
1188 * Set up the argument/result storage required for the RPC call.
1189 */
Trond Myklebustdbae4c72008-04-14 14:54:53 -04001190static int nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001191 struct nfs_write_data *data,
1192 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Trond Myklebust84115e12007-07-14 15:39:59 -04001194 struct nfs_page *first = nfs_list_entry(head->next);
1195 struct inode *inode = first->wb_context->path.dentry->d_inode;
1196 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
Trond Myklebust3ff75762007-07-14 15:40:00 -04001197 int priority = flush_task_priority(how);
Trond Myklebust07737692007-10-25 18:42:54 -04001198 struct rpc_task *task;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001199 struct rpc_message msg = {
1200 .rpc_argp = &data->args,
1201 .rpc_resp = &data->res,
1202 .rpc_cred = first->wb_context->cred,
1203 };
Trond Myklebust84115e12007-07-14 15:39:59 -04001204 struct rpc_task_setup task_setup_data = {
Trond Myklebust07737692007-10-25 18:42:54 -04001205 .task = &data->task,
Trond Myklebust84115e12007-07-14 15:39:59 -04001206 .rpc_client = NFS_CLIENT(inode),
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001207 .rpc_message = &msg,
Trond Myklebust84115e12007-07-14 15:39:59 -04001208 .callback_ops = &nfs_commit_ops,
1209 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001210 .workqueue = nfsiod_workqueue,
Trond Myklebust84115e12007-07-14 15:39:59 -04001211 .flags = flags,
Trond Myklebust3ff75762007-07-14 15:40:00 -04001212 .priority = priority,
Trond Myklebust84115e12007-07-14 15:39:59 -04001213 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 /* Set up the RPC argument and reply structs
1216 * NB: take care not to mess about with data->commit et al. */
1217
1218 list_splice_init(head, &data->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 data->inode = inode;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001221 data->cred = msg.rpc_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001224 /* Note: we always request a commit of the entire inode */
1225 data->args.offset = 0;
1226 data->args.count = 0;
Trond Myklebust383ba712008-02-19 20:04:20 -05001227 data->args.context = get_nfs_open_context(first->wb_context);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001228 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 data->res.fattr = &data->fattr;
1230 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001231 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001232
1233 /* Set up the initial task struct. */
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001234 NFS_PROTO(inode)->commit_setup(data, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Chuck Levera3f565b2007-01-31 12:14:01 -05001236 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001237
Trond Myklebust07737692007-10-25 18:42:54 -04001238 task = rpc_run_task(&task_setup_data);
Trond Myklebustdbae4c72008-04-14 14:54:53 -04001239 if (IS_ERR(task))
1240 return PTR_ERR(task);
1241 rpc_put_task(task);
1242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
1245/*
1246 * Commit dirty pages
1247 */
1248static int
Chuck Lever40859d72005-11-30 18:09:02 -05001249nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 struct nfs_write_data *data;
1252 struct nfs_page *req;
1253
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001254 data = nfs_commitdata_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 if (!data)
1257 goto out_bad;
1258
1259 /* Set up the argument struct */
Trond Myklebustdbae4c72008-04-14 14:54:53 -04001260 return nfs_commit_rpcsetup(head, data, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 out_bad:
1262 while (!list_empty(head)) {
1263 req = nfs_list_entry(head->next);
1264 nfs_list_remove_request(req);
1265 nfs_mark_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001266 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -07001267 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1268 BDI_RECLAIMABLE);
Trond Myklebust9fd367f2007-06-17 15:10:24 -04001269 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
1271 return -ENOMEM;
1272}
1273
1274/*
1275 * COMMIT call returned
1276 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001277static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001279 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Chuck Levera3f565b2007-01-31 12:14:01 -05001281 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 task->tk_pid, task->tk_status);
1283
Trond Myklebust788e7a82006-03-20 13:44:27 -05001284 /* Call the NFS version-specific code */
1285 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1286 return;
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001287}
1288
1289static void nfs_commit_release(void *calldata)
1290{
1291 struct nfs_write_data *data = calldata;
1292 struct nfs_page *req;
1293 int status = data->task.tk_status;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 while (!list_empty(&data->pages)) {
1296 req = nfs_list_entry(data->pages.next);
1297 nfs_list_remove_request(req);
Trond Myklebuste468bae2008-06-13 13:25:22 -04001298 nfs_clear_request_commit(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Chuck Lever48186c72008-06-11 17:56:05 -04001300 dprintk("NFS: commit (%s/%lld %d@%lld)",
Trond Myklebust88be9f92007-06-05 10:42:27 -04001301 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1302 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 req->wb_bytes,
1304 (long long)req_offset(req));
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001305 if (status < 0) {
1306 nfs_context_set_write_error(req->wb_context, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 nfs_inode_remove_request(req);
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001308 dprintk(", error = %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 goto next;
1310 }
1311
1312 /* Okay, COMMIT succeeded, apparently. Check the verifier
1313 * returned by the server against all stored verfs. */
1314 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1315 /* We have a match */
1316 nfs_inode_remove_request(req);
1317 dprintk(" OK\n");
1318 goto next;
1319 }
1320 /* We have a mismatch. Write the page again */
1321 dprintk(" mismatch\n");
Fred6d884e82008-03-19 11:24:38 -04001322 nfs_mark_request_dirty(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 next:
Trond Myklebust9fd367f2007-06-17 15:10:24 -04001324 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001326 nfs_commitdata_release(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001328
1329static const struct rpc_call_ops nfs_commit_ops = {
1330 .rpc_call_done = nfs_commit_done,
1331 .rpc_release = nfs_commit_release,
1332};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001334int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001337 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Trond Myklebust587142f2007-07-02 09:57:54 -04001339 spin_lock(&inode->i_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001340 res = nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebust587142f2007-07-02 09:57:54 -04001341 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001343 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001344 if (error < 0)
1345 return error;
1346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return res;
1348}
Trond Myklebustc63c7b02007-04-02 19:29:52 -04001349#else
1350static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1351{
1352 return 0;
1353}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354#endif
1355
Trond Myklebust1c759502006-10-09 16:18:38 -04001356long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Trond Myklebust1c759502006-10-09 16:18:38 -04001358 struct inode *inode = mapping->host;
Trond Myklebustca52fec2007-04-17 17:22:13 -04001359 pgoff_t idx_start, idx_end;
Trond Myklebust1c759502006-10-09 16:18:38 -04001360 unsigned int npages = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001361 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001362 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebust3f442542006-09-17 14:46:44 -04001363 long pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Trond Myklebust1c759502006-10-09 16:18:38 -04001365 /* FIXME */
1366 if (wbc->range_cyclic)
1367 idx_start = 0;
1368 else {
1369 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1370 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1371 if (idx_end > idx_start) {
Trond Myklebustca52fec2007-04-17 17:22:13 -04001372 pgoff_t l_npages = 1 + idx_end - idx_start;
Trond Myklebust1c759502006-10-09 16:18:38 -04001373 npages = l_npages;
1374 if (sizeof(npages) != sizeof(l_npages) &&
Trond Myklebustca52fec2007-04-17 17:22:13 -04001375 (pgoff_t)npages != l_npages)
Trond Myklebust1c759502006-10-09 16:18:38 -04001376 npages = 0;
1377 }
1378 }
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001379 how &= ~FLUSH_NOCOMMIT;
Trond Myklebust587142f2007-07-02 09:57:54 -04001380 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 do {
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001382 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1383 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001384 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001385 if (nocommit)
1386 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001387 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebust724c4392007-04-17 17:22:13 -04001388 if (pages == 0)
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001389 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001390 if (how & FLUSH_INVALIDATE) {
Trond Myklebust587142f2007-07-02 09:57:54 -04001391 spin_unlock(&inode->i_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001392 nfs_cancel_commit_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001393 ret = pages;
Trond Myklebust587142f2007-07-02 09:57:54 -04001394 spin_lock(&inode->i_lock);
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001395 continue;
1396 }
1397 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebust587142f2007-07-02 09:57:54 -04001398 spin_unlock(&inode->i_lock);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001399 ret = nfs_commit_list(inode, &head, how);
Trond Myklebust587142f2007-07-02 09:57:54 -04001400 spin_lock(&inode->i_lock);
1401
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001402 } while (ret >= 0);
Trond Myklebust587142f2007-07-02 09:57:54 -04001403 spin_unlock(&inode->i_lock);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001404 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405}
1406
Trond Myklebust34901f72007-07-25 14:09:54 -04001407static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
Trond Myklebust1c759502006-10-09 16:18:38 -04001408{
Trond Myklebust1c759502006-10-09 16:18:38 -04001409 int ret;
1410
Trond Myklebust34901f72007-07-25 14:09:54 -04001411 ret = nfs_writepages(mapping, wbc);
Trond Myklebust61822ab2006-12-05 00:35:42 -05001412 if (ret < 0)
1413 goto out;
Trond Myklebust34901f72007-07-25 14:09:54 -04001414 ret = nfs_sync_mapping_wait(mapping, wbc, how);
Trond Myklebusted90ef52007-07-20 13:13:28 -04001415 if (ret < 0)
1416 goto out;
1417 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001418out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001419 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Trond Myklebust1c759502006-10-09 16:18:38 -04001420 return ret;
1421}
1422
Trond Myklebust34901f72007-07-25 14:09:54 -04001423/* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
1424static int nfs_write_mapping(struct address_space *mapping, int how)
1425{
1426 struct writeback_control wbc = {
1427 .bdi = mapping->backing_dev_info,
1428 .sync_mode = WB_SYNC_NONE,
1429 .nr_to_write = LONG_MAX,
1430 .for_writepages = 1,
1431 .range_cyclic = 1,
1432 };
1433 int ret;
1434
1435 ret = __nfs_write_mapping(mapping, &wbc, how);
1436 if (ret < 0)
1437 return ret;
1438 wbc.sync_mode = WB_SYNC_ALL;
1439 return __nfs_write_mapping(mapping, &wbc, how);
1440}
1441
Trond Myklebusted90ef52007-07-20 13:13:28 -04001442/*
1443 * flush the inode to disk.
1444 */
1445int nfs_wb_all(struct inode *inode)
Trond Myklebust1c759502006-10-09 16:18:38 -04001446{
Trond Myklebusted90ef52007-07-20 13:13:28 -04001447 return nfs_write_mapping(inode->i_mapping, 0);
1448}
Trond Myklebust1c759502006-10-09 16:18:38 -04001449
Trond Myklebusted90ef52007-07-20 13:13:28 -04001450int nfs_wb_nocommit(struct inode *inode)
1451{
1452 return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
Trond Myklebust1c759502006-10-09 16:18:38 -04001453}
1454
Trond Myklebust1b3b4a12007-08-28 10:29:36 -04001455int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1456{
1457 struct nfs_page *req;
1458 loff_t range_start = page_offset(page);
1459 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1460 struct writeback_control wbc = {
1461 .bdi = page->mapping->backing_dev_info,
1462 .sync_mode = WB_SYNC_ALL,
1463 .nr_to_write = LONG_MAX,
1464 .range_start = range_start,
1465 .range_end = range_end,
1466 };
1467 int ret = 0;
1468
1469 BUG_ON(!PageLocked(page));
1470 for (;;) {
1471 req = nfs_page_find_request(page);
1472 if (req == NULL)
1473 goto out;
Trond Myklebuste468bae2008-06-13 13:25:22 -04001474 if (test_bit(PG_CLEAN, &req->wb_flags)) {
Trond Myklebust1b3b4a12007-08-28 10:29:36 -04001475 nfs_release_request(req);
1476 break;
1477 }
1478 if (nfs_lock_request_dontget(req)) {
1479 nfs_inode_remove_request(req);
1480 /*
1481 * In case nfs_inode_remove_request has marked the
1482 * page as being dirty
1483 */
1484 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1485 nfs_unlock_request(req);
1486 break;
1487 }
1488 ret = nfs_wait_on_request(req);
1489 if (ret < 0)
1490 goto out;
1491 }
1492 if (!PagePrivate(page))
1493 return 0;
1494 ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
1495out:
1496 return ret;
1497}
1498
Adrian Bunk5334eb12007-11-21 15:04:31 -08001499static int nfs_wb_page_priority(struct inode *inode, struct page *page,
1500 int how)
Trond Myklebust1c759502006-10-09 16:18:38 -04001501{
1502 loff_t range_start = page_offset(page);
1503 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001504 struct writeback_control wbc = {
1505 .bdi = page->mapping->backing_dev_info,
1506 .sync_mode = WB_SYNC_ALL,
1507 .nr_to_write = LONG_MAX,
1508 .range_start = range_start,
1509 .range_end = range_end,
1510 };
1511 int ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001512
Trond Myklebust73e33022008-04-11 16:03:54 -04001513 do {
1514 if (clear_page_dirty_for_io(page)) {
1515 ret = nfs_writepage_locked(page, &wbc);
1516 if (ret < 0)
1517 goto out_error;
1518 } else if (!PagePrivate(page))
1519 break;
1520 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001521 if (ret < 0)
Trond Myklebust73e33022008-04-11 16:03:54 -04001522 goto out_error;
1523 } while (PagePrivate(page));
1524 return 0;
1525out_error:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001526 __mark_inode_dirty(inode, I_DIRTY_PAGES);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001527 return ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001528}
1529
1530/*
1531 * Write back all requests on one page - we do this before reading it.
1532 */
1533int nfs_wb_page(struct inode *inode, struct page* page)
1534{
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001535 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
Trond Myklebust1c759502006-10-09 16:18:38 -04001536}
1537
David Howellsf7b422b2006-06-09 09:34:33 -04001538int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
1540 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1541 sizeof(struct nfs_write_data),
1542 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001543 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if (nfs_wdata_cachep == NULL)
1545 return -ENOMEM;
1546
Matthew Dobson93d23412006-03-26 01:37:50 -08001547 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1548 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 if (nfs_wdata_mempool == NULL)
1550 return -ENOMEM;
1551
Matthew Dobson93d23412006-03-26 01:37:50 -08001552 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1553 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if (nfs_commit_mempool == NULL)
1555 return -ENOMEM;
1556
Peter Zijlstra89a09142007-03-16 13:38:26 -08001557 /*
1558 * NFS congestion size, scale with available memory.
1559 *
1560 * 64MB: 8192k
1561 * 128MB: 11585k
1562 * 256MB: 16384k
1563 * 512MB: 23170k
1564 * 1GB: 32768k
1565 * 2GB: 46340k
1566 * 4GB: 65536k
1567 * 8GB: 92681k
1568 * 16GB: 131072k
1569 *
1570 * This allows larger machines to have larger/more transfers.
1571 * Limit the default to 256M
1572 */
1573 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1574 if (nfs_congestion_kb > 256*1024)
1575 nfs_congestion_kb = 256*1024;
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 return 0;
1578}
1579
David Brownell266bee82006-06-27 12:59:15 -07001580void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
1582 mempool_destroy(nfs_commit_mempool);
1583 mempool_destroy(nfs_wdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001584 kmem_cache_destroy(nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585}
1586