Skip to contentSkip to conversation

Btrfs: fix pages truncation in btrfs_ioctl_clone()

PATCH In review Li Zefan 3 participants 5 messages Sep 16, 2011 – Sep 19, 2011 Btrfs file system

lore
Share & tools
lore

Li Zefan

author
#0 lore raw

PATCH 1/1 Btrfs: fix pages truncation in btrfs_ioctl_clone()

It's a bug in commit f81c9cdc567cd3160ff9e64868d9a1a7ee226480 (Btrfs: truncate pages from clone ioctl target range)

We should pass the dest range to the truncate function, but not the src range.

Also move the function before locking extent state.

---
 fs/btrfs/ioctl.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
fs/btrfs/ioctl.c +4 -4
index 5492bb3..f6af8b0 100644
@@ -2237,6 +2237,10 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
 			goto out_unlock;
 	}
 
+	/* truncate page cache pages from target inode range */
+	truncate_inode_pages_range(&inode->i_data, destoff,
+				   PAGE_CACHE_ALIGN(destoff + len) - 1);
+
 	/* do any pending delalloc/csum calc on src, one way or
 	   another, and lock file content */
 	while (1) {
@@ -2253,10 +2257,6 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
 		btrfs_wait_ordered_range(src, off, len);
 	}
 
-	/* truncate page cache pages from target inode range */
-	truncate_inode_pages_range(&inode->i_data, off,
-				   ALIGN(off + len, PAGE_CACHE_SIZE) - 1);
-
 	/* clone data */
 	key.objectid = btrfs_ino(src);
 	key.type = BTRFS_EXTENT_DATA_KEY;
Signature · 1 line
1.7.3.1
#1 lore raw
Quoted 5 lines · Li Zefan

It's a bug in commit f81c9cdc567cd3160ff9e64868d9a1a7ee226480 (Btrfs: truncate pages from clone ioctl target range)

We should pass the dest range to the truncate function, but not the src range.

Sigh... yes.

Quoted 1 line

Also move the function before locking extent state.

Hmm, any reason? i_mutex protects us from a racing write(2), but what about a racing mmap()? e.g.

cloner: truncates dest pages
writer: mmap -> page_mkwrite locks extent, creates new dirty page, unlocks
cloner: locks extent, clones, unlocks extent

sage

Quoted 36 lines
---
 fs/btrfs/ioctl.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
fs/btrfs/ioctl.c +4 -4
index 5492bb3..f6af8b0 100644
@@ -2237,6 +2237,10 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
 			goto out_unlock;
 	}
 
+	/* truncate page cache pages from target inode range */
+	truncate_inode_pages_range(&inode->i_data, destoff,
+				   PAGE_CACHE_ALIGN(destoff + len) - 1);
+
 	/* do any pending delalloc/csum calc on src, one way or
 	   another, and lock file content */
 	while (1) {
@@ -2253,10 +2257,6 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
 		btrfs_wait_ordered_range(src, off, len);
 	}
 
-	/* truncate page cache pages from target inode range */
-	truncate_inode_pages_range(&inode->i_data, off,
-				   ALIGN(off + len, PAGE_CACHE_SIZE) - 1);
-
 	/* clone data */
 	key.objectid = btrfs_ino(src);
 	key.type = BTRFS_EXTENT_DATA_KEY;
Signature · 1 line
1.7.3.1
#2 lore raw

Excerpts from Sage Weil's message of 2011-09-16 12:39:06 -0400:

Quoted 9 lines · Li Zefan
Quoted 5 lines

It's a bug in commit f81c9cdc567cd3160ff9e64868d9a1a7ee226480 (Btrfs: truncate pages from clone ioctl target range)

We should pass the dest range to the truncate function, but not the src range.

Sigh... yes.

Quoted 8 lines
Quoted 1 line

Also move the function before locking extent state.

Hmm, any reason? i_mutex protects us from a racing write(2), but what about a racing mmap()? e.g.

cloner: truncates dest pages
writer: mmap -> page_mkwrite locks extent, creates new dirty page, unlocks
cloner: locks extent, clones, unlocks extent

Thanks guys. The locking order is page lock -> extent lock. So if we call truncate_inode_pages with the extent lock held, we're off into ABBA.

If we want to avoid the mmap race, we'll have to look for the dirty pages with the extent lock held, drop the lock and goto back to the truncate_inode_pages call.

-chris

#3 lore raw
Quoted 15 lines · Chris Mason

Excerpts from Sage Weil's message of 2011-09-16 12:39:06 -0400:

Quoted 14 lines · Li Zefan
Quoted 5 lines

It's a bug in commit f81c9cdc567cd3160ff9e64868d9a1a7ee226480 (Btrfs: truncate pages from clone ioctl target range)

We should pass the dest range to the truncate function, but not the src range.

Sigh... yes.

Quoted 1 line

Also move the function before locking extent state.

Hmm, any reason? i_mutex protects us from a racing write(2), but what about a racing mmap()? e.g.

Quoted 7 lines
Quoted 3 lines

cloner: truncates dest pages
writer: mmap -> page_mkwrite locks extent, creates new dirty page, unlocks
cloner: locks extent, clones, unlocks extent

Thanks guys. The locking order is page lock -> extent lock. So if we call truncate_inode_pages with the extent lock held, we're off into ABBA.

Oh right. This patch makes sense now.

Quoted 3 lines

If we want to avoid the mmap race, we'll have to look for the dirty pages with the extent lock held, drop the lock and goto back to the truncate_inode_pages call.

Yeah, given that (at Li points out) we're not locking dst extents at all, I don't think it's worth worrying about now. Sorry for the noise!

sage

author
#4 lore raw
In reply to Sage Weil
Quoted 9 lines
Quoted 1 line

Also move the function before locking extent state.

Hmm, any reason? i_mutex protects us from a racing write(2), but what about a racing mmap()? e.g.

cloner: truncates dest pages
writer: mmap -> page_mkwrite locks extent, creates new dirty page, unlocks
cloner: locks extent, clones, unlocks extent

(besides Chris' comments)

How can we avoid the race on dst file by locking src file extent state..

--
Li Zefan