[RFC PATCH mtd-utils 020/110] ubifs-utils: Add linux type definitions

From: Zhihao Cheng
Date: Fri Jun 07 2024 - 00:30:01 EST


Add linux type definitions, because there are many types
(eg. u8/u16/u64) used in UBIFS linux kernel libs. Besides
move type conversions (eg. cpu_to_le16, cpu_to_le32, etc.)
into type definitions header file.

This is a preparation for replacing implementation of UBIFS utils with
linux kernel libs.

Signed-off-by: Zhihao Cheng <chengzhihao1@xxxxxxxxxx>
---
ubifs-utils/Makemodule.am | 1 +
ubifs-utils/common/crypto.c | 1 +
ubifs-utils/common/defs.h | 30 -------------
ubifs-utils/common/fscrypt.c | 3 +-
ubifs-utils/common/fscrypt.h | 7 +--
ubifs-utils/common/linux_types.h | 89 +++++++++++++++++++++++++++++++++++++
ubifs-utils/common/sign.c | 2 +-
ubifs-utils/mkfs.ubifs/mkfs.ubifs.c | 9 ++--
8 files changed, 104 insertions(+), 38 deletions(-)
create mode 100644 ubifs-utils/common/linux_types.h

diff --git a/ubifs-utils/Makemodule.am b/ubifs-utils/Makemodule.am
index d58570fe..0f9c0fa6 100644
--- a/ubifs-utils/Makemodule.am
+++ b/ubifs-utils/Makemodule.am
@@ -1,5 +1,6 @@
common_SOURCES = \
ubifs-utils/common/compiler_attributes.h \
+ ubifs-utils/common/linux_types.h \
ubifs-utils/common/defs.h \
ubifs-utils/common/crc16.h \
ubifs-utils/common/crc16.c \
diff --git a/ubifs-utils/common/crypto.c b/ubifs-utils/common/crypto.c
index 2ecd8da1..e4ef3491 100644
--- a/ubifs-utils/common/crypto.c
+++ b/ubifs-utils/common/crypto.c
@@ -23,6 +23,7 @@
#include <string.h>
#include <assert.h>

+#include "linux_types.h"
#include "fscrypt.h"
#include "defs.h"
#include "ubifs.h"
diff --git a/ubifs-utils/common/defs.h b/ubifs-utils/common/defs.h
index cafc94af..dd3b806e 100644
--- a/ubifs-utils/common/defs.h
+++ b/ubifs-utils/common/defs.h
@@ -9,7 +9,6 @@
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
-#include <byteswap.h>
#include <errno.h>

#include "ubifs.h"
@@ -27,37 +26,8 @@ enum { MKFS_PROGRAM_TYPE = 0 };
printf("%s: %s: " fmt "\n", PROGRAM_NAME, __FUNCTION__, ##__VA_ARGS__); \
} while(0)

-#define t16(x) ({ \
- uint16_t __b = (x); \
- (__LITTLE_ENDIAN==__BYTE_ORDER) ? __b : bswap_16(__b); \
-})
-
-#define t32(x) ({ \
- uint32_t __b = (x); \
- (__LITTLE_ENDIAN==__BYTE_ORDER) ? __b : bswap_32(__b); \
-})
-
-#define t64(x) ({ \
- uint64_t __b = (x); \
- (__LITTLE_ENDIAN==__BYTE_ORDER) ? __b : bswap_64(__b); \
-})
-
-#define cpu_to_le16(x) ((__le16){t16(x)})
-#define cpu_to_le32(x) ((__le32){t32(x)})
-#define cpu_to_le64(x) ((__le64){t64(x)})
-
-#define le16_to_cpu(x) (t16((x)))
-#define le32_to_cpu(x) (t32((x)))
-#define le64_to_cpu(x) (t64((x)))
-
#define unlikely(x) (x)

-struct qstr
-{
- char *name;
- size_t len;
-};
-
/**
* fls - find last (most-significant) bit set
* @x: the word to search
diff --git a/ubifs-utils/common/fscrypt.c b/ubifs-utils/common/fscrypt.c
index 895d5c72..f39faa76 100644
--- a/ubifs-utils/common/fscrypt.c
+++ b/ubifs-utils/common/fscrypt.c
@@ -20,6 +20,7 @@

#include <endian.h>

+#include "linux_types.h"
#include "fscrypt.h"
#include "defs.h"
#include "ubifs.h"
@@ -88,7 +89,7 @@ unsigned int fscrypt_fname_encrypted_size(struct fscrypt_context *fctx,
return round_up(ilen, padding);
}

-int encrypt_path(void **outbuf, void *data, unsigned int data_len,
+int encrypt_path(void **outbuf, const void *data, unsigned int data_len,
unsigned int max_namelen, struct fscrypt_context *fctx)
{
void *inbuf, *crypt_key;
diff --git a/ubifs-utils/common/fscrypt.h b/ubifs-utils/common/fscrypt.h
index b8a599de..4a073e97 100644
--- a/ubifs-utils/common/fscrypt.h
+++ b/ubifs-utils/common/fscrypt.h
@@ -107,7 +107,7 @@ struct fscrypt_context *inherit_fscrypt_context(struct fscrypt_context *fctx);
void free_fscrypt_context(struct fscrypt_context *fctx);
unsigned int fscrypt_fname_encrypted_size(struct fscrypt_context *fctx,
unsigned int ilen);
-int encrypt_path(void **outbuf, void *data, unsigned int data_len,
+int encrypt_path(void **outbuf, const void *data, unsigned int data_len,
unsigned int max_namelen, struct fscrypt_context *fctx);
int encrypt_data_node(struct fscrypt_context *fctx, unsigned int block_no,
struct ubifs_data_node *dn, size_t length);
@@ -138,8 +138,9 @@ static inline void free_fscrypt_context(struct fscrypt_context *fctx)
assert(!fctx);
}

-static inline int encrypt_path(void **outbuf, void *data, unsigned int data_len,
- unsigned int max_namelen, struct fscrypt_context *fctx)
+static inline int encrypt_path(void **outbuf, const void *data,
+ unsigned int data_len, unsigned int max_namelen,
+ struct fscrypt_context *fctx)
{
(void)outbuf;
(void)data;
diff --git a/ubifs-utils/common/linux_types.h b/ubifs-utils/common/linux_types.h
new file mode 100644
index 00000000..556b2e25
--- /dev/null
+++ b/ubifs-utils/common/linux_types.h
@@ -0,0 +1,89 @@
+#ifndef __LINUX_TYPES_H__
+#define __LINUX_TYPES_H__
+
+#include <linux/types.h>
+#include <sys/types.h>
+#include <byteswap.h>
+#include <stdint.h>
+#include <unistd.h>
+
+#include "compiler_attributes.h"
+
+typedef __u8 u8;
+typedef __u16 u16;
+typedef __u32 u32;
+typedef __u64 u64;
+
+typedef __s64 time64_t;
+
+struct qstr {
+ const char *name;
+ size_t len;
+};
+
+struct fscrypt_name {
+ struct qstr disk_name;
+};
+
+#define fname_name(p) ((p)->disk_name.name)
+#define fname_len(p) ((p)->disk_name.len)
+
+#define t16(x) ({ \
+ uint16_t __b = (x); \
+ (__LITTLE_ENDIAN==__BYTE_ORDER) ? __b : bswap_16(__b); \
+})
+
+#define t32(x) ({ \
+ uint32_t __b = (x); \
+ (__LITTLE_ENDIAN==__BYTE_ORDER) ? __b : bswap_32(__b); \
+})
+
+#define t64(x) ({ \
+ uint64_t __b = (x); \
+ (__LITTLE_ENDIAN==__BYTE_ORDER) ? __b : bswap_64(__b); \
+})
+
+#define cpu_to_le16(x) ((__le16){t16(x)})
+#define cpu_to_le32(x) ((__le32){t32(x)})
+#define cpu_to_le64(x) ((__le64){t64(x)})
+
+#define le16_to_cpu(x) (t16((x)))
+#define le32_to_cpu(x) (t32((x)))
+#define le64_to_cpu(x) (t64((x)))
+
+#define check_mul_overflow(a, b, d) ({ \
+ typeof(a) __a = (a); \
+ typeof(b) __b = (b); \
+ typeof(d) __d = (d); \
+ (void) (&__a == &__b); \
+ (void) (&__a == __d); \
+ __builtin_mul_overflow(__a, __b, __d); \
+})
+
+static inline __must_check size_t array_size(size_t a, size_t b)
+{
+ size_t bytes;
+ if (check_mul_overflow(a, b, &bytes))
+ return SIZE_MAX;
+
+ return bytes;
+}
+
+static inline int int_log2(unsigned int arg)
+{
+ int l = 0;
+
+ arg >>= 1;
+ while (arg) {
+ l++;
+ arg >>= 1;
+ }
+ return l;
+}
+
+#undef PAGE_SIZE
+#define PAGE_SIZE (getpagesize())
+#undef PAGE_SHIFT
+#define PAGE_SHIFT (int_log2(PAGE_SIZE))
+
+#endif
diff --git a/ubifs-utils/common/sign.c b/ubifs-utils/common/sign.c
index 7530503a..dfbc96bf 100644
--- a/ubifs-utils/common/sign.c
+++ b/ubifs-utils/common/sign.c
@@ -28,7 +28,7 @@
#include <openssl/conf.h>
#include <err.h>

-#include "compiler_attributes.h"
+#include "linux_types.h"
#include "sign.h"
#include "defs.h"
#include "ubifs.h"
diff --git a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c
index 2181595e..c2f5a29d 100644
--- a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c
+++ b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c
@@ -46,6 +46,7 @@
#include <zstd.h>
#endif

+#include "linux_types.h"
#include "defs.h"
#include "crypto.h"
#include "fscrypt.h"
@@ -1207,12 +1208,14 @@ static int add_xattr(struct ubifs_ino_node *host_ino, struct stat *st,
struct ubifs_ino_node *ino;
struct ubifs_dent_node *xent;
struct qstr nm;
+ char *tmp_name;
union ubifs_key xkey, nkey;
int len, ret;

nm.len = strlen(name);
- nm.name = xmalloc(nm.len + 1);
- memcpy(nm.name, name, nm.len + 1);
+ tmp_name = xmalloc(nm.len + 1);
+ memcpy(tmp_name, name, nm.len + 1);
+ nm.name = tmp_name;

host_ino->xattr_cnt++;
host_ino->xattr_size += CALC_DENT_SIZE(nm.len);
@@ -1240,7 +1243,7 @@ static int add_xattr(struct ubifs_ino_node *host_ino, struct stat *st,

xent->inum = cpu_to_le64(inum);

- ret = add_node(&xkey, nm.name, nm.len, xent, len);
+ ret = add_node(&xkey, tmp_name, nm.len, xent, len);
if (ret)
goto out;

--
2.13.6