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