xref: /titanic_41/usr/src/cmd/devfsadm/md_link.c (revision d89fccd8788afe1e920f842edd883fe192a1b8fe)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <meta.h>
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/.+$"
38 #define	MD_LINK_RE_SHARED	"^md/shared/[0-9]+/r?dsk/.+$"
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 metadevice:
81  *	/dev/md/dsk/foobar --> /devices/pseudo/md@0:0,100,blk
82  *	/dev/md/rdsk/foobar --> /devices/pseudo/md@0:0,100,raw
83  *
84  * Where 'foobar' is user specified arbitrary name and '100'
85  * is the minor number returned by MD_IOCMAKE_DEV ioctl
86  *
87  */
88 static int
89 md_create(di_minor_t minor, di_node_t node)
90 {
91 	char mn[MAXNAMELEN + 1];
92 	char path[PATH_MAX + 1];
93 	int set = -1, ret;
94 	char *type, *dir;
95 	char *device_name;
96 	dev_t minor_devt = di_minor_devt(minor);
97 	int key;
98 	md_error_t ep;
99 
100 	(void) strcpy(mn, di_minor_name(minor));
101 
102 	/*
103 	 * Check whether we are being requested to setup the admin
104 	 * device link or one of the metadevice links. They need
105 	 * to be treated differently.
106 	 */
107 
108 	if (strcmp(mn, "admin") == 0) {
109 		/* there is only one admin link and always in /dev/md/admin */
110 		(void) devfsadm_mklink("md/admin", node, minor, 0);
111 	} else {
112 		/*
113 		 * Extract out the minor components and create the
114 		 * appropriate links. The node looks like:
115 		 * md@<set>,<mdev>,<type>
116 		 * where the <set> number is the named diskset,
117 		 * <mdev> is the metadevice number, and <type>
118 		 * is the trailing "blk" or "raw" indication.
119 		 *
120 		 * NOTE: when <set> is non-zero, we need to create
121 		 * under the "shared" directory entry instead of linking
122 		 * into the top level dsk/rdsk directories.
123 		 */
124 		ret = sscanf(mn, "%d,", &set);
125 		if (ret == 1 && (type = strrchr(mn, ',')) != NULL) {
126 			type++;
127 			if (strcmp(type, "blk") == 0) {
128 				dir = "dsk";
129 			} else {
130 				dir = "rdsk";
131 			}
132 
133 			(void) memset(&ep, '\0', sizeof (ep));
134 			if ((device_name = meta_getnmentbydev(set,
135 			    MD_SIDEWILD, minor_devt, NULL, NULL,
136 			    &key, &ep)) == NULL) {
137 				(void) close_admin(&ep);
138 				return (DEVFSADM_CONTINUE);
139 			}
140 
141 			if (set == 0) {
142 				/* this is a simple md */
143 				(void) snprintf(path, sizeof (path),
144 				"md/%s/%s", dir, basename(device_name));
145 			} else {
146 				/* this is a shared md */
147 				(void) snprintf(path, sizeof (path),
148 				"md/shared/%d/%s/%s", set, dir,
149 				basename(device_name));
150 			}
151 			(void) devfsadm_mklink(path, node, minor, 0);
152 			Free(device_name);
153 		}
154 	}
155 
156 	(void) close_admin(&ep);
157 	return (DEVFSADM_CONTINUE);
158 }
159