xref: /titanic_41/usr/src/cmd/devfsadm/md_link.c (revision fd9cb95cbb2f626355a60efb9d02c5f0a33c10e6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <regex.h>
30 #include <devfsadm.h>
31 #include <stdio.h>
32 #include <strings.h>
33 #include <stdlib.h>
34 #include <limits.h>
35 #include <sys/mkdev.h>
36 
37 #define	MD_LINK_RE_DEVICES	"^md/r?dsk/d[0-9]+$"
38 #define	MD_LINK_RE_SHARED	"^md/shared/[0-9]+/r?dsk/d[0-9]+$"
39 #define	MD_LINK_RE_ADMIN	"^md/admin"
40 
41 /*
42  * The devfsadm link module require the next section to
43  * be defined in order to know what and when to call functions
44  * in the module on device creation and removal.
45  */
46 
47 /* setup for device creation */
48 
49 static int md_create(di_minor_t minor, di_node_t node);
50 
51 static devfsadm_create_t md_cbt[] = {
52 	{ "pseudo", "ddi_pseudo", "md",
53 	    TYPE_EXACT | DRV_EXACT, ILEVEL_0, md_create,
54 	},
55 };
56 
57 DEVFSADM_CREATE_INIT_V0(md_cbt);
58 
59 /*
60  * remove devices - always allow disks to be dynamically removed. Only allow
61  *		    admin device to be removed at reboot.
62  */
63 
64 static devfsadm_remove_t md_remove_cbt[] = {
65 	{"pseudo", MD_LINK_RE_DEVICES, RM_ALWAYS | RM_PRE | RM_HOT,
66 	    ILEVEL_0, devfsadm_rm_all},
67 	{"pseudo", MD_LINK_RE_SHARED, RM_ALWAYS | RM_PRE | RM_HOT,
68 	    ILEVEL_0, devfsadm_rm_all},
69 	{"pseudo", MD_LINK_RE_ADMIN, RM_ALWAYS | RM_PRE,
70 	    ILEVEL_0, devfsadm_rm_all},
71 };
72 
73 DEVFSADM_REMOVE_INIT_V0(md_remove_cbt);
74 
75 
76 /*
77  * For the admin device:
78  *	/dev/md/admin -> /devices/pseudo/md@0:admin
79  *
80  * For each other device
81  *	/dev/md/dsk/dN -> /devices/pseudo/md@0:0,N,blk
82  *	/dev/md/rdsk/dN -> /devices/pseudo/md@0:0,N,raw
83  *	/dev/md/shared/M/dsk/dN /devices/pseudo/md@0:M,N,blk
84  *	/dev/md/shared/M/rdsk/dN /devices/pseudo/md@0:M,N,raw
85  */
86 static int
87 md_create(di_minor_t minor, di_node_t node)
88 {
89 	char mn[MAXNAMELEN + 1];
90 	char path[PATH_MAX + 1];
91 	int set = -1, mdev = -1, ret;
92 	char *type, *dir;
93 
94 	(void) strcpy(mn, di_minor_name(minor));
95 
96 	/*
97 	 * Check whether we are being requested to setup the admin
98 	 * device link or one of the metadevice links. They need
99 	 * to be treated differently.
100 	 */
101 
102 	if (strcmp(mn, "admin") == 0) {
103 		/* there is only one admin link and always in /dev/md/admin */
104 		(void) devfsadm_mklink("md/admin", node, minor, 0);
105 	} else {
106 		/*
107 		 * Extract out the minor components and create the
108 		 * appropriate links. The node looks like:
109 		 * md@<set>,<mdev>,<type>
110 		 * where the <set> number is the named diskset,
111 		 * <mdev> is the metadevice number, and <type>
112 		 * is the trailing "blk" or "raw" indication.
113 		 *
114 		 * NOTE: when <set> is non-zero, we need to create
115 		 * under the "shared" directory entry instead of linking
116 		 * into the top level dsk/rdsk directories.
117 		 */
118 		ret = sscanf(mn, "%d,%d,", &set, &mdev);
119 		if (ret == 2 && (type = strrchr(mn, ',')) != NULL) {
120 			type ++;
121 			if (strcmp(type, "blk") == 0) {
122 				dir = "dsk";
123 			} else {
124 				dir = "rdsk";
125 			}
126 			if (set == 0) {
127 				/* this is a simple md */
128 				(void) snprintf(path, sizeof (path),
129 					"md/%s/d%d", dir, mdev);
130 			} else {
131 				/* this is a shared md */
132 				(void) snprintf(path, sizeof (path),
133 					"md/shared/%d/%s/d%d",
134 					set, dir, mdev);
135 			}
136 			(void) devfsadm_mklink(path, node, minor, 0);
137 		}
138 	}
139 	return (DEVFSADM_CONTINUE);
140 }
141