xref: /illumos-gate/usr/src/cmd/devfsadm/ramdisk_link.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
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 2002-2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <regex.h>
28 #include <devfsadm.h>
29 #include <stdio.h>
30 #include <strings.h>
31 #include <stdlib.h>
32 #include <limits.h>
33 #include <sys/stat.h>
34 #include <sys/mkdev.h>
35 #include <sys/ramdisk.h>
36 
37 static int ramdisk(di_minor_t di_minor, di_node_t node);
38 
39 /*
40  * devfs create callback register
41  */
42 static devfsadm_create_t ramdisk_create_cbt[] = {
43 	{ "pseudo", "ddi_pseudo", RD_DRIVER_NAME,
44 	    TYPE_EXACT | DRV_EXACT, ILEVEL_0, ramdisk,
45 	},
46 };
47 DEVFSADM_CREATE_INIT_V0(ramdisk_create_cbt);
48 
49 /*
50  * devfs cleanup register
51  */
52 #define	RAMDISK_LINK_RE	"^r?ramdisk/.+$"
53 
54 static devfsadm_remove_t ramdisk_remove_cbt[] = {
55 	{ "pseudo", RAMDISK_LINK_RE, RM_ALWAYS | RM_PRE | RM_HOT,
56 	    ILEVEL_0, devfsadm_rm_all},
57 };
58 DEVFSADM_REMOVE_INIT_V0(ramdisk_remove_cbt);
59 
60 static char *debug_mid = "ramdisk_mid";
61 
62 int
minor_init(void)63 minor_init(void)
64 {
65 	devfsadm_print(debug_mid, "ramdisk_link: minor_init\n");
66 	return (DEVFSADM_SUCCESS);
67 }
68 
69 int
minor_fini(void)70 minor_fini(void)
71 {
72 	devfsadm_print(debug_mid, "ramdisk_link: minor_fini\n");
73 	return (DEVFSADM_SUCCESS);
74 }
75 
76 /*
77  * This function is called for every ramdisk minor node.
78  * Calls enumerate to assign a logical ramdisk id, and then
79  * devfsadm_mklink to make the link.
80  *
81  * For pseudo ramdisk devices:
82  *
83  *	/dev/ramdiskctl      -> /devices/pseudo/ramdisk@0:ctl
84  *	/dev/ramdisk/<name>  -> /devices/pseudo/ramdisk@0:<name>
85  *	/dev/rramdisk/<name> -> /devices/pseudo/ramdisk@0:<name>,raw
86  *
87  * For OBP-created ramdisk devices:
88  *
89  *	/dev/ramdisk/<name>  -> /devices/ramdisk-<name>:a
90  *	/dev/rramdisk/<name> -> /devices/ramdisk-<name>:a,raw
91  */
92 static int
ramdisk(di_minor_t di_minor,di_node_t node)93 ramdisk(di_minor_t di_minor, di_node_t node)
94 {
95 	char	*name;
96 	char	devnm[MAXNAMELEN + 1];
97 	char	path[PATH_MAX];
98 
99 	/*
100 	 * If this is an OBP-created ramdisk use the node name, having first
101 	 * stripped the "ramdisk-" prefix.  For pseudo ramdisks use the minor
102 	 * name, having first stripped any ",raw" suffix.
103 	 */
104 	if (di_nodeid(node) == DI_PROM_NODEID) {
105 		RD_STRIP_PREFIX(name, di_node_name(node));
106 		(void) strlcpy(devnm, name, sizeof (devnm));
107 	} else {
108 		(void) strlcpy(devnm, di_minor_name(di_minor), sizeof (devnm));
109 		RD_STRIP_SUFFIX(devnm);
110 	}
111 
112 	if (strcmp(devnm, RD_CTL_NODE) == 0) {
113 		(void) devfsadm_mklink(RD_CTL_NAME, node, di_minor, 0);
114 	} else {
115 		/*
116 		 * Make the link in /dev/ramdisk or /dev/rramdisk.
117 		 */
118 		(void) snprintf(path, sizeof (path), "%s/%s",
119 		    di_minor_spectype(di_minor) == S_IFBLK ?
120 		    RD_BLOCK_NAME : RD_CHAR_NAME, devnm);
121 		(void) devfsadm_mklink(path, node, di_minor, 0);
122 	}
123 
124 	return (DEVFSADM_CONTINUE);
125 }
126