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