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