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