8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libdiskmgt / common / inuse_zpool.c
blobe4f9ab5a3bc91669a45f8c594a7e550112687dca
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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.
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <strings.h>
36 #include <unistd.h>
37 #include <sys/param.h>
38 #include <sys/errno.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <fcntl.h>
42 #include <thread.h>
43 #include <synch.h>
44 #include <dlfcn.h>
45 #include <link.h>
46 #include <ctype.h>
47 #include <sys/fs/zfs.h>
49 #include <libzfs.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();
67 static int
68 inuse_zpool_common(char *slice, nvlist_t *attrs, int *errp, char *type)
70 int found = 0;
71 char *name;
72 int fd;
73 pool_state_t state;
74 boolean_t used;
76 *errp = 0;
77 if (slice == NULL) {
78 return (found);
81 (void) mutex_lock(&init_lock);
84 * Dynamically load libzfs
86 if (!initialized) {
87 if (!init_zpool()) {
88 (void) mutex_unlock(&init_lock);
89 return (found);
91 initialized = B_TRUE;
93 (void) mutex_unlock(&init_lock);
94 (void) rw_rdlock(&zpool_lock);
95 if ((fd = open(slice, O_RDONLY)) > 0) {
96 name = NULL;
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) {
101 found = 1;
102 } else if (state == POOL_STATE_SPARE) {
103 found = 1;
104 type = DM_USE_SPARE_ZPOOL;
105 } else if (state == POOL_STATE_L2CACHE) {
106 found = 1;
107 type = DM_USE_L2CACHE_ZPOOL;
109 } else {
110 found = 1;
113 if (found) {
114 libdiskmgt_add_str(attrs, DM_USED_BY,
115 type, errp);
116 libdiskmgt_add_str(attrs, DM_USED_NAME,
117 name, errp);
120 if (name)
121 free(name);
122 (void) close(fd);
124 (void) rw_unlock(&zpool_lock);
126 return (found);
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.
144 static void*
145 init_zpool()
147 void *lh = NULL;
149 if ((lh = dlopen("libzfs.so", RTLD_NOW)) == NULL) {
150 return (lh);
154 * Instantiate the functions needed to get zpool configuration
155 * data
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) {
162 (void) dlclose(lh);
163 return (NULL);
166 if ((zfs_hdl = (*zfsdl_libzfs_init)(B_FALSE)) == NULL) {
167 (void) dlclose(lh);
168 return (NULL);
171 return (lh);