xref: /titanic_44/usr/src/lib/libdiskmgt/common/inuse_zpool.c (revision 164c0dd6f561db19bdaf1d0b7f2a8dec44355b69)
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 "libdiskmgt.h"
50 #include "disks_private.h"
51 
52 /*
53  * Pointers to libzfs.so functions that we dynamically resolve.
54  */
55 static	int	(*zfsdl_zpool_in_use)(int fd, pool_state_t *state, char **name);
56 
57 static mutex_t			init_lock = DEFAULTMUTEX;
58 static rwlock_t			zpool_lock = DEFAULTRWLOCK;
59 static	int			initialized = 0;
60 
61 static void	*init_zpool();
62 
63 static int
64 inuse_zpool_common(char *slice, nvlist_t *attrs, int *errp, char *type)
65 {
66 	int		found = 0;
67 	char		*name;
68 	int		fd;
69 	pool_state_t	state;
70 
71 	*errp = 0;
72 	if (slice == NULL) {
73 	    return (found);
74 	}
75 
76 	(void) mutex_lock(&init_lock);
77 
78 	/*
79 	 * Dynamically load libzfs
80 	 */
81 	if (!initialized) {
82 		if (!init_zpool()) {
83 			(void) mutex_unlock(&init_lock);
84 			return (found);
85 		}
86 		initialized = 1;
87 	}
88 	(void) mutex_unlock(&init_lock);
89 	(void) rw_rdlock(&zpool_lock);
90 	if ((fd = open(slice, O_RDONLY)) > 0) {
91 		if (zfsdl_zpool_in_use(fd, &state, &name)) {
92 			if (strcmp(type, DM_USE_ACTIVE_ZPOOL) == 0) {
93 				if (state == POOL_STATE_ACTIVE)
94 					found = 1;
95 			} else {
96 				found = 1;
97 			}
98 
99 			if (found) {
100 				libdiskmgt_add_str(attrs, DM_USED_BY,
101 				    type, errp);
102 				libdiskmgt_add_str(attrs, DM_USED_NAME,
103 					name, errp);
104 			}
105 		}
106 		(void) close(fd);
107 	}
108 	(void) rw_unlock(&zpool_lock);
109 
110 	return (found);
111 }
112 
113 int
114 inuse_active_zpool(char *slice, nvlist_t *attrs, int *errp)
115 {
116 	return (inuse_zpool_common(slice, attrs, errp, DM_USE_ACTIVE_ZPOOL));
117 }
118 
119 int
120 inuse_exported_zpool(char *slice, nvlist_t *attrs, int *errp)
121 {
122 	return (inuse_zpool_common(slice, attrs, errp, DM_USE_EXPORTED_ZPOOL));
123 }
124 
125 /*
126  * Try to dynamically link the zfs functions we need.
127  */
128 static void*
129 init_zpool()
130 {
131 	void	*lh = NULL;
132 
133 	if ((lh = dlopen("libzfs.so", RTLD_NOW)) == NULL) {
134 		return (lh);
135 	}
136 	/*
137 	 * Instantiate the functions needed to get zpool configuration
138 	 * data
139 	 */
140 	if ((zfsdl_zpool_in_use = (int (*)(int, pool_state_t *, char **))
141 	    dlsym(lh, "zpool_in_use")) == NULL) {
142 		(void) dlclose(lh);
143 		return (NULL);
144 	}
145 
146 	return (lh);
147 }
148