xref: /illumos-gate/usr/src/lib/libdiskmgt/common/inuse_zpool.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
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.
29  */
30 
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>
46 
47 #include <libzfs.h>
48 #include "libdiskmgt.h"
49 #include "disks_private.h"
50 
51 /*
52  * Pointers to libzfs.so functions that we dynamically resolve.
53  */
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);
57 
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;
62 
63 static void	*init_zpool();
64 
65 static int
66 inuse_zpool_common(char *slice, nvlist_t *attrs, int *errp, char *type)
67 {
68 	int		found = 0;
69 	char		*name;
70 	int		fd;
71 	pool_state_t	state;
72 	boolean_t	used;
73 
74 	*errp = 0;
75 	if (slice == NULL) {
76 	    return (found);
77 	}
78 
79 	(void) mutex_lock(&init_lock);
80 
81 	/*
82 	 * Dynamically load libzfs
83 	 */
84 	if (!initialized) {
85 		if (!init_zpool()) {
86 			(void) mutex_unlock(&init_lock);
87 			return (found);
88 		}
89 		initialized = B_TRUE;
90 	}
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;
106 				}
107 			} else {
108 				found = 1;
109 			}
110 
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);
116 			}
117 		}
118 		if (name)
119 			free(name);
120 		(void) close(fd);
121 	}
122 	(void) rw_unlock(&zpool_lock);
123 
124 	return (found);
125 }
126 
127 int
128 inuse_active_zpool(char *slice, nvlist_t *attrs, int *errp)
129 {
130 	return (inuse_zpool_common(slice, attrs, errp, DM_USE_ACTIVE_ZPOOL));
131 }
132 
133 int
134 inuse_exported_zpool(char *slice, nvlist_t *attrs, int *errp)
135 {
136 	return (inuse_zpool_common(slice, attrs, errp, DM_USE_EXPORTED_ZPOOL));
137 }
138 
139 /*
140  * Try to dynamically link the zfs functions we need.
141  */
142 static void*
143 init_zpool()
144 {
145 	void	*lh = NULL;
146 
147 	if ((lh = dlopen("libzfs.so", RTLD_NOW)) == NULL) {
148 		return (lh);
149 	}
150 
151 	/*
152 	 * Instantiate the functions needed to get zpool configuration
153 	 * data
154 	 */
155 	if ((zfsdl_libzfs_init = (libzfs_handle_t *(*)(boolean_t))
156 	    dlsym(lh, "libzfs_init")) == NULL ||
157 	    (zfsdl_zpool_in_use = (int (*)(libzfs_handle_t *, int,
158 	    pool_state_t *, char **, boolean_t *))
159 	    dlsym(lh, "zpool_in_use")) == NULL) {
160 		(void) dlclose(lh);
161 		return (NULL);
162 	}
163 
164 	if ((zfs_hdl = (*zfsdl_libzfs_init)(B_FALSE)) == NULL) {
165 		(void) dlclose(lh);
166 		return (NULL);
167 	}
168 
169 	return (lh);
170 }
171