Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp812137lqt; Thu, 6 Jun 2024 21:47:26 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUp9U0n1zDVp05coGkSYiaz3bb/FmExt7Wwb+0Np1DiDvQR9LkbsDvbQGhIPrj8c1HnK2RUDxx9/VwwwckyR8yaEXMM51byzJP4MahXKQ== X-Google-Smtp-Source: AGHT+IFXApSoBH/ePqphE48rcHduVMFKrO9uxF7IJWo1am9C3y1DVZ0nZS6R5LL3BXt68bIO3E1s X-Received: by 2002:a17:903:32cf:b0:1f6:4b4f:c0a3 with SMTP id d9443c01a7336-1f6d02f4872mr18594155ad.34.1717735646485; Thu, 06 Jun 2024 21:47:26 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717735646; cv=pass; d=google.com; s=arc-20160816; b=sUm8q7Jn1eYLuz4F0vIzXXpBLiwlJc1F2vyq88on5CocXN/DhI+ZhoLkgQAVX70Z1J aiL+rbee9Km3om7ROUKk5ulJCgma0q6EDzqcw0eH+nkFuLdSb1paK3DUR22ZErmnn8bx VB54dhYAtFjZiit4YD6pHHf8rDX1zticxd74aX3FASJExocSfBYjZuUZrUQ0yblfTeAB 3pO04nCJvHGexJ2Fz9IQX5kNFhXft6CxMPKMXYwHxypenh3KpjqS9DnyDqCFjCSH59Xn 83IjEUHKtSo7oJ7HlnzEj/lOjlkvXRnCiqu2wvWhB2KvRDb1bpQdvIDhIvb7yXvpN/bx JUaQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=iCZ6yH4mzp/0+3sJR0PQyflZFs79iIYflMDCyLUgUzg=; fh=yXYChwFvQA/NaaLzCC0IJdkW11OhjXgO4DHXzVmnGFw=; b=0QUhJN7XY7ZUMCxipCbZw4kiGgrGH/IMeyDzrcc1nxy4qLjMl/oVpFtnAGEI1b9rdk XXEbJ7cUlbTsadIN7KejUkLHPaE4Nokzz8ySYffds2ZRJlclxVPb/5ivLRRE+TrQ4gwT 2m8+1yf4qbkAGK6Urlpy9r1Bww5IO8lYG2zyZxzlDTbq4oa3IMHccY9t0JwTm2Vk85p6 Evb+HUOtW5y6a3oU8qqb5j9WT4clC5OUVzFO6iOYj4GHqDjay+zzb36cJAvn048Szx8Z p92yRQtfWKo3Am18tu5ePYlznGpus7AdciZnrewJrMd/RnRjP3MPJmqcaLNppx5OBfrD CTsw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-205364-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205364-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id d9443c01a7336-1f6bd7e4c51si23914415ad.339.2024.06.06.21.47.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jun 2024 21:47:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-205364-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-205364-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205364-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 4D28C2855C5 for ; Fri, 7 Jun 2024 04:47:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7DA29168C29; Fri, 7 Jun 2024 04:27:53 +0000 (UTC) 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: 8bit Content-Type: text/plain X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemm600013.china.huawei.com (7.193.23.68) 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.ubifs/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 = 0; bool corrupted_truncation = false; @@ -1306,15 +1306,24 @@ static void calculate_file_info(struct ubifs_info *c, struct scanned_file *file, for (node = rb_first(&file->xattr_files); node; node = rb_next(node)) { xattr_file = rb_entry(node, struct scanned_file, rb); + dent_node = rb_entry(rb_first(&xattr_file->dent_nodes), + struct scanned_dent_node, rb); + 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 = 1; + xattr_file->calc_size = xattr_file->ino.size; + + file->calc_xcnt += 1; + file->calc_xsz += CALC_DENT_SIZE(dent_node->nlen); + file->calc_xsz += CALC_XATTR_BYTES(xattr_file->ino.size); + file->calc_xnms += dent_node->nlen; } if (file->inum == UBIFS_ROOT_INO) { file->calc_nlink += 2; file->calc_size += UBIFS_INO_NODE_SZ; - return; + return 0; } 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 = lookup_file(file_tree, key_inum(c, &dent_node->key)); if (!parent_file) { ubifs_assert(c, 0); - return; + return 0; } parent_file->calc_nlink += 1; parent_file->calc_size += CALC_DENT_SIZE(dent_node->nlen); - return; - } - - if (file->ino.is_xattr) { - file->calc_nlink = 1; - file->calc_size = file->ino.size; - - dent_node = rb_entry(rb_first(&file->dent_nodes), - struct scanned_dent_node, rb); - parent_file = lookup_file(file_tree, key_inum(c, &dent_node->key)); - if (!parent_file) { - ubifs_assert(c, 0); - return; - } - parent_file->calc_xcnt += 1; - parent_file->calc_xsz += CALC_DENT_SIZE(dent_node->nlen); - parent_file->calc_xsz += CALC_XATTR_BYTES(file->ino.size); - parent_file->calc_xnms += dent_node->nlen; - return; + return 0; } for (node = rb_first(&file->dent_nodes); node; node = rb_next(node)) { @@ -1359,7 +1350,7 @@ static void calculate_file_info(struct ubifs_info *c, struct scanned_file *file, parent_file = lookup_file(file_tree, key_inum(c, &dent_node->key)); if (!parent_file) { ubifs_assert(c, 0); - return; + return 0; } parent_file->calc_size += 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 = file->ino.size; - return; + return 0; } /* @@ -1452,10 +1443,22 @@ drop_data: data_node = list_entry(drop_list.next, struct scanned_data_node, list); + if (FSCK(c)->mode != REBUILD_MODE) { + /* + * Don't ask, inconsistent file correcting will be + * asked in function correct_file_info(). + */ + int err = 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; } /** @@ -1490,6 +1493,7 @@ static int correct_file_info(struct ubifs_info *c, struct scanned_file *file) file->calc_size == file->ino.size) return 0; + handle_invalid_file(c, FILE_IS_INCONSISTENT, file, NULL); lnum = 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 = rb_first(tree); node; node = rb_next(node)) { file = rb_entry(node, struct scanned_file, rb); - calculate_file_info(c, file, tree); + err = calculate_file_info(c, file, tree); + if (err) + return err; } for (node = rb_first(tree); node; node = rb_next(node)) { @@ -1548,5 +1554,21 @@ int check_and_correct_files(struct ubifs_info *c) return err; } + if (list_empty(&FSCK(c)->disconnected_files)) + return 0; + + ubifs_assert(c, FSCK(c)->mode != REBUILD_MODE); + list_for_each_entry(file, &FSCK(c)->disconnected_files, list) { + err = calculate_file_info(c, file, tree); + if (err) + return err; + + /* Reset disconnected file's nlink as one. */ + file->calc_nlink = 1; + err = 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/fsck.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; } + log_out(c, "Check and correct files"); + err = check_and_correct_files(c); + if (err) { + exit_code |= 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 = do_fsck(); if (err && FSCK(c)->try_rebuild) { diff --git a/ubifs-utils/fsck.ubifs/fsck.ubifs.h b/ubifs-utils/fsck.ubifs/fsck.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 = 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 }; enum { HAS_DATA_CORRUPTED = 1, HAS_TNC_CORRUPTED = 2 }; diff --git a/ubifs-utils/fsck.ubifs/problem.c b/ubifs-utils/fsck.ubifs/problem.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[] = { {PROBLEM_FIXABLE | PROBLEM_MUST_FIX, "File is disconnected(regular file without 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 unreachable"}, // DENTRY_IS_UNREACHABLE + {PROBLEM_FIXABLE | PROBLEM_MUST_FIX, "File is inconsistent"}, // FILE_IS_INCONSISTENT }; 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) == UBIFS_XENT_KEY ? "(xattr)" : ""); break; } + case FILE_IS_INCONSISTENT: + { + const struct invalid_file_problem *ifp = (const struct invalid_file_problem *)priv; + const struct scanned_file *file = 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; -- 2.13.6