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