From 626bf781b2253c784c55e79e24ee27e3c9c28816 Mon Sep 17 00:00:00 2001 From: James Simmons Date: Wed, 5 Feb 2025 10:13:02 -0500 Subject: [PATCH] LU-9859 libcfs: rework special libcfs string handling The header libcfs_string.h contains NID string specific items so move them to the LNet header lib-types.h. Only functions for string to or from mask handling is left in libcfs_string.c. We can merged that into debug.c where it is first used. Test-Parameters: trivial Change-Id: I79c56594af065dfd08b8205a05326fa0ebf36d55 Signed-off-by: James Simmons Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/57944 Tested-by: jenkins Tested-by: Maloo Reviewed-by: Shaun Tancheff Reviewed-by: Arshad Hussain Reviewed-by: Timothy Day Reviewed-by: Oleg Drokin --- libcfs/include/libcfs/Makefile.am | 3 +- libcfs/include/libcfs/libcfs.h | 1 - libcfs/include/libcfs/libcfs_debug.h | 6 ++ libcfs/include/libcfs/libcfs_string.h | 55 ------------- libcfs/libcfs/Makefile.in | 3 +- libcfs/libcfs/debug.c | 131 ++++++++++++++++++++++++++++- libcfs/libcfs/libcfs_string.c | 151 ---------------------------------- lnet/include/lnet/lib-types.h | 23 ++++++ lnet/lnet/lib-cpt.c | 2 +- lustre/mdd/mdd_lproc.c | 1 - 10 files changed, 162 insertions(+), 214 deletions(-) delete mode 100644 libcfs/include/libcfs/libcfs_string.h delete mode 100644 libcfs/libcfs/libcfs_string.c diff --git a/libcfs/include/libcfs/Makefile.am b/libcfs/include/libcfs/Makefile.am index 1d337cc..cd38c01 100644 --- a/libcfs/include/libcfs/Makefile.am +++ b/libcfs/include/libcfs/Makefile.am @@ -16,5 +16,4 @@ EXTRA_DIST = \ libcfs_debug.h \ libcfs_fail.h \ libcfs_hash.h \ - libcfs_private.h \ - libcfs_string.h + libcfs_private.h diff --git a/libcfs/include/libcfs/libcfs.h b/libcfs/include/libcfs/libcfs.h index e8e1193..7b35c9b 100644 --- a/libcfs/include/libcfs/libcfs.h +++ b/libcfs/include/libcfs/libcfs.h @@ -31,7 +31,6 @@ #include #include #include -#include #include #include diff --git a/libcfs/include/libcfs/libcfs_debug.h b/libcfs/include/libcfs/libcfs_debug.h index 1375dda..f68ebb1 100644 --- a/libcfs/include/libcfs/libcfs_debug.h +++ b/libcfs/include/libcfs/libcfs_debug.h @@ -37,6 +37,12 @@ extern char *libcfs_debug_file_path; struct task_struct; +/* Convert a text string to a bitmask */ +int cfs_str2mask(const char *str, const char *(*bit2str)(int bit), + u64 *oldmask, u64 minmask, u64 allmask, u64 defmask); +int cfs_mask2str(char *str, int size, u64 mask, const char *(*bit2str)(int), + char sep); + int libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys); int libcfs_debug_str2mask(int *mask, const char *str, int is_subsys); void libcfs_debug_dumpstack(struct task_struct *tsk); diff --git a/libcfs/include/libcfs/libcfs_string.h b/libcfs/include/libcfs/libcfs_string.h deleted file mode 100644 index 8f2616f..0000000 --- a/libcfs/include/libcfs/libcfs_string.h +++ /dev/null @@ -1,55 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ - -/* - * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. - * Use is subject to license terms. - * - * Copyright (c) 2012, 2017, Intel Corporation. - */ - -/* - * This file is part of Lustre, http://www.lustre.org/ - * - * Generic string manipulation functions. - * - * Author: Nathan Rutman - */ - -#ifndef __LIBCFS_STRING_H__ -#define __LIBCFS_STRING_H__ - -/* libcfs_string.c */ -/* Convert a text string to a bitmask */ -int cfs_str2mask(const char *str, const char *(*bit2str)(int bit), - u64 *oldmask, u64 minmask, u64 allmask, u64 defmask); -int cfs_mask2str(char *str, int size, u64 mask, const char *(*bit2str)(int), - char sep); - -/* - * Structure to represent \ token of the syntax. - */ -struct cfs_range_expr { - /* - * Link to cfs_expr_list::el_exprs. - */ - struct list_head re_link; - u32 re_lo; - u32 re_hi; - u32 re_stride; -}; - -struct cfs_expr_list { - struct list_head el_link; - struct list_head el_exprs; -}; - -int cfs_expr_list_match(u32 value, struct cfs_expr_list *expr_list); -int cfs_expr_list_values(struct cfs_expr_list *expr_list, - int max, u32 **values); -void cfs_expr_list_free(struct cfs_expr_list *expr_list); -int cfs_expr_list_parse(char *str, int len, unsigned int min, unsigned int max, - struct cfs_expr_list **elpp); -void cfs_expr_list_free_list(struct list_head *list); -#define cfs_expr_list_values_free(values, num) CFS_FREE_PTR_ARRAY(values, num) - -#endif diff --git a/libcfs/libcfs/Makefile.in b/libcfs/libcfs/Makefile.in index 5ad8ba8..fcbf367 100644 --- a/libcfs/libcfs/Makefile.in +++ b/libcfs/libcfs/Makefile.in @@ -21,8 +21,7 @@ default: all libcfs-linux-objs := $(addprefix linux/,$(libcfs-linux-objs)) libcfs-crypto-objs := $(addprefix crypto/,$(libcfs-crypto-objs)) -libcfs-all-objs := debug.o fail.o module.o tracefile.o \ - libcfs_string.o hash.o +libcfs-all-objs := debug.o fail.o module.o tracefile.o hash.o libcfs-objs := $(libcfs-linux-objs) $(libcfs-all-objs) @LLCRYPT_TRUE@libcfs-objs += $(libcfs-crypto-objs) diff --git a/libcfs/libcfs/debug.c b/libcfs/libcfs/debug.c index eca35d7..eaf70b9 100644 --- a/libcfs/libcfs/debug.c +++ b/libcfs/libcfs/debug.c @@ -17,7 +17,6 @@ #include #include -#include #include #include #include @@ -267,6 +266,136 @@ static const char *libcfs_debug_dbg2str(int debug) return libcfs_debug_masks[debug]; } +/* convert a binary mask to a string of bit names */ +int cfs_mask2str(char *str, int size, u64 mask, const char *(*bit2str)(int bit), + char sep) +{ + const char *token; + int len = 0; + int i; + + if (mask == 0) { /* "0" */ + if (size > 0) + str[0] = '0'; + len = 1; + } else { /* space-separated tokens */ + for (i = 0; i < 64; i++) { + if ((mask & BIT(i)) == 0) + continue; + + token = bit2str(i); + if (!token) /* unused bit */ + continue; + + if (len > 0) { /* separator? */ + if (len < size) + str[len] = sep; + len++; + } + + while (*token != 0) { + if (len < size) + str[len] = *token; + token++; + len++; + } + } + } + + /* terminate 'str' */ + if (len < size) + str[len++] = '\n'; + if (len < size) + str[len] = '\0'; + else + str[size - 1] = '\0'; + + return len; +} +EXPORT_SYMBOL(cfs_mask2str); + +/* Convert a text string to a bitmask */ +int cfs_str2mask(const char *str, const char *(*bit2str)(int bit), + u64 *oldmask, u64 minmask, u64 allmask, u64 defmask) +{ + const char *debugstr; + u64 newmask = *oldmask, found = 0; + + ENTRY; + /* must be a list of tokens separated by whitespace or comma, + * and optionally an operator ('+' or '-'). If an operator + * appears first in , '*oldmask' is used as the starting point + * (relative), otherwise minmask is used (absolute). An operator + * applies to all following tokens up to the next operator. + */ + while (*str != 0) { + int i, len; + char op = 0; + + while (isspace(*str) || *str == ',') + str++; + if (*str == 0) + break; + if (*str == '+' || *str == '-') { + op = *str++; + while (isspace(*str)) + str++; + if (*str == 0) /* trailing op */ + return -EINVAL; + } else if (!found) + newmask = minmask; + + + /* find token length */ + for (len = 0; str[len] != 0 && !isspace(str[len]) && + str[len] != '+' && str[len] != '-' && str[len] != ','; + len++); + + /* match token */ + found = 0; + for (i = 0; i < 32; i++) { + debugstr = bit2str(i); + if (debugstr != NULL && + strlen(debugstr) == len && + strncasecmp(str, debugstr, len) == 0) { + if (op == '-') + newmask &= ~BIT(i); + else + newmask |= BIT(i); + found = 1; + break; + } + } + if (!found && len == 3 && + (strncasecmp(str, "ALL", len) == 0)) { + if (op == '-') + newmask = minmask; + else + newmask = allmask; + found = 1; + } + if (!found && strcasecmp(str, "DEFAULT") == 0) { + if (op == '-') + newmask = (newmask & ~defmask) | minmask; + else if (op == '+') + newmask |= defmask; + else + newmask = defmask; + found = 1; + } + if (!found) { + CWARN("unknown mask '%.*s'.\n" + "mask usage: [+|-] ...\n", len, str); + return -EINVAL; + } + str += len; + } + + *oldmask = newmask; + return 0; +} +EXPORT_SYMBOL(cfs_str2mask); + int libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys) { const char *(*bit2str)(int bit) = is_subsys ? libcfs_debug_subsys2str : diff --git a/libcfs/libcfs/libcfs_string.c b/libcfs/libcfs/libcfs_string.c deleted file mode 100644 index f144062..0000000 --- a/libcfs/libcfs/libcfs_string.c +++ /dev/null @@ -1,151 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 - -/* - * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. - * Use is subject to license terms. - * - * Copyright (c) 2012, 2017, Intel Corporation. - */ - -/* - * This file is part of Lustre, http://www.lustre.org/ - * - * String manipulation functions. - * - * Author: Nathan Rutman - */ - -#include -#include -#include - - -/* convert a binary mask to a string of bit names */ -int cfs_mask2str(char *str, int size, u64 mask, const char *(*bit2str)(int bit), - char sep) -{ - int len = 0; - const char *token; - int i; - - if (mask == 0) { /* "0" */ - if (size > 0) - str[0] = '0'; - len = 1; - } else { /* space-separated tokens */ - for (i = 0; i < 64; i++) { - if ((mask & BIT(i)) == 0) - continue; - - token = bit2str(i); - if (!token) /* unused bit */ - continue; - - if (len > 0) { /* separator? */ - if (len < size) - str[len] = sep; - len++; - } - - while (*token != 0) { - if (len < size) - str[len] = *token; - token++; - len++; - } - } - } - - /* terminate 'str' */ - if (len < size) - str[len++] = '\n'; - if (len < size) - str[len] = '\0'; - else - str[size - 1] = '\0'; - - return len; -} -EXPORT_SYMBOL(cfs_mask2str); - -/* Convert a text string to a bitmask */ -int cfs_str2mask(const char *str, const char *(*bit2str)(int bit), - u64 *oldmask, u64 minmask, u64 allmask, u64 defmask) -{ - const char *debugstr; - u64 newmask = *oldmask, found = 0; - - ENTRY; - /* must be a list of tokens separated by whitespace or comma, - * and optionally an operator ('+' or '-'). If an operator - * appears first in , '*oldmask' is used as the starting point - * (relative), otherwise minmask is used (absolute). An operator - * applies to all following tokens up to the next operator. - */ - while (*str != 0) { - int i, len; - char op = 0; - - while (isspace(*str) || *str == ',') - str++; - if (*str == 0) - break; - if (*str == '+' || *str == '-') { - op = *str++; - while (isspace(*str)) - str++; - if (*str == 0) /* trailing op */ - return -EINVAL; - } else if (!found) - newmask = minmask; - - - /* find token length */ - for (len = 0; str[len] != 0 && !isspace(str[len]) && - str[len] != '+' && str[len] != '-' && str[len] != ','; - len++); - - /* match token */ - found = 0; - for (i = 0; i < 32; i++) { - debugstr = bit2str(i); - if (debugstr != NULL && - strlen(debugstr) == len && - strncasecmp(str, debugstr, len) == 0) { - if (op == '-') - newmask &= ~BIT(i); - else - newmask |= BIT(i); - found = 1; - break; - } - } - if (!found && len == 3 && - (strncasecmp(str, "ALL", len) == 0)) { - if (op == '-') - newmask = minmask; - else - newmask = allmask; - found = 1; - } - if (!found && strcasecmp(str, "DEFAULT") == 0) { - if (op == '-') - newmask = (newmask & ~defmask) | minmask; - else if (op == '+') - newmask |= defmask; - else - newmask = defmask; - found = 1; - } - if (!found) { - CWARN("unknown mask '%.*s'.\n" - "mask usage: [+|-] ...\n", len, str); - return -EINVAL; - } - str += len; - } - - *oldmask = newmask; - return 0; -} -EXPORT_SYMBOL(cfs_str2mask); diff --git a/lnet/include/lnet/lib-types.h b/lnet/include/lnet/lib-types.h index c3c8124..63711fa 100644 --- a/lnet/include/lnet/lib-types.h +++ b/lnet/include/lnet/lib-types.h @@ -37,6 +37,29 @@ int cfs_match_nid_net(struct lnet_nid *nid, u32 net, struct list_head *net_num_list, struct list_head *addr); +/* Structure to represent \ token of the syntax. */ +struct cfs_range_expr { + /* Link to cfs_expr_list::el_exprs. */ + struct list_head re_link; + u32 re_lo; + u32 re_hi; + u32 re_stride; +}; + +struct cfs_expr_list { + struct list_head el_link; + struct list_head el_exprs; +}; + +int cfs_expr_list_match(u32 value, struct cfs_expr_list *expr_list); +int cfs_expr_list_values(struct cfs_expr_list *expr_list, + int max, u32 **values); +void cfs_expr_list_free(struct cfs_expr_list *expr_list); +int cfs_expr_list_parse(char *str, int len, unsigned int min, unsigned int max, + struct cfs_expr_list **elpp); +void cfs_expr_list_free_list(struct list_head *list); +#define cfs_expr_list_values_free(values, num) CFS_FREE_PTR_ARRAY(values, num) + /* Max payload size */ #define LNET_MAX_PAYLOAD LNET_MTU diff --git a/lnet/lnet/lib-cpt.c b/lnet/lnet/lib-cpt.c index 71c2b42..4f5475a 100644 --- a/lnet/lnet/lib-cpt.c +++ b/lnet/lnet/lib-cpt.c @@ -18,8 +18,8 @@ #include #include -#include #include +#include #include /** virtual processing unit */ diff --git a/lustre/mdd/mdd_lproc.c b/lustre/mdd/mdd_lproc.c index 047c390..5b72f04 100644 --- a/lustre/mdd/mdd_lproc.c +++ b/lustre/mdd/mdd_lproc.c @@ -21,7 +21,6 @@ #include #include #include -#include #include "mdd_internal.h" static ssize_t uuid_show(struct kobject *kobj, struct attribute *attr, -- 1.8.3.1