xref: /illumos-gate/usr/src/cmd/devfsadm/lofi_link.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 <regex.h>
29 #include <devfsadm.h>
30 #include <stdio.h>
31 #include <strings.h>
32 #include <stdlib.h>
33 #include <limits.h>
34 #include <sys/mkdev.h>
35 #include <sys/lofi.h>
36 
37 
38 static int lofi(di_minor_t minor, di_node_t node);
39 static int lofi_rm_all(char *link);
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_V1_t lofi_remove_cbt[] = {
55 	{"pseudo", "^r?lofi/[0-9]+$", RM_ALWAYS | RM_PRE | RM_HOT,
56 	    ILEVEL_0, lofi_rm_all},
57 };
58 DEVFSADM_REMOVE_INIT_V1(lofi_remove_cbt);
59 
60 /*
61  * Wrapper around devfsadm_rm_all() that allows termination of remove
62  * process
63  */
64 static int
65 lofi_rm_all(char *link)
66 {
67 	devfsadm_rm_all(link);
68 	return (DEVFSADM_TERMINATE);
69 }
70 
71 
72 /*
73  * For the master device:
74  *	/dev/lofictl -> /devices/pseudo/lofi@0:ctl
75  * For each other device
76  *	/dev/lofi/1 -> /devices/pseudo/lofi@0:1
77  *	/dev/rlofi/1 -> /devices/pseudo/lofi@0:1,raw
78  */
79 static int
80 lofi(di_minor_t minor, di_node_t node)
81 {
82 	dev_t	dev;
83 	char mn[MAXNAMELEN + 1];
84 	char blkname[MAXNAMELEN + 1];
85 	char rawname[MAXNAMELEN + 1];
86 	char path[PATH_MAX + 1];
87 
88 	(void) strcpy(mn, di_minor_name(minor));
89 
90 	if (strcmp(mn, "ctl") == 0) {
91 		(void) devfsadm_mklink(LOFI_CTL_NAME, node, minor, 0);
92 	} else {
93 		dev = di_minor_devt(minor);
94 		(void) snprintf(blkname, sizeof (blkname), "%d",
95 		    (int)minor(dev));
96 		(void) snprintf(rawname, sizeof (rawname), "%d,raw",
97 		    (int)minor(dev));
98 
99 		if (strcmp(mn, blkname) == 0) {
100 			(void) snprintf(path, sizeof (path), "%s/%s",
101 			    LOFI_BLOCK_NAME, blkname);
102 		} else if (strcmp(mn, rawname) == 0) {
103 			(void) snprintf(path, sizeof (path), "%s/%s",
104 			    LOFI_CHAR_NAME, blkname);
105 		} else {
106 			return (DEVFSADM_CONTINUE);
107 		}
108 
109 		(void) devfsadm_mklink(path, node, minor, 0);
110 	}
111 	return (DEVFSADM_CONTINUE);
112 }
113