btrfs-progs: subvolume: check deleting default subvolume

Deleting the default subvolume is not permitted and kernel prints a
message to the system log. This is not immediately clear to the user and
we had requests to improve that.

This patch will read the default subvolume id and reject deletion
without trying to delete it.

Issue: #274
Issue: #255
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=207975
Signed-off-by: Sidong Yang <realwakka@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Sidong Yang 2020-09-28 15:07:29 +00:00 committed by David Sterba
parent 65ecbc7e0e
commit 87804a3f06
1 changed files with 27 additions and 0 deletions

View File

@ -264,6 +264,7 @@ static int cmd_subvol_delete(const struct cmd_struct *cmd,
struct seen_fsid *seen_fsid_hash[SEEN_FSID_HASH_SIZE] = { NULL, }; struct seen_fsid *seen_fsid_hash[SEEN_FSID_HASH_SIZE] = { NULL, };
enum { COMMIT_AFTER = 1, COMMIT_EACH = 2 }; enum { COMMIT_AFTER = 1, COMMIT_EACH = 2 };
enum btrfs_util_error err; enum btrfs_util_error err;
uint64_t default_subvol_id = 0, target_subvol_id = 0;
optind = 0; optind = 0;
while (1) { while (1) {
@ -360,6 +361,32 @@ again:
goto out; goto out;
} }
err = btrfs_util_get_default_subvolume_fd(fd, &default_subvol_id);
if (err) {
warning("cannot read default subvolume id: %m");
default_subvol_id = 0;
}
if (subvolid > 0) {
target_subvol_id = subvolid;
} else {
err = btrfs_util_subvolume_id(path, &target_subvol_id);
if (err) {
ret = 1;
goto out;
}
}
if (target_subvol_id == default_subvol_id) {
warning("not deleting default subvolume id %llu '%s%s%s'",
(u64)default_subvol_id,
(subvolid == 0 ? dname : ""),
(subvolid == 0 ? "/" : ""),
(subvolid == 0 ? vname : full_subvolpath));
ret = 1;
goto out;
}
pr_verbose(MUST_LOG, "Delete subvolume (%s): ", pr_verbose(MUST_LOG, "Delete subvolume (%s): ",
commit_mode == COMMIT_EACH || commit_mode == COMMIT_EACH ||
(commit_mode == COMMIT_AFTER && cnt + 1 == argc) ? (commit_mode == COMMIT_AFTER && cnt + 1 == argc) ?