blob: 4cb88df12f8314d7ca684a8b4d61bf902bb79a5f [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);
Fred Isamanf8512ad2008-03-19 11:24:39 -040042static void nfs_redirty_request(struct nfs_page *req);
Trond Myklebust788e7a82006-03-20 13:44:27 -050043static const struct rpc_call_ops nfs_write_partial_ops;
44static const struct rpc_call_ops nfs_write_full_ops;
45static const struct rpc_call_ops nfs_commit_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Christoph Lametere18b8902006-12-06 20:33:20 -080047static struct kmem_cache *nfs_wdata_cachep;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050048static mempool_t *nfs_wdata_mempool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static mempool_t *nfs_commit_mempool;
50
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070051struct nfs_write_data *nfs_commit_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Christoph Lametere6b4f8d2006-12-06 20:33:14 -080053 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
Chuck Lever40859d72005-11-30 18:09:02 -050054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 if (p) {
56 memset(p, 0, sizeof(*p));
57 INIT_LIST_HEAD(&p->pages);
58 }
59 return p;
60}
61
Trond Myklebust5e4424a2008-02-25 21:53:49 -080062void nfs_commit_free(struct nfs_write_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Chuck Lever40859d72005-11-30 18:09:02 -050064 if (p && (p->pagevec != &p->page_array[0]))
65 kfree(p->pagevec);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 mempool_free(p, nfs_commit_mempool);
67}
68
Trond Myklebust8d5658c2007-04-10 09:26:35 -040069struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050070{
Christoph Lametere6b4f8d2006-12-06 20:33:14 -080071 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050072
73 if (p) {
74 memset(p, 0, sizeof(*p));
75 INIT_LIST_HEAD(&p->pages);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070076 p->npages = pagecount;
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040077 if (pagecount <= ARRAY_SIZE(p->page_array))
78 p->pagevec = p->page_array;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050079 else {
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040080 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
81 if (!p->pagevec) {
Trond Myklebust3feb2d42006-03-20 13:44:37 -050082 mempool_free(p, nfs_wdata_mempool);
83 p = NULL;
84 }
85 }
86 }
87 return p;
88}
89
Trond Myklebust5e4424a2008-02-25 21:53:49 -080090static void nfs_writedata_free(struct nfs_write_data *p)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050091{
92 if (p && (p->pagevec != &p->page_array[0]))
93 kfree(p->pagevec);
94 mempool_free(p, nfs_wdata_mempool);
95}
96
Trond Myklebust383ba712008-02-19 20:04:20 -050097void nfs_writedata_release(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Trond Myklebust383ba712008-02-19 20:04:20 -050099 struct nfs_write_data *wdata = data;
100
101 put_nfs_open_context(wdata->args.context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 nfs_writedata_free(wdata);
103}
104
Trond Myklebust7b159fc2007-07-25 14:09:54 -0400105static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
106{
107 ctx->error = error;
108 smp_wmb();
109 set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
110}
111
Trond Myklebust277459d2006-12-05 00:35:35 -0500112static struct nfs_page *nfs_page_find_request_locked(struct page *page)
113{
114 struct nfs_page *req = NULL;
115
116 if (PagePrivate(page)) {
117 req = (struct nfs_page *)page_private(page);
118 if (req != NULL)
Trond Myklebustc03b4022007-06-17 13:26:38 -0400119 kref_get(&req->wb_kref);
Trond Myklebust277459d2006-12-05 00:35:35 -0500120 }
121 return req;
122}
123
124static struct nfs_page *nfs_page_find_request(struct page *page)
125{
Trond Myklebust587142f2007-07-02 09:57:54 -0400126 struct inode *inode = page->mapping->host;
Trond Myklebust277459d2006-12-05 00:35:35 -0500127 struct nfs_page *req = NULL;
Trond Myklebust277459d2006-12-05 00:35:35 -0500128
Trond Myklebust587142f2007-07-02 09:57:54 -0400129 spin_lock(&inode->i_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500130 req = nfs_page_find_request_locked(page);
Trond Myklebust587142f2007-07-02 09:57:54 -0400131 spin_unlock(&inode->i_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500132 return req;
133}
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/* Adjust the file length if we're writing beyond the end */
136static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
137{
138 struct inode *inode = page->mapping->host;
139 loff_t end, i_size = i_size_read(inode);
Trond Myklebustca52fec2007-04-17 17:22:13 -0400140 pgoff_t end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 if (i_size > 0 && page->index < end_index)
143 return;
144 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
145 if (i_size >= end)
146 return;
Chuck Lever91d5b472006-03-20 13:44:14 -0500147 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 i_size_write(inode, end);
149}
150
Trond Myklebusta301b772007-02-06 11:07:15 -0800151/* A writeback failed: mark the page as bad, and invalidate the page cache */
152static void nfs_set_pageerror(struct page *page)
153{
154 SetPageError(page);
155 nfs_zap_mapping(page->mapping->host, page->mapping);
156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/* We can set the PG_uptodate flag if we see that a write request
159 * covers the full page.
160 */
161static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
162{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (PageUptodate(page))
164 return;
165 if (base != 0)
166 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500167 if (count != nfs_page_length(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500169 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Trond Myklebuste21195a2006-12-05 00:35:39 -0500172static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 unsigned int offset, unsigned int count)
174{
175 struct nfs_page *req;
Trond Myklebuste21195a2006-12-05 00:35:39 -0500176 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Trond Myklebuste21195a2006-12-05 00:35:39 -0500178 for (;;) {
179 req = nfs_update_request(ctx, page, offset, count);
180 if (!IS_ERR(req))
181 break;
182 ret = PTR_ERR(req);
183 if (ret != -EBUSY)
184 return ret;
185 ret = nfs_wb_page(page->mapping->host, page);
186 if (ret != 0)
187 return ret;
188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 /* Update file length */
190 nfs_grow_file(page, offset, count);
Trond Myklebustacee4782008-01-22 17:13:07 -0500191 nfs_clear_page_tag_locked(req);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195static int wb_priority(struct writeback_control *wbc)
196{
197 if (wbc->for_reclaim)
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400198 return FLUSH_HIGHPRI | FLUSH_STABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (wbc->for_kupdate)
200 return FLUSH_LOWPRI;
201 return 0;
202}
203
204/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800205 * NFS congestion control
206 */
207
208int nfs_congestion_kb;
209
210#define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
211#define NFS_CONGESTION_OFF_THRESH \
212 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
213
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400214static int nfs_set_page_writeback(struct page *page)
Peter Zijlstra89a09142007-03-16 13:38:26 -0800215{
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400216 int ret = test_set_page_writeback(page);
217
218 if (!ret) {
Peter Zijlstra89a09142007-03-16 13:38:26 -0800219 struct inode *inode = page->mapping->host;
220 struct nfs_server *nfss = NFS_SERVER(inode);
221
Peter Zijlstra277866a2007-05-08 00:35:12 -0700222 if (atomic_long_inc_return(&nfss->writeback) >
Peter Zijlstra89a09142007-03-16 13:38:26 -0800223 NFS_CONGESTION_ON_THRESH)
224 set_bdi_congested(&nfss->backing_dev_info, WRITE);
225 }
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400226 return ret;
Peter Zijlstra89a09142007-03-16 13:38:26 -0800227}
228
229static void nfs_end_page_writeback(struct page *page)
230{
231 struct inode *inode = page->mapping->host;
232 struct nfs_server *nfss = NFS_SERVER(inode);
233
234 end_page_writeback(page);
Peter Zijlstrac4dc4be2007-10-16 23:25:41 -0700235 if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
Peter Zijlstra89a09142007-03-16 13:38:26 -0800236 clear_bdi_congested(&nfss->backing_dev_info, WRITE);
Peter Zijlstra89a09142007-03-16 13:38:26 -0800237}
238
239/*
Trond Myklebuste261f512006-12-05 00:35:41 -0500240 * Find an associated nfs write request, and prepare to flush it out
Trond Myklebust9cccef92007-07-22 17:09:05 -0400241 * May return an error if the user signalled nfs_wait_on_request().
Trond Myklebuste261f512006-12-05 00:35:41 -0500242 */
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400243static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
244 struct page *page)
Trond Myklebuste261f512006-12-05 00:35:41 -0500245{
Trond Myklebust587142f2007-07-02 09:57:54 -0400246 struct inode *inode = page->mapping->host;
Trond Myklebuste261f512006-12-05 00:35:41 -0500247 struct nfs_page *req;
Trond Myklebuste261f512006-12-05 00:35:41 -0500248 int ret;
249
Trond Myklebust587142f2007-07-02 09:57:54 -0400250 spin_lock(&inode->i_lock);
Trond Myklebuste261f512006-12-05 00:35:41 -0500251 for(;;) {
252 req = nfs_page_find_request_locked(page);
253 if (req == NULL) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400254 spin_unlock(&inode->i_lock);
Trond Myklebust9cccef92007-07-22 17:09:05 -0400255 return 0;
Trond Myklebuste261f512006-12-05 00:35:41 -0500256 }
Trond Myklebustacee4782008-01-22 17:13:07 -0500257 if (nfs_set_page_tag_locked(req))
Trond Myklebuste261f512006-12-05 00:35:41 -0500258 break;
259 /* Note: If we hold the page lock, as is the case in nfs_writepage,
Trond Myklebustacee4782008-01-22 17:13:07 -0500260 * then the call to nfs_set_page_tag_locked() will always
Trond Myklebuste261f512006-12-05 00:35:41 -0500261 * succeed provided that someone hasn't already marked the
262 * request as dirty (in which case we don't care).
263 */
Trond Myklebust587142f2007-07-02 09:57:54 -0400264 spin_unlock(&inode->i_lock);
Trond Myklebuste261f512006-12-05 00:35:41 -0500265 ret = nfs_wait_on_request(req);
266 nfs_release_request(req);
267 if (ret != 0)
268 return ret;
Trond Myklebust587142f2007-07-02 09:57:54 -0400269 spin_lock(&inode->i_lock);
Trond Myklebuste261f512006-12-05 00:35:41 -0500270 }
Trond Myklebust612c9382007-04-20 16:12:45 -0400271 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
272 /* This request is marked for commit */
Trond Myklebust587142f2007-07-02 09:57:54 -0400273 spin_unlock(&inode->i_lock);
Trond Myklebustacee4782008-01-22 17:13:07 -0500274 nfs_clear_page_tag_locked(req);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400275 nfs_pageio_complete(pgio);
Trond Myklebust9cccef92007-07-22 17:09:05 -0400276 return 0;
Trond Myklebust612c9382007-04-20 16:12:45 -0400277 }
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400278 if (nfs_set_page_writeback(page) != 0) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400279 spin_unlock(&inode->i_lock);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400280 BUG();
281 }
Trond Myklebust587142f2007-07-02 09:57:54 -0400282 spin_unlock(&inode->i_lock);
Fred Isamanf8512ad2008-03-19 11:24:39 -0400283 if (!nfs_pageio_add_request(pgio, req)) {
284 nfs_redirty_request(req);
285 nfs_end_page_writeback(page);
286 nfs_clear_page_tag_locked(req);
287 return pgio->pg_error;
288 }
Trond Myklebust9cccef92007-07-22 17:09:05 -0400289 return 0;
Trond Myklebuste261f512006-12-05 00:35:41 -0500290}
291
Trond Myklebustf758c8852007-07-22 19:27:32 -0400292static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
293{
294 struct inode *inode = page->mapping->host;
295
296 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
297 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
298
299 nfs_pageio_cond_complete(pgio, page->index);
300 return nfs_page_async_flush(pgio, page);
301}
302
Trond Myklebuste261f512006-12-05 00:35:41 -0500303/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 * Write an mmapped page to the server.
305 */
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500306static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Trond Myklebustf758c8852007-07-22 19:27:32 -0400308 struct nfs_pageio_descriptor pgio;
Trond Myklebuste261f512006-12-05 00:35:41 -0500309 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Trond Myklebustf758c8852007-07-22 19:27:32 -0400311 nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
312 err = nfs_do_writepage(page, wbc, &pgio);
313 nfs_pageio_complete(&pgio);
314 if (err < 0)
315 return err;
316 if (pgio.pg_error < 0)
317 return pgio.pg_error;
318 return 0;
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500319}
320
321int nfs_writepage(struct page *page, struct writeback_control *wbc)
322{
Trond Myklebustf758c8852007-07-22 19:27:32 -0400323 int ret;
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500324
Trond Myklebustf758c8852007-07-22 19:27:32 -0400325 ret = nfs_writepage_locked(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 unlock_page(page);
Trond Myklebustf758c8852007-07-22 19:27:32 -0400327 return ret;
328}
329
330static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
331{
332 int ret;
333
334 ret = nfs_do_writepage(page, wbc, data);
335 unlock_page(page);
336 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
340{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 struct inode *inode = mapping->host;
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400342 struct nfs_pageio_descriptor pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 int err;
344
Chuck Lever91d5b472006-03-20 13:44:14 -0500345 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
346
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400347 nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
Trond Myklebustf758c8852007-07-22 19:27:32 -0400348 err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400349 nfs_pageio_complete(&pgio);
Trond Myklebustf758c8852007-07-22 19:27:32 -0400350 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return err;
Trond Myklebustf758c8852007-07-22 19:27:32 -0400352 if (pgio.pg_error < 0)
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400353 return pgio.pg_error;
354 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357/*
358 * Insert a write request into an inode
359 */
Nick Piggin27852592008-02-04 23:48:37 -0800360static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 struct nfs_inode *nfsi = NFS_I(inode);
363 int error;
364
365 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
Nick Piggin27852592008-02-04 23:48:37 -0800366 BUG_ON(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (!nfsi->npages) {
368 igrab(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (nfs_have_delegation(inode, FMODE_WRITE))
370 nfsi->change_attr++;
371 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500372 SetPagePrivate(req->wb_page);
Trond Myklebust277459d2006-12-05 00:35:35 -0500373 set_page_private(req->wb_page, (unsigned long)req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 nfsi->npages++;
Trond Myklebustc03b4022007-06-17 13:26:38 -0400375 kref_get(&req->wb_kref);
Nick Piggin27852592008-02-04 23:48:37 -0800376 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
377 NFS_PAGE_TAG_LOCKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
380/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800381 * Remove a write request from an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 */
383static void nfs_inode_remove_request(struct nfs_page *req)
384{
Trond Myklebust88be9f92007-06-05 10:42:27 -0400385 struct inode *inode = req->wb_context->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 struct nfs_inode *nfsi = NFS_I(inode);
387
388 BUG_ON (!NFS_WBACK_BUSY(req));
389
Trond Myklebust587142f2007-07-02 09:57:54 -0400390 spin_lock(&inode->i_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500391 set_page_private(req->wb_page, 0);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500392 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
394 nfsi->npages--;
395 if (!nfsi->npages) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400396 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 iput(inode);
398 } else
Trond Myklebust587142f2007-07-02 09:57:54 -0400399 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 nfs_clear_request(req);
401 nfs_release_request(req);
402}
403
Trond Myklebust61822ab2006-12-05 00:35:42 -0500404static void
405nfs_redirty_request(struct nfs_page *req)
406{
Trond Myklebust61822ab2006-12-05 00:35:42 -0500407 __set_page_dirty_nobuffers(req->wb_page);
408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/*
411 * Check if a request is dirty
412 */
413static inline int
414nfs_dirty_request(struct nfs_page *req)
415{
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400416 struct page *page = req->wb_page;
417
Trond Myklebust612c9382007-04-20 16:12:45 -0400418 if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags))
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400419 return 0;
420 return !PageWriteback(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
423#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
424/*
425 * Add a request to the inode's commit list.
426 */
427static void
428nfs_mark_request_commit(struct nfs_page *req)
429{
Trond Myklebust88be9f92007-06-05 10:42:27 -0400430 struct inode *inode = req->wb_context->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 struct nfs_inode *nfsi = NFS_I(inode);
432
Trond Myklebust587142f2007-07-02 09:57:54 -0400433 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 nfsi->ncommit++;
Trond Myklebust612c9382007-04-20 16:12:45 -0400435 set_bit(PG_NEED_COMMIT, &(req)->wb_flags);
Trond Myklebust5c369682007-06-17 15:27:42 -0400436 radix_tree_tag_set(&nfsi->nfs_page_tree,
437 req->wb_index,
438 NFS_PAGE_TAG_COMMIT);
Trond Myklebust587142f2007-07-02 09:57:54 -0400439 spin_unlock(&inode->i_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700440 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -0700441 inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
Trond Myklebusta1803042006-12-05 00:45:19 -0500442 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400444
445static inline
446int nfs_write_need_commit(struct nfs_write_data *data)
447{
448 return data->verf.committed != NFS_FILE_SYNC;
449}
450
451static inline
452int nfs_reschedule_unstable_write(struct nfs_page *req)
453{
Trond Myklebust612c9382007-04-20 16:12:45 -0400454 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400455 nfs_mark_request_commit(req);
456 return 1;
457 }
458 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
459 nfs_redirty_request(req);
460 return 1;
461 }
462 return 0;
463}
464#else
465static inline void
466nfs_mark_request_commit(struct nfs_page *req)
467{
468}
469
470static inline
471int nfs_write_need_commit(struct nfs_write_data *data)
472{
473 return 0;
474}
475
476static inline
477int nfs_reschedule_unstable_write(struct nfs_page *req)
478{
479 return 0;
480}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481#endif
482
483/*
484 * Wait for a request to complete.
485 *
Matthew Wilcox150030b2007-12-06 16:24:39 -0500486 * Interruptible by fatal signals only.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 */
Trond Myklebustca52fec2007-04-17 17:22:13 -0400488static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct nfs_inode *nfsi = NFS_I(inode);
491 struct nfs_page *req;
Trond Myklebustca52fec2007-04-17 17:22:13 -0400492 pgoff_t idx_end, next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 unsigned int res = 0;
494 int error;
495
496 if (npages == 0)
497 idx_end = ~0;
498 else
499 idx_end = idx_start + npages - 1;
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 next = idx_start;
Trond Myklebust9fd367f2007-06-17 15:10:24 -0400502 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 -0700503 if (req->wb_index > idx_end)
504 break;
505
506 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000507 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Trond Myklebustc03b4022007-06-17 13:26:38 -0400509 kref_get(&req->wb_kref);
Trond Myklebust587142f2007-07-02 09:57:54 -0400510 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 error = nfs_wait_on_request(req);
512 nfs_release_request(req);
Trond Myklebust587142f2007-07-02 09:57:54 -0400513 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (error < 0)
515 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 res++;
517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return res;
519}
520
Trond Myklebust83715ad2006-07-05 13:17:12 -0400521static void nfs_cancel_commit_list(struct list_head *head)
522{
523 struct nfs_page *req;
524
525 while(!list_empty(head)) {
526 req = nfs_list_entry(head->next);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700527 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -0700528 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
529 BDI_RECLAIMABLE);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400530 nfs_list_remove_request(req);
Trond Myklebust612c9382007-04-20 16:12:45 -0400531 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400532 nfs_inode_remove_request(req);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700533 nfs_unlock_request(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400534 }
535}
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
538/*
539 * nfs_scan_commit - Scan an inode for commit requests
540 * @inode: NFS inode to scan
541 * @dst: destination list
542 * @idx_start: lower bound of page->index to scan.
543 * @npages: idx_start + npages sets the upper bound to scan.
544 *
545 * Moves requests from the inode's 'commit' request list.
546 * The requests are *not* checked to ensure that they form a contiguous set.
547 */
548static int
Trond Myklebustca52fec2007-04-17 17:22:13 -0400549nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000552 int res = 0;
553
554 if (nfsi->ncommit != 0) {
Trond Myklebust5c369682007-06-17 15:27:42 -0400555 res = nfs_scan_list(nfsi, dst, idx_start, npages,
556 NFS_PAGE_TAG_COMMIT);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000557 nfsi->ncommit -= res;
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return res;
560}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500561#else
Trond Myklebustca52fec2007-04-17 17:22:13 -0400562static 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 -0500563{
564 return 0;
565}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566#endif
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568/*
569 * Try to update any existing write request, or create one if there is none.
570 * In order to match, the request's credentials must match those of
571 * the calling process.
572 *
573 * Note: Should always be called with the Page Lock held!
574 */
575static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
Trond Myklebuste21195a2006-12-05 00:35:39 -0500576 struct page *page, unsigned int offset, unsigned int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Peter Zijlstra89a09142007-03-16 13:38:26 -0800578 struct address_space *mapping = page->mapping;
579 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 struct nfs_page *req, *new = NULL;
Trond Myklebustca52fec2007-04-17 17:22:13 -0400581 pgoff_t rqend, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 end = offset + bytes;
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 for (;;) {
586 /* Loop over all inode entries and see if we find
587 * A request for the page we wish to update
588 */
Nick Piggin27852592008-02-04 23:48:37 -0800589 if (new) {
590 if (radix_tree_preload(GFP_NOFS)) {
591 nfs_release_request(new);
592 return ERR_PTR(-ENOMEM);
593 }
594 }
595
Trond Myklebust587142f2007-07-02 09:57:54 -0400596 spin_lock(&inode->i_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500597 req = nfs_page_find_request_locked(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (req) {
Trond Myklebustacee4782008-01-22 17:13:07 -0500599 if (!nfs_set_page_tag_locked(req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 int error;
Trond Myklebust277459d2006-12-05 00:35:35 -0500601
Trond Myklebust587142f2007-07-02 09:57:54 -0400602 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 error = nfs_wait_on_request(req);
604 nfs_release_request(req);
Neil Brown1dd594b2006-03-20 13:44:04 -0500605 if (error < 0) {
Nick Piggin27852592008-02-04 23:48:37 -0800606 if (new) {
607 radix_tree_preload_end();
Neil Brown1dd594b2006-03-20 13:44:04 -0500608 nfs_release_request(new);
Nick Piggin27852592008-02-04 23:48:37 -0800609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return ERR_PTR(error);
Neil Brown1dd594b2006-03-20 13:44:04 -0500611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 continue;
613 }
Trond Myklebust587142f2007-07-02 09:57:54 -0400614 spin_unlock(&inode->i_lock);
Nick Piggin27852592008-02-04 23:48:37 -0800615 if (new) {
616 radix_tree_preload_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 nfs_release_request(new);
Nick Piggin27852592008-02-04 23:48:37 -0800618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 break;
620 }
621
622 if (new) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 nfs_lock_request_dontget(new);
Nick Piggin27852592008-02-04 23:48:37 -0800624 nfs_inode_add_request(inode, new);
Trond Myklebust587142f2007-07-02 09:57:54 -0400625 spin_unlock(&inode->i_lock);
Nick Piggin27852592008-02-04 23:48:37 -0800626 radix_tree_preload_end();
Trond Myklebust61e930a2007-10-18 17:08:05 -0400627 req = new;
628 goto zero_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
Trond Myklebust587142f2007-07-02 09:57:54 -0400630 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 new = nfs_create_request(ctx, inode, page, offset, bytes);
633 if (IS_ERR(new))
634 return new;
635 }
636
637 /* We have a request for our page.
638 * If the creds don't match, or the
639 * page addresses don't match,
640 * tell the caller to wait on the conflicting
641 * request.
642 */
643 rqend = req->wb_offset + req->wb_bytes;
644 if (req->wb_context != ctx
645 || req->wb_page != page
646 || !nfs_dirty_request(req)
647 || offset > rqend || end < req->wb_offset) {
Trond Myklebustacee4782008-01-22 17:13:07 -0500648 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return ERR_PTR(-EBUSY);
650 }
651
652 /* Okay, the request matches. Update the region */
653 if (offset < req->wb_offset) {
654 req->wb_offset = offset;
655 req->wb_pgbase = offset;
Trond Myklebust61e930a2007-10-18 17:08:05 -0400656 req->wb_bytes = max(end, rqend) - req->wb_offset;
657 goto zero_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659
660 if (end > rqend)
661 req->wb_bytes = end - req->wb_offset;
662
663 return req;
Trond Myklebust61e930a2007-10-18 17:08:05 -0400664zero_page:
665 /* If this page might potentially be marked as up to date,
666 * then we need to zero any uninitalised data. */
667 if (req->wb_pgbase == 0 && req->wb_bytes != PAGE_CACHE_SIZE
668 && !PageUptodate(req->wb_page))
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800669 zero_user_segment(req->wb_page, req->wb_bytes, PAGE_CACHE_SIZE);
Trond Myklebust61e930a2007-10-18 17:08:05 -0400670 return req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
673int nfs_flush_incompatible(struct file *file, struct page *page)
674{
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400675 struct nfs_open_context *ctx = nfs_file_open_context(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 struct nfs_page *req;
Trond Myklebust1a545332006-12-05 00:35:40 -0500677 int do_flush, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 /*
679 * Look for a request corresponding to this page. If there
680 * is one, and it belongs to another file, we flush it out
681 * before we try to copy anything into the page. Do this
682 * due to the lack of an ACCESS-type call in NFSv2.
683 * Also do the same if we find a request from an existing
684 * dropped page.
685 */
Trond Myklebust1a545332006-12-05 00:35:40 -0500686 do {
687 req = nfs_page_find_request(page);
688 if (req == NULL)
689 return 0;
690 do_flush = req->wb_page != page || req->wb_context != ctx
Trond Myklebuste261f512006-12-05 00:35:41 -0500691 || !nfs_dirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 nfs_release_request(req);
Trond Myklebust1a545332006-12-05 00:35:40 -0500693 if (!do_flush)
694 return 0;
695 status = nfs_wb_page(page->mapping->host, page);
696 } while (status == 0);
697 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
700/*
Trond Myklebust5d47a352008-02-07 17:24:07 -0500701 * If the page cache is marked as unsafe or invalid, then we can't rely on
702 * the PageUptodate() flag. In this case, we will need to turn off
703 * write optimisations that depend on the page contents being correct.
704 */
705static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
706{
707 return PageUptodate(page) &&
708 !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
709}
710
711/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 * Update and possibly write a cached page of an NFS file.
713 *
714 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
715 * things with a page scheduled for an RPC call (e.g. invalidate it).
716 */
717int nfs_updatepage(struct file *file, struct page *page,
718 unsigned int offset, unsigned int count)
719{
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400720 struct nfs_open_context *ctx = nfs_file_open_context(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 int status = 0;
723
Chuck Lever91d5b472006-03-20 13:44:14 -0500724 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800727 file->f_path.dentry->d_parent->d_name.name,
728 file->f_path.dentry->d_name.name, count,
Chuck Lever0bbacc42005-11-01 16:53:32 -0500729 (long long)(page_offset(page) +offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 /* If we're not using byte range locks, and we know the page
Trond Myklebust5d47a352008-02-07 17:24:07 -0500732 * is up to date, it may be more efficient to extend the write
733 * to cover the entire page in order to avoid fragmentation
734 * inefficiencies.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 */
Trond Myklebust5d47a352008-02-07 17:24:07 -0500736 if (nfs_write_pageuptodate(page, inode) &&
737 inode->i_flock == NULL &&
Trond Myklebust4b5621f2008-02-25 15:56:29 -0800738 !(file->f_flags & O_SYNC)) {
Trond Myklebust49a70f22006-12-05 00:35:38 -0500739 count = max(count + offset, nfs_page_length(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
742
Trond Myklebuste21195a2006-12-05 00:35:39 -0500743 status = nfs_writepage_setup(ctx, page, offset, count);
Trond Myklebuste261f512006-12-05 00:35:41 -0500744 __set_page_dirty_nobuffers(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
747 status, (long long)i_size_read(inode));
748 if (status < 0)
Trond Myklebusta301b772007-02-06 11:07:15 -0800749 nfs_set_pageerror(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return status;
751}
752
753static void nfs_writepage_release(struct nfs_page *req)
754{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Trond Myklebust44dd1512007-05-19 11:58:03 -0400756 if (PageError(req->wb_page)) {
757 nfs_end_page_writeback(req->wb_page);
758 nfs_inode_remove_request(req);
759 } else if (!nfs_reschedule_unstable_write(req)) {
760 /* Set the PG_uptodate flag */
761 nfs_mark_uptodate(req->wb_page, req->wb_pgbase, req->wb_bytes);
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400762 nfs_end_page_writeback(req->wb_page);
763 nfs_inode_remove_request(req);
764 } else
765 nfs_end_page_writeback(req->wb_page);
Trond Myklebust9fd367f2007-06-17 15:10:24 -0400766 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
Trond Myklebust3ff75762007-07-14 15:40:00 -0400769static int flush_task_priority(int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
772 case FLUSH_HIGHPRI:
773 return RPC_PRIORITY_HIGH;
774 case FLUSH_LOWPRI:
775 return RPC_PRIORITY_LOW;
776 }
777 return RPC_PRIORITY_NORMAL;
778}
779
780/*
781 * Set up the argument/result storage required for the RPC call.
782 */
783static void nfs_write_rpcsetup(struct nfs_page *req,
784 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500785 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 unsigned int count, unsigned int offset,
787 int how)
788{
Trond Myklebust84115e12007-07-14 15:39:59 -0400789 struct inode *inode = req->wb_context->path.dentry->d_inode;
790 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
Trond Myklebust3ff75762007-07-14 15:40:00 -0400791 int priority = flush_task_priority(how);
Trond Myklebust07737692007-10-25 18:42:54 -0400792 struct rpc_task *task;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400793 struct rpc_message msg = {
794 .rpc_argp = &data->args,
795 .rpc_resp = &data->res,
796 .rpc_cred = req->wb_context->cred,
797 };
Trond Myklebust84115e12007-07-14 15:39:59 -0400798 struct rpc_task_setup task_setup_data = {
799 .rpc_client = NFS_CLIENT(inode),
Trond Myklebust07737692007-10-25 18:42:54 -0400800 .task = &data->task,
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400801 .rpc_message = &msg,
Trond Myklebust84115e12007-07-14 15:39:59 -0400802 .callback_ops = call_ops,
803 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -0500804 .workqueue = nfsiod_workqueue,
Trond Myklebust84115e12007-07-14 15:39:59 -0400805 .flags = flags,
Trond Myklebust3ff75762007-07-14 15:40:00 -0400806 .priority = priority,
Trond Myklebust84115e12007-07-14 15:39:59 -0400807 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 /* Set up the RPC argument and reply structs
810 * NB: take care not to mess about with data->commit et al. */
811
812 data->req = req;
Trond Myklebust88be9f92007-06-05 10:42:27 -0400813 data->inode = inode = req->wb_context->path.dentry->d_inode;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400814 data->cred = msg.rpc_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 data->args.fh = NFS_FH(inode);
817 data->args.offset = req_offset(req) + offset;
818 data->args.pgbase = req->wb_pgbase + offset;
819 data->args.pages = data->pagevec;
820 data->args.count = count;
Trond Myklebust383ba712008-02-19 20:04:20 -0500821 data->args.context = get_nfs_open_context(req->wb_context);
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400822 data->args.stable = NFS_UNSTABLE;
823 if (how & FLUSH_STABLE) {
824 data->args.stable = NFS_DATA_SYNC;
825 if (!NFS_I(inode)->ncommit)
826 data->args.stable = NFS_FILE_SYNC;
827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 data->res.fattr = &data->fattr;
830 data->res.count = count;
831 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400832 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Trond Myklebust788e7a82006-03-20 13:44:27 -0500834 /* Set up the initial task struct. */
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400835 NFS_PROTO(inode)->write_setup(data, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Chuck Levera3f565b2007-01-31 12:14:01 -0500837 dprintk("NFS: %5u initiated write call "
838 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500839 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 inode->i_sb->s_id,
841 (long long)NFS_FILEID(inode),
842 count,
843 (unsigned long long)data->args.offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Trond Myklebust07737692007-10-25 18:42:54 -0400845 task = rpc_run_task(&task_setup_data);
846 if (!IS_ERR(task))
847 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
850/*
851 * Generate multiple small requests to write out a single
852 * contiguous dirty area on one page.
853 */
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400854static 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 -0700855{
856 struct nfs_page *req = nfs_list_entry(head->next);
857 struct page *page = req->wb_page;
858 struct nfs_write_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700859 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
860 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 int requests = 0;
862 LIST_HEAD(list);
863
864 nfs_list_remove_request(req);
865
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400866 nbytes = count;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700867 do {
868 size_t len = min(nbytes, wsize);
869
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400870 data = nfs_writedata_alloc(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (!data)
872 goto out_bad;
873 list_add(&data->pages, &list);
874 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700875 nbytes -= len;
876 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 atomic_set(&req->wb_complete, requests);
878
879 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 offset = 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400881 nbytes = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 do {
883 data = list_entry(list.next, struct nfs_write_data, pages);
884 list_del_init(&data->pages);
885
886 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400888 if (nbytes < wsize)
889 wsize = nbytes;
890 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
891 wsize, offset, how);
892 offset += wsize;
893 nbytes -= wsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 } while (nbytes != 0);
895
896 return 0;
897
898out_bad:
899 while (!list_empty(&list)) {
900 data = list_entry(list.next, struct nfs_write_data, pages);
901 list_del(&data->pages);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500902 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
Trond Myklebust61822ab2006-12-05 00:35:42 -0500904 nfs_redirty_request(req);
Trond Myklebust6d677e32007-04-20 16:12:40 -0400905 nfs_end_page_writeback(req->wb_page);
Trond Myklebust9fd367f2007-06-17 15:10:24 -0400906 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return -ENOMEM;
908}
909
910/*
911 * Create an RPC task for the given write request and kick it.
912 * The page must have been locked by the caller.
913 *
914 * It may happen that the page we're passed is not marked dirty.
915 * This is the case if nfs_updatepage detects a conflicting request
916 * that has been written but not committed.
917 */
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400918static 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 -0700919{
920 struct nfs_page *req;
921 struct page **pages;
922 struct nfs_write_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400924 data = nfs_writedata_alloc(npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (!data)
926 goto out_bad;
927
928 pages = data->pagevec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 while (!list_empty(head)) {
930 req = nfs_list_entry(head->next);
931 nfs_list_remove_request(req);
932 nfs_list_add_request(req, &data->pages);
933 ClearPageError(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 *pages++ = req->wb_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936 req = nfs_list_entry(data->pages.next);
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 /* Set up the argument struct */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500939 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return 0;
942 out_bad:
943 while (!list_empty(head)) {
Trond Myklebust10afec92007-05-14 17:16:04 -0400944 req = nfs_list_entry(head->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 nfs_list_remove_request(req);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500946 nfs_redirty_request(req);
Trond Myklebust6d677e32007-04-20 16:12:40 -0400947 nfs_end_page_writeback(req->wb_page);
Trond Myklebust9fd367f2007-06-17 15:10:24 -0400948 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950 return -ENOMEM;
951}
952
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400953static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
954 struct inode *inode, int ioflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Chuck Leverbf4285e2007-12-20 14:54:57 -0500956 size_t wsize = NFS_SERVER(inode)->wsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400958 if (wsize < PAGE_CACHE_SIZE)
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400959 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400960 else
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400961 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
963
964/*
965 * Handle a write reply that flushed part of a page.
966 */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500967static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Trond Myklebust788e7a82006-03-20 13:44:27 -0500969 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 struct nfs_page *req = data->req;
971 struct page *page = req->wb_page;
972
973 dprintk("NFS: write (%s/%Ld %d@%Ld)",
Trond Myklebust88be9f92007-06-05 10:42:27 -0400974 req->wb_context->path.dentry->d_inode->i_sb->s_id,
975 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 req->wb_bytes,
977 (long long)req_offset(req));
978
Trond Myklebust788e7a82006-03-20 13:44:27 -0500979 if (nfs_writeback_done(task, data) != 0)
980 return;
981
982 if (task->tk_status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -0800983 nfs_set_pageerror(page);
Trond Myklebust7b159fc2007-07-25 14:09:54 -0400984 nfs_context_set_write_error(req->wb_context, task->tk_status);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500985 dprintk(", error = %d\n", task->tk_status);
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400986 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
988
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400989 if (nfs_write_need_commit(data)) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400990 struct inode *inode = page->mapping->host;
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400991
Trond Myklebust587142f2007-07-02 09:57:54 -0400992 spin_lock(&inode->i_lock);
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400993 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
994 /* Do nothing we need to resend the writes */
995 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
996 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
997 dprintk(" defer commit\n");
998 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
999 set_bit(PG_NEED_RESCHED, &req->wb_flags);
1000 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
1001 dprintk(" server reboot detected\n");
1002 }
Trond Myklebust587142f2007-07-02 09:57:54 -04001003 spin_unlock(&inode->i_lock);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001004 } else
1005 dprintk(" OK\n");
1006
1007out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (atomic_dec_and_test(&req->wb_complete))
1009 nfs_writepage_release(req);
1010}
1011
Trond Myklebust788e7a82006-03-20 13:44:27 -05001012static const struct rpc_call_ops nfs_write_partial_ops = {
1013 .rpc_call_done = nfs_writeback_done_partial,
1014 .rpc_release = nfs_writedata_release,
1015};
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017/*
1018 * Handle a write reply that flushes a whole page.
1019 *
1020 * FIXME: There is an inherent race with invalidate_inode_pages and
1021 * writebacks since the page->count is kept > 1 for as long
1022 * as the page has a write request pending.
1023 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001024static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001026 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 struct nfs_page *req;
1028 struct page *page;
1029
Trond Myklebust788e7a82006-03-20 13:44:27 -05001030 if (nfs_writeback_done(task, data) != 0)
1031 return;
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 /* Update attributes as result of writeback. */
1034 while (!list_empty(&data->pages)) {
1035 req = nfs_list_entry(data->pages.next);
1036 nfs_list_remove_request(req);
1037 page = req->wb_page;
1038
1039 dprintk("NFS: write (%s/%Ld %d@%Ld)",
Trond Myklebust88be9f92007-06-05 10:42:27 -04001040 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1041 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 req->wb_bytes,
1043 (long long)req_offset(req));
1044
Trond Myklebust788e7a82006-03-20 13:44:27 -05001045 if (task->tk_status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -08001046 nfs_set_pageerror(page);
Trond Myklebust7b159fc2007-07-25 14:09:54 -04001047 nfs_context_set_write_error(req->wb_context, task->tk_status);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001048 dprintk(", error = %d\n", task->tk_status);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001049 goto remove_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001052 if (nfs_write_need_commit(data)) {
1053 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1054 nfs_mark_request_commit(req);
1055 nfs_end_page_writeback(page);
1056 dprintk(" marked for commit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 goto next;
1058 }
Trond Myklebust44dd1512007-05-19 11:58:03 -04001059 /* Set the PG_uptodate flag? */
1060 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001061 dprintk(" OK\n");
1062remove_request:
1063 nfs_end_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 nfs_inode_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 next:
Trond Myklebust9fd367f2007-06-17 15:10:24 -04001066 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068}
1069
Trond Myklebust788e7a82006-03-20 13:44:27 -05001070static const struct rpc_call_ops nfs_write_full_ops = {
1071 .rpc_call_done = nfs_writeback_done_full,
1072 .rpc_release = nfs_writedata_release,
1073};
1074
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076/*
1077 * This function is called when the WRITE call is complete.
1078 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001079int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 struct nfs_writeargs *argp = &data->args;
1082 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001083 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Chuck Levera3f565b2007-01-31 12:14:01 -05001085 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 task->tk_pid, task->tk_status);
1087
Chuck Leverf551e442006-09-20 14:33:04 -04001088 /*
1089 * ->write_done will attempt to use post-op attributes to detect
1090 * conflicting writes by other clients. A strict interpretation
1091 * of close-to-open would allow us to continue caching even if
1092 * another writer had changed the file, but some applications
1093 * depend on tighter cache coherency when writing.
1094 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001095 status = NFS_PROTO(data->inode)->write_done(task, data);
1096 if (status != 0)
1097 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001098 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1101 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1102 /* We tried a write call, but the server did not
1103 * commit data to stable storage even though we
1104 * requested it.
1105 * Note: There is a known bug in Tru64 < 5.0 in which
1106 * the server reports NFS_DATA_SYNC, but performs
1107 * NFS_FILE_SYNC. We therefore implement this checking
1108 * as a dprintk() in order to avoid filling syslog.
1109 */
1110 static unsigned long complain;
1111
1112 if (time_before(complain, jiffies)) {
1113 dprintk("NFS: faulty NFS server %s:"
1114 " (committed = %d) != (stable = %d)\n",
David Howells54ceac42006-08-22 20:06:13 -04001115 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 resp->verf->committed, argp->stable);
1117 complain = jiffies + 300 * HZ;
1118 }
1119 }
1120#endif
1121 /* Is this a short write? */
1122 if (task->tk_status >= 0 && resp->count < argp->count) {
1123 static unsigned long complain;
1124
Chuck Lever91d5b472006-03-20 13:44:14 -05001125 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 /* Has the server at least made some progress? */
1128 if (resp->count != 0) {
1129 /* Was this an NFSv2 write or an NFSv3 stable write? */
1130 if (resp->verf->committed != NFS_UNSTABLE) {
1131 /* Resend from where the server left off */
1132 argp->offset += resp->count;
1133 argp->pgbase += resp->count;
1134 argp->count -= resp->count;
1135 } else {
1136 /* Resend as a stable write in order to avoid
1137 * headaches in the case of a server crash.
1138 */
1139 argp->stable = NFS_FILE_SYNC;
1140 }
1141 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001142 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144 if (time_before(complain, jiffies)) {
1145 printk(KERN_WARNING
1146 "NFS: Server wrote zero bytes, expected %u.\n",
1147 argp->count);
1148 complain = jiffies + 300 * HZ;
1149 }
1150 /* Can't do anything about it except throw an error. */
1151 task->tk_status = -EIO;
1152 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001153 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
1156
1157#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust383ba712008-02-19 20:04:20 -05001158void nfs_commit_release(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Trond Myklebust383ba712008-02-19 20:04:20 -05001160 struct nfs_write_data *wdata = data;
1161
1162 put_nfs_open_context(wdata->args.context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 nfs_commit_free(wdata);
1164}
1165
1166/*
1167 * Set up the argument/result storage required for the RPC call.
1168 */
1169static void nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001170 struct nfs_write_data *data,
1171 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Trond Myklebust84115e12007-07-14 15:39:59 -04001173 struct nfs_page *first = nfs_list_entry(head->next);
1174 struct inode *inode = first->wb_context->path.dentry->d_inode;
1175 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
Trond Myklebust3ff75762007-07-14 15:40:00 -04001176 int priority = flush_task_priority(how);
Trond Myklebust07737692007-10-25 18:42:54 -04001177 struct rpc_task *task;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001178 struct rpc_message msg = {
1179 .rpc_argp = &data->args,
1180 .rpc_resp = &data->res,
1181 .rpc_cred = first->wb_context->cred,
1182 };
Trond Myklebust84115e12007-07-14 15:39:59 -04001183 struct rpc_task_setup task_setup_data = {
Trond Myklebust07737692007-10-25 18:42:54 -04001184 .task = &data->task,
Trond Myklebust84115e12007-07-14 15:39:59 -04001185 .rpc_client = NFS_CLIENT(inode),
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001186 .rpc_message = &msg,
Trond Myklebust84115e12007-07-14 15:39:59 -04001187 .callback_ops = &nfs_commit_ops,
1188 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001189 .workqueue = nfsiod_workqueue,
Trond Myklebust84115e12007-07-14 15:39:59 -04001190 .flags = flags,
Trond Myklebust3ff75762007-07-14 15:40:00 -04001191 .priority = priority,
Trond Myklebust84115e12007-07-14 15:39:59 -04001192 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 /* Set up the RPC argument and reply structs
1195 * NB: take care not to mess about with data->commit et al. */
1196
1197 list_splice_init(head, &data->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 data->inode = inode;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001200 data->cred = msg.rpc_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001203 /* Note: we always request a commit of the entire inode */
1204 data->args.offset = 0;
1205 data->args.count = 0;
Trond Myklebust383ba712008-02-19 20:04:20 -05001206 data->args.context = get_nfs_open_context(first->wb_context);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001207 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 data->res.fattr = &data->fattr;
1209 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001210 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001211
1212 /* Set up the initial task struct. */
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001213 NFS_PROTO(inode)->commit_setup(data, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Chuck Levera3f565b2007-01-31 12:14:01 -05001215 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001216
Trond Myklebust07737692007-10-25 18:42:54 -04001217 task = rpc_run_task(&task_setup_data);
1218 if (!IS_ERR(task))
1219 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
1222/*
1223 * Commit dirty pages
1224 */
1225static int
Chuck Lever40859d72005-11-30 18:09:02 -05001226nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
1228 struct nfs_write_data *data;
1229 struct nfs_page *req;
1230
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001231 data = nfs_commit_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 if (!data)
1234 goto out_bad;
1235
1236 /* Set up the argument struct */
1237 nfs_commit_rpcsetup(head, data, how);
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return 0;
1240 out_bad:
1241 while (!list_empty(head)) {
1242 req = nfs_list_entry(head->next);
1243 nfs_list_remove_request(req);
1244 nfs_mark_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001245 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -07001246 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1247 BDI_RECLAIMABLE);
Trond Myklebust9fd367f2007-06-17 15:10:24 -04001248 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
1250 return -ENOMEM;
1251}
1252
1253/*
1254 * COMMIT call returned
1255 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001256static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001258 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Chuck Levera3f565b2007-01-31 12:14:01 -05001261 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 task->tk_pid, task->tk_status);
1263
Trond Myklebust788e7a82006-03-20 13:44:27 -05001264 /* Call the NFS version-specific code */
1265 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1266 return;
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 while (!list_empty(&data->pages)) {
1269 req = nfs_list_entry(data->pages.next);
1270 nfs_list_remove_request(req);
Trond Myklebust612c9382007-04-20 16:12:45 -04001271 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
Christoph Lameterfd39fc82006-06-30 01:55:40 -07001272 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -07001273 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1274 BDI_RECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
Trond Myklebust88be9f92007-06-05 10:42:27 -04001277 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1278 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 req->wb_bytes,
1280 (long long)req_offset(req));
1281 if (task->tk_status < 0) {
Trond Myklebust7b159fc2007-07-25 14:09:54 -04001282 nfs_context_set_write_error(req->wb_context, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 nfs_inode_remove_request(req);
1284 dprintk(", error = %d\n", task->tk_status);
1285 goto next;
1286 }
1287
1288 /* Okay, COMMIT succeeded, apparently. Check the verifier
1289 * returned by the server against all stored verfs. */
1290 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1291 /* We have a match */
Trond Myklebust44dd1512007-05-19 11:58:03 -04001292 /* Set the PG_uptodate flag */
1293 nfs_mark_uptodate(req->wb_page, req->wb_pgbase,
1294 req->wb_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 nfs_inode_remove_request(req);
1296 dprintk(" OK\n");
1297 goto next;
1298 }
1299 /* We have a mismatch. Write the page again */
1300 dprintk(" mismatch\n");
Trond Myklebust61822ab2006-12-05 00:35:42 -05001301 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 next:
Trond Myklebust9fd367f2007-06-17 15:10:24 -04001303 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001306
1307static const struct rpc_call_ops nfs_commit_ops = {
1308 .rpc_call_done = nfs_commit_done,
1309 .rpc_release = nfs_commit_release,
1310};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001312int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001315 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Trond Myklebust587142f2007-07-02 09:57:54 -04001317 spin_lock(&inode->i_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001318 res = nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebust587142f2007-07-02 09:57:54 -04001319 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001321 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001322 if (error < 0)
1323 return error;
1324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return res;
1326}
Trond Myklebustc63c7b02007-04-02 19:29:52 -04001327#else
1328static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1329{
1330 return 0;
1331}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332#endif
1333
Trond Myklebust1c759502006-10-09 16:18:38 -04001334long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
Trond Myklebust1c759502006-10-09 16:18:38 -04001336 struct inode *inode = mapping->host;
Trond Myklebustca52fec2007-04-17 17:22:13 -04001337 pgoff_t idx_start, idx_end;
Trond Myklebust1c759502006-10-09 16:18:38 -04001338 unsigned int npages = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001339 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001340 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebust3f442542006-09-17 14:46:44 -04001341 long pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Trond Myklebust1c759502006-10-09 16:18:38 -04001343 /* FIXME */
1344 if (wbc->range_cyclic)
1345 idx_start = 0;
1346 else {
1347 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1348 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1349 if (idx_end > idx_start) {
Trond Myklebustca52fec2007-04-17 17:22:13 -04001350 pgoff_t l_npages = 1 + idx_end - idx_start;
Trond Myklebust1c759502006-10-09 16:18:38 -04001351 npages = l_npages;
1352 if (sizeof(npages) != sizeof(l_npages) &&
Trond Myklebustca52fec2007-04-17 17:22:13 -04001353 (pgoff_t)npages != l_npages)
Trond Myklebust1c759502006-10-09 16:18:38 -04001354 npages = 0;
1355 }
1356 }
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001357 how &= ~FLUSH_NOCOMMIT;
Trond Myklebust587142f2007-07-02 09:57:54 -04001358 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 do {
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001360 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1361 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001362 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001363 if (nocommit)
1364 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001365 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebust724c4392007-04-17 17:22:13 -04001366 if (pages == 0)
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001367 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001368 if (how & FLUSH_INVALIDATE) {
Trond Myklebust587142f2007-07-02 09:57:54 -04001369 spin_unlock(&inode->i_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001370 nfs_cancel_commit_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001371 ret = pages;
Trond Myklebust587142f2007-07-02 09:57:54 -04001372 spin_lock(&inode->i_lock);
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001373 continue;
1374 }
1375 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebust587142f2007-07-02 09:57:54 -04001376 spin_unlock(&inode->i_lock);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001377 ret = nfs_commit_list(inode, &head, how);
Trond Myklebust587142f2007-07-02 09:57:54 -04001378 spin_lock(&inode->i_lock);
1379
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001380 } while (ret >= 0);
Trond Myklebust587142f2007-07-02 09:57:54 -04001381 spin_unlock(&inode->i_lock);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001382 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
1384
Trond Myklebust34901f72007-07-25 14:09:54 -04001385static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
Trond Myklebust1c759502006-10-09 16:18:38 -04001386{
Trond Myklebust1c759502006-10-09 16:18:38 -04001387 int ret;
1388
Trond Myklebust34901f72007-07-25 14:09:54 -04001389 ret = nfs_writepages(mapping, wbc);
Trond Myklebust61822ab2006-12-05 00:35:42 -05001390 if (ret < 0)
1391 goto out;
Trond Myklebust34901f72007-07-25 14:09:54 -04001392 ret = nfs_sync_mapping_wait(mapping, wbc, how);
Trond Myklebusted90ef52007-07-20 13:13:28 -04001393 if (ret < 0)
1394 goto out;
1395 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001396out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001397 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Trond Myklebust1c759502006-10-09 16:18:38 -04001398 return ret;
1399}
1400
Trond Myklebust34901f72007-07-25 14:09:54 -04001401/* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
1402static int nfs_write_mapping(struct address_space *mapping, int how)
1403{
1404 struct writeback_control wbc = {
1405 .bdi = mapping->backing_dev_info,
1406 .sync_mode = WB_SYNC_NONE,
1407 .nr_to_write = LONG_MAX,
1408 .for_writepages = 1,
1409 .range_cyclic = 1,
1410 };
1411 int ret;
1412
1413 ret = __nfs_write_mapping(mapping, &wbc, how);
1414 if (ret < 0)
1415 return ret;
1416 wbc.sync_mode = WB_SYNC_ALL;
1417 return __nfs_write_mapping(mapping, &wbc, how);
1418}
1419
Trond Myklebusted90ef52007-07-20 13:13:28 -04001420/*
1421 * flush the inode to disk.
1422 */
1423int nfs_wb_all(struct inode *inode)
Trond Myklebust1c759502006-10-09 16:18:38 -04001424{
Trond Myklebusted90ef52007-07-20 13:13:28 -04001425 return nfs_write_mapping(inode->i_mapping, 0);
1426}
Trond Myklebust1c759502006-10-09 16:18:38 -04001427
Trond Myklebusted90ef52007-07-20 13:13:28 -04001428int nfs_wb_nocommit(struct inode *inode)
1429{
1430 return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
Trond Myklebust1c759502006-10-09 16:18:38 -04001431}
1432
Trond Myklebust1b3b4a12007-08-28 10:29:36 -04001433int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1434{
1435 struct nfs_page *req;
1436 loff_t range_start = page_offset(page);
1437 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1438 struct writeback_control wbc = {
1439 .bdi = page->mapping->backing_dev_info,
1440 .sync_mode = WB_SYNC_ALL,
1441 .nr_to_write = LONG_MAX,
1442 .range_start = range_start,
1443 .range_end = range_end,
1444 };
1445 int ret = 0;
1446
1447 BUG_ON(!PageLocked(page));
1448 for (;;) {
1449 req = nfs_page_find_request(page);
1450 if (req == NULL)
1451 goto out;
1452 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1453 nfs_release_request(req);
1454 break;
1455 }
1456 if (nfs_lock_request_dontget(req)) {
1457 nfs_inode_remove_request(req);
1458 /*
1459 * In case nfs_inode_remove_request has marked the
1460 * page as being dirty
1461 */
1462 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1463 nfs_unlock_request(req);
1464 break;
1465 }
1466 ret = nfs_wait_on_request(req);
1467 if (ret < 0)
1468 goto out;
1469 }
1470 if (!PagePrivate(page))
1471 return 0;
1472 ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
1473out:
1474 return ret;
1475}
1476
Adrian Bunk5334eb12007-11-21 15:04:31 -08001477static int nfs_wb_page_priority(struct inode *inode, struct page *page,
1478 int how)
Trond Myklebust1c759502006-10-09 16:18:38 -04001479{
1480 loff_t range_start = page_offset(page);
1481 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001482 struct writeback_control wbc = {
1483 .bdi = page->mapping->backing_dev_info,
1484 .sync_mode = WB_SYNC_ALL,
1485 .nr_to_write = LONG_MAX,
1486 .range_start = range_start,
1487 .range_end = range_end,
1488 };
1489 int ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001490
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001491 BUG_ON(!PageLocked(page));
Trond Myklebustc63c7b02007-04-02 19:29:52 -04001492 if (clear_page_dirty_for_io(page)) {
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001493 ret = nfs_writepage_locked(page, &wbc);
1494 if (ret < 0)
1495 goto out;
1496 }
Trond Myklebustf40313a2007-01-13 02:28:08 -05001497 if (!PagePrivate(page))
1498 return 0;
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001499 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1500 if (ret >= 0)
1501 return 0;
1502out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001503 __mark_inode_dirty(inode, I_DIRTY_PAGES);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001504 return ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001505}
1506
1507/*
1508 * Write back all requests on one page - we do this before reading it.
1509 */
1510int nfs_wb_page(struct inode *inode, struct page* page)
1511{
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001512 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
Trond Myklebust1c759502006-10-09 16:18:38 -04001513}
1514
David Howellsf7b422b2006-06-09 09:34:33 -04001515int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
1517 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1518 sizeof(struct nfs_write_data),
1519 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001520 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (nfs_wdata_cachep == NULL)
1522 return -ENOMEM;
1523
Matthew Dobson93d23412006-03-26 01:37:50 -08001524 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1525 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (nfs_wdata_mempool == NULL)
1527 return -ENOMEM;
1528
Matthew Dobson93d23412006-03-26 01:37:50 -08001529 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1530 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 if (nfs_commit_mempool == NULL)
1532 return -ENOMEM;
1533
Peter Zijlstra89a09142007-03-16 13:38:26 -08001534 /*
1535 * NFS congestion size, scale with available memory.
1536 *
1537 * 64MB: 8192k
1538 * 128MB: 11585k
1539 * 256MB: 16384k
1540 * 512MB: 23170k
1541 * 1GB: 32768k
1542 * 2GB: 46340k
1543 * 4GB: 65536k
1544 * 8GB: 92681k
1545 * 16GB: 131072k
1546 *
1547 * This allows larger machines to have larger/more transfers.
1548 * Limit the default to 256M
1549 */
1550 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1551 if (nfs_congestion_kb > 256*1024)
1552 nfs_congestion_kb = 256*1024;
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return 0;
1555}
1556
David Brownell266bee82006-06-27 12:59:15 -07001557void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
1559 mempool_destroy(nfs_commit_mempool);
1560 mempool_destroy(nfs_wdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001561 kmem_cache_destroy(nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563