Home | History | Annotate | Line # | Download | only in libukfs
ukfs.c revision 1.38
      1  1.38     pooka /*	$NetBSD: ukfs.c,v 1.38 2009/10/07 20:51:00 pooka Exp $	*/
      2   1.1     pooka 
      3   1.1     pooka /*
      4  1.38     pooka  * Copyright (c) 2007, 2008, 2009  Antti Kantee.  All Rights Reserved.
      5   1.1     pooka  *
      6   1.1     pooka  * Development of this software was supported by the
      7   1.1     pooka  * Finnish Cultural Foundation.
      8   1.1     pooka  *
      9   1.1     pooka  * Redistribution and use in source and binary forms, with or without
     10   1.1     pooka  * modification, are permitted provided that the following conditions
     11   1.1     pooka  * are met:
     12   1.1     pooka  * 1. Redistributions of source code must retain the above copyright
     13   1.1     pooka  *    notice, this list of conditions and the following disclaimer.
     14   1.1     pooka  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1     pooka  *    notice, this list of conditions and the following disclaimer in the
     16   1.1     pooka  *    documentation and/or other materials provided with the distribution.
     17   1.1     pooka  *
     18   1.1     pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19   1.1     pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20   1.1     pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21   1.1     pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22   1.1     pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23   1.1     pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24   1.1     pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25   1.1     pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26   1.1     pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27   1.1     pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28   1.1     pooka  * SUCH DAMAGE.
     29   1.1     pooka  */
     30   1.1     pooka 
     31   1.1     pooka /*
     32   1.1     pooka  * This library enables access to files systems directly without
     33   1.1     pooka  * involving system calls.
     34   1.1     pooka  */
     35   1.1     pooka 
     36   1.1     pooka #ifdef __linux__
     37   1.1     pooka #define _XOPEN_SOURCE 500
     38   1.1     pooka #define _BSD_SOURCE
     39   1.1     pooka #define _FILE_OFFSET_BITS 64
     40   1.1     pooka #endif
     41   1.1     pooka 
     42   1.3     pooka #include <sys/param.h>
     43   1.3     pooka #include <sys/queue.h>
     44   1.1     pooka #include <sys/stat.h>
     45   1.4     pooka #include <sys/sysctl.h>
     46   1.4     pooka #include <sys/mount.h>
     47   1.1     pooka 
     48   1.1     pooka #include <assert.h>
     49   1.3     pooka #include <dirent.h>
     50   1.3     pooka #include <dlfcn.h>
     51   1.1     pooka #include <err.h>
     52   1.1     pooka #include <errno.h>
     53  1.11     pooka #include <fcntl.h>
     54   1.1     pooka #include <pthread.h>
     55   1.1     pooka #include <stdio.h>
     56   1.1     pooka #include <stdlib.h>
     57   1.1     pooka #include <string.h>
     58   1.1     pooka #include <unistd.h>
     59   1.1     pooka #include <stdint.h>
     60   1.1     pooka 
     61   1.1     pooka #include <rump/ukfs.h>
     62   1.1     pooka 
     63   1.1     pooka #include <rump/rump.h>
     64   1.1     pooka #include <rump/rump_syscalls.h>
     65   1.1     pooka 
     66  1.38     pooka #include "ukfs_int_disklabel.h"
     67  1.38     pooka 
     68   1.1     pooka #define UKFS_MODE_DEFAULT 0555
     69   1.1     pooka 
     70   1.1     pooka struct ukfs {
     71   1.1     pooka 	struct mount *ukfs_mp;
     72   1.1     pooka 	struct vnode *ukfs_rvp;
     73  1.37     pooka 	void *ukfs_specific;
     74   1.1     pooka 
     75   1.1     pooka 	pthread_spinlock_t ukfs_spin;
     76   1.1     pooka 	pid_t ukfs_nextpid;
     77   1.1     pooka 	struct vnode *ukfs_cdir;
     78  1.11     pooka 	int ukfs_devfd;
     79  1.30     pooka 	char *ukfs_devpath;
     80  1.30     pooka 	char *ukfs_mountpath;
     81   1.1     pooka };
     82   1.1     pooka 
     83  1.30     pooka static int builddirs(const char *, mode_t,
     84  1.30     pooka     int (*mkdirfn)(struct ukfs *, const char *, mode_t), struct ukfs *);
     85  1.30     pooka 
     86   1.1     pooka struct mount *
     87   1.1     pooka ukfs_getmp(struct ukfs *ukfs)
     88   1.1     pooka {
     89   1.1     pooka 
     90   1.1     pooka 	return ukfs->ukfs_mp;
     91   1.1     pooka }
     92   1.1     pooka 
     93   1.1     pooka struct vnode *
     94   1.1     pooka ukfs_getrvp(struct ukfs *ukfs)
     95   1.1     pooka {
     96   1.1     pooka 	struct vnode *rvp;
     97   1.1     pooka 
     98   1.1     pooka 	rvp = ukfs->ukfs_rvp;
     99   1.1     pooka 	rump_vp_incref(rvp);
    100   1.1     pooka 
    101   1.1     pooka 	return rvp;
    102   1.1     pooka }
    103   1.1     pooka 
    104  1.37     pooka void
    105  1.37     pooka ukfs_setspecific(struct ukfs *ukfs, void *priv)
    106  1.37     pooka {
    107  1.37     pooka 
    108  1.37     pooka 	ukfs->ukfs_specific = priv;
    109  1.37     pooka }
    110  1.37     pooka 
    111  1.37     pooka void *
    112  1.37     pooka ukfs_getspecific(struct ukfs *ukfs)
    113  1.37     pooka {
    114  1.37     pooka 
    115  1.37     pooka 	return ukfs->ukfs_specific;
    116  1.37     pooka }
    117  1.37     pooka 
    118  1.20     pooka #ifdef DONT_WANT_PTHREAD_LINKAGE
    119  1.20     pooka #define pthread_spin_lock(a)
    120  1.20     pooka #define pthread_spin_unlock(a)
    121  1.20     pooka #define pthread_spin_init(a,b)
    122  1.20     pooka #define pthread_spin_destroy(a)
    123  1.20     pooka #endif
    124  1.20     pooka 
    125   1.1     pooka static pid_t
    126   1.1     pooka nextpid(struct ukfs *ukfs)
    127   1.1     pooka {
    128   1.1     pooka 	pid_t npid;
    129   1.1     pooka 
    130   1.1     pooka 	pthread_spin_lock(&ukfs->ukfs_spin);
    131   1.5     pooka 	if (ukfs->ukfs_nextpid == 0)
    132   1.5     pooka 		ukfs->ukfs_nextpid++;
    133   1.1     pooka 	npid = ukfs->ukfs_nextpid++;
    134   1.1     pooka 	pthread_spin_unlock(&ukfs->ukfs_spin);
    135   1.1     pooka 
    136   1.1     pooka 	return npid;
    137   1.1     pooka }
    138   1.1     pooka 
    139   1.1     pooka static void
    140   1.1     pooka precall(struct ukfs *ukfs)
    141   1.1     pooka {
    142   1.1     pooka 	struct vnode *rvp, *cvp;
    143   1.1     pooka 
    144   1.1     pooka 	rump_setup_curlwp(nextpid(ukfs), 1, 1);
    145   1.1     pooka 	rvp = ukfs_getrvp(ukfs);
    146   1.1     pooka 	pthread_spin_lock(&ukfs->ukfs_spin);
    147   1.1     pooka 	cvp = ukfs->ukfs_cdir;
    148   1.1     pooka 	pthread_spin_unlock(&ukfs->ukfs_spin);
    149   1.1     pooka 	rump_rcvp_set(rvp, cvp); /* takes refs */
    150   1.1     pooka 	rump_vp_rele(rvp);
    151   1.1     pooka }
    152   1.1     pooka 
    153   1.1     pooka static void
    154   1.1     pooka postcall(struct ukfs *ukfs)
    155   1.1     pooka {
    156   1.1     pooka 	struct vnode *rvp;
    157   1.1     pooka 
    158   1.1     pooka 	rvp = ukfs_getrvp(ukfs);
    159   1.1     pooka 	rump_rcvp_set(NULL, rvp);
    160   1.1     pooka 	rump_vp_rele(rvp);
    161   1.1     pooka 	rump_clear_curlwp();
    162   1.1     pooka }
    163   1.1     pooka 
    164   1.1     pooka int
    165  1.10     pooka _ukfs_init(int version)
    166   1.1     pooka {
    167  1.10     pooka 	int rv;
    168  1.10     pooka 
    169  1.10     pooka 	if (version != UKFS_VERSION) {
    170  1.10     pooka 		printf("incompatible ukfs version, %d vs. %d\n",
    171  1.10     pooka 		    version, UKFS_VERSION);
    172  1.10     pooka 		errno = EPROGMISMATCH;
    173  1.10     pooka 		return -1;
    174  1.10     pooka 	}
    175   1.1     pooka 
    176  1.10     pooka 	if ((rv = rump_init()) != 0) {
    177  1.10     pooka 		errno = rv;
    178  1.10     pooka 		return -1;
    179  1.10     pooka 	}
    180   1.1     pooka 
    181   1.1     pooka 	return 0;
    182   1.1     pooka }
    183   1.1     pooka 
    184  1.31     pooka /*ARGSUSED*/
    185  1.30     pooka static int
    186  1.30     pooka rumpmkdir(struct ukfs *dummy, const char *path, mode_t mode)
    187  1.30     pooka {
    188  1.30     pooka 
    189  1.30     pooka 	return rump_sys_mkdir(path, mode);
    190  1.30     pooka }
    191  1.30     pooka 
    192  1.38     pooka int
    193  1.38     pooka ukfs_partition_probe(char *devpath, int *partition)
    194  1.38     pooka {
    195  1.38     pooka 	char *p;
    196  1.38     pooka 	int rv = 0;
    197  1.38     pooka 
    198  1.38     pooka 	/*
    199  1.38     pooka 	 * Check for disklabel magic in pathname:
    200  1.38     pooka 	 * /regularpath%PART:<char>%\0
    201  1.38     pooka 	 */
    202  1.38     pooka #define MAGICADJ(p, n) (p+sizeof(UKFS_PARTITION_SCANMAGIC)-1+n)
    203  1.38     pooka 	if ((p = strstr(devpath, UKFS_PARTITION_SCANMAGIC)) != NULL
    204  1.38     pooka 	    && strlen(p) == UKFS_PARTITION_MAGICLEN
    205  1.38     pooka 	    && *(MAGICADJ(p,1)) == '%') {
    206  1.38     pooka 		if (*(MAGICADJ(p,0)) >= 'a' &&
    207  1.38     pooka 		    *(MAGICADJ(p,0)) < 'a' + UKFS_MAXPARTITIONS) {
    208  1.38     pooka 			*partition = *(MAGICADJ(p,0)) - 'a';
    209  1.38     pooka 			*p = '\0';
    210  1.38     pooka 		} else {
    211  1.38     pooka 			rv = EINVAL;
    212  1.38     pooka 		}
    213  1.38     pooka 	} else {
    214  1.38     pooka 		*partition = UKFS_PARTITION_NONE;
    215  1.38     pooka 	}
    216  1.38     pooka 
    217  1.38     pooka 	return rv;
    218  1.38     pooka }
    219  1.38     pooka 
    220  1.38     pooka /*
    221  1.38     pooka  * Open the disk file and flock it.  Also, if we are operation on
    222  1.38     pooka  * an embedded partition, find the partition offset and size from
    223  1.38     pooka  * the disklabel.
    224  1.38     pooka  *
    225  1.38     pooka  * We hard-fail only in two cases:
    226  1.38     pooka  *  1) we failed to get the partition info out (don't know what offset
    227  1.38     pooka  *     to mount from)
    228  1.38     pooka  *  2) we failed to flock the source device (i.e. flock() fails,
    229  1.38     pooka  *     not e.g. open() before it)
    230  1.38     pooka  *
    231  1.38     pooka  * Otherwise we let the code proceed to mount and let the file system
    232  1.38     pooka  * throw the proper error.  The only questionable bit is that if we
    233  1.38     pooka  * soft-fail before flock() and mount does succeed...
    234  1.38     pooka  *
    235  1.38     pooka  * Returns: -1 error (errno reports error code)
    236  1.38     pooka  *           0 success
    237  1.38     pooka  *
    238  1.38     pooka  * dfdp: -1  device is not open
    239  1.38     pooka  *        n  device is open
    240  1.38     pooka  */
    241  1.38     pooka static int
    242  1.38     pooka process_diskdevice(const char *devpath, int partition, int rdonly,
    243  1.38     pooka 	int *dfdp, uint64_t *devoff, uint64_t *devsize)
    244   1.1     pooka {
    245  1.38     pooka 	char buf[65536];
    246  1.22     pooka 	struct stat sb;
    247  1.38     pooka 	struct ukfs_disklabel dl;
    248  1.38     pooka 	struct ukfs_partition *pp;
    249  1.38     pooka 	int rv = 0, devfd;
    250  1.38     pooka 
    251  1.38     pooka 	/* defaults */
    252  1.38     pooka 	*devoff = 0;
    253  1.38     pooka 	*devsize = RUMP_ETFS_SIZE_ENDOFF;
    254  1.38     pooka 	*dfdp = -1;
    255  1.38     pooka 
    256  1.38     pooka 	devfd = open(devpath, rdonly ? O_RDONLY : O_RDWR);
    257  1.38     pooka 	if (devfd == -1) {
    258  1.38     pooka 		if (UKFS_USEPARTITION(partition))
    259  1.38     pooka 			rv = errno;
    260  1.38     pooka 		goto out;
    261  1.38     pooka 	}
    262  1.38     pooka 
    263  1.38     pooka 	/*
    264  1.38     pooka 	 * Locate the disklabel and find the partition in question.
    265  1.38     pooka 	 */
    266  1.38     pooka 	if (UKFS_USEPARTITION(partition)) {
    267  1.38     pooka 		if (pread(devfd, buf, sizeof(buf), 0) == -1) {
    268  1.38     pooka 			rv = errno;
    269  1.38     pooka 			goto out;
    270  1.38     pooka 		}
    271  1.38     pooka 
    272  1.38     pooka 		if (ukfs_disklabel_scan(&dl, buf, sizeof(buf)) != 0) {
    273  1.38     pooka 			rv = ENOENT;
    274  1.38     pooka 			goto out;
    275  1.38     pooka 		}
    276  1.38     pooka 
    277  1.38     pooka 		if (dl.d_npartitions < partition) {
    278  1.38     pooka 			rv = ENOENT;
    279  1.38     pooka 			goto out;
    280  1.38     pooka 		}
    281  1.38     pooka 
    282  1.38     pooka 		pp = &dl.d_partitions[partition];
    283  1.38     pooka 		*devoff = pp->p_offset << DEV_BSHIFT;
    284  1.38     pooka 		*devsize = pp->p_size << DEV_BSHIFT;
    285  1.38     pooka 	}
    286  1.38     pooka 
    287  1.38     pooka 	if (fstat(devfd, &sb) == -1) {
    288  1.38     pooka 		rv = errno;
    289  1.38     pooka 		goto out;
    290  1.38     pooka 	}
    291   1.1     pooka 
    292  1.11     pooka 	/*
    293  1.38     pooka 	 * We do this only for non-block device since the
    294  1.38     pooka 	 * (NetBSD) kernel allows block device open only once.
    295  1.38     pooka 	 * We also need to close the device for fairly obvious reasons.
    296  1.11     pooka 	 */
    297  1.38     pooka 	if (!S_ISBLK(sb.st_mode)) {
    298  1.38     pooka 		if (flock(devfd, LOCK_NB | (rdonly ? LOCK_SH:LOCK_EX)) == -1) {
    299  1.38     pooka 			warnx("ukfs_mount: cannot get %s lock on "
    300  1.38     pooka 			    "device", rdonly ? "shared" : "exclusive");
    301  1.11     pooka 			rv = errno;
    302  1.11     pooka 			goto out;
    303  1.11     pooka 		}
    304  1.38     pooka 	} else {
    305  1.38     pooka 		close(devfd);
    306  1.38     pooka 		devfd = -1;
    307  1.38     pooka 	}
    308  1.38     pooka 	*dfdp = devfd;
    309  1.22     pooka 
    310  1.38     pooka  out:
    311  1.38     pooka 	if (rv) {
    312  1.38     pooka 		if (devfd != -1)
    313  1.22     pooka 			close(devfd);
    314  1.38     pooka 		errno = rv;
    315  1.38     pooka 		rv = -1;
    316  1.11     pooka 	}
    317   1.1     pooka 
    318  1.38     pooka 	return rv;
    319  1.38     pooka }
    320  1.38     pooka 
    321  1.38     pooka static struct ukfs *
    322  1.38     pooka doukfsmount(const char *vfsname, const char *devpath, int partition,
    323  1.38     pooka 	const char *mountpath, int mntflags, void *arg, size_t alen)
    324  1.38     pooka {
    325  1.38     pooka 	struct ukfs *fs = NULL;
    326  1.38     pooka 	int rv = 0, devfd;
    327  1.38     pooka 	uint64_t devoff, devsize;
    328  1.38     pooka 	int mounted = 0;
    329  1.38     pooka 	int regged = 0;
    330  1.38     pooka 
    331  1.38     pooka 	if (partition != UKFS_PARTITION_NA)
    332  1.38     pooka 		process_diskdevice(devpath, partition, mntflags & MNT_RDONLY,
    333  1.38     pooka 		    &devfd, &devoff, &devsize);
    334  1.38     pooka 
    335   1.1     pooka 	fs = malloc(sizeof(struct ukfs));
    336   1.1     pooka 	if (fs == NULL) {
    337   1.1     pooka 		rv = ENOMEM;
    338   1.1     pooka 		goto out;
    339   1.1     pooka 	}
    340   1.1     pooka 	memset(fs, 0, sizeof(struct ukfs));
    341  1.30     pooka 
    342  1.30     pooka 	/* create our mountpoint.  this is never removed. */
    343  1.30     pooka 	if (builddirs(mountpath, 0777, rumpmkdir, NULL) == -1) {
    344  1.30     pooka 		if (errno != EEXIST) {
    345  1.30     pooka 			rv = errno;
    346  1.30     pooka 			goto out;
    347  1.30     pooka 		}
    348  1.30     pooka 	}
    349   1.1     pooka 
    350  1.38     pooka 	if (partition != UKFS_PARTITION_NA) {
    351  1.38     pooka 		rv = rump_etfs_register_withsize(devpath, devpath,
    352  1.38     pooka 		    RUMP_ETFS_BLK, devoff, devsize);
    353  1.33     pooka 		if (rv) {
    354  1.33     pooka 			goto out;
    355  1.33     pooka 		}
    356  1.33     pooka 		regged = 1;
    357  1.33     pooka 	}
    358  1.38     pooka 
    359  1.30     pooka 	rv = rump_sys_mount(vfsname, mountpath, mntflags, arg, alen);
    360   1.1     pooka 	if (rv) {
    361  1.34     pooka 		rv = errno;
    362   1.1     pooka 		goto out;
    363   1.1     pooka 	}
    364  1.30     pooka 	mounted = 1;
    365  1.30     pooka 	rv = rump_vfs_getmp(mountpath, &fs->ukfs_mp);
    366  1.11     pooka 	if (rv) {
    367  1.11     pooka 		goto out;
    368  1.11     pooka 	}
    369  1.30     pooka 	rv = rump_vfs_root(fs->ukfs_mp, &fs->ukfs_rvp, 0);
    370  1.30     pooka 	if (rv) {
    371  1.30     pooka 		goto out;
    372  1.30     pooka 	}
    373  1.30     pooka 
    374  1.33     pooka 	if (regged) {
    375  1.33     pooka 		fs->ukfs_devpath = strdup(devpath);
    376  1.33     pooka 	}
    377  1.30     pooka 	fs->ukfs_mountpath = strdup(mountpath);
    378  1.11     pooka 	fs->ukfs_cdir = ukfs_getrvp(fs);
    379  1.11     pooka 	pthread_spin_init(&fs->ukfs_spin, PTHREAD_PROCESS_SHARED);
    380  1.11     pooka 	fs->ukfs_devfd = devfd;
    381  1.11     pooka 	assert(rv == 0);
    382   1.1     pooka 
    383   1.1     pooka  out:
    384   1.1     pooka 	if (rv) {
    385  1.30     pooka 		if (fs) {
    386  1.30     pooka 			if (fs->ukfs_rvp)
    387  1.30     pooka 				rump_vp_rele(fs->ukfs_rvp);
    388   1.1     pooka 			free(fs);
    389  1.30     pooka 			fs = NULL;
    390  1.30     pooka 		}
    391  1.30     pooka 		if (mounted)
    392  1.30     pooka 			rump_sys_unmount(mountpath, MNT_FORCE);
    393  1.33     pooka 		if (regged)
    394  1.33     pooka 			rump_etfs_remove(devpath);
    395  1.11     pooka 		if (devfd != -1) {
    396  1.11     pooka 			flock(devfd, LOCK_UN);
    397  1.11     pooka 			close(devfd);
    398  1.11     pooka 		}
    399  1.34     pooka 		errno = rv;
    400   1.1     pooka 	}
    401   1.1     pooka 
    402   1.1     pooka 	return fs;
    403   1.1     pooka }
    404   1.1     pooka 
    405  1.38     pooka struct ukfs *
    406  1.38     pooka ukfs_mount(const char *vfsname, const char *devpath,
    407  1.38     pooka 	const char *mountpath, int mntflags, void *arg, size_t alen)
    408  1.38     pooka {
    409  1.38     pooka 
    410  1.38     pooka 	return doukfsmount(vfsname, devpath, UKFS_PARTITION_NA,
    411  1.38     pooka 	    mountpath, mntflags, arg, alen);
    412  1.38     pooka }
    413  1.38     pooka 
    414  1.38     pooka struct ukfs *
    415  1.38     pooka ukfs_mount_disk(const char *vfsname, const char *devpath, int partition,
    416  1.38     pooka 	const char *mountpath, int mntflags, void *arg, size_t alen)
    417  1.38     pooka {
    418  1.38     pooka 
    419  1.38     pooka 	return doukfsmount(vfsname, devpath, partition,
    420  1.38     pooka 	    mountpath, mntflags, arg, alen);
    421  1.38     pooka }
    422  1.38     pooka 
    423  1.30     pooka int
    424   1.1     pooka ukfs_release(struct ukfs *fs, int flags)
    425   1.1     pooka {
    426   1.1     pooka 
    427   1.1     pooka 	if ((flags & UKFS_RELFLAG_NOUNMOUNT) == 0) {
    428  1.37     pooka 		int rv, mntflag, error;
    429   1.9     pooka 
    430  1.30     pooka 		ukfs_chdir(fs, "/");
    431  1.30     pooka 		mntflag = 0;
    432  1.30     pooka 		if (flags & UKFS_RELFLAG_FORCE)
    433  1.30     pooka 			mntflag = MNT_FORCE;
    434  1.36     pooka 		rump_setup_curlwp(nextpid(fs), 1, 1);
    435  1.37     pooka 		rump_vp_rele(fs->ukfs_rvp);
    436  1.37     pooka 		fs->ukfs_rvp = NULL;
    437  1.30     pooka 		rv = rump_sys_unmount(fs->ukfs_mountpath, mntflag);
    438  1.37     pooka 		if (rv == -1) {
    439  1.37     pooka 			error = errno;
    440  1.37     pooka 			rump_vfs_root(fs->ukfs_mp, &fs->ukfs_rvp, 0);
    441  1.37     pooka 			rump_clear_curlwp();
    442  1.30     pooka 			ukfs_chdir(fs, fs->ukfs_mountpath);
    443  1.37     pooka 			errno = error;
    444  1.30     pooka 			return -1;
    445  1.30     pooka 		}
    446  1.37     pooka 		rump_clear_curlwp();
    447   1.1     pooka 	}
    448   1.1     pooka 
    449  1.33     pooka 	if (fs->ukfs_devpath) {
    450  1.33     pooka 		rump_etfs_remove(fs->ukfs_devpath);
    451  1.33     pooka 		free(fs->ukfs_devpath);
    452  1.33     pooka 	}
    453  1.30     pooka 	free(fs->ukfs_mountpath);
    454   1.1     pooka 
    455   1.1     pooka 	pthread_spin_destroy(&fs->ukfs_spin);
    456  1.16  stacktic 	if (fs->ukfs_devfd != -1) {
    457  1.11     pooka 		flock(fs->ukfs_devfd, LOCK_UN);
    458  1.16  stacktic 		close(fs->ukfs_devfd);
    459  1.16  stacktic 	}
    460   1.1     pooka 	free(fs);
    461  1.30     pooka 
    462  1.30     pooka 	return 0;
    463   1.1     pooka }
    464   1.1     pooka 
    465   1.1     pooka #define STDCALL(ukfs, thecall)						\
    466   1.1     pooka 	int rv = 0;							\
    467   1.1     pooka 									\
    468   1.1     pooka 	precall(ukfs);							\
    469  1.21     pooka 	rv = thecall;							\
    470   1.1     pooka 	postcall(ukfs);							\
    471  1.21     pooka 	return rv;
    472   1.1     pooka 
    473   1.1     pooka int
    474  1.24     pooka ukfs_opendir(struct ukfs *ukfs, const char *dirname, struct ukfs_dircookie **c)
    475   1.1     pooka {
    476   1.1     pooka 	struct vnode *vp;
    477  1.24     pooka 	int rv;
    478   1.1     pooka 
    479   1.1     pooka 	precall(ukfs);
    480   1.1     pooka 	rv = rump_namei(RUMP_NAMEI_LOOKUP, RUMP_NAMEI_LOCKLEAF, dirname,
    481   1.1     pooka 	    NULL, &vp, NULL);
    482   1.1     pooka 	postcall(ukfs);
    483  1.24     pooka 
    484  1.24     pooka 	if (rv == 0) {
    485  1.24     pooka 		RUMP_VOP_UNLOCK(vp, 0);
    486  1.24     pooka 	} else {
    487  1.24     pooka 		errno = rv;
    488  1.24     pooka 		rv = -1;
    489  1.24     pooka 	}
    490  1.24     pooka 
    491  1.24     pooka 	/*LINTED*/
    492  1.24     pooka 	*c = (struct ukfs_dircookie *)vp;
    493  1.24     pooka 	return rv;
    494  1.24     pooka }
    495  1.24     pooka 
    496  1.24     pooka static int
    497  1.24     pooka getmydents(struct vnode *vp, off_t *off, uint8_t *buf, size_t bufsize)
    498  1.24     pooka {
    499  1.24     pooka 	struct uio *uio;
    500  1.24     pooka 	size_t resid;
    501  1.24     pooka 	int rv, eofflag;
    502  1.24     pooka 	kauth_cred_t cred;
    503  1.24     pooka 
    504   1.1     pooka 	uio = rump_uio_setup(buf, bufsize, *off, RUMPUIO_READ);
    505   1.9     pooka 	cred = rump_cred_suserget();
    506   1.9     pooka 	rv = RUMP_VOP_READDIR(vp, uio, cred, &eofflag, NULL, NULL);
    507   1.9     pooka 	rump_cred_suserput(cred);
    508  1.17     pooka 	RUMP_VOP_UNLOCK(vp, 0);
    509   1.1     pooka 	*off = rump_uio_getoff(uio);
    510   1.1     pooka 	resid = rump_uio_free(uio);
    511   1.1     pooka 
    512   1.1     pooka 	if (rv) {
    513   1.1     pooka 		errno = rv;
    514   1.1     pooka 		return -1;
    515   1.1     pooka 	}
    516   1.1     pooka 
    517   1.1     pooka 	/* LINTED: not totally correct return type, but follows syscall */
    518   1.1     pooka 	return bufsize - resid;
    519   1.1     pooka }
    520   1.1     pooka 
    521  1.24     pooka /*ARGSUSED*/
    522  1.24     pooka int
    523  1.24     pooka ukfs_getdents_cookie(struct ukfs *ukfs, struct ukfs_dircookie *c, off_t *off,
    524  1.24     pooka 	uint8_t *buf, size_t bufsize)
    525  1.24     pooka {
    526  1.24     pooka 	/*LINTED*/
    527  1.24     pooka 	struct vnode *vp = (struct vnode *)c;
    528  1.24     pooka 
    529  1.24     pooka 	RUMP_VOP_LOCK(vp, RUMP_LK_SHARED);
    530  1.24     pooka 	return getmydents(vp, off, buf, bufsize);
    531  1.24     pooka }
    532  1.24     pooka 
    533  1.24     pooka int
    534  1.24     pooka ukfs_getdents(struct ukfs *ukfs, const char *dirname, off_t *off,
    535  1.24     pooka 	uint8_t *buf, size_t bufsize)
    536  1.24     pooka {
    537  1.24     pooka 	struct vnode *vp;
    538  1.24     pooka 	int rv;
    539  1.24     pooka 
    540  1.24     pooka 	precall(ukfs);
    541  1.24     pooka 	rv = rump_namei(RUMP_NAMEI_LOOKUP, RUMP_NAMEI_LOCKLEAF, dirname,
    542  1.24     pooka 	    NULL, &vp, NULL);
    543  1.24     pooka 	postcall(ukfs);
    544  1.24     pooka 	if (rv) {
    545  1.24     pooka 		errno = rv;
    546  1.24     pooka 		return -1;
    547  1.24     pooka 	}
    548  1.24     pooka 
    549  1.24     pooka 	rv = getmydents(vp, off, buf, bufsize);
    550  1.24     pooka 	rump_vp_rele(vp);
    551  1.24     pooka 	return rv;
    552  1.24     pooka }
    553  1.24     pooka 
    554  1.24     pooka /*ARGSUSED*/
    555  1.24     pooka int
    556  1.24     pooka ukfs_closedir(struct ukfs *ukfs, struct ukfs_dircookie *c)
    557  1.24     pooka {
    558  1.24     pooka 
    559  1.24     pooka 	/*LINTED*/
    560  1.24     pooka 	rump_vp_rele((struct vnode *)c);
    561  1.24     pooka 	return 0;
    562  1.24     pooka }
    563  1.24     pooka 
    564  1.24     pooka int
    565  1.24     pooka ukfs_open(struct ukfs *ukfs, const char *filename, int flags)
    566  1.24     pooka {
    567  1.24     pooka 	int fd;
    568  1.24     pooka 
    569  1.24     pooka 	precall(ukfs);
    570  1.24     pooka 	fd = rump_sys_open(filename, flags, 0);
    571  1.24     pooka 	postcall(ukfs);
    572  1.24     pooka 	if (fd == -1)
    573  1.24     pooka 		return -1;
    574  1.24     pooka 
    575  1.24     pooka 	return fd;
    576  1.24     pooka }
    577  1.24     pooka 
    578   1.1     pooka ssize_t
    579   1.1     pooka ukfs_read(struct ukfs *ukfs, const char *filename, off_t off,
    580   1.1     pooka 	uint8_t *buf, size_t bufsize)
    581   1.1     pooka {
    582  1.21     pooka 	int fd;
    583   1.1     pooka 	ssize_t xfer = -1; /* XXXgcc */
    584   1.1     pooka 
    585   1.1     pooka 	precall(ukfs);
    586  1.21     pooka 	fd = rump_sys_open(filename, RUMP_O_RDONLY, 0);
    587  1.21     pooka 	if (fd == -1)
    588   1.1     pooka 		goto out;
    589   1.1     pooka 
    590  1.27     pooka 	xfer = rump_sys_pread(fd, buf, bufsize, off);
    591  1.21     pooka 	rump_sys_close(fd);
    592   1.1     pooka 
    593   1.1     pooka  out:
    594   1.1     pooka 	postcall(ukfs);
    595  1.21     pooka 	if (fd == -1) {
    596   1.1     pooka 		return -1;
    597   1.1     pooka 	}
    598   1.1     pooka 	return xfer;
    599   1.1     pooka }
    600   1.1     pooka 
    601  1.24     pooka /*ARGSUSED*/
    602  1.24     pooka ssize_t
    603  1.24     pooka ukfs_read_fd(struct ukfs *ukfs, int fd, off_t off, uint8_t *buf, size_t buflen)
    604  1.24     pooka {
    605  1.24     pooka 
    606  1.27     pooka 	return rump_sys_pread(fd, buf, buflen, off);
    607  1.24     pooka }
    608  1.24     pooka 
    609   1.1     pooka ssize_t
    610   1.1     pooka ukfs_write(struct ukfs *ukfs, const char *filename, off_t off,
    611   1.1     pooka 	uint8_t *buf, size_t bufsize)
    612   1.1     pooka {
    613  1.21     pooka 	int fd;
    614   1.1     pooka 	ssize_t xfer = -1; /* XXXgcc */
    615   1.1     pooka 
    616   1.1     pooka 	precall(ukfs);
    617  1.21     pooka 	fd = rump_sys_open(filename, RUMP_O_WRONLY, 0);
    618  1.21     pooka 	if (fd == -1)
    619   1.1     pooka 		goto out;
    620   1.1     pooka 
    621   1.1     pooka 	/* write and commit */
    622  1.27     pooka 	xfer = rump_sys_pwrite(fd, buf, bufsize, off);
    623  1.21     pooka 	if (xfer > 0)
    624  1.21     pooka 		rump_sys_fsync(fd);
    625   1.1     pooka 
    626  1.21     pooka 	rump_sys_close(fd);
    627   1.1     pooka 
    628   1.1     pooka  out:
    629   1.1     pooka 	postcall(ukfs);
    630  1.21     pooka 	if (fd == -1) {
    631   1.1     pooka 		return -1;
    632   1.1     pooka 	}
    633   1.1     pooka 	return xfer;
    634   1.1     pooka }
    635   1.1     pooka 
    636  1.24     pooka /*ARGSUSED*/
    637  1.24     pooka ssize_t
    638  1.24     pooka ukfs_write_fd(struct ukfs *ukfs, int fd, off_t off, uint8_t *buf, size_t buflen,
    639  1.24     pooka 	int dosync)
    640  1.24     pooka {
    641  1.24     pooka 	ssize_t xfer;
    642  1.24     pooka 
    643  1.27     pooka 	xfer = rump_sys_pwrite(fd, buf, buflen, off);
    644  1.24     pooka 	if (xfer > 0 && dosync)
    645  1.24     pooka 		rump_sys_fsync(fd);
    646  1.24     pooka 
    647  1.24     pooka 	return xfer;
    648  1.24     pooka }
    649  1.24     pooka 
    650  1.24     pooka /*ARGSUSED*/
    651  1.24     pooka int
    652  1.24     pooka ukfs_close(struct ukfs *ukfs, int fd)
    653  1.24     pooka {
    654  1.24     pooka 
    655  1.24     pooka 	rump_sys_close(fd);
    656  1.24     pooka 	return 0;
    657  1.24     pooka }
    658  1.24     pooka 
    659   1.1     pooka int
    660   1.1     pooka ukfs_create(struct ukfs *ukfs, const char *filename, mode_t mode)
    661   1.1     pooka {
    662  1.21     pooka 	int fd;
    663   1.1     pooka 
    664   1.1     pooka 	precall(ukfs);
    665  1.21     pooka 	fd = rump_sys_open(filename, RUMP_O_WRONLY | RUMP_O_CREAT, mode);
    666  1.21     pooka 	if (fd == -1)
    667  1.21     pooka 		return -1;
    668  1.21     pooka 	rump_sys_close(fd);
    669   1.1     pooka 
    670   1.1     pooka 	postcall(ukfs);
    671   1.1     pooka 	return 0;
    672   1.1     pooka }
    673   1.1     pooka 
    674   1.1     pooka int
    675   1.1     pooka ukfs_mknod(struct ukfs *ukfs, const char *path, mode_t mode, dev_t dev)
    676   1.1     pooka {
    677   1.1     pooka 
    678  1.21     pooka 	STDCALL(ukfs, rump_sys_mknod(path, mode, dev));
    679   1.1     pooka }
    680   1.1     pooka 
    681   1.1     pooka int
    682   1.1     pooka ukfs_mkfifo(struct ukfs *ukfs, const char *path, mode_t mode)
    683   1.1     pooka {
    684   1.1     pooka 
    685  1.21     pooka 	STDCALL(ukfs, rump_sys_mkfifo(path, mode));
    686   1.1     pooka }
    687   1.1     pooka 
    688   1.1     pooka int
    689   1.1     pooka ukfs_mkdir(struct ukfs *ukfs, const char *filename, mode_t mode)
    690   1.1     pooka {
    691   1.1     pooka 
    692  1.21     pooka 	STDCALL(ukfs, rump_sys_mkdir(filename, mode));
    693   1.1     pooka }
    694   1.1     pooka 
    695   1.1     pooka int
    696   1.1     pooka ukfs_remove(struct ukfs *ukfs, const char *filename)
    697   1.1     pooka {
    698   1.1     pooka 
    699  1.21     pooka 	STDCALL(ukfs, rump_sys_unlink(filename));
    700   1.1     pooka }
    701   1.1     pooka 
    702   1.1     pooka int
    703   1.1     pooka ukfs_rmdir(struct ukfs *ukfs, const char *filename)
    704   1.1     pooka {
    705   1.1     pooka 
    706  1.21     pooka 	STDCALL(ukfs, rump_sys_rmdir(filename));
    707   1.1     pooka }
    708   1.1     pooka 
    709   1.1     pooka int
    710   1.1     pooka ukfs_link(struct ukfs *ukfs, const char *filename, const char *f_create)
    711   1.1     pooka {
    712   1.1     pooka 
    713  1.21     pooka 	STDCALL(ukfs, rump_sys_link(filename, f_create));
    714   1.1     pooka }
    715   1.1     pooka 
    716   1.1     pooka int
    717   1.1     pooka ukfs_symlink(struct ukfs *ukfs, const char *filename, const char *linkname)
    718   1.1     pooka {
    719   1.1     pooka 
    720  1.21     pooka 	STDCALL(ukfs, rump_sys_symlink(filename, linkname));
    721   1.1     pooka }
    722   1.1     pooka 
    723   1.1     pooka ssize_t
    724   1.1     pooka ukfs_readlink(struct ukfs *ukfs, const char *filename,
    725   1.1     pooka 	char *linkbuf, size_t buflen)
    726   1.1     pooka {
    727   1.1     pooka 	ssize_t rv;
    728   1.1     pooka 
    729   1.1     pooka 	precall(ukfs);
    730  1.21     pooka 	rv = rump_sys_readlink(filename, linkbuf, buflen);
    731   1.1     pooka 	postcall(ukfs);
    732   1.1     pooka 	return rv;
    733   1.1     pooka }
    734   1.1     pooka 
    735   1.1     pooka int
    736   1.1     pooka ukfs_rename(struct ukfs *ukfs, const char *from, const char *to)
    737   1.1     pooka {
    738   1.1     pooka 
    739  1.21     pooka 	STDCALL(ukfs, rump_sys_rename(from, to));
    740   1.1     pooka }
    741   1.1     pooka 
    742   1.1     pooka int
    743   1.1     pooka ukfs_chdir(struct ukfs *ukfs, const char *path)
    744   1.1     pooka {
    745   1.1     pooka 	struct vnode *newvp, *oldvp;
    746   1.1     pooka 	int rv;
    747   1.1     pooka 
    748   1.1     pooka 	precall(ukfs);
    749  1.21     pooka 	rv = rump_sys_chdir(path);
    750  1.21     pooka 	if (rv == -1)
    751   1.1     pooka 		goto out;
    752   1.1     pooka 
    753   1.1     pooka 	newvp = rump_cdir_get();
    754   1.1     pooka 	pthread_spin_lock(&ukfs->ukfs_spin);
    755   1.1     pooka 	oldvp = ukfs->ukfs_cdir;
    756   1.1     pooka 	ukfs->ukfs_cdir = newvp;
    757   1.1     pooka 	pthread_spin_unlock(&ukfs->ukfs_spin);
    758   1.1     pooka 	if (oldvp)
    759   1.1     pooka 		rump_vp_rele(oldvp);
    760   1.1     pooka 
    761   1.1     pooka  out:
    762   1.1     pooka 	postcall(ukfs);
    763  1.21     pooka 	return rv;
    764   1.1     pooka }
    765   1.1     pooka 
    766  1.28     pooka /*
    767  1.28     pooka  * If we want to use post-time_t file systems on pre-time_t hosts,
    768  1.28     pooka  * we must translate the stat structure.  Since we don't currently
    769  1.28     pooka  * have a general method for making compat calls in rump, special-case
    770  1.28     pooka  * this one.
    771  1.28     pooka  *
    772  1.28     pooka  * Note that this does not allow making system calls to older rump
    773  1.28     pooka  * kernels from newer hosts.
    774  1.28     pooka  */
    775  1.28     pooka #define VERS_TIMECHANGE 599000700
    776  1.28     pooka 
    777  1.28     pooka static int
    778  1.28     pooka needcompat(void)
    779  1.28     pooka {
    780  1.28     pooka 
    781  1.28     pooka #ifdef __NetBSD__
    782  1.30     pooka 	/*LINTED*/
    783  1.28     pooka 	return __NetBSD_Version__ < VERS_TIMECHANGE
    784  1.28     pooka 	    && rump_getversion() >= VERS_TIMECHANGE;
    785  1.28     pooka #else
    786  1.28     pooka 	return 0;
    787  1.28     pooka #endif
    788  1.28     pooka }
    789  1.28     pooka 
    790   1.1     pooka int
    791   1.1     pooka ukfs_stat(struct ukfs *ukfs, const char *filename, struct stat *file_stat)
    792   1.1     pooka {
    793  1.28     pooka 	int rv;
    794   1.1     pooka 
    795  1.28     pooka 	precall(ukfs);
    796  1.28     pooka 	if (needcompat())
    797  1.28     pooka 		rv = rump_sys___stat30(filename, file_stat);
    798  1.28     pooka 	else
    799  1.28     pooka 		rv = rump_sys_stat(filename, file_stat);
    800  1.28     pooka 	postcall(ukfs);
    801  1.28     pooka 
    802  1.28     pooka 	return rv;
    803   1.1     pooka }
    804   1.1     pooka 
    805   1.1     pooka int
    806   1.1     pooka ukfs_lstat(struct ukfs *ukfs, const char *filename, struct stat *file_stat)
    807   1.1     pooka {
    808  1.28     pooka 	int rv;
    809   1.1     pooka 
    810  1.28     pooka 	precall(ukfs);
    811  1.28     pooka 	if (needcompat())
    812  1.28     pooka 		rv = rump_sys___lstat30(filename, file_stat);
    813  1.28     pooka 	else
    814  1.28     pooka 		rv = rump_sys_lstat(filename, file_stat);
    815  1.28     pooka 	postcall(ukfs);
    816  1.28     pooka 
    817  1.28     pooka 	return rv;
    818   1.1     pooka }
    819   1.1     pooka 
    820   1.1     pooka int
    821   1.1     pooka ukfs_chmod(struct ukfs *ukfs, const char *filename, mode_t mode)
    822   1.1     pooka {
    823   1.1     pooka 
    824  1.21     pooka 	STDCALL(ukfs, rump_sys_chmod(filename, mode));
    825   1.1     pooka }
    826   1.1     pooka 
    827   1.1     pooka int
    828   1.1     pooka ukfs_lchmod(struct ukfs *ukfs, const char *filename, mode_t mode)
    829   1.1     pooka {
    830   1.1     pooka 
    831  1.21     pooka 	STDCALL(ukfs, rump_sys_lchmod(filename, mode));
    832   1.1     pooka }
    833   1.1     pooka 
    834   1.1     pooka int
    835   1.1     pooka ukfs_chown(struct ukfs *ukfs, const char *filename, uid_t uid, gid_t gid)
    836   1.1     pooka {
    837   1.1     pooka 
    838  1.21     pooka 	STDCALL(ukfs, rump_sys_chown(filename, uid, gid));
    839   1.1     pooka }
    840   1.1     pooka 
    841   1.1     pooka int
    842   1.1     pooka ukfs_lchown(struct ukfs *ukfs, const char *filename, uid_t uid, gid_t gid)
    843   1.1     pooka {
    844   1.1     pooka 
    845  1.21     pooka 	STDCALL(ukfs, rump_sys_lchown(filename, uid, gid));
    846   1.1     pooka }
    847   1.1     pooka 
    848   1.1     pooka int
    849   1.1     pooka ukfs_chflags(struct ukfs *ukfs, const char *filename, u_long flags)
    850   1.1     pooka {
    851   1.1     pooka 
    852  1.21     pooka 	STDCALL(ukfs, rump_sys_chflags(filename, flags));
    853   1.1     pooka }
    854   1.1     pooka 
    855   1.1     pooka int
    856   1.1     pooka ukfs_lchflags(struct ukfs *ukfs, const char *filename, u_long flags)
    857   1.1     pooka {
    858   1.1     pooka 
    859  1.21     pooka 	STDCALL(ukfs, rump_sys_lchflags(filename, flags));
    860   1.1     pooka }
    861   1.1     pooka 
    862   1.1     pooka int
    863   1.1     pooka ukfs_utimes(struct ukfs *ukfs, const char *filename, const struct timeval *tptr)
    864   1.1     pooka {
    865   1.1     pooka 
    866  1.21     pooka 	STDCALL(ukfs, rump_sys_utimes(filename, tptr));
    867   1.1     pooka }
    868   1.1     pooka 
    869   1.1     pooka int
    870   1.1     pooka ukfs_lutimes(struct ukfs *ukfs, const char *filename,
    871   1.1     pooka 	      const struct timeval *tptr)
    872   1.1     pooka {
    873   1.1     pooka 
    874  1.21     pooka 	STDCALL(ukfs, rump_sys_lutimes(filename, tptr));
    875   1.1     pooka }
    876   1.1     pooka 
    877   1.3     pooka /*
    878   1.3     pooka  * Dynamic module support
    879   1.3     pooka  */
    880   1.3     pooka 
    881   1.3     pooka /* load one library */
    882   1.3     pooka 
    883   1.3     pooka /*
    884   1.3     pooka  * XXX: the dlerror stuff isn't really threadsafe, but then again I
    885   1.3     pooka  * can't protect against other threads calling dl*() outside of ukfs,
    886   1.3     pooka  * so just live with it being flimsy
    887   1.3     pooka  */
    888   1.3     pooka int
    889   1.3     pooka ukfs_modload(const char *fname)
    890   1.3     pooka {
    891  1.26     pooka 	void *handle;
    892  1.26     pooka 	struct modinfo **mi;
    893   1.3     pooka 	int error;
    894   1.3     pooka 
    895   1.3     pooka 	handle = dlopen(fname, RTLD_GLOBAL);
    896   1.3     pooka 	if (handle == NULL) {
    897  1.13     pooka 		const char *dlmsg = dlerror();
    898  1.13     pooka 		if (strstr(dlmsg, "Undefined symbol"))
    899   1.3     pooka 			return 0;
    900  1.13     pooka 		warnx("dlopen %s failed: %s\n", fname, dlmsg);
    901   1.3     pooka 		/* XXXerrno */
    902   1.3     pooka 		return -1;
    903   1.3     pooka 	}
    904   1.3     pooka 
    905  1.26     pooka 	mi = dlsym(handle, "__start_link_set_modules");
    906  1.26     pooka 	if (mi) {
    907  1.26     pooka 		error = rump_module_init(*mi, NULL);
    908   1.3     pooka 		if (error)
    909   1.3     pooka 			goto errclose;
    910   1.3     pooka 		return 1;
    911   1.3     pooka 	}
    912   1.3     pooka 	error = EINVAL;
    913   1.3     pooka 
    914   1.3     pooka  errclose:
    915   1.3     pooka 	dlclose(handle);
    916   1.3     pooka 	errno = error;
    917   1.3     pooka 	return -1;
    918   1.3     pooka }
    919   1.3     pooka 
    920   1.3     pooka struct loadfail {
    921   1.3     pooka 	char *pname;
    922   1.3     pooka 
    923   1.3     pooka 	LIST_ENTRY(loadfail) entries;
    924   1.3     pooka };
    925   1.3     pooka 
    926   1.3     pooka #define RUMPFSMOD_PREFIX "librumpfs_"
    927   1.3     pooka #define RUMPFSMOD_SUFFIX ".so"
    928   1.3     pooka 
    929   1.3     pooka int
    930   1.3     pooka ukfs_modload_dir(const char *dir)
    931   1.3     pooka {
    932   1.3     pooka 	char nbuf[MAXPATHLEN+1], *p;
    933   1.3     pooka 	struct dirent entry, *result;
    934   1.3     pooka 	DIR *libdir;
    935   1.3     pooka 	struct loadfail *lf, *nlf;
    936   1.3     pooka 	int error, nloaded = 0, redo;
    937   1.3     pooka 	LIST_HEAD(, loadfail) lfs;
    938   1.3     pooka 
    939   1.3     pooka 	libdir = opendir(dir);
    940   1.3     pooka 	if (libdir == NULL)
    941   1.3     pooka 		return -1;
    942   1.3     pooka 
    943   1.3     pooka 	LIST_INIT(&lfs);
    944   1.3     pooka 	for (;;) {
    945   1.3     pooka 		if ((error = readdir_r(libdir, &entry, &result)) != 0)
    946   1.3     pooka 			break;
    947   1.3     pooka 		if (!result)
    948   1.3     pooka 			break;
    949   1.3     pooka 		if (strncmp(result->d_name, RUMPFSMOD_PREFIX,
    950   1.3     pooka 		    strlen(RUMPFSMOD_PREFIX)) != 0)
    951   1.3     pooka 			continue;
    952   1.3     pooka 		if (((p = strstr(result->d_name, RUMPFSMOD_SUFFIX)) == NULL)
    953   1.3     pooka 		    || strlen(p) != strlen(RUMPFSMOD_SUFFIX))
    954   1.3     pooka 			continue;
    955   1.3     pooka 		strlcpy(nbuf, dir, sizeof(nbuf));
    956   1.3     pooka 		strlcat(nbuf, "/", sizeof(nbuf));
    957   1.3     pooka 		strlcat(nbuf, result->d_name, sizeof(nbuf));
    958   1.3     pooka 		switch (ukfs_modload(nbuf)) {
    959   1.3     pooka 		case 0:
    960   1.3     pooka 			lf = malloc(sizeof(*lf));
    961   1.3     pooka 			if (lf == NULL) {
    962   1.3     pooka 				error = ENOMEM;
    963   1.3     pooka 				break;
    964   1.3     pooka 			}
    965   1.3     pooka 			lf->pname = strdup(nbuf);
    966   1.3     pooka 			if (lf->pname == NULL) {
    967   1.3     pooka 				free(lf);
    968   1.3     pooka 				error = ENOMEM;
    969   1.3     pooka 				break;
    970   1.3     pooka 			}
    971   1.3     pooka 			LIST_INSERT_HEAD(&lfs, lf, entries);
    972   1.3     pooka 			break;
    973   1.3     pooka 		case 1:
    974   1.3     pooka 			nloaded++;
    975   1.3     pooka 			break;
    976   1.3     pooka 		default:
    977   1.3     pooka 			/* ignore errors */
    978   1.3     pooka 			break;
    979   1.3     pooka 		}
    980   1.3     pooka 	}
    981   1.3     pooka 	closedir(libdir);
    982   1.3     pooka 	if (error && nloaded != 0)
    983   1.3     pooka 		error = 0;
    984   1.3     pooka 
    985   1.3     pooka 	/*
    986   1.3     pooka 	 * El-cheapo dependency calculator.  Just try to load the
    987   1.3     pooka 	 * modules n times in a loop
    988   1.3     pooka 	 */
    989   1.3     pooka 	for (redo = 1; redo;) {
    990   1.3     pooka 		redo = 0;
    991   1.3     pooka 		nlf = LIST_FIRST(&lfs);
    992   1.3     pooka 		while ((lf = nlf) != NULL) {
    993   1.3     pooka 			nlf = LIST_NEXT(lf, entries);
    994   1.3     pooka 			if (ukfs_modload(lf->pname) == 1) {
    995   1.3     pooka 				nloaded++;
    996   1.3     pooka 				redo = 1;
    997   1.3     pooka 				LIST_REMOVE(lf, entries);
    998   1.3     pooka 				free(lf->pname);
    999   1.3     pooka 				free(lf);
   1000   1.3     pooka 			}
   1001   1.3     pooka 		}
   1002   1.3     pooka 	}
   1003   1.3     pooka 
   1004   1.3     pooka 	while ((lf = LIST_FIRST(&lfs)) != NULL) {
   1005   1.3     pooka 		LIST_REMOVE(lf, entries);
   1006   1.3     pooka 		free(lf->pname);
   1007   1.3     pooka 		free(lf);
   1008   1.3     pooka 	}
   1009   1.3     pooka 
   1010   1.3     pooka 	if (error && nloaded == 0) {
   1011   1.3     pooka 		errno = error;
   1012   1.3     pooka 		return -1;
   1013   1.3     pooka 	}
   1014   1.3     pooka 
   1015   1.3     pooka 	return nloaded;
   1016   1.3     pooka }
   1017   1.3     pooka 
   1018   1.4     pooka /* XXX: this code uses definitions from NetBSD, needs rumpdefs */
   1019   1.4     pooka ssize_t
   1020   1.4     pooka ukfs_vfstypes(char *buf, size_t buflen)
   1021   1.4     pooka {
   1022   1.4     pooka 	int mib[3];
   1023   1.4     pooka 	struct sysctlnode q, ans[128];
   1024   1.4     pooka 	size_t alen;
   1025  1.21     pooka 	int i;
   1026   1.4     pooka 
   1027   1.4     pooka 	mib[0] = CTL_VFS;
   1028   1.4     pooka 	mib[1] = VFS_GENERIC;
   1029   1.4     pooka 	mib[2] = CTL_QUERY;
   1030   1.4     pooka 	alen = sizeof(ans);
   1031   1.4     pooka 
   1032   1.4     pooka 	memset(&q, 0, sizeof(q));
   1033   1.4     pooka 	q.sysctl_flags = SYSCTL_VERSION;
   1034   1.4     pooka 
   1035  1.21     pooka 	if (rump_sys___sysctl(mib, 3, ans, &alen, &q, sizeof(q)) == -1) {
   1036   1.4     pooka 		return -1;
   1037   1.4     pooka 	}
   1038   1.4     pooka 
   1039   1.4     pooka 	for (i = 0; i < alen/sizeof(ans[0]); i++)
   1040   1.4     pooka 		if (strcmp("fstypes", ans[i].sysctl_name) == 0)
   1041   1.4     pooka 			break;
   1042   1.4     pooka 	if (i == alen/sizeof(ans[0])) {
   1043   1.4     pooka 		errno = ENXIO;
   1044   1.4     pooka 		return -1;
   1045   1.4     pooka 	}
   1046   1.4     pooka 
   1047   1.4     pooka 	mib[0] = CTL_VFS;
   1048   1.4     pooka 	mib[1] = VFS_GENERIC;
   1049   1.4     pooka 	mib[2] = ans[i].sysctl_num;
   1050   1.4     pooka 
   1051  1.21     pooka 	if (rump_sys___sysctl(mib, 3, buf, &buflen, NULL, 0) == -1) {
   1052   1.4     pooka 		return -1;
   1053   1.4     pooka 	}
   1054   1.4     pooka 
   1055   1.4     pooka 	return buflen;
   1056   1.4     pooka }
   1057   1.3     pooka 
   1058   1.3     pooka /*
   1059   1.3     pooka  * Utilities
   1060   1.3     pooka  */
   1061  1.30     pooka static int
   1062  1.30     pooka builddirs(const char *pathname, mode_t mode,
   1063  1.30     pooka 	int (*mkdirfn)(struct ukfs *, const char *, mode_t), struct ukfs *fs)
   1064   1.1     pooka {
   1065   1.1     pooka 	char *f1, *f2;
   1066   1.1     pooka 	int rv;
   1067   1.1     pooka 	mode_t mask;
   1068   1.1     pooka 	bool end;
   1069   1.1     pooka 
   1070   1.1     pooka 	/*ukfs_umask((mask = ukfs_umask(0)));*/
   1071   1.1     pooka 	umask((mask = umask(0)));
   1072   1.1     pooka 
   1073   1.1     pooka 	f1 = f2 = strdup(pathname);
   1074   1.1     pooka 	if (f1 == NULL) {
   1075   1.1     pooka 		errno = ENOMEM;
   1076   1.1     pooka 		return -1;
   1077   1.1     pooka 	}
   1078   1.1     pooka 
   1079   1.1     pooka 	end = false;
   1080   1.1     pooka 	for (;;) {
   1081   1.1     pooka 		/* find next component */
   1082   1.1     pooka 		f2 += strspn(f2, "/");
   1083   1.1     pooka 		f2 += strcspn(f2, "/");
   1084   1.1     pooka 		if (*f2 == '\0')
   1085   1.1     pooka 			end = true;
   1086   1.1     pooka 		else
   1087   1.1     pooka 			*f2 = '\0';
   1088   1.1     pooka 
   1089  1.30     pooka 		rv = mkdirfn(fs, f1, mode & ~mask);
   1090   1.1     pooka 		if (errno == EEXIST)
   1091   1.1     pooka 			rv = 0;
   1092   1.1     pooka 
   1093   1.1     pooka 		if (rv == -1 || *f2 != '\0' || end)
   1094   1.1     pooka 			break;
   1095   1.1     pooka 
   1096   1.1     pooka 		*f2 = '/';
   1097   1.1     pooka 	}
   1098   1.1     pooka 
   1099   1.1     pooka 	free(f1);
   1100   1.1     pooka 
   1101   1.1     pooka 	return rv;
   1102   1.1     pooka }
   1103  1.30     pooka 
   1104  1.30     pooka int
   1105  1.30     pooka ukfs_util_builddirs(struct ukfs *ukfs, const char *pathname, mode_t mode)
   1106  1.30     pooka {
   1107  1.30     pooka 
   1108  1.30     pooka 	return builddirs(pathname, mode, ukfs_mkdir, ukfs);
   1109  1.30     pooka }
   1110