From a9f2397ee9f8c1e2c18072361f1f819155242ed2 Mon Sep 17 00:00:00 2001 From: Etienne Dechamps Date: Thu, 5 Jul 2012 09:22:03 +0200 Subject: [PATCH] Determine the hostid on demand. Currently, the SPL tries to determine the hostid at module load. The hostid is usually determined by running the userland program "hostid" during module initialization. Unfortunately, when the module initializes, it may be way too soon to be able to run any userland programs. This is especially true when the module is compiled directly inside the kernel (built-in); in that case, the SPL would try to run hostid when the kernel is still initializing, which of course is doomed to fail. This patch fixes the issue by deferring hostid generation until something actually needs the hostid (that is, when zone_get_hostid() is called), thus switching to a "on-initialization" model to a "on-demand" (lazy loading) model. ZFS only needs the hostid when some pool operations are requested, and this always happens way after the kernel has finished initialization, thus solving the problem. Signed-off-by: Brian Behlendorf Issue zfsonlinux/zfs#851 --- module/spl/spl-generic.c | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/module/spl/spl-generic.c b/module/spl/spl-generic.c index 13f426733..cc2766452 100644 --- a/module/spl/spl-generic.c +++ b/module/spl/spl-generic.c @@ -546,11 +546,29 @@ hostid_exec(void) uint32_t zone_get_hostid(void *zone) { + static int first = 1; unsigned long hostid; + int rc; /* Only the global zone is supported */ ASSERT(zone == NULL); + if (first) { + first = 0; + + /* + * Get the hostid if it was not passed as a module parameter. + * Try reading the /etc/hostid file directly, and then fall + * back to calling the /usr/bin/hostid utility. + */ + if ((spl_hostid == HW_INVALID_HOSTID) && + (rc = hostid_read()) && (rc = hostid_exec())) + return HW_INVALID_HOSTID; + + printk(KERN_NOTICE "SPL: using hostid 0x%08x\n", + (unsigned int) spl_hostid); + } + if (ddi_strtoul(hw_serial, NULL, HW_HOSTID_LEN-1, &hostid) != 0) return HW_INVALID_HOSTID; @@ -632,16 +650,6 @@ __init spl_init(void) if ((rc = spl_zlib_init())) SGOTO(out9, rc); - /* - * Get the hostid if it was not passed as a module parameter. Try - * reading the /etc/hostid file directly, and then fall back to calling - * the /usr/bin/hostid utility. - */ - - if (spl_hostid == HW_INVALID_HOSTID - && (rc = hostid_read()) && (rc = hostid_exec())) - SGOTO(out10, rc = -EADDRNOTAVAIL); - #ifndef HAVE_KALLSYMS_LOOKUP_NAME if ((rc = set_kallsyms_lookup_name())) SGOTO(out10, rc = -EADDRNOTAVAIL); @@ -653,9 +661,8 @@ __init spl_init(void) if ((rc = spl_vn_init_kallsyms_lookup())) SGOTO(out10, rc); - printk(KERN_NOTICE "SPL: Loaded module v%s-%s%s, using hostid " - "0x%08x\n", SPL_META_VERSION, SPL_META_RELEASE, SPL_DEBUG_STR, - (unsigned int) spl_hostid); + printk(KERN_NOTICE "SPL: Loaded module v%s-%s%s\n", SPL_META_VERSION, + SPL_META_RELEASE, SPL_DEBUG_STR); SRETURN(rc); out10: spl_zlib_fini(); -- 2.39.5