blob: 6240e644f249168d9e6c363f365524e41b6392e2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/write.c
3 *
Trond Myklebust7c85d902006-12-13 15:23:48 -05004 * Write file data over NFS.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/types.h>
10#include <linux/slab.h>
11#include <linux/mm.h>
12#include <linux/pagemap.h>
13#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/writeback.h>
Peter Zijlstra89a09142007-03-16 13:38:26 -080015#include <linux/swap.h>
Trond Myklebust074cc1d2009-08-10 08:54:13 -040016#include <linux/migrate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <linux/sunrpc/clnt.h>
19#include <linux/nfs_fs.h>
20#include <linux/nfs_mount.h>
21#include <linux/nfs_page.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070022#include <linux/backing-dev.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include "delegation.h"
Trond Myklebust49a70f22006-12-05 00:35:38 -050027#include "internal.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050028#include "iostat.h"
Andy Adamsondef6ed72009-04-01 09:22:26 -040029#include "nfs4_fs.h"
Trond Myklebust074cc1d2009-08-10 08:54:13 -040030#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#define NFSDBG_FACILITY NFSDBG_PAGECACHE
33
34#define MIN_POOL_WRITE (32)
35#define MIN_POOL_COMMIT (4)
36
37/*
38 * Local function declarations
39 */
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 Myklebustc9d8f892008-04-15 16:56:39 -040051struct nfs_write_data *nfs_commitdata_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);
Andy Adamson5f7dbd52009-04-01 09:22:05 -040058 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
60 return p;
61}
62
Trond Myklebust5e4424a2008-02-25 21:53:49 -080063void nfs_commit_free(struct nfs_write_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Chuck Lever40859d72005-11-30 18:09:02 -050065 if (p && (p->pagevec != &p->page_array[0]))
66 kfree(p->pagevec);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 mempool_free(p, nfs_commit_mempool);
68}
69
Trond Myklebust8d5658c2007-04-10 09:26:35 -040070struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050071{
Christoph Lametere6b4f8d2006-12-06 20:33:14 -080072 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050073
74 if (p) {
75 memset(p, 0, sizeof(*p));
76 INIT_LIST_HEAD(&p->pages);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070077 p->npages = pagecount;
Andy Adamson5f7dbd52009-04-01 09:22:05 -040078 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040079 if (pagecount <= ARRAY_SIZE(p->page_array))
80 p->pagevec = p->page_array;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050081 else {
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040082 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
83 if (!p->pagevec) {
Trond Myklebust3feb2d42006-03-20 13:44:37 -050084 mempool_free(p, nfs_wdata_mempool);
85 p = NULL;
86 }
87 }
88 }
89 return p;
90}
91
Trond Myklebust5e4424a2008-02-25 21:53:49 -080092static void nfs_writedata_free(struct nfs_write_data *p)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050093{
94 if (p && (p->pagevec != &p->page_array[0]))
95 kfree(p->pagevec);
96 mempool_free(p, nfs_wdata_mempool);
97}
98
Trond Myklebust383ba712008-02-19 20:04:20 -050099void nfs_writedata_release(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Trond Myklebust383ba712008-02-19 20:04:20 -0500101 struct nfs_write_data *wdata = data;
102
103 put_nfs_open_context(wdata->args.context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 nfs_writedata_free(wdata);
105}
106
Trond Myklebust7b159fc2007-07-25 14:09:54 -0400107static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
108{
109 ctx->error = error;
110 smp_wmb();
111 set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
112}
113
Trond Myklebust277459d2006-12-05 00:35:35 -0500114static struct nfs_page *nfs_page_find_request_locked(struct page *page)
115{
116 struct nfs_page *req = NULL;
117
118 if (PagePrivate(page)) {
119 req = (struct nfs_page *)page_private(page);
120 if (req != NULL)
Trond Myklebustc03b4022007-06-17 13:26:38 -0400121 kref_get(&req->wb_kref);
Trond Myklebust277459d2006-12-05 00:35:35 -0500122 }
123 return req;
124}
125
126static struct nfs_page *nfs_page_find_request(struct page *page)
127{
Trond Myklebust587142f2007-07-02 09:57:54 -0400128 struct inode *inode = page->mapping->host;
Trond Myklebust277459d2006-12-05 00:35:35 -0500129 struct nfs_page *req = NULL;
Trond Myklebust277459d2006-12-05 00:35:35 -0500130
Trond Myklebust587142f2007-07-02 09:57:54 -0400131 spin_lock(&inode->i_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500132 req = nfs_page_find_request_locked(page);
Trond Myklebust587142f2007-07-02 09:57:54 -0400133 spin_unlock(&inode->i_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500134 return req;
135}
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/* Adjust the file length if we're writing beyond the end */
138static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
139{
140 struct inode *inode = page->mapping->host;
Trond Myklebusta3d01452008-06-11 12:21:19 -0400141 loff_t end, i_size;
142 pgoff_t end_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Trond Myklebusta3d01452008-06-11 12:21:19 -0400144 spin_lock(&inode->i_lock);
145 i_size = i_size_read(inode);
146 end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (i_size > 0 && page->index < end_index)
Trond Myklebusta3d01452008-06-11 12:21:19 -0400148 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
150 if (i_size >= end)
Trond Myklebusta3d01452008-06-11 12:21:19 -0400151 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 i_size_write(inode, end);
Trond Myklebusta3d01452008-06-11 12:21:19 -0400153 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
154out:
155 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Trond Myklebusta301b772007-02-06 11:07:15 -0800158/* A writeback failed: mark the page as bad, and invalidate the page cache */
159static void nfs_set_pageerror(struct page *page)
160{
161 SetPageError(page);
162 nfs_zap_mapping(page->mapping->host, page->mapping);
163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/* We can set the PG_uptodate flag if we see that a write request
166 * covers the full page.
167 */
168static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
169{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (PageUptodate(page))
171 return;
172 if (base != 0)
173 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500174 if (count != nfs_page_length(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500176 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static int wb_priority(struct writeback_control *wbc)
180{
181 if (wbc->for_reclaim)
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400182 return FLUSH_HIGHPRI | FLUSH_STABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (wbc->for_kupdate)
184 return FLUSH_LOWPRI;
185 return 0;
186}
187
188/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800189 * NFS congestion control
190 */
191
192int nfs_congestion_kb;
193
194#define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
195#define NFS_CONGESTION_OFF_THRESH \
196 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
197
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400198static int nfs_set_page_writeback(struct page *page)
Peter Zijlstra89a09142007-03-16 13:38:26 -0800199{
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400200 int ret = test_set_page_writeback(page);
201
202 if (!ret) {
Peter Zijlstra89a09142007-03-16 13:38:26 -0800203 struct inode *inode = page->mapping->host;
204 struct nfs_server *nfss = NFS_SERVER(inode);
205
Peter Zijlstra277866a2007-05-08 00:35:12 -0700206 if (atomic_long_inc_return(&nfss->writeback) >
Jens Axboe8aa7e842009-07-09 14:52:32 +0200207 NFS_CONGESTION_ON_THRESH) {
208 set_bdi_congested(&nfss->backing_dev_info,
209 BLK_RW_ASYNC);
210 }
Peter Zijlstra89a09142007-03-16 13:38:26 -0800211 }
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400212 return ret;
Peter Zijlstra89a09142007-03-16 13:38:26 -0800213}
214
215static void nfs_end_page_writeback(struct page *page)
216{
217 struct inode *inode = page->mapping->host;
218 struct nfs_server *nfss = NFS_SERVER(inode);
219
220 end_page_writeback(page);
Peter Zijlstrac4dc4be2007-10-16 23:25:41 -0700221 if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
Jens Axboe8aa7e842009-07-09 14:52:32 +0200222 clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
Peter Zijlstra89a09142007-03-16 13:38:26 -0800223}
224
Trond Myklebust074cc1d2009-08-10 08:54:13 -0400225static struct nfs_page *nfs_find_and_lock_request(struct page *page)
Trond Myklebuste261f512006-12-05 00:35:41 -0500226{
Trond Myklebust587142f2007-07-02 09:57:54 -0400227 struct inode *inode = page->mapping->host;
Trond Myklebuste261f512006-12-05 00:35:41 -0500228 struct nfs_page *req;
Trond Myklebuste261f512006-12-05 00:35:41 -0500229 int ret;
230
Trond Myklebust587142f2007-07-02 09:57:54 -0400231 spin_lock(&inode->i_lock);
Trond Myklebust074cc1d2009-08-10 08:54:13 -0400232 for (;;) {
Trond Myklebuste261f512006-12-05 00:35:41 -0500233 req = nfs_page_find_request_locked(page);
Trond Myklebust074cc1d2009-08-10 08:54:13 -0400234 if (req == NULL)
235 break;
Trond Myklebustacee4782008-01-22 17:13:07 -0500236 if (nfs_set_page_tag_locked(req))
Trond Myklebuste261f512006-12-05 00:35:41 -0500237 break;
238 /* Note: If we hold the page lock, as is the case in nfs_writepage,
Trond Myklebustacee4782008-01-22 17:13:07 -0500239 * then the call to nfs_set_page_tag_locked() will always
Trond Myklebuste261f512006-12-05 00:35:41 -0500240 * succeed provided that someone hasn't already marked the
241 * request as dirty (in which case we don't care).
242 */
Trond Myklebust587142f2007-07-02 09:57:54 -0400243 spin_unlock(&inode->i_lock);
Trond Myklebuste261f512006-12-05 00:35:41 -0500244 ret = nfs_wait_on_request(req);
245 nfs_release_request(req);
246 if (ret != 0)
Trond Myklebust074cc1d2009-08-10 08:54:13 -0400247 return ERR_PTR(ret);
Trond Myklebust587142f2007-07-02 09:57:54 -0400248 spin_lock(&inode->i_lock);
Trond Myklebuste261f512006-12-05 00:35:41 -0500249 }
Trond Myklebust587142f2007-07-02 09:57:54 -0400250 spin_unlock(&inode->i_lock);
Trond Myklebust074cc1d2009-08-10 08:54:13 -0400251 return req;
252}
253
254/*
255 * Find an associated nfs write request, and prepare to flush it out
256 * May return an error if the user signalled nfs_wait_on_request().
257 */
258static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
259 struct page *page)
260{
261 struct nfs_page *req;
262 int ret = 0;
263
264 req = nfs_find_and_lock_request(page);
265 if (!req)
266 goto out;
267 ret = PTR_ERR(req);
268 if (IS_ERR(req))
269 goto out;
270
271 ret = nfs_set_page_writeback(page);
272 BUG_ON(ret != 0);
273 BUG_ON(test_bit(PG_CLEAN, &req->wb_flags));
274
Fred Isamanf8512ad2008-03-19 11:24:39 -0400275 if (!nfs_pageio_add_request(pgio, req)) {
276 nfs_redirty_request(req);
Trond Myklebust074cc1d2009-08-10 08:54:13 -0400277 ret = pgio->pg_error;
Fred Isamanf8512ad2008-03-19 11:24:39 -0400278 }
Trond Myklebust074cc1d2009-08-10 08:54:13 -0400279out:
280 return ret;
Trond Myklebuste261f512006-12-05 00:35:41 -0500281}
282
Trond Myklebustf758c8852007-07-22 19:27:32 -0400283static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
284{
285 struct inode *inode = page->mapping->host;
286
287 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
288 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
289
290 nfs_pageio_cond_complete(pgio, page->index);
291 return nfs_page_async_flush(pgio, page);
292}
293
Trond Myklebuste261f512006-12-05 00:35:41 -0500294/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 * Write an mmapped page to the server.
296 */
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500297static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Trond Myklebustf758c8852007-07-22 19:27:32 -0400299 struct nfs_pageio_descriptor pgio;
Trond Myklebuste261f512006-12-05 00:35:41 -0500300 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Trond Myklebustf758c8852007-07-22 19:27:32 -0400302 nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
303 err = nfs_do_writepage(page, wbc, &pgio);
304 nfs_pageio_complete(&pgio);
305 if (err < 0)
306 return err;
307 if (pgio.pg_error < 0)
308 return pgio.pg_error;
309 return 0;
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500310}
311
312int nfs_writepage(struct page *page, struct writeback_control *wbc)
313{
Trond Myklebustf758c8852007-07-22 19:27:32 -0400314 int ret;
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500315
Trond Myklebustf758c8852007-07-22 19:27:32 -0400316 ret = nfs_writepage_locked(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 unlock_page(page);
Trond Myklebustf758c8852007-07-22 19:27:32 -0400318 return ret;
319}
320
321static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
322{
323 int ret;
324
325 ret = nfs_do_writepage(page, wbc, data);
326 unlock_page(page);
327 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
331{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 struct inode *inode = mapping->host;
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400333 unsigned long *bitlock = &NFS_I(inode)->flags;
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400334 struct nfs_pageio_descriptor pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 int err;
336
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400337 /* Stop dirtying of new pages while we sync */
338 err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
339 nfs_wait_bit_killable, TASK_KILLABLE);
340 if (err)
341 goto out_err;
342
Chuck Lever91d5b472006-03-20 13:44:14 -0500343 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
344
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400345 nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
Trond Myklebustf758c8852007-07-22 19:27:32 -0400346 err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400347 nfs_pageio_complete(&pgio);
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400348
349 clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
350 smp_mb__after_clear_bit();
351 wake_up_bit(bitlock, NFS_INO_FLUSHING);
352
Trond Myklebustf758c8852007-07-22 19:27:32 -0400353 if (err < 0)
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400354 goto out_err;
355 err = pgio.pg_error;
356 if (err < 0)
357 goto out_err;
Trond Myklebustc63c7b02007-04-02 19:29:52 -0400358 return 0;
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400359out_err:
360 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363/*
364 * Insert a write request into an inode
365 */
Trond Myklebuste7d39062008-06-13 12:12:32 -0400366static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 struct nfs_inode *nfsi = NFS_I(inode);
369 int error;
370
Trond Myklebuste7d39062008-06-13 12:12:32 -0400371 error = radix_tree_preload(GFP_NOFS);
372 if (error != 0)
373 goto out;
374
375 /* Lock the request! */
376 nfs_lock_request_dontget(req);
377
378 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
Nick Piggin27852592008-02-04 23:48:37 -0800380 BUG_ON(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (!nfsi->npages) {
382 igrab(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (nfs_have_delegation(inode, FMODE_WRITE))
384 nfsi->change_attr++;
385 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500386 SetPagePrivate(req->wb_page);
Trond Myklebust277459d2006-12-05 00:35:35 -0500387 set_page_private(req->wb_page, (unsigned long)req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 nfsi->npages++;
Trond Myklebustc03b4022007-06-17 13:26:38 -0400389 kref_get(&req->wb_kref);
Nick Piggin27852592008-02-04 23:48:37 -0800390 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
391 NFS_PAGE_TAG_LOCKED);
Trond Myklebuste7d39062008-06-13 12:12:32 -0400392 spin_unlock(&inode->i_lock);
393 radix_tree_preload_end();
394out:
395 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
398/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800399 * Remove a write request from an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 */
401static void nfs_inode_remove_request(struct nfs_page *req)
402{
Trond Myklebust88be9f92007-06-05 10:42:27 -0400403 struct inode *inode = req->wb_context->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 struct nfs_inode *nfsi = NFS_I(inode);
405
406 BUG_ON (!NFS_WBACK_BUSY(req));
407
Trond Myklebust587142f2007-07-02 09:57:54 -0400408 spin_lock(&inode->i_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500409 set_page_private(req->wb_page, 0);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500410 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
412 nfsi->npages--;
413 if (!nfsi->npages) {
Trond Myklebust587142f2007-07-02 09:57:54 -0400414 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 iput(inode);
416 } else
Trond Myklebust587142f2007-07-02 09:57:54 -0400417 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 nfs_clear_request(req);
419 nfs_release_request(req);
420}
421
Trond Myklebust61822ab2006-12-05 00:35:42 -0500422static void
Fred6d884e82008-03-19 11:24:38 -0400423nfs_mark_request_dirty(struct nfs_page *req)
Trond Myklebust61822ab2006-12-05 00:35:42 -0500424{
Trond Myklebust61822ab2006-12-05 00:35:42 -0500425 __set_page_dirty_nobuffers(req->wb_page);
426}
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
429/*
430 * Add a request to the inode's commit list.
431 */
432static void
433nfs_mark_request_commit(struct nfs_page *req)
434{
Trond Myklebust88be9f92007-06-05 10:42:27 -0400435 struct inode *inode = req->wb_context->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 struct nfs_inode *nfsi = NFS_I(inode);
437
Trond Myklebust587142f2007-07-02 09:57:54 -0400438 spin_lock(&inode->i_lock);
Trond Myklebuste468bae2008-06-13 13:25:22 -0400439 set_bit(PG_CLEAN, &(req)->wb_flags);
Trond Myklebust5c369682007-06-17 15:27:42 -0400440 radix_tree_tag_set(&nfsi->nfs_page_tree,
441 req->wb_index,
442 NFS_PAGE_TAG_COMMIT);
Trond Myklebust587142f2007-07-02 09:57:54 -0400443 spin_unlock(&inode->i_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700444 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -0700445 inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
Trond Myklebusta1803042006-12-05 00:45:19 -0500446 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400448
Trond Myklebuste468bae2008-06-13 13:25:22 -0400449static int
450nfs_clear_request_commit(struct nfs_page *req)
451{
452 struct page *page = req->wb_page;
453
454 if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) {
455 dec_zone_page_state(page, NR_UNSTABLE_NFS);
456 dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE);
457 return 1;
458 }
459 return 0;
460}
461
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400462static inline
463int nfs_write_need_commit(struct nfs_write_data *data)
464{
465 return data->verf.committed != NFS_FILE_SYNC;
466}
467
468static inline
469int nfs_reschedule_unstable_write(struct nfs_page *req)
470{
Trond Myklebuste468bae2008-06-13 13:25:22 -0400471 if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) {
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400472 nfs_mark_request_commit(req);
473 return 1;
474 }
475 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
Fred6d884e82008-03-19 11:24:38 -0400476 nfs_mark_request_dirty(req);
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400477 return 1;
478 }
479 return 0;
480}
481#else
482static inline void
483nfs_mark_request_commit(struct nfs_page *req)
484{
485}
486
Trond Myklebuste468bae2008-06-13 13:25:22 -0400487static inline int
488nfs_clear_request_commit(struct nfs_page *req)
489{
490 return 0;
491}
492
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400493static inline
494int nfs_write_need_commit(struct nfs_write_data *data)
495{
496 return 0;
497}
498
499static inline
500int nfs_reschedule_unstable_write(struct nfs_page *req)
501{
502 return 0;
503}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504#endif
505
506/*
507 * Wait for a request to complete.
508 *
Matthew Wilcox150030b2007-12-06 16:24:39 -0500509 * Interruptible by fatal signals only.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 */
Trond Myklebustca52fec2007-04-17 17:22:13 -0400511static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 struct nfs_inode *nfsi = NFS_I(inode);
514 struct nfs_page *req;
Trond Myklebustca52fec2007-04-17 17:22:13 -0400515 pgoff_t idx_end, next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 unsigned int res = 0;
517 int error;
518
519 if (npages == 0)
520 idx_end = ~0;
521 else
522 idx_end = idx_start + npages - 1;
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 next = idx_start;
Trond Myklebust9fd367f2007-06-17 15:10:24 -0400525 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 -0700526 if (req->wb_index > idx_end)
527 break;
528
529 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000530 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Trond Myklebustc03b4022007-06-17 13:26:38 -0400532 kref_get(&req->wb_kref);
Trond Myklebust587142f2007-07-02 09:57:54 -0400533 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 error = nfs_wait_on_request(req);
535 nfs_release_request(req);
Trond Myklebust587142f2007-07-02 09:57:54 -0400536 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (error < 0)
538 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 res++;
540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return res;
542}
543
Trond Myklebust83715ad2006-07-05 13:17:12 -0400544static void nfs_cancel_commit_list(struct list_head *head)
545{
546 struct nfs_page *req;
547
548 while(!list_empty(head)) {
549 req = nfs_list_entry(head->next);
550 nfs_list_remove_request(req);
Trond Myklebuste468bae2008-06-13 13:25:22 -0400551 nfs_clear_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400552 nfs_inode_remove_request(req);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700553 nfs_unlock_request(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400554 }
555}
556
Trond Myklebust47c62562009-03-16 08:13:41 -0400557#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebustfb8a1f12009-03-11 14:10:29 -0400558static int
559nfs_need_commit(struct nfs_inode *nfsi)
560{
561 return radix_tree_tagged(&nfsi->nfs_page_tree, NFS_PAGE_TAG_COMMIT);
562}
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564/*
565 * nfs_scan_commit - Scan an inode for commit requests
566 * @inode: NFS inode to scan
567 * @dst: destination list
568 * @idx_start: lower bound of page->index to scan.
569 * @npages: idx_start + npages sets the upper bound to scan.
570 *
571 * Moves requests from the inode's 'commit' request list.
572 * The requests are *not* checked to ensure that they form a contiguous set.
573 */
574static int
Trond Myklebustca52fec2007-04-17 17:22:13 -0400575nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000578
Trond Myklebustfb8a1f12009-03-11 14:10:29 -0400579 if (!nfs_need_commit(nfsi))
580 return 0;
581
582 return nfs_scan_list(nfsi, dst, idx_start, npages, NFS_PAGE_TAG_COMMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500584#else
Trond Myklebustfb8a1f12009-03-11 14:10:29 -0400585static inline int nfs_need_commit(struct nfs_inode *nfsi)
586{
587 return 0;
588}
589
Trond Myklebustca52fec2007-04-17 17:22:13 -0400590static 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 -0500591{
592 return 0;
593}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594#endif
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596/*
Trond Myklebuste7d39062008-06-13 12:12:32 -0400597 * Search for an existing write request, and attempt to update
598 * it to reflect a new dirty region on a given page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 *
Trond Myklebuste7d39062008-06-13 12:12:32 -0400600 * If the attempt fails, then the existing request is flushed out
601 * to disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 */
Trond Myklebuste7d39062008-06-13 12:12:32 -0400603static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
604 struct page *page,
605 unsigned int offset,
606 unsigned int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Trond Myklebuste7d39062008-06-13 12:12:32 -0400608 struct nfs_page *req;
609 unsigned int rqend;
610 unsigned int end;
611 int error;
612
613 if (!PagePrivate(page))
614 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 end = offset + bytes;
Trond Myklebuste7d39062008-06-13 12:12:32 -0400617 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 for (;;) {
Trond Myklebust277459d2006-12-05 00:35:35 -0500620 req = nfs_page_find_request_locked(page);
Trond Myklebuste7d39062008-06-13 12:12:32 -0400621 if (req == NULL)
622 goto out_unlock;
Trond Myklebust277459d2006-12-05 00:35:35 -0500623
Trond Myklebuste7d39062008-06-13 12:12:32 -0400624 rqend = req->wb_offset + req->wb_bytes;
625 /*
626 * Tell the caller to flush out the request if
627 * the offsets are non-contiguous.
628 * Note: nfs_flush_incompatible() will already
629 * have flushed out requests having wrong owners.
630 */
Trond Myklebuste468bae2008-06-13 13:25:22 -0400631 if (offset > rqend
Trond Myklebuste7d39062008-06-13 12:12:32 -0400632 || end < req->wb_offset)
633 goto out_flushme;
634
635 if (nfs_set_page_tag_locked(req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Trond Myklebuste7d39062008-06-13 12:12:32 -0400638 /* The request is locked, so wait and then retry */
Trond Myklebust587142f2007-07-02 09:57:54 -0400639 spin_unlock(&inode->i_lock);
Trond Myklebuste7d39062008-06-13 12:12:32 -0400640 error = nfs_wait_on_request(req);
641 nfs_release_request(req);
642 if (error != 0)
643 goto out_err;
644 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646
Trond Myklebuste468bae2008-06-13 13:25:22 -0400647 if (nfs_clear_request_commit(req))
648 radix_tree_tag_clear(&NFS_I(inode)->nfs_page_tree,
649 req->wb_index, NFS_PAGE_TAG_COMMIT);
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 /* Okay, the request matches. Update the region */
652 if (offset < req->wb_offset) {
653 req->wb_offset = offset;
654 req->wb_pgbase = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (end > rqend)
657 req->wb_bytes = end - req->wb_offset;
Trond Myklebuste7d39062008-06-13 12:12:32 -0400658 else
659 req->wb_bytes = rqend - req->wb_offset;
660out_unlock:
661 spin_unlock(&inode->i_lock);
662 return req;
663out_flushme:
664 spin_unlock(&inode->i_lock);
665 nfs_release_request(req);
666 error = nfs_wb_page(inode, page);
667out_err:
668 return ERR_PTR(error);
669}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Trond Myklebuste7d39062008-06-13 12:12:32 -0400671/*
672 * Try to update an existing write request, or create one if there is none.
673 *
674 * Note: Should always be called with the Page Lock held to prevent races
675 * if we have to add a new request. Also assumes that the caller has
676 * already called nfs_flush_incompatible() if necessary.
677 */
678static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
679 struct page *page, unsigned int offset, unsigned int bytes)
680{
681 struct inode *inode = page->mapping->host;
682 struct nfs_page *req;
683 int error;
684
685 req = nfs_try_to_update_request(inode, page, offset, bytes);
686 if (req != NULL)
687 goto out;
688 req = nfs_create_request(ctx, inode, page, offset, bytes);
689 if (IS_ERR(req))
690 goto out;
691 error = nfs_inode_add_request(inode, req);
692 if (error != 0) {
693 nfs_release_request(req);
694 req = ERR_PTR(error);
695 }
Trond Myklebustefc91ed2008-06-10 18:31:00 -0400696out:
Trond Myklebust61e930a2007-10-18 17:08:05 -0400697 return req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
Trond Myklebuste7d39062008-06-13 12:12:32 -0400700static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
701 unsigned int offset, unsigned int count)
702{
703 struct nfs_page *req;
704
705 req = nfs_setup_write_request(ctx, page, offset, count);
706 if (IS_ERR(req))
707 return PTR_ERR(req);
708 /* Update file length */
709 nfs_grow_file(page, offset, count);
710 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
711 nfs_clear_page_tag_locked(req);
712 return 0;
713}
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715int nfs_flush_incompatible(struct file *file, struct page *page)
716{
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400717 struct nfs_open_context *ctx = nfs_file_open_context(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 struct nfs_page *req;
Trond Myklebust1a545332006-12-05 00:35:40 -0500719 int do_flush, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 /*
721 * Look for a request corresponding to this page. If there
722 * is one, and it belongs to another file, we flush it out
723 * before we try to copy anything into the page. Do this
724 * due to the lack of an ACCESS-type call in NFSv2.
725 * Also do the same if we find a request from an existing
726 * dropped page.
727 */
Trond Myklebust1a545332006-12-05 00:35:40 -0500728 do {
729 req = nfs_page_find_request(page);
730 if (req == NULL)
731 return 0;
Trond Myklebuste468bae2008-06-13 13:25:22 -0400732 do_flush = req->wb_page != page || req->wb_context != ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 nfs_release_request(req);
Trond Myklebust1a545332006-12-05 00:35:40 -0500734 if (!do_flush)
735 return 0;
736 status = nfs_wb_page(page->mapping->host, page);
737 } while (status == 0);
738 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
741/*
Trond Myklebust5d47a352008-02-07 17:24:07 -0500742 * If the page cache is marked as unsafe or invalid, then we can't rely on
743 * the PageUptodate() flag. In this case, we will need to turn off
744 * write optimisations that depend on the page contents being correct.
745 */
746static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
747{
748 return PageUptodate(page) &&
749 !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
750}
751
752/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 * Update and possibly write a cached page of an NFS file.
754 *
755 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
756 * things with a page scheduled for an RPC call (e.g. invalidate it).
757 */
758int nfs_updatepage(struct file *file, struct page *page,
759 unsigned int offset, unsigned int count)
760{
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400761 struct nfs_open_context *ctx = nfs_file_open_context(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 int status = 0;
764
Chuck Lever91d5b472006-03-20 13:44:14 -0500765 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
766
Chuck Lever48186c72008-06-11 17:56:05 -0400767 dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n",
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800768 file->f_path.dentry->d_parent->d_name.name,
769 file->f_path.dentry->d_name.name, count,
Chuck Lever48186c72008-06-11 17:56:05 -0400770 (long long)(page_offset(page) + offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 /* If we're not using byte range locks, and we know the page
Trond Myklebust5d47a352008-02-07 17:24:07 -0500773 * is up to date, it may be more efficient to extend the write
774 * to cover the entire page in order to avoid fragmentation
775 * inefficiencies.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 */
Trond Myklebust5d47a352008-02-07 17:24:07 -0500777 if (nfs_write_pageuptodate(page, inode) &&
778 inode->i_flock == NULL &&
Trond Myklebust4b5621f2008-02-25 15:56:29 -0800779 !(file->f_flags & O_SYNC)) {
Trond Myklebust49a70f22006-12-05 00:35:38 -0500780 count = max(count + offset, nfs_page_length(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
783
Trond Myklebuste21195a2006-12-05 00:35:39 -0500784 status = nfs_writepage_setup(ctx, page, offset, count);
Trond Myklebust03fa9e82008-06-05 16:02:35 -0400785 if (status < 0)
786 nfs_set_pageerror(page);
787 else
788 __set_page_dirty_nobuffers(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Chuck Lever48186c72008-06-11 17:56:05 -0400790 dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 status, (long long)i_size_read(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return status;
793}
794
795static void nfs_writepage_release(struct nfs_page *req)
796{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Trond Myklebust7e5f6142008-05-25 12:59:19 -0400798 if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req)) {
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400799 nfs_end_page_writeback(req->wb_page);
800 nfs_inode_remove_request(req);
801 } else
802 nfs_end_page_writeback(req->wb_page);
Trond Myklebust9fd367f2007-06-17 15:10:24 -0400803 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
Trond Myklebust3ff75762007-07-14 15:40:00 -0400806static int flush_task_priority(int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
809 case FLUSH_HIGHPRI:
810 return RPC_PRIORITY_HIGH;
811 case FLUSH_LOWPRI:
812 return RPC_PRIORITY_LOW;
813 }
814 return RPC_PRIORITY_NORMAL;
815}
816
817/*
818 * Set up the argument/result storage required for the RPC call.
819 */
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400820static int nfs_write_rpcsetup(struct nfs_page *req,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500822 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 unsigned int count, unsigned int offset,
824 int how)
825{
Trond Myklebust84115e12007-07-14 15:39:59 -0400826 struct inode *inode = req->wb_context->path.dentry->d_inode;
827 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
Trond Myklebust3ff75762007-07-14 15:40:00 -0400828 int priority = flush_task_priority(how);
Trond Myklebust07737692007-10-25 18:42:54 -0400829 struct rpc_task *task;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400830 struct rpc_message msg = {
831 .rpc_argp = &data->args,
832 .rpc_resp = &data->res,
833 .rpc_cred = req->wb_context->cred,
834 };
Trond Myklebust84115e12007-07-14 15:39:59 -0400835 struct rpc_task_setup task_setup_data = {
836 .rpc_client = NFS_CLIENT(inode),
Trond Myklebust07737692007-10-25 18:42:54 -0400837 .task = &data->task,
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400838 .rpc_message = &msg,
Trond Myklebust84115e12007-07-14 15:39:59 -0400839 .callback_ops = call_ops,
840 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -0500841 .workqueue = nfsiod_workqueue,
Trond Myklebust84115e12007-07-14 15:39:59 -0400842 .flags = flags,
Trond Myklebust3ff75762007-07-14 15:40:00 -0400843 .priority = priority,
Trond Myklebust84115e12007-07-14 15:39:59 -0400844 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 /* Set up the RPC argument and reply structs
847 * NB: take care not to mess about with data->commit et al. */
848
849 data->req = req;
Trond Myklebust88be9f92007-06-05 10:42:27 -0400850 data->inode = inode = req->wb_context->path.dentry->d_inode;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400851 data->cred = msg.rpc_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 data->args.fh = NFS_FH(inode);
854 data->args.offset = req_offset(req) + offset;
855 data->args.pgbase = req->wb_pgbase + offset;
856 data->args.pages = data->pagevec;
857 data->args.count = count;
Trond Myklebust383ba712008-02-19 20:04:20 -0500858 data->args.context = get_nfs_open_context(req->wb_context);
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400859 data->args.stable = NFS_UNSTABLE;
860 if (how & FLUSH_STABLE) {
861 data->args.stable = NFS_DATA_SYNC;
Trond Myklebustfb8a1f12009-03-11 14:10:29 -0400862 if (!nfs_need_commit(NFS_I(inode)))
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400863 data->args.stable = NFS_FILE_SYNC;
864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 data->res.fattr = &data->fattr;
867 data->res.count = count;
868 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400869 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Trond Myklebust788e7a82006-03-20 13:44:27 -0500871 /* Set up the initial task struct. */
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400872 NFS_PROTO(inode)->write_setup(data, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Chuck Levera3f565b2007-01-31 12:14:01 -0500874 dprintk("NFS: %5u initiated write call "
Chuck Lever48186c72008-06-11 17:56:05 -0400875 "(req %s/%lld, %u bytes @ offset %llu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500876 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 inode->i_sb->s_id,
878 (long long)NFS_FILEID(inode),
879 count,
880 (unsigned long long)data->args.offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Trond Myklebust07737692007-10-25 18:42:54 -0400882 task = rpc_run_task(&task_setup_data);
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400883 if (IS_ERR(task))
884 return PTR_ERR(task);
885 rpc_put_task(task);
886 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
Fred6d884e82008-03-19 11:24:38 -0400889/* If a nfs_flush_* function fails, it should remove reqs from @head and
890 * call this on each, which will prepare them to be retried on next
891 * writeback using standard nfs.
892 */
893static void nfs_redirty_request(struct nfs_page *req)
894{
895 nfs_mark_request_dirty(req);
896 nfs_end_page_writeback(req->wb_page);
897 nfs_clear_page_tag_locked(req);
898}
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900/*
901 * Generate multiple small requests to write out a single
902 * contiguous dirty area on one page.
903 */
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400904static 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 -0700905{
906 struct nfs_page *req = nfs_list_entry(head->next);
907 struct page *page = req->wb_page;
908 struct nfs_write_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700909 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
910 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 int requests = 0;
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400912 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 LIST_HEAD(list);
914
915 nfs_list_remove_request(req);
916
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400917 nbytes = count;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700918 do {
919 size_t len = min(nbytes, wsize);
920
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400921 data = nfs_writedata_alloc(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (!data)
923 goto out_bad;
924 list_add(&data->pages, &list);
925 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700926 nbytes -= len;
927 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 atomic_set(&req->wb_complete, requests);
929
930 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 offset = 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400932 nbytes = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 do {
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400934 int ret2;
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 data = list_entry(list.next, struct nfs_write_data, pages);
937 list_del_init(&data->pages);
938
939 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400941 if (nbytes < wsize)
942 wsize = nbytes;
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400943 ret2 = nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400944 wsize, offset, how);
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400945 if (ret == 0)
946 ret = ret2;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400947 offset += wsize;
948 nbytes -= wsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 } while (nbytes != 0);
950
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400951 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953out_bad:
954 while (!list_empty(&list)) {
955 data = list_entry(list.next, struct nfs_write_data, pages);
956 list_del(&data->pages);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500957 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
Trond Myklebust61822ab2006-12-05 00:35:42 -0500959 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return -ENOMEM;
961}
962
963/*
964 * Create an RPC task for the given write request and kick it.
965 * The page must have been locked by the caller.
966 *
967 * It may happen that the page we're passed is not marked dirty.
968 * This is the case if nfs_updatepage detects a conflicting request
969 * that has been written but not committed.
970 */
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400971static 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 -0700972{
973 struct nfs_page *req;
974 struct page **pages;
975 struct nfs_write_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400977 data = nfs_writedata_alloc(npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (!data)
979 goto out_bad;
980
981 pages = data->pagevec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 while (!list_empty(head)) {
983 req = nfs_list_entry(head->next);
984 nfs_list_remove_request(req);
985 nfs_list_add_request(req, &data->pages);
986 ClearPageError(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 *pages++ = req->wb_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989 req = nfs_list_entry(data->pages.next);
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 /* Set up the argument struct */
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400992 return nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 out_bad:
994 while (!list_empty(head)) {
Trond Myklebust10afec92007-05-14 17:16:04 -0400995 req = nfs_list_entry(head->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 nfs_list_remove_request(req);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500997 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999 return -ENOMEM;
1000}
1001
Trond Myklebustc63c7b02007-04-02 19:29:52 -04001002static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1003 struct inode *inode, int ioflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Chuck Leverbf4285e2007-12-20 14:54:57 -05001005 size_t wsize = NFS_SERVER(inode)->wsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Trond Myklebustbcb71bb2007-04-02 18:48:28 -04001007 if (wsize < PAGE_CACHE_SIZE)
Trond Myklebustc63c7b02007-04-02 19:29:52 -04001008 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -04001009 else
Trond Myklebustc63c7b02007-04-02 19:29:52 -04001010 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
1013/*
1014 * Handle a write reply that flushed part of a page.
1015 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001016static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001018 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Chuck Lever48186c72008-06-11 17:56:05 -04001020 dprintk("NFS: %5u write(%s/%lld %d@%lld)",
1021 task->tk_pid,
1022 data->req->wb_context->path.dentry->d_inode->i_sb->s_id,
1023 (long long)
1024 NFS_FILEID(data->req->wb_context->path.dentry->d_inode),
1025 data->req->wb_bytes, (long long)req_offset(data->req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001027 nfs_writeback_done(task, data);
1028}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001029
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001030static void nfs_writeback_release_partial(void *calldata)
1031{
1032 struct nfs_write_data *data = calldata;
1033 struct nfs_page *req = data->req;
1034 struct page *page = req->wb_page;
1035 int status = data->task.tk_status;
1036
1037 if (status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -08001038 nfs_set_pageerror(page);
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001039 nfs_context_set_write_error(req->wb_context, status);
1040 dprintk(", error = %d\n", status);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001041 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001044 if (nfs_write_need_commit(data)) {
Trond Myklebust587142f2007-07-02 09:57:54 -04001045 struct inode *inode = page->mapping->host;
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001046
Trond Myklebust587142f2007-07-02 09:57:54 -04001047 spin_lock(&inode->i_lock);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001048 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
1049 /* Do nothing we need to resend the writes */
1050 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1051 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1052 dprintk(" defer commit\n");
1053 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1054 set_bit(PG_NEED_RESCHED, &req->wb_flags);
1055 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
1056 dprintk(" server reboot detected\n");
1057 }
Trond Myklebust587142f2007-07-02 09:57:54 -04001058 spin_unlock(&inode->i_lock);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001059 } else
1060 dprintk(" OK\n");
1061
1062out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (atomic_dec_and_test(&req->wb_complete))
1064 nfs_writepage_release(req);
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001065 nfs_writedata_release(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
Andy Adamsondef6ed72009-04-01 09:22:26 -04001068#if defined(CONFIG_NFS_V4_1)
1069void nfs_write_prepare(struct rpc_task *task, void *calldata)
1070{
1071 struct nfs_write_data *data = calldata;
1072 struct nfs_client *clp = (NFS_SERVER(data->inode))->nfs_client;
1073
1074 if (nfs4_setup_sequence(clp, &data->args.seq_args,
1075 &data->res.seq_res, 1, task))
1076 return;
1077 rpc_call_start(task);
1078}
1079#endif /* CONFIG_NFS_V4_1 */
1080
Trond Myklebust788e7a82006-03-20 13:44:27 -05001081static const struct rpc_call_ops nfs_write_partial_ops = {
Andy Adamsondef6ed72009-04-01 09:22:26 -04001082#if defined(CONFIG_NFS_V4_1)
1083 .rpc_call_prepare = nfs_write_prepare,
1084#endif /* CONFIG_NFS_V4_1 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001085 .rpc_call_done = nfs_writeback_done_partial,
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001086 .rpc_release = nfs_writeback_release_partial,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001087};
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089/*
1090 * Handle a write reply that flushes a whole page.
1091 *
1092 * FIXME: There is an inherent race with invalidate_inode_pages and
1093 * writebacks since the page->count is kept > 1 for as long
1094 * as the page has a write request pending.
1095 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001096static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001098 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001100 nfs_writeback_done(task, data);
1101}
1102
1103static void nfs_writeback_release_full(void *calldata)
1104{
1105 struct nfs_write_data *data = calldata;
1106 int status = data->task.tk_status;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 /* Update attributes as result of writeback. */
1109 while (!list_empty(&data->pages)) {
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001110 struct nfs_page *req = nfs_list_entry(data->pages.next);
1111 struct page *page = req->wb_page;
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 nfs_list_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Chuck Lever48186c72008-06-11 17:56:05 -04001115 dprintk("NFS: %5u write (%s/%lld %d@%lld)",
1116 data->task.tk_pid,
Trond Myklebust88be9f92007-06-05 10:42:27 -04001117 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1118 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 req->wb_bytes,
1120 (long long)req_offset(req));
1121
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001122 if (status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -08001123 nfs_set_pageerror(page);
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001124 nfs_context_set_write_error(req->wb_context, status);
1125 dprintk(", error = %d\n", status);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001126 goto remove_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001129 if (nfs_write_need_commit(data)) {
1130 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1131 nfs_mark_request_commit(req);
1132 nfs_end_page_writeback(page);
1133 dprintk(" marked for commit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 goto next;
1135 }
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001136 dprintk(" OK\n");
1137remove_request:
1138 nfs_end_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 nfs_inode_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 next:
Trond Myklebust9fd367f2007-06-17 15:10:24 -04001141 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001143 nfs_writedata_release(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
Trond Myklebust788e7a82006-03-20 13:44:27 -05001146static const struct rpc_call_ops nfs_write_full_ops = {
Andy Adamsondef6ed72009-04-01 09:22:26 -04001147#if defined(CONFIG_NFS_V4_1)
1148 .rpc_call_prepare = nfs_write_prepare,
1149#endif /* CONFIG_NFS_V4_1 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001150 .rpc_call_done = nfs_writeback_done_full,
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001151 .rpc_release = nfs_writeback_release_full,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001152};
1153
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155/*
1156 * This function is called when the WRITE call is complete.
1157 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001158int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 struct nfs_writeargs *argp = &data->args;
1161 struct nfs_writeres *resp = &data->res;
Andy Adamsoneedc0202009-04-01 09:22:41 -04001162 struct nfs_server *server = NFS_SERVER(data->inode);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001163 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Chuck Levera3f565b2007-01-31 12:14:01 -05001165 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 task->tk_pid, task->tk_status);
1167
Chuck Leverf551e442006-09-20 14:33:04 -04001168 /*
1169 * ->write_done will attempt to use post-op attributes to detect
1170 * conflicting writes by other clients. A strict interpretation
1171 * of close-to-open would allow us to continue caching even if
1172 * another writer had changed the file, but some applications
1173 * depend on tighter cache coherency when writing.
1174 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001175 status = NFS_PROTO(data->inode)->write_done(task, data);
1176 if (status != 0)
1177 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001178 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1181 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1182 /* We tried a write call, but the server did not
1183 * commit data to stable storage even though we
1184 * requested it.
1185 * Note: There is a known bug in Tru64 < 5.0 in which
1186 * the server reports NFS_DATA_SYNC, but performs
1187 * NFS_FILE_SYNC. We therefore implement this checking
1188 * as a dprintk() in order to avoid filling syslog.
1189 */
1190 static unsigned long complain;
1191
1192 if (time_before(complain, jiffies)) {
Chuck Lever48186c72008-06-11 17:56:05 -04001193 dprintk("NFS: faulty NFS server %s:"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 " (committed = %d) != (stable = %d)\n",
Andy Adamsoneedc0202009-04-01 09:22:41 -04001195 server->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 resp->verf->committed, argp->stable);
1197 complain = jiffies + 300 * HZ;
1198 }
1199 }
1200#endif
1201 /* Is this a short write? */
1202 if (task->tk_status >= 0 && resp->count < argp->count) {
1203 static unsigned long complain;
1204
Chuck Lever91d5b472006-03-20 13:44:14 -05001205 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 /* Has the server at least made some progress? */
1208 if (resp->count != 0) {
1209 /* Was this an NFSv2 write or an NFSv3 stable write? */
1210 if (resp->verf->committed != NFS_UNSTABLE) {
1211 /* Resend from where the server left off */
1212 argp->offset += resp->count;
1213 argp->pgbase += resp->count;
1214 argp->count -= resp->count;
1215 } else {
1216 /* Resend as a stable write in order to avoid
1217 * headaches in the case of a server crash.
1218 */
1219 argp->stable = NFS_FILE_SYNC;
1220 }
Andy Adamsoneedc0202009-04-01 09:22:41 -04001221 nfs4_restart_rpc(task, server->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001222 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224 if (time_before(complain, jiffies)) {
1225 printk(KERN_WARNING
1226 "NFS: Server wrote zero bytes, expected %u.\n",
1227 argp->count);
1228 complain = jiffies + 300 * HZ;
1229 }
1230 /* Can't do anything about it except throw an error. */
1231 task->tk_status = -EIO;
1232 }
Andy Adamsoneedc0202009-04-01 09:22:41 -04001233 nfs4_sequence_free_slot(server->nfs_client, &data->res.seq_res);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001234 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
1237
1238#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001239void nfs_commitdata_release(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
Trond Myklebust383ba712008-02-19 20:04:20 -05001241 struct nfs_write_data *wdata = data;
1242
1243 put_nfs_open_context(wdata->args.context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 nfs_commit_free(wdata);
1245}
1246
1247/*
1248 * Set up the argument/result storage required for the RPC call.
1249 */
Trond Myklebustdbae4c72008-04-14 14:54:53 -04001250static int nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001251 struct nfs_write_data *data,
1252 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
Trond Myklebust84115e12007-07-14 15:39:59 -04001254 struct nfs_page *first = nfs_list_entry(head->next);
1255 struct inode *inode = first->wb_context->path.dentry->d_inode;
1256 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
Trond Myklebust3ff75762007-07-14 15:40:00 -04001257 int priority = flush_task_priority(how);
Trond Myklebust07737692007-10-25 18:42:54 -04001258 struct rpc_task *task;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001259 struct rpc_message msg = {
1260 .rpc_argp = &data->args,
1261 .rpc_resp = &data->res,
1262 .rpc_cred = first->wb_context->cred,
1263 };
Trond Myklebust84115e12007-07-14 15:39:59 -04001264 struct rpc_task_setup task_setup_data = {
Trond Myklebust07737692007-10-25 18:42:54 -04001265 .task = &data->task,
Trond Myklebust84115e12007-07-14 15:39:59 -04001266 .rpc_client = NFS_CLIENT(inode),
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001267 .rpc_message = &msg,
Trond Myklebust84115e12007-07-14 15:39:59 -04001268 .callback_ops = &nfs_commit_ops,
1269 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001270 .workqueue = nfsiod_workqueue,
Trond Myklebust84115e12007-07-14 15:39:59 -04001271 .flags = flags,
Trond Myklebust3ff75762007-07-14 15:40:00 -04001272 .priority = priority,
Trond Myklebust84115e12007-07-14 15:39:59 -04001273 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 /* Set up the RPC argument and reply structs
1276 * NB: take care not to mess about with data->commit et al. */
1277
1278 list_splice_init(head, &data->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 data->inode = inode;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001281 data->cred = msg.rpc_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001284 /* Note: we always request a commit of the entire inode */
1285 data->args.offset = 0;
1286 data->args.count = 0;
Trond Myklebust383ba712008-02-19 20:04:20 -05001287 data->args.context = get_nfs_open_context(first->wb_context);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001288 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 data->res.fattr = &data->fattr;
1290 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001291 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001292
1293 /* Set up the initial task struct. */
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001294 NFS_PROTO(inode)->commit_setup(data, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Chuck Levera3f565b2007-01-31 12:14:01 -05001296 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04001297
Trond Myklebust07737692007-10-25 18:42:54 -04001298 task = rpc_run_task(&task_setup_data);
Trond Myklebustdbae4c72008-04-14 14:54:53 -04001299 if (IS_ERR(task))
1300 return PTR_ERR(task);
1301 rpc_put_task(task);
1302 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303}
1304
1305/*
1306 * Commit dirty pages
1307 */
1308static int
Chuck Lever40859d72005-11-30 18:09:02 -05001309nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
1311 struct nfs_write_data *data;
1312 struct nfs_page *req;
1313
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001314 data = nfs_commitdata_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 if (!data)
1317 goto out_bad;
1318
1319 /* Set up the argument struct */
Trond Myklebustdbae4c72008-04-14 14:54:53 -04001320 return nfs_commit_rpcsetup(head, data, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 out_bad:
1322 while (!list_empty(head)) {
1323 req = nfs_list_entry(head->next);
1324 nfs_list_remove_request(req);
1325 nfs_mark_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001326 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Peter Zijlstrac9e51e42007-10-16 23:25:47 -07001327 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1328 BDI_RECLAIMABLE);
Trond Myklebust9fd367f2007-06-17 15:10:24 -04001329 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
1331 return -ENOMEM;
1332}
1333
1334/*
1335 * COMMIT call returned
1336 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001337static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001339 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Chuck Levera3f565b2007-01-31 12:14:01 -05001341 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 task->tk_pid, task->tk_status);
1343
Trond Myklebust788e7a82006-03-20 13:44:27 -05001344 /* Call the NFS version-specific code */
1345 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1346 return;
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001347}
1348
1349static void nfs_commit_release(void *calldata)
1350{
1351 struct nfs_write_data *data = calldata;
1352 struct nfs_page *req;
1353 int status = data->task.tk_status;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 while (!list_empty(&data->pages)) {
1356 req = nfs_list_entry(data->pages.next);
1357 nfs_list_remove_request(req);
Trond Myklebuste468bae2008-06-13 13:25:22 -04001358 nfs_clear_request_commit(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Chuck Lever48186c72008-06-11 17:56:05 -04001360 dprintk("NFS: commit (%s/%lld %d@%lld)",
Trond Myklebust88be9f92007-06-05 10:42:27 -04001361 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1362 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 req->wb_bytes,
1364 (long long)req_offset(req));
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001365 if (status < 0) {
1366 nfs_context_set_write_error(req->wb_context, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 nfs_inode_remove_request(req);
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001368 dprintk(", error = %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 goto next;
1370 }
1371
1372 /* Okay, COMMIT succeeded, apparently. Check the verifier
1373 * returned by the server against all stored verfs. */
1374 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1375 /* We have a match */
1376 nfs_inode_remove_request(req);
1377 dprintk(" OK\n");
1378 goto next;
1379 }
1380 /* We have a mismatch. Write the page again */
1381 dprintk(" mismatch\n");
Fred6d884e82008-03-19 11:24:38 -04001382 nfs_mark_request_dirty(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 next:
Trond Myklebust9fd367f2007-06-17 15:10:24 -04001384 nfs_clear_page_tag_locked(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
Trond Myklebustc9d8f892008-04-15 16:56:39 -04001386 nfs_commitdata_release(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001388
1389static const struct rpc_call_ops nfs_commit_ops = {
Andy Adamson21d9a852009-04-01 09:22:27 -04001390#if defined(CONFIG_NFS_V4_1)
1391 .rpc_call_prepare = nfs_write_prepare,
1392#endif /* CONFIG_NFS_V4_1 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001393 .rpc_call_done = nfs_commit_done,
1394 .rpc_release = nfs_commit_release,
1395};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001397int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001400 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Trond Myklebust587142f2007-07-02 09:57:54 -04001402 spin_lock(&inode->i_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001403 res = nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebust587142f2007-07-02 09:57:54 -04001404 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001406 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001407 if (error < 0)
1408 return error;
1409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return res;
1411}
Trond Myklebustc63c7b02007-04-02 19:29:52 -04001412#else
1413static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1414{
1415 return 0;
1416}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417#endif
1418
Trond Myklebust1c759502006-10-09 16:18:38 -04001419long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
Trond Myklebust1c759502006-10-09 16:18:38 -04001421 struct inode *inode = mapping->host;
Trond Myklebustca52fec2007-04-17 17:22:13 -04001422 pgoff_t idx_start, idx_end;
Trond Myklebust1c759502006-10-09 16:18:38 -04001423 unsigned int npages = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001424 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001425 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebust3f442542006-09-17 14:46:44 -04001426 long pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Trond Myklebust1c759502006-10-09 16:18:38 -04001428 /* FIXME */
1429 if (wbc->range_cyclic)
1430 idx_start = 0;
1431 else {
1432 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1433 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1434 if (idx_end > idx_start) {
Trond Myklebustca52fec2007-04-17 17:22:13 -04001435 pgoff_t l_npages = 1 + idx_end - idx_start;
Trond Myklebust1c759502006-10-09 16:18:38 -04001436 npages = l_npages;
1437 if (sizeof(npages) != sizeof(l_npages) &&
Trond Myklebustca52fec2007-04-17 17:22:13 -04001438 (pgoff_t)npages != l_npages)
Trond Myklebust1c759502006-10-09 16:18:38 -04001439 npages = 0;
1440 }
1441 }
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001442 how &= ~FLUSH_NOCOMMIT;
Trond Myklebust587142f2007-07-02 09:57:54 -04001443 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 do {
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001445 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1446 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001447 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001448 if (nocommit)
1449 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001450 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebust724c4392007-04-17 17:22:13 -04001451 if (pages == 0)
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001452 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001453 if (how & FLUSH_INVALIDATE) {
Trond Myklebust587142f2007-07-02 09:57:54 -04001454 spin_unlock(&inode->i_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001455 nfs_cancel_commit_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001456 ret = pages;
Trond Myklebust587142f2007-07-02 09:57:54 -04001457 spin_lock(&inode->i_lock);
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001458 continue;
1459 }
1460 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebust587142f2007-07-02 09:57:54 -04001461 spin_unlock(&inode->i_lock);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001462 ret = nfs_commit_list(inode, &head, how);
Trond Myklebust587142f2007-07-02 09:57:54 -04001463 spin_lock(&inode->i_lock);
1464
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001465 } while (ret >= 0);
Trond Myklebust587142f2007-07-02 09:57:54 -04001466 spin_unlock(&inode->i_lock);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001467 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
Trond Myklebust34901f72007-07-25 14:09:54 -04001470static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
Trond Myklebust1c759502006-10-09 16:18:38 -04001471{
Trond Myklebust1c759502006-10-09 16:18:38 -04001472 int ret;
1473
Trond Myklebust34901f72007-07-25 14:09:54 -04001474 ret = nfs_writepages(mapping, wbc);
Trond Myklebust61822ab2006-12-05 00:35:42 -05001475 if (ret < 0)
1476 goto out;
Trond Myklebust34901f72007-07-25 14:09:54 -04001477 ret = nfs_sync_mapping_wait(mapping, wbc, how);
Trond Myklebusted90ef52007-07-20 13:13:28 -04001478 if (ret < 0)
1479 goto out;
1480 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001481out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001482 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Trond Myklebust1c759502006-10-09 16:18:38 -04001483 return ret;
1484}
1485
Trond Myklebust34901f72007-07-25 14:09:54 -04001486/* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
1487static int nfs_write_mapping(struct address_space *mapping, int how)
1488{
1489 struct writeback_control wbc = {
1490 .bdi = mapping->backing_dev_info,
Trond Myklebust72cb77f2009-03-11 14:10:30 -04001491 .sync_mode = WB_SYNC_ALL,
Trond Myklebust34901f72007-07-25 14:09:54 -04001492 .nr_to_write = LONG_MAX,
Trond Myklebustd7fb1202008-10-06 20:08:56 -04001493 .range_start = 0,
1494 .range_end = LLONG_MAX,
Trond Myklebust34901f72007-07-25 14:09:54 -04001495 .for_writepages = 1,
Trond Myklebust34901f72007-07-25 14:09:54 -04001496 };
Trond Myklebust34901f72007-07-25 14:09:54 -04001497
Trond Myklebust34901f72007-07-25 14:09:54 -04001498 return __nfs_write_mapping(mapping, &wbc, how);
1499}
1500
Trond Myklebusted90ef52007-07-20 13:13:28 -04001501/*
1502 * flush the inode to disk.
1503 */
1504int nfs_wb_all(struct inode *inode)
Trond Myklebust1c759502006-10-09 16:18:38 -04001505{
Trond Myklebusted90ef52007-07-20 13:13:28 -04001506 return nfs_write_mapping(inode->i_mapping, 0);
1507}
Trond Myklebust1c759502006-10-09 16:18:38 -04001508
Trond Myklebusted90ef52007-07-20 13:13:28 -04001509int nfs_wb_nocommit(struct inode *inode)
1510{
1511 return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
Trond Myklebust1c759502006-10-09 16:18:38 -04001512}
1513
Trond Myklebust1b3b4a12007-08-28 10:29:36 -04001514int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1515{
1516 struct nfs_page *req;
1517 loff_t range_start = page_offset(page);
1518 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1519 struct writeback_control wbc = {
1520 .bdi = page->mapping->backing_dev_info,
1521 .sync_mode = WB_SYNC_ALL,
1522 .nr_to_write = LONG_MAX,
1523 .range_start = range_start,
1524 .range_end = range_end,
1525 };
1526 int ret = 0;
1527
1528 BUG_ON(!PageLocked(page));
1529 for (;;) {
1530 req = nfs_page_find_request(page);
1531 if (req == NULL)
1532 goto out;
Trond Myklebuste468bae2008-06-13 13:25:22 -04001533 if (test_bit(PG_CLEAN, &req->wb_flags)) {
Trond Myklebust1b3b4a12007-08-28 10:29:36 -04001534 nfs_release_request(req);
1535 break;
1536 }
1537 if (nfs_lock_request_dontget(req)) {
1538 nfs_inode_remove_request(req);
1539 /*
1540 * In case nfs_inode_remove_request has marked the
1541 * page as being dirty
1542 */
1543 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1544 nfs_unlock_request(req);
1545 break;
1546 }
1547 ret = nfs_wait_on_request(req);
1548 if (ret < 0)
1549 goto out;
1550 }
1551 if (!PagePrivate(page))
1552 return 0;
1553 ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
1554out:
1555 return ret;
1556}
1557
Adrian Bunk5334eb12007-11-21 15:04:31 -08001558static int nfs_wb_page_priority(struct inode *inode, struct page *page,
1559 int how)
Trond Myklebust1c759502006-10-09 16:18:38 -04001560{
1561 loff_t range_start = page_offset(page);
1562 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001563 struct writeback_control wbc = {
1564 .bdi = page->mapping->backing_dev_info,
1565 .sync_mode = WB_SYNC_ALL,
1566 .nr_to_write = LONG_MAX,
1567 .range_start = range_start,
1568 .range_end = range_end,
1569 };
1570 int ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001571
Trond Myklebust73e33022008-04-11 16:03:54 -04001572 do {
1573 if (clear_page_dirty_for_io(page)) {
1574 ret = nfs_writepage_locked(page, &wbc);
1575 if (ret < 0)
1576 goto out_error;
1577 } else if (!PagePrivate(page))
1578 break;
1579 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001580 if (ret < 0)
Trond Myklebust73e33022008-04-11 16:03:54 -04001581 goto out_error;
1582 } while (PagePrivate(page));
1583 return 0;
1584out_error:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001585 __mark_inode_dirty(inode, I_DIRTY_PAGES);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001586 return ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001587}
1588
1589/*
1590 * Write back all requests on one page - we do this before reading it.
1591 */
1592int nfs_wb_page(struct inode *inode, struct page* page)
1593{
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001594 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
Trond Myklebust1c759502006-10-09 16:18:38 -04001595}
1596
Trond Myklebust074cc1d2009-08-10 08:54:13 -04001597#ifdef CONFIG_MIGRATION
1598int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1599 struct page *page)
1600{
1601 struct nfs_page *req;
1602 int ret;
1603
1604 if (PageFsCache(page))
1605 nfs_fscache_release_page(page, GFP_KERNEL);
1606
1607 req = nfs_find_and_lock_request(page);
1608 ret = PTR_ERR(req);
1609 if (IS_ERR(req))
1610 goto out;
1611
1612 ret = migrate_page(mapping, newpage, page);
1613 if (!req)
1614 goto out;
1615 if (ret)
1616 goto out_unlock;
1617 page_cache_get(newpage);
1618 req->wb_page = newpage;
1619 SetPagePrivate(newpage);
1620 set_page_private(newpage, page_private(page));
1621 ClearPagePrivate(page);
1622 set_page_private(page, 0);
1623 page_cache_release(page);
1624out_unlock:
1625 nfs_clear_page_tag_locked(req);
1626 nfs_release_request(req);
1627out:
1628 return ret;
1629}
1630#endif
1631
David Howellsf7b422b2006-06-09 09:34:33 -04001632int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
1634 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1635 sizeof(struct nfs_write_data),
1636 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001637 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (nfs_wdata_cachep == NULL)
1639 return -ENOMEM;
1640
Matthew Dobson93d23412006-03-26 01:37:50 -08001641 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1642 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (nfs_wdata_mempool == NULL)
1644 return -ENOMEM;
1645
Matthew Dobson93d23412006-03-26 01:37:50 -08001646 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1647 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (nfs_commit_mempool == NULL)
1649 return -ENOMEM;
1650
Peter Zijlstra89a09142007-03-16 13:38:26 -08001651 /*
1652 * NFS congestion size, scale with available memory.
1653 *
1654 * 64MB: 8192k
1655 * 128MB: 11585k
1656 * 256MB: 16384k
1657 * 512MB: 23170k
1658 * 1GB: 32768k
1659 * 2GB: 46340k
1660 * 4GB: 65536k
1661 * 8GB: 92681k
1662 * 16GB: 131072k
1663 *
1664 * This allows larger machines to have larger/more transfers.
1665 * Limit the default to 256M
1666 */
1667 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1668 if (nfs_congestion_kb > 256*1024)
1669 nfs_congestion_kb = 256*1024;
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 return 0;
1672}
1673
David Brownell266bee82006-06-27 12:59:15 -07001674void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
1676 mempool_destroy(nfs_commit_mempool);
1677 mempool_destroy(nfs_wdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001678 kmem_cache_destroy(nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680