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