xref: /onnv-gate/usr/src/lib/lvm/libmeta/common/meta_admin.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1992-1994, 2000-2002 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * miscellaneous utilities
32*0Sstevel@tonic-gate  */
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include <meta.h>
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate static	int	meta_fd = -1;
37*0Sstevel@tonic-gate static	major_t	meta_major;
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate /*
40*0Sstevel@tonic-gate  * open administrative device
41*0Sstevel@tonic-gate  */
42*0Sstevel@tonic-gate int
43*0Sstevel@tonic-gate open_admin(
44*0Sstevel@tonic-gate 	md_error_t	*ep
45*0Sstevel@tonic-gate )
46*0Sstevel@tonic-gate {
47*0Sstevel@tonic-gate 	struct stat	buf;
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate 	/* if not already open */
50*0Sstevel@tonic-gate 	if (meta_fd < 0) {
51*0Sstevel@tonic-gate 		ulong_t	dversion = 0;
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate 		/* try read/write fall back to readonly */
54*0Sstevel@tonic-gate 		if ((meta_fd = open(ADMSPECIAL, O_RDWR, 0)) < 0) {
55*0Sstevel@tonic-gate 			if (errno != EACCES)
56*0Sstevel@tonic-gate 				return (mdsyserror(ep, errno, ADMSPECIAL));
57*0Sstevel@tonic-gate 			if ((meta_fd = open(ADMSPECIAL, O_RDONLY, 0)) < 0)
58*0Sstevel@tonic-gate 				return (mdsyserror(ep, errno, ADMSPECIAL));
59*0Sstevel@tonic-gate 		}
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 		/* get major */
62*0Sstevel@tonic-gate 		if (fstat(meta_fd, &buf) != 0)
63*0Sstevel@tonic-gate 			return (mdsyserror(ep, errno, ADMSPECIAL));
64*0Sstevel@tonic-gate 		meta_major = major(buf.st_rdev);
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate 		/* check driver version */
67*0Sstevel@tonic-gate 		if (metaioctl(MD_IOCGVERSION, &dversion, ep, NULL) != 0)
68*0Sstevel@tonic-gate 			return (-1);
69*0Sstevel@tonic-gate 		if (dversion != MD_DVERSION)
70*0Sstevel@tonic-gate 			return (mderror(ep, MDE_DVERSION, NULL));
71*0Sstevel@tonic-gate 	}
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 	/* return fd */
74*0Sstevel@tonic-gate 	return (meta_fd);
75*0Sstevel@tonic-gate }
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate int
78*0Sstevel@tonic-gate close_admin(
79*0Sstevel@tonic-gate 	md_error_t	*ep
80*0Sstevel@tonic-gate )
81*0Sstevel@tonic-gate {
82*0Sstevel@tonic-gate 	if (meta_fd >= 0) {
83*0Sstevel@tonic-gate 		if (close(meta_fd) == -1)
84*0Sstevel@tonic-gate 			return (mdsyserror(ep, errno, ADMSPECIAL));
85*0Sstevel@tonic-gate 		meta_fd = -1;
86*0Sstevel@tonic-gate 	}
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 	return (0);
89*0Sstevel@tonic-gate }
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate /*
92*0Sstevel@tonic-gate  * Returns True if the md_dev64_t passed in is a metadevice.
93*0Sstevel@tonic-gate  * Else it returns False.
94*0Sstevel@tonic-gate  */
95*0Sstevel@tonic-gate int
96*0Sstevel@tonic-gate meta_dev_ismeta(
97*0Sstevel@tonic-gate 	md_dev64_t	dev
98*0Sstevel@tonic-gate )
99*0Sstevel@tonic-gate {
100*0Sstevel@tonic-gate 	int		fd;
101*0Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	fd = open_admin(&status);
104*0Sstevel@tonic-gate 	assert(fd >= 0);
105*0Sstevel@tonic-gate 	return (meta_getmajor(dev) == meta_major);
106*0Sstevel@tonic-gate }
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate int
110*0Sstevel@tonic-gate meta_get_nunits(md_error_t *ep)
111*0Sstevel@tonic-gate {
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	static set_t		max_nunits = 0;
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	if (max_nunits == 0)
116*0Sstevel@tonic-gate 		if (metaioctl(MD_IOCGETNUNITS, &max_nunits, ep, NULL) != 0)
117*0Sstevel@tonic-gate 			return (-1);
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	return (max_nunits);
120*0Sstevel@tonic-gate }
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate md_dev64_t
123*0Sstevel@tonic-gate metamakedev(minor_t mnum)
124*0Sstevel@tonic-gate {
125*0Sstevel@tonic-gate 	int		fd;
126*0Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	fd = open_admin(&status);
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 	assert(fd >= 0);
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	return (((md_dev64_t)meta_major << NBITSMINOR64) | mnum);
133*0Sstevel@tonic-gate }
134