Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C47A6C2BBCA for ; Tue, 25 Jun 2024 03:15:28 +0000 (UTC) Authentication-Results: lists.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.a=rsa-sha256 header.s=default header.b=kC8DNFah; dkim-atps=neutral Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4W7VML24MDz3cRs for ; Tue, 25 Jun 2024 13:15:26 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: lists.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.a=rsa-sha256 header.s=default header.b=kC8DNFah; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=linux.alibaba.com (client-ip=115.124.30.97; helo=out30-97.freemail.mail.aliyun.com; envelope-from=hsiangkao@linux.alibaba.com; receiver=lists.ozlabs.org) Received: from out30-97.freemail.mail.aliyun.com (out30-97.freemail.mail.aliyun.com [115.124.30.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4W7VMD06n1z3cBN for ; Tue, 25 Jun 2024 13:15:18 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1719285316; h=Message-ID:Date:MIME-Version:Subject:To:From:Content-Type; bh=LcqrXrl7klDnoGLHw9gfdTuF73jwyer7paPSfivskbQ=; b=kC8DNFahzftU62wVhUTjKWfz4X6sIaJluvrZ/ZHXZXEqOBsL3bTR4CYThumGt3oRe4CN0MksobBlR60IfB5Qp0lk3enBqxTIiFyZH5uRPKRVFB2vNxWrp13RrbHXOqDG4Mb/pjNbHkpv06Gw7QkZvpcLSWSoXXXcmWYk5WweRfA= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R831e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037067113;MF=hsiangkao@linux.alibaba.com;NM=1;PH=DS;RN=2;SR=0;TI=SMTPD_---0W9E0BZ8_1719285314; Received: from 30.97.48.187(mailfrom:hsiangkao@linux.alibaba.com fp:SMTPD_---0W9E0BZ8_1719285314) by smtp.aliyun-inc.com; Tue, 25 Jun 2024 11:15:14 +0800 Message-ID: <8bec6edd-b8e0-4fd9-8c0b-a6deb24deb21@linux.alibaba.com> Date: Tue, 25 Jun 2024 11:15:13 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] erofs-utils: move erofs_sb_csum_set() and erofs_mkfs_alloc_root() into lib/ for external use To: Hongzhen Luo , linux-erofs@lists.ozlabs.org References: <20240625031005.1334796-1-hongzhen@linux.alibaba.com> From: Gao Xiang In-Reply-To: <20240625031005.1334796-1-hongzhen@linux.alibaba.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: linux-erofs@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development of Linux EROFS file system List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: linux-erofs-bounces+linux-erofs=archiver.kernel.org@lists.ozlabs.org Sender: "Linux-erofs" On 2024/6/25 11:10, Hongzhen Luo wrote: erofs-utils: lib: add erofs_{rebuild_make_root,enable_sb_chksum} Move erofs_sb_csum_set() and erofs_mkfs_alloc_root() into liberofs for external use. Thanks, Gao Xiang > Signed-off-by: Hongzhen Luo > --- > include/erofs/inode.h | 1 + > include/erofs/internal.h | 1 + > lib/inode.c | 16 +++++++++ > lib/super.c | 47 ++++++++++++++++++++++++ > mkfs/main.c | 77 +++++----------------------------------- > 5 files changed, 73 insertions(+), 69 deletions(-) > > diff --git a/include/erofs/inode.h b/include/erofs/inode.h > index 2af8e6c..604161c 100644 > --- a/include/erofs/inode.h > +++ b/include/erofs/inode.h > @@ -45,6 +45,7 @@ struct erofs_inode *erofs_mkfs_build_tree_from_path(struct erofs_sb_info *sbi, > struct erofs_inode *erofs_mkfs_build_special_from_fd(struct erofs_sb_info *sbi, > int fd, const char *name); > int erofs_fixup_root_inode(struct erofs_inode *root); > +struct erofs_inode *erofs_rebuild_make_root(struct erofs_sb_info *sbi); > > #ifdef __cplusplus > } > diff --git a/include/erofs/internal.h b/include/erofs/internal.h > index 8ed5b0e..361477a 100644 > --- a/include/erofs/internal.h > +++ b/include/erofs/internal.h > @@ -403,6 +403,7 @@ void erofs_put_super(struct erofs_sb_info *sbi); > int erofs_writesb(struct erofs_sb_info *sbi, struct erofs_buffer_head *sb_bh, > erofs_blk_t *blocks); > struct erofs_buffer_head *erofs_reserve_sb(struct erofs_sb_info *sbi); > +int erofs_enable_sb_chksum(struct erofs_sb_info *sbi, u32 *crc); > > /* namei.c */ > int erofs_read_inode_from_disk(struct erofs_inode *vi); > diff --git a/lib/inode.c b/lib/inode.c > index 60a076a..68f16f4 100644 > --- a/lib/inode.c > +++ b/lib/inode.c > @@ -1986,3 +1986,19 @@ int erofs_fixup_root_inode(struct erofs_inode *root) > free(ibuf); > return err; > } > + > +struct erofs_inode *erofs_rebuild_make_root(struct erofs_sb_info *sbi) > +{ > + struct erofs_inode *root; > + > + root = erofs_new_inode(sbi); > + if (IS_ERR(root)) > + return root; > + root->i_srcpath = strdup("/"); > + root->i_mode = S_IFDIR | 0777; > + root->i_parent = root; > + root->i_mtime = root->sbi->build_time; > + root->i_mtime_nsec = root->sbi->build_time_nsec; > + erofs_init_empty_dir(root); > + return root; > +} > diff --git a/lib/super.c b/lib/super.c > index 69c57c8..7579875 100644 > --- a/lib/super.c > +++ b/lib/super.c > @@ -232,3 +232,50 @@ err_bdrop: > erofs_bdrop(bh, true); > return ERR_PTR(err); > } > + > +int erofs_enable_sb_chksum(struct erofs_sb_info *sbi, u32 *crc) > +{ > + int ret; > + u8 buf[EROFS_MAX_BLOCK_SIZE]; > + unsigned int len; > + struct erofs_super_block *sb; > + > + ret = erofs_blk_read(sbi, 0, buf, 0, erofs_blknr(sbi, EROFS_SUPER_END) + 1); > + if (ret) { > + erofs_err("failed to read superblock to set checksum: %s", > + erofs_strerror(ret)); > + return ret; > + } > + > + /* > + * skip the first 1024 bytes, to allow for the installation > + * of x86 boot sectors and other oddities. > + */ > + sb = (struct erofs_super_block *)(buf + EROFS_SUPER_OFFSET); > + > + if (le32_to_cpu(sb->magic) != EROFS_SUPER_MAGIC_V1) { > + erofs_err("internal error: not an erofs valid image"); > + return -EFAULT; > + } > + > + /* turn on checksum feature */ > + sb->feature_compat = cpu_to_le32(le32_to_cpu(sb->feature_compat) | > + EROFS_FEATURE_COMPAT_SB_CHKSUM); > + if (erofs_blksiz(sbi) > EROFS_SUPER_OFFSET) > + len = erofs_blksiz(sbi) - EROFS_SUPER_OFFSET; > + else > + len = erofs_blksiz(sbi); > + *crc = erofs_crc32c(~0, (u8 *)sb, len); > + > + /* set up checksum field to erofs_super_block */ > + sb->checksum = cpu_to_le32(*crc); > + > + ret = erofs_blk_write(sbi, buf, 0, 1); > + if (ret) { > + erofs_err("failed to write checksummed superblock: %s", > + erofs_strerror(ret)); > + return ret; > + } > + > + return 0; > +} > diff --git a/mkfs/main.c b/mkfs/main.c > index e20e8c0..28a271d 100644 > --- a/mkfs/main.c > +++ b/mkfs/main.c > @@ -975,55 +975,6 @@ static int mkfs_parse_options_cfg(int argc, char *argv[]) > return 0; > } > > -static int erofs_mkfs_superblock_csum_set(void) > -{ > - int ret; > - u8 buf[EROFS_MAX_BLOCK_SIZE]; > - u32 crc; > - unsigned int len; > - struct erofs_super_block *sb; > - > - ret = erofs_blk_read(&sbi, 0, buf, 0, erofs_blknr(&sbi, EROFS_SUPER_END) + 1); > - if (ret) { > - erofs_err("failed to read superblock to set checksum: %s", > - erofs_strerror(ret)); > - return ret; > - } > - > - /* > - * skip the first 1024 bytes, to allow for the installation > - * of x86 boot sectors and other oddities. > - */ > - sb = (struct erofs_super_block *)(buf + EROFS_SUPER_OFFSET); > - > - if (le32_to_cpu(sb->magic) != EROFS_SUPER_MAGIC_V1) { > - erofs_err("internal error: not an erofs valid image"); > - return -EFAULT; > - } > - > - /* turn on checksum feature */ > - sb->feature_compat = cpu_to_le32(le32_to_cpu(sb->feature_compat) | > - EROFS_FEATURE_COMPAT_SB_CHKSUM); > - if (erofs_blksiz(&sbi) > EROFS_SUPER_OFFSET) > - len = erofs_blksiz(&sbi) - EROFS_SUPER_OFFSET; > - else > - len = erofs_blksiz(&sbi); > - crc = erofs_crc32c(~0, (u8 *)sb, len); > - > - /* set up checksum field to erofs_super_block */ > - sb->checksum = cpu_to_le32(crc); > - > - ret = erofs_blk_write(&sbi, buf, 0, 1); > - if (ret) { > - erofs_err("failed to write checksummed superblock: %s", > - erofs_strerror(ret)); > - return ret; > - } > - > - erofs_info("superblock checksum 0x%08x written", crc); > - return 0; > -} > - > static void erofs_mkfs_default_options(void) > { > cfg.c_showprogress = true; > @@ -1075,22 +1026,6 @@ void erofs_show_progs(int argc, char *argv[]) > printf("%s %s\n", basename(argv[0]), cfg.c_version); > } > > -static struct erofs_inode *erofs_mkfs_alloc_root(struct erofs_sb_info *sbi) > -{ > - struct erofs_inode *root; > - > - root = erofs_new_inode(sbi); > - if (IS_ERR(root)) > - return root; > - root->i_srcpath = strdup("/"); > - root->i_mode = S_IFDIR | 0777; > - root->i_parent = root; > - root->i_mtime = root->sbi->build_time; > - root->i_mtime_nsec = root->sbi->build_time_nsec; > - erofs_init_empty_dir(root); > - return root; > -} > - > static int erofs_mkfs_rebuild_load_trees(struct erofs_inode *root) > { > struct erofs_sb_info *src; > @@ -1198,6 +1133,7 @@ int main(int argc, char **argv) > erofs_blk_t nblocks; > struct timeval t; > FILE *packedfile = NULL; > + u32 crc; > > erofs_init_configure(); > erofs_mkfs_default_options(); > @@ -1390,7 +1326,7 @@ int main(int argc, char **argv) > erofs_inode_manager_init(); > > if (tar_mode) { > - root = erofs_mkfs_alloc_root(&sbi); > + root = erofs_rebuild_make_root(&sbi); > if (IS_ERR(root)) { > err = PTR_ERR(root); > goto exit; > @@ -1405,7 +1341,7 @@ int main(int argc, char **argv) > if (err < 0) > goto exit; > } else if (rebuild_mode) { > - root = erofs_mkfs_alloc_root(&sbi); > + root = erofs_rebuild_make_root(&sbi); > if (IS_ERR(root)) { > err = PTR_ERR(root); > goto exit; > @@ -1471,8 +1407,11 @@ int main(int argc, char **argv) > > err = erofs_dev_resize(&sbi, nblocks); > > - if (!err && erofs_sb_has_sb_chksum(&sbi)) > - err = erofs_mkfs_superblock_csum_set(); > + if (!err && erofs_sb_has_sb_chksum(&sbi)) { > + err = erofs_enable_sb_chksum(&sbi, &crc); > + if (!err) > + erofs_info("superblock checksum 0x%08x written", crc); > + } > exit: > if (root) > erofs_iput(root);