4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * Attempt to dynamically link in the ZFS libzfs.so.1 so that we can
30 * see if there are any ZFS zpools on any of the slices.
37 #include <sys/param.h>
38 #include <sys/errno.h>
39 #include <sys/types.h>
47 #include <sys/fs/zfs.h>
50 #include "libdiskmgt.h"
51 #include "disks_private.h"
54 * Pointers to libzfs.so functions that we dynamically resolve.
56 static int (*zfsdl_zpool_in_use
)(libzfs_handle_t
*hdl
, int fd
,
57 pool_state_t
*state
, char **name
, boolean_t
*);
58 static libzfs_handle_t
*(*zfsdl_libzfs_init
)(boolean_t
);
60 static mutex_t init_lock
= DEFAULTMUTEX
;
61 static rwlock_t zpool_lock
= DEFAULTRWLOCK
;
62 static boolean_t initialized
;
63 static libzfs_handle_t
*zfs_hdl
;
65 static void *init_zpool();
68 inuse_zpool_common(char *slice
, nvlist_t
*attrs
, int *errp
, char *type
)
81 (void) mutex_lock(&init_lock
);
84 * Dynamically load libzfs
88 (void) mutex_unlock(&init_lock
);
93 (void) mutex_unlock(&init_lock
);
94 (void) rw_rdlock(&zpool_lock
);
95 if ((fd
= open(slice
, O_RDONLY
)) > 0) {
97 if (zfsdl_zpool_in_use(zfs_hdl
, fd
, &state
,
98 &name
, &used
) == 0 && used
) {
99 if (strcmp(type
, DM_USE_ACTIVE_ZPOOL
) == 0) {
100 if (state
== POOL_STATE_ACTIVE
) {
102 } else if (state
== POOL_STATE_SPARE
) {
104 type
= DM_USE_SPARE_ZPOOL
;
105 } else if (state
== POOL_STATE_L2CACHE
) {
107 type
= DM_USE_L2CACHE_ZPOOL
;
114 libdiskmgt_add_str(attrs
, DM_USED_BY
,
116 libdiskmgt_add_str(attrs
, DM_USED_NAME
,
124 (void) rw_unlock(&zpool_lock
);
130 inuse_active_zpool(char *slice
, nvlist_t
*attrs
, int *errp
)
132 return (inuse_zpool_common(slice
, attrs
, errp
, DM_USE_ACTIVE_ZPOOL
));
136 inuse_exported_zpool(char *slice
, nvlist_t
*attrs
, int *errp
)
138 return (inuse_zpool_common(slice
, attrs
, errp
, DM_USE_EXPORTED_ZPOOL
));
142 * Try to dynamically link the zfs functions we need.
149 if ((lh
= dlopen("libzfs.so", RTLD_NOW
)) == NULL
) {
154 * Instantiate the functions needed to get zpool configuration
157 if ((zfsdl_libzfs_init
= (libzfs_handle_t
*(*)(boolean_t
))
158 dlsym(lh
, "libzfs_init")) == NULL
||
159 (zfsdl_zpool_in_use
= (int (*)(libzfs_handle_t
*, int,
160 pool_state_t
*, char **, boolean_t
*))
161 dlsym(lh
, "zpool_in_use")) == NULL
) {
166 if ((zfs_hdl
= (*zfsdl_libzfs_init
)(B_FALSE
)) == NULL
) {