SUNRPC: Don't attempt to destroy expired RPCSEC_GSS credentials..
[deliverable/linux.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
e9f7bee1 51struct nfs_write_data *nfs_commit_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
TM
417 return 0;
418 return !PageWriteback(req->wb_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 */
27852596
NP
587 if (new) {
588 if (radix_tree_preload(GFP_NOFS)) {
589 nfs_release_request(new);
590 return ERR_PTR(-ENOMEM);
591 }
592 }
593
587142f8 594 spin_lock(&inode->i_lock);
277459d2 595 req = nfs_page_find_request_locked(page);
1da177e4 596 if (req) {
acee478a 597 if (!nfs_set_page_tag_locked(req)) {
1da177e4 598 int error;
277459d2 599
587142f8 600 spin_unlock(&inode->i_lock);
1da177e4
LT
601 error = nfs_wait_on_request(req);
602 nfs_release_request(req);
1dd594b2 603 if (error < 0) {
27852596
NP
604 if (new) {
605 radix_tree_preload_end();
1dd594b2 606 nfs_release_request(new);
27852596 607 }
1da177e4 608 return ERR_PTR(error);
1dd594b2 609 }
1da177e4
LT
610 continue;
611 }
587142f8 612 spin_unlock(&inode->i_lock);
27852596
NP
613 if (new) {
614 radix_tree_preload_end();
1da177e4 615 nfs_release_request(new);
27852596 616 }
1da177e4
LT
617 break;
618 }
619
620 if (new) {
1da177e4 621 nfs_lock_request_dontget(new);
27852596 622 nfs_inode_add_request(inode, new);
587142f8 623 spin_unlock(&inode->i_lock);
27852596 624 radix_tree_preload_end();
61e930a9
TM
625 req = new;
626 goto zero_page;
1da177e4 627 }
587142f8 628 spin_unlock(&inode->i_lock);
1da177e4
LT
629
630 new = nfs_create_request(ctx, inode, page, offset, bytes);
631 if (IS_ERR(new))
632 return new;
633 }
634
635 /* We have a request for our page.
636 * If the creds don't match, or the
637 * page addresses don't match,
638 * tell the caller to wait on the conflicting
639 * request.
640 */
641 rqend = req->wb_offset + req->wb_bytes;
642 if (req->wb_context != ctx
643 || req->wb_page != page
644 || !nfs_dirty_request(req)
645 || offset > rqend || end < req->wb_offset) {
acee478a 646 nfs_clear_page_tag_locked(req);
1da177e4
LT
647 return ERR_PTR(-EBUSY);
648 }
649
650 /* Okay, the request matches. Update the region */
651 if (offset < req->wb_offset) {
652 req->wb_offset = offset;
653 req->wb_pgbase = offset;
61e930a9
TM
654 req->wb_bytes = max(end, rqend) - req->wb_offset;
655 goto zero_page;
1da177e4
LT
656 }
657
658 if (end > rqend)
659 req->wb_bytes = end - req->wb_offset;
660
661 return req;
61e930a9
TM
662zero_page:
663 /* If this page might potentially be marked as up to date,
664 * then we need to zero any uninitalised data. */
665 if (req->wb_pgbase == 0 && req->wb_bytes != PAGE_CACHE_SIZE
666 && !PageUptodate(req->wb_page))
eebd2aa3 667 zero_user_segment(req->wb_page, req->wb_bytes, PAGE_CACHE_SIZE);
61e930a9 668 return req;
1da177e4
LT
669}
670
671int nfs_flush_incompatible(struct file *file, struct page *page)
672{
cd3758e3 673 struct nfs_open_context *ctx = nfs_file_open_context(file);
1da177e4 674 struct nfs_page *req;
1a54533e 675 int do_flush, status;
1da177e4
LT
676 /*
677 * Look for a request corresponding to this page. If there
678 * is one, and it belongs to another file, we flush it out
679 * before we try to copy anything into the page. Do this
680 * due to the lack of an ACCESS-type call in NFSv2.
681 * Also do the same if we find a request from an existing
682 * dropped page.
683 */
1a54533e
TM
684 do {
685 req = nfs_page_find_request(page);
686 if (req == NULL)
687 return 0;
688 do_flush = req->wb_page != page || req->wb_context != ctx
e261f51f 689 || !nfs_dirty_request(req);
1da177e4 690 nfs_release_request(req);
1a54533e
TM
691 if (!do_flush)
692 return 0;
693 status = nfs_wb_page(page->mapping->host, page);
694 } while (status == 0);
695 return status;
1da177e4
LT
696}
697
5d47a356
TM
698/*
699 * If the page cache is marked as unsafe or invalid, then we can't rely on
700 * the PageUptodate() flag. In this case, we will need to turn off
701 * write optimisations that depend on the page contents being correct.
702 */
703static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
704{
705 return PageUptodate(page) &&
706 !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
707}
708
1da177e4
LT
709/*
710 * Update and possibly write a cached page of an NFS file.
711 *
712 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
713 * things with a page scheduled for an RPC call (e.g. invalidate it).
714 */
715int nfs_updatepage(struct file *file, struct page *page,
716 unsigned int offset, unsigned int count)
717{
cd3758e3 718 struct nfs_open_context *ctx = nfs_file_open_context(file);
1da177e4 719 struct inode *inode = page->mapping->host;
1da177e4
LT
720 int status = 0;
721
91d5b470
CL
722 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
723
1da177e4 724 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
01cce933
JJS
725 file->f_path.dentry->d_parent->d_name.name,
726 file->f_path.dentry->d_name.name, count,
0bbacc40 727 (long long)(page_offset(page) +offset));
1da177e4 728
1da177e4 729 /* If we're not using byte range locks, and we know the page
5d47a356
TM
730 * is up to date, it may be more efficient to extend the write
731 * to cover the entire page in order to avoid fragmentation
732 * inefficiencies.
1da177e4 733 */
5d47a356
TM
734 if (nfs_write_pageuptodate(page, inode) &&
735 inode->i_flock == NULL &&
4b5621f6 736 !(file->f_flags & O_SYNC)) {
49a70f27 737 count = max(count + offset, nfs_page_length(page));
1da177e4 738 offset = 0;
1da177e4
LT
739 }
740
e21195a7 741 status = nfs_writepage_setup(ctx, page, offset, count);
e261f51f 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));
746 if (status < 0)
a301b777 747 nfs_set_pageerror(page);
1da177e4
LT
748 return status;
749}
750
751static void nfs_writepage_release(struct nfs_page *req)
752{
1da177e4 753
44dd151d
TM
754 if (PageError(req->wb_page)) {
755 nfs_end_page_writeback(req->wb_page);
756 nfs_inode_remove_request(req);
757 } else if (!nfs_reschedule_unstable_write(req)) {
758 /* Set the PG_uptodate flag */
759 nfs_mark_uptodate(req->wb_page, req->wb_pgbase, req->wb_bytes);
8e821cad
TM
760 nfs_end_page_writeback(req->wb_page);
761 nfs_inode_remove_request(req);
762 } else
763 nfs_end_page_writeback(req->wb_page);
9fd367f0 764 nfs_clear_page_tag_locked(req);
1da177e4
LT
765}
766
3ff7576d 767static int flush_task_priority(int how)
1da177e4
LT
768{
769 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
770 case FLUSH_HIGHPRI:
771 return RPC_PRIORITY_HIGH;
772 case FLUSH_LOWPRI:
773 return RPC_PRIORITY_LOW;
774 }
775 return RPC_PRIORITY_NORMAL;
776}
777
778/*
779 * Set up the argument/result storage required for the RPC call.
780 */
781static void nfs_write_rpcsetup(struct nfs_page *req,
782 struct nfs_write_data *data,
788e7a89 783 const struct rpc_call_ops *call_ops,
1da177e4
LT
784 unsigned int count, unsigned int offset,
785 int how)
786{
84115e1c
TM
787 struct inode *inode = req->wb_context->path.dentry->d_inode;
788 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
3ff7576d 789 int priority = flush_task_priority(how);
07737691 790 struct rpc_task *task;
bdc7f021
TM
791 struct rpc_message msg = {
792 .rpc_argp = &data->args,
793 .rpc_resp = &data->res,
794 .rpc_cred = req->wb_context->cred,
795 };
84115e1c
TM
796 struct rpc_task_setup task_setup_data = {
797 .rpc_client = NFS_CLIENT(inode),
07737691 798 .task = &data->task,
bdc7f021 799 .rpc_message = &msg,
84115e1c
TM
800 .callback_ops = call_ops,
801 .callback_data = data,
101070ca 802 .workqueue = nfsiod_workqueue,
84115e1c 803 .flags = flags,
3ff7576d 804 .priority = priority,
84115e1c 805 };
1da177e4
LT
806
807 /* Set up the RPC argument and reply structs
808 * NB: take care not to mess about with data->commit et al. */
809
810 data->req = req;
88be9f99 811 data->inode = inode = req->wb_context->path.dentry->d_inode;
bdc7f021 812 data->cred = msg.rpc_cred;
1da177e4
LT
813
814 data->args.fh = NFS_FH(inode);
815 data->args.offset = req_offset(req) + offset;
816 data->args.pgbase = req->wb_pgbase + offset;
817 data->args.pages = data->pagevec;
818 data->args.count = count;
383ba719 819 data->args.context = get_nfs_open_context(req->wb_context);
bdc7f021
TM
820 data->args.stable = NFS_UNSTABLE;
821 if (how & FLUSH_STABLE) {
822 data->args.stable = NFS_DATA_SYNC;
823 if (!NFS_I(inode)->ncommit)
824 data->args.stable = NFS_FILE_SYNC;
825 }
1da177e4
LT
826
827 data->res.fattr = &data->fattr;
828 data->res.count = count;
829 data->res.verf = &data->verf;
0e574af1 830 nfs_fattr_init(&data->fattr);
1da177e4 831
788e7a89 832 /* Set up the initial task struct. */
bdc7f021 833 NFS_PROTO(inode)->write_setup(data, &msg);
1da177e4 834
a3f565b1
CL
835 dprintk("NFS: %5u initiated write call "
836 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
0bbacc40 837 data->task.tk_pid,
1da177e4
LT
838 inode->i_sb->s_id,
839 (long long)NFS_FILEID(inode),
840 count,
841 (unsigned long long)data->args.offset);
1da177e4 842
07737691
TM
843 task = rpc_run_task(&task_setup_data);
844 if (!IS_ERR(task))
845 rpc_put_task(task);
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
LT
870 int requests = 0;
871 LIST_HEAD(list);
872
873 nfs_list_remove_request(req);
874
bcb71bba 875 nbytes = count;
e9f7bee1
TM
876 do {
877 size_t len = min(nbytes, wsize);
878
8d5658c9 879 data = nfs_writedata_alloc(1);
1da177e4
LT
880 if (!data)
881 goto out_bad;
882 list_add(&data->pages, &list);
883 requests++;
e9f7bee1
TM
884 nbytes -= len;
885 } while (nbytes != 0);
1da177e4
LT
886 atomic_set(&req->wb_complete, requests);
887
888 ClearPageError(page);
1da177e4 889 offset = 0;
bcb71bba 890 nbytes = count;
1da177e4
LT
891 do {
892 data = list_entry(list.next, struct nfs_write_data, pages);
893 list_del_init(&data->pages);
894
895 data->pagevec[0] = page;
1da177e4 896
bcb71bba
TM
897 if (nbytes < wsize)
898 wsize = nbytes;
899 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
900 wsize, offset, how);
901 offset += wsize;
902 nbytes -= wsize;
1da177e4
LT
903 } while (nbytes != 0);
904
905 return 0;
906
907out_bad:
908 while (!list_empty(&list)) {
909 data = list_entry(list.next, struct nfs_write_data, pages);
910 list_del(&data->pages);
8aca67f0 911 nfs_writedata_release(data);
1da177e4 912 }
61822ab5 913 nfs_redirty_request(req);
1da177e4
LT
914 return -ENOMEM;
915}
916
917/*
918 * Create an RPC task for the given write request and kick it.
919 * The page must have been locked by the caller.
920 *
921 * It may happen that the page we're passed is not marked dirty.
922 * This is the case if nfs_updatepage detects a conflicting request
923 * that has been written but not committed.
924 */
8d5658c9 925static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
1da177e4
LT
926{
927 struct nfs_page *req;
928 struct page **pages;
929 struct nfs_write_data *data;
1da177e4 930
8d5658c9 931 data = nfs_writedata_alloc(npages);
1da177e4
LT
932 if (!data)
933 goto out_bad;
934
935 pages = data->pagevec;
1da177e4
LT
936 while (!list_empty(head)) {
937 req = nfs_list_entry(head->next);
938 nfs_list_remove_request(req);
939 nfs_list_add_request(req, &data->pages);
940 ClearPageError(req->wb_page);
1da177e4 941 *pages++ = req->wb_page;
1da177e4
LT
942 }
943 req = nfs_list_entry(data->pages.next);
944
1da177e4 945 /* Set up the argument struct */
788e7a89 946 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
1da177e4 947
1da177e4
LT
948 return 0;
949 out_bad:
950 while (!list_empty(head)) {
10afec90 951 req = nfs_list_entry(head->next);
1da177e4 952 nfs_list_remove_request(req);
61822ab5 953 nfs_redirty_request(req);
1da177e4
LT
954 }
955 return -ENOMEM;
956}
957
c63c7b05
TM
958static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
959 struct inode *inode, int ioflags)
1da177e4 960{
bf4285e7 961 size_t wsize = NFS_SERVER(inode)->wsize;
1da177e4 962
bcb71bba 963 if (wsize < PAGE_CACHE_SIZE)
c63c7b05 964 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
bcb71bba 965 else
c63c7b05 966 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
1da177e4
LT
967}
968
969/*
970 * Handle a write reply that flushed part of a page.
971 */
788e7a89 972static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
1da177e4 973{
788e7a89 974 struct nfs_write_data *data = calldata;
1da177e4
LT
975 struct nfs_page *req = data->req;
976 struct page *page = req->wb_page;
977
978 dprintk("NFS: write (%s/%Ld %d@%Ld)",
88be9f99
TM
979 req->wb_context->path.dentry->d_inode->i_sb->s_id,
980 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1da177e4
LT
981 req->wb_bytes,
982 (long long)req_offset(req));
983
788e7a89
TM
984 if (nfs_writeback_done(task, data) != 0)
985 return;
986
987 if (task->tk_status < 0) {
a301b777 988 nfs_set_pageerror(page);
7b159fc1 989 nfs_context_set_write_error(req->wb_context, task->tk_status);
788e7a89 990 dprintk(", error = %d\n", task->tk_status);
8e821cad 991 goto out;
1da177e4
LT
992 }
993
8e821cad 994 if (nfs_write_need_commit(data)) {
587142f8 995 struct inode *inode = page->mapping->host;
8e821cad 996
587142f8 997 spin_lock(&inode->i_lock);
8e821cad
TM
998 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
999 /* Do nothing we need to resend the writes */
1000 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1001 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1002 dprintk(" defer commit\n");
1003 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1004 set_bit(PG_NEED_RESCHED, &req->wb_flags);
1005 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
1006 dprintk(" server reboot detected\n");
1007 }
587142f8 1008 spin_unlock(&inode->i_lock);
8e821cad
TM
1009 } else
1010 dprintk(" OK\n");
1011
1012out:
1da177e4
LT
1013 if (atomic_dec_and_test(&req->wb_complete))
1014 nfs_writepage_release(req);
1015}
1016
788e7a89
TM
1017static const struct rpc_call_ops nfs_write_partial_ops = {
1018 .rpc_call_done = nfs_writeback_done_partial,
1019 .rpc_release = nfs_writedata_release,
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
LT
1032 struct nfs_page *req;
1033 struct page *page;
1034
788e7a89
TM
1035 if (nfs_writeback_done(task, data) != 0)
1036 return;
1037
1da177e4
LT
1038 /* Update attributes as result of writeback. */
1039 while (!list_empty(&data->pages)) {
1040 req = nfs_list_entry(data->pages.next);
1041 nfs_list_remove_request(req);
1042 page = req->wb_page;
1043
1044 dprintk("NFS: write (%s/%Ld %d@%Ld)",
88be9f99
TM
1045 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1046 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1da177e4
LT
1047 req->wb_bytes,
1048 (long long)req_offset(req));
1049
788e7a89 1050 if (task->tk_status < 0) {
a301b777 1051 nfs_set_pageerror(page);
7b159fc1 1052 nfs_context_set_write_error(req->wb_context, task->tk_status);
788e7a89 1053 dprintk(", error = %d\n", task->tk_status);
8e821cad 1054 goto remove_request;
1da177e4 1055 }
1da177e4 1056
8e821cad
TM
1057 if (nfs_write_need_commit(data)) {
1058 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1059 nfs_mark_request_commit(req);
1060 nfs_end_page_writeback(page);
1061 dprintk(" marked for commit\n");
1da177e4
LT
1062 goto next;
1063 }
44dd151d
TM
1064 /* Set the PG_uptodate flag? */
1065 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
8e821cad
TM
1066 dprintk(" OK\n");
1067remove_request:
1068 nfs_end_page_writeback(page);
1da177e4 1069 nfs_inode_remove_request(req);
1da177e4 1070 next:
9fd367f0 1071 nfs_clear_page_tag_locked(req);
1da177e4
LT
1072 }
1073}
1074
788e7a89
TM
1075static const struct rpc_call_ops nfs_write_full_ops = {
1076 .rpc_call_done = nfs_writeback_done_full,
1077 .rpc_release = nfs_writedata_release,
1078};
1079
1080
1da177e4
LT
1081/*
1082 * This function is called when the WRITE call is complete.
1083 */
462d5b32 1084int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1da177e4 1085{
1da177e4
LT
1086 struct nfs_writeargs *argp = &data->args;
1087 struct nfs_writeres *resp = &data->res;
788e7a89 1088 int status;
1da177e4 1089
a3f565b1 1090 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1da177e4
LT
1091 task->tk_pid, task->tk_status);
1092
f551e44f
CL
1093 /*
1094 * ->write_done will attempt to use post-op attributes to detect
1095 * conflicting writes by other clients. A strict interpretation
1096 * of close-to-open would allow us to continue caching even if
1097 * another writer had changed the file, but some applications
1098 * depend on tighter cache coherency when writing.
1099 */
788e7a89
TM
1100 status = NFS_PROTO(data->inode)->write_done(task, data);
1101 if (status != 0)
1102 return status;
91d5b470
CL
1103 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1104
1da177e4
LT
1105#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1106 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1107 /* We tried a write call, but the server did not
1108 * commit data to stable storage even though we
1109 * requested it.
1110 * Note: There is a known bug in Tru64 < 5.0 in which
1111 * the server reports NFS_DATA_SYNC, but performs
1112 * NFS_FILE_SYNC. We therefore implement this checking
1113 * as a dprintk() in order to avoid filling syslog.
1114 */
1115 static unsigned long complain;
1116
1117 if (time_before(complain, jiffies)) {
1118 dprintk("NFS: faulty NFS server %s:"
1119 " (committed = %d) != (stable = %d)\n",
54ceac45 1120 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
1da177e4
LT
1121 resp->verf->committed, argp->stable);
1122 complain = jiffies + 300 * HZ;
1123 }
1124 }
1125#endif
1126 /* Is this a short write? */
1127 if (task->tk_status >= 0 && resp->count < argp->count) {
1128 static unsigned long complain;
1129
91d5b470
CL
1130 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1131
1da177e4
LT
1132 /* Has the server at least made some progress? */
1133 if (resp->count != 0) {
1134 /* Was this an NFSv2 write or an NFSv3 stable write? */
1135 if (resp->verf->committed != NFS_UNSTABLE) {
1136 /* Resend from where the server left off */
1137 argp->offset += resp->count;
1138 argp->pgbase += resp->count;
1139 argp->count -= resp->count;
1140 } else {
1141 /* Resend as a stable write in order to avoid
1142 * headaches in the case of a server crash.
1143 */
1144 argp->stable = NFS_FILE_SYNC;
1145 }
1146 rpc_restart_call(task);
788e7a89 1147 return -EAGAIN;
1da177e4
LT
1148 }
1149 if (time_before(complain, jiffies)) {
1150 printk(KERN_WARNING
1151 "NFS: Server wrote zero bytes, expected %u.\n",
1152 argp->count);
1153 complain = jiffies + 300 * HZ;
1154 }
1155 /* Can't do anything about it except throw an error. */
1156 task->tk_status = -EIO;
1157 }
788e7a89 1158 return 0;
1da177e4
LT
1159}
1160
1161
1162#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
383ba719 1163void nfs_commit_release(void *data)
1da177e4 1164{
383ba719
TM
1165 struct nfs_write_data *wdata = data;
1166
1167 put_nfs_open_context(wdata->args.context);
1da177e4
LT
1168 nfs_commit_free(wdata);
1169}
1170
1171/*
1172 * Set up the argument/result storage required for the RPC call.
1173 */
1174static void nfs_commit_rpcsetup(struct list_head *head,
788e7a89
TM
1175 struct nfs_write_data *data,
1176 int how)
1da177e4 1177{
84115e1c
TM
1178 struct nfs_page *first = nfs_list_entry(head->next);
1179 struct inode *inode = first->wb_context->path.dentry->d_inode;
1180 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
3ff7576d 1181 int priority = flush_task_priority(how);
07737691 1182 struct rpc_task *task;
bdc7f021
TM
1183 struct rpc_message msg = {
1184 .rpc_argp = &data->args,
1185 .rpc_resp = &data->res,
1186 .rpc_cred = first->wb_context->cred,
1187 };
84115e1c 1188 struct rpc_task_setup task_setup_data = {
07737691 1189 .task = &data->task,
84115e1c 1190 .rpc_client = NFS_CLIENT(inode),
bdc7f021 1191 .rpc_message = &msg,
84115e1c
TM
1192 .callback_ops = &nfs_commit_ops,
1193 .callback_data = data,
101070ca 1194 .workqueue = nfsiod_workqueue,
84115e1c 1195 .flags = flags,
3ff7576d 1196 .priority = priority,
84115e1c 1197 };
1da177e4
LT
1198
1199 /* Set up the RPC argument and reply structs
1200 * NB: take care not to mess about with data->commit et al. */
1201
1202 list_splice_init(head, &data->pages);
1da177e4 1203
1da177e4 1204 data->inode = inode;
bdc7f021 1205 data->cred = msg.rpc_cred;
1da177e4
LT
1206
1207 data->args.fh = NFS_FH(data->inode);
3da28eb1
TM
1208 /* Note: we always request a commit of the entire inode */
1209 data->args.offset = 0;
1210 data->args.count = 0;
383ba719 1211 data->args.context = get_nfs_open_context(first->wb_context);
3da28eb1 1212 data->res.count = 0;
1da177e4
LT
1213 data->res.fattr = &data->fattr;
1214 data->res.verf = &data->verf;
0e574af1 1215 nfs_fattr_init(&data->fattr);
788e7a89
TM
1216
1217 /* Set up the initial task struct. */
bdc7f021 1218 NFS_PROTO(inode)->commit_setup(data, &msg);
1da177e4 1219
a3f565b1 1220 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
bdc7f021 1221
07737691
TM
1222 task = rpc_run_task(&task_setup_data);
1223 if (!IS_ERR(task))
1224 rpc_put_task(task);
1da177e4
LT
1225}
1226
1227/*
1228 * Commit dirty pages
1229 */
1230static int
40859d7e 1231nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1da177e4
LT
1232{
1233 struct nfs_write_data *data;
1234 struct nfs_page *req;
1235
e9f7bee1 1236 data = nfs_commit_alloc();
1da177e4
LT
1237
1238 if (!data)
1239 goto out_bad;
1240
1241 /* Set up the argument struct */
1242 nfs_commit_rpcsetup(head, data, how);
1243
1da177e4
LT
1244 return 0;
1245 out_bad:
1246 while (!list_empty(head)) {
1247 req = nfs_list_entry(head->next);
1248 nfs_list_remove_request(req);
1249 nfs_mark_request_commit(req);
83715ad5 1250 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
c9e51e41
PZ
1251 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1252 BDI_RECLAIMABLE);
9fd367f0 1253 nfs_clear_page_tag_locked(req);
1da177e4
LT
1254 }
1255 return -ENOMEM;
1256}
1257
1258/*
1259 * COMMIT call returned
1260 */
788e7a89 1261static void nfs_commit_done(struct rpc_task *task, void *calldata)
1da177e4 1262{
963d8fe5 1263 struct nfs_write_data *data = calldata;
1da177e4 1264 struct nfs_page *req;
1da177e4 1265
a3f565b1 1266 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1da177e4
LT
1267 task->tk_pid, task->tk_status);
1268
788e7a89
TM
1269 /* Call the NFS version-specific code */
1270 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1271 return;
1272
1da177e4
LT
1273 while (!list_empty(&data->pages)) {
1274 req = nfs_list_entry(data->pages.next);
1275 nfs_list_remove_request(req);
612c9384 1276 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
fd39fc85 1277 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
c9e51e41
PZ
1278 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1279 BDI_RECLAIMABLE);
1da177e4
LT
1280
1281 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
88be9f99
TM
1282 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1283 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1da177e4
LT
1284 req->wb_bytes,
1285 (long long)req_offset(req));
1286 if (task->tk_status < 0) {
7b159fc1 1287 nfs_context_set_write_error(req->wb_context, task->tk_status);
1da177e4
LT
1288 nfs_inode_remove_request(req);
1289 dprintk(", error = %d\n", task->tk_status);
1290 goto next;
1291 }
1292
1293 /* Okay, COMMIT succeeded, apparently. Check the verifier
1294 * returned by the server against all stored verfs. */
1295 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1296 /* We have a match */
44dd151d
TM
1297 /* Set the PG_uptodate flag */
1298 nfs_mark_uptodate(req->wb_page, req->wb_pgbase,
1299 req->wb_bytes);
1da177e4
LT
1300 nfs_inode_remove_request(req);
1301 dprintk(" OK\n");
1302 goto next;
1303 }
1304 /* We have a mismatch. Write the page again */
1305 dprintk(" mismatch\n");
6d884e8f 1306 nfs_mark_request_dirty(req);
1da177e4 1307 next:
9fd367f0 1308 nfs_clear_page_tag_locked(req);
1da177e4 1309 }
1da177e4 1310}
788e7a89
TM
1311
1312static const struct rpc_call_ops nfs_commit_ops = {
1313 .rpc_call_done = nfs_commit_done,
1314 .rpc_release = nfs_commit_release,
1315};
1da177e4 1316
3da28eb1 1317int nfs_commit_inode(struct inode *inode, int how)
1da177e4 1318{
1da177e4 1319 LIST_HEAD(head);
7d46a49f 1320 int res;
1da177e4 1321
587142f8 1322 spin_lock(&inode->i_lock);
3da28eb1 1323 res = nfs_scan_commit(inode, &head, 0, 0);
587142f8 1324 spin_unlock(&inode->i_lock);
1da177e4 1325 if (res) {
7d46a49f 1326 int error = nfs_commit_list(inode, &head, how);
3da28eb1
TM
1327 if (error < 0)
1328 return error;
1329 }
1da177e4
LT
1330 return res;
1331}
c63c7b05
TM
1332#else
1333static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1334{
1335 return 0;
1336}
1da177e4
LT
1337#endif
1338
1c75950b 1339long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
1da177e4 1340{
1c75950b 1341 struct inode *inode = mapping->host;
ca52fec1 1342 pgoff_t idx_start, idx_end;
1c75950b 1343 unsigned int npages = 0;
c42de9dd 1344 LIST_HEAD(head);
70b9ecbd 1345 int nocommit = how & FLUSH_NOCOMMIT;
3f442547 1346 long pages, ret;
1da177e4 1347
1c75950b
TM
1348 /* FIXME */
1349 if (wbc->range_cyclic)
1350 idx_start = 0;
1351 else {
1352 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1353 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1354 if (idx_end > idx_start) {
ca52fec1 1355 pgoff_t l_npages = 1 + idx_end - idx_start;
1c75950b
TM
1356 npages = l_npages;
1357 if (sizeof(npages) != sizeof(l_npages) &&
ca52fec1 1358 (pgoff_t)npages != l_npages)
1c75950b
TM
1359 npages = 0;
1360 }
1361 }
c42de9dd 1362 how &= ~FLUSH_NOCOMMIT;
587142f8 1363 spin_lock(&inode->i_lock);
1da177e4 1364 do {
c42de9dd
TM
1365 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1366 if (ret != 0)
70b9ecbd 1367 continue;
c42de9dd
TM
1368 if (nocommit)
1369 break;
d2ccddf0 1370 pages = nfs_scan_commit(inode, &head, idx_start, npages);
724c439c 1371 if (pages == 0)
c42de9dd 1372 break;
d2ccddf0 1373 if (how & FLUSH_INVALIDATE) {
587142f8 1374 spin_unlock(&inode->i_lock);
83715ad5 1375 nfs_cancel_commit_list(&head);
e8e058e8 1376 ret = pages;
587142f8 1377 spin_lock(&inode->i_lock);
d2ccddf0
TM
1378 continue;
1379 }
1380 pages += nfs_scan_commit(inode, &head, 0, 0);
587142f8 1381 spin_unlock(&inode->i_lock);
c42de9dd 1382 ret = nfs_commit_list(inode, &head, how);
587142f8
TM
1383 spin_lock(&inode->i_lock);
1384
c42de9dd 1385 } while (ret >= 0);
587142f8 1386 spin_unlock(&inode->i_lock);
c42de9dd 1387 return ret;
1da177e4
LT
1388}
1389
34901f70 1390static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
1c75950b 1391{
1c75950b
TM
1392 int ret;
1393
34901f70 1394 ret = nfs_writepages(mapping, wbc);
61822ab5
TM
1395 if (ret < 0)
1396 goto out;
34901f70 1397 ret = nfs_sync_mapping_wait(mapping, wbc, how);
ed90ef51
TM
1398 if (ret < 0)
1399 goto out;
1400 return 0;
61822ab5 1401out:
e507d9eb 1402 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1c75950b
TM
1403 return ret;
1404}
1405
34901f70
TM
1406/* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
1407static int nfs_write_mapping(struct address_space *mapping, int how)
1408{
1409 struct writeback_control wbc = {
1410 .bdi = mapping->backing_dev_info,
1411 .sync_mode = WB_SYNC_NONE,
1412 .nr_to_write = LONG_MAX,
1413 .for_writepages = 1,
1414 .range_cyclic = 1,
1415 };
1416 int ret;
1417
1418 ret = __nfs_write_mapping(mapping, &wbc, how);
1419 if (ret < 0)
1420 return ret;
1421 wbc.sync_mode = WB_SYNC_ALL;
1422 return __nfs_write_mapping(mapping, &wbc, how);
1423}
1424
ed90ef51
TM
1425/*
1426 * flush the inode to disk.
1427 */
1428int nfs_wb_all(struct inode *inode)
1c75950b 1429{
ed90ef51
TM
1430 return nfs_write_mapping(inode->i_mapping, 0);
1431}
1c75950b 1432
ed90ef51
TM
1433int nfs_wb_nocommit(struct inode *inode)
1434{
1435 return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
1c75950b
TM
1436}
1437
1b3b4a1a
TM
1438int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1439{
1440 struct nfs_page *req;
1441 loff_t range_start = page_offset(page);
1442 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1443 struct writeback_control wbc = {
1444 .bdi = page->mapping->backing_dev_info,
1445 .sync_mode = WB_SYNC_ALL,
1446 .nr_to_write = LONG_MAX,
1447 .range_start = range_start,
1448 .range_end = range_end,
1449 };
1450 int ret = 0;
1451
1452 BUG_ON(!PageLocked(page));
1453 for (;;) {
1454 req = nfs_page_find_request(page);
1455 if (req == NULL)
1456 goto out;
1457 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1458 nfs_release_request(req);
1459 break;
1460 }
1461 if (nfs_lock_request_dontget(req)) {
1462 nfs_inode_remove_request(req);
1463 /*
1464 * In case nfs_inode_remove_request has marked the
1465 * page as being dirty
1466 */
1467 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1468 nfs_unlock_request(req);
1469 break;
1470 }
1471 ret = nfs_wait_on_request(req);
1472 if (ret < 0)
1473 goto out;
1474 }
1475 if (!PagePrivate(page))
1476 return 0;
1477 ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
1478out:
1479 return ret;
1480}
1481
5334eb13
AB
1482static int nfs_wb_page_priority(struct inode *inode, struct page *page,
1483 int how)
1c75950b
TM
1484{
1485 loff_t range_start = page_offset(page);
1486 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
4d770ccf
TM
1487 struct writeback_control wbc = {
1488 .bdi = page->mapping->backing_dev_info,
1489 .sync_mode = WB_SYNC_ALL,
1490 .nr_to_write = LONG_MAX,
1491 .range_start = range_start,
1492 .range_end = range_end,
1493 };
1494 int ret;
1c75950b 1495
4d770ccf 1496 BUG_ON(!PageLocked(page));
c63c7b05 1497 if (clear_page_dirty_for_io(page)) {
4d770ccf
TM
1498 ret = nfs_writepage_locked(page, &wbc);
1499 if (ret < 0)
1500 goto out;
1501 }
f40313ac
TM
1502 if (!PagePrivate(page))
1503 return 0;
4d770ccf
TM
1504 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1505 if (ret >= 0)
1506 return 0;
1507out:
e507d9eb 1508 __mark_inode_dirty(inode, I_DIRTY_PAGES);
4d770ccf 1509 return ret;
1c75950b
TM
1510}
1511
1512/*
1513 * Write back all requests on one page - we do this before reading it.
1514 */
1515int nfs_wb_page(struct inode *inode, struct page* page)
1516{
4d770ccf 1517 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
1c75950b
TM
1518}
1519
f7b422b1 1520int __init nfs_init_writepagecache(void)
1da177e4
LT
1521{
1522 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1523 sizeof(struct nfs_write_data),
1524 0, SLAB_HWCACHE_ALIGN,
20c2df83 1525 NULL);
1da177e4
LT
1526 if (nfs_wdata_cachep == NULL)
1527 return -ENOMEM;
1528
93d2341c
MD
1529 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1530 nfs_wdata_cachep);
1da177e4
LT
1531 if (nfs_wdata_mempool == NULL)
1532 return -ENOMEM;
1533
93d2341c
MD
1534 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1535 nfs_wdata_cachep);
1da177e4
LT
1536 if (nfs_commit_mempool == NULL)
1537 return -ENOMEM;
1538
89a09141
PZ
1539 /*
1540 * NFS congestion size, scale with available memory.
1541 *
1542 * 64MB: 8192k
1543 * 128MB: 11585k
1544 * 256MB: 16384k
1545 * 512MB: 23170k
1546 * 1GB: 32768k
1547 * 2GB: 46340k
1548 * 4GB: 65536k
1549 * 8GB: 92681k
1550 * 16GB: 131072k
1551 *
1552 * This allows larger machines to have larger/more transfers.
1553 * Limit the default to 256M
1554 */
1555 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1556 if (nfs_congestion_kb > 256*1024)
1557 nfs_congestion_kb = 256*1024;
1558
1da177e4
LT
1559 return 0;
1560}
1561
266bee88 1562void nfs_destroy_writepagecache(void)
1da177e4
LT
1563{
1564 mempool_destroy(nfs_commit_mempool);
1565 mempool_destroy(nfs_wdata_mempool);
1a1d92c1 1566 kmem_cache_destroy(nfs_wdata_cachep);
1da177e4
LT
1567}
1568
This page took 0.42893 seconds and 5 git commands to generate.