From patchwork Sat Oct 22 07:20:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 7427 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp1097840wrr; Sat, 22 Oct 2022 01:08:53 -0700 (PDT) X-Google-Smtp-Source: AMsMyM77osbr6K75uIKLXDYQLMMzqHSVGK9stmO9v8otT3YKkzHskSiWFLVhg6i2gmnVhUlYzeQZ X-Received: by 2002:a17:902:7408:b0:186:88be:1cca with SMTP id g8-20020a170902740800b0018688be1ccamr2864259pll.155.1666426133586; Sat, 22 Oct 2022 01:08:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666426133; cv=none; d=google.com; s=arc-20160816; b=eY1/orrnC2a5fSmDrAzrsYxBAblfCQ8psPb0InRdw7RNO5863hTaUb7Ql/5l9E/T83 5jvZTETSRqyskNv+4G2W1d/jq4yee2JeWkdJoDxhyEicQsF+lrbhxkLsNKVQIjSmry6g X6Ingc00FF4xofBASCwYC16A7xOqVo9yiBAXj2q0kZ7LbTQuqUYuZLEREYJIOCEipIgh a9j/Gb7ypgxV/EK0+c8Ubqa+P7g/Ym2H300WKADSCYt9wlbYe83OqqnYHcFcrgqwabKf UXIoY0q+nKT5J9wVsy8oW6yhzChyDeEON9t0SZULfOUgeGG+C561aEE3I272QFJbJR5/ bRKA== 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=01QJ14LeMpd1FNFss190nAQVI4dAt+8gXh5aDxGKpRk=; b=csDCSph8excfCiP7rIB+pIzxwdcNwj+Bu6WK0Nru08Onl8KJANWHA13zydEo5PRgmm 0G61zgbRGsXAl3mcPMy6ZruIOXY6GaIfyfU+67ynCvtxiXlZkp9ukACzLC+mX3kvDVH0 D8ZB/OrPq8twfVZ1N2IStRcGIzRscqW7Zqvf0TMi63NtWFuQOY4JxDnRiUlIiEyTkRi8 PBPY9iYNcgsmFwoCImMnO5P+FXNRYAlSL7U3/W9sAY0ptrTpY6n7MSAZJs7amjSf8m6f T6JgOqXf5jOpnsazcCik3qfvkSRsNgzRPf9VP4KKqpb0EEZM5YN0kuncmFEW9uRBJlFz +tJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=HRWv8JX6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 16-20020aa79150000000b0055f35357a40si24397289pfi.122.2022.10.22.01.08.41; Sat, 22 Oct 2022 01:08:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=HRWv8JX6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S233040AbiJVIIL (ORCPT + 99 others); Sat, 22 Oct 2022 04:08:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57476 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232862AbiJVIET (ORCPT ); Sat, 22 Oct 2022 04:04:19 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 478ED2D2C1B; Sat, 22 Oct 2022 00:51:45 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 9663AB82DFB; Sat, 22 Oct 2022 07:41:15 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0E2BBC433C1; Sat, 22 Oct 2022 07:41:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666424474; bh=HJEF7endQpM+60VPzFKP7f1JvxvvavZo44m56jkX12A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HRWv8JX6/w93BX/QkPwAvqqocsg7JKqVIw77VNmerIEkFlnuE1uPyQBnQFGPpIwrH Tn6Lv6GXEP4x2fqpGXXGYeHnJ3C2D3GcnUgFtlo0asE90pkmV0UhnCbfTX8QvguEZZ QLYtY+DIfSQ03MYmZegRHK4YyFIpws1vC5mrm5io= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Zhihao Cheng , Jan Kara , Theodore Tso Subject: [PATCH 5.19 132/717] ext4: fix dir corruption when ext4_dx_add_entry() fails Date: Sat, 22 Oct 2022 09:20:11 +0200 Message-Id: <20221022072438.874019929@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221022072415.034382448@linuxfoundation.org> References: <20221022072415.034382448@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747374449874404231?= X-GMAIL-MSGID: =?utf-8?q?1747374449874404231?= From: Zhihao Cheng commit 7177dd009c7c04290891e9a534cd47d1b620bd04 upstream. Following process may lead to fs corruption: 1. ext4_create(dir/foo) ext4_add_nondir ext4_add_entry ext4_dx_add_entry a. add_dirent_to_buf ext4_mark_inode_dirty ext4_handle_dirty_metadata // dir inode bh is recorded into journal b. ext4_append // dx_get_count(entries) == dx_get_limit(entries) ext4_bread(EXT4_GET_BLOCKS_CREATE) ext4_getblk ext4_map_blocks ext4_ext_map_blocks ext4_mb_new_blocks dquot_alloc_block dquot_alloc_space_nodirty inode_add_bytes // update dir's i_blocks ext4_ext_insert_extent ext4_ext_dirty // record extent bh into journal ext4_handle_dirty_metadata(bh) // record new block into journal inode->i_size += inode->i_sb->s_blocksize // new size(in mem) c. ext4_handle_dirty_dx_node(bh2) // record dir's new block(dx_node) into journal d. ext4_handle_dirty_dx_node((frame - 1)->bh) e. ext4_handle_dirty_dx_node(frame->bh) f. do_split // ret err! g. add_dirent_to_buf ext4_mark_inode_dirty(dir) // update raw_inode on disk(skipped) 2. fsck -a /dev/sdb drop last block(dx_node) which beyonds dir's i_size. /dev/sdb: recovering journal /dev/sdb contains a file system with errors, check forced. /dev/sdb: Inode 12, end of extent exceeds allowed value (logical block 128, physical block 3938, len 1) 3. fsck -fn /dev/sdb dx_node->entry[i].blk > dir->i_size Pass 2: Checking directory structure Problem in HTREE directory inode 12 (/dir): bad block number 128. Clear HTree index? no Problem in HTREE directory inode 12: block #3 has invalid depth (2) Problem in HTREE directory inode 12: block #3 has bad max hash Problem in HTREE directory inode 12: block #3 not referenced Fix it by marking inode dirty directly inside ext4_append(). Fetch a reproducer in [Link]. Link: https://bugzilla.kernel.org/show_bug.cgi?id=216466 Cc: stable@vger.kernel.org Signed-off-by: Zhihao Cheng Reviewed-by: Jan Kara Link: https://lore.kernel.org/r/20220911045204.516460-1-chengzhihao1@huawei.com Signed-off-by: Theodore Ts'o Signed-off-by: Greg Kroah-Hartman --- fs/ext4/namei.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -85,15 +85,20 @@ static struct buffer_head *ext4_append(h return bh; inode->i_size += inode->i_sb->s_blocksize; EXT4_I(inode)->i_disksize = inode->i_size; + err = ext4_mark_inode_dirty(handle, inode); + if (err) + goto out; BUFFER_TRACE(bh, "get_write_access"); err = ext4_journal_get_write_access(handle, inode->i_sb, bh, EXT4_JTR_NONE); - if (err) { - brelse(bh); - ext4_std_error(inode->i_sb, err); - return ERR_PTR(err); - } + if (err) + goto out; return bh; + +out: + brelse(bh); + ext4_std_error(inode->i_sb, err); + return ERR_PTR(err); } static int ext4_dx_csum_verify(struct inode *inode,