Home | History | Annotate | Line # | Download | only in rumpvfs
devnodes.c revision 1.6
      1 /*	$NetBSD: devnodes.c,v 1.6 2011/02/10 11:00:45 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.6 2011/02/10 11:00:45 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 int
     44 rump_vfs_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 0;
     56 }
     57 
     58 int
     59 rump_vfs_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, devmajor_t blk, devmajor_t chr, devminor_t dmin,
    114 	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, MAXPATHLEN, "/dev/%s%s%s", base, cstr1, cstr2);
    133 	if (blk != NODEV) {
    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 		sprintf(buf, "/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 	for (i = 0; i < dcvecsize; i++) {
    179 		dc = &dcvec[i];
    180 
    181 		switch (dc->d_class) {
    182 		case DEVNODE_DONTBOTHER:
    183 			break;
    184 		case DEVNODE_SINGLE:
    185 			if (dc->d_flags & DEVNODE_FLAG_ISMINOR0) {
    186 				themin = dc->d_vectdim[0];
    187 			} else {
    188 				themin = 0;
    189 			}
    190 			makeonenode(pnbuf,
    191 			    dc->d_bmajor, dc->d_cmajor, themin,
    192 			    dc->d_name, -1, -1);
    193 			break;
    194 		case DEVNODE_VECTOR:
    195 			for (v1 = 0; v1 < dc->d_vectdim[0]; v1++) {
    196 				if (dc->d_vectdim[1] == 0) {
    197 					makeonenode(pnbuf,
    198 					    dc->d_bmajor, dc->d_cmajor,
    199 					    v1, dc->d_name, v1, -1);
    200 				} else {
    201 					for (v2 = 0;
    202 					    v2 < dc->d_vectdim[1]; v2++) {
    203 						makeonenode(pnbuf,
    204 						    dc->d_bmajor, dc->d_cmajor,
    205 						    v1 * dc->d_vectdim[1] + v2,
    206 						    dc->d_name, v1, v2);
    207 					}
    208 				}
    209 			}
    210 
    211 			/* add some extra sanity checks here */
    212 			if (dc->d_flags & DEVNODE_FLAG_LINKZERO) {
    213 				/*
    214 				 * ok, so we cheat a bit since
    215 				 * symlink isn't supported on rumpfs ...
    216 				 */
    217 				makeonenode(pnbuf, -1, dc->d_cmajor, 0,
    218 				    dc->d_name, -1, -1);
    219 
    220 			}
    221 			break;
    222 		}
    223 	}
    224 
    225 	PNBUF_PUT(pnbuf);
    226 }
    227