mm: fix exec activate_mm vs TLB shootdown and lazy tlb switching race
[linux/fpc-iii.git] / fs / nfs / write.c
blob767e46c09074bcba486db1e5dce4b7ede21a49c3
1 /*
2 * linux/fs/nfs/write.c
4 * Write file data over NFS.
6 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
7 */
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>
14 #include <linux/writeback.h>
15 #include <linux/swap.h>
16 #include <linux/migrate.h>
18 #include <linux/sunrpc/clnt.h>
19 #include <linux/nfs_fs.h>
20 #include <linux/nfs_mount.h>
21 #include <linux/nfs_page.h>
22 #include <linux/backing-dev.h>
23 #include <linux/export.h>
24 #include <linux/freezer.h>
25 #include <linux/wait.h>
27 #include <linux/uaccess.h>
29 #include "delegation.h"
30 #include "internal.h"
31 #include "iostat.h"
32 #include "nfs4_fs.h"
33 #include "fscache.h"
34 #include "pnfs.h"
36 #include "nfstrace.h"
38 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
40 #define MIN_POOL_WRITE (32)
41 #define MIN_POOL_COMMIT (4)
43 struct nfs_io_completion {
44 void (*complete)(void *data);
45 void *data;
46 struct kref refcount;
50 * Local function declarations
52 static void nfs_redirty_request(struct nfs_page *req);
53 static const struct rpc_call_ops nfs_commit_ops;
54 static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops;
55 static const struct nfs_commit_completion_ops nfs_commit_completion_ops;
56 static const struct nfs_rw_ops nfs_rw_write_ops;
57 static void nfs_clear_request_commit(struct nfs_page *req);
58 static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
59 struct inode *inode);
60 static struct nfs_page *
61 nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
62 struct page *page);
64 static struct kmem_cache *nfs_wdata_cachep;
65 static mempool_t *nfs_wdata_mempool;
66 static struct kmem_cache *nfs_cdata_cachep;
67 static mempool_t *nfs_commit_mempool;
69 struct nfs_commit_data *nfs_commitdata_alloc(bool never_fail)
71 struct nfs_commit_data *p;
73 if (never_fail)
74 p = mempool_alloc(nfs_commit_mempool, GFP_NOIO);
75 else {
76 /* It is OK to do some reclaim, not no safe to wait
77 * for anything to be returned to the pool.
78 * mempool_alloc() cannot handle that particular combination,
79 * so we need two separate attempts.
81 p = mempool_alloc(nfs_commit_mempool, GFP_NOWAIT);
82 if (!p)
83 p = kmem_cache_alloc(nfs_cdata_cachep, GFP_NOIO |
84 __GFP_NOWARN | __GFP_NORETRY);
85 if (!p)
86 return NULL;
89 memset(p, 0, sizeof(*p));
90 INIT_LIST_HEAD(&p->pages);
91 return p;
93 EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
95 void nfs_commit_free(struct nfs_commit_data *p)
97 mempool_free(p, nfs_commit_mempool);
99 EXPORT_SYMBOL_GPL(nfs_commit_free);
101 static struct nfs_pgio_header *nfs_writehdr_alloc(void)
103 struct nfs_pgio_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOIO);
105 memset(p, 0, sizeof(*p));
106 p->rw_mode = FMODE_WRITE;
107 return p;
110 static void nfs_writehdr_free(struct nfs_pgio_header *hdr)
112 mempool_free(hdr, nfs_wdata_mempool);
115 static struct nfs_io_completion *nfs_io_completion_alloc(gfp_t gfp_flags)
117 return kmalloc(sizeof(struct nfs_io_completion), gfp_flags);
120 static void nfs_io_completion_init(struct nfs_io_completion *ioc,
121 void (*complete)(void *), void *data)
123 ioc->complete = complete;
124 ioc->data = data;
125 kref_init(&ioc->refcount);
128 static void nfs_io_completion_release(struct kref *kref)
130 struct nfs_io_completion *ioc = container_of(kref,
131 struct nfs_io_completion, refcount);
132 ioc->complete(ioc->data);
133 kfree(ioc);
136 static void nfs_io_completion_get(struct nfs_io_completion *ioc)
138 if (ioc != NULL)
139 kref_get(&ioc->refcount);
142 static void nfs_io_completion_put(struct nfs_io_completion *ioc)
144 if (ioc != NULL)
145 kref_put(&ioc->refcount, nfs_io_completion_release);
148 static struct nfs_page *
149 nfs_page_private_request(struct page *page)
151 if (!PagePrivate(page))
152 return NULL;
153 return (struct nfs_page *)page_private(page);
157 * nfs_page_find_head_request_locked - find head request associated with @page
159 * must be called while holding the inode lock.
161 * returns matching head request with reference held, or NULL if not found.
163 static struct nfs_page *
164 nfs_page_find_private_request(struct page *page)
166 struct address_space *mapping = page_file_mapping(page);
167 struct nfs_page *req;
169 if (!PagePrivate(page))
170 return NULL;
171 spin_lock(&mapping->private_lock);
172 req = nfs_page_private_request(page);
173 if (req) {
174 WARN_ON_ONCE(req->wb_head != req);
175 kref_get(&req->wb_kref);
177 spin_unlock(&mapping->private_lock);
178 return req;
181 static struct nfs_page *
182 nfs_page_find_swap_request(struct page *page)
184 struct inode *inode = page_file_mapping(page)->host;
185 struct nfs_inode *nfsi = NFS_I(inode);
186 struct nfs_page *req = NULL;
187 if (!PageSwapCache(page))
188 return NULL;
189 mutex_lock(&nfsi->commit_mutex);
190 if (PageSwapCache(page)) {
191 req = nfs_page_search_commits_for_head_request_locked(nfsi,
192 page);
193 if (req) {
194 WARN_ON_ONCE(req->wb_head != req);
195 kref_get(&req->wb_kref);
198 mutex_unlock(&nfsi->commit_mutex);
199 return req;
203 * nfs_page_find_head_request - find head request associated with @page
205 * returns matching head request with reference held, or NULL if not found.
207 static struct nfs_page *nfs_page_find_head_request(struct page *page)
209 struct nfs_page *req;
211 req = nfs_page_find_private_request(page);
212 if (!req)
213 req = nfs_page_find_swap_request(page);
214 return req;
217 /* Adjust the file length if we're writing beyond the end */
218 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
220 struct inode *inode = page_file_mapping(page)->host;
221 loff_t end, i_size;
222 pgoff_t end_index;
224 spin_lock(&inode->i_lock);
225 i_size = i_size_read(inode);
226 end_index = (i_size - 1) >> PAGE_SHIFT;
227 if (i_size > 0 && page_index(page) < end_index)
228 goto out;
229 end = page_file_offset(page) + ((loff_t)offset+count);
230 if (i_size >= end)
231 goto out;
232 i_size_write(inode, end);
233 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
234 out:
235 spin_unlock(&inode->i_lock);
238 /* A writeback failed: mark the page as bad, and invalidate the page cache */
239 static void nfs_set_pageerror(struct address_space *mapping)
241 nfs_zap_mapping(mapping->host, mapping);
245 * nfs_page_group_search_locked
246 * @head - head request of page group
247 * @page_offset - offset into page
249 * Search page group with head @head to find a request that contains the
250 * page offset @page_offset.
252 * Returns a pointer to the first matching nfs request, or NULL if no
253 * match is found.
255 * Must be called with the page group lock held
257 static struct nfs_page *
258 nfs_page_group_search_locked(struct nfs_page *head, unsigned int page_offset)
260 struct nfs_page *req;
262 req = head;
263 do {
264 if (page_offset >= req->wb_pgbase &&
265 page_offset < (req->wb_pgbase + req->wb_bytes))
266 return req;
268 req = req->wb_this_page;
269 } while (req != head);
271 return NULL;
275 * nfs_page_group_covers_page
276 * @head - head request of page group
278 * Return true if the page group with head @head covers the whole page,
279 * returns false otherwise
281 static bool nfs_page_group_covers_page(struct nfs_page *req)
283 struct nfs_page *tmp;
284 unsigned int pos = 0;
285 unsigned int len = nfs_page_length(req->wb_page);
287 nfs_page_group_lock(req);
289 for (;;) {
290 tmp = nfs_page_group_search_locked(req->wb_head, pos);
291 if (!tmp)
292 break;
293 pos = tmp->wb_pgbase + tmp->wb_bytes;
296 nfs_page_group_unlock(req);
297 return pos >= len;
300 /* We can set the PG_uptodate flag if we see that a write request
301 * covers the full page.
303 static void nfs_mark_uptodate(struct nfs_page *req)
305 if (PageUptodate(req->wb_page))
306 return;
307 if (!nfs_page_group_covers_page(req))
308 return;
309 SetPageUptodate(req->wb_page);
312 static int wb_priority(struct writeback_control *wbc)
314 int ret = 0;
316 if (wbc->sync_mode == WB_SYNC_ALL)
317 ret = FLUSH_COND_STABLE;
318 return ret;
322 * NFS congestion control
325 int nfs_congestion_kb;
327 #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
328 #define NFS_CONGESTION_OFF_THRESH \
329 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
331 static void nfs_set_page_writeback(struct page *page)
333 struct inode *inode = page_file_mapping(page)->host;
334 struct nfs_server *nfss = NFS_SERVER(inode);
335 int ret = test_set_page_writeback(page);
337 WARN_ON_ONCE(ret != 0);
339 if (atomic_long_inc_return(&nfss->writeback) >
340 NFS_CONGESTION_ON_THRESH)
341 set_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
344 static void nfs_end_page_writeback(struct nfs_page *req)
346 struct inode *inode = page_file_mapping(req->wb_page)->host;
347 struct nfs_server *nfss = NFS_SERVER(inode);
348 bool is_done;
350 is_done = nfs_page_group_sync_on_bit(req, PG_WB_END);
351 nfs_unlock_request(req);
352 if (!is_done)
353 return;
355 end_page_writeback(req->wb_page);
356 if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
357 clear_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
361 * nfs_unroll_locks_and_wait - unlock all newly locked reqs and wait on @req
363 * this is a helper function for nfs_lock_and_join_requests
365 * @inode - inode associated with request page group, must be holding inode lock
366 * @head - head request of page group, must be holding head lock
367 * @req - request that couldn't lock and needs to wait on the req bit lock
369 * NOTE: this must be called holding page_group bit lock
370 * which will be released before returning.
372 * returns 0 on success, < 0 on error.
374 static void
375 nfs_unroll_locks(struct inode *inode, struct nfs_page *head,
376 struct nfs_page *req)
378 struct nfs_page *tmp;
380 /* relinquish all the locks successfully grabbed this run */
381 for (tmp = head->wb_this_page ; tmp != req; tmp = tmp->wb_this_page) {
382 if (!kref_read(&tmp->wb_kref))
383 continue;
384 nfs_unlock_and_release_request(tmp);
389 * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests
391 * @destroy_list - request list (using wb_this_page) terminated by @old_head
392 * @old_head - the old head of the list
394 * All subrequests must be locked and removed from all lists, so at this point
395 * they are only "active" in this function, and possibly in nfs_wait_on_request
396 * with a reference held by some other context.
398 static void
399 nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list,
400 struct nfs_page *old_head,
401 struct inode *inode)
403 while (destroy_list) {
404 struct nfs_page *subreq = destroy_list;
406 destroy_list = (subreq->wb_this_page == old_head) ?
407 NULL : subreq->wb_this_page;
409 /* Note: lock subreq in order to change subreq->wb_head */
410 nfs_page_set_headlock(subreq);
411 WARN_ON_ONCE(old_head != subreq->wb_head);
413 /* make sure old group is not used */
414 subreq->wb_this_page = subreq;
415 subreq->wb_head = subreq;
417 clear_bit(PG_REMOVE, &subreq->wb_flags);
419 /* Note: races with nfs_page_group_destroy() */
420 if (!kref_read(&subreq->wb_kref)) {
421 /* Check if we raced with nfs_page_group_destroy() */
422 if (test_and_clear_bit(PG_TEARDOWN, &subreq->wb_flags)) {
423 nfs_page_clear_headlock(subreq);
424 nfs_free_request(subreq);
425 } else
426 nfs_page_clear_headlock(subreq);
427 continue;
429 nfs_page_clear_headlock(subreq);
431 nfs_release_request(old_head);
433 if (test_and_clear_bit(PG_INODE_REF, &subreq->wb_flags)) {
434 nfs_release_request(subreq);
435 atomic_long_dec(&NFS_I(inode)->nrequests);
438 /* subreq is now totally disconnected from page group or any
439 * write / commit lists. last chance to wake any waiters */
440 nfs_unlock_and_release_request(subreq);
445 * nfs_lock_and_join_requests - join all subreqs to the head req and return
446 * a locked reference, cancelling any pending
447 * operations for this page.
449 * @page - the page used to lookup the "page group" of nfs_page structures
451 * This function joins all sub requests to the head request by first
452 * locking all requests in the group, cancelling any pending operations
453 * and finally updating the head request to cover the whole range covered by
454 * the (former) group. All subrequests are removed from any write or commit
455 * lists, unlinked from the group and destroyed.
457 * Returns a locked, referenced pointer to the head request - which after
458 * this call is guaranteed to be the only request associated with the page.
459 * Returns NULL if no requests are found for @page, or a ERR_PTR if an
460 * error was encountered.
462 static struct nfs_page *
463 nfs_lock_and_join_requests(struct page *page)
465 struct inode *inode = page_file_mapping(page)->host;
466 struct nfs_page *head, *subreq;
467 struct nfs_page *destroy_list = NULL;
468 unsigned int total_bytes;
469 int ret;
471 try_again:
473 * A reference is taken only on the head request which acts as a
474 * reference to the whole page group - the group will not be destroyed
475 * until the head reference is released.
477 head = nfs_page_find_head_request(page);
478 if (!head)
479 return NULL;
481 /* lock the page head first in order to avoid an ABBA inefficiency */
482 if (!nfs_lock_request(head)) {
483 ret = nfs_wait_on_request(head);
484 nfs_release_request(head);
485 if (ret < 0)
486 return ERR_PTR(ret);
487 goto try_again;
490 /* Ensure that nobody removed the request before we locked it */
491 if (head != nfs_page_private_request(page) && !PageSwapCache(page)) {
492 nfs_unlock_and_release_request(head);
493 goto try_again;
496 ret = nfs_page_group_lock(head);
497 if (ret < 0) {
498 nfs_unlock_and_release_request(head);
499 return ERR_PTR(ret);
502 /* lock each request in the page group */
503 total_bytes = head->wb_bytes;
504 for (subreq = head->wb_this_page; subreq != head;
505 subreq = subreq->wb_this_page) {
507 if (!kref_get_unless_zero(&subreq->wb_kref)) {
508 if (subreq->wb_offset == head->wb_offset + total_bytes)
509 total_bytes += subreq->wb_bytes;
510 continue;
513 while (!nfs_lock_request(subreq)) {
515 * Unlock page to allow nfs_page_group_sync_on_bit()
516 * to succeed
518 nfs_page_group_unlock(head);
519 ret = nfs_wait_on_request(subreq);
520 if (!ret)
521 ret = nfs_page_group_lock(head);
522 if (ret < 0) {
523 nfs_unroll_locks(inode, head, subreq);
524 nfs_release_request(subreq);
525 nfs_unlock_and_release_request(head);
526 return ERR_PTR(ret);
530 * Subrequests are always contiguous, non overlapping
531 * and in order - but may be repeated (mirrored writes).
533 if (subreq->wb_offset == (head->wb_offset + total_bytes)) {
534 /* keep track of how many bytes this group covers */
535 total_bytes += subreq->wb_bytes;
536 } else if (WARN_ON_ONCE(subreq->wb_offset < head->wb_offset ||
537 ((subreq->wb_offset + subreq->wb_bytes) >
538 (head->wb_offset + total_bytes)))) {
539 nfs_page_group_unlock(head);
540 nfs_unroll_locks(inode, head, subreq);
541 nfs_unlock_and_release_request(subreq);
542 nfs_unlock_and_release_request(head);
543 return ERR_PTR(-EIO);
547 /* Now that all requests are locked, make sure they aren't on any list.
548 * Commit list removal accounting is done after locks are dropped */
549 subreq = head;
550 do {
551 nfs_clear_request_commit(subreq);
552 subreq = subreq->wb_this_page;
553 } while (subreq != head);
555 /* unlink subrequests from head, destroy them later */
556 if (head->wb_this_page != head) {
557 /* destroy list will be terminated by head */
558 destroy_list = head->wb_this_page;
559 head->wb_this_page = head;
561 /* change head request to cover whole range that
562 * the former page group covered */
563 head->wb_bytes = total_bytes;
566 /* Postpone destruction of this request */
567 if (test_and_clear_bit(PG_REMOVE, &head->wb_flags)) {
568 set_bit(PG_INODE_REF, &head->wb_flags);
569 kref_get(&head->wb_kref);
570 atomic_long_inc(&NFS_I(inode)->nrequests);
573 nfs_page_group_unlock(head);
575 nfs_destroy_unlinked_subrequests(destroy_list, head, inode);
577 /* Did we lose a race with nfs_inode_remove_request()? */
578 if (!(PagePrivate(page) || PageSwapCache(page))) {
579 nfs_unlock_and_release_request(head);
580 return NULL;
583 /* still holds ref on head from nfs_page_find_head_request
584 * and still has lock on head from lock loop */
585 return head;
588 static void nfs_write_error_remove_page(struct nfs_page *req)
590 nfs_end_page_writeback(req);
591 generic_error_remove_page(page_file_mapping(req->wb_page),
592 req->wb_page);
593 nfs_release_request(req);
596 static bool
597 nfs_error_is_fatal_on_server(int err)
599 switch (err) {
600 case 0:
601 case -ERESTARTSYS:
602 case -EINTR:
603 return false;
605 return nfs_error_is_fatal(err);
609 * Find an associated nfs write request, and prepare to flush it out
610 * May return an error if the user signalled nfs_wait_on_request().
612 static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
613 struct page *page)
615 struct nfs_page *req;
616 int ret = 0;
618 req = nfs_lock_and_join_requests(page);
619 if (!req)
620 goto out;
621 ret = PTR_ERR(req);
622 if (IS_ERR(req))
623 goto out;
625 nfs_set_page_writeback(page);
626 WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
628 ret = req->wb_context->error;
629 /* If there is a fatal error that covers this write, just exit */
630 if (nfs_error_is_fatal_on_server(ret))
631 goto out_launder;
633 ret = 0;
634 if (!nfs_pageio_add_request(pgio, req)) {
635 ret = pgio->pg_error;
637 * Remove the problematic req upon fatal errors on the server
639 if (nfs_error_is_fatal(ret)) {
640 nfs_context_set_write_error(req->wb_context, ret);
641 if (nfs_error_is_fatal_on_server(ret))
642 goto out_launder;
643 } else
644 ret = -EAGAIN;
645 nfs_redirty_request(req);
646 } else
647 nfs_add_stats(page_file_mapping(page)->host,
648 NFSIOS_WRITEPAGES, 1);
649 out:
650 return ret;
651 out_launder:
652 nfs_write_error_remove_page(req);
653 return 0;
656 static int nfs_do_writepage(struct page *page, struct writeback_control *wbc,
657 struct nfs_pageio_descriptor *pgio)
659 int ret;
661 nfs_pageio_cond_complete(pgio, page_index(page));
662 ret = nfs_page_async_flush(pgio, page);
663 if (ret == -EAGAIN) {
664 redirty_page_for_writepage(wbc, page);
665 ret = 0;
667 return ret;
671 * Write an mmapped page to the server.
673 static int nfs_writepage_locked(struct page *page,
674 struct writeback_control *wbc)
676 struct nfs_pageio_descriptor pgio;
677 struct inode *inode = page_file_mapping(page)->host;
678 int err;
680 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
681 nfs_pageio_init_write(&pgio, inode, 0,
682 false, &nfs_async_write_completion_ops);
683 err = nfs_do_writepage(page, wbc, &pgio);
684 nfs_pageio_complete(&pgio);
685 if (err < 0)
686 return err;
687 if (pgio.pg_error < 0)
688 return pgio.pg_error;
689 return 0;
692 int nfs_writepage(struct page *page, struct writeback_control *wbc)
694 int ret;
696 ret = nfs_writepage_locked(page, wbc);
697 unlock_page(page);
698 return ret;
701 static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
703 int ret;
705 ret = nfs_do_writepage(page, wbc, data);
706 unlock_page(page);
707 return ret;
710 static void nfs_io_completion_commit(void *inode)
712 nfs_commit_inode(inode, 0);
715 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
717 struct inode *inode = mapping->host;
718 struct nfs_pageio_descriptor pgio;
719 struct nfs_io_completion *ioc = nfs_io_completion_alloc(GFP_NOFS);
720 int err;
722 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
724 if (ioc)
725 nfs_io_completion_init(ioc, nfs_io_completion_commit, inode);
727 nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false,
728 &nfs_async_write_completion_ops);
729 pgio.pg_io_completion = ioc;
730 err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
731 nfs_pageio_complete(&pgio);
732 nfs_io_completion_put(ioc);
734 if (err < 0)
735 goto out_err;
736 err = pgio.pg_error;
737 if (err < 0)
738 goto out_err;
739 return 0;
740 out_err:
741 return err;
745 * Insert a write request into an inode
747 static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
749 struct address_space *mapping = page_file_mapping(req->wb_page);
750 struct nfs_inode *nfsi = NFS_I(inode);
752 WARN_ON_ONCE(req->wb_this_page != req);
754 /* Lock the request! */
755 nfs_lock_request(req);
758 * Swap-space should not get truncated. Hence no need to plug the race
759 * with invalidate/truncate.
761 spin_lock(&mapping->private_lock);
762 if (!nfs_have_writebacks(inode) &&
763 NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE)) {
764 spin_lock(&inode->i_lock);
765 inode->i_version++;
766 spin_unlock(&inode->i_lock);
768 if (likely(!PageSwapCache(req->wb_page))) {
769 set_bit(PG_MAPPED, &req->wb_flags);
770 SetPagePrivate(req->wb_page);
771 set_page_private(req->wb_page, (unsigned long)req);
773 spin_unlock(&mapping->private_lock);
774 atomic_long_inc(&nfsi->nrequests);
775 /* this a head request for a page group - mark it as having an
776 * extra reference so sub groups can follow suit.
777 * This flag also informs pgio layer when to bump nrequests when
778 * adding subrequests. */
779 WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags));
780 kref_get(&req->wb_kref);
784 * Remove a write request from an inode
786 static void nfs_inode_remove_request(struct nfs_page *req)
788 struct address_space *mapping = page_file_mapping(req->wb_page);
789 struct inode *inode = mapping->host;
790 struct nfs_inode *nfsi = NFS_I(inode);
791 struct nfs_page *head;
793 if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) {
794 head = req->wb_head;
796 spin_lock(&mapping->private_lock);
797 if (likely(head->wb_page && !PageSwapCache(head->wb_page))) {
798 set_page_private(head->wb_page, 0);
799 ClearPagePrivate(head->wb_page);
800 clear_bit(PG_MAPPED, &head->wb_flags);
802 spin_unlock(&mapping->private_lock);
805 if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags)) {
806 nfs_release_request(req);
807 atomic_long_dec(&nfsi->nrequests);
811 static void
812 nfs_mark_request_dirty(struct nfs_page *req)
814 if (req->wb_page)
815 __set_page_dirty_nobuffers(req->wb_page);
819 * nfs_page_search_commits_for_head_request_locked
821 * Search through commit lists on @inode for the head request for @page.
822 * Must be called while holding the inode (which is cinfo) lock.
824 * Returns the head request if found, or NULL if not found.
826 static struct nfs_page *
827 nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
828 struct page *page)
830 struct nfs_page *freq, *t;
831 struct nfs_commit_info cinfo;
832 struct inode *inode = &nfsi->vfs_inode;
834 nfs_init_cinfo_from_inode(&cinfo, inode);
836 /* search through pnfs commit lists */
837 freq = pnfs_search_commit_reqs(inode, &cinfo, page);
838 if (freq)
839 return freq->wb_head;
841 /* Linearly search the commit list for the correct request */
842 list_for_each_entry_safe(freq, t, &cinfo.mds->list, wb_list) {
843 if (freq->wb_page == page)
844 return freq->wb_head;
847 return NULL;
851 * nfs_request_add_commit_list_locked - add request to a commit list
852 * @req: pointer to a struct nfs_page
853 * @dst: commit list head
854 * @cinfo: holds list lock and accounting info
856 * This sets the PG_CLEAN bit, updates the cinfo count of
857 * number of outstanding requests requiring a commit as well as
858 * the MM page stats.
860 * The caller must hold NFS_I(cinfo->inode)->commit_mutex, and the
861 * nfs_page lock.
863 void
864 nfs_request_add_commit_list_locked(struct nfs_page *req, struct list_head *dst,
865 struct nfs_commit_info *cinfo)
867 set_bit(PG_CLEAN, &req->wb_flags);
868 nfs_list_add_request(req, dst);
869 atomic_long_inc(&cinfo->mds->ncommit);
871 EXPORT_SYMBOL_GPL(nfs_request_add_commit_list_locked);
874 * nfs_request_add_commit_list - add request to a commit list
875 * @req: pointer to a struct nfs_page
876 * @dst: commit list head
877 * @cinfo: holds list lock and accounting info
879 * This sets the PG_CLEAN bit, updates the cinfo count of
880 * number of outstanding requests requiring a commit as well as
881 * the MM page stats.
883 * The caller must _not_ hold the cinfo->lock, but must be
884 * holding the nfs_page lock.
886 void
887 nfs_request_add_commit_list(struct nfs_page *req, struct nfs_commit_info *cinfo)
889 mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
890 nfs_request_add_commit_list_locked(req, &cinfo->mds->list, cinfo);
891 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
892 if (req->wb_page)
893 nfs_mark_page_unstable(req->wb_page, cinfo);
895 EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
898 * nfs_request_remove_commit_list - Remove request from a commit list
899 * @req: pointer to a nfs_page
900 * @cinfo: holds list lock and accounting info
902 * This clears the PG_CLEAN bit, and updates the cinfo's count of
903 * number of outstanding requests requiring a commit
904 * It does not update the MM page stats.
906 * The caller _must_ hold the cinfo->lock and the nfs_page lock.
908 void
909 nfs_request_remove_commit_list(struct nfs_page *req,
910 struct nfs_commit_info *cinfo)
912 if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
913 return;
914 nfs_list_remove_request(req);
915 atomic_long_dec(&cinfo->mds->ncommit);
917 EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
919 static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
920 struct inode *inode)
922 cinfo->inode = inode;
923 cinfo->mds = &NFS_I(inode)->commit_info;
924 cinfo->ds = pnfs_get_ds_info(inode);
925 cinfo->dreq = NULL;
926 cinfo->completion_ops = &nfs_commit_completion_ops;
929 void nfs_init_cinfo(struct nfs_commit_info *cinfo,
930 struct inode *inode,
931 struct nfs_direct_req *dreq)
933 if (dreq)
934 nfs_init_cinfo_from_dreq(cinfo, dreq);
935 else
936 nfs_init_cinfo_from_inode(cinfo, inode);
938 EXPORT_SYMBOL_GPL(nfs_init_cinfo);
941 * Add a request to the inode's commit list.
943 void
944 nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
945 struct nfs_commit_info *cinfo, u32 ds_commit_idx)
947 if (pnfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx))
948 return;
949 nfs_request_add_commit_list(req, cinfo);
952 static void
953 nfs_clear_page_commit(struct page *page)
955 dec_node_page_state(page, NR_UNSTABLE_NFS);
956 dec_wb_stat(&inode_to_bdi(page_file_mapping(page)->host)->wb,
957 WB_RECLAIMABLE);
960 /* Called holding the request lock on @req */
961 static void
962 nfs_clear_request_commit(struct nfs_page *req)
964 if (test_bit(PG_CLEAN, &req->wb_flags)) {
965 struct inode *inode = d_inode(req->wb_context->dentry);
966 struct nfs_commit_info cinfo;
968 nfs_init_cinfo_from_inode(&cinfo, inode);
969 mutex_lock(&NFS_I(inode)->commit_mutex);
970 if (!pnfs_clear_request_commit(req, &cinfo)) {
971 nfs_request_remove_commit_list(req, &cinfo);
973 mutex_unlock(&NFS_I(inode)->commit_mutex);
974 nfs_clear_page_commit(req->wb_page);
978 int nfs_write_need_commit(struct nfs_pgio_header *hdr)
980 if (hdr->verf.committed == NFS_DATA_SYNC)
981 return hdr->lseg == NULL;
982 return hdr->verf.committed != NFS_FILE_SYNC;
985 static void nfs_async_write_init(struct nfs_pgio_header *hdr)
987 nfs_io_completion_get(hdr->io_completion);
990 static void nfs_write_completion(struct nfs_pgio_header *hdr)
992 struct nfs_commit_info cinfo;
993 unsigned long bytes = 0;
995 if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
996 goto out;
997 nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
998 while (!list_empty(&hdr->pages)) {
999 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
1001 bytes += req->wb_bytes;
1002 nfs_list_remove_request(req);
1003 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
1004 (hdr->good_bytes < bytes)) {
1005 nfs_set_pageerror(page_file_mapping(req->wb_page));
1006 nfs_context_set_write_error(req->wb_context, hdr->error);
1007 goto remove_req;
1009 if (nfs_write_need_commit(hdr)) {
1010 memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf));
1011 nfs_mark_request_commit(req, hdr->lseg, &cinfo,
1012 hdr->pgio_mirror_idx);
1013 goto next;
1015 remove_req:
1016 nfs_inode_remove_request(req);
1017 next:
1018 nfs_end_page_writeback(req);
1019 nfs_release_request(req);
1021 out:
1022 nfs_io_completion_put(hdr->io_completion);
1023 hdr->release(hdr);
1026 unsigned long
1027 nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
1029 return atomic_long_read(&cinfo->mds->ncommit);
1032 /* NFS_I(cinfo->inode)->commit_mutex held by caller */
1034 nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
1035 struct nfs_commit_info *cinfo, int max)
1037 struct nfs_page *req, *tmp;
1038 int ret = 0;
1040 restart:
1041 list_for_each_entry_safe(req, tmp, src, wb_list) {
1042 kref_get(&req->wb_kref);
1043 if (!nfs_lock_request(req)) {
1044 int status;
1046 /* Prevent deadlock with nfs_lock_and_join_requests */
1047 if (!list_empty(dst)) {
1048 nfs_release_request(req);
1049 continue;
1051 /* Ensure we make progress to prevent livelock */
1052 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
1053 status = nfs_wait_on_request(req);
1054 nfs_release_request(req);
1055 mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
1056 if (status < 0)
1057 break;
1058 goto restart;
1060 nfs_request_remove_commit_list(req, cinfo);
1061 clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
1062 nfs_list_add_request(req, dst);
1063 ret++;
1064 if ((ret == max) && !cinfo->dreq)
1065 break;
1066 cond_resched();
1068 return ret;
1070 EXPORT_SYMBOL_GPL(nfs_scan_commit_list);
1073 * nfs_scan_commit - Scan an inode for commit requests
1074 * @inode: NFS inode to scan
1075 * @dst: mds destination list
1076 * @cinfo: mds and ds lists of reqs ready to commit
1078 * Moves requests from the inode's 'commit' request list.
1079 * The requests are *not* checked to ensure that they form a contiguous set.
1082 nfs_scan_commit(struct inode *inode, struct list_head *dst,
1083 struct nfs_commit_info *cinfo)
1085 int ret = 0;
1087 if (!atomic_long_read(&cinfo->mds->ncommit))
1088 return 0;
1089 mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
1090 if (atomic_long_read(&cinfo->mds->ncommit) > 0) {
1091 const int max = INT_MAX;
1093 ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
1094 cinfo, max);
1095 ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
1097 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
1098 return ret;
1102 * Search for an existing write request, and attempt to update
1103 * it to reflect a new dirty region on a given page.
1105 * If the attempt fails, then the existing request is flushed out
1106 * to disk.
1108 static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
1109 struct page *page,
1110 unsigned int offset,
1111 unsigned int bytes)
1113 struct nfs_page *req;
1114 unsigned int rqend;
1115 unsigned int end;
1116 int error;
1118 end = offset + bytes;
1120 req = nfs_lock_and_join_requests(page);
1121 if (IS_ERR_OR_NULL(req))
1122 return req;
1124 rqend = req->wb_offset + req->wb_bytes;
1126 * Tell the caller to flush out the request if
1127 * the offsets are non-contiguous.
1128 * Note: nfs_flush_incompatible() will already
1129 * have flushed out requests having wrong owners.
1131 if (offset > rqend || end < req->wb_offset)
1132 goto out_flushme;
1134 /* Okay, the request matches. Update the region */
1135 if (offset < req->wb_offset) {
1136 req->wb_offset = offset;
1137 req->wb_pgbase = offset;
1139 if (end > rqend)
1140 req->wb_bytes = end - req->wb_offset;
1141 else
1142 req->wb_bytes = rqend - req->wb_offset;
1143 return req;
1144 out_flushme:
1146 * Note: we mark the request dirty here because
1147 * nfs_lock_and_join_requests() cannot preserve
1148 * commit flags, so we have to replay the write.
1150 nfs_mark_request_dirty(req);
1151 nfs_unlock_and_release_request(req);
1152 error = nfs_wb_page(inode, page);
1153 return (error < 0) ? ERR_PTR(error) : NULL;
1157 * Try to update an existing write request, or create one if there is none.
1159 * Note: Should always be called with the Page Lock held to prevent races
1160 * if we have to add a new request. Also assumes that the caller has
1161 * already called nfs_flush_incompatible() if necessary.
1163 static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
1164 struct page *page, unsigned int offset, unsigned int bytes)
1166 struct inode *inode = page_file_mapping(page)->host;
1167 struct nfs_page *req;
1169 req = nfs_try_to_update_request(inode, page, offset, bytes);
1170 if (req != NULL)
1171 goto out;
1172 req = nfs_create_request(ctx, page, NULL, offset, bytes);
1173 if (IS_ERR(req))
1174 goto out;
1175 nfs_inode_add_request(inode, req);
1176 out:
1177 return req;
1180 static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
1181 unsigned int offset, unsigned int count)
1183 struct nfs_page *req;
1185 req = nfs_setup_write_request(ctx, page, offset, count);
1186 if (IS_ERR(req))
1187 return PTR_ERR(req);
1188 /* Update file length */
1189 nfs_grow_file(page, offset, count);
1190 nfs_mark_uptodate(req);
1191 nfs_mark_request_dirty(req);
1192 nfs_unlock_and_release_request(req);
1193 return 0;
1196 int nfs_flush_incompatible(struct file *file, struct page *page)
1198 struct nfs_open_context *ctx = nfs_file_open_context(file);
1199 struct nfs_lock_context *l_ctx;
1200 struct file_lock_context *flctx = file_inode(file)->i_flctx;
1201 struct nfs_page *req;
1202 int do_flush, status;
1204 * Look for a request corresponding to this page. If there
1205 * is one, and it belongs to another file, we flush it out
1206 * before we try to copy anything into the page. Do this
1207 * due to the lack of an ACCESS-type call in NFSv2.
1208 * Also do the same if we find a request from an existing
1209 * dropped page.
1211 do {
1212 req = nfs_page_find_head_request(page);
1213 if (req == NULL)
1214 return 0;
1215 l_ctx = req->wb_lock_context;
1216 do_flush = req->wb_page != page ||
1217 !nfs_match_open_context(req->wb_context, ctx);
1218 if (l_ctx && flctx &&
1219 !(list_empty_careful(&flctx->flc_posix) &&
1220 list_empty_careful(&flctx->flc_flock))) {
1221 do_flush |= l_ctx->lockowner != current->files;
1223 nfs_release_request(req);
1224 if (!do_flush)
1225 return 0;
1226 status = nfs_wb_page(page_file_mapping(page)->host, page);
1227 } while (status == 0);
1228 return status;
1232 * Avoid buffered writes when a open context credential's key would
1233 * expire soon.
1235 * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
1237 * Return 0 and set a credential flag which triggers the inode to flush
1238 * and performs NFS_FILE_SYNC writes if the key will expired within
1239 * RPC_KEY_EXPIRE_TIMEO.
1242 nfs_key_timeout_notify(struct file *filp, struct inode *inode)
1244 struct nfs_open_context *ctx = nfs_file_open_context(filp);
1245 struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
1247 return rpcauth_key_timeout_notify(auth, ctx->cred);
1251 * Test if the open context credential key is marked to expire soon.
1253 bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx, struct inode *inode)
1255 struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
1257 return rpcauth_cred_key_to_expire(auth, ctx->cred);
1261 * If the page cache is marked as unsafe or invalid, then we can't rely on
1262 * the PageUptodate() flag. In this case, we will need to turn off
1263 * write optimisations that depend on the page contents being correct.
1265 static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
1267 struct nfs_inode *nfsi = NFS_I(inode);
1269 if (nfs_have_delegated_attributes(inode))
1270 goto out;
1271 if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
1272 return false;
1273 smp_rmb();
1274 if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags))
1275 return false;
1276 out:
1277 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
1278 return false;
1279 return PageUptodate(page) != 0;
1282 static bool
1283 is_whole_file_wrlock(struct file_lock *fl)
1285 return fl->fl_start == 0 && fl->fl_end == OFFSET_MAX &&
1286 fl->fl_type == F_WRLCK;
1289 /* If we know the page is up to date, and we're not using byte range locks (or
1290 * if we have the whole file locked for writing), it may be more efficient to
1291 * extend the write to cover the entire page in order to avoid fragmentation
1292 * inefficiencies.
1294 * If the file is opened for synchronous writes then we can just skip the rest
1295 * of the checks.
1297 static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode)
1299 int ret;
1300 struct file_lock_context *flctx = inode->i_flctx;
1301 struct file_lock *fl;
1303 if (file->f_flags & O_DSYNC)
1304 return 0;
1305 if (!nfs_write_pageuptodate(page, inode))
1306 return 0;
1307 if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
1308 return 1;
1309 if (!flctx || (list_empty_careful(&flctx->flc_flock) &&
1310 list_empty_careful(&flctx->flc_posix)))
1311 return 1;
1313 /* Check to see if there are whole file write locks */
1314 ret = 0;
1315 spin_lock(&flctx->flc_lock);
1316 if (!list_empty(&flctx->flc_posix)) {
1317 fl = list_first_entry(&flctx->flc_posix, struct file_lock,
1318 fl_list);
1319 if (is_whole_file_wrlock(fl))
1320 ret = 1;
1321 } else if (!list_empty(&flctx->flc_flock)) {
1322 fl = list_first_entry(&flctx->flc_flock, struct file_lock,
1323 fl_list);
1324 if (fl->fl_type == F_WRLCK)
1325 ret = 1;
1327 spin_unlock(&flctx->flc_lock);
1328 return ret;
1332 * Update and possibly write a cached page of an NFS file.
1334 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
1335 * things with a page scheduled for an RPC call (e.g. invalidate it).
1337 int nfs_updatepage(struct file *file, struct page *page,
1338 unsigned int offset, unsigned int count)
1340 struct nfs_open_context *ctx = nfs_file_open_context(file);
1341 struct address_space *mapping = page_file_mapping(page);
1342 struct inode *inode = mapping->host;
1343 int status = 0;
1345 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
1347 dprintk("NFS: nfs_updatepage(%pD2 %d@%lld)\n",
1348 file, count, (long long)(page_file_offset(page) + offset));
1350 if (!count)
1351 goto out;
1353 if (nfs_can_extend_write(file, page, inode)) {
1354 count = max(count + offset, nfs_page_length(page));
1355 offset = 0;
1358 status = nfs_writepage_setup(ctx, page, offset, count);
1359 if (status < 0)
1360 nfs_set_pageerror(mapping);
1361 else
1362 __set_page_dirty_nobuffers(page);
1363 out:
1364 dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
1365 status, (long long)i_size_read(inode));
1366 return status;
1369 static int flush_task_priority(int how)
1371 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
1372 case FLUSH_HIGHPRI:
1373 return RPC_PRIORITY_HIGH;
1374 case FLUSH_LOWPRI:
1375 return RPC_PRIORITY_LOW;
1377 return RPC_PRIORITY_NORMAL;
1380 static void nfs_initiate_write(struct nfs_pgio_header *hdr,
1381 struct rpc_message *msg,
1382 const struct nfs_rpc_ops *rpc_ops,
1383 struct rpc_task_setup *task_setup_data, int how)
1385 int priority = flush_task_priority(how);
1387 task_setup_data->priority = priority;
1388 rpc_ops->write_setup(hdr, msg);
1389 trace_nfs_initiate_write(hdr->inode, hdr->io_start, hdr->good_bytes,
1390 hdr->args.stable);
1392 nfs4_state_protect_write(NFS_SERVER(hdr->inode)->nfs_client,
1393 &task_setup_data->rpc_client, msg, hdr);
1396 /* If a nfs_flush_* function fails, it should remove reqs from @head and
1397 * call this on each, which will prepare them to be retried on next
1398 * writeback using standard nfs.
1400 static void nfs_redirty_request(struct nfs_page *req)
1402 nfs_mark_request_dirty(req);
1403 set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
1404 nfs_end_page_writeback(req);
1405 nfs_release_request(req);
1408 static void nfs_async_write_error(struct list_head *head, int error)
1410 struct nfs_page *req;
1412 while (!list_empty(head)) {
1413 req = nfs_list_entry(head->next);
1414 nfs_list_remove_request(req);
1415 if (nfs_error_is_fatal(error)) {
1416 nfs_context_set_write_error(req->wb_context, error);
1417 if (nfs_error_is_fatal_on_server(error)) {
1418 nfs_write_error_remove_page(req);
1419 continue;
1422 nfs_redirty_request(req);
1426 static void nfs_async_write_reschedule_io(struct nfs_pgio_header *hdr)
1428 nfs_async_write_error(&hdr->pages, 0);
1431 static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1432 .init_hdr = nfs_async_write_init,
1433 .error_cleanup = nfs_async_write_error,
1434 .completion = nfs_write_completion,
1435 .reschedule_io = nfs_async_write_reschedule_io,
1438 void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1439 struct inode *inode, int ioflags, bool force_mds,
1440 const struct nfs_pgio_completion_ops *compl_ops)
1442 struct nfs_server *server = NFS_SERVER(inode);
1443 const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
1445 #ifdef CONFIG_NFS_V4_1
1446 if (server->pnfs_curr_ld && !force_mds)
1447 pg_ops = server->pnfs_curr_ld->pg_write_ops;
1448 #endif
1449 nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops,
1450 server->wsize, ioflags);
1452 EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
1454 void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1456 struct nfs_pgio_mirror *mirror;
1458 if (pgio->pg_ops && pgio->pg_ops->pg_cleanup)
1459 pgio->pg_ops->pg_cleanup(pgio);
1461 pgio->pg_ops = &nfs_pgio_rw_ops;
1463 nfs_pageio_stop_mirroring(pgio);
1465 mirror = &pgio->pg_mirrors[0];
1466 mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1468 EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1471 void nfs_commit_prepare(struct rpc_task *task, void *calldata)
1473 struct nfs_commit_data *data = calldata;
1475 NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
1479 * Special version of should_remove_suid() that ignores capabilities.
1481 static int nfs_should_remove_suid(const struct inode *inode)
1483 umode_t mode = inode->i_mode;
1484 int kill = 0;
1486 /* suid always must be killed */
1487 if (unlikely(mode & S_ISUID))
1488 kill = ATTR_KILL_SUID;
1491 * sgid without any exec bits is just a mandatory locking mark; leave
1492 * it alone. If some exec bits are set, it's a real sgid; kill it.
1494 if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
1495 kill |= ATTR_KILL_SGID;
1497 if (unlikely(kill && S_ISREG(mode)))
1498 return kill;
1500 return 0;
1503 static void nfs_writeback_check_extend(struct nfs_pgio_header *hdr,
1504 struct nfs_fattr *fattr)
1506 struct nfs_pgio_args *argp = &hdr->args;
1507 struct nfs_pgio_res *resp = &hdr->res;
1508 u64 size = argp->offset + resp->count;
1510 if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
1511 fattr->size = size;
1512 if (nfs_size_to_loff_t(fattr->size) < i_size_read(hdr->inode)) {
1513 fattr->valid &= ~NFS_ATTR_FATTR_SIZE;
1514 return;
1516 if (size != fattr->size)
1517 return;
1518 /* Set attribute barrier */
1519 nfs_fattr_set_barrier(fattr);
1520 /* ...and update size */
1521 fattr->valid |= NFS_ATTR_FATTR_SIZE;
1524 void nfs_writeback_update_inode(struct nfs_pgio_header *hdr)
1526 struct nfs_fattr *fattr = &hdr->fattr;
1527 struct inode *inode = hdr->inode;
1529 spin_lock(&inode->i_lock);
1530 nfs_writeback_check_extend(hdr, fattr);
1531 nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
1532 spin_unlock(&inode->i_lock);
1534 EXPORT_SYMBOL_GPL(nfs_writeback_update_inode);
1537 * This function is called when the WRITE call is complete.
1539 static int nfs_writeback_done(struct rpc_task *task,
1540 struct nfs_pgio_header *hdr,
1541 struct inode *inode)
1543 int status;
1546 * ->write_done will attempt to use post-op attributes to detect
1547 * conflicting writes by other clients. A strict interpretation
1548 * of close-to-open would allow us to continue caching even if
1549 * another writer had changed the file, but some applications
1550 * depend on tighter cache coherency when writing.
1552 status = NFS_PROTO(inode)->write_done(task, hdr);
1553 if (status != 0)
1554 return status;
1556 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count);
1557 trace_nfs_writeback_done(inode, task->tk_status,
1558 hdr->args.offset, hdr->res.verf);
1560 if (hdr->res.verf->committed < hdr->args.stable &&
1561 task->tk_status >= 0) {
1562 /* We tried a write call, but the server did not
1563 * commit data to stable storage even though we
1564 * requested it.
1565 * Note: There is a known bug in Tru64 < 5.0 in which
1566 * the server reports NFS_DATA_SYNC, but performs
1567 * NFS_FILE_SYNC. We therefore implement this checking
1568 * as a dprintk() in order to avoid filling syslog.
1570 static unsigned long complain;
1572 /* Note this will print the MDS for a DS write */
1573 if (time_before(complain, jiffies)) {
1574 dprintk("NFS: faulty NFS server %s:"
1575 " (committed = %d) != (stable = %d)\n",
1576 NFS_SERVER(inode)->nfs_client->cl_hostname,
1577 hdr->res.verf->committed, hdr->args.stable);
1578 complain = jiffies + 300 * HZ;
1582 /* Deal with the suid/sgid bit corner case */
1583 if (nfs_should_remove_suid(inode))
1584 nfs_mark_for_revalidate(inode);
1585 return 0;
1589 * This function is called when the WRITE call is complete.
1591 static void nfs_writeback_result(struct rpc_task *task,
1592 struct nfs_pgio_header *hdr)
1594 struct nfs_pgio_args *argp = &hdr->args;
1595 struct nfs_pgio_res *resp = &hdr->res;
1597 if (resp->count < argp->count) {
1598 static unsigned long complain;
1600 /* This a short write! */
1601 nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE);
1603 /* Has the server at least made some progress? */
1604 if (resp->count == 0) {
1605 if (time_before(complain, jiffies)) {
1606 printk(KERN_WARNING
1607 "NFS: Server wrote zero bytes, expected %u.\n",
1608 argp->count);
1609 complain = jiffies + 300 * HZ;
1611 nfs_set_pgio_error(hdr, -EIO, argp->offset);
1612 task->tk_status = -EIO;
1613 return;
1616 /* For non rpc-based layout drivers, retry-through-MDS */
1617 if (!task->tk_ops) {
1618 hdr->pnfs_error = -EAGAIN;
1619 return;
1622 /* Was this an NFSv2 write or an NFSv3 stable write? */
1623 if (resp->verf->committed != NFS_UNSTABLE) {
1624 /* Resend from where the server left off */
1625 hdr->mds_offset += resp->count;
1626 argp->offset += resp->count;
1627 argp->pgbase += resp->count;
1628 argp->count -= resp->count;
1629 } else {
1630 /* Resend as a stable write in order to avoid
1631 * headaches in the case of a server crash.
1633 argp->stable = NFS_FILE_SYNC;
1635 rpc_restart_call_prepare(task);
1639 static int wait_on_commit(struct nfs_mds_commit_info *cinfo)
1641 return wait_on_atomic_t(&cinfo->rpcs_out,
1642 nfs_wait_atomic_killable, TASK_KILLABLE);
1645 static void nfs_commit_begin(struct nfs_mds_commit_info *cinfo)
1647 atomic_inc(&cinfo->rpcs_out);
1650 static void nfs_commit_end(struct nfs_mds_commit_info *cinfo)
1652 if (atomic_dec_and_test(&cinfo->rpcs_out))
1653 wake_up_atomic_t(&cinfo->rpcs_out);
1656 void nfs_commitdata_release(struct nfs_commit_data *data)
1658 put_nfs_open_context(data->context);
1659 nfs_commit_free(data);
1661 EXPORT_SYMBOL_GPL(nfs_commitdata_release);
1663 int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
1664 const struct nfs_rpc_ops *nfs_ops,
1665 const struct rpc_call_ops *call_ops,
1666 int how, int flags)
1668 struct rpc_task *task;
1669 int priority = flush_task_priority(how);
1670 struct rpc_message msg = {
1671 .rpc_argp = &data->args,
1672 .rpc_resp = &data->res,
1673 .rpc_cred = data->cred,
1675 struct rpc_task_setup task_setup_data = {
1676 .task = &data->task,
1677 .rpc_client = clnt,
1678 .rpc_message = &msg,
1679 .callback_ops = call_ops,
1680 .callback_data = data,
1681 .workqueue = nfsiod_workqueue,
1682 .flags = RPC_TASK_ASYNC | flags,
1683 .priority = priority,
1685 /* Set up the initial task struct. */
1686 nfs_ops->commit_setup(data, &msg);
1687 trace_nfs_initiate_commit(data);
1689 dprintk("NFS: initiated commit call\n");
1691 nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client,
1692 NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg);
1694 task = rpc_run_task(&task_setup_data);
1695 if (IS_ERR(task))
1696 return PTR_ERR(task);
1697 if (how & FLUSH_SYNC)
1698 rpc_wait_for_completion_task(task);
1699 rpc_put_task(task);
1700 return 0;
1702 EXPORT_SYMBOL_GPL(nfs_initiate_commit);
1704 static loff_t nfs_get_lwb(struct list_head *head)
1706 loff_t lwb = 0;
1707 struct nfs_page *req;
1709 list_for_each_entry(req, head, wb_list)
1710 if (lwb < (req_offset(req) + req->wb_bytes))
1711 lwb = req_offset(req) + req->wb_bytes;
1713 return lwb;
1717 * Set up the argument/result storage required for the RPC call.
1719 void nfs_init_commit(struct nfs_commit_data *data,
1720 struct list_head *head,
1721 struct pnfs_layout_segment *lseg,
1722 struct nfs_commit_info *cinfo)
1724 struct nfs_page *first = nfs_list_entry(head->next);
1725 struct inode *inode = d_inode(first->wb_context->dentry);
1727 /* Set up the RPC argument and reply structs
1728 * NB: take care not to mess about with data->commit et al. */
1730 list_splice_init(head, &data->pages);
1732 data->inode = inode;
1733 data->cred = first->wb_context->cred;
1734 data->lseg = lseg; /* reference transferred */
1735 /* only set lwb for pnfs commit */
1736 if (lseg)
1737 data->lwb = nfs_get_lwb(&data->pages);
1738 data->mds_ops = &nfs_commit_ops;
1739 data->completion_ops = cinfo->completion_ops;
1740 data->dreq = cinfo->dreq;
1742 data->args.fh = NFS_FH(data->inode);
1743 /* Note: we always request a commit of the entire inode */
1744 data->args.offset = 0;
1745 data->args.count = 0;
1746 data->context = get_nfs_open_context(first->wb_context);
1747 data->res.fattr = &data->fattr;
1748 data->res.verf = &data->verf;
1749 nfs_fattr_init(&data->fattr);
1751 EXPORT_SYMBOL_GPL(nfs_init_commit);
1753 void nfs_retry_commit(struct list_head *page_list,
1754 struct pnfs_layout_segment *lseg,
1755 struct nfs_commit_info *cinfo,
1756 u32 ds_commit_idx)
1758 struct nfs_page *req;
1760 while (!list_empty(page_list)) {
1761 req = nfs_list_entry(page_list->next);
1762 nfs_list_remove_request(req);
1763 nfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx);
1764 if (!cinfo->dreq)
1765 nfs_clear_page_commit(req->wb_page);
1766 nfs_unlock_and_release_request(req);
1769 EXPORT_SYMBOL_GPL(nfs_retry_commit);
1771 static void
1772 nfs_commit_resched_write(struct nfs_commit_info *cinfo,
1773 struct nfs_page *req)
1775 __set_page_dirty_nobuffers(req->wb_page);
1779 * Commit dirty pages
1781 static int
1782 nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1783 struct nfs_commit_info *cinfo)
1785 struct nfs_commit_data *data;
1787 /* another commit raced with us */
1788 if (list_empty(head))
1789 return 0;
1791 data = nfs_commitdata_alloc(true);
1793 /* Set up the argument struct */
1794 nfs_init_commit(data, head, NULL, cinfo);
1795 atomic_inc(&cinfo->mds->rpcs_out);
1796 return nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(inode),
1797 data->mds_ops, how, 0);
1801 * COMMIT call returned
1803 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1805 struct nfs_commit_data *data = calldata;
1807 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1808 task->tk_pid, task->tk_status);
1810 /* Call the NFS version-specific code */
1811 NFS_PROTO(data->inode)->commit_done(task, data);
1812 trace_nfs_commit_done(data);
1815 static void nfs_commit_release_pages(struct nfs_commit_data *data)
1817 const struct nfs_writeverf *verf = data->res.verf;
1818 struct nfs_page *req;
1819 int status = data->task.tk_status;
1820 struct nfs_commit_info cinfo;
1821 struct nfs_server *nfss;
1823 while (!list_empty(&data->pages)) {
1824 req = nfs_list_entry(data->pages.next);
1825 nfs_list_remove_request(req);
1826 if (req->wb_page)
1827 nfs_clear_page_commit(req->wb_page);
1829 dprintk("NFS: commit (%s/%llu %d@%lld)",
1830 req->wb_context->dentry->d_sb->s_id,
1831 (unsigned long long)NFS_FILEID(d_inode(req->wb_context->dentry)),
1832 req->wb_bytes,
1833 (long long)req_offset(req));
1834 if (status < 0) {
1835 nfs_context_set_write_error(req->wb_context, status);
1836 if (req->wb_page)
1837 nfs_inode_remove_request(req);
1838 dprintk_cont(", error = %d\n", status);
1839 goto next;
1842 /* Okay, COMMIT succeeded, apparently. Check the verifier
1843 * returned by the server against all stored verfs. */
1844 if (verf->committed > NFS_UNSTABLE &&
1845 !nfs_write_verifier_cmp(&req->wb_verf, &verf->verifier)) {
1846 /* We have a match */
1847 if (req->wb_page)
1848 nfs_inode_remove_request(req);
1849 dprintk_cont(" OK\n");
1850 goto next;
1852 /* We have a mismatch. Write the page again */
1853 dprintk_cont(" mismatch\n");
1854 nfs_mark_request_dirty(req);
1855 set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
1856 next:
1857 nfs_unlock_and_release_request(req);
1858 /* Latency breaker */
1859 cond_resched();
1861 nfss = NFS_SERVER(data->inode);
1862 if (atomic_long_read(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
1863 clear_bdi_congested(inode_to_bdi(data->inode), BLK_RW_ASYNC);
1865 nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1866 nfs_commit_end(cinfo.mds);
1869 static void nfs_commit_release(void *calldata)
1871 struct nfs_commit_data *data = calldata;
1873 data->completion_ops->completion(data);
1874 nfs_commitdata_release(calldata);
1877 static const struct rpc_call_ops nfs_commit_ops = {
1878 .rpc_call_prepare = nfs_commit_prepare,
1879 .rpc_call_done = nfs_commit_done,
1880 .rpc_release = nfs_commit_release,
1883 static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1884 .completion = nfs_commit_release_pages,
1885 .resched_write = nfs_commit_resched_write,
1888 int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1889 int how, struct nfs_commit_info *cinfo)
1891 int status;
1893 status = pnfs_commit_list(inode, head, how, cinfo);
1894 if (status == PNFS_NOT_ATTEMPTED)
1895 status = nfs_commit_list(inode, head, how, cinfo);
1896 return status;
1899 static int __nfs_commit_inode(struct inode *inode, int how,
1900 struct writeback_control *wbc)
1902 LIST_HEAD(head);
1903 struct nfs_commit_info cinfo;
1904 int may_wait = how & FLUSH_SYNC;
1905 int ret, nscan;
1907 nfs_init_cinfo_from_inode(&cinfo, inode);
1908 nfs_commit_begin(cinfo.mds);
1909 for (;;) {
1910 ret = nscan = nfs_scan_commit(inode, &head, &cinfo);
1911 if (ret <= 0)
1912 break;
1913 ret = nfs_generic_commit_list(inode, &head, how, &cinfo);
1914 if (ret < 0)
1915 break;
1916 ret = 0;
1917 if (wbc && wbc->sync_mode == WB_SYNC_NONE) {
1918 if (nscan < wbc->nr_to_write)
1919 wbc->nr_to_write -= nscan;
1920 else
1921 wbc->nr_to_write = 0;
1923 if (nscan < INT_MAX)
1924 break;
1925 cond_resched();
1927 nfs_commit_end(cinfo.mds);
1928 if (ret || !may_wait)
1929 return ret;
1930 return wait_on_commit(cinfo.mds);
1933 int nfs_commit_inode(struct inode *inode, int how)
1935 return __nfs_commit_inode(inode, how, NULL);
1937 EXPORT_SYMBOL_GPL(nfs_commit_inode);
1939 int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
1941 struct nfs_inode *nfsi = NFS_I(inode);
1942 int flags = FLUSH_SYNC;
1943 int ret = 0;
1945 if (wbc->sync_mode == WB_SYNC_NONE) {
1946 /* no commits means nothing needs to be done */
1947 if (!atomic_long_read(&nfsi->commit_info.ncommit))
1948 goto check_requests_outstanding;
1950 /* Don't commit yet if this is a non-blocking flush and there
1951 * are a lot of outstanding writes for this mapping.
1953 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1954 goto out_mark_dirty;
1956 /* don't wait for the COMMIT response */
1957 flags = 0;
1960 ret = __nfs_commit_inode(inode, flags, wbc);
1961 if (!ret) {
1962 if (flags & FLUSH_SYNC)
1963 return 0;
1964 } else if (atomic_long_read(&nfsi->commit_info.ncommit))
1965 goto out_mark_dirty;
1967 check_requests_outstanding:
1968 if (!atomic_read(&nfsi->commit_info.rpcs_out))
1969 return ret;
1970 out_mark_dirty:
1971 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
1972 return ret;
1974 EXPORT_SYMBOL_GPL(nfs_write_inode);
1977 * Wrapper for filemap_write_and_wait_range()
1979 * Needed for pNFS in order to ensure data becomes visible to the
1980 * client.
1982 int nfs_filemap_write_and_wait_range(struct address_space *mapping,
1983 loff_t lstart, loff_t lend)
1985 int ret;
1987 ret = filemap_write_and_wait_range(mapping, lstart, lend);
1988 if (ret == 0)
1989 ret = pnfs_sync_inode(mapping->host, true);
1990 return ret;
1992 EXPORT_SYMBOL_GPL(nfs_filemap_write_and_wait_range);
1995 * flush the inode to disk.
1997 int nfs_wb_all(struct inode *inode)
1999 int ret;
2001 trace_nfs_writeback_inode_enter(inode);
2003 ret = filemap_write_and_wait(inode->i_mapping);
2004 if (ret)
2005 goto out;
2006 ret = nfs_commit_inode(inode, FLUSH_SYNC);
2007 if (ret < 0)
2008 goto out;
2009 pnfs_sync_inode(inode, true);
2010 ret = 0;
2012 out:
2013 trace_nfs_writeback_inode_exit(inode, ret);
2014 return ret;
2016 EXPORT_SYMBOL_GPL(nfs_wb_all);
2018 int nfs_wb_page_cancel(struct inode *inode, struct page *page)
2020 struct nfs_page *req;
2021 int ret = 0;
2023 wait_on_page_writeback(page);
2025 /* blocking call to cancel all requests and join to a single (head)
2026 * request */
2027 req = nfs_lock_and_join_requests(page);
2029 if (IS_ERR(req)) {
2030 ret = PTR_ERR(req);
2031 } else if (req) {
2032 /* all requests from this page have been cancelled by
2033 * nfs_lock_and_join_requests, so just remove the head
2034 * request from the inode / page_private pointer and
2035 * release it */
2036 nfs_inode_remove_request(req);
2037 nfs_unlock_and_release_request(req);
2040 return ret;
2044 * Write back all requests on one page - we do this before reading it.
2046 int nfs_wb_page(struct inode *inode, struct page *page)
2048 loff_t range_start = page_file_offset(page);
2049 loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1);
2050 struct writeback_control wbc = {
2051 .sync_mode = WB_SYNC_ALL,
2052 .nr_to_write = 0,
2053 .range_start = range_start,
2054 .range_end = range_end,
2056 int ret;
2058 trace_nfs_writeback_page_enter(inode);
2060 for (;;) {
2061 wait_on_page_writeback(page);
2062 if (clear_page_dirty_for_io(page)) {
2063 ret = nfs_writepage_locked(page, &wbc);
2064 if (ret < 0)
2065 goto out_error;
2066 continue;
2068 ret = 0;
2069 if (!PagePrivate(page))
2070 break;
2071 ret = nfs_commit_inode(inode, FLUSH_SYNC);
2072 if (ret < 0)
2073 goto out_error;
2075 out_error:
2076 trace_nfs_writeback_page_exit(inode, ret);
2077 return ret;
2080 #ifdef CONFIG_MIGRATION
2081 int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
2082 struct page *page, enum migrate_mode mode)
2085 * If PagePrivate is set, then the page is currently associated with
2086 * an in-progress read or write request. Don't try to migrate it.
2088 * FIXME: we could do this in principle, but we'll need a way to ensure
2089 * that we can safely release the inode reference while holding
2090 * the page lock.
2092 if (PagePrivate(page))
2093 return -EBUSY;
2095 if (!nfs_fscache_release_page(page, GFP_KERNEL))
2096 return -EBUSY;
2098 return migrate_page(mapping, newpage, page, mode);
2100 #endif
2102 int __init nfs_init_writepagecache(void)
2104 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
2105 sizeof(struct nfs_pgio_header),
2106 0, SLAB_HWCACHE_ALIGN,
2107 NULL);
2108 if (nfs_wdata_cachep == NULL)
2109 return -ENOMEM;
2111 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
2112 nfs_wdata_cachep);
2113 if (nfs_wdata_mempool == NULL)
2114 goto out_destroy_write_cache;
2116 nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
2117 sizeof(struct nfs_commit_data),
2118 0, SLAB_HWCACHE_ALIGN,
2119 NULL);
2120 if (nfs_cdata_cachep == NULL)
2121 goto out_destroy_write_mempool;
2123 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
2124 nfs_cdata_cachep);
2125 if (nfs_commit_mempool == NULL)
2126 goto out_destroy_commit_cache;
2129 * NFS congestion size, scale with available memory.
2131 * 64MB: 8192k
2132 * 128MB: 11585k
2133 * 256MB: 16384k
2134 * 512MB: 23170k
2135 * 1GB: 32768k
2136 * 2GB: 46340k
2137 * 4GB: 65536k
2138 * 8GB: 92681k
2139 * 16GB: 131072k
2141 * This allows larger machines to have larger/more transfers.
2142 * Limit the default to 256M
2144 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
2145 if (nfs_congestion_kb > 256*1024)
2146 nfs_congestion_kb = 256*1024;
2148 return 0;
2150 out_destroy_commit_cache:
2151 kmem_cache_destroy(nfs_cdata_cachep);
2152 out_destroy_write_mempool:
2153 mempool_destroy(nfs_wdata_mempool);
2154 out_destroy_write_cache:
2155 kmem_cache_destroy(nfs_wdata_cachep);
2156 return -ENOMEM;
2159 void nfs_destroy_writepagecache(void)
2161 mempool_destroy(nfs_commit_mempool);
2162 kmem_cache_destroy(nfs_cdata_cachep);
2163 mempool_destroy(nfs_wdata_mempool);
2164 kmem_cache_destroy(nfs_wdata_cachep);
2167 static const struct nfs_rw_ops nfs_rw_write_ops = {
2168 .rw_alloc_header = nfs_writehdr_alloc,
2169 .rw_free_header = nfs_writehdr_free,
2170 .rw_done = nfs_writeback_done,
2171 .rw_result = nfs_writeback_result,
2172 .rw_initiate = nfs_initiate_write,