From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.160.180 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795207916; b=iGumHikR0NpxNsuiJ9i7kzbkrDyY6/3R4MVIqhA3TRqmVDtu3TPKwItO8pAEU4PntW7/ Ah4vAyegIOhEI1j5hAdiz1m0pBeSdEwS0eMRG+H6jLwlhgCODC1KuhPdV1D3AVY1GUy45 Lc5jwaFOpapLVn9RWCT9Ps5OuR2evMM/gljn2n/icBJSLnNCbzjOrLaC9cqgCCKnwPIGw +c4MORMQi3FIMgG8uOyf8NaPoMaKb2j4SCsfoGqSjX9WsISiYxboINWftOF2YqiDVMriB N7n/NdRUdzSOE68A6kfObSl5PtRvHa5Z0YD1nGBErLldk6Yj/uwzE0cSab9dDh5ydVA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795207916; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:Date:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key; bh=jc4B/ZAyaInl7xXWrF7rGyBRRKoCLa1nL9SU5Vv5qNU=; b=YJkhesrJQDV3m2F+7uX6CPqjS6NTXvXD34DK4+p4h8SCwsg8+mcRld9/SgKd1Vv9WuiI BYOLerMizbxqw//rZevSGm4QTNA5V832V8Hw03pfEUDTyM0bLiAVEK8YNbh/r7/9m46li iGf9UvYPjDzLya2iAxarguHVKX8NK2/i5EWuc2ckpLfh4BFv/TADnJICXY0kPhnMson58 5csggqUqiqUx4SvLkTyWMTVT8d2IL98atYUsdRws82nvlBLlW+EY15Ph72KZGBsAulLWx vuNq5g77DqUTcjEGHYmWsfAwKVpJJnBHUKFNa9WHurgcuRcy29mGJa9qna6BSn32slw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=toxicpanda.com; spf=pass smtp.mailfrom=toxicpanda.com; dkim=pass header.d=toxicpanda.com header.i=@toxicpanda.com header.a=rsa-sha256 header.s=google header.b=COicZSln; arc=none smtp.remote-ip=209.85.160.180 Received: from mail-qt1-f180.google.com (mail-qt1-f180.google.com [209.85.160.180]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2D4AB606A33D for ; Fri, 20 Nov 2026 20:51:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2D4AB606A33D Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda.com header.i=@toxicpanda.com header.a=rsa-sha256 header.s=google header.b=COicZSln Received: by mail-qt1-f180.google.com with SMTP id d75a77b69052e-53664b4860bso14959811cf.1 for ; Fri, 20 Nov 2026 12:51:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda.com; s=google; t=1795207912; x=1795812712; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=jc4B/ZAyaInl7xXWrF7rGyBRRKoCLa1nL9SU5Vv5qNU=; b=COicZSlnI2LoFQnjVuFe2SjVSDVuGwUiDRZq9+ijLwX7Nt2qrhWQjd+4EuK0Z+frFj QwWJej/Yb8+BmjtAxZvQgz7DJSoeg7IgbYuik/qw7NC5aLjf6RInpE/Fpa4//Zlu61Is 21+lOGCQTAcUk7feZZbojkaXI+euoGfqCHkQWB/SXEWNxiNnm50uXMR2by1Alu8/ABo7 e1PmHNz6OjUtX22L8VYtsv8fe7Ebgtiq6lBGyN2xq7GRE7p7NlDYtc7Ib2zAIiPo62ba FuFJdcAHNOBKvjlgpqnImzNQkLWBoDt6tOBum9XAxRdp6SiJjSWh1t/XVNe2se9mk00+ Dzig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795207912; x=1795812712; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=jc4B/ZAyaInl7xXWrF7rGyBRRKoCLa1nL9SU5Vv5qNU=; b=R+XUWuv4bd3k9wlfgHVoqO6YzwL6u7cR8GLRQYvNsiFQYeodJ1L41eLZrHje3l3VG+ 1ADOmuhL3T6IgLP6eLVqzN5ZhWdqjFsGCndEQsWjThJNbZ83soWB1IxoVCR8CFZVbRWq 8SYnO2gcfosES7A1ajVk9RWysMyMlsgp4s32AXWbF5R8zanSJJlaxLKK03UBpETxgM36 nUcUqFDBIUcz6a6EMh5zmMyLvx7fQnZiUR5zNNATwBLIWNRyiEGyCFBSF3XPvZpRHmNz 3lvRY0k32LnBt/dHwFSLjXoRnfFsUUJKTV/AwHCPa7auuC9enk5A8KP7CfusrjLC+sna LEYQ== X-Forwarded-Encrypted: i=1; AL+hErEWyVA/ieK3OObOMCNWtxHkFMYZKgZvBSxqdwnHIfZnuPX6bZg/m1DYqj8vDCmaEcSbhLequ1oX63LyCK4=@vger.kernel.org X-Gm-Message-State: APuU4Z8unGI3Yv3653TVP5i0zC+o4I7W35aGhUwMm3qMquYUKQIxBb91 HHi+TBCODNhPomucq+2Y+ZG/l2qE3yls46M4X3fvMQOv72bJXdglPEM6vKhOsXWVqDw= X-Gm-Gg: AYBFou12f1I2te7pnUtnYrSPAoywoN9yQKXnnbFhXANjNhqKgHj31+JZTWapoYkfO8Z 8QwQSxE0WFzykKt6uSUXU8O03MOtzLa/kxEcoW541oiuqgBbkr6e4rEysMhlh9Sd07LH0H54mhl S9v747sqjKAfE9yEq1FvNroe30tlch4DyrW5y47tRAGQeN83ChajOhGQLbZCc2gO4ePuOor0kwv HF8svSSiuVHLYLqTmVpQqCwtK0tUZUN/ALgMPnkD4FVMpnXCJcnjcbXSGSS6SgEWQ5kEcl9+gYp B53ztDb00i2RvXAsv9ae3nXwpWZ97zUb198vBVOO/eLpUkweffGAav5jhC8segdijoA3FWRUQPb 1A385YdECM6AmfN210DrFZgZaYn6vO1s40BdiiQKeDKCJhDx+MXyA1tf7jY6YlHfaWDvZ7JRff3 exBwjKx7fDqUfO/gJDsWihPwMR7XlZ6oQ6yfeCFeo8oL/VUDre7q3KHByl+M486p97Sks4J9f9m ITLUWmCGiYNfAcZCf+IjjrVz4btWJ50HEKcEU4GBp68FxETTO4YSt/cAiv1faHpHzaJCPj5qbKb BetLayoIfCYRgLPe+mdaDxfnzByO9y7XsttXpEk9gbiuxg== X-Received: by 2002:a05:620a:bd3:b0:93c:6417:f9c8 with SMTP id af79cd13be357-949ba9b3e00mr741851985a.29.1795207912095; Fri, 20 Nov 2026 12:51:52 -0800 (PST) Received: from toxicpanda.com ([153.61.196.250]) by smtp.gmail.com with ESMTPSA id af79cd13be357-949bb3f2de5sm272377285a.29.2026.11.20.12.51.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Nov 2026 12:51:51 -0800 (PST) From: Josef Bacik Date: Fri, 20 Nov 2026 20:51:34 +0000 Subject: [PATCH v3 6/6] btrfs: don't trim the target of a device replace X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261120-b4-btrfs-data-integrity-v3-6-4de4b91b826e@toxicpanda.com> References: <20261120-b4-btrfs-data-integrity-v3-0-4de4b91b826e@toxicpanda.com> In-Reply-To: <20261120-b4-btrfs-data-integrity-v3-0-4de4b91b826e@toxicpanda.com> To: David Sterba , Chris Mason , Naohiro Aota , Johannes Thumshirn , Qu Wenruo , Boris Burkov , Filipe Manana Cc: linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Josef Bacik X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openssh-sha256; t=1795207902; l=3117; i=josef@toxicpanda.com; h=from:subject:message-id; bh=6BSEnwYPwNZRs2G7kiQGSKi44oKpPrCpybE/T7MwIc4=; b=U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAgUBr36M/n0nWN0DNbnxwzIiCZez6MG JiruuNaSCI/zXsAAAAGcGF0YXR0AAAAAAAAAAZzaGE1MTIAAABTAAAAC3NzaC1lZDI1NTE5AAAA QE9YRkkcwKTg/hUTfjKeHveiN7lDSuBlgfza409+XlcP1bYi3qHDlVEECSGH0vz3T3UpdCVtQu5 w3MHA7ojxjQg= X-Developer-Key: i=josef@toxicpanda.com; a=openssh; fpr=SHA256:C8kOX2QUJCMqnCX+KEeoqRAjLo9L+ELOSH2NSAJHqGA FITRIM also discards the space of every device that is not allocated to a chunk. btrfs_trim_free_extents() goes over fs_devices->devices and, for each device that is writeable, discards the ranges that have neither CHUNK_ALLOCATED nor CHUNK_TRIMMED set in device->alloc_state. The target device of a running replace operation is on that list and it is writeable. But its alloc_state is empty. The allocated ranges of the source device are only copied to it by btrfs_set_target_alloc_state(), when the replace finishes. Until then the whole device looks unallocated, so an fstrim discards everything that was copied to it so far, and what is being written to it in parallel to the source device. The replace finishes without any error and the source device is dropped. With a single device filesystem, 700M of data and fstrim called in a loop while the replace runs: BTRFS info: dev_replace from /dev/vdb (devid 1) to /dev/vdc finished BTRFS error: bad tree block start, mirror 1 want 30638080 have 0 BTRFS error: bad tree block start, mirror 2 want 30638080 have 0 BTRFS info: scrub: not finished on devid 1 with status: -5 Skip the target device, and don't count it in btrfs_ioctl_fitrim() either. The flag is cleared with the device list mutex held, after the alloc_state was set up, and the trimming code holds that mutex while it works on a device. Fixes: 929be17a9b49 ("btrfs: Switch btrfs_trim_free_extents to find_first_clear_extent_bit") CC: stable@vger.kernel.org Reviewed-by: Qu Wenruo Reviewed-by: Filipe Manana Signed-off-by: Josef Bacik --- fs/btrfs/extent-tree.c | 8 ++++++++ fs/btrfs/ioctl.c | 3 +++ 2 files changed, 11 insertions(+) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index 9b0a5c6b96f3..22155507f905 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -6641,6 +6641,14 @@ static int btrfs_trim_free_extents_throttle(struct btrfs_device *device, if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) || !device->bdev) return 0; + /* + * The target of a running device replace has nothing in its alloc_state + * until the replace finishes, all of it would look unallocated and we + * would discard what was already copied to it. + */ + if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) + return 0; + /* Discard not supported = nothing to do. */ if (!bdev_max_discard_sectors(device->bdev)) return 0; diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c index 651a780dd7b1..3d1ed1a5911c 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -507,6 +507,9 @@ static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info, dev_list) { if (!device->bdev || !bdev_max_discard_sectors(device->bdev)) continue; + /* Trimming skips the target of a device replace. */ + if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) + continue; num_devices++; minlen = min_t(u64, bdev_discard_granularity(device->bdev), minlen); -- 2.55.0