Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4839050pxj; Wed, 9 Jun 2021 03:12:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx0VU2hHVs+6VGYYxALByicH16g1R2LCwXK2bCiNvSaQ5OPe7AlDFbd+WpQl6NzznvElGfP X-Received: by 2002:a05:6402:1511:: with SMTP id f17mr3516111edw.377.1623233530678; Wed, 09 Jun 2021 03:12:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623233530; cv=none; d=google.com; s=arc-20160816; b=VNchHKmEul1gq2L4J3A/0BG2D+bboiq/BdZUIducvVGz/B+OXMR6znkAg7pefPhyGF BloAObDWvIdankIO5yQUM/+YpQuwAsxfxDYhf9GsFcR9Cnb1kaXhDT2qraMM5xKMXmIE Um+z2RadCIOLeVQmMewsBdTHM4z6A4mtASaEiLxS5pHnUu5nOMxl1Q1VLzKeRXuF0EQC r9PLdcjoucPlE1CG063pY8O9tTijiEvaM7oAXNYxOTq8fI95oz9Yj+FhiEaB2sO7uEUC +4b2MLs0vFtlJODlb8Yq+mJEk7qEW7BoTzc+CdNTBtbfibSvdqdyjcnpbs/R7RTdgfLv 8Nhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=LCCEGjvKh6hvCX4r9M8kpY4ol4DsIr+Hl+hWqmx6Bc0=; b=aIFvMwlA/LHDDJWD6zj0Sznv6Zp55unHN8wP06cRCKlG96MymmRnnqVRJ44qo01KE9 XpcRpUZMPixjmkfCJPzwuDNp34Tds4SsvyDtz4trBZXaRA7geczS7IgvrkWyj3f9tZSG zZH11XpoSfy8HIhuvIELc5E6TdAoGkV0UIv8X8faSLSTTPY/Szf3FK52JHvEDp9qawhc lM2XmAm6SBdWvk/k85m3N6hpKLh9eHwKJs6Xbo5fgmbotor1YX1riC7Z8/aPsYgagEJz 9eA2ycQH70TScacZQX46gS4F3/BRulWzerz1Z46/xjF18aXrBpD22330lep2BrvuBgxh TyYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=tFFgmqaT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id yd12si1849715ejb.602.2021.06.09.03.11.47; Wed, 09 Jun 2021 03:12:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=tFFgmqaT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236739AbhFHSzI (ORCPT + 99 others); Tue, 8 Jun 2021 14:55:08 -0400 Received: from mail.kernel.org ([198.145.29.99]:44348 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235786AbhFHStN (ORCPT ); Tue, 8 Jun 2021 14:49:13 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7FC6B61460; Tue, 8 Jun 2021 18:38:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1623177534; bh=Jss5QohljpTZK5YQFe2a/he3ZQtbDuqj2C0MBXBnO2o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tFFgmqaT/C9I+THPsPDjkiusuYjC/2Wg45kgYnXbT9fO+IW/iP8U3IZ+OZOsRX2+X u/wmNFPlrQxUwQEv8r8eLGtUtT0h4jB40wEGBeAgcNOER3f7bme0lZxJ/kPHCgUIum LLUJLN7KyFroQaBOEpsjy1fXDtCp+ZBlQlx+b4ys= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Chris Murphy , Filipe Manana , Anand Jain , David Sterba , Sudip Mukherjee Subject: [PATCH 5.4 69/78] btrfs: fix unmountable seed device after fstrim Date: Tue, 8 Jun 2021 20:27:38 +0200 Message-Id: <20210608175937.602886335@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210608175935.254388043@linuxfoundation.org> References: <20210608175935.254388043@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Anand Jain commit 5e753a817b2d5991dfe8a801b7b1e8e79a1c5a20 upstream. The following test case reproduces an issue of wrongly freeing in-use blocks on the readonly seed device when fstrim is called on the rw sprout device. As shown below. Create a seed device and add a sprout device to it: $ mkfs.btrfs -fq -dsingle -msingle /dev/loop0 $ btrfstune -S 1 /dev/loop0 $ mount /dev/loop0 /btrfs $ btrfs dev add -f /dev/loop1 /btrfs BTRFS info (device loop0): relocating block group 290455552 flags system BTRFS info (device loop0): relocating block group 1048576 flags system BTRFS info (device loop0): disk added /dev/loop1 $ umount /btrfs Mount the sprout device and run fstrim: $ mount /dev/loop1 /btrfs $ fstrim /btrfs $ umount /btrfs Now try to mount the seed device, and it fails: $ mount /dev/loop0 /btrfs mount: /btrfs: wrong fs type, bad option, bad superblock on /dev/loop0, missing codepage or helper program, or other error. Block 5292032 is missing on the readonly seed device: $ dmesg -kt | tail BTRFS error (device loop0): bad tree block start, want 5292032 have 0 BTRFS warning (device loop0): couldn't read-tree root BTRFS error (device loop0): open_ctree failed >From the dump-tree of the seed device (taken before the fstrim). Block 5292032 belonged to the block group starting at 5242880: $ btrfs inspect dump-tree -e /dev/loop0 | grep -A1 BLOCK_GROUP item 3 key (5242880 BLOCK_GROUP_ITEM 8388608) itemoff 16169 itemsize 24 block group used 114688 chunk_objectid 256 flags METADATA >From the dump-tree of the sprout device (taken before the fstrim). fstrim used block-group 5242880 to find the related free space to free: $ btrfs inspect dump-tree -e /dev/loop1 | grep -A1 BLOCK_GROUP item 1 key (5242880 BLOCK_GROUP_ITEM 8388608) itemoff 16226 itemsize 24 block group used 32768 chunk_objectid 256 flags METADATA BPF kernel tracing the fstrim command finds the missing block 5292032 within the range of the discarded blocks as below: kprobe:btrfs_discard_extent { printf("freeing start %llu end %llu num_bytes %llu:\n", arg1, arg1+arg2, arg2); } freeing start 5259264 end 5406720 num_bytes 147456 Fix this by avoiding the discard command to the readonly seed device. Reported-by: Chris Murphy CC: stable@vger.kernel.org # 4.4+ Reviewed-by: Filipe Manana Signed-off-by: Anand Jain Signed-off-by: David Sterba Signed-off-by: Sudip Mukherjee Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/extent-tree.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -1338,16 +1338,20 @@ int btrfs_discard_extent(struct btrfs_fs for (i = 0; i < bbio->num_stripes; i++, stripe++) { u64 bytes; struct request_queue *req_q; + struct btrfs_device *device = stripe->dev; - if (!stripe->dev->bdev) { + if (!device->bdev) { ASSERT(btrfs_test_opt(fs_info, DEGRADED)); continue; } - req_q = bdev_get_queue(stripe->dev->bdev); + req_q = bdev_get_queue(device->bdev); if (!blk_queue_discard(req_q)) continue; - ret = btrfs_issue_discard(stripe->dev->bdev, + if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) + continue; + + ret = btrfs_issue_discard(device->bdev, stripe->physical, stripe->length, &bytes);