# HG changeset patch # User Alek Pinchuk # Date 1375745496 25200 # Node ID aba21162d62bbc2d5f61baa9126563604d017b6b # Parent 1e57939bc87d8ba6c14849822e558dc514f0d6e8 3944 libtopo zfs module will clobber libzfs handle when an instance of the module unloads Reviewed by: Hans Rosenfeld Reviewed by: Boris Protopopov Reviewed by: Gary Mills Reviewed by: Joshua M. Clulow Approved by: Dan McDonald diff -r 1e57939bc87d -r aba21162d62b usr/src/lib/fm/topo/libtopo/common/zfs.c --- a/usr/src/lib/fm/topo/libtopo/common/zfs.c Wed Aug 07 12:16:22 2013 -0800 +++ b/usr/src/lib/fm/topo/libtopo/common/zfs.c Mon Aug 05 16:31:36 2013 -0700 @@ -22,6 +22,7 @@ /* * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. + * Copyright 2013 Nexenta Systems, Inc. All rights reserved. */ #include @@ -41,6 +42,7 @@ #include #include #include +#include static int zfs_enum(topo_mod_t *, tnode_t *, const char *, topo_instance_t, topo_instance_t, void *, void *); @@ -59,7 +61,9 @@ static const topo_modinfo_t zfs_info = { ZFS, FM_FMRI_SCHEME_ZFS, ZFS_VERSION, &zfs_ops }; -static libzfs_handle_t *g_zfs; +static libzfs_handle_t *g_zfs = NULL; +static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER; +static int g_refcount = 0; int zfs_init(topo_mod_t *mod, topo_version_t version) @@ -80,8 +84,18 @@ "%s\n", topo_mod_errmsg(mod)); return (-1); /* mod errno already set */ } - if (!g_zfs) - g_zfs = libzfs_init(); + + (void) pthread_mutex_lock(&g_lock); + if (g_refcount == 0) { + if ((g_zfs = libzfs_init()) == NULL) { + (void) pthread_mutex_unlock(&g_lock); + topo_mod_dprintf(mod, "libzfs_init() failed"); + topo_mod_unregister(mod); + return (topo_mod_seterrno(mod, EMOD_UNKNOWN)); + } + } + g_refcount++; + (void) pthread_mutex_unlock(&g_lock); return (0); } @@ -89,10 +103,13 @@ void zfs_fini(topo_mod_t *mod) { - if (g_zfs) { + (void) pthread_mutex_lock(&g_lock); + g_refcount--; + if (g_refcount == 0) { libzfs_fini(g_zfs); g_zfs = NULL; } + (void) pthread_mutex_unlock(&g_lock); topo_mod_unregister(mod); } @@ -155,7 +172,7 @@ cb.cb_guid = pool_guid; cb.cb_pool = NULL; - if (g_zfs != NULL && zpool_iter(g_zfs, find_pool, &cb) == 1) { + if (zpool_iter(g_zfs, find_pool, &cb) == 1) { name = zpool_get_name(cb.cb_pool); } else { (void) snprintf(guidbuf, sizeof (guidbuf), "%llx", pool_guid);