blob: 1a999939fedf8cce2007e00f6575817a71cf18f5 [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);
Trond Myklebuste468bae2008-06-13 13:25:22 -0400407 set_bit(PG_CLEAN, &(req)->wb_flags);
Trond Myklebust5c369682007-06-17 15:27:42 -0400408 radix_tree_tag_set(&nfsi->nfs_page_tree,
409 req->wb_index,
410 NFS_PAGE_TAG_COMMIT);
Trond Myklebust587142f2007-07-02 09:57:54 -0400411 spin_unlock(&inode->i_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700412 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -0700413 inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
Trond Myklebusta1803042006-12-05 00:45:19 -0500414 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400416
Trond Myklebuste468bae2008-06-13 13:25:22 -0400417static int
418nfs_clear_request_commit(struct nfs_page *req)
419{
420 struct page *page = req->wb_page;
421
422 if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) {
423 dec_zone_page_state(page, NR_UNSTABLE_NFS);
424 dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE);
425 return 1;
426 }
427 return 0;
428}
429
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400430static inline
431int nfs_write_need_commit(struct nfs_write_data *data)
432{
433 return data->verf.committed != NFS_FILE_SYNC;
434}
435
436static inline
437int nfs_reschedule_unstable_write(struct nfs_page *req)
438{
Trond Myklebuste468bae2008-06-13 13:25:22 -0400439 if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) {
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400440 nfs_mark_request_commit(req);
441 return 1;
442 }
443 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
Fred6d884e82008-03-19 11:24:38 -0400444 nfs_mark_request_dirty(req);
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400445 return 1;
446 }
447 return 0;
448}
449#else
450static inline void
451nfs_mark_request_commit(struct nfs_page *req)
452{
453}
454
Trond Myklebuste468bae2008-06-13 13:25:22 -0400455static inline int
456nfs_clear_request_commit(struct nfs_page *req)
457{
458 return 0;
459}
460
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400461static inline
462int nfs_write_need_commit(struct nfs_write_data *data)
463{
464 return 0;
465}
466
467static inline
468int nfs_reschedule_unstable_write(struct nfs_page *req)
469{
470 return 0;
471}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472#endif
473
474/*
475 * Wait for a request to complete.
476 *
Matthew Wilcox150030b2007-12-06 16:24:39 -0500477 * Interruptible by fatal signals only.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 */
Trond Myklebustca52fec2007-04-17 17:22:13 -0400479static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 struct nfs_inode *nfsi = NFS_I(inode);
482 struct nfs_page *req;
Trond Myklebustca52fec2007-04-17 17:22:13 -0400483 pgoff_t idx_end, next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 unsigned int res = 0;
485 int error;
486
487 if (npages == 0)
488 idx_end = ~0;
489 else
490 idx_end = idx_start + npages - 1;
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 next = idx_start;
Trond Myklebust9fd367f2007-06-17 15:10:24 -0400493 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 -0700494 if (req->wb_index > idx_end)
495 break;
496
497 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000498 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Trond Myklebustc03b4022007-06-17 13:26:38 -0400500 kref_get(&req->wb_kref);
Trond Myklebust587142f2007-07-02 09:57:54 -0400501 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 error = nfs_wait_on_request(req);
503 nfs_release_request(req);
Trond Myklebust587142f2007-07-02 09:57:54 -0400504 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (error < 0)
506 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 res++;
508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return res;
510}
511
Trond Myklebust83715ad2006-07-05 13:17:12 -0400512static void nfs_cancel_commit_list(struct list_head *head)
513{
514 struct nfs_page *req;
515
516 while(!list_empty(head)) {
517 req = nfs_list_entry(head->next);
518 nfs_list_remove_request(req);
Trond Myklebuste468bae2008-06-13 13:25:22 -0400519 nfs_clear_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400520 nfs_inode_remove_request(req);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700521 nfs_unlock_request(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400522 }
523}
524
Trond Myklebustfb8a1f12009-03-11 14:10:29 -0400525static int
526nfs_need_commit(struct nfs_inode *nfsi)
527{
528 return radix_tree_tagged(&nfsi->nfs_page_tree, NFS_PAGE_TAG_COMMIT);
529}
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
532/*
533 * nfs_scan_commit - Scan an inode for commit requests
534 * @inode: NFS inode to scan
535 * @dst: destination list
536 * @idx_start: lower bound of page->index to scan.
537 * @npages: idx_start + npages sets the upper bound to scan.
538 *
539 * Moves requests from the inode's 'commit' request list.
540 * The requests are *not* checked to ensure that they form a contiguous set.
541 */
542static int
Trond Myklebustca52fec2007-04-17 17:22:13 -0400543nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000546
Trond Myklebustfb8a1f12009-03-11 14:10:29 -0400547 if (!nfs_need_commit(nfsi))
548 return 0;
549
550 return nfs_scan_list(nfsi, dst, idx_start, npages, NFS_PAGE_TAG_COMMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500552#else
Trond Myklebustfb8a1f12009-03-11 14:10:29 -0400553static inline int nfs_need_commit(struct nfs_inode *nfsi)
554{
555 return 0;
556}
557
Trond Myklebustca52fec2007-04-17 17:22:13 -0400558static 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 -0500559{
560 return 0;
561}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562#endif
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564/*
Trond Myklebuste7d39062008-06-13 12:12:32 -0400565 * Search for an existing write request, and attempt to update
566 * it to reflect a new dirty region on a given page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 *
Trond Myklebuste7d39062008-06-13 12:12:32 -0400568 * If the attempt fails, then the existing request is flushed out
569 * to disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 */
Trond Myklebuste7d39062008-06-13 12:12:32 -0400571static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
572 struct page *page,
573 unsigned int offset,
574 unsigned int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Trond Myklebuste7d39062008-06-13 12:12:32 -0400576 struct nfs_page *req;
577 unsigned int rqend;
578 unsigned int end;
579 int error;
580
581 if (!PagePrivate(page))
582 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 end = offset + bytes;
Trond Myklebuste7d39062008-06-13 12:12:32 -0400585 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 for (;;) {
Trond Myklebust277459d2006-12-05 00:35:35 -0500588 req = nfs_page_find_request_locked(page);
Trond Myklebuste7d39062008-06-13 12:12:32 -0400589 if (req == NULL)
590 goto out_unlock;
Trond Myklebust277459d2006-12-05 00:35:35 -0500591
Trond Myklebuste7d39062008-06-13 12:12:32 -0400592 rqend = req->wb_offset + req->wb_bytes;
593 /*
594 * Tell the caller to flush out the request if
595 * the offsets are non-contiguous.
596 * Note: nfs_flush_incompatible() will already
597 * have flushed out requests having wrong owners.
598 */
Trond Myklebuste468bae2008-06-13 13:25:22 -0400599 if (offset > rqend
Trond Myklebuste7d39062008-06-13 12:12:32 -0400600 || end < req->wb_offset)
601 goto out_flushme;
602
603 if (nfs_set_page_tag_locked(req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Trond Myklebuste7d39062008-06-13 12:12:32 -0400606 /* The request is locked, so wait and then retry */
Trond Myklebust587142f2007-07-02 09:57:54 -0400607 spin_unlock(&inode->i_lock);
Trond Myklebuste7d39062008-06-13 12:12:32 -0400608 error = nfs_wait_on_request(req);
609 nfs_release_request(req);
610 if (error != 0)
611 goto out_err;
612 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614
Trond Myklebuste468bae2008-06-13 13:25:22 -0400615 if (nfs_clear_request_commit(req))
616 radix_tree_tag_clear(&NFS_I(inode)->nfs_page_tree,
617 req->wb_index, NFS_PAGE_TAG_COMMIT);
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /* Okay, the request matches. Update the region */
620 if (offset < req->wb_offset) {
621 req->wb_offset = offset;
622 req->wb_pgbase = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (end > rqend)
625 req->wb_bytes = end - req->wb_offset;
Trond Myklebuste7d39062008-06-13 12:12:32 -0400626 else
627 req->wb_bytes = rqend - req->wb_offset;
628out_unlock:
629 spin_unlock(&inode->i_lock);
630 return req;
631out_flushme:
632 spin_unlock(&inode->i_lock);
633 nfs_release_request(req);
634 error = nfs_wb_page(inode, page);
635out_err:
636 return ERR_PTR(error);
637}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Trond Myklebuste7d39062008-06-13 12:12:32 -0400639/*
640 * Try to update an existing write request, or create one if there is none.
641 *
642 * Note: Should always be called with the Page Lock held to prevent races
643 * if we have to add a new request. Also assumes that the caller has
644 * already called nfs_flush_incompatible() if necessary.
645 */
646static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
647 struct page *page, unsigned int offset, unsigned int bytes)
648{
649 struct inode *inode = page->mapping->host;
650 struct nfs_page *req;
651 int error;
652
653 req = nfs_try_to_update_request(inode, page, offset, bytes);
654 if (req != NULL)
655 goto out;
656 req = nfs_create_request(ctx, inode, page, offset, bytes);
657 if (IS_ERR(req))
658 goto out;
659 error = nfs_inode_add_request(inode, req);
660 if (error != 0) {
661 nfs_release_request(req);
662 req = ERR_PTR(error);
663 }
Trond Myklebustefc91ed2008-06-10 18:31:00 -0400664out:
Trond Myklebust61e930a2007-10-18 17:08:05 -0400665 return req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
Trond Myklebuste7d39062008-06-13 12:12:32 -0400668static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
669 unsigned int offset, unsigned int count)
670{
671 struct nfs_page *req;
672
673 req = nfs_setup_write_request(ctx, page, offset, count);
674 if (IS_ERR(req))
675 return PTR_ERR(req);
676 /* Update file length */
677 nfs_grow_file(page, offset, count);
678 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
679 nfs_clear_page_tag_locked(req);
680 return 0;
681}
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683int nfs_flush_incompatible(struct file *file, struct page *page)
684{
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400685 struct nfs_open_context *ctx = nfs_file_open_context(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 struct nfs_page *req;
Trond Myklebust1a545332006-12-05 00:35:40 -0500687 int do_flush, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /*
689 * Look for a request corresponding to this page. If there
690 * is one, and it belongs to another file, we flush it out
691 * before we try to copy anything into the page. Do this
692 * due to the lack of an ACCESS-type call in NFSv2.
693 * Also do the same if we find a request from an existing
694 * dropped page.
695 */
Trond Myklebust1a545332006-12-05 00:35:40 -0500696 do {
697 req = nfs_page_find_request(page);
698 if (req == NULL)
699 return 0;
Trond Myklebuste468bae2008-06-13 13:25:22 -0400700 do_flush = req->wb_page != page || req->wb_context != ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 nfs_release_request(req);
Trond Myklebust1a545332006-12-05 00:35:40 -0500702 if (!do_flush)
703 return 0;
704 status = nfs_wb_page(page->mapping->host, page);
705 } while (status == 0);
706 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
709/*
Trond Myklebust5d47a352008-02-07 17:24:07 -0500710 * If the page cache is marked as unsafe or invalid, then we can't rely on
711 * the PageUptodate() flag. In this case, we will need to turn off
712 * write optimisations that depend on the page contents being correct.
713 */
714static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
715{
716 return PageUptodate(page) &&
717 !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
718}
719
720/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 * Update and possibly write a cached page of an NFS file.
722 *
723 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
724 * things with a page scheduled for an RPC call (e.g. invalidate it).
725 */
726int nfs_updatepage(struct file *file, struct page *page,
727 unsigned int offset, unsigned int count)
728{
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400729 struct nfs_open_context *ctx = nfs_file_open_context(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 int status = 0;
732
Chuck Lever91d5b472006-03-20 13:44:14 -0500733 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
734
Chuck Lever48186c72008-06-11 17:56:05 -0400735 dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n",
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800736 file->f_path.dentry->d_parent->d_name.name,
737 file->f_path.dentry->d_name.name, count,
Chuck Lever48186c72008-06-11 17:56:05 -0400738 (long long)(page_offset(page) + offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 /* If we're not using byte range locks, and we know the page
Trond Myklebust5d47a352008-02-07 17:24:07 -0500741 * is up to date, it may be more efficient to extend the write
742 * to cover the entire page in order to avoid fragmentation
743 * inefficiencies.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 */
Trond Myklebust5d47a352008-02-07 17:24:07 -0500745 if (nfs_write_pageuptodate(page, inode) &&
746 inode->i_flock == NULL &&
Trond Myklebust4b5621f2008-02-25 15:56:29 -0800747 !(file->f_flags & O_SYNC)) {
Trond Myklebust49a70f22006-12-05 00:35:38 -0500748 count = max(count + offset, nfs_page_length(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
751
Trond Myklebuste21195a2006-12-05 00:35:39 -0500752 status = nfs_writepage_setup(ctx, page, offset, count);
Trond Myklebust03fa9e82008-06-05 16:02:35 -0400753 if (status < 0)
754 nfs_set_pageerror(page);
755 else
756 __set_page_dirty_nobuffers(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Chuck Lever48186c72008-06-11 17:56:05 -0400758 dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 status, (long long)i_size_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return status;
761}
762
763static void nfs_writepage_release(struct nfs_page *req)
764{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Trond Myklebust7e5f6142008-05-25 12:59:19 -0400766 if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req)) {
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 */
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400788static int nfs_write_rpcsetup(struct nfs_page *req,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 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,
Trond Myklebust101070c2008-02-19 20:04:23 -0500809 .workqueue = nfsiod_workqueue,
Trond Myklebust84115e12007-07-14 15:39:59 -0400810 .flags = flags,
Trond Myklebust3ff75762007-07-14 15:40:00 -0400811 .priority = priority,
Trond Myklebust84115e12007-07-14 15:39:59 -0400812 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 /* Set up the RPC argument and reply structs
815 * NB: take care not to mess about with data->commit et al. */
816
817 data->req = req;
Trond Myklebust88be9f92007-06-05 10:42:27 -0400818 data->inode = inode = req->wb_context->path.dentry->d_inode;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400819 data->cred = msg.rpc_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 data->args.fh = NFS_FH(inode);
822 data->args.offset = req_offset(req) + offset;
823 data->args.pgbase = req->wb_pgbase + offset;
824 data->args.pages = data->pagevec;
825 data->args.count = count;
Trond Myklebust383ba712008-02-19 20:04:20 -0500826 data->args.context = get_nfs_open_context(req->wb_context);
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400827 data->args.stable = NFS_UNSTABLE;
828 if (how & FLUSH_STABLE) {
829 data->args.stable = NFS_DATA_SYNC;
Trond Myklebustfb8a1f12009-03-11 14:10:29 -0400830 if (!nfs_need_commit(NFS_I(inode)))
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400831 data->args.stable = NFS_FILE_SYNC;
832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 data->res.fattr = &data->fattr;
835 data->res.count = count;
836 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400837 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Trond Myklebust788e7a82006-03-20 13:44:27 -0500839 /* Set up the initial task struct. */
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400840 NFS_PROTO(inode)->write_setup(data, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Chuck Levera3f565b2007-01-31 12:14:01 -0500842 dprintk("NFS: %5u initiated write call "
Chuck Lever48186c72008-06-11 17:56:05 -0400843 "(req %s/%lld, %u bytes @ offset %llu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500844 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 inode->i_sb->s_id,
846 (long long)NFS_FILEID(inode),
847 count,
848 (unsigned long long)data->args.offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Trond Myklebust07737692007-10-25 18:42:54 -0400850 task = rpc_run_task(&task_setup_data);
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400851 if (IS_ERR(task))
852 return PTR_ERR(task);
853 rpc_put_task(task);
854 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
Fred6d884e82008-03-19 11:24:38 -0400857/* If a nfs_flush_* function fails, it should remove reqs from @head and
858 * call this on each, which will prepare them to be retried on next
859 * writeback using standard nfs.
860 */
861static void nfs_redirty_request(struct nfs_page *req)
862{
863 nfs_mark_request_dirty(req);
864 nfs_end_page_writeback(req->wb_page);
865 nfs_clear_page_tag_locked(req);
866}
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868/*
869 * Generate multiple small requests to write out a single
870 * contiguous dirty area on one page.
871 */
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400872static 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 -0700873{
874 struct nfs_page *req = nfs_list_entry(head->next);
875 struct page *page = req->wb_page;
876 struct nfs_write_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700877 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
878 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 int requests = 0;
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400880 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 LIST_HEAD(list);
882
883 nfs_list_remove_request(req);
884
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400885 nbytes = count;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700886 do {
887 size_t len = min(nbytes, wsize);
888
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400889 data = nfs_writedata_alloc(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (!data)
891 goto out_bad;
892 list_add(&data->pages, &list);
893 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700894 nbytes -= len;
895 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 atomic_set(&req->wb_complete, requests);
897
898 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 offset = 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400900 nbytes = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 do {
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400902 int ret2;
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 data = list_entry(list.next, struct nfs_write_data, pages);
905 list_del_init(&data->pages);
906
907 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400909 if (nbytes < wsize)
910 wsize = nbytes;
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400911 ret2 = nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400912 wsize, offset, how);
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400913 if (ret == 0)
914 ret = ret2;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400915 offset += wsize;
916 nbytes -= wsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 } while (nbytes != 0);
918
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400919 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921out_bad:
922 while (!list_empty(&list)) {
923 data = list_entry(list.next, struct nfs_write_data, pages);
924 list_del(&data->pages);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500925 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
Trond Myklebust61822ab2006-12-05 00:35:42 -0500927 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return -ENOMEM;
929}
930
931/*
932 * Create an RPC task for the given write request and kick it.
933 * The page must have been locked by the caller.
934 *
935 * It may happen that the page we're passed is not marked dirty.
936 * This is the case if nfs_updatepage detects a conflicting request
937 * that has been written but not committed.
938 */
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400939static 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 -0700940{
941 struct nfs_page *req;
942 struct page **pages;
943 struct nfs_write_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400945 data = nfs_writedata_alloc(npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (!data)
947 goto out_bad;
948
949 pages = data->pagevec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 while (!list_empty(head)) {
951 req = nfs_list_entry(head->next);
952 nfs_list_remove_request(req);
953 nfs_list_add_request(req, &data->pages);
954 ClearPageError(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 *pages++ = req->wb_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957 req = nfs_list_entry(data->pages.next);
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 /* Set up the argument struct */
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400960 return nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 out_bad:
962 while (!list_empty(head)) {
Trond Myklebust10afec92007-05-14 17:16:04 -0400963 req = nfs_list_entry(head->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 nfs_list_remove_request(req);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500965 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
967 return -ENOMEM;
968}
969
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400970static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
971 struct inode *inode, int ioflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Chuck Leverbf4285e2007-12-20 14:54:57 -0500973 size_t wsize = NFS_SERVER(inode)->wsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400975 if (wsize < PAGE_CACHE_SIZE)
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400976 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400977 else
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400978 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
981/*
982 * Handle a write reply that flushed part of a page.
983 */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500984static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Trond Myklebust788e7a82006-03-20 13:44:27 -0500986 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Chuck Lever48186c72008-06-11 17:56:05 -0400988 dprintk("NFS: %5u write(%s/%lld %d@%lld)",
989 task->tk_pid,
990 data->req->wb_context->path.dentry->d_inode->i_sb->s_id,
991 (long long)
992 NFS_FILEID(data->req->wb_context->path.dentry->d_inode),
993 data->req->wb_bytes, (long long)req_offset(data->req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Trond Myklebustc9d8f892008-04-15 16:56:39 -0400995 nfs_writeback_done(task, data);
996}
Trond Myklebust788e7a82006-03-20 13:44:27 -0500997
Trond Myklebustc9d8f892008-04-15 16:56:39 -0400998static void nfs_writeback_release_partial(void *calldata)
999{
1000 struct nfs_write_data *data = calldata;
1001 struct nfs_page *req = data->req;
1002 struct page *page = req->wb_page;
1003 int status = data->task.tk_status;
1004
1005 if (status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -08001006 nfs_set_pageerror(page);
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001007 nfs_context_set_write_error(req->wb_context, status);
1008 dprintk(", error = %d\n", status);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001009 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
1011
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001012 if (nfs_write_need_commit(data)) {
Trond Myklebust587142f2007-07-02 09:57:54 -04001013 struct inode *inode = page->mapping->host;
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001014
Trond Myklebust587142f2007-07-02 09:57:54 -04001015 spin_lock(&inode->i_lock);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001016 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
1017 /* Do nothing we need to resend the writes */
1018 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1019 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1020 dprintk(" defer commit\n");
1021 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1022 set_bit(PG_NEED_RESCHED, &req->wb_flags);
1023 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
1024 dprintk(" server reboot detected\n");
1025 }
Trond Myklebust587142f2007-07-02 09:57:54 -04001026 spin_unlock(&inode->i_lock);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001027 } else
1028 dprintk(" OK\n");
1029
1030out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (atomic_dec_and_test(&req->wb_complete))
1032 nfs_writepage_release(req);
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001033 nfs_writedata_release(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
Trond Myklebust788e7a82006-03-20 13:44:27 -05001036static const struct rpc_call_ops nfs_write_partial_ops = {
1037 .rpc_call_done = nfs_writeback_done_partial,
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001038 .rpc_release = nfs_writeback_release_partial,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001039};
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041/*
1042 * Handle a write reply that flushes a whole page.
1043 *
1044 * FIXME: There is an inherent race with invalidate_inode_pages and
1045 * writebacks since the page->count is kept > 1 for as long
1046 * as the page has a write request pending.
1047 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001048static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001050 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001052 nfs_writeback_done(task, data);
1053}
1054
1055static void nfs_writeback_release_full(void *calldata)
1056{
1057 struct nfs_write_data *data = calldata;
1058 int status = data->task.tk_status;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 /* Update attributes as result of writeback. */
1061 while (!list_empty(&data->pages)) {
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001062 struct nfs_page *req = nfs_list_entry(data->pages.next);
1063 struct page *page = req->wb_page;
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 nfs_list_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Chuck Lever48186c72008-06-11 17:56:05 -04001067 dprintk("NFS: %5u write (%s/%lld %d@%lld)",
1068 data->task.tk_pid,
Trond Myklebust88be9f92007-06-05 10:42:27 -04001069 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1070 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 req->wb_bytes,
1072 (long long)req_offset(req));
1073
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001074 if (status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -08001075 nfs_set_pageerror(page);
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001076 nfs_context_set_write_error(req->wb_context, status);
1077 dprintk(", error = %d\n", status);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001078 goto remove_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001081 if (nfs_write_need_commit(data)) {
1082 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1083 nfs_mark_request_commit(req);
1084 nfs_end_page_writeback(page);
1085 dprintk(" marked for commit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 goto next;
1087 }
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001088 dprintk(" OK\n");
1089remove_request:
1090 nfs_end_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 nfs_inode_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 next:
Trond Myklebust9fd367f2007-06-17 15:10:24 -04001093 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001095 nfs_writedata_release(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
Trond Myklebust788e7a82006-03-20 13:44:27 -05001098static const struct rpc_call_ops nfs_write_full_ops = {
1099 .rpc_call_done = nfs_writeback_done_full,
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001100 .rpc_release = nfs_writeback_release_full,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001101};
1102
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104/*
1105 * This function is called when the WRITE call is complete.
1106 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001107int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 struct nfs_writeargs *argp = &data->args;
1110 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001111 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Chuck Levera3f565b2007-01-31 12:14:01 -05001113 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 task->tk_pid, task->tk_status);
1115
Chuck Leverf551e442006-09-20 14:33:04 -04001116 /*
1117 * ->write_done will attempt to use post-op attributes to detect
1118 * conflicting writes by other clients. A strict interpretation
1119 * of close-to-open would allow us to continue caching even if
1120 * another writer had changed the file, but some applications
1121 * depend on tighter cache coherency when writing.
1122 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001123 status = NFS_PROTO(data->inode)->write_done(task, data);
1124 if (status != 0)
1125 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001126 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1129 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1130 /* We tried a write call, but the server did not
1131 * commit data to stable storage even though we
1132 * requested it.
1133 * Note: There is a known bug in Tru64 < 5.0 in which
1134 * the server reports NFS_DATA_SYNC, but performs
1135 * NFS_FILE_SYNC. We therefore implement this checking
1136 * as a dprintk() in order to avoid filling syslog.
1137 */
1138 static unsigned long complain;
1139
1140 if (time_before(complain, jiffies)) {
Chuck Lever48186c72008-06-11 17:56:05 -04001141 dprintk("NFS: faulty NFS server %s:"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 " (committed = %d) != (stable = %d)\n",
David Howells54ceac42006-08-22 20:06:13 -04001143 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 resp->verf->committed, argp->stable);
1145 complain = jiffies + 300 * HZ;
1146 }
1147 }
1148#endif
1149 /* Is this a short write? */
1150 if (task->tk_status >= 0 && resp->count < argp->count) {
1151 static unsigned long complain;
1152
Chuck Lever91d5b472006-03-20 13:44:14 -05001153 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 /* Has the server at least made some progress? */
1156 if (resp->count != 0) {
1157 /* Was this an NFSv2 write or an NFSv3 stable write? */
1158 if (resp->verf->committed != NFS_UNSTABLE) {
1159 /* Resend from where the server left off */
1160 argp->offset += resp->count;
1161 argp->pgbase += resp->count;
1162 argp->count -= resp->count;
1163 } else {
1164 /* Resend as a stable write in order to avoid
1165 * headaches in the case of a server crash.
1166 */
1167 argp->stable = NFS_FILE_SYNC;
1168 }
1169 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001170 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172 if (time_before(complain, jiffies)) {
1173 printk(KERN_WARNING
1174 "NFS: Server wrote zero bytes, expected %u.\n",
1175 argp->count);
1176 complain = jiffies + 300 * HZ;
1177 }
1178 /* Can't do anything about it except throw an error. */
1179 task->tk_status = -EIO;
1180 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001181 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
1184
1185#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001186void nfs_commitdata_release(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Trond Myklebust383ba712008-02-19 20:04:20 -05001188 struct nfs_write_data *wdata = data;
1189
1190 put_nfs_open_context(wdata->args.context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 nfs_commit_free(wdata);
1192}
1193
1194/*
1195 * Set up the argument/result storage required for the RPC call.
1196 */
Trond Myklebustdbae4c72008-04-14 14:54:53 -04001197static int nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001198 struct nfs_write_data *data,
1199 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Trond Myklebust84115e12007-07-14 15:39:59 -04001201 struct nfs_page *first = nfs_list_entry(head->next);
1202 struct inode *inode = first->wb_context->path.dentry->d_inode;
1203 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
Trond Myklebust3ff75762007-07-14 15:40:00 -04001204 int priority = flush_task_priority(how);
Trond Myklebust07737692007-10-25 18:42:54 -04001205 struct rpc_task *task;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001206 struct rpc_message msg = {
1207 .rpc_argp = &data->args,
1208 .rpc_resp = &data->res,
1209 .rpc_cred = first->wb_context->cred,
1210 };
Trond Myklebust84115e12007-07-14 15:39:59 -04001211 struct rpc_task_setup task_setup_data = {
Trond Myklebust07737692007-10-25 18:42:54 -04001212 .task = &data->task,
Trond Myklebust84115e12007-07-14 15:39:59 -04001213 .rpc_client = NFS_CLIENT(inode),
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001214 .rpc_message = &msg,
Trond Myklebust84115e12007-07-14 15:39:59 -04001215 .callback_ops = &nfs_commit_ops,
1216 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001217 .workqueue = nfsiod_workqueue,
Trond Myklebust84115e12007-07-14 15:39:59 -04001218 .flags = flags,
Trond Myklebust3ff75762007-07-14 15:40:00 -04001219 .priority = priority,
Trond Myklebust84115e12007-07-14 15:39:59 -04001220 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 /* Set up the RPC argument and reply structs
1223 * NB: take care not to mess about with data->commit et al. */
1224
1225 list_splice_init(head, &data->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 data->inode = inode;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001228 data->cred = msg.rpc_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001231 /* Note: we always request a commit of the entire inode */
1232 data->args.offset = 0;
1233 data->args.count = 0;
Trond Myklebust383ba712008-02-19 20:04:20 -05001234 data->args.context = get_nfs_open_context(first->wb_context);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001235 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 data->res.fattr = &data->fattr;
1237 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001238 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001239
1240 /* Set up the initial task struct. */
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001241 NFS_PROTO(inode)->commit_setup(data, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Chuck Levera3f565b2007-01-31 12:14:01 -05001243 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001244
Trond Myklebust07737692007-10-25 18:42:54 -04001245 task = rpc_run_task(&task_setup_data);
Trond Myklebustdbae4c72008-04-14 14:54:53 -04001246 if (IS_ERR(task))
1247 return PTR_ERR(task);
1248 rpc_put_task(task);
1249 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250}
1251
1252/*
1253 * Commit dirty pages
1254 */
1255static int
Chuck Lever40859d72005-11-30 18:09:02 -05001256nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
1258 struct nfs_write_data *data;
1259 struct nfs_page *req;
1260
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001261 data = nfs_commitdata_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 if (!data)
1264 goto out_bad;
1265
1266 /* Set up the argument struct */
Trond Myklebustdbae4c72008-04-14 14:54:53 -04001267 return nfs_commit_rpcsetup(head, data, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 out_bad:
1269 while (!list_empty(head)) {
1270 req = nfs_list_entry(head->next);
1271 nfs_list_remove_request(req);
1272 nfs_mark_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001273 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -07001274 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1275 BDI_RECLAIMABLE);
Trond Myklebust9fd367f2007-06-17 15:10:24 -04001276 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278 return -ENOMEM;
1279}
1280
1281/*
1282 * COMMIT call returned
1283 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001284static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001286 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Chuck Levera3f565b2007-01-31 12:14:01 -05001288 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 task->tk_pid, task->tk_status);
1290
Trond Myklebust788e7a82006-03-20 13:44:27 -05001291 /* Call the NFS version-specific code */
1292 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1293 return;
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001294}
1295
1296static void nfs_commit_release(void *calldata)
1297{
1298 struct nfs_write_data *data = calldata;
1299 struct nfs_page *req;
1300 int status = data->task.tk_status;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 while (!list_empty(&data->pages)) {
1303 req = nfs_list_entry(data->pages.next);
1304 nfs_list_remove_request(req);
Trond Myklebuste468bae2008-06-13 13:25:22 -04001305 nfs_clear_request_commit(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Chuck Lever48186c72008-06-11 17:56:05 -04001307 dprintk("NFS: commit (%s/%lld %d@%lld)",
Trond Myklebust88be9f92007-06-05 10:42:27 -04001308 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1309 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 req->wb_bytes,
1311 (long long)req_offset(req));
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001312 if (status < 0) {
1313 nfs_context_set_write_error(req->wb_context, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 nfs_inode_remove_request(req);
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001315 dprintk(", error = %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 goto next;
1317 }
1318
1319 /* Okay, COMMIT succeeded, apparently. Check the verifier
1320 * returned by the server against all stored verfs. */
1321 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1322 /* We have a match */
1323 nfs_inode_remove_request(req);
1324 dprintk(" OK\n");
1325 goto next;
1326 }
1327 /* We have a mismatch. Write the page again */
1328 dprintk(" mismatch\n");
Fred6d884e82008-03-19 11:24:38 -04001329 nfs_mark_request_dirty(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 next:
Trond Myklebust9fd367f2007-06-17 15:10:24 -04001331 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001333 nfs_commitdata_release(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001335
1336static const struct rpc_call_ops nfs_commit_ops = {
1337 .rpc_call_done = nfs_commit_done,
1338 .rpc_release = nfs_commit_release,
1339};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001341int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001344 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Trond Myklebust587142f2007-07-02 09:57:54 -04001346 spin_lock(&inode->i_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001347 res = nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebust587142f2007-07-02 09:57:54 -04001348 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001350 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001351 if (error < 0)
1352 return error;
1353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return res;
1355}
Trond Myklebustc63c7b02007-04-02 19:29:52 -04001356#else
1357static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1358{
1359 return 0;
1360}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361#endif
1362
Trond Myklebust1c759502006-10-09 16:18:38 -04001363long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Trond Myklebust1c759502006-10-09 16:18:38 -04001365 struct inode *inode = mapping->host;
Trond Myklebustca52fec2007-04-17 17:22:13 -04001366 pgoff_t idx_start, idx_end;
Trond Myklebust1c759502006-10-09 16:18:38 -04001367 unsigned int npages = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001368 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001369 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebust3f442542006-09-17 14:46:44 -04001370 long pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Trond Myklebust1c759502006-10-09 16:18:38 -04001372 /* FIXME */
1373 if (wbc->range_cyclic)
1374 idx_start = 0;
1375 else {
1376 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1377 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1378 if (idx_end > idx_start) {
Trond Myklebustca52fec2007-04-17 17:22:13 -04001379 pgoff_t l_npages = 1 + idx_end - idx_start;
Trond Myklebust1c759502006-10-09 16:18:38 -04001380 npages = l_npages;
1381 if (sizeof(npages) != sizeof(l_npages) &&
Trond Myklebustca52fec2007-04-17 17:22:13 -04001382 (pgoff_t)npages != l_npages)
Trond Myklebust1c759502006-10-09 16:18:38 -04001383 npages = 0;
1384 }
1385 }
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001386 how &= ~FLUSH_NOCOMMIT;
Trond Myklebust587142f2007-07-02 09:57:54 -04001387 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 do {
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001389 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1390 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001391 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001392 if (nocommit)
1393 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001394 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebust724c4392007-04-17 17:22:13 -04001395 if (pages == 0)
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001396 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001397 if (how & FLUSH_INVALIDATE) {
Trond Myklebust587142f2007-07-02 09:57:54 -04001398 spin_unlock(&inode->i_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001399 nfs_cancel_commit_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001400 ret = pages;
Trond Myklebust587142f2007-07-02 09:57:54 -04001401 spin_lock(&inode->i_lock);
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001402 continue;
1403 }
1404 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebust587142f2007-07-02 09:57:54 -04001405 spin_unlock(&inode->i_lock);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001406 ret = nfs_commit_list(inode, &head, how);
Trond Myklebust587142f2007-07-02 09:57:54 -04001407 spin_lock(&inode->i_lock);
1408
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001409 } while (ret >= 0);
Trond Myklebust587142f2007-07-02 09:57:54 -04001410 spin_unlock(&inode->i_lock);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001411 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
1413
Trond Myklebust34901f72007-07-25 14:09:54 -04001414static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
Trond Myklebust1c759502006-10-09 16:18:38 -04001415{
Trond Myklebust1c759502006-10-09 16:18:38 -04001416 int ret;
1417
Trond Myklebust34901f72007-07-25 14:09:54 -04001418 ret = nfs_writepages(mapping, wbc);
Trond Myklebust61822ab2006-12-05 00:35:42 -05001419 if (ret < 0)
1420 goto out;
Trond Myklebust34901f72007-07-25 14:09:54 -04001421 ret = nfs_sync_mapping_wait(mapping, wbc, how);
Trond Myklebusted90ef52007-07-20 13:13:28 -04001422 if (ret < 0)
1423 goto out;
1424 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001425out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001426 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Trond Myklebust1c759502006-10-09 16:18:38 -04001427 return ret;
1428}
1429
Trond Myklebust34901f72007-07-25 14:09:54 -04001430/* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
1431static int nfs_write_mapping(struct address_space *mapping, int how)
1432{
1433 struct writeback_control wbc = {
1434 .bdi = mapping->backing_dev_info,
1435 .sync_mode = WB_SYNC_NONE,
1436 .nr_to_write = LONG_MAX,
Trond Myklebustd7fb1202008-10-06 20:08:56 -04001437 .range_start = 0,
1438 .range_end = LLONG_MAX,
Trond Myklebust34901f72007-07-25 14:09:54 -04001439 .for_writepages = 1,
Trond Myklebust34901f72007-07-25 14:09:54 -04001440 };
1441 int ret;
1442
1443 ret = __nfs_write_mapping(mapping, &wbc, how);
1444 if (ret < 0)
1445 return ret;
1446 wbc.sync_mode = WB_SYNC_ALL;
1447 return __nfs_write_mapping(mapping, &wbc, how);
1448}
1449
Trond Myklebusted90ef52007-07-20 13:13:28 -04001450/*
1451 * flush the inode to disk.
1452 */
1453int nfs_wb_all(struct inode *inode)
Trond Myklebust1c759502006-10-09 16:18:38 -04001454{
Trond Myklebusted90ef52007-07-20 13:13:28 -04001455 return nfs_write_mapping(inode->i_mapping, 0);
1456}
Trond Myklebust1c759502006-10-09 16:18:38 -04001457
Trond Myklebusted90ef52007-07-20 13:13:28 -04001458int nfs_wb_nocommit(struct inode *inode)
1459{
1460 return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
Trond Myklebust1c759502006-10-09 16:18:38 -04001461}
1462
Trond Myklebust1b3b4a12007-08-28 10:29:36 -04001463int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1464{
1465 struct nfs_page *req;
1466 loff_t range_start = page_offset(page);
1467 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1468 struct writeback_control wbc = {
1469 .bdi = page->mapping->backing_dev_info,
1470 .sync_mode = WB_SYNC_ALL,
1471 .nr_to_write = LONG_MAX,
1472 .range_start = range_start,
1473 .range_end = range_end,
1474 };
1475 int ret = 0;
1476
1477 BUG_ON(!PageLocked(page));
1478 for (;;) {
1479 req = nfs_page_find_request(page);
1480 if (req == NULL)
1481 goto out;
Trond Myklebuste468bae2008-06-13 13:25:22 -04001482 if (test_bit(PG_CLEAN, &req->wb_flags)) {
Trond Myklebust1b3b4a12007-08-28 10:29:36 -04001483 nfs_release_request(req);
1484 break;
1485 }
1486 if (nfs_lock_request_dontget(req)) {
1487 nfs_inode_remove_request(req);
1488 /*
1489 * In case nfs_inode_remove_request has marked the
1490 * page as being dirty
1491 */
1492 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1493 nfs_unlock_request(req);
1494 break;
1495 }
1496 ret = nfs_wait_on_request(req);
1497 if (ret < 0)
1498 goto out;
1499 }
1500 if (!PagePrivate(page))
1501 return 0;
1502 ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
1503out:
1504 return ret;
1505}
1506
Adrian Bunk5334eb12007-11-21 15:04:31 -08001507static int nfs_wb_page_priority(struct inode *inode, struct page *page,
1508 int how)
Trond Myklebust1c759502006-10-09 16:18:38 -04001509{
1510 loff_t range_start = page_offset(page);
1511 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001512 struct writeback_control wbc = {
1513 .bdi = page->mapping->backing_dev_info,
1514 .sync_mode = WB_SYNC_ALL,
1515 .nr_to_write = LONG_MAX,
1516 .range_start = range_start,
1517 .range_end = range_end,
1518 };
1519 int ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001520
Trond Myklebust73e33022008-04-11 16:03:54 -04001521 do {
1522 if (clear_page_dirty_for_io(page)) {
1523 ret = nfs_writepage_locked(page, &wbc);
1524 if (ret < 0)
1525 goto out_error;
1526 } else if (!PagePrivate(page))
1527 break;
1528 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001529 if (ret < 0)
Trond Myklebust73e33022008-04-11 16:03:54 -04001530 goto out_error;
1531 } while (PagePrivate(page));
1532 return 0;
1533out_error:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001534 __mark_inode_dirty(inode, I_DIRTY_PAGES);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001535 return ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001536}
1537
1538/*
1539 * Write back all requests on one page - we do this before reading it.
1540 */
1541int nfs_wb_page(struct inode *inode, struct page* page)
1542{
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001543 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
Trond Myklebust1c759502006-10-09 16:18:38 -04001544}
1545
David Howellsf7b422b2006-06-09 09:34:33 -04001546int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1549 sizeof(struct nfs_write_data),
1550 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001551 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 if (nfs_wdata_cachep == NULL)
1553 return -ENOMEM;
1554
Matthew Dobson93d23412006-03-26 01:37:50 -08001555 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1556 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (nfs_wdata_mempool == NULL)
1558 return -ENOMEM;
1559
Matthew Dobson93d23412006-03-26 01:37:50 -08001560 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1561 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 if (nfs_commit_mempool == NULL)
1563 return -ENOMEM;
1564
Peter Zijlstra89a09142007-03-16 13:38:26 -08001565 /*
1566 * NFS congestion size, scale with available memory.
1567 *
1568 * 64MB: 8192k
1569 * 128MB: 11585k
1570 * 256MB: 16384k
1571 * 512MB: 23170k
1572 * 1GB: 32768k
1573 * 2GB: 46340k
1574 * 4GB: 65536k
1575 * 8GB: 92681k
1576 * 16GB: 131072k
1577 *
1578 * This allows larger machines to have larger/more transfers.
1579 * Limit the default to 256M
1580 */
1581 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1582 if (nfs_congestion_kb > 256*1024)
1583 nfs_congestion_kb = 256*1024;
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return 0;
1586}
1587
David Brownell266bee82006-06-27 12:59:15 -07001588void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
1590 mempool_destroy(nfs_commit_mempool);
1591 mempool_destroy(nfs_wdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001592 kmem_cache_destroy(nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594