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