Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3d850dd4 authored by Filipe Manana's avatar Filipe Manana Committed by Chris Mason
Browse files

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.

Signed-off-by: default avatarFilipe Manana <fdmanana@suse.com>
Reviewed-by: default avatarLiu Bo <bo.li.liu@oracle.com>
Signed-off-by: default avatarChris Mason <clm@fb.com>
parent 5f806c3a
Loading
Loading
Loading
Loading
+20 −9
Original line number Original line Diff line number Diff line
@@ -2669,23 +2669,34 @@ static long btrfs_fallocate(struct file *file, int mode,
							1 << inode->i_blkbits,
							1 << inode->i_blkbits,
							offset + len,
							offset + len,
							&alloc_hint);
							&alloc_hint);

			if (ret < 0) {
				free_extent_map(em);
				break;
			}
		} else if (actual_end > inode->i_size &&
		} else if (actual_end > inode->i_size &&
			   !(mode & FALLOC_FL_KEEP_SIZE)) {
			   !(mode & FALLOC_FL_KEEP_SIZE)) {
			struct btrfs_trans_handle *trans;

			/*
			/*
			 * We didn't need to allocate any more space, but we
			 * We didn't need to allocate any more space, but we
			 * still extended the size of the file so we need to
			 * still extended the size of the file so we need to
			 * update i_size.
			 * update i_size and the inode item.
			 */
			 */
			trans = btrfs_start_transaction(root, 1);
			if (IS_ERR(trans)) {
				ret = PTR_ERR(trans);
			} else {
				inode->i_ctime = CURRENT_TIME;
				inode->i_ctime = CURRENT_TIME;
				i_size_write(inode, actual_end);
				i_size_write(inode, actual_end);
			btrfs_ordered_update_i_size(inode, actual_end, NULL);
				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);
		free_extent_map(em);
		if (ret < 0)
			break;


		cur_offset = last_byte;
		cur_offset = last_byte;
		if (cur_offset >= alloc_end) {
		if (cur_offset >= alloc_end) {