]> www.infradead.org Git - mtd-utils.git/commitdiff
ubifs-utils: Add linux type definitions
authorZhihao Cheng <chengzhihao1@huawei.com>
Mon, 11 Nov 2024 08:36:42 +0000 (16:36 +0800)
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>
Mon, 11 Nov 2024 09:32:45 +0000 (10:32 +0100)
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@huawei.com>
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
ubifs-utils/Makemodule.am
ubifs-utils/common/crypto.c
ubifs-utils/common/defs.h
ubifs-utils/common/fscrypt.c
ubifs-utils/common/fscrypt.h
ubifs-utils/common/linux_types.h [new file with mode: 0644]
ubifs-utils/common/sign.c
ubifs-utils/mkfs.ubifs/mkfs.ubifs.c

index d58570fe8a21015f43ce1fbe988ae6befaec62b5..0f9c0fa6e8f7fae98a8b08ab6b5b0387a06984e5 100644 (file)
@@ -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 \
index 2ecd8da1b340720ea431ac92c07c24a8d84bdb95..e4ef3491dc1d4fe6a47f9afa708f88753b6d62ca 100644 (file)
@@ -23,6 +23,7 @@
 #include <string.h>
 #include <assert.h>
 
+#include "linux_types.h"
 #include "fscrypt.h"
 #include "defs.h"
 #include "ubifs.h"
index cafc94af862d44c92485989b5064aabfec1a9a3f..dd3b806e3263b891a4010086b5ae15d43cc08a2f 100644 (file)
@@ -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
index 895d5c72f28b1f7c4cc46d548e3a4cd7ea173242..f39faa769004ff0345f40046eea61a4acb7f37c4 100644 (file)
@@ -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;
index b8a599deef285c769d057499044fd136cef67bce..4a073e974f742ad1030510c899e44a8b1b16611b 100644 (file)
@@ -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 (file)
index 0000000..556b2e2
--- /dev/null
@@ -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
index 7530503a54e5d62bd969d28da11732e51c6a6a4e..dfbc96bf1322161ad92a03e103ba5a54f74acf6c 100644 (file)
@@ -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"
index 2181595e3a114db8e82776a93598408270d52dd7..c2f5a29df84831337ea79c55b78a63b9523a7b66 100644 (file)
@@ -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;