Don't cache the system hostid

Historically the SPL cached the system hostid the first time it
was accessed.  This was done to speed up subsequent accesses.
But in practice the system host id is rarely accessed and its
inconvenient that it doesn't promptly detect /etc/hostid
configuration changes.  Therefore, zone_get_hostid() has been
updated to always refresh the system hostid reported.

Reviewed-by: Olaf Faaland <faaland1@llnl.gov>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #626
This commit is contained in:
Brian Behlendorf 2017-07-10 15:24:52 -04:00
parent dfbd813ec7
commit c93d9dff36
2 changed files with 30 additions and 46 deletions

View File

@ -499,80 +499,63 @@ module_param(spl_hostid_path, charp, 0444);
MODULE_PARM_DESC(spl_hostid_path, "The system hostid file (/etc/hostid)"); MODULE_PARM_DESC(spl_hostid_path, "The system hostid file (/etc/hostid)");
static int static int
hostid_read(void) hostid_read(uint32_t *hostid)
{ {
int result;
uint64_t size; uint64_t size;
struct _buf *file; struct _buf *file;
uint32_t hostid = 0; uint32_t value = 0;
int error;
file = kobj_open_file(spl_hostid_path); file = kobj_open_file(spl_hostid_path);
if (file == (struct _buf *)-1) if (file == (struct _buf *)-1)
return -1; return (ENOENT);
result = kobj_get_filesize(file, &size); error = kobj_get_filesize(file, &size);
if (error) {
if (result != 0) {
printk(KERN_WARNING
"SPL: kobj_get_filesize returned %i on %s\n",
result, spl_hostid_path);
kobj_close_file(file); kobj_close_file(file);
return -2; return (error);
} }
if (size < sizeof(HW_HOSTID_MASK)) { if (size < sizeof(HW_HOSTID_MASK)) {
printk(KERN_WARNING
"SPL: Ignoring the %s file because it is %llu bytes; "
"expecting %lu bytes instead.\n", spl_hostid_path,
size, (unsigned long)sizeof(HW_HOSTID_MASK));
kobj_close_file(file); kobj_close_file(file);
return -3; return (EINVAL);
} }
/* Read directly into the variable like eglibc does. */ /*
/* Short reads are okay; native behavior is preserved. */ * Read directly into the variable like eglibc does.
result = kobj_read_file(file, (char *)&hostid, sizeof(hostid), 0); * Short reads are okay; native behavior is preserved.
*/
if (result < 0) { error = kobj_read_file(file, (char *)&value, sizeof(value), 0);
printk(KERN_WARNING if (error < 0) {
"SPL: kobj_read_file returned %i on %s\n",
result, spl_hostid_path);
kobj_close_file(file); kobj_close_file(file);
return -4; return (EIO);
} }
/* Mask down to 32 bits like coreutils does. */ /* Mask down to 32 bits like coreutils does. */
spl_hostid = hostid & HW_HOSTID_MASK; *hostid = (value & HW_HOSTID_MASK);
kobj_close_file(file); kobj_close_file(file);
return 0; return 0;
} }
/*
* Return the system hostid. Preferentially use the spl_hostid module option
* when set, otherwise use the value in the /etc/hostid file.
*/
uint32_t uint32_t
zone_get_hostid(void *zone) zone_get_hostid(void *zone)
{ {
static int first = 1; uint32_t hostid;
/* Only the global zone is supported */ ASSERT3P(zone, ==, NULL);
ASSERT(zone == NULL);
if (first) { if (spl_hostid != 0)
first = 0; return ((uint32_t)(spl_hostid & HW_HOSTID_MASK));
spl_hostid &= HW_HOSTID_MASK; if (hostid_read(&hostid) == 0)
/* return (hostid);
* Get the hostid if it was not passed as a module parameter.
* Try reading the /etc/hostid file directly.
*/
if (spl_hostid == 0 && hostid_read())
spl_hostid = 0;
return (0);
printk(KERN_NOTICE "SPL: using hostid 0x%08x\n",
(unsigned int) spl_hostid);
}
return spl_hostid;
} }
EXPORT_SYMBOL(zone_get_hostid); EXPORT_SYMBOL(zone_get_hostid);

View File

@ -203,7 +203,8 @@ proc_dohostid(struct ctl_table *table, int write,
return (-EINVAL); return (-EINVAL);
} else { } else {
len = snprintf(str, sizeof(str), "%lx", spl_hostid); len = snprintf(str, sizeof(str), "%lx",
(unsigned long) zone_get_hostid(NULL));
if (*ppos >= len) if (*ppos >= len)
rc = 0; rc = 0;
else else