Linux-EROFS Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Jingbo Xu <jefflexu@linux.alibaba.com>
To: xiang@kernel.org, linux-erofs@lists.ozlabs.org
Subject: [PATCH v2 14/16] erofs-utils: generate data block bitmap when loading tree
Date: Wed,  2 Aug 2023 17:17:48 +0800	[thread overview]
Message-ID: <20230802091750.74181-14-jefflexu@linux.alibaba.com> (raw)
In-Reply-To: <20230802091750.74181-1-jefflexu@linux.alibaba.com>

Enhance erofs_rebuild_load_tree() making it generate a bitmap describing
used data blocks by all regular files in given erofs image.

Signed-off-by: Jingbo Xu <jefflexu@linux.alibaba.com>
---
 include/erofs/rebuild.h |  3 ++-
 lib/rebuild.c           | 39 ++++++++++++++++++++++++++++++++++-----
 2 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/include/erofs/rebuild.h b/include/erofs/rebuild.h
index 57c0e42..55cd856 100644
--- a/include/erofs/rebuild.h
+++ b/include/erofs/rebuild.h
@@ -21,7 +21,8 @@ struct erofs_img *erofs_get_img(const char *path);
 int erofs_add_img(const char *path);
 void erofs_cleanup_imgs(void);
 
-int erofs_rebuild_load_tree(struct erofs_inode *root, struct erofs_img *img);
+int erofs_rebuild_load_tree(struct erofs_inode *root, struct erofs_img *img,
+		unsigned long **datablk_map, unsigned long *datablks);
 
 struct erofs_dentry *erofs_rebuild_get_dentry(struct erofs_inode *pwd,
 		char *path, bool aufs, bool *whout, bool *opq);
diff --git a/lib/rebuild.c b/lib/rebuild.c
index 33b539f..f641c40 100644
--- a/lib/rebuild.c
+++ b/lib/rebuild.c
@@ -11,6 +11,7 @@
 #include "erofs/dir.h"
 #include "erofs/xattr.h"
 #include "erofs/blobchunk.h"
+#include "erofs/bitops.h"
 #include "erofs/internal.h"
 
 #ifdef HAVE_LINUX_AUFS_TYPE_H
@@ -125,7 +126,7 @@ struct erofs_dentry *erofs_rebuild_get_dentry(struct erofs_inode *pwd,
 }
 
 static int erofs_rebuild_fill_inode_map(struct erofs_inode *inode,
-				struct erofs_inode *vi, dev_t dev)
+		struct erofs_inode *vi, dev_t dev, unsigned long *datablk)
 {
 	int ret;
 	unsigned int count, unit, chunkbits, deviceid, i;
@@ -178,6 +179,9 @@ static int erofs_rebuild_fill_inode_map(struct erofs_inode *inode,
 			goto err;
 		}
 		*(void **)idx++ = chunk;
+
+		if (datablk)
+			__set_bit(blkaddr, datablk);
 		erofs_dbg("\t%s: chunk %d (deviceid %u, blkaddr %u)",
 			inode->i_srcpath, i, deviceid, blkaddr);
 
@@ -191,7 +195,7 @@ err:
 }
 
 static int erofs_rebuild_fill_inode(struct erofs_inode *inode,
-			struct erofs_inode *vi, dev_t dev)
+		struct erofs_inode *vi, dev_t dev, unsigned long *datablk)
 {
 	int ret = 0;
 
@@ -226,7 +230,7 @@ static int erofs_rebuild_fill_inode(struct erofs_inode *inode,
 	case S_IFREG:
 		inode->i_size = vi->i_size;
 		if (inode->i_size)
-			ret = erofs_rebuild_fill_inode_map(inode, vi, dev);
+			ret = erofs_rebuild_fill_inode_map(inode, vi, dev, datablk);
 		else
 			inode->u.i_blkaddr = NULL_ADDR;
 		break;
@@ -241,6 +245,7 @@ struct erofs_rebuild_dir_context {
 	struct erofs_dir_context ctx;
 	struct erofs_inode *root;
 	dev_t dev;
+	unsigned long *datablk;
 };
 
 static int erofs_rebuild_dirent_iter(struct erofs_dir_context *ctx)
@@ -313,7 +318,8 @@ static int erofs_rebuild_dirent_iter(struct erofs_dir_context *ctx)
 
 			inode->i_parent = d->inode;
 			inode->dev = rctx->dev;
-			ret = erofs_rebuild_fill_inode(inode, &vi, rctx->dev);
+			ret = erofs_rebuild_fill_inode(inode, &vi, rctx->dev,
+						       rctx->datablk);
 			if (ret)
 				goto exit_inode;
 
@@ -345,11 +351,13 @@ exit_inode:
 	goto exit;
 }
 
-int erofs_rebuild_load_tree(struct erofs_inode *root, struct erofs_img *img)
+int erofs_rebuild_load_tree(struct erofs_inode *root, struct erofs_img *img,
+		unsigned long **datablk_map, unsigned long *datablks)
 {
 	struct erofs_inode inode = {};
 	struct erofs_sb_info *sbi = &img->sbi;
 	struct erofs_rebuild_dir_context ctx;
+	unsigned long *datablk = NULL;
 	int ret;
 
 	erofs_dbg("Loading image %s...", img->path);
@@ -366,11 +374,27 @@ int erofs_rebuild_load_tree(struct erofs_inode *root, struct erofs_img *img)
 		goto exit;
 	}
 
+	if (datablk_map) {
+		if (sbi->extra_devices) {
+			erofs_err("don't support scanning data blocks for extra devices");
+			ret = -EOPNOTSUPP;
+			goto exit;
+		}
+
+		datablk = calloc(BITS_TO_LONGS(sbi->total_blocks),
+				sizeof(unsigned long));
+		if (!datablk) {
+			ret = -ENOMEM;
+			goto exit;
+		}
+	}
+
 	inode.nid = sbi->root_nid;
 	inode.sbi = sbi;
 	ret = erofs_read_inode_from_disk(&inode);
 	if (ret) {
 		erofs_err("failed to read root inode of image %s", img->path);
+		free(datablk);
 		goto exit;
 	}
 	inode.i_srcpath = strdup("/");
@@ -380,9 +404,14 @@ int erofs_rebuild_load_tree(struct erofs_inode *root, struct erofs_img *img)
 		.ctx.cb = erofs_rebuild_dirent_iter,
 		.root = root,
 		.dev = img->dev,
+		.datablk = datablk,
 	};
 	ret = erofs_iterate_dir(&ctx.ctx, false);
 	free(inode.i_srcpath);
+	if (datablk_map) {
+		*datablk_map = datablk;
+		*datablks = sbi->total_blocks;
+	}
 exit:
 	dev_close(sbi);
 	return ret;
-- 
2.19.1.6.gb485710b


  parent reply	other threads:[~2023-08-02  9:18 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-02  9:17 [PATCH v2 01/16] erofs-utils: fix tar.h Jingbo Xu
2023-08-02  9:17 ` [PATCH v2 02/16] erofs-utils: lib: add list_splice_tail() helper Jingbo Xu
2023-08-02  9:17 ` [PATCH v2 03/16] erofs-utils: lib: add bitops library Jingbo Xu
2023-08-02  9:17 ` [PATCH v2 04/16] erofs-utils: read i_ino in erofs_read_inode_from_disk() Jingbo Xu
2023-08-02  9:17 ` [PATCH v2 05/16] erofs-utils: read dis->blocks in erofs_init_devices() Jingbo Xu
2023-08-02  9:17 ` [PATCH v2 06/16] erofs-utils: add erofs_read_xattrs_from_disk() helper Jingbo Xu
2023-08-02  9:17 ` [PATCH v2 07/16] erofs-utils: add erofs_inode_tag_opaque() helper Jingbo Xu
2023-08-02  9:17 ` [PATCH v2 08/16] erofs-utils: add erofs_insert_ihash() helper Jingbo Xu
2023-08-02  9:17 ` [PATCH v2 09/16] erofs-utils: add erofs_rebuild_dump_tree() helper Jingbo Xu
2023-08-02  9:17 ` [PATCH v2 10/16] erofs-utils: add erofs_rebuild_get_dentry() helper Jingbo Xu
2023-08-02  9:17 ` [PATCH v2 11/16] erofs-utils: make erofs_get_unhashed_chunk() global Jingbo Xu
2023-08-02  9:17 ` [PATCH v2 12/16] erofs-utils: add erofs image helper Jingbo Xu
2023-08-02  9:17 ` [PATCH v2 13/16] erofs-utils: add erofs_rebuild_load_tree() helper Jingbo Xu
2023-08-02  9:17 ` Jingbo Xu [this message]
2023-08-02  9:17 ` [PATCH v2 15/16] erofs-utils: mkfs: add dependency on zlib_LIBS Jingbo Xu
2023-08-02  9:17 ` [PATCH v2 16/16] erofs-utils: mkfs: introduce rebuild mode Jingbo Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230802091750.74181-14-jefflexu@linux.alibaba.com \
    --to=jefflexu@linux.alibaba.com \
    --cc=linux-erofs@lists.ozlabs.org \
    --cc=xiang@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox