xref: /titanic_50/usr/src/lib/libdiskmgt/common/inuse_zpool.c (revision e07d9cb85217949d497b02d7211de8a197d2f2eb)
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 2006 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
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 				}
106 			} else {
107 				found = 1;
108 			}
109 
110 			if (found) {
111 				libdiskmgt_add_str(attrs, DM_USED_BY,
112 				    type, errp);
113 				libdiskmgt_add_str(attrs, DM_USED_NAME,
114 				    name, errp);
115 			}
116 		}
117 		if (name)
118 			free(name);
119 		(void) close(fd);
120 	}
121 	(void) rw_unlock(&zpool_lock);
122 
123 	return (found);
124 }
125 
126 int
127 inuse_active_zpool(char *slice, nvlist_t *attrs, int *errp)
128 {
129 	return (inuse_zpool_common(slice, attrs, errp, DM_USE_ACTIVE_ZPOOL));
130 }
131 
132 int
133 inuse_exported_zpool(char *slice, nvlist_t *attrs, int *errp)
134 {
135 	return (inuse_zpool_common(slice, attrs, errp, DM_USE_EXPORTED_ZPOOL));
136 }
137 
138 /*
139  * Try to dynamically link the zfs functions we need.
140  */
141 static void*
142 init_zpool()
143 {
144 	void	*lh = NULL;
145 
146 	if ((lh = dlopen("libzfs.so", RTLD_NOW)) == NULL) {
147 		return (lh);
148 	}
149 
150 	/*
151 	 * Instantiate the functions needed to get zpool configuration
152 	 * data
153 	 */
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);
161 	}
162 
163 	if ((zfs_hdl = (*zfsdl_libzfs_init)(B_FALSE)) == NULL) {
164 		(void) dlclose(lh);
165 		return (NULL);
166 	}
167 
168 	return (lh);
169 }
170