Btrfs: do not allow trimming when a fs is mounted with the nologreplay option
PATCH 1/1 Btrfs: do not allow trimming when a fs is mounted with the nologreplay option
From: Filipe Manana <[email protected]>
Whan a filesystem is mounted with the nologreplay mount option, which requires it to be mounted in RO mode as well, we can not allow discard on free space inside block groups, because log trees refer to extents that are not pinned in a block group's free space cache (pinning the extents is precisely the first phase of replaying a log tree).
So do not allow the fitrim ioctl to do anything when the filesystem is mounted with the nologreplay option, because later it can be mounted RW without that option, which causes log replay to happen and result in either a failure to replay the log trees (leading to a mount failure), a crash or some silent corruption.
--- fs/btrfs/ioctl.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
fs/btrfs/ioctl.c +10 -0
@@ -501,6 +501,16 @@ static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)if (!capable (CAP_SYS_ADMIN ))return -EPERM ; +/* +* If the fs is mounted with nologreplay, which requires it to be +* mounted in RO mode as well, we can not allow discard on free space +* inside block groups, because log trees refer to extents that are not +* pinned in a block group's free space cache (pinning the extents is +* precisely the first phase of replaying a log tree). +*/ +if (btrfs_test_opt (fs_info,NOLOGREPLAY )) +return -EROFS ; +rcu_read_lock ();list_for_each_entry_rcu (device, &fs_info->fs_devices->devices, dev_list) {
Signature · 1 line
2.11.0
Quoted 8 lines · On 26.03.19 г. 12:49 ч., [email protected] wrote:
From: Filipe Manana <[email protected]>
Whan a filesystem is mounted with the nologreplay mount option, which requires it to be mounted in RO mode as well, we can not allow discard on free space inside block groups, because log trees refer to extents that are not pinned in a block group's free space cache (pinning the extents is precisely the first phase of replaying a log tree).
Quoted 8 lines
So do not allow the fitrim ioctl to do anything when the filesystem is mounted with the nologreplay option, because later it can be mounted RW without that option, which causes log replay to happen and result in either a failure to replay the log trees (leading to a mount failure), a crash or some silent corruption.
Reported-by Darrick J. Wong <[email protected]>Signed-off-by Filipe Manana <[email protected]>
Does it make sense to make the check a bit more specific and only return EROFS when NOLOGREPLAY and the log tree has non-null generation?
In any case:
Quoted 26 lines
--- fs/btrfs/ioctl.c | 10 ++++++++++ 1 file changed, 10 insertions(+)fs/btrfs/ioctl.c +10
-0@@ -501,6 +501,16 @@ static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)if (!capable (CAP_SYS_ADMIN ))return -EPERM ; +/* +* If the fs is mounted with nologreplay, which requires it to be +* mounted in RO mode as well, we can not allow discard on free space +* inside block groups, because log trees refer to extents that are not +* pinned in a block group's free space cache (pinning the extents is +* precisely the first phase of replaying a log tree). +*/ +if (btrfs_test_opt (fs_info,NOLOGREPLAY )) +return -EROFS ; +rcu_read_lock ();list_for_each_entry_rcu (device, &fs_info->fs_devices->devices, dev_list) {
Quoted 18 lines · Nikolay Borisov
Quoted 14 lines · On 26.03.19 г. 12:49 ч., [email protected] wrote:
From: Filipe Manana <[email protected]>
Whan a filesystem is mounted with the nologreplay mount option, which requires it to be mounted in RO mode as well, we can not allow discard on free space inside block groups, because log trees refer to extents that are not pinned in a block group's free space cache (pinning the extents is precisely the first phase of replaying a log tree).
So do not allow the fitrim ioctl to do anything when the filesystem is mounted with the nologreplay option, because later it can be mounted RW without that option, which causes log replay to happen and result in either a failure to replay the log trees (leading to a mount failure), a crash or some silent corruption.
Quoted 5 lines
Quoted 2 lines
Reported-by Darrick J. Wong <[email protected]>Signed-off-by Filipe Manana <[email protected]>Does it make sense to make the check a bit more specific and only return EROFS when NOLOGREPLAY and the log tree has non-null generation?
It would make sense checking if there's actually a log tree as well. Neither the xfs nor ext4 (which is already in Linus' tree) do such equivalent checks, nor the proposed fstests test case makes sure a journal/log exists.
Not against it, but this isn't a common use case either.
Quoted 31 lines
In any case:
Reviewed-by Nikolay Borisov <[email protected]>Quoted 26 lines
--- fs/btrfs/ioctl.c | 10 ++++++++++ 1 file changed, 10 insertions(+)fs/btrfs/ioctl.c +10
-0@@ -501,6 +501,16 @@ static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)if (!capable (CAP_SYS_ADMIN ))return -EPERM ; +/* +* If the fs is mounted with nologreplay, which requires it to be +* mounted in RO mode as well, we can not allow discard on free space +* inside block groups, because log trees refer to extents that are not +* pinned in a block group's free space cache (pinning the extents is +* precisely the first phase of replaying a log tree). +*/ +if (btrfs_test_opt (fs_info,NOLOGREPLAY )) +return -EROFS ; +rcu_read_lock ();list_for_each_entry_rcu (device, &fs_info->fs_devices->devices, dev_list) {
Quoted 25 lines · Filipe Manana
Quoted 18 lines · Nikolay Borisov
Quoted 14 lines · On 26.03.19 г. 12:49 ч., [email protected] wrote:
From: Filipe Manana <[email protected]>
Whan a filesystem is mounted with the nologreplay mount option, which requires it to be mounted in RO mode as well, we can not allow discard on free space inside block groups, because log trees refer to extents that are not pinned in a block group's free space cache (pinning the extents is precisely the first phase of replaying a log tree).
So do not allow the fitrim ioctl to do anything when the filesystem is mounted with the nologreplay option, because later it can be mounted RW without that option, which causes log replay to happen and result in either a failure to replay the log trees (leading to a mount failure), a crash or some silent corruption.
Quoted 5 lines
Quoted 2 lines
Reported-by Darrick J. Wong <[email protected]>Signed-off-by Filipe Manana <[email protected]>Does it make sense to make the check a bit more specific and only return EROFS when NOLOGREPLAY and the log tree has non-null generation?
Quoted 6 lines
It would make sense checking if there's actually a log tree as well. Neither the xfs nor ext4 (which is already in Linus' tree) do such equivalent checks, nor the proposed fstests test case makes sure a journal/log exists.
Not against it, but this isn't a common use case either.
I think of this as sorts of "optimisation" where if we don't have a tree then we can allow trim. Though this is much simpler so I'm fine with it as well.
Quoted 33 lines
Quoted 32 lines
In any case:
Reviewed-by Nikolay Borisov <[email protected]>Quoted 26 lines
--- fs/btrfs/ioctl.c | 10 ++++++++++ 1 file changed, 10 insertions(+)fs/btrfs/ioctl.c +10
-0@@ -501,6 +501,16 @@ static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)if (!capable (CAP_SYS_ADMIN ))return -EPERM ; +/* +* If the fs is mounted with nologreplay, which requires it to be +* mounted in RO mode as well, we can not allow discard on free space +* inside block groups, because log trees refer to extents that are not +* pinned in a block group's free space cache (pinning the extents is +* precisely the first phase of replaying a log tree). +*/ +if (btrfs_test_opt (fs_info,NOLOGREPLAY )) +return -EROFS ; +rcu_read_lock ();list_for_each_entry_rcu (device, &fs_info->fs_devices->devices, dev_list) {
Quoted 28 lines · Nikolay Borisov
Quoted 27 lines · Filipe Manana
Quoted 15 lines · Nikolay Borisov
Quoted 14 lines · On 26.03.19 г. 12:49 ч., [email protected] wrote:
From: Filipe Manana <[email protected]>
Whan a filesystem is mounted with the nologreplay mount option, which requires it to be mounted in RO mode as well, we can not allow discard on free space inside block groups, because log trees refer to extents that are not pinned in a block group's free space cache (pinning the extents is precisely the first phase of replaying a log tree).
So do not allow the fitrim ioctl to do anything when the filesystem is mounted with the nologreplay option, because later it can be mounted RW without that option, which causes log replay to happen and result in either a failure to replay the log trees (leading to a mount failure), a crash or some silent corruption.
Quoted 5 lines
Quoted 2 lines
Reported-by Darrick J. Wong <[email protected]>Signed-off-by Filipe Manana <[email protected]>Does it make sense to make the check a bit more specific and only return EROFS when NOLOGREPLAY and the log tree has non-null generation?
It would make sense checking if there's actually a log tree as well. Neither the xfs nor ext4 (which is already in Linus' tree) do such equivalent checks, nor the proposed fstests test case makes sure a journal/log exists.
Quoted 5 lines
Quoted 1 line
Not against it, but this isn't a common use case either.
I think of this as sorts of "optimisation" where if we don't have a tree then we can allow trim. Though this is much simpler so I'm fine with it as well.
Agreed, the simple solution sounds ok to me, trim is not a critical operation so we don't need to try harder to make it work even with the mount option.
Quoted 17 lines · Nikolay Borisov
Quoted 14 lines · On 26.03.19 г. 12:49 ч., [email protected] wrote:
From: Filipe Manana <[email protected]>
Whan a filesystem is mounted with the nologreplay mount option, which requires it to be mounted in RO mode as well, we can not allow discard on free space inside block groups, because log trees refer to extents that are not pinned in a block group's free space cache (pinning the extents is precisely the first phase of replaying a log tree).
So do not allow the fitrim ioctl to do anything when the filesystem is mounted with the nologreplay option, because later it can be mounted RW without that option, which causes log replay to happen and result in either a failure to replay the log trees (leading to a mount failure), a crash or some silent corruption.
Quoted 5 lines
Quoted 2 lines
Reported-by Darrick J. Wong <[email protected]>Signed-off-by Filipe Manana <[email protected]>Does it make sense to make the check a bit more specific and only return EROFS when NOLOGREPLAY and the log tree has non-null generation?
To me fstrim is a WRITE operation, why it is allowed even in RO mount?
Thanks,
Qu
Quoted 31 lines
In any case:
Reviewed-by Nikolay Borisov <[email protected]>Quoted 26 lines
--- fs/btrfs/ioctl.c | 10 ++++++++++ 1 file changed, 10 insertions(+)fs/btrfs/ioctl.c +10
-0@@ -501,6 +501,16 @@ static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)if (!capable (CAP_SYS_ADMIN ))return -EPERM ; +/* +* If the fs is mounted with nologreplay, which requires it to be +* mounted in RO mode as well, we can not allow discard on free space +* inside block groups, because log trees refer to extents that are not +* pinned in a block group's free space cache (pinning the extents is +* precisely the first phase of replaying a log tree). +*/ +if (btrfs_test_opt (fs_info,NOLOGREPLAY )) +return -EROFS ; +rcu_read_lock ();list_for_each_entry_rcu (device, &fs_info->fs_devices->devices, dev_list) {
Quoted 20 lines · Qu Wenruo
Quoted 17 lines · Nikolay Borisov
Quoted 14 lines · On 26.03.19 г. 12:49 ч., [email protected] wrote:
From: Filipe Manana <[email protected]>
Whan a filesystem is mounted with the nologreplay mount option, which requires it to be mounted in RO mode as well, we can not allow discard on free space inside block groups, because log trees refer to extents that are not pinned in a block group's free space cache (pinning the extents is precisely the first phase of replaying a log tree).
So do not allow the fitrim ioctl to do anything when the filesystem is mounted with the nologreplay option, because later it can be mounted RW without that option, which causes log replay to happen and result in either a failure to replay the log trees (leading to a mount failure), a crash or some silent corruption.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Reported-by Darrick J. Wong <[email protected]>Signed-off-by Filipe Manana <[email protected]>Does it make sense to make the check a bit more specific and only return EROFS when NOLOGREPLAY and the log tree has non-null generation?
To me fstrim is a WRITE operation, why it is allowed even in RO mount?
It's write to the block device, not to the filesystem.