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