linux-ext4.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ext4: issue WARN_ON_ONCE() if page gets truncated in ext4_write_[da_]begin()
@ 2015-05-07  9:17 Xiaoguang Wang
  2015-05-17 17:51 ` Theodore Ts'o
  0 siblings, 1 reply; 2+ messages in thread
From: Xiaoguang Wang @ 2015-05-07  9:17 UTC (permalink / raw)
  To: linux-ext4; +Cc: tytso, Xiaoguang Wang

In ext4_write_[da_]begin(), when we get a page successfully by calling
grab_cache_page_write_begin(), then unless this page is truncated by
ext4_truncate_failed_write(), which is called when ext4_write_[da_]begin()
run into some errors, otherwise I think this page won't be truncated by
other kernel path because we're holding i_mutex.

In this patch, if page is truncated by ext4_truncate_failed_write(), then
we just put it and call grab_cache_page_write_begin() again to get a new
page, then this check 'if (page->mapping != mapping)' won't be necessary.
We can remove this check, but according to Jan Kara's suggestion, we issue
WARN_ON_ONCE() here if page is truncated, that means something is wrong.
(Thanks to Jan Kara for his help)

Signed-off-by: Xiaoguang Wang <wangxg.fnst@cn.fujitsu.com>
---
 fs/ext4/inode.c | 35 ++++++++++++++++++++++++++---------
 1 file changed, 26 insertions(+), 9 deletions(-)

diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 4415cea..5961fdf 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -984,7 +984,7 @@ static int ext4_write_begin(struct file *file, struct address_space *mapping,
 	struct inode *inode = mapping->host;
 	int ret, needed_blocks;
 	handle_t *handle;
-	int retries = 0;
+	int retries = 0, page_truncated = 0;
 	struct page *page;
 	pgoff_t index;
 	unsigned from, to;
@@ -1029,7 +1029,7 @@ retry_journal:
 	}
 
 	lock_page(page);
-	if (page->mapping != mapping) {
+	if (WARN_ON_ONCE(page->mapping != mapping)) {
 		/* The page got truncated from under us */
 		unlock_page(page);
 		page_cache_release(page);
@@ -1074,6 +1074,7 @@ retry_journal:
 		ext4_journal_stop(handle);
 		if (pos + len > inode->i_size) {
 			ext4_truncate_failed_write(inode);
+			page_truncated = 1;
 			/*
 			 * If truncate failed early the inode might
 			 * still be on the orphan list; we need to
@@ -1085,8 +1086,15 @@ retry_journal:
 		}
 
 		if (ret == -ENOSPC &&
-		    ext4_should_retry_alloc(inode->i_sb, &retries))
-			goto retry_journal;
+		    ext4_should_retry_alloc(inode->i_sb, &retries)) {
+			if (page_truncated) {
+				page_cache_release(page);
+				page_truncated = 0;
+				goto retry_grab;
+			} else {
+				goto retry_journal;
+			}
+		}
 		page_cache_release(page);
 		return ret;
 	}
@@ -2609,7 +2617,7 @@ static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
 			       loff_t pos, unsigned len, unsigned flags,
 			       struct page **pagep, void **fsdata)
 {
-	int ret, retries = 0;
+	int ret, retries = 0, page_truncated = 0;
 	struct page *page;
 	pgoff_t index;
 	struct inode *inode = mapping->host;
@@ -2663,7 +2671,7 @@ retry_journal:
 	}
 
 	lock_page(page);
-	if (page->mapping != mapping) {
+	if (WARN_ON_ONCE(page->mapping != mapping)) {
 		/* The page got truncated from under us */
 		unlock_page(page);
 		page_cache_release(page);
@@ -2687,12 +2695,21 @@ retry_journal:
 		 * outside i_size.  Trim these off again. Don't need
 		 * i_size_read because we hold i_mutex.
 		 */
-		if (pos + len > inode->i_size)
+		if (pos + len > inode->i_size) {
 			ext4_truncate_failed_write(inode);
+			page_truncated = 1;
+		}
 
 		if (ret == -ENOSPC &&
-		    ext4_should_retry_alloc(inode->i_sb, &retries))
-			goto retry_journal;
+		    ext4_should_retry_alloc(inode->i_sb, &retries)) {
+			if (page_truncated) {
+				page_cache_release(page);
+				page_truncated = 0;
+				goto retry_grab;
+			} else {
+				goto retry_journal;
+			}
+		}
 
 		page_cache_release(page);
 		return ret;
-- 
1.8.3.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] ext4: issue WARN_ON_ONCE() if page gets truncated in ext4_write_[da_]begin()
  2015-05-07  9:17 [PATCH] ext4: issue WARN_ON_ONCE() if page gets truncated in ext4_write_[da_]begin() Xiaoguang Wang
@ 2015-05-17 17:51 ` Theodore Ts'o
  0 siblings, 0 replies; 2+ messages in thread
From: Theodore Ts'o @ 2015-05-17 17:51 UTC (permalink / raw)
  To: Xiaoguang Wang; +Cc: linux-ext4

On Thu, May 07, 2015 at 05:17:23PM +0800, Xiaoguang Wang wrote:
> In ext4_write_[da_]begin(), when we get a page successfully by calling
> grab_cache_page_write_begin(), then unless this page is truncated by
> ext4_truncate_failed_write(), which is called when ext4_write_[da_]begin()
> run into some errors, otherwise I think this page won't be truncated by
> other kernel path because we're holding i_mutex.
> 
> In this patch, if page is truncated by ext4_truncate_failed_write(), then
> we just put it and call grab_cache_page_write_begin() again to get a new
> page, then this check 'if (page->mapping != mapping)' won't be necessary.
> We can remove this check, but according to Jan Kara's suggestion, we issue
> WARN_ON_ONCE() here if page is truncated, that means something is wrong.
> (Thanks to Jan Kara for his help)

It would be helpful if the commit description explained why this
change is a benefit.  There's a very detailed explanation about why
it's safe, but why do we need to release the page only to try again to
grab it by jumping to retry_grab instead of retry_journal?  What
problem are you trying to solve?

Thanks, regards,

					- Ted

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-05-17 17:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-05-07  9:17 [PATCH] ext4: issue WARN_ON_ONCE() if page gets truncated in ext4_write_[da_]begin() Xiaoguang Wang
2015-05-17 17:51 ` Theodore Ts'o

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).