1*45916cd2Sjpk /*
2*45916cd2Sjpk  * CDDL HEADER START
3*45916cd2Sjpk  *
4*45916cd2Sjpk  * The contents of this file are subject to the terms of the
5*45916cd2Sjpk  * Common Development and Distribution License (the "License").
6*45916cd2Sjpk  * You may not use this file except in compliance with the License.
7*45916cd2Sjpk  *
8*45916cd2Sjpk  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*45916cd2Sjpk  * or http://www.opensolaris.org/os/licensing.
10*45916cd2Sjpk  * See the License for the specific language governing permissions
11*45916cd2Sjpk  * and limitations under the License.
12*45916cd2Sjpk  *
13*45916cd2Sjpk  * When distributing Covered Code, include this CDDL HEADER in each
14*45916cd2Sjpk  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*45916cd2Sjpk  * If applicable, add the following below this CDDL HEADER, with the
16*45916cd2Sjpk  * fields enclosed by brackets "[]" replaced with your own identifying
17*45916cd2Sjpk  * information: Portions Copyright [yyyy] [name of copyright owner]
18*45916cd2Sjpk  *
19*45916cd2Sjpk  * CDDL HEADER END
20*45916cd2Sjpk  */
21*45916cd2Sjpk 
22*45916cd2Sjpk /*
23*45916cd2Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*45916cd2Sjpk  * Use is subject to license terms.
25*45916cd2Sjpk  */
26*45916cd2Sjpk 
27*45916cd2Sjpk #include <stdlib.h>
28*45916cd2Sjpk #include <errno.h>
29*45916cd2Sjpk #include <tsol/label.h>
30*45916cd2Sjpk #include <bsm/devices.h>
31*45916cd2Sjpk 
32*45916cd2Sjpk 
33*45916cd2Sjpk /*
34*45916cd2Sjpk  * getdevicerange
35*45916cd2Sjpk  *	Gets the minimum and maximum labels within which the device can
36*45916cd2Sjpk  *	be used. If label range is not specified for the device in
37*45916cd2Sjpk  *	device_allocate, defaults to admin_low and admin_high.
38*45916cd2Sjpk  *	Returns malloc'ed blrange pointer, or NULL on any error.
39*45916cd2Sjpk  */
40*45916cd2Sjpk blrange_t *
getdevicerange(const char * dev)41*45916cd2Sjpk getdevicerange(const char *dev)
42*45916cd2Sjpk {
43*45916cd2Sjpk 	int		err;
44*45916cd2Sjpk 	char		*lstr;
45*45916cd2Sjpk 	devalloc_t	*da;
46*45916cd2Sjpk 	devmap_t	*dm;
47*45916cd2Sjpk 	blrange_t	*range;
48*45916cd2Sjpk 
49*45916cd2Sjpk 	errno = 0;
50*45916cd2Sjpk 	if ((range = malloc(sizeof (blrange_t))) == NULL)
51*45916cd2Sjpk 		return (NULL);
52*45916cd2Sjpk 	if ((range->lower_bound = blabel_alloc()) == NULL) {
53*45916cd2Sjpk 		free(range);
54*45916cd2Sjpk 		return (NULL);
55*45916cd2Sjpk 	}
56*45916cd2Sjpk 	if ((range->upper_bound = blabel_alloc()) == NULL) {
57*45916cd2Sjpk 		blabel_free(range->lower_bound);
58*45916cd2Sjpk 		free(range);
59*45916cd2Sjpk 		return (NULL);
60*45916cd2Sjpk 	}
61*45916cd2Sjpk 
62*45916cd2Sjpk 	/*
63*45916cd2Sjpk 	 * If an entry is found for the named device,
64*45916cd2Sjpk 	 * return its label range.
65*45916cd2Sjpk 	 */
66*45916cd2Sjpk 	setdaent();
67*45916cd2Sjpk 	if ((da = getdanam((char *)dev)) == NULL) {
68*45916cd2Sjpk 		setdmapent();
69*45916cd2Sjpk 		/* check for an actual device file */
70*45916cd2Sjpk 		if ((dm = getdmapdev((char *)dev)) != NULL) {
71*45916cd2Sjpk 			da = getdanam(dm->dmap_devname);
72*45916cd2Sjpk 			freedmapent(dm);
73*45916cd2Sjpk 		}
74*45916cd2Sjpk 		enddmapent();
75*45916cd2Sjpk 	}
76*45916cd2Sjpk 	enddaent();
77*45916cd2Sjpk 	if (da == NULL) {
78*45916cd2Sjpk 		bsllow(range->lower_bound);
79*45916cd2Sjpk 		bslhigh(range->upper_bound);
80*45916cd2Sjpk 	} else {
81*45916cd2Sjpk 		lstr = kva_match(da->da_devopts, DAOPT_MINLABEL);
82*45916cd2Sjpk 		if (lstr == NULL) {
83*45916cd2Sjpk 			bsllow(range->lower_bound);
84*45916cd2Sjpk 		} else if (stobsl(lstr, range->lower_bound, NO_CORRECTION,
85*45916cd2Sjpk 		    &err) == 0) {
86*45916cd2Sjpk 			blabel_free(range->lower_bound);
87*45916cd2Sjpk 			blabel_free(range->upper_bound);
88*45916cd2Sjpk 			free(range);
89*45916cd2Sjpk 			errno = ENOTSUP;
90*45916cd2Sjpk 			return (NULL);
91*45916cd2Sjpk 		}
92*45916cd2Sjpk 		lstr = kva_match(da->da_devopts, DAOPT_MAXLABEL);
93*45916cd2Sjpk 		if (lstr == NULL) {
94*45916cd2Sjpk 			bslhigh(range->upper_bound);
95*45916cd2Sjpk 		} else if (stobsl(lstr, range->upper_bound, NO_CORRECTION,
96*45916cd2Sjpk 		    &err) == 0) {
97*45916cd2Sjpk 			blabel_free(range->lower_bound);
98*45916cd2Sjpk 			blabel_free(range->upper_bound);
99*45916cd2Sjpk 			free(range);
100*45916cd2Sjpk 			errno = ENOTSUP;
101*45916cd2Sjpk 			return (NULL);
102*45916cd2Sjpk 		}
103*45916cd2Sjpk 		freedaent(da);
104*45916cd2Sjpk 	}
105*45916cd2Sjpk 
106*45916cd2Sjpk 	return (range);
107*45916cd2Sjpk }
108