From 46dcacd1f77e0d70727c3ef601f58f9f6cf0af46 Mon Sep 17 00:00:00 2001 From: deen <deen> Date: Wed, 11 Apr 2007 18:33:27 +0000 Subject: [PATCH] Move random uuid functions to prng.c b=10802 i=adilger i=green --- lustre/ChangeLog | 4 +++ lustre/include/lustre_lib.h | 2 ++ lustre/include/obd_class.h | 1 - lustre/liblustre/llite_lib.c | 2 +- lustre/liblustre/llite_lib.h | 1 - lustre/liblustre/lutil.c | 31 ------------------------ lustre/liblustre/super.c | 2 +- lustre/liblustre/tests/echo_test.c | 4 +-- lustre/llite/llite_lib.c | 4 +-- lustre/llite/super25.c | 2 +- lustre/mds/handler.c | 2 +- lustre/obdclass/class_obd.c | 1 - lustre/obdclass/lustre_handles.c | 3 ++- lustre/obdclass/obd_mount.c | 2 +- lustre/obdclass/prng.c | 39 ++++++++++++++++++++++++++++++ lustre/obdclass/uuid.c | 22 ----------------- 16 files changed, 56 insertions(+), 66 deletions(-) diff --git a/lustre/ChangeLog b/lustre/ChangeLog index bbbba9de67..3a13399d01 100644 --- a/lustre/ChangeLog +++ b/lustre/ChangeLog @@ -31,6 +31,10 @@ Severity : enhancement Bugzilla : 10651 Description: Nanosecond timestamp support. +Severity : enhancement +Bugzilla : 10802 +Description: Move random uuid functions to prng.c. + -------------------------------------------------------------------------------- tbd Cluster File Systems, Inc. <info@clusterfs.com> diff --git a/lustre/include/lustre_lib.h b/lustre/include/lustre_lib.h index 451d16cbeb..648946539c 100644 --- a/lustre/include/lustre_lib.h +++ b/lustre/include/lustre_lib.h @@ -42,6 +42,8 @@ /* prng.c */ unsigned int ll_rand(void); /* returns a random 32-bit integer */ void ll_srand(unsigned int, unsigned int); /* seed the generator */ +void ll_get_random_bytes(void *buf, int size); +void ll_generate_random_uuid(unsigned char uuid_out[16]); /* target.c */ struct ptlrpc_request; diff --git a/lustre/include/obd_class.h b/lustre/include/obd_class.h index dcd0876c1c..6275fb44e8 100644 --- a/lustre/include/obd_class.h +++ b/lustre/include/obd_class.h @@ -1404,7 +1404,6 @@ extern void obd_sysctl_clean (void); /* uuid.c */ typedef __u8 class_uuid_t[16]; -void class_generate_random_uuid(class_uuid_t uuid); void class_uuid_unparse(class_uuid_t in, struct obd_uuid *out); /* lustre_peer.c */ diff --git a/lustre/liblustre/llite_lib.c b/lustre/liblustre/llite_lib.c index 6f0c57037a..d179c10e69 100644 --- a/lustre/liblustre/llite_lib.c +++ b/lustre/liblustre/llite_lib.c @@ -84,7 +84,7 @@ int liblustre_process_log(struct config_llog_instance *cfg, struct obd_connect_data *ocd = NULL; ENTRY; - generate_random_uuid(uuid); + ll_generate_random_uuid(uuid); class_uuid_unparse(uuid, &mgc_uuid); nid = libcfs_str2nid(mgsnid); diff --git a/lustre/liblustre/llite_lib.h b/lustre/liblustre/llite_lib.h index 7e4084a2f2..3df6cad2ee 100644 --- a/lustre/liblustre/llite_lib.h +++ b/lustre/liblustre/llite_lib.h @@ -173,7 +173,6 @@ struct mount_option_s ((unsigned long)(ptr) == 0 || (unsigned long)(ptr) > -1000UL) /* llite_lib.c */ -void generate_random_uuid(unsigned char uuid_out[16]); int liblustre_process_log(struct config_llog_instance *cfg, char *mgsnid, char *profile, int allow_recov); int ll_parse_mount_target(const char *target, char **mgsnid, diff --git a/lustre/liblustre/lutil.c b/lustre/liblustre/lutil.c index db203f3767..250c70e719 100644 --- a/lustre/liblustre/lutil.c +++ b/lustre/liblustre/lutil.c @@ -129,32 +129,6 @@ void liblustre_init_random() ll_srand(tv.tv_sec ^ __swab32(seed[0]), tv.tv_usec ^__swab32(getpid())); } -void get_random_bytes(void *buf, int size) -{ - int *p = buf; - int rem; - LASSERT(size >= 0); - - rem = min((unsigned long)buf & (sizeof(int) - 1), size); - if (rem) { - int val = ll_rand(); - memcpy(buf, &val, rem); - p = buf + rem; - size -= rem; - } - - while (size >= sizeof(int)) { - *p = ll_rand(); - size -= sizeof(int); - p++; - } - buf = p; - if (size) { - int val = ll_rand(); - memcpy(buf, &val, size); - } -} - static void init_capability(int *res) { #ifdef HAVE_LIBCAP @@ -238,11 +212,6 @@ int liblustre_init_current(char *comm) return 0; } -void generate_random_uuid(unsigned char uuid_out[16]) -{ - get_random_bytes(uuid_out, sizeof(uuid_out)); -} - int init_lib_portals() { int rc; diff --git a/lustre/liblustre/super.c b/lustre/liblustre/super.c index cb50b8deb9..8105df743b 100644 --- a/lustre/liblustre/super.c +++ b/lustre/liblustre/super.c @@ -1884,7 +1884,7 @@ llu_fsswop_mount(const char *source, RETURN(-ENOMEM); INIT_LIST_HEAD(&sbi->ll_conn_chain); - generate_random_uuid(uuid); + ll_generate_random_uuid(uuid); class_uuid_unparse(uuid, &sbi->ll_sb_uuid); /* generate a string unique to this super, let's try diff --git a/lustre/liblustre/tests/echo_test.c b/lustre/liblustre/tests/echo_test.c index dcb31a06cd..62a11de2c4 100644 --- a/lustre/liblustre/tests/echo_test.c +++ b/lustre/liblustre/tests/echo_test.c @@ -96,9 +96,9 @@ static int connect_echo_client(void) int err; ENTRY; - generate_random_uuid(osc_uuid); + ll_generate_random_uuid(osc_uuid); class_uuid_unparse(osc_uuid, &osc_uuid_str); - generate_random_uuid(echo_uuid); + ll_generate_random_uuid(echo_uuid); class_uuid_unparse(echo_uuid, &echo_uuid_str); nid = libcfs_str2nid(echo_server_nid); diff --git a/lustre/llite/llite_lib.c b/lustre/llite/llite_lib.c index a4778660c1..b1fb5fca81 100644 --- a/lustre/llite/llite_lib.c +++ b/lustre/llite/llite_lib.c @@ -75,7 +75,7 @@ static struct ll_sb_info *ll_init_sbi(void) INIT_LIST_HEAD(&sbi->ll_conn_chain); INIT_LIST_HEAD(&sbi->ll_orphan_dentry_list); - class_generate_random_uuid(uuid); + ll_generate_random_uuid(uuid); class_uuid_unparse(uuid, &sbi->ll_sb_uuid); CDEBUG(D_HA, "generated uuid: %s\n", sbi->ll_sb_uuid.uuid); @@ -702,7 +702,7 @@ static int old_lustre_process_log(struct super_block *sb, char *newprofile, int i, rc = 0, recov_bk = 1, failnodes = 0; ENTRY; - class_generate_random_uuid(uuid); + ll_generate_random_uuid(uuid); class_uuid_unparse(uuid, &mdc_uuid); CDEBUG(D_HA, "generated uuid: %s\n", mdc_uuid.uuid); diff --git a/lustre/llite/super25.c b/lustre/llite/super25.c index 9fb337ed10..e56d77387d 100644 --- a/lustre/llite/super25.c +++ b/lustre/llite/super25.c @@ -131,7 +131,7 @@ static int __init init_lustre_lite(void) lustre_register_client_fill_super(ll_fill_super); lustre_register_client_process_config(ll_process_config); - get_random_bytes(seed, sizeof(seed)); + ll_get_random_bytes(seed, sizeof(seed)); /* Nodes with small feet have little entropy * the NID for this node gives the most entropy in the low bits */ diff --git a/lustre/mds/handler.c b/lustre/mds/handler.c index 04d0802e96..bf9646b2f9 100644 --- a/lustre/mds/handler.c +++ b/lustre/mds/handler.c @@ -1946,7 +1946,7 @@ static int mds_setup(struct obd_device *obd, obd_count len, void *buf) if (lcfg->lcfg_bufcount >= 4 && LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) { class_uuid_t uuid; - generate_random_uuid(uuid); + ll_generate_random_uuid(uuid); class_uuid_unparse(uuid, &mds->mds_lov_uuid); OBD_ALLOC(mds->mds_profile, LUSTRE_CFG_BUFLEN(lcfg, 3)); diff --git a/lustre/obdclass/class_obd.c b/lustre/obdclass/class_obd.c index ccf80f3d8f..1684e728a0 100644 --- a/lustre/obdclass/class_obd.c +++ b/lustre/obdclass/class_obd.c @@ -412,7 +412,6 @@ EXPORT_SYMBOL(class_disconnect); EXPORT_SYMBOL(class_num2obd); /* uuid.c */ -EXPORT_SYMBOL(class_generate_random_uuid); EXPORT_SYMBOL(class_uuid_unparse); EXPORT_SYMBOL(lustre_uuid_to_peer); diff --git a/lustre/obdclass/lustre_handles.c b/lustre/obdclass/lustre_handles.c index 21d6f50232..9d4d1ecfd7 100644 --- a/lustre/obdclass/lustre_handles.c +++ b/lustre/obdclass/lustre_handles.c @@ -30,6 +30,7 @@ #include <obd_support.h> #include <lustre_handles.h> +#include <lustre_lib.h> spinlock_t handle_lock; static __u64 handle_base; @@ -145,7 +146,7 @@ int class_handle_init(void) bucket--) CFS_INIT_LIST_HEAD(bucket); - get_random_bytes(&handle_base, sizeof(handle_base)); + ll_get_random_bytes(&handle_base, sizeof(handle_base)); LASSERT(handle_base != 0ULL); return 0; diff --git a/lustre/obdclass/obd_mount.c b/lustre/obdclass/obd_mount.c index 30782b78e3..0dd655ea16 100644 --- a/lustre/obdclass/obd_mount.c +++ b/lustre/obdclass/obd_mount.c @@ -640,7 +640,7 @@ static int lustre_start_mgc(struct super_block *sb) /* Random uuid for MGC allows easier reconnects */ OBD_ALLOC_PTR(uuid); - class_generate_random_uuid(uuidc); + ll_generate_random_uuid(uuidc); class_uuid_unparse(uuidc, uuid); /* Start the MGC */ diff --git a/lustre/obdclass/prng.c b/lustre/obdclass/prng.c index b3c2a753e0..bd419082b5 100644 --- a/lustre/obdclass/prng.c +++ b/lustre/obdclass/prng.c @@ -15,8 +15,10 @@ #ifndef __KERNEL__ #include <liblustre.h> +#define get_random_bytes(val, size) (*val) = 0 #endif #include <obd_class.h> +#include <linux/random.h> /* From: George Marsaglia <geo@stat.fsu.edu> @@ -66,3 +68,40 @@ void ll_srand(unsigned int seed1, unsigned int seed2) seed_y = seed2; } EXPORT_SYMBOL(ll_srand); + +void ll_get_random_bytes(void *buf, int size) +{ + int *p = buf; + int rem, tmp; + + LASSERT(size >= 0); + + rem = min((int)((unsigned long)buf & (sizeof(int) - 1)), size); + if (rem) { + get_random_bytes(&tmp, sizeof(tmp)); + tmp ^= ll_rand(); + memcpy(buf, &tmp, rem); + p = buf + rem; + size -= rem; + } + + while (size >= sizeof(int)) { + get_random_bytes(&tmp, sizeof(tmp)); + *p = ll_rand() ^ tmp; + size -= sizeof(int); + p++; + } + buf = p; + if (size) { + get_random_bytes(&tmp, sizeof(tmp)); + tmp ^= ll_rand(); + memcpy(buf, &tmp, size); + } +} +EXPORT_SYMBOL(ll_get_random_bytes); + +void ll_generate_random_uuid(unsigned char uuid_out[16]) +{ + ll_get_random_bytes(uuid_out, sizeof(uuid_out)); +} +EXPORT_SYMBOL(ll_generate_random_uuid); diff --git a/lustre/obdclass/uuid.c b/lustre/obdclass/uuid.c index 09302bdbd3..ad072ff71d 100644 --- a/lustre/obdclass/uuid.c +++ b/lustre/obdclass/uuid.c @@ -125,28 +125,6 @@ int class_uuid_parse(struct obd_uuid in, class_uuid_t uu) void generate_random_uuid(unsigned char uuid_out[16]); -/* We need to have some extra twiddling here because some systems have - * no random state when they start up. */ -void class_generate_random_uuid(class_uuid_t uuid) -{ - struct timeval t; - int *i, j, k; - - LASSERT(sizeof(class_uuid_t) % sizeof(*i) == 0); - - j = jiffies; - do_gettimeofday(&t); - k = t.tv_usec; - - generate_random_uuid(uuid); - - for (i = (int *)uuid; (char *)i < (char *)uuid + sizeof(class_uuid_t); i++) { - *i ^= j ^ k; - j = ((j << 8) & 0xffffff00) | ((j >> 24) & 0x000000ff); - k = ((k >> 8) & 0x00ffffff) | ((k << 24) & 0xff000000); - } -} - void class_uuid_unparse(class_uuid_t uu, struct obd_uuid *out) { struct uuid uuid; -- GitLab