From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.burntcomma.com (mail2.burntcomma.com [217.169.27.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A724D2FFDD5 for ; Wed, 13 Aug 2025 14:35:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.169.27.34 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755095729; cv=none; b=VG2Wes7zzS9J9L9ufGYMK3lHNOl5Ws4xIm96RqbNMIoZ3ktSCOH/d2QKF6kJSAj9TOJxU8XdvoDvV9f5nATm5UI0UcTyaDlFCr8IH1/riGk1gbBMFkPy6DIvJgnlymkjQ2OVm9iNZinYaX7cSkyFI1O9Ka0t7NB13EuZf8zeb4Q= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755095729; c=relaxed/simple; bh=IelO6SUYpzC2GOe/eKs8cL2t4b+HX5bT65fShYghlNs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Mime-Version; b=fR7s6MuS5U0mYec7GBAAeZ1s4QUdOLLAUvCWyaPqO11wTCQwX/3ZN9WPuSTTHlia37SjtqV2NvnR0t7ONW732FK7G1oDvilPswQUWvWBWF9GCRCctNTE6GPbpAU5daR0x/i4oIZMCDv5kesmH11wY1vEQ2o6n1YwG+DhQg19U0Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=harmstone.com; spf=pass smtp.mailfrom=harmstone.com; dkim=pass (1024-bit key) header.d=harmstone.com header.i=@harmstone.com header.b=huY2Fhyf; arc=none smtp.client-ip=217.169.27.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=harmstone.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=harmstone.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=harmstone.com header.i=@harmstone.com header.b="huY2Fhyf" Received: from localhost.localdomain (beren.burntcomma.com [IPv6:2a02:8012:8cf0:0:ce28:aaff:fe0d:6db2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (Client did not present a certificate) by mail.burntcomma.com (Postfix) with ESMTPSA id 902B42A759D; Wed, 13 Aug 2025 15:35:13 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=harmstone.com; s=mail; t=1755095713; bh=VzIs6a+zqisDN2GUqFELgnXG05Zvol38l2U+usw5hqI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=huY2FhyfokydICYhYlJ9/AWmkRXdkv8xafRHfA7q4bsDrd30PZn22HGtPaprJzF4G HJ6oX/kcPtWyNUR8lbusokjAEWZV+AnVv/jbBiQacK3cValV+mRhjx34Z/WUGEHEUX iuEC9xFBTTXnih/TPwXvAzoD2tAbsH8pO8hrOo9M= From: Mark Harmstone To: linux-btrfs@vger.kernel.org Cc: Mark Harmstone Subject: [PATCH v2 14/16] btrfs: add do_remap param to btrfs_discard_extent() Date: Wed, 13 Aug 2025 15:34:56 +0100 Message-ID: <20250813143509.31073-15-mark@harmstone.com> In-Reply-To: <20250813143509.31073-1-mark@harmstone.com> References: <20250813143509.31073-1-mark@harmstone.com> Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: 8bit btrfs_discard_extent() can be called either when an extent is removed or from walking the free-space tree. With a remapped block group these two things are no longer equivalent: the extent's addresses are remapped, while the free-space tree exclusively uses underlying addresses. Add a do_remap parameter to btrfs_discard_extent() and btrfs_map_discard(), saying whether or not the address needs to be run through the remap tree first. Signed-off-by: Mark Harmstone --- fs/btrfs/extent-tree.c | 11 +++++++---- fs/btrfs/extent-tree.h | 2 +- fs/btrfs/free-space-cache.c | 2 +- fs/btrfs/inode.c | 2 +- fs/btrfs/volumes.c | 25 +++++++++++++++++++++++-- fs/btrfs/volumes.h | 2 +- 6 files changed, 34 insertions(+), 10 deletions(-) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index 1e5a68addf25..b02e99b41553 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -1380,7 +1380,7 @@ static int do_discard_extent(struct btrfs_discard_stripe *stripe, u64 *bytes) } int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr, - u64 num_bytes, u64 *actual_bytes) + u64 num_bytes, u64 *actual_bytes, bool do_remap) { int ret = 0; u64 discarded_bytes = 0; @@ -1398,7 +1398,8 @@ int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr, int i; num_bytes = end - cur; - stripes = btrfs_map_discard(fs_info, cur, &num_bytes, &num_stripes); + stripes = btrfs_map_discard(fs_info, cur, &num_bytes, + &num_stripes, do_remap); if (IS_ERR(stripes)) { ret = PTR_ERR(stripes); if (ret == -EOPNOTSUPP) @@ -2868,7 +2869,8 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans) if (btrfs_test_opt(fs_info, DISCARD_SYNC)) ret = btrfs_discard_extent(fs_info, start, - end + 1 - start, NULL); + end + 1 - start, NULL, + true); next_state = btrfs_next_extent_state(unpin, cached_state); btrfs_clear_extent_dirty(unpin, start, end, &cached_state); @@ -2926,7 +2928,8 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans) ret = -EROFS; if (!TRANS_ABORTED(trans)) ret = btrfs_discard_extent(fs_info, block_group->start, - block_group->length, NULL); + block_group->length, NULL, + true); /* * Not strictly necessary to lock, as the block_group should be diff --git a/fs/btrfs/extent-tree.h b/fs/btrfs/extent-tree.h index 82d3a82dc712..679f6f1319b7 100644 --- a/fs/btrfs/extent-tree.h +++ b/fs/btrfs/extent-tree.h @@ -163,7 +163,7 @@ int btrfs_drop_subtree(struct btrfs_trans_handle *trans, struct extent_buffer *parent); void btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info, u64 start, u64 end); int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr, - u64 num_bytes, u64 *actual_bytes); + u64 num_bytes, u64 *actual_bytes, bool do_remap); int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range); #endif diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c index 5d8d1570a5c9..b9ef7a8a7996 100644 --- a/fs/btrfs/free-space-cache.c +++ b/fs/btrfs/free-space-cache.c @@ -3672,7 +3672,7 @@ static int do_trimming(struct btrfs_block_group *block_group, spin_unlock(&block_group->lock); spin_unlock(&space_info->lock); - ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed); + ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed, false); if (!ret) { *total_trimmed += trimmed; trim_state = BTRFS_TRIM_STATE_TRIMMED; diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 4001cd2a08b1..ef61007892bd 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -3293,7 +3293,7 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent) btrfs_discard_extent(fs_info, ordered_extent->disk_bytenr, ordered_extent->disk_num_bytes, - NULL); + NULL, true); btrfs_free_reserved_extent(fs_info, ordered_extent->disk_bytenr, ordered_extent->disk_num_bytes, true); diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index b4fe2e928992..e13f16a7a904 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -3490,7 +3490,8 @@ int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset, * filesystem's point of view. */ if (btrfs_is_zoned(fs_info)) { - ret = btrfs_discard_extent(fs_info, chunk_offset, length, NULL); + ret = btrfs_discard_extent(fs_info, chunk_offset, length, NULL, + true); if (ret) btrfs_info(fs_info, "failed to reset zone %llu after relocation", @@ -6143,7 +6144,7 @@ void btrfs_put_bioc(struct btrfs_io_context *bioc) */ struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info, u64 logical, u64 *length_ret, - u32 *num_stripes) + u32 *num_stripes, bool do_remap) { struct btrfs_chunk_map *map; struct btrfs_discard_stripe *stripes; @@ -6167,6 +6168,26 @@ struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info, if (IS_ERR(map)) return ERR_CAST(map); + if (do_remap && map->type & BTRFS_BLOCK_GROUP_REMAPPED) { + u64 new_logical = logical; + + ret = btrfs_translate_remap(fs_info, &new_logical, &length, + false); + if (ret) + goto out_free_map; + + if (new_logical != logical) { + btrfs_free_chunk_map(map); + + map = btrfs_get_chunk_map(fs_info, new_logical, + length); + if (IS_ERR(map)) + return ERR_CAST(map); + + logical = new_logical; + } + } + /* we don't discard raid56 yet */ if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { ret = -EOPNOTSUPP; diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h index 64b34710b68b..7abf3b119345 100644 --- a/fs/btrfs/volumes.h +++ b/fs/btrfs/volumes.h @@ -727,7 +727,7 @@ int btrfs_map_repair_block(struct btrfs_fs_info *fs_info, u32 length, int mirror_num); struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info, u64 logical, u64 *length_ret, - u32 *num_stripes); + u32 *num_stripes, bool do_remap); int btrfs_read_sys_array(struct btrfs_fs_info *fs_info); int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info); struct btrfs_block_group *btrfs_create_chunk(struct btrfs_trans_handle *trans, -- 2.49.1