From nobody Fri Dec 19 02:38:30 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AAE8DC4332F for ; Wed, 13 Dec 2023 14:43:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1441896AbjLMOnS (ORCPT ); Wed, 13 Dec 2023 09:43:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233684AbjLMOnF (ORCPT ); Wed, 13 Dec 2023 09:43:05 -0500 Received: from esa6.hgst.iphmx.com (esa6.hgst.iphmx.com [216.71.154.45]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 84B99DD; Wed, 13 Dec 2023 06:43:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1702478590; x=1734014590; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=kCWRP4ScRPyTqiJ4MDp9iTyJnJWsDUgaRphcdVMeqP4=; b=N41Le6KxFn1+00e4QkNJYWknHyiC5pUZlCIMXZl1JleqpUahAZlo1Q3S c06LK5bQA2/g0ot0tLej1i9/cERf0NOpphmxCAJ7eZqS/jpcZ6OZzATWm c7E370XqTda7YgF/nfcR2nF3PKSXxG6g7j2wcIHgeCxmQgk9ePOufLgJw 0C1sLz7qkhzLYe8u3njnFwQKBSMxB9G3HR2C5fSiyvfhnA9Laof+hywgR 5/9l2s83g4w4NSJ8UreIib1HM5BTwQwUEWnQllmKZrPL6XjKiaM3efWX9 DZxvnnT/tpX402YvnsPfaCB9F+i4Z6W0iNWcCXGttr/NbknHmPPTJyFZs g==; X-CSE-ConnectionGUID: rxeWZ6TySI6feA77rUiLtA== X-CSE-MsgGUID: oJw4pwKgRxye/OJealKLtA== X-IronPort-AV: E=Sophos;i="6.04,272,1695657600"; d="scan'208";a="4802945" Received: from uls-op-cesaip02.wdc.com (HELO uls-op-cesaep02.wdc.com) ([199.255.45.15]) by ob1.hgst.iphmx.com with ESMTP; 13 Dec 2023 22:43:09 +0800 IronPort-SDR: FWVbcpiYP1oJc9svd3OUOlP063r4p9weCnI58aYtoI/Jwtl6e2tD2wzFH2vhFQrBNIqXgMSmf9 YjZkfrBVP/KA== Received: from uls-op-cesaip02.wdc.com ([10.248.3.37]) by uls-op-cesaep02.wdc.com with ESMTP/TLS/ECDHE-RSA-AES128-GCM-SHA256; 13 Dec 2023 05:48:21 -0800 IronPort-SDR: Sdqk3/eBzRzzkp2HGk8fT6lwuLwp1XheEUB6rD5taGVGQtS9g2aGj/uFym8ECulRPJu571htm7 Mz5ocnGzDYDw== WDCIronportException: Internal Received: from unknown (HELO redsun91.ssa.fujisawa.hgst.com) ([10.149.66.6]) by uls-op-cesaip02.wdc.com with ESMTP; 13 Dec 2023 06:43:09 -0800 From: Johannes Thumshirn Date: Wed, 13 Dec 2023 06:42:57 -0800 Subject: [PATCH v2 02/13] btrfs: re-introduce struct btrfs_io_geometry MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20231213-btrfs_map_block-cleanup-v2-2-cf5cfb9e2400@wdc.com> References: <20231213-btrfs_map_block-cleanup-v2-0-cf5cfb9e2400@wdc.com> In-Reply-To: <20231213-btrfs_map_block-cleanup-v2-0-cf5cfb9e2400@wdc.com> To: Chris Mason , Josef Bacik , David Sterba Cc: linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org, Johannes Thumshirn X-Mailer: b4 0.12.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1702478586; l=11440; i=johannes.thumshirn@wdc.com; s=20230613; h=from:subject:message-id; bh=kCWRP4ScRPyTqiJ4MDp9iTyJnJWsDUgaRphcdVMeqP4=; b=OFP/4/oB9kN+9QYp7QGL72N2aaB/7kQePUB5cM8B57xxcsjfqxEKzWkE0ZgWdL3QvXQU4ufHk wFQ2RN9N/S+Am1+6rQ+f7CW/Uplka857APsWBc8ByWin+H4jp6VfSbR X-Developer-Key: i=johannes.thumshirn@wdc.com; a=ed25519; pk=TGmHKs78FdPi+QhrViEvjKIGwReUGCfa+3LEnGoR2KM= Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Re-introduce struct btrfs_io_geometry, holding the necessary bits and pieces needed in btrfs_map_block() to decide the I/O geometry of a specific block mapping. Signed-off-by: Johannes Thumshirn --- fs/btrfs/volumes.c | 159 ++++++++++++++++++++++++++++++-------------------= ---- 1 file changed, 89 insertions(+), 70 deletions(-) diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index 1011178a244c..ea830ff0c0e3 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -41,6 +41,17 @@ BTRFS_BLOCK_GROUP_RAID10 | \ BTRFS_BLOCK_GROUP_RAID56_MASK) =20 +struct btrfs_io_geometry { + u32 stripe_index; + u32 stripe_nr; + int mirror_num; + int num_stripes; + u64 stripe_offset; + u64 raid56_full_stripe_start; + int max_errors; + enum btrfs_map_op op; +}; + const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] =3D { [BTRFS_RAID_RAID10] =3D { .sub_stripes =3D 2, @@ -6393,28 +6404,27 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, = enum btrfs_map_op op, struct btrfs_io_stripe *smap, int *mirror_num_ret) { struct btrfs_chunk_map *map; + struct btrfs_io_geometry io_geom =3D { }; u64 map_offset; - u64 stripe_offset; - u32 stripe_nr; - u32 stripe_index; int data_stripes; int i; int ret =3D 0; - int mirror_num =3D (mirror_num_ret ? *mirror_num_ret : 0); - int num_stripes; int num_copies; - int max_errors =3D 0; struct btrfs_io_context *bioc =3D NULL; struct btrfs_dev_replace *dev_replace =3D &fs_info->dev_replace; int dev_replace_is_ongoing =3D 0; u16 num_alloc_stripes; - u64 raid56_full_stripe_start =3D (u64)-1; u64 max_len; =20 ASSERT(bioc_ret); =20 + io_geom.mirror_num =3D (mirror_num_ret ? *mirror_num_ret : 0); + io_geom.num_stripes =3D 1; + io_geom.stripe_index =3D 0; + io_geom.op =3D op; + num_copies =3D btrfs_num_copies(fs_info, logical, fs_info->sectorsize); - if (mirror_num > num_copies) + if (io_geom.mirror_num > num_copies) return -EINVAL; =20 map =3D btrfs_get_chunk_map(fs_info, logical, *length); @@ -6424,8 +6434,10 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, e= num btrfs_map_op op, data_stripes =3D nr_data_stripes(map); =20 map_offset =3D logical - map->start; - max_len =3D btrfs_max_io_len(map, op, map_offset, &stripe_nr, - &stripe_offset, &raid56_full_stripe_start); + io_geom.raid56_full_stripe_start =3D (u64)-1; + max_len =3D btrfs_max_io_len(map, io_geom.op, map_offset, &io_geom.stripe= _nr, + &io_geom.stripe_offset, + &io_geom.raid56_full_stripe_start); *length =3D min_t(u64, map->chunk_len - map_offset, max_len); =20 down_read(&dev_replace->rwsem); @@ -6437,53 +6449,51 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, = enum btrfs_map_op op, if (!dev_replace_is_ongoing) up_read(&dev_replace->rwsem); =20 - num_stripes =3D 1; - stripe_index =3D 0; if (map->type & BTRFS_BLOCK_GROUP_RAID0) { - stripe_index =3D stripe_nr % map->num_stripes; - stripe_nr /=3D map->num_stripes; + io_geom.stripe_index =3D io_geom.stripe_nr % map->num_stripes; + io_geom.stripe_nr /=3D map->num_stripes; if (op =3D=3D BTRFS_MAP_READ) - mirror_num =3D 1; + io_geom.mirror_num =3D 1; } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) { if (op !=3D BTRFS_MAP_READ) { - num_stripes =3D map->num_stripes; - } else if (mirror_num) { - stripe_index =3D mirror_num - 1; + io_geom.num_stripes =3D map->num_stripes; + } else if (io_geom.mirror_num) { + io_geom.stripe_index =3D io_geom.mirror_num - 1; } else { - stripe_index =3D find_live_mirror(fs_info, map, 0, + io_geom.stripe_index =3D find_live_mirror(fs_info, map, 0, dev_replace_is_ongoing); - mirror_num =3D stripe_index + 1; + io_geom.mirror_num =3D io_geom.stripe_index + 1; } =20 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { if (op !=3D BTRFS_MAP_READ) { - num_stripes =3D map->num_stripes; - } else if (mirror_num) { - stripe_index =3D mirror_num - 1; + io_geom.num_stripes =3D map->num_stripes; + } else if (io_geom.mirror_num) { + io_geom.stripe_index =3D io_geom.mirror_num - 1; } else { - mirror_num =3D 1; + io_geom.mirror_num =3D 1; } =20 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { u32 factor =3D map->num_stripes / map->sub_stripes; =20 - stripe_index =3D (stripe_nr % factor) * map->sub_stripes; - stripe_nr /=3D factor; + io_geom.stripe_index =3D (io_geom.stripe_nr % factor) * map->sub_stripes; + io_geom.stripe_nr /=3D factor; =20 if (op !=3D BTRFS_MAP_READ) - num_stripes =3D map->sub_stripes; - else if (mirror_num) - stripe_index +=3D mirror_num - 1; + io_geom.num_stripes =3D map->sub_stripes; + else if (io_geom.mirror_num) + io_geom.stripe_index +=3D io_geom.mirror_num - 1; else { - int old_stripe_index =3D stripe_index; - stripe_index =3D find_live_mirror(fs_info, map, - stripe_index, + int old_stripe_index =3D io_geom.stripe_index; + io_geom.stripe_index =3D find_live_mirror(fs_info, map, + io_geom.stripe_index, dev_replace_is_ongoing); - mirror_num =3D stripe_index - old_stripe_index + 1; + io_geom.mirror_num =3D io_geom.stripe_index - old_stripe_index + 1; } =20 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { - if (op !=3D BTRFS_MAP_READ || mirror_num > 1) { + if (op !=3D BTRFS_MAP_READ || io_geom.mirror_num > 1) { /* * Needs full stripe mapping. * @@ -6495,29 +6505,33 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, = enum btrfs_map_op op, * but that can be expensive. Here we just divide * @stripe_nr with @data_stripes. */ - stripe_nr /=3D data_stripes; + io_geom.stripe_nr /=3D data_stripes; =20 /* RAID[56] write or recovery. Return all stripes */ - num_stripes =3D map->num_stripes; - max_errors =3D btrfs_chunk_max_errors(map); + io_geom.num_stripes =3D map->num_stripes; + io_geom.max_errors =3D btrfs_chunk_max_errors(map); =20 /* Return the length to the full stripe end */ *length =3D min(logical + *length, - raid56_full_stripe_start + map->start + - btrfs_stripe_nr_to_offset(data_stripes)) - + io_geom.raid56_full_stripe_start + + map->start + + btrfs_stripe_nr_to_offset( + data_stripes)) - logical; - stripe_index =3D 0; - stripe_offset =3D 0; + io_geom.stripe_index =3D 0; + io_geom.stripe_offset =3D 0; } else { - ASSERT(mirror_num <=3D 1); + ASSERT(io_geom.mirror_num <=3D 1); /* Just grab the data stripe directly. */ - stripe_index =3D stripe_nr % data_stripes; - stripe_nr /=3D data_stripes; + io_geom.stripe_index =3D io_geom.stripe_nr % data_stripes; + io_geom.stripe_nr /=3D data_stripes; =20 /* We distribute the parity blocks across stripes */ - stripe_index =3D (stripe_nr + stripe_index) % map->num_stripes; - if (op =3D=3D BTRFS_MAP_READ && mirror_num < 1) - mirror_num =3D 1; + io_geom.stripe_index =3D + (io_geom.stripe_nr + io_geom.stripe_index) % + map->num_stripes; + if (op =3D=3D BTRFS_MAP_READ && io_geom.mirror_num < 1) + io_geom.mirror_num =3D 1; } } else { /* @@ -6525,19 +6539,19 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, = enum btrfs_map_op op, * device we have to walk to find the data, and stripe_index is * the number of our device in the stripe array */ - stripe_index =3D stripe_nr % map->num_stripes; - stripe_nr /=3D map->num_stripes; - mirror_num =3D stripe_index + 1; + io_geom.stripe_index =3D io_geom.stripe_nr % map->num_stripes; + io_geom.stripe_nr /=3D map->num_stripes; + io_geom.mirror_num =3D io_geom.stripe_index + 1; } - if (stripe_index >=3D map->num_stripes) { + if (io_geom.stripe_index >=3D map->num_stripes) { btrfs_crit(fs_info, "stripe index math went horribly wrong, got stripe_index=3D%u, num_s= tripes=3D%u", - stripe_index, map->num_stripes); + io_geom.stripe_index, map->num_stripes); ret =3D -EINVAL; goto out; } =20 - num_alloc_stripes =3D num_stripes; + num_alloc_stripes =3D io_geom.num_stripes; if (dev_replace_is_ongoing && dev_replace->tgtdev !=3D NULL && op !=3D BTRFS_MAP_READ) /* @@ -6555,11 +6569,12 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, = enum btrfs_map_op op, * I/O context structure. */ if (is_single_device_io(fs_info, smap, map, num_alloc_stripes, op, - mirror_num)) { + io_geom.mirror_num)) { ret =3D set_io_stripe(fs_info, op, logical, length, smap, map, - stripe_index, stripe_offset, stripe_nr); + io_geom.stripe_index, io_geom.stripe_offset, + io_geom.stripe_nr); if (mirror_num_ret) - *mirror_num_ret =3D mirror_num; + *mirror_num_ret =3D io_geom.mirror_num; *bioc_ret =3D NULL; goto out; } @@ -6579,7 +6594,7 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, en= um btrfs_map_op op, * It's still mostly the same as other profiles, just with extra rotation. */ if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && - (op !=3D BTRFS_MAP_READ || mirror_num > 1)) { + (op !=3D BTRFS_MAP_READ || io_geom.mirror_num > 1)) { /* * For RAID56 @stripe_nr is already the number of full stripes * before us, which is also the rotation value (needs to modulo @@ -6589,12 +6604,13 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, = enum btrfs_map_op op, * modulo, to reduce one modulo call. */ bioc->full_stripe_logical =3D map->start + - btrfs_stripe_nr_to_offset(stripe_nr * data_stripes); - for (int i =3D 0; i < num_stripes; i++) { + btrfs_stripe_nr_to_offset(io_geom.stripe_nr * data_stripes); + for (int i =3D 0; i < io_geom.num_stripes; i++) { ret =3D set_io_stripe(fs_info, op, logical, length, &bioc->stripes[i], map, - (i + stripe_nr) % num_stripes, - stripe_offset, stripe_nr); + (i + io_geom.stripe_nr) % io_geom.num_stripes, + io_geom.stripe_offset, + io_geom.stripe_nr); if (ret < 0) break; } @@ -6603,13 +6619,15 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, = enum btrfs_map_op op, * For all other non-RAID56 profiles, just copy the target * stripe into the bioc. */ - for (i =3D 0; i < num_stripes; i++) { + for (i =3D 0; i < io_geom.num_stripes; i++) { ret =3D set_io_stripe(fs_info, op, logical, length, - &bioc->stripes[i], map, stripe_index, - stripe_offset, stripe_nr); + &bioc->stripes[i], map, + io_geom.stripe_index, + io_geom.stripe_offset, + io_geom.stripe_nr); if (ret < 0) break; - stripe_index++; + io_geom.stripe_index++; } } =20 @@ -6620,18 +6638,19 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, = enum btrfs_map_op op, } =20 if (op !=3D BTRFS_MAP_READ) - max_errors =3D btrfs_chunk_max_errors(map); + io_geom.max_errors =3D btrfs_chunk_max_errors(map); =20 if (dev_replace_is_ongoing && dev_replace->tgtdev !=3D NULL && op !=3D BTRFS_MAP_READ) { handle_ops_on_dev_replace(op, bioc, dev_replace, logical, - &num_stripes, &max_errors); + &io_geom.num_stripes, + &io_geom.max_errors); } =20 *bioc_ret =3D bioc; - bioc->num_stripes =3D num_stripes; - bioc->max_errors =3D max_errors; - bioc->mirror_num =3D mirror_num; + bioc->num_stripes =3D io_geom.num_stripes; + bioc->max_errors =3D io_geom.max_errors; + bioc->mirror_num =3D io_geom.mirror_num; =20 out: if (dev_replace_is_ongoing) { --=20 2.43.0