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

Commit 5d5e103a authored by Josef Bacik's avatar Josef Bacik Committed by Chris Mason
Browse files

Btrfs: fix possible ENOSPC problems with truncate



There's a problem where we don't do any space reservation for truncates, which
can cause you to OOPs because you will be allowed to go off in the weeds a bit
since we don't account for the delalloc bytes that are created as a result of
the truncate.

Signed-off-by: default avatarJosef Bacik <jbacik@redhat.com>
Signed-off-by: default avatarChris Mason <chris.mason@oracle.com>
parent 0eda294d
Loading
Loading
Loading
Loading
+24 −3
Original line number Diff line number Diff line
@@ -3032,12 +3032,22 @@ static int btrfs_truncate_page(struct address_space *mapping, loff_t from)

	if ((offset & (blocksize - 1)) == 0)
		goto out;
	ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
	if (ret)
		goto out;

	ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
	if (ret)
		goto out;

	ret = -ENOMEM;
again:
	page = grab_cache_page(mapping, index);
	if (!page)
	if (!page) {
		btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
		btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
		goto out;
	}

	page_start = page_offset(page);
	page_end = page_start + PAGE_CACHE_SIZE - 1;
@@ -3070,6 +3080,10 @@ static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
		goto again;
	}

	clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
			  EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
			  GFP_NOFS);

	ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
	if (ret) {
		unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
@@ -3088,6 +3102,9 @@ static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
	unlock_extent(io_tree, page_start, page_end, GFP_NOFS);

out_unlock:
	if (ret)
		btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
	btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
	unlock_page(page);
	page_cache_release(page);
out:
@@ -3111,7 +3128,9 @@ int btrfs_cont_expand(struct inode *inode, loff_t size)
	if (size <= hole_start)
		return 0;

	btrfs_truncate_page(inode->i_mapping, inode->i_size);
	err = btrfs_truncate_page(inode->i_mapping, inode->i_size);
	if (err)
		return err;

	while (1) {
		struct btrfs_ordered_extent *ordered;
@@ -5008,7 +5027,9 @@ static void btrfs_truncate(struct inode *inode)
	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
		return;

	btrfs_truncate_page(inode->i_mapping, inode->i_size);
	ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
	if (ret)
		return;
	btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);

	trans = btrfs_start_transaction(root, 1);