blob: feca8c648766ba986999f14be8f9f839328f6884 [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;
136 loff_t end, i_size = i_size_read(inode);
Trond Myklebustca52fec2007-04-17 17:22:13 -0400137 pgoff_t end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 if (i_size > 0 && page->index < end_index)
140 return;
141 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
142 if (i_size >= end)
143 return;
Chuck Lever91d5b472006-03-20 13:44:14 -0500144 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 i_size_write(inode, end);
146}
147
Trond Myklebusta301b772007-02-06 11:07:15 -0800148/* A writeback failed: mark the page as bad, and invalidate the page cache */
149static void nfs_set_pageerror(struct page *page)
150{
151 SetPageError(page);
152 nfs_zap_mapping(page->mapping->host, page->mapping);
153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/* We can set the PG_uptodate flag if we see that a write request
156 * covers the full page.
157 */
158static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
159{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (PageUptodate(page))
161 return;
162 if (base != 0)
163 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500164 if (count != nfs_page_length(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500166 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169static int wb_priority(struct writeback_control *wbc)
170{
171 if (wbc->for_reclaim)
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400172 return FLUSH_HIGHPRI | FLUSH_STABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (wbc->for_kupdate)
174 return FLUSH_LOWPRI;
175 return 0;
176}
177
178/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800179 * NFS congestion control
180 */
181
182int nfs_congestion_kb;
183
184#define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
185#define NFS_CONGESTION_OFF_THRESH \
186 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
187
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400188static int nfs_set_page_writeback(struct page *page)
Peter Zijlstra89a09142007-03-16 13:38:26 -0800189{
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400190 int ret = test_set_page_writeback(page);
191
192 if (!ret) {
Peter Zijlstra89a09142007-03-16 13:38:26 -0800193 struct inode *inode = page->mapping->host;
194 struct nfs_server *nfss = NFS_SERVER(inode);
195
Peter Zijlstra277866a2007-05-08 00:35:12 -0700196 if (atomic_long_inc_return(&nfss->writeback) >
Peter Zijlstra89a09142007-03-16 13:38:26 -0800197 NFS_CONGESTION_ON_THRESH)
198 set_bdi_congested(&nfss->backing_dev_info, WRITE);
199 }
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400200 return ret;
Peter Zijlstra89a09142007-03-16 13:38:26 -0800201}
202
203static void nfs_end_page_writeback(struct page *page)
204{
205 struct inode *inode = page->mapping->host;
206 struct nfs_server *nfss = NFS_SERVER(inode);
207
208 end_page_writeback(page);
Peter Zijlstrac4dc4be2007-10-16 23:25:41 -0700209 if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
Peter Zijlstra89a09142007-03-16 13:38:26 -0800210 clear_bdi_congested(&nfss->backing_dev_info, WRITE);
Peter Zijlstra89a09142007-03-16 13:38:26 -0800211}
212
213/*
Trond Myklebuste261f512006-12-05 00:35:41 -0500214 * Find an associated nfs write request, and prepare to flush it out
Trond Myklebust9cccef92007-07-22 17:09:05 -0400215 * May return an error if the user signalled nfs_wait_on_request().
Trond Myklebuste261f512006-12-05 00:35:41 -0500216 */
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400217static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
218 struct page *page)
Trond Myklebuste261f512006-12-05 00:35:41 -0500219{
Trond Myklebust587142f2007-07-02 09:57:54 -0400220 struct inode *inode = page->mapping->host;
Trond Myklebuste261f512006-12-05 00:35:41 -0500221 struct nfs_page *req;
Trond Myklebuste261f512006-12-05 00:35:41 -0500222 int ret;
223
Trond Myklebust587142f2007-07-02 09:57:54 -0400224 spin_lock(&inode->i_lock);
Trond Myklebuste261f512006-12-05 00:35:41 -0500225 for(;;) {
226 req = nfs_page_find_request_locked(page);
227 if (req == NULL) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400228 spin_unlock(&inode->i_lock);
Trond Myklebust9cccef92007-07-22 17:09:05 -0400229 return 0;
Trond Myklebuste261f512006-12-05 00:35:41 -0500230 }
Trond Myklebustacee4782008-01-22 17:13:07 -0500231 if (nfs_set_page_tag_locked(req))
Trond Myklebuste261f512006-12-05 00:35:41 -0500232 break;
233 /* Note: If we hold the page lock, as is the case in nfs_writepage,
Trond Myklebustacee4782008-01-22 17:13:07 -0500234 * then the call to nfs_set_page_tag_locked() will always
Trond Myklebuste261f512006-12-05 00:35:41 -0500235 * succeed provided that someone hasn't already marked the
236 * request as dirty (in which case we don't care).
237 */
Trond Myklebust587142f2007-07-02 09:57:54 -0400238 spin_unlock(&inode->i_lock);
Trond Myklebuste261f512006-12-05 00:35:41 -0500239 ret = nfs_wait_on_request(req);
240 nfs_release_request(req);
241 if (ret != 0)
242 return ret;
Trond Myklebust587142f2007-07-02 09:57:54 -0400243 spin_lock(&inode->i_lock);
Trond Myklebuste261f512006-12-05 00:35:41 -0500244 }
Trond Myklebuste468bae2008-06-13 13:25:22 -0400245 if (test_bit(PG_CLEAN, &req->wb_flags)) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400246 spin_unlock(&inode->i_lock);
Trond Myklebuste468bae2008-06-13 13:25:22 -0400247 BUG();
Trond Myklebust612c9382007-04-20 16:12:45 -0400248 }
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400249 if (nfs_set_page_writeback(page) != 0) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400250 spin_unlock(&inode->i_lock);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400251 BUG();
252 }
Trond Myklebust587142f2007-07-02 09:57:54 -0400253 spin_unlock(&inode->i_lock);
Fred Isamanf8512ad2008-03-19 11:24:39 -0400254 if (!nfs_pageio_add_request(pgio, req)) {
255 nfs_redirty_request(req);
Fred Isamanf8512ad2008-03-19 11:24:39 -0400256 return pgio->pg_error;
257 }
Trond Myklebust9cccef92007-07-22 17:09:05 -0400258 return 0;
Trond Myklebuste261f512006-12-05 00:35:41 -0500259}
260
Trond Myklebustf758c8852007-07-22 19:27:32 -0400261static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
262{
263 struct inode *inode = page->mapping->host;
264
265 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
266 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
267
268 nfs_pageio_cond_complete(pgio, page->index);
269 return nfs_page_async_flush(pgio, page);
270}
271
Trond Myklebuste261f512006-12-05 00:35:41 -0500272/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * Write an mmapped page to the server.
274 */
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500275static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Trond Myklebustf758c8852007-07-22 19:27:32 -0400277 struct nfs_pageio_descriptor pgio;
Trond Myklebuste261f512006-12-05 00:35:41 -0500278 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Trond Myklebustf758c8852007-07-22 19:27:32 -0400280 nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
281 err = nfs_do_writepage(page, wbc, &pgio);
282 nfs_pageio_complete(&pgio);
283 if (err < 0)
284 return err;
285 if (pgio.pg_error < 0)
286 return pgio.pg_error;
287 return 0;
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500288}
289
290int nfs_writepage(struct page *page, struct writeback_control *wbc)
291{
Trond Myklebustf758c8852007-07-22 19:27:32 -0400292 int ret;
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500293
Trond Myklebustf758c8852007-07-22 19:27:32 -0400294 ret = nfs_writepage_locked(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 unlock_page(page);
Trond Myklebustf758c8852007-07-22 19:27:32 -0400296 return ret;
297}
298
299static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
300{
301 int ret;
302
303 ret = nfs_do_writepage(page, wbc, data);
304 unlock_page(page);
305 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
309{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 struct inode *inode = mapping->host;
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400311 struct nfs_pageio_descriptor pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 int err;
313
Chuck Lever91d5b472006-03-20 13:44:14 -0500314 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
315
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400316 nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
Trond Myklebustf758c8852007-07-22 19:27:32 -0400317 err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400318 nfs_pageio_complete(&pgio);
Trond Myklebustf758c8852007-07-22 19:27:32 -0400319 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return err;
Trond Myklebustf758c8852007-07-22 19:27:32 -0400321 if (pgio.pg_error < 0)
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400322 return pgio.pg_error;
323 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
326/*
327 * Insert a write request into an inode
328 */
Trond Myklebuste7d39062008-06-13 12:12:32 -0400329static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 struct nfs_inode *nfsi = NFS_I(inode);
332 int error;
333
Trond Myklebuste7d39062008-06-13 12:12:32 -0400334 error = radix_tree_preload(GFP_NOFS);
335 if (error != 0)
336 goto out;
337
338 /* Lock the request! */
339 nfs_lock_request_dontget(req);
340
341 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
Nick Piggin27852592008-02-04 23:48:37 -0800343 BUG_ON(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (!nfsi->npages) {
345 igrab(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (nfs_have_delegation(inode, FMODE_WRITE))
347 nfsi->change_attr++;
348 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500349 SetPagePrivate(req->wb_page);
Trond Myklebust277459d2006-12-05 00:35:35 -0500350 set_page_private(req->wb_page, (unsigned long)req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 nfsi->npages++;
Trond Myklebustc03b4022007-06-17 13:26:38 -0400352 kref_get(&req->wb_kref);
Nick Piggin27852592008-02-04 23:48:37 -0800353 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
354 NFS_PAGE_TAG_LOCKED);
Trond Myklebuste7d39062008-06-13 12:12:32 -0400355 spin_unlock(&inode->i_lock);
356 radix_tree_preload_end();
357out:
358 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
361/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800362 * Remove a write request from an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 */
364static void nfs_inode_remove_request(struct nfs_page *req)
365{
Trond Myklebust88be9f92007-06-05 10:42:27 -0400366 struct inode *inode = req->wb_context->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 struct nfs_inode *nfsi = NFS_I(inode);
368
369 BUG_ON (!NFS_WBACK_BUSY(req));
370
Trond Myklebust587142f2007-07-02 09:57:54 -0400371 spin_lock(&inode->i_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500372 set_page_private(req->wb_page, 0);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500373 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
375 nfsi->npages--;
376 if (!nfsi->npages) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400377 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 iput(inode);
379 } else
Trond Myklebust587142f2007-07-02 09:57:54 -0400380 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 nfs_clear_request(req);
382 nfs_release_request(req);
383}
384
Trond Myklebust61822ab2006-12-05 00:35:42 -0500385static void
Fred6d884e82008-03-19 11:24:38 -0400386nfs_mark_request_dirty(struct nfs_page *req)
Trond Myklebust61822ab2006-12-05 00:35:42 -0500387{
Trond Myklebust61822ab2006-12-05 00:35:42 -0500388 __set_page_dirty_nobuffers(req->wb_page);
389}
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
392/*
393 * Add a request to the inode's commit list.
394 */
395static void
396nfs_mark_request_commit(struct nfs_page *req)
397{
Trond Myklebust88be9f92007-06-05 10:42:27 -0400398 struct inode *inode = req->wb_context->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 struct nfs_inode *nfsi = NFS_I(inode);
400
Trond Myklebust587142f2007-07-02 09:57:54 -0400401 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 nfsi->ncommit++;
Trond Myklebuste468bae2008-06-13 13:25:22 -0400403 set_bit(PG_CLEAN, &(req)->wb_flags);
Trond Myklebust5c369682007-06-17 15:27:42 -0400404 radix_tree_tag_set(&nfsi->nfs_page_tree,
405 req->wb_index,
406 NFS_PAGE_TAG_COMMIT);
Trond Myklebust587142f2007-07-02 09:57:54 -0400407 spin_unlock(&inode->i_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700408 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -0700409 inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
Trond Myklebusta1803042006-12-05 00:45:19 -0500410 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400412
Trond Myklebuste468bae2008-06-13 13:25:22 -0400413static int
414nfs_clear_request_commit(struct nfs_page *req)
415{
416 struct page *page = req->wb_page;
417
418 if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) {
419 dec_zone_page_state(page, NR_UNSTABLE_NFS);
420 dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE);
421 return 1;
422 }
423 return 0;
424}
425
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400426static inline
427int nfs_write_need_commit(struct nfs_write_data *data)
428{
429 return data->verf.committed != NFS_FILE_SYNC;
430}
431
432static inline
433int nfs_reschedule_unstable_write(struct nfs_page *req)
434{
Trond Myklebuste468bae2008-06-13 13:25:22 -0400435 if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) {
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400436 nfs_mark_request_commit(req);
437 return 1;
438 }
439 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
Fred6d884e82008-03-19 11:24:38 -0400440 nfs_mark_request_dirty(req);
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400441 return 1;
442 }
443 return 0;
444}
445#else
446static inline void
447nfs_mark_request_commit(struct nfs_page *req)
448{
449}
450
Trond Myklebuste468bae2008-06-13 13:25:22 -0400451static inline int
452nfs_clear_request_commit(struct nfs_page *req)
453{
454 return 0;
455}
456
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400457static inline
458int nfs_write_need_commit(struct nfs_write_data *data)
459{
460 return 0;
461}
462
463static inline
464int nfs_reschedule_unstable_write(struct nfs_page *req)
465{
466 return 0;
467}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468#endif
469
470/*
471 * Wait for a request to complete.
472 *
Matthew Wilcox150030b2007-12-06 16:24:39 -0500473 * Interruptible by fatal signals only.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 */
Trond Myklebustca52fec2007-04-17 17:22:13 -0400475static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 struct nfs_inode *nfsi = NFS_I(inode);
478 struct nfs_page *req;
Trond Myklebustca52fec2007-04-17 17:22:13 -0400479 pgoff_t idx_end, next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 unsigned int res = 0;
481 int error;
482
483 if (npages == 0)
484 idx_end = ~0;
485 else
486 idx_end = idx_start + npages - 1;
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 next = idx_start;
Trond Myklebust9fd367f2007-06-17 15:10:24 -0400489 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 -0700490 if (req->wb_index > idx_end)
491 break;
492
493 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000494 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Trond Myklebustc03b4022007-06-17 13:26:38 -0400496 kref_get(&req->wb_kref);
Trond Myklebust587142f2007-07-02 09:57:54 -0400497 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 error = nfs_wait_on_request(req);
499 nfs_release_request(req);
Trond Myklebust587142f2007-07-02 09:57:54 -0400500 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (error < 0)
502 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 res++;
504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return res;
506}
507
Trond Myklebust83715ad2006-07-05 13:17:12 -0400508static void nfs_cancel_commit_list(struct list_head *head)
509{
510 struct nfs_page *req;
511
512 while(!list_empty(head)) {
513 req = nfs_list_entry(head->next);
514 nfs_list_remove_request(req);
Trond Myklebuste468bae2008-06-13 13:25:22 -0400515 nfs_clear_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400516 nfs_inode_remove_request(req);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700517 nfs_unlock_request(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400518 }
519}
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
522/*
523 * nfs_scan_commit - Scan an inode for commit requests
524 * @inode: NFS inode to scan
525 * @dst: destination list
526 * @idx_start: lower bound of page->index to scan.
527 * @npages: idx_start + npages sets the upper bound to scan.
528 *
529 * Moves requests from the inode's 'commit' request list.
530 * The requests are *not* checked to ensure that they form a contiguous set.
531 */
532static int
Trond Myklebustca52fec2007-04-17 17:22:13 -0400533nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000536 int res = 0;
537
538 if (nfsi->ncommit != 0) {
Trond Myklebust5c369682007-06-17 15:27:42 -0400539 res = nfs_scan_list(nfsi, dst, idx_start, npages,
540 NFS_PAGE_TAG_COMMIT);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000541 nfsi->ncommit -= res;
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return res;
544}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500545#else
Trond Myklebustca52fec2007-04-17 17:22:13 -0400546static 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 -0500547{
548 return 0;
549}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550#endif
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552/*
Trond Myklebuste7d39062008-06-13 12:12:32 -0400553 * Search for an existing write request, and attempt to update
554 * it to reflect a new dirty region on a given page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 *
Trond Myklebuste7d39062008-06-13 12:12:32 -0400556 * If the attempt fails, then the existing request is flushed out
557 * to disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 */
Trond Myklebuste7d39062008-06-13 12:12:32 -0400559static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
560 struct page *page,
561 unsigned int offset,
562 unsigned int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Trond Myklebuste7d39062008-06-13 12:12:32 -0400564 struct nfs_page *req;
565 unsigned int rqend;
566 unsigned int end;
567 int error;
568
569 if (!PagePrivate(page))
570 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 end = offset + bytes;
Trond Myklebuste7d39062008-06-13 12:12:32 -0400573 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 for (;;) {
Trond Myklebust277459d2006-12-05 00:35:35 -0500576 req = nfs_page_find_request_locked(page);
Trond Myklebuste7d39062008-06-13 12:12:32 -0400577 if (req == NULL)
578 goto out_unlock;
Trond Myklebust277459d2006-12-05 00:35:35 -0500579
Trond Myklebuste7d39062008-06-13 12:12:32 -0400580 rqend = req->wb_offset + req->wb_bytes;
581 /*
582 * Tell the caller to flush out the request if
583 * the offsets are non-contiguous.
584 * Note: nfs_flush_incompatible() will already
585 * have flushed out requests having wrong owners.
586 */
Trond Myklebuste468bae2008-06-13 13:25:22 -0400587 if (offset > rqend
Trond Myklebuste7d39062008-06-13 12:12:32 -0400588 || end < req->wb_offset)
589 goto out_flushme;
590
591 if (nfs_set_page_tag_locked(req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Trond Myklebuste7d39062008-06-13 12:12:32 -0400594 /* The request is locked, so wait and then retry */
Trond Myklebust587142f2007-07-02 09:57:54 -0400595 spin_unlock(&inode->i_lock);
Trond Myklebuste7d39062008-06-13 12:12:32 -0400596 error = nfs_wait_on_request(req);
597 nfs_release_request(req);
598 if (error != 0)
599 goto out_err;
600 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602
Trond Myklebuste468bae2008-06-13 13:25:22 -0400603 if (nfs_clear_request_commit(req))
604 radix_tree_tag_clear(&NFS_I(inode)->nfs_page_tree,
605 req->wb_index, NFS_PAGE_TAG_COMMIT);
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /* Okay, the request matches. Update the region */
608 if (offset < req->wb_offset) {
609 req->wb_offset = offset;
610 req->wb_pgbase = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (end > rqend)
613 req->wb_bytes = end - req->wb_offset;
Trond Myklebuste7d39062008-06-13 12:12:32 -0400614 else
615 req->wb_bytes = rqend - req->wb_offset;
616out_unlock:
617 spin_unlock(&inode->i_lock);
618 return req;
619out_flushme:
620 spin_unlock(&inode->i_lock);
621 nfs_release_request(req);
622 error = nfs_wb_page(inode, page);
623out_err:
624 return ERR_PTR(error);
625}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Trond Myklebuste7d39062008-06-13 12:12:32 -0400627/*
628 * Try to update an existing write request, or create one if there is none.
629 *
630 * Note: Should always be called with the Page Lock held to prevent races
631 * if we have to add a new request. Also assumes that the caller has
632 * already called nfs_flush_incompatible() if necessary.
633 */
634static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
635 struct page *page, unsigned int offset, unsigned int bytes)
636{
637 struct inode *inode = page->mapping->host;
638 struct nfs_page *req;
639 int error;
640
641 req = nfs_try_to_update_request(inode, page, offset, bytes);
642 if (req != NULL)
643 goto out;
644 req = nfs_create_request(ctx, inode, page, offset, bytes);
645 if (IS_ERR(req))
646 goto out;
647 error = nfs_inode_add_request(inode, req);
648 if (error != 0) {
649 nfs_release_request(req);
650 req = ERR_PTR(error);
651 }
Trond Myklebustefc91ed2008-06-10 18:31:00 -0400652out:
Trond Myklebust61e930a2007-10-18 17:08:05 -0400653 return req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Trond Myklebuste7d39062008-06-13 12:12:32 -0400656static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
657 unsigned int offset, unsigned int count)
658{
659 struct nfs_page *req;
660
661 req = nfs_setup_write_request(ctx, page, offset, count);
662 if (IS_ERR(req))
663 return PTR_ERR(req);
664 /* Update file length */
665 nfs_grow_file(page, offset, count);
666 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
667 nfs_clear_page_tag_locked(req);
668 return 0;
669}
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671int nfs_flush_incompatible(struct file *file, struct page *page)
672{
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400673 struct nfs_open_context *ctx = nfs_file_open_context(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 struct nfs_page *req;
Trond Myklebust1a545332006-12-05 00:35:40 -0500675 int do_flush, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 /*
677 * Look for a request corresponding to this page. If there
678 * is one, and it belongs to another file, we flush it out
679 * before we try to copy anything into the page. Do this
680 * due to the lack of an ACCESS-type call in NFSv2.
681 * Also do the same if we find a request from an existing
682 * dropped page.
683 */
Trond Myklebust1a545332006-12-05 00:35:40 -0500684 do {
685 req = nfs_page_find_request(page);
686 if (req == NULL)
687 return 0;
Trond Myklebuste468bae2008-06-13 13:25:22 -0400688 do_flush = req->wb_page != page || req->wb_context != ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 nfs_release_request(req);
Trond Myklebust1a545332006-12-05 00:35:40 -0500690 if (!do_flush)
691 return 0;
692 status = nfs_wb_page(page->mapping->host, page);
693 } while (status == 0);
694 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
697/*
Trond Myklebust5d47a352008-02-07 17:24:07 -0500698 * If the page cache is marked as unsafe or invalid, then we can't rely on
699 * the PageUptodate() flag. In this case, we will need to turn off
700 * write optimisations that depend on the page contents being correct.
701 */
702static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
703{
704 return PageUptodate(page) &&
705 !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
706}
707
708/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 * Update and possibly write a cached page of an NFS file.
710 *
711 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
712 * things with a page scheduled for an RPC call (e.g. invalidate it).
713 */
714int nfs_updatepage(struct file *file, struct page *page,
715 unsigned int offset, unsigned int count)
716{
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400717 struct nfs_open_context *ctx = nfs_file_open_context(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 int status = 0;
720
Chuck Lever91d5b472006-03-20 13:44:14 -0500721 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
722
Chuck Lever48186c72008-06-11 17:56:05 -0400723 dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n",
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800724 file->f_path.dentry->d_parent->d_name.name,
725 file->f_path.dentry->d_name.name, count,
Chuck Lever48186c72008-06-11 17:56:05 -0400726 (long long)(page_offset(page) + offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 /* If we're not using byte range locks, and we know the page
Trond Myklebust5d47a352008-02-07 17:24:07 -0500729 * is up to date, it may be more efficient to extend the write
730 * to cover the entire page in order to avoid fragmentation
731 * inefficiencies.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 */
Trond Myklebust5d47a352008-02-07 17:24:07 -0500733 if (nfs_write_pageuptodate(page, inode) &&
734 inode->i_flock == NULL &&
Trond Myklebust4b5621f2008-02-25 15:56:29 -0800735 !(file->f_flags & O_SYNC)) {
Trond Myklebust49a70f22006-12-05 00:35:38 -0500736 count = max(count + offset, nfs_page_length(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739
Trond Myklebuste21195a2006-12-05 00:35:39 -0500740 status = nfs_writepage_setup(ctx, page, offset, count);
Trond Myklebust03fa9e82008-06-05 16:02:35 -0400741 if (status < 0)
742 nfs_set_pageerror(page);
743 else
744 __set_page_dirty_nobuffers(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Chuck Lever48186c72008-06-11 17:56:05 -0400746 dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 status, (long long)i_size_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return status;
749}
750
751static void nfs_writepage_release(struct nfs_page *req)
752{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Trond Myklebust7e5f6142008-05-25 12:59:19 -0400754 if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req)) {
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400755 nfs_end_page_writeback(req->wb_page);
756 nfs_inode_remove_request(req);
757 } else
758 nfs_end_page_writeback(req->wb_page);
Trond Myklebust9fd367f2007-06-17 15:10:24 -0400759 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Trond Myklebust3ff75762007-07-14 15:40:00 -0400762static int flush_task_priority(int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
765 case FLUSH_HIGHPRI:
766 return RPC_PRIORITY_HIGH;
767 case FLUSH_LOWPRI:
768 return RPC_PRIORITY_LOW;
769 }
770 return RPC_PRIORITY_NORMAL;
771}
772
773/*
774 * Set up the argument/result storage required for the RPC call.
775 */
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400776static int nfs_write_rpcsetup(struct nfs_page *req,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500778 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 unsigned int count, unsigned int offset,
780 int how)
781{
Trond Myklebust84115e12007-07-14 15:39:59 -0400782 struct inode *inode = req->wb_context->path.dentry->d_inode;
783 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
Trond Myklebust3ff75762007-07-14 15:40:00 -0400784 int priority = flush_task_priority(how);
Trond Myklebust07737692007-10-25 18:42:54 -0400785 struct rpc_task *task;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400786 struct rpc_message msg = {
787 .rpc_argp = &data->args,
788 .rpc_resp = &data->res,
789 .rpc_cred = req->wb_context->cred,
790 };
Trond Myklebust84115e12007-07-14 15:39:59 -0400791 struct rpc_task_setup task_setup_data = {
792 .rpc_client = NFS_CLIENT(inode),
Trond Myklebust07737692007-10-25 18:42:54 -0400793 .task = &data->task,
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400794 .rpc_message = &msg,
Trond Myklebust84115e12007-07-14 15:39:59 -0400795 .callback_ops = call_ops,
796 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -0500797 .workqueue = nfsiod_workqueue,
Trond Myklebust84115e12007-07-14 15:39:59 -0400798 .flags = flags,
Trond Myklebust3ff75762007-07-14 15:40:00 -0400799 .priority = priority,
Trond Myklebust84115e12007-07-14 15:39:59 -0400800 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 /* Set up the RPC argument and reply structs
803 * NB: take care not to mess about with data->commit et al. */
804
805 data->req = req;
Trond Myklebust88be9f92007-06-05 10:42:27 -0400806 data->inode = inode = req->wb_context->path.dentry->d_inode;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400807 data->cred = msg.rpc_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 data->args.fh = NFS_FH(inode);
810 data->args.offset = req_offset(req) + offset;
811 data->args.pgbase = req->wb_pgbase + offset;
812 data->args.pages = data->pagevec;
813 data->args.count = count;
Trond Myklebust383ba712008-02-19 20:04:20 -0500814 data->args.context = get_nfs_open_context(req->wb_context);
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400815 data->args.stable = NFS_UNSTABLE;
816 if (how & FLUSH_STABLE) {
817 data->args.stable = NFS_DATA_SYNC;
818 if (!NFS_I(inode)->ncommit)
819 data->args.stable = NFS_FILE_SYNC;
820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 data->res.fattr = &data->fattr;
823 data->res.count = count;
824 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400825 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Trond Myklebust788e7a82006-03-20 13:44:27 -0500827 /* Set up the initial task struct. */
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400828 NFS_PROTO(inode)->write_setup(data, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Chuck Levera3f565b2007-01-31 12:14:01 -0500830 dprintk("NFS: %5u initiated write call "
Chuck Lever48186c72008-06-11 17:56:05 -0400831 "(req %s/%lld, %u bytes @ offset %llu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500832 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 inode->i_sb->s_id,
834 (long long)NFS_FILEID(inode),
835 count,
836 (unsigned long long)data->args.offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Trond Myklebust07737692007-10-25 18:42:54 -0400838 task = rpc_run_task(&task_setup_data);
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400839 if (IS_ERR(task))
840 return PTR_ERR(task);
841 rpc_put_task(task);
842 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
Fred6d884e82008-03-19 11:24:38 -0400845/* If a nfs_flush_* function fails, it should remove reqs from @head and
846 * call this on each, which will prepare them to be retried on next
847 * writeback using standard nfs.
848 */
849static void nfs_redirty_request(struct nfs_page *req)
850{
851 nfs_mark_request_dirty(req);
852 nfs_end_page_writeback(req->wb_page);
853 nfs_clear_page_tag_locked(req);
854}
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856/*
857 * Generate multiple small requests to write out a single
858 * contiguous dirty area on one page.
859 */
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400860static 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 -0700861{
862 struct nfs_page *req = nfs_list_entry(head->next);
863 struct page *page = req->wb_page;
864 struct nfs_write_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700865 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
866 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 int requests = 0;
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400868 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 LIST_HEAD(list);
870
871 nfs_list_remove_request(req);
872
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400873 nbytes = count;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700874 do {
875 size_t len = min(nbytes, wsize);
876
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400877 data = nfs_writedata_alloc(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (!data)
879 goto out_bad;
880 list_add(&data->pages, &list);
881 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700882 nbytes -= len;
883 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 atomic_set(&req->wb_complete, requests);
885
886 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 offset = 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400888 nbytes = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 do {
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400890 int ret2;
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 data = list_entry(list.next, struct nfs_write_data, pages);
893 list_del_init(&data->pages);
894
895 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400897 if (nbytes < wsize)
898 wsize = nbytes;
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400899 ret2 = nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400900 wsize, offset, how);
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400901 if (ret == 0)
902 ret = ret2;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400903 offset += wsize;
904 nbytes -= wsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 } while (nbytes != 0);
906
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400907 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909out_bad:
910 while (!list_empty(&list)) {
911 data = list_entry(list.next, struct nfs_write_data, pages);
912 list_del(&data->pages);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500913 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
Trond Myklebust61822ab2006-12-05 00:35:42 -0500915 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return -ENOMEM;
917}
918
919/*
920 * Create an RPC task for the given write request and kick it.
921 * The page must have been locked by the caller.
922 *
923 * It may happen that the page we're passed is not marked dirty.
924 * This is the case if nfs_updatepage detects a conflicting request
925 * that has been written but not committed.
926 */
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400927static 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 -0700928{
929 struct nfs_page *req;
930 struct page **pages;
931 struct nfs_write_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400933 data = nfs_writedata_alloc(npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (!data)
935 goto out_bad;
936
937 pages = data->pagevec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 while (!list_empty(head)) {
939 req = nfs_list_entry(head->next);
940 nfs_list_remove_request(req);
941 nfs_list_add_request(req, &data->pages);
942 ClearPageError(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 *pages++ = req->wb_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945 req = nfs_list_entry(data->pages.next);
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 /* Set up the argument struct */
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400948 return nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 out_bad:
950 while (!list_empty(head)) {
Trond Myklebust10afec92007-05-14 17:16:04 -0400951 req = nfs_list_entry(head->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 nfs_list_remove_request(req);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500953 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
955 return -ENOMEM;
956}
957
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400958static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
959 struct inode *inode, int ioflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Chuck Leverbf4285e2007-12-20 14:54:57 -0500961 size_t wsize = NFS_SERVER(inode)->wsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400963 if (wsize < PAGE_CACHE_SIZE)
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400964 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400965 else
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400966 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
969/*
970 * Handle a write reply that flushed part of a page.
971 */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500972static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Trond Myklebust788e7a82006-03-20 13:44:27 -0500974 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Chuck Lever48186c72008-06-11 17:56:05 -0400976 dprintk("NFS: %5u write(%s/%lld %d@%lld)",
977 task->tk_pid,
978 data->req->wb_context->path.dentry->d_inode->i_sb->s_id,
979 (long long)
980 NFS_FILEID(data->req->wb_context->path.dentry->d_inode),
981 data->req->wb_bytes, (long long)req_offset(data->req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Trond Myklebustc9d8f892008-04-15 16:56:39 -0400983 nfs_writeback_done(task, data);
984}
Trond Myklebust788e7a82006-03-20 13:44:27 -0500985
Trond Myklebustc9d8f892008-04-15 16:56:39 -0400986static void nfs_writeback_release_partial(void *calldata)
987{
988 struct nfs_write_data *data = calldata;
989 struct nfs_page *req = data->req;
990 struct page *page = req->wb_page;
991 int status = data->task.tk_status;
992
993 if (status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -0800994 nfs_set_pageerror(page);
Trond Myklebustc9d8f892008-04-15 16:56:39 -0400995 nfs_context_set_write_error(req->wb_context, status);
996 dprintk(", error = %d\n", status);
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400997 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001000 if (nfs_write_need_commit(data)) {
Trond Myklebust587142f2007-07-02 09:57:54 -04001001 struct inode *inode = page->mapping->host;
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001002
Trond Myklebust587142f2007-07-02 09:57:54 -04001003 spin_lock(&inode->i_lock);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001004 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
1005 /* Do nothing we need to resend the writes */
1006 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1007 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1008 dprintk(" defer commit\n");
1009 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1010 set_bit(PG_NEED_RESCHED, &req->wb_flags);
1011 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
1012 dprintk(" server reboot detected\n");
1013 }
Trond Myklebust587142f2007-07-02 09:57:54 -04001014 spin_unlock(&inode->i_lock);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001015 } else
1016 dprintk(" OK\n");
1017
1018out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (atomic_dec_and_test(&req->wb_complete))
1020 nfs_writepage_release(req);
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001021 nfs_writedata_release(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
Trond Myklebust788e7a82006-03-20 13:44:27 -05001024static const struct rpc_call_ops nfs_write_partial_ops = {
1025 .rpc_call_done = nfs_writeback_done_partial,
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001026 .rpc_release = nfs_writeback_release_partial,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001027};
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029/*
1030 * Handle a write reply that flushes a whole page.
1031 *
1032 * FIXME: There is an inherent race with invalidate_inode_pages and
1033 * writebacks since the page->count is kept > 1 for as long
1034 * as the page has a write request pending.
1035 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001036static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001038 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001040 nfs_writeback_done(task, data);
1041}
1042
1043static void nfs_writeback_release_full(void *calldata)
1044{
1045 struct nfs_write_data *data = calldata;
1046 int status = data->task.tk_status;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 /* Update attributes as result of writeback. */
1049 while (!list_empty(&data->pages)) {
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001050 struct nfs_page *req = nfs_list_entry(data->pages.next);
1051 struct page *page = req->wb_page;
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 nfs_list_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Chuck Lever48186c72008-06-11 17:56:05 -04001055 dprintk("NFS: %5u write (%s/%lld %d@%lld)",
1056 data->task.tk_pid,
Trond Myklebust88be9f92007-06-05 10:42:27 -04001057 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1058 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 req->wb_bytes,
1060 (long long)req_offset(req));
1061
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001062 if (status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -08001063 nfs_set_pageerror(page);
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001064 nfs_context_set_write_error(req->wb_context, status);
1065 dprintk(", error = %d\n", status);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001066 goto remove_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001069 if (nfs_write_need_commit(data)) {
1070 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1071 nfs_mark_request_commit(req);
1072 nfs_end_page_writeback(page);
1073 dprintk(" marked for commit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 goto next;
1075 }
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001076 dprintk(" OK\n");
1077remove_request:
1078 nfs_end_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 nfs_inode_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 next:
Trond Myklebust9fd367f2007-06-17 15:10:24 -04001081 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001083 nfs_writedata_release(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
1085
Trond Myklebust788e7a82006-03-20 13:44:27 -05001086static const struct rpc_call_ops nfs_write_full_ops = {
1087 .rpc_call_done = nfs_writeback_done_full,
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001088 .rpc_release = nfs_writeback_release_full,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001089};
1090
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092/*
1093 * This function is called when the WRITE call is complete.
1094 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001095int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 struct nfs_writeargs *argp = &data->args;
1098 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001099 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Chuck Levera3f565b2007-01-31 12:14:01 -05001101 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 task->tk_pid, task->tk_status);
1103
Chuck Leverf551e442006-09-20 14:33:04 -04001104 /*
1105 * ->write_done will attempt to use post-op attributes to detect
1106 * conflicting writes by other clients. A strict interpretation
1107 * of close-to-open would allow us to continue caching even if
1108 * another writer had changed the file, but some applications
1109 * depend on tighter cache coherency when writing.
1110 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001111 status = NFS_PROTO(data->inode)->write_done(task, data);
1112 if (status != 0)
1113 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001114 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1117 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1118 /* We tried a write call, but the server did not
1119 * commit data to stable storage even though we
1120 * requested it.
1121 * Note: There is a known bug in Tru64 < 5.0 in which
1122 * the server reports NFS_DATA_SYNC, but performs
1123 * NFS_FILE_SYNC. We therefore implement this checking
1124 * as a dprintk() in order to avoid filling syslog.
1125 */
1126 static unsigned long complain;
1127
1128 if (time_before(complain, jiffies)) {
Chuck Lever48186c72008-06-11 17:56:05 -04001129 dprintk("NFS: faulty NFS server %s:"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 " (committed = %d) != (stable = %d)\n",
David Howells54ceac42006-08-22 20:06:13 -04001131 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 resp->verf->committed, argp->stable);
1133 complain = jiffies + 300 * HZ;
1134 }
1135 }
1136#endif
1137 /* Is this a short write? */
1138 if (task->tk_status >= 0 && resp->count < argp->count) {
1139 static unsigned long complain;
1140
Chuck Lever91d5b472006-03-20 13:44:14 -05001141 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 /* Has the server at least made some progress? */
1144 if (resp->count != 0) {
1145 /* Was this an NFSv2 write or an NFSv3 stable write? */
1146 if (resp->verf->committed != NFS_UNSTABLE) {
1147 /* Resend from where the server left off */
1148 argp->offset += resp->count;
1149 argp->pgbase += resp->count;
1150 argp->count -= resp->count;
1151 } else {
1152 /* Resend as a stable write in order to avoid
1153 * headaches in the case of a server crash.
1154 */
1155 argp->stable = NFS_FILE_SYNC;
1156 }
1157 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001158 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
1160 if (time_before(complain, jiffies)) {
1161 printk(KERN_WARNING
1162 "NFS: Server wrote zero bytes, expected %u.\n",
1163 argp->count);
1164 complain = jiffies + 300 * HZ;
1165 }
1166 /* Can't do anything about it except throw an error. */
1167 task->tk_status = -EIO;
1168 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
1172
1173#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001174void nfs_commitdata_release(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
Trond Myklebust383ba712008-02-19 20:04:20 -05001176 struct nfs_write_data *wdata = data;
1177
1178 put_nfs_open_context(wdata->args.context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 nfs_commit_free(wdata);
1180}
1181
1182/*
1183 * Set up the argument/result storage required for the RPC call.
1184 */
Trond Myklebustdbae4c72008-04-14 14:54:53 -04001185static int nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001186 struct nfs_write_data *data,
1187 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Trond Myklebust84115e12007-07-14 15:39:59 -04001189 struct nfs_page *first = nfs_list_entry(head->next);
1190 struct inode *inode = first->wb_context->path.dentry->d_inode;
1191 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
Trond Myklebust3ff75762007-07-14 15:40:00 -04001192 int priority = flush_task_priority(how);
Trond Myklebust07737692007-10-25 18:42:54 -04001193 struct rpc_task *task;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001194 struct rpc_message msg = {
1195 .rpc_argp = &data->args,
1196 .rpc_resp = &data->res,
1197 .rpc_cred = first->wb_context->cred,
1198 };
Trond Myklebust84115e12007-07-14 15:39:59 -04001199 struct rpc_task_setup task_setup_data = {
Trond Myklebust07737692007-10-25 18:42:54 -04001200 .task = &data->task,
Trond Myklebust84115e12007-07-14 15:39:59 -04001201 .rpc_client = NFS_CLIENT(inode),
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001202 .rpc_message = &msg,
Trond Myklebust84115e12007-07-14 15:39:59 -04001203 .callback_ops = &nfs_commit_ops,
1204 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001205 .workqueue = nfsiod_workqueue,
Trond Myklebust84115e12007-07-14 15:39:59 -04001206 .flags = flags,
Trond Myklebust3ff75762007-07-14 15:40:00 -04001207 .priority = priority,
Trond Myklebust84115e12007-07-14 15:39:59 -04001208 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 /* Set up the RPC argument and reply structs
1211 * NB: take care not to mess about with data->commit et al. */
1212
1213 list_splice_init(head, &data->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 data->inode = inode;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001216 data->cred = msg.rpc_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001219 /* Note: we always request a commit of the entire inode */
1220 data->args.offset = 0;
1221 data->args.count = 0;
Trond Myklebust383ba712008-02-19 20:04:20 -05001222 data->args.context = get_nfs_open_context(first->wb_context);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001223 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 data->res.fattr = &data->fattr;
1225 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001226 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001227
1228 /* Set up the initial task struct. */
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001229 NFS_PROTO(inode)->commit_setup(data, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Chuck Levera3f565b2007-01-31 12:14:01 -05001231 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001232
Trond Myklebust07737692007-10-25 18:42:54 -04001233 task = rpc_run_task(&task_setup_data);
Trond Myklebustdbae4c72008-04-14 14:54:53 -04001234 if (IS_ERR(task))
1235 return PTR_ERR(task);
1236 rpc_put_task(task);
1237 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
1240/*
1241 * Commit dirty pages
1242 */
1243static int
Chuck Lever40859d72005-11-30 18:09:02 -05001244nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
1246 struct nfs_write_data *data;
1247 struct nfs_page *req;
1248
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001249 data = nfs_commitdata_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 if (!data)
1252 goto out_bad;
1253
1254 /* Set up the argument struct */
Trond Myklebustdbae4c72008-04-14 14:54:53 -04001255 return nfs_commit_rpcsetup(head, data, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 out_bad:
1257 while (!list_empty(head)) {
1258 req = nfs_list_entry(head->next);
1259 nfs_list_remove_request(req);
1260 nfs_mark_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001261 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -07001262 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1263 BDI_RECLAIMABLE);
Trond Myklebust9fd367f2007-06-17 15:10:24 -04001264 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266 return -ENOMEM;
1267}
1268
1269/*
1270 * COMMIT call returned
1271 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001272static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001274 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Chuck Levera3f565b2007-01-31 12:14:01 -05001276 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 task->tk_pid, task->tk_status);
1278
Trond Myklebust788e7a82006-03-20 13:44:27 -05001279 /* Call the NFS version-specific code */
1280 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1281 return;
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001282}
1283
1284static void nfs_commit_release(void *calldata)
1285{
1286 struct nfs_write_data *data = calldata;
1287 struct nfs_page *req;
1288 int status = data->task.tk_status;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 while (!list_empty(&data->pages)) {
1291 req = nfs_list_entry(data->pages.next);
1292 nfs_list_remove_request(req);
Trond Myklebuste468bae2008-06-13 13:25:22 -04001293 nfs_clear_request_commit(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Chuck Lever48186c72008-06-11 17:56:05 -04001295 dprintk("NFS: commit (%s/%lld %d@%lld)",
Trond Myklebust88be9f92007-06-05 10:42:27 -04001296 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1297 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 req->wb_bytes,
1299 (long long)req_offset(req));
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001300 if (status < 0) {
1301 nfs_context_set_write_error(req->wb_context, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 nfs_inode_remove_request(req);
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001303 dprintk(", error = %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 goto next;
1305 }
1306
1307 /* Okay, COMMIT succeeded, apparently. Check the verifier
1308 * returned by the server against all stored verfs. */
1309 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1310 /* We have a match */
1311 nfs_inode_remove_request(req);
1312 dprintk(" OK\n");
1313 goto next;
1314 }
1315 /* We have a mismatch. Write the page again */
1316 dprintk(" mismatch\n");
Fred6d884e82008-03-19 11:24:38 -04001317 nfs_mark_request_dirty(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 next:
Trond Myklebust9fd367f2007-06-17 15:10:24 -04001319 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 }
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001321 nfs_commitdata_release(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001323
1324static const struct rpc_call_ops nfs_commit_ops = {
1325 .rpc_call_done = nfs_commit_done,
1326 .rpc_release = nfs_commit_release,
1327};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001329int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001332 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Trond Myklebust587142f2007-07-02 09:57:54 -04001334 spin_lock(&inode->i_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001335 res = nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebust587142f2007-07-02 09:57:54 -04001336 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001338 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001339 if (error < 0)
1340 return error;
1341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return res;
1343}
Trond Myklebustc63c7b02007-04-02 19:29:52 -04001344#else
1345static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1346{
1347 return 0;
1348}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349#endif
1350
Trond Myklebust1c759502006-10-09 16:18:38 -04001351long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Trond Myklebust1c759502006-10-09 16:18:38 -04001353 struct inode *inode = mapping->host;
Trond Myklebustca52fec2007-04-17 17:22:13 -04001354 pgoff_t idx_start, idx_end;
Trond Myklebust1c759502006-10-09 16:18:38 -04001355 unsigned int npages = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001356 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001357 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebust3f442542006-09-17 14:46:44 -04001358 long pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Trond Myklebust1c759502006-10-09 16:18:38 -04001360 /* FIXME */
1361 if (wbc->range_cyclic)
1362 idx_start = 0;
1363 else {
1364 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1365 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1366 if (idx_end > idx_start) {
Trond Myklebustca52fec2007-04-17 17:22:13 -04001367 pgoff_t l_npages = 1 + idx_end - idx_start;
Trond Myklebust1c759502006-10-09 16:18:38 -04001368 npages = l_npages;
1369 if (sizeof(npages) != sizeof(l_npages) &&
Trond Myklebustca52fec2007-04-17 17:22:13 -04001370 (pgoff_t)npages != l_npages)
Trond Myklebust1c759502006-10-09 16:18:38 -04001371 npages = 0;
1372 }
1373 }
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001374 how &= ~FLUSH_NOCOMMIT;
Trond Myklebust587142f2007-07-02 09:57:54 -04001375 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 do {
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001377 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1378 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001379 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001380 if (nocommit)
1381 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001382 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebust724c4392007-04-17 17:22:13 -04001383 if (pages == 0)
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001384 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001385 if (how & FLUSH_INVALIDATE) {
Trond Myklebust587142f2007-07-02 09:57:54 -04001386 spin_unlock(&inode->i_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001387 nfs_cancel_commit_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001388 ret = pages;
Trond Myklebust587142f2007-07-02 09:57:54 -04001389 spin_lock(&inode->i_lock);
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001390 continue;
1391 }
1392 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebust587142f2007-07-02 09:57:54 -04001393 spin_unlock(&inode->i_lock);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001394 ret = nfs_commit_list(inode, &head, how);
Trond Myklebust587142f2007-07-02 09:57:54 -04001395 spin_lock(&inode->i_lock);
1396
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001397 } while (ret >= 0);
Trond Myklebust587142f2007-07-02 09:57:54 -04001398 spin_unlock(&inode->i_lock);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001399 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
1401
Trond Myklebust34901f72007-07-25 14:09:54 -04001402static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
Trond Myklebust1c759502006-10-09 16:18:38 -04001403{
Trond Myklebust1c759502006-10-09 16:18:38 -04001404 int ret;
1405
Trond Myklebust34901f72007-07-25 14:09:54 -04001406 ret = nfs_writepages(mapping, wbc);
Trond Myklebust61822ab2006-12-05 00:35:42 -05001407 if (ret < 0)
1408 goto out;
Trond Myklebust34901f72007-07-25 14:09:54 -04001409 ret = nfs_sync_mapping_wait(mapping, wbc, how);
Trond Myklebusted90ef52007-07-20 13:13:28 -04001410 if (ret < 0)
1411 goto out;
1412 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001413out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001414 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Trond Myklebust1c759502006-10-09 16:18:38 -04001415 return ret;
1416}
1417
Trond Myklebust34901f72007-07-25 14:09:54 -04001418/* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
1419static int nfs_write_mapping(struct address_space *mapping, int how)
1420{
1421 struct writeback_control wbc = {
1422 .bdi = mapping->backing_dev_info,
1423 .sync_mode = WB_SYNC_NONE,
1424 .nr_to_write = LONG_MAX,
1425 .for_writepages = 1,
1426 .range_cyclic = 1,
1427 };
1428 int ret;
1429
1430 ret = __nfs_write_mapping(mapping, &wbc, how);
1431 if (ret < 0)
1432 return ret;
1433 wbc.sync_mode = WB_SYNC_ALL;
1434 return __nfs_write_mapping(mapping, &wbc, how);
1435}
1436
Trond Myklebusted90ef52007-07-20 13:13:28 -04001437/*
1438 * flush the inode to disk.
1439 */
1440int nfs_wb_all(struct inode *inode)
Trond Myklebust1c759502006-10-09 16:18:38 -04001441{
Trond Myklebusted90ef52007-07-20 13:13:28 -04001442 return nfs_write_mapping(inode->i_mapping, 0);
1443}
Trond Myklebust1c759502006-10-09 16:18:38 -04001444
Trond Myklebusted90ef52007-07-20 13:13:28 -04001445int nfs_wb_nocommit(struct inode *inode)
1446{
1447 return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
Trond Myklebust1c759502006-10-09 16:18:38 -04001448}
1449
Trond Myklebust1b3b4a12007-08-28 10:29:36 -04001450int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1451{
1452 struct nfs_page *req;
1453 loff_t range_start = page_offset(page);
1454 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1455 struct writeback_control wbc = {
1456 .bdi = page->mapping->backing_dev_info,
1457 .sync_mode = WB_SYNC_ALL,
1458 .nr_to_write = LONG_MAX,
1459 .range_start = range_start,
1460 .range_end = range_end,
1461 };
1462 int ret = 0;
1463
1464 BUG_ON(!PageLocked(page));
1465 for (;;) {
1466 req = nfs_page_find_request(page);
1467 if (req == NULL)
1468 goto out;
Trond Myklebuste468bae2008-06-13 13:25:22 -04001469 if (test_bit(PG_CLEAN, &req->wb_flags)) {
Trond Myklebust1b3b4a12007-08-28 10:29:36 -04001470 nfs_release_request(req);
1471 break;
1472 }
1473 if (nfs_lock_request_dontget(req)) {
1474 nfs_inode_remove_request(req);
1475 /*
1476 * In case nfs_inode_remove_request has marked the
1477 * page as being dirty
1478 */
1479 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1480 nfs_unlock_request(req);
1481 break;
1482 }
1483 ret = nfs_wait_on_request(req);
1484 if (ret < 0)
1485 goto out;
1486 }
1487 if (!PagePrivate(page))
1488 return 0;
1489 ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
1490out:
1491 return ret;
1492}
1493
Adrian Bunk5334eb12007-11-21 15:04:31 -08001494static int nfs_wb_page_priority(struct inode *inode, struct page *page,
1495 int how)
Trond Myklebust1c759502006-10-09 16:18:38 -04001496{
1497 loff_t range_start = page_offset(page);
1498 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001499 struct writeback_control wbc = {
1500 .bdi = page->mapping->backing_dev_info,
1501 .sync_mode = WB_SYNC_ALL,
1502 .nr_to_write = LONG_MAX,
1503 .range_start = range_start,
1504 .range_end = range_end,
1505 };
1506 int ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001507
Trond Myklebust73e33022008-04-11 16:03:54 -04001508 do {
1509 if (clear_page_dirty_for_io(page)) {
1510 ret = nfs_writepage_locked(page, &wbc);
1511 if (ret < 0)
1512 goto out_error;
1513 } else if (!PagePrivate(page))
1514 break;
1515 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001516 if (ret < 0)
Trond Myklebust73e33022008-04-11 16:03:54 -04001517 goto out_error;
1518 } while (PagePrivate(page));
1519 return 0;
1520out_error:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001521 __mark_inode_dirty(inode, I_DIRTY_PAGES);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001522 return ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001523}
1524
1525/*
1526 * Write back all requests on one page - we do this before reading it.
1527 */
1528int nfs_wb_page(struct inode *inode, struct page* page)
1529{
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001530 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
Trond Myklebust1c759502006-10-09 16:18:38 -04001531}
1532
David Howellsf7b422b2006-06-09 09:34:33 -04001533int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
1535 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1536 sizeof(struct nfs_write_data),
1537 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001538 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 if (nfs_wdata_cachep == NULL)
1540 return -ENOMEM;
1541
Matthew Dobson93d23412006-03-26 01:37:50 -08001542 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1543 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if (nfs_wdata_mempool == NULL)
1545 return -ENOMEM;
1546
Matthew Dobson93d23412006-03-26 01:37:50 -08001547 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1548 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 if (nfs_commit_mempool == NULL)
1550 return -ENOMEM;
1551
Peter Zijlstra89a09142007-03-16 13:38:26 -08001552 /*
1553 * NFS congestion size, scale with available memory.
1554 *
1555 * 64MB: 8192k
1556 * 128MB: 11585k
1557 * 256MB: 16384k
1558 * 512MB: 23170k
1559 * 1GB: 32768k
1560 * 2GB: 46340k
1561 * 4GB: 65536k
1562 * 8GB: 92681k
1563 * 16GB: 131072k
1564 *
1565 * This allows larger machines to have larger/more transfers.
1566 * Limit the default to 256M
1567 */
1568 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1569 if (nfs_congestion_kb > 256*1024)
1570 nfs_congestion_kb = 256*1024;
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return 0;
1573}
1574
David Brownell266bee82006-06-27 12:59:15 -07001575void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
1577 mempool_destroy(nfs_commit_mempool);
1578 mempool_destroy(nfs_wdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001579 kmem_cache_destroy(nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581