dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libdiskmgt / common / inuse_zpool.c
blob194852d2ec3dfe8569cee47958fa5caf422c6e8f
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.
27 * Attempt to dynamically link in the ZFS libzfs.so.1 so that we can
28 * see if there are any ZFS zpools on any of the slices.
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <strings.h>
34 #include <unistd.h>
35 #include <sys/param.h>
36 #include <sys/errno.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <thread.h>
41 #include <synch.h>
42 #include <dlfcn.h>
43 #include <link.h>
44 #include <ctype.h>
45 #include <sys/fs/zfs.h>
47 #include <libzfs.h>
48 #include "libdiskmgt.h"
49 #include "disks_private.h"
52 * Pointers to libzfs.so functions that we dynamically resolve.
54 static int (*zfsdl_zpool_in_use)(libzfs_handle_t *hdl, int fd,
55 pool_state_t *state, char **name, boolean_t *);
56 static libzfs_handle_t *(*zfsdl_libzfs_init)(boolean_t);
58 static mutex_t init_lock = DEFAULTMUTEX;
59 static rwlock_t zpool_lock = DEFAULTRWLOCK;
60 static boolean_t initialized;
61 static libzfs_handle_t *zfs_hdl;
63 static void *init_zpool();
65 static int
66 inuse_zpool_common(char *slice, nvlist_t *attrs, int *errp, char *type)
68 int found = 0;
69 char *name;
70 int fd;
71 pool_state_t state;
72 boolean_t used;
74 *errp = 0;
75 if (slice == NULL) {
76 return (found);
79 (void) mutex_lock(&init_lock);
82 * Dynamically load libzfs
84 if (!initialized) {
85 if (!init_zpool()) {
86 (void) mutex_unlock(&init_lock);
87 return (found);
89 initialized = B_TRUE;
91 (void) mutex_unlock(&init_lock);
92 (void) rw_rdlock(&zpool_lock);
93 if ((fd = open(slice, O_RDONLY)) > 0) {
94 name = NULL;
95 if (zfsdl_zpool_in_use(zfs_hdl, fd, &state,
96 &name, &used) == 0 && used) {
97 if (strcmp(type, DM_USE_ACTIVE_ZPOOL) == 0) {
98 if (state == POOL_STATE_ACTIVE) {
99 found = 1;
100 } else if (state == POOL_STATE_SPARE) {
101 found = 1;
102 type = DM_USE_SPARE_ZPOOL;
103 } else if (state == POOL_STATE_L2CACHE) {
104 found = 1;
105 type = DM_USE_L2CACHE_ZPOOL;
107 } else {
108 found = 1;
111 if (found) {
112 libdiskmgt_add_str(attrs, DM_USED_BY,
113 type, errp);
114 libdiskmgt_add_str(attrs, DM_USED_NAME,
115 name, errp);
118 free(name);
119 (void) close(fd);
121 (void) rw_unlock(&zpool_lock);
123 return (found);
127 inuse_active_zpool(char *slice, nvlist_t *attrs, int *errp)
129 return (inuse_zpool_common(slice, attrs, errp, DM_USE_ACTIVE_ZPOOL));
133 inuse_exported_zpool(char *slice, nvlist_t *attrs, int *errp)
135 return (inuse_zpool_common(slice, attrs, errp, DM_USE_EXPORTED_ZPOOL));
139 * Try to dynamically link the zfs functions we need.
141 static void*
142 init_zpool()
144 void *lh = NULL;
146 if ((lh = dlopen("libzfs.so", RTLD_NOW)) == NULL) {
147 return (lh);
151 * Instantiate the functions needed to get zpool configuration
152 * data
154 if ((zfsdl_libzfs_init = (libzfs_handle_t *(*)(boolean_t))
155 dlsym(lh, "libzfs_init")) == NULL ||
156 (zfsdl_zpool_in_use = (int (*)(libzfs_handle_t *, int,
157 pool_state_t *, char **, boolean_t *))
158 dlsym(lh, "zpool_in_use")) == NULL) {
159 (void) dlclose(lh);
160 return (NULL);
163 if ((zfs_hdl = (*zfsdl_libzfs_init)(B_FALSE)) == NULL) {
164 (void) dlclose(lh);
165 return (NULL);
168 return (lh);