From patchwork Mon Nov 11 09:01:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhihao Cheng X-Patchwork-Id: 2009682 X-Patchwork-Delegate: david.oberhollenzer@sigma-star.at Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=NmPx70i0; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Xn3WN6gVtz1xty for ; Mon, 11 Nov 2024 20:04:00 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:CC:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Wg4lpFM40/KqPyec/otKBAgnP9Uec1j0uC/tp4xu3D4=; b=NmPx70i0pT65oN c103H8oJi0mUunCfyd/n85I6k8mPLcx09LE2kBUl0kJrarr28vTM42rNdde0DsnLZfEcTE899OvY4 b6WdS+UM/RonykVogSSx8q016SA9ZdgVHits9X2SDoT1rquN/COms0Vm/bT7s/1drTBR0jhmVlKdt xCGeA1bgH1zvL4BsyFVO34ts/ouwgye8YEdI6xrX8iNK9uAQo0UxHjMxIrUwVU70sglq8WMuSR0L4 HwqfWZbQoeTYbhQYmCA9tolt71r/o9luP7jjZuW1S813XZn6IPv8RoyXmE5Y6wZGBgYBKRmHAvt/E 6WJTLFub6mWZ8OnUkFwg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tAQKo-0000000Gych-1Fwm; Mon, 11 Nov 2024 09:03:46 +0000 Received: from szxga04-in.huawei.com ([45.249.212.190]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tAQJr-0000000Gxnb-2OzO for linux-mtd@lists.infradead.org; Mon, 11 Nov 2024 09:02:57 +0000 Received: from mail.maildlp.com (unknown [172.19.88.234]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4Xn3Rt4L0fz2Dh0Q; Mon, 11 Nov 2024 17:00:58 +0800 (CST) Received: from kwepemk500005.china.huawei.com (unknown [7.202.194.90]) by mail.maildlp.com (Postfix) with ESMTPS id E96E91401F4; Mon, 11 Nov 2024 17:02:45 +0800 (CST) Received: from huawei.com (10.175.112.188) by kwepemk500005.china.huawei.com (7.202.194.90) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 11 Nov 2024 17:02:45 +0800 From: Zhihao Cheng To: , , CC: Subject: [PATCH mtd-utils v2 RESEND 060/102] fsck.ubifs: rebuild_fs: Record used LEBs Date: Mon, 11 Nov 2024 17:01:14 +0800 Message-ID: <20241111090124.2070455-21-chengzhihao1@huawei.com> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20241111083711.1916125-1-chengzhihao1@huawei.com> References: <20241111083711.1916125-1-chengzhihao1@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.112.188] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To kwepemk500005.china.huawei.com (7.202.194.90) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241111_010248_072760_EC2D056E X-CRM114-Status: GOOD ( 14.30 ) X-Spam-Score: -4.2 (----) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: This is the 7/12 step of rebuilding. Record used LEBs which may hold useful nodes, then left unused LEBs could be taken for storing new index tree. Notice, LEB that contains effective nodes on deleted [...] Content analysis details: (-4.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 0.0 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED RBL: ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. [45.249.212.190 listed in sa-trusted.bondedsender.org] 0.0 RCVD_IN_VALIDITY_SAFE_BLOCKED RBL: ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. [45.249.212.190 listed in sa-accredit.habeas.com] 0.0 RCVD_IN_MSPIKE_H3 RBL: Good reputation (+3) [45.249.212.190 listed in wl.mailspike.net] -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at https://www.dnswl.org/, medium trust [45.249.212.190 listed in list.dnswl.org] 0.0 RCVD_IN_VALIDITY_RPBL_BLOCKED RBL: ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. [45.249.212.190 listed in bl.score.senderscore.com] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.0 RCVD_IN_MSPIKE_WL Mailspike good senders -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org This is the 7/12 step of rebuilding. Record used LEBs which may hold useful nodes, then left unused LEBs could be taken for storing new index tree. Notice, LEB that contains effective nodes on deleted trees in step 1 is regarded as used. Signed-off-by: Zhihao Cheng --- ubifs-utils/fsck.ubifs/fsck.ubifs.h | 2 + ubifs-utils/fsck.ubifs/rebuild_fs.c | 116 +++++++++++++++++++++++++++++++++++- 2 files changed, 115 insertions(+), 3 deletions(-) diff --git a/ubifs-utils/fsck.ubifs/fsck.ubifs.h b/ubifs-utils/fsck.ubifs/fsck.ubifs.h index bb5d1c49..a3b755bf 100644 --- a/ubifs-utils/fsck.ubifs/fsck.ubifs.h +++ b/ubifs-utils/fsck.ubifs/fsck.ubifs.h @@ -180,9 +180,11 @@ struct scanned_file { /** * ubifs_rebuild_info - UBIFS rebuilding information. + * @used_lebs: a bitmap used for recording used lebs * @scanned_files: tree of all scanned files */ struct ubifs_rebuild_info { + unsigned long *used_lebs; struct rb_root scanned_files; }; diff --git a/ubifs-utils/fsck.ubifs/rebuild_fs.c b/ubifs-utils/fsck.ubifs/rebuild_fs.c index ecf6b0c6..3be08446 100644 --- a/ubifs-utils/fsck.ubifs/rebuild_fs.c +++ b/ubifs-utils/fsck.ubifs/rebuild_fs.c @@ -48,19 +48,29 @@ static int init_rebuild_info(struct ubifs_info *c) if (!FSCK(c)->rebuild) { err = -ENOMEM; log_err(c, errno, "can not allocate rebuild info"); - goto out; + goto free_sbuf; } FSCK(c)->rebuild->scanned_files = RB_ROOT; + FSCK(c)->rebuild->used_lebs = kcalloc(BITS_TO_LONGS(c->main_lebs), + sizeof(unsigned long), GFP_KERNEL); + if (!FSCK(c)->rebuild->used_lebs) { + err = -ENOMEM; + log_err(c, errno, "can not allocate bitmap of used lebs"); + goto free_rebuild; + } return 0; -out: +free_rebuild: + kfree(FSCK(c)->rebuild); +free_sbuf: vfree(c->sbuf); return err; } static void destroy_rebuild_info(struct ubifs_info *c) { + kfree(FSCK(c)->rebuild->used_lebs); kfree(FSCK(c)->rebuild); vfree(c->sbuf); } @@ -451,6 +461,9 @@ static void remove_del_nodes(struct ubifs_info *c, struct scanned_info *si) valid_ino_node = lookup_valid_ino_node(c, si, del_ino_node); if (valid_ino_node) { + int lnum = del_ino_node->header.lnum; + + set_bit(lnum - c->main_first, FSCK(c)->rebuild->used_lebs); rb_erase(&valid_ino_node->rb, &si->valid_inos); kfree(valid_ino_node); } @@ -466,6 +479,9 @@ static void remove_del_nodes(struct ubifs_info *c, struct scanned_info *si) valid_dent_node = lookup_valid_dent_node(c, si, del_dent_node); if (valid_dent_node) { + int lnum = del_dent_node->header.lnum; + + set_bit(lnum - c->main_first, FSCK(c)->rebuild->used_lebs); rb_erase(&valid_dent_node->rb, &si->valid_dents); kfree(valid_dent_node); } @@ -627,6 +643,94 @@ static void extract_dentry_tree(struct ubifs_info *c) } } +static const char *get_file_name(struct ubifs_info *c, struct scanned_file *file) +{ + static char name[UBIFS_MAX_NLEN + 1]; + struct rb_node *node; + struct scanned_dent_node *dent_node; + + node = rb_first(&file->dent_nodes); + if (!node) { + ubifs_assert(c, file->inum == UBIFS_ROOT_INO); + return "/"; + } + + if (c->encrypted && !file->ino.is_xattr) + /* Encrypted file name. */ + return ""; + + /* Get name from any one dentry. */ + dent_node = rb_entry(node, struct scanned_dent_node, rb); + memcpy(name, dent_node->name, dent_node->nlen); + /* @dent->name could be non '\0' terminated. */ + name[dent_node->nlen] = '\0'; + return name; +} + +static void record_file_used_lebs(struct ubifs_info *c, + struct scanned_file *file) +{ + int lnum; + struct rb_node *node; + struct scanned_file *xattr_file; + struct scanned_dent_node *dent_node; + struct scanned_data_node *data_node; + + dbg_fsck("recovered file(inum:%lu name:%s type:%s), in %s", + file->inum, get_file_name(c, file), + file->ino.is_xattr ? "xattr" : + ubifs_get_type_name(ubifs_get_dent_type(file->ino.mode)), + c->dev_name); + + lnum = file->ino.header.lnum; + set_bit(lnum - c->main_first, FSCK(c)->rebuild->used_lebs); + + if (file->trun.header.exist) { + lnum = file->trun.header.lnum; + set_bit(lnum - c->main_first, FSCK(c)->rebuild->used_lebs); + } + + for (node = rb_first(&file->data_nodes); node; node = rb_next(node)) { + data_node = rb_entry(node, struct scanned_data_node, rb); + + lnum = data_node->header.lnum; + set_bit(lnum - c->main_first, FSCK(c)->rebuild->used_lebs); + } + + for (node = rb_first(&file->dent_nodes); node; node = rb_next(node)) { + dent_node = rb_entry(node, struct scanned_dent_node, rb); + + lnum = dent_node->header.lnum; + set_bit(lnum - c->main_first, FSCK(c)->rebuild->used_lebs); + } + + for (node = rb_first(&file->xattr_files); node; node = rb_next(node)) { + xattr_file = rb_entry(node, struct scanned_file, rb); + + record_file_used_lebs(c, xattr_file); + } +} + +/** + * record_used_lebs - record used LEBs. + * @c: UBIFS file-system description object + * + * This function records all used LEBs which may hold useful nodes, then left + * unused LEBs could be taken for storing new index tree. + */ +static void record_used_lebs(struct ubifs_info *c) +{ + struct rb_node *node; + struct scanned_file *file; + struct rb_root *tree = &FSCK(c)->rebuild->scanned_files; + + for (node = rb_first(tree); node; node = rb_next(node)) { + file = rb_entry(node, struct scanned_file, rb); + + record_file_used_lebs(c, file); + } +} + /** * ubifs_rebuild_filesystem - Rebuild filesystem. * @c: UBIFS file-system description object @@ -680,8 +784,14 @@ int ubifs_rebuild_filesystem(struct ubifs_info *c) /* Step 6: Check & correct files' information. */ log_out(c, "Check & correct file information"); err = check_and_correct_files(c); - if (err) + if (err) { exit_code |= FSCK_ERROR; + goto out; + } + + /* Step 7: Record used LEBs. */ + log_out(c, "Record used LEBs"); + record_used_lebs(c); out: destroy_scanned_info(c, &si);