Skip to contentSkip to conversation

Btrfs: fix data bytes_may_use underflow with fallocate due to failed quota reserve

PATCH Superseded robbieko 2 participants 4 messages Jan 14, 2019 – Mar 26, 2019 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

robbieko

author
#0 lore raw

PATCH 1/1 Btrfs: fix data bytes_may_use underflow with fallocate due to failed quota reserve

From: Robbie Ko <[email protected]>

When doing fallocate, we first add the range to the reserve_list and then reserve the qutoa.
If quota reservation fails, we'll release all reserved parts of reserve_list.
However, cur_offset doen't update to indicate that this range is already been inserted into the list.
Therefore, the same range is freed twice.
One at list_for_each_entry loop, and the other at the end of the function.
This will result in WARN_ON on bytes_may_use when we free the remaining space.

Fixes ("btrfs: update btrfs_space_info's bytes_may_use timely")
---
 fs/btrfs/file.c | 1 +
 1 file changed, 1 insertion(+)
fs/btrfs/file.c +1 -0
index d38dc8c..43c6c8a 100644
@@ -3132,6 +3132,7 @@ static long btrfs_fallocate(struct file *file, int mode,
 			ret = btrfs_qgroup_reserve_data(inode, &data_reserved,
 					cur_offset, last_byte - cur_offset);
 			if (ret < 0) {
+				cur_offset = last_byte;
 				free_extent_map(em);
 				break;
 			}
Signature · 1 line
1.9.1
author
#1 lore raw

Hi All,
Can someone help review the patch ?
Thanks.

robbieko 於 2019-01-14 11:15 寫到:

Quoted 33 lines

From: Robbie Ko <[email protected]>

When doing fallocate, we first add the range to the reserve_list and then reserve the qutoa.
If quota reservation fails, we'll release all reserved parts of reserve_list.
However, cur_offset doen't update to indicate that this range is already been inserted into the list.
Therefore, the same range is freed twice.
One at list_for_each_entry loop, and the other at the end of the function.
This will result in WARN_ON on bytes_may_use when we free the remaining space.

Fixes ("btrfs: update btrfs_space_info's bytes_may_use timely")
---
 fs/btrfs/file.c | 1 +
 1 file changed, 1 insertion(+)
fs/btrfs/file.c +1 -0
index d38dc8c..43c6c8a 100644
@@ -3132,6 +3132,7 @@ static long btrfs_fallocate(struct file *file, 
 int mode,
 			ret = btrfs_qgroup_reserve_data(inode, &data_reserved,
 					cur_offset, last_byte - cur_offset);
 			if (ret < 0) {
+				cur_offset = last_byte;
 				free_extent_map(em);
 				break;

}

#2 lore raw
In reply to robbieko
Quoted 5 lines · robbieko

From: Robbie Ko <[email protected]>

When doing fallocate, we first add the range to the reserve_list and then reserve the qutoa.

qutoa -> quota

Quoted 3 lines

If quota reservation fails, we'll release all reserved parts of reserve_list.
However, cur_offset doen't update to indicate that this range is

"doen't update" -> is not updated to indicate ...

Quoted 3 lines

already been inserted into the list.
Therefore, the same range is freed twice.
One at list_for_each_entry loop, and the other at the end of the

One -> once
and the other at -> and once at

Quoted 5 lines

function.
This will result in WARN_ON on bytes_may_use when we free the remaining space.

Fixes ("btrfs: update btrfs_space_info's bytes_may_use timely")

Missing commit id.

Quoted 10 lines
---
 fs/btrfs/file.c | 1 +
 1 file changed, 1 insertion(+)
fs/btrfs/file.c +0 -0
index d38dc8c..43c6c8a 100644

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

Quoted 4 lines
fs/btrfs/file.c +1 -0
@@ -3132,6 +3132,7 @@ static long btrfs_fallocate(struct file *file, int mode,
                        ret = btrfs_qgroup_reserve_data(inode, &data_reserved,
                                        cur_offset, last_byte - cur_offset);
                        if (ret < 0) {
+                               cur_offset = last_byte;

This will fix the problem if there's only one range, or we happen to be very lucky and cur_offset
matches exactly the middle of the entire range passed to fallocate() (in case the range covers
a sparse region of the file, a region with holes and extents).

At the end, under the 'out' label we have a call to:

btrfs_free_reserved_data_space(inode, data_reserved, alloc_start,
alloc_end - cur_offset);

That doesn't fell right - the start offset, third argument, should be cur_offset. Everything from
alloc_start to cur_offset was freed by the list_for_each_entry_safe_loop.

Quoted 6 lines
fs/btrfs/file.c +0 -1
                                free_extent_map(em);
                                break;
                        }
--

1.9.1

Signature · 3 lines
Filipe David Manana,

“Whether you think you can, or you think you can't — you're right.”
author
#3 lore raw

Filipe Manana 於 2019-03-25 19:14 寫到:

Quoted 54 lines · robbieko
Quoted 5 lines

From: Robbie Ko <[email protected]>

When doing fallocate, we first add the range to the reserve_list and then reserve the qutoa.

qutoa -> quota

Quoted 3 lines

If quota reservation fails, we'll release all reserved parts of reserve_list.
However, cur_offset doen't update to indicate that this range is

"doen't update" -> is not updated to indicate ...

Quoted 3 lines

already been inserted into the list.
Therefore, the same range is freed twice.
One at list_for_each_entry loop, and the other at the end of the

One -> once
and the other at -> and once at

Quoted 5 lines

function.
This will result in WARN_ON on bytes_may_use when we free the remaining space.

Fixes ("btrfs: update btrfs_space_info's bytes_may_use timely")

Missing commit id.

Quoted 10 lines
---
 fs/btrfs/file.c | 1 +
 1 file changed, 1 insertion(+)
fs/btrfs/file.c +0 -0
index d38dc8c..43c6c8a 100644

fs/btrfs/file.c static long btrfs_fallocate(struct file *file, · line 3138

Quoted 7 lines
fs/btrfs/file.c +1 -0
@@ -3132,6 +3132,7 @@ static long btrfs_fallocate(struct file *file, 
 int mode,
                        ret = btrfs_qgroup_reserve_data(inode, 
 &data_reserved,
                                        cur_offset, last_byte - 
 cur_offset);
                        if (ret < 0) {
+                               cur_offset = last_byte;

This will fix the problem if there's only one range, or we happen to be very lucky and cur_offset
matches exactly the middle of the entire range passed to fallocate() (in case the range covers
a sparse region of the file, a region with holes and extents).

Quoted 8 lines

At the end, under the 'out' label we have a call to:

btrfs_free_reserved_data_space(inode, data_reserved, alloc_start,
alloc_end - cur_offset);

That doesn't fell right - the start offset, third argument, should be cur_offset. Everything from
alloc_start to cur_offset was freed by the list_for_each_entry_safe_loop.

Ok, I will send patch v2.
Thank you.

Quoted 11 lines
Quoted 11 lines




                                free_extent_map(em);
                                break;
                        }

--
1.9.1