Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp456560pxb; Thu, 9 Sep 2021 05:02:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxoJFufRfS5kdsE8LDmpLu5GDFV/w69GIwnRECtLmEStqtxyhcJO+p203DMMYPL6zQNyOWK X-Received: by 2002:a5e:a813:: with SMTP id c19mr2371971ioa.199.1631188942479; Thu, 09 Sep 2021 05:02:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631188942; cv=none; d=google.com; s=arc-20160816; b=hDCUj0+e5b/6kOJMj7yxULotJMzE64nUN+jdhg8KkvDeyt+DVrnDqB8yZ9xuzvGFis LsUa4Tqn9AaarOHia4CSYK8aEapz010/RdsIL8tu2FcET6ZYKkhJBk9Syl3HMPNGsYT4 VUCKsWc7RYH8zPIiiSv6e4tukp1I9rv4VsdBnLPWo+0NxV4MWu93zLoNuYp/TrOxLHOJ wqIgbr6gLuOEJWYaqSdFxfhLvwaubI1Nrel6rJI4SEng8P2bxsrdZxar/xnlxoQ7R22k 8vuGQKM1t/RAtaxPTMxSolWw1FDAkR2Mv8opJJAznEpZ/ytlgj3Iqu9tCmitBB7k3+Cf acQw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=y+bB2XoWK8QDSu4Feof5alcgDraNbrvwxDYoT7DzTXc=; b=k02GTqc95bXSfNhH34cGzBiUD5dM5mMRlKGsggSu71F3rL01yoDEuDKnO+TS3y3zBV 9sEXpqYoPs2GJhuuQ3L6f25U2Jt0Ltb6FJthn9StDR/QDfN17dGjnly88XhEO6+SlGHt bbyBCls1EQRlASy4XGKLrRYzt7L6kMYfc2mm4TgoDg8yVtyU1VKkh0ak1OOvJrGPkP9v 1yHud6V3sdAeK1iMWnaDd3J9LcufsRka/vHe2u51Jb3uIFJmZauBvQzNyfol+19KUfpl 8nGvF5ird6AGAQfc5U34LefLaffNVB2D6vHsGBIsnR2FhbQ8+PFNps3RqlPnahaOQxLY sXMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=K8cIkzSf; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 132si1634903jay.7.2021.09.09.05.02.08; Thu, 09 Sep 2021 05:02:22 -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=@kernel.org header.s=k20201202 header.b=K8cIkzSf; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344209AbhIIMCJ (ORCPT + 99 others); Thu, 9 Sep 2021 08:02:09 -0400 Received: from mail.kernel.org ([198.145.29.99]:34406 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239669AbhIIL4d (ORCPT ); Thu, 9 Sep 2021 07:56:33 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id B7013613C8; Thu, 9 Sep 2021 11:45:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1631187918; bh=Q7+PDq3PcKfGT0wocrGwCTzWvfRbCJ1Xva38avQb21A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=K8cIkzSflDmK/l7N0q06BZBUakJvGZ403/4zn2t75dWGT+LhjyZmV1uuJXaPM+a1r uLgcTr/kYkRMh7iIYCskpaqVWh5qiH8L8F6xza85rFUoK+PxeBy34br7nK2CQZGZVR Kh3pKZBu2Kwwp9gCwJd4nugqiDnsLsJW30qdKuwNEvBBK06K9BM+B57ivi5CYV50XJ BJ8VeBdZ2KZ/TBSLc+K3BW8Dy8j2OumKAFcYwGN/UeiLEJevaHOJ39GvrOcFOJF6Kg 2jwlcGB3vdFg49/PJrGcXDxr73QtJ4Ry97LHgvJYR+/1eKq2EtjuugZo9ymgGAM74o AEF9tdArE3kFQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Qu Wenruo , Anand Jain , David Sterba , Sasha Levin , linux-btrfs@vger.kernel.org Subject: [PATCH AUTOSEL 5.14 193/252] btrfs: grab correct extent map for subpage compressed extent read Date: Thu, 9 Sep 2021 07:40:07 -0400 Message-Id: <20210909114106.141462-193-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210909114106.141462-1-sashal@kernel.org> References: <20210909114106.141462-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Qu Wenruo [ Upstream commit 557023ea9f06baf2659b232b08b8e8711f7001a6 ] [BUG] When subpage compressed read write support is enabled, btrfs/038 always fails with EIO. A simplified script can easily trigger the problem: mkfs.btrfs -f -s 4k $dev mount $dev $mnt -o compress=lzo xfs_io -f -c "truncate 118811" $mnt/foo xfs_io -c "pwrite -S 0x0d -b 39987 92267 39987" $mnt/foo > /dev/null sync btrfs subvolume snapshot -r $mnt $mnt/mysnap1 xfs_io -c "pwrite -S 0x3e -b 80000 200000 80000" $mnt/foo > /dev/null sync xfs_io -c "pwrite -S 0xdc -b 10000 250000 10000" $mnt/foo > /dev/null xfs_io -c "pwrite -S 0xff -b 10000 300000 10000" $mnt/foo > /dev/null sync btrfs subvolume snapshot -r $mnt $mnt/mysnap2 cat $mnt/mysnap2/foo # Above cat will fail due to EIO [CAUSE] The problem is in btrfs_submit_compressed_read(). When it tries to grab the extent map of the read range, it uses the following call: em = lookup_extent_mapping(em_tree, page_offset(bio_first_page_all(bio)), fs_info->sectorsize); The problem is in the page_offset(bio_first_page_all(bio)) part. The offending inode has the following file extent layout item 10 key (257 EXTENT_DATA 131072) itemoff 15639 itemsize 53 generation 8 type 1 (regular) extent data disk byte 13680640 nr 4096 extent data offset 0 nr 4096 ram 4096 extent compression 0 (none) item 11 key (257 EXTENT_DATA 135168) itemoff 15586 itemsize 53 generation 8 type 1 (regular) extent data disk byte 0 nr 0 item 12 key (257 EXTENT_DATA 196608) itemoff 15533 itemsize 53 generation 8 type 1 (regular) extent data disk byte 13676544 nr 4096 extent data offset 0 nr 53248 ram 86016 extent compression 2 (lzo) And the bio passed in has the following parameters: page_offset(bio_first_page_all(bio)) = 131072 bio_first_bvec_all(bio)->bv_offset = 65536 If we use page_offset(bio_first_page_all(bio) without adding bv_offset, we will get an extent map for file offset 131072, not 196608. This means we read uncompressed data from disk, and later decompression will definitely fail. [FIX] Take bv_offset into consideration when trying to grab an extent map. And add an ASSERT() to ensure we're really getting a compressed extent. Thankfully this won't affect anything but subpage, thus we only need to ensure this patch get merged before we enabled basic subpage support. Reviewed-by: Anand Jain Signed-off-by: Qu Wenruo Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/compression.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c index 30d82cdf128c..ad700cf287c9 100644 --- a/fs/btrfs/compression.c +++ b/fs/btrfs/compression.c @@ -673,6 +673,7 @@ blk_status_t btrfs_submit_compressed_read(struct inode *inode, struct bio *bio, struct page *page; struct bio *comp_bio; u64 cur_disk_byte = bio->bi_iter.bi_sector << 9; + u64 file_offset; u64 em_len; u64 em_start; struct extent_map *em; @@ -682,15 +683,17 @@ blk_status_t btrfs_submit_compressed_read(struct inode *inode, struct bio *bio, em_tree = &BTRFS_I(inode)->extent_tree; + file_offset = bio_first_bvec_all(bio)->bv_offset + + page_offset(bio_first_page_all(bio)); + /* we need the actual starting offset of this extent in the file */ read_lock(&em_tree->lock); - em = lookup_extent_mapping(em_tree, - page_offset(bio_first_page_all(bio)), - fs_info->sectorsize); + em = lookup_extent_mapping(em_tree, file_offset, fs_info->sectorsize); read_unlock(&em_tree->lock); if (!em) return BLK_STS_IOERR; + ASSERT(em->compress_type != BTRFS_COMPRESS_NONE); compressed_len = em->block_len; cb = kmalloc(compressed_bio_size(fs_info, compressed_len), GFP_NOFS); if (!cb) -- 2.30.2