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