Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp3813623rwn; Sat, 10 Sep 2022 21:54:05 -0700 (PDT) X-Google-Smtp-Source: AA6agR7wPE15FbZeGlF4JoY/e3GmqoAylxl2I5HfC7HWkTlUs3e+rRX6x5zYYSaQqSDTperp6HPs X-Received: by 2002:a17:902:d589:b0:174:63e0:5a5c with SMTP id k9-20020a170902d58900b0017463e05a5cmr20977163plh.5.1662872045630; Sat, 10 Sep 2022 21:54:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662872045; cv=none; d=google.com; s=arc-20160816; b=olKcEGYVOAwkj27VEIpPJpoOAAGawP69fK3dcYTGbZkUqcqrTMvjUOA1Ot0ly1UytH +4H47qc09GX8yxUdDFg5J01oxQNeS3VDREFsPPxmOGNT8Xp4pbJmMa84NKWs5VfrL3fw W10GbMvslEsHuB5Fsk7CrCO+rZZc6Gq8cCtTo9dT8+r5/pfGx2XNge/rc1D7c2zOmDLO pHyzdCRr0UBe9L9KfhqegE3uRgTxKV+NGl9u2FFD9o3QEZCrxGyJubL1n98fcGPwo3nF suRSt+LthwDWe9Ozr1utDNVsSzGtGXdIGM86x5C8IT9wfKvAqX0D4pIgW6tT4uvle52J DXBQ== 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 :message-id:date:subject:cc:to:from; bh=YpPovvtFFABaiSNPRJbVJSW0n3X/BNrkglEQaKB2PKc=; b=c9O3N4I5eGvOoujAhokrPFE2ZdgyntDVdbt8okLadciIhlhJ2ziWYFaUytDyAQb7ug oNmCRxz66OSraBNjvuLa45/Iy+16WpyU3vIfLP3fQEZ59sEKOuw5CJOJNf9/wTcRHoOl 1BRqWRegPwKDhMmZCVgvMnLwRASZU3YYhis3/huTk5U9ygf+2kPy3m4n60VG7IvqpWEU NCQAXQM6QLVpy0daqG+iGrGw9EJ0WlSNtTC2Z2bRzWGWlNTuA8JTV9JXtA+aiFiVo93P oYYwkK/59QUjra1WDzaFSqHmXto2BUZ3MhgeGa+OB/BWRiO2vkmcwUIFYqyb5/X9uKeV Eebw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z12-20020a056a00240c00b00542d64d4f76si964147pfh.27.2022.09.10.21.53.41; Sat, 10 Sep 2022 21:54:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-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; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229892AbiIKElK (ORCPT + 99 others); Sun, 11 Sep 2022 00:41:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46914 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229636AbiIKElJ (ORCPT ); Sun, 11 Sep 2022 00:41:09 -0400 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F8B541D17; Sat, 10 Sep 2022 21:41:07 -0700 (PDT) Received: from dggemv711-chm.china.huawei.com (unknown [172.30.72.57]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4MQH7G6hG9zHnkn; Sun, 11 Sep 2022 12:39:06 +0800 (CST) Received: from kwepemm600013.china.huawei.com (7.193.23.68) by dggemv711-chm.china.huawei.com (10.1.198.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Sun, 11 Sep 2022 12:41:05 +0800 Received: from huawei.com (10.175.127.227) by kwepemm600013.china.huawei.com (7.193.23.68) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Sun, 11 Sep 2022 12:41:04 +0800 From: Zhihao Cheng To: , , CC: , , , Subject: [PATCH v2] ext4: Fix dir corruption when ext4_dx_add_entry() fails Date: Sun, 11 Sep 2022 12:52:04 +0800 Message-ID: <20220911045204.516460-1-chengzhihao1@huawei.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.127.227] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To kwepemm600013.china.huawei.com (7.193.23.68) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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-ext4@vger.kernel.org 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 --- v1->v2: mark inode dirty inside ext4_append(). fs/ext4/namei.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c index 3a31b662f661..0d0e41d2dee8 100644 --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -85,15 +85,20 @@ static struct buffer_head *ext4_append(handle_t *handle, 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, -- 2.31.1