From: Simon Glass <simon.glass@canonical.com> The ext4l code defines string helper functions locally instead of in the standard Linux compatibility headers. Move these functions to their proper locations: - strtomem_pad(): macro added to linux/string.h - strscpy_pad(): macro added to linux/string.h - strreplace(): declaration in linux/string.h, implementation in lib/string.c - kmemdup_nul(): declaration in linux/slab.h (alongside kmemdup()), implementation in lib/string.c This makes these functions available to other parts of U-Boot and reduces duplication in ext4l. Co-developed-by: Claude Opus 4.5 <noreply@anthropic.com> Signed-off-by: Simon Glass <simon.glass@canonical.com> --- fs/ext4l/ext4_uboot.h | 20 ++++---------------- fs/ext4l/stub.c | 32 ++++---------------------------- include/linux/slab.h | 13 +++++++++++++ include/linux/string.h | 41 +++++++++++++++++++++++++++++++++++++++++ lib/string.c | 25 +++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 44 deletions(-) diff --git a/fs/ext4l/ext4_uboot.h b/fs/ext4l/ext4_uboot.h index ce40888879e..7b90276b2bc 100644 --- a/fs/ext4l/ext4_uboot.h +++ b/fs/ext4l/ext4_uboot.h @@ -934,16 +934,8 @@ struct dx_hash_info { /* pr_warn_once is in linux/printk.h */ /* lockdep_assert_held_read is in linux/lockdep.h */ - -/* strtomem_pad - copy string to fixed-size buffer with padding */ -#define strtomem_pad(dest, src, pad) do { \ - size_t _len = strlen(src); \ - if (_len >= sizeof(dest)) \ - _len = sizeof(dest); \ - memcpy(dest, src, _len); \ - if (_len < sizeof(dest)) \ - memset((char *)(dest) + _len, (pad), sizeof(dest) - _len); \ -} while (0) +/* strtomem_pad is in linux/string.h */ +/* strscpy_pad is in linux/string.h */ /* Memory weight - count set bits */ static inline unsigned long memweight(const void *ptr, size_t bytes) @@ -2075,10 +2067,7 @@ struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid, /* I/O priority - declaration for stub.c */ int IOPRIO_PRIO_VALUE(int class, int data); -/* String operations */ -char *kmemdup_nul(const char *s, size_t len, gfp_t gfp); -#define strscpy_pad(dst, src) strncpy(dst, src, sizeof(dst)) - +/* kmemdup_nul is in linux/slab.h */ /* fscrypt declarations are in ext4_fscrypt.h */ /* Memory allocation - declarations for stub.c */ @@ -2191,8 +2180,7 @@ struct mb_cache_entry { void generic_set_sb_d_ops(struct super_block *sb); struct dentry *d_make_root(struct inode *inode); -/* String operations - declarations for stub.c */ -char *strreplace(const char *str, char old, char new); +/* strreplace is in linux/string.h */ /* Ratelimit - declaration for stub.c */ void ratelimit_state_init(void *rs, int interval, int burst); diff --git a/fs/ext4l/stub.c b/fs/ext4l/stub.c index a0c21dfd14c..c14132815cd 100644 --- a/fs/ext4l/stub.c +++ b/fs/ext4l/stub.c @@ -373,32 +373,8 @@ int ext4_update_overhead(struct super_block *sb, bool force) return 0; } -/* String stubs */ -/* strtomem_pad is now a macro in ext4_uboot.h */ - -char *strreplace(const char *str, char old, char new) -{ - char *s = (char *)str; - - while (*s) { - if (*s == old) - *s = new; - s++; - } - return (char *)str; -} - -char *kmemdup_nul(const char *s, size_t len, gfp_t gfp) -{ - char *buf; - - buf = kmalloc(len + 1, gfp); - if (buf) { - memcpy(buf, s, len); - buf[len] = '\0'; - } - return buf; -} +/* strtomem_pad, strscpy_pad, strreplace are now in linux/string.h */ +/* kmemdup_nul is now in linux/slab.h, with implementation in lib/string.c */ /* Page allocation */ unsigned long get_zeroed_page(gfp_t gfp) @@ -506,8 +482,8 @@ int sb_set_blocksize(struct super_block *sb, int size) return size; } -/* strscpy_pad is now a macro in ext4_uboot.h */ -/* kmemdup_nul is defined earlier in this file */ +/* strscpy_pad is now a macro in linux/string.h */ +/* kmemdup_nul is now in lib/string.c */ /* Address check */ int generic_check_addressable(unsigned int blocksize_bits, u64 num_blocks) diff --git a/include/linux/slab.h b/include/linux/slab.h index f0c0add0cbd..4f413f93fa3 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -87,6 +87,19 @@ static inline void *krealloc(const void *p, size_t new_size, gfp_t flags) void *kmemdup(const void *src, size_t len, gfp_t gfp); +/** + * kmemdup_nul - Duplicate a string with null termination + * @s: Source string + * @len: Maximum length to copy + * @gfp: GFP flags for allocation + * + * Allocates len + 1 bytes, copies up to @len bytes from @s, and + * ensures the result is null-terminated. + * + * Return: pointer to new string, or NULL on allocation failure + */ +char *kmemdup_nul(const char *s, size_t len, gfp_t gfp); + /* kmem_cache stubs */ struct kmem_cache { int sz; diff --git a/include/linux/string.h b/include/linux/string.h index d943fcce690..591d99c46a1 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -147,6 +147,47 @@ char *memdup(const void *src, size_t len); unsigned long ustrtoul(const char *cp, char **endp, unsigned int base); unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base); +/** + * strreplace() - Replace all occurrences of a character in a string + * @str: The string to operate on + * @old: The character being replaced + * @new: The character @old is replaced with + * + * Replaces all occurrences of character @old with character @new in + * the string @str in place. + * + * Return: pointer to the string @str itself + */ +char *strreplace(char *str, char old, char new); + +/** + * strtomem_pad - Copy string to fixed-size buffer with padding + * @dest: Destination buffer (must be an array, not a pointer) + * @src: Source string + * @pad: Padding character to fill remaining space + * + * Copy @src to @dest, truncating if necessary. If @src is shorter + * than @dest, fill the remaining bytes with @pad. + */ +#define strtomem_pad(dest, src, pad) do { \ + size_t _len = strlen(src); \ + if (_len >= sizeof(dest)) \ + _len = sizeof(dest); \ + memcpy(dest, src, _len); \ + if (_len < sizeof(dest)) \ + memset((char *)(dest) + _len, (pad), \ + sizeof(dest) - _len); \ +} while (0) + +/** + * strscpy_pad - Copy string to fixed-size buffer with padding + * @dest: Destination buffer (must be an array) + * @src: Source string + * + * Copy @src to @dest ensuring null termination and zero-padding. + */ +#define strscpy_pad(dest, src) strncpy(dest, src, sizeof(dest)) + #ifdef __cplusplus } #endif diff --git a/lib/string.c b/lib/string.c index d56f88d4a84..e297eb99df1 100644 --- a/lib/string.c +++ b/lib/string.c @@ -20,6 +20,7 @@ #include <limits.h> #include <linux/compiler.h> #include <linux/ctype.h> +#include <linux/slab.h> #include <linux/string.h> #include <linux/types.h> #include <malloc.h> @@ -802,3 +803,27 @@ void *memchr_inv(const void *start, int c, size_t bytes) return check_bytes8(start, value, bytes % 8); } #endif + +char *strreplace(char *str, char old, char new) +{ + char *s = str; + + while (*s) { + if (*s == old) + *s = new; + s++; + } + return str; +} + +char *kmemdup_nul(const char *s, size_t len, gfp_t gfp) +{ + char *buf; + + buf = kmalloc(len + 1, gfp); + if (buf) { + memcpy(buf, s, len); + buf[len] = '\0'; + } + return buf; +} -- 2.43.0