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