From nobody Thu Feb 12 22:05:37 2026 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4AD2115FA78 for ; Fri, 7 Jun 2024 04:27:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.187 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717734468; cv=none; b=X3NTLIQroFxAOdG9oa86h97csrQPhNFHTO8LDTne6Tft+eUTLllUplGUaths8gmfy9A4Y8LSsbJxE9CZ4WdxGF+tiOCFddYmohkitzUI2Jxa4x6WKLHjyYT5NmRjalAkpwEfYexvKzS6SiZPRgeapXCHKn2A9YCJycURv8T8zco= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717734468; c=relaxed/simple; bh=y8ElvKpX/zdh0qFJL1BVe/8KgSgB0DT3jAdj/gBG5Lc=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=dhQygUqCP6Dq+s9j6IjnfB9ToZbwQv4WquSqcTlZbP2ZmJA0XCCPhFYGSkj2EyvHHH9Mg89af9UIGFLBxLnz0OiIq6jle/1OORP9tlhrRPnq39lqNOJ2tRBfplRGne6K0BfoU81YB6J+rzw3EcQuICYzyVuRCSCT/xbT8g/SRWM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.252]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4VwSkQ4KjXzsTSr; Fri, 7 Jun 2024 12:23:42 +0800 (CST) Received: from kwepemm600013.china.huawei.com (unknown [7.193.23.68]) by mail.maildlp.com (Postfix) with ESMTPS id 43225180085; Fri, 7 Jun 2024 12:27:41 +0800 (CST) Received: from huawei.com (10.175.104.67) by kwepemm600013.china.huawei.com (7.193.23.68) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Fri, 7 Jun 2024 12:27:31 +0800 From: Zhihao Cheng To: , , , , , CC: , Subject: [RFC PATCH mtd-utils 087/110] fsck.ubifs: Check and correct files' information Date: Fri, 7 Jun 2024 12:25:52 +0800 Message-ID: <20240607042615.2069840-88-chengzhihao1@huawei.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240607042615.2069840-1-chengzhihao1@huawei.com> References: <20240607042615.2069840-1-chengzhihao1@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemm600013.china.huawei.com (7.193.23.68) Content-Type: text/plain; charset="utf-8" From: Xiang Yang This is the 10/18 step of fsck. Check and handle inconsistent files, the checking rule is same as rebuild mode which has been implemented in check_and_correct_files, but the methods of handling are different: 1. Correct the file information for safe mode, danger mode and normal mode with 'yes' answer, other modes will exit. Signed-off-by: Xiang Yang Signed-off-by: Zhihao Cheng --- ubifs-utils/fsck.ubifs/extract_files.c | 76 ++++++++++++++++++++++--------= ---- ubifs-utils/fsck.ubifs/fsck.ubifs.c | 8 ++++ ubifs-utils/fsck.ubifs/fsck.ubifs.h | 2 +- ubifs-utils/fsck.ubifs/problem.c | 16 +++++++ 4 files changed, 74 insertions(+), 28 deletions(-) diff --git a/ubifs-utils/fsck.ubifs/extract_files.c b/ubifs-utils/fsck.ubif= s/extract_files.c index b24445be..c83d3774 100644 --- a/ubifs-utils/fsck.ubifs/extract_files.c +++ b/ubifs-utils/fsck.ubifs/extract_files.c @@ -1292,8 +1292,8 @@ reachable: * data nodes and truncation node. The calculated informaion will be used * to correct inode node. */ -static void calculate_file_info(struct ubifs_info *c, struct scanned_file = *file, - struct rb_root *file_tree) +static int calculate_file_info(struct ubifs_info *c, struct scanned_file *= file, + struct rb_root *file_tree) { int nlink =3D 0; bool corrupted_truncation =3D false; @@ -1306,15 +1306,24 @@ static void calculate_file_info(struct ubifs_info *= c, struct scanned_file *file, =20 for (node =3D rb_first(&file->xattr_files); node; node =3D rb_next(node))= { xattr_file =3D rb_entry(node, struct scanned_file, rb); + dent_node =3D rb_entry(rb_first(&xattr_file->dent_nodes), + struct scanned_dent_node, rb); =20 + ubifs_assert(c, xattr_file->ino.is_xattr); ubifs_assert(c, !rb_first(&xattr_file->xattr_files)); - calculate_file_info(c, xattr_file, file_tree); + xattr_file->calc_nlink =3D 1; + xattr_file->calc_size =3D xattr_file->ino.size; + + file->calc_xcnt +=3D 1; + file->calc_xsz +=3D CALC_DENT_SIZE(dent_node->nlen); + file->calc_xsz +=3D CALC_XATTR_BYTES(xattr_file->ino.size); + file->calc_xnms +=3D dent_node->nlen; } =20 if (file->inum =3D=3D UBIFS_ROOT_INO) { file->calc_nlink +=3D 2; file->calc_size +=3D UBIFS_INO_NODE_SZ; - return; + return 0; } =20 if (S_ISDIR(file->ino.mode)) { @@ -1326,29 +1335,11 @@ static void calculate_file_info(struct ubifs_info *= c, struct scanned_file *file, parent_file =3D lookup_file(file_tree, key_inum(c, &dent_node->key)); if (!parent_file) { ubifs_assert(c, 0); - return; + return 0; } parent_file->calc_nlink +=3D 1; parent_file->calc_size +=3D CALC_DENT_SIZE(dent_node->nlen); - return; - } - - if (file->ino.is_xattr) { - file->calc_nlink =3D 1; - file->calc_size =3D file->ino.size; - - dent_node =3D rb_entry(rb_first(&file->dent_nodes), - struct scanned_dent_node, rb); - parent_file =3D lookup_file(file_tree, key_inum(c, &dent_node->key)); - if (!parent_file) { - ubifs_assert(c, 0); - return; - } - parent_file->calc_xcnt +=3D 1; - parent_file->calc_xsz +=3D CALC_DENT_SIZE(dent_node->nlen); - parent_file->calc_xsz +=3D CALC_XATTR_BYTES(file->ino.size); - parent_file->calc_xnms +=3D dent_node->nlen; - return; + return 0; } =20 for (node =3D rb_first(&file->dent_nodes); node; node =3D rb_next(node)) { @@ -1359,7 +1350,7 @@ static void calculate_file_info(struct ubifs_info *c,= struct scanned_file *file, parent_file =3D lookup_file(file_tree, key_inum(c, &dent_node->key)); if (!parent_file) { ubifs_assert(c, 0); - return; + return 0; } parent_file->calc_size +=3D CALC_DENT_SIZE(dent_node->nlen); } @@ -1368,7 +1359,7 @@ static void calculate_file_info(struct ubifs_info *c,= struct scanned_file *file, if (!S_ISREG(file->ino.mode)) { /* No need to verify i_size for symlink/sock/block/char/fifo. */ file->calc_size =3D file->ino.size; - return; + return 0; } =20 /* @@ -1452,10 +1443,22 @@ drop_data: data_node =3D list_entry(drop_list.next, struct scanned_data_node, list); =20 + if (FSCK(c)->mode !=3D REBUILD_MODE) { + /* + * Don't ask, inconsistent file correcting will be + * asked in function correct_file_info(). + */ + int err =3D delete_node(c, &data_node->key, + data_node->header.lnum, data_node->header.offs); + if (err) + return err; + } list_del(&data_node->list); rb_erase(&data_node->rb, &file->data_nodes); kfree(data_node); } + + return 0; } =20 /** @@ -1490,6 +1493,7 @@ static int correct_file_info(struct ubifs_info *c, st= ruct scanned_file *file) file->calc_size =3D=3D file->ino.size) return 0; =20 + handle_invalid_file(c, FILE_IS_INCONSISTENT, file, NULL); lnum =3D file->ino.header.lnum; dbg_fsck("correct file(inum:%lu type:%s), nlink %u->%u, xattr cnt %u->%u,= xattr size %u->%u, xattr names %u->%u, size %llu->%llu, at %d:%d, in %s", file->inum, file->ino.is_xattr ? "xattr" : @@ -1537,7 +1541,9 @@ int check_and_correct_files(struct ubifs_info *c) for (node =3D rb_first(tree); node; node =3D rb_next(node)) { file =3D rb_entry(node, struct scanned_file, rb); =20 - calculate_file_info(c, file, tree); + err =3D calculate_file_info(c, file, tree); + if (err) + return err; } =20 for (node =3D rb_first(tree); node; node =3D rb_next(node)) { @@ -1548,5 +1554,21 @@ int check_and_correct_files(struct ubifs_info *c) return err; } =20 + if (list_empty(&FSCK(c)->disconnected_files)) + return 0; + + ubifs_assert(c, FSCK(c)->mode !=3D REBUILD_MODE); + list_for_each_entry(file, &FSCK(c)->disconnected_files, list) { + err =3D calculate_file_info(c, file, tree); + if (err) + return err; + + /* Reset disconnected file's nlink as one. */ + file->calc_nlink =3D 1; + err =3D correct_file_info(c, file); + if (err) + return err; + } + return 0; } diff --git a/ubifs-utils/fsck.ubifs/fsck.ubifs.c b/ubifs-utils/fsck.ubifs/f= sck.ubifs.c index fd4890a0..85a5baf0 100644 --- a/ubifs-utils/fsck.ubifs/fsck.ubifs.c +++ b/ubifs-utils/fsck.ubifs/fsck.ubifs.c @@ -460,6 +460,13 @@ static int do_fsck(void) goto free_disconnected_files; } =20 + log_out(c, "Check and correct files"); + err =3D check_and_correct_files(c); + if (err) { + exit_code |=3D FSCK_ERROR; + goto free_disconnected_files; + } + free_disconnected_files: destroy_file_list(c, &FSCK(c)->disconnected_files); free_used_lebs: @@ -504,6 +511,7 @@ int main(int argc, char *argv[]) * Step 7: Update files' size * Step 8: Check and handle invalid files * Step 9: Check and handle unreachable files + * Step 10: Check and correct files */ err =3D do_fsck(); if (err && FSCK(c)->try_rebuild) { diff --git a/ubifs-utils/fsck.ubifs/fsck.ubifs.h b/ubifs-utils/fsck.ubifs/f= sck.ubifs.h index ba4771a3..d1423c01 100644 --- a/ubifs-utils/fsck.ubifs/fsck.ubifs.h +++ b/ubifs-utils/fsck.ubifs/fsck.ubifs.h @@ -43,7 +43,7 @@ enum { SB_CORRUPTED =3D 0, MST_CORRUPTED, LOG_CORRUPTED, = BUD_CORRUPTED, FILE_HAS_0_NLINK_INODE, FILE_HAS_INCONSIST_TYPE, FILE_HAS_TOO_MANY_= DENT, FILE_SHOULDNT_HAVE_DATA, FILE_HAS_NO_DENT, XATTR_HAS_NO_HOST, XATTR_HAS_WRONG_HOST, FILE_HAS_NO_ENCRYPT, FILE_IS_DISCONNECTED, - FILE_ROOT_HAS_DENT, DENTRY_IS_UNREACHABLE }; + FILE_ROOT_HAS_DENT, DENTRY_IS_UNREACHABLE, FILE_IS_INCONSISTENT }; =20 enum { HAS_DATA_CORRUPTED =3D 1, HAS_TNC_CORRUPTED =3D 2 }; =20 diff --git a/ubifs-utils/fsck.ubifs/problem.c b/ubifs-utils/fsck.ubifs/prob= lem.c index 0395a34f..e8f08606 100644 --- a/ubifs-utils/fsck.ubifs/problem.c +++ b/ubifs-utils/fsck.ubifs/problem.c @@ -59,6 +59,7 @@ static const struct fsck_problem problem_table[] =3D { {PROBLEM_FIXABLE | PROBLEM_MUST_FIX, "File is disconnected(regular file w= ithout dentries)"}, // FILE_IS_DISCONNECTED {PROBLEM_FIXABLE | PROBLEM_MUST_FIX | PROBLEM_DROP_DATA, "Root dir should= not have a dentry"}, // FILE_ROOT_HAS_DENT {PROBLEM_FIXABLE | PROBLEM_MUST_FIX | PROBLEM_DROP_DATA, "Dentry is unrea= chable"}, // DENTRY_IS_UNREACHABLE + {PROBLEM_FIXABLE | PROBLEM_MUST_FIX, "File is inconsistent"}, // FILE_IS_= INCONSISTENT }; =20 static const char *get_question(const struct fsck_problem *problem, @@ -212,6 +213,21 @@ static void print_problem(const struct ubifs_info *c, key_type(c, &dent_node->key) =3D=3D UBIFS_XENT_KEY ? "(xattr)" : ""); break; } + case FILE_IS_INCONSISTENT: + { + const struct invalid_file_problem *ifp =3D (const struct invalid_file_pr= oblem *)priv; + const struct scanned_file *file =3D ifp->file; + + log_out(c, "problem: %s, ino %lu type %s, nlink %u xcnt %u xsz %u xnms %= u size %llu, " + "should be nlink %u xcnt %u xsz %u xnms %u size %llu", + problem->desc, file->inum, + file->ino.is_xattr ? "xattr" : ubifs_get_type_name(ubifs_get_dent_type(= file->ino.mode)), + file->ino.nlink, file->ino.xcnt, file->ino.xsz, + file->ino.xnms, file->ino.size, + file->calc_nlink, file->calc_xcnt, file->calc_xsz, + file->calc_xnms, file->calc_size); + break; + } default: log_out(c, "problem: %s", problem->desc); break; --=20 2.13.6