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