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 2006 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 #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 } 107 (void) rw_unlock(&zpool_lock); 108 109 return (found); 110 } 111 112 int 113 inuse_active_zpool(char *slice, nvlist_t *attrs, int *errp) 114 { 115 return (inuse_zpool_common(slice, attrs, errp, DM_USE_ACTIVE_ZPOOL)); 116 } 117 118 int 119 inuse_exported_zpool(char *slice, nvlist_t *attrs, int *errp) 120 { 121 return (inuse_zpool_common(slice, attrs, errp, DM_USE_EXPORTED_ZPOOL)); 122 } 123 124 /* 125 * Try to dynamically link the zfs functions we need. 126 */ 127 static void* 128 init_zpool() 129 { 130 void *lh = NULL; 131 132 if ((lh = dlopen("libzfs.so", RTLD_NOW)) == NULL) { 133 return (lh); 134 } 135 /* 136 * Instantiate the functions needed to get zpool configuration 137 * data 138 */ 139 if ((zfsdl_zpool_in_use = (int (*)(int, pool_state_t *, char **)) 140 dlsym(lh, "zpool_in_use")) == NULL) { 141 (void) dlclose(lh); 142 return (NULL); 143 } 144 145 return (lh); 146 } 147