blob: 30513029c00cb630ef5f78dd2afb270a0d0983f5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/write.c
3 *
Trond Myklebust7c85d902006-12-13 15:23:48 -05004 * Write file data over NFS.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/types.h>
10#include <linux/slab.h>
11#include <linux/mm.h>
12#include <linux/pagemap.h>
13#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/writeback.h>
Peter Zijlstra89a09142007-03-16 13:38:26 -080015#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#include <linux/sunrpc/clnt.h>
18#include <linux/nfs_fs.h>
19#include <linux/nfs_mount.h>
20#include <linux/nfs_page.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070021#include <linux/backing-dev.h>
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include "delegation.h"
Trond Myklebust49a70f22006-12-05 00:35:38 -050026#include "internal.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050027#include "iostat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#define NFSDBG_FACILITY NFSDBG_PAGECACHE
30
31#define MIN_POOL_WRITE (32)
32#define MIN_POOL_COMMIT (4)
33
34/*
35 * Local function declarations
36 */
37static struct nfs_page * nfs_update_request(struct nfs_open_context*,
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 struct page *,
39 unsigned int, unsigned int);
Trond Myklebustc63c7b02007-04-02 19:29:52 -040040static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc,
41 struct inode *inode, int ioflags);
Trond Myklebust788e7a82006-03-20 13:44:27 -050042static const struct rpc_call_ops nfs_write_partial_ops;
43static const struct rpc_call_ops nfs_write_full_ops;
44static const struct rpc_call_ops nfs_commit_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Christoph Lametere18b8902006-12-06 20:33:20 -080046static struct kmem_cache *nfs_wdata_cachep;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050047static mempool_t *nfs_wdata_mempool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static mempool_t *nfs_commit_mempool;
49
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070050struct nfs_write_data *nfs_commit_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Christoph Lametere6b4f8d2006-12-06 20:33:14 -080052 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
Chuck Lever40859d72005-11-30 18:09:02 -050053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 if (p) {
55 memset(p, 0, sizeof(*p));
56 INIT_LIST_HEAD(&p->pages);
57 }
58 return p;
59}
60
Trond Myklebust5e4424a2008-02-25 21:53:49 -080061void nfs_commit_free(struct nfs_write_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Chuck Lever40859d72005-11-30 18:09:02 -050063 if (p && (p->pagevec != &p->page_array[0]))
64 kfree(p->pagevec);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 mempool_free(p, nfs_commit_mempool);
66}
67
Trond Myklebust8d5658c2007-04-10 09:26:35 -040068struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050069{
Christoph Lametere6b4f8d2006-12-06 20:33:14 -080070 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050071
72 if (p) {
73 memset(p, 0, sizeof(*p));
74 INIT_LIST_HEAD(&p->pages);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070075 p->npages = pagecount;
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040076 if (pagecount <= ARRAY_SIZE(p->page_array))
77 p->pagevec = p->page_array;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050078 else {
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040079 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
80 if (!p->pagevec) {
Trond Myklebust3feb2d42006-03-20 13:44:37 -050081 mempool_free(p, nfs_wdata_mempool);
82 p = NULL;
83 }
84 }
85 }
86 return p;
87}
88
Trond Myklebust5e4424a2008-02-25 21:53:49 -080089static void nfs_writedata_free(struct nfs_write_data *p)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050090{
91 if (p && (p->pagevec != &p->page_array[0]))
92 kfree(p->pagevec);
93 mempool_free(p, nfs_wdata_mempool);
94}
95
Trond Myklebust383ba712008-02-19 20:04:20 -050096void nfs_writedata_release(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Trond Myklebust383ba712008-02-19 20:04:20 -050098 struct nfs_write_data *wdata = data;
99
100 put_nfs_open_context(wdata->args.context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 nfs_writedata_free(wdata);
102}
103
Trond Myklebust7b159fc2007-07-25 14:09:54 -0400104static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
105{
106 ctx->error = error;
107 smp_wmb();
108 set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
109}
110
Trond Myklebust277459d2006-12-05 00:35:35 -0500111static struct nfs_page *nfs_page_find_request_locked(struct page *page)
112{
113 struct nfs_page *req = NULL;
114
115 if (PagePrivate(page)) {
116 req = (struct nfs_page *)page_private(page);
117 if (req != NULL)
Trond Myklebustc03b4022007-06-17 13:26:38 -0400118 kref_get(&req->wb_kref);
Trond Myklebust277459d2006-12-05 00:35:35 -0500119 }
120 return req;
121}
122
123static struct nfs_page *nfs_page_find_request(struct page *page)
124{
Trond Myklebust587142f2007-07-02 09:57:54 -0400125 struct inode *inode = page->mapping->host;
Trond Myklebust277459d2006-12-05 00:35:35 -0500126 struct nfs_page *req = NULL;
Trond Myklebust277459d2006-12-05 00:35:35 -0500127
Trond Myklebust587142f2007-07-02 09:57:54 -0400128 spin_lock(&inode->i_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500129 req = nfs_page_find_request_locked(page);
Trond Myklebust587142f2007-07-02 09:57:54 -0400130 spin_unlock(&inode->i_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500131 return req;
132}
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/* Adjust the file length if we're writing beyond the end */
135static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
136{
137 struct inode *inode = page->mapping->host;
138 loff_t end, i_size = i_size_read(inode);
Trond Myklebustca52fec2007-04-17 17:22:13 -0400139 pgoff_t end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 if (i_size > 0 && page->index < end_index)
142 return;
143 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
144 if (i_size >= end)
145 return;
Chuck Lever91d5b472006-03-20 13:44:14 -0500146 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 i_size_write(inode, end);
148}
149
Trond Myklebusta301b772007-02-06 11:07:15 -0800150/* A writeback failed: mark the page as bad, and invalidate the page cache */
151static void nfs_set_pageerror(struct page *page)
152{
153 SetPageError(page);
154 nfs_zap_mapping(page->mapping->host, page->mapping);
155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/* We can set the PG_uptodate flag if we see that a write request
158 * covers the full page.
159 */
160static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
161{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (PageUptodate(page))
163 return;
164 if (base != 0)
165 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500166 if (count != nfs_page_length(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500168 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Trond Myklebuste21195a2006-12-05 00:35:39 -0500171static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 unsigned int offset, unsigned int count)
173{
174 struct nfs_page *req;
Trond Myklebuste21195a2006-12-05 00:35:39 -0500175 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Trond Myklebuste21195a2006-12-05 00:35:39 -0500177 for (;;) {
178 req = nfs_update_request(ctx, page, offset, count);
179 if (!IS_ERR(req))
180 break;
181 ret = PTR_ERR(req);
182 if (ret != -EBUSY)
183 return ret;
184 ret = nfs_wb_page(page->mapping->host, page);
185 if (ret != 0)
186 return ret;
187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 /* Update file length */
189 nfs_grow_file(page, offset, count);
Trond Myklebustacee4782008-01-22 17:13:07 -0500190 nfs_clear_page_tag_locked(req);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100191 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194static int wb_priority(struct writeback_control *wbc)
195{
196 if (wbc->for_reclaim)
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400197 return FLUSH_HIGHPRI | FLUSH_STABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (wbc->for_kupdate)
199 return FLUSH_LOWPRI;
200 return 0;
201}
202
203/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800204 * NFS congestion control
205 */
206
207int nfs_congestion_kb;
208
209#define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
210#define NFS_CONGESTION_OFF_THRESH \
211 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
212
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400213static int nfs_set_page_writeback(struct page *page)
Peter Zijlstra89a09142007-03-16 13:38:26 -0800214{
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400215 int ret = test_set_page_writeback(page);
216
217 if (!ret) {
Peter Zijlstra89a09142007-03-16 13:38:26 -0800218 struct inode *inode = page->mapping->host;
219 struct nfs_server *nfss = NFS_SERVER(inode);
220
Peter Zijlstra277866a2007-05-08 00:35:12 -0700221 if (atomic_long_inc_return(&nfss->writeback) >
Peter Zijlstra89a09142007-03-16 13:38:26 -0800222 NFS_CONGESTION_ON_THRESH)
223 set_bdi_congested(&nfss->backing_dev_info, WRITE);
224 }
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400225 return ret;
Peter Zijlstra89a09142007-03-16 13:38:26 -0800226}
227
228static void nfs_end_page_writeback(struct page *page)
229{
230 struct inode *inode = page->mapping->host;
231 struct nfs_server *nfss = NFS_SERVER(inode);
232
233 end_page_writeback(page);
Peter Zijlstrac4dc4be2007-10-16 23:25:41 -0700234 if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
Peter Zijlstra89a09142007-03-16 13:38:26 -0800235 clear_bdi_congested(&nfss->backing_dev_info, WRITE);
Peter Zijlstra89a09142007-03-16 13:38:26 -0800236}
237
238/*
Trond Myklebuste261f512006-12-05 00:35:41 -0500239 * Find an associated nfs write request, and prepare to flush it out
Trond Myklebust9cccef92007-07-22 17:09:05 -0400240 * May return an error if the user signalled nfs_wait_on_request().
Trond Myklebuste261f512006-12-05 00:35:41 -0500241 */
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400242static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
243 struct page *page)
Trond Myklebuste261f512006-12-05 00:35:41 -0500244{
Trond Myklebust587142f2007-07-02 09:57:54 -0400245 struct inode *inode = page->mapping->host;
Trond Myklebuste261f512006-12-05 00:35:41 -0500246 struct nfs_page *req;
Trond Myklebuste261f512006-12-05 00:35:41 -0500247 int ret;
248
Trond Myklebust587142f2007-07-02 09:57:54 -0400249 spin_lock(&inode->i_lock);
Trond Myklebuste261f512006-12-05 00:35:41 -0500250 for(;;) {
251 req = nfs_page_find_request_locked(page);
252 if (req == NULL) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400253 spin_unlock(&inode->i_lock);
Trond Myklebust9cccef92007-07-22 17:09:05 -0400254 return 0;
Trond Myklebuste261f512006-12-05 00:35:41 -0500255 }
Trond Myklebustacee4782008-01-22 17:13:07 -0500256 if (nfs_set_page_tag_locked(req))
Trond Myklebuste261f512006-12-05 00:35:41 -0500257 break;
258 /* Note: If we hold the page lock, as is the case in nfs_writepage,
Trond Myklebustacee4782008-01-22 17:13:07 -0500259 * then the call to nfs_set_page_tag_locked() will always
Trond Myklebuste261f512006-12-05 00:35:41 -0500260 * succeed provided that someone hasn't already marked the
261 * request as dirty (in which case we don't care).
262 */
Trond Myklebust587142f2007-07-02 09:57:54 -0400263 spin_unlock(&inode->i_lock);
Trond Myklebuste261f512006-12-05 00:35:41 -0500264 ret = nfs_wait_on_request(req);
265 nfs_release_request(req);
266 if (ret != 0)
267 return ret;
Trond Myklebust587142f2007-07-02 09:57:54 -0400268 spin_lock(&inode->i_lock);
Trond Myklebuste261f512006-12-05 00:35:41 -0500269 }
Trond Myklebust612c9382007-04-20 16:12:45 -0400270 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
271 /* This request is marked for commit */
Trond Myklebust587142f2007-07-02 09:57:54 -0400272 spin_unlock(&inode->i_lock);
Trond Myklebustacee4782008-01-22 17:13:07 -0500273 nfs_clear_page_tag_locked(req);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400274 nfs_pageio_complete(pgio);
Trond Myklebust9cccef92007-07-22 17:09:05 -0400275 return 0;
Trond Myklebust612c9382007-04-20 16:12:45 -0400276 }
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400277 if (nfs_set_page_writeback(page) != 0) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400278 spin_unlock(&inode->i_lock);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400279 BUG();
280 }
Trond Myklebust587142f2007-07-02 09:57:54 -0400281 spin_unlock(&inode->i_lock);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400282 nfs_pageio_add_request(pgio, req);
Trond Myklebust9cccef92007-07-22 17:09:05 -0400283 return 0;
Trond Myklebuste261f512006-12-05 00:35:41 -0500284}
285
Trond Myklebustf758c8852007-07-22 19:27:32 -0400286static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
287{
288 struct inode *inode = page->mapping->host;
289
290 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
291 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
292
293 nfs_pageio_cond_complete(pgio, page->index);
294 return nfs_page_async_flush(pgio, page);
295}
296
Trond Myklebuste261f512006-12-05 00:35:41 -0500297/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 * Write an mmapped page to the server.
299 */
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500300static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Trond Myklebustf758c8852007-07-22 19:27:32 -0400302 struct nfs_pageio_descriptor pgio;
Trond Myklebuste261f512006-12-05 00:35:41 -0500303 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Trond Myklebustf758c8852007-07-22 19:27:32 -0400305 nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
306 err = nfs_do_writepage(page, wbc, &pgio);
307 nfs_pageio_complete(&pgio);
308 if (err < 0)
309 return err;
310 if (pgio.pg_error < 0)
311 return pgio.pg_error;
312 return 0;
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500313}
314
315int nfs_writepage(struct page *page, struct writeback_control *wbc)
316{
Trond Myklebustf758c8852007-07-22 19:27:32 -0400317 int ret;
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500318
Trond Myklebustf758c8852007-07-22 19:27:32 -0400319 ret = nfs_writepage_locked(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 unlock_page(page);
Trond Myklebustf758c8852007-07-22 19:27:32 -0400321 return ret;
322}
323
324static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
325{
326 int ret;
327
328 ret = nfs_do_writepage(page, wbc, data);
329 unlock_page(page);
330 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
334{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 struct inode *inode = mapping->host;
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400336 struct nfs_pageio_descriptor pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 int err;
338
Chuck Lever91d5b472006-03-20 13:44:14 -0500339 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
340
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400341 nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
Trond Myklebustf758c8852007-07-22 19:27:32 -0400342 err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400343 nfs_pageio_complete(&pgio);
Trond Myklebustf758c8852007-07-22 19:27:32 -0400344 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return err;
Trond Myklebustf758c8852007-07-22 19:27:32 -0400346 if (pgio.pg_error < 0)
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400347 return pgio.pg_error;
348 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
351/*
352 * Insert a write request into an inode
353 */
354static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
355{
356 struct nfs_inode *nfsi = NFS_I(inode);
357 int error;
358
359 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
360 BUG_ON(error == -EEXIST);
361 if (error)
362 return error;
363 if (!nfsi->npages) {
364 igrab(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (nfs_have_delegation(inode, FMODE_WRITE))
366 nfsi->change_attr++;
367 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500368 SetPagePrivate(req->wb_page);
Trond Myklebust277459d2006-12-05 00:35:35 -0500369 set_page_private(req->wb_page, (unsigned long)req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 nfsi->npages++;
Trond Myklebustc03b4022007-06-17 13:26:38 -0400371 kref_get(&req->wb_kref);
Trond Myklebustacee4782008-01-22 17:13:07 -0500372 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_LOCKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return 0;
374}
375
376/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800377 * Remove a write request from an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 */
379static void nfs_inode_remove_request(struct nfs_page *req)
380{
Trond Myklebust88be9f92007-06-05 10:42:27 -0400381 struct inode *inode = req->wb_context->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 struct nfs_inode *nfsi = NFS_I(inode);
383
384 BUG_ON (!NFS_WBACK_BUSY(req));
385
Trond Myklebust587142f2007-07-02 09:57:54 -0400386 spin_lock(&inode->i_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500387 set_page_private(req->wb_page, 0);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500388 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
390 nfsi->npages--;
391 if (!nfsi->npages) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400392 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 iput(inode);
394 } else
Trond Myklebust587142f2007-07-02 09:57:54 -0400395 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 nfs_clear_request(req);
397 nfs_release_request(req);
398}
399
Trond Myklebust61822ab2006-12-05 00:35:42 -0500400static void
401nfs_redirty_request(struct nfs_page *req)
402{
Trond Myklebust61822ab2006-12-05 00:35:42 -0500403 __set_page_dirty_nobuffers(req->wb_page);
404}
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/*
407 * Check if a request is dirty
408 */
409static inline int
410nfs_dirty_request(struct nfs_page *req)
411{
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400412 struct page *page = req->wb_page;
413
Trond Myklebust612c9382007-04-20 16:12:45 -0400414 if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags))
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400415 return 0;
416 return !PageWriteback(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
419#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
420/*
421 * Add a request to the inode's commit list.
422 */
423static void
424nfs_mark_request_commit(struct nfs_page *req)
425{
Trond Myklebust88be9f92007-06-05 10:42:27 -0400426 struct inode *inode = req->wb_context->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 struct nfs_inode *nfsi = NFS_I(inode);
428
Trond Myklebust587142f2007-07-02 09:57:54 -0400429 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 nfsi->ncommit++;
Trond Myklebust612c9382007-04-20 16:12:45 -0400431 set_bit(PG_NEED_COMMIT, &(req)->wb_flags);
Trond Myklebust5c369682007-06-17 15:27:42 -0400432 radix_tree_tag_set(&nfsi->nfs_page_tree,
433 req->wb_index,
434 NFS_PAGE_TAG_COMMIT);
Trond Myklebust587142f2007-07-02 09:57:54 -0400435 spin_unlock(&inode->i_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700436 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -0700437 inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
Trond Myklebusta1803042006-12-05 00:45:19 -0500438 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400440
441static inline
442int nfs_write_need_commit(struct nfs_write_data *data)
443{
444 return data->verf.committed != NFS_FILE_SYNC;
445}
446
447static inline
448int nfs_reschedule_unstable_write(struct nfs_page *req)
449{
Trond Myklebust612c9382007-04-20 16:12:45 -0400450 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400451 nfs_mark_request_commit(req);
452 return 1;
453 }
454 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
455 nfs_redirty_request(req);
456 return 1;
457 }
458 return 0;
459}
460#else
461static inline void
462nfs_mark_request_commit(struct nfs_page *req)
463{
464}
465
466static inline
467int nfs_write_need_commit(struct nfs_write_data *data)
468{
469 return 0;
470}
471
472static inline
473int nfs_reschedule_unstable_write(struct nfs_page *req)
474{
475 return 0;
476}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477#endif
478
479/*
480 * Wait for a request to complete.
481 *
Matthew Wilcox150030b2007-12-06 16:24:39 -0500482 * Interruptible by fatal signals only.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 */
Trond Myklebustca52fec2007-04-17 17:22:13 -0400484static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 struct nfs_inode *nfsi = NFS_I(inode);
487 struct nfs_page *req;
Trond Myklebustca52fec2007-04-17 17:22:13 -0400488 pgoff_t idx_end, next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 unsigned int res = 0;
490 int error;
491
492 if (npages == 0)
493 idx_end = ~0;
494 else
495 idx_end = idx_start + npages - 1;
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 next = idx_start;
Trond Myklebust9fd367f2007-06-17 15:10:24 -0400498 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 -0700499 if (req->wb_index > idx_end)
500 break;
501
502 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000503 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Trond Myklebustc03b4022007-06-17 13:26:38 -0400505 kref_get(&req->wb_kref);
Trond Myklebust587142f2007-07-02 09:57:54 -0400506 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 error = nfs_wait_on_request(req);
508 nfs_release_request(req);
Trond Myklebust587142f2007-07-02 09:57:54 -0400509 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (error < 0)
511 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 res++;
513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return res;
515}
516
Trond Myklebust83715ad2006-07-05 13:17:12 -0400517static void nfs_cancel_commit_list(struct list_head *head)
518{
519 struct nfs_page *req;
520
521 while(!list_empty(head)) {
522 req = nfs_list_entry(head->next);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700523 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -0700524 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
525 BDI_RECLAIMABLE);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400526 nfs_list_remove_request(req);
Trond Myklebust612c9382007-04-20 16:12:45 -0400527 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400528 nfs_inode_remove_request(req);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700529 nfs_unlock_request(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400530 }
531}
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
534/*
535 * nfs_scan_commit - Scan an inode for commit requests
536 * @inode: NFS inode to scan
537 * @dst: destination list
538 * @idx_start: lower bound of page->index to scan.
539 * @npages: idx_start + npages sets the upper bound to scan.
540 *
541 * Moves requests from the inode's 'commit' request list.
542 * The requests are *not* checked to ensure that they form a contiguous set.
543 */
544static int
Trond Myklebustca52fec2007-04-17 17:22:13 -0400545nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000548 int res = 0;
549
550 if (nfsi->ncommit != 0) {
Trond Myklebust5c369682007-06-17 15:27:42 -0400551 res = nfs_scan_list(nfsi, dst, idx_start, npages,
552 NFS_PAGE_TAG_COMMIT);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000553 nfsi->ncommit -= res;
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return res;
556}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500557#else
Trond Myklebustca52fec2007-04-17 17:22:13 -0400558static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500559{
560 return 0;
561}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562#endif
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564/*
565 * Try to update any existing write request, or create one if there is none.
566 * In order to match, the request's credentials must match those of
567 * the calling process.
568 *
569 * Note: Should always be called with the Page Lock held!
570 */
571static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
Trond Myklebuste21195a2006-12-05 00:35:39 -0500572 struct page *page, unsigned int offset, unsigned int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Peter Zijlstra89a09142007-03-16 13:38:26 -0800574 struct address_space *mapping = page->mapping;
575 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 struct nfs_page *req, *new = NULL;
Trond Myklebustca52fec2007-04-17 17:22:13 -0400577 pgoff_t rqend, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 end = offset + bytes;
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 for (;;) {
582 /* Loop over all inode entries and see if we find
583 * A request for the page we wish to update
584 */
Trond Myklebust587142f2007-07-02 09:57:54 -0400585 spin_lock(&inode->i_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500586 req = nfs_page_find_request_locked(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (req) {
Trond Myklebustacee4782008-01-22 17:13:07 -0500588 if (!nfs_set_page_tag_locked(req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 int error;
Trond Myklebust277459d2006-12-05 00:35:35 -0500590
Trond Myklebust587142f2007-07-02 09:57:54 -0400591 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 error = nfs_wait_on_request(req);
593 nfs_release_request(req);
Neil Brown1dd594b2006-03-20 13:44:04 -0500594 if (error < 0) {
595 if (new)
596 nfs_release_request(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return ERR_PTR(error);
Neil Brown1dd594b2006-03-20 13:44:04 -0500598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 continue;
600 }
Trond Myklebust587142f2007-07-02 09:57:54 -0400601 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (new)
603 nfs_release_request(new);
604 break;
605 }
606
607 if (new) {
608 int error;
609 nfs_lock_request_dontget(new);
610 error = nfs_inode_add_request(inode, new);
611 if (error) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400612 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 nfs_unlock_request(new);
614 return ERR_PTR(error);
615 }
Trond Myklebust587142f2007-07-02 09:57:54 -0400616 spin_unlock(&inode->i_lock);
Trond Myklebust61e930a2007-10-18 17:08:05 -0400617 req = new;
618 goto zero_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
Trond Myklebust587142f2007-07-02 09:57:54 -0400620 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 new = nfs_create_request(ctx, inode, page, offset, bytes);
623 if (IS_ERR(new))
624 return new;
625 }
626
627 /* We have a request for our page.
628 * If the creds don't match, or the
629 * page addresses don't match,
630 * tell the caller to wait on the conflicting
631 * request.
632 */
633 rqend = req->wb_offset + req->wb_bytes;
634 if (req->wb_context != ctx
635 || req->wb_page != page
636 || !nfs_dirty_request(req)
637 || offset > rqend || end < req->wb_offset) {
Trond Myklebustacee4782008-01-22 17:13:07 -0500638 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return ERR_PTR(-EBUSY);
640 }
641
642 /* Okay, the request matches. Update the region */
643 if (offset < req->wb_offset) {
644 req->wb_offset = offset;
645 req->wb_pgbase = offset;
Trond Myklebust61e930a2007-10-18 17:08:05 -0400646 req->wb_bytes = max(end, rqend) - req->wb_offset;
647 goto zero_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649
650 if (end > rqend)
651 req->wb_bytes = end - req->wb_offset;
652
653 return req;
Trond Myklebust61e930a2007-10-18 17:08:05 -0400654zero_page:
655 /* If this page might potentially be marked as up to date,
656 * then we need to zero any uninitalised data. */
657 if (req->wb_pgbase == 0 && req->wb_bytes != PAGE_CACHE_SIZE
658 && !PageUptodate(req->wb_page))
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800659 zero_user_segment(req->wb_page, req->wb_bytes, PAGE_CACHE_SIZE);
Trond Myklebust61e930a2007-10-18 17:08:05 -0400660 return req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
663int nfs_flush_incompatible(struct file *file, struct page *page)
664{
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400665 struct nfs_open_context *ctx = nfs_file_open_context(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 struct nfs_page *req;
Trond Myklebust1a545332006-12-05 00:35:40 -0500667 int do_flush, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 /*
669 * Look for a request corresponding to this page. If there
670 * is one, and it belongs to another file, we flush it out
671 * before we try to copy anything into the page. Do this
672 * due to the lack of an ACCESS-type call in NFSv2.
673 * Also do the same if we find a request from an existing
674 * dropped page.
675 */
Trond Myklebust1a545332006-12-05 00:35:40 -0500676 do {
677 req = nfs_page_find_request(page);
678 if (req == NULL)
679 return 0;
680 do_flush = req->wb_page != page || req->wb_context != ctx
Trond Myklebuste261f512006-12-05 00:35:41 -0500681 || !nfs_dirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 nfs_release_request(req);
Trond Myklebust1a545332006-12-05 00:35:40 -0500683 if (!do_flush)
684 return 0;
685 status = nfs_wb_page(page->mapping->host, page);
686 } while (status == 0);
687 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
690/*
Trond Myklebust5d47a352008-02-07 17:24:07 -0500691 * If the page cache is marked as unsafe or invalid, then we can't rely on
692 * the PageUptodate() flag. In this case, we will need to turn off
693 * write optimisations that depend on the page contents being correct.
694 */
695static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
696{
697 return PageUptodate(page) &&
698 !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
699}
700
701/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 * Update and possibly write a cached page of an NFS file.
703 *
704 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
705 * things with a page scheduled for an RPC call (e.g. invalidate it).
706 */
707int nfs_updatepage(struct file *file, struct page *page,
708 unsigned int offset, unsigned int count)
709{
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400710 struct nfs_open_context *ctx = nfs_file_open_context(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 int status = 0;
713
Chuck Lever91d5b472006-03-20 13:44:14 -0500714 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800717 file->f_path.dentry->d_parent->d_name.name,
718 file->f_path.dentry->d_name.name, count,
Chuck Lever0bbacc42005-11-01 16:53:32 -0500719 (long long)(page_offset(page) +offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 /* If we're not using byte range locks, and we know the page
Trond Myklebust5d47a352008-02-07 17:24:07 -0500722 * is up to date, it may be more efficient to extend the write
723 * to cover the entire page in order to avoid fragmentation
724 * inefficiencies.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 */
Trond Myklebust5d47a352008-02-07 17:24:07 -0500726 if (nfs_write_pageuptodate(page, inode) &&
727 inode->i_flock == NULL &&
Trond Myklebust4b5621f2008-02-25 15:56:29 -0800728 !(file->f_flags & O_SYNC)) {
Trond Myklebust49a70f22006-12-05 00:35:38 -0500729 count = max(count + offset, nfs_page_length(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
732
Trond Myklebuste21195a2006-12-05 00:35:39 -0500733 status = nfs_writepage_setup(ctx, page, offset, count);
Trond Myklebuste261f512006-12-05 00:35:41 -0500734 __set_page_dirty_nobuffers(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
737 status, (long long)i_size_read(inode));
738 if (status < 0)
Trond Myklebusta301b772007-02-06 11:07:15 -0800739 nfs_set_pageerror(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return status;
741}
742
743static void nfs_writepage_release(struct nfs_page *req)
744{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Trond Myklebust44dd1512007-05-19 11:58:03 -0400746 if (PageError(req->wb_page)) {
747 nfs_end_page_writeback(req->wb_page);
748 nfs_inode_remove_request(req);
749 } else if (!nfs_reschedule_unstable_write(req)) {
750 /* Set the PG_uptodate flag */
751 nfs_mark_uptodate(req->wb_page, req->wb_pgbase, req->wb_bytes);
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400752 nfs_end_page_writeback(req->wb_page);
753 nfs_inode_remove_request(req);
754 } else
755 nfs_end_page_writeback(req->wb_page);
Trond Myklebust9fd367f2007-06-17 15:10:24 -0400756 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
Trond Myklebust3ff75762007-07-14 15:40:00 -0400759static int flush_task_priority(int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
761 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
762 case FLUSH_HIGHPRI:
763 return RPC_PRIORITY_HIGH;
764 case FLUSH_LOWPRI:
765 return RPC_PRIORITY_LOW;
766 }
767 return RPC_PRIORITY_NORMAL;
768}
769
770/*
771 * Set up the argument/result storage required for the RPC call.
772 */
773static void nfs_write_rpcsetup(struct nfs_page *req,
774 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500775 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 unsigned int count, unsigned int offset,
777 int how)
778{
Trond Myklebust84115e12007-07-14 15:39:59 -0400779 struct inode *inode = req->wb_context->path.dentry->d_inode;
780 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
Trond Myklebust3ff75762007-07-14 15:40:00 -0400781 int priority = flush_task_priority(how);
Trond Myklebust07737692007-10-25 18:42:54 -0400782 struct rpc_task *task;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400783 struct rpc_message msg = {
784 .rpc_argp = &data->args,
785 .rpc_resp = &data->res,
786 .rpc_cred = req->wb_context->cred,
787 };
Trond Myklebust84115e12007-07-14 15:39:59 -0400788 struct rpc_task_setup task_setup_data = {
789 .rpc_client = NFS_CLIENT(inode),
Trond Myklebust07737692007-10-25 18:42:54 -0400790 .task = &data->task,
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400791 .rpc_message = &msg,
Trond Myklebust84115e12007-07-14 15:39:59 -0400792 .callback_ops = call_ops,
793 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -0500794 .workqueue = nfsiod_workqueue,
Trond Myklebust84115e12007-07-14 15:39:59 -0400795 .flags = flags,
Trond Myklebust3ff75762007-07-14 15:40:00 -0400796 .priority = priority,
Trond Myklebust84115e12007-07-14 15:39:59 -0400797 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 /* Set up the RPC argument and reply structs
800 * NB: take care not to mess about with data->commit et al. */
801
802 data->req = req;
Trond Myklebust88be9f92007-06-05 10:42:27 -0400803 data->inode = inode = req->wb_context->path.dentry->d_inode;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400804 data->cred = msg.rpc_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 data->args.fh = NFS_FH(inode);
807 data->args.offset = req_offset(req) + offset;
808 data->args.pgbase = req->wb_pgbase + offset;
809 data->args.pages = data->pagevec;
810 data->args.count = count;
Trond Myklebust383ba712008-02-19 20:04:20 -0500811 data->args.context = get_nfs_open_context(req->wb_context);
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400812 data->args.stable = NFS_UNSTABLE;
813 if (how & FLUSH_STABLE) {
814 data->args.stable = NFS_DATA_SYNC;
815 if (!NFS_I(inode)->ncommit)
816 data->args.stable = NFS_FILE_SYNC;
817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 data->res.fattr = &data->fattr;
820 data->res.count = count;
821 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400822 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Trond Myklebust788e7a82006-03-20 13:44:27 -0500824 /* Set up the initial task struct. */
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400825 NFS_PROTO(inode)->write_setup(data, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Chuck Levera3f565b2007-01-31 12:14:01 -0500827 dprintk("NFS: %5u initiated write call "
828 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500829 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 inode->i_sb->s_id,
831 (long long)NFS_FILEID(inode),
832 count,
833 (unsigned long long)data->args.offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Trond Myklebust07737692007-10-25 18:42:54 -0400835 task = rpc_run_task(&task_setup_data);
836 if (!IS_ERR(task))
837 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
840/*
841 * Generate multiple small requests to write out a single
842 * contiguous dirty area on one page.
843 */
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400844static 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 -0700845{
846 struct nfs_page *req = nfs_list_entry(head->next);
847 struct page *page = req->wb_page;
848 struct nfs_write_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700849 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
850 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 int requests = 0;
852 LIST_HEAD(list);
853
854 nfs_list_remove_request(req);
855
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400856 nbytes = count;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700857 do {
858 size_t len = min(nbytes, wsize);
859
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400860 data = nfs_writedata_alloc(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (!data)
862 goto out_bad;
863 list_add(&data->pages, &list);
864 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700865 nbytes -= len;
866 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 atomic_set(&req->wb_complete, requests);
868
869 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 offset = 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400871 nbytes = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 do {
873 data = list_entry(list.next, struct nfs_write_data, pages);
874 list_del_init(&data->pages);
875
876 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400878 if (nbytes < wsize)
879 wsize = nbytes;
880 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
881 wsize, offset, how);
882 offset += wsize;
883 nbytes -= wsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 } while (nbytes != 0);
885
886 return 0;
887
888out_bad:
889 while (!list_empty(&list)) {
890 data = list_entry(list.next, struct nfs_write_data, pages);
891 list_del(&data->pages);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500892 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
Trond Myklebust61822ab2006-12-05 00:35:42 -0500894 nfs_redirty_request(req);
Trond Myklebust6d677e32007-04-20 16:12:40 -0400895 nfs_end_page_writeback(req->wb_page);
Trond Myklebust9fd367f2007-06-17 15:10:24 -0400896 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return -ENOMEM;
898}
899
900/*
901 * Create an RPC task for the given write request and kick it.
902 * The page must have been locked by the caller.
903 *
904 * It may happen that the page we're passed is not marked dirty.
905 * This is the case if nfs_updatepage detects a conflicting request
906 * that has been written but not committed.
907 */
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400908static 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 -0700909{
910 struct nfs_page *req;
911 struct page **pages;
912 struct nfs_write_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400914 data = nfs_writedata_alloc(npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (!data)
916 goto out_bad;
917
918 pages = data->pagevec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 while (!list_empty(head)) {
920 req = nfs_list_entry(head->next);
921 nfs_list_remove_request(req);
922 nfs_list_add_request(req, &data->pages);
923 ClearPageError(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 *pages++ = req->wb_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926 req = nfs_list_entry(data->pages.next);
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 /* Set up the argument struct */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500929 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return 0;
932 out_bad:
933 while (!list_empty(head)) {
Trond Myklebust10afec92007-05-14 17:16:04 -0400934 req = nfs_list_entry(head->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 nfs_list_remove_request(req);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500936 nfs_redirty_request(req);
Trond Myklebust6d677e32007-04-20 16:12:40 -0400937 nfs_end_page_writeback(req->wb_page);
Trond Myklebust9fd367f2007-06-17 15:10:24 -0400938 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940 return -ENOMEM;
941}
942
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400943static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
944 struct inode *inode, int ioflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Chuck Leverbf4285e2007-12-20 14:54:57 -0500946 size_t wsize = NFS_SERVER(inode)->wsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400948 if (wsize < PAGE_CACHE_SIZE)
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400949 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400950 else
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400951 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
954/*
955 * Handle a write reply that flushed part of a page.
956 */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500957static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Trond Myklebust788e7a82006-03-20 13:44:27 -0500959 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 struct nfs_page *req = data->req;
961 struct page *page = req->wb_page;
962
963 dprintk("NFS: write (%s/%Ld %d@%Ld)",
Trond Myklebust88be9f92007-06-05 10:42:27 -0400964 req->wb_context->path.dentry->d_inode->i_sb->s_id,
965 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 req->wb_bytes,
967 (long long)req_offset(req));
968
Trond Myklebust788e7a82006-03-20 13:44:27 -0500969 if (nfs_writeback_done(task, data) != 0)
970 return;
971
972 if (task->tk_status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -0800973 nfs_set_pageerror(page);
Trond Myklebust7b159fc2007-07-25 14:09:54 -0400974 nfs_context_set_write_error(req->wb_context, task->tk_status);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500975 dprintk(", error = %d\n", task->tk_status);
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400976 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
978
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400979 if (nfs_write_need_commit(data)) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400980 struct inode *inode = page->mapping->host;
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400981
Trond Myklebust587142f2007-07-02 09:57:54 -0400982 spin_lock(&inode->i_lock);
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400983 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
984 /* Do nothing we need to resend the writes */
985 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
986 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
987 dprintk(" defer commit\n");
988 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
989 set_bit(PG_NEED_RESCHED, &req->wb_flags);
990 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
991 dprintk(" server reboot detected\n");
992 }
Trond Myklebust587142f2007-07-02 09:57:54 -0400993 spin_unlock(&inode->i_lock);
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400994 } else
995 dprintk(" OK\n");
996
997out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (atomic_dec_and_test(&req->wb_complete))
999 nfs_writepage_release(req);
1000}
1001
Trond Myklebust788e7a82006-03-20 13:44:27 -05001002static const struct rpc_call_ops nfs_write_partial_ops = {
1003 .rpc_call_done = nfs_writeback_done_partial,
1004 .rpc_release = nfs_writedata_release,
1005};
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007/*
1008 * Handle a write reply that flushes a whole page.
1009 *
1010 * FIXME: There is an inherent race with invalidate_inode_pages and
1011 * writebacks since the page->count is kept > 1 for as long
1012 * as the page has a write request pending.
1013 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001014static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001016 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 struct nfs_page *req;
1018 struct page *page;
1019
Trond Myklebust788e7a82006-03-20 13:44:27 -05001020 if (nfs_writeback_done(task, data) != 0)
1021 return;
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 /* Update attributes as result of writeback. */
1024 while (!list_empty(&data->pages)) {
1025 req = nfs_list_entry(data->pages.next);
1026 nfs_list_remove_request(req);
1027 page = req->wb_page;
1028
1029 dprintk("NFS: write (%s/%Ld %d@%Ld)",
Trond Myklebust88be9f92007-06-05 10:42:27 -04001030 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1031 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 req->wb_bytes,
1033 (long long)req_offset(req));
1034
Trond Myklebust788e7a82006-03-20 13:44:27 -05001035 if (task->tk_status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -08001036 nfs_set_pageerror(page);
Trond Myklebust7b159fc2007-07-25 14:09:54 -04001037 nfs_context_set_write_error(req->wb_context, task->tk_status);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001038 dprintk(", error = %d\n", task->tk_status);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001039 goto remove_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001042 if (nfs_write_need_commit(data)) {
1043 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1044 nfs_mark_request_commit(req);
1045 nfs_end_page_writeback(page);
1046 dprintk(" marked for commit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 goto next;
1048 }
Trond Myklebust44dd1512007-05-19 11:58:03 -04001049 /* Set the PG_uptodate flag? */
1050 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001051 dprintk(" OK\n");
1052remove_request:
1053 nfs_end_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 nfs_inode_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 next:
Trond Myklebust9fd367f2007-06-17 15:10:24 -04001056 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
1058}
1059
Trond Myklebust788e7a82006-03-20 13:44:27 -05001060static const struct rpc_call_ops nfs_write_full_ops = {
1061 .rpc_call_done = nfs_writeback_done_full,
1062 .rpc_release = nfs_writedata_release,
1063};
1064
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066/*
1067 * This function is called when the WRITE call is complete.
1068 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001069int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 struct nfs_writeargs *argp = &data->args;
1072 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001073 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Chuck Levera3f565b2007-01-31 12:14:01 -05001075 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 task->tk_pid, task->tk_status);
1077
Chuck Leverf551e442006-09-20 14:33:04 -04001078 /*
1079 * ->write_done will attempt to use post-op attributes to detect
1080 * conflicting writes by other clients. A strict interpretation
1081 * of close-to-open would allow us to continue caching even if
1082 * another writer had changed the file, but some applications
1083 * depend on tighter cache coherency when writing.
1084 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001085 status = NFS_PROTO(data->inode)->write_done(task, data);
1086 if (status != 0)
1087 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001088 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1091 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1092 /* We tried a write call, but the server did not
1093 * commit data to stable storage even though we
1094 * requested it.
1095 * Note: There is a known bug in Tru64 < 5.0 in which
1096 * the server reports NFS_DATA_SYNC, but performs
1097 * NFS_FILE_SYNC. We therefore implement this checking
1098 * as a dprintk() in order to avoid filling syslog.
1099 */
1100 static unsigned long complain;
1101
1102 if (time_before(complain, jiffies)) {
1103 dprintk("NFS: faulty NFS server %s:"
1104 " (committed = %d) != (stable = %d)\n",
David Howells54ceac42006-08-22 20:06:13 -04001105 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 resp->verf->committed, argp->stable);
1107 complain = jiffies + 300 * HZ;
1108 }
1109 }
1110#endif
1111 /* Is this a short write? */
1112 if (task->tk_status >= 0 && resp->count < argp->count) {
1113 static unsigned long complain;
1114
Chuck Lever91d5b472006-03-20 13:44:14 -05001115 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 /* Has the server at least made some progress? */
1118 if (resp->count != 0) {
1119 /* Was this an NFSv2 write or an NFSv3 stable write? */
1120 if (resp->verf->committed != NFS_UNSTABLE) {
1121 /* Resend from where the server left off */
1122 argp->offset += resp->count;
1123 argp->pgbase += resp->count;
1124 argp->count -= resp->count;
1125 } else {
1126 /* Resend as a stable write in order to avoid
1127 * headaches in the case of a server crash.
1128 */
1129 argp->stable = NFS_FILE_SYNC;
1130 }
1131 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001132 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
1134 if (time_before(complain, jiffies)) {
1135 printk(KERN_WARNING
1136 "NFS: Server wrote zero bytes, expected %u.\n",
1137 argp->count);
1138 complain = jiffies + 300 * HZ;
1139 }
1140 /* Can't do anything about it except throw an error. */
1141 task->tk_status = -EIO;
1142 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001143 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
1146
1147#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust383ba712008-02-19 20:04:20 -05001148void nfs_commit_release(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Trond Myklebust383ba712008-02-19 20:04:20 -05001150 struct nfs_write_data *wdata = data;
1151
1152 put_nfs_open_context(wdata->args.context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 nfs_commit_free(wdata);
1154}
1155
1156/*
1157 * Set up the argument/result storage required for the RPC call.
1158 */
1159static void nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001160 struct nfs_write_data *data,
1161 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Trond Myklebust84115e12007-07-14 15:39:59 -04001163 struct nfs_page *first = nfs_list_entry(head->next);
1164 struct inode *inode = first->wb_context->path.dentry->d_inode;
1165 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
Trond Myklebust3ff75762007-07-14 15:40:00 -04001166 int priority = flush_task_priority(how);
Trond Myklebust07737692007-10-25 18:42:54 -04001167 struct rpc_task *task;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001168 struct rpc_message msg = {
1169 .rpc_argp = &data->args,
1170 .rpc_resp = &data->res,
1171 .rpc_cred = first->wb_context->cred,
1172 };
Trond Myklebust84115e12007-07-14 15:39:59 -04001173 struct rpc_task_setup task_setup_data = {
Trond Myklebust07737692007-10-25 18:42:54 -04001174 .task = &data->task,
Trond Myklebust84115e12007-07-14 15:39:59 -04001175 .rpc_client = NFS_CLIENT(inode),
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001176 .rpc_message = &msg,
Trond Myklebust84115e12007-07-14 15:39:59 -04001177 .callback_ops = &nfs_commit_ops,
1178 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001179 .workqueue = nfsiod_workqueue,
Trond Myklebust84115e12007-07-14 15:39:59 -04001180 .flags = flags,
Trond Myklebust3ff75762007-07-14 15:40:00 -04001181 .priority = priority,
Trond Myklebust84115e12007-07-14 15:39:59 -04001182 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 /* Set up the RPC argument and reply structs
1185 * NB: take care not to mess about with data->commit et al. */
1186
1187 list_splice_init(head, &data->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 data->inode = inode;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001190 data->cred = msg.rpc_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001193 /* Note: we always request a commit of the entire inode */
1194 data->args.offset = 0;
1195 data->args.count = 0;
Trond Myklebust383ba712008-02-19 20:04:20 -05001196 data->args.context = get_nfs_open_context(first->wb_context);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001197 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 data->res.fattr = &data->fattr;
1199 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001200 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001201
1202 /* Set up the initial task struct. */
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001203 NFS_PROTO(inode)->commit_setup(data, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Chuck Levera3f565b2007-01-31 12:14:01 -05001205 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001206
Trond Myklebust07737692007-10-25 18:42:54 -04001207 task = rpc_run_task(&task_setup_data);
1208 if (!IS_ERR(task))
1209 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
1212/*
1213 * Commit dirty pages
1214 */
1215static int
Chuck Lever40859d72005-11-30 18:09:02 -05001216nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
1218 struct nfs_write_data *data;
1219 struct nfs_page *req;
1220
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001221 data = nfs_commit_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 if (!data)
1224 goto out_bad;
1225
1226 /* Set up the argument struct */
1227 nfs_commit_rpcsetup(head, data, how);
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return 0;
1230 out_bad:
1231 while (!list_empty(head)) {
1232 req = nfs_list_entry(head->next);
1233 nfs_list_remove_request(req);
1234 nfs_mark_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001235 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -07001236 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1237 BDI_RECLAIMABLE);
Trond Myklebust9fd367f2007-06-17 15:10:24 -04001238 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240 return -ENOMEM;
1241}
1242
1243/*
1244 * COMMIT call returned
1245 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001246static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001248 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Chuck Levera3f565b2007-01-31 12:14:01 -05001251 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 task->tk_pid, task->tk_status);
1253
Trond Myklebust788e7a82006-03-20 13:44:27 -05001254 /* Call the NFS version-specific code */
1255 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1256 return;
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 while (!list_empty(&data->pages)) {
1259 req = nfs_list_entry(data->pages.next);
1260 nfs_list_remove_request(req);
Trond Myklebust612c9382007-04-20 16:12:45 -04001261 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
Christoph Lameterfd39fc82006-06-30 01:55:40 -07001262 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -07001263 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1264 BDI_RECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
Trond Myklebust88be9f92007-06-05 10:42:27 -04001267 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1268 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 req->wb_bytes,
1270 (long long)req_offset(req));
1271 if (task->tk_status < 0) {
Trond Myklebust7b159fc2007-07-25 14:09:54 -04001272 nfs_context_set_write_error(req->wb_context, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 nfs_inode_remove_request(req);
1274 dprintk(", error = %d\n", task->tk_status);
1275 goto next;
1276 }
1277
1278 /* Okay, COMMIT succeeded, apparently. Check the verifier
1279 * returned by the server against all stored verfs. */
1280 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1281 /* We have a match */
Trond Myklebust44dd1512007-05-19 11:58:03 -04001282 /* Set the PG_uptodate flag */
1283 nfs_mark_uptodate(req->wb_page, req->wb_pgbase,
1284 req->wb_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 nfs_inode_remove_request(req);
1286 dprintk(" OK\n");
1287 goto next;
1288 }
1289 /* We have a mismatch. Write the page again */
1290 dprintk(" mismatch\n");
Trond Myklebust61822ab2006-12-05 00:35:42 -05001291 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 next:
Trond Myklebust9fd367f2007-06-17 15:10:24 -04001293 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001296
1297static const struct rpc_call_ops nfs_commit_ops = {
1298 .rpc_call_done = nfs_commit_done,
1299 .rpc_release = nfs_commit_release,
1300};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001302int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001305 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Trond Myklebust587142f2007-07-02 09:57:54 -04001307 spin_lock(&inode->i_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001308 res = nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebust587142f2007-07-02 09:57:54 -04001309 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001311 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001312 if (error < 0)
1313 return error;
1314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return res;
1316}
Trond Myklebustc63c7b02007-04-02 19:29:52 -04001317#else
1318static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1319{
1320 return 0;
1321}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322#endif
1323
Trond Myklebust1c759502006-10-09 16:18:38 -04001324long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Trond Myklebust1c759502006-10-09 16:18:38 -04001326 struct inode *inode = mapping->host;
Trond Myklebustca52fec2007-04-17 17:22:13 -04001327 pgoff_t idx_start, idx_end;
Trond Myklebust1c759502006-10-09 16:18:38 -04001328 unsigned int npages = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001329 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001330 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebust3f442542006-09-17 14:46:44 -04001331 long pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Trond Myklebust1c759502006-10-09 16:18:38 -04001333 /* FIXME */
1334 if (wbc->range_cyclic)
1335 idx_start = 0;
1336 else {
1337 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1338 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1339 if (idx_end > idx_start) {
Trond Myklebustca52fec2007-04-17 17:22:13 -04001340 pgoff_t l_npages = 1 + idx_end - idx_start;
Trond Myklebust1c759502006-10-09 16:18:38 -04001341 npages = l_npages;
1342 if (sizeof(npages) != sizeof(l_npages) &&
Trond Myklebustca52fec2007-04-17 17:22:13 -04001343 (pgoff_t)npages != l_npages)
Trond Myklebust1c759502006-10-09 16:18:38 -04001344 npages = 0;
1345 }
1346 }
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001347 how &= ~FLUSH_NOCOMMIT;
Trond Myklebust587142f2007-07-02 09:57:54 -04001348 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 do {
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001350 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1351 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001352 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001353 if (nocommit)
1354 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001355 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebust724c4392007-04-17 17:22:13 -04001356 if (pages == 0)
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001357 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001358 if (how & FLUSH_INVALIDATE) {
Trond Myklebust587142f2007-07-02 09:57:54 -04001359 spin_unlock(&inode->i_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001360 nfs_cancel_commit_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001361 ret = pages;
Trond Myklebust587142f2007-07-02 09:57:54 -04001362 spin_lock(&inode->i_lock);
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001363 continue;
1364 }
1365 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebust587142f2007-07-02 09:57:54 -04001366 spin_unlock(&inode->i_lock);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001367 ret = nfs_commit_list(inode, &head, how);
Trond Myklebust587142f2007-07-02 09:57:54 -04001368 spin_lock(&inode->i_lock);
1369
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001370 } while (ret >= 0);
Trond Myklebust587142f2007-07-02 09:57:54 -04001371 spin_unlock(&inode->i_lock);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001372 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
Trond Myklebust34901f72007-07-25 14:09:54 -04001375static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
Trond Myklebust1c759502006-10-09 16:18:38 -04001376{
Trond Myklebust1c759502006-10-09 16:18:38 -04001377 int ret;
1378
Trond Myklebust34901f72007-07-25 14:09:54 -04001379 ret = nfs_writepages(mapping, wbc);
Trond Myklebust61822ab2006-12-05 00:35:42 -05001380 if (ret < 0)
1381 goto out;
Trond Myklebust34901f72007-07-25 14:09:54 -04001382 ret = nfs_sync_mapping_wait(mapping, wbc, how);
Trond Myklebusted90ef52007-07-20 13:13:28 -04001383 if (ret < 0)
1384 goto out;
1385 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001386out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001387 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Trond Myklebust1c759502006-10-09 16:18:38 -04001388 return ret;
1389}
1390
Trond Myklebust34901f72007-07-25 14:09:54 -04001391/* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
1392static int nfs_write_mapping(struct address_space *mapping, int how)
1393{
1394 struct writeback_control wbc = {
1395 .bdi = mapping->backing_dev_info,
1396 .sync_mode = WB_SYNC_NONE,
1397 .nr_to_write = LONG_MAX,
1398 .for_writepages = 1,
1399 .range_cyclic = 1,
1400 };
1401 int ret;
1402
1403 ret = __nfs_write_mapping(mapping, &wbc, how);
1404 if (ret < 0)
1405 return ret;
1406 wbc.sync_mode = WB_SYNC_ALL;
1407 return __nfs_write_mapping(mapping, &wbc, how);
1408}
1409
Trond Myklebusted90ef52007-07-20 13:13:28 -04001410/*
1411 * flush the inode to disk.
1412 */
1413int nfs_wb_all(struct inode *inode)
Trond Myklebust1c759502006-10-09 16:18:38 -04001414{
Trond Myklebusted90ef52007-07-20 13:13:28 -04001415 return nfs_write_mapping(inode->i_mapping, 0);
1416}
Trond Myklebust1c759502006-10-09 16:18:38 -04001417
Trond Myklebusted90ef52007-07-20 13:13:28 -04001418int nfs_wb_nocommit(struct inode *inode)
1419{
1420 return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
Trond Myklebust1c759502006-10-09 16:18:38 -04001421}
1422
Trond Myklebust1b3b4a12007-08-28 10:29:36 -04001423int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1424{
1425 struct nfs_page *req;
1426 loff_t range_start = page_offset(page);
1427 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1428 struct writeback_control wbc = {
1429 .bdi = page->mapping->backing_dev_info,
1430 .sync_mode = WB_SYNC_ALL,
1431 .nr_to_write = LONG_MAX,
1432 .range_start = range_start,
1433 .range_end = range_end,
1434 };
1435 int ret = 0;
1436
1437 BUG_ON(!PageLocked(page));
1438 for (;;) {
1439 req = nfs_page_find_request(page);
1440 if (req == NULL)
1441 goto out;
1442 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1443 nfs_release_request(req);
1444 break;
1445 }
1446 if (nfs_lock_request_dontget(req)) {
1447 nfs_inode_remove_request(req);
1448 /*
1449 * In case nfs_inode_remove_request has marked the
1450 * page as being dirty
1451 */
1452 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1453 nfs_unlock_request(req);
1454 break;
1455 }
1456 ret = nfs_wait_on_request(req);
1457 if (ret < 0)
1458 goto out;
1459 }
1460 if (!PagePrivate(page))
1461 return 0;
1462 ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
1463out:
1464 return ret;
1465}
1466
Adrian Bunk5334eb12007-11-21 15:04:31 -08001467static int nfs_wb_page_priority(struct inode *inode, struct page *page,
1468 int how)
Trond Myklebust1c759502006-10-09 16:18:38 -04001469{
1470 loff_t range_start = page_offset(page);
1471 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001472 struct writeback_control wbc = {
1473 .bdi = page->mapping->backing_dev_info,
1474 .sync_mode = WB_SYNC_ALL,
1475 .nr_to_write = LONG_MAX,
1476 .range_start = range_start,
1477 .range_end = range_end,
1478 };
1479 int ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001480
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001481 BUG_ON(!PageLocked(page));
Trond Myklebustc63c7b02007-04-02 19:29:52 -04001482 if (clear_page_dirty_for_io(page)) {
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001483 ret = nfs_writepage_locked(page, &wbc);
1484 if (ret < 0)
1485 goto out;
1486 }
Trond Myklebustf40313a2007-01-13 02:28:08 -05001487 if (!PagePrivate(page))
1488 return 0;
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001489 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1490 if (ret >= 0)
1491 return 0;
1492out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001493 __mark_inode_dirty(inode, I_DIRTY_PAGES);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001494 return ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001495}
1496
1497/*
1498 * Write back all requests on one page - we do this before reading it.
1499 */
1500int nfs_wb_page(struct inode *inode, struct page* page)
1501{
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001502 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
Trond Myklebust1c759502006-10-09 16:18:38 -04001503}
1504
David Howellsf7b422b2006-06-09 09:34:33 -04001505int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1508 sizeof(struct nfs_write_data),
1509 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001510 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if (nfs_wdata_cachep == NULL)
1512 return -ENOMEM;
1513
Matthew Dobson93d23412006-03-26 01:37:50 -08001514 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1515 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 if (nfs_wdata_mempool == NULL)
1517 return -ENOMEM;
1518
Matthew Dobson93d23412006-03-26 01:37:50 -08001519 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1520 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (nfs_commit_mempool == NULL)
1522 return -ENOMEM;
1523
Peter Zijlstra89a09142007-03-16 13:38:26 -08001524 /*
1525 * NFS congestion size, scale with available memory.
1526 *
1527 * 64MB: 8192k
1528 * 128MB: 11585k
1529 * 256MB: 16384k
1530 * 512MB: 23170k
1531 * 1GB: 32768k
1532 * 2GB: 46340k
1533 * 4GB: 65536k
1534 * 8GB: 92681k
1535 * 16GB: 131072k
1536 *
1537 * This allows larger machines to have larger/more transfers.
1538 * Limit the default to 256M
1539 */
1540 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1541 if (nfs_congestion_kb > 256*1024)
1542 nfs_congestion_kb = 256*1024;
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return 0;
1545}
1546
David Brownell266bee82006-06-27 12:59:15 -07001547void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
1549 mempool_destroy(nfs_commit_mempool);
1550 mempool_destroy(nfs_wdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001551 kmem_cache_destroy(nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
1553