Skip to contentSkip to conversation

erofs-mkfs: mkfs.erofs source code first preview

RFC PATCH Superseded [email protected] 2 participants 8 messages Nov 16, 2018 – Nov 17, 2018

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover erofs-mkfs: mkfs.erofs source code first preview

From: Li Guifu <[email protected]>

This patchset introduces the source code for mkfs.erofs. It can build erofs image from scratch according to a specific directory.

Currently, it can build lz4 compressed images and uncompressed images. Regular files, directories and special files are supported. However it can only generate erofs inode v1 format due to our product requirements, and inode v2 format support is still under development.

This is our first attempt to release this piece of code, which isn't clean enough. It will be cleaned up with our community development.

Comments are welcome.

Thanks,
Li Guifu

Li Guifu (5):
erofs-mkfs: add erofs on-disk layout
erofs-mkfs: introduce mkfs basic framework
erofs-mkfs: support to build a image from the specfic root directory
erofs-mkfs: add compression support
erofs-mkfs: add miscellaneous files

Signature · 1 line
2.17.1
#1 lore raw

RFC PATCH 1/5 erofs-mkfs: add erofs on-disk layout

From: Li Guifu <[email protected]>

This patch adds a header file describing the erofs on-disk layout, which should be kept in line with the kernel implementation all the time.

Signed-off-by Li Guifu <bluce.liguifu at huawei.com>
Signed-off-by Miao Xie <miaoxie at huawei.com>
Signed-off-by Fang Wei <fangwei1 at huawei.com>
---
 erofs_fs.h | 298 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 298 insertions(+)
 create mode 100644 erofs_fs.h
erofs_fs.h new file +298 -0
new file mode 100644
index 0000000..c0b5510
@@ -0,0 +1,298 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * erofs_fs.h
+ *
+ * Copyright (C) 2017-2018 HUAWEI, Inc.
+ *             http://www.huawei.com/
+ * Created by Li Guifu <bluce.liguifu at huawei.com>
+ */
+#ifndef __EROFS_FS_H
+#define __EROFS_FS_H
+
+#include "erofs_types.h"
+#define __packed __attribute__((__packed__))
+
+
+/* Enhanced(Extended) ROM File System */
+#define EROFS_SUPER_MAGIC_V1    0xE0F5E1E2
+#define EROFS_SUPER_OFFSET      1024
+
+struct erofs_super_block {
+/*  0 */__le32 magic;           /* in the little endian */
+/*  4 */__le32 checksum;        /* crc32c(super_block) */
+/*  8 */__le32 features;
+/* 12 */__u8 blkszbits;         /* support block_size == PAGE_SIZE only */
+/* 13 */__u8 reserved;
+
+/* 14 */__le16 root_nid;
+/* 16 */__le64 inos;            /* total valid ino # (== f_files - f_favail) */
+
+/* 24 */__le64 build_time;      /* inode v1 time derivation */
+/* 32 */__le32 build_time_nsec;
+/* 36 */__le32 blocks;          /* used for statfs */
+/* 40 */__le32 meta_blkaddr;
+/* 44 */__le32 xattr_blkaddr;
+/* 48 */__u8 uuid[16];          /* 128-bit uuid for volume */
+/* 64 */__u8 volume_name[16];   /* volume name */
+
+/* 80 */__u8 reserved2[48];     /* 128 bytes */
+} __packed;
+
+#define __EROFS_BIT(_prefix, _cur, _pre)	enum {	\
+	_prefix ## _cur ## _BIT = _prefix ## _pre ## _BIT + \
+		_prefix ## _pre ## _BITS }
+
+/*
+ * erofs inode data mapping:
+ * 0 - inode plain without inline data A:
+ * inode, [xattrs], ... | ... | no-holed data
+ * 1 - inode VLE compression B:
+ * inode, [xattrs], extents ... | ...
+ * 2 - inode plain with inline data C:
+ * inode, [xattrs], last_inline_data, ... | ... | no-holed data
+ * 3~7 - reserved
+ */
+enum {
+	EROFS_INODE_LAYOUT_PLAIN,
+	EROFS_INODE_LAYOUT_COMPRESSION,
+	EROFS_INODE_LAYOUT_INLINE,
+	EROFS_INODE_LAYOUT_MAX
+};
+
+#define EROFS_I_VERSION_BITS            1
+#define EROFS_I_DATA_MAPPING_BITS       3
+
+#define EROFS_I_VERSION_BIT             0
+__EROFS_BIT(EROFS_I_, DATA_MAPPING, VERSION);
+
+struct erofs_inode_v1 {
+/*  0 */__le16 i_advise;
+
+/* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
+/*  2 */__le16 i_xattr_icount;
+/*  4 */__le16 i_mode;
+/*  6 */__le16 i_nlink;
+/*  8 */__le32 i_size;
+/* 12 */__le32 i_reserved;
+/* 16 */union {
+		/* file total compressed blocks for data mapping 1 */
+		__le32 compressed_blocks;
+		__le32 raw_blkaddr;
+
+		/* for device files, used to indicate old/new device # */
+		__le32 rdev;
+	} i_u __packed;
+/* 20 */__le32 i_ino;           /* only used for 32-bit stat compatibility */
+/* 24 */__le16 i_uid;
+/* 26 */__le16 i_gid;
+/* 28 */__le32 i_checksum;
+} __packed;
+
+/* 32 bytes on-disk inode */
+#define EROFS_INODE_LAYOUT_V1   0
+/* 64 bytes on-disk inode */
+#define EROFS_INODE_LAYOUT_V2   1
+
+struct erofs_inode_v2 {
+	__le16 i_advise;
+
+	/* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
+	__le16 i_xattr_icount;
+	__le16 i_mode;
+	__le16 i_reserved;      /* 8 bytes */
+	__le64 i_size;          /* 16 bytes */
+	union {
+		/* file total compressed blocks for data mapping 1 */
+		__le32 compressed_blocks;
+		__le32 raw_blkaddr;
+
+		/* for device files, used to indicate old/new device # */
+		__le32 rdev;
+	} i_u __packed;
+
+	/* only used for 32-bit stat compatibility */
+	__le32 i_ino;           /* 24 bytes */
+
+	__le32 i_uid;
+	__le32 i_gid;
+	__le64 i_ctime;         /* 32 bytes */
+	__le32 i_ctime_nsec;
+	__le32 i_nlink;
+	__u8   i_reserved2[12];
+	__le32 i_checksum;      /* 64 bytes */
+} __packed;
+
+#define EROFS_MAX_SHARED_XATTRS         (128)
+/* h_shared_count between 129 ... 255 are special # */
+#define EROFS_SHARED_XATTR_EXTENT       (255)
+
+/*
+ * inline xattrs (n == i_xattr_icount):
+ * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
+ *          12 bytes           /                   \
+ *                            /                     \
+ *                           /-----------------------\
+ *                           |  erofs_xattr_entries+ |
+ *                           +-----------------------+
+ * inline xattrs must starts in erofs_xattr_ibody_header,
+ * for read-only fs, no need to introduce h_refcount
+ */
+struct erofs_xattr_ibody_header {
+	__le32 h_checksum;
+	__u8   h_shared_count;
+	__u8   h_reserved[7];
+	__le32 h_shared_xattrs[0];      /* shared xattr id array */
+} __packed;
+
+/* Name indexes */
+#define EROFS_XATTR_INDEX_USER              1
+#define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS  2
+#define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
+#define EROFS_XATTR_INDEX_TRUSTED           4
+#define EROFS_XATTR_INDEX_LUSTRE            5
+#define EROFS_XATTR_INDEX_SECURITY          6
+
+/* xattr entry (for both inline & shared xattrs) */
+struct erofs_xattr_entry {
+	__u8   e_name_len;      /* length of name */
+	__u8   e_name_index;    /* attribute name index */
+	__le16 e_value_size;    /* size of attribute value */
+	/* followed by e_name and e_value */
+	char   e_name[0];       /* attribute name */
+} __packed;
+
+#define ondisk_xattr_ibody_size(count)	({\
+	u32 __count = le16_to_cpu(count); \
+	((__count) == 0) ? 0 : \
+	sizeof(struct erofs_xattr_ibody_header) + \
+		sizeof(__u32) * ((__count) - 1); })
+
+#define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry))
+#define EROFS_XATTR_ENTRY_SIZE(entry) EROFS_XATTR_ALIGN( \
+	sizeof(struct erofs_xattr_entry) + \
+	(entry)->e_name_len + le16_to_cpu((entry)->e_value_size))
+
+/* have to be aligned with 8 bytes on disk */
+struct erofs_extent_header {
+	__le32 eh_checksum;
+	__le32 eh_reserved[3];
+} __packed;
+
+/*
+ * Z_EROFS Variable-sized Logical Extent cluster type:
+ *    0 - literal (uncompressed) cluster
+ *    1 - compressed cluster (for the head logical cluster)
+ *    2 - compressed cluster (for the other logical clusters)
+ *
+ * In detail,
+ *    0 - literal (uncompressed) cluster,
+ *        di_advise = 0
+ *        di_clusterofs = the literal data offset of the cluster
+ *        di_blkaddr = the blkaddr of the literal cluster
+ *
+ *    1 - compressed cluster (for the head logical cluster)
+ *        di_advise = 1
+ *        di_clusterofs = the decompressed data offset of the cluster
+ *        di_blkaddr = the blkaddr of the compressed cluster
+ *
+ *    2 - compressed cluster (for the other logical clusters)
+ *        di_advise = 2
+ *        di_clusterofs =
+ *           the decompressed data offset in its own head cluster
+ *        di_u.delta[0] = distance to its corresponding head cluster
+ *        di_u.delta[1] = distance to its corresponding tail cluster
+ *                (di_advise could be 0, 1 or 2)
+ */
+enum {
+	Z_EROFS_VLE_CLUSTER_TYPE_PLAIN,
+	Z_EROFS_VLE_CLUSTER_TYPE_HEAD,
+	Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD,
+	Z_EROFS_VLE_CLUSTER_TYPE_RESERVED,
+	Z_EROFS_VLE_CLUSTER_TYPE_MAX
+};
+
+#define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS        2
+#define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT         0
+
+struct z_erofs_vle_decompressed_index {
+	__le16 di_advise;
+	/* where to decompress in the head cluster */
+	__le16 di_clusterofs;
+
+	union {
+		/* for the head cluster */
+		__le32 blkaddr;
+		/*
+		 * for the rest clusters
+		 * eg. for 4k page-sized cluster, maximum 4K*64k = 256M)
+		 * [0] - pointing to the head cluster
+		 * [1] - pointing to the tail cluster
+		 */
+		__le16 delta[2];
+	} di_u __packed;		/* 8 bytes */
+} __packed;
+
+#define Z_EROFS_VLE_EXTENT_ALIGN(size) round_up(size, \
+	sizeof(struct z_erofs_vle_decompressed_index))
+
+/* dirent sorts in alphabet order, thus we can do binary search */
+struct erofs_dirent {
+	__le64 nid;     /*  0, node number */
+	__le16 nameoff; /*  8, start offset of file name */
+	__u8 file_type; /* 10, file type */
+	__u8 reserved;  /* 11, reserved */
+} __packed;
+
+/* file types used in inode_info->flags */
+enum {
+	EROFS_FT_UNKNOWN,
+	EROFS_FT_REG_FILE,
+	EROFS_FT_DIR,
+	EROFS_FT_CHRDEV,
+	EROFS_FT_BLKDEV,
+	EROFS_FT_FIFO,
+	EROFS_FT_SOCK,
+	EROFS_FT_SYMLINK,
+	EROFS_FT_MAX
+};
+
+#define EROFS_NAME_LEN      255
+
+/* check the EROFS on-disk layout strictly at compile time */
+static inline void erofs_check_ondisk_layout_definitions(void)
+{
+	BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
+	BUILD_BUG_ON(sizeof(struct erofs_inode_v1) != 32);
+	BUILD_BUG_ON(sizeof(struct erofs_inode_v2) != 64);
+	BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);
+	BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
+	BUILD_BUG_ON(sizeof(struct erofs_extent_header) != 16);
+	BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8);
+	BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
+
+	BUILD_BUG_ON(BIT(Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) <
+		     Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1);
+}
+
+#define EROFS_SB_SIZE sizeof(struct erofs_super_block)
+
+#define EROFS_DECOMPR_IDX_SZ        sizeof(struct z_erofs_vle_decompressed_index)
+#define EROFS_DECOMPR_IDXS_PER_BLK  (EROFS_BLKSIZE / EROFS_DECOMPR_IDX_SZ)
+
+#define EROFS_INODE_V1_SIZE     sizeof(struct erofs_inode_v1)
+#define EROFS_INODE_V2_SIZE     sizeof(struct erofs_inode_v2)
+
+#define EROFS_DIRENT_SIZE       sizeof(struct erofs_dirent)
+#define EROFS_DECOMPR_IDX_SZ    sizeof(struct z_erofs_vle_decompressed_index)
+#define EROFS_DECOMPR_IDXS_PER_BLK  (EROFS_BLKSIZE / EROFS_DECOMPR_IDX_SZ)
+
+#define ondisk_extent_size(data_mapping_mode, count) \
+	((data_mapping_mode) == EROFS_INODE_LAYOUT_COMPRESSION ? \
+	(sizeof(struct erofs_extent_header) + \
+	sizeof(__u32) * le32_to_cpu(count)) : 0)
+
+#define EROFS_INLINE_GENERIC_ALIGN_SIZE     (4)
+#define EROFS_INLINE_INDEX_ALIGN_SIZE       (8)
+
+#endif
+
Signature · 1 line
2.17.1
#2 lore raw

RFC PATCH 1/5 erofs-mkfs: add erofs on-disk layout

Quoted 286 lines · On 2018/11/16 11:30, [email protected] wrote:

From: Li Guifu <bluce.liguifu at huawei.com>

This patch adds a header file describing the erofs on-disk layout, which should be kept in line with the kernel implementation all the time.

Signed-off-by Li Guifu <bluce.liguifu at huawei.com>
Signed-off-by Miao Xie <miaoxie at huawei.com>
Signed-off-by Fang Wei <fangwei1 at huawei.com>
---
 erofs_fs.h | 298 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 298 insertions(+)
 create mode 100644 erofs_fs.h
erofs_fs.h new file +267 -0
new file mode 100644
index 0000000..c0b5510
@@ -0,0 +1,298 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * erofs_fs.h
+ *
+ * Copyright (C) 2017-2018 HUAWEI, Inc.
+ *             http://www.huawei.com/
+ * Created by Li Guifu <bluce.liguifu at huawei.com>
+ */
+#ifndef __EROFS_FS_H
+#define __EROFS_FS_H
+
+#include "erofs_types.h"
+#define __packed __attribute__((__packed__))
+
+
+/* Enhanced(Extended) ROM File System */
+#define EROFS_SUPER_MAGIC_V1    0xE0F5E1E2
+#define EROFS_SUPER_OFFSET      1024
+
+struct erofs_super_block {
+/*  0 */__le32 magic;           /* in the little endian */
+/*  4 */__le32 checksum;        /* crc32c(super_block) */
+/*  8 */__le32 features;
+/* 12 */__u8 blkszbits;         /* support block_size == PAGE_SIZE only */
+/* 13 */__u8 reserved;
+
+/* 14 */__le16 root_nid;
+/* 16 */__le64 inos;            /* total valid ino # (== f_files - f_favail) */
+
+/* 24 */__le64 build_time;      /* inode v1 time derivation */
+/* 32 */__le32 build_time_nsec;
+/* 36 */__le32 blocks;          /* used for statfs */
+/* 40 */__le32 meta_blkaddr;
+/* 44 */__le32 xattr_blkaddr;
+/* 48 */__u8 uuid[16];          /* 128-bit uuid for volume */
+/* 64 */__u8 volume_name[16];   /* volume name */
+
+/* 80 */__u8 reserved2[48];     /* 128 bytes */
+} __packed;
+
+#define __EROFS_BIT(_prefix, _cur, _pre)	enum {	\
+	_prefix ## _cur ## _BIT = _prefix ## _pre ## _BIT + \
+		_prefix ## _pre ## _BITS }
+
+/*
+ * erofs inode data mapping:
+ * 0 - inode plain without inline data A:
+ * inode, [xattrs], ... | ... | no-holed data
+ * 1 - inode VLE compression B:
+ * inode, [xattrs], extents ... | ...
+ * 2 - inode plain with inline data C:
+ * inode, [xattrs], last_inline_data, ... | ... | no-holed data
+ * 3~7 - reserved
+ */
+enum {
+	EROFS_INODE_LAYOUT_PLAIN,
+	EROFS_INODE_LAYOUT_COMPRESSION,
+	EROFS_INODE_LAYOUT_INLINE,
+	EROFS_INODE_LAYOUT_MAX
+};
+
+#define EROFS_I_VERSION_BITS            1
+#define EROFS_I_DATA_MAPPING_BITS       3
+
+#define EROFS_I_VERSION_BIT             0
+__EROFS_BIT(EROFS_I_, DATA_MAPPING, VERSION);
+
+struct erofs_inode_v1 {
+/*  0 */__le16 i_advise;
+
+/* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
+/*  2 */__le16 i_xattr_icount;
+/*  4 */__le16 i_mode;
+/*  6 */__le16 i_nlink;
+/*  8 */__le32 i_size;
+/* 12 */__le32 i_reserved;
+/* 16 */union {
+		/* file total compressed blocks for data mapping 1 */
+		__le32 compressed_blocks;
+		__le32 raw_blkaddr;
+
+		/* for device files, used to indicate old/new device # */
+		__le32 rdev;
+	} i_u __packed;
+/* 20 */__le32 i_ino;           /* only used for 32-bit stat compatibility */
+/* 24 */__le16 i_uid;
+/* 26 */__le16 i_gid;
+/* 28 */__le32 i_checksum;
+} __packed;
+
+/* 32 bytes on-disk inode */
+#define EROFS_INODE_LAYOUT_V1   0
+/* 64 bytes on-disk inode */
+#define EROFS_INODE_LAYOUT_V2   1
+
+struct erofs_inode_v2 {
+	__le16 i_advise;
+
+	/* 1 header + n-1 * 4 bytes inline xattr to keep continuity */
+	__le16 i_xattr_icount;
+	__le16 i_mode;
+	__le16 i_reserved;      /* 8 bytes */
+	__le64 i_size;          /* 16 bytes */
+	union {
+		/* file total compressed blocks for data mapping 1 */
+		__le32 compressed_blocks;
+		__le32 raw_blkaddr;
+
+		/* for device files, used to indicate old/new device # */
+		__le32 rdev;
+	} i_u __packed;
+
+	/* only used for 32-bit stat compatibility */
+	__le32 i_ino;           /* 24 bytes */
+
+	__le32 i_uid;
+	__le32 i_gid;
+	__le64 i_ctime;         /* 32 bytes */
+	__le32 i_ctime_nsec;
+	__le32 i_nlink;
+	__u8   i_reserved2[12];
+	__le32 i_checksum;      /* 64 bytes */
+} __packed;
+
+#define EROFS_MAX_SHARED_XATTRS         (128)
+/* h_shared_count between 129 ... 255 are special # */
+#define EROFS_SHARED_XATTR_EXTENT       (255)
+
+/*
+ * inline xattrs (n == i_xattr_icount):
+ * erofs_xattr_ibody_header(1) + (n - 1) * 4 bytes
+ *          12 bytes           /                   \
+ *                            /                     \
+ *                           /-----------------------\
+ *                           |  erofs_xattr_entries+ |
+ *                           +-----------------------+
+ * inline xattrs must starts in erofs_xattr_ibody_header,
+ * for read-only fs, no need to introduce h_refcount
+ */
+struct erofs_xattr_ibody_header {
+	__le32 h_checksum;
+	__u8   h_shared_count;
+	__u8   h_reserved[7];
+	__le32 h_shared_xattrs[0];      /* shared xattr id array */
+} __packed;
+
+/* Name indexes */
+#define EROFS_XATTR_INDEX_USER              1
+#define EROFS_XATTR_INDEX_POSIX_ACL_ACCESS  2
+#define EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
+#define EROFS_XATTR_INDEX_TRUSTED           4
+#define EROFS_XATTR_INDEX_LUSTRE            5
+#define EROFS_XATTR_INDEX_SECURITY          6
+
+/* xattr entry (for both inline & shared xattrs) */
+struct erofs_xattr_entry {
+	__u8   e_name_len;      /* length of name */
+	__u8   e_name_index;    /* attribute name index */
+	__le16 e_value_size;    /* size of attribute value */
+	/* followed by e_name and e_value */
+	char   e_name[0];       /* attribute name */
+} __packed;
+
+#define ondisk_xattr_ibody_size(count)	({\
+	u32 __count = le16_to_cpu(count); \
+	((__count) == 0) ? 0 : \
+	sizeof(struct erofs_xattr_ibody_header) + \
+		sizeof(__u32) * ((__count) - 1); })
+
+#define EROFS_XATTR_ALIGN(size) round_up(size, sizeof(struct erofs_xattr_entry))
+#define EROFS_XATTR_ENTRY_SIZE(entry) EROFS_XATTR_ALIGN( \
+	sizeof(struct erofs_xattr_entry) + \
+	(entry)->e_name_len + le16_to_cpu((entry)->e_value_size))
+
+/* have to be aligned with 8 bytes on disk */
+struct erofs_extent_header {
+	__le32 eh_checksum;
+	__le32 eh_reserved[3];
+} __packed;
+
+/*
+ * Z_EROFS Variable-sized Logical Extent cluster type:
+ *    0 - literal (uncompressed) cluster
+ *    1 - compressed cluster (for the head logical cluster)
+ *    2 - compressed cluster (for the other logical clusters)
+ *
+ * In detail,
+ *    0 - literal (uncompressed) cluster,
+ *        di_advise = 0
+ *        di_clusterofs = the literal data offset of the cluster
+ *        di_blkaddr = the blkaddr of the literal cluster
+ *
+ *    1 - compressed cluster (for the head logical cluster)
+ *        di_advise = 1
+ *        di_clusterofs = the decompressed data offset of the cluster
+ *        di_blkaddr = the blkaddr of the compressed cluster
+ *
+ *    2 - compressed cluster (for the other logical clusters)
+ *        di_advise = 2
+ *        di_clusterofs =
+ *           the decompressed data offset in its own head cluster
+ *        di_u.delta[0] = distance to its corresponding head cluster
+ *        di_u.delta[1] = distance to its corresponding tail cluster
+ *                (di_advise could be 0, 1 or 2)
+ */
+enum {
+	Z_EROFS_VLE_CLUSTER_TYPE_PLAIN,
+	Z_EROFS_VLE_CLUSTER_TYPE_HEAD,
+	Z_EROFS_VLE_CLUSTER_TYPE_NONHEAD,
+	Z_EROFS_VLE_CLUSTER_TYPE_RESERVED,
+	Z_EROFS_VLE_CLUSTER_TYPE_MAX
+};
+
+#define Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS        2
+#define Z_EROFS_VLE_DI_CLUSTER_TYPE_BIT         0
+
+struct z_erofs_vle_decompressed_index {
+	__le16 di_advise;
+	/* where to decompress in the head cluster */
+	__le16 di_clusterofs;
+
+	union {
+		/* for the head cluster */
+		__le32 blkaddr;
+		/*
+		 * for the rest clusters
+		 * eg. for 4k page-sized cluster, maximum 4K*64k = 256M)
+		 * [0] - pointing to the head cluster
+		 * [1] - pointing to the tail cluster
+		 */
+		__le16 delta[2];
+	} di_u __packed;		/* 8 bytes */
+} __packed;
+
+#define Z_EROFS_VLE_EXTENT_ALIGN(size) round_up(size, \
+	sizeof(struct z_erofs_vle_decompressed_index))
+
+/* dirent sorts in alphabet order, thus we can do binary search */
+struct erofs_dirent {
+	__le64 nid;     /*  0, node number */
+	__le16 nameoff; /*  8, start offset of file name */
+	__u8 file_type; /* 10, file type */
+	__u8 reserved;  /* 11, reserved */
+} __packed;
+
+/* file types used in inode_info->flags */
+enum {
+	EROFS_FT_UNKNOWN,
+	EROFS_FT_REG_FILE,
+	EROFS_FT_DIR,
+	EROFS_FT_CHRDEV,
+	EROFS_FT_BLKDEV,
+	EROFS_FT_FIFO,
+	EROFS_FT_SOCK,
+	EROFS_FT_SYMLINK,
+	EROFS_FT_MAX
+};
+
+#define EROFS_NAME_LEN      255
+
+/* check the EROFS on-disk layout strictly at compile time */
+static inline void erofs_check_ondisk_layout_definitions(void)
+{
+	BUILD_BUG_ON(sizeof(struct erofs_super_block) != 128);
+	BUILD_BUG_ON(sizeof(struct erofs_inode_v1) != 32);
+	BUILD_BUG_ON(sizeof(struct erofs_inode_v2) != 64);
+	BUILD_BUG_ON(sizeof(struct erofs_xattr_ibody_header) != 12);

erofs_fs.h line 277

Quoted 10 lines
erofs_fs.h new file +10 -0
+	BUILD_BUG_ON(sizeof(struct erofs_xattr_entry) != 4);
+	BUILD_BUG_ON(sizeof(struct erofs_extent_header) != 16);
+	BUILD_BUG_ON(sizeof(struct z_erofs_vle_decompressed_index) != 8);
+	BUILD_BUG_ON(sizeof(struct erofs_dirent) != 12);
+
+	BUILD_BUG_ON(BIT(Z_EROFS_VLE_DI_CLUSTER_TYPE_BITS) <
+		     Z_EROFS_VLE_CLUSTER_TYPE_MAX - 1);
+}
+
+#define EROFS_SB_SIZE sizeof(struct erofs_super_block)

how about directly using sizeof(struct erofs_super_block)? what is the benefit to define this?

p.s. such the following mkfs stuffs should be moved into a new mkfs-specfic file, (eg. internal.h or other names).

erofs_fs.h should be the same as the kernel implementation.

erofs_fs.h line 280

Quoted 3 lines
erofs_fs.h new file +3 -0
+
+#define EROFS_DECOMPR_IDX_SZ        sizeof(struct z_erofs_vle_decompressed_index)
+#define EROFS_DECOMPR_IDXS_PER_BLK  (EROFS_BLKSIZE / EROFS_DECOMPR_IDX_SZ)

duplicate Definition.

Quoted 5 lines
erofs_fs.h new file +5 -0
+
+#define EROFS_INODE_V1_SIZE     sizeof(struct erofs_inode_v1)
+#define EROFS_INODE_V2_SIZE     sizeof(struct erofs_inode_v2)
+
+#define EROFS_DIRENT_SIZE       sizeof(struct erofs_dirent)

erofs_fs.h line 295

Quoted 10 lines
erofs_fs.h new file +10 -0
+#define EROFS_DECOMPR_IDX_SZ    sizeof(struct z_erofs_vle_decompressed_index)
+#define EROFS_DECOMPR_IDXS_PER_BLK  (EROFS_BLKSIZE / EROFS_DECOMPR_IDX_SZ)
+
+#define ondisk_extent_size(data_mapping_mode, count) \
+	((data_mapping_mode) == EROFS_INODE_LAYOUT_COMPRESSION ? \
+	(sizeof(struct erofs_extent_header) + \
+	sizeof(__u32) * le32_to_cpu(count)) : 0)
+
+#define EROFS_INLINE_GENERIC_ALIGN_SIZE     (4)
+#define EROFS_INLINE_INDEX_ALIGN_SIZE       (8)

what are these two macros used for?

Thanks,
Gao Xiang

Quoted 4 lines
erofs_fs.h new file +3 -0
+
+#endif
+
#3 lore raw

RFC PATCH 2/5 erofs-mkfs: introduce mkfs basic framework

In reply to [email protected]

From: Li Guifu <[email protected]>

This patch adds basic code to generate erofs file systems and empty images can be created properly.

Some parameters are also introduced in this patch, used for setting the debugging level, whether to compress or not.

Signed-off-by Li Guifu <bluce.liguifu at huawei.com>
Signed-off-by Miao Xie <miaoxie at huawei.com>
Signed-off-by Fang Wei <fangwei1 at huawei.com>
---
 erofs_config.c |  46 +++++++++++++
 erofs_config.h |  44 +++++++++++++
 erofs_debug.h  |  67 +++++++++++++++++++
 erofs_error.h  |  25 +++++++
 erofs_io.c     | 175 +++++++++++++++++++++++++++++++++++++++++++++++++
 erofs_io.h     |  24 +++++++
 erofs_types.h  |  52 +++++++++++++++
 list_head.h    | 110 +++++++++++++++++++++++++++++++
 mkfs_erofs.h   |  83 +++++++++++++++++++++++
 mkfs_main.c    | 168 +++++++++++++++++++++++++++++++++++++++++++++++
 10 files changed, 794 insertions(+)
 create mode 100644 erofs_config.c
 create mode 100644 erofs_config.h
 create mode 100644 erofs_debug.h
 create mode 100644 erofs_error.h
 create mode 100644 erofs_io.c
 create mode 100644 erofs_io.h
 create mode 100644 erofs_types.h
 create mode 100644 list_head.h
 create mode 100644 mkfs_erofs.h
 create mode 100644 mkfs_main.c
erofs_config.c new file +46 -0
new file mode 100644
index 0000000..e1217a0
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * erofs_config.c
+ *
+ * Copyright (C) 2018 HUAWEI, Inc.
+ *             http://www.huawei.com/
+ * Created by Li Guifu <bluce.liguifu at huawei.com>
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "erofs_config.h"
+
+struct erofs_configure erofs_cfg;
+
+
+void mkfs_init_configure(void)
+{
+	memset(&erofs_cfg, 0, sizeof(erofs_cfg));
+	erofs_cfg.c_alg_name = "none";
+	erofs_cfg.c_dbg_lvl = 0;
+	erofs_cfg.c_version = EROFS_MKFS_VERSION "   " __DATE__ " " __TIME__;
+}
+
+void mkfs_dump_config(void)
+{
+	fprintf(stderr,  "\tc_version:             [%8s]\n", erofs_cfg.c_version);
+	fprintf(stderr,  "\tc_img_path:            [%8s]\n", erofs_cfg.c_img_path);
+	fprintf(stderr,  "\tc_src_path:            [%8s]\n", erofs_cfg.c_src_path);
+	fprintf(stderr,  "\tc_dbg_lvl:             [%8d]\n", erofs_cfg.c_dbg_lvl);
+	fprintf(stderr,  "\tc_dry_run:             [%8d]\n", erofs_cfg.c_dry_run);
+	fprintf(stderr,  "\tc_alg_name:            [%8s]\n", erofs_cfg.c_alg_name);
+	fprintf(stderr,  "\tc_compr_maxsz:         [%8d]\n", erofs_cfg.c_compr_maxsz);
+	fprintf(stderr,  "\tc_compr_lvl:           [%8d]\n", erofs_cfg.c_compr_lvl);
+	fprintf(stderr,  "\tc_compr_boundary:      [%8d]\n", erofs_cfg.c_compr_boundary);
+	fprintf(stderr,  "\tc_compr_ratio_limit:   [%8d]\n", erofs_cfg.c_compr_ratio_limit);
+}
+
+void mkfs_free_config(void)
+{
+	if(erofs_cfg.c_img_path)
+		free(erofs_cfg.c_img_path);
+
+	if(erofs_cfg.c_src_path)
+		free(erofs_cfg.c_src_path);
+}
erofs_config.h new file +44 -0

44 lines not on this page · raw message

erofs_debug.h new file +67 -0

67 lines not on this page · raw message

erofs_error.h new file +25 -0
new file mode 100644
index 0000000..2075081
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * erofs_error.h
+ *
+ * Copyright (C) 2018 HUAWEI, Inc.
+ *             http://www.huawei.com/
+ * Created by Li Guifu <bluce.liguifu at huawei.com>
+ */
+#ifndef __EROFS_ERROR_H__
+#define __EROFS_ERROR_H__
+
+#define MAX_ERRNO       (4095)
+#define IS_ERR_VALUE(x)     \
+	((unsigned long)(void *)(x) >= (unsigned long)-MAX_ERRNO)
+
+static inline void* ERR_PTR(long error)
+{
+	return (void*) error;
+}
+
+static inline int IS_ERR(const void* ptr)
+{
+	return IS_ERR_VALUE((unsigned long)ptr);
+}
+#endif
erofs_io.c new file +175 -0

175 lines not on this page · raw message

erofs_io.h new file +24 -0
new file mode 100644
index 0000000..bbb3ab3
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * erofs_io.h
+ *
+ * Copyright (C) 2018 HUAWEI, Inc.
+ *             http://www.huawei.com/
+ * Created by Li Guifu <bluce.liguifu at huawei.com>
+ */
+#ifndef __EROFS_IO_H__
+#define __EROFS_IO_H__
+
+#include <sys/cdefs.h>
+#include "erofs_types.h"
+
+int dev_open(const char* devname);
+void dev_close(void);
+int dev_write(void* buf, u64 offset, size_t len);
+int dev_write_block(void* buf, u32 blkaddr);
+int dev_fsync(void);
+u64 dev_length(void);
+
+#endif
+
+
erofs_types.h new file +52 -0

52 lines not on this page · raw message

list_head.h new file +110 -0

111 lines not on this page · raw message

mkfs_erofs.h new file +83 -0

83 lines not on this page · raw message

mkfs_main.c new file +168 -0

168 lines not on this page · raw message

Signature · 1 line
2.17.1
#4 lore raw

RFC PATCH 2/5 erofs-mkfs: introduce mkfs basic framework

Hi Guifu,

Quoted 5 lines · On 2018/11/16 11:30, [email protected] wrote:

From: Li Guifu <bluce.liguifu at huawei.com>

This patch adds basic code to generate erofs file systems and empty images can be created properly.

Quoted 7 lines

Some parameters are also introduced in this patch, used for setting the debugging level, whether to compress or not.

Signed-off-by Li Guifu <bluce.liguifu at huawei.com>
Signed-off-by Miao Xie <miaoxie at huawei.com>
Signed-off-by Fang Wei <fangwei1 at huawei.com>

---

I have fixed some checkpatch errors for you, please check them out and send v2 to the mailing list when you have some time to do.

https://git.kernel.org/pub/scm/linux/kernel/git/xiang/erofs-utils.git/

Thanks,
Gao Xiang

#5 lore raw

RFC PATCH 3/5 erofs-mkfs: support to build a image from the specfic root directory

In reply to [email protected]

From: Li Guifu <[email protected]>

Currently, regular files, directories, special files defined in POSIX are supported and the generated image can be mounted by kernel.

Signed-off-by Li Guifu <bluce.liguifu at huawei.com>
Signed-off-by Miao Xie <miaoxie at huawei.com>
Signed-off-by Fang Wei <fangwei1 at huawei.com>
---
 erofs_cache.c | 211 ++++++++++++++
 erofs_cache.h |  58 ++++
 mkfs_erofs.h  |  36 ---
 mkfs_file.c   | 304 ++++++++++++++++++++
 mkfs_file.h   |  79 ++++++
 mkfs_inode.c  | 773 ++++++++++++++++++++++++++++++++++++++++++++++++++
 mkfs_inode.h  |  84 ++++++
 mkfs_main.c   |  28 ++
 8 files changed, 1537 insertions(+), 36 deletions(-)
 create mode 100644 erofs_cache.c
 create mode 100644 erofs_cache.h
 create mode 100644 mkfs_file.c
 create mode 100644 mkfs_file.h
 create mode 100644 mkfs_inode.c
 create mode 100644 mkfs_inode.h
erofs_cache.c new file +211 -0
new file mode 100644
index 0000000..a2153fe
@@ -0,0 +1,211 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * erofs_cache.c
+ *
+ * Copyright (C) 2018 HUAWEI, Inc.
+ *             http://www.huawei.com/
+ * Created by Miao Xie <miaoxie at huawei.com>
+ */
+#include <string.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <errno.h>
+
+#include "erofs_fs.h"
+#include "erofs_types.h"
+#include "erofs_cache.h"
+#include "erofs_error.h"
+#include "erofs_debug.h"
+#include "erofs_io.h"
+
+#include "mkfs_erofs.h"
+#include "mkfs_inode.h"
+
+/* The 1st block is used for SUPER BLOCK, so skip it. */
+static u64 erofs_current_block = 1;
+static LIST_HEAD(erofs_global_blocks);
+static LIST_HEAD(erofs_free_blocks);
+static LIST_HEAD(erofs_full_blocks);
+
+static char* erofs_blk_buf;
+
+/*
+ * Note: Since we reserved the 1st block for super block, so we can reuse
+ * this block number as a special number, we use it to tell the caller that
+ * there is NO ENOUGH FREE SPACE!!!!
+ *
+ * So the result value:
+ * !0 : Allocate the blocks successfully, this is block number of
+ *      the 1st block.
+ * 0  : ENOSPC, There is no enough space.
+ */
+u32 erofs_alloc_blocks(u32 nblocks)
+{
+	u32 blkaddr;
+	u64 devlen;
+
+	assert(nblocks);
+
+	devlen = dev_length();
+	if (erofs_current_block > (u64)UINT32_MAX ||
+	    erofs_current_block + nblocks > ((u64)UINT32_MAX) + 1 ||
+	    (erofs_current_block + nblocks) << EROFS_BLOCKSIZE_BITS > devlen) {
+		erofs_err("There is no enough free space(curr: %llu, need: %u, device blocks: %llu).",
+			   (unsigned long long)erofs_current_block, nblocks,
+			   (unsigned long long)devlen >> EROFS_BLOCKSIZE_BITS);
+		return 0;
+	}
+
+	blkaddr = (u32)erofs_current_block;
+	erofs_current_block += nblocks;
+
+	return blkaddr;
+}
+
+u64 erofs_get_total_blocks(void)
+{
+	return erofs_current_block;
+}
+
+
+block_buffer_t* erofs_alloc_multi_block_buffer(u32 nblocks)
+{
+	block_buffer_t*     blk;
+	block_buffer_t*     next;
+	block_buffer_t*     first;
+	struct list_head    blocks;
+	u32 blkaddr;
+	u32 i;
+	int ret;
+
+	init_list_head(&blocks);
+
+	for (i = 0; i < nblocks; i++) {
+		blk = (block_buffer_t*)malloc(sizeof(block_buffer_t));
+		if (!blk) {
+			erofs_err("Fail to alloc memory for block buffer");
+			ret = -ENOMEM;
+			goto free_block_buffer;
+		}
+		memset(blk, 0, sizeof(block_buffer_t));
+		init_list_head(&blk->bb_metadata_list);
+
+		list_add_tail(&blk->bb_global_node, &blocks);
+	}
+
+	blkaddr = erofs_alloc_blocks(nblocks);
+	if (!blkaddr) {
+		ret = -ENOSPC;
+		goto free_block_buffer;
+	}
+
+	first = list_first_entry(&blocks, block_buffer_t, bb_global_node);
+	list_for_each_entry_safe(blk, next, &blocks, bb_global_node) {
+		blk->bb_blkaddr = blkaddr;
+		blkaddr++;
+
+		list_del(&blk->bb_global_node);
+		list_add_tail(&blk->bb_global_node, &erofs_global_blocks);
+		list_add_tail(&blk->bb_alloc_node, &erofs_free_blocks);
+	}
+
+	return first;
+
+free_block_buffer:
+	list_for_each_entry_safe(blk, next, &blocks, bb_global_node) {
+		list_del(&blk->bb_global_node);
+		free(blk);
+	}
+	return ERR_PTR(ret);
+}
+
+block_buffer_t* erofs_alloc_single_block_buffer(void)
+{
+	return erofs_alloc_multi_block_buffer(1);
+}
+
+block_buffer_t* erofs_look_up_free_pos(int request_size)
+{
+	block_buffer_t* blk = NULL;
+
+	list_for_each_entry(blk, &erofs_free_blocks, bb_alloc_node) {
+		if ((request_size + blk->bb_free_slot * EROFS_SLOTSIZE) <
+		     EROFS_BLKSIZE)
+			return blk;
+	}
+
+	blk = erofs_alloc_single_block_buffer();
+	return blk;
+}
+
+int erofs_flush_all_blocks(void)
+{
+	struct block_buffer* blk;
+	struct erofs_meta_node*  node;
+	struct erofs_node_info*  inode;
+	struct erofs_index_info* index;
+	char*            pbuf;
+	int         count;
+	int         ret = 0;
+
+	list_for_each_entry(blk, &erofs_global_blocks, bb_global_node) {
+		pbuf = erofs_blk_buf;
+		memset(pbuf, 0, EROFS_BLKSIZE);
+
+		list_for_each_entry(node, &blk->bb_metadata_list, m_node) {
+			switch (node->m_type) {
+			case EROFS_META_INODE:
+				inode = (struct erofs_node_info*)node;
+
+				count = erofs_write_inode_buffer(inode, pbuf);
+				break;
+			case EROFS_META_INDEX:
+				index = (struct erofs_index_info*)node;
+
+				count = erofs_write_index_buffer(index, pbuf);
+				break;
+			default:
+				erofs_err("Wrong metadata type");
+				assert(0);
+			}
+
+			count = round_up(count, EROFS_SLOTSIZE);
+			assert(count == node->m_len);
+			pbuf += count;
+		}
+
+		ret = dev_write(erofs_blk_buf, BLKNO_TO_ADDR(blk->bb_blkaddr),
+				EROFS_BLKSIZE);
+		if (ret)
+			break;
+	}
+
+	return ret;
+}
+
+void erofs_put_block_buffer(struct block_buffer* blk)
+{
+	if (blk->bb_free_slot == MAX_NID_INDEX_PER_BLK) {
+		list_del(&blk->bb_alloc_node);
+		list_add_tail(&blk->bb_alloc_node, &erofs_full_blocks);
+	} else if (blk->bb_free_slot > MAX_NID_INDEX_PER_BLK) {
+		erofs_err("block buffer overflow: free_slot = %d, MAX_NID_INDEX_PER_BLK = %d",
+			  blk->bb_free_slot, MAX_NID_INDEX_PER_BLK);
+		assert(0);
+	}
+}
+
+int erofs_cache_init(u64 start_blk)
+{
+	erofs_blk_buf = malloc(EROFS_BLKSIZE);
+	if (!erofs_blk_buf)
+		return -ENOMEM;
+	erofs_current_block = start_blk;
+	return 0;
+}
+
+void erofs_cache_deinit(void)
+{
+	if (erofs_blk_buf)
+		free(erofs_blk_buf);
+}
erofs_cache.h new file +58 -0

58 lines not on this page · raw message

mkfs_erofs.h +0 -36
index 1790862..e408474 100644
@@ -44,40 +44,4 @@ typedef unsigned int __u32;
 #define MAX_NID_INDEX_PER_BLK   (EROFS_BLKSIZE / EROFS_SLOTSIZE)
 
 
-struct erofs_node_info {
-	/* Original member */
-	struct list_head list;
-	struct list_head subdir_head;/* sub dirs or files */
-	struct list_head xattr_head;
-
-	u64 inode_base_addr;         /* base address of a inode */
-	char name[NAME_MAX];         /* the name of current inode */
-	char fullpath[PATH_MAX + 1];
-	u16 d_nameoff;
-	u16         i_iver;          /* Inode Version */
-	u16         i_dmode;         /* Data mode */
-	u16         i_xattr_scnt;    /* Inline xattr space count */
-	u16         i_shared_count;
-	u16         i_mode;
-	u8          i_type;          /* Inode type: File, Dir...*/
-	u64         i_size;
-	union {
-		u32     i_blkaddr;
-		u32     i_blocks;
-		u32     i_rdev;
-	};
-	u32         i_ino;
-	u32         i_uid;
-	u32         i_gid;
-	u64         i_ctime;
-	u32         i_ctime_nsec;
-	u32         i_nlink;
-
-	/* If compress file, we use it store index info */
-	char*       i_inline_data;
-	int         i_inline_datalen;
-	int         i_inline_align_size;
-};
-
-
 #endif
mkfs_file.c new file +304 -0

305 lines not on this page · raw message

mkfs_file.h new file +79 -0

80 lines not on this page · raw message

mkfs_inode.c new file +773 -0

773 lines not on this page · raw message

mkfs_inode.h new file +84 -0

84 lines not on this page · raw message

mkfs_main.c +28 -0
index d4ac161..69c88dc 100644
@@ -18,6 +18,7 @@
 #include "erofs_fs.h"
 #include "mkfs_erofs.h"
 #include "erofs_io.h"
+#include "mkfs_inode.h"
 #define pr_fmt(fmt) "MKFS: "FUNC_LINE_FMT fmt"\n"
 #include "erofs_debug.h"
 
@@ -132,6 +133,8 @@ void mkfs_update_erofs_header(u64 root_addr)
 	}
 
 	sb->meta_blkaddr = cpu_to_le32(ADDR_TO_BLKNO(size));
+	sb->blocks   = cpu_to_le32(erofs_get_total_blocks());
+	sb->root_nid = cpu_to_le16(mkfs_addr_to_nid(root_addr));
 
 	sb_buf = calloc(size, 1);
 	if (!sb_buf) {
@@ -157,12 +160,37 @@ int main(int argc, char** argv)
 	mkfs_init_configure();
 	mkfs_parse_options_cfg(argc, argv);
 
+	proot_node = mkfs_prepare_root_inode(erofs_cfg.c_src_path);
+	if(!proot_node)
+		goto exit;
+	err = erofs_create_files_list(proot_node);
+	if(err)
+		goto exit;
+
+	err = erofs_cache_init(ADDR_TO_BLKNO(BLK_ALIGN(EROFS_HEADER_SIZE)));
+	if (err)
+		goto exit;
+
+	mkfs_relocate_sub_inodes(proot_node);
+
+	mkfs_do_write_inodes_data(proot_node);
+
+	err = erofs_flush_all_blocks();
+	if (err)
+		goto exit;
+
 	mkfs_update_erofs_header(proot_node->i_base_addr);
 
 	dev_close();
 	mkfs_free_config();
 	erofs_info("done");
 
+exit:
+
+	if (err)
+		erofs_err("\tError: Could not format the device!!!\n");
+
+	erofs_cache_deinit();
 	return err;
 }
 
Signature · 1 line
2.17.1
#6 lore raw

RFC PATCH 4/5 erofs-mkfs: add compression support

In reply to [email protected]

From: Li Guifu <[email protected]>

This patch introduces a compression framework, so it can support different compression algorithm in principle, currently lz4hc is added right now due to the kernel implementation.

Signed-off-by Li Guifu <bluce.liguifu at huawei.com>
Signed-off-by Miao Xie <miaoxie at huawei.com>
Signed-off-by Fang Wei <fangwei1 at huawei.com>
---
 erofs_compressor.c | 144 ++++++++++
 erofs_compressor.h |  49 ++++
 erofs_lz4hc.c      |  58 ++++
 erofs_lz4hc.h      |  22 ++
 mkfs_file.c        | 689 ++++++++++++++++++++++++++++++++++++++++++++-
 mkfs_inode.c       |  61 +++-
 mkfs_main.c        |   4 +
 7 files changed, 1024 insertions(+), 3 deletions(-)
 create mode 100644 erofs_compressor.c
 create mode 100644 erofs_compressor.h
 create mode 100644 erofs_lz4hc.c
 create mode 100644 erofs_lz4hc.h
erofs_compressor.c new file +144 -0
new file mode 100644
index 0000000..093b28a
@@ -0,0 +1,144 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * erofs_compressor.c
+ *
+ * Copyright (C) 2017-2018 HUAWEI, Inc.
+ *             http://www.huawei.com/
+ * Created by Miao Xie <miaoxie at huawei.com>
+ */
+#include <string.h>
+#include <assert.h>
+
+#include "erofs_error.h"
+#include "erofs_compressor.h"
+#include "erofs_lz4hc.h"
+#include "erofs_debug.h"
+#include "mkfs_erofs.h"
+
+static struct erofs_compr_alg erofs_compr_desc[EROFS_COMPR_ALG_MAX] = {
+	[EROFS_COMPR_NONE] = {
+		.ca_name    = "none",
+		.ca_idx     = EROFS_COMPR_NONE,
+		.ca_max_lvl = 0,
+		.ca_min_lvl = 0,
+		.ca_def_lvl = 0,
+		.ca_compress    = NULL,
+		.ca_init    = NULL,
+		.ca_deinit  = NULL,
+	},
+	[EROFS_COMPR_LZ4HC] = {
+		.ca_name    = "lz4hc",
+		.ca_idx     = EROFS_COMPR_LZ4HC,
+		.ca_max_lvl = LZ4HC_CLEVEL_MAX,
+		.ca_min_lvl = LZ4HC_CLEVEL_MIN,
+		.ca_def_lvl = EROFS_COMPR_LZ4HC_DEF_LVL,
+		.ca_compress    = erofs_lz4hc_compress,
+		.ca_init    = erofs_lz4hc_init,
+		.ca_deinit  = erofs_lz4hc_deinit,
+	},
+};
+
+void erofs_compress_alg_init(const char* name)
+{
+	int level;
+	struct erofs_compr_alg* alg;
+
+	if (!name) {
+		erofs_err("compress alg name is NULL !!!");
+		exit(EXIT_FAILURE);
+	}
+
+	/* name:  lz4hc or none */
+	alg = erofs_get_compress_alg(name);
+	if(!alg) {
+		erofs_err("can found alg[%s]", name);
+		exit(EXIT_FAILURE);
+	}
+	erofs_cfg.c_compr_alg       = alg;
+	erofs_cfg.c_compr_maxsz = BLK_ALIGN(EROFS_CONFIG_COMPR_MAX_SZ);
+
+	level = erofs_adjust_compress_level(alg, EROFS_COMPR_LZ4HC_DEF_LVL);
+	erofs_cfg.c_compr_lvl       = level;
+	erofs_cfg.c_compr_boundary  = EROFS_CONFIG_COMPR_DEF_BOUNDARY;
+	erofs_cfg.c_compr_ratio_limit   = EROFS_CONFIG_COMPR_RATIO_MAX_LIMIT;
+
+}
+struct erofs_compr_alg* erofs_get_compress_alg(const char* name)
+{
+	int i;
+
+	for (i = EROFS_COMPR_NONE; i < EROFS_COMPR_ALG_MAX; i++) {
+		if (strcmp(name, erofs_compr_desc[i].ca_name) == 0)
+			return &erofs_compr_desc[i];
+	}
+
+	return NULL;
+}
+
+int erofs_adjust_compress_level(struct erofs_compr_alg* alg, int lvl)
+{
+	if (!alg)
+		return 0;
+
+	if (lvl > alg->ca_max_lvl) {
+		erofs_err("Compress level(%d) is greater than max level(%d), adjust it to default level(%d).\n",
+			   lvl, alg->ca_max_lvl, EROFS_COMPR_LZ4HC_DEF_LVL);
+		return alg->ca_def_lvl;
+	}
+
+	if (lvl < alg->ca_min_lvl) {
+		erofs_err("Compress level(%d) is less than min level(%d), adjust it to default level(%d).\n",
+			   lvl, alg->ca_min_lvl, EROFS_COMPR_LZ4HC_DEF_LVL);
+		return alg->ca_def_lvl;
+	}
+
+	return lvl;
+}
+
+void* erofs_compress_init(struct erofs_compr_alg* alg)
+{
+	void* ctx;
+
+	if (!alg->ca_init)
+		return NULL;
+
+	ctx = alg->ca_init();
+
+	return ctx;
+}
+
+void erofs_compress_deinit(struct erofs_compr_alg* alg, void* cctx)
+{
+	if (!alg->ca_deinit)
+		return;
+
+	alg->ca_deinit(cctx);
+}
+
+int64_t erofs_compress_onctx(struct erofs_compr_alg* alg, void* ctx,
+			     char* in, size_t insz,
+			     char* out, size_t outsz, size_t* srcsz, int lvl)
+{
+	assert(alg->ca_compress);
+
+	return alg->ca_compress(in, insz, out, outsz, srcsz, lvl, ctx);
+}
+
+int64_t erofs_compress(struct erofs_compr_alg* alg, char* in, size_t insz,
+		       char* out, size_t outsz, size_t* srcsz, int lvl)
+{
+	void* ctx = NULL;
+	int64_t ret;
+
+	if (alg->ca_init) {
+		ctx = alg->ca_init();
+		if (IS_ERR(ctx))
+			return EROFS_COMPRESS_ERROR;
+	}
+
+	ret = alg->ca_compress(in, insz, out, outsz, srcsz, lvl, ctx);
+	if (alg->ca_deinit)
+		alg->ca_deinit(ctx);
+
+	return ret;
+}
erofs_compressor.h new file +49 -0

49 lines not on this page · raw message

erofs_lz4hc.c new file +58 -0

58 lines not on this page · raw message

erofs_lz4hc.h new file +22 -0
new file mode 100644
index 0000000..7eef918
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * erofs_lz4hc.h
+ *
+ * Copyright (C) 2018 HUAWEI, Inc.
+ *             http://www.huawei.com/
+ * Created by Li Guifu <bluce.liguifu at huawei.com>
+ */
+#ifndef __EROFS_LZ4HC_H__
+#define __EROFS_LZ4HC_H__
+
+#include <stdint.h>
+#include <lz4hc.h>
+
+#define EROFS_COMPR_LZ4HC_DEF_LVL       (9)
+
+void* erofs_lz4hc_init(void);
+void erofs_lz4hc_deinit(void* ctx);
+int64_t erofs_lz4hc_compress(char* in, size_t insz, char* out,
+			     size_t outsz, size_t* inszptr,
+			     int level, void*);
+#endif
mkfs_file.c +688 -1

699 lines not on this page · raw message

mkfs_inode.c +59 -2

91 lines not on this page · raw message

mkfs_main.c +4 -0
index 69c88dc..7467619 100644
@@ -19,6 +19,7 @@
 #include "mkfs_erofs.h"
 #include "erofs_io.h"
 #include "mkfs_inode.h"
+#include "erofs_compressor.h"
 #define pr_fmt(fmt) "MKFS: "FUNC_LINE_FMT fmt"\n"
 #include "erofs_debug.h"
 
@@ -110,6 +111,9 @@ static void mkfs_parse_options_cfg(int argc, char* argv[])
 		usage(argv[0]);
 	}
 
+	assert(erofs_cfg.c_alg_name);
+	erofs_compress_alg_init(erofs_cfg.c_alg_name);
+
 	mkfs_dump_config();
 
 	if (dev_open(erofs_cfg.c_img_path) < 0) {
Signature · 1 line
2.17.1
#7 lore raw

RFC PATCH 5/5 erofs-mkfs: add miscellaneous files

In reply to [email protected]

From: Li Guifu <[email protected]>

Add build environment, license and maintainers etc.

Signed-off-by Li Guifu <bluce.liguifu at huawei.com>
Signed-off-by Miao Xie <miaoxie at huawei.com>
Signed-off-by Fang Wei <fangwei1 at huawei.com>
---
 .gitignore   |  44 ++++++++++++++++++++
 AUTHORS      |   6 +++
 COPYING      | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++
 ChangeLog    |   0
 Makefile.am  |  35 ++++++++++++++++
 NEWS         |   0
 README       |  52 ++++++++++++++++++++++++
 configure.ac |  49 ++++++++++++++++++++++
 8 files changed, 299 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 AUTHORS
 create mode 100644 COPYING
 create mode 100644 ChangeLog
 create mode 100644 Makefile.am
 create mode 100644 NEWS
 create mode 100644 README
 create mode 100644 configure.ac
.gitignore new file +44 -0
new file mode 100644
index 0000000..7e0a1cf
@@ -0,0 +1,44 @@
+autom4te.cache
+build
+build.profiled
+build.static
+FILES
+^core
+*~
+patches
+Makefile
+*.bak
+*.diff
+*.dSYM
+*.o
+*.a
+*.orig
+*.patch
+*.pc
+*.rej
+*.swp
+00[0-9][1-9]*.patch
+MCONFIG
+asm_types.h
+config.log
+config.status
+cscope.*
+tags
+TAGS
+.deps/
+Makefile.in
+aclocal.m4
+compile
+config.guess
+config.h
+config.h.in
+config.sub
+configure
+depcomp
+install-sh
+libtool
+ltmain.sh
+m4/
+missing
+stamp-h1
+
AUTHORS new file +6 -0
new file mode 100644
index 0000000..6666775
@@ -0,0 +1,6 @@
+EROFS MKFS TOOL
+M: Li Guifu  <bluce.liguifu at huawei.com>
+M: Fang Wei  <fangwei1 at huawei.com>
+S: Maintained
+L: linux-erofs at lists.ozlabs.org
+
COPYING new file +113 -0

114 lines not on this page · raw message

ChangeLog new file +0 -0
new file mode 100644
index 0000000..e69de29
Makefile.am new file +35 -0
new file mode 100644
index 0000000..b3dc472
@@ -0,0 +1,35 @@
+# Makefile.am
+AUTOMAKE_OPTIONS = foreign
+bin_PROGRAMS     = mkfs.erofs
+mkfs_erofs_SOURCES = erofs_compressor.c \
+					 mkfs_file.c \
+					 erofs_lz4hc.c \
+					 mkfs_main.c \
+					 erofs_cache.c \
+					 erofs_io.c \
+					 mkfs_inode.c \
+					 erofs_config.c
+
+noinst_HEADERS = erofs_config.h  \
+				 mkfs_inode.h \
+				 erofs_error.h \
+				 erofs_lz4hc.h \
+				 list_head.h \
+				 erofs_cache.h \
+				 erofs_fs.h \
+				 erofs_debug.h \
+				 mkfs_erofs.h \
+				 erofs_compressor.h \
+				 erofs_io.h \
+				 erofs_types.h \
+				 mkfs_file.h
+
+mkfs_erofs_CFLAGS = -Wall -Werror -DEROFS_MKFS_VERSION=\"v1.0\"
+mkfs_erofs_LDFLAGS  = --static
+mkfs_erofs_LDADD   = $(LIBLZ4_STATIC)
+ACLOCAL_AMFLAGS = -I m4
+
+if SUPPORT_LARG_FILE_AT_BIT32
+    mkfs_erofs_CFLAGS += -D_FILE_OFFSET_BITS=64
+endif
+
NEWS new file +0 -0
new file mode 100644
index 0000000..e69de29
README new file +52 -0

52 lines not on this page · raw message

configure.ac new file +49 -0

49 lines not on this page · raw message

Signature · 1 line
2.17.1