usr/src/lib/libdiskmgt/common/inuse_zpool.c
author sjelinek
Thu, 27 Oct 2005 09:01:18 -0700
changeset 767 0aa24dfb5d87
child 1352 b7039548de2f
permissions -rw-r--r--
PSARC 2004/776 device checking for fs utilities PSARC 2005/461 Device in use checking environment variables 5084421 libdiskmgt needs to detect in use ZFS data 5085739 remove workaround for bug 4725434 6194015 Device in use checking for Solaris utilities-PSARC/2004/776 6261853 libdiskmgt does not work correctly in all cases on amd64 6268374 libdiskmgt allocates incorrect size for readdir_r() dirent argument 6291309 PSARC/2005/461 - libdiskmgt should enable bypassing of inuse checking 6301815 PSARC/2005/461-Need Sun private libdiskmgt flag for use during install to disable sysevent starting
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
767
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
     1
/*
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
     2
 * CDDL HEADER START
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
     3
 *
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
     4
 * The contents of this file are subject to the terms of the
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
     5
 * Common Development and Distribution License, Version 1.0 only
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
     6
 * (the "License").  You may not use this file except in compliance
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
     7
 * with the License.
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
     8
 *
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
     9
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    10
 * or http://www.opensolaris.org/os/licensing.
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    11
 * See the License for the specific language governing permissions
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    12
 * and limitations under the License.
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    13
 *
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    14
 * When distributing Covered Code, include this CDDL HEADER in each
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    15
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    16
 * If applicable, add the following below this CDDL HEADER, with the
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    17
 * fields enclosed by brackets "[]" replaced with your own identifying
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    18
 * information: Portions Copyright [yyyy] [name of copyright owner]
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    19
 *
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    20
 * CDDL HEADER END
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    21
 */
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    22
/*
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    23
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    24
 * Use is subject to license terms.
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    25
 */
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    26
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    27
#pragma ident	"%Z%%M%	%I%	%E% SMI"
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    28
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    29
/*
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    30
 * Attempt to dynamically link in the ZFS libzfs.so.1 so that we can
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    31
 * see if there are any ZFS zpools on any of the slices.
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    32
 */
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    33
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    34
#include <stdlib.h>
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    35
#include <stdio.h>
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    36
#include <strings.h>
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    37
#include <sys/param.h>
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    38
#include <sys/errno.h>
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    39
#include <sys/types.h>
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    40
#include <sys/stat.h>
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    41
#include <fcntl.h>
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    42
#include <thread.h>
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    43
#include <synch.h>
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    44
#include <dlfcn.h>
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    45
#include <link.h>
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    46
#include <ctype.h>
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    47
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    48
#include "libdiskmgt.h"
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    49
#include "disks_private.h"
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    50
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    51
/*
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    52
 * Pointers to libzfs.so functions that we dynamically resolve.
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    53
 */
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    54
static	int	(*zfsdl_zpool_in_use)(int fd, char **desc, char **name);
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    55
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    56
static mutex_t			init_lock = DEFAULTMUTEX;
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    57
static rwlock_t			zpool_lock = DEFAULTRWLOCK;
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    58
static	int			initialized = 0;
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    59
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    60
static void	*init_zpool();
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    61
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    62
int
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    63
inuse_zpool(char *slice, nvlist_t *attrs, int *errp)
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    64
{
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    65
	int		found = 0;
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    66
	char		*desc, *name;
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    67
	int		fd;
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    68
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    69
	*errp = 0;
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    70
	if (slice == NULL) {
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    71
	    return (found);
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    72
	}
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    73
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    74
	(void) mutex_lock(&init_lock);
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    75
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    76
	/*
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    77
	 * Dynamically load libzfs
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    78
	 */
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    79
	if (!initialized) {
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    80
		if (!init_zpool()) {
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    81
			(void) mutex_unlock(&init_lock);
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    82
			return (found);
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    83
		}
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    84
		initialized = 1;
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    85
	}
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    86
	(void) mutex_unlock(&init_lock);
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    87
	(void) rw_rdlock(&zpool_lock);
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    88
	if ((fd = open(slice, O_RDONLY)) > 0) {
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    89
		if (zfsdl_zpool_in_use(fd, &desc, &name)) {
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    90
			libdiskmgt_add_str(attrs, DM_USED_BY,
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    91
				DM_USE_ZPOOL, errp);
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    92
			libdiskmgt_add_str(attrs, DM_USED_NAME,
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    93
				name, errp);
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    94
			found = 1;
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    95
		}
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    96
	}
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    97
	(void) rw_unlock(&zpool_lock);
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    98
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
    99
	return (found);
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
   100
}
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
   101
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
   102
/*
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
   103
 * Try to dynamically link the zfs functions we need.
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
   104
 */
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
   105
static void*
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
   106
init_zpool()
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
   107
{
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
   108
	void	*lh = NULL;
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
   109
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
   110
	if ((lh = dlopen("libzfs.so", RTLD_NOW)) == NULL) {
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
   111
		return (lh);
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
   112
	}
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
   113
	/*
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
   114
	 * Instantiate the functions needed to get zpool configuration
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
   115
	 * data
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
   116
	 */
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
   117
	if ((zfsdl_zpool_in_use = (int (*)(int, char **, char **))dlsym(lh,
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
   118
	    "zpool_in_use")) == NULL) {
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
   119
		(void) dlclose(lh);
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
   120
		return (NULL);
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
   121
	}
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
   122
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
   123
	return (lh);
0aa24dfb5d87 PSARC 2004/776 device checking for fs utilities
sjelinek
parents:
diff changeset
   124
}