blob: 6f7a4af3bc46d9d7286b4c329005bd94b34331cc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
49#include <linux/config.h>
50#include <linux/types.h>
51#include <linux/slab.h>
52#include <linux/mm.h>
53#include <linux/pagemap.h>
54#include <linux/file.h>
55#include <linux/mpage.h>
56#include <linux/writeback.h>
57
58#include <linux/sunrpc/clnt.h>
59#include <linux/nfs_fs.h>
60#include <linux/nfs_mount.h>
61#include <linux/nfs_page.h>
62#include <asm/uaccess.h>
63#include <linux/smp_lock.h>
64
65#include "delegation.h"
66
67#define NFSDBG_FACILITY NFSDBG_PAGECACHE
68
69#define MIN_POOL_WRITE (32)
70#define MIN_POOL_COMMIT (4)
71
72/*
73 * Local function declarations
74 */
75static struct nfs_page * nfs_update_request(struct nfs_open_context*,
76 struct inode *,
77 struct page *,
78 unsigned int, unsigned int);
79static void nfs_writeback_done_partial(struct nfs_write_data *, int);
80static void nfs_writeback_done_full(struct nfs_write_data *, int);
81static int nfs_wait_on_write_congestion(struct address_space *, int);
82static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int);
83static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
84 unsigned int npages, int how);
85
86static kmem_cache_t *nfs_wdata_cachep;
87mempool_t *nfs_wdata_mempool;
88static mempool_t *nfs_commit_mempool;
89
90static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
91
92static inline struct nfs_write_data *nfs_commit_alloc(void)
93{
94 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS);
95 if (p) {
96 memset(p, 0, sizeof(*p));
97 INIT_LIST_HEAD(&p->pages);
98 }
99 return p;
100}
101
102static inline void nfs_commit_free(struct nfs_write_data *p)
103{
104 mempool_free(p, nfs_commit_mempool);
105}
106
107static void nfs_writedata_release(struct rpc_task *task)
108{
109 struct nfs_write_data *wdata = (struct nfs_write_data *)task->tk_calldata;
110 nfs_writedata_free(wdata);
111}
112
113/* Adjust the file length if we're writing beyond the end */
114static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
115{
116 struct inode *inode = page->mapping->host;
117 loff_t end, i_size = i_size_read(inode);
118 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
119
120 if (i_size > 0 && page->index < end_index)
121 return;
122 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
123 if (i_size >= end)
124 return;
125 i_size_write(inode, end);
126}
127
128/* We can set the PG_uptodate flag if we see that a write request
129 * covers the full page.
130 */
131static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
132{
133 loff_t end_offs;
134
135 if (PageUptodate(page))
136 return;
137 if (base != 0)
138 return;
139 if (count == PAGE_CACHE_SIZE) {
140 SetPageUptodate(page);
141 return;
142 }
143
144 end_offs = i_size_read(page->mapping->host) - 1;
145 if (end_offs < 0)
146 return;
147 /* Is this the last page? */
148 if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT))
149 return;
150 /* This is the last page: set PG_uptodate if we cover the entire
151 * extent of the data, then zero the rest of the page.
152 */
153 if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) {
154 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
155 SetPageUptodate(page);
156 }
157}
158
159/*
160 * Write a page synchronously.
161 * Offset is the data offset within the page.
162 */
163static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
164 struct page *page, unsigned int offset, unsigned int count,
165 int how)
166{
167 unsigned int wsize = NFS_SERVER(inode)->wsize;
168 int result, written = 0;
169 struct nfs_write_data *wdata;
170
171 wdata = nfs_writedata_alloc();
172 if (!wdata)
173 return -ENOMEM;
174
175 wdata->flags = how;
176 wdata->cred = ctx->cred;
177 wdata->inode = inode;
178 wdata->args.fh = NFS_FH(inode);
179 wdata->args.context = ctx;
180 wdata->args.pages = &page;
181 wdata->args.stable = NFS_FILE_SYNC;
182 wdata->args.pgbase = offset;
183 wdata->args.count = wsize;
184 wdata->res.fattr = &wdata->fattr;
185 wdata->res.verf = &wdata->verf;
186
187 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
188 inode->i_sb->s_id,
189 (long long)NFS_FILEID(inode),
190 count, (long long)(page_offset(page) + offset));
191
192 nfs_begin_data_update(inode);
193 do {
194 if (count < wsize)
195 wdata->args.count = count;
196 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
197
198 result = NFS_PROTO(inode)->write(wdata);
199
200 if (result < 0) {
201 /* Must mark the page invalid after I/O error */
202 ClearPageUptodate(page);
203 goto io_error;
204 }
205 if (result < wdata->args.count)
206 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
207 wdata->args.count, result);
208
209 wdata->args.offset += result;
210 wdata->args.pgbase += result;
211 written += result;
212 count -= result;
213 } while (count);
214 /* Update file length */
215 nfs_grow_file(page, offset, written);
216 /* Set the PG_uptodate flag? */
217 nfs_mark_uptodate(page, offset, written);
218
219 if (PageError(page))
220 ClearPageError(page);
221
222io_error:
223 nfs_end_data_update_defer(inode);
224 nfs_writedata_free(wdata);
225 return written ? written : result;
226}
227
228static int nfs_writepage_async(struct nfs_open_context *ctx,
229 struct inode *inode, struct page *page,
230 unsigned int offset, unsigned int count)
231{
232 struct nfs_page *req;
233 int status;
234
235 req = nfs_update_request(ctx, inode, page, offset, count);
236 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
237 if (status < 0)
238 goto out;
239 /* Update file length */
240 nfs_grow_file(page, offset, count);
241 /* Set the PG_uptodate flag? */
242 nfs_mark_uptodate(page, offset, count);
243 nfs_unlock_request(req);
244 out:
245 return status;
246}
247
248static int wb_priority(struct writeback_control *wbc)
249{
250 if (wbc->for_reclaim)
251 return FLUSH_HIGHPRI;
252 if (wbc->for_kupdate)
253 return FLUSH_LOWPRI;
254 return 0;
255}
256
257/*
258 * Write an mmapped page to the server.
259 */
260int nfs_writepage(struct page *page, struct writeback_control *wbc)
261{
262 struct nfs_open_context *ctx;
263 struct inode *inode = page->mapping->host;
264 unsigned long end_index;
265 unsigned offset = PAGE_CACHE_SIZE;
266 loff_t i_size = i_size_read(inode);
267 int inode_referenced = 0;
268 int priority = wb_priority(wbc);
269 int err;
270
271 /*
272 * Note: We need to ensure that we have a reference to the inode
273 * if we are to do asynchronous writes. If not, waiting
274 * in nfs_wait_on_request() may deadlock with clear_inode().
275 *
276 * If igrab() fails here, then it is in any case safe to
277 * call nfs_wb_page(), since there will be no pending writes.
278 */
279 if (igrab(inode) != 0)
280 inode_referenced = 1;
281 end_index = i_size >> PAGE_CACHE_SHIFT;
282
283 /* Ensure we've flushed out any previous writes */
284 nfs_wb_page_priority(inode, page, priority);
285
286 /* easy case */
287 if (page->index < end_index)
288 goto do_it;
289 /* things got complicated... */
290 offset = i_size & (PAGE_CACHE_SIZE-1);
291
292 /* OK, are we completely out? */
293 err = 0; /* potential race with truncate - ignore */
294 if (page->index >= end_index+1 || !offset)
295 goto out;
296do_it:
297 ctx = nfs_find_open_context(inode, FMODE_WRITE);
298 if (ctx == NULL) {
299 err = -EBADF;
300 goto out;
301 }
302 lock_kernel();
303 if (!IS_SYNC(inode) && inode_referenced) {
304 err = nfs_writepage_async(ctx, inode, page, 0, offset);
305 if (err >= 0) {
306 err = 0;
307 if (wbc->for_reclaim)
308 nfs_flush_inode(inode, 0, 0, FLUSH_STABLE);
309 }
310 } else {
311 err = nfs_writepage_sync(ctx, inode, page, 0,
312 offset, priority);
313 if (err >= 0) {
314 if (err != offset)
315 redirty_page_for_writepage(wbc, page);
316 err = 0;
317 }
318 }
319 unlock_kernel();
320 put_nfs_open_context(ctx);
321out:
322 unlock_page(page);
323 if (inode_referenced)
324 iput(inode);
325 return err;
326}
327
328/*
329 * Note: causes nfs_update_request() to block on the assumption
330 * that the writeback is generated due to memory pressure.
331 */
332int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
333{
334 struct backing_dev_info *bdi = mapping->backing_dev_info;
335 struct inode *inode = mapping->host;
336 int err;
337
338 err = generic_writepages(mapping, wbc);
339 if (err)
340 return err;
341 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
342 if (wbc->nonblocking)
343 return 0;
344 nfs_wait_on_write_congestion(mapping, 0);
345 }
346 err = nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
347 if (err < 0)
348 goto out;
349 wbc->nr_to_write -= err;
350 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
351 err = nfs_wait_on_requests(inode, 0, 0);
352 if (err < 0)
353 goto out;
354 }
355 err = nfs_commit_inode(inode, 0, 0, wb_priority(wbc));
356 if (err > 0) {
357 wbc->nr_to_write -= err;
358 err = 0;
359 }
360out:
361 clear_bit(BDI_write_congested, &bdi->state);
362 wake_up_all(&nfs_write_congestion);
363 return err;
364}
365
366/*
367 * Insert a write request into an inode
368 */
369static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
370{
371 struct nfs_inode *nfsi = NFS_I(inode);
372 int error;
373
374 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
375 BUG_ON(error == -EEXIST);
376 if (error)
377 return error;
378 if (!nfsi->npages) {
379 igrab(inode);
380 nfs_begin_data_update(inode);
381 if (nfs_have_delegation(inode, FMODE_WRITE))
382 nfsi->change_attr++;
383 }
384 nfsi->npages++;
385 atomic_inc(&req->wb_count);
386 return 0;
387}
388
389/*
390 * Insert a write request into an inode
391 */
392static void nfs_inode_remove_request(struct nfs_page *req)
393{
394 struct inode *inode = req->wb_context->dentry->d_inode;
395 struct nfs_inode *nfsi = NFS_I(inode);
396
397 BUG_ON (!NFS_WBACK_BUSY(req));
398
399 spin_lock(&nfsi->req_lock);
400 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
401 nfsi->npages--;
402 if (!nfsi->npages) {
403 spin_unlock(&nfsi->req_lock);
404 nfs_end_data_update_defer(inode);
405 iput(inode);
406 } else
407 spin_unlock(&nfsi->req_lock);
408 nfs_clear_request(req);
409 nfs_release_request(req);
410}
411
412/*
413 * Find a request
414 */
415static inline struct nfs_page *
416_nfs_find_request(struct inode *inode, unsigned long index)
417{
418 struct nfs_inode *nfsi = NFS_I(inode);
419 struct nfs_page *req;
420
421 req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index);
422 if (req)
423 atomic_inc(&req->wb_count);
424 return req;
425}
426
427static struct nfs_page *
428nfs_find_request(struct inode *inode, unsigned long index)
429{
430 struct nfs_page *req;
431 struct nfs_inode *nfsi = NFS_I(inode);
432
433 spin_lock(&nfsi->req_lock);
434 req = _nfs_find_request(inode, index);
435 spin_unlock(&nfsi->req_lock);
436 return req;
437}
438
439/*
440 * Add a request to the inode's dirty list.
441 */
442static void
443nfs_mark_request_dirty(struct nfs_page *req)
444{
445 struct inode *inode = req->wb_context->dentry->d_inode;
446 struct nfs_inode *nfsi = NFS_I(inode);
447
448 spin_lock(&nfsi->req_lock);
449 nfs_list_add_request(req, &nfsi->dirty);
450 nfsi->ndirty++;
451 spin_unlock(&nfsi->req_lock);
452 inc_page_state(nr_dirty);
453 mark_inode_dirty(inode);
454}
455
456/*
457 * Check if a request is dirty
458 */
459static inline int
460nfs_dirty_request(struct nfs_page *req)
461{
462 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
463 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
464}
465
466#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
467/*
468 * Add a request to the inode's commit list.
469 */
470static void
471nfs_mark_request_commit(struct nfs_page *req)
472{
473 struct inode *inode = req->wb_context->dentry->d_inode;
474 struct nfs_inode *nfsi = NFS_I(inode);
475
476 spin_lock(&nfsi->req_lock);
477 nfs_list_add_request(req, &nfsi->commit);
478 nfsi->ncommit++;
479 spin_unlock(&nfsi->req_lock);
480 inc_page_state(nr_unstable);
481 mark_inode_dirty(inode);
482}
483#endif
484
485/*
486 * Wait for a request to complete.
487 *
488 * Interruptible by signals only if mounted with intr flag.
489 */
490static int
491nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
492{
493 struct nfs_inode *nfsi = NFS_I(inode);
494 struct nfs_page *req;
495 unsigned long idx_end, next;
496 unsigned int res = 0;
497 int error;
498
499 if (npages == 0)
500 idx_end = ~0;
501 else
502 idx_end = idx_start + npages - 1;
503
504 spin_lock(&nfsi->req_lock);
505 next = idx_start;
506 while (radix_tree_gang_lookup(&nfsi->nfs_page_tree, (void **)&req, next, 1)) {
507 if (req->wb_index > idx_end)
508 break;
509
510 next = req->wb_index + 1;
511 if (!NFS_WBACK_BUSY(req))
512 continue;
513
514 atomic_inc(&req->wb_count);
515 spin_unlock(&nfsi->req_lock);
516 error = nfs_wait_on_request(req);
517 nfs_release_request(req);
518 if (error < 0)
519 return error;
520 spin_lock(&nfsi->req_lock);
521 res++;
522 }
523 spin_unlock(&nfsi->req_lock);
524 return res;
525}
526
527/*
528 * nfs_scan_dirty - Scan an inode for dirty requests
529 * @inode: NFS inode to scan
530 * @dst: destination list
531 * @idx_start: lower bound of page->index to scan.
532 * @npages: idx_start + npages sets the upper bound to scan.
533 *
534 * Moves requests from the inode's dirty page list.
535 * The requests are *not* checked to ensure that they form a contiguous set.
536 */
537static int
538nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
539{
540 struct nfs_inode *nfsi = NFS_I(inode);
541 int res;
542 res = nfs_scan_list(&nfsi->dirty, dst, idx_start, npages);
543 nfsi->ndirty -= res;
544 sub_page_state(nr_dirty,res);
545 if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
546 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
547 return res;
548}
549
550#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
551/*
552 * nfs_scan_commit - Scan an inode for commit requests
553 * @inode: NFS inode to scan
554 * @dst: destination list
555 * @idx_start: lower bound of page->index to scan.
556 * @npages: idx_start + npages sets the upper bound to scan.
557 *
558 * Moves requests from the inode's 'commit' request list.
559 * The requests are *not* checked to ensure that they form a contiguous set.
560 */
561static int
562nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
563{
564 struct nfs_inode *nfsi = NFS_I(inode);
565 int res;
566 res = nfs_scan_list(&nfsi->commit, dst, idx_start, npages);
567 nfsi->ncommit -= res;
568 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
569 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
570 return res;
571}
572#endif
573
574static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
575{
576 struct backing_dev_info *bdi = mapping->backing_dev_info;
577 DEFINE_WAIT(wait);
578 int ret = 0;
579
580 might_sleep();
581
582 if (!bdi_write_congested(bdi))
583 return 0;
584 if (intr) {
585 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
586 sigset_t oldset;
587
588 rpc_clnt_sigmask(clnt, &oldset);
589 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
590 if (bdi_write_congested(bdi)) {
591 if (signalled())
592 ret = -ERESTARTSYS;
593 else
594 schedule();
595 }
596 rpc_clnt_sigunmask(clnt, &oldset);
597 } else {
598 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
599 if (bdi_write_congested(bdi))
600 schedule();
601 }
602 finish_wait(&nfs_write_congestion, &wait);
603 return ret;
604}
605
606
607/*
608 * Try to update any existing write request, or create one if there is none.
609 * In order to match, the request's credentials must match those of
610 * the calling process.
611 *
612 * Note: Should always be called with the Page Lock held!
613 */
614static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
615 struct inode *inode, struct page *page,
616 unsigned int offset, unsigned int bytes)
617{
618 struct nfs_server *server = NFS_SERVER(inode);
619 struct nfs_inode *nfsi = NFS_I(inode);
620 struct nfs_page *req, *new = NULL;
621 unsigned long rqend, end;
622
623 end = offset + bytes;
624
625 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
626 return ERR_PTR(-ERESTARTSYS);
627 for (;;) {
628 /* Loop over all inode entries and see if we find
629 * A request for the page we wish to update
630 */
631 spin_lock(&nfsi->req_lock);
632 req = _nfs_find_request(inode, page->index);
633 if (req) {
634 if (!nfs_lock_request_dontget(req)) {
635 int error;
636 spin_unlock(&nfsi->req_lock);
637 error = nfs_wait_on_request(req);
638 nfs_release_request(req);
639 if (error < 0)
640 return ERR_PTR(error);
641 continue;
642 }
643 spin_unlock(&nfsi->req_lock);
644 if (new)
645 nfs_release_request(new);
646 break;
647 }
648
649 if (new) {
650 int error;
651 nfs_lock_request_dontget(new);
652 error = nfs_inode_add_request(inode, new);
653 if (error) {
654 spin_unlock(&nfsi->req_lock);
655 nfs_unlock_request(new);
656 return ERR_PTR(error);
657 }
658 spin_unlock(&nfsi->req_lock);
659 nfs_mark_request_dirty(new);
660 return new;
661 }
662 spin_unlock(&nfsi->req_lock);
663
664 new = nfs_create_request(ctx, inode, page, offset, bytes);
665 if (IS_ERR(new))
666 return new;
667 }
668
669 /* We have a request for our page.
670 * If the creds don't match, or the
671 * page addresses don't match,
672 * tell the caller to wait on the conflicting
673 * request.
674 */
675 rqend = req->wb_offset + req->wb_bytes;
676 if (req->wb_context != ctx
677 || req->wb_page != page
678 || !nfs_dirty_request(req)
679 || offset > rqend || end < req->wb_offset) {
680 nfs_unlock_request(req);
681 return ERR_PTR(-EBUSY);
682 }
683
684 /* Okay, the request matches. Update the region */
685 if (offset < req->wb_offset) {
686 req->wb_offset = offset;
687 req->wb_pgbase = offset;
688 req->wb_bytes = rqend - req->wb_offset;
689 }
690
691 if (end > rqend)
692 req->wb_bytes = end - req->wb_offset;
693
694 return req;
695}
696
697int nfs_flush_incompatible(struct file *file, struct page *page)
698{
699 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
700 struct inode *inode = page->mapping->host;
701 struct nfs_page *req;
702 int status = 0;
703 /*
704 * Look for a request corresponding to this page. If there
705 * is one, and it belongs to another file, we flush it out
706 * before we try to copy anything into the page. Do this
707 * due to the lack of an ACCESS-type call in NFSv2.
708 * Also do the same if we find a request from an existing
709 * dropped page.
710 */
711 req = nfs_find_request(inode, page->index);
712 if (req) {
713 if (req->wb_page != page || ctx != req->wb_context)
714 status = nfs_wb_page(inode, page);
715 nfs_release_request(req);
716 }
717 return (status < 0) ? status : 0;
718}
719
720/*
721 * Update and possibly write a cached page of an NFS file.
722 *
723 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
724 * things with a page scheduled for an RPC call (e.g. invalidate it).
725 */
726int nfs_updatepage(struct file *file, struct page *page,
727 unsigned int offset, unsigned int count)
728{
729 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
730 struct dentry *dentry = file->f_dentry;
731 struct inode *inode = page->mapping->host;
732 struct nfs_page *req;
733 int status = 0;
734
735 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
736 dentry->d_parent->d_name.name, dentry->d_name.name,
737 count, (long long)(page_offset(page) +offset));
738
739 if (IS_SYNC(inode)) {
740 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
741 if (status > 0) {
742 if (offset == 0 && status == PAGE_CACHE_SIZE)
743 SetPageUptodate(page);
744 return 0;
745 }
746 return status;
747 }
748
749 /* If we're not using byte range locks, and we know the page
750 * is entirely in cache, it may be more efficient to avoid
751 * fragmenting write requests.
752 */
753 if (PageUptodate(page) && inode->i_flock == NULL) {
754 loff_t end_offs = i_size_read(inode) - 1;
755 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
756
757 count += offset;
758 offset = 0;
759 if (unlikely(end_offs < 0)) {
760 /* Do nothing */
761 } else if (page->index == end_index) {
762 unsigned int pglen;
763 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
764 if (count < pglen)
765 count = pglen;
766 } else if (page->index < end_index)
767 count = PAGE_CACHE_SIZE;
768 }
769
770 /*
771 * Try to find an NFS request corresponding to this page
772 * and update it.
773 * If the existing request cannot be updated, we must flush
774 * it out now.
775 */
776 do {
777 req = nfs_update_request(ctx, inode, page, offset, count);
778 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
779 if (status != -EBUSY)
780 break;
781 /* Request could not be updated. Flush it out and try again */
782 status = nfs_wb_page(inode, page);
783 } while (status >= 0);
784 if (status < 0)
785 goto done;
786
787 status = 0;
788
789 /* Update file length */
790 nfs_grow_file(page, offset, count);
791 /* Set the PG_uptodate flag? */
792 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
793 nfs_unlock_request(req);
794done:
795 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
796 status, (long long)i_size_read(inode));
797 if (status < 0)
798 ClearPageUptodate(page);
799 return status;
800}
801
802static void nfs_writepage_release(struct nfs_page *req)
803{
804 end_page_writeback(req->wb_page);
805
806#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
807 if (!PageError(req->wb_page)) {
808 if (NFS_NEED_RESCHED(req)) {
809 nfs_mark_request_dirty(req);
810 goto out;
811 } else if (NFS_NEED_COMMIT(req)) {
812 nfs_mark_request_commit(req);
813 goto out;
814 }
815 }
816 nfs_inode_remove_request(req);
817
818out:
819 nfs_clear_commit(req);
820 nfs_clear_reschedule(req);
821#else
822 nfs_inode_remove_request(req);
823#endif
824 nfs_unlock_request(req);
825}
826
827static inline int flush_task_priority(int how)
828{
829 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
830 case FLUSH_HIGHPRI:
831 return RPC_PRIORITY_HIGH;
832 case FLUSH_LOWPRI:
833 return RPC_PRIORITY_LOW;
834 }
835 return RPC_PRIORITY_NORMAL;
836}
837
838/*
839 * Set up the argument/result storage required for the RPC call.
840 */
841static void nfs_write_rpcsetup(struct nfs_page *req,
842 struct nfs_write_data *data,
843 unsigned int count, unsigned int offset,
844 int how)
845{
846 struct rpc_task *task = &data->task;
847 struct inode *inode;
848
849 /* Set up the RPC argument and reply structs
850 * NB: take care not to mess about with data->commit et al. */
851
852 data->req = req;
853 data->inode = inode = req->wb_context->dentry->d_inode;
854 data->cred = req->wb_context->cred;
855
856 data->args.fh = NFS_FH(inode);
857 data->args.offset = req_offset(req) + offset;
858 data->args.pgbase = req->wb_pgbase + offset;
859 data->args.pages = data->pagevec;
860 data->args.count = count;
861 data->args.context = req->wb_context;
862
863 data->res.fattr = &data->fattr;
864 data->res.count = count;
865 data->res.verf = &data->verf;
866
867 NFS_PROTO(inode)->write_setup(data, how);
868
869 data->task.tk_priority = flush_task_priority(how);
870 data->task.tk_cookie = (unsigned long)inode;
871 data->task.tk_calldata = data;
872 /* Release requests */
873 data->task.tk_release = nfs_writedata_release;
874
875 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
876 task->tk_pid,
877 inode->i_sb->s_id,
878 (long long)NFS_FILEID(inode),
879 count,
880 (unsigned long long)data->args.offset);
881}
882
883static void nfs_execute_write(struct nfs_write_data *data)
884{
885 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
886 sigset_t oldset;
887
888 rpc_clnt_sigmask(clnt, &oldset);
889 lock_kernel();
890 rpc_execute(&data->task);
891 unlock_kernel();
892 rpc_clnt_sigunmask(clnt, &oldset);
893}
894
895/*
896 * Generate multiple small requests to write out a single
897 * contiguous dirty area on one page.
898 */
899static int nfs_flush_multi(struct list_head *head, struct inode *inode, int how)
900{
901 struct nfs_page *req = nfs_list_entry(head->next);
902 struct page *page = req->wb_page;
903 struct nfs_write_data *data;
904 unsigned int wsize = NFS_SERVER(inode)->wsize;
905 unsigned int nbytes, offset;
906 int requests = 0;
907 LIST_HEAD(list);
908
909 nfs_list_remove_request(req);
910
911 nbytes = req->wb_bytes;
912 for (;;) {
913 data = nfs_writedata_alloc();
914 if (!data)
915 goto out_bad;
916 list_add(&data->pages, &list);
917 requests++;
918 if (nbytes <= wsize)
919 break;
920 nbytes -= wsize;
921 }
922 atomic_set(&req->wb_complete, requests);
923
924 ClearPageError(page);
925 SetPageWriteback(page);
926 offset = 0;
927 nbytes = req->wb_bytes;
928 do {
929 data = list_entry(list.next, struct nfs_write_data, pages);
930 list_del_init(&data->pages);
931
932 data->pagevec[0] = page;
933 data->complete = nfs_writeback_done_partial;
934
935 if (nbytes > wsize) {
936 nfs_write_rpcsetup(req, data, wsize, offset, how);
937 offset += wsize;
938 nbytes -= wsize;
939 } else {
940 nfs_write_rpcsetup(req, data, nbytes, offset, how);
941 nbytes = 0;
942 }
943 nfs_execute_write(data);
944 } while (nbytes != 0);
945
946 return 0;
947
948out_bad:
949 while (!list_empty(&list)) {
950 data = list_entry(list.next, struct nfs_write_data, pages);
951 list_del(&data->pages);
952 nfs_writedata_free(data);
953 }
954 nfs_mark_request_dirty(req);
955 nfs_unlock_request(req);
956 return -ENOMEM;
957}
958
959/*
960 * Create an RPC task for the given write request and kick it.
961 * The page must have been locked by the caller.
962 *
963 * It may happen that the page we're passed is not marked dirty.
964 * This is the case if nfs_updatepage detects a conflicting request
965 * that has been written but not committed.
966 */
967static int nfs_flush_one(struct list_head *head, struct inode *inode, int how)
968{
969 struct nfs_page *req;
970 struct page **pages;
971 struct nfs_write_data *data;
972 unsigned int count;
973
974 if (NFS_SERVER(inode)->wsize < PAGE_CACHE_SIZE)
975 return nfs_flush_multi(head, inode, how);
976
977 data = nfs_writedata_alloc();
978 if (!data)
979 goto out_bad;
980
981 pages = data->pagevec;
982 count = 0;
983 while (!list_empty(head)) {
984 req = nfs_list_entry(head->next);
985 nfs_list_remove_request(req);
986 nfs_list_add_request(req, &data->pages);
987 ClearPageError(req->wb_page);
988 SetPageWriteback(req->wb_page);
989 *pages++ = req->wb_page;
990 count += req->wb_bytes;
991 }
992 req = nfs_list_entry(data->pages.next);
993
994 data->complete = nfs_writeback_done_full;
995 /* Set up the argument struct */
996 nfs_write_rpcsetup(req, data, count, 0, how);
997
998 nfs_execute_write(data);
999 return 0;
1000 out_bad:
1001 while (!list_empty(head)) {
1002 struct nfs_page *req = nfs_list_entry(head->next);
1003 nfs_list_remove_request(req);
1004 nfs_mark_request_dirty(req);
1005 nfs_unlock_request(req);
1006 }
1007 return -ENOMEM;
1008}
1009
1010static int
1011nfs_flush_list(struct list_head *head, int wpages, int how)
1012{
1013 LIST_HEAD(one_request);
1014 struct nfs_page *req;
1015 int error = 0;
1016 unsigned int pages = 0;
1017
1018 while (!list_empty(head)) {
1019 pages += nfs_coalesce_requests(head, &one_request, wpages);
1020 req = nfs_list_entry(one_request.next);
1021 error = nfs_flush_one(&one_request, req->wb_context->dentry->d_inode, how);
1022 if (error < 0)
1023 break;
1024 }
1025 if (error >= 0)
1026 return pages;
1027
1028 while (!list_empty(head)) {
1029 req = nfs_list_entry(head->next);
1030 nfs_list_remove_request(req);
1031 nfs_mark_request_dirty(req);
1032 nfs_unlock_request(req);
1033 }
1034 return error;
1035}
1036
1037/*
1038 * Handle a write reply that flushed part of a page.
1039 */
1040static void nfs_writeback_done_partial(struct nfs_write_data *data, int status)
1041{
1042 struct nfs_page *req = data->req;
1043 struct page *page = req->wb_page;
1044
1045 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1046 req->wb_context->dentry->d_inode->i_sb->s_id,
1047 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1048 req->wb_bytes,
1049 (long long)req_offset(req));
1050
1051 if (status < 0) {
1052 ClearPageUptodate(page);
1053 SetPageError(page);
1054 req->wb_context->error = status;
1055 dprintk(", error = %d\n", status);
1056 } else {
1057#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1058 if (data->verf.committed < NFS_FILE_SYNC) {
1059 if (!NFS_NEED_COMMIT(req)) {
1060 nfs_defer_commit(req);
1061 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1062 dprintk(" defer commit\n");
1063 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1064 nfs_defer_reschedule(req);
1065 dprintk(" server reboot detected\n");
1066 }
1067 } else
1068#endif
1069 dprintk(" OK\n");
1070 }
1071
1072 if (atomic_dec_and_test(&req->wb_complete))
1073 nfs_writepage_release(req);
1074}
1075
1076/*
1077 * Handle a write reply that flushes a whole page.
1078 *
1079 * FIXME: There is an inherent race with invalidate_inode_pages and
1080 * writebacks since the page->count is kept > 1 for as long
1081 * as the page has a write request pending.
1082 */
1083static void nfs_writeback_done_full(struct nfs_write_data *data, int status)
1084{
1085 struct nfs_page *req;
1086 struct page *page;
1087
1088 /* Update attributes as result of writeback. */
1089 while (!list_empty(&data->pages)) {
1090 req = nfs_list_entry(data->pages.next);
1091 nfs_list_remove_request(req);
1092 page = req->wb_page;
1093
1094 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1095 req->wb_context->dentry->d_inode->i_sb->s_id,
1096 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1097 req->wb_bytes,
1098 (long long)req_offset(req));
1099
1100 if (status < 0) {
1101 ClearPageUptodate(page);
1102 SetPageError(page);
1103 req->wb_context->error = status;
1104 end_page_writeback(page);
1105 nfs_inode_remove_request(req);
1106 dprintk(", error = %d\n", status);
1107 goto next;
1108 }
1109 end_page_writeback(page);
1110
1111#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1112 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1113 nfs_inode_remove_request(req);
1114 dprintk(" OK\n");
1115 goto next;
1116 }
1117 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1118 nfs_mark_request_commit(req);
1119 dprintk(" marked for commit\n");
1120#else
1121 nfs_inode_remove_request(req);
1122#endif
1123 next:
1124 nfs_unlock_request(req);
1125 }
1126}
1127
1128/*
1129 * This function is called when the WRITE call is complete.
1130 */
1131void nfs_writeback_done(struct rpc_task *task)
1132{
1133 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
1134 struct nfs_writeargs *argp = &data->args;
1135 struct nfs_writeres *resp = &data->res;
1136
1137 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1138 task->tk_pid, task->tk_status);
1139
1140#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1141 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1142 /* We tried a write call, but the server did not
1143 * commit data to stable storage even though we
1144 * requested it.
1145 * Note: There is a known bug in Tru64 < 5.0 in which
1146 * the server reports NFS_DATA_SYNC, but performs
1147 * NFS_FILE_SYNC. We therefore implement this checking
1148 * as a dprintk() in order to avoid filling syslog.
1149 */
1150 static unsigned long complain;
1151
1152 if (time_before(complain, jiffies)) {
1153 dprintk("NFS: faulty NFS server %s:"
1154 " (committed = %d) != (stable = %d)\n",
1155 NFS_SERVER(data->inode)->hostname,
1156 resp->verf->committed, argp->stable);
1157 complain = jiffies + 300 * HZ;
1158 }
1159 }
1160#endif
1161 /* Is this a short write? */
1162 if (task->tk_status >= 0 && resp->count < argp->count) {
1163 static unsigned long complain;
1164
1165 /* Has the server at least made some progress? */
1166 if (resp->count != 0) {
1167 /* Was this an NFSv2 write or an NFSv3 stable write? */
1168 if (resp->verf->committed != NFS_UNSTABLE) {
1169 /* Resend from where the server left off */
1170 argp->offset += resp->count;
1171 argp->pgbase += resp->count;
1172 argp->count -= resp->count;
1173 } else {
1174 /* Resend as a stable write in order to avoid
1175 * headaches in the case of a server crash.
1176 */
1177 argp->stable = NFS_FILE_SYNC;
1178 }
1179 rpc_restart_call(task);
1180 return;
1181 }
1182 if (time_before(complain, jiffies)) {
1183 printk(KERN_WARNING
1184 "NFS: Server wrote zero bytes, expected %u.\n",
1185 argp->count);
1186 complain = jiffies + 300 * HZ;
1187 }
1188 /* Can't do anything about it except throw an error. */
1189 task->tk_status = -EIO;
1190 }
1191
1192 /*
1193 * Process the nfs_page list
1194 */
1195 data->complete(data, task->tk_status);
1196}
1197
1198
1199#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1200static void nfs_commit_release(struct rpc_task *task)
1201{
1202 struct nfs_write_data *wdata = (struct nfs_write_data *)task->tk_calldata;
1203 nfs_commit_free(wdata);
1204}
1205
1206/*
1207 * Set up the argument/result storage required for the RPC call.
1208 */
1209static void nfs_commit_rpcsetup(struct list_head *head,
1210 struct nfs_write_data *data, int how)
1211{
1212 struct rpc_task *task = &data->task;
1213 struct nfs_page *first, *last;
1214 struct inode *inode;
1215 loff_t start, end, len;
1216
1217 /* Set up the RPC argument and reply structs
1218 * NB: take care not to mess about with data->commit et al. */
1219
1220 list_splice_init(head, &data->pages);
1221 first = nfs_list_entry(data->pages.next);
1222 last = nfs_list_entry(data->pages.prev);
1223 inode = first->wb_context->dentry->d_inode;
1224
1225 /*
1226 * Determine the offset range of requests in the COMMIT call.
1227 * We rely on the fact that data->pages is an ordered list...
1228 */
1229 start = req_offset(first);
1230 end = req_offset(last) + last->wb_bytes;
1231 len = end - start;
1232 /* If 'len' is not a 32-bit quantity, pass '0' in the COMMIT call */
1233 if (end >= i_size_read(inode) || len < 0 || len > (~((u32)0) >> 1))
1234 len = 0;
1235
1236 data->inode = inode;
1237 data->cred = first->wb_context->cred;
1238
1239 data->args.fh = NFS_FH(data->inode);
1240 data->args.offset = start;
1241 data->args.count = len;
1242 data->res.count = len;
1243 data->res.fattr = &data->fattr;
1244 data->res.verf = &data->verf;
1245
1246 NFS_PROTO(inode)->commit_setup(data, how);
1247
1248 data->task.tk_priority = flush_task_priority(how);
1249 data->task.tk_cookie = (unsigned long)inode;
1250 data->task.tk_calldata = data;
1251 /* Release requests */
1252 data->task.tk_release = nfs_commit_release;
1253
1254 dprintk("NFS: %4d initiated commit call\n", task->tk_pid);
1255}
1256
1257/*
1258 * Commit dirty pages
1259 */
1260static int
1261nfs_commit_list(struct list_head *head, int how)
1262{
1263 struct nfs_write_data *data;
1264 struct nfs_page *req;
1265
1266 data = nfs_commit_alloc();
1267
1268 if (!data)
1269 goto out_bad;
1270
1271 /* Set up the argument struct */
1272 nfs_commit_rpcsetup(head, data, how);
1273
1274 nfs_execute_write(data);
1275 return 0;
1276 out_bad:
1277 while (!list_empty(head)) {
1278 req = nfs_list_entry(head->next);
1279 nfs_list_remove_request(req);
1280 nfs_mark_request_commit(req);
1281 nfs_unlock_request(req);
1282 }
1283 return -ENOMEM;
1284}
1285
1286/*
1287 * COMMIT call returned
1288 */
1289void
1290nfs_commit_done(struct rpc_task *task)
1291{
1292 struct nfs_write_data *data = (struct nfs_write_data *)task->tk_calldata;
1293 struct nfs_page *req;
1294 int res = 0;
1295
1296 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1297 task->tk_pid, task->tk_status);
1298
1299 while (!list_empty(&data->pages)) {
1300 req = nfs_list_entry(data->pages.next);
1301 nfs_list_remove_request(req);
1302
1303 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1304 req->wb_context->dentry->d_inode->i_sb->s_id,
1305 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1306 req->wb_bytes,
1307 (long long)req_offset(req));
1308 if (task->tk_status < 0) {
1309 req->wb_context->error = task->tk_status;
1310 nfs_inode_remove_request(req);
1311 dprintk(", error = %d\n", task->tk_status);
1312 goto next;
1313 }
1314
1315 /* Okay, COMMIT succeeded, apparently. Check the verifier
1316 * returned by the server against all stored verfs. */
1317 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1318 /* We have a match */
1319 nfs_inode_remove_request(req);
1320 dprintk(" OK\n");
1321 goto next;
1322 }
1323 /* We have a mismatch. Write the page again */
1324 dprintk(" mismatch\n");
1325 nfs_mark_request_dirty(req);
1326 next:
1327 nfs_unlock_request(req);
1328 res++;
1329 }
1330 sub_page_state(nr_unstable,res);
1331}
1332#endif
1333
1334static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
1335 unsigned int npages, int how)
1336{
1337 struct nfs_inode *nfsi = NFS_I(inode);
1338 LIST_HEAD(head);
1339 int res,
1340 error = 0;
1341
1342 spin_lock(&nfsi->req_lock);
1343 res = nfs_scan_dirty(inode, &head, idx_start, npages);
1344 spin_unlock(&nfsi->req_lock);
1345 if (res)
1346 error = nfs_flush_list(&head, NFS_SERVER(inode)->wpages, how);
1347 if (error < 0)
1348 return error;
1349 return res;
1350}
1351
1352#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1353int nfs_commit_inode(struct inode *inode, unsigned long idx_start,
1354 unsigned int npages, int how)
1355{
1356 struct nfs_inode *nfsi = NFS_I(inode);
1357 LIST_HEAD(head);
1358 int res,
1359 error = 0;
1360
1361 spin_lock(&nfsi->req_lock);
1362 res = nfs_scan_commit(inode, &head, idx_start, npages);
1363 if (res) {
1364 res += nfs_scan_commit(inode, &head, 0, 0);
1365 spin_unlock(&nfsi->req_lock);
1366 error = nfs_commit_list(&head, how);
1367 } else
1368 spin_unlock(&nfsi->req_lock);
1369 if (error < 0)
1370 return error;
1371 return res;
1372}
1373#endif
1374
1375int nfs_sync_inode(struct inode *inode, unsigned long idx_start,
1376 unsigned int npages, int how)
1377{
1378 int error,
1379 wait;
1380
1381 wait = how & FLUSH_WAIT;
1382 how &= ~FLUSH_WAIT;
1383
1384 do {
1385 error = 0;
1386 if (wait)
1387 error = nfs_wait_on_requests(inode, idx_start, npages);
1388 if (error == 0)
1389 error = nfs_flush_inode(inode, idx_start, npages, how);
1390#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1391 if (error == 0)
1392 error = nfs_commit_inode(inode, idx_start, npages, how);
1393#endif
1394 } while (error > 0);
1395 return error;
1396}
1397
1398int nfs_init_writepagecache(void)
1399{
1400 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1401 sizeof(struct nfs_write_data),
1402 0, SLAB_HWCACHE_ALIGN,
1403 NULL, NULL);
1404 if (nfs_wdata_cachep == NULL)
1405 return -ENOMEM;
1406
1407 nfs_wdata_mempool = mempool_create(MIN_POOL_WRITE,
1408 mempool_alloc_slab,
1409 mempool_free_slab,
1410 nfs_wdata_cachep);
1411 if (nfs_wdata_mempool == NULL)
1412 return -ENOMEM;
1413
1414 nfs_commit_mempool = mempool_create(MIN_POOL_COMMIT,
1415 mempool_alloc_slab,
1416 mempool_free_slab,
1417 nfs_wdata_cachep);
1418 if (nfs_commit_mempool == NULL)
1419 return -ENOMEM;
1420
1421 return 0;
1422}
1423
1424void nfs_destroy_writepagecache(void)
1425{
1426 mempool_destroy(nfs_commit_mempool);
1427 mempool_destroy(nfs_wdata_mempool);
1428 if (kmem_cache_destroy(nfs_wdata_cachep))
1429 printk(KERN_INFO "nfs_write_data: not all structures were freed\n");
1430}
1431