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 2004 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 #include <regex.h>
30*0Sstevel@tonic-gate #include <devfsadm.h>
31*0Sstevel@tonic-gate #include <stdio.h>
32*0Sstevel@tonic-gate #include <strings.h>
33*0Sstevel@tonic-gate #include <stdlib.h>
34*0Sstevel@tonic-gate #include <limits.h>
35*0Sstevel@tonic-gate #include <sys/mkdev.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #define	MD_LINK_RE_DEVICES	"^md/r?dsk/d[0-9]+$"
38*0Sstevel@tonic-gate #define	MD_LINK_RE_SHARED	"^md/shared/[0-9]+/r?dsk/d[0-9]+$"
39*0Sstevel@tonic-gate #define	MD_LINK_RE_ADMIN	"^md/admin"
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate /*
42*0Sstevel@tonic-gate  * The devfsadm link module require the next section to
43*0Sstevel@tonic-gate  * be defined in order to know what and when to call functions
44*0Sstevel@tonic-gate  * in the module on device creation and removal.
45*0Sstevel@tonic-gate  */
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate /* setup for device creation */
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate static int md_create(di_minor_t minor, di_node_t node);
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate static devfsadm_create_t md_cbt[] = {
52*0Sstevel@tonic-gate 	{ "pseudo", "ddi_pseudo", "md",
53*0Sstevel@tonic-gate 	    TYPE_EXACT | DRV_EXACT, ILEVEL_0, md_create,
54*0Sstevel@tonic-gate 	},
55*0Sstevel@tonic-gate };
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate DEVFSADM_CREATE_INIT_V0(md_cbt);
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate /*
60*0Sstevel@tonic-gate  * remove devices - always allow disks to be dynamically removed. Only allow
61*0Sstevel@tonic-gate  *		    admin device to be removed at reboot.
62*0Sstevel@tonic-gate  */
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate static devfsadm_remove_t md_remove_cbt[] = {
65*0Sstevel@tonic-gate 	{"pseudo", MD_LINK_RE_DEVICES, RM_ALWAYS | RM_PRE | RM_HOT,
66*0Sstevel@tonic-gate 	    ILEVEL_0, devfsadm_rm_all},
67*0Sstevel@tonic-gate 	{"pseudo", MD_LINK_RE_SHARED, RM_ALWAYS | RM_PRE | RM_HOT,
68*0Sstevel@tonic-gate 	    ILEVEL_0, devfsadm_rm_all},
69*0Sstevel@tonic-gate 	{"pseudo", MD_LINK_RE_ADMIN, RM_ALWAYS | RM_PRE,
70*0Sstevel@tonic-gate 	    ILEVEL_0, devfsadm_rm_all},
71*0Sstevel@tonic-gate };
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate DEVFSADM_REMOVE_INIT_V0(md_remove_cbt);
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate /*
77*0Sstevel@tonic-gate  * For the admin device:
78*0Sstevel@tonic-gate  *	/dev/md/admin -> /devices/pseudo/md@0:admin
79*0Sstevel@tonic-gate  *
80*0Sstevel@tonic-gate  * For each other device
81*0Sstevel@tonic-gate  *	/dev/md/dsk/dN -> /devices/pseudo/md@0:0,N,blk
82*0Sstevel@tonic-gate  *	/dev/md/rdsk/dN -> /devices/pseudo/md@0:0,N,raw
83*0Sstevel@tonic-gate  *	/dev/md/shared/M/dsk/dN /devices/pseudo/md@0:M,N,blk
84*0Sstevel@tonic-gate  *	/dev/md/shared/M/rdsk/dN /devices/pseudo/md@0:M,N,raw
85*0Sstevel@tonic-gate  */
86*0Sstevel@tonic-gate static int
87*0Sstevel@tonic-gate md_create(di_minor_t minor, di_node_t node)
88*0Sstevel@tonic-gate {
89*0Sstevel@tonic-gate 	char mn[MAXNAMELEN + 1];
90*0Sstevel@tonic-gate 	char path[PATH_MAX + 1];
91*0Sstevel@tonic-gate 	int set = -1, mdev = -1, ret;
92*0Sstevel@tonic-gate 	char *type, *dir;
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 	(void) strcpy(mn, di_minor_name(minor));
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	/*
97*0Sstevel@tonic-gate 	 * Check whether we are being requested to setup the admin
98*0Sstevel@tonic-gate 	 * device link or one of the metadevice links. They need
99*0Sstevel@tonic-gate 	 * to be treated differently.
100*0Sstevel@tonic-gate 	 */
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	if (strcmp(mn, "admin") == 0) {
103*0Sstevel@tonic-gate 		/* there is only one admin link and always in /dev/md/admin */
104*0Sstevel@tonic-gate 		(void) devfsadm_mklink("md/admin", node, minor, 0);
105*0Sstevel@tonic-gate 	} else {
106*0Sstevel@tonic-gate 		/*
107*0Sstevel@tonic-gate 		 * Extract out the minor components and create the
108*0Sstevel@tonic-gate 		 * appropriate links. The node looks like:
109*0Sstevel@tonic-gate 		 * md@<set>,<mdev>,<type>
110*0Sstevel@tonic-gate 		 * where the <set> number is the named diskset,
111*0Sstevel@tonic-gate 		 * <mdev> is the metadevice number, and <type>
112*0Sstevel@tonic-gate 		 * is the trailing "blk" or "raw" indication.
113*0Sstevel@tonic-gate 		 *
114*0Sstevel@tonic-gate 		 * NOTE: when <set> is non-zero, we need to create
115*0Sstevel@tonic-gate 		 * under the "shared" directory entry instead of linking
116*0Sstevel@tonic-gate 		 * into the top level dsk/rdsk directories.
117*0Sstevel@tonic-gate 		 */
118*0Sstevel@tonic-gate 		ret = sscanf(mn, "%d,%d,", &set, &mdev);
119*0Sstevel@tonic-gate 		if (ret == 2 && (type = strrchr(mn, ',')) != NULL) {
120*0Sstevel@tonic-gate 			type ++;
121*0Sstevel@tonic-gate 			if (strcmp(type, "blk") == 0) {
122*0Sstevel@tonic-gate 				dir = "dsk";
123*0Sstevel@tonic-gate 			} else {
124*0Sstevel@tonic-gate 				dir = "rdsk";
125*0Sstevel@tonic-gate 			}
126*0Sstevel@tonic-gate 			if (set == 0) {
127*0Sstevel@tonic-gate 				/* this is a simple md */
128*0Sstevel@tonic-gate 				(void) snprintf(path, sizeof (path),
129*0Sstevel@tonic-gate 					"md/%s/d%d", dir, mdev);
130*0Sstevel@tonic-gate 			} else {
131*0Sstevel@tonic-gate 				/* this is a shared md */
132*0Sstevel@tonic-gate 				(void) snprintf(path, sizeof (path),
133*0Sstevel@tonic-gate 					"md/shared/%d/%s/d%d",
134*0Sstevel@tonic-gate 					set, dir, mdev);
135*0Sstevel@tonic-gate 			}
136*0Sstevel@tonic-gate 			(void) devfsadm_mklink(path, node, minor, 0);
137*0Sstevel@tonic-gate 		}
138*0Sstevel@tonic-gate 	}
139*0Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
140*0Sstevel@tonic-gate }
141