xref: /titanic_51/usr/src/cmd/devfsadm/zfs_link.c (revision 672986541be54a7a471bb088e60780c37e371d7e)
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 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
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/fs/zfs.h>
37 
38 /* zfs and zvol name info */
39 
40 #define	ZVOL_LINK_RE_DEVICES	"zvol/r?dsk/.*/.*$"
41 
42 static int zfs(di_minor_t minor, di_node_t node);
43 
44 /*
45  * devfs create callback register
46  */
47 static devfsadm_create_t zfs_create_cbt[] = {
48 	{ "pseudo", "ddi_pseudo", ZFS_DRIVER,
49 	    TYPE_EXACT | DRV_EXACT, ILEVEL_0, zfs,
50 	},
51 };
52 DEVFSADM_CREATE_INIT_V0(zfs_create_cbt);
53 
54 /*
55  * devfs cleanup register
56  */
57 static devfsadm_remove_t zfs_remove_cbt[] = {
58 	{ "pseudo", ZVOL_LINK_RE_DEVICES, RM_HOT | RM_POST,
59 	    ILEVEL_0, devfsadm_rm_all },
60 };
61 DEVFSADM_REMOVE_INIT_V0(zfs_remove_cbt);
62 
63 /*
64  * For the zfs control node:
65  *	/dev/zfs -> /devices/pseudo/zfs@0:zfs
66  * For zvols:
67  *	/dev/zvol/dsk/<pool>/<dataset> -> /devices/pseudo/zfs@0:1
68  *	/dev/zvol/rdsk/<pool>/<dataset> -> /devices/pseudo/zfs@0:1,raw
69  */
70 static int
71 zfs(di_minor_t minor, di_node_t node)
72 {
73 	dev_t	dev;
74 	int	err;
75 	char mn[MAXNAMELEN + 1];
76 	char blkname[MAXNAMELEN + 1];
77 	char rawname[MAXNAMELEN + 1];
78 	char path[PATH_MAX + 1];
79 	char *name;
80 
81 	(void) strcpy(mn, di_minor_name(minor));
82 
83 	if (strcmp(mn, ZFS_DRIVER) == 0) {
84 		(void) devfsadm_mklink(ZFS_DRIVER, node, minor, 0);
85 	} else {
86 		dev = di_minor_devt(minor);
87 		err = di_prop_lookup_strings(dev, node, ZVOL_PROP_NAME, &name);
88 		if (err < 0) {
89 			/* property not defined so can't do anything */
90 			return (DEVFSADM_CONTINUE);
91 		}
92 		(void) snprintf(blkname, sizeof (blkname), "%dc",
93 		    (int)minor(dev));
94 		(void) snprintf(rawname, sizeof (rawname), "%dc,raw",
95 		    (int)minor(dev));
96 
97 		/*
98 		 * This is where the actual public name gets constructed.
99 		 * Change the snprintf format to change the public
100 		 * path that gets constructed.
101 		 */
102 		if (strcmp(mn, blkname) == 0) {
103 			(void) snprintf(path, sizeof (path), "%s/%s",
104 			    ZVOL_DEV_DIR, name);
105 		} else if (strcmp(mn, rawname) == 0) {
106 			(void) snprintf(path, sizeof (path), "%s/%s",
107 			    ZVOL_RDEV_DIR, name);
108 		} else {
109 			return (DEVFSADM_CONTINUE);
110 		}
111 
112 		(void) devfsadm_mklink(path, node, minor, 0);
113 	}
114 	return (DEVFSADM_CONTINUE);
115 }
116