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