xref: /netbsd-src/sys/rump/librump/rumpvfs/devnodes.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: devnodes.c,v 1.10 2014/06/20 11:27:25 pooka Exp $	*/
2 
3 /*
4  * Copyright (c) 2009 Antti Kantee.  All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: devnodes.c,v 1.10 2014/06/20 11:27:25 pooka Exp $");
30 
31 #include <sys/param.h>
32 #include <sys/device.h>
33 #include <sys/filedesc.h>
34 #include <sys/kmem.h>
35 #include <sys/lwp.h>
36 #include <sys/namei.h>
37 #include <sys/stat.h>
38 #include <sys/vfs_syscalls.h>
39 
40 #include "rump_vfs_private.h"
41 
42 /* realqvik(tm) "devfs" */
43 static int
44 makeonedevnode(dev_t devtype, const char *devname,
45 	devmajor_t majnum, devminor_t minnum)
46 {
47 	register_t retval;
48 	int error;
49 
50 	error = do_sys_mknod(curlwp, devname, 0666 | devtype,
51 	    makedev(majnum, minnum), &retval, UIO_SYSSPACE);
52 	if (error == EEXIST) /* XXX: should check it's actually the same */
53 		error = 0;
54 
55 	return error;
56 }
57 
58 static int
59 makedevnodes(dev_t devtype, const char *basename, char minchar,
60 	devmajor_t maj, devminor_t minnum, int nnodes)
61 {
62 	int error = 0;
63 	char *devname, *p;
64 	size_t devlen;
65 	register_t retval;
66 
67 	devlen = strlen(basename) + 1 + 1; /* +letter +0 */
68 	devname = kmem_zalloc(devlen, KM_SLEEP);
69 	strlcpy(devname, basename, devlen);
70 	p = devname + devlen-2;
71 
72 	for (; nnodes > 0; nnodes--, minchar++, minnum++) {
73 		KASSERT(minchar <= 'z');
74 		*p = minchar;
75 
76 		if ((error = do_sys_mknod(curlwp, devname, 0666 | devtype,
77 		    makedev(maj, minnum), &retval, UIO_SYSSPACE))) {
78 			if (error == EEXIST)
79 				error = 0;
80 			else
81 				goto out;
82 		}
83 	}
84 
85  out:
86 	kmem_free(devname, devlen);
87 	return error;
88 }
89 
90 enum { NOTEXIST, SAME, DIFFERENT };
91 static int
92 doesitexist(const char *path, bool isblk, devmajor_t dmaj, devminor_t dmin)
93 {
94 	struct stat sb;
95 	int error;
96 
97 	error = do_sys_stat(path, 0, &sb);
98 	/* even if not ENOENT, we might be able to create it */
99 	if (error)
100 		return NOTEXIST;
101 
102 	if (major(sb.st_rdev) != dmaj || minor(sb.st_rdev) != dmin)
103 		return DIFFERENT;
104 	if (isblk && !S_ISBLK(sb.st_mode))
105 		return DIFFERENT;
106 	if (!isblk && !S_ISCHR(sb.st_mode))
107 		return DIFFERENT;
108 
109 	return SAME;
110 }
111 
112 static void
113 makeonenode(char *buf, size_t len, devmajor_t blk, devmajor_t chr,
114     devminor_t dmin, const char *base, int c1, int c2)
115 {
116 	char cstr1[2] = {0,0}, cstr2[2] = {0,0};
117 	register_t rv;
118 	int error;
119 
120 	if (c1 != -1) {
121 		cstr1[0] = '0' + c1;
122 		cstr1[1] = '\0';
123 	}
124 
125 	if (c2 != -1) {
126 		cstr2[0] = 'a' + c2;
127 		cstr2[1] = '\0';
128 
129 	}
130 
131 	/* block device */
132 	snprintf(buf, len, "/dev/%s%s%s", base, cstr1, cstr2);
133 	if (blk != NODEVMAJOR) {
134 		switch (doesitexist(buf, true, blk, dmin)) {
135 		case DIFFERENT:
136 			aprint_verbose("mkdevnodes: block device %s "
137 			    "already exists\n", buf);
138 			break;
139 		case NOTEXIST:
140 			if ((error = do_sys_mknod(curlwp, buf, 0600 | S_IFBLK,
141 			    makedev(blk, dmin), &rv, UIO_SYSSPACE)) != 0)
142 				aprint_verbose("mkdevnodes: failed to "
143 				    "create %s: %d\n", buf, error);
144 			break;
145 		case SAME:
146 			/* done */
147 			break;
148 		}
149 		snprintf(buf, len, "/dev/r%s%s%s", base, cstr1, cstr2);
150 	}
151 
152 	switch (doesitexist(buf, true, chr, dmin)) {
153 	case DIFFERENT:
154 		aprint_verbose("mkdevnodes: character device %s "
155 		    "already exists\n", buf);
156 		break;
157 	case NOTEXIST:
158 		if ((error = do_sys_mknod(curlwp, buf, 0600 | S_IFCHR,
159 		    makedev(chr, dmin), &rv, UIO_SYSSPACE)) != 0)
160 			aprint_verbose("mkdevnodes: failed to "
161 			    "create %s: %d\n", buf, error);
162 		break;
163 	case SAME:
164 		/* yeehaa */
165 		break;
166 	}
167 }
168 
169 void
170 rump_vfs_builddevs(struct devsw_conv *dcvec, size_t dcvecsize)
171 {
172 	char *pnbuf = PNBUF_GET();
173 	devminor_t themin;
174 	struct devsw_conv *dc;
175 	size_t i;
176 	int v1, v2;
177 
178 	rump_vfs_makeonedevnode = makeonedevnode;
179 	rump_vfs_makedevnodes = makedevnodes;
180 
181 	for (i = 0; i < dcvecsize; i++) {
182 		dc = &dcvec[i];
183 
184 		switch (dc->d_class) {
185 		case DEVNODE_DONTBOTHER:
186 			break;
187 		case DEVNODE_SINGLE:
188 			if (dc->d_flags & DEVNODE_FLAG_ISMINOR0) {
189 				themin = dc->d_vectdim[0];
190 			} else {
191 				themin = 0;
192 			}
193 			makeonenode(pnbuf, MAXPATHLEN,
194 			    dc->d_bmajor, dc->d_cmajor, themin,
195 			    dc->d_name, -1, -1);
196 			break;
197 		case DEVNODE_VECTOR:
198 			for (v1 = 0; v1 < dc->d_vectdim[0]; v1++) {
199 				if (dc->d_vectdim[1] == 0) {
200 					makeonenode(pnbuf, MAXPATHLEN,
201 					    dc->d_bmajor, dc->d_cmajor,
202 					    v1, dc->d_name, v1, -1);
203 				} else {
204 					for (v2 = 0;
205 					    v2 < dc->d_vectdim[1]; v2++) {
206 						makeonenode(pnbuf, MAXPATHLEN,
207 						    dc->d_bmajor, dc->d_cmajor,
208 						    v1 * dc->d_vectdim[1] + v2,
209 						    dc->d_name, v1, v2);
210 					}
211 				}
212 			}
213 
214 			/* add some extra sanity checks here */
215 			if (dc->d_flags & DEVNODE_FLAG_LINKZERO) {
216 				/*
217 				 * ok, so we cheat a bit since
218 				 * symlink isn't supported on rumpfs ...
219 				 */
220 				makeonenode(pnbuf, MAXPATHLEN,
221 				    -1, dc->d_cmajor, 0, dc->d_name, -1, -1);
222 
223 			}
224 			break;
225 		}
226 	}
227 
228 	PNBUF_PUT(pnbuf);
229 }
230