xref: /titanic_41/usr/src/cmd/devfsadm/lofi_link.c (revision ea8dc4b6d2251b437950c0056bc626b311c73c27)
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 (c) 1998-1999,2001 by Sun Microsystems, Inc.
24  * All rights reserved.
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 #include <sys/lofi.h>
37 
38 
39 static int lofi(di_minor_t minor, di_node_t node);
40 
41 /*
42  * devfs create callback register
43  */
44 static devfsadm_create_t lofi_create_cbt[] = {
45 	{ "pseudo", "ddi_pseudo", LOFI_DRIVER_NAME,
46 	    TYPE_EXACT | DRV_EXACT, ILEVEL_0, lofi,
47 	},
48 };
49 DEVFSADM_CREATE_INIT_V0(lofi_create_cbt);
50 
51 /*
52  * devfs cleanup register
53  */
54 static devfsadm_remove_t lofi_remove_cbt[] = {
55 	{"pseudo", "^r?lofi/[0-9]+$", RM_ALWAYS | RM_PRE | RM_HOT,
56 	    ILEVEL_0, devfsadm_rm_all},
57 };
58 DEVFSADM_REMOVE_INIT_V0(lofi_remove_cbt);
59 
60 
61 /*
62  * For the master device:
63  *	/dev/lofictl -> /devices/pseudo/lofi@0:ctl
64  * For each other device
65  *	/dev/lofi/1 -> /devices/pseudo/lofi@0:1
66  *	/dev/rlofi/1 -> /devices/pseudo/lofi@0:1,raw
67  */
68 static int
69 lofi(di_minor_t minor, di_node_t node)
70 {
71 	dev_t	dev;
72 	char mn[MAXNAMELEN + 1];
73 	char blkname[MAXNAMELEN + 1];
74 	char rawname[MAXNAMELEN + 1];
75 	char path[PATH_MAX + 1];
76 
77 	(void) strcpy(mn, di_minor_name(minor));
78 
79 	if (strcmp(mn, "ctl") == 0) {
80 		(void) devfsadm_mklink(LOFI_CTL_NAME, node, minor, 0);
81 	} else {
82 		dev = di_minor_devt(minor);
83 		(void) snprintf(blkname, sizeof (blkname), "%d",
84 		    (int)minor(dev));
85 		(void) snprintf(rawname, sizeof (rawname), "%d,raw",
86 		    (int)minor(dev));
87 
88 		if (strcmp(mn, blkname) == 0) {
89 			(void) snprintf(path, sizeof (path), "%s/%s",
90 			    LOFI_BLOCK_NAME, blkname);
91 		} else if (strcmp(mn, rawname) == 0) {
92 			(void) snprintf(path, sizeof (path), "%s/%s",
93 			    LOFI_CHAR_NAME, blkname);
94 		} else {
95 			return (DEVFSADM_CONTINUE);
96 		}
97 
98 		(void) devfsadm_mklink(path, node, minor, 0);
99 	}
100 	return (DEVFSADM_CONTINUE);
101 }
102