blob: a630ad65d64c3bf99eab728f8093f011241a6b17 [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>
Trond Myklebust074cc1d2009-08-10 08:54:13 -040016#include <linux/migrate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <linux/sunrpc/clnt.h>
19#include <linux/nfs_fs.h>
20#include <linux/nfs_mount.h>
21#include <linux/nfs_page.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070022#include <linux/backing-dev.h>
Paul Gortmakerafeacc82011-05-26 16:00:52 -040023#include <linux/export.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include "delegation.h"
Trond Myklebust49a70f22006-12-05 00:35:38 -050028#include "internal.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050029#include "iostat.h"
Andy Adamsondef6ed72009-04-01 09:22:26 -040030#include "nfs4_fs.h"
Trond Myklebust074cc1d2009-08-10 08:54:13 -040031#include "fscache.h"
Fred Isaman94ad1c82011-03-01 01:34:14 +000032#include "pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#define NFSDBG_FACILITY NFSDBG_PAGECACHE
35
36#define MIN_POOL_WRITE (32)
37#define MIN_POOL_COMMIT (4)
38
39/*
40 * Local function declarations
41 */
Trond Myklebustc63c7b02007-04-02 19:29:52 -040042static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc,
43 struct inode *inode, int ioflags);
Fred Isamanf8512ad2008-03-19 11:24:39 -040044static void nfs_redirty_request(struct nfs_page *req);
Trond Myklebust788e7a82006-03-20 13:44:27 -050045static const struct rpc_call_ops nfs_write_partial_ops;
46static const struct rpc_call_ops nfs_write_full_ops;
47static const struct rpc_call_ops nfs_commit_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Christoph Lametere18b8902006-12-06 20:33:20 -080049static struct kmem_cache *nfs_wdata_cachep;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050050static mempool_t *nfs_wdata_mempool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static mempool_t *nfs_commit_mempool;
52
Trond Myklebustc9d8f892008-04-15 16:56:39 -040053struct nfs_write_data *nfs_commitdata_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Christoph Lametere6b4f8d2006-12-06 20:33:14 -080055 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
Chuck Lever40859d72005-11-30 18:09:02 -050056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 if (p) {
58 memset(p, 0, sizeof(*p));
59 INIT_LIST_HEAD(&p->pages);
60 }
61 return p;
62}
Fred Isamane0c2b382011-03-23 13:27:53 +000063EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Trond Myklebust5e4424a2008-02-25 21:53:49 -080065void nfs_commit_free(struct nfs_write_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Chuck Lever40859d72005-11-30 18:09:02 -050067 if (p && (p->pagevec != &p->page_array[0]))
68 kfree(p->pagevec);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 mempool_free(p, nfs_commit_mempool);
70}
Fred Isamane0c2b382011-03-23 13:27:53 +000071EXPORT_SYMBOL_GPL(nfs_commit_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Trond Myklebust8d5658c2007-04-10 09:26:35 -040073struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050074{
Christoph Lametere6b4f8d2006-12-06 20:33:14 -080075 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050076
77 if (p) {
78 memset(p, 0, sizeof(*p));
79 INIT_LIST_HEAD(&p->pages);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070080 p->npages = pagecount;
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040081 if (pagecount <= ARRAY_SIZE(p->page_array))
82 p->pagevec = p->page_array;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050083 else {
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040084 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
85 if (!p->pagevec) {
Trond Myklebust3feb2d42006-03-20 13:44:37 -050086 mempool_free(p, nfs_wdata_mempool);
87 p = NULL;
88 }
89 }
90 }
91 return p;
92}
93
Trond Myklebust1ae88b22009-08-12 09:12:30 -040094void nfs_writedata_free(struct nfs_write_data *p)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050095{
96 if (p && (p->pagevec != &p->page_array[0]))
97 kfree(p->pagevec);
98 mempool_free(p, nfs_wdata_mempool);
99}
100
Trond Myklebustdce81292011-07-13 15:59:19 -0400101void nfs_writedata_release(struct nfs_write_data *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Fred Isaman5053aa52011-03-03 15:13:43 +0000103 put_lseg(wdata->lseg);
Trond Myklebust383ba712008-02-19 20:04:20 -0500104 put_nfs_open_context(wdata->args.context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 nfs_writedata_free(wdata);
106}
107
Trond Myklebust7b159fc2007-07-25 14:09:54 -0400108static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
109{
110 ctx->error = error;
111 smp_wmb();
112 set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
113}
114
Trond Myklebust277459d2006-12-05 00:35:35 -0500115static struct nfs_page *nfs_page_find_request_locked(struct page *page)
116{
117 struct nfs_page *req = NULL;
118
119 if (PagePrivate(page)) {
120 req = (struct nfs_page *)page_private(page);
121 if (req != NULL)
Trond Myklebustc03b4022007-06-17 13:26:38 -0400122 kref_get(&req->wb_kref);
Trond Myklebust277459d2006-12-05 00:35:35 -0500123 }
124 return req;
125}
126
127static struct nfs_page *nfs_page_find_request(struct page *page)
128{
Trond Myklebust587142f2007-07-02 09:57:54 -0400129 struct inode *inode = page->mapping->host;
Trond Myklebust277459d2006-12-05 00:35:35 -0500130 struct nfs_page *req = NULL;
Trond Myklebust277459d2006-12-05 00:35:35 -0500131
Trond Myklebust587142f2007-07-02 09:57:54 -0400132 spin_lock(&inode->i_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500133 req = nfs_page_find_request_locked(page);
Trond Myklebust587142f2007-07-02 09:57:54 -0400134 spin_unlock(&inode->i_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500135 return req;
136}
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/* Adjust the file length if we're writing beyond the end */
139static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
140{
141 struct inode *inode = page->mapping->host;
Trond Myklebusta3d01452008-06-11 12:21:19 -0400142 loff_t end, i_size;
143 pgoff_t end_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Trond Myklebusta3d01452008-06-11 12:21:19 -0400145 spin_lock(&inode->i_lock);
146 i_size = i_size_read(inode);
147 end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (i_size > 0 && page->index < end_index)
Trond Myklebusta3d01452008-06-11 12:21:19 -0400149 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
151 if (i_size >= end)
Trond Myklebusta3d01452008-06-11 12:21:19 -0400152 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 i_size_write(inode, end);
Trond Myklebusta3d01452008-06-11 12:21:19 -0400154 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
155out:
156 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Trond Myklebusta301b772007-02-06 11:07:15 -0800159/* A writeback failed: mark the page as bad, and invalidate the page cache */
160static void nfs_set_pageerror(struct page *page)
161{
162 SetPageError(page);
163 nfs_zap_mapping(page->mapping->host, page->mapping);
164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/* We can set the PG_uptodate flag if we see that a write request
167 * covers the full page.
168 */
169static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
170{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (PageUptodate(page))
172 return;
173 if (base != 0)
174 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500175 if (count != nfs_page_length(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500177 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static int wb_priority(struct writeback_control *wbc)
181{
182 if (wbc->for_reclaim)
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400183 return FLUSH_HIGHPRI | FLUSH_STABLE;
Wu Fengguangb17621f2009-12-03 13:54:25 +0100184 if (wbc->for_kupdate || wbc->for_background)
Trond Myklebustb31268a2011-03-21 17:02:00 -0400185 return FLUSH_LOWPRI | FLUSH_COND_STABLE;
186 return FLUSH_COND_STABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
189/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800190 * NFS congestion control
191 */
192
193int nfs_congestion_kb;
194
195#define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
196#define NFS_CONGESTION_OFF_THRESH \
197 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
198
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400199static int nfs_set_page_writeback(struct page *page)
Peter Zijlstra89a09142007-03-16 13:38:26 -0800200{
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400201 int ret = test_set_page_writeback(page);
202
203 if (!ret) {
Peter Zijlstra89a09142007-03-16 13:38:26 -0800204 struct inode *inode = page->mapping->host;
205 struct nfs_server *nfss = NFS_SERVER(inode);
206
Trond Myklebusta6305dd2010-04-09 19:07:08 -0400207 page_cache_get(page);
Peter Zijlstra277866a2007-05-08 00:35:12 -0700208 if (atomic_long_inc_return(&nfss->writeback) >
Jens Axboe8aa7e842009-07-09 14:52:32 +0200209 NFS_CONGESTION_ON_THRESH) {
210 set_bdi_congested(&nfss->backing_dev_info,
211 BLK_RW_ASYNC);
212 }
Peter Zijlstra89a09142007-03-16 13:38:26 -0800213 }
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400214 return ret;
Peter Zijlstra89a09142007-03-16 13:38:26 -0800215}
216
217static void nfs_end_page_writeback(struct page *page)
218{
219 struct inode *inode = page->mapping->host;
220 struct nfs_server *nfss = NFS_SERVER(inode);
221
222 end_page_writeback(page);
Trond Myklebusta6305dd2010-04-09 19:07:08 -0400223 page_cache_release(page);
Peter Zijlstrac4dc4be2007-10-16 23:25:41 -0700224 if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
Jens Axboe8aa7e842009-07-09 14:52:32 +0200225 clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
Peter Zijlstra89a09142007-03-16 13:38:26 -0800226}
227
Trond Myklebustcfb506e2010-07-30 15:31:57 -0400228static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock)
Trond Myklebuste261f512006-12-05 00:35:41 -0500229{
Trond Myklebust587142f2007-07-02 09:57:54 -0400230 struct inode *inode = page->mapping->host;
Trond Myklebuste261f512006-12-05 00:35:41 -0500231 struct nfs_page *req;
Trond Myklebuste261f512006-12-05 00:35:41 -0500232 int ret;
233
Trond Myklebust587142f2007-07-02 09:57:54 -0400234 spin_lock(&inode->i_lock);
Trond Myklebust074cc1d2009-08-10 08:54:13 -0400235 for (;;) {
Trond Myklebuste261f512006-12-05 00:35:41 -0500236 req = nfs_page_find_request_locked(page);
Trond Myklebust074cc1d2009-08-10 08:54:13 -0400237 if (req == NULL)
238 break;
Fred Isaman9994b622012-03-08 17:29:34 -0500239 if (nfs_lock_request_dontget(req))
Trond Myklebuste261f512006-12-05 00:35:41 -0500240 break;
241 /* Note: If we hold the page lock, as is the case in nfs_writepage,
Fred Isaman9994b622012-03-08 17:29:34 -0500242 * then the call to nfs_lock_request_dontget() will always
Trond Myklebuste261f512006-12-05 00:35:41 -0500243 * succeed provided that someone hasn't already marked the
244 * request as dirty (in which case we don't care).
245 */
Trond Myklebust587142f2007-07-02 09:57:54 -0400246 spin_unlock(&inode->i_lock);
Trond Myklebustcfb506e2010-07-30 15:31:57 -0400247 if (!nonblock)
248 ret = nfs_wait_on_request(req);
249 else
250 ret = -EAGAIN;
Trond Myklebuste261f512006-12-05 00:35:41 -0500251 nfs_release_request(req);
252 if (ret != 0)
Trond Myklebust074cc1d2009-08-10 08:54:13 -0400253 return ERR_PTR(ret);
Trond Myklebust587142f2007-07-02 09:57:54 -0400254 spin_lock(&inode->i_lock);
Trond Myklebuste261f512006-12-05 00:35:41 -0500255 }
Trond Myklebust587142f2007-07-02 09:57:54 -0400256 spin_unlock(&inode->i_lock);
Trond Myklebust074cc1d2009-08-10 08:54:13 -0400257 return req;
258}
259
260/*
261 * Find an associated nfs write request, and prepare to flush it out
262 * May return an error if the user signalled nfs_wait_on_request().
263 */
264static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
Trond Myklebustcfb506e2010-07-30 15:31:57 -0400265 struct page *page, bool nonblock)
Trond Myklebust074cc1d2009-08-10 08:54:13 -0400266{
267 struct nfs_page *req;
268 int ret = 0;
269
Trond Myklebustcfb506e2010-07-30 15:31:57 -0400270 req = nfs_find_and_lock_request(page, nonblock);
Trond Myklebust074cc1d2009-08-10 08:54:13 -0400271 if (!req)
272 goto out;
273 ret = PTR_ERR(req);
274 if (IS_ERR(req))
275 goto out;
276
277 ret = nfs_set_page_writeback(page);
278 BUG_ON(ret != 0);
279 BUG_ON(test_bit(PG_CLEAN, &req->wb_flags));
280
Fred Isamanf8512ad2008-03-19 11:24:39 -0400281 if (!nfs_pageio_add_request(pgio, req)) {
282 nfs_redirty_request(req);
Trond Myklebust074cc1d2009-08-10 08:54:13 -0400283 ret = pgio->pg_error;
Fred Isamanf8512ad2008-03-19 11:24:39 -0400284 }
Trond Myklebust074cc1d2009-08-10 08:54:13 -0400285out:
286 return ret;
Trond Myklebuste261f512006-12-05 00:35:41 -0500287}
288
Trond Myklebustf758c8852007-07-22 19:27:32 -0400289static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
290{
291 struct inode *inode = page->mapping->host;
Trond Myklebustcfb506e2010-07-30 15:31:57 -0400292 int ret;
Trond Myklebustf758c8852007-07-22 19:27:32 -0400293
294 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
295 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
296
297 nfs_pageio_cond_complete(pgio, page->index);
Wu Fengguang1b430be2010-10-26 14:21:26 -0700298 ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE);
Trond Myklebustcfb506e2010-07-30 15:31:57 -0400299 if (ret == -EAGAIN) {
300 redirty_page_for_writepage(wbc, page);
301 ret = 0;
302 }
303 return ret;
Trond Myklebustf758c8852007-07-22 19:27:32 -0400304}
305
Trond Myklebuste261f512006-12-05 00:35:41 -0500306/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 * Write an mmapped page to the server.
308 */
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500309static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Trond Myklebustf758c8852007-07-22 19:27:32 -0400311 struct nfs_pageio_descriptor pgio;
Trond Myklebuste261f512006-12-05 00:35:41 -0500312 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Trond Myklebustf758c8852007-07-22 19:27:32 -0400314 nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
315 err = nfs_do_writepage(page, wbc, &pgio);
316 nfs_pageio_complete(&pgio);
317 if (err < 0)
318 return err;
319 if (pgio.pg_error < 0)
320 return pgio.pg_error;
321 return 0;
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500322}
323
324int nfs_writepage(struct page *page, struct writeback_control *wbc)
325{
Trond Myklebustf758c8852007-07-22 19:27:32 -0400326 int ret;
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500327
Trond Myklebustf758c8852007-07-22 19:27:32 -0400328 ret = nfs_writepage_locked(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 unlock_page(page);
Trond Myklebustf758c8852007-07-22 19:27:32 -0400330 return ret;
331}
332
333static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
334{
335 int ret;
336
337 ret = nfs_do_writepage(page, wbc, data);
338 unlock_page(page);
339 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
343{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 struct inode *inode = mapping->host;
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400345 unsigned long *bitlock = &NFS_I(inode)->flags;
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400346 struct nfs_pageio_descriptor pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 int err;
348
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400349 /* Stop dirtying of new pages while we sync */
350 err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
351 nfs_wait_bit_killable, TASK_KILLABLE);
352 if (err)
353 goto out_err;
354
Chuck Lever91d5b472006-03-20 13:44:14 -0500355 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
356
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400357 nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
Trond Myklebustf758c8852007-07-22 19:27:32 -0400358 err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400359 nfs_pageio_complete(&pgio);
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400360
361 clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
362 smp_mb__after_clear_bit();
363 wake_up_bit(bitlock, NFS_INO_FLUSHING);
364
Trond Myklebustf758c8852007-07-22 19:27:32 -0400365 if (err < 0)
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400366 goto out_err;
367 err = pgio.pg_error;
368 if (err < 0)
369 goto out_err;
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400370 return 0;
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400371out_err:
372 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375/*
376 * Insert a write request into an inode
377 */
Fred Isamand6d6dc72012-03-08 17:29:35 -0500378static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebuste7d39062008-06-13 12:12:32 -0400381
382 /* Lock the request! */
383 nfs_lock_request_dontget(req);
384
385 spin_lock(&inode->i_lock);
Trond Myklebust4d65c522011-03-25 14:15:11 -0400386 if (!nfsi->npages && nfs_have_delegation(inode, FMODE_WRITE))
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700387 inode->i_version++;
Trond Myklebust2df485a2010-12-07 22:39:17 -0500388 set_bit(PG_MAPPED, &req->wb_flags);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500389 SetPagePrivate(req->wb_page);
Trond Myklebust277459d2006-12-05 00:35:35 -0500390 set_page_private(req->wb_page, (unsigned long)req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 nfsi->npages++;
Trond Myklebustc03b4022007-06-17 13:26:38 -0400392 kref_get(&req->wb_kref);
Trond Myklebuste7d39062008-06-13 12:12:32 -0400393 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
Fred Isamand6d6dc72012-03-08 17:29:35 -0500396static struct pnfs_layout_segment *nfs_clear_request_commit(struct nfs_page *req);
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800399 * Remove a write request from an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 */
401static void nfs_inode_remove_request(struct nfs_page *req)
402{
Al Viro3d4ff432011-06-22 18:40:12 -0400403 struct inode *inode = req->wb_context->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 struct nfs_inode *nfsi = NFS_I(inode);
Fred Isamand6d6dc72012-03-08 17:29:35 -0500405 struct pnfs_layout_segment *lseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 BUG_ON (!NFS_WBACK_BUSY(req));
408
Trond Myklebust587142f2007-07-02 09:57:54 -0400409 spin_lock(&inode->i_lock);
Fred Isamand6d6dc72012-03-08 17:29:35 -0500410 lseg = nfs_clear_request_commit(req);
Trond Myklebust277459d2006-12-05 00:35:35 -0500411 set_page_private(req->wb_page, 0);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500412 ClearPagePrivate(req->wb_page);
Trond Myklebust2df485a2010-12-07 22:39:17 -0500413 clear_bit(PG_MAPPED, &req->wb_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 nfsi->npages--;
Trond Myklebust4d65c522011-03-25 14:15:11 -0400415 spin_unlock(&inode->i_lock);
Fred Isamand6d6dc72012-03-08 17:29:35 -0500416 put_lseg(lseg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 nfs_release_request(req);
418}
419
Trond Myklebust61822ab2006-12-05 00:35:42 -0500420static void
Fred6d884e82008-03-19 11:24:38 -0400421nfs_mark_request_dirty(struct nfs_page *req)
Trond Myklebust61822ab2006-12-05 00:35:42 -0500422{
Trond Myklebust61822ab2006-12-05 00:35:42 -0500423 __set_page_dirty_nobuffers(req->wb_page);
424}
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
427/*
428 * Add a request to the inode's commit list.
429 */
430static void
Fred Isamana861a1e2011-03-23 13:27:51 +0000431nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Al Viro3d4ff432011-06-22 18:40:12 -0400433 struct inode *inode = req->wb_context->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 struct nfs_inode *nfsi = NFS_I(inode);
Fred Isamand6d6dc72012-03-08 17:29:35 -0500435 struct list_head *clist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Fred Isamand6d6dc72012-03-08 17:29:35 -0500437 clist = pnfs_choose_commit_list(req, lseg);
Trond Myklebust587142f2007-07-02 09:57:54 -0400438 spin_lock(&inode->i_lock);
Trond Myklebuste468bae2008-06-13 13:25:22 -0400439 set_bit(PG_CLEAN, &(req)->wb_flags);
Fred Isamand6d6dc72012-03-08 17:29:35 -0500440 nfs_list_add_request(req, clist);
Trond Myklebustff778d02010-02-19 16:53:39 -0800441 nfsi->ncommit++;
Trond Myklebust587142f2007-07-02 09:57:54 -0400442 spin_unlock(&inode->i_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700443 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -0700444 inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
Trond Myklebusta1803042006-12-05 00:45:19 -0500445 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400447
Fred Isamand6d6dc72012-03-08 17:29:35 -0500448static void
449nfs_clear_page_commit(struct page *page)
450{
451 dec_zone_page_state(page, NR_UNSTABLE_NFS);
452 dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE);
453}
454
455static struct pnfs_layout_segment *
Trond Myklebuste468bae2008-06-13 13:25:22 -0400456nfs_clear_request_commit(struct nfs_page *req)
457{
Fred Isamand6d6dc72012-03-08 17:29:35 -0500458 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebuste468bae2008-06-13 13:25:22 -0400459
460 if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) {
Fred Isamand6d6dc72012-03-08 17:29:35 -0500461 nfs_clear_page_commit(req->wb_page);
462 lseg = pnfs_clear_request_commit(req);
463 NFS_I(req->wb_context->dentry->d_inode)->ncommit--;
464 list_del(&req->wb_list);
Trond Myklebuste468bae2008-06-13 13:25:22 -0400465 }
Fred Isamand6d6dc72012-03-08 17:29:35 -0500466 return lseg;
Trond Myklebuste468bae2008-06-13 13:25:22 -0400467}
468
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400469static inline
470int nfs_write_need_commit(struct nfs_write_data *data)
471{
Fred Isaman465d5242011-03-23 13:27:44 +0000472 if (data->verf.committed == NFS_DATA_SYNC)
473 return data->lseg == NULL;
474 else
475 return data->verf.committed != NFS_FILE_SYNC;
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400476}
477
478static inline
Fred Isamana861a1e2011-03-23 13:27:51 +0000479int nfs_reschedule_unstable_write(struct nfs_page *req,
480 struct nfs_write_data *data)
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400481{
Trond Myklebuste468bae2008-06-13 13:25:22 -0400482 if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) {
Fred Isamana861a1e2011-03-23 13:27:51 +0000483 nfs_mark_request_commit(req, data->lseg);
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400484 return 1;
485 }
486 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
Fred6d884e82008-03-19 11:24:38 -0400487 nfs_mark_request_dirty(req);
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400488 return 1;
489 }
490 return 0;
491}
492#else
493static inline void
Fred Isamana861a1e2011-03-23 13:27:51 +0000494nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg)
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400495{
496}
497
Fred Isamand6d6dc72012-03-08 17:29:35 -0500498static inline struct pnfs_layout_segment *
Trond Myklebuste468bae2008-06-13 13:25:22 -0400499nfs_clear_request_commit(struct nfs_page *req)
500{
Fred Isamand6d6dc72012-03-08 17:29:35 -0500501 return NULL;
Trond Myklebuste468bae2008-06-13 13:25:22 -0400502}
503
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400504static inline
505int nfs_write_need_commit(struct nfs_write_data *data)
506{
507 return 0;
508}
509
510static inline
Fred Isamana861a1e2011-03-23 13:27:51 +0000511int nfs_reschedule_unstable_write(struct nfs_page *req,
512 struct nfs_write_data *data)
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400513{
514 return 0;
515}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516#endif
517
Trond Myklebust47c62562009-03-16 08:13:41 -0400518#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebustfb8a1f12009-03-11 14:10:29 -0400519static int
520nfs_need_commit(struct nfs_inode *nfsi)
521{
Fred Isamand6d6dc72012-03-08 17:29:35 -0500522 return nfsi->ncommit > 0;
Trond Myklebustfb8a1f12009-03-11 14:10:29 -0400523}
524
Fred Isamand6d6dc72012-03-08 17:29:35 -0500525/* i_lock held by caller */
526int
527nfs_scan_commit_list(struct list_head *src, struct list_head *dst, int max)
528{
529 struct nfs_page *req, *tmp;
530 int ret = 0;
531
532 list_for_each_entry_safe(req, tmp, src, wb_list) {
533 if (nfs_lock_request_dontget(req)) {
534 kref_get(&req->wb_kref);
535 list_move_tail(&req->wb_list, dst);
536 clear_bit(PG_CLEAN, &(req)->wb_flags);
537 ret++;
538 if (ret == max)
539 break;
540 }
541 }
542 return ret;
543}
544EXPORT_SYMBOL_GPL(nfs_scan_commit_list);
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546/*
547 * nfs_scan_commit - Scan an inode for commit requests
548 * @inode: NFS inode to scan
549 * @dst: destination list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 *
551 * Moves requests from the inode's 'commit' request list.
552 * The requests are *not* checked to ensure that they form a contiguous set.
553 */
554static int
Fred Isamand6d6dc72012-03-08 17:29:35 -0500555nfs_scan_commit(struct inode *inode, struct list_head *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 struct nfs_inode *nfsi = NFS_I(inode);
Fred Isamand6d6dc72012-03-08 17:29:35 -0500558 int ret = 0;
Trond Myklebustfb8a1f12009-03-11 14:10:29 -0400559
Dave Chinner0d88f6e2011-04-12 19:18:08 +1000560 spin_lock(&inode->i_lock);
Fred Isamand6d6dc72012-03-08 17:29:35 -0500561 if (nfsi->ncommit > 0) {
562 int pnfs_ret;
563
564 ret = nfs_scan_commit_list(&nfsi->commit_list, dst, INT_MAX);
565 pnfs_ret = pnfs_scan_commit_lists(inode, INT_MAX - ret);
566 if (pnfs_ret) {
567 ret += pnfs_ret;
568 set_bit(NFS_INO_PNFS_COMMIT, &nfsi->flags);
569 }
Trond Myklebustff778d02010-02-19 16:53:39 -0800570 nfsi->ncommit -= ret;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500571 }
Dave Chinner0d88f6e2011-04-12 19:18:08 +1000572 spin_unlock(&inode->i_lock);
Trond Myklebustff778d02010-02-19 16:53:39 -0800573 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
Fred Isamand6d6dc72012-03-08 17:29:35 -0500575
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500576#else
Trond Myklebustfb8a1f12009-03-11 14:10:29 -0400577static inline int nfs_need_commit(struct nfs_inode *nfsi)
578{
579 return 0;
580}
581
Fred Isamand6d6dc72012-03-08 17:29:35 -0500582static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst)
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500583{
584 return 0;
585}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586#endif
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588/*
Trond Myklebuste7d39062008-06-13 12:12:32 -0400589 * Search for an existing write request, and attempt to update
590 * it to reflect a new dirty region on a given page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 *
Trond Myklebuste7d39062008-06-13 12:12:32 -0400592 * If the attempt fails, then the existing request is flushed out
593 * to disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 */
Trond Myklebuste7d39062008-06-13 12:12:32 -0400595static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
596 struct page *page,
597 unsigned int offset,
598 unsigned int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Trond Myklebuste7d39062008-06-13 12:12:32 -0400600 struct nfs_page *req;
601 unsigned int rqend;
602 unsigned int end;
603 int error;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500604 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebuste7d39062008-06-13 12:12:32 -0400605
606 if (!PagePrivate(page))
607 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 end = offset + bytes;
Trond Myklebuste7d39062008-06-13 12:12:32 -0400610 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 for (;;) {
Trond Myklebust277459d2006-12-05 00:35:35 -0500613 req = nfs_page_find_request_locked(page);
Trond Myklebuste7d39062008-06-13 12:12:32 -0400614 if (req == NULL)
615 goto out_unlock;
Trond Myklebust277459d2006-12-05 00:35:35 -0500616
Trond Myklebuste7d39062008-06-13 12:12:32 -0400617 rqend = req->wb_offset + req->wb_bytes;
618 /*
619 * Tell the caller to flush out the request if
620 * the offsets are non-contiguous.
621 * Note: nfs_flush_incompatible() will already
622 * have flushed out requests having wrong owners.
623 */
Trond Myklebuste468bae2008-06-13 13:25:22 -0400624 if (offset > rqend
Trond Myklebuste7d39062008-06-13 12:12:32 -0400625 || end < req->wb_offset)
626 goto out_flushme;
627
Fred Isaman9994b622012-03-08 17:29:34 -0500628 if (nfs_lock_request_dontget(req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Trond Myklebuste7d39062008-06-13 12:12:32 -0400631 /* The request is locked, so wait and then retry */
Trond Myklebust587142f2007-07-02 09:57:54 -0400632 spin_unlock(&inode->i_lock);
Trond Myklebuste7d39062008-06-13 12:12:32 -0400633 error = nfs_wait_on_request(req);
634 nfs_release_request(req);
635 if (error != 0)
636 goto out_err;
637 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639
Fred Isamand6d6dc72012-03-08 17:29:35 -0500640 lseg = nfs_clear_request_commit(req);
Trond Myklebuste468bae2008-06-13 13:25:22 -0400641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 /* Okay, the request matches. Update the region */
643 if (offset < req->wb_offset) {
644 req->wb_offset = offset;
645 req->wb_pgbase = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (end > rqend)
648 req->wb_bytes = end - req->wb_offset;
Trond Myklebuste7d39062008-06-13 12:12:32 -0400649 else
650 req->wb_bytes = rqend - req->wb_offset;
651out_unlock:
652 spin_unlock(&inode->i_lock);
Fred Isamand6d6dc72012-03-08 17:29:35 -0500653 put_lseg(lseg);
Trond Myklebuste7d39062008-06-13 12:12:32 -0400654 return req;
655out_flushme:
656 spin_unlock(&inode->i_lock);
657 nfs_release_request(req);
658 error = nfs_wb_page(inode, page);
659out_err:
660 return ERR_PTR(error);
661}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Trond Myklebuste7d39062008-06-13 12:12:32 -0400663/*
664 * Try to update an existing write request, or create one if there is none.
665 *
666 * Note: Should always be called with the Page Lock held to prevent races
667 * if we have to add a new request. Also assumes that the caller has
668 * already called nfs_flush_incompatible() if necessary.
669 */
670static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
671 struct page *page, unsigned int offset, unsigned int bytes)
672{
673 struct inode *inode = page->mapping->host;
674 struct nfs_page *req;
Trond Myklebuste7d39062008-06-13 12:12:32 -0400675
676 req = nfs_try_to_update_request(inode, page, offset, bytes);
677 if (req != NULL)
678 goto out;
679 req = nfs_create_request(ctx, inode, page, offset, bytes);
680 if (IS_ERR(req))
681 goto out;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500682 nfs_inode_add_request(inode, req);
Trond Myklebustefc91ed2008-06-10 18:31:00 -0400683out:
Trond Myklebust61e930a2007-10-18 17:08:05 -0400684 return req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
Trond Myklebuste7d39062008-06-13 12:12:32 -0400687static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
688 unsigned int offset, unsigned int count)
689{
690 struct nfs_page *req;
691
692 req = nfs_setup_write_request(ctx, page, offset, count);
693 if (IS_ERR(req))
694 return PTR_ERR(req);
695 /* Update file length */
696 nfs_grow_file(page, offset, count);
697 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
Trond Myklebusta6305dd2010-04-09 19:07:08 -0400698 nfs_mark_request_dirty(req);
Fred Isaman9994b622012-03-08 17:29:34 -0500699 nfs_unlock_request(req);
Trond Myklebuste7d39062008-06-13 12:12:32 -0400700 return 0;
701}
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703int nfs_flush_incompatible(struct file *file, struct page *page)
704{
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400705 struct nfs_open_context *ctx = nfs_file_open_context(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 struct nfs_page *req;
Trond Myklebust1a545332006-12-05 00:35:40 -0500707 int do_flush, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 /*
709 * Look for a request corresponding to this page. If there
710 * is one, and it belongs to another file, we flush it out
711 * before we try to copy anything into the page. Do this
712 * due to the lack of an ACCESS-type call in NFSv2.
713 * Also do the same if we find a request from an existing
714 * dropped page.
715 */
Trond Myklebust1a545332006-12-05 00:35:40 -0500716 do {
717 req = nfs_page_find_request(page);
718 if (req == NULL)
719 return 0;
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400720 do_flush = req->wb_page != page || req->wb_context != ctx ||
721 req->wb_lock_context->lockowner != current->files ||
722 req->wb_lock_context->pid != current->tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 nfs_release_request(req);
Trond Myklebust1a545332006-12-05 00:35:40 -0500724 if (!do_flush)
725 return 0;
726 status = nfs_wb_page(page->mapping->host, page);
727 } while (status == 0);
728 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
731/*
Trond Myklebust5d47a352008-02-07 17:24:07 -0500732 * If the page cache is marked as unsafe or invalid, then we can't rely on
733 * the PageUptodate() flag. In this case, we will need to turn off
734 * write optimisations that depend on the page contents being correct.
735 */
736static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
737{
738 return PageUptodate(page) &&
739 !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
740}
741
742/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 * Update and possibly write a cached page of an NFS file.
744 *
745 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
746 * things with a page scheduled for an RPC call (e.g. invalidate it).
747 */
748int nfs_updatepage(struct file *file, struct page *page,
749 unsigned int offset, unsigned int count)
750{
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400751 struct nfs_open_context *ctx = nfs_file_open_context(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 int status = 0;
754
Chuck Lever91d5b472006-03-20 13:44:14 -0500755 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
756
Chuck Lever48186c72008-06-11 17:56:05 -0400757 dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n",
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800758 file->f_path.dentry->d_parent->d_name.name,
759 file->f_path.dentry->d_name.name, count,
Chuck Lever48186c72008-06-11 17:56:05 -0400760 (long long)(page_offset(page) + offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 /* If we're not using byte range locks, and we know the page
Trond Myklebust5d47a352008-02-07 17:24:07 -0500763 * is up to date, it may be more efficient to extend the write
764 * to cover the entire page in order to avoid fragmentation
765 * inefficiencies.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 */
Trond Myklebust5d47a352008-02-07 17:24:07 -0500767 if (nfs_write_pageuptodate(page, inode) &&
768 inode->i_flock == NULL &&
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +0100769 !(file->f_flags & O_DSYNC)) {
Trond Myklebust49a70f22006-12-05 00:35:38 -0500770 count = max(count + offset, nfs_page_length(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773
Trond Myklebuste21195a2006-12-05 00:35:39 -0500774 status = nfs_writepage_setup(ctx, page, offset, count);
Trond Myklebust03fa9e82008-06-05 16:02:35 -0400775 if (status < 0)
776 nfs_set_pageerror(page);
Trond Myklebust59b7c052011-10-17 18:22:55 -0700777 else
778 __set_page_dirty_nobuffers(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Chuck Lever48186c72008-06-11 17:56:05 -0400780 dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 status, (long long)i_size_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return status;
783}
784
Fred Isamana861a1e2011-03-23 13:27:51 +0000785static void nfs_writepage_release(struct nfs_page *req,
786 struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Trond Myklebusta6305dd2010-04-09 19:07:08 -0400788 struct page *page = req->wb_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Fred Isamana861a1e2011-03-23 13:27:51 +0000790 if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req, data))
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400791 nfs_inode_remove_request(req);
Fred Isaman9994b622012-03-08 17:29:34 -0500792 nfs_unlock_request(req);
Trond Myklebusta6305dd2010-04-09 19:07:08 -0400793 nfs_end_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
Trond Myklebust3ff75762007-07-14 15:40:00 -0400796static int flush_task_priority(int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
798 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
799 case FLUSH_HIGHPRI:
800 return RPC_PRIORITY_HIGH;
801 case FLUSH_LOWPRI:
802 return RPC_PRIORITY_LOW;
803 }
804 return RPC_PRIORITY_NORMAL;
805}
806
Fred Isamana69aef12011-03-03 15:13:47 +0000807int nfs_initiate_write(struct nfs_write_data *data,
Andy Adamsond138d5d2011-03-03 15:13:41 +0000808 struct rpc_clnt *clnt,
809 const struct rpc_call_ops *call_ops,
810 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Andy Adamsond138d5d2011-03-03 15:13:41 +0000812 struct inode *inode = data->inode;
Trond Myklebust3ff75762007-07-14 15:40:00 -0400813 int priority = flush_task_priority(how);
Trond Myklebust07737692007-10-25 18:42:54 -0400814 struct rpc_task *task;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400815 struct rpc_message msg = {
816 .rpc_argp = &data->args,
817 .rpc_resp = &data->res,
Andy Adamsond138d5d2011-03-03 15:13:41 +0000818 .rpc_cred = data->cred,
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400819 };
Trond Myklebust84115e12007-07-14 15:39:59 -0400820 struct rpc_task_setup task_setup_data = {
Andy Adamsond138d5d2011-03-03 15:13:41 +0000821 .rpc_client = clnt,
Trond Myklebust07737692007-10-25 18:42:54 -0400822 .task = &data->task,
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400823 .rpc_message = &msg,
Trond Myklebust84115e12007-07-14 15:39:59 -0400824 .callback_ops = call_ops,
825 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -0500826 .workqueue = nfsiod_workqueue,
Trond Myklebust2c61be02010-04-09 19:54:50 -0400827 .flags = RPC_TASK_ASYNC,
Trond Myklebust3ff75762007-07-14 15:40:00 -0400828 .priority = priority,
Trond Myklebust84115e12007-07-14 15:39:59 -0400829 };
Trond Myklebust2c61be02010-04-09 19:54:50 -0400830 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Andy Adamsond138d5d2011-03-03 15:13:41 +0000832 /* Set up the initial task struct. */
833 NFS_PROTO(inode)->write_setup(data, &msg);
834
835 dprintk("NFS: %5u initiated write call "
836 "(req %s/%lld, %u bytes @ offset %llu)\n",
837 data->task.tk_pid,
838 inode->i_sb->s_id,
839 (long long)NFS_FILEID(inode),
840 data->args.count,
841 (unsigned long long)data->args.offset);
842
843 task = rpc_run_task(&task_setup_data);
844 if (IS_ERR(task)) {
845 ret = PTR_ERR(task);
846 goto out;
847 }
848 if (how & FLUSH_SYNC) {
849 ret = rpc_wait_for_completion_task(task);
850 if (ret == 0)
851 ret = task->tk_status;
852 }
853 rpc_put_task(task);
854out:
855 return ret;
856}
Fred Isamana69aef12011-03-03 15:13:47 +0000857EXPORT_SYMBOL_GPL(nfs_initiate_write);
Andy Adamsond138d5d2011-03-03 15:13:41 +0000858
859/*
860 * Set up the argument/result storage required for the RPC call.
861 */
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400862static void nfs_write_rpcsetup(struct nfs_page *req,
Andy Adamsond138d5d2011-03-03 15:13:41 +0000863 struct nfs_write_data *data,
Andy Adamsond138d5d2011-03-03 15:13:41 +0000864 unsigned int count, unsigned int offset,
865 int how)
866{
Al Viro3d4ff432011-06-22 18:40:12 -0400867 struct inode *inode = req->wb_context->dentry->d_inode;
Andy Adamsond138d5d2011-03-03 15:13:41 +0000868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 /* Set up the RPC argument and reply structs
870 * NB: take care not to mess about with data->commit et al. */
871
872 data->req = req;
Al Viro3d4ff432011-06-22 18:40:12 -0400873 data->inode = inode = req->wb_context->dentry->d_inode;
Andy Adamsond138d5d2011-03-03 15:13:41 +0000874 data->cred = req->wb_context->cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 data->args.fh = NFS_FH(inode);
877 data->args.offset = req_offset(req) + offset;
Boaz Harrosh2bea0382011-06-16 11:35:46 -0400878 /* pnfs_set_layoutcommit needs this */
879 data->mds_offset = data->args.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 data->args.pgbase = req->wb_pgbase + offset;
881 data->args.pages = data->pagevec;
882 data->args.count = count;
Trond Myklebust383ba712008-02-19 20:04:20 -0500883 data->args.context = get_nfs_open_context(req->wb_context);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400884 data->args.lock_context = req->wb_lock_context;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400885 data->args.stable = NFS_UNSTABLE;
Trond Myklebust87ed5eb2011-07-12 13:42:01 -0400886 switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
887 case 0:
888 break;
889 case FLUSH_COND_STABLE:
890 if (nfs_need_commit(NFS_I(inode)))
891 break;
892 default:
893 data->args.stable = NFS_FILE_SYNC;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 data->res.fattr = &data->fattr;
897 data->res.count = count;
898 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400899 nfs_fattr_init(&data->fattr);
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400900}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400902static int nfs_do_write(struct nfs_write_data *data,
903 const struct rpc_call_ops *call_ops,
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400904 int how)
905{
Stephen Rothwell5f00bcb2011-07-25 13:59:46 -0400906 struct inode *inode = data->args.context->dentry->d_inode;
Andy Adamson0382b742011-03-03 15:13:45 +0000907
Andy Adamsond138d5d2011-03-03 15:13:41 +0000908 return nfs_initiate_write(data, NFS_CLIENT(inode), call_ops, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
Trond Myklebust275acaa2011-07-12 13:42:02 -0400911static int nfs_do_multiple_writes(struct list_head *head,
912 const struct rpc_call_ops *call_ops,
Trond Myklebust275acaa2011-07-12 13:42:02 -0400913 int how)
914{
915 struct nfs_write_data *data;
916 int ret = 0;
917
918 while (!list_empty(head)) {
919 int ret2;
920
921 data = list_entry(head->next, struct nfs_write_data, list);
922 list_del_init(&data->list);
923
Trond Myklebustdce81292011-07-13 15:59:19 -0400924 ret2 = nfs_do_write(data, call_ops, how);
Trond Myklebust275acaa2011-07-12 13:42:02 -0400925 if (ret == 0)
926 ret = ret2;
927 }
928 return ret;
929}
930
Fred6d884e82008-03-19 11:24:38 -0400931/* If a nfs_flush_* function fails, it should remove reqs from @head and
932 * call this on each, which will prepare them to be retried on next
933 * writeback using standard nfs.
934 */
935static void nfs_redirty_request(struct nfs_page *req)
936{
Trond Myklebusta6305dd2010-04-09 19:07:08 -0400937 struct page *page = req->wb_page;
938
Fred6d884e82008-03-19 11:24:38 -0400939 nfs_mark_request_dirty(req);
Fred Isaman9994b622012-03-08 17:29:34 -0500940 nfs_unlock_request(req);
Trond Myklebusta6305dd2010-04-09 19:07:08 -0400941 nfs_end_page_writeback(page);
Fred6d884e82008-03-19 11:24:38 -0400942}
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944/*
945 * Generate multiple small requests to write out a single
946 * contiguous dirty area on one page.
947 */
Trond Myklebust275acaa2011-07-12 13:42:02 -0400948static int nfs_flush_multi(struct nfs_pageio_descriptor *desc, struct list_head *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Fred Isamanc76069b2011-03-03 15:13:48 +0000950 struct nfs_page *req = nfs_list_entry(desc->pg_list.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 struct page *page = req->wb_page;
952 struct nfs_write_data *data;
Trond Myklebustd0979712011-07-12 13:42:02 -0400953 size_t wsize = desc->pg_bsize, nbytes;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700954 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 int requests = 0;
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400956 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 nfs_list_remove_request(req);
959
Trond Myklebustb31268a2011-03-21 17:02:00 -0400960 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
961 (desc->pg_moreio || NFS_I(desc->pg_inode)->ncommit ||
962 desc->pg_count > wsize))
963 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
964
965
Trond Myklebust275acaa2011-07-12 13:42:02 -0400966 offset = 0;
Fred Isamanc76069b2011-03-03 15:13:48 +0000967 nbytes = desc->pg_count;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700968 do {
969 size_t len = min(nbytes, wsize);
970
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400971 data = nfs_writedata_alloc(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (!data)
973 goto out_bad;
Trond Myklebust275acaa2011-07-12 13:42:02 -0400974 data->pagevec[0] = page;
Trond Myklebustf13c3622011-09-12 11:47:53 -0400975 nfs_write_rpcsetup(req, data, len, offset, desc->pg_ioflags);
Trond Myklebust275acaa2011-07-12 13:42:02 -0400976 list_add(&data->list, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700978 nbytes -= len;
Trond Myklebust275acaa2011-07-12 13:42:02 -0400979 offset += len;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700980 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 atomic_set(&req->wb_complete, requests);
Trond Myklebust50828d72011-07-12 13:42:02 -0400982 desc->pg_rpc_callops = &nfs_write_partial_ops;
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400983 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985out_bad:
Trond Myklebust275acaa2011-07-12 13:42:02 -0400986 while (!list_empty(res)) {
987 data = list_entry(res->next, struct nfs_write_data, list);
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400988 list_del(&data->list);
Fred Isaman0da2a4ac2011-01-19 14:18:50 -0500989 nfs_writedata_free(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
Trond Myklebust61822ab2006-12-05 00:35:42 -0500991 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return -ENOMEM;
993}
994
995/*
996 * Create an RPC task for the given write request and kick it.
997 * The page must have been locked by the caller.
998 *
999 * It may happen that the page we're passed is not marked dirty.
1000 * This is the case if nfs_updatepage detects a conflicting request
1001 * that has been written but not committed.
1002 */
Trond Myklebust275acaa2011-07-12 13:42:02 -04001003static int nfs_flush_one(struct nfs_pageio_descriptor *desc, struct list_head *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
1005 struct nfs_page *req;
1006 struct page **pages;
1007 struct nfs_write_data *data;
Fred Isamanc76069b2011-03-03 15:13:48 +00001008 struct list_head *head = &desc->pg_list;
Peng Tao3b609182011-07-15 03:33:42 -04001009 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Fred Isamanc76069b2011-03-03 15:13:48 +00001011 data = nfs_writedata_alloc(nfs_page_array_len(desc->pg_base,
1012 desc->pg_count));
Fred Isaman44b83792011-03-03 15:13:44 +00001013 if (!data) {
1014 while (!list_empty(head)) {
1015 req = nfs_list_entry(head->next);
1016 nfs_list_remove_request(req);
1017 nfs_redirty_request(req);
1018 }
1019 ret = -ENOMEM;
1020 goto out;
1021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 pages = data->pagevec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 while (!list_empty(head)) {
1024 req = nfs_list_entry(head->next);
1025 nfs_list_remove_request(req);
1026 nfs_list_add_request(req, &data->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 *pages++ = req->wb_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029 req = nfs_list_entry(data->pages.next);
1030
Trond Myklebustb31268a2011-03-21 17:02:00 -04001031 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1032 (desc->pg_moreio || NFS_I(desc->pg_inode)->ncommit))
1033 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 /* Set up the argument struct */
Trond Myklebust6e4efd52011-07-12 13:42:02 -04001036 nfs_write_rpcsetup(req, data, desc->pg_count, 0, desc->pg_ioflags);
Trond Myklebust275acaa2011-07-12 13:42:02 -04001037 list_add(&data->list, res);
Trond Myklebust50828d72011-07-12 13:42:02 -04001038 desc->pg_rpc_callops = &nfs_write_full_ops;
Fred Isaman44b83792011-03-03 15:13:44 +00001039out:
Fred Isaman44b83792011-03-03 15:13:44 +00001040 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
Trond Myklebustdce81292011-07-13 15:59:19 -04001043int nfs_generic_flush(struct nfs_pageio_descriptor *desc, struct list_head *head)
1044{
1045 if (desc->pg_bsize < PAGE_CACHE_SIZE)
1046 return nfs_flush_multi(desc, head);
1047 return nfs_flush_one(desc, head);
1048}
1049
1050static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
Trond Myklebust1751c362011-06-10 13:30:23 -04001051{
Trond Myklebust275acaa2011-07-12 13:42:02 -04001052 LIST_HEAD(head);
1053 int ret;
1054
Trond Myklebustdce81292011-07-13 15:59:19 -04001055 ret = nfs_generic_flush(desc, &head);
Trond Myklebust50828d72011-07-12 13:42:02 -04001056 if (ret == 0)
1057 ret = nfs_do_multiple_writes(&head, desc->pg_rpc_callops,
Trond Myklebustdce81292011-07-13 15:59:19 -04001058 desc->pg_ioflags);
Trond Myklebust275acaa2011-07-12 13:42:02 -04001059 return ret;
Trond Myklebust1751c362011-06-10 13:30:23 -04001060}
Trond Myklebust1751c362011-06-10 13:30:23 -04001061
1062static const struct nfs_pageio_ops nfs_pageio_write_ops = {
1063 .pg_test = nfs_generic_pg_test,
1064 .pg_doio = nfs_generic_pg_writepages,
1065};
1066
Trond Myklebuste2fecb22012-01-06 08:57:46 -05001067void nfs_pageio_init_write_mds(struct nfs_pageio_descriptor *pgio,
Trond Myklebust1751c362011-06-10 13:30:23 -04001068 struct inode *inode, int ioflags)
1069{
1070 nfs_pageio_init(pgio, inode, &nfs_pageio_write_ops,
1071 NFS_SERVER(inode)->wsize, ioflags);
1072}
1073
Trond Myklebustdce81292011-07-13 15:59:19 -04001074void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1075{
1076 pgio->pg_ops = &nfs_pageio_write_ops;
1077 pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1078}
Trond Myklebust1f945352011-07-13 15:59:57 -04001079EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
Trond Myklebustdce81292011-07-13 15:59:19 -04001080
Trond Myklebustc63c7b02007-04-02 19:29:52 -04001081static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1082 struct inode *inode, int ioflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Trond Myklebust1751c362011-06-10 13:30:23 -04001084 if (!pnfs_pageio_init_write(pgio, inode, ioflags))
1085 nfs_pageio_init_write_mds(pgio, inode, ioflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
1087
1088/*
1089 * Handle a write reply that flushed part of a page.
1090 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001091static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001093 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Chuck Lever48186c72008-06-11 17:56:05 -04001095 dprintk("NFS: %5u write(%s/%lld %d@%lld)",
1096 task->tk_pid,
Al Viro3d4ff432011-06-22 18:40:12 -04001097 data->req->wb_context->dentry->d_inode->i_sb->s_id,
Chuck Lever48186c72008-06-11 17:56:05 -04001098 (long long)
Al Viro3d4ff432011-06-22 18:40:12 -04001099 NFS_FILEID(data->req->wb_context->dentry->d_inode),
Chuck Lever48186c72008-06-11 17:56:05 -04001100 data->req->wb_bytes, (long long)req_offset(data->req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001102 nfs_writeback_done(task, data);
1103}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001104
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001105static void nfs_writeback_release_partial(void *calldata)
1106{
1107 struct nfs_write_data *data = calldata;
1108 struct nfs_page *req = data->req;
1109 struct page *page = req->wb_page;
1110 int status = data->task.tk_status;
1111
1112 if (status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -08001113 nfs_set_pageerror(page);
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001114 nfs_context_set_write_error(req->wb_context, status);
1115 dprintk(", error = %d\n", status);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001116 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001119 if (nfs_write_need_commit(data)) {
Trond Myklebust587142f2007-07-02 09:57:54 -04001120 struct inode *inode = page->mapping->host;
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001121
Trond Myklebust587142f2007-07-02 09:57:54 -04001122 spin_lock(&inode->i_lock);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001123 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
1124 /* Do nothing we need to resend the writes */
1125 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1126 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1127 dprintk(" defer commit\n");
1128 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1129 set_bit(PG_NEED_RESCHED, &req->wb_flags);
1130 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
1131 dprintk(" server reboot detected\n");
1132 }
Trond Myklebust587142f2007-07-02 09:57:54 -04001133 spin_unlock(&inode->i_lock);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001134 } else
1135 dprintk(" OK\n");
1136
1137out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (atomic_dec_and_test(&req->wb_complete))
Fred Isamana861a1e2011-03-23 13:27:51 +00001139 nfs_writepage_release(req, data);
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001140 nfs_writedata_release(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
Andy Adamsondef6ed72009-04-01 09:22:26 -04001143#if defined(CONFIG_NFS_V4_1)
1144void nfs_write_prepare(struct rpc_task *task, void *calldata)
1145{
1146 struct nfs_write_data *data = calldata;
Andy Adamsondef6ed72009-04-01 09:22:26 -04001147
Trond Myklebust035168a2010-06-16 09:52:26 -04001148 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
1149 &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001150 &data->res.seq_res, task))
Andy Adamsondef6ed72009-04-01 09:22:26 -04001151 return;
1152 rpc_call_start(task);
1153}
1154#endif /* CONFIG_NFS_V4_1 */
1155
Trond Myklebust788e7a82006-03-20 13:44:27 -05001156static const struct rpc_call_ops nfs_write_partial_ops = {
Andy Adamsondef6ed72009-04-01 09:22:26 -04001157#if defined(CONFIG_NFS_V4_1)
1158 .rpc_call_prepare = nfs_write_prepare,
1159#endif /* CONFIG_NFS_V4_1 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001160 .rpc_call_done = nfs_writeback_done_partial,
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001161 .rpc_release = nfs_writeback_release_partial,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001162};
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164/*
1165 * Handle a write reply that flushes a whole page.
1166 *
1167 * FIXME: There is an inherent race with invalidate_inode_pages and
1168 * writebacks since the page->count is kept > 1 for as long
1169 * as the page has a write request pending.
1170 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001171static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001173 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001175 nfs_writeback_done(task, data);
1176}
1177
1178static void nfs_writeback_release_full(void *calldata)
1179{
1180 struct nfs_write_data *data = calldata;
Trond Myklebuste2fecb22012-01-06 08:57:46 -05001181 int status = data->task.tk_status;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 /* Update attributes as result of writeback. */
1184 while (!list_empty(&data->pages)) {
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001185 struct nfs_page *req = nfs_list_entry(data->pages.next);
1186 struct page *page = req->wb_page;
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 nfs_list_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Chuck Lever48186c72008-06-11 17:56:05 -04001190 dprintk("NFS: %5u write (%s/%lld %d@%lld)",
1191 data->task.tk_pid,
Al Viro3d4ff432011-06-22 18:40:12 -04001192 req->wb_context->dentry->d_inode->i_sb->s_id,
1193 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 req->wb_bytes,
1195 (long long)req_offset(req));
1196
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001197 if (status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -08001198 nfs_set_pageerror(page);
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001199 nfs_context_set_write_error(req->wb_context, status);
1200 dprintk(", error = %d\n", status);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001201 goto remove_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001204 if (nfs_write_need_commit(data)) {
1205 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
Fred Isamana861a1e2011-03-23 13:27:51 +00001206 nfs_mark_request_commit(req, data->lseg);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001207 dprintk(" marked for commit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 goto next;
1209 }
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001210 dprintk(" OK\n");
1211remove_request:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 nfs_inode_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 next:
Fred Isaman9994b622012-03-08 17:29:34 -05001214 nfs_unlock_request(req);
Trond Myklebusta6305dd2010-04-09 19:07:08 -04001215 nfs_end_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001217 nfs_writedata_release(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
Trond Myklebust788e7a82006-03-20 13:44:27 -05001220static const struct rpc_call_ops nfs_write_full_ops = {
Andy Adamsondef6ed72009-04-01 09:22:26 -04001221#if defined(CONFIG_NFS_V4_1)
1222 .rpc_call_prepare = nfs_write_prepare,
1223#endif /* CONFIG_NFS_V4_1 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001224 .rpc_call_done = nfs_writeback_done_full,
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001225 .rpc_release = nfs_writeback_release_full,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001226};
1227
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229/*
1230 * This function is called when the WRITE call is complete.
1231 */
Fred Isaman13602892011-02-11 15:42:38 +00001232void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 struct nfs_writeargs *argp = &data->args;
1235 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001236 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Chuck Levera3f565b2007-01-31 12:14:01 -05001238 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 task->tk_pid, task->tk_status);
1240
Chuck Leverf551e442006-09-20 14:33:04 -04001241 /*
1242 * ->write_done will attempt to use post-op attributes to detect
1243 * conflicting writes by other clients. A strict interpretation
1244 * of close-to-open would allow us to continue caching even if
1245 * another writer had changed the file, but some applications
1246 * depend on tighter cache coherency when writing.
1247 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001248 status = NFS_PROTO(data->inode)->write_done(task, data);
1249 if (status != 0)
Fred Isaman13602892011-02-11 15:42:38 +00001250 return;
Chuck Lever91d5b472006-03-20 13:44:14 -05001251 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1254 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1255 /* We tried a write call, but the server did not
1256 * commit data to stable storage even though we
1257 * requested it.
1258 * Note: There is a known bug in Tru64 < 5.0 in which
1259 * the server reports NFS_DATA_SYNC, but performs
1260 * NFS_FILE_SYNC. We therefore implement this checking
1261 * as a dprintk() in order to avoid filling syslog.
1262 */
1263 static unsigned long complain;
1264
Fred Isamana69aef12011-03-03 15:13:47 +00001265 /* Note this will print the MDS for a DS write */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (time_before(complain, jiffies)) {
Chuck Lever48186c72008-06-11 17:56:05 -04001267 dprintk("NFS: faulty NFS server %s:"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 " (committed = %d) != (stable = %d)\n",
Rakib Mullick2b72c9c2011-11-01 12:23:42 +06001269 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 resp->verf->committed, argp->stable);
1271 complain = jiffies + 300 * HZ;
1272 }
1273 }
1274#endif
1275 /* Is this a short write? */
1276 if (task->tk_status >= 0 && resp->count < argp->count) {
1277 static unsigned long complain;
1278
Chuck Lever91d5b472006-03-20 13:44:14 -05001279 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 /* Has the server at least made some progress? */
1282 if (resp->count != 0) {
1283 /* Was this an NFSv2 write or an NFSv3 stable write? */
1284 if (resp->verf->committed != NFS_UNSTABLE) {
1285 /* Resend from where the server left off */
Fred Isamana69aef12011-03-03 15:13:47 +00001286 data->mds_offset += resp->count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 argp->offset += resp->count;
1288 argp->pgbase += resp->count;
1289 argp->count -= resp->count;
1290 } else {
1291 /* Resend as a stable write in order to avoid
1292 * headaches in the case of a server crash.
1293 */
1294 argp->stable = NFS_FILE_SYNC;
1295 }
Trond Myklebustd00c5d42011-10-19 12:17:29 -07001296 rpc_restart_call_prepare(task);
Fred Isaman13602892011-02-11 15:42:38 +00001297 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
1299 if (time_before(complain, jiffies)) {
1300 printk(KERN_WARNING
1301 "NFS: Server wrote zero bytes, expected %u.\n",
1302 argp->count);
1303 complain = jiffies + 300 * HZ;
1304 }
1305 /* Can't do anything about it except throw an error. */
1306 task->tk_status = -EIO;
1307 }
Fred Isaman13602892011-02-11 15:42:38 +00001308 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
1311
1312#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust71d0a612010-04-22 15:35:57 -04001313static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
1314{
Trond Myklebustb8413f92011-03-21 15:37:01 -04001315 int ret;
1316
Trond Myklebust71d0a612010-04-22 15:35:57 -04001317 if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
1318 return 1;
Trond Myklebustb8413f92011-03-21 15:37:01 -04001319 if (!may_wait)
1320 return 0;
1321 ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1322 NFS_INO_COMMIT,
1323 nfs_wait_bit_killable,
1324 TASK_KILLABLE);
1325 return (ret < 0) ? ret : 1;
Trond Myklebust71d0a612010-04-22 15:35:57 -04001326}
1327
Fred Isamane0c2b382011-03-23 13:27:53 +00001328void nfs_commit_clear_lock(struct nfs_inode *nfsi)
Trond Myklebust71d0a612010-04-22 15:35:57 -04001329{
1330 clear_bit(NFS_INO_COMMIT, &nfsi->flags);
1331 smp_mb__after_clear_bit();
1332 wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
1333}
Fred Isamane0c2b382011-03-23 13:27:53 +00001334EXPORT_SYMBOL_GPL(nfs_commit_clear_lock);
Trond Myklebust71d0a612010-04-22 15:35:57 -04001335
Fred Isamane0c2b382011-03-23 13:27:53 +00001336void nfs_commitdata_release(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Trond Myklebust383ba712008-02-19 20:04:20 -05001338 struct nfs_write_data *wdata = data;
1339
Fred Isaman988b6dc2011-03-23 13:27:52 +00001340 put_lseg(wdata->lseg);
Trond Myklebust383ba712008-02-19 20:04:20 -05001341 put_nfs_open_context(wdata->args.context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 nfs_commit_free(wdata);
1343}
Fred Isamane0c2b382011-03-23 13:27:53 +00001344EXPORT_SYMBOL_GPL(nfs_commitdata_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Fred Isamane0c2b382011-03-23 13:27:53 +00001346int nfs_initiate_commit(struct nfs_write_data *data, struct rpc_clnt *clnt,
Fred Isaman9ace33c2011-03-23 13:27:45 +00001347 const struct rpc_call_ops *call_ops,
1348 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Trond Myklebust07737692007-10-25 18:42:54 -04001350 struct rpc_task *task;
Fred Isaman9ace33c2011-03-23 13:27:45 +00001351 int priority = flush_task_priority(how);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001352 struct rpc_message msg = {
1353 .rpc_argp = &data->args,
1354 .rpc_resp = &data->res,
Fred Isaman9ace33c2011-03-23 13:27:45 +00001355 .rpc_cred = data->cred,
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001356 };
Trond Myklebust84115e12007-07-14 15:39:59 -04001357 struct rpc_task_setup task_setup_data = {
Trond Myklebust07737692007-10-25 18:42:54 -04001358 .task = &data->task,
Fred Isaman9ace33c2011-03-23 13:27:45 +00001359 .rpc_client = clnt,
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001360 .rpc_message = &msg,
Fred Isaman9ace33c2011-03-23 13:27:45 +00001361 .callback_ops = call_ops,
Trond Myklebust84115e12007-07-14 15:39:59 -04001362 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001363 .workqueue = nfsiod_workqueue,
Trond Myklebust2c61be02010-04-09 19:54:50 -04001364 .flags = RPC_TASK_ASYNC,
Trond Myklebust3ff75762007-07-14 15:40:00 -04001365 .priority = priority,
Trond Myklebust84115e12007-07-14 15:39:59 -04001366 };
Trond Myklebust788e7a82006-03-20 13:44:27 -05001367 /* Set up the initial task struct. */
Fred Isaman9ace33c2011-03-23 13:27:45 +00001368 NFS_PROTO(data->inode)->commit_setup(data, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Chuck Levera3f565b2007-01-31 12:14:01 -05001370 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001371
Trond Myklebust07737692007-10-25 18:42:54 -04001372 task = rpc_run_task(&task_setup_data);
Trond Myklebustdbae4c72008-04-14 14:54:53 -04001373 if (IS_ERR(task))
1374 return PTR_ERR(task);
Jeff Laytond2224e72011-03-06 17:14:13 +00001375 if (how & FLUSH_SYNC)
1376 rpc_wait_for_completion_task(task);
Trond Myklebustdbae4c72008-04-14 14:54:53 -04001377 rpc_put_task(task);
1378 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
Fred Isamane0c2b382011-03-23 13:27:53 +00001380EXPORT_SYMBOL_GPL(nfs_initiate_commit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382/*
Fred Isaman9ace33c2011-03-23 13:27:45 +00001383 * Set up the argument/result storage required for the RPC call.
1384 */
Fred Isamane0c2b382011-03-23 13:27:53 +00001385void nfs_init_commit(struct nfs_write_data *data,
Fred Isaman988b6dc2011-03-23 13:27:52 +00001386 struct list_head *head,
1387 struct pnfs_layout_segment *lseg)
Fred Isaman9ace33c2011-03-23 13:27:45 +00001388{
1389 struct nfs_page *first = nfs_list_entry(head->next);
Al Viro3d4ff432011-06-22 18:40:12 -04001390 struct inode *inode = first->wb_context->dentry->d_inode;
Fred Isaman9ace33c2011-03-23 13:27:45 +00001391
1392 /* Set up the RPC argument and reply structs
1393 * NB: take care not to mess about with data->commit et al. */
1394
1395 list_splice_init(head, &data->pages);
1396
1397 data->inode = inode;
1398 data->cred = first->wb_context->cred;
Fred Isaman988b6dc2011-03-23 13:27:52 +00001399 data->lseg = lseg; /* reference transferred */
Fred Isaman9ace33c2011-03-23 13:27:45 +00001400 data->mds_ops = &nfs_commit_ops;
1401
1402 data->args.fh = NFS_FH(data->inode);
1403 /* Note: we always request a commit of the entire inode */
1404 data->args.offset = 0;
1405 data->args.count = 0;
1406 data->args.context = get_nfs_open_context(first->wb_context);
1407 data->res.count = 0;
1408 data->res.fattr = &data->fattr;
1409 data->res.verf = &data->verf;
1410 nfs_fattr_init(&data->fattr);
1411}
Fred Isamane0c2b382011-03-23 13:27:53 +00001412EXPORT_SYMBOL_GPL(nfs_init_commit);
Fred Isaman9ace33c2011-03-23 13:27:45 +00001413
Fred Isamane0c2b382011-03-23 13:27:53 +00001414void nfs_retry_commit(struct list_head *page_list,
Fred Isamana861a1e2011-03-23 13:27:51 +00001415 struct pnfs_layout_segment *lseg)
Fred Isaman64bfeb42011-03-23 13:27:47 +00001416{
1417 struct nfs_page *req;
1418
1419 while (!list_empty(page_list)) {
1420 req = nfs_list_entry(page_list->next);
1421 nfs_list_remove_request(req);
Fred Isamana861a1e2011-03-23 13:27:51 +00001422 nfs_mark_request_commit(req, lseg);
Fred Isaman64bfeb42011-03-23 13:27:47 +00001423 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1424 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1425 BDI_RECLAIMABLE);
Fred Isaman9994b622012-03-08 17:29:34 -05001426 nfs_unlock_request(req);
Fred Isaman64bfeb42011-03-23 13:27:47 +00001427 }
1428}
Fred Isamane0c2b382011-03-23 13:27:53 +00001429EXPORT_SYMBOL_GPL(nfs_retry_commit);
Fred Isaman64bfeb42011-03-23 13:27:47 +00001430
Fred Isaman9ace33c2011-03-23 13:27:45 +00001431/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 * Commit dirty pages
1433 */
1434static int
Chuck Lever40859d72005-11-30 18:09:02 -05001435nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
1437 struct nfs_write_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001439 data = nfs_commitdata_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 if (!data)
1442 goto out_bad;
1443
1444 /* Set up the argument struct */
Fred Isaman988b6dc2011-03-23 13:27:52 +00001445 nfs_init_commit(data, head, NULL);
Fred Isaman9ace33c2011-03-23 13:27:45 +00001446 return nfs_initiate_commit(data, NFS_CLIENT(inode), data->mds_ops, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 out_bad:
Fred Isamana861a1e2011-03-23 13:27:51 +00001448 nfs_retry_commit(head, NULL);
Trond Myklebust71d0a612010-04-22 15:35:57 -04001449 nfs_commit_clear_lock(NFS_I(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return -ENOMEM;
1451}
1452
1453/*
1454 * COMMIT call returned
1455 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001456static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001458 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Chuck Levera3f565b2007-01-31 12:14:01 -05001460 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 task->tk_pid, task->tk_status);
1462
Trond Myklebust788e7a82006-03-20 13:44:27 -05001463 /* Call the NFS version-specific code */
Trond Myklebustc0d0e962011-04-12 12:29:15 -04001464 NFS_PROTO(data->inode)->commit_done(task, data);
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001465}
1466
Fred Isamane0c2b382011-03-23 13:27:53 +00001467void nfs_commit_release_pages(struct nfs_write_data *data)
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001468{
Fred Isaman5917ce82011-03-23 13:27:48 +00001469 struct nfs_page *req;
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001470 int status = data->task.tk_status;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 while (!list_empty(&data->pages)) {
1473 req = nfs_list_entry(data->pages.next);
1474 nfs_list_remove_request(req);
Fred Isamand6d6dc72012-03-08 17:29:35 -05001475 nfs_clear_page_commit(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Chuck Lever48186c72008-06-11 17:56:05 -04001477 dprintk("NFS: commit (%s/%lld %d@%lld)",
Al Viro3d4ff432011-06-22 18:40:12 -04001478 req->wb_context->dentry->d_sb->s_id,
1479 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 req->wb_bytes,
1481 (long long)req_offset(req));
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001482 if (status < 0) {
1483 nfs_context_set_write_error(req->wb_context, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 nfs_inode_remove_request(req);
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001485 dprintk(", error = %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 goto next;
1487 }
1488
1489 /* Okay, COMMIT succeeded, apparently. Check the verifier
1490 * returned by the server against all stored verfs. */
1491 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1492 /* We have a match */
1493 nfs_inode_remove_request(req);
1494 dprintk(" OK\n");
1495 goto next;
1496 }
1497 /* We have a mismatch. Write the page again */
1498 dprintk(" mismatch\n");
Fred6d884e82008-03-19 11:24:38 -04001499 nfs_mark_request_dirty(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 next:
Fred Isaman9994b622012-03-08 17:29:34 -05001501 nfs_unlock_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
Fred Isaman5917ce82011-03-23 13:27:48 +00001503}
Fred Isamane0c2b382011-03-23 13:27:53 +00001504EXPORT_SYMBOL_GPL(nfs_commit_release_pages);
Fred Isaman5917ce82011-03-23 13:27:48 +00001505
1506static void nfs_commit_release(void *calldata)
1507{
1508 struct nfs_write_data *data = calldata;
1509
1510 nfs_commit_release_pages(data);
Trond Myklebust71d0a612010-04-22 15:35:57 -04001511 nfs_commit_clear_lock(NFS_I(data->inode));
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001512 nfs_commitdata_release(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001514
1515static const struct rpc_call_ops nfs_commit_ops = {
Andy Adamson21d9a852009-04-01 09:22:27 -04001516#if defined(CONFIG_NFS_V4_1)
1517 .rpc_call_prepare = nfs_write_prepare,
1518#endif /* CONFIG_NFS_V4_1 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001519 .rpc_call_done = nfs_commit_done,
1520 .rpc_release = nfs_commit_release,
1521};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Trond Myklebustb608b282010-07-30 15:31:54 -04001523int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 LIST_HEAD(head);
Trond Myklebust71d0a612010-04-22 15:35:57 -04001526 int may_wait = how & FLUSH_SYNC;
Trond Myklebustb8413f92011-03-21 15:37:01 -04001527 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Trond Myklebustb8413f92011-03-21 15:37:01 -04001529 res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1530 if (res <= 0)
Trond Myklebustc5efa5f2010-05-26 08:42:11 -04001531 goto out_mark_dirty;
Fred Isamand6d6dc72012-03-08 17:29:35 -05001532 res = nfs_scan_commit(inode, &head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 if (res) {
Fred Isamana861a1e2011-03-23 13:27:51 +00001534 int error;
1535
1536 error = pnfs_commit_list(inode, &head, how);
1537 if (error == PNFS_NOT_ATTEMPTED)
1538 error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001539 if (error < 0)
1540 return error;
Trond Myklebustb8413f92011-03-21 15:37:01 -04001541 if (!may_wait)
Trond Myklebustc5efa5f2010-05-26 08:42:11 -04001542 goto out_mark_dirty;
Trond Myklebustb8413f92011-03-21 15:37:01 -04001543 error = wait_on_bit(&NFS_I(inode)->flags,
1544 NFS_INO_COMMIT,
1545 nfs_wait_bit_killable,
1546 TASK_KILLABLE);
1547 if (error < 0)
1548 return error;
Trond Myklebust71d0a612010-04-22 15:35:57 -04001549 } else
1550 nfs_commit_clear_lock(NFS_I(inode));
Trond Myklebustc5efa5f2010-05-26 08:42:11 -04001551 return res;
1552 /* Note: If we exit without ensuring that the commit is complete,
1553 * we must mark the inode as dirty. Otherwise, future calls to
1554 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1555 * that the data is on the disk.
1556 */
1557out_mark_dirty:
1558 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return res;
1560}
Trond Myklebust8fc795f2010-02-19 16:46:56 -08001561
1562static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
1563{
Trond Myklebust420e3642010-02-19 17:00:02 -08001564 struct nfs_inode *nfsi = NFS_I(inode);
1565 int flags = FLUSH_SYNC;
1566 int ret = 0;
Trond Myklebust8fc795f2010-02-19 16:46:56 -08001567
Jeff Layton3236c3e2011-10-11 09:49:21 -04001568 /* no commits means nothing needs to be done */
1569 if (!nfsi->ncommit)
1570 return ret;
1571
Jeff Laytona00dd6c2010-09-28 09:14:01 -04001572 if (wbc->sync_mode == WB_SYNC_NONE) {
1573 /* Don't commit yet if this is a non-blocking flush and there
1574 * are a lot of outstanding writes for this mapping.
1575 */
1576 if (nfsi->ncommit <= (nfsi->npages >> 1))
1577 goto out_mark_dirty;
Trond Myklebust420e3642010-02-19 17:00:02 -08001578
Jeff Laytona00dd6c2010-09-28 09:14:01 -04001579 /* don't wait for the COMMIT response */
Trond Myklebust420e3642010-02-19 17:00:02 -08001580 flags = 0;
Jeff Laytona00dd6c2010-09-28 09:14:01 -04001581 }
1582
Trond Myklebust420e3642010-02-19 17:00:02 -08001583 ret = nfs_commit_inode(inode, flags);
1584 if (ret >= 0) {
1585 if (wbc->sync_mode == WB_SYNC_NONE) {
1586 if (ret < wbc->nr_to_write)
1587 wbc->nr_to_write -= ret;
1588 else
1589 wbc->nr_to_write = 0;
1590 }
Trond Myklebust8fc795f2010-02-19 16:46:56 -08001591 return 0;
Trond Myklebust420e3642010-02-19 17:00:02 -08001592 }
1593out_mark_dirty:
Trond Myklebust8fc795f2010-02-19 16:46:56 -08001594 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
1595 return ret;
1596}
Trond Myklebustc63c7b02007-04-02 19:29:52 -04001597#else
Trond Myklebust8fc795f2010-02-19 16:46:56 -08001598static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
1599{
1600 return 0;
1601}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602#endif
1603
Trond Myklebust8fc795f2010-02-19 16:46:56 -08001604int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
1605{
Andy Adamson863a3c62011-03-23 13:27:54 +00001606 int ret;
1607
1608 ret = nfs_commit_unstable_pages(inode, wbc);
1609 if (ret >= 0 && test_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(inode)->flags)) {
Andy Adamsonef311532011-03-12 02:58:10 -05001610 int status;
1611 bool sync = true;
Andy Adamson863a3c62011-03-23 13:27:54 +00001612
Wu Fengguang846d5a02011-05-05 21:10:38 -06001613 if (wbc->sync_mode == WB_SYNC_NONE)
Andy Adamsonef311532011-03-12 02:58:10 -05001614 sync = false;
Andy Adamson863a3c62011-03-23 13:27:54 +00001615
1616 status = pnfs_layoutcommit_inode(inode, sync);
1617 if (status < 0)
1618 return status;
1619 }
1620 return ret;
Trond Myklebust8fc795f2010-02-19 16:46:56 -08001621}
1622
Trond Myklebustacdc53b2010-02-19 17:03:26 -08001623/*
1624 * flush the inode to disk.
1625 */
1626int nfs_wb_all(struct inode *inode)
Trond Myklebust34901f72007-07-25 14:09:54 -04001627{
1628 struct writeback_control wbc = {
Trond Myklebust72cb77f2009-03-11 14:10:30 -04001629 .sync_mode = WB_SYNC_ALL,
Trond Myklebust34901f72007-07-25 14:09:54 -04001630 .nr_to_write = LONG_MAX,
Trond Myklebustd7fb1202008-10-06 20:08:56 -04001631 .range_start = 0,
1632 .range_end = LLONG_MAX,
Trond Myklebust34901f72007-07-25 14:09:54 -04001633 };
Trond Myklebust34901f72007-07-25 14:09:54 -04001634
Trond Myklebustacdc53b2010-02-19 17:03:26 -08001635 return sync_inode(inode, &wbc);
Trond Myklebust1c759502006-10-09 16:18:38 -04001636}
1637
Trond Myklebust1b3b4a12007-08-28 10:29:36 -04001638int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1639{
1640 struct nfs_page *req;
Trond Myklebust1b3b4a12007-08-28 10:29:36 -04001641 int ret = 0;
1642
1643 BUG_ON(!PageLocked(page));
1644 for (;;) {
Trond Myklebustba8b06e2010-04-27 18:33:54 -04001645 wait_on_page_writeback(page);
Trond Myklebust1b3b4a12007-08-28 10:29:36 -04001646 req = nfs_page_find_request(page);
1647 if (req == NULL)
Trond Myklebust1b3b4a12007-08-28 10:29:36 -04001648 break;
Trond Myklebust1b3b4a12007-08-28 10:29:36 -04001649 if (nfs_lock_request_dontget(req)) {
1650 nfs_inode_remove_request(req);
1651 /*
1652 * In case nfs_inode_remove_request has marked the
1653 * page as being dirty
1654 */
1655 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1656 nfs_unlock_request(req);
1657 break;
1658 }
1659 ret = nfs_wait_on_request(req);
Trond Myklebustc9edda72010-01-26 15:41:34 -05001660 nfs_release_request(req);
Trond Myklebust1b3b4a12007-08-28 10:29:36 -04001661 if (ret < 0)
Trond Myklebustc9889502010-02-19 17:03:21 -08001662 break;
Trond Myklebust1b3b4a12007-08-28 10:29:36 -04001663 }
Trond Myklebust1b3b4a12007-08-28 10:29:36 -04001664 return ret;
1665}
1666
Trond Myklebust7f2f12d2010-02-19 17:03:28 -08001667/*
1668 * Write back all requests on one page - we do this before reading it.
1669 */
1670int nfs_wb_page(struct inode *inode, struct page *page)
Trond Myklebust1c759502006-10-09 16:18:38 -04001671{
1672 loff_t range_start = page_offset(page);
1673 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001674 struct writeback_control wbc = {
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001675 .sync_mode = WB_SYNC_ALL,
Trond Myklebust7f2f12d2010-02-19 17:03:28 -08001676 .nr_to_write = 0,
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001677 .range_start = range_start,
1678 .range_end = range_end,
1679 };
1680 int ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001681
Trond Myklebust0522f6a2010-05-26 08:42:24 -04001682 for (;;) {
Trond Myklebustba8b06e2010-04-27 18:33:54 -04001683 wait_on_page_writeback(page);
Trond Myklebust73e33022008-04-11 16:03:54 -04001684 if (clear_page_dirty_for_io(page)) {
1685 ret = nfs_writepage_locked(page, &wbc);
1686 if (ret < 0)
1687 goto out_error;
Trond Myklebust0522f6a2010-05-26 08:42:24 -04001688 continue;
Trond Myklebust7f2f12d2010-02-19 17:03:28 -08001689 }
Trond Myklebust0522f6a2010-05-26 08:42:24 -04001690 if (!PagePrivate(page))
1691 break;
1692 ret = nfs_commit_inode(inode, FLUSH_SYNC);
Trond Myklebustba8b06e2010-04-27 18:33:54 -04001693 if (ret < 0)
Trond Myklebust73e33022008-04-11 16:03:54 -04001694 goto out_error;
Trond Myklebust7f2f12d2010-02-19 17:03:28 -08001695 }
Trond Myklebust73e33022008-04-11 16:03:54 -04001696 return 0;
1697out_error:
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001698 return ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001699}
1700
Trond Myklebust074cc1d2009-08-10 08:54:13 -04001701#ifdef CONFIG_MIGRATION
1702int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001703 struct page *page, enum migrate_mode mode)
Trond Myklebust074cc1d2009-08-10 08:54:13 -04001704{
Jeff Layton2da95652011-10-12 10:57:42 -04001705 /*
1706 * If PagePrivate is set, then the page is currently associated with
1707 * an in-progress read or write request. Don't try to migrate it.
1708 *
1709 * FIXME: we could do this in principle, but we'll need a way to ensure
1710 * that we can safely release the inode reference while holding
1711 * the page lock.
1712 */
1713 if (PagePrivate(page))
1714 return -EBUSY;
Trond Myklebust074cc1d2009-08-10 08:54:13 -04001715
Trond Myklebust7549ad52010-02-08 09:32:34 -05001716 nfs_fscache_release_page(page, GFP_KERNEL);
Trond Myklebust074cc1d2009-08-10 08:54:13 -04001717
Mel Gormana6bc32b2012-01-12 17:19:43 -08001718 return migrate_page(mapping, newpage, page, mode);
Trond Myklebust074cc1d2009-08-10 08:54:13 -04001719}
1720#endif
1721
David Howellsf7b422b2006-06-09 09:34:33 -04001722int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
1724 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1725 sizeof(struct nfs_write_data),
1726 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001727 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 if (nfs_wdata_cachep == NULL)
1729 return -ENOMEM;
1730
Matthew Dobson93d23412006-03-26 01:37:50 -08001731 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1732 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 if (nfs_wdata_mempool == NULL)
1734 return -ENOMEM;
1735
Matthew Dobson93d23412006-03-26 01:37:50 -08001736 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1737 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 if (nfs_commit_mempool == NULL)
1739 return -ENOMEM;
1740
Peter Zijlstra89a09142007-03-16 13:38:26 -08001741 /*
1742 * NFS congestion size, scale with available memory.
1743 *
1744 * 64MB: 8192k
1745 * 128MB: 11585k
1746 * 256MB: 16384k
1747 * 512MB: 23170k
1748 * 1GB: 32768k
1749 * 2GB: 46340k
1750 * 4GB: 65536k
1751 * 8GB: 92681k
1752 * 16GB: 131072k
1753 *
1754 * This allows larger machines to have larger/more transfers.
1755 * Limit the default to 256M
1756 */
1757 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1758 if (nfs_congestion_kb > 256*1024)
1759 nfs_congestion_kb = 256*1024;
1760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 return 0;
1762}
1763
David Brownell266bee82006-06-27 12:59:15 -07001764void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
1766 mempool_destroy(nfs_commit_mempool);
1767 mempool_destroy(nfs_wdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001768 kmem_cache_destroy(nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769}
1770