]> git.ipfire.org Git - people/ms/linux.git/blame - fs/nfs/write.c
NFS: Make nfs_updatepage() mark the page as dirty.
[people/ms/linux.git] / fs / nfs / write.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/nfs/write.c
3 *
4 * Writing file data over NFS.
5 *
6 * We do it like this: When a (user) process wishes to write data to an
7 * NFS file, a write request is allocated that contains the RPC task data
8 * plus some info on the page to be written, and added to the inode's
9 * write chain. If the process writes past the end of the page, an async
10 * RPC call to write the page is scheduled immediately; otherwise, the call
11 * is delayed for a few seconds.
12 *
13 * Just like readahead, no async I/O is performed if wsize < PAGE_SIZE.
14 *
15 * Write requests are kept on the inode's writeback list. Each entry in
16 * that list references the page (portion) to be written. When the
17 * cache timeout has expired, the RPC task is woken up, and tries to
18 * lock the page. As soon as it manages to do so, the request is moved
19 * from the writeback list to the writelock list.
20 *
21 * Note: we must make sure never to confuse the inode passed in the
22 * write_page request with the one in page->inode. As far as I understand
23 * it, these are different when doing a swap-out.
24 *
25 * To understand everything that goes on here and in the NFS read code,
26 * one should be aware that a page is locked in exactly one of the following
27 * cases:
28 *
29 * - A write request is in progress.
30 * - A user process is in generic_file_write/nfs_update_page
31 * - A user process is in generic_file_read
32 *
33 * Also note that because of the way pages are invalidated in
34 * nfs_revalidate_inode, the following assertions hold:
35 *
36 * - If a page is dirty, there will be no read requests (a page will
37 * not be re-read unless invalidated by nfs_revalidate_inode).
38 * - If the page is not uptodate, there will be no pending write
39 * requests, and no process will be in nfs_update_page.
40 *
41 * FIXME: Interaction with the vmscan routines is not optimal yet.
42 * Either vmscan must be made nfs-savvy, or we need a different page
43 * reclaim concept that supports something like FS-independent
44 * buffer_heads with a b_ops-> field.
45 *
46 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
47 */
48
1da177e4
LT
49#include <linux/types.h>
50#include <linux/slab.h>
51#include <linux/mm.h>
52#include <linux/pagemap.h>
53#include <linux/file.h>
1da177e4
LT
54#include <linux/writeback.h>
55
56#include <linux/sunrpc/clnt.h>
57#include <linux/nfs_fs.h>
58#include <linux/nfs_mount.h>
59#include <linux/nfs_page.h>
3fcfab16
AM
60#include <linux/backing-dev.h>
61
1da177e4
LT
62#include <asm/uaccess.h>
63#include <linux/smp_lock.h>
64
65#include "delegation.h"
49a70f27 66#include "internal.h"
91d5b470 67#include "iostat.h"
1da177e4
LT
68
69#define NFSDBG_FACILITY NFSDBG_PAGECACHE
70
71#define MIN_POOL_WRITE (32)
72#define MIN_POOL_COMMIT (4)
73
74/*
75 * Local function declarations
76 */
77static struct nfs_page * nfs_update_request(struct nfs_open_context*,
1da177e4
LT
78 struct page *,
79 unsigned int, unsigned int);
e261f51f 80static void nfs_mark_request_dirty(struct nfs_page *req);
1da177e4
LT
81static int nfs_wait_on_write_congestion(struct address_space *, int);
82static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int);
3f442547 83static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how);
1c75950b 84static int nfs_wb_page_priority(struct inode *inode, struct page *page, int how);
788e7a89
TM
85static const struct rpc_call_ops nfs_write_partial_ops;
86static const struct rpc_call_ops nfs_write_full_ops;
87static const struct rpc_call_ops nfs_commit_ops;
1da177e4
LT
88
89static kmem_cache_t *nfs_wdata_cachep;
3feb2d49 90static mempool_t *nfs_wdata_mempool;
1da177e4
LT
91static mempool_t *nfs_commit_mempool;
92
93static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
94
e9f7bee1 95struct nfs_write_data *nfs_commit_alloc(void)
1da177e4
LT
96{
97 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS);
40859d7e 98
1da177e4
LT
99 if (p) {
100 memset(p, 0, sizeof(*p));
101 INIT_LIST_HEAD(&p->pages);
102 }
103 return p;
104}
105
8aca67f0 106void nfs_commit_rcu_free(struct rcu_head *head)
1da177e4 107{
8aca67f0 108 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
40859d7e
CL
109 if (p && (p->pagevec != &p->page_array[0]))
110 kfree(p->pagevec);
1da177e4
LT
111 mempool_free(p, nfs_commit_mempool);
112}
113
8aca67f0
TM
114void nfs_commit_free(struct nfs_write_data *wdata)
115{
116 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
117}
118
e9f7bee1 119struct nfs_write_data *nfs_writedata_alloc(size_t len)
3feb2d49 120{
e9f7bee1 121 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3feb2d49
TM
122 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
123
124 if (p) {
125 memset(p, 0, sizeof(*p));
126 INIT_LIST_HEAD(&p->pages);
e9f7bee1 127 p->npages = pagecount;
0d0b5cb3
CL
128 if (pagecount <= ARRAY_SIZE(p->page_array))
129 p->pagevec = p->page_array;
3feb2d49 130 else {
0d0b5cb3
CL
131 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
132 if (!p->pagevec) {
3feb2d49
TM
133 mempool_free(p, nfs_wdata_mempool);
134 p = NULL;
135 }
136 }
137 }
138 return p;
139}
140
8aca67f0 141static void nfs_writedata_rcu_free(struct rcu_head *head)
3feb2d49 142{
8aca67f0 143 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
3feb2d49
TM
144 if (p && (p->pagevec != &p->page_array[0]))
145 kfree(p->pagevec);
146 mempool_free(p, nfs_wdata_mempool);
147}
148
8aca67f0
TM
149static void nfs_writedata_free(struct nfs_write_data *wdata)
150{
151 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
152}
153
963d8fe5 154void nfs_writedata_release(void *wdata)
1da177e4 155{
1da177e4
LT
156 nfs_writedata_free(wdata);
157}
158
277459d2
TM
159static struct nfs_page *nfs_page_find_request_locked(struct page *page)
160{
161 struct nfs_page *req = NULL;
162
163 if (PagePrivate(page)) {
164 req = (struct nfs_page *)page_private(page);
165 if (req != NULL)
166 atomic_inc(&req->wb_count);
167 }
168 return req;
169}
170
171static struct nfs_page *nfs_page_find_request(struct page *page)
172{
173 struct nfs_page *req = NULL;
174 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
175
176 spin_lock(req_lock);
177 req = nfs_page_find_request_locked(page);
178 spin_unlock(req_lock);
179 return req;
180}
181
1da177e4
LT
182/* Adjust the file length if we're writing beyond the end */
183static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
184{
185 struct inode *inode = page->mapping->host;
186 loff_t end, i_size = i_size_read(inode);
187 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
188
189 if (i_size > 0 && page->index < end_index)
190 return;
191 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
192 if (i_size >= end)
193 return;
91d5b470 194 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
1da177e4
LT
195 i_size_write(inode, end);
196}
197
198/* We can set the PG_uptodate flag if we see that a write request
199 * covers the full page.
200 */
201static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
202{
1da177e4
LT
203 if (PageUptodate(page))
204 return;
205 if (base != 0)
206 return;
49a70f27 207 if (count != nfs_page_length(page))
1da177e4 208 return;
49a70f27 209 if (count != PAGE_CACHE_SIZE)
1da177e4 210 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
49a70f27 211 SetPageUptodate(page);
1da177e4
LT
212}
213
e21195a7 214static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
1da177e4
LT
215 unsigned int offset, unsigned int count)
216{
217 struct nfs_page *req;
e21195a7 218 int ret;
1da177e4 219
e21195a7
TM
220 for (;;) {
221 req = nfs_update_request(ctx, page, offset, count);
222 if (!IS_ERR(req))
223 break;
224 ret = PTR_ERR(req);
225 if (ret != -EBUSY)
226 return ret;
227 ret = nfs_wb_page(page->mapping->host, page);
228 if (ret != 0)
229 return ret;
230 }
1da177e4
LT
231 /* Update file length */
232 nfs_grow_file(page, offset, count);
233 /* Set the PG_uptodate flag? */
234 nfs_mark_uptodate(page, offset, count);
235 nfs_unlock_request(req);
abd3e641 236 return 0;
1da177e4
LT
237}
238
239static int wb_priority(struct writeback_control *wbc)
240{
241 if (wbc->for_reclaim)
242 return FLUSH_HIGHPRI;
243 if (wbc->for_kupdate)
244 return FLUSH_LOWPRI;
245 return 0;
246}
247
e261f51f
TM
248/*
249 * Find an associated nfs write request, and prepare to flush it out
250 * Returns 1 if there was no write request, or if the request was
251 * already tagged by nfs_set_page_dirty.Returns 0 if the request
252 * was not tagged.
253 * May also return an error if the user signalled nfs_wait_on_request().
254 */
255static int nfs_page_mark_flush(struct page *page)
256{
257 struct nfs_page *req;
258 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
259 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 }
282 spin_unlock(req_lock);
283 if (test_and_set_bit(PG_FLUSHING, &req->wb_flags) == 0)
284 nfs_mark_request_dirty(req);
285 ret = test_bit(PG_NEED_FLUSH, &req->wb_flags);
286 nfs_unlock_request(req);
287 return ret;
288}
289
1da177e4
LT
290/*
291 * Write an mmapped page to the server.
292 */
4d770ccf 293static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
1da177e4
LT
294{
295 struct nfs_open_context *ctx;
296 struct inode *inode = page->mapping->host;
49a70f27 297 unsigned offset;
e261f51f 298 int err;
1da177e4 299
91d5b470
CL
300 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
301 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
302
e261f51f
TM
303 err = nfs_page_mark_flush(page);
304 if (err <= 0)
305 goto out;
306 err = 0;
49a70f27
TM
307 offset = nfs_page_length(page);
308 if (!offset)
1da177e4 309 goto out;
49a70f27 310
d530838b 311 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
1da177e4
LT
312 if (ctx == NULL) {
313 err = -EBADF;
314 goto out;
315 }
200baa21 316 err = nfs_writepage_setup(ctx, page, 0, offset);
1da177e4 317 put_nfs_open_context(ctx);
e261f51f
TM
318 if (err != 0)
319 goto out;
320 err = nfs_page_mark_flush(page);
321 if (err > 0)
322 err = 0;
1da177e4 323out:
200baa21
TM
324 if (!wbc->for_writepages)
325 nfs_flush_mapping(page->mapping, wbc, wb_priority(wbc));
4d770ccf
TM
326 return err;
327}
328
329int nfs_writepage(struct page *page, struct writeback_control *wbc)
330{
331 int err;
332
333 err = nfs_writepage_locked(page, wbc);
1da177e4 334 unlock_page(page);
1da177e4
LT
335 return err;
336}
337
338/*
339 * Note: causes nfs_update_request() to block on the assumption
340 * that the writeback is generated due to memory pressure.
341 */
342int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
343{
344 struct backing_dev_info *bdi = mapping->backing_dev_info;
345 struct inode *inode = mapping->host;
346 int err;
347
91d5b470
CL
348 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
349
1da177e4
LT
350 err = generic_writepages(mapping, wbc);
351 if (err)
352 return err;
353 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
354 if (wbc->nonblocking)
355 return 0;
356 nfs_wait_on_write_congestion(mapping, 0);
357 }
28c6925f 358 err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
1da177e4
LT
359 if (err < 0)
360 goto out;
91d5b470 361 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
1da177e4
LT
362 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
363 err = nfs_wait_on_requests(inode, 0, 0);
364 if (err < 0)
365 goto out;
366 }
3da28eb1 367 err = nfs_commit_inode(inode, wb_priority(wbc));
3f442547 368 if (err > 0)
1da177e4 369 err = 0;
1da177e4
LT
370out:
371 clear_bit(BDI_write_congested, &bdi->state);
372 wake_up_all(&nfs_write_congestion);
3fcfab16 373 congestion_end(WRITE);
1da177e4
LT
374 return err;
375}
376
377/*
378 * Insert a write request into an inode
379 */
380static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
381{
382 struct nfs_inode *nfsi = NFS_I(inode);
383 int error;
384
385 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
386 BUG_ON(error == -EEXIST);
387 if (error)
388 return error;
389 if (!nfsi->npages) {
390 igrab(inode);
391 nfs_begin_data_update(inode);
392 if (nfs_have_delegation(inode, FMODE_WRITE))
393 nfsi->change_attr++;
394 }
deb7d638 395 SetPagePrivate(req->wb_page);
277459d2 396 set_page_private(req->wb_page, (unsigned long)req);
1da177e4
LT
397 nfsi->npages++;
398 atomic_inc(&req->wb_count);
399 return 0;
400}
401
402/*
403 * Insert a write request into an inode
404 */
405static void nfs_inode_remove_request(struct nfs_page *req)
406{
407 struct inode *inode = req->wb_context->dentry->d_inode;
408 struct nfs_inode *nfsi = NFS_I(inode);
409
410 BUG_ON (!NFS_WBACK_BUSY(req));
411
412 spin_lock(&nfsi->req_lock);
277459d2 413 set_page_private(req->wb_page, 0);
deb7d638 414 ClearPagePrivate(req->wb_page);
1da177e4
LT
415 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
416 nfsi->npages--;
417 if (!nfsi->npages) {
418 spin_unlock(&nfsi->req_lock);
951a143b 419 nfs_end_data_update(inode);
1da177e4
LT
420 iput(inode);
421 } else
422 spin_unlock(&nfsi->req_lock);
423 nfs_clear_request(req);
424 nfs_release_request(req);
425}
426
1da177e4
LT
427/*
428 * Add a request to the inode's dirty list.
429 */
430static void
431nfs_mark_request_dirty(struct nfs_page *req)
432{
433 struct inode *inode = req->wb_context->dentry->d_inode;
434 struct nfs_inode *nfsi = NFS_I(inode);
435
436 spin_lock(&nfsi->req_lock);
3da28eb1
TM
437 radix_tree_tag_set(&nfsi->nfs_page_tree,
438 req->wb_index, NFS_PAGE_TAG_DIRTY);
1da177e4
LT
439 nfs_list_add_request(req, &nfsi->dirty);
440 nfsi->ndirty++;
441 spin_unlock(&nfsi->req_lock);
b1e7a8fd 442 inc_zone_page_state(req->wb_page, NR_FILE_DIRTY);
1da177e4
LT
443 mark_inode_dirty(inode);
444}
445
446/*
447 * Check if a request is dirty
448 */
449static inline int
450nfs_dirty_request(struct nfs_page *req)
451{
e261f51f 452 return test_bit(PG_FLUSHING, &req->wb_flags) == 0;
1da177e4
LT
453}
454
455#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
456/*
457 * Add a request to the inode's commit list.
458 */
459static void
460nfs_mark_request_commit(struct nfs_page *req)
461{
462 struct inode *inode = req->wb_context->dentry->d_inode;
463 struct nfs_inode *nfsi = NFS_I(inode);
464
465 spin_lock(&nfsi->req_lock);
466 nfs_list_add_request(req, &nfsi->commit);
467 nfsi->ncommit++;
468 spin_unlock(&nfsi->req_lock);
fd39fc85 469 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1da177e4
LT
470 mark_inode_dirty(inode);
471}
472#endif
473
474/*
475 * Wait for a request to complete.
476 *
477 * Interruptible by signals only if mounted with intr flag.
478 */
c42de9dd 479static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
1da177e4
LT
480{
481 struct nfs_inode *nfsi = NFS_I(inode);
482 struct nfs_page *req;
483 unsigned long idx_end, next;
484 unsigned int res = 0;
485 int error;
486
487 if (npages == 0)
488 idx_end = ~0;
489 else
490 idx_end = idx_start + npages - 1;
491
1da177e4 492 next = idx_start;
c6a556b8 493 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
1da177e4
LT
494 if (req->wb_index > idx_end)
495 break;
496
497 next = req->wb_index + 1;
c6a556b8 498 BUG_ON(!NFS_WBACK_BUSY(req));
1da177e4
LT
499
500 atomic_inc(&req->wb_count);
501 spin_unlock(&nfsi->req_lock);
502 error = nfs_wait_on_request(req);
503 nfs_release_request(req);
c42de9dd 504 spin_lock(&nfsi->req_lock);
1da177e4
LT
505 if (error < 0)
506 return error;
1da177e4
LT
507 res++;
508 }
1da177e4
LT
509 return res;
510}
511
c42de9dd
TM
512static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
513{
514 struct nfs_inode *nfsi = NFS_I(inode);
515 int ret;
516
517 spin_lock(&nfsi->req_lock);
518 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
519 spin_unlock(&nfsi->req_lock);
520 return ret;
521}
522
83715ad5 523static void nfs_cancel_dirty_list(struct list_head *head)
d2ccddf0
TM
524{
525 struct nfs_page *req;
526 while(!list_empty(head)) {
527 req = nfs_list_entry(head->next);
528 nfs_list_remove_request(req);
529 nfs_inode_remove_request(req);
530 nfs_clear_page_writeback(req);
531 }
532}
533
83715ad5
TM
534static void nfs_cancel_commit_list(struct list_head *head)
535{
536 struct nfs_page *req;
537
538 while(!list_empty(head)) {
539 req = nfs_list_entry(head->next);
b6dff26a 540 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
83715ad5
TM
541 nfs_list_remove_request(req);
542 nfs_inode_remove_request(req);
b6dff26a 543 nfs_unlock_request(req);
83715ad5
TM
544 }
545}
546
1da177e4
LT
547#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
548/*
549 * nfs_scan_commit - Scan an inode for commit requests
550 * @inode: NFS inode to scan
551 * @dst: destination list
552 * @idx_start: lower bound of page->index to scan.
553 * @npages: idx_start + npages sets the upper bound to scan.
554 *
555 * Moves requests from the inode's 'commit' request list.
556 * The requests are *not* checked to ensure that they form a contiguous set.
557 */
558static int
559nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
560{
561 struct nfs_inode *nfsi = NFS_I(inode);
3da28eb1
TM
562 int res = 0;
563
564 if (nfsi->ncommit != 0) {
d2ccddf0 565 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
3da28eb1
TM
566 nfsi->ncommit -= res;
567 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
568 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
569 }
1da177e4
LT
570 return res;
571}
c42de9dd
TM
572#else
573static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
574{
575 return 0;
576}
1da177e4
LT
577#endif
578
579static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
580{
581 struct backing_dev_info *bdi = mapping->backing_dev_info;
582 DEFINE_WAIT(wait);
583 int ret = 0;
584
585 might_sleep();
586
587 if (!bdi_write_congested(bdi))
588 return 0;
91d5b470
CL
589
590 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
591
1da177e4
LT
592 if (intr) {
593 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
594 sigset_t oldset;
595
596 rpc_clnt_sigmask(clnt, &oldset);
597 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
598 if (bdi_write_congested(bdi)) {
599 if (signalled())
600 ret = -ERESTARTSYS;
601 else
602 schedule();
603 }
604 rpc_clnt_sigunmask(clnt, &oldset);
605 } else {
606 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
607 if (bdi_write_congested(bdi))
608 schedule();
609 }
610 finish_wait(&nfs_write_congestion, &wait);
611 return ret;
612}
613
614
615/*
616 * Try to update any existing write request, or create one if there is none.
617 * In order to match, the request's credentials must match those of
618 * the calling process.
619 *
620 * Note: Should always be called with the Page Lock held!
621 */
622static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
e21195a7 623 struct page *page, unsigned int offset, unsigned int bytes)
1da177e4 624{
e21195a7 625 struct inode *inode = page->mapping->host;
1da177e4
LT
626 struct nfs_inode *nfsi = NFS_I(inode);
627 struct nfs_page *req, *new = NULL;
628 unsigned long rqend, end;
629
630 end = offset + bytes;
631
e21195a7 632 if (nfs_wait_on_write_congestion(page->mapping, NFS_SERVER(inode)->flags & NFS_MOUNT_INTR))
1da177e4
LT
633 return ERR_PTR(-ERESTARTSYS);
634 for (;;) {
635 /* Loop over all inode entries and see if we find
636 * A request for the page we wish to update
637 */
638 spin_lock(&nfsi->req_lock);
277459d2 639 req = nfs_page_find_request_locked(page);
1da177e4
LT
640 if (req) {
641 if (!nfs_lock_request_dontget(req)) {
642 int error;
277459d2 643
1da177e4
LT
644 spin_unlock(&nfsi->req_lock);
645 error = nfs_wait_on_request(req);
646 nfs_release_request(req);
1dd594b2
NB
647 if (error < 0) {
648 if (new)
649 nfs_release_request(new);
1da177e4 650 return ERR_PTR(error);
1dd594b2 651 }
1da177e4
LT
652 continue;
653 }
654 spin_unlock(&nfsi->req_lock);
655 if (new)
656 nfs_release_request(new);
657 break;
658 }
659
660 if (new) {
661 int error;
662 nfs_lock_request_dontget(new);
663 error = nfs_inode_add_request(inode, new);
664 if (error) {
665 spin_unlock(&nfsi->req_lock);
666 nfs_unlock_request(new);
667 return ERR_PTR(error);
668 }
669 spin_unlock(&nfsi->req_lock);
1da177e4
LT
670 return new;
671 }
672 spin_unlock(&nfsi->req_lock);
673
674 new = nfs_create_request(ctx, inode, page, offset, bytes);
675 if (IS_ERR(new))
676 return new;
677 }
678
679 /* We have a request for our page.
680 * If the creds don't match, or the
681 * page addresses don't match,
682 * tell the caller to wait on the conflicting
683 * request.
684 */
685 rqend = req->wb_offset + req->wb_bytes;
686 if (req->wb_context != ctx
687 || req->wb_page != page
688 || !nfs_dirty_request(req)
689 || offset > rqend || end < req->wb_offset) {
690 nfs_unlock_request(req);
691 return ERR_PTR(-EBUSY);
692 }
693
694 /* Okay, the request matches. Update the region */
695 if (offset < req->wb_offset) {
696 req->wb_offset = offset;
697 req->wb_pgbase = offset;
698 req->wb_bytes = rqend - req->wb_offset;
699 }
700
701 if (end > rqend)
702 req->wb_bytes = end - req->wb_offset;
703
704 return req;
705}
706
707int nfs_flush_incompatible(struct file *file, struct page *page)
708{
709 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
1da177e4 710 struct nfs_page *req;
1a54533e 711 int do_flush, status;
1da177e4
LT
712 /*
713 * Look for a request corresponding to this page. If there
714 * is one, and it belongs to another file, we flush it out
715 * before we try to copy anything into the page. Do this
716 * due to the lack of an ACCESS-type call in NFSv2.
717 * Also do the same if we find a request from an existing
718 * dropped page.
719 */
1a54533e
TM
720 do {
721 req = nfs_page_find_request(page);
722 if (req == NULL)
723 return 0;
724 do_flush = req->wb_page != page || req->wb_context != ctx
e261f51f 725 || !nfs_dirty_request(req);
1da177e4 726 nfs_release_request(req);
1a54533e
TM
727 if (!do_flush)
728 return 0;
729 status = nfs_wb_page(page->mapping->host, page);
730 } while (status == 0);
731 return status;
1da177e4
LT
732}
733
734/*
735 * Update and possibly write a cached page of an NFS file.
736 *
737 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
738 * things with a page scheduled for an RPC call (e.g. invalidate it).
739 */
740int nfs_updatepage(struct file *file, struct page *page,
741 unsigned int offset, unsigned int count)
742{
743 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
1da177e4 744 struct inode *inode = page->mapping->host;
1da177e4
LT
745 int status = 0;
746
91d5b470
CL
747 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
748
1da177e4 749 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
0bbacc40
CL
750 file->f_dentry->d_parent->d_name.name,
751 file->f_dentry->d_name.name, count,
752 (long long)(page_offset(page) +offset));
1da177e4 753
1da177e4
LT
754 /* If we're not using byte range locks, and we know the page
755 * is entirely in cache, it may be more efficient to avoid
756 * fragmenting write requests.
757 */
ab0a3dbe 758 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
49a70f27 759 count = max(count + offset, nfs_page_length(page));
1da177e4 760 offset = 0;
1da177e4
LT
761 }
762
e21195a7 763 status = nfs_writepage_setup(ctx, page, offset, count);
e261f51f 764 __set_page_dirty_nobuffers(page);
1da177e4 765
1da177e4
LT
766 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
767 status, (long long)i_size_read(inode));
768 if (status < 0)
769 ClearPageUptodate(page);
770 return status;
771}
772
773static void nfs_writepage_release(struct nfs_page *req)
774{
775 end_page_writeback(req->wb_page);
776
777#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
778 if (!PageError(req->wb_page)) {
779 if (NFS_NEED_RESCHED(req)) {
780 nfs_mark_request_dirty(req);
781 goto out;
782 } else if (NFS_NEED_COMMIT(req)) {
783 nfs_mark_request_commit(req);
784 goto out;
785 }
786 }
787 nfs_inode_remove_request(req);
788
789out:
790 nfs_clear_commit(req);
791 nfs_clear_reschedule(req);
792#else
793 nfs_inode_remove_request(req);
794#endif
c6a556b8 795 nfs_clear_page_writeback(req);
1da177e4
LT
796}
797
798static inline int flush_task_priority(int how)
799{
800 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
801 case FLUSH_HIGHPRI:
802 return RPC_PRIORITY_HIGH;
803 case FLUSH_LOWPRI:
804 return RPC_PRIORITY_LOW;
805 }
806 return RPC_PRIORITY_NORMAL;
807}
808
809/*
810 * Set up the argument/result storage required for the RPC call.
811 */
812static void nfs_write_rpcsetup(struct nfs_page *req,
813 struct nfs_write_data *data,
788e7a89 814 const struct rpc_call_ops *call_ops,
1da177e4
LT
815 unsigned int count, unsigned int offset,
816 int how)
817{
1da177e4 818 struct inode *inode;
788e7a89 819 int flags;
1da177e4
LT
820
821 /* Set up the RPC argument and reply structs
822 * NB: take care not to mess about with data->commit et al. */
823
824 data->req = req;
825 data->inode = inode = req->wb_context->dentry->d_inode;
826 data->cred = req->wb_context->cred;
827
828 data->args.fh = NFS_FH(inode);
829 data->args.offset = req_offset(req) + offset;
830 data->args.pgbase = req->wb_pgbase + offset;
831 data->args.pages = data->pagevec;
832 data->args.count = count;
833 data->args.context = req->wb_context;
834
835 data->res.fattr = &data->fattr;
836 data->res.count = count;
837 data->res.verf = &data->verf;
0e574af1 838 nfs_fattr_init(&data->fattr);
1da177e4 839
788e7a89
TM
840 /* Set up the initial task struct. */
841 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
842 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
1da177e4
LT
843 NFS_PROTO(inode)->write_setup(data, how);
844
845 data->task.tk_priority = flush_task_priority(how);
846 data->task.tk_cookie = (unsigned long)inode;
1da177e4
LT
847
848 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
0bbacc40 849 data->task.tk_pid,
1da177e4
LT
850 inode->i_sb->s_id,
851 (long long)NFS_FILEID(inode),
852 count,
853 (unsigned long long)data->args.offset);
854}
855
856static void nfs_execute_write(struct nfs_write_data *data)
857{
858 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
859 sigset_t oldset;
860
861 rpc_clnt_sigmask(clnt, &oldset);
1da177e4 862 rpc_execute(&data->task);
1da177e4
LT
863 rpc_clnt_sigunmask(clnt, &oldset);
864}
865
866/*
867 * Generate multiple small requests to write out a single
868 * contiguous dirty area on one page.
869 */
7d46a49f 870static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
1da177e4
LT
871{
872 struct nfs_page *req = nfs_list_entry(head->next);
873 struct page *page = req->wb_page;
874 struct nfs_write_data *data;
e9f7bee1
TM
875 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
876 unsigned int offset;
1da177e4
LT
877 int requests = 0;
878 LIST_HEAD(list);
879
880 nfs_list_remove_request(req);
881
882 nbytes = req->wb_bytes;
e9f7bee1
TM
883 do {
884 size_t len = min(nbytes, wsize);
885
886 data = nfs_writedata_alloc(len);
1da177e4
LT
887 if (!data)
888 goto out_bad;
889 list_add(&data->pages, &list);
890 requests++;
e9f7bee1
TM
891 nbytes -= len;
892 } while (nbytes != 0);
1da177e4
LT
893 atomic_set(&req->wb_complete, requests);
894
895 ClearPageError(page);
bb713d6d 896 set_page_writeback(page);
1da177e4
LT
897 offset = 0;
898 nbytes = req->wb_bytes;
899 do {
900 data = list_entry(list.next, struct nfs_write_data, pages);
901 list_del_init(&data->pages);
902
903 data->pagevec[0] = page;
1da177e4
LT
904
905 if (nbytes > wsize) {
788e7a89
TM
906 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
907 wsize, offset, how);
1da177e4
LT
908 offset += wsize;
909 nbytes -= wsize;
910 } else {
788e7a89
TM
911 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
912 nbytes, offset, how);
1da177e4
LT
913 nbytes = 0;
914 }
915 nfs_execute_write(data);
916 } while (nbytes != 0);
917
918 return 0;
919
920out_bad:
921 while (!list_empty(&list)) {
922 data = list_entry(list.next, struct nfs_write_data, pages);
923 list_del(&data->pages);
8aca67f0 924 nfs_writedata_release(data);
1da177e4
LT
925 }
926 nfs_mark_request_dirty(req);
c6a556b8 927 nfs_clear_page_writeback(req);
1da177e4
LT
928 return -ENOMEM;
929}
930
931/*
932 * Create an RPC task for the given write request and kick it.
933 * The page must have been locked by the caller.
934 *
935 * It may happen that the page we're passed is not marked dirty.
936 * This is the case if nfs_updatepage detects a conflicting request
937 * that has been written but not committed.
938 */
7d46a49f 939static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
1da177e4
LT
940{
941 struct nfs_page *req;
942 struct page **pages;
943 struct nfs_write_data *data;
944 unsigned int count;
945
e9f7bee1 946 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
1da177e4
LT
947 if (!data)
948 goto out_bad;
949
950 pages = data->pagevec;
951 count = 0;
952 while (!list_empty(head)) {
953 req = nfs_list_entry(head->next);
954 nfs_list_remove_request(req);
955 nfs_list_add_request(req, &data->pages);
956 ClearPageError(req->wb_page);
bb713d6d 957 set_page_writeback(req->wb_page);
1da177e4
LT
958 *pages++ = req->wb_page;
959 count += req->wb_bytes;
960 }
961 req = nfs_list_entry(data->pages.next);
962
1da177e4 963 /* Set up the argument struct */
788e7a89 964 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
1da177e4
LT
965
966 nfs_execute_write(data);
967 return 0;
968 out_bad:
969 while (!list_empty(head)) {
970 struct nfs_page *req = nfs_list_entry(head->next);
971 nfs_list_remove_request(req);
972 nfs_mark_request_dirty(req);
c6a556b8 973 nfs_clear_page_writeback(req);
1da177e4
LT
974 }
975 return -ENOMEM;
976}
977
7d46a49f 978static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
1da177e4
LT
979{
980 LIST_HEAD(one_request);
7d46a49f
TM
981 int (*flush_one)(struct inode *, struct list_head *, int);
982 struct nfs_page *req;
983 int wpages = NFS_SERVER(inode)->wpages;
984 int wsize = NFS_SERVER(inode)->wsize;
985 int error;
1da177e4 986
7d46a49f
TM
987 flush_one = nfs_flush_one;
988 if (wsize < PAGE_CACHE_SIZE)
989 flush_one = nfs_flush_multi;
990 /* For single writes, FLUSH_STABLE is more efficient */
991 if (npages <= wpages && npages == NFS_I(inode)->npages
992 && nfs_list_entry(head->next)->wb_bytes <= wsize)
993 how |= FLUSH_STABLE;
994
995 do {
996 nfs_coalesce_requests(head, &one_request, wpages);
1da177e4 997 req = nfs_list_entry(one_request.next);
7d46a49f 998 error = flush_one(inode, &one_request, how);
1da177e4 999 if (error < 0)
7d46a49f
TM
1000 goto out_err;
1001 } while (!list_empty(head));
1002 return 0;
1003out_err:
1da177e4
LT
1004 while (!list_empty(head)) {
1005 req = nfs_list_entry(head->next);
1006 nfs_list_remove_request(req);
1007 nfs_mark_request_dirty(req);
c6a556b8 1008 nfs_clear_page_writeback(req);
1da177e4
LT
1009 }
1010 return error;
1011}
1012
1013/*
1014 * Handle a write reply that flushed part of a page.
1015 */
788e7a89 1016static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
1da177e4 1017{
788e7a89 1018 struct nfs_write_data *data = calldata;
1da177e4
LT
1019 struct nfs_page *req = data->req;
1020 struct page *page = req->wb_page;
1021
1022 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1023 req->wb_context->dentry->d_inode->i_sb->s_id,
1024 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1025 req->wb_bytes,
1026 (long long)req_offset(req));
1027
788e7a89
TM
1028 if (nfs_writeback_done(task, data) != 0)
1029 return;
1030
1031 if (task->tk_status < 0) {
1da177e4
LT
1032 ClearPageUptodate(page);
1033 SetPageError(page);
788e7a89
TM
1034 req->wb_context->error = task->tk_status;
1035 dprintk(", error = %d\n", task->tk_status);
1da177e4
LT
1036 } else {
1037#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1038 if (data->verf.committed < NFS_FILE_SYNC) {
1039 if (!NFS_NEED_COMMIT(req)) {
1040 nfs_defer_commit(req);
1041 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1042 dprintk(" defer commit\n");
1043 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1044 nfs_defer_reschedule(req);
1045 dprintk(" server reboot detected\n");
1046 }
1047 } else
1048#endif
1049 dprintk(" OK\n");
1050 }
1051
1052 if (atomic_dec_and_test(&req->wb_complete))
1053 nfs_writepage_release(req);
1054}
1055
788e7a89
TM
1056static 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
1da177e4
LT
1061/*
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 */
788e7a89 1068static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1da177e4 1069{
788e7a89 1070 struct nfs_write_data *data = calldata;
1da177e4
LT
1071 struct nfs_page *req;
1072 struct page *page;
1073
788e7a89
TM
1074 if (nfs_writeback_done(task, data) != 0)
1075 return;
1076
1da177e4
LT
1077 /* 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
788e7a89 1089 if (task->tk_status < 0) {
1da177e4
LT
1090 ClearPageUptodate(page);
1091 SetPageError(page);
788e7a89 1092 req->wb_context->error = task->tk_status;
1da177e4
LT
1093 end_page_writeback(page);
1094 nfs_inode_remove_request(req);
788e7a89 1095 dprintk(", error = %d\n", task->tk_status);
1da177e4
LT
1096 goto next;
1097 }
1098 end_page_writeback(page);
1099
1100#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1101 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1102 nfs_inode_remove_request(req);
1103 dprintk(" OK\n");
1104 goto next;
1105 }
1106 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1107 nfs_mark_request_commit(req);
1108 dprintk(" marked for commit\n");
1109#else
1110 nfs_inode_remove_request(req);
1111#endif
1112 next:
c6a556b8 1113 nfs_clear_page_writeback(req);
1da177e4
LT
1114 }
1115}
1116
788e7a89
TM
1117static const struct rpc_call_ops nfs_write_full_ops = {
1118 .rpc_call_done = nfs_writeback_done_full,
1119 .rpc_release = nfs_writedata_release,
1120};
1121
1122
1da177e4
LT
1123/*
1124 * This function is called when the WRITE call is complete.
1125 */
462d5b32 1126int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1da177e4 1127{
1da177e4
LT
1128 struct nfs_writeargs *argp = &data->args;
1129 struct nfs_writeres *resp = &data->res;
788e7a89 1130 int status;
1da177e4
LT
1131
1132 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1133 task->tk_pid, task->tk_status);
1134
f551e44f
CL
1135 /*
1136 * ->write_done will attempt to use post-op attributes to detect
1137 * conflicting writes by other clients. A strict interpretation
1138 * of close-to-open would allow us to continue caching even if
1139 * another writer had changed the file, but some applications
1140 * depend on tighter cache coherency when writing.
1141 */
788e7a89
TM
1142 status = NFS_PROTO(data->inode)->write_done(task, data);
1143 if (status != 0)
1144 return status;
91d5b470
CL
1145 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1146
1da177e4
LT
1147#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1148 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1149 /* We tried a write call, but the server did not
1150 * commit data to stable storage even though we
1151 * requested it.
1152 * Note: There is a known bug in Tru64 < 5.0 in which
1153 * the server reports NFS_DATA_SYNC, but performs
1154 * NFS_FILE_SYNC. We therefore implement this checking
1155 * as a dprintk() in order to avoid filling syslog.
1156 */
1157 static unsigned long complain;
1158
1159 if (time_before(complain, jiffies)) {
1160 dprintk("NFS: faulty NFS server %s:"
1161 " (committed = %d) != (stable = %d)\n",
54ceac45 1162 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
1da177e4
LT
1163 resp->verf->committed, argp->stable);
1164 complain = jiffies + 300 * HZ;
1165 }
1166 }
1167#endif
1168 /* Is this a short write? */
1169 if (task->tk_status >= 0 && resp->count < argp->count) {
1170 static unsigned long complain;
1171
91d5b470
CL
1172 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1173
1da177e4
LT
1174 /* Has the server at least made some progress? */
1175 if (resp->count != 0) {
1176 /* Was this an NFSv2 write or an NFSv3 stable write? */
1177 if (resp->verf->committed != NFS_UNSTABLE) {
1178 /* Resend from where the server left off */
1179 argp->offset += resp->count;
1180 argp->pgbase += resp->count;
1181 argp->count -= resp->count;
1182 } else {
1183 /* Resend as a stable write in order to avoid
1184 * headaches in the case of a server crash.
1185 */
1186 argp->stable = NFS_FILE_SYNC;
1187 }
1188 rpc_restart_call(task);
788e7a89 1189 return -EAGAIN;
1da177e4
LT
1190 }
1191 if (time_before(complain, jiffies)) {
1192 printk(KERN_WARNING
1193 "NFS: Server wrote zero bytes, expected %u.\n",
1194 argp->count);
1195 complain = jiffies + 300 * HZ;
1196 }
1197 /* Can't do anything about it except throw an error. */
1198 task->tk_status = -EIO;
1199 }
788e7a89 1200 return 0;
1da177e4
LT
1201}
1202
1203
1204#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
963d8fe5 1205void nfs_commit_release(void *wdata)
1da177e4 1206{
1da177e4
LT
1207 nfs_commit_free(wdata);
1208}
1209
1210/*
1211 * Set up the argument/result storage required for the RPC call.
1212 */
1213static void nfs_commit_rpcsetup(struct list_head *head,
788e7a89
TM
1214 struct nfs_write_data *data,
1215 int how)
1da177e4 1216{
3da28eb1 1217 struct nfs_page *first;
1da177e4 1218 struct inode *inode;
788e7a89 1219 int flags;
1da177e4
LT
1220
1221 /* Set up the RPC argument and reply structs
1222 * NB: take care not to mess about with data->commit et al. */
1223
1224 list_splice_init(head, &data->pages);
1225 first = nfs_list_entry(data->pages.next);
1da177e4
LT
1226 inode = first->wb_context->dentry->d_inode;
1227
1da177e4
LT
1228 data->inode = inode;
1229 data->cred = first->wb_context->cred;
1230
1231 data->args.fh = NFS_FH(data->inode);
3da28eb1
TM
1232 /* Note: we always request a commit of the entire inode */
1233 data->args.offset = 0;
1234 data->args.count = 0;
1235 data->res.count = 0;
1da177e4
LT
1236 data->res.fattr = &data->fattr;
1237 data->res.verf = &data->verf;
0e574af1 1238 nfs_fattr_init(&data->fattr);
788e7a89
TM
1239
1240 /* Set up the initial task struct. */
1241 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1242 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
1da177e4
LT
1243 NFS_PROTO(inode)->commit_setup(data, how);
1244
1245 data->task.tk_priority = flush_task_priority(how);
1246 data->task.tk_cookie = (unsigned long)inode;
1da177e4 1247
0bbacc40 1248 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
1da177e4
LT
1249}
1250
1251/*
1252 * Commit dirty pages
1253 */
1254static int
40859d7e 1255nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1da177e4
LT
1256{
1257 struct nfs_write_data *data;
1258 struct nfs_page *req;
1259
e9f7bee1 1260 data = nfs_commit_alloc();
1da177e4
LT
1261
1262 if (!data)
1263 goto out_bad;
1264
1265 /* Set up the argument struct */
1266 nfs_commit_rpcsetup(head, data, how);
1267
1268 nfs_execute_write(data);
1269 return 0;
1270 out_bad:
1271 while (!list_empty(head)) {
1272 req = nfs_list_entry(head->next);
1273 nfs_list_remove_request(req);
1274 nfs_mark_request_commit(req);
83715ad5 1275 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
5c2d97cb 1276 nfs_clear_page_writeback(req);
1da177e4
LT
1277 }
1278 return -ENOMEM;
1279}
1280
1281/*
1282 * COMMIT call returned
1283 */
788e7a89 1284static void nfs_commit_done(struct rpc_task *task, void *calldata)
1da177e4 1285{
963d8fe5 1286 struct nfs_write_data *data = calldata;
1da177e4 1287 struct nfs_page *req;
1da177e4
LT
1288
1289 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1290 task->tk_pid, task->tk_status);
1291
788e7a89
TM
1292 /* Call the NFS version-specific code */
1293 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1294 return;
1295
1da177e4
LT
1296 while (!list_empty(&data->pages)) {
1297 req = nfs_list_entry(data->pages.next);
1298 nfs_list_remove_request(req);
fd39fc85 1299 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1da177e4
LT
1300
1301 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1302 req->wb_context->dentry->d_inode->i_sb->s_id,
1303 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1304 req->wb_bytes,
1305 (long long)req_offset(req));
1306 if (task->tk_status < 0) {
1307 req->wb_context->error = task->tk_status;
1308 nfs_inode_remove_request(req);
1309 dprintk(", error = %d\n", task->tk_status);
1310 goto next;
1311 }
1312
1313 /* Okay, COMMIT succeeded, apparently. Check the verifier
1314 * returned by the server against all stored verfs. */
1315 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1316 /* We have a match */
1317 nfs_inode_remove_request(req);
1318 dprintk(" OK\n");
1319 goto next;
1320 }
1321 /* We have a mismatch. Write the page again */
1322 dprintk(" mismatch\n");
1323 nfs_mark_request_dirty(req);
1324 next:
c6a556b8 1325 nfs_clear_page_writeback(req);
1da177e4 1326 }
1da177e4 1327}
788e7a89
TM
1328
1329static const struct rpc_call_ops nfs_commit_ops = {
1330 .rpc_call_done = nfs_commit_done,
1331 .rpc_release = nfs_commit_release,
1332};
c42de9dd
TM
1333#else
1334static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1335{
1336 return 0;
1337}
1da177e4
LT
1338#endif
1339
3f442547 1340static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
1da177e4 1341{
28c6925f 1342 struct nfs_inode *nfsi = NFS_I(mapping->host);
1da177e4 1343 LIST_HEAD(head);
3f442547 1344 long res;
1da177e4
LT
1345
1346 spin_lock(&nfsi->req_lock);
3f442547 1347 res = nfs_scan_dirty(mapping, wbc, &head);
1da177e4 1348 spin_unlock(&nfsi->req_lock);
ab0a3dbe 1349 if (res) {
28c6925f 1350 int error = nfs_flush_list(mapping->host, &head, res, how);
7d46a49f
TM
1351 if (error < 0)
1352 return error;
ab0a3dbe 1353 }
1da177e4
LT
1354 return res;
1355}
1356
1357#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
3da28eb1 1358int nfs_commit_inode(struct inode *inode, int how)
1da177e4
LT
1359{
1360 struct nfs_inode *nfsi = NFS_I(inode);
1361 LIST_HEAD(head);
7d46a49f 1362 int res;
1da177e4
LT
1363
1364 spin_lock(&nfsi->req_lock);
3da28eb1
TM
1365 res = nfs_scan_commit(inode, &head, 0, 0);
1366 spin_unlock(&nfsi->req_lock);
1da177e4 1367 if (res) {
7d46a49f 1368 int error = nfs_commit_list(inode, &head, how);
3da28eb1
TM
1369 if (error < 0)
1370 return error;
1371 }
1da177e4
LT
1372 return res;
1373}
1374#endif
1375
1c75950b 1376long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
1da177e4 1377{
1c75950b 1378 struct inode *inode = mapping->host;
c42de9dd 1379 struct nfs_inode *nfsi = NFS_I(inode);
1c75950b
TM
1380 unsigned long idx_start, idx_end;
1381 unsigned int npages = 0;
c42de9dd 1382 LIST_HEAD(head);
70b9ecbd 1383 int nocommit = how & FLUSH_NOCOMMIT;
3f442547 1384 long pages, ret;
1da177e4 1385
1c75950b
TM
1386 /* FIXME */
1387 if (wbc->range_cyclic)
1388 idx_start = 0;
1389 else {
1390 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1391 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1392 if (idx_end > idx_start) {
1393 unsigned long l_npages = 1 + idx_end - idx_start;
1394 npages = l_npages;
1395 if (sizeof(npages) != sizeof(l_npages) &&
1396 (unsigned long)npages != l_npages)
1397 npages = 0;
1398 }
1399 }
c42de9dd
TM
1400 how &= ~FLUSH_NOCOMMIT;
1401 spin_lock(&nfsi->req_lock);
1da177e4 1402 do {
1c75950b 1403 wbc->pages_skipped = 0;
c42de9dd
TM
1404 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1405 if (ret != 0)
70b9ecbd 1406 continue;
1c75950b 1407 pages = nfs_scan_dirty(mapping, wbc, &head);
c42de9dd
TM
1408 if (pages != 0) {
1409 spin_unlock(&nfsi->req_lock);
e8e058e8 1410 if (how & FLUSH_INVALIDATE) {
83715ad5 1411 nfs_cancel_dirty_list(&head);
e8e058e8
TM
1412 ret = pages;
1413 } else
d2ccddf0 1414 ret = nfs_flush_list(inode, &head, pages, how);
c42de9dd
TM
1415 spin_lock(&nfsi->req_lock);
1416 continue;
1417 }
1c75950b
TM
1418 if (wbc->pages_skipped != 0)
1419 continue;
c42de9dd
TM
1420 if (nocommit)
1421 break;
d2ccddf0 1422 pages = nfs_scan_commit(inode, &head, idx_start, npages);
1c75950b
TM
1423 if (pages == 0) {
1424 if (wbc->pages_skipped != 0)
1425 continue;
c42de9dd 1426 break;
1c75950b 1427 }
d2ccddf0
TM
1428 if (how & FLUSH_INVALIDATE) {
1429 spin_unlock(&nfsi->req_lock);
83715ad5 1430 nfs_cancel_commit_list(&head);
e8e058e8 1431 ret = pages;
d2ccddf0
TM
1432 spin_lock(&nfsi->req_lock);
1433 continue;
1434 }
1435 pages += nfs_scan_commit(inode, &head, 0, 0);
c42de9dd
TM
1436 spin_unlock(&nfsi->req_lock);
1437 ret = nfs_commit_list(inode, &head, how);
1438 spin_lock(&nfsi->req_lock);
1439 } while (ret >= 0);
1440 spin_unlock(&nfsi->req_lock);
1441 return ret;
1da177e4
LT
1442}
1443
1c75950b
TM
1444/*
1445 * flush the inode to disk.
1446 */
1447int nfs_wb_all(struct inode *inode)
1448{
1449 struct address_space *mapping = inode->i_mapping;
1450 struct writeback_control wbc = {
1451 .bdi = mapping->backing_dev_info,
1452 .sync_mode = WB_SYNC_ALL,
1453 .nr_to_write = LONG_MAX,
1454 .range_cyclic = 1,
1455 };
1456 int ret;
1457
1458 ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
1459 if (ret >= 0)
1460 return 0;
1461 return ret;
1462}
1463
1464int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
1465{
1466 struct writeback_control wbc = {
1467 .bdi = mapping->backing_dev_info,
1468 .sync_mode = WB_SYNC_ALL,
1469 .nr_to_write = LONG_MAX,
1470 .range_start = range_start,
1471 .range_end = range_end,
1472 };
1473 int ret;
1474
1475 ret = nfs_sync_mapping_wait(mapping, &wbc, how);
1476 if (ret >= 0)
1477 return 0;
1478 return ret;
1479}
1480
1481static int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
1482{
1483 loff_t range_start = page_offset(page);
1484 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
4d770ccf
TM
1485 struct writeback_control wbc = {
1486 .bdi = page->mapping->backing_dev_info,
1487 .sync_mode = WB_SYNC_ALL,
1488 .nr_to_write = LONG_MAX,
1489 .range_start = range_start,
1490 .range_end = range_end,
1491 };
1492 int ret;
1c75950b 1493
4d770ccf
TM
1494 BUG_ON(!PageLocked(page));
1495 if (!(how & FLUSH_NOWRITEPAGE) && clear_page_dirty_for_io(page)) {
1496 ret = nfs_writepage_locked(page, &wbc);
1497 if (ret < 0)
1498 goto out;
1499 }
1500 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1501 if (ret >= 0)
1502 return 0;
1503out:
1504 return ret;
1c75950b
TM
1505}
1506
1507/*
1508 * Write back all requests on one page - we do this before reading it.
1509 */
1510int nfs_wb_page(struct inode *inode, struct page* page)
1511{
4d770ccf 1512 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
1c75950b
TM
1513}
1514
1a54533e
TM
1515int nfs_set_page_dirty(struct page *page)
1516{
1517 struct nfs_page *req;
1518
1519 req = nfs_page_find_request(page);
1520 if (req != NULL) {
1521 /* Mark any existing write requests for flushing */
1522 set_bit(PG_NEED_FLUSH, &req->wb_flags);
1523 nfs_release_request(req);
1524 }
1525 return __set_page_dirty_nobuffers(page);
1526}
1527
1c75950b 1528
f7b422b1 1529int __init nfs_init_writepagecache(void)
1da177e4
LT
1530{
1531 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1532 sizeof(struct nfs_write_data),
1533 0, SLAB_HWCACHE_ALIGN,
1534 NULL, NULL);
1535 if (nfs_wdata_cachep == NULL)
1536 return -ENOMEM;
1537
93d2341c
MD
1538 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1539 nfs_wdata_cachep);
1da177e4
LT
1540 if (nfs_wdata_mempool == NULL)
1541 return -ENOMEM;
1542
93d2341c
MD
1543 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1544 nfs_wdata_cachep);
1da177e4
LT
1545 if (nfs_commit_mempool == NULL)
1546 return -ENOMEM;
1547
1548 return 0;
1549}
1550
266bee88 1551void nfs_destroy_writepagecache(void)
1da177e4
LT
1552{
1553 mempool_destroy(nfs_commit_mempool);
1554 mempool_destroy(nfs_wdata_mempool);
1a1d92c1 1555 kmem_cache_destroy(nfs_wdata_cachep);
1da177e4
LT
1556}
1557