Skip to contentSkip to conversation

Btrfs: add missing inode item update in fallocate()

PATCH Superseded Filipe Manana 3 participants 6 messages Mar 12, 2015 – Mar 14, 2015 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Btrfs: add missing inode item update in fallocate()

If we fallocate(), without the keep size flag, into an area already covered by an extent previously fallocated, we were updating the inode's i_size but we weren't updating the inode item in the fs/subvol tree. A following umount
+ mount would result in a loss of the inode's size (and an fsync would miss too the fact that the inode changed).

Reproducer:

  $ mkfs.btrfs -f /dev/sdd
  $ mount /dev/sdd /mnt
  $ fallocate -n -l 1M /mnt/foobar
  $ fallocate -l 512K /mnt/foobar
  $ umount /mnt
  $ mount /dev/sdd /mnt
  $ od -t x1 /mnt/foobar
  0000000

The expected result is:

  $ od -t x1 /mnt/foobar
  0000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  *
  2000000

A test case for fstests follows soon.

---
 fs/btrfs/file.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
fs/btrfs/file.c +16 -5
index 8f256b1..fb4bd79 100644
@@ -2677,13 +2677,10 @@ static long btrfs_fallocate(struct file *file, int mode,
 							1 << inode->i_blkbits,
 							offset + len,
 							&alloc_hint);
-
-			if (ret < 0) {
-				free_extent_map(em);
-				break;
-			}
 		} else if (actual_end > inode->i_size &&
 			   !(mode & FALLOC_FL_KEEP_SIZE)) {
+			struct btrfs_trans_handle *trans;
+
 			/*
 			 * We didn't need to allocate any more space, but we
 			 * still extended the size of the file so we need to
@@ -2692,8 +2689,22 @@ static long btrfs_fallocate(struct file *file, int mode,
 			inode->i_ctime = CURRENT_TIME;
 			i_size_write(inode, actual_end);
 			btrfs_ordered_update_i_size(inode, actual_end, NULL);
+			/* 1 unit for inode item */
+			trans = btrfs_start_transaction(root, 1);
+			if (IS_ERR(trans)) {
+				ret = PTR_ERR(trans);
+			} else {
+				ret = btrfs_update_inode(trans, root, inode);
+				if (ret)
+					btrfs_end_transaction(trans, root);
+				else
+					ret = btrfs_end_transaction(trans,
+								    root);
+			}
 		}
 		free_extent_map(em);
+		if (ret < 0)
+			break;
 
 		cur_offset = last_byte;
 		if (cur_offset >= alloc_end) {
Signature · 1 line
2.1.3
#1 lore raw
Quoted 52 lines · Filipe Manana

If we fallocate(), without the keep size flag, into an area already covered by an extent previously fallocated, we were updating the inode's i_size but we weren't updating the inode item in the fs/subvol tree. A following umount
+ mount would result in a loss of the inode's size (and an fsync would miss too the fact that the inode changed).

Reproducer:

  $ mkfs.btrfs -f /dev/sdd
  $ mount /dev/sdd /mnt
  $ fallocate -n -l 1M /mnt/foobar
  $ fallocate -l 512K /mnt/foobar
  $ umount /mnt
  $ mount /dev/sdd /mnt
  $ od -t x1 /mnt/foobar
  0000000

The expected result is:

  $ od -t x1 /mnt/foobar
  0000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  *
  2000000

A test case for fstests follows soon.

---
 fs/btrfs/file.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
fs/btrfs/file.c +2 -5
index 8f256b1..fb4bd79 100644
@@ -2677,13 +2677,10 @@ static long btrfs_fallocate(struct file *file, int mode,
 							1 << inode->i_blkbits,
 							offset + len,
 							&alloc_hint);
-
-			if (ret < 0) {
-				free_extent_map(em);
-				break;
-			}
 		} else if (actual_end > inode->i_size &&
 			   !(mode & FALLOC_FL_KEEP_SIZE)) {
+			struct btrfs_trans_handle *trans;
+
 			/*
 			 * We didn't need to allocate any more space, but we
 			 * still extended the size of the file so we need to

fs/btrfs/file.c static long btrfs_fallocate(struct file *file, int mode, · line 2695

Quoted 7 lines
fs/btrfs/file.c +4 -0
@@ -2692,8 +2689,22 @@ static long btrfs_fallocate(struct file *file, int mode,
 			inode->i_ctime = CURRENT_TIME;
 			i_size_write(inode, actual_end);
 			btrfs_ordered_update_i_size(inode, actual_end, NULL);
+			/* 1 unit for inode item */
+			trans = btrfs_start_transaction(root, 1);
+			if (IS_ERR(trans)) {
+				ret = PTR_ERR(trans);

I prefer putting this earlier, before i_size updates, to
protect us from another isize inconsistence(disk_i_size, isize vs isize in inode_item).

Basically I mean,

			...
			trans = btrfs_start_transaction(root, 1);
			if (IS_ERR(trans)) {
				ret = PTR_ERR(trans);
			} else {
	  			inode->i_ctime = CURRENT_TIME;
				...
			}

fs/btrfs/file.c static long btrfs_fallocate(struct file *file, int mode, · line 2702

Quoted 7 lines
fs/btrfs/file.c +7 -0
+			} else {
+				ret = btrfs_update_inode(trans, root, inode);
+				if (ret)
+					btrfs_end_transaction(trans, root);
+				else
+					ret = btrfs_end_transaction(trans,
+								    root);

calling same end_transaction() for two times seems kind of weird for me, what about this?

ret2 = btrfs_end_transaction(trans, root);
if (!ret)

	ret = ret2;

But anyway, I'm okay with both.

Thanks,

-liubo

Quoted 14 lines
fs/btrfs/file.c +2 -0
 		}
 		free_extent_map(em);
+		if (ret < 0)
+			break;
 
 		cur_offset = last_byte;
 		if (cur_offset >= alloc_end) {
Signature · 6 lines
2.1.3

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw
Quoted 55 lines · Liu Bo
Quoted 54 lines · Filipe Manana

If we fallocate(), without the keep size flag, into an area already covered by an extent previously fallocated, we were updating the inode's i_size but we weren't updating the inode item in the fs/subvol tree. A following umount
+ mount would result in a loss of the inode's size (and an fsync would miss too the fact that the inode changed).

Reproducer:

  $ mkfs.btrfs -f /dev/sdd
  $ mount /dev/sdd /mnt
  $ fallocate -n -l 1M /mnt/foobar
  $ fallocate -l 512K /mnt/foobar
  $ umount /mnt
  $ mount /dev/sdd /mnt
  $ od -t x1 /mnt/foobar
  0000000

The expected result is:

  $ od -t x1 /mnt/foobar
  0000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  *
  2000000

A test case for fstests follows soon.

---
 fs/btrfs/file.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
fs/btrfs/file.c +2 -5
index 8f256b1..fb4bd79 100644
@@ -2677,13 +2677,10 @@ static long btrfs_fallocate(struct file *file, int mode,
                                                      1 << inode->i_blkbits,
                                                      offset + len,
                                                      &alloc_hint);
-
-                     if (ret < 0) {
-                             free_extent_map(em);
-                             break;
-                     }
              } else if (actual_end > inode->i_size &&
                         !(mode & FALLOC_FL_KEEP_SIZE)) {
+                     struct btrfs_trans_handle *trans;
+
                      /*
                       * We didn't need to allocate any more space, but we
                       * still extended the size of the file so we need to
@@ -2692,8 +2689,22 @@ static long btrfs_fallocate(struct file *file, int mode,
                      inode->i_ctime = CURRENT_TIME;
                      i_size_write(inode, actual_end);
Quoted 8 lines

fs/btrfs/file.c static long btrfs_fallocate(struct file *file, int mode, · line 2695

Quoted 5 lines
fs/btrfs/file.c +4 -0
                      btrfs_ordered_update_i_size(inode, actual_end, NULL);
+                     /* 1 unit for inode item */
+                     trans = btrfs_start_transaction(root, 1);
+                     if (IS_ERR(trans)) {
+                             ret = PTR_ERR(trans);

I prefer putting this earlier, before i_size updates, to
protect us from another isize inconsistence(disk_i_size, isize vs isize in inode_item).

I don't think that's a problem at all.
Updating i_size (and all other btrfs_inode fields) before updating the inode item is done pretty much everywhere (examples:
__btrfs_prealloc_file_range, btrfs_finish_ordered_io, etc, etc).

So unless you can point exactly how it can become a problem, I'll leave as it is.

Quoted 19 lines

Basically I mean,

                        ...
                        trans = btrfs_start_transaction(root, 1);
                        if (IS_ERR(trans)) {
                                ret = PTR_ERR(trans);
                        } else {
                                inode->i_ctime = CURRENT_TIME;
                                ...
                        }

fs/btrfs/file.c static long btrfs_fallocate(struct file *file, int mode, · line 2702

Quoted 7 lines
fs/btrfs/file.c +7 -0
+                     } else {
+                             ret = btrfs_update_inode(trans, root, inode);
+                             if (ret)
+                                     btrfs_end_transaction(trans, root);
+                             else
+                                     ret = btrfs_end_transaction(trans,
+                                                                 root);
Quoted 2 lines

calling same end_transaction() for two times seems kind of weird for me, what about this?

It's called one time.

Quoted 4 lines

ret2 = btrfs_end_transaction(trans, root);
if (!ret)

        ret = ret2;

Doesn't change correctness at all, purely stylistic choice.

thanks

Quoted 25 lines

But anyway, I'm okay with both.

Thanks,

-liubo

Quoted 10 lines
fs/btrfs/file.c +2 -1
              }
              free_extent_map(em);
+             if (ret < 0)
+                     break;
 
              cur_offset = last_byte;
              if (cur_offset >= alloc_end) {
--

2.1.3

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 5 lines
Filipe David Manana,

"Reasonable men adapt themselves to the world.
 Unreasonable men adapt the world to themselves.
 That's why all progress depends on unreasonable men."
#3 lore raw
Quoted 65 lines · Filipe David Manana
Quoted 55 lines · Liu Bo
Quoted 54 lines · Filipe Manana

If we fallocate(), without the keep size flag, into an area already covered by an extent previously fallocated, we were updating the inode's i_size but we weren't updating the inode item in the fs/subvol tree. A following umount
+ mount would result in a loss of the inode's size (and an fsync would miss too the fact that the inode changed).

Reproducer:

  $ mkfs.btrfs -f /dev/sdd
  $ mount /dev/sdd /mnt
  $ fallocate -n -l 1M /mnt/foobar
  $ fallocate -l 512K /mnt/foobar
  $ umount /mnt
  $ mount /dev/sdd /mnt
  $ od -t x1 /mnt/foobar
  0000000

The expected result is:

  $ od -t x1 /mnt/foobar
  0000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  *
  2000000

A test case for fstests follows soon.

---
 fs/btrfs/file.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
fs/btrfs/file.c +2 -5
index 8f256b1..fb4bd79 100644
@@ -2677,13 +2677,10 @@ static long btrfs_fallocate(struct file *file, int mode,
                                                      1 << inode->i_blkbits,
                                                      offset + len,
                                                      &alloc_hint);
-
-                     if (ret < 0) {
-                             free_extent_map(em);
-                             break;
-                     }
              } else if (actual_end > inode->i_size &&
                         !(mode & FALLOC_FL_KEEP_SIZE)) {
+                     struct btrfs_trans_handle *trans;
+
                      /*
                       * We didn't need to allocate any more space, but we
                       * still extended the size of the file so we need to
@@ -2692,8 +2689,22 @@ static long btrfs_fallocate(struct file *file, int mode,
                      inode->i_ctime = CURRENT_TIME;
                      i_size_write(inode, actual_end);
Quoted 8 lines

fs/btrfs/file.c static long btrfs_fallocate(struct file *file, int mode, · line 2695

Quoted 5 lines
fs/btrfs/file.c +4 -0
                      btrfs_ordered_update_i_size(inode, actual_end, NULL);
+                     /* 1 unit for inode item */
+                     trans = btrfs_start_transaction(root, 1);
+                     if (IS_ERR(trans)) {
+                             ret = PTR_ERR(trans);

I prefer putting this earlier, before i_size updates, to
protect us from another isize inconsistence(disk_i_size, isize vs isize in inode_item).

Quoted 4 lines

I don't think that's a problem at all.
Updating i_size (and all other btrfs_inode fields) before updating the inode item is done pretty much everywhere (examples:
__btrfs_prealloc_file_range, btrfs_finish_ordered_io, etc, etc).

So re-reading this, you meant starting the transaction before doing the size update and not doing the inode item update before updating the i_size. So that's ok for me.

Quoted 71 lines

So unless you can point exactly how it can become a problem, I'll leave as it is.

Quoted 19 lines

Basically I mean,

                        ...
                        trans = btrfs_start_transaction(root, 1);
                        if (IS_ERR(trans)) {
                                ret = PTR_ERR(trans);
                        } else {
                                inode->i_ctime = CURRENT_TIME;
                                ...
                        }

fs/btrfs/file.c static long btrfs_fallocate(struct file *file, int mode, · line 2702

Quoted 7 lines
fs/btrfs/file.c +7 -0
+                     } else {
+                             ret = btrfs_update_inode(trans, root, inode);
+                             if (ret)
+                                     btrfs_end_transaction(trans, root);
+                             else
+                                     ret = btrfs_end_transaction(trans,
+                                                                 root);
Quoted 2 lines

calling same end_transaction() for two times seems kind of weird for me, what about this?

It's called one time.

Quoted 4 lines

ret2 = btrfs_end_transaction(trans, root);
if (!ret)

        ret = ret2;

Doesn't change correctness at all, purely stylistic choice.

thanks

Quoted 17 lines

But anyway, I'm okay with both.

Thanks,

-liubo

Quoted 10 lines
fs/btrfs/file.c +2 -1
              }
              free_extent_map(em);
+             if (ret < 0)
+                     break;
 
              cur_offset = last_byte;
              if (cur_offset >= alloc_end) {
--

2.1.3

Quoted 8 lines
Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
Filipe David Manana,

"Reasonable men adapt themselves to the world.
Unreasonable men adapt the world to themselves. That's why all progress depends on unreasonable men."

Signature · 5 lines
Filipe David Manana,

"Reasonable men adapt themselves to the world.
 Unreasonable men adapt the world to themselves.
 That's why all progress depends on unreasonable men."
#4 lore raw

PATCH v2 1/1 Btrfs: add missing inode item update in fallocate()

In reply to Filipe Manana

If we fallocate(), without the keep size flag, into an area already covered by an extent previously fallocated, we were updating the inode's i_size but we weren't updating the inode item in the fs/subvol tree. A following umount
+ mount would result in a loss of the inode's size (and an fsync would miss too the fact that the inode changed).

Reproducer:

  $ mkfs.btrfs -f /dev/sdd
  $ mount /dev/sdd /mnt
  $ fallocate -n -l 1M /mnt/foobar
  $ fallocate -l 512K /mnt/foobar
  $ umount /mnt
  $ mount /dev/sdd /mnt
  $ od -t x1 /mnt/foobar
  0000000

The expected result is:

  $ od -t x1 /mnt/foobar
  0000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  *
  2000000

A test case for fstests follows soon.

---

V2: Update the inode's sizes and ctime while holding a transaction
open.

 fs/btrfs/file.c | 29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)
fs/btrfs/file.c +20 -9
index 8f256b1..c261ff0 100644
@@ -2677,23 +2677,34 @@ static long btrfs_fallocate(struct file *file, int mode,
 							1 << inode->i_blkbits,
 							offset + len,
 							&alloc_hint);
-
-			if (ret < 0) {
-				free_extent_map(em);
-				break;
-			}
 		} else if (actual_end > inode->i_size &&
 			   !(mode & FALLOC_FL_KEEP_SIZE)) {
+			struct btrfs_trans_handle *trans;
+
 			/*
 			 * We didn't need to allocate any more space, but we
 			 * still extended the size of the file so we need to
-			 * update i_size.
+			 * update i_size and the inode item.
 			 */
-			inode->i_ctime = CURRENT_TIME;
-			i_size_write(inode, actual_end);
-			btrfs_ordered_update_i_size(inode, actual_end, NULL);
+			trans = btrfs_start_transaction(root, 1);
+			if (IS_ERR(trans)) {
+				ret = PTR_ERR(trans);
+			} else {
+				inode->i_ctime = CURRENT_TIME;
+				i_size_write(inode, actual_end);
+				btrfs_ordered_update_i_size(inode, actual_end,
+							    NULL);
+				ret = btrfs_update_inode(trans, root, inode);
+				if (ret)
+					btrfs_end_transaction(trans, root);
+				else
+					ret = btrfs_end_transaction(trans,
+								    root);
+			}
 		}
 		free_extent_map(em);
+		if (ret < 0)
+			break;
 
 		cur_offset = last_byte;
 		if (cur_offset >= alloc_end) {
Signature · 1 line
2.1.3
Reviewed-by on v2 1/1
#5 lore raw
Quoted 17 lines · Filipe Manana

If we fallocate(), without the keep size flag, into an area already covered by an extent previously fallocated, we were updating the inode's i_size but we weren't updating the inode item in the fs/subvol tree. A following umount
+ mount would result in a loss of the inode's size (and an fsync would miss too the fact that the inode changed).

Reproducer:

  $ mkfs.btrfs -f /dev/sdd
  $ mount /dev/sdd /mnt
  $ fallocate -n -l 1M /mnt/foobar
  $ fallocate -l 512K /mnt/foobar
  $ umount /mnt
  $ mount /dev/sdd /mnt
  $ od -t x1 /mnt/foobar
  0000000
Quoted 8 lines

The expected result is:

  $ od -t x1 /mnt/foobar
  0000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  *
  2000000

A test case for fstests follows soon.

Looks good.

Quoted 65 lines

---

V2: Update the inode's sizes and ctime while holding a transaction
open.

 fs/btrfs/file.c | 29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)
fs/btrfs/file.c +20 -9
index 8f256b1..c261ff0 100644
@@ -2677,23 +2677,34 @@ static long btrfs_fallocate(struct file *file, int mode,
 							1 << inode->i_blkbits,
 							offset + len,
 							&alloc_hint);
-
-			if (ret < 0) {
-				free_extent_map(em);
-				break;
-			}
 		} else if (actual_end > inode->i_size &&
 			   !(mode & FALLOC_FL_KEEP_SIZE)) {
+			struct btrfs_trans_handle *trans;
+
 			/*
 			 * We didn't need to allocate any more space, but we
 			 * still extended the size of the file so we need to
-			 * update i_size.
+			 * update i_size and the inode item.
 			 */
-			inode->i_ctime = CURRENT_TIME;
-			i_size_write(inode, actual_end);
-			btrfs_ordered_update_i_size(inode, actual_end, NULL);
+			trans = btrfs_start_transaction(root, 1);
+			if (IS_ERR(trans)) {
+				ret = PTR_ERR(trans);
+			} else {
+				inode->i_ctime = CURRENT_TIME;
+				i_size_write(inode, actual_end);
+				btrfs_ordered_update_i_size(inode, actual_end,
+							    NULL);
+				ret = btrfs_update_inode(trans, root, inode);
+				if (ret)
+					btrfs_end_transaction(trans, root);
+				else
+					ret = btrfs_end_transaction(trans,
+								    root);
+			}
 		}
 		free_extent_map(em);
+		if (ret < 0)
+			break;
 
 		cur_offset = last_byte;
 		if (cur_offset >= alloc_end) {
Signature · 6 lines
2.1.3

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html