4 * Write file data over NFS.
6 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
9 #include <linux/types.h>
10 #include <linux/slab.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>
25 #include <asm/uaccess.h>
27 #include "delegation.h"
36 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
38 #define MIN_POOL_WRITE (32)
39 #define MIN_POOL_COMMIT (4)
42 * Local function declarations
44 static void nfs_redirty_request(struct nfs_page *req);
45 static const struct rpc_call_ops nfs_commit_ops;
46 static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops;
47 static const struct nfs_commit_completion_ops nfs_commit_completion_ops;
48 static const struct nfs_rw_ops nfs_rw_write_ops;
49 static void nfs_clear_request_commit(struct nfs_page *req);
51 static struct kmem_cache *nfs_wdata_cachep;
52 static mempool_t *nfs_wdata_mempool;
53 static struct kmem_cache *nfs_cdata_cachep;
54 static mempool_t *nfs_commit_mempool;
56 struct nfs_commit_data *nfs_commitdata_alloc(void)
58 struct nfs_commit_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOIO);
61 memset(p, 0, sizeof(*p));
62 INIT_LIST_HEAD(&p->pages);
66 EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
68 void nfs_commit_free(struct nfs_commit_data *p)
70 mempool_free(p, nfs_commit_mempool);
72 EXPORT_SYMBOL_GPL(nfs_commit_free);
74 static struct nfs_pgio_header *nfs_writehdr_alloc(void)
76 struct nfs_pgio_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOIO);
79 memset(p, 0, sizeof(*p));
83 static void nfs_writehdr_free(struct nfs_pgio_header *hdr)
85 mempool_free(hdr, nfs_wdata_mempool);
88 static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
92 set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
96 * nfs_page_find_head_request_locked - find head request associated with @page
98 * must be called while holding the inode lock.
100 * returns matching head request with reference held, or NULL if not found.
102 static struct nfs_page *
103 nfs_page_find_head_request_locked(struct nfs_inode *nfsi, struct page *page)
105 struct nfs_page *req = NULL;
107 if (PagePrivate(page))
108 req = (struct nfs_page *)page_private(page);
109 else if (unlikely(PageSwapCache(page))) {
110 struct nfs_page *freq, *t;
112 /* Linearly search the commit list for the correct req */
113 list_for_each_entry_safe(freq, t, &nfsi->commit_info.list, wb_list) {
114 if (freq->wb_page == page) {
122 WARN_ON_ONCE(req->wb_head != req);
124 kref_get(&req->wb_kref);
131 * nfs_page_find_head_request - find head request associated with @page
133 * returns matching head request with reference held, or NULL if not found.
135 static struct nfs_page *nfs_page_find_head_request(struct page *page)
137 struct inode *inode = page_file_mapping(page)->host;
138 struct nfs_page *req = NULL;
140 spin_lock(&inode->i_lock);
141 req = nfs_page_find_head_request_locked(NFS_I(inode), page);
142 spin_unlock(&inode->i_lock);
146 /* Adjust the file length if we're writing beyond the end */
147 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
149 struct inode *inode = page_file_mapping(page)->host;
153 spin_lock(&inode->i_lock);
154 i_size = i_size_read(inode);
155 end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
156 if (i_size > 0 && page_file_index(page) < end_index)
158 end = page_file_offset(page) + ((loff_t)offset+count);
161 i_size_write(inode, end);
162 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
164 spin_unlock(&inode->i_lock);
167 /* A writeback failed: mark the page as bad, and invalidate the page cache */
168 static void nfs_set_pageerror(struct page *page)
170 nfs_zap_mapping(page_file_mapping(page)->host, page_file_mapping(page));
174 * nfs_page_group_search_locked
175 * @head - head request of page group
176 * @page_offset - offset into page
178 * Search page group with head @head to find a request that contains the
179 * page offset @page_offset.
181 * Returns a pointer to the first matching nfs request, or NULL if no
184 * Must be called with the page group lock held
186 static struct nfs_page *
187 nfs_page_group_search_locked(struct nfs_page *head, unsigned int page_offset)
189 struct nfs_page *req;
191 WARN_ON_ONCE(head != head->wb_head);
192 WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_head->wb_flags));
196 if (page_offset >= req->wb_pgbase &&
197 page_offset < (req->wb_pgbase + req->wb_bytes))
200 req = req->wb_this_page;
201 } while (req != head);
207 * nfs_page_group_covers_page
208 * @head - head request of page group
210 * Return true if the page group with head @head covers the whole page,
211 * returns false otherwise
213 static bool nfs_page_group_covers_page(struct nfs_page *req)
215 struct nfs_page *tmp;
216 unsigned int pos = 0;
217 unsigned int len = nfs_page_length(req->wb_page);
219 nfs_page_group_lock(req);
222 tmp = nfs_page_group_search_locked(req->wb_head, pos);
224 /* no way this should happen */
225 WARN_ON_ONCE(tmp->wb_pgbase != pos);
226 pos += tmp->wb_bytes - (pos - tmp->wb_pgbase);
228 } while (tmp && pos < len);
230 nfs_page_group_unlock(req);
231 WARN_ON_ONCE(pos > len);
235 /* We can set the PG_uptodate flag if we see that a write request
236 * covers the full page.
238 static void nfs_mark_uptodate(struct nfs_page *req)
240 if (PageUptodate(req->wb_page))
242 if (!nfs_page_group_covers_page(req))
244 SetPageUptodate(req->wb_page);
247 static int wb_priority(struct writeback_control *wbc)
249 if (wbc->for_reclaim)
250 return FLUSH_HIGHPRI | FLUSH_STABLE;
251 if (wbc->for_kupdate || wbc->for_background)
252 return FLUSH_LOWPRI | FLUSH_COND_STABLE;
253 return FLUSH_COND_STABLE;
257 * NFS congestion control
260 int nfs_congestion_kb;
262 #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
263 #define NFS_CONGESTION_OFF_THRESH \
264 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
266 static void nfs_set_page_writeback(struct page *page)
268 struct nfs_server *nfss = NFS_SERVER(page_file_mapping(page)->host);
269 int ret = test_set_page_writeback(page);
271 WARN_ON_ONCE(ret != 0);
273 if (atomic_long_inc_return(&nfss->writeback) >
274 NFS_CONGESTION_ON_THRESH) {
275 set_bdi_congested(&nfss->backing_dev_info,
280 static void nfs_end_page_writeback(struct nfs_page *req)
282 struct inode *inode = page_file_mapping(req->wb_page)->host;
283 struct nfs_server *nfss = NFS_SERVER(inode);
285 if (!nfs_page_group_sync_on_bit(req, PG_WB_END))
288 end_page_writeback(req->wb_page);
289 if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
290 clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
294 /* nfs_page_group_clear_bits
295 * @req - an nfs request
296 * clears all page group related bits from @req
299 nfs_page_group_clear_bits(struct nfs_page *req)
301 clear_bit(PG_TEARDOWN, &req->wb_flags);
302 clear_bit(PG_UNLOCKPAGE, &req->wb_flags);
303 clear_bit(PG_UPTODATE, &req->wb_flags);
304 clear_bit(PG_WB_END, &req->wb_flags);
305 clear_bit(PG_REMOVE, &req->wb_flags);
310 * nfs_unroll_locks_and_wait - unlock all newly locked reqs and wait on @req
312 * this is a helper function for nfs_lock_and_join_requests
314 * @inode - inode associated with request page group, must be holding inode lock
315 * @head - head request of page group, must be holding head lock
316 * @req - request that couldn't lock and needs to wait on the req bit lock
317 * @nonblock - if true, don't actually wait
319 * NOTE: this must be called holding page_group bit lock and inode spin lock
320 * and BOTH will be released before returning.
322 * returns 0 on success, < 0 on error.
325 nfs_unroll_locks_and_wait(struct inode *inode, struct nfs_page *head,
326 struct nfs_page *req, bool nonblock)
327 __releases(&inode->i_lock)
329 struct nfs_page *tmp;
332 /* relinquish all the locks successfully grabbed this run */
333 for (tmp = head ; tmp != req; tmp = tmp->wb_this_page)
334 nfs_unlock_request(tmp);
336 WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
338 /* grab a ref on the request that will be waited on */
339 kref_get(&req->wb_kref);
341 nfs_page_group_unlock(head);
342 spin_unlock(&inode->i_lock);
344 /* release ref from nfs_page_find_head_request_locked */
345 nfs_release_request(head);
348 ret = nfs_wait_on_request(req);
351 nfs_release_request(req);
357 * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests
359 * @destroy_list - request list (using wb_this_page) terminated by @old_head
360 * @old_head - the old head of the list
362 * All subrequests must be locked and removed from all lists, so at this point
363 * they are only "active" in this function, and possibly in nfs_wait_on_request
364 * with a reference held by some other context.
367 nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list,
368 struct nfs_page *old_head)
370 while (destroy_list) {
371 struct nfs_page *subreq = destroy_list;
373 destroy_list = (subreq->wb_this_page == old_head) ?
374 NULL : subreq->wb_this_page;
376 WARN_ON_ONCE(old_head != subreq->wb_head);
378 /* make sure old group is not used */
379 subreq->wb_head = subreq;
380 subreq->wb_this_page = subreq;
382 nfs_clear_request_commit(subreq);
384 /* subreq is now totally disconnected from page group or any
385 * write / commit lists. last chance to wake any waiters */
386 nfs_unlock_request(subreq);
388 if (!test_bit(PG_TEARDOWN, &subreq->wb_flags)) {
389 /* release ref on old head request */
390 nfs_release_request(old_head);
392 nfs_page_group_clear_bits(subreq);
394 /* release the PG_INODE_REF reference */
395 if (test_and_clear_bit(PG_INODE_REF, &subreq->wb_flags))
396 nfs_release_request(subreq);
400 WARN_ON_ONCE(test_bit(PG_CLEAN, &subreq->wb_flags));
401 /* zombie requests have already released the last
402 * reference and were waiting on the rest of the
403 * group to complete. Since it's no longer part of a
404 * group, simply free the request */
405 nfs_page_group_clear_bits(subreq);
406 nfs_free_request(subreq);
412 * nfs_lock_and_join_requests - join all subreqs to the head req and return
413 * a locked reference, cancelling any pending
414 * operations for this page.
416 * @page - the page used to lookup the "page group" of nfs_page structures
417 * @nonblock - if true, don't block waiting for request locks
419 * This function joins all sub requests to the head request by first
420 * locking all requests in the group, cancelling any pending operations
421 * and finally updating the head request to cover the whole range covered by
422 * the (former) group. All subrequests are removed from any write or commit
423 * lists, unlinked from the group and destroyed.
425 * Returns a locked, referenced pointer to the head request - which after
426 * this call is guaranteed to be the only request associated with the page.
427 * Returns NULL if no requests are found for @page, or a ERR_PTR if an
428 * error was encountered.
430 static struct nfs_page *
431 nfs_lock_and_join_requests(struct page *page, bool nonblock)
433 struct inode *inode = page_file_mapping(page)->host;
434 struct nfs_page *head, *subreq;
435 struct nfs_page *destroy_list = NULL;
436 unsigned int total_bytes;
442 WARN_ON_ONCE(destroy_list);
444 spin_lock(&inode->i_lock);
447 * A reference is taken only on the head request which acts as a
448 * reference to the whole page group - the group will not be destroyed
449 * until the head reference is released.
451 head = nfs_page_find_head_request_locked(NFS_I(inode), page);
454 spin_unlock(&inode->i_lock);
458 /* lock each request in the page group */
459 nfs_page_group_lock(head);
463 * Subrequests are always contiguous, non overlapping
464 * and in order. If not, it's a programming error.
466 WARN_ON_ONCE(subreq->wb_offset !=
467 (head->wb_offset + total_bytes));
469 /* keep track of how many bytes this group covers */
470 total_bytes += subreq->wb_bytes;
472 if (!nfs_lock_request(subreq)) {
473 /* releases page group bit lock and
474 * inode spin lock and all references */
475 ret = nfs_unroll_locks_and_wait(inode, head,
484 subreq = subreq->wb_this_page;
485 } while (subreq != head);
487 /* Now that all requests are locked, make sure they aren't on any list.
488 * Commit list removal accounting is done after locks are dropped */
491 nfs_list_remove_request(subreq);
492 subreq = subreq->wb_this_page;
493 } while (subreq != head);
495 /* unlink subrequests from head, destroy them later */
496 if (head->wb_this_page != head) {
497 /* destroy list will be terminated by head */
498 destroy_list = head->wb_this_page;
499 head->wb_this_page = head;
501 /* change head request to cover whole range that
502 * the former page group covered */
503 head->wb_bytes = total_bytes;
507 * prepare head request to be added to new pgio descriptor
509 nfs_page_group_clear_bits(head);
512 * some part of the group was still on the inode list - otherwise
513 * the group wouldn't be involved in async write.
514 * grab a reference for the head request, iff it needs one.
516 if (!test_and_set_bit(PG_INODE_REF, &head->wb_flags))
517 kref_get(&head->wb_kref);
519 nfs_page_group_unlock(head);
521 /* drop lock to clear_request_commit the head req and clean up
522 * requests on destroy list */
523 spin_unlock(&inode->i_lock);
525 nfs_destroy_unlinked_subrequests(destroy_list, head);
527 /* clean up commit list state */
528 nfs_clear_request_commit(head);
530 /* still holds ref on head from nfs_page_find_head_request_locked
531 * and still has lock on head from lock loop */
536 * Find an associated nfs write request, and prepare to flush it out
537 * May return an error if the user signalled nfs_wait_on_request().
539 static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
540 struct page *page, bool nonblock)
542 struct nfs_page *req;
545 req = nfs_lock_and_join_requests(page, nonblock);
552 nfs_set_page_writeback(page);
553 WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
556 if (!nfs_pageio_add_request(pgio, req)) {
557 nfs_redirty_request(req);
558 ret = pgio->pg_error;
564 static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
566 struct inode *inode = page_file_mapping(page)->host;
569 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
570 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
572 nfs_pageio_cond_complete(pgio, page_file_index(page));
573 ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE);
574 if (ret == -EAGAIN) {
575 redirty_page_for_writepage(wbc, page);
582 * Write an mmapped page to the server.
584 static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
586 struct nfs_pageio_descriptor pgio;
589 nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc),
590 false, &nfs_async_write_completion_ops);
591 err = nfs_do_writepage(page, wbc, &pgio);
592 nfs_pageio_complete(&pgio);
595 if (pgio.pg_error < 0)
596 return pgio.pg_error;
600 int nfs_writepage(struct page *page, struct writeback_control *wbc)
604 ret = nfs_writepage_locked(page, wbc);
609 static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
613 ret = nfs_do_writepage(page, wbc, data);
618 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
620 struct inode *inode = mapping->host;
621 unsigned long *bitlock = &NFS_I(inode)->flags;
622 struct nfs_pageio_descriptor pgio;
625 /* Stop dirtying of new pages while we sync */
626 err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
627 nfs_wait_bit_killable, TASK_KILLABLE);
631 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
633 nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false,
634 &nfs_async_write_completion_ops);
635 err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
636 nfs_pageio_complete(&pgio);
638 clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
639 smp_mb__after_atomic();
640 wake_up_bit(bitlock, NFS_INO_FLUSHING);
653 * Insert a write request into an inode
655 static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
657 struct nfs_inode *nfsi = NFS_I(inode);
659 WARN_ON_ONCE(req->wb_this_page != req);
661 /* Lock the request! */
662 nfs_lock_request(req);
664 spin_lock(&inode->i_lock);
665 if (!nfsi->npages && NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
668 * Swap-space should not get truncated. Hence no need to plug the race
669 * with invalidate/truncate.
671 if (likely(!PageSwapCache(req->wb_page))) {
672 set_bit(PG_MAPPED, &req->wb_flags);
673 SetPagePrivate(req->wb_page);
674 set_page_private(req->wb_page, (unsigned long)req);
677 /* this a head request for a page group - mark it as having an
678 * extra reference so sub groups can follow suit */
679 WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags));
680 kref_get(&req->wb_kref);
681 spin_unlock(&inode->i_lock);
685 * Remove a write request from an inode
687 static void nfs_inode_remove_request(struct nfs_page *req)
689 struct inode *inode = req->wb_context->dentry->d_inode;
690 struct nfs_inode *nfsi = NFS_I(inode);
691 struct nfs_page *head;
693 if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) {
696 spin_lock(&inode->i_lock);
697 if (likely(!PageSwapCache(head->wb_page))) {
698 set_page_private(head->wb_page, 0);
699 ClearPagePrivate(head->wb_page);
700 clear_bit(PG_MAPPED, &head->wb_flags);
703 spin_unlock(&inode->i_lock);
706 if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags))
707 nfs_release_request(req);
711 nfs_mark_request_dirty(struct nfs_page *req)
713 __set_page_dirty_nobuffers(req->wb_page);
716 #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
718 * nfs_request_add_commit_list - add request to a commit list
719 * @req: pointer to a struct nfs_page
720 * @dst: commit list head
721 * @cinfo: holds list lock and accounting info
723 * This sets the PG_CLEAN bit, updates the cinfo count of
724 * number of outstanding requests requiring a commit as well as
727 * The caller must _not_ hold the cinfo->lock, but must be
728 * holding the nfs_page lock.
731 nfs_request_add_commit_list(struct nfs_page *req, struct list_head *dst,
732 struct nfs_commit_info *cinfo)
734 set_bit(PG_CLEAN, &(req)->wb_flags);
735 spin_lock(cinfo->lock);
736 nfs_list_add_request(req, dst);
737 cinfo->mds->ncommit++;
738 spin_unlock(cinfo->lock);
740 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
741 inc_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
743 __mark_inode_dirty(req->wb_context->dentry->d_inode,
747 EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
750 * nfs_request_remove_commit_list - Remove request from a commit list
751 * @req: pointer to a nfs_page
752 * @cinfo: holds list lock and accounting info
754 * This clears the PG_CLEAN bit, and updates the cinfo's count of
755 * number of outstanding requests requiring a commit
756 * It does not update the MM page stats.
758 * The caller _must_ hold the cinfo->lock and the nfs_page lock.
761 nfs_request_remove_commit_list(struct nfs_page *req,
762 struct nfs_commit_info *cinfo)
764 if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
766 nfs_list_remove_request(req);
767 cinfo->mds->ncommit--;
769 EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
771 static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
774 cinfo->lock = &inode->i_lock;
775 cinfo->mds = &NFS_I(inode)->commit_info;
776 cinfo->ds = pnfs_get_ds_info(inode);
778 cinfo->completion_ops = &nfs_commit_completion_ops;
781 void nfs_init_cinfo(struct nfs_commit_info *cinfo,
783 struct nfs_direct_req *dreq)
786 nfs_init_cinfo_from_dreq(cinfo, dreq);
788 nfs_init_cinfo_from_inode(cinfo, inode);
790 EXPORT_SYMBOL_GPL(nfs_init_cinfo);
793 * Add a request to the inode's commit list.
796 nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
797 struct nfs_commit_info *cinfo)
799 if (pnfs_mark_request_commit(req, lseg, cinfo))
801 nfs_request_add_commit_list(req, &cinfo->mds->list, cinfo);
805 nfs_clear_page_commit(struct page *page)
807 dec_zone_page_state(page, NR_UNSTABLE_NFS);
808 dec_bdi_stat(page_file_mapping(page)->backing_dev_info, BDI_RECLAIMABLE);
812 nfs_clear_request_commit(struct nfs_page *req)
814 if (test_bit(PG_CLEAN, &req->wb_flags)) {
815 struct inode *inode = req->wb_context->dentry->d_inode;
816 struct nfs_commit_info cinfo;
818 nfs_init_cinfo_from_inode(&cinfo, inode);
819 if (!pnfs_clear_request_commit(req, &cinfo)) {
820 spin_lock(cinfo.lock);
821 nfs_request_remove_commit_list(req, &cinfo);
822 spin_unlock(cinfo.lock);
824 nfs_clear_page_commit(req->wb_page);
828 int nfs_write_need_commit(struct nfs_pgio_header *hdr)
830 if (hdr->verf.committed == NFS_DATA_SYNC)
831 return hdr->lseg == NULL;
832 return hdr->verf.committed != NFS_FILE_SYNC;
836 static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
841 void nfs_init_cinfo(struct nfs_commit_info *cinfo,
843 struct nfs_direct_req *dreq)
848 nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
849 struct nfs_commit_info *cinfo)
854 nfs_clear_request_commit(struct nfs_page *req)
858 int nfs_write_need_commit(struct nfs_pgio_header *hdr)
865 static void nfs_write_completion(struct nfs_pgio_header *hdr)
867 struct nfs_commit_info cinfo;
868 unsigned long bytes = 0;
870 if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
872 nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
873 while (!list_empty(&hdr->pages)) {
874 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
876 bytes += req->wb_bytes;
877 nfs_list_remove_request(req);
878 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
879 (hdr->good_bytes < bytes)) {
880 nfs_set_pageerror(req->wb_page);
881 nfs_context_set_write_error(req->wb_context, hdr->error);
884 if (nfs_write_need_commit(hdr)) {
885 memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf));
886 nfs_mark_request_commit(req, hdr->lseg, &cinfo);
890 nfs_inode_remove_request(req);
892 nfs_unlock_request(req);
893 nfs_end_page_writeback(req);
894 nfs_release_request(req);
900 #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
902 nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
904 return cinfo->mds->ncommit;
907 /* cinfo->lock held by caller */
909 nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
910 struct nfs_commit_info *cinfo, int max)
912 struct nfs_page *req, *tmp;
915 list_for_each_entry_safe(req, tmp, src, wb_list) {
916 if (!nfs_lock_request(req))
918 kref_get(&req->wb_kref);
919 if (cond_resched_lock(cinfo->lock))
920 list_safe_reset_next(req, tmp, wb_list);
921 nfs_request_remove_commit_list(req, cinfo);
922 nfs_list_add_request(req, dst);
924 if ((ret == max) && !cinfo->dreq)
931 * nfs_scan_commit - Scan an inode for commit requests
932 * @inode: NFS inode to scan
933 * @dst: mds destination list
934 * @cinfo: mds and ds lists of reqs ready to commit
936 * Moves requests from the inode's 'commit' request list.
937 * The requests are *not* checked to ensure that they form a contiguous set.
940 nfs_scan_commit(struct inode *inode, struct list_head *dst,
941 struct nfs_commit_info *cinfo)
945 spin_lock(cinfo->lock);
946 if (cinfo->mds->ncommit > 0) {
947 const int max = INT_MAX;
949 ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
951 ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
953 spin_unlock(cinfo->lock);
958 unsigned long nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
963 int nfs_scan_commit(struct inode *inode, struct list_head *dst,
964 struct nfs_commit_info *cinfo)
971 * Search for an existing write request, and attempt to update
972 * it to reflect a new dirty region on a given page.
974 * If the attempt fails, then the existing request is flushed out
977 static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
982 struct nfs_page *req;
987 if (!PagePrivate(page))
990 end = offset + bytes;
991 spin_lock(&inode->i_lock);
994 req = nfs_page_find_head_request_locked(NFS_I(inode), page);
998 /* should be handled by nfs_flush_incompatible */
999 WARN_ON_ONCE(req->wb_head != req);
1000 WARN_ON_ONCE(req->wb_this_page != req);
1002 rqend = req->wb_offset + req->wb_bytes;
1004 * Tell the caller to flush out the request if
1005 * the offsets are non-contiguous.
1006 * Note: nfs_flush_incompatible() will already
1007 * have flushed out requests having wrong owners.
1010 || end < req->wb_offset)
1013 if (nfs_lock_request(req))
1016 /* The request is locked, so wait and then retry */
1017 spin_unlock(&inode->i_lock);
1018 error = nfs_wait_on_request(req);
1019 nfs_release_request(req);
1022 spin_lock(&inode->i_lock);
1025 /* Okay, the request matches. Update the region */
1026 if (offset < req->wb_offset) {
1027 req->wb_offset = offset;
1028 req->wb_pgbase = offset;
1031 req->wb_bytes = end - req->wb_offset;
1033 req->wb_bytes = rqend - req->wb_offset;
1035 spin_unlock(&inode->i_lock);
1037 nfs_clear_request_commit(req);
1040 spin_unlock(&inode->i_lock);
1041 nfs_release_request(req);
1042 error = nfs_wb_page(inode, page);
1044 return ERR_PTR(error);
1048 * Try to update an existing write request, or create one if there is none.
1050 * Note: Should always be called with the Page Lock held to prevent races
1051 * if we have to add a new request. Also assumes that the caller has
1052 * already called nfs_flush_incompatible() if necessary.
1054 static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
1055 struct page *page, unsigned int offset, unsigned int bytes)
1057 struct inode *inode = page_file_mapping(page)->host;
1058 struct nfs_page *req;
1060 req = nfs_try_to_update_request(inode, page, offset, bytes);
1063 req = nfs_create_request(ctx, page, NULL, offset, bytes);
1066 nfs_inode_add_request(inode, req);
1071 static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
1072 unsigned int offset, unsigned int count)
1074 struct nfs_page *req;
1076 req = nfs_setup_write_request(ctx, page, offset, count);
1078 return PTR_ERR(req);
1079 /* Update file length */
1080 nfs_grow_file(page, offset, count);
1081 nfs_mark_uptodate(req);
1082 nfs_mark_request_dirty(req);
1083 nfs_unlock_and_release_request(req);
1087 int nfs_flush_incompatible(struct file *file, struct page *page)
1089 struct nfs_open_context *ctx = nfs_file_open_context(file);
1090 struct nfs_lock_context *l_ctx;
1091 struct nfs_page *req;
1092 int do_flush, status;
1094 * Look for a request corresponding to this page. If there
1095 * is one, and it belongs to another file, we flush it out
1096 * before we try to copy anything into the page. Do this
1097 * due to the lack of an ACCESS-type call in NFSv2.
1098 * Also do the same if we find a request from an existing
1102 req = nfs_page_find_head_request(page);
1105 l_ctx = req->wb_lock_context;
1106 do_flush = req->wb_page != page || req->wb_context != ctx;
1107 /* for now, flush if more than 1 request in page_group */
1108 do_flush |= req->wb_this_page != req;
1109 if (l_ctx && ctx->dentry->d_inode->i_flock != NULL) {
1110 do_flush |= l_ctx->lockowner.l_owner != current->files
1111 || l_ctx->lockowner.l_pid != current->tgid;
1113 nfs_release_request(req);
1116 status = nfs_wb_page(page_file_mapping(page)->host, page);
1117 } while (status == 0);
1122 * Avoid buffered writes when a open context credential's key would
1125 * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
1127 * Return 0 and set a credential flag which triggers the inode to flush
1128 * and performs NFS_FILE_SYNC writes if the key will expired within
1129 * RPC_KEY_EXPIRE_TIMEO.
1132 nfs_key_timeout_notify(struct file *filp, struct inode *inode)
1134 struct nfs_open_context *ctx = nfs_file_open_context(filp);
1135 struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
1137 return rpcauth_key_timeout_notify(auth, ctx->cred);
1141 * Test if the open context credential key is marked to expire soon.
1143 bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx)
1145 return rpcauth_cred_key_to_expire(ctx->cred);
1149 * If the page cache is marked as unsafe or invalid, then we can't rely on
1150 * the PageUptodate() flag. In this case, we will need to turn off
1151 * write optimisations that depend on the page contents being correct.
1153 static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
1155 struct nfs_inode *nfsi = NFS_I(inode);
1157 if (nfs_have_delegated_attributes(inode))
1159 if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
1162 if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags))
1165 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
1167 return PageUptodate(page) != 0;
1170 /* If we know the page is up to date, and we're not using byte range locks (or
1171 * if we have the whole file locked for writing), it may be more efficient to
1172 * extend the write to cover the entire page in order to avoid fragmentation
1175 * If the file is opened for synchronous writes then we can just skip the rest
1178 static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode)
1180 if (file->f_flags & O_DSYNC)
1182 if (!nfs_write_pageuptodate(page, inode))
1184 if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
1186 if (inode->i_flock == NULL || (inode->i_flock->fl_start == 0 &&
1187 inode->i_flock->fl_end == OFFSET_MAX &&
1188 inode->i_flock->fl_type != F_RDLCK))
1194 * Update and possibly write a cached page of an NFS file.
1196 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
1197 * things with a page scheduled for an RPC call (e.g. invalidate it).
1199 int nfs_updatepage(struct file *file, struct page *page,
1200 unsigned int offset, unsigned int count)
1202 struct nfs_open_context *ctx = nfs_file_open_context(file);
1203 struct inode *inode = page_file_mapping(page)->host;
1206 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
1208 dprintk("NFS: nfs_updatepage(%pD2 %d@%lld)\n",
1209 file, count, (long long)(page_file_offset(page) + offset));
1211 if (nfs_can_extend_write(file, page, inode)) {
1212 count = max(count + offset, nfs_page_length(page));
1216 status = nfs_writepage_setup(ctx, page, offset, count);
1218 nfs_set_pageerror(page);
1220 __set_page_dirty_nobuffers(page);
1222 dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
1223 status, (long long)i_size_read(inode));
1227 static int flush_task_priority(int how)
1229 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
1231 return RPC_PRIORITY_HIGH;
1233 return RPC_PRIORITY_LOW;
1235 return RPC_PRIORITY_NORMAL;
1238 static void nfs_initiate_write(struct nfs_pgio_header *hdr,
1239 struct rpc_message *msg,
1240 struct rpc_task_setup *task_setup_data, int how)
1242 struct inode *inode = hdr->inode;
1243 int priority = flush_task_priority(how);
1245 task_setup_data->priority = priority;
1246 NFS_PROTO(inode)->write_setup(hdr, msg);
1248 nfs4_state_protect_write(NFS_SERVER(inode)->nfs_client,
1249 &task_setup_data->rpc_client, msg, hdr);
1252 /* If a nfs_flush_* function fails, it should remove reqs from @head and
1253 * call this on each, which will prepare them to be retried on next
1254 * writeback using standard nfs.
1256 static void nfs_redirty_request(struct nfs_page *req)
1258 nfs_mark_request_dirty(req);
1259 nfs_unlock_request(req);
1260 nfs_end_page_writeback(req);
1261 nfs_release_request(req);
1264 static void nfs_async_write_error(struct list_head *head)
1266 struct nfs_page *req;
1268 while (!list_empty(head)) {
1269 req = nfs_list_entry(head->next);
1270 nfs_list_remove_request(req);
1271 nfs_redirty_request(req);
1275 static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1276 .error_cleanup = nfs_async_write_error,
1277 .completion = nfs_write_completion,
1280 void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1281 struct inode *inode, int ioflags, bool force_mds,
1282 const struct nfs_pgio_completion_ops *compl_ops)
1284 struct nfs_server *server = NFS_SERVER(inode);
1285 const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
1287 #ifdef CONFIG_NFS_V4_1
1288 if (server->pnfs_curr_ld && !force_mds)
1289 pg_ops = server->pnfs_curr_ld->pg_write_ops;
1291 nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops,
1292 server->wsize, ioflags);
1294 EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
1296 void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1298 pgio->pg_ops = &nfs_pgio_rw_ops;
1299 pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1301 EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1304 void nfs_commit_prepare(struct rpc_task *task, void *calldata)
1306 struct nfs_commit_data *data = calldata;
1308 NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
1311 static void nfs_writeback_release_common(struct nfs_pgio_header *hdr)
1317 * Special version of should_remove_suid() that ignores capabilities.
1319 static int nfs_should_remove_suid(const struct inode *inode)
1321 umode_t mode = inode->i_mode;
1324 /* suid always must be killed */
1325 if (unlikely(mode & S_ISUID))
1326 kill = ATTR_KILL_SUID;
1329 * sgid without any exec bits is just a mandatory locking mark; leave
1330 * it alone. If some exec bits are set, it's a real sgid; kill it.
1332 if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
1333 kill |= ATTR_KILL_SGID;
1335 if (unlikely(kill && S_ISREG(mode)))
1342 * This function is called when the WRITE call is complete.
1344 static int nfs_writeback_done(struct rpc_task *task,
1345 struct nfs_pgio_header *hdr,
1346 struct inode *inode)
1351 * ->write_done will attempt to use post-op attributes to detect
1352 * conflicting writes by other clients. A strict interpretation
1353 * of close-to-open would allow us to continue caching even if
1354 * another writer had changed the file, but some applications
1355 * depend on tighter cache coherency when writing.
1357 status = NFS_PROTO(inode)->write_done(task, hdr);
1360 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count);
1362 #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
1363 if (hdr->res.verf->committed < hdr->args.stable &&
1364 task->tk_status >= 0) {
1365 /* We tried a write call, but the server did not
1366 * commit data to stable storage even though we
1368 * Note: There is a known bug in Tru64 < 5.0 in which
1369 * the server reports NFS_DATA_SYNC, but performs
1370 * NFS_FILE_SYNC. We therefore implement this checking
1371 * as a dprintk() in order to avoid filling syslog.
1373 static unsigned long complain;
1375 /* Note this will print the MDS for a DS write */
1376 if (time_before(complain, jiffies)) {
1377 dprintk("NFS: faulty NFS server %s:"
1378 " (committed = %d) != (stable = %d)\n",
1379 NFS_SERVER(inode)->nfs_client->cl_hostname,
1380 hdr->res.verf->committed, hdr->args.stable);
1381 complain = jiffies + 300 * HZ;
1386 /* Deal with the suid/sgid bit corner case */
1387 if (nfs_should_remove_suid(inode))
1388 nfs_mark_for_revalidate(inode);
1393 * This function is called when the WRITE call is complete.
1395 static void nfs_writeback_result(struct rpc_task *task,
1396 struct nfs_pgio_header *hdr)
1398 struct nfs_pgio_args *argp = &hdr->args;
1399 struct nfs_pgio_res *resp = &hdr->res;
1401 if (resp->count < argp->count) {
1402 static unsigned long complain;
1404 /* This a short write! */
1405 nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE);
1407 /* Has the server at least made some progress? */
1408 if (resp->count == 0) {
1409 if (time_before(complain, jiffies)) {
1411 "NFS: Server wrote zero bytes, expected %u.\n",
1413 complain = jiffies + 300 * HZ;
1415 nfs_set_pgio_error(hdr, -EIO, argp->offset);
1416 task->tk_status = -EIO;
1419 /* Was this an NFSv2 write or an NFSv3 stable write? */
1420 if (resp->verf->committed != NFS_UNSTABLE) {
1421 /* Resend from where the server left off */
1422 hdr->mds_offset += resp->count;
1423 argp->offset += resp->count;
1424 argp->pgbase += resp->count;
1425 argp->count -= resp->count;
1427 /* Resend as a stable write in order to avoid
1428 * headaches in the case of a server crash.
1430 argp->stable = NFS_FILE_SYNC;
1432 rpc_restart_call_prepare(task);
1437 #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
1438 static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
1442 if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
1446 ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1448 nfs_wait_bit_killable,
1450 return (ret < 0) ? ret : 1;
1453 static void nfs_commit_clear_lock(struct nfs_inode *nfsi)
1455 clear_bit(NFS_INO_COMMIT, &nfsi->flags);
1456 smp_mb__after_atomic();
1457 wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
1460 void nfs_commitdata_release(struct nfs_commit_data *data)
1462 put_nfs_open_context(data->context);
1463 nfs_commit_free(data);
1465 EXPORT_SYMBOL_GPL(nfs_commitdata_release);
1467 int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
1468 const struct rpc_call_ops *call_ops,
1471 struct rpc_task *task;
1472 int priority = flush_task_priority(how);
1473 struct rpc_message msg = {
1474 .rpc_argp = &data->args,
1475 .rpc_resp = &data->res,
1476 .rpc_cred = data->cred,
1478 struct rpc_task_setup task_setup_data = {
1479 .task = &data->task,
1481 .rpc_message = &msg,
1482 .callback_ops = call_ops,
1483 .callback_data = data,
1484 .workqueue = nfsiod_workqueue,
1485 .flags = RPC_TASK_ASYNC | flags,
1486 .priority = priority,
1488 /* Set up the initial task struct. */
1489 NFS_PROTO(data->inode)->commit_setup(data, &msg);
1491 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1493 nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client,
1494 NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg);
1496 task = rpc_run_task(&task_setup_data);
1498 return PTR_ERR(task);
1499 if (how & FLUSH_SYNC)
1500 rpc_wait_for_completion_task(task);
1504 EXPORT_SYMBOL_GPL(nfs_initiate_commit);
1507 * Set up the argument/result storage required for the RPC call.
1509 void nfs_init_commit(struct nfs_commit_data *data,
1510 struct list_head *head,
1511 struct pnfs_layout_segment *lseg,
1512 struct nfs_commit_info *cinfo)
1514 struct nfs_page *first = nfs_list_entry(head->next);
1515 struct inode *inode = first->wb_context->dentry->d_inode;
1517 /* Set up the RPC argument and reply structs
1518 * NB: take care not to mess about with data->commit et al. */
1520 list_splice_init(head, &data->pages);
1522 data->inode = inode;
1523 data->cred = first->wb_context->cred;
1524 data->lseg = lseg; /* reference transferred */
1525 data->mds_ops = &nfs_commit_ops;
1526 data->completion_ops = cinfo->completion_ops;
1527 data->dreq = cinfo->dreq;
1529 data->args.fh = NFS_FH(data->inode);
1530 /* Note: we always request a commit of the entire inode */
1531 data->args.offset = 0;
1532 data->args.count = 0;
1533 data->context = get_nfs_open_context(first->wb_context);
1534 data->res.fattr = &data->fattr;
1535 data->res.verf = &data->verf;
1536 nfs_fattr_init(&data->fattr);
1538 EXPORT_SYMBOL_GPL(nfs_init_commit);
1540 void nfs_retry_commit(struct list_head *page_list,
1541 struct pnfs_layout_segment *lseg,
1542 struct nfs_commit_info *cinfo)
1544 struct nfs_page *req;
1546 while (!list_empty(page_list)) {
1547 req = nfs_list_entry(page_list->next);
1548 nfs_list_remove_request(req);
1549 nfs_mark_request_commit(req, lseg, cinfo);
1551 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1552 dec_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
1555 nfs_unlock_and_release_request(req);
1558 EXPORT_SYMBOL_GPL(nfs_retry_commit);
1561 * Commit dirty pages
1564 nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1565 struct nfs_commit_info *cinfo)
1567 struct nfs_commit_data *data;
1569 data = nfs_commitdata_alloc();
1574 /* Set up the argument struct */
1575 nfs_init_commit(data, head, NULL, cinfo);
1576 atomic_inc(&cinfo->mds->rpcs_out);
1577 return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops,
1580 nfs_retry_commit(head, NULL, cinfo);
1581 cinfo->completion_ops->error_cleanup(NFS_I(inode));
1586 * COMMIT call returned
1588 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1590 struct nfs_commit_data *data = calldata;
1592 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1593 task->tk_pid, task->tk_status);
1595 /* Call the NFS version-specific code */
1596 NFS_PROTO(data->inode)->commit_done(task, data);
1599 static void nfs_commit_release_pages(struct nfs_commit_data *data)
1601 struct nfs_page *req;
1602 int status = data->task.tk_status;
1603 struct nfs_commit_info cinfo;
1605 while (!list_empty(&data->pages)) {
1606 req = nfs_list_entry(data->pages.next);
1607 nfs_list_remove_request(req);
1608 nfs_clear_page_commit(req->wb_page);
1610 dprintk("NFS: commit (%s/%llu %d@%lld)",
1611 req->wb_context->dentry->d_sb->s_id,
1612 (unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1614 (long long)req_offset(req));
1616 nfs_context_set_write_error(req->wb_context, status);
1617 nfs_inode_remove_request(req);
1618 dprintk(", error = %d\n", status);
1622 /* Okay, COMMIT succeeded, apparently. Check the verifier
1623 * returned by the server against all stored verfs. */
1624 if (!memcmp(&req->wb_verf, &data->verf.verifier, sizeof(req->wb_verf))) {
1625 /* We have a match */
1626 nfs_inode_remove_request(req);
1630 /* We have a mismatch. Write the page again */
1631 dprintk(" mismatch\n");
1632 nfs_mark_request_dirty(req);
1633 set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
1635 nfs_unlock_and_release_request(req);
1637 nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1638 if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
1639 nfs_commit_clear_lock(NFS_I(data->inode));
1642 static void nfs_commit_release(void *calldata)
1644 struct nfs_commit_data *data = calldata;
1646 data->completion_ops->completion(data);
1647 nfs_commitdata_release(calldata);
1650 static const struct rpc_call_ops nfs_commit_ops = {
1651 .rpc_call_prepare = nfs_commit_prepare,
1652 .rpc_call_done = nfs_commit_done,
1653 .rpc_release = nfs_commit_release,
1656 static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1657 .completion = nfs_commit_release_pages,
1658 .error_cleanup = nfs_commit_clear_lock,
1661 int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1662 int how, struct nfs_commit_info *cinfo)
1666 status = pnfs_commit_list(inode, head, how, cinfo);
1667 if (status == PNFS_NOT_ATTEMPTED)
1668 status = nfs_commit_list(inode, head, how, cinfo);
1672 int nfs_commit_inode(struct inode *inode, int how)
1675 struct nfs_commit_info cinfo;
1676 int may_wait = how & FLUSH_SYNC;
1679 res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1681 goto out_mark_dirty;
1682 nfs_init_cinfo_from_inode(&cinfo, inode);
1683 res = nfs_scan_commit(inode, &head, &cinfo);
1687 error = nfs_generic_commit_list(inode, &head, how, &cinfo);
1691 goto out_mark_dirty;
1692 error = wait_on_bit(&NFS_I(inode)->flags,
1694 nfs_wait_bit_killable,
1699 nfs_commit_clear_lock(NFS_I(inode));
1701 /* Note: If we exit without ensuring that the commit is complete,
1702 * we must mark the inode as dirty. Otherwise, future calls to
1703 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1704 * that the data is on the disk.
1707 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
1711 static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
1713 struct nfs_inode *nfsi = NFS_I(inode);
1714 int flags = FLUSH_SYNC;
1717 /* no commits means nothing needs to be done */
1718 if (!nfsi->commit_info.ncommit)
1721 if (wbc->sync_mode == WB_SYNC_NONE) {
1722 /* Don't commit yet if this is a non-blocking flush and there
1723 * are a lot of outstanding writes for this mapping.
1725 if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1))
1726 goto out_mark_dirty;
1728 /* don't wait for the COMMIT response */
1732 ret = nfs_commit_inode(inode, flags);
1734 if (wbc->sync_mode == WB_SYNC_NONE) {
1735 if (ret < wbc->nr_to_write)
1736 wbc->nr_to_write -= ret;
1738 wbc->nr_to_write = 0;
1743 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
1747 static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
1753 int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
1755 return nfs_commit_unstable_pages(inode, wbc);
1757 EXPORT_SYMBOL_GPL(nfs_write_inode);
1760 * flush the inode to disk.
1762 int nfs_wb_all(struct inode *inode)
1764 struct writeback_control wbc = {
1765 .sync_mode = WB_SYNC_ALL,
1766 .nr_to_write = LONG_MAX,
1768 .range_end = LLONG_MAX,
1772 trace_nfs_writeback_inode_enter(inode);
1774 ret = sync_inode(inode, &wbc);
1776 trace_nfs_writeback_inode_exit(inode, ret);
1779 EXPORT_SYMBOL_GPL(nfs_wb_all);
1781 int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1783 struct nfs_page *req;
1786 wait_on_page_writeback(page);
1788 /* blocking call to cancel all requests and join to a single (head)
1790 req = nfs_lock_and_join_requests(page, false);
1795 /* all requests from this page have been cancelled by
1796 * nfs_lock_and_join_requests, so just remove the head
1797 * request from the inode / page_private pointer and
1799 nfs_inode_remove_request(req);
1801 * In case nfs_inode_remove_request has marked the
1802 * page as being dirty
1804 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1805 nfs_unlock_and_release_request(req);
1812 * Write back all requests on one page - we do this before reading it.
1814 int nfs_wb_page(struct inode *inode, struct page *page)
1816 loff_t range_start = page_file_offset(page);
1817 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1818 struct writeback_control wbc = {
1819 .sync_mode = WB_SYNC_ALL,
1821 .range_start = range_start,
1822 .range_end = range_end,
1826 trace_nfs_writeback_page_enter(inode);
1829 wait_on_page_writeback(page);
1830 if (clear_page_dirty_for_io(page)) {
1831 ret = nfs_writepage_locked(page, &wbc);
1837 if (!PagePrivate(page))
1839 ret = nfs_commit_inode(inode, FLUSH_SYNC);
1844 trace_nfs_writeback_page_exit(inode, ret);
1848 #ifdef CONFIG_MIGRATION
1849 int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1850 struct page *page, enum migrate_mode mode)
1853 * If PagePrivate is set, then the page is currently associated with
1854 * an in-progress read or write request. Don't try to migrate it.
1856 * FIXME: we could do this in principle, but we'll need a way to ensure
1857 * that we can safely release the inode reference while holding
1860 if (PagePrivate(page))
1863 if (!nfs_fscache_release_page(page, GFP_KERNEL))
1866 return migrate_page(mapping, newpage, page, mode);
1870 int __init nfs_init_writepagecache(void)
1872 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1873 sizeof(struct nfs_pgio_header),
1874 0, SLAB_HWCACHE_ALIGN,
1876 if (nfs_wdata_cachep == NULL)
1879 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1881 if (nfs_wdata_mempool == NULL)
1882 goto out_destroy_write_cache;
1884 nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
1885 sizeof(struct nfs_commit_data),
1886 0, SLAB_HWCACHE_ALIGN,
1888 if (nfs_cdata_cachep == NULL)
1889 goto out_destroy_write_mempool;
1891 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1893 if (nfs_commit_mempool == NULL)
1894 goto out_destroy_commit_cache;
1897 * NFS congestion size, scale with available memory.
1909 * This allows larger machines to have larger/more transfers.
1910 * Limit the default to 256M
1912 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1913 if (nfs_congestion_kb > 256*1024)
1914 nfs_congestion_kb = 256*1024;
1918 out_destroy_commit_cache:
1919 kmem_cache_destroy(nfs_cdata_cachep);
1920 out_destroy_write_mempool:
1921 mempool_destroy(nfs_wdata_mempool);
1922 out_destroy_write_cache:
1923 kmem_cache_destroy(nfs_wdata_cachep);
1927 void nfs_destroy_writepagecache(void)
1929 mempool_destroy(nfs_commit_mempool);
1930 kmem_cache_destroy(nfs_cdata_cachep);
1931 mempool_destroy(nfs_wdata_mempool);
1932 kmem_cache_destroy(nfs_wdata_cachep);
1935 static const struct nfs_rw_ops nfs_rw_write_ops = {
1936 .rw_mode = FMODE_WRITE,
1937 .rw_alloc_header = nfs_writehdr_alloc,
1938 .rw_free_header = nfs_writehdr_free,
1939 .rw_release = nfs_writeback_release_common,
1940 .rw_done = nfs_writeback_done,
1941 .rw_result = nfs_writeback_result,
1942 .rw_initiate = nfs_initiate_write,