blob: ce5b4a9f2d8b33197a0f5561e4edf4aed7cfb09d [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>
24#include <linux/smp_lock.h>
25
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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#define NFSDBG_FACILITY NFSDBG_PAGECACHE
31
32#define MIN_POOL_WRITE (32)
33#define MIN_POOL_COMMIT (4)
34
35/*
36 * Local function declarations
37 */
38static struct nfs_page * nfs_update_request(struct nfs_open_context*,
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct page *,
40 unsigned int, unsigned int);
Trond Myklebust3f442542006-09-17 14:46:44 -040041static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how);
Trond Myklebust788e7a82006-03-20 13:44:27 -050042static const struct rpc_call_ops nfs_write_partial_ops;
43static const struct rpc_call_ops nfs_write_full_ops;
44static const struct rpc_call_ops nfs_commit_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Christoph Lametere18b8902006-12-06 20:33:20 -080046static struct kmem_cache *nfs_wdata_cachep;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050047static mempool_t *nfs_wdata_mempool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static mempool_t *nfs_commit_mempool;
49
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070050struct nfs_write_data *nfs_commit_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Christoph Lametere6b4f8d2006-12-06 20:33:14 -080052 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
Chuck Lever40859d72005-11-30 18:09:02 -050053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 if (p) {
55 memset(p, 0, sizeof(*p));
56 INIT_LIST_HEAD(&p->pages);
57 }
58 return p;
59}
60
Trond Myklebust8aca67f2006-11-13 16:23:44 -050061void nfs_commit_rcu_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Trond Myklebust8aca67f2006-11-13 16:23:44 -050063 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
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 Myklebust8aca67f2006-11-13 16:23:44 -050069void nfs_commit_free(struct nfs_write_data *wdata)
70{
71 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
72}
73
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070074struct nfs_write_data *nfs_writedata_alloc(size_t len)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050075{
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070076 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Christoph Lametere6b4f8d2006-12-06 20:33:14 -080077 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050078
79 if (p) {
80 memset(p, 0, sizeof(*p));
81 INIT_LIST_HEAD(&p->pages);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070082 p->npages = pagecount;
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040083 if (pagecount <= ARRAY_SIZE(p->page_array))
84 p->pagevec = p->page_array;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050085 else {
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040086 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
87 if (!p->pagevec) {
Trond Myklebust3feb2d42006-03-20 13:44:37 -050088 mempool_free(p, nfs_wdata_mempool);
89 p = NULL;
90 }
91 }
92 }
93 return p;
94}
95
Trond Myklebust8aca67f2006-11-13 16:23:44 -050096static void nfs_writedata_rcu_free(struct rcu_head *head)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050097{
Trond Myklebust8aca67f2006-11-13 16:23:44 -050098 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050099 if (p && (p->pagevec != &p->page_array[0]))
100 kfree(p->pagevec);
101 mempool_free(p, nfs_wdata_mempool);
102}
103
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500104static void nfs_writedata_free(struct nfs_write_data *wdata)
105{
106 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
107}
108
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100109void nfs_writedata_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 nfs_writedata_free(wdata);
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)
121 atomic_inc(&req->wb_count);
122 }
123 return req;
124}
125
126static struct nfs_page *nfs_page_find_request(struct page *page)
127{
128 struct nfs_page *req = NULL;
129 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
130
131 spin_lock(req_lock);
132 req = nfs_page_find_request_locked(page);
133 spin_unlock(req_lock);
134 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;
141 loff_t end, i_size = i_size_read(inode);
142 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
143
144 if (i_size > 0 && page->index < end_index)
145 return;
146 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
147 if (i_size >= end)
148 return;
Chuck Lever91d5b472006-03-20 13:44:14 -0500149 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 i_size_write(inode, end);
151}
152
Trond Myklebusta301b772007-02-06 11:07:15 -0800153/* A writeback failed: mark the page as bad, and invalidate the page cache */
154static void nfs_set_pageerror(struct page *page)
155{
156 SetPageError(page);
157 nfs_zap_mapping(page->mapping->host, page->mapping);
158}
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/* We can set the PG_uptodate flag if we see that a write request
161 * covers the full page.
162 */
163static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
164{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (PageUptodate(page))
166 return;
167 if (base != 0)
168 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500169 if (count != nfs_page_length(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500171 if (count != PAGE_CACHE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
Trond Myklebust49a70f22006-12-05 00:35:38 -0500173 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
Trond Myklebuste21195a2006-12-05 00:35:39 -0500176static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 unsigned int offset, unsigned int count)
178{
179 struct nfs_page *req;
Trond Myklebuste21195a2006-12-05 00:35:39 -0500180 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Trond Myklebuste21195a2006-12-05 00:35:39 -0500182 for (;;) {
183 req = nfs_update_request(ctx, page, offset, count);
184 if (!IS_ERR(req))
185 break;
186 ret = PTR_ERR(req);
187 if (ret != -EBUSY)
188 return ret;
189 ret = nfs_wb_page(page->mapping->host, page);
190 if (ret != 0)
191 return ret;
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /* Update file length */
194 nfs_grow_file(page, offset, count);
195 /* Set the PG_uptodate flag? */
196 nfs_mark_uptodate(page, offset, count);
197 nfs_unlock_request(req);
Trond Myklebustabd3e642006-01-03 09:55:02 +0100198 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
201static int wb_priority(struct writeback_control *wbc)
202{
203 if (wbc->for_reclaim)
204 return FLUSH_HIGHPRI;
205 if (wbc->for_kupdate)
206 return FLUSH_LOWPRI;
207 return 0;
208}
209
210/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800211 * NFS congestion control
212 */
213
214int nfs_congestion_kb;
215
216#define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
217#define NFS_CONGESTION_OFF_THRESH \
218 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
219
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400220static int nfs_set_page_writeback(struct page *page)
Peter Zijlstra89a09142007-03-16 13:38:26 -0800221{
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400222 int ret = test_set_page_writeback(page);
223
224 if (!ret) {
Peter Zijlstra89a09142007-03-16 13:38:26 -0800225 struct inode *inode = page->mapping->host;
226 struct nfs_server *nfss = NFS_SERVER(inode);
227
228 if (atomic_inc_return(&nfss->writeback) >
229 NFS_CONGESTION_ON_THRESH)
230 set_bdi_congested(&nfss->backing_dev_info, WRITE);
231 }
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400232 return ret;
Peter Zijlstra89a09142007-03-16 13:38:26 -0800233}
234
235static void nfs_end_page_writeback(struct page *page)
236{
237 struct inode *inode = page->mapping->host;
238 struct nfs_server *nfss = NFS_SERVER(inode);
239
240 end_page_writeback(page);
241 if (atomic_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) {
242 clear_bdi_congested(&nfss->backing_dev_info, WRITE);
243 congestion_end(WRITE);
244 }
245}
246
247/*
Trond Myklebuste261f512006-12-05 00:35:41 -0500248 * Find an associated nfs write request, and prepare to flush it out
249 * Returns 1 if there was no write request, or if the request was
250 * already tagged by nfs_set_page_dirty.Returns 0 if the request
251 * was not tagged.
252 * May also return an error if the user signalled nfs_wait_on_request().
253 */
254static int nfs_page_mark_flush(struct page *page)
255{
256 struct nfs_page *req;
Trond Myklebust612c9382007-04-20 16:12:45 -0400257 struct nfs_inode *nfsi = NFS_I(page->mapping->host);
258 spinlock_t *req_lock = &nfsi->req_lock;
Trond Myklebuste261f512006-12-05 00:35:41 -0500259 int ret;
260
261 spin_lock(req_lock);
262 for(;;) {
263 req = nfs_page_find_request_locked(page);
264 if (req == NULL) {
265 spin_unlock(req_lock);
266 return 1;
267 }
268 if (nfs_lock_request_dontget(req))
269 break;
270 /* Note: If we hold the page lock, as is the case in nfs_writepage,
271 * then the call to nfs_lock_request_dontget() will always
272 * succeed provided that someone hasn't already marked the
273 * request as dirty (in which case we don't care).
274 */
275 spin_unlock(req_lock);
276 ret = nfs_wait_on_request(req);
277 nfs_release_request(req);
278 if (ret != 0)
279 return ret;
280 spin_lock(req_lock);
281 }
Trond Myklebust612c9382007-04-20 16:12:45 -0400282 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
283 /* This request is marked for commit */
284 spin_unlock(req_lock);
285 nfs_unlock_request(req);
286 return 1;
287 }
Trond Myklebusteb4cac12007-04-15 16:21:49 -0400288 if (nfs_set_page_writeback(page) == 0) {
289 nfs_list_remove_request(req);
Trond Myklebust612c9382007-04-20 16:12:45 -0400290 /* add the request to the inode's dirty list. */
291 radix_tree_tag_set(&nfsi->nfs_page_tree,
292 req->wb_index, NFS_PAGE_TAG_DIRTY);
293 nfs_list_add_request(req, &nfsi->dirty);
294 nfsi->ndirty++;
295 spin_unlock(req_lock);
296 __mark_inode_dirty(page->mapping->host, I_DIRTY_PAGES);
297 } else
298 spin_unlock(req_lock);
Trond Myklebuste261f512006-12-05 00:35:41 -0500299 ret = test_bit(PG_NEED_FLUSH, &req->wb_flags);
300 nfs_unlock_request(req);
301 return ret;
302}
303
304/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 * Write an mmapped page to the server.
306 */
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500307static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 struct nfs_open_context *ctx;
310 struct inode *inode = page->mapping->host;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500311 unsigned offset;
Trond Myklebuste261f512006-12-05 00:35:41 -0500312 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Chuck Lever91d5b472006-03-20 13:44:14 -0500314 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
315 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
316
Trond Myklebuste261f512006-12-05 00:35:41 -0500317 err = nfs_page_mark_flush(page);
318 if (err <= 0)
319 goto out;
320 err = 0;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500321 offset = nfs_page_length(page);
322 if (!offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 goto out;
Trond Myklebust49a70f22006-12-05 00:35:38 -0500324
Trond Myklebustd5308382005-11-04 15:33:38 -0500325 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (ctx == NULL) {
327 err = -EBADF;
328 goto out;
329 }
Trond Myklebust200baa22006-12-05 00:35:40 -0500330 err = nfs_writepage_setup(ctx, page, 0, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 put_nfs_open_context(ctx);
Trond Myklebuste261f512006-12-05 00:35:41 -0500332 if (err != 0)
333 goto out;
334 err = nfs_page_mark_flush(page);
335 if (err > 0)
336 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337out:
Trond Myklebust200baa22006-12-05 00:35:40 -0500338 if (!wbc->for_writepages)
Trond Myklebust02241bc2007-01-13 02:28:07 -0500339 nfs_flush_mapping(page->mapping, wbc, FLUSH_STABLE|wb_priority(wbc));
Trond Myklebust4d770cc2006-12-05 00:35:41 -0500340 return err;
341}
342
343int nfs_writepage(struct page *page, struct writeback_control *wbc)
344{
345 int err;
346
347 err = nfs_writepage_locked(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return err;
350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
353{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 struct inode *inode = mapping->host;
355 int err;
356
Chuck Lever91d5b472006-03-20 13:44:14 -0500357 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 err = generic_writepages(mapping, wbc);
360 if (err)
361 return err;
Trond Myklebust28c69252006-09-16 13:04:50 -0400362 err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (err < 0)
364 goto out;
Chuck Lever91d5b472006-03-20 13:44:14 -0500365 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
Trond Myklebustd30c8342006-12-13 15:23:47 -0500366 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return err;
369}
370
371/*
372 * Insert a write request into an inode
373 */
374static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
375{
376 struct nfs_inode *nfsi = NFS_I(inode);
377 int error;
378
379 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
380 BUG_ON(error == -EEXIST);
381 if (error)
382 return error;
383 if (!nfsi->npages) {
384 igrab(inode);
385 nfs_begin_data_update(inode);
386 if (nfs_have_delegation(inode, FMODE_WRITE))
387 nfsi->change_attr++;
388 }
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500389 SetPagePrivate(req->wb_page);
Trond Myklebust277459d2006-12-05 00:35:35 -0500390 set_page_private(req->wb_page, (unsigned long)req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 nfsi->npages++;
392 atomic_inc(&req->wb_count);
393 return 0;
394}
395
396/*
Peter Zijlstra89a09142007-03-16 13:38:26 -0800397 * Remove a write request from an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 */
399static void nfs_inode_remove_request(struct nfs_page *req)
400{
401 struct inode *inode = req->wb_context->dentry->d_inode;
402 struct nfs_inode *nfsi = NFS_I(inode);
403
404 BUG_ON (!NFS_WBACK_BUSY(req));
405
406 spin_lock(&nfsi->req_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500407 set_page_private(req->wb_page, 0);
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500408 ClearPagePrivate(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
410 nfsi->npages--;
411 if (!nfsi->npages) {
412 spin_unlock(&nfsi->req_lock);
Trond Myklebust951a1432005-06-22 17:16:30 +0000413 nfs_end_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 iput(inode);
415 } else
416 spin_unlock(&nfsi->req_lock);
417 nfs_clear_request(req);
418 nfs_release_request(req);
419}
420
Trond Myklebust61822ab2006-12-05 00:35:42 -0500421static void
422nfs_redirty_request(struct nfs_page *req)
423{
Trond Myklebust61822ab2006-12-05 00:35:42 -0500424 __set_page_dirty_nobuffers(req->wb_page);
425}
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427/*
428 * Check if a request is dirty
429 */
430static inline int
431nfs_dirty_request(struct nfs_page *req)
432{
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400433 struct page *page = req->wb_page;
434
Trond Myklebust612c9382007-04-20 16:12:45 -0400435 if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags))
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400436 return 0;
437 return !PageWriteback(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
440#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
441/*
442 * Add a request to the inode's commit list.
443 */
444static void
445nfs_mark_request_commit(struct nfs_page *req)
446{
447 struct inode *inode = req->wb_context->dentry->d_inode;
448 struct nfs_inode *nfsi = NFS_I(inode);
449
450 spin_lock(&nfsi->req_lock);
451 nfs_list_add_request(req, &nfsi->commit);
452 nfsi->ncommit++;
Trond Myklebust612c9382007-04-20 16:12:45 -0400453 set_bit(PG_NEED_COMMIT, &(req)->wb_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 spin_unlock(&nfsi->req_lock);
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700455 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebusta1803042006-12-05 00:45:19 -0500456 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400458
459static inline
460int nfs_write_need_commit(struct nfs_write_data *data)
461{
462 return data->verf.committed != NFS_FILE_SYNC;
463}
464
465static inline
466int nfs_reschedule_unstable_write(struct nfs_page *req)
467{
Trond Myklebust612c9382007-04-20 16:12:45 -0400468 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400469 nfs_mark_request_commit(req);
470 return 1;
471 }
472 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
473 nfs_redirty_request(req);
474 return 1;
475 }
476 return 0;
477}
478#else
479static inline void
480nfs_mark_request_commit(struct nfs_page *req)
481{
482}
483
484static inline
485int nfs_write_need_commit(struct nfs_write_data *data)
486{
487 return 0;
488}
489
490static inline
491int nfs_reschedule_unstable_write(struct nfs_page *req)
492{
493 return 0;
494}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495#endif
496
497/*
498 * Wait for a request to complete.
499 *
500 * Interruptible by signals only if mounted with intr flag.
501 */
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500502static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 struct nfs_inode *nfsi = NFS_I(inode);
505 struct nfs_page *req;
506 unsigned long idx_end, next;
507 unsigned int res = 0;
508 int error;
509
510 if (npages == 0)
511 idx_end = ~0;
512 else
513 idx_end = idx_start + npages - 1;
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 next = idx_start;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000516 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (req->wb_index > idx_end)
518 break;
519
520 next = req->wb_index + 1;
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000521 BUG_ON(!NFS_WBACK_BUSY(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 atomic_inc(&req->wb_count);
524 spin_unlock(&nfsi->req_lock);
525 error = nfs_wait_on_request(req);
526 nfs_release_request(req);
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500527 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (error < 0)
529 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 res++;
531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return res;
533}
534
Trond Myklebust83715ad2006-07-05 13:17:12 -0400535static void nfs_cancel_dirty_list(struct list_head *head)
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400536{
537 struct nfs_page *req;
538 while(!list_empty(head)) {
539 req = nfs_list_entry(head->next);
540 nfs_list_remove_request(req);
Trond Myklebust5a6d41b2007-04-14 19:10:12 -0400541 nfs_end_page_writeback(req->wb_page);
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400542 nfs_inode_remove_request(req);
543 nfs_clear_page_writeback(req);
544 }
545}
546
Trond Myklebust83715ad2006-07-05 13:17:12 -0400547static void nfs_cancel_commit_list(struct list_head *head)
548{
549 struct nfs_page *req;
550
551 while(!list_empty(head)) {
552 req = nfs_list_entry(head->next);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700553 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400554 nfs_list_remove_request(req);
Trond Myklebust612c9382007-04-20 16:12:45 -0400555 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400556 nfs_inode_remove_request(req);
Trond Myklebustb6dff262006-10-19 23:28:38 -0700557 nfs_unlock_request(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -0400558 }
559}
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
562/*
563 * nfs_scan_commit - Scan an inode for commit requests
564 * @inode: NFS inode to scan
565 * @dst: destination list
566 * @idx_start: lower bound of page->index to scan.
567 * @npages: idx_start + npages sets the upper bound to scan.
568 *
569 * Moves requests from the inode's 'commit' request list.
570 * The requests are *not* checked to ensure that they form a contiguous set.
571 */
572static int
573nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
574{
575 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000576 int res = 0;
577
578 if (nfsi->ncommit != 0) {
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400579 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000580 nfsi->ncommit -= res;
581 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
582 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return res;
585}
Trond Myklebustc42de9d2006-03-20 13:44:51 -0500586#else
587static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
588{
589 return 0;
590}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591#endif
592
Peter Zijlstra89a09142007-03-16 13:38:26 -0800593static int nfs_wait_on_write_congestion(struct address_space *mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Peter Zijlstra89a09142007-03-16 13:38:26 -0800595 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 struct backing_dev_info *bdi = mapping->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 int ret = 0;
598
599 might_sleep();
600
601 if (!bdi_write_congested(bdi))
602 return 0;
Chuck Lever91d5b472006-03-20 13:44:14 -0500603
Peter Zijlstra89a09142007-03-16 13:38:26 -0800604 nfs_inc_stats(inode, NFSIOS_CONGESTIONWAIT);
Chuck Lever91d5b472006-03-20 13:44:14 -0500605
Peter Zijlstra89a09142007-03-16 13:38:26 -0800606 do {
607 struct rpc_clnt *clnt = NFS_CLIENT(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 sigset_t oldset;
609
610 rpc_clnt_sigmask(clnt, &oldset);
Peter Zijlstra89a09142007-03-16 13:38:26 -0800611 ret = congestion_wait_interruptible(WRITE, HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 rpc_clnt_sigunmask(clnt, &oldset);
Peter Zijlstra89a09142007-03-16 13:38:26 -0800613 if (ret == -ERESTARTSYS)
614 break;
615 ret = 0;
616 } while (bdi_write_congested(bdi));
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return ret;
619}
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621/*
622 * Try to update any existing write request, or create one if there is none.
623 * In order to match, the request's credentials must match those of
624 * the calling process.
625 *
626 * Note: Should always be called with the Page Lock held!
627 */
628static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
Trond Myklebuste21195a2006-12-05 00:35:39 -0500629 struct page *page, unsigned int offset, unsigned int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Peter Zijlstra89a09142007-03-16 13:38:26 -0800631 struct address_space *mapping = page->mapping;
632 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 struct nfs_inode *nfsi = NFS_I(inode);
634 struct nfs_page *req, *new = NULL;
635 unsigned long rqend, end;
636
637 end = offset + bytes;
638
Peter Zijlstra89a09142007-03-16 13:38:26 -0800639 if (nfs_wait_on_write_congestion(mapping))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return ERR_PTR(-ERESTARTSYS);
641 for (;;) {
642 /* Loop over all inode entries and see if we find
643 * A request for the page we wish to update
644 */
645 spin_lock(&nfsi->req_lock);
Trond Myklebust277459d2006-12-05 00:35:35 -0500646 req = nfs_page_find_request_locked(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (req) {
648 if (!nfs_lock_request_dontget(req)) {
649 int error;
Trond Myklebust277459d2006-12-05 00:35:35 -0500650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 spin_unlock(&nfsi->req_lock);
652 error = nfs_wait_on_request(req);
653 nfs_release_request(req);
Neil Brown1dd594b2006-03-20 13:44:04 -0500654 if (error < 0) {
655 if (new)
656 nfs_release_request(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return ERR_PTR(error);
Neil Brown1dd594b2006-03-20 13:44:04 -0500658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 continue;
660 }
661 spin_unlock(&nfsi->req_lock);
662 if (new)
663 nfs_release_request(new);
664 break;
665 }
666
667 if (new) {
668 int error;
669 nfs_lock_request_dontget(new);
670 error = nfs_inode_add_request(inode, new);
671 if (error) {
672 spin_unlock(&nfsi->req_lock);
673 nfs_unlock_request(new);
674 return ERR_PTR(error);
675 }
676 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return new;
678 }
679 spin_unlock(&nfsi->req_lock);
680
681 new = nfs_create_request(ctx, inode, page, offset, bytes);
682 if (IS_ERR(new))
683 return new;
684 }
685
686 /* We have a request for our page.
687 * If the creds don't match, or the
688 * page addresses don't match,
689 * tell the caller to wait on the conflicting
690 * request.
691 */
692 rqend = req->wb_offset + req->wb_bytes;
693 if (req->wb_context != ctx
694 || req->wb_page != page
695 || !nfs_dirty_request(req)
696 || offset > rqend || end < req->wb_offset) {
697 nfs_unlock_request(req);
698 return ERR_PTR(-EBUSY);
699 }
700
701 /* Okay, the request matches. Update the region */
702 if (offset < req->wb_offset) {
703 req->wb_offset = offset;
704 req->wb_pgbase = offset;
705 req->wb_bytes = rqend - req->wb_offset;
706 }
707
708 if (end > rqend)
709 req->wb_bytes = end - req->wb_offset;
710
711 return req;
712}
713
714int nfs_flush_incompatible(struct file *file, struct page *page)
715{
716 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 struct nfs_page *req;
Trond Myklebust1a545332006-12-05 00:35:40 -0500718 int do_flush, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 /*
720 * Look for a request corresponding to this page. If there
721 * is one, and it belongs to another file, we flush it out
722 * before we try to copy anything into the page. Do this
723 * due to the lack of an ACCESS-type call in NFSv2.
724 * Also do the same if we find a request from an existing
725 * dropped page.
726 */
Trond Myklebust1a545332006-12-05 00:35:40 -0500727 do {
728 req = nfs_page_find_request(page);
729 if (req == NULL)
730 return 0;
731 do_flush = req->wb_page != page || req->wb_context != ctx
Trond Myklebuste261f512006-12-05 00:35:41 -0500732 || !nfs_dirty_request(req);
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/*
742 * Update and possibly write a cached page of an NFS file.
743 *
744 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
745 * things with a page scheduled for an RPC call (e.g. invalidate it).
746 */
747int nfs_updatepage(struct file *file, struct page *page,
748 unsigned int offset, unsigned int count)
749{
750 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 int status = 0;
753
Chuck Lever91d5b472006-03-20 13:44:14 -0500754 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800757 file->f_path.dentry->d_parent->d_name.name,
758 file->f_path.dentry->d_name.name, count,
Chuck Lever0bbacc42005-11-01 16:53:32 -0500759 (long long)(page_offset(page) +offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /* If we're not using byte range locks, and we know the page
762 * is entirely in cache, it may be more efficient to avoid
763 * fragmenting write requests.
764 */
Trond Myklebustab0a3db2005-06-22 17:16:30 +0000765 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
Trond Myklebust49a70f22006-12-05 00:35:38 -0500766 count = max(count + offset, nfs_page_length(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769
Trond Myklebuste21195a2006-12-05 00:35:39 -0500770 status = nfs_writepage_setup(ctx, page, offset, count);
Trond Myklebuste261f512006-12-05 00:35:41 -0500771 __set_page_dirty_nobuffers(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
774 status, (long long)i_size_read(inode));
775 if (status < 0)
Trond Myklebusta301b772007-02-06 11:07:15 -0800776 nfs_set_pageerror(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return status;
778}
779
780static void nfs_writepage_release(struct nfs_page *req)
781{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Trond Myklebust8e821ca2007-04-20 16:12:34 -0400783 if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req)) {
784 nfs_end_page_writeback(req->wb_page);
785 nfs_inode_remove_request(req);
786 } else
787 nfs_end_page_writeback(req->wb_page);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000788 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
791static inline int flush_task_priority(int how)
792{
793 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
794 case FLUSH_HIGHPRI:
795 return RPC_PRIORITY_HIGH;
796 case FLUSH_LOWPRI:
797 return RPC_PRIORITY_LOW;
798 }
799 return RPC_PRIORITY_NORMAL;
800}
801
802/*
803 * Set up the argument/result storage required for the RPC call.
804 */
805static void nfs_write_rpcsetup(struct nfs_page *req,
806 struct nfs_write_data *data,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500807 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 unsigned int count, unsigned int offset,
809 int how)
810{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -0500812 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 /* Set up the RPC argument and reply structs
815 * NB: take care not to mess about with data->commit et al. */
816
817 data->req = req;
818 data->inode = inode = req->wb_context->dentry->d_inode;
819 data->cred = req->wb_context->cred;
820
821 data->args.fh = NFS_FH(inode);
822 data->args.offset = req_offset(req) + offset;
823 data->args.pgbase = req->wb_pgbase + offset;
824 data->args.pages = data->pagevec;
825 data->args.count = count;
826 data->args.context = req->wb_context;
827
828 data->res.fattr = &data->fattr;
829 data->res.count = count;
830 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400831 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Trond Myklebust788e7a82006-03-20 13:44:27 -0500833 /* Set up the initial task struct. */
834 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
835 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 NFS_PROTO(inode)->write_setup(data, how);
837
838 data->task.tk_priority = flush_task_priority(how);
839 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Chuck Levera3f565b2007-01-31 12:14:01 -0500841 dprintk("NFS: %5u initiated write call "
842 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500843 data->task.tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 inode->i_sb->s_id,
845 (long long)NFS_FILEID(inode),
846 count,
847 (unsigned long long)data->args.offset);
848}
849
850static void nfs_execute_write(struct nfs_write_data *data)
851{
852 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
853 sigset_t oldset;
854
855 rpc_clnt_sigmask(clnt, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 rpc_execute(&data->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 rpc_clnt_sigunmask(clnt, &oldset);
858}
859
860/*
861 * Generate multiple small requests to write out a single
862 * contiguous dirty area on one page.
863 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500864static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 struct nfs_page *req = nfs_list_entry(head->next);
867 struct page *page = req->wb_page;
868 struct nfs_write_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700869 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
870 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 int requests = 0;
872 LIST_HEAD(list);
873
874 nfs_list_remove_request(req);
875
876 nbytes = req->wb_bytes;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700877 do {
878 size_t len = min(nbytes, wsize);
879
880 data = nfs_writedata_alloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if (!data)
882 goto out_bad;
883 list_add(&data->pages, &list);
884 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700885 nbytes -= len;
886 } while (nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 atomic_set(&req->wb_complete, requests);
888
889 ClearPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 offset = 0;
891 nbytes = req->wb_bytes;
892 do {
893 data = list_entry(list.next, struct nfs_write_data, pages);
894 list_del_init(&data->pages);
895
896 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 if (nbytes > wsize) {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500899 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
900 wsize, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 offset += wsize;
902 nbytes -= wsize;
903 } else {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500904 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
905 nbytes, offset, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 nbytes = 0;
907 }
908 nfs_execute_write(data);
909 } while (nbytes != 0);
910
911 return 0;
912
913out_bad:
914 while (!list_empty(&list)) {
915 data = list_entry(list.next, struct nfs_write_data, pages);
916 list_del(&data->pages);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500917 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
Trond Myklebust61822ab2006-12-05 00:35:42 -0500919 nfs_redirty_request(req);
Trond Myklebust6d677e32007-04-20 16:12:40 -0400920 nfs_end_page_writeback(req->wb_page);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000921 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return -ENOMEM;
923}
924
925/*
926 * Create an RPC task for the given write request and kick it.
927 * The page must have been locked by the caller.
928 *
929 * It may happen that the page we're passed is not marked dirty.
930 * This is the case if nfs_updatepage detects a conflicting request
931 * that has been written but not committed.
932 */
Trond Myklebust7d46a492006-03-20 13:44:50 -0500933static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
935 struct nfs_page *req;
936 struct page **pages;
937 struct nfs_write_data *data;
938 unsigned int count;
939
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700940 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (!data)
942 goto out_bad;
943
944 pages = data->pagevec;
945 count = 0;
946 while (!list_empty(head)) {
947 req = nfs_list_entry(head->next);
948 nfs_list_remove_request(req);
949 nfs_list_add_request(req, &data->pages);
950 ClearPageError(req->wb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 *pages++ = req->wb_page;
952 count += req->wb_bytes;
953 }
954 req = nfs_list_entry(data->pages.next);
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 /* Set up the argument struct */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500957 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 nfs_execute_write(data);
960 return 0;
961 out_bad:
962 while (!list_empty(head)) {
963 struct nfs_page *req = nfs_list_entry(head->next);
964 nfs_list_remove_request(req);
Trond Myklebust61822ab2006-12-05 00:35:42 -0500965 nfs_redirty_request(req);
Trond Myklebust6d677e32007-04-20 16:12:40 -0400966 nfs_end_page_writeback(req->wb_page);
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000967 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
969 return -ENOMEM;
970}
971
Trond Myklebust7d46a492006-03-20 13:44:50 -0500972static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
974 LIST_HEAD(one_request);
Trond Myklebust7d46a492006-03-20 13:44:50 -0500975 int (*flush_one)(struct inode *, struct list_head *, int);
976 struct nfs_page *req;
977 int wpages = NFS_SERVER(inode)->wpages;
978 int wsize = NFS_SERVER(inode)->wsize;
979 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Trond Myklebust7d46a492006-03-20 13:44:50 -0500981 flush_one = nfs_flush_one;
982 if (wsize < PAGE_CACHE_SIZE)
983 flush_one = nfs_flush_multi;
984 /* For single writes, FLUSH_STABLE is more efficient */
985 if (npages <= wpages && npages == NFS_I(inode)->npages
986 && nfs_list_entry(head->next)->wb_bytes <= wsize)
987 how |= FLUSH_STABLE;
988
989 do {
990 nfs_coalesce_requests(head, &one_request, wpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 req = nfs_list_entry(one_request.next);
Trond Myklebust7d46a492006-03-20 13:44:50 -0500992 error = flush_one(inode, &one_request, how);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (error < 0)
Trond Myklebust7d46a492006-03-20 13:44:50 -0500994 goto out_err;
995 } while (!list_empty(head));
996 return 0;
997out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 while (!list_empty(head)) {
999 req = nfs_list_entry(head->next);
1000 nfs_list_remove_request(req);
Trond Myklebust61822ab2006-12-05 00:35:42 -05001001 nfs_redirty_request(req);
Trond Myklebust6d677e32007-04-20 16:12:40 -04001002 nfs_end_page_writeback(req->wb_page);
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001003 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005 return error;
1006}
1007
1008/*
1009 * Handle a write reply that flushed part of a page.
1010 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001011static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001013 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 struct nfs_page *req = data->req;
1015 struct page *page = req->wb_page;
1016
1017 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1018 req->wb_context->dentry->d_inode->i_sb->s_id,
1019 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1020 req->wb_bytes,
1021 (long long)req_offset(req));
1022
Trond Myklebust788e7a82006-03-20 13:44:27 -05001023 if (nfs_writeback_done(task, data) != 0)
1024 return;
1025
1026 if (task->tk_status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -08001027 nfs_set_pageerror(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001028 req->wb_context->error = task->tk_status;
1029 dprintk(", error = %d\n", task->tk_status);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001030 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001033 if (nfs_write_need_commit(data)) {
1034 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
1035
1036 spin_lock(req_lock);
1037 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
1038 /* Do nothing we need to resend the writes */
1039 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1040 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1041 dprintk(" defer commit\n");
1042 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1043 set_bit(PG_NEED_RESCHED, &req->wb_flags);
1044 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
1045 dprintk(" server reboot detected\n");
1046 }
1047 spin_unlock(req_lock);
1048 } else
1049 dprintk(" OK\n");
1050
1051out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (atomic_dec_and_test(&req->wb_complete))
1053 nfs_writepage_release(req);
1054}
1055
Trond Myklebust788e7a82006-03-20 13:44:27 -05001056static const struct rpc_call_ops nfs_write_partial_ops = {
1057 .rpc_call_done = nfs_writeback_done_partial,
1058 .rpc_release = nfs_writedata_release,
1059};
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061/*
1062 * Handle a write reply that flushes a whole page.
1063 *
1064 * FIXME: There is an inherent race with invalidate_inode_pages and
1065 * writebacks since the page->count is kept > 1 for as long
1066 * as the page has a write request pending.
1067 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001068static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Trond Myklebust788e7a82006-03-20 13:44:27 -05001070 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 struct nfs_page *req;
1072 struct page *page;
1073
Trond Myklebust788e7a82006-03-20 13:44:27 -05001074 if (nfs_writeback_done(task, data) != 0)
1075 return;
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 /* Update attributes as result of writeback. */
1078 while (!list_empty(&data->pages)) {
1079 req = nfs_list_entry(data->pages.next);
1080 nfs_list_remove_request(req);
1081 page = req->wb_page;
1082
1083 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1084 req->wb_context->dentry->d_inode->i_sb->s_id,
1085 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1086 req->wb_bytes,
1087 (long long)req_offset(req));
1088
Trond Myklebust788e7a82006-03-20 13:44:27 -05001089 if (task->tk_status < 0) {
Trond Myklebusta301b772007-02-06 11:07:15 -08001090 nfs_set_pageerror(page);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001091 req->wb_context->error = task->tk_status;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001092 dprintk(", error = %d\n", task->tk_status);
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001093 goto remove_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001096 if (nfs_write_need_commit(data)) {
1097 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1098 nfs_mark_request_commit(req);
1099 nfs_end_page_writeback(page);
1100 dprintk(" marked for commit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 goto next;
1102 }
Trond Myklebust8e821ca2007-04-20 16:12:34 -04001103 dprintk(" OK\n");
1104remove_request:
1105 nfs_end_page_writeback(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 nfs_inode_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001108 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110}
1111
Trond Myklebust788e7a82006-03-20 13:44:27 -05001112static const struct rpc_call_ops nfs_write_full_ops = {
1113 .rpc_call_done = nfs_writeback_done_full,
1114 .rpc_release = nfs_writedata_release,
1115};
1116
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118/*
1119 * This function is called when the WRITE call is complete.
1120 */
Chuck Lever462d5b32006-03-20 13:44:32 -05001121int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 struct nfs_writeargs *argp = &data->args;
1124 struct nfs_writeres *resp = &data->res;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001125 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Chuck Levera3f565b2007-01-31 12:14:01 -05001127 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 task->tk_pid, task->tk_status);
1129
Chuck Leverf551e442006-09-20 14:33:04 -04001130 /*
1131 * ->write_done will attempt to use post-op attributes to detect
1132 * conflicting writes by other clients. A strict interpretation
1133 * of close-to-open would allow us to continue caching even if
1134 * another writer had changed the file, but some applications
1135 * depend on tighter cache coherency when writing.
1136 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001137 status = NFS_PROTO(data->inode)->write_done(task, data);
1138 if (status != 0)
1139 return status;
Chuck Lever91d5b472006-03-20 13:44:14 -05001140 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1143 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1144 /* We tried a write call, but the server did not
1145 * commit data to stable storage even though we
1146 * requested it.
1147 * Note: There is a known bug in Tru64 < 5.0 in which
1148 * the server reports NFS_DATA_SYNC, but performs
1149 * NFS_FILE_SYNC. We therefore implement this checking
1150 * as a dprintk() in order to avoid filling syslog.
1151 */
1152 static unsigned long complain;
1153
1154 if (time_before(complain, jiffies)) {
1155 dprintk("NFS: faulty NFS server %s:"
1156 " (committed = %d) != (stable = %d)\n",
David Howells54ceac42006-08-22 20:06:13 -04001157 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 resp->verf->committed, argp->stable);
1159 complain = jiffies + 300 * HZ;
1160 }
1161 }
1162#endif
1163 /* Is this a short write? */
1164 if (task->tk_status >= 0 && resp->count < argp->count) {
1165 static unsigned long complain;
1166
Chuck Lever91d5b472006-03-20 13:44:14 -05001167 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 /* Has the server at least made some progress? */
1170 if (resp->count != 0) {
1171 /* Was this an NFSv2 write or an NFSv3 stable write? */
1172 if (resp->verf->committed != NFS_UNSTABLE) {
1173 /* Resend from where the server left off */
1174 argp->offset += resp->count;
1175 argp->pgbase += resp->count;
1176 argp->count -= resp->count;
1177 } else {
1178 /* Resend as a stable write in order to avoid
1179 * headaches in the case of a server crash.
1180 */
1181 argp->stable = NFS_FILE_SYNC;
1182 }
1183 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001184 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186 if (time_before(complain, jiffies)) {
1187 printk(KERN_WARNING
1188 "NFS: Server wrote zero bytes, expected %u.\n",
1189 argp->count);
1190 complain = jiffies + 300 * HZ;
1191 }
1192 /* Can't do anything about it except throw an error. */
1193 task->tk_status = -EIO;
1194 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05001195 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
1198
1199#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001200void nfs_commit_release(void *wdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 nfs_commit_free(wdata);
1203}
1204
1205/*
1206 * Set up the argument/result storage required for the RPC call.
1207 */
1208static void nfs_commit_rpcsetup(struct list_head *head,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001209 struct nfs_write_data *data,
1210 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001212 struct nfs_page *first;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 struct inode *inode;
Trond Myklebust788e7a82006-03-20 13:44:27 -05001214 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 /* Set up the RPC argument and reply structs
1217 * NB: take care not to mess about with data->commit et al. */
1218
1219 list_splice_init(head, &data->pages);
1220 first = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 inode = first->wb_context->dentry->d_inode;
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 data->inode = inode;
1224 data->cred = first->wb_context->cred;
1225
1226 data->args.fh = NFS_FH(data->inode);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001227 /* Note: we always request a commit of the entire inode */
1228 data->args.offset = 0;
1229 data->args.count = 0;
1230 data->res.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 data->res.fattr = &data->fattr;
1232 data->res.verf = &data->verf;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001233 nfs_fattr_init(&data->fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05001234
1235 /* Set up the initial task struct. */
1236 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1237 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 NFS_PROTO(inode)->commit_setup(data, how);
1239
1240 data->task.tk_priority = flush_task_priority(how);
1241 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Chuck Levera3f565b2007-01-31 12:14:01 -05001243 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
1246/*
1247 * Commit dirty pages
1248 */
1249static int
Chuck Lever40859d72005-11-30 18:09:02 -05001250nfs_commit_list(struct inode *inode, struct list_head *head, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
1252 struct nfs_write_data *data;
1253 struct nfs_page *req;
1254
Trond Myklebuste9f7bee2006-09-08 09:48:54 -07001255 data = nfs_commit_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 if (!data)
1258 goto out_bad;
1259
1260 /* Set up the argument struct */
1261 nfs_commit_rpcsetup(head, data, how);
1262
1263 nfs_execute_write(data);
1264 return 0;
1265 out_bad:
1266 while (!list_empty(head)) {
1267 req = nfs_list_entry(head->next);
1268 nfs_list_remove_request(req);
1269 nfs_mark_request_commit(req);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001270 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Trond Myklebust5c2d97c2006-09-18 23:20:35 -04001271 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
1273 return -ENOMEM;
1274}
1275
1276/*
1277 * COMMIT call returned
1278 */
Trond Myklebust788e7a82006-03-20 13:44:27 -05001279static void nfs_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001281 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 struct nfs_page *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Chuck Levera3f565b2007-01-31 12:14:01 -05001284 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 task->tk_pid, task->tk_status);
1286
Trond Myklebust788e7a82006-03-20 13:44:27 -05001287 /* Call the NFS version-specific code */
1288 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1289 return;
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 while (!list_empty(&data->pages)) {
1292 req = nfs_list_entry(data->pages.next);
1293 nfs_list_remove_request(req);
Trond Myklebust612c9382007-04-20 16:12:45 -04001294 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
Christoph Lameterfd39fc82006-06-30 01:55:40 -07001295 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1298 req->wb_context->dentry->d_inode->i_sb->s_id,
1299 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1300 req->wb_bytes,
1301 (long long)req_offset(req));
1302 if (task->tk_status < 0) {
1303 req->wb_context->error = task->tk_status;
1304 nfs_inode_remove_request(req);
1305 dprintk(", error = %d\n", task->tk_status);
1306 goto next;
1307 }
1308
1309 /* Okay, COMMIT succeeded, apparently. Check the verifier
1310 * returned by the server against all stored verfs. */
1311 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1312 /* We have a match */
1313 nfs_inode_remove_request(req);
1314 dprintk(" OK\n");
1315 goto next;
1316 }
1317 /* We have a mismatch. Write the page again */
1318 dprintk(" mismatch\n");
Trond Myklebust61822ab2006-12-05 00:35:42 -05001319 nfs_redirty_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 next:
Trond Myklebustc6a556b2005-06-22 17:16:30 +00001321 nfs_clear_page_writeback(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
Trond Myklebust788e7a82006-03-20 13:44:27 -05001324
1325static const struct rpc_call_ops nfs_commit_ops = {
1326 .rpc_call_done = nfs_commit_done,
1327 .rpc_release = nfs_commit_release,
1328};
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001329#else
1330static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1331{
1332 return 0;
1333}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334#endif
1335
Trond Myklebust3f442542006-09-17 14:46:44 -04001336static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Trond Myklebust28c69252006-09-16 13:04:50 -04001338 struct nfs_inode *nfsi = NFS_I(mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 LIST_HEAD(head);
Trond Myklebust3f442542006-09-17 14:46:44 -04001340 long res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 spin_lock(&nfsi->req_lock);
Trond Myklebust3f442542006-09-17 14:46:44 -04001343 res = nfs_scan_dirty(mapping, wbc, &head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 spin_unlock(&nfsi->req_lock);
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001345 if (res) {
Trond Myklebust28c69252006-09-16 13:04:50 -04001346 int error = nfs_flush_list(mapping->host, &head, res, how);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001347 if (error < 0)
1348 return error;
Trond Myklebustab0a3db2005-06-22 17:16:30 +00001349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return res;
1351}
1352
1353#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001354int nfs_commit_inode(struct inode *inode, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
1356 struct nfs_inode *nfsi = NFS_I(inode);
1357 LIST_HEAD(head);
Trond Myklebust7d46a492006-03-20 13:44:50 -05001358 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 spin_lock(&nfsi->req_lock);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001361 res = nfs_scan_commit(inode, &head, 0, 0);
1362 spin_unlock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (res) {
Trond Myklebust7d46a492006-03-20 13:44:50 -05001364 int error = nfs_commit_list(inode, &head, how);
Trond Myklebust3da28eb2005-06-22 17:16:31 +00001365 if (error < 0)
1366 return error;
1367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return res;
1369}
1370#endif
1371
Trond Myklebust1c759502006-10-09 16:18:38 -04001372long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Trond Myklebust1c759502006-10-09 16:18:38 -04001374 struct inode *inode = mapping->host;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001375 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust1c759502006-10-09 16:18:38 -04001376 unsigned long idx_start, idx_end;
1377 unsigned int npages = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001378 LIST_HEAD(head);
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001379 int nocommit = how & FLUSH_NOCOMMIT;
Trond Myklebust3f442542006-09-17 14:46:44 -04001380 long pages, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Trond Myklebust1c759502006-10-09 16:18:38 -04001382 /* FIXME */
1383 if (wbc->range_cyclic)
1384 idx_start = 0;
1385 else {
1386 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1387 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1388 if (idx_end > idx_start) {
1389 unsigned long l_npages = 1 + idx_end - idx_start;
1390 npages = l_npages;
1391 if (sizeof(npages) != sizeof(l_npages) &&
1392 (unsigned long)npages != l_npages)
1393 npages = 0;
1394 }
1395 }
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001396 how &= ~FLUSH_NOCOMMIT;
1397 spin_lock(&nfsi->req_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 do {
Trond Myklebust1c759502006-10-09 16:18:38 -04001399 wbc->pages_skipped = 0;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001400 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1401 if (ret != 0)
Trond Myklebust70b9ecb2006-01-03 09:55:34 +01001402 continue;
Trond Myklebust1c759502006-10-09 16:18:38 -04001403 pages = nfs_scan_dirty(mapping, wbc, &head);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001404 if (pages != 0) {
1405 spin_unlock(&nfsi->req_lock);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001406 if (how & FLUSH_INVALIDATE) {
Trond Myklebust83715ad2006-07-05 13:17:12 -04001407 nfs_cancel_dirty_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001408 ret = pages;
1409 } else
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001410 ret = nfs_flush_list(inode, &head, pages, how);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001411 spin_lock(&nfsi->req_lock);
1412 continue;
1413 }
Trond Myklebust1c759502006-10-09 16:18:38 -04001414 if (wbc->pages_skipped != 0)
1415 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001416 if (nocommit)
1417 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001418 pages = nfs_scan_commit(inode, &head, idx_start, npages);
Trond Myklebust1c759502006-10-09 16:18:38 -04001419 if (pages == 0) {
1420 if (wbc->pages_skipped != 0)
1421 continue;
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001422 break;
Trond Myklebust1c759502006-10-09 16:18:38 -04001423 }
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001424 if (how & FLUSH_INVALIDATE) {
1425 spin_unlock(&nfsi->req_lock);
Trond Myklebust83715ad2006-07-05 13:17:12 -04001426 nfs_cancel_commit_list(&head);
Trond Myklebuste8e058e2006-11-15 17:31:56 -05001427 ret = pages;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -04001428 spin_lock(&nfsi->req_lock);
1429 continue;
1430 }
1431 pages += nfs_scan_commit(inode, &head, 0, 0);
Trond Myklebustc42de9d2006-03-20 13:44:51 -05001432 spin_unlock(&nfsi->req_lock);
1433 ret = nfs_commit_list(inode, &head, how);
1434 spin_lock(&nfsi->req_lock);
1435 } while (ret >= 0);
1436 spin_unlock(&nfsi->req_lock);
1437 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438}
1439
Trond Myklebust1c759502006-10-09 16:18:38 -04001440/*
1441 * flush the inode to disk.
1442 */
1443int nfs_wb_all(struct inode *inode)
1444{
1445 struct address_space *mapping = inode->i_mapping;
1446 struct writeback_control wbc = {
1447 .bdi = mapping->backing_dev_info,
1448 .sync_mode = WB_SYNC_ALL,
1449 .nr_to_write = LONG_MAX,
Trond Myklebust61822ab2006-12-05 00:35:42 -05001450 .for_writepages = 1,
Trond Myklebust1c759502006-10-09 16:18:38 -04001451 .range_cyclic = 1,
1452 };
1453 int ret;
1454
Trond Myklebust61822ab2006-12-05 00:35:42 -05001455 ret = generic_writepages(mapping, &wbc);
1456 if (ret < 0)
1457 goto out;
Trond Myklebust1c759502006-10-09 16:18:38 -04001458 ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
1459 if (ret >= 0)
1460 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001461out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001462 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Trond Myklebust1c759502006-10-09 16:18:38 -04001463 return ret;
1464}
1465
1466int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
1467{
1468 struct writeback_control wbc = {
1469 .bdi = mapping->backing_dev_info,
1470 .sync_mode = WB_SYNC_ALL,
1471 .nr_to_write = LONG_MAX,
1472 .range_start = range_start,
1473 .range_end = range_end,
Trond Myklebust61822ab2006-12-05 00:35:42 -05001474 .for_writepages = 1,
Trond Myklebust1c759502006-10-09 16:18:38 -04001475 };
1476 int ret;
1477
Trond Myklebust61822ab2006-12-05 00:35:42 -05001478 if (!(how & FLUSH_NOWRITEPAGE)) {
1479 ret = generic_writepages(mapping, &wbc);
1480 if (ret < 0)
1481 goto out;
1482 }
Trond Myklebust1c759502006-10-09 16:18:38 -04001483 ret = nfs_sync_mapping_wait(mapping, &wbc, how);
1484 if (ret >= 0)
1485 return 0;
Trond Myklebust61822ab2006-12-05 00:35:42 -05001486out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001487 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Trond Myklebust1c759502006-10-09 16:18:38 -04001488 return ret;
1489}
1490
Trond Myklebust61822ab2006-12-05 00:35:42 -05001491int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
Trond Myklebust1c759502006-10-09 16:18:38 -04001492{
1493 loff_t range_start = page_offset(page);
1494 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001495 struct writeback_control wbc = {
1496 .bdi = page->mapping->backing_dev_info,
1497 .sync_mode = WB_SYNC_ALL,
1498 .nr_to_write = LONG_MAX,
1499 .range_start = range_start,
1500 .range_end = range_end,
1501 };
1502 int ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001503
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001504 BUG_ON(!PageLocked(page));
1505 if (!(how & FLUSH_NOWRITEPAGE) && clear_page_dirty_for_io(page)) {
1506 ret = nfs_writepage_locked(page, &wbc);
1507 if (ret < 0)
1508 goto out;
1509 }
Trond Myklebustf40313a2007-01-13 02:28:08 -05001510 if (!PagePrivate(page))
1511 return 0;
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001512 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1513 if (ret >= 0)
1514 return 0;
1515out:
Trond Myklebuste507d9e2006-12-05 00:35:42 -05001516 __mark_inode_dirty(inode, I_DIRTY_PAGES);
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001517 return ret;
Trond Myklebust1c759502006-10-09 16:18:38 -04001518}
1519
1520/*
1521 * Write back all requests on one page - we do this before reading it.
1522 */
1523int nfs_wb_page(struct inode *inode, struct page* page)
1524{
Trond Myklebust4d770cc2006-12-05 00:35:41 -05001525 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
Trond Myklebust1c759502006-10-09 16:18:38 -04001526}
1527
Trond Myklebust1a545332006-12-05 00:35:40 -05001528int nfs_set_page_dirty(struct page *page)
1529{
1530 struct nfs_page *req;
1531
1532 req = nfs_page_find_request(page);
1533 if (req != NULL) {
1534 /* Mark any existing write requests for flushing */
1535 set_bit(PG_NEED_FLUSH, &req->wb_flags);
1536 nfs_release_request(req);
1537 }
1538 return __set_page_dirty_nobuffers(page);
1539}
1540
Trond Myklebust1c759502006-10-09 16:18:38 -04001541
David Howellsf7b422b2006-06-09 09:34:33 -04001542int __init nfs_init_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
1544 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1545 sizeof(struct nfs_write_data),
1546 0, SLAB_HWCACHE_ALIGN,
1547 NULL, NULL);
1548 if (nfs_wdata_cachep == NULL)
1549 return -ENOMEM;
1550
Matthew Dobson93d23412006-03-26 01:37:50 -08001551 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1552 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (nfs_wdata_mempool == NULL)
1554 return -ENOMEM;
1555
Matthew Dobson93d23412006-03-26 01:37:50 -08001556 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1557 nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if (nfs_commit_mempool == NULL)
1559 return -ENOMEM;
1560
Peter Zijlstra89a09142007-03-16 13:38:26 -08001561 /*
1562 * NFS congestion size, scale with available memory.
1563 *
1564 * 64MB: 8192k
1565 * 128MB: 11585k
1566 * 256MB: 16384k
1567 * 512MB: 23170k
1568 * 1GB: 32768k
1569 * 2GB: 46340k
1570 * 4GB: 65536k
1571 * 8GB: 92681k
1572 * 16GB: 131072k
1573 *
1574 * This allows larger machines to have larger/more transfers.
1575 * Limit the default to 256M
1576 */
1577 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1578 if (nfs_congestion_kb > 256*1024)
1579 nfs_congestion_kb = 256*1024;
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return 0;
1582}
1583
David Brownell266bee82006-06-27 12:59:15 -07001584void nfs_destroy_writepagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
1586 mempool_destroy(nfs_commit_mempool);
1587 mempool_destroy(nfs_wdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001588 kmem_cache_destroy(nfs_wdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
1590