]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/nfs/write.c
Linux 3.0-rc5
[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;
867 data->args.pgbase = req->wb_pgbase + offset;
868 data->args.pages = data->pagevec;
869 data->args.count = count;
383ba719 870 data->args.context = get_nfs_open_context(req->wb_context);
f11ac8db 871 data->args.lock_context = req->wb_lock_context;
bdc7f021 872 data->args.stable = NFS_UNSTABLE;
b31268ac 873 if (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
bdc7f021 874 data->args.stable = NFS_DATA_SYNC;
fb8a1f11 875 if (!nfs_need_commit(NFS_I(inode)))
bdc7f021
TM
876 data->args.stable = NFS_FILE_SYNC;
877 }
1da177e4
LT
878
879 data->res.fattr = &data->fattr;
880 data->res.count = count;
881 data->res.verf = &data->verf;
0e574af1 882 nfs_fattr_init(&data->fattr);
1da177e4 883
0382b744
AA
884 if (data->lseg &&
885 (pnfs_try_to_write_data(data, call_ops, how) == PNFS_ATTEMPTED))
886 return 0;
887
d138d5d1 888 return nfs_initiate_write(data, NFS_CLIENT(inode), call_ops, how);
1da177e4
LT
889}
890
6d884e8f
F
891/* If a nfs_flush_* function fails, it should remove reqs from @head and
892 * call this on each, which will prepare them to be retried on next
893 * writeback using standard nfs.
894 */
895static void nfs_redirty_request(struct nfs_page *req)
896{
a6305ddb
TM
897 struct page *page = req->wb_page;
898
6d884e8f 899 nfs_mark_request_dirty(req);
6d884e8f 900 nfs_clear_page_tag_locked(req);
a6305ddb 901 nfs_end_page_writeback(page);
6d884e8f
F
902}
903
1da177e4
LT
904/*
905 * Generate multiple small requests to write out a single
906 * contiguous dirty area on one page.
907 */
c76069bd 908static int nfs_flush_multi(struct nfs_pageio_descriptor *desc)
1da177e4 909{
c76069bd 910 struct nfs_page *req = nfs_list_entry(desc->pg_list.next);
1da177e4
LT
911 struct page *page = req->wb_page;
912 struct nfs_write_data *data;
c76069bd 913 size_t wsize = NFS_SERVER(desc->pg_inode)->wsize, nbytes;
e9f7bee1 914 unsigned int offset;
1da177e4 915 int requests = 0;
dbae4c73 916 int ret = 0;
c76069bd 917 struct pnfs_layout_segment *lseg;
1da177e4
LT
918 LIST_HEAD(list);
919
920 nfs_list_remove_request(req);
921
b31268ac
TM
922 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
923 (desc->pg_moreio || NFS_I(desc->pg_inode)->ncommit ||
924 desc->pg_count > wsize))
925 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
926
927
c76069bd 928 nbytes = desc->pg_count;
e9f7bee1
TM
929 do {
930 size_t len = min(nbytes, wsize);
931
8d5658c9 932 data = nfs_writedata_alloc(1);
1da177e4
LT
933 if (!data)
934 goto out_bad;
935 list_add(&data->pages, &list);
936 requests++;
e9f7bee1
TM
937 nbytes -= len;
938 } while (nbytes != 0);
1da177e4
LT
939 atomic_set(&req->wb_complete, requests);
940
c76069bd 941 BUG_ON(desc->pg_lseg);
fb3296eb
BH
942 lseg = pnfs_update_layout(desc->pg_inode, req->wb_context,
943 req_offset(req), desc->pg_count,
944 IOMODE_RW, GFP_NOFS);
1da177e4 945 ClearPageError(page);
1da177e4 946 offset = 0;
c76069bd 947 nbytes = desc->pg_count;
1da177e4 948 do {
dbae4c73
TM
949 int ret2;
950
1da177e4
LT
951 data = list_entry(list.next, struct nfs_write_data, pages);
952 list_del_init(&data->pages);
953
954 data->pagevec[0] = page;
1da177e4 955
bcb71bba
TM
956 if (nbytes < wsize)
957 wsize = nbytes;
dbae4c73 958 ret2 = nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
c76069bd 959 wsize, offset, lseg, desc->pg_ioflags);
dbae4c73
TM
960 if (ret == 0)
961 ret = ret2;
bcb71bba
TM
962 offset += wsize;
963 nbytes -= wsize;
1da177e4
LT
964 } while (nbytes != 0);
965
44b83799 966 put_lseg(lseg);
36fe432d 967 desc->pg_lseg = NULL;
dbae4c73 968 return ret;
1da177e4
LT
969
970out_bad:
971 while (!list_empty(&list)) {
972 data = list_entry(list.next, struct nfs_write_data, pages);
973 list_del(&data->pages);
0da2a4ac 974 nfs_writedata_free(data);
1da177e4 975 }
61822ab5 976 nfs_redirty_request(req);
1da177e4
LT
977 return -ENOMEM;
978}
979
980/*
981 * Create an RPC task for the given write request and kick it.
982 * The page must have been locked by the caller.
983 *
984 * It may happen that the page we're passed is not marked dirty.
985 * This is the case if nfs_updatepage detects a conflicting request
986 * that has been written but not committed.
987 */
c76069bd 988static int nfs_flush_one(struct nfs_pageio_descriptor *desc)
1da177e4
LT
989{
990 struct nfs_page *req;
991 struct page **pages;
992 struct nfs_write_data *data;
c76069bd
FI
993 struct list_head *head = &desc->pg_list;
994 struct pnfs_layout_segment *lseg = desc->pg_lseg;
44b83799 995 int ret;
1da177e4 996
c76069bd
FI
997 data = nfs_writedata_alloc(nfs_page_array_len(desc->pg_base,
998 desc->pg_count));
44b83799
FI
999 if (!data) {
1000 while (!list_empty(head)) {
1001 req = nfs_list_entry(head->next);
1002 nfs_list_remove_request(req);
1003 nfs_redirty_request(req);
1004 }
1005 ret = -ENOMEM;
1006 goto out;
1007 }
1da177e4 1008 pages = data->pagevec;
1da177e4
LT
1009 while (!list_empty(head)) {
1010 req = nfs_list_entry(head->next);
1011 nfs_list_remove_request(req);
1012 nfs_list_add_request(req, &data->pages);
1013 ClearPageError(req->wb_page);
1da177e4 1014 *pages++ = req->wb_page;
1da177e4
LT
1015 }
1016 req = nfs_list_entry(data->pages.next);
44b83799 1017 if ((!lseg) && list_is_singular(&data->pages))
fb3296eb
BH
1018 lseg = pnfs_update_layout(desc->pg_inode, req->wb_context,
1019 req_offset(req), desc->pg_count,
1020 IOMODE_RW, GFP_NOFS);
1da177e4 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
c63c7b05
TM
1034static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1035 struct inode *inode, int ioflags)
1da177e4 1036{
bf4285e7 1037 size_t wsize = NFS_SERVER(inode)->wsize;
1da177e4 1038
bcb71bba 1039 if (wsize < PAGE_CACHE_SIZE)
c63c7b05 1040 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
bcb71bba 1041 else
c63c7b05 1042 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
1da177e4
LT
1043}
1044
1045/*
1046 * Handle a write reply that flushed part of a page.
1047 */
788e7a89 1048static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
1da177e4 1049{
788e7a89 1050 struct nfs_write_data *data = calldata;
1da177e4 1051
48186c7d
CL
1052 dprintk("NFS: %5u write(%s/%lld %d@%lld)",
1053 task->tk_pid,
1054 data->req->wb_context->path.dentry->d_inode->i_sb->s_id,
1055 (long long)
1056 NFS_FILEID(data->req->wb_context->path.dentry->d_inode),
1057 data->req->wb_bytes, (long long)req_offset(data->req));
1da177e4 1058
c9d8f89d
TM
1059 nfs_writeback_done(task, data);
1060}
788e7a89 1061
c9d8f89d
TM
1062static void nfs_writeback_release_partial(void *calldata)
1063{
1064 struct nfs_write_data *data = calldata;
1065 struct nfs_page *req = data->req;
1066 struct page *page = req->wb_page;
1067 int status = data->task.tk_status;
1068
1069 if (status < 0) {
a301b777 1070 nfs_set_pageerror(page);
c9d8f89d
TM
1071 nfs_context_set_write_error(req->wb_context, status);
1072 dprintk(", error = %d\n", status);
8e821cad 1073 goto out;
1da177e4
LT
1074 }
1075
8e821cad 1076 if (nfs_write_need_commit(data)) {
587142f8 1077 struct inode *inode = page->mapping->host;
8e821cad 1078
587142f8 1079 spin_lock(&inode->i_lock);
8e821cad
TM
1080 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
1081 /* Do nothing we need to resend the writes */
1082 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1083 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1084 dprintk(" defer commit\n");
1085 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1086 set_bit(PG_NEED_RESCHED, &req->wb_flags);
1087 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
1088 dprintk(" server reboot detected\n");
1089 }
587142f8 1090 spin_unlock(&inode->i_lock);
8e821cad
TM
1091 } else
1092 dprintk(" OK\n");
1093
1094out:
1da177e4 1095 if (atomic_dec_and_test(&req->wb_complete))
a861a1e1 1096 nfs_writepage_release(req, data);
c9d8f89d 1097 nfs_writedata_release(calldata);
1da177e4
LT
1098}
1099
def6ed7e
AA
1100#if defined(CONFIG_NFS_V4_1)
1101void nfs_write_prepare(struct rpc_task *task, void *calldata)
1102{
1103 struct nfs_write_data *data = calldata;
def6ed7e 1104
035168ab
TM
1105 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
1106 &data->args.seq_args,
def6ed7e
AA
1107 &data->res.seq_res, 1, task))
1108 return;
1109 rpc_call_start(task);
1110}
1111#endif /* CONFIG_NFS_V4_1 */
1112
788e7a89 1113static const struct rpc_call_ops nfs_write_partial_ops = {
def6ed7e
AA
1114#if defined(CONFIG_NFS_V4_1)
1115 .rpc_call_prepare = nfs_write_prepare,
1116#endif /* CONFIG_NFS_V4_1 */
788e7a89 1117 .rpc_call_done = nfs_writeback_done_partial,
c9d8f89d 1118 .rpc_release = nfs_writeback_release_partial,
788e7a89
TM
1119};
1120
1da177e4
LT
1121/*
1122 * Handle a write reply that flushes a whole page.
1123 *
1124 * FIXME: There is an inherent race with invalidate_inode_pages and
1125 * writebacks since the page->count is kept > 1 for as long
1126 * as the page has a write request pending.
1127 */
788e7a89 1128static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1da177e4 1129{
788e7a89 1130 struct nfs_write_data *data = calldata;
1da177e4 1131
c9d8f89d
TM
1132 nfs_writeback_done(task, data);
1133}
1134
1135static void nfs_writeback_release_full(void *calldata)
1136{
1137 struct nfs_write_data *data = calldata;
1138 int status = data->task.tk_status;
788e7a89 1139
1da177e4
LT
1140 /* Update attributes as result of writeback. */
1141 while (!list_empty(&data->pages)) {
c9d8f89d
TM
1142 struct nfs_page *req = nfs_list_entry(data->pages.next);
1143 struct page *page = req->wb_page;
1144
1da177e4 1145 nfs_list_remove_request(req);
1da177e4 1146
48186c7d
CL
1147 dprintk("NFS: %5u write (%s/%lld %d@%lld)",
1148 data->task.tk_pid,
88be9f99
TM
1149 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1150 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1da177e4
LT
1151 req->wb_bytes,
1152 (long long)req_offset(req));
1153
c9d8f89d 1154 if (status < 0) {
a301b777 1155 nfs_set_pageerror(page);
c9d8f89d
TM
1156 nfs_context_set_write_error(req->wb_context, status);
1157 dprintk(", error = %d\n", status);
8e821cad 1158 goto remove_request;
1da177e4 1159 }
1da177e4 1160
8e821cad
TM
1161 if (nfs_write_need_commit(data)) {
1162 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
a861a1e1 1163 nfs_mark_request_commit(req, data->lseg);
8e821cad 1164 dprintk(" marked for commit\n");
1da177e4
LT
1165 goto next;
1166 }
8e821cad
TM
1167 dprintk(" OK\n");
1168remove_request:
1da177e4 1169 nfs_inode_remove_request(req);
1da177e4 1170 next:
9fd367f0 1171 nfs_clear_page_tag_locked(req);
a6305ddb 1172 nfs_end_page_writeback(page);
1da177e4 1173 }
c9d8f89d 1174 nfs_writedata_release(calldata);
1da177e4
LT
1175}
1176
788e7a89 1177static const struct rpc_call_ops nfs_write_full_ops = {
def6ed7e
AA
1178#if defined(CONFIG_NFS_V4_1)
1179 .rpc_call_prepare = nfs_write_prepare,
1180#endif /* CONFIG_NFS_V4_1 */
788e7a89 1181 .rpc_call_done = nfs_writeback_done_full,
c9d8f89d 1182 .rpc_release = nfs_writeback_release_full,
788e7a89
TM
1183};
1184
1185
1da177e4
LT
1186/*
1187 * This function is called when the WRITE call is complete.
1188 */
13602896 1189void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1da177e4 1190{
1da177e4
LT
1191 struct nfs_writeargs *argp = &data->args;
1192 struct nfs_writeres *resp = &data->res;
eedc020e 1193 struct nfs_server *server = NFS_SERVER(data->inode);
788e7a89 1194 int status;
1da177e4 1195
a3f565b1 1196 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1da177e4
LT
1197 task->tk_pid, task->tk_status);
1198
f551e44f
CL
1199 /*
1200 * ->write_done will attempt to use post-op attributes to detect
1201 * conflicting writes by other clients. A strict interpretation
1202 * of close-to-open would allow us to continue caching even if
1203 * another writer had changed the file, but some applications
1204 * depend on tighter cache coherency when writing.
1205 */
788e7a89
TM
1206 status = NFS_PROTO(data->inode)->write_done(task, data);
1207 if (status != 0)
13602896 1208 return;
91d5b470
CL
1209 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1210
1da177e4
LT
1211#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1212 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1213 /* We tried a write call, but the server did not
1214 * commit data to stable storage even though we
1215 * requested it.
1216 * Note: There is a known bug in Tru64 < 5.0 in which
1217 * the server reports NFS_DATA_SYNC, but performs
1218 * NFS_FILE_SYNC. We therefore implement this checking
1219 * as a dprintk() in order to avoid filling syslog.
1220 */
1221 static unsigned long complain;
1222
a69aef14 1223 /* Note this will print the MDS for a DS write */
1da177e4 1224 if (time_before(complain, jiffies)) {
48186c7d 1225 dprintk("NFS: faulty NFS server %s:"
1da177e4 1226 " (committed = %d) != (stable = %d)\n",
eedc020e 1227 server->nfs_client->cl_hostname,
1da177e4
LT
1228 resp->verf->committed, argp->stable);
1229 complain = jiffies + 300 * HZ;
1230 }
1231 }
1232#endif
1233 /* Is this a short write? */
1234 if (task->tk_status >= 0 && resp->count < argp->count) {
1235 static unsigned long complain;
1236
91d5b470
CL
1237 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1238
1da177e4
LT
1239 /* Has the server at least made some progress? */
1240 if (resp->count != 0) {
1241 /* Was this an NFSv2 write or an NFSv3 stable write? */
1242 if (resp->verf->committed != NFS_UNSTABLE) {
1243 /* Resend from where the server left off */
a69aef14 1244 data->mds_offset += resp->count;
1da177e4
LT
1245 argp->offset += resp->count;
1246 argp->pgbase += resp->count;
1247 argp->count -= resp->count;
1248 } else {
1249 /* Resend as a stable write in order to avoid
1250 * headaches in the case of a server crash.
1251 */
1252 argp->stable = NFS_FILE_SYNC;
1253 }
0110ee15 1254 nfs_restart_rpc(task, server->nfs_client);
13602896 1255 return;
1da177e4
LT
1256 }
1257 if (time_before(complain, jiffies)) {
1258 printk(KERN_WARNING
1259 "NFS: Server wrote zero bytes, expected %u.\n",
1260 argp->count);
1261 complain = jiffies + 300 * HZ;
1262 }
1263 /* Can't do anything about it except throw an error. */
1264 task->tk_status = -EIO;
1265 }
13602896 1266 return;
1da177e4
LT
1267}
1268
1269
1270#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
71d0a611
TM
1271static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
1272{
b8413f98
TM
1273 int ret;
1274
71d0a611
TM
1275 if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
1276 return 1;
b8413f98
TM
1277 if (!may_wait)
1278 return 0;
1279 ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1280 NFS_INO_COMMIT,
1281 nfs_wait_bit_killable,
1282 TASK_KILLABLE);
1283 return (ret < 0) ? ret : 1;
71d0a611
TM
1284}
1285
e0c2b380 1286void nfs_commit_clear_lock(struct nfs_inode *nfsi)
71d0a611
TM
1287{
1288 clear_bit(NFS_INO_COMMIT, &nfsi->flags);
1289 smp_mb__after_clear_bit();
1290 wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
1291}
e0c2b380 1292EXPORT_SYMBOL_GPL(nfs_commit_clear_lock);
71d0a611 1293
e0c2b380 1294void nfs_commitdata_release(void *data)
1da177e4 1295{
383ba719
TM
1296 struct nfs_write_data *wdata = data;
1297
988b6dce 1298 put_lseg(wdata->lseg);
383ba719 1299 put_nfs_open_context(wdata->args.context);
1da177e4
LT
1300 nfs_commit_free(wdata);
1301}
e0c2b380 1302EXPORT_SYMBOL_GPL(nfs_commitdata_release);
1da177e4 1303
e0c2b380 1304int nfs_initiate_commit(struct nfs_write_data *data, struct rpc_clnt *clnt,
9ace33cd
FI
1305 const struct rpc_call_ops *call_ops,
1306 int how)
1da177e4 1307{
07737691 1308 struct rpc_task *task;
9ace33cd 1309 int priority = flush_task_priority(how);
bdc7f021
TM
1310 struct rpc_message msg = {
1311 .rpc_argp = &data->args,
1312 .rpc_resp = &data->res,
9ace33cd 1313 .rpc_cred = data->cred,
bdc7f021 1314 };
84115e1c 1315 struct rpc_task_setup task_setup_data = {
07737691 1316 .task = &data->task,
9ace33cd 1317 .rpc_client = clnt,
bdc7f021 1318 .rpc_message = &msg,
9ace33cd 1319 .callback_ops = call_ops,
84115e1c 1320 .callback_data = data,
101070ca 1321 .workqueue = nfsiod_workqueue,
2c61be0a 1322 .flags = RPC_TASK_ASYNC,
3ff7576d 1323 .priority = priority,
84115e1c 1324 };
9ace33cd
FI
1325 /* Set up the initial task struct. */
1326 NFS_PROTO(data->inode)->commit_setup(data, &msg);
1327
1328 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1329
1330 task = rpc_run_task(&task_setup_data);
1331 if (IS_ERR(task))
1332 return PTR_ERR(task);
1333 if (how & FLUSH_SYNC)
1334 rpc_wait_for_completion_task(task);
1335 rpc_put_task(task);
1336 return 0;
1337}
e0c2b380 1338EXPORT_SYMBOL_GPL(nfs_initiate_commit);
9ace33cd
FI
1339
1340/*
1341 * Set up the argument/result storage required for the RPC call.
1342 */
e0c2b380 1343void nfs_init_commit(struct nfs_write_data *data,
988b6dce
FI
1344 struct list_head *head,
1345 struct pnfs_layout_segment *lseg)
9ace33cd
FI
1346{
1347 struct nfs_page *first = nfs_list_entry(head->next);
1348 struct inode *inode = first->wb_context->path.dentry->d_inode;
1da177e4
LT
1349
1350 /* Set up the RPC argument and reply structs
1351 * NB: take care not to mess about with data->commit et al. */
1352
1353 list_splice_init(head, &data->pages);
1da177e4 1354
1da177e4 1355 data->inode = inode;
9ace33cd 1356 data->cred = first->wb_context->cred;
988b6dce 1357 data->lseg = lseg; /* reference transferred */
9ace33cd 1358 data->mds_ops = &nfs_commit_ops;
1da177e4
LT
1359
1360 data->args.fh = NFS_FH(data->inode);
3da28eb1
TM
1361 /* Note: we always request a commit of the entire inode */
1362 data->args.offset = 0;
1363 data->args.count = 0;
383ba719 1364 data->args.context = get_nfs_open_context(first->wb_context);
3da28eb1 1365 data->res.count = 0;
1da177e4
LT
1366 data->res.fattr = &data->fattr;
1367 data->res.verf = &data->verf;
0e574af1 1368 nfs_fattr_init(&data->fattr);
1da177e4 1369}
e0c2b380 1370EXPORT_SYMBOL_GPL(nfs_init_commit);
1da177e4 1371
e0c2b380 1372void nfs_retry_commit(struct list_head *page_list,
a861a1e1 1373 struct pnfs_layout_segment *lseg)
64bfeb49
FI
1374{
1375 struct nfs_page *req;
1376
1377 while (!list_empty(page_list)) {
1378 req = nfs_list_entry(page_list->next);
1379 nfs_list_remove_request(req);
a861a1e1 1380 nfs_mark_request_commit(req, lseg);
64bfeb49
FI
1381 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1382 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1383 BDI_RECLAIMABLE);
1384 nfs_clear_page_tag_locked(req);
1385 }
1386}
e0c2b380 1387EXPORT_SYMBOL_GPL(nfs_retry_commit);
64bfeb49 1388
1da177e4
LT
1389/*
1390 * Commit dirty pages
1391 */
1392static int
40859d7e 1393nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1da177e4
LT
1394{
1395 struct nfs_write_data *data;
1da177e4 1396
c9d8f89d 1397 data = nfs_commitdata_alloc();
1da177e4
LT
1398
1399 if (!data)
1400 goto out_bad;
1401
1402 /* Set up the argument struct */
988b6dce 1403 nfs_init_commit(data, head, NULL);
9ace33cd 1404 return nfs_initiate_commit(data, NFS_CLIENT(inode), data->mds_ops, how);
1da177e4 1405 out_bad:
a861a1e1 1406 nfs_retry_commit(head, NULL);
71d0a611 1407 nfs_commit_clear_lock(NFS_I(inode));
1da177e4
LT
1408 return -ENOMEM;
1409}
1410
1411/*
1412 * COMMIT call returned
1413 */
788e7a89 1414static void nfs_commit_done(struct rpc_task *task, void *calldata)
1da177e4 1415{
963d8fe5 1416 struct nfs_write_data *data = calldata;
1da177e4 1417
a3f565b1 1418 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1da177e4
LT
1419 task->tk_pid, task->tk_status);
1420
788e7a89 1421 /* Call the NFS version-specific code */
c0d0e96b 1422 NFS_PROTO(data->inode)->commit_done(task, data);
c9d8f89d
TM
1423}
1424
e0c2b380 1425void nfs_commit_release_pages(struct nfs_write_data *data)
c9d8f89d 1426{
5917ce84 1427 struct nfs_page *req;
c9d8f89d 1428 int status = data->task.tk_status;
788e7a89 1429
1da177e4
LT
1430 while (!list_empty(&data->pages)) {
1431 req = nfs_list_entry(data->pages.next);
1432 nfs_list_remove_request(req);
e468bae9 1433 nfs_clear_request_commit(req);
1da177e4 1434
48186c7d 1435 dprintk("NFS: commit (%s/%lld %d@%lld)",
88be9f99
TM
1436 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1437 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1da177e4
LT
1438 req->wb_bytes,
1439 (long long)req_offset(req));
c9d8f89d
TM
1440 if (status < 0) {
1441 nfs_context_set_write_error(req->wb_context, status);
1da177e4 1442 nfs_inode_remove_request(req);
c9d8f89d 1443 dprintk(", error = %d\n", status);
1da177e4
LT
1444 goto next;
1445 }
1446
1447 /* Okay, COMMIT succeeded, apparently. Check the verifier
1448 * returned by the server against all stored verfs. */
1449 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1450 /* We have a match */
1451 nfs_inode_remove_request(req);
1452 dprintk(" OK\n");
1453 goto next;
1454 }
1455 /* We have a mismatch. Write the page again */
1456 dprintk(" mismatch\n");
6d884e8f 1457 nfs_mark_request_dirty(req);
1da177e4 1458 next:
9fd367f0 1459 nfs_clear_page_tag_locked(req);
1da177e4 1460 }
5917ce84 1461}
e0c2b380 1462EXPORT_SYMBOL_GPL(nfs_commit_release_pages);
5917ce84
FI
1463
1464static void nfs_commit_release(void *calldata)
1465{
1466 struct nfs_write_data *data = calldata;
1467
1468 nfs_commit_release_pages(data);
71d0a611 1469 nfs_commit_clear_lock(NFS_I(data->inode));
c9d8f89d 1470 nfs_commitdata_release(calldata);
1da177e4 1471}
788e7a89
TM
1472
1473static const struct rpc_call_ops nfs_commit_ops = {
21d9a851
AA
1474#if defined(CONFIG_NFS_V4_1)
1475 .rpc_call_prepare = nfs_write_prepare,
1476#endif /* CONFIG_NFS_V4_1 */
788e7a89
TM
1477 .rpc_call_done = nfs_commit_done,
1478 .rpc_release = nfs_commit_release,
1479};
1da177e4 1480
b608b283 1481int nfs_commit_inode(struct inode *inode, int how)
1da177e4 1482{
1da177e4 1483 LIST_HEAD(head);
71d0a611 1484 int may_wait = how & FLUSH_SYNC;
b8413f98 1485 int res;
1da177e4 1486
b8413f98
TM
1487 res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1488 if (res <= 0)
c5efa5fc 1489 goto out_mark_dirty;
3da28eb1 1490 res = nfs_scan_commit(inode, &head, 0, 0);
1da177e4 1491 if (res) {
a861a1e1
FI
1492 int error;
1493
1494 error = pnfs_commit_list(inode, &head, how);
1495 if (error == PNFS_NOT_ATTEMPTED)
1496 error = nfs_commit_list(inode, &head, how);
3da28eb1
TM
1497 if (error < 0)
1498 return error;
b8413f98 1499 if (!may_wait)
c5efa5fc 1500 goto out_mark_dirty;
b8413f98
TM
1501 error = wait_on_bit(&NFS_I(inode)->flags,
1502 NFS_INO_COMMIT,
1503 nfs_wait_bit_killable,
1504 TASK_KILLABLE);
1505 if (error < 0)
1506 return error;
71d0a611
TM
1507 } else
1508 nfs_commit_clear_lock(NFS_I(inode));
c5efa5fc
TM
1509 return res;
1510 /* Note: If we exit without ensuring that the commit is complete,
1511 * we must mark the inode as dirty. Otherwise, future calls to
1512 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1513 * that the data is on the disk.
1514 */
1515out_mark_dirty:
1516 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
1da177e4
LT
1517 return res;
1518}
8fc795f7
TM
1519
1520static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
1521{
420e3646
TM
1522 struct nfs_inode *nfsi = NFS_I(inode);
1523 int flags = FLUSH_SYNC;
1524 int ret = 0;
8fc795f7 1525
a00dd6c0
JL
1526 if (wbc->sync_mode == WB_SYNC_NONE) {
1527 /* Don't commit yet if this is a non-blocking flush and there
1528 * are a lot of outstanding writes for this mapping.
1529 */
1530 if (nfsi->ncommit <= (nfsi->npages >> 1))
1531 goto out_mark_dirty;
420e3646 1532
a00dd6c0 1533 /* don't wait for the COMMIT response */
420e3646 1534 flags = 0;
a00dd6c0
JL
1535 }
1536
420e3646
TM
1537 ret = nfs_commit_inode(inode, flags);
1538 if (ret >= 0) {
1539 if (wbc->sync_mode == WB_SYNC_NONE) {
1540 if (ret < wbc->nr_to_write)
1541 wbc->nr_to_write -= ret;
1542 else
1543 wbc->nr_to_write = 0;
1544 }
8fc795f7 1545 return 0;
420e3646
TM
1546 }
1547out_mark_dirty:
8fc795f7
TM
1548 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
1549 return ret;
1550}
c63c7b05 1551#else
8fc795f7
TM
1552static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
1553{
1554 return 0;
1555}
1da177e4
LT
1556#endif
1557
8fc795f7
TM
1558int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
1559{
863a3c6c
AA
1560 int ret;
1561
1562 ret = nfs_commit_unstable_pages(inode, wbc);
1563 if (ret >= 0 && test_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(inode)->flags)) {
ef311537
AA
1564 int status;
1565 bool sync = true;
863a3c6c 1566
ef311537
AA
1567 if (wbc->sync_mode == WB_SYNC_NONE || wbc->nonblocking ||
1568 wbc->for_background)
1569 sync = false;
863a3c6c
AA
1570
1571 status = pnfs_layoutcommit_inode(inode, sync);
1572 if (status < 0)
1573 return status;
1574 }
1575 return ret;
8fc795f7
TM
1576}
1577
acdc53b2
TM
1578/*
1579 * flush the inode to disk.
1580 */
1581int nfs_wb_all(struct inode *inode)
34901f70
TM
1582{
1583 struct writeback_control wbc = {
72cb77f4 1584 .sync_mode = WB_SYNC_ALL,
34901f70 1585 .nr_to_write = LONG_MAX,
d7fb1207
TM
1586 .range_start = 0,
1587 .range_end = LLONG_MAX,
34901f70 1588 };
34901f70 1589
acdc53b2 1590 return sync_inode(inode, &wbc);
1c75950b
TM
1591}
1592
1b3b4a1a
TM
1593int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1594{
1595 struct nfs_page *req;
1b3b4a1a
TM
1596 int ret = 0;
1597
1598 BUG_ON(!PageLocked(page));
1599 for (;;) {
ba8b06e6 1600 wait_on_page_writeback(page);
1b3b4a1a
TM
1601 req = nfs_page_find_request(page);
1602 if (req == NULL)
1b3b4a1a 1603 break;
1b3b4a1a
TM
1604 if (nfs_lock_request_dontget(req)) {
1605 nfs_inode_remove_request(req);
1606 /*
1607 * In case nfs_inode_remove_request has marked the
1608 * page as being dirty
1609 */
1610 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1611 nfs_unlock_request(req);
1612 break;
1613 }
1614 ret = nfs_wait_on_request(req);
c9edda71 1615 nfs_release_request(req);
1b3b4a1a 1616 if (ret < 0)
c988950e 1617 break;
1b3b4a1a 1618 }
1b3b4a1a
TM
1619 return ret;
1620}
1621
7f2f12d9
TM
1622/*
1623 * Write back all requests on one page - we do this before reading it.
1624 */
1625int nfs_wb_page(struct inode *inode, struct page *page)
1c75950b
TM
1626{
1627 loff_t range_start = page_offset(page);
1628 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
4d770ccf 1629 struct writeback_control wbc = {
4d770ccf 1630 .sync_mode = WB_SYNC_ALL,
7f2f12d9 1631 .nr_to_write = 0,
4d770ccf
TM
1632 .range_start = range_start,
1633 .range_end = range_end,
1634 };
1635 int ret;
1c75950b 1636
0522f6ad 1637 for (;;) {
ba8b06e6 1638 wait_on_page_writeback(page);
73e3302f
TM
1639 if (clear_page_dirty_for_io(page)) {
1640 ret = nfs_writepage_locked(page, &wbc);
1641 if (ret < 0)
1642 goto out_error;
0522f6ad 1643 continue;
7f2f12d9 1644 }
0522f6ad
TM
1645 if (!PagePrivate(page))
1646 break;
1647 ret = nfs_commit_inode(inode, FLUSH_SYNC);
ba8b06e6 1648 if (ret < 0)
73e3302f 1649 goto out_error;
7f2f12d9 1650 }
73e3302f
TM
1651 return 0;
1652out_error:
4d770ccf 1653 return ret;
1c75950b
TM
1654}
1655
074cc1de
TM
1656#ifdef CONFIG_MIGRATION
1657int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1658 struct page *page)
1659{
1660 struct nfs_page *req;
1661 int ret;
1662
7549ad5f 1663 nfs_fscache_release_page(page, GFP_KERNEL);
074cc1de 1664
cfb506e1 1665 req = nfs_find_and_lock_request(page, false);
074cc1de
TM
1666 ret = PTR_ERR(req);
1667 if (IS_ERR(req))
1668 goto out;
1669
1670 ret = migrate_page(mapping, newpage, page);
1671 if (!req)
1672 goto out;
1673 if (ret)
1674 goto out_unlock;
1675 page_cache_get(newpage);
190f38e5 1676 spin_lock(&mapping->host->i_lock);
074cc1de
TM
1677 req->wb_page = newpage;
1678 SetPagePrivate(newpage);
190f38e5 1679 set_page_private(newpage, (unsigned long)req);
074cc1de
TM
1680 ClearPagePrivate(page);
1681 set_page_private(page, 0);
190f38e5 1682 spin_unlock(&mapping->host->i_lock);
074cc1de
TM
1683 page_cache_release(page);
1684out_unlock:
1685 nfs_clear_page_tag_locked(req);
074cc1de
TM
1686out:
1687 return ret;
1688}
1689#endif
1690
f7b422b1 1691int __init nfs_init_writepagecache(void)
1da177e4
LT
1692{
1693 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1694 sizeof(struct nfs_write_data),
1695 0, SLAB_HWCACHE_ALIGN,
20c2df83 1696 NULL);
1da177e4
LT
1697 if (nfs_wdata_cachep == NULL)
1698 return -ENOMEM;
1699
93d2341c
MD
1700 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1701 nfs_wdata_cachep);
1da177e4
LT
1702 if (nfs_wdata_mempool == NULL)
1703 return -ENOMEM;
1704
93d2341c
MD
1705 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1706 nfs_wdata_cachep);
1da177e4
LT
1707 if (nfs_commit_mempool == NULL)
1708 return -ENOMEM;
1709
89a09141
PZ
1710 /*
1711 * NFS congestion size, scale with available memory.
1712 *
1713 * 64MB: 8192k
1714 * 128MB: 11585k
1715 * 256MB: 16384k
1716 * 512MB: 23170k
1717 * 1GB: 32768k
1718 * 2GB: 46340k
1719 * 4GB: 65536k
1720 * 8GB: 92681k
1721 * 16GB: 131072k
1722 *
1723 * This allows larger machines to have larger/more transfers.
1724 * Limit the default to 256M
1725 */
1726 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1727 if (nfs_congestion_kb > 256*1024)
1728 nfs_congestion_kb = 256*1024;
1729
1da177e4
LT
1730 return 0;
1731}
1732
266bee88 1733void nfs_destroy_writepagecache(void)
1da177e4
LT
1734{
1735 mempool_destroy(nfs_commit_mempool);
1736 mempool_destroy(nfs_wdata_mempool);
1a1d92c1 1737 kmem_cache_destroy(nfs_wdata_cachep);
1da177e4
LT
1738}
1739