1*2723Scth /*
2*2723Scth * CDDL HEADER START
3*2723Scth *
4*2723Scth * The contents of this file are subject to the terms of the
5*2723Scth * Common Development and Distribution License (the "License").
6*2723Scth * You may not use this file except in compliance with the License.
7*2723Scth *
8*2723Scth * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*2723Scth * or http://www.opensolaris.org/os/licensing.
10*2723Scth * See the License for the specific language governing permissions
11*2723Scth * and limitations under the License.
12*2723Scth *
13*2723Scth * When distributing Covered Code, include this CDDL HEADER in each
14*2723Scth * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*2723Scth * If applicable, add the following below this CDDL HEADER, with the
16*2723Scth * fields enclosed by brackets "[]" replaced with your own identifying
17*2723Scth * information: Portions Copyright [yyyy] [name of copyright owner]
18*2723Scth *
19*2723Scth * CDDL HEADER END
20*2723Scth */
21*2723Scth /*
22*2723Scth * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23*2723Scth * Use is subject to license terms.
24*2723Scth */
25*2723Scth
26*2723Scth #pragma ident "%Z%%M% %I% %E% SMI"
27*2723Scth
28*2723Scth #include "libdevinfo.h"
29*2723Scth #include <strings.h>
30*2723Scth #include <sys/modctl.h>
31*2723Scth
32*2723Scth /*
33*2723Scth *
34*2723Scth * This file contains interfaces to translate <driver><instance><minorname>
35*2723Scth * information into /devices and /dev paths. It does this using interfaces to
36*2723Scth * the kernel instance tree so that it can provide translations for devices
37*2723Scth * which are no longer present. An example consumer of these interfaces is
38*2723Scth * iostat(1M) - which shows, in its first iteration, activity since reboot.
39*2723Scth * With persistant kstats, a device which was busy a long time ago can still
40*2723Scth * have a decaying presence in iostat output, and that output, when '-n' is
41*2723Scth * used, should show the public name.
42*2723Scth */
43*2723Scth
44*2723Scth typedef struct {
45*2723Scth di_devlink_handle_t i_devlink_hdl;
46*2723Scth } *idim_t;
47*2723Scth
48*2723Scth di_dim_t
di_dim_init()49*2723Scth di_dim_init()
50*2723Scth {
51*2723Scth idim_t idim;
52*2723Scth
53*2723Scth if ((idim = (idim_t)malloc(sizeof (*idim))) == NULL)
54*2723Scth return (NULL);
55*2723Scth idim->i_devlink_hdl = di_devlink_init(NULL, 0);
56*2723Scth if (idim->i_devlink_hdl == NULL) {
57*2723Scth free(idim);
58*2723Scth return (NULL);
59*2723Scth }
60*2723Scth return ((di_dim_t)idim);
61*2723Scth }
62*2723Scth
63*2723Scth void
di_dim_fini(di_dim_t dim)64*2723Scth di_dim_fini(di_dim_t dim)
65*2723Scth {
66*2723Scth idim_t idim = (idim_t)dim;
67*2723Scth
68*2723Scth if (idim->i_devlink_hdl) {
69*2723Scth (void) di_devlink_fini(&idim->i_devlink_hdl);
70*2723Scth }
71*2723Scth free(idim);
72*2723Scth }
73*2723Scth
74*2723Scth /*ARGSUSED*/
75*2723Scth char *
di_dim_path_devices(di_dim_t dim,char * drv_name,int instance,char * minor_name)76*2723Scth di_dim_path_devices(di_dim_t dim, char *drv_name, int instance,
77*2723Scth char *minor_name)
78*2723Scth {
79*2723Scth major_t major;
80*2723Scth int len;
81*2723Scth int mlen;
82*2723Scth char *devices;
83*2723Scth
84*2723Scth /* convert drv_name to major_t */
85*2723Scth if (modctl(MODGETMAJBIND, drv_name, strlen(drv_name) + 1, &major) < 0)
86*2723Scth return (NULL);
87*2723Scth
88*2723Scth /* find the length of the devices path given major,instance */
89*2723Scth if (modctl(MODGETDEVFSPATH_MI_LEN, major, instance, &len) != 0)
90*2723Scth return (NULL);
91*2723Scth
92*2723Scth /*
93*2723Scth * MODGETDEVFSPATH_MI_LEN result includes '\0' termination, but we
94*2723Scth * may need to add space for ":<minor_name>"
95*2723Scth */
96*2723Scth if (minor_name)
97*2723Scth mlen = len + 1 + strlen(minor_name);
98*2723Scth else
99*2723Scth mlen = len;
100*2723Scth if ((devices = (char *)malloc(mlen)) == NULL)
101*2723Scth return (NULL);
102*2723Scth
103*2723Scth if (modctl(MODGETDEVFSPATH_MI, major, instance, len, devices) != 0) {
104*2723Scth free(devices);
105*2723Scth return (NULL);
106*2723Scth }
107*2723Scth
108*2723Scth if (minor_name) {
109*2723Scth /* add ':<minot_name>' to the end of /devices path */
110*2723Scth (void) strcat(devices, ":");
111*2723Scth (void) strcat(devices, minor_name);
112*2723Scth }
113*2723Scth return (devices);
114*2723Scth }
115*2723Scth
116*2723Scth /* di_dim_path_dev di_devlink callback */
117*2723Scth static int
di_dim_path_dev_callback(di_devlink_t dl,void * arg)118*2723Scth di_dim_path_dev_callback(di_devlink_t dl, void *arg)
119*2723Scth {
120*2723Scth char **devp = (char **)arg;
121*2723Scth char *devpath = (char *)di_devlink_path(dl);
122*2723Scth
123*2723Scth if (devpath)
124*2723Scth *devp = strdup(devpath);
125*2723Scth return (DI_WALK_TERMINATE);
126*2723Scth }
127*2723Scth
128*2723Scth char *
di_dim_path_dev(di_dim_t dim,char * drv_name,int instance,char * minor_name)129*2723Scth di_dim_path_dev(di_dim_t dim, char *drv_name, int instance, char *minor_name)
130*2723Scth {
131*2723Scth idim_t idim = (idim_t)dim;
132*2723Scth char *devices;
133*2723Scth char *dev = NULL;
134*2723Scth
135*2723Scth /* we must have a minor_name to resolve to a public name */
136*2723Scth if (minor_name == NULL)
137*2723Scth return (NULL);
138*2723Scth
139*2723Scth /* convert <driver><instance><minor_name> to /devices path */
140*2723Scth devices = di_dim_path_devices(dim, drv_name, instance, minor_name);
141*2723Scth if (devices == NULL)
142*2723Scth return (NULL);
143*2723Scth
144*2723Scth /* convert /devices path to /dev path */
145*2723Scth (void) di_devlink_walk(idim->i_devlink_hdl, NULL,
146*2723Scth devices, DI_PRIMARY_LINK, (void *)&dev, di_dim_path_dev_callback);
147*2723Scth free(devices);
148*2723Scth return (dev);
149*2723Scth }
150