Skip to content

Commit

Permalink
Determine the hostid on demand.
Browse files Browse the repository at this point in the history
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 <[email protected]>
Issue openzfs/zfs#851
  • Loading branch information
dechamps authored and behlendorf committed Jul 26, 2012
1 parent c167aad commit a9f2397
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions module/spl/spl-generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down

0 comments on commit a9f2397

Please sign in to comment.