|
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, Version 1.0 only |
|
6 * (the "License"). You may not use this file except in compliance |
|
7 * with the License. |
|
8 * |
|
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
10 * or http://www.opensolaris.org/os/licensing. |
|
11 * See the License for the specific language governing permissions |
|
12 * and limitations under the License. |
|
13 * |
|
14 * When distributing Covered Code, include this CDDL HEADER in each |
|
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
16 * If applicable, add the following below this CDDL HEADER, with the |
|
17 * fields enclosed by brackets "[]" replaced with your own identifying |
|
18 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
19 * |
|
20 * CDDL HEADER END |
|
21 */ |
|
22 /* |
|
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. |
|
24 * Use is subject to license terms. |
|
25 */ |
|
26 |
|
27 #pragma ident "%Z%%M% %I% %E% SMI" |
|
28 |
|
29 /* |
|
30 * Attempt to dynamically link in the ZFS libzfs.so.1 so that we can |
|
31 * see if there are any ZFS zpools on any of the slices. |
|
32 */ |
|
33 |
|
34 #include <stdlib.h> |
|
35 #include <stdio.h> |
|
36 #include <strings.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 |
|
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)(int fd, char **desc, char **name); |
|
55 |
|
56 static mutex_t init_lock = DEFAULTMUTEX; |
|
57 static rwlock_t zpool_lock = DEFAULTRWLOCK; |
|
58 static int initialized = 0; |
|
59 |
|
60 static void *init_zpool(); |
|
61 |
|
62 int |
|
63 inuse_zpool(char *slice, nvlist_t *attrs, int *errp) |
|
64 { |
|
65 int found = 0; |
|
66 char *desc, *name; |
|
67 int fd; |
|
68 |
|
69 *errp = 0; |
|
70 if (slice == NULL) { |
|
71 return (found); |
|
72 } |
|
73 |
|
74 (void) mutex_lock(&init_lock); |
|
75 |
|
76 /* |
|
77 * Dynamically load libzfs |
|
78 */ |
|
79 if (!initialized) { |
|
80 if (!init_zpool()) { |
|
81 (void) mutex_unlock(&init_lock); |
|
82 return (found); |
|
83 } |
|
84 initialized = 1; |
|
85 } |
|
86 (void) mutex_unlock(&init_lock); |
|
87 (void) rw_rdlock(&zpool_lock); |
|
88 if ((fd = open(slice, O_RDONLY)) > 0) { |
|
89 if (zfsdl_zpool_in_use(fd, &desc, &name)) { |
|
90 libdiskmgt_add_str(attrs, DM_USED_BY, |
|
91 DM_USE_ZPOOL, errp); |
|
92 libdiskmgt_add_str(attrs, DM_USED_NAME, |
|
93 name, errp); |
|
94 found = 1; |
|
95 } |
|
96 } |
|
97 (void) rw_unlock(&zpool_lock); |
|
98 |
|
99 return (found); |
|
100 } |
|
101 |
|
102 /* |
|
103 * Try to dynamically link the zfs functions we need. |
|
104 */ |
|
105 static void* |
|
106 init_zpool() |
|
107 { |
|
108 void *lh = NULL; |
|
109 |
|
110 if ((lh = dlopen("libzfs.so", RTLD_NOW)) == NULL) { |
|
111 return (lh); |
|
112 } |
|
113 /* |
|
114 * Instantiate the functions needed to get zpool configuration |
|
115 * data |
|
116 */ |
|
117 if ((zfsdl_zpool_in_use = (int (*)(int, char **, char **))dlsym(lh, |
|
118 "zpool_in_use")) == NULL) { |
|
119 (void) dlclose(lh); |
|
120 return (NULL); |
|
121 } |
|
122 |
|
123 return (lh); |
|
124 } |