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