Skip to content

Commit ef5cb96

Browse files
maharmstonekdave
authored andcommitted
btrfs-progs: mkfs: discard temporary chunks
Change cleanup_temp_chunks() so that we discard the range covered by the temporary chunks when we delete them. RAID5 and 6 aren't an issue here, as the temporary chunks are only ever created SINGLE. Pull-request: #1021 Signed-off-by: Mark Harmstone <mark@harmstone.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 11550af commit ef5cb96

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

mkfs/main.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,64 @@ static int next_block_group(struct btrfs_root *root,
634634
return ret;
635635
}
636636

637+
static int discard_logical_range_mirror(struct btrfs_fs_info *fs_info, int mirror,
638+
u64 start, u64 len)
639+
{
640+
struct btrfs_multi_bio *multi = NULL;
641+
int ret;
642+
u64 cur_offset = 0;
643+
u64 cur_len;
644+
645+
while (cur_offset < len) {
646+
struct btrfs_device *device;
647+
648+
cur_len = len - cur_offset;
649+
ret = btrfs_map_block(fs_info, READ, start + cur_offset, &cur_len,
650+
&multi, mirror, NULL);
651+
if (ret)
652+
return ret;
653+
654+
cur_len = min(cur_len, len - cur_offset);
655+
656+
for (int i = 0; i < multi->num_stripes; i++) {
657+
device = multi->stripes[i].dev;
658+
659+
ret = device_discard_blocks(device->fd,
660+
multi->stripes[i].physical,
661+
cur_len);
662+
663+
if (ret < 0) {
664+
free(multi);
665+
666+
if (ret == -EOPNOTSUPP)
667+
ret = 0;
668+
669+
return ret;
670+
}
671+
}
672+
free(multi);
673+
multi = NULL;
674+
cur_offset += cur_len;
675+
}
676+
677+
return 0;
678+
}
679+
680+
static int discard_logical_range(struct btrfs_fs_info *fs_info, u64 start, u64 len)
681+
{
682+
int ret, num_copies;
683+
684+
num_copies = btrfs_num_copies(fs_info, start, len);
685+
686+
for (int i = 0; i < num_copies; i++) {
687+
ret = discard_logical_range_mirror(fs_info, i + 1, start, len);
688+
if (ret < 0)
689+
return ret;
690+
}
691+
692+
return 0;
693+
}
694+
637695
/* This function will cleanup */
638696
static int cleanup_temp_chunks(struct btrfs_fs_info *fs_info,
639697
struct mkfs_allocation *alloc,
@@ -695,6 +753,14 @@ static int cleanup_temp_chunks(struct btrfs_fs_info *fs_info,
695753
sys_profile)) {
696754
u64 flags = btrfs_block_group_flags(path.nodes[0], bgi);
697755

756+
if (opt_discard) {
757+
ret = discard_logical_range(fs_info,
758+
found_key.objectid,
759+
found_key.offset);
760+
if (ret < 0)
761+
goto out;
762+
}
763+
698764
ret = btrfs_remove_block_group(trans,
699765
found_key.objectid, found_key.offset);
700766
if (ret < 0)

0 commit comments

Comments
 (0)