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