xref: /titanic_41/usr/src/lib/libdiskmgt/common/inuse_zpool.c (revision fa94a07fd0519b8abfd871ad8fe60e6bebe1e2bb)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
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.
31  */
32 
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>
48 
49 #include <libzfs.h>
50 #include "libdiskmgt.h"
51 #include "disks_private.h"
52 
53 /*
54  * Pointers to libzfs.so functions that we dynamically resolve.
55  */
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);
59 
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;
64 
65 static void	*init_zpool();
66 
67 static int
inuse_zpool_common(char * slice,nvlist_t * attrs,int * errp,char * type)68 inuse_zpool_common(char *slice, nvlist_t *attrs, int *errp, char *type)
69 {
70 	int		found = 0;
71 	char		*name;
72 	int		fd;
73 	pool_state_t	state;
74 	boolean_t	used;
75 
76 	*errp = 0;
77 	if (slice == NULL) {
78 	    return (found);
79 	}
80 
81 	(void) mutex_lock(&init_lock);
82 
83 	/*
84 	 * Dynamically load libzfs
85 	 */
86 	if (!initialized) {
87 		if (!init_zpool()) {
88 			(void) mutex_unlock(&init_lock);
89 			return (found);
90 		}
91 		initialized = B_TRUE;
92 	}
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;
108 				}
109 			} else {
110 				found = 1;
111 			}
112 
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);
118 			}
119 		}
120 		if (name)
121 			free(name);
122 		(void) close(fd);
123 	}
124 	(void) rw_unlock(&zpool_lock);
125 
126 	return (found);
127 }
128 
129 int
inuse_active_zpool(char * slice,nvlist_t * attrs,int * errp)130 inuse_active_zpool(char *slice, nvlist_t *attrs, int *errp)
131 {
132 	return (inuse_zpool_common(slice, attrs, errp, DM_USE_ACTIVE_ZPOOL));
133 }
134 
135 int
inuse_exported_zpool(char * slice,nvlist_t * attrs,int * errp)136 inuse_exported_zpool(char *slice, nvlist_t *attrs, int *errp)
137 {
138 	return (inuse_zpool_common(slice, attrs, errp, DM_USE_EXPORTED_ZPOOL));
139 }
140 
141 /*
142  * Try to dynamically link the zfs functions we need.
143  */
144 static void*
init_zpool()145 init_zpool()
146 {
147 	void	*lh = NULL;
148 
149 	if ((lh = dlopen("libzfs.so", RTLD_NOW)) == NULL) {
150 		return (lh);
151 	}
152 
153 	/*
154 	 * Instantiate the functions needed to get zpool configuration
155 	 * data
156 	 */
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);
164 	}
165 
166 	if ((zfs_hdl = (*zfsdl_libzfs_init)(B_FALSE)) == NULL) {
167 		(void) dlclose(lh);
168 		return (NULL);
169 	}
170 
171 	return (lh);
172 }
173