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