linux-erofs.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: Gao Xiang via Linux-erofs <linux-erofs@lists.ozlabs.org>
To: linux-erofs@lists.ozlabs.org, bluce.liguifu@huawei.com,
	miaoxie@huawei.com, fangwei1@huawei.com
Subject: [PATCH 2/3] erofs-utils: resize image to the correct size
Date: Tue, 17 Sep 2019 13:49:12 +0800	[thread overview]
Message-ID: <20190917054913.24187-2-hsiangkao@aol.com> (raw)
In-Reply-To: <20190917054913.24187-1-hsiangkao@aol.com>

From: Gao Xiang <gaoxiang25@huawei.com>

In the end, it's necessary to resize image to
the proper size since buffers could be dropped.

Signed-off-by: Gao Xiang <gaoxiang25@huawei.com>
---
 include/erofs/io.h |  1 +
 lib/io.c           | 32 +++++++++++++++++++++++++++++++-
 mkfs/main.c        | 11 ++++++++---
 3 files changed, 40 insertions(+), 4 deletions(-)

diff --git a/include/erofs/io.h b/include/erofs/io.h
index 4b574bd..9775047 100644
--- a/include/erofs/io.h
+++ b/include/erofs/io.h
@@ -21,6 +21,7 @@ void dev_close(void);
 int dev_write(const void *buf, u64 offset, size_t len);
 int dev_fillzero(u64 offset, size_t len, bool padding);
 int dev_fsync(void);
+int dev_resize(erofs_blk_t nblocks);
 u64 dev_length(void);
 
 static inline int blk_write(const void *buf, erofs_blk_t blkaddr,
diff --git a/lib/io.c b/lib/io.c
index 15c5a35..7f5f94d 100644
--- a/lib/io.c
+++ b/lib/io.c
@@ -79,6 +79,7 @@ int dev_open(const char *dev)
 			close(fd);
 			return ret;
 		}
+		erofs_devsz = round_down(erofs_devsz, EROFS_BLKSIZ);
 		break;
 	case S_IFREG:
 		ret = ftruncate(fd, 0);
@@ -98,7 +99,6 @@ int dev_open(const char *dev)
 
 	erofs_devname = dev;
 	erofs_devfd = fd;
-	erofs_devsz = round_down(erofs_devsz, EROFS_BLKSIZ);
 
 	erofs_info("successfully to open %s", dev);
 	return 0;
@@ -177,3 +177,33 @@ int dev_fsync(void)
 	}
 	return 0;
 }
+
+int dev_resize(unsigned int blocks)
+{
+	int ret;
+	struct stat st;
+	u64 length;
+
+	if (cfg.c_dry_run || erofs_devsz != INT64_MAX)
+		return 0;
+
+	ret = fstat(erofs_devfd, &st);
+	if (ret) {
+		erofs_err("failed to fstat.");
+		return -errno;
+	}
+
+	length = (u64)blocks * EROFS_BLKSIZ;
+	if (st.st_size == length)
+		return 0;
+	if (st.st_size > length)
+		return ftruncate(erofs_devfd, length);
+
+	length = length - st.st_size;
+#if defined(HAVE_FALLOCATE)
+	if (fallocate(erofs_devfd, 0, st.st_size, length) >= 0)
+		return 0;
+#endif
+	return dev_fillzero(st.st_size, length, true);
+}
+
diff --git a/mkfs/main.c b/mkfs/main.c
index 5efbf30..2dfd68e 100644
--- a/mkfs/main.c
+++ b/mkfs/main.c
@@ -145,7 +145,8 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 }
 
 int erofs_mkfs_update_super_block(struct erofs_buffer_head *bh,
-				  erofs_nid_t root_nid)
+				  erofs_nid_t root_nid,
+				  erofs_blk_t *blocks)
 {
 	struct erofs_super_block sb = {
 		.magic     = cpu_to_le32(EROFS_SUPER_MAGIC_V1),
@@ -166,7 +167,8 @@ int erofs_mkfs_update_super_block(struct erofs_buffer_head *bh,
 		sb.build_time_nsec = cpu_to_le32(t.tv_usec);
 	}
 
-	sb.blocks       = cpu_to_le32(erofs_mapbh(NULL, true));
+	*blocks         = erofs_mapbh(NULL, true);
+	sb.blocks       = cpu_to_le32(*blocks);
 	sb.root_nid     = cpu_to_le16(root_nid);
 
 	buf = calloc(sb_blksize, 1);
@@ -189,6 +191,7 @@ int main(int argc, char **argv)
 	struct erofs_inode *root_inode;
 	erofs_nid_t root_nid;
 	struct stat64 st;
+	erofs_blk_t nblocks;
 
 	erofs_init_configure();
 	fprintf(stderr, "%s %s\n", basename(argv[0]), cfg.c_version);
@@ -250,13 +253,15 @@ int main(int argc, char **argv)
 	root_nid = erofs_lookupnid(root_inode);
 	erofs_iput(root_inode);
 
-	err = erofs_mkfs_update_super_block(sb_bh, root_nid);
+	err = erofs_mkfs_update_super_block(sb_bh, root_nid, &nblocks);
 	if (err)
 		goto exit;
 
 	/* flush all remaining buffers */
 	if (!erofs_bflush(NULL))
 		err = -EIO;
+	else
+		err = dev_resize(nblocks);
 exit:
 	z_erofs_compress_exit();
 	dev_close();
-- 
2.17.1


  reply	other threads:[~2019-09-17  5:50 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20190917054913.24187-1-hsiangkao.ref@aol.com>
2019-09-17  5:49 ` [PATCH 1/3] erofs-utils: complete special file support Gao Xiang via Linux-erofs
2019-09-17  5:49   ` Gao Xiang via Linux-erofs [this message]
2019-09-17 15:15     ` [PATCH 2/3] erofs-utils: resize image to the correct size Li Guifu
2019-09-17  5:49   ` [PATCH 3/3] erofs-utils: keep up with in-kernel ondisk format naming Gao Xiang via Linux-erofs
2019-09-17 15:15     ` Li Guifu
2019-09-17 15:15   ` [PATCH 1/3] erofs-utils: complete special file support Li Guifu

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=20190917054913.24187-2-hsiangkao@aol.com \
    --to=linux-erofs@lists.ozlabs.org \
    --cc=bluce.liguifu@huawei.com \
    --cc=fangwei1@huawei.com \
    --cc=hsiangkao@aol.com \
    --cc=miaoxie@huawei.com \
    /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;
as well as URLs for NNTP newsgroup(s).