Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp809868lqt; Thu, 6 Jun 2024 21:39:44 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVTdZCvydB3Ogigv426pq1hHgvvC0117omUdGFAeFvZwOvyKZcN2xwU5OUQZK6ERsErWc9gB1XJwMI/r31BAeZPCPpSwfhwzHTAI1vrOA== X-Google-Smtp-Source: AGHT+IHLnMrJ0ALaYrMNp+Re0A7dShZNFX8wVS4YfN51jOeb8dZqR5RUurxVYLO4mdk17qk0J/p6 X-Received: by 2002:a05:6214:3c97:b0:6af:5575:5d36 with SMTP id 6a1803df08f44-6b059f2d42emr15760806d6.48.1717735184226; Thu, 06 Jun 2024 21:39:44 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717735184; cv=pass; d=google.com; s=arc-20160816; b=jIsO4sXH7+F5TJ2AXgRWQPlpfitTOvFUb0SZK6kd56KH4vei+GNRU5CmjQmDpc0/tN SzDMpaZyW3NT3W7CwxVPBxU6z4yWjStoUttriG0JXC5u+ou604ZNAzd1EDgCju4IGmo7 VsNUAT3euI7S8MocaAdce0JTvjcSt/5B/c3ieMZ0snGuq850zmtKSoPsMh1P1tOEcuhJ S4nlRDMRA8x0+D+j1lj7x4Y3i/WW5OMT3aXN7lE9QwqWcYTHLBLwNe9sw01pEa4zaPge tH2ySEUXZg0Fun9tapGBsY/5SLKp/3o9doEBYoDQ5/iXOxqnKlmi/REccrJQhKTXYwEs aWsw== 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=Jx3RsWB8uk4l0PJlWfvUoAGOXJxaaT1aylj5Tfav1Jc=; fh=yXYChwFvQA/NaaLzCC0IJdkW11OhjXgO4DHXzVmnGFw=; b=biq+Cz8KcxfCI4Z89E77WrZY63YpIPK1Jkhi7jA+IPKkE+MmfapW3Qj4mdMZtWzIFP p8RKOUYKlygWz6b1y4PkSDpUkwRWSC7/uOm6LPU4xBO3Z5PIKOhBLebbdMacoS6fJai2 RIW+0bQzmHd3Tnvop4ZEeGIngvUyfPSxqEdMXYGd0wUTaWMJoREYBkN2jBf3mWRtM7pq fO3IE2NVFUK57Qhtb481OnCoo6TS5Fm9AffqdsdkqbX1FisUTvvbDPYgJHHh9fSX7pbg PQNl7i36458+XLZxlFM8oScimSWCRWKPUkdWPZ+cZC11lPTSQjS0FJrg9B+iwkYa0YfK HBEQ==; 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-205335-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205335-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id 6a1803df08f44-6b04fa4717bsi30717236d6.595.2024.06.06.21.39.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jun 2024 21:39:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-205335-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; 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-205335-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205335-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id D817E1C2366F for ; Fri, 7 Jun 2024 04:39:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 414E415B130; Fri, 7 Jun 2024 04:27:33 +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 91A4915AAC6 for ; Fri, 7 Jun 2024 04:27:27 +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=1717734450; cv=none; b=V9z30Xh6R43fDBgjtvAv5kV2z6KmBRW6v1kz53DcdsUrmfRiRG41xFYLY1Ubm4fxyWAnenUS7r+vV7OvjxgnIYoyWoGDFhw/7wZaxsHuRkvGKAs3AOKrW6t7r52J2IQSkeuhjHIAtTPUgieBrpsSoBsgagBx4p42V7D0KoMCPLU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717734450; c=relaxed/simple; bh=1E4SxR/zEJiSNt+fYgWJ0tixMquI1CWt99bw/e/XtyQ=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=te3pORpzvfm4h4XjFjkFpi7Q9M381EE+RAugMRWMI1qV579GjvtUhbd6t8znKY6HQQdeQQeH5Iqks+D3h0xH02Zmp61iNd3TJtZNGVTzoxCw/qNe3MjKSnj4RSEHoWxzSR05lQaAKaFJMzCTWFyOt8eU3eUVPF2xbo0A9lril9k= 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 4VwSk70d9BzsTSD; Fri, 7 Jun 2024 12:23:27 +0800 (CST) Received: from kwepemm600013.china.huawei.com (unknown [7.193.23.68]) by mail.maildlp.com (Postfix) with ESMTPS id B8A53180085; Fri, 7 Jun 2024 12:27:25 +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:09 +0800 From: Zhihao Cheng To: , , , , , CC: , Subject: [RFC PATCH mtd-utils 058/110] fsck.ubifs: Add inconsistent problem handling asking function Date: Fri, 7 Jun 2024 12:25:23 +0800 Message-ID: <20240607042615.2069840-59-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) There are four dimensions to define the type of inconsistent problems: 1. fixable: Some inconsistent problems can't be fixed, for example corrupted superblock. Un-fixable problem will abort program. 2. must fix: Some inconsistent problems can be ignored(eg. incorrect isize), but some are not(eg. corrupted TNC), which will affect the subsequent fsck steps. 3. drop data: Some fixing methods will drop user data, which is unacceptable for safe mode. If it happens, fsck will be aborted. 4. need rebuild: Some inconsistent problems depends on rebuilding filesystem to be fixed(eg. corrupted master node, corrupted TNC). Define an asking function to handle above kinds of inconsistent problems. Signed-off-by: Zhihao Cheng --- ubifs-utils/Makemodule.am | 3 +- ubifs-utils/fsck.ubifs/fsck.ubifs.h | 3 + ubifs-utils/fsck.ubifs/problem.c | 111 ++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 ubifs-utils/fsck.ubifs/problem.c diff --git a/ubifs-utils/Makemodule.am b/ubifs-utils/Makemodule.am index 0ed91dff..faff911f 100644 --- a/ubifs-utils/Makemodule.am +++ b/ubifs-utils/Makemodule.am @@ -79,7 +79,8 @@ fsck_ubifs_SOURCES = \ $(common_SOURCES) \ $(libubifs_SOURCES) \ ubifs-utils/fsck.ubifs/fsck.ubifs.h \ - ubifs-utils/fsck.ubifs/fsck.ubifs.c + ubifs-utils/fsck.ubifs/fsck.ubifs.c \ + ubifs-utils/fsck.ubifs/problem.c fsck_ubifs_LDADD = libmtd.a libubi.a $(ZLIB_LIBS) $(LZO_LIBS) $(ZSTD_LIBS) $(UUID_LIBS) $(LIBSELINUX_LIBS) $(OPENSSL_LIBS) -lm -lpthread fsck_ubifs_CPPFLAGS = $(AM_CPPFLAGS) $(ZLIB_CFLAGS) $(LZO_CFLAGS) $(ZSTD_CFLAGS) $(UUID_CFLAGS) $(LIBSELINUX_CFLAGS) \ diff --git a/ubifs-utils/fsck.ubifs/fsck.ubifs.h b/ubifs-utils/fsck.ubifs/fsck.ubifs.h index d267dd24..b9783c15 100644 --- a/ubifs-utils/fsck.ubifs/fsck.ubifs.h +++ b/ubifs-utils/fsck.ubifs/fsck.ubifs.h @@ -88,4 +88,7 @@ static inline const char *mode_name(const struct ubifs_info *c) /* Exit code for fsck program. */ extern int exit_code; +/* problem.c */ +bool fix_problem(const struct ubifs_info *c, int problem_type); + #endif diff --git a/ubifs-utils/fsck.ubifs/problem.c b/ubifs-utils/fsck.ubifs/problem.c new file mode 100644 index 00000000..9a8c2e06 --- /dev/null +++ b/ubifs-utils/fsck.ubifs/problem.c @@ -0,0 +1,111 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024, Huawei Technologies Co, Ltd. + * + * Authors: Zhihao Cheng + */ + +#include +#include +#include + +#include "ubifs.h" +#include "defs.h" +#include "debug.h" +#include "fsck.ubifs.h" + +/* + * problem flags. + * + * PROBLEM_FIXABLE: problem is fixable, unsolvable problem such as corrupted + * super block will abort the fsck program + * PROBLEM_MUST_FIX: problem must be fixed because it will affect the subsequent + * fsck process, otherwise aborting the fsck program + * PROBLEM_DROP_DATA: user data could be dropped after fixing the problem + * PROBLEM_NEED_REBUILD: rebuilding filesystem is needed to fix the problem + */ +#define PROBLEM_FIXABLE (1<<0) +#define PROBLEM_MUST_FIX (1<<1) +#define PROBLEM_DROP_DATA (1<<2) +#define PROBLEM_NEED_REBUILD (1<<3) + +struct fsck_problem { + unsigned int flags; + const char *desc; +}; + +static const struct fsck_problem problem_table[] = {}; + +static void print_problem(const struct ubifs_info *c, + const struct fsck_problem *problem) +{ + log_out(c, "problem: %s", problem->desc); +} + +static void fatal_error(const struct ubifs_info *c, + const struct fsck_problem *problem) +{ + if (!(problem->flags & PROBLEM_FIXABLE)) + log_out(c, "inconsistent problem cannot be fixed"); + else + log_out(c, "inconsistent problem must be fixed"); + exit(exit_code); +} + +/** + * fix_problem - whether fixing the inconsistent problem + * @c: UBIFS file-system description object + * @problem_type: the type of inconsistent problem + * + * This function decides to fix/skip the inconsistent problem or abort the + * program according to @problem_type, returns %true if the problem should + * be fixed, returns %false if the problem will be skipped. + */ +bool fix_problem(const struct ubifs_info *c, int problem_type) +{ + bool ans, ask = true, def_y = true; + const struct fsck_problem *problem = &problem_table[problem_type]; + const char *question = (problem->flags & PROBLEM_NEED_REBUILD) ? + "Rebuild filesystem?" : "Fix it?"; + + ubifs_assert(c, FSCK(c)->mode != REBUILD_MODE); + + if (!(problem->flags & PROBLEM_FIXABLE)) { + exit_code |= FSCK_UNCORRECTED; + fatal_error(c, problem); + } + + if (FSCK(c)->mode == CHECK_MODE || + ((problem->flags & PROBLEM_DROP_DATA) && FSCK(c)->mode == SAFE_MODE) || + ((problem->flags & PROBLEM_NEED_REBUILD) && + (FSCK(c)->mode == SAFE_MODE || FSCK(c)->mode == DANGER_MODE0))) + def_y = false; + + if ((problem->flags & PROBLEM_NEED_REBUILD) && + (FSCK(c)->mode == DANGER_MODE0 || FSCK(c)->mode == DANGER_MODE1)) + ask = false; + + print_problem(c, problem); + ans = def_y; + if (FSCK(c)->mode == NORMAL_MODE) { + printf("%s[%d] (%s%s)", c->program_name, getpid(), + c->dev_name ? : "-", mode_name(c)); + if (prompt(question, def_y)) + ans = true; + else + ans = false; + } else { + if (ask) + log_out(c, "%s %c\n", question, def_y ? 'y' : 'n'); + } + + if (!ans) { + exit_code |= FSCK_UNCORRECTED; + if (problem->flags & PROBLEM_MUST_FIX) + fatal_error(c, problem); + } else { + exit_code |= FSCK_NONDESTRUCT; + } + + return ans; +} -- 2.13.6