xref: /titanic_41/usr/src/cmd/devfsadm/md_link.c (revision 9dd828891378a0a6a509ab601b4c5c20ca5562ec)
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 #include <sdssc.h>
37 
38 #define	MD_LINK_RE_DEVICES	"^md/r?dsk/.+$"
39 #define	MD_LINK_RE_SHARED	"^md/shared/[0-9]+/r?dsk/.+$"
40 #define	MD_LINK_RE_ADMIN	"^md/admin"
41 
42 /*
43  * The devfsadm link module require the next section to
44  * be defined in order to know what and when to call functions
45  * in the module on device creation and removal.
46  */
47 
48 /* setup for device creation */
49 
50 static int md_create(di_minor_t minor, di_node_t node);
51 
52 static devfsadm_create_t md_cbt[] = {
53 	{ "pseudo", "ddi_pseudo", "md",
54 	    TYPE_EXACT | DRV_EXACT, ILEVEL_0, md_create,
55 	},
56 };
57 
58 DEVFSADM_CREATE_INIT_V0(md_cbt);
59 
60 /*
61  * remove devices - always allow disks to be dynamically removed. Only allow
62  *		    admin device to be removed at reboot.
63  */
64 
65 static devfsadm_remove_t md_remove_cbt[] = {
66 	{"pseudo", MD_LINK_RE_DEVICES, RM_ALWAYS | RM_PRE | RM_HOT,
67 	    ILEVEL_0, devfsadm_rm_all},
68 	{"pseudo", MD_LINK_RE_SHARED, RM_ALWAYS | RM_PRE | RM_HOT,
69 	    ILEVEL_0, devfsadm_rm_all},
70 	{"pseudo", MD_LINK_RE_ADMIN, RM_ALWAYS | RM_PRE,
71 	    ILEVEL_0, devfsadm_rm_all},
72 };
73 
74 DEVFSADM_REMOVE_INIT_V0(md_remove_cbt);
75 
76 
77 /*
78  * For the admin device:
79  *	/dev/md/admin -> /devices/pseudo/md@0:admin
80  *
81  * For metadevice:
82  *	/dev/md/dsk/foobar --> /devices/pseudo/md@0:0,100,blk
83  *	/dev/md/rdsk/foobar --> /devices/pseudo/md@0:0,100,raw
84  *
85  * Where 'foobar' is user specified arbitrary name and '100'
86  * is the minor number returned by MD_IOCMAKE_DEV ioctl
87  *
88  */
89 static int
90 md_create(di_minor_t minor, di_node_t node)
91 {
92 	char mn[MAXNAMELEN + 1];
93 	char path[PATH_MAX + 1];
94 	char set_path[PATH_MAX +1];
95 	char sym_path[PATH_MAX + 1];
96 	int set = -1, ret;
97 	char *type, *dir;
98 	char *device_name;
99 	dev_t minor_devt = di_minor_devt(minor);
100 	int key;
101 	mdsetname_t	*sp = NULL;
102 	md_error_t ep;
103 
104 	/*
105 	 * Initialize sdssc entry points. Don't worry about the return
106 	 * value here since the interface functions will get initialized
107 	 * correctly regardless.
108 	 */
109 	(void) sdssc_bind_library();
110 
111 	(void) strcpy(mn, di_minor_name(minor));
112 
113 	/*
114 	 * Check whether we are being requested to setup the admin
115 	 * device link or one of the metadevice links. They need
116 	 * to be treated differently.
117 	 */
118 
119 	if (strcmp(mn, "admin") == 0) {
120 		/* there is only one admin link and always in /dev/md/admin */
121 		(void) devfsadm_mklink("md/admin", node, minor, 0);
122 	} else {
123 		/*
124 		 * Extract out the minor components and create the
125 		 * appropriate links. The node looks like:
126 		 * md@<set>,<mdev>,<type>
127 		 * where the <set> number is the named diskset,
128 		 * <mdev> is the metadevice number, and <type>
129 		 * is the trailing "blk" or "raw" indication.
130 		 *
131 		 * NOTE: when <set> is non-zero, we need to create
132 		 * under the "shared" directory entry instead of linking
133 		 * into the top level dsk/rdsk directories.
134 		 */
135 		ret = sscanf(mn, "%d,", &set);
136 		if (ret == 1 && (type = strrchr(mn, ',')) != NULL) {
137 			type++;
138 			if (strcmp(type, "blk") == 0) {
139 				dir = "dsk";
140 			} else {
141 				dir = "rdsk";
142 			}
143 
144 			(void) memset(&ep, '\0', sizeof (ep));
145 			if ((device_name = meta_getnmentbydev(set,
146 			    MD_SIDEWILD, minor_devt, NULL, NULL,
147 			    &key, &ep)) == NULL) {
148 				(void) close_admin(&ep);
149 				return (DEVFSADM_CONTINUE);
150 			}
151 
152 			if (set == 0) {
153 				/* this is a simple md */
154 				(void) snprintf(path, sizeof (path),
155 				"md/%s/%s", dir, basename(device_name));
156 			} else {
157 				/* this is a shared md */
158 				(void) snprintf(path, sizeof (path),
159 				"md/shared/%d/%s/%s", set, dir,
160 				basename(device_name));
161 
162 				/*
163 				 * flush the caches so the next call to
164 				 * metasetnosetname will get us the
165 				 * updated cache.
166 				 */
167 				metaflushnames(0);
168 				if ((sp = metasetnosetname(set, &ep))
169 				    != NULL) {
170 					(void) snprintf(set_path,
171 					    sizeof (set_path), "md/shared/%d",
172 					    sp->setno);
173 					(void) snprintf(sym_path,
174 					    sizeof (sym_path), "md/%s",
175 					    sp->setname);
176 				}
177 			}
178 			(void) devfsadm_mklink(path, node, minor, 0);
179 			Free(device_name);
180 
181 			if (sp != NULL) {
182 				(void) devfsadm_secondary_link(sym_path,
183 				    set_path, 0);
184 			}
185 		}
186 	}
187 
188 	(void) close_admin(&ep);
189 	return (DEVFSADM_CONTINUE);
190 }
191