[IPIP]: Use proper net in (mostly) routing calls.
[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>
1da177e4
LT
16
17#include <linux/sunrpc/clnt.h>
18#include <linux/nfs_fs.h>
19#include <linux/nfs_mount.h>
20#include <linux/nfs_page.h>
3fcfab16
AM
21#include <linux/backing-dev.h>
22
1da177e4 23#include <asm/uaccess.h>
1da177e4
LT
24
25#include "delegation.h"
49a70f27 26#include "internal.h"
91d5b470 27#include "iostat.h"
1da177e4
LT
28
29#define NFSDBG_FACILITY NFSDBG_PAGECACHE
30
31#define MIN_POOL_WRITE (32)
32#define MIN_POOL_COMMIT (4)
33
34/*
35 * Local function declarations
36 */
37static struct nfs_page * nfs_update_request(struct nfs_open_context*,
1da177e4
LT
38 struct page *,
39 unsigned int, unsigned int);
c63c7b05
TM
40static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc,
41 struct inode *inode, int ioflags);
f8512ad0 42static void nfs_redirty_request(struct nfs_page *req);
788e7a89
TM
43static const struct rpc_call_ops nfs_write_partial_ops;
44static const struct rpc_call_ops nfs_write_full_ops;
45static const struct rpc_call_ops nfs_commit_ops;
1da177e4 46
e18b890b 47static struct kmem_cache *nfs_wdata_cachep;
3feb2d49 48static mempool_t *nfs_wdata_mempool;
1da177e4
LT
49static mempool_t *nfs_commit_mempool;
50
e9f7bee1 51struct nfs_write_data *nfs_commit_alloc(void)
1da177e4 52{
e6b4f8da 53 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
40859d7e 54
1da177e4
LT
55 if (p) {
56 memset(p, 0, sizeof(*p));
57 INIT_LIST_HEAD(&p->pages);
58 }
59 return p;
60}
61
10afec90 62static void nfs_commit_rcu_free(struct rcu_head *head)
1da177e4 63{
8aca67f0 64 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
40859d7e
CL
65 if (p && (p->pagevec != &p->page_array[0]))
66 kfree(p->pagevec);
1da177e4
LT
67 mempool_free(p, nfs_commit_mempool);
68}
69
8aca67f0
TM
70void nfs_commit_free(struct nfs_write_data *wdata)
71{
72 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
73}
74
8d5658c9 75struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
3feb2d49 76{
e6b4f8da 77 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
3feb2d49
TM
78
79 if (p) {
80 memset(p, 0, sizeof(*p));
81 INIT_LIST_HEAD(&p->pages);
e9f7bee1 82 p->npages = pagecount;
0d0b5cb3
CL
83 if (pagecount <= ARRAY_SIZE(p->page_array))
84 p->pagevec = p->page_array;
3feb2d49 85 else {
0d0b5cb3
CL
86 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
87 if (!p->pagevec) {
3feb2d49
TM
88 mempool_free(p, nfs_wdata_mempool);
89 p = NULL;
90 }
91 }
92 }
93 return p;
94}
95
8aca67f0 96static void nfs_writedata_rcu_free(struct rcu_head *head)
3feb2d49 97{
8aca67f0 98 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
3feb2d49
TM
99 if (p && (p->pagevec != &p->page_array[0]))
100 kfree(p->pagevec);
101 mempool_free(p, nfs_wdata_mempool);
102}
103
8aca67f0
TM
104static void nfs_writedata_free(struct nfs_write_data *wdata)
105{
106 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
107}
108
963d8fe5 109void nfs_writedata_release(void *wdata)
1da177e4 110{
1da177e4
LT
111 nfs_writedata_free(wdata);
112}
113
7b159fc1
TM
114static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
115{
116 ctx->error = error;
117 smp_wmb();
118 set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
119}
120
277459d2
TM
121static struct nfs_page *nfs_page_find_request_locked(struct page *page)
122{
123 struct nfs_page *req = NULL;
124
125 if (PagePrivate(page)) {
126 req = (struct nfs_page *)page_private(page);
127 if (req != NULL)
c03b4024 128 kref_get(&req->wb_kref);
277459d2
TM
129 }
130 return req;
131}
132
133static struct nfs_page *nfs_page_find_request(struct page *page)
134{
587142f8 135 struct inode *inode = page->mapping->host;
277459d2 136 struct nfs_page *req = NULL;
277459d2 137
587142f8 138 spin_lock(&inode->i_lock);
277459d2 139 req = nfs_page_find_request_locked(page);
587142f8 140 spin_unlock(&inode->i_lock);
277459d2
TM
141 return req;
142}
143
1da177e4
LT
144/* Adjust the file length if we're writing beyond the end */
145static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
146{
147 struct inode *inode = page->mapping->host;
148 loff_t end, i_size = i_size_read(inode);
ca52fec1 149 pgoff_t end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
1da177e4
LT
150
151 if (i_size > 0 && page->index < end_index)
152 return;
153 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
154 if (i_size >= end)
155 return;
91d5b470 156 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
1da177e4
LT
157 i_size_write(inode, end);
158}
159
a301b777
TM
160/* A writeback failed: mark the page as bad, and invalidate the page cache */
161static void nfs_set_pageerror(struct page *page)
162{
163 SetPageError(page);
164 nfs_zap_mapping(page->mapping->host, page->mapping);
165}
166
1da177e4
LT
167/* We can set the PG_uptodate flag if we see that a write request
168 * covers the full page.
169 */
170static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
171{
1da177e4
LT
172 if (PageUptodate(page))
173 return;
174 if (base != 0)
175 return;
49a70f27 176 if (count != nfs_page_length(page))
1da177e4 177 return;
49a70f27 178 SetPageUptodate(page);
1da177e4
LT
179}
180
e21195a7 181static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
1da177e4
LT
182 unsigned int offset, unsigned int count)
183{
184 struct nfs_page *req;
e21195a7 185 int ret;
1da177e4 186
e21195a7
TM
187 for (;;) {
188 req = nfs_update_request(ctx, page, offset, count);
189 if (!IS_ERR(req))
190 break;
191 ret = PTR_ERR(req);
192 if (ret != -EBUSY)
193 return ret;
194 ret = nfs_wb_page(page->mapping->host, page);
195 if (ret != 0)
196 return ret;
197 }
1da177e4
LT
198 /* Update file length */
199 nfs_grow_file(page, offset, count);
acee478a 200 nfs_clear_page_tag_locked(req);
abd3e641 201 return 0;
1da177e4
LT
202}
203
204static int wb_priority(struct writeback_control *wbc)
205{
206 if (wbc->for_reclaim)
c63c7b05 207 return FLUSH_HIGHPRI | FLUSH_STABLE;
1da177e4
LT
208 if (wbc->for_kupdate)
209 return FLUSH_LOWPRI;
210 return 0;
211}
212
89a09141
PZ
213/*
214 * NFS congestion control
215 */
216
217int nfs_congestion_kb;
218
219#define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
220#define NFS_CONGESTION_OFF_THRESH \
221 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
222
5a6d41b3 223static int nfs_set_page_writeback(struct page *page)
89a09141 224{
5a6d41b3
TM
225 int ret = test_set_page_writeback(page);
226
227 if (!ret) {
89a09141
PZ
228 struct inode *inode = page->mapping->host;
229 struct nfs_server *nfss = NFS_SERVER(inode);
230
277866a0 231 if (atomic_long_inc_return(&nfss->writeback) >
89a09141
PZ
232 NFS_CONGESTION_ON_THRESH)
233 set_bdi_congested(&nfss->backing_dev_info, WRITE);
234 }
5a6d41b3 235 return ret;
89a09141
PZ
236}
237
238static void nfs_end_page_writeback(struct page *page)
239{
240 struct inode *inode = page->mapping->host;
241 struct nfs_server *nfss = NFS_SERVER(inode);
242
243 end_page_writeback(page);
c4dc4bee 244 if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
89a09141 245 clear_bdi_congested(&nfss->backing_dev_info, WRITE);
89a09141
PZ
246}
247
e261f51f
TM
248/*
249 * Find an associated nfs write request, and prepare to flush it out
9cccef95 250 * May return an error if the user signalled nfs_wait_on_request().
e261f51f 251 */
c63c7b05
TM
252static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
253 struct page *page)
e261f51f 254{
587142f8 255 struct inode *inode = page->mapping->host;
e261f51f 256 struct nfs_page *req;
e261f51f
TM
257 int ret;
258
587142f8 259 spin_lock(&inode->i_lock);
e261f51f
TM
260 for(;;) {
261 req = nfs_page_find_request_locked(page);
262 if (req == NULL) {
587142f8 263 spin_unlock(&inode->i_lock);
9cccef95 264 return 0;
e261f51f 265 }
acee478a 266 if (nfs_set_page_tag_locked(req))
e261f51f
TM
267 break;
268 /* Note: If we hold the page lock, as is the case in nfs_writepage,
acee478a 269 * then the call to nfs_set_page_tag_locked() will always
e261f51f
TM
270 * succeed provided that someone hasn't already marked the
271 * request as dirty (in which case we don't care).
272 */
587142f8 273 spin_unlock(&inode->i_lock);
e261f51f
TM
274 ret = nfs_wait_on_request(req);
275 nfs_release_request(req);
276 if (ret != 0)
277 return ret;
587142f8 278 spin_lock(&inode->i_lock);
e261f51f 279 }
612c9384
TM
280 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
281 /* This request is marked for commit */
587142f8 282 spin_unlock(&inode->i_lock);
acee478a 283 nfs_clear_page_tag_locked(req);
c63c7b05 284 nfs_pageio_complete(pgio);
9cccef95 285 return 0;
612c9384 286 }
c63c7b05 287 if (nfs_set_page_writeback(page) != 0) {
587142f8 288 spin_unlock(&inode->i_lock);
c63c7b05
TM
289 BUG();
290 }
587142f8 291 spin_unlock(&inode->i_lock);
f8512ad0
FI
292 if (!nfs_pageio_add_request(pgio, req)) {
293 nfs_redirty_request(req);
294 nfs_end_page_writeback(page);
295 nfs_clear_page_tag_locked(req);
296 return pgio->pg_error;
297 }
9cccef95 298 return 0;
e261f51f
TM
299}
300
f758c885 301static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
1da177e4 302{
1da177e4 303 struct inode *inode = page->mapping->host;
1da177e4 304
91d5b470
CL
305 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
306 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
307
7fe7f848 308 nfs_pageio_cond_complete(pgio, page->index);
f758c885
TM
309 return nfs_page_async_flush(pgio, page);
310}
7fe7f848 311
f758c885
TM
312/*
313 * Write an mmapped page to the server.
314 */
315static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
316{
317 struct nfs_pageio_descriptor pgio;
318 int err;
49a70f27 319
f758c885
TM
320 nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
321 err = nfs_do_writepage(page, wbc, &pgio);
322 nfs_pageio_complete(&pgio);
323 if (err < 0)
324 return err;
325 if (pgio.pg_error < 0)
326 return pgio.pg_error;
327 return 0;
4d770ccf
TM
328}
329
330int nfs_writepage(struct page *page, struct writeback_control *wbc)
331{
f758c885 332 int ret;
4d770ccf 333
f758c885 334 ret = nfs_writepage_locked(page, wbc);
1da177e4 335 unlock_page(page);
f758c885
TM
336 return ret;
337}
338
339static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
340{
341 int ret;
342
343 ret = nfs_do_writepage(page, wbc, data);
344 unlock_page(page);
345 return ret;
1da177e4
LT
346}
347
1da177e4
LT
348int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
349{
1da177e4 350 struct inode *inode = mapping->host;
c63c7b05 351 struct nfs_pageio_descriptor pgio;
1da177e4
LT
352 int 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);
f758c885 359 if (err < 0)
1da177e4 360 return err;
f758c885 361 if (pgio.pg_error < 0)
c63c7b05
TM
362 return pgio.pg_error;
363 return 0;
1da177e4
LT
364}
365
366/*
367 * Insert a write request into an inode
368 */
369static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
370{
371 struct nfs_inode *nfsi = NFS_I(inode);
372 int error;
373
374 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
375 BUG_ON(error == -EEXIST);
376 if (error)
377 return error;
378 if (!nfsi->npages) {
379 igrab(inode);
1da177e4
LT
380 if (nfs_have_delegation(inode, FMODE_WRITE))
381 nfsi->change_attr++;
382 }
deb7d638 383 SetPagePrivate(req->wb_page);
277459d2 384 set_page_private(req->wb_page, (unsigned long)req);
1da177e4 385 nfsi->npages++;
c03b4024 386 kref_get(&req->wb_kref);
acee478a 387 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_LOCKED);
1da177e4
LT
388 return 0;
389}
390
391/*
89a09141 392 * Remove a write request from an inode
1da177e4
LT
393 */
394static void nfs_inode_remove_request(struct nfs_page *req)
395{
88be9f99 396 struct inode *inode = req->wb_context->path.dentry->d_inode;
1da177e4
LT
397 struct nfs_inode *nfsi = NFS_I(inode);
398
399 BUG_ON (!NFS_WBACK_BUSY(req));
400
587142f8 401 spin_lock(&inode->i_lock);
277459d2 402 set_page_private(req->wb_page, 0);
deb7d638 403 ClearPagePrivate(req->wb_page);
1da177e4
LT
404 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
405 nfsi->npages--;
406 if (!nfsi->npages) {
587142f8 407 spin_unlock(&inode->i_lock);
1da177e4
LT
408 iput(inode);
409 } else
587142f8 410 spin_unlock(&inode->i_lock);
1da177e4
LT
411 nfs_clear_request(req);
412 nfs_release_request(req);
413}
414
61822ab5
TM
415static void
416nfs_redirty_request(struct nfs_page *req)
417{
61822ab5
TM
418 __set_page_dirty_nobuffers(req->wb_page);
419}
420
1da177e4
LT
421/*
422 * Check if a request is dirty
423 */
424static inline int
425nfs_dirty_request(struct nfs_page *req)
426{
5a6d41b3
TM
427 struct page *page = req->wb_page;
428
612c9384 429 if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags))
5a6d41b3
TM
430 return 0;
431 return !PageWriteback(req->wb_page);
1da177e4
LT
432}
433
434#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
435/*
436 * Add a request to the inode's commit list.
437 */
438static void
439nfs_mark_request_commit(struct nfs_page *req)
440{
88be9f99 441 struct inode *inode = req->wb_context->path.dentry->d_inode;
1da177e4
LT
442 struct nfs_inode *nfsi = NFS_I(inode);
443
587142f8 444 spin_lock(&inode->i_lock);
1da177e4 445 nfsi->ncommit++;
612c9384 446 set_bit(PG_NEED_COMMIT, &(req)->wb_flags);
5c369683
TM
447 radix_tree_tag_set(&nfsi->nfs_page_tree,
448 req->wb_index,
449 NFS_PAGE_TAG_COMMIT);
587142f8 450 spin_unlock(&inode->i_lock);
fd39fc85 451 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
c9e51e41 452 inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
a1803044 453 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
1da177e4 454}
8e821cad
TM
455
456static inline
457int nfs_write_need_commit(struct nfs_write_data *data)
458{
459 return data->verf.committed != NFS_FILE_SYNC;
460}
461
462static inline
463int nfs_reschedule_unstable_write(struct nfs_page *req)
464{
612c9384 465 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
8e821cad
TM
466 nfs_mark_request_commit(req);
467 return 1;
468 }
469 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
470 nfs_redirty_request(req);
471 return 1;
472 }
473 return 0;
474}
475#else
476static inline void
477nfs_mark_request_commit(struct nfs_page *req)
478{
479}
480
481static inline
482int nfs_write_need_commit(struct nfs_write_data *data)
483{
484 return 0;
485}
486
487static inline
488int nfs_reschedule_unstable_write(struct nfs_page *req)
489{
490 return 0;
491}
1da177e4
LT
492#endif
493
494/*
495 * Wait for a request to complete.
496 *
150030b7 497 * Interruptible by fatal signals only.
1da177e4 498 */
ca52fec1 499static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages)
1da177e4
LT
500{
501 struct nfs_inode *nfsi = NFS_I(inode);
502 struct nfs_page *req;
ca52fec1 503 pgoff_t idx_end, next;
1da177e4
LT
504 unsigned int res = 0;
505 int error;
506
507 if (npages == 0)
508 idx_end = ~0;
509 else
510 idx_end = idx_start + npages - 1;
511
1da177e4 512 next = idx_start;
9fd367f0 513 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_LOCKED)) {
1da177e4
LT
514 if (req->wb_index > idx_end)
515 break;
516
517 next = req->wb_index + 1;
c6a556b8 518 BUG_ON(!NFS_WBACK_BUSY(req));
1da177e4 519
c03b4024 520 kref_get(&req->wb_kref);
587142f8 521 spin_unlock(&inode->i_lock);
1da177e4
LT
522 error = nfs_wait_on_request(req);
523 nfs_release_request(req);
587142f8 524 spin_lock(&inode->i_lock);
1da177e4
LT
525 if (error < 0)
526 return error;
1da177e4
LT
527 res++;
528 }
1da177e4
LT
529 return res;
530}
531
83715ad5
TM
532static void nfs_cancel_commit_list(struct list_head *head)
533{
534 struct nfs_page *req;
535
536 while(!list_empty(head)) {
537 req = nfs_list_entry(head->next);
b6dff26a 538 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
c9e51e41
PZ
539 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
540 BDI_RECLAIMABLE);
83715ad5 541 nfs_list_remove_request(req);
612c9384 542 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
83715ad5 543 nfs_inode_remove_request(req);
b6dff26a 544 nfs_unlock_request(req);
83715ad5
TM
545 }
546}
547
1da177e4
LT
548#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
549/*
550 * nfs_scan_commit - Scan an inode for commit requests
551 * @inode: NFS inode to scan
552 * @dst: destination list
553 * @idx_start: lower bound of page->index to scan.
554 * @npages: idx_start + npages sets the upper bound to scan.
555 *
556 * Moves requests from the inode's 'commit' request list.
557 * The requests are *not* checked to ensure that they form a contiguous set.
558 */
559static int
ca52fec1 560nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
1da177e4
LT
561{
562 struct nfs_inode *nfsi = NFS_I(inode);
3da28eb1
TM
563 int res = 0;
564
565 if (nfsi->ncommit != 0) {
5c369683
TM
566 res = nfs_scan_list(nfsi, dst, idx_start, npages,
567 NFS_PAGE_TAG_COMMIT);
3da28eb1 568 nfsi->ncommit -= res;
3da28eb1 569 }
1da177e4
LT
570 return res;
571}
c42de9dd 572#else
ca52fec1 573static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
c42de9dd
TM
574{
575 return 0;
576}
1da177e4
LT
577#endif
578
1da177e4
LT
579/*
580 * Try to update any existing write request, or create one if there is none.
581 * In order to match, the request's credentials must match those of
582 * the calling process.
583 *
584 * Note: Should always be called with the Page Lock held!
585 */
586static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
e21195a7 587 struct page *page, unsigned int offset, unsigned int bytes)
1da177e4 588{
89a09141
PZ
589 struct address_space *mapping = page->mapping;
590 struct inode *inode = mapping->host;
1da177e4 591 struct nfs_page *req, *new = NULL;
ca52fec1 592 pgoff_t rqend, end;
1da177e4
LT
593
594 end = offset + bytes;
595
1da177e4
LT
596 for (;;) {
597 /* Loop over all inode entries and see if we find
598 * A request for the page we wish to update
599 */
587142f8 600 spin_lock(&inode->i_lock);
277459d2 601 req = nfs_page_find_request_locked(page);
1da177e4 602 if (req) {
acee478a 603 if (!nfs_set_page_tag_locked(req)) {
1da177e4 604 int error;
277459d2 605
587142f8 606 spin_unlock(&inode->i_lock);
1da177e4
LT
607 error = nfs_wait_on_request(req);
608 nfs_release_request(req);
1dd594b2
NB
609 if (error < 0) {
610 if (new)
611 nfs_release_request(new);
1da177e4 612 return ERR_PTR(error);
1dd594b2 613 }
1da177e4
LT
614 continue;
615 }
587142f8 616 spin_unlock(&inode->i_lock);
1da177e4
LT
617 if (new)
618 nfs_release_request(new);
619 break;
620 }
621
622 if (new) {
623 int error;
624 nfs_lock_request_dontget(new);
625 error = nfs_inode_add_request(inode, new);
626 if (error) {
587142f8 627 spin_unlock(&inode->i_lock);
1da177e4
LT
628 nfs_unlock_request(new);
629 return ERR_PTR(error);
630 }
587142f8 631 spin_unlock(&inode->i_lock);
61e930a9
TM
632 req = new;
633 goto zero_page;
1da177e4 634 }
587142f8 635 spin_unlock(&inode->i_lock);
1da177e4
LT
636
637 new = nfs_create_request(ctx, inode, page, offset, bytes);
638 if (IS_ERR(new))
639 return new;
640 }
641
642 /* We have a request for our page.
643 * If the creds don't match, or the
644 * page addresses don't match,
645 * tell the caller to wait on the conflicting
646 * request.
647 */
648 rqend = req->wb_offset + req->wb_bytes;
649 if (req->wb_context != ctx
650 || req->wb_page != page
651 || !nfs_dirty_request(req)
652 || offset > rqend || end < req->wb_offset) {
acee478a 653 nfs_clear_page_tag_locked(req);
1da177e4
LT
654 return ERR_PTR(-EBUSY);
655 }
656
657 /* Okay, the request matches. Update the region */
658 if (offset < req->wb_offset) {
659 req->wb_offset = offset;
660 req->wb_pgbase = offset;
61e930a9
TM
661 req->wb_bytes = max(end, rqend) - req->wb_offset;
662 goto zero_page;
1da177e4
LT
663 }
664
665 if (end > rqend)
666 req->wb_bytes = end - req->wb_offset;
667
668 return req;
61e930a9
TM
669zero_page:
670 /* If this page might potentially be marked as up to date,
671 * then we need to zero any uninitalised data. */
672 if (req->wb_pgbase == 0 && req->wb_bytes != PAGE_CACHE_SIZE
673 && !PageUptodate(req->wb_page))
eebd2aa3 674 zero_user_segment(req->wb_page, req->wb_bytes, PAGE_CACHE_SIZE);
61e930a9 675 return req;
1da177e4
LT
676}
677
678int nfs_flush_incompatible(struct file *file, struct page *page)
679{
cd3758e3 680 struct nfs_open_context *ctx = nfs_file_open_context(file);
1da177e4 681 struct nfs_page *req;
1a54533e 682 int do_flush, status;
1da177e4
LT
683 /*
684 * Look for a request corresponding to this page. If there
685 * is one, and it belongs to another file, we flush it out
686 * before we try to copy anything into the page. Do this
687 * due to the lack of an ACCESS-type call in NFSv2.
688 * Also do the same if we find a request from an existing
689 * dropped page.
690 */
1a54533e
TM
691 do {
692 req = nfs_page_find_request(page);
693 if (req == NULL)
694 return 0;
695 do_flush = req->wb_page != page || req->wb_context != ctx
e261f51f 696 || !nfs_dirty_request(req);
1da177e4 697 nfs_release_request(req);
1a54533e
TM
698 if (!do_flush)
699 return 0;
700 status = nfs_wb_page(page->mapping->host, page);
701 } while (status == 0);
702 return status;
1da177e4
LT
703}
704
5d47a356
TM
705/*
706 * If the page cache is marked as unsafe or invalid, then we can't rely on
707 * the PageUptodate() flag. In this case, we will need to turn off
708 * write optimisations that depend on the page contents being correct.
709 */
710static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
711{
712 return PageUptodate(page) &&
713 !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
714}
715
1da177e4
LT
716/*
717 * Update and possibly write a cached page of an NFS file.
718 *
719 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
720 * things with a page scheduled for an RPC call (e.g. invalidate it).
721 */
722int nfs_updatepage(struct file *file, struct page *page,
723 unsigned int offset, unsigned int count)
724{
cd3758e3 725 struct nfs_open_context *ctx = nfs_file_open_context(file);
1da177e4 726 struct inode *inode = page->mapping->host;
1da177e4
LT
727 int status = 0;
728
91d5b470
CL
729 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
730
1da177e4 731 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
01cce933
JJS
732 file->f_path.dentry->d_parent->d_name.name,
733 file->f_path.dentry->d_name.name, count,
0bbacc40 734 (long long)(page_offset(page) +offset));
1da177e4 735
1da177e4 736 /* If we're not using byte range locks, and we know the page
5d47a356
TM
737 * is up to date, it may be more efficient to extend the write
738 * to cover the entire page in order to avoid fragmentation
739 * inefficiencies.
1da177e4 740 */
5d47a356
TM
741 if (nfs_write_pageuptodate(page, inode) &&
742 inode->i_flock == NULL &&
af1b8c2f 743 !(file->f_flags & O_SYNC)) {
49a70f27 744 count = max(count + offset, nfs_page_length(page));
1da177e4 745 offset = 0;
1da177e4
LT
746 }
747
e21195a7 748 status = nfs_writepage_setup(ctx, page, offset, count);
e261f51f 749 __set_page_dirty_nobuffers(page);
1da177e4 750
1da177e4
LT
751 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
752 status, (long long)i_size_read(inode));
753 if (status < 0)
a301b777 754 nfs_set_pageerror(page);
1da177e4
LT
755 return status;
756}
757
758static void nfs_writepage_release(struct nfs_page *req)
759{
1da177e4 760
44dd151d
TM
761 if (PageError(req->wb_page)) {
762 nfs_end_page_writeback(req->wb_page);
763 nfs_inode_remove_request(req);
764 } else if (!nfs_reschedule_unstable_write(req)) {
765 /* Set the PG_uptodate flag */
766 nfs_mark_uptodate(req->wb_page, req->wb_pgbase, req->wb_bytes);
8e821cad
TM
767 nfs_end_page_writeback(req->wb_page);
768 nfs_inode_remove_request(req);
769 } else
770 nfs_end_page_writeback(req->wb_page);
9fd367f0 771 nfs_clear_page_tag_locked(req);
1da177e4
LT
772}
773
3ff7576d 774static int flush_task_priority(int how)
1da177e4
LT
775{
776 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
777 case FLUSH_HIGHPRI:
778 return RPC_PRIORITY_HIGH;
779 case FLUSH_LOWPRI:
780 return RPC_PRIORITY_LOW;
781 }
782 return RPC_PRIORITY_NORMAL;
783}
784
785/*
786 * Set up the argument/result storage required for the RPC call.
787 */
788static void nfs_write_rpcsetup(struct nfs_page *req,
789 struct nfs_write_data *data,
788e7a89 790 const struct rpc_call_ops *call_ops,
1da177e4
LT
791 unsigned int count, unsigned int offset,
792 int how)
793{
84115e1c
TM
794 struct inode *inode = req->wb_context->path.dentry->d_inode;
795 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
3ff7576d 796 int priority = flush_task_priority(how);
07737691 797 struct rpc_task *task;
bdc7f021
TM
798 struct rpc_message msg = {
799 .rpc_argp = &data->args,
800 .rpc_resp = &data->res,
801 .rpc_cred = req->wb_context->cred,
802 };
84115e1c
TM
803 struct rpc_task_setup task_setup_data = {
804 .rpc_client = NFS_CLIENT(inode),
07737691 805 .task = &data->task,
bdc7f021 806 .rpc_message = &msg,
84115e1c
TM
807 .callback_ops = call_ops,
808 .callback_data = data,
809 .flags = flags,
3ff7576d 810 .priority = priority,
84115e1c 811 };
1da177e4
LT
812
813 /* Set up the RPC argument and reply structs
814 * NB: take care not to mess about with data->commit et al. */
815
816 data->req = req;
88be9f99 817 data->inode = inode = req->wb_context->path.dentry->d_inode;
bdc7f021 818 data->cred = msg.rpc_cred;
1da177e4
LT
819
820 data->args.fh = NFS_FH(inode);
821 data->args.offset = req_offset(req) + offset;
822 data->args.pgbase = req->wb_pgbase + offset;
823 data->args.pages = data->pagevec;
824 data->args.count = count;
825 data->args.context = req->wb_context;
bdc7f021
TM
826 data->args.stable = NFS_UNSTABLE;
827 if (how & FLUSH_STABLE) {
828 data->args.stable = NFS_DATA_SYNC;
829 if (!NFS_I(inode)->ncommit)
830 data->args.stable = NFS_FILE_SYNC;
831 }
1da177e4
LT
832
833 data->res.fattr = &data->fattr;
834 data->res.count = count;
835 data->res.verf = &data->verf;
0e574af1 836 nfs_fattr_init(&data->fattr);
1da177e4 837
788e7a89 838 /* Set up the initial task struct. */
bdc7f021 839 NFS_PROTO(inode)->write_setup(data, &msg);
1da177e4 840
a3f565b1
CL
841 dprintk("NFS: %5u initiated write call "
842 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
0bbacc40 843 data->task.tk_pid,
1da177e4
LT
844 inode->i_sb->s_id,
845 (long long)NFS_FILEID(inode),
846 count,
847 (unsigned long long)data->args.offset);
1da177e4 848
07737691
TM
849 task = rpc_run_task(&task_setup_data);
850 if (!IS_ERR(task))
851 rpc_put_task(task);
1da177e4
LT
852}
853
854/*
855 * Generate multiple small requests to write out a single
856 * contiguous dirty area on one page.
857 */
8d5658c9 858static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
1da177e4
LT
859{
860 struct nfs_page *req = nfs_list_entry(head->next);
861 struct page *page = req->wb_page;
862 struct nfs_write_data *data;
e9f7bee1
TM
863 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
864 unsigned int offset;
1da177e4
LT
865 int requests = 0;
866 LIST_HEAD(list);
867
868 nfs_list_remove_request(req);
869
bcb71bba 870 nbytes = count;
e9f7bee1
TM
871 do {
872 size_t len = min(nbytes, wsize);
873
8d5658c9 874 data = nfs_writedata_alloc(1);
1da177e4
LT
875 if (!data)
876 goto out_bad;
877 list_add(&data->pages, &list);
878 requests++;
e9f7bee1
TM
879 nbytes -= len;
880 } while (nbytes != 0);
1da177e4
LT
881 atomic_set(&req->wb_complete, requests);
882
883 ClearPageError(page);
1da177e4 884 offset = 0;
bcb71bba 885 nbytes = count;
1da177e4
LT
886 do {
887 data = list_entry(list.next, struct nfs_write_data, pages);
888 list_del_init(&data->pages);
889
890 data->pagevec[0] = page;
1da177e4 891
bcb71bba
TM
892 if (nbytes < wsize)
893 wsize = nbytes;
894 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
895 wsize, offset, how);
896 offset += wsize;
897 nbytes -= wsize;
1da177e4
LT
898 } while (nbytes != 0);
899
900 return 0;
901
902out_bad:
903 while (!list_empty(&list)) {
904 data = list_entry(list.next, struct nfs_write_data, pages);
905 list_del(&data->pages);
8aca67f0 906 nfs_writedata_release(data);
1da177e4 907 }
61822ab5 908 nfs_redirty_request(req);
6d677e35 909 nfs_end_page_writeback(req->wb_page);
9fd367f0 910 nfs_clear_page_tag_locked(req);
1da177e4
LT
911 return -ENOMEM;
912}
913
914/*
915 * Create an RPC task for the given write request and kick it.
916 * The page must have been locked by the caller.
917 *
918 * It may happen that the page we're passed is not marked dirty.
919 * This is the case if nfs_updatepage detects a conflicting request
920 * that has been written but not committed.
921 */
8d5658c9 922static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
1da177e4
LT
923{
924 struct nfs_page *req;
925 struct page **pages;
926 struct nfs_write_data *data;
1da177e4 927
8d5658c9 928 data = nfs_writedata_alloc(npages);
1da177e4
LT
929 if (!data)
930 goto out_bad;
931
932 pages = data->pagevec;
1da177e4
LT
933 while (!list_empty(head)) {
934 req = nfs_list_entry(head->next);
935 nfs_list_remove_request(req);
936 nfs_list_add_request(req, &data->pages);
937 ClearPageError(req->wb_page);
1da177e4 938 *pages++ = req->wb_page;
1da177e4
LT
939 }
940 req = nfs_list_entry(data->pages.next);
941
1da177e4 942 /* Set up the argument struct */
788e7a89 943 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
1da177e4 944
1da177e4
LT
945 return 0;
946 out_bad:
947 while (!list_empty(head)) {
10afec90 948 req = nfs_list_entry(head->next);
1da177e4 949 nfs_list_remove_request(req);
61822ab5 950 nfs_redirty_request(req);
6d677e35 951 nfs_end_page_writeback(req->wb_page);
9fd367f0 952 nfs_clear_page_tag_locked(req);
1da177e4
LT
953 }
954 return -ENOMEM;
955}
956
c63c7b05
TM
957static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
958 struct inode *inode, int ioflags)
1da177e4 959{
bf4285e7 960 size_t wsize = NFS_SERVER(inode)->wsize;
1da177e4 961
bcb71bba 962 if (wsize < PAGE_CACHE_SIZE)
c63c7b05 963 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
bcb71bba 964 else
c63c7b05 965 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
1da177e4
LT
966}
967
968/*
969 * Handle a write reply that flushed part of a page.
970 */
788e7a89 971static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
1da177e4 972{
788e7a89 973 struct nfs_write_data *data = calldata;
1da177e4
LT
974 struct nfs_page *req = data->req;
975 struct page *page = req->wb_page;
976
977 dprintk("NFS: write (%s/%Ld %d@%Ld)",
88be9f99
TM
978 req->wb_context->path.dentry->d_inode->i_sb->s_id,
979 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1da177e4
LT
980 req->wb_bytes,
981 (long long)req_offset(req));
982
788e7a89
TM
983 if (nfs_writeback_done(task, data) != 0)
984 return;
985
986 if (task->tk_status < 0) {
a301b777 987 nfs_set_pageerror(page);
7b159fc1 988 nfs_context_set_write_error(req->wb_context, task->tk_status);
788e7a89 989 dprintk(", error = %d\n", task->tk_status);
8e821cad 990 goto out;
1da177e4
LT
991 }
992
8e821cad 993 if (nfs_write_need_commit(data)) {
587142f8 994 struct inode *inode = page->mapping->host;
8e821cad 995
587142f8 996 spin_lock(&inode->i_lock);
8e821cad
TM
997 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
998 /* Do nothing we need to resend the writes */
999 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1000 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1001 dprintk(" defer commit\n");
1002 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1003 set_bit(PG_NEED_RESCHED, &req->wb_flags);
1004 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
1005 dprintk(" server reboot detected\n");
1006 }
587142f8 1007 spin_unlock(&inode->i_lock);
8e821cad
TM
1008 } else
1009 dprintk(" OK\n");
1010
1011out:
1da177e4
LT
1012 if (atomic_dec_and_test(&req->wb_complete))
1013 nfs_writepage_release(req);
1014}
1015
788e7a89
TM
1016static const struct rpc_call_ops nfs_write_partial_ops = {
1017 .rpc_call_done = nfs_writeback_done_partial,
1018 .rpc_release = nfs_writedata_release,
1019};
1020
1da177e4
LT
1021/*
1022 * Handle a write reply that flushes a whole page.
1023 *
1024 * FIXME: There is an inherent race with invalidate_inode_pages and
1025 * writebacks since the page->count is kept > 1 for as long
1026 * as the page has a write request pending.
1027 */
788e7a89 1028static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1da177e4 1029{
788e7a89 1030 struct nfs_write_data *data = calldata;
1da177e4
LT
1031 struct nfs_page *req;
1032 struct page *page;
1033
788e7a89
TM
1034 if (nfs_writeback_done(task, data) != 0)
1035 return;
1036
1da177e4
LT
1037 /* Update attributes as result of writeback. */
1038 while (!list_empty(&data->pages)) {
1039 req = nfs_list_entry(data->pages.next);
1040 nfs_list_remove_request(req);
1041 page = req->wb_page;
1042
1043 dprintk("NFS: write (%s/%Ld %d@%Ld)",
88be9f99
TM
1044 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1045 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1da177e4
LT
1046 req->wb_bytes,
1047 (long long)req_offset(req));
1048
788e7a89 1049 if (task->tk_status < 0) {
a301b777 1050 nfs_set_pageerror(page);
7b159fc1 1051 nfs_context_set_write_error(req->wb_context, task->tk_status);
788e7a89 1052 dprintk(", error = %d\n", task->tk_status);
8e821cad 1053 goto remove_request;
1da177e4 1054 }
1da177e4 1055
8e821cad
TM
1056 if (nfs_write_need_commit(data)) {
1057 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1058 nfs_mark_request_commit(req);
1059 nfs_end_page_writeback(page);
1060 dprintk(" marked for commit\n");
1da177e4
LT
1061 goto next;
1062 }
44dd151d
TM
1063 /* Set the PG_uptodate flag? */
1064 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
8e821cad
TM
1065 dprintk(" OK\n");
1066remove_request:
1067 nfs_end_page_writeback(page);
1da177e4 1068 nfs_inode_remove_request(req);
1da177e4 1069 next:
9fd367f0 1070 nfs_clear_page_tag_locked(req);
1da177e4
LT
1071 }
1072}
1073
788e7a89
TM
1074static const struct rpc_call_ops nfs_write_full_ops = {
1075 .rpc_call_done = nfs_writeback_done_full,
1076 .rpc_release = nfs_writedata_release,
1077};
1078
1079
1da177e4
LT
1080/*
1081 * This function is called when the WRITE call is complete.
1082 */
462d5b32 1083int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1da177e4 1084{
1da177e4
LT
1085 struct nfs_writeargs *argp = &data->args;
1086 struct nfs_writeres *resp = &data->res;
788e7a89 1087 int status;
1da177e4 1088
a3f565b1 1089 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1da177e4
LT
1090 task->tk_pid, task->tk_status);
1091
f551e44f
CL
1092 /*
1093 * ->write_done will attempt to use post-op attributes to detect
1094 * conflicting writes by other clients. A strict interpretation
1095 * of close-to-open would allow us to continue caching even if
1096 * another writer had changed the file, but some applications
1097 * depend on tighter cache coherency when writing.
1098 */
788e7a89
TM
1099 status = NFS_PROTO(data->inode)->write_done(task, data);
1100 if (status != 0)
1101 return status;
91d5b470
CL
1102 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1103
1da177e4
LT
1104#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1105 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1106 /* We tried a write call, but the server did not
1107 * commit data to stable storage even though we
1108 * requested it.
1109 * Note: There is a known bug in Tru64 < 5.0 in which
1110 * the server reports NFS_DATA_SYNC, but performs
1111 * NFS_FILE_SYNC. We therefore implement this checking
1112 * as a dprintk() in order to avoid filling syslog.
1113 */
1114 static unsigned long complain;
1115
1116 if (time_before(complain, jiffies)) {
1117 dprintk("NFS: faulty NFS server %s:"
1118 " (committed = %d) != (stable = %d)\n",
54ceac45 1119 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
1da177e4
LT
1120 resp->verf->committed, argp->stable);
1121 complain = jiffies + 300 * HZ;
1122 }
1123 }
1124#endif
1125 /* Is this a short write? */
1126 if (task->tk_status >= 0 && resp->count < argp->count) {
1127 static unsigned long complain;
1128
91d5b470
CL
1129 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1130
1da177e4
LT
1131 /* Has the server at least made some progress? */
1132 if (resp->count != 0) {
1133 /* Was this an NFSv2 write or an NFSv3 stable write? */
1134 if (resp->verf->committed != NFS_UNSTABLE) {
1135 /* Resend from where the server left off */
1136 argp->offset += resp->count;
1137 argp->pgbase += resp->count;
1138 argp->count -= resp->count;
1139 } else {
1140 /* Resend as a stable write in order to avoid
1141 * headaches in the case of a server crash.
1142 */
1143 argp->stable = NFS_FILE_SYNC;
1144 }
1145 rpc_restart_call(task);
788e7a89 1146 return -EAGAIN;
1da177e4
LT
1147 }
1148 if (time_before(complain, jiffies)) {
1149 printk(KERN_WARNING
1150 "NFS: Server wrote zero bytes, expected %u.\n",
1151 argp->count);
1152 complain = jiffies + 300 * HZ;
1153 }
1154 /* Can't do anything about it except throw an error. */
1155 task->tk_status = -EIO;
1156 }
788e7a89 1157 return 0;
1da177e4
LT
1158}
1159
1160
1161#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
963d8fe5 1162void nfs_commit_release(void *wdata)
1da177e4 1163{
1da177e4
LT
1164 nfs_commit_free(wdata);
1165}
1166
1167/*
1168 * Set up the argument/result storage required for the RPC call.
1169 */
1170static void nfs_commit_rpcsetup(struct list_head *head,
788e7a89
TM
1171 struct nfs_write_data *data,
1172 int how)
1da177e4 1173{
84115e1c
TM
1174 struct nfs_page *first = nfs_list_entry(head->next);
1175 struct inode *inode = first->wb_context->path.dentry->d_inode;
1176 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
3ff7576d 1177 int priority = flush_task_priority(how);
07737691 1178 struct rpc_task *task;
bdc7f021
TM
1179 struct rpc_message msg = {
1180 .rpc_argp = &data->args,
1181 .rpc_resp = &data->res,
1182 .rpc_cred = first->wb_context->cred,
1183 };
84115e1c 1184 struct rpc_task_setup task_setup_data = {
07737691 1185 .task = &data->task,
84115e1c 1186 .rpc_client = NFS_CLIENT(inode),
bdc7f021 1187 .rpc_message = &msg,
84115e1c
TM
1188 .callback_ops = &nfs_commit_ops,
1189 .callback_data = data,
1190 .flags = flags,
3ff7576d 1191 .priority = priority,
84115e1c 1192 };
1da177e4
LT
1193
1194 /* Set up the RPC argument and reply structs
1195 * NB: take care not to mess about with data->commit et al. */
1196
1197 list_splice_init(head, &data->pages);
1da177e4 1198
1da177e4 1199 data->inode = inode;
bdc7f021 1200 data->cred = msg.rpc_cred;
1da177e4
LT
1201
1202 data->args.fh = NFS_FH(data->inode);
3da28eb1
TM
1203 /* Note: we always request a commit of the entire inode */
1204 data->args.offset = 0;
1205 data->args.count = 0;
1206 data->res.count = 0;
1da177e4
LT
1207 data->res.fattr = &data->fattr;
1208 data->res.verf = &data->verf;
0e574af1 1209 nfs_fattr_init(&data->fattr);
788e7a89
TM
1210
1211 /* Set up the initial task struct. */
bdc7f021 1212 NFS_PROTO(inode)->commit_setup(data, &msg);
1da177e4 1213
a3f565b1 1214 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
bdc7f021 1215
07737691
TM
1216 task = rpc_run_task(&task_setup_data);
1217 if (!IS_ERR(task))
1218 rpc_put_task(task);
1da177e4
LT
1219}
1220
1221/*
1222 * Commit dirty pages
1223 */
1224static int
40859d7e 1225nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1da177e4
LT
1226{
1227 struct nfs_write_data *data;
1228 struct nfs_page *req;
1229
e9f7bee1 1230 data = nfs_commit_alloc();
1da177e4
LT
1231
1232 if (!data)
1233 goto out_bad;
1234
1235 /* Set up the argument struct */
1236 nfs_commit_rpcsetup(head, data, how);
1237
1da177e4
LT
1238 return 0;
1239 out_bad:
1240 while (!list_empty(head)) {
1241 req = nfs_list_entry(head->next);
1242 nfs_list_remove_request(req);
1243 nfs_mark_request_commit(req);
83715ad5 1244 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
c9e51e41
PZ
1245 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1246 BDI_RECLAIMABLE);
9fd367f0 1247 nfs_clear_page_tag_locked(req);
1da177e4
LT
1248 }
1249 return -ENOMEM;
1250}
1251
1252/*
1253 * COMMIT call returned
1254 */
788e7a89 1255static void nfs_commit_done(struct rpc_task *task, void *calldata)
1da177e4 1256{
963d8fe5 1257 struct nfs_write_data *data = calldata;
1da177e4 1258 struct nfs_page *req;
1da177e4 1259
a3f565b1 1260 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1da177e4
LT
1261 task->tk_pid, task->tk_status);
1262
788e7a89
TM
1263 /* Call the NFS version-specific code */
1264 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1265 return;
1266
1da177e4
LT
1267 while (!list_empty(&data->pages)) {
1268 req = nfs_list_entry(data->pages.next);
1269 nfs_list_remove_request(req);
612c9384 1270 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
fd39fc85 1271 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
c9e51e41
PZ
1272 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1273 BDI_RECLAIMABLE);
1da177e4
LT
1274
1275 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
88be9f99
TM
1276 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1277 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1da177e4
LT
1278 req->wb_bytes,
1279 (long long)req_offset(req));
1280 if (task->tk_status < 0) {
7b159fc1 1281 nfs_context_set_write_error(req->wb_context, task->tk_status);
1da177e4
LT
1282 nfs_inode_remove_request(req);
1283 dprintk(", error = %d\n", task->tk_status);
1284 goto next;
1285 }
1286
1287 /* Okay, COMMIT succeeded, apparently. Check the verifier
1288 * returned by the server against all stored verfs. */
1289 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1290 /* We have a match */
44dd151d
TM
1291 /* Set the PG_uptodate flag */
1292 nfs_mark_uptodate(req->wb_page, req->wb_pgbase,
1293 req->wb_bytes);
1da177e4
LT
1294 nfs_inode_remove_request(req);
1295 dprintk(" OK\n");
1296 goto next;
1297 }
1298 /* We have a mismatch. Write the page again */
1299 dprintk(" mismatch\n");
61822ab5 1300 nfs_redirty_request(req);
1da177e4 1301 next:
9fd367f0 1302 nfs_clear_page_tag_locked(req);
1da177e4 1303 }
1da177e4 1304}
788e7a89
TM
1305
1306static const struct rpc_call_ops nfs_commit_ops = {
1307 .rpc_call_done = nfs_commit_done,
1308 .rpc_release = nfs_commit_release,
1309};
1da177e4 1310
3da28eb1 1311int nfs_commit_inode(struct inode *inode, int how)
1da177e4 1312{
1da177e4 1313 LIST_HEAD(head);
7d46a49f 1314 int res;
1da177e4 1315
587142f8 1316 spin_lock(&inode->i_lock);
3da28eb1 1317 res = nfs_scan_commit(inode, &head, 0, 0);
587142f8 1318 spin_unlock(&inode->i_lock);
1da177e4 1319 if (res) {
7d46a49f 1320 int error = nfs_commit_list(inode, &head, how);
3da28eb1
TM
1321 if (error < 0)
1322 return error;
1323 }
1da177e4
LT
1324 return res;
1325}
c63c7b05
TM
1326#else
1327static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1328{
1329 return 0;
1330}
1da177e4
LT
1331#endif
1332
1c75950b 1333long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
1da177e4 1334{
1c75950b 1335 struct inode *inode = mapping->host;
ca52fec1 1336 pgoff_t idx_start, idx_end;
1c75950b 1337 unsigned int npages = 0;
c42de9dd 1338 LIST_HEAD(head);
70b9ecbd 1339 int nocommit = how & FLUSH_NOCOMMIT;
3f442547 1340 long pages, ret;
1da177e4 1341
1c75950b
TM
1342 /* FIXME */
1343 if (wbc->range_cyclic)
1344 idx_start = 0;
1345 else {
1346 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1347 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1348 if (idx_end > idx_start) {
ca52fec1 1349 pgoff_t l_npages = 1 + idx_end - idx_start;
1c75950b
TM
1350 npages = l_npages;
1351 if (sizeof(npages) != sizeof(l_npages) &&
ca52fec1 1352 (pgoff_t)npages != l_npages)
1c75950b
TM
1353 npages = 0;
1354 }
1355 }
c42de9dd 1356 how &= ~FLUSH_NOCOMMIT;
587142f8 1357 spin_lock(&inode->i_lock);
1da177e4 1358 do {
c42de9dd
TM
1359 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1360 if (ret != 0)
70b9ecbd 1361 continue;
c42de9dd
TM
1362 if (nocommit)
1363 break;
d2ccddf0 1364 pages = nfs_scan_commit(inode, &head, idx_start, npages);
724c439c 1365 if (pages == 0)
c42de9dd 1366 break;
d2ccddf0 1367 if (how & FLUSH_INVALIDATE) {
587142f8 1368 spin_unlock(&inode->i_lock);
83715ad5 1369 nfs_cancel_commit_list(&head);
e8e058e8 1370 ret = pages;
587142f8 1371 spin_lock(&inode->i_lock);
d2ccddf0
TM
1372 continue;
1373 }
1374 pages += nfs_scan_commit(inode, &head, 0, 0);
587142f8 1375 spin_unlock(&inode->i_lock);
c42de9dd 1376 ret = nfs_commit_list(inode, &head, how);
587142f8
TM
1377 spin_lock(&inode->i_lock);
1378
c42de9dd 1379 } while (ret >= 0);
587142f8 1380 spin_unlock(&inode->i_lock);
c42de9dd 1381 return ret;
1da177e4
LT
1382}
1383
34901f70 1384static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
1c75950b 1385{
1c75950b
TM
1386 int ret;
1387
34901f70 1388 ret = nfs_writepages(mapping, wbc);
61822ab5
TM
1389 if (ret < 0)
1390 goto out;
34901f70 1391 ret = nfs_sync_mapping_wait(mapping, wbc, how);
ed90ef51
TM
1392 if (ret < 0)
1393 goto out;
1394 return 0;
61822ab5 1395out:
e507d9eb 1396 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1c75950b
TM
1397 return ret;
1398}
1399
34901f70
TM
1400/* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
1401static int nfs_write_mapping(struct address_space *mapping, int how)
1402{
1403 struct writeback_control wbc = {
1404 .bdi = mapping->backing_dev_info,
1405 .sync_mode = WB_SYNC_NONE,
1406 .nr_to_write = LONG_MAX,
1407 .for_writepages = 1,
1408 .range_cyclic = 1,
1409 };
1410 int ret;
1411
1412 ret = __nfs_write_mapping(mapping, &wbc, how);
1413 if (ret < 0)
1414 return ret;
1415 wbc.sync_mode = WB_SYNC_ALL;
1416 return __nfs_write_mapping(mapping, &wbc, how);
1417}
1418
ed90ef51
TM
1419/*
1420 * flush the inode to disk.
1421 */
1422int nfs_wb_all(struct inode *inode)
1c75950b 1423{
ed90ef51
TM
1424 return nfs_write_mapping(inode->i_mapping, 0);
1425}
1c75950b 1426
ed90ef51
TM
1427int nfs_wb_nocommit(struct inode *inode)
1428{
1429 return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
1c75950b
TM
1430}
1431
1b3b4a1a
TM
1432int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1433{
1434 struct nfs_page *req;
1435 loff_t range_start = page_offset(page);
1436 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1437 struct writeback_control wbc = {
1438 .bdi = page->mapping->backing_dev_info,
1439 .sync_mode = WB_SYNC_ALL,
1440 .nr_to_write = LONG_MAX,
1441 .range_start = range_start,
1442 .range_end = range_end,
1443 };
1444 int ret = 0;
1445
1446 BUG_ON(!PageLocked(page));
1447 for (;;) {
1448 req = nfs_page_find_request(page);
1449 if (req == NULL)
1450 goto out;
1451 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1452 nfs_release_request(req);
1453 break;
1454 }
1455 if (nfs_lock_request_dontget(req)) {
1456 nfs_inode_remove_request(req);
1457 /*
1458 * In case nfs_inode_remove_request has marked the
1459 * page as being dirty
1460 */
1461 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1462 nfs_unlock_request(req);
1463 break;
1464 }
1465 ret = nfs_wait_on_request(req);
1466 if (ret < 0)
1467 goto out;
1468 }
1469 if (!PagePrivate(page))
1470 return 0;
1471 ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
1472out:
1473 return ret;
1474}
1475
5334eb13
AB
1476static int nfs_wb_page_priority(struct inode *inode, struct page *page,
1477 int how)
1c75950b
TM
1478{
1479 loff_t range_start = page_offset(page);
1480 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
4d770ccf
TM
1481 struct writeback_control wbc = {
1482 .bdi = page->mapping->backing_dev_info,
1483 .sync_mode = WB_SYNC_ALL,
1484 .nr_to_write = LONG_MAX,
1485 .range_start = range_start,
1486 .range_end = range_end,
1487 };
1488 int ret;
1c75950b 1489
4d770ccf 1490 BUG_ON(!PageLocked(page));
c63c7b05 1491 if (clear_page_dirty_for_io(page)) {
4d770ccf
TM
1492 ret = nfs_writepage_locked(page, &wbc);
1493 if (ret < 0)
1494 goto out;
1495 }
f40313ac
TM
1496 if (!PagePrivate(page))
1497 return 0;
4d770ccf
TM
1498 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1499 if (ret >= 0)
1500 return 0;
1501out:
e507d9eb 1502 __mark_inode_dirty(inode, I_DIRTY_PAGES);
4d770ccf 1503 return ret;
1c75950b
TM
1504}
1505
1506/*
1507 * Write back all requests on one page - we do this before reading it.
1508 */
1509int nfs_wb_page(struct inode *inode, struct page* page)
1510{
4d770ccf 1511 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
1c75950b
TM
1512}
1513
f7b422b1 1514int __init nfs_init_writepagecache(void)
1da177e4
LT
1515{
1516 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1517 sizeof(struct nfs_write_data),
1518 0, SLAB_HWCACHE_ALIGN,
20c2df83 1519 NULL);
1da177e4
LT
1520 if (nfs_wdata_cachep == NULL)
1521 return -ENOMEM;
1522
93d2341c
MD
1523 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1524 nfs_wdata_cachep);
1da177e4
LT
1525 if (nfs_wdata_mempool == NULL)
1526 return -ENOMEM;
1527
93d2341c
MD
1528 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1529 nfs_wdata_cachep);
1da177e4
LT
1530 if (nfs_commit_mempool == NULL)
1531 return -ENOMEM;
1532
89a09141
PZ
1533 /*
1534 * NFS congestion size, scale with available memory.
1535 *
1536 * 64MB: 8192k
1537 * 128MB: 11585k
1538 * 256MB: 16384k
1539 * 512MB: 23170k
1540 * 1GB: 32768k
1541 * 2GB: 46340k
1542 * 4GB: 65536k
1543 * 8GB: 92681k
1544 * 16GB: 131072k
1545 *
1546 * This allows larger machines to have larger/more transfers.
1547 * Limit the default to 256M
1548 */
1549 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1550 if (nfs_congestion_kb > 256*1024)
1551 nfs_congestion_kb = 256*1024;
1552
1da177e4
LT
1553 return 0;
1554}
1555
266bee88 1556void nfs_destroy_writepagecache(void)
1da177e4
LT
1557{
1558 mempool_destroy(nfs_commit_mempool);
1559 mempool_destroy(nfs_wdata_mempool);
1a1d92c1 1560 kmem_cache_destroy(nfs_wdata_cachep);
1da177e4
LT
1561}
1562
This page took 0.628433 seconds and 5 git commands to generate.