xref: /titanic_44/usr/src/lib/libcontract/common/device.c (revision 25e8c5aa2b496d9026e958ac731a610167574f59)
1*25e8c5aaSvikram /*
2*25e8c5aaSvikram  * CDDL HEADER START
3*25e8c5aaSvikram  *
4*25e8c5aaSvikram  * The contents of this file are subject to the terms of the
5*25e8c5aaSvikram  * Common Development and Distribution License (the "License").
6*25e8c5aaSvikram  * You may not use this file except in compliance with the License.
7*25e8c5aaSvikram  *
8*25e8c5aaSvikram  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*25e8c5aaSvikram  * or http://www.opensolaris.org/os/licensing.
10*25e8c5aaSvikram  * See the License for the specific language governing permissions
11*25e8c5aaSvikram  * and limitations under the License.
12*25e8c5aaSvikram  *
13*25e8c5aaSvikram  * When distributing Covered Code, include this CDDL HEADER in each
14*25e8c5aaSvikram  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*25e8c5aaSvikram  * If applicable, add the following below this CDDL HEADER, with the
16*25e8c5aaSvikram  * fields enclosed by brackets "[]" replaced with your own identifying
17*25e8c5aaSvikram  * information: Portions Copyright [yyyy] [name of copyright owner]
18*25e8c5aaSvikram  *
19*25e8c5aaSvikram  * CDDL HEADER END
20*25e8c5aaSvikram  */
21*25e8c5aaSvikram /*
22*25e8c5aaSvikram  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23*25e8c5aaSvikram  * Use is subject to license terms.
24*25e8c5aaSvikram  */
25*25e8c5aaSvikram 
26*25e8c5aaSvikram #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*25e8c5aaSvikram 
28*25e8c5aaSvikram #include <sys/ctfs.h>
29*25e8c5aaSvikram #include <sys/contract.h>
30*25e8c5aaSvikram #include <sys/contract/device.h>
31*25e8c5aaSvikram #include <errno.h>
32*25e8c5aaSvikram #include <unistd.h>
33*25e8c5aaSvikram #include <string.h>
34*25e8c5aaSvikram #include <libnvpair.h>
35*25e8c5aaSvikram #include <limits.h>
36*25e8c5aaSvikram #include <sys/stat.h>
37*25e8c5aaSvikram #include <libcontract.h>
38*25e8c5aaSvikram #include "libcontract_impl.h"
39*25e8c5aaSvikram 
40*25e8c5aaSvikram /*
41*25e8c5aaSvikram  * Device contract template routines
42*25e8c5aaSvikram  */
43*25e8c5aaSvikram 
44*25e8c5aaSvikram int
45*25e8c5aaSvikram ct_dev_tmpl_set_minor(int fd, char *minor)
46*25e8c5aaSvikram {
47*25e8c5aaSvikram 	return (ct_tmpl_set_internal(fd, CTDP_MINOR, (uintptr_t)minor));
48*25e8c5aaSvikram }
49*25e8c5aaSvikram 
50*25e8c5aaSvikram int
51*25e8c5aaSvikram ct_dev_tmpl_set_aset(int fd, uint_t aset)
52*25e8c5aaSvikram {
53*25e8c5aaSvikram 	return (ct_tmpl_set_internal(fd, CTDP_ACCEPT, aset));
54*25e8c5aaSvikram }
55*25e8c5aaSvikram 
56*25e8c5aaSvikram int
57*25e8c5aaSvikram ct_dev_tmpl_set_noneg(int fd)
58*25e8c5aaSvikram {
59*25e8c5aaSvikram 	return (ct_tmpl_set_internal(fd, CTDP_NONEG, CTDP_NONEG_SET));
60*25e8c5aaSvikram }
61*25e8c5aaSvikram 
62*25e8c5aaSvikram int
63*25e8c5aaSvikram ct_dev_tmpl_clear_noneg(int fd)
64*25e8c5aaSvikram {
65*25e8c5aaSvikram 	return (ct_tmpl_set_internal(fd, CTDP_NONEG, CTDP_NONEG_CLEAR));
66*25e8c5aaSvikram }
67*25e8c5aaSvikram 
68*25e8c5aaSvikram int
69*25e8c5aaSvikram ct_dev_tmpl_get_minor(int fd, char *buf, size_t *buflenp)
70*25e8c5aaSvikram {
71*25e8c5aaSvikram 	char	path[PATH_MAX];
72*25e8c5aaSvikram 	int	error;
73*25e8c5aaSvikram 	size_t	len;
74*25e8c5aaSvikram 
75*25e8c5aaSvikram 	error = ct_tmpl_get_internal_string(fd, CTDP_MINOR, path);
76*25e8c5aaSvikram 	if (error) {
77*25e8c5aaSvikram 		return (error);
78*25e8c5aaSvikram 	}
79*25e8c5aaSvikram 
80*25e8c5aaSvikram 	len = strlcpy(buf, path, *buflenp);
81*25e8c5aaSvikram 	if (len >= *buflenp) {
82*25e8c5aaSvikram 		*buflenp = len + 1;
83*25e8c5aaSvikram 		return (EOVERFLOW);
84*25e8c5aaSvikram 	}
85*25e8c5aaSvikram 
86*25e8c5aaSvikram 	return (0);
87*25e8c5aaSvikram }
88*25e8c5aaSvikram 
89*25e8c5aaSvikram int
90*25e8c5aaSvikram ct_dev_tmpl_get_aset(int fd, uint_t *aset)
91*25e8c5aaSvikram {
92*25e8c5aaSvikram 	return (ct_tmpl_get_internal(fd, CTDP_ACCEPT, aset));
93*25e8c5aaSvikram }
94*25e8c5aaSvikram 
95*25e8c5aaSvikram int
96*25e8c5aaSvikram ct_dev_tmpl_get_noneg(int fd, uint_t *negp)
97*25e8c5aaSvikram {
98*25e8c5aaSvikram 	return (ct_tmpl_get_internal(fd, CTDP_NONEG, negp));
99*25e8c5aaSvikram }
100*25e8c5aaSvikram 
101*25e8c5aaSvikram /*
102*25e8c5aaSvikram  * Device contract event routines
103*25e8c5aaSvikram  */
104*25e8c5aaSvikram 
105*25e8c5aaSvikram /*
106*25e8c5aaSvikram  * No device contract specific event routines
107*25e8c5aaSvikram  */
108*25e8c5aaSvikram 
109*25e8c5aaSvikram 
110*25e8c5aaSvikram /*
111*25e8c5aaSvikram  * Device contract status routines
112*25e8c5aaSvikram  */
113*25e8c5aaSvikram 
114*25e8c5aaSvikram int
115*25e8c5aaSvikram ct_dev_status_get_aset(ct_stathdl_t stathdl, uint_t *aset)
116*25e8c5aaSvikram {
117*25e8c5aaSvikram 	struct ctlib_status_info *info = stathdl;
118*25e8c5aaSvikram 
119*25e8c5aaSvikram 	if (info->status.ctst_type != CTT_DEVICE)
120*25e8c5aaSvikram 		return (EINVAL);
121*25e8c5aaSvikram 
122*25e8c5aaSvikram 	if (info->nvl == NULL)
123*25e8c5aaSvikram 		return (ENOENT);
124*25e8c5aaSvikram 
125*25e8c5aaSvikram 	return (nvlist_lookup_uint32(info->nvl, CTDS_ASET, aset));
126*25e8c5aaSvikram }
127*25e8c5aaSvikram 
128*25e8c5aaSvikram int
129*25e8c5aaSvikram ct_dev_status_get_noneg(ct_stathdl_t stathdl, uint_t *negp)
130*25e8c5aaSvikram {
131*25e8c5aaSvikram 	struct ctlib_status_info *info = stathdl;
132*25e8c5aaSvikram 
133*25e8c5aaSvikram 	if (info->status.ctst_type != CTT_DEVICE)
134*25e8c5aaSvikram 		return (EINVAL);
135*25e8c5aaSvikram 
136*25e8c5aaSvikram 	if (info->nvl == NULL)
137*25e8c5aaSvikram 		return (ENOENT);
138*25e8c5aaSvikram 
139*25e8c5aaSvikram 	return (nvlist_lookup_uint32(info->nvl, CTDS_NONEG, negp));
140*25e8c5aaSvikram }
141*25e8c5aaSvikram 
142*25e8c5aaSvikram int
143*25e8c5aaSvikram ct_dev_status_get_dev_state(ct_stathdl_t stathdl, uint_t *statep)
144*25e8c5aaSvikram {
145*25e8c5aaSvikram 	struct ctlib_status_info *info = stathdl;
146*25e8c5aaSvikram 
147*25e8c5aaSvikram 	if (info->status.ctst_type != CTT_DEVICE)
148*25e8c5aaSvikram 		return (EINVAL);
149*25e8c5aaSvikram 
150*25e8c5aaSvikram 	if (info->nvl == NULL)
151*25e8c5aaSvikram 		return (ENOENT);
152*25e8c5aaSvikram 
153*25e8c5aaSvikram 	return (nvlist_lookup_uint32(info->nvl, CTDS_STATE, statep));
154*25e8c5aaSvikram }
155*25e8c5aaSvikram 
156*25e8c5aaSvikram int
157*25e8c5aaSvikram ct_dev_status_get_minor(ct_stathdl_t stathdl, char **bufp)
158*25e8c5aaSvikram {
159*25e8c5aaSvikram 	int error;
160*25e8c5aaSvikram 	struct ctlib_status_info *info = stathdl;
161*25e8c5aaSvikram 
162*25e8c5aaSvikram 	if (bufp == NULL)
163*25e8c5aaSvikram 		return (EINVAL);
164*25e8c5aaSvikram 
165*25e8c5aaSvikram 	if (info->status.ctst_type != CTT_DEVICE)
166*25e8c5aaSvikram 		return (EINVAL);
167*25e8c5aaSvikram 
168*25e8c5aaSvikram 	if (info->nvl == NULL)
169*25e8c5aaSvikram 		return (ENOENT);
170*25e8c5aaSvikram 
171*25e8c5aaSvikram 	error = nvlist_lookup_string(info->nvl, CTDS_MINOR, bufp);
172*25e8c5aaSvikram 	if (error != 0) {
173*25e8c5aaSvikram 		return (error);
174*25e8c5aaSvikram 	}
175*25e8c5aaSvikram 
176*25e8c5aaSvikram 	return (0);
177*25e8c5aaSvikram }
178