Home | History | Annotate | Line # | Download | only in libukfs
ukfs.c revision 1.56
      1  1.56     pooka /*	$NetBSD: ukfs.c,v 1.56 2011/01/02 13:01:45 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.54     pooka 	pthread_spinlock_t ukfs_spin;
     72  1.54     pooka 
     73   1.1     pooka 	struct mount *ukfs_mp;
     74  1.54     pooka 	struct lwp *ukfs_lwp;
     75  1.37     pooka 	void *ukfs_specific;
     76   1.1     pooka 
     77  1.11     pooka 	int ukfs_devfd;
     78  1.54     pooka 
     79  1.30     pooka 	char *ukfs_devpath;
     80  1.30     pooka 	char *ukfs_mountpath;
     81  1.54     pooka 	char *ukfs_cwd;
     82  1.54     pooka 
     83  1.45     pooka 	struct ukfs_part *ukfs_part;
     84   1.1     pooka };
     85   1.1     pooka 
     86  1.30     pooka static int builddirs(const char *, mode_t,
     87  1.30     pooka     int (*mkdirfn)(struct ukfs *, const char *, mode_t), struct ukfs *);
     88  1.30     pooka 
     89   1.1     pooka struct mount *
     90   1.1     pooka ukfs_getmp(struct ukfs *ukfs)
     91   1.1     pooka {
     92   1.1     pooka 
     93   1.1     pooka 	return ukfs->ukfs_mp;
     94   1.1     pooka }
     95   1.1     pooka 
     96  1.37     pooka void
     97  1.37     pooka ukfs_setspecific(struct ukfs *ukfs, void *priv)
     98  1.37     pooka {
     99  1.37     pooka 
    100  1.37     pooka 	ukfs->ukfs_specific = priv;
    101  1.37     pooka }
    102  1.37     pooka 
    103  1.37     pooka void *
    104  1.37     pooka ukfs_getspecific(struct ukfs *ukfs)
    105  1.37     pooka {
    106  1.37     pooka 
    107  1.37     pooka 	return ukfs->ukfs_specific;
    108  1.37     pooka }
    109  1.37     pooka 
    110  1.20     pooka #ifdef DONT_WANT_PTHREAD_LINKAGE
    111  1.20     pooka #define pthread_spin_lock(a)
    112  1.20     pooka #define pthread_spin_unlock(a)
    113  1.20     pooka #define pthread_spin_init(a,b)
    114  1.20     pooka #define pthread_spin_destroy(a)
    115  1.20     pooka #endif
    116  1.20     pooka 
    117  1.54     pooka static int
    118  1.54     pooka precall(struct ukfs *ukfs, struct lwp **curlwp)
    119   1.1     pooka {
    120   1.1     pooka 
    121  1.54     pooka 	/* save previous.  ensure start from pristine context */
    122  1.54     pooka 	*curlwp = rump_pub_lwproc_curlwp();
    123  1.54     pooka 	if (*curlwp)
    124  1.54     pooka 		rump_pub_lwproc_switch(ukfs->ukfs_lwp);
    125  1.56     pooka 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
    126  1.54     pooka 
    127  1.54     pooka 	if (rump_sys_chroot(ukfs->ukfs_mountpath) == -1)
    128  1.54     pooka 		return errno;
    129  1.54     pooka 	if (rump_sys_chdir(ukfs->ukfs_cwd) == -1)
    130  1.54     pooka 		return errno;
    131  1.54     pooka 
    132  1.54     pooka 	return 0;
    133   1.1     pooka }
    134   1.1     pooka 
    135   1.1     pooka static void
    136  1.54     pooka postcall(struct lwp *curlwp)
    137   1.1     pooka {
    138  1.53     pooka 
    139  1.53     pooka 	rump_pub_lwproc_releaselwp();
    140  1.54     pooka 	if (curlwp)
    141  1.54     pooka 		rump_pub_lwproc_switch(curlwp);
    142   1.1     pooka }
    143   1.1     pooka 
    144  1.54     pooka #define PRECALL()							\
    145  1.54     pooka struct lwp *ukfs_curlwp;						\
    146  1.54     pooka do {									\
    147  1.54     pooka 	int ukfs_rv;							\
    148  1.54     pooka 	if ((ukfs_rv = precall(ukfs, &ukfs_curlwp)) != 0) {		\
    149  1.54     pooka 		errno = ukfs_rv;					\
    150  1.54     pooka 		return -1;						\
    151  1.54     pooka 	}								\
    152  1.54     pooka } while (/*CONSTCOND*/0)
    153  1.54     pooka 
    154  1.54     pooka #define POSTCALL() postcall(ukfs_curlwp);
    155  1.54     pooka 
    156  1.43     pooka struct ukfs_part {
    157  1.47     pooka 	pthread_spinlock_t part_lck;
    158  1.47     pooka 	int part_refcount;
    159  1.47     pooka 
    160  1.43     pooka 	int part_type;
    161  1.43     pooka 	char part_labelchar;
    162  1.43     pooka 	off_t part_devoff;
    163  1.43     pooka 	off_t part_devsize;
    164  1.43     pooka };
    165  1.43     pooka 
    166  1.43     pooka enum ukfs_parttype { UKFS_PART_NONE, UKFS_PART_DISKLABEL, UKFS_PART_OFFSET };
    167  1.43     pooka 
    168  1.43     pooka static struct ukfs_part ukfs__part_none = {
    169  1.43     pooka 	.part_type = UKFS_PART_NONE,
    170  1.43     pooka 	.part_devoff = 0,
    171  1.43     pooka 	.part_devsize = RUMP_ETFS_SIZE_ENDOFF,
    172  1.43     pooka };
    173  1.43     pooka static struct ukfs_part ukfs__part_na;
    174  1.44     pooka struct ukfs_part *ukfs_part_none = &ukfs__part_none;
    175  1.44     pooka struct ukfs_part *ukfs_part_na = &ukfs__part_na;
    176  1.43     pooka 
    177  1.45     pooka #define PART2LOCKSIZE(len) ((len) == RUMP_ETFS_SIZE_ENDOFF ? 0 : (len))
    178  1.45     pooka 
    179   1.1     pooka int
    180  1.10     pooka _ukfs_init(int version)
    181   1.1     pooka {
    182  1.10     pooka 	int rv;
    183  1.10     pooka 
    184  1.10     pooka 	if (version != UKFS_VERSION) {
    185  1.10     pooka 		printf("incompatible ukfs version, %d vs. %d\n",
    186  1.10     pooka 		    version, UKFS_VERSION);
    187  1.10     pooka 		errno = EPROGMISMATCH;
    188  1.10     pooka 		return -1;
    189  1.10     pooka 	}
    190   1.1     pooka 
    191  1.10     pooka 	if ((rv = rump_init()) != 0) {
    192  1.10     pooka 		errno = rv;
    193  1.10     pooka 		return -1;
    194  1.10     pooka 	}
    195   1.1     pooka 
    196   1.1     pooka 	return 0;
    197   1.1     pooka }
    198   1.1     pooka 
    199  1.31     pooka /*ARGSUSED*/
    200  1.30     pooka static int
    201  1.30     pooka rumpmkdir(struct ukfs *dummy, const char *path, mode_t mode)
    202  1.30     pooka {
    203  1.30     pooka 
    204  1.30     pooka 	return rump_sys_mkdir(path, mode);
    205  1.30     pooka }
    206  1.30     pooka 
    207  1.38     pooka int
    208  1.43     pooka ukfs_part_probe(char *devpath, struct ukfs_part **partp)
    209  1.38     pooka {
    210  1.43     pooka 	struct ukfs_part *part;
    211  1.38     pooka 	char *p;
    212  1.43     pooka 	int error = 0;
    213  1.43     pooka 	int devfd = -1;
    214  1.43     pooka 
    215  1.43     pooka 	if ((p = strstr(devpath, UKFS_PARTITION_SCANMAGIC)) != NULL) {
    216  1.43     pooka 		fprintf(stderr, "ukfs: %%PART is deprecated.  use "
    217  1.43     pooka 		    "%%DISKLABEL instead\n");
    218  1.43     pooka 		errno = ENODEV;
    219  1.43     pooka 		return -1;
    220  1.43     pooka 	}
    221  1.43     pooka 
    222  1.43     pooka 	part = malloc(sizeof(*part));
    223  1.43     pooka 	if (part == NULL) {
    224  1.43     pooka 		errno = ENOMEM;
    225  1.43     pooka 		return -1;
    226  1.43     pooka 	}
    227  1.47     pooka 	if (pthread_spin_init(&part->part_lck, PTHREAD_PROCESS_PRIVATE) == -1) {
    228  1.47     pooka 		error = errno;
    229  1.47     pooka 		free(part);
    230  1.47     pooka 		errno = error;
    231  1.47     pooka 		return -1;
    232  1.47     pooka 	}
    233  1.43     pooka 	part->part_type = UKFS_PART_NONE;
    234  1.47     pooka 	part->part_refcount = 1;
    235  1.38     pooka 
    236  1.38     pooka 	/*
    237  1.43     pooka 	 * Check for magic in pathname:
    238  1.43     pooka 	 *   disklabel: /regularpath%DISKLABEL:labelchar%\0
    239  1.43     pooka 	 *     offsets: /regularpath%OFFSET:start,end%\0
    240  1.38     pooka 	 */
    241  1.43     pooka #define MAGICADJ_DISKLABEL(p, n) (p+sizeof(UKFS_DISKLABEL_SCANMAGIC)-1+n)
    242  1.43     pooka 	if ((p = strstr(devpath, UKFS_DISKLABEL_SCANMAGIC)) != NULL
    243  1.43     pooka 	    && strlen(p) == UKFS_DISKLABEL_MAGICLEN
    244  1.43     pooka 	    && *(MAGICADJ_DISKLABEL(p,1)) == '%') {
    245  1.43     pooka 		if (*(MAGICADJ_DISKLABEL(p,0)) >= 'a' &&
    246  1.43     pooka 		    *(MAGICADJ_DISKLABEL(p,0)) < 'a' + UKFS_MAXPARTITIONS) {
    247  1.43     pooka 			struct ukfs__disklabel dl;
    248  1.43     pooka 			struct ukfs__partition *pp;
    249  1.43     pooka 			char buf[65536];
    250  1.43     pooka 			char labelchar = *(MAGICADJ_DISKLABEL(p,0));
    251  1.43     pooka 			int partition = labelchar - 'a';
    252  1.43     pooka 
    253  1.38     pooka 			*p = '\0';
    254  1.43     pooka 			devfd = open(devpath, O_RDONLY);
    255  1.43     pooka 			if (devfd == -1) {
    256  1.43     pooka 				error = errno;
    257  1.43     pooka 				goto out;
    258  1.43     pooka 			}
    259  1.43     pooka 
    260  1.43     pooka 			/* Locate the disklabel and find the partition. */
    261  1.43     pooka 			if (pread(devfd, buf, sizeof(buf), 0) == -1) {
    262  1.43     pooka 				error = errno;
    263  1.43     pooka 				goto out;
    264  1.43     pooka 			}
    265  1.43     pooka 
    266  1.43     pooka 			if (ukfs__disklabel_scan(&dl, buf, sizeof(buf)) != 0) {
    267  1.43     pooka 				error = ENOENT;
    268  1.43     pooka 				goto out;
    269  1.43     pooka 			}
    270  1.43     pooka 
    271  1.43     pooka 			if (dl.d_npartitions < partition) {
    272  1.43     pooka 				error = ENOENT;
    273  1.43     pooka 				goto out;
    274  1.43     pooka 			}
    275  1.43     pooka 
    276  1.43     pooka 			pp = &dl.d_partitions[partition];
    277  1.43     pooka 			part->part_type = UKFS_PART_DISKLABEL;
    278  1.43     pooka 			part->part_labelchar = labelchar;
    279  1.43     pooka 			part->part_devoff = pp->p_offset << DEV_BSHIFT;
    280  1.43     pooka 			part->part_devsize = pp->p_size << DEV_BSHIFT;
    281  1.38     pooka 		} else {
    282  1.43     pooka 			error = EINVAL;
    283  1.43     pooka 		}
    284  1.43     pooka #define MAGICADJ_OFFSET(p, n) (p+sizeof(UKFS_OFFSET_SCANMAGIC)-1+n)
    285  1.43     pooka 	} else if (((p = strstr(devpath, UKFS_OFFSET_SCANMAGIC)) != NULL)
    286  1.43     pooka 	    && (strlen(p) >= UKFS_OFFSET_MINLEN)) {
    287  1.43     pooka 		char *comma, *pers, *ep, *nptr;
    288  1.43     pooka 		u_quad_t val;
    289  1.43     pooka 
    290  1.43     pooka 		comma = strchr(p, ',');
    291  1.43     pooka 		if (comma == NULL) {
    292  1.43     pooka 			error = EINVAL;
    293  1.43     pooka 			goto out;
    294  1.43     pooka 		}
    295  1.43     pooka 		pers = strchr(comma, '%');
    296  1.43     pooka 		if (pers == NULL) {
    297  1.43     pooka 			error = EINVAL;
    298  1.43     pooka 			goto out;
    299  1.43     pooka 		}
    300  1.43     pooka 		*comma = '\0';
    301  1.43     pooka 		*pers = '\0';
    302  1.43     pooka 		*p = '\0';
    303  1.43     pooka 
    304  1.43     pooka 		nptr = MAGICADJ_OFFSET(p,0);
    305  1.43     pooka 		/* check if string is negative */
    306  1.43     pooka 		if (*nptr == '-') {
    307  1.43     pooka 			error = ERANGE;
    308  1.43     pooka 			goto out;
    309  1.43     pooka 		}
    310  1.43     pooka 		val = strtouq(nptr, &ep, 10);
    311  1.43     pooka 		if (val == UQUAD_MAX) {
    312  1.43     pooka 			error = ERANGE;
    313  1.43     pooka 			goto out;
    314  1.43     pooka 		}
    315  1.43     pooka 		if (*ep != '\0') {
    316  1.43     pooka 			error = EADDRNOTAVAIL; /* creative ;) */
    317  1.43     pooka 			goto out;
    318  1.43     pooka 		}
    319  1.43     pooka 		part->part_devoff = val;
    320  1.43     pooka 
    321  1.43     pooka 		/* omstart */
    322  1.43     pooka 
    323  1.43     pooka 		nptr = comma+1;
    324  1.43     pooka 		/* check if string is negative */
    325  1.43     pooka 		if (*nptr == '-') {
    326  1.43     pooka 			error = ERANGE;
    327  1.43     pooka 			goto out;
    328  1.43     pooka 		}
    329  1.43     pooka 		val = strtouq(nptr, &ep, 10);
    330  1.43     pooka 		if (val == UQUAD_MAX) {
    331  1.43     pooka 			error = ERANGE;
    332  1.43     pooka 			goto out;
    333  1.43     pooka 		}
    334  1.43     pooka 		if (*ep != '\0') {
    335  1.43     pooka 			error = EADDRNOTAVAIL; /* creative ;) */
    336  1.43     pooka 			goto out;
    337  1.38     pooka 		}
    338  1.43     pooka 		part->part_devsize = val;
    339  1.43     pooka 		part->part_type = UKFS_PART_OFFSET;
    340  1.38     pooka 	} else {
    341  1.47     pooka 		ukfs_part_release(part);
    342  1.43     pooka 		part = ukfs_part_none;
    343  1.43     pooka 	}
    344  1.43     pooka 
    345  1.43     pooka  out:
    346  1.43     pooka 	if (devfd != -1)
    347  1.43     pooka 		close(devfd);
    348  1.43     pooka 	if (error) {
    349  1.43     pooka 		free(part);
    350  1.43     pooka 		errno = error;
    351  1.43     pooka 	} else {
    352  1.43     pooka 		*partp = part;
    353  1.43     pooka 	}
    354  1.43     pooka 
    355  1.43     pooka 	return error ? -1 : 0;
    356  1.43     pooka }
    357  1.43     pooka 
    358  1.43     pooka int
    359  1.43     pooka ukfs_part_tostring(struct ukfs_part *part, char *str, size_t strsize)
    360  1.43     pooka {
    361  1.43     pooka 	int rv;
    362  1.43     pooka 
    363  1.43     pooka 	*str = '\0';
    364  1.43     pooka 	/* "pseudo" values */
    365  1.43     pooka 	if (part == ukfs_part_na) {
    366  1.43     pooka 		errno = EINVAL;
    367  1.43     pooka 		return -1;
    368  1.43     pooka 	}
    369  1.43     pooka 	if (part == ukfs_part_none)
    370  1.43     pooka 		return 0;
    371  1.43     pooka 
    372  1.43     pooka 	rv = 0;
    373  1.43     pooka 	switch (part->part_type) {
    374  1.43     pooka 	case UKFS_PART_NONE:
    375  1.43     pooka 		break;
    376  1.43     pooka 
    377  1.43     pooka 	case UKFS_PART_DISKLABEL:
    378  1.43     pooka 		snprintf(str, strsize, "%%DISKLABEL:%c%%",part->part_labelchar);
    379  1.43     pooka 		rv = 1;
    380  1.43     pooka 		break;
    381  1.43     pooka 
    382  1.43     pooka 	case UKFS_PART_OFFSET:
    383  1.43     pooka 		snprintf(str, strsize, "[%llu,%llu]",
    384  1.43     pooka 		    (unsigned long long)part->part_devoff,
    385  1.43     pooka 		    (unsigned long long)(part->part_devoff+part->part_devsize));
    386  1.43     pooka 		rv = 1;
    387  1.43     pooka 		break;
    388  1.38     pooka 	}
    389  1.38     pooka 
    390  1.38     pooka 	return rv;
    391  1.38     pooka }
    392  1.38     pooka 
    393  1.45     pooka static void
    394  1.45     pooka unlockdev(int fd, struct ukfs_part *part)
    395  1.45     pooka {
    396  1.45     pooka 	struct flock flarg;
    397  1.45     pooka 
    398  1.47     pooka 	if (part == ukfs_part_na)
    399  1.47     pooka 		return;
    400  1.47     pooka 
    401  1.45     pooka 	memset(&flarg, 0, sizeof(flarg));
    402  1.45     pooka 	flarg.l_type = F_UNLCK;
    403  1.45     pooka 	flarg.l_whence = SEEK_SET;
    404  1.45     pooka 	flarg.l_start = part->part_devoff;
    405  1.45     pooka 	flarg.l_len = PART2LOCKSIZE(part->part_devsize);
    406  1.45     pooka 	if (fcntl(fd, F_SETLK, &flarg) == -1)
    407  1.45     pooka 		warn("ukfs: cannot unlock device file");
    408  1.45     pooka }
    409  1.45     pooka 
    410  1.38     pooka /*
    411  1.38     pooka  * Open the disk file and flock it.  Also, if we are operation on
    412  1.38     pooka  * an embedded partition, find the partition offset and size from
    413  1.38     pooka  * the disklabel.
    414  1.38     pooka  *
    415  1.38     pooka  * We hard-fail only in two cases:
    416  1.38     pooka  *  1) we failed to get the partition info out (don't know what offset
    417  1.38     pooka  *     to mount from)
    418  1.45     pooka  *  2) we failed to flock the source device (i.e. fcntl() fails,
    419  1.38     pooka  *     not e.g. open() before it)
    420  1.38     pooka  *
    421  1.38     pooka  * Otherwise we let the code proceed to mount and let the file system
    422  1.38     pooka  * throw the proper error.  The only questionable bit is that if we
    423  1.45     pooka  * soft-fail before flock and mount does succeed...
    424  1.38     pooka  *
    425  1.38     pooka  * Returns: -1 error (errno reports error code)
    426  1.38     pooka  *           0 success
    427  1.38     pooka  *
    428  1.38     pooka  * dfdp: -1  device is not open
    429  1.38     pooka  *        n  device is open
    430  1.38     pooka  */
    431  1.38     pooka static int
    432  1.43     pooka process_diskdevice(const char *devpath, struct ukfs_part *part, int rdonly,
    433  1.43     pooka 	int *dfdp)
    434   1.1     pooka {
    435  1.22     pooka 	struct stat sb;
    436  1.38     pooka 	int rv = 0, devfd;
    437  1.38     pooka 
    438  1.38     pooka 	/* defaults */
    439  1.38     pooka 	*dfdp = -1;
    440  1.38     pooka 
    441  1.38     pooka 	devfd = open(devpath, rdonly ? O_RDONLY : O_RDWR);
    442  1.38     pooka 	if (devfd == -1) {
    443  1.43     pooka 		rv = errno;
    444  1.38     pooka 		goto out;
    445  1.38     pooka 	}
    446  1.38     pooka 
    447  1.38     pooka 	if (fstat(devfd, &sb) == -1) {
    448  1.38     pooka 		rv = errno;
    449  1.38     pooka 		goto out;
    450  1.38     pooka 	}
    451   1.1     pooka 
    452  1.11     pooka 	/*
    453  1.38     pooka 	 * We do this only for non-block device since the
    454  1.38     pooka 	 * (NetBSD) kernel allows block device open only once.
    455  1.38     pooka 	 * We also need to close the device for fairly obvious reasons.
    456  1.11     pooka 	 */
    457  1.38     pooka 	if (!S_ISBLK(sb.st_mode)) {
    458  1.45     pooka 		struct flock flarg;
    459  1.45     pooka 
    460  1.45     pooka 		memset(&flarg, 0, sizeof(flarg));
    461  1.45     pooka 		flarg.l_type = rdonly ? F_RDLCK : F_WRLCK;
    462  1.45     pooka 		flarg.l_whence = SEEK_SET;
    463  1.45     pooka 		flarg.l_start = part->part_devoff;
    464  1.45     pooka 		flarg.l_len = PART2LOCKSIZE(part->part_devsize);
    465  1.45     pooka 		if (fcntl(devfd, F_SETLK, &flarg) == -1) {
    466  1.45     pooka 			pid_t holder;
    467  1.45     pooka 			int sverrno;
    468  1.45     pooka 
    469  1.45     pooka 			sverrno = errno;
    470  1.45     pooka 			if (fcntl(devfd, F_GETLK, &flarg) != 1)
    471  1.45     pooka 				holder = flarg.l_pid;
    472  1.45     pooka 			else
    473  1.45     pooka 				holder = -1;
    474  1.45     pooka 			warnx("ukfs_mount: cannot lock device.  held by pid %d",
    475  1.45     pooka 			    holder);
    476  1.45     pooka 			rv = sverrno;
    477  1.11     pooka 			goto out;
    478  1.11     pooka 		}
    479  1.38     pooka 	} else {
    480  1.38     pooka 		close(devfd);
    481  1.38     pooka 		devfd = -1;
    482  1.38     pooka 	}
    483  1.38     pooka 	*dfdp = devfd;
    484  1.22     pooka 
    485  1.38     pooka  out:
    486  1.38     pooka 	if (rv) {
    487  1.38     pooka 		if (devfd != -1)
    488  1.22     pooka 			close(devfd);
    489  1.11     pooka 	}
    490   1.1     pooka 
    491  1.38     pooka 	return rv;
    492  1.38     pooka }
    493  1.38     pooka 
    494  1.49     pooka struct mountinfo {
    495  1.49     pooka 	const char *mi_vfsname;
    496  1.49     pooka 	const char *mi_mountpath;
    497  1.49     pooka 	int mi_mntflags;
    498  1.49     pooka 	void *mi_arg;
    499  1.49     pooka 	size_t mi_alen;
    500  1.49     pooka 	int *mi_error;
    501  1.49     pooka };
    502  1.49     pooka static void *
    503  1.49     pooka mfs_mounter(void *arg)
    504  1.49     pooka {
    505  1.49     pooka 	struct mountinfo *mi = arg;
    506  1.49     pooka 	int rv;
    507  1.49     pooka 
    508  1.49     pooka 	rv = rump_sys_mount(mi->mi_vfsname, mi->mi_mountpath, mi->mi_mntflags,
    509  1.49     pooka 	    mi->mi_arg, mi->mi_alen);
    510  1.49     pooka 	if (rv) {
    511  1.49     pooka 		warn("mfs mount failed.  fix me.");
    512  1.49     pooka 		abort(); /* XXX */
    513  1.49     pooka 	}
    514  1.49     pooka 
    515  1.49     pooka 	return NULL;
    516  1.49     pooka }
    517  1.49     pooka 
    518  1.38     pooka static struct ukfs *
    519  1.43     pooka doukfsmount(const char *vfsname, const char *devpath, struct ukfs_part *part,
    520  1.38     pooka 	const char *mountpath, int mntflags, void *arg, size_t alen)
    521  1.38     pooka {
    522  1.38     pooka 	struct ukfs *fs = NULL;
    523  1.54     pooka 	struct lwp *curlwp;
    524  1.39     pooka 	int rv = 0, devfd = -1;
    525  1.38     pooka 	int mounted = 0;
    526  1.38     pooka 	int regged = 0;
    527  1.38     pooka 
    528  1.47     pooka 	pthread_spin_lock(&part->part_lck);
    529  1.47     pooka 	part->part_refcount++;
    530  1.47     pooka 	pthread_spin_unlock(&part->part_lck);
    531  1.43     pooka 	if (part != ukfs_part_na) {
    532  1.43     pooka 		if ((rv = process_diskdevice(devpath, part,
    533  1.43     pooka 		    mntflags & MNT_RDONLY, &devfd)) != 0)
    534  1.43     pooka 			goto out;
    535  1.43     pooka 	}
    536  1.38     pooka 
    537   1.1     pooka 	fs = malloc(sizeof(struct ukfs));
    538   1.1     pooka 	if (fs == NULL) {
    539   1.1     pooka 		rv = ENOMEM;
    540   1.1     pooka 		goto out;
    541   1.1     pooka 	}
    542   1.1     pooka 	memset(fs, 0, sizeof(struct ukfs));
    543  1.30     pooka 
    544  1.30     pooka 	/* create our mountpoint.  this is never removed. */
    545  1.30     pooka 	if (builddirs(mountpath, 0777, rumpmkdir, NULL) == -1) {
    546  1.30     pooka 		if (errno != EEXIST) {
    547  1.30     pooka 			rv = errno;
    548  1.30     pooka 			goto out;
    549  1.30     pooka 		}
    550  1.30     pooka 	}
    551   1.1     pooka 
    552  1.43     pooka 	if (part != ukfs_part_na) {
    553  1.43     pooka 		/* LINTED */
    554  1.40     pooka 		rv = rump_pub_etfs_register_withsize(devpath, devpath,
    555  1.43     pooka 		    RUMP_ETFS_BLK, part->part_devoff, part->part_devsize);
    556  1.33     pooka 		if (rv) {
    557  1.33     pooka 			goto out;
    558  1.33     pooka 		}
    559  1.33     pooka 		regged = 1;
    560  1.33     pooka 	}
    561  1.38     pooka 
    562  1.49     pooka 	/*
    563  1.49     pooka 	 * MFS is special since mount(2) doesn't return.  Hence, we
    564  1.49     pooka 	 * create a thread here.  Could fix mfs to return, but there's
    565  1.49     pooka 	 * too much history for me to bother.
    566  1.49     pooka 	 */
    567  1.49     pooka 	if (strcmp(vfsname, MOUNT_MFS) == 0) {
    568  1.49     pooka 		pthread_t pt;
    569  1.49     pooka 		struct mountinfo mi;
    570  1.49     pooka 		int i;
    571  1.49     pooka 
    572  1.49     pooka 		mi.mi_vfsname = vfsname;
    573  1.49     pooka 		mi.mi_mountpath = mountpath;
    574  1.49     pooka 		mi.mi_mntflags = mntflags;
    575  1.49     pooka 		mi.mi_arg = arg;
    576  1.49     pooka 		mi.mi_alen = alen;
    577  1.49     pooka 
    578  1.49     pooka 		if (pthread_create(&pt, NULL, mfs_mounter, &mi) == -1) {
    579  1.49     pooka 			rv = errno;
    580  1.49     pooka 			goto out;
    581  1.49     pooka 		}
    582  1.49     pooka 
    583  1.49     pooka 		for (i = 0;i < 100000; i++) {
    584  1.49     pooka 			struct statvfs svfsb;
    585  1.49     pooka 
    586  1.49     pooka 			rv = rump_sys_statvfs1(mountpath, &svfsb, ST_WAIT);
    587  1.49     pooka 			if (rv == -1) {
    588  1.49     pooka 				rv = errno;
    589  1.49     pooka 				goto out;
    590  1.49     pooka 			}
    591  1.49     pooka 
    592  1.49     pooka 			if (strcmp(svfsb.f_mntonname, mountpath) == 0 &&
    593  1.49     pooka 			    strcmp(svfsb.f_fstypename, MOUNT_MFS) == 0) {
    594  1.49     pooka 				break;
    595  1.49     pooka 			}
    596  1.49     pooka 			usleep(1);
    597  1.49     pooka 		}
    598  1.49     pooka 	} else {
    599  1.49     pooka 		rv = rump_sys_mount(vfsname, mountpath, mntflags, arg, alen);
    600  1.49     pooka 		if (rv) {
    601  1.49     pooka 			rv = errno;
    602  1.49     pooka 			goto out;
    603  1.49     pooka 		}
    604   1.1     pooka 	}
    605  1.49     pooka 
    606  1.30     pooka 	mounted = 1;
    607  1.40     pooka 	rv = rump_pub_vfs_getmp(mountpath, &fs->ukfs_mp);
    608  1.11     pooka 	if (rv) {
    609  1.11     pooka 		goto out;
    610  1.11     pooka 	}
    611  1.30     pooka 
    612  1.33     pooka 	if (regged) {
    613  1.33     pooka 		fs->ukfs_devpath = strdup(devpath);
    614  1.33     pooka 	}
    615  1.30     pooka 	fs->ukfs_mountpath = strdup(mountpath);
    616  1.11     pooka 	pthread_spin_init(&fs->ukfs_spin, PTHREAD_PROCESS_SHARED);
    617  1.11     pooka 	fs->ukfs_devfd = devfd;
    618  1.45     pooka 	fs->ukfs_part = part;
    619  1.11     pooka 	assert(rv == 0);
    620   1.1     pooka 
    621  1.54     pooka 	curlwp = rump_pub_lwproc_curlwp();
    622  1.54     pooka 	rump_pub_lwproc_newlwp(0);
    623  1.54     pooka 	fs->ukfs_lwp = rump_pub_lwproc_curlwp();
    624  1.54     pooka 	fs->ukfs_cwd = strdup("/");
    625  1.54     pooka 	rump_pub_lwproc_switch(curlwp);
    626  1.54     pooka 
    627   1.1     pooka  out:
    628   1.1     pooka 	if (rv) {
    629  1.30     pooka 		if (fs) {
    630   1.1     pooka 			free(fs);
    631  1.30     pooka 			fs = NULL;
    632  1.30     pooka 		}
    633  1.30     pooka 		if (mounted)
    634  1.30     pooka 			rump_sys_unmount(mountpath, MNT_FORCE);
    635  1.33     pooka 		if (regged)
    636  1.40     pooka 			rump_pub_etfs_remove(devpath);
    637  1.11     pooka 		if (devfd != -1) {
    638  1.46     pooka 			unlockdev(devfd, part);
    639  1.11     pooka 			close(devfd);
    640  1.11     pooka 		}
    641  1.45     pooka 		ukfs_part_release(part);
    642  1.34     pooka 		errno = rv;
    643   1.1     pooka 	}
    644   1.1     pooka 
    645   1.1     pooka 	return fs;
    646   1.1     pooka }
    647   1.1     pooka 
    648  1.38     pooka struct ukfs *
    649  1.38     pooka ukfs_mount(const char *vfsname, const char *devpath,
    650  1.38     pooka 	const char *mountpath, int mntflags, void *arg, size_t alen)
    651  1.38     pooka {
    652  1.38     pooka 
    653  1.43     pooka 	return doukfsmount(vfsname, devpath, ukfs_part_na,
    654  1.38     pooka 	    mountpath, mntflags, arg, alen);
    655  1.38     pooka }
    656  1.38     pooka 
    657  1.38     pooka struct ukfs *
    658  1.43     pooka ukfs_mount_disk(const char *vfsname, const char *devpath,
    659  1.43     pooka 	struct ukfs_part *part, const char *mountpath, int mntflags,
    660  1.43     pooka 	void *arg, size_t alen)
    661  1.38     pooka {
    662  1.38     pooka 
    663  1.43     pooka 	return doukfsmount(vfsname, devpath, part,
    664  1.38     pooka 	    mountpath, mntflags, arg, alen);
    665  1.38     pooka }
    666  1.38     pooka 
    667  1.30     pooka int
    668   1.1     pooka ukfs_release(struct ukfs *fs, int flags)
    669   1.1     pooka {
    670  1.54     pooka 	struct lwp *curlwp = rump_pub_lwproc_curlwp();
    671  1.54     pooka 
    672  1.54     pooka 	/* get root lwp */
    673  1.54     pooka 	rump_pub_lwproc_switch(fs->ukfs_lwp);
    674  1.56     pooka 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
    675   1.1     pooka 
    676   1.1     pooka 	if ((flags & UKFS_RELFLAG_NOUNMOUNT) == 0) {
    677  1.37     pooka 		int rv, mntflag, error;
    678   1.9     pooka 
    679  1.30     pooka 		mntflag = 0;
    680  1.30     pooka 		if (flags & UKFS_RELFLAG_FORCE)
    681  1.30     pooka 			mntflag = MNT_FORCE;
    682  1.54     pooka 
    683  1.30     pooka 		rv = rump_sys_unmount(fs->ukfs_mountpath, mntflag);
    684  1.37     pooka 		if (rv == -1) {
    685  1.37     pooka 			error = errno;
    686  1.53     pooka 			rump_pub_lwproc_releaselwp();
    687  1.54     pooka 			if (curlwp)
    688  1.54     pooka 				rump_pub_lwproc_switch(curlwp);
    689  1.37     pooka 			errno = error;
    690  1.30     pooka 			return -1;
    691  1.30     pooka 		}
    692   1.1     pooka 	}
    693   1.1     pooka 
    694  1.33     pooka 	if (fs->ukfs_devpath) {
    695  1.40     pooka 		rump_pub_etfs_remove(fs->ukfs_devpath);
    696  1.33     pooka 		free(fs->ukfs_devpath);
    697  1.33     pooka 	}
    698  1.30     pooka 	free(fs->ukfs_mountpath);
    699  1.54     pooka 	free(fs->ukfs_cwd);
    700  1.54     pooka 
    701  1.54     pooka 	/* release this routine's lwp and ukfs base lwp */
    702  1.54     pooka 	rump_pub_lwproc_releaselwp();
    703  1.54     pooka 	rump_pub_lwproc_switch(fs->ukfs_lwp);
    704  1.54     pooka 	rump_pub_lwproc_releaselwp();
    705   1.1     pooka 
    706   1.1     pooka 	pthread_spin_destroy(&fs->ukfs_spin);
    707  1.16  stacktic 	if (fs->ukfs_devfd != -1) {
    708  1.45     pooka 		unlockdev(fs->ukfs_devfd, fs->ukfs_part);
    709  1.16  stacktic 		close(fs->ukfs_devfd);
    710  1.16  stacktic 	}
    711  1.47     pooka 	ukfs_part_release(fs->ukfs_part);
    712   1.1     pooka 	free(fs);
    713  1.30     pooka 
    714  1.54     pooka 	if (curlwp)
    715  1.54     pooka 		rump_pub_lwproc_switch(curlwp);
    716  1.54     pooka 
    717  1.30     pooka 	return 0;
    718   1.1     pooka }
    719   1.1     pooka 
    720  1.43     pooka void
    721  1.43     pooka ukfs_part_release(struct ukfs_part *part)
    722  1.43     pooka {
    723  1.47     pooka 	int release;
    724  1.43     pooka 
    725  1.47     pooka 	if (part != ukfs_part_none && part != ukfs_part_na) {
    726  1.47     pooka 		pthread_spin_lock(&part->part_lck);
    727  1.47     pooka 		release = --part->part_refcount == 0;
    728  1.47     pooka 		pthread_spin_unlock(&part->part_lck);
    729  1.47     pooka 		if (release) {
    730  1.47     pooka 			pthread_spin_destroy(&part->part_lck);
    731  1.47     pooka 			free(part);
    732  1.47     pooka 		}
    733  1.47     pooka 	}
    734  1.43     pooka }
    735  1.43     pooka 
    736   1.1     pooka #define STDCALL(ukfs, thecall)						\
    737   1.1     pooka 	int rv = 0;							\
    738   1.1     pooka 									\
    739  1.54     pooka 	PRECALL();							\
    740  1.21     pooka 	rv = thecall;							\
    741  1.54     pooka 	POSTCALL();							\
    742  1.21     pooka 	return rv;
    743   1.1     pooka 
    744   1.1     pooka int
    745  1.24     pooka ukfs_opendir(struct ukfs *ukfs, const char *dirname, struct ukfs_dircookie **c)
    746   1.1     pooka {
    747   1.1     pooka 	struct vnode *vp;
    748  1.24     pooka 	int rv;
    749   1.1     pooka 
    750  1.54     pooka 	PRECALL();
    751  1.40     pooka 	rv = rump_pub_namei(RUMP_NAMEI_LOOKUP, RUMP_NAMEI_LOCKLEAF, dirname,
    752   1.1     pooka 	    NULL, &vp, NULL);
    753  1.54     pooka 	POSTCALL();
    754  1.24     pooka 
    755  1.24     pooka 	if (rv == 0) {
    756  1.51   hannken 		RUMP_VOP_UNLOCK(vp);
    757  1.24     pooka 	} else {
    758  1.24     pooka 		errno = rv;
    759  1.24     pooka 		rv = -1;
    760  1.24     pooka 	}
    761  1.24     pooka 
    762  1.24     pooka 	/*LINTED*/
    763  1.24     pooka 	*c = (struct ukfs_dircookie *)vp;
    764  1.24     pooka 	return rv;
    765  1.24     pooka }
    766  1.24     pooka 
    767  1.24     pooka static int
    768  1.24     pooka getmydents(struct vnode *vp, off_t *off, uint8_t *buf, size_t bufsize)
    769  1.24     pooka {
    770  1.24     pooka 	struct uio *uio;
    771  1.24     pooka 	size_t resid;
    772  1.24     pooka 	int rv, eofflag;
    773  1.50     pooka 	struct kauth_cred *cred;
    774  1.24     pooka 
    775  1.40     pooka 	uio = rump_pub_uio_setup(buf, bufsize, *off, RUMPUIO_READ);
    776  1.53     pooka 	cred = rump_pub_cred_create(0, 0, 0, NULL);
    777   1.9     pooka 	rv = RUMP_VOP_READDIR(vp, uio, cred, &eofflag, NULL, NULL);
    778  1.40     pooka 	rump_pub_cred_put(cred);
    779  1.51   hannken 	RUMP_VOP_UNLOCK(vp);
    780  1.40     pooka 	*off = rump_pub_uio_getoff(uio);
    781  1.40     pooka 	resid = rump_pub_uio_free(uio);
    782   1.1     pooka 
    783   1.1     pooka 	if (rv) {
    784   1.1     pooka 		errno = rv;
    785   1.1     pooka 		return -1;
    786   1.1     pooka 	}
    787   1.1     pooka 
    788   1.1     pooka 	/* LINTED: not totally correct return type, but follows syscall */
    789   1.1     pooka 	return bufsize - resid;
    790   1.1     pooka }
    791   1.1     pooka 
    792  1.24     pooka /*ARGSUSED*/
    793  1.24     pooka int
    794  1.24     pooka ukfs_getdents_cookie(struct ukfs *ukfs, struct ukfs_dircookie *c, off_t *off,
    795  1.24     pooka 	uint8_t *buf, size_t bufsize)
    796  1.24     pooka {
    797  1.24     pooka 	/*LINTED*/
    798  1.24     pooka 	struct vnode *vp = (struct vnode *)c;
    799  1.24     pooka 
    800  1.24     pooka 	RUMP_VOP_LOCK(vp, RUMP_LK_SHARED);
    801  1.24     pooka 	return getmydents(vp, off, buf, bufsize);
    802  1.24     pooka }
    803  1.24     pooka 
    804  1.24     pooka int
    805  1.24     pooka ukfs_getdents(struct ukfs *ukfs, const char *dirname, off_t *off,
    806  1.24     pooka 	uint8_t *buf, size_t bufsize)
    807  1.24     pooka {
    808  1.24     pooka 	struct vnode *vp;
    809  1.24     pooka 	int rv;
    810  1.24     pooka 
    811  1.54     pooka 	PRECALL();
    812  1.40     pooka 	rv = rump_pub_namei(RUMP_NAMEI_LOOKUP, RUMP_NAMEI_LOCKLEAF, dirname,
    813  1.24     pooka 	    NULL, &vp, NULL);
    814  1.24     pooka 	if (rv) {
    815  1.54     pooka 		POSTCALL();
    816  1.24     pooka 		errno = rv;
    817  1.24     pooka 		return -1;
    818  1.24     pooka 	}
    819  1.24     pooka 
    820  1.24     pooka 	rv = getmydents(vp, off, buf, bufsize);
    821  1.40     pooka 	rump_pub_vp_rele(vp);
    822  1.54     pooka 	POSTCALL();
    823  1.24     pooka 	return rv;
    824  1.24     pooka }
    825  1.24     pooka 
    826  1.24     pooka /*ARGSUSED*/
    827  1.24     pooka int
    828  1.24     pooka ukfs_closedir(struct ukfs *ukfs, struct ukfs_dircookie *c)
    829  1.24     pooka {
    830  1.24     pooka 
    831  1.24     pooka 	/*LINTED*/
    832  1.40     pooka 	rump_pub_vp_rele((struct vnode *)c);
    833  1.24     pooka 	return 0;
    834  1.24     pooka }
    835  1.24     pooka 
    836  1.24     pooka int
    837  1.24     pooka ukfs_open(struct ukfs *ukfs, const char *filename, int flags)
    838  1.24     pooka {
    839  1.24     pooka 	int fd;
    840  1.24     pooka 
    841  1.54     pooka 	PRECALL();
    842  1.24     pooka 	fd = rump_sys_open(filename, flags, 0);
    843  1.54     pooka 	POSTCALL();
    844  1.24     pooka 	if (fd == -1)
    845  1.24     pooka 		return -1;
    846  1.24     pooka 
    847  1.24     pooka 	return fd;
    848  1.24     pooka }
    849  1.24     pooka 
    850   1.1     pooka ssize_t
    851   1.1     pooka ukfs_read(struct ukfs *ukfs, const char *filename, off_t off,
    852   1.1     pooka 	uint8_t *buf, size_t bufsize)
    853   1.1     pooka {
    854  1.21     pooka 	int fd;
    855   1.1     pooka 	ssize_t xfer = -1; /* XXXgcc */
    856   1.1     pooka 
    857  1.54     pooka 	PRECALL();
    858  1.21     pooka 	fd = rump_sys_open(filename, RUMP_O_RDONLY, 0);
    859  1.21     pooka 	if (fd == -1)
    860   1.1     pooka 		goto out;
    861   1.1     pooka 
    862  1.27     pooka 	xfer = rump_sys_pread(fd, buf, bufsize, off);
    863  1.21     pooka 	rump_sys_close(fd);
    864   1.1     pooka 
    865   1.1     pooka  out:
    866  1.54     pooka 	POSTCALL();
    867  1.21     pooka 	if (fd == -1) {
    868   1.1     pooka 		return -1;
    869   1.1     pooka 	}
    870   1.1     pooka 	return xfer;
    871   1.1     pooka }
    872   1.1     pooka 
    873  1.24     pooka /*ARGSUSED*/
    874  1.24     pooka ssize_t
    875  1.24     pooka ukfs_read_fd(struct ukfs *ukfs, int fd, off_t off, uint8_t *buf, size_t buflen)
    876  1.24     pooka {
    877  1.24     pooka 
    878  1.27     pooka 	return rump_sys_pread(fd, buf, buflen, off);
    879  1.24     pooka }
    880  1.24     pooka 
    881   1.1     pooka ssize_t
    882   1.1     pooka ukfs_write(struct ukfs *ukfs, const char *filename, off_t off,
    883   1.1     pooka 	uint8_t *buf, size_t bufsize)
    884   1.1     pooka {
    885  1.21     pooka 	int fd;
    886   1.1     pooka 	ssize_t xfer = -1; /* XXXgcc */
    887   1.1     pooka 
    888  1.54     pooka 	PRECALL();
    889  1.21     pooka 	fd = rump_sys_open(filename, RUMP_O_WRONLY, 0);
    890  1.21     pooka 	if (fd == -1)
    891   1.1     pooka 		goto out;
    892   1.1     pooka 
    893   1.1     pooka 	/* write and commit */
    894  1.27     pooka 	xfer = rump_sys_pwrite(fd, buf, bufsize, off);
    895  1.21     pooka 	if (xfer > 0)
    896  1.21     pooka 		rump_sys_fsync(fd);
    897   1.1     pooka 
    898  1.21     pooka 	rump_sys_close(fd);
    899   1.1     pooka 
    900   1.1     pooka  out:
    901  1.54     pooka 	POSTCALL();
    902  1.21     pooka 	if (fd == -1) {
    903   1.1     pooka 		return -1;
    904   1.1     pooka 	}
    905   1.1     pooka 	return xfer;
    906   1.1     pooka }
    907   1.1     pooka 
    908  1.24     pooka /*ARGSUSED*/
    909  1.24     pooka ssize_t
    910  1.24     pooka ukfs_write_fd(struct ukfs *ukfs, int fd, off_t off, uint8_t *buf, size_t buflen,
    911  1.24     pooka 	int dosync)
    912  1.24     pooka {
    913  1.24     pooka 	ssize_t xfer;
    914  1.24     pooka 
    915  1.27     pooka 	xfer = rump_sys_pwrite(fd, buf, buflen, off);
    916  1.24     pooka 	if (xfer > 0 && dosync)
    917  1.24     pooka 		rump_sys_fsync(fd);
    918  1.24     pooka 
    919  1.24     pooka 	return xfer;
    920  1.24     pooka }
    921  1.24     pooka 
    922  1.24     pooka /*ARGSUSED*/
    923  1.24     pooka int
    924  1.24     pooka ukfs_close(struct ukfs *ukfs, int fd)
    925  1.24     pooka {
    926  1.24     pooka 
    927  1.24     pooka 	rump_sys_close(fd);
    928  1.24     pooka 	return 0;
    929  1.24     pooka }
    930  1.24     pooka 
    931   1.1     pooka int
    932   1.1     pooka ukfs_create(struct ukfs *ukfs, const char *filename, mode_t mode)
    933   1.1     pooka {
    934  1.21     pooka 	int fd;
    935   1.1     pooka 
    936  1.54     pooka 	PRECALL();
    937  1.21     pooka 	fd = rump_sys_open(filename, RUMP_O_WRONLY | RUMP_O_CREAT, mode);
    938  1.21     pooka 	if (fd == -1)
    939  1.21     pooka 		return -1;
    940  1.21     pooka 	rump_sys_close(fd);
    941   1.1     pooka 
    942  1.54     pooka 	POSTCALL();
    943   1.1     pooka 	return 0;
    944   1.1     pooka }
    945   1.1     pooka 
    946   1.1     pooka int
    947   1.1     pooka ukfs_mknod(struct ukfs *ukfs, const char *path, mode_t mode, dev_t dev)
    948   1.1     pooka {
    949   1.1     pooka 
    950  1.21     pooka 	STDCALL(ukfs, rump_sys_mknod(path, mode, dev));
    951   1.1     pooka }
    952   1.1     pooka 
    953   1.1     pooka int
    954   1.1     pooka ukfs_mkfifo(struct ukfs *ukfs, const char *path, mode_t mode)
    955   1.1     pooka {
    956   1.1     pooka 
    957  1.21     pooka 	STDCALL(ukfs, rump_sys_mkfifo(path, mode));
    958   1.1     pooka }
    959   1.1     pooka 
    960   1.1     pooka int
    961   1.1     pooka ukfs_mkdir(struct ukfs *ukfs, const char *filename, mode_t mode)
    962   1.1     pooka {
    963   1.1     pooka 
    964  1.21     pooka 	STDCALL(ukfs, rump_sys_mkdir(filename, mode));
    965   1.1     pooka }
    966   1.1     pooka 
    967   1.1     pooka int
    968   1.1     pooka ukfs_remove(struct ukfs *ukfs, const char *filename)
    969   1.1     pooka {
    970   1.1     pooka 
    971  1.21     pooka 	STDCALL(ukfs, rump_sys_unlink(filename));
    972   1.1     pooka }
    973   1.1     pooka 
    974   1.1     pooka int
    975   1.1     pooka ukfs_rmdir(struct ukfs *ukfs, const char *filename)
    976   1.1     pooka {
    977   1.1     pooka 
    978  1.21     pooka 	STDCALL(ukfs, rump_sys_rmdir(filename));
    979   1.1     pooka }
    980   1.1     pooka 
    981   1.1     pooka int
    982   1.1     pooka ukfs_link(struct ukfs *ukfs, const char *filename, const char *f_create)
    983   1.1     pooka {
    984   1.1     pooka 
    985  1.21     pooka 	STDCALL(ukfs, rump_sys_link(filename, f_create));
    986   1.1     pooka }
    987   1.1     pooka 
    988   1.1     pooka int
    989   1.1     pooka ukfs_symlink(struct ukfs *ukfs, const char *filename, const char *linkname)
    990   1.1     pooka {
    991   1.1     pooka 
    992  1.21     pooka 	STDCALL(ukfs, rump_sys_symlink(filename, linkname));
    993   1.1     pooka }
    994   1.1     pooka 
    995   1.1     pooka ssize_t
    996   1.1     pooka ukfs_readlink(struct ukfs *ukfs, const char *filename,
    997   1.1     pooka 	char *linkbuf, size_t buflen)
    998   1.1     pooka {
    999   1.1     pooka 	ssize_t rv;
   1000   1.1     pooka 
   1001  1.54     pooka 	PRECALL();
   1002  1.21     pooka 	rv = rump_sys_readlink(filename, linkbuf, buflen);
   1003  1.54     pooka 	POSTCALL();
   1004   1.1     pooka 	return rv;
   1005   1.1     pooka }
   1006   1.1     pooka 
   1007   1.1     pooka int
   1008   1.1     pooka ukfs_rename(struct ukfs *ukfs, const char *from, const char *to)
   1009   1.1     pooka {
   1010   1.1     pooka 
   1011  1.21     pooka 	STDCALL(ukfs, rump_sys_rename(from, to));
   1012   1.1     pooka }
   1013   1.1     pooka 
   1014   1.1     pooka int
   1015   1.1     pooka ukfs_chdir(struct ukfs *ukfs, const char *path)
   1016   1.1     pooka {
   1017  1.54     pooka 	char *newpath, *oldpath;
   1018   1.1     pooka 	int rv;
   1019   1.1     pooka 
   1020  1.54     pooka 	PRECALL();
   1021  1.21     pooka 	rv = rump_sys_chdir(path);
   1022  1.21     pooka 	if (rv == -1)
   1023   1.1     pooka 		goto out;
   1024   1.1     pooka 
   1025  1.54     pooka 	newpath = malloc(MAXPATHLEN);
   1026  1.54     pooka 	if (rump_sys___getcwd(newpath, MAXPATHLEN) == -1) {
   1027  1.54     pooka 		goto out;
   1028  1.54     pooka 	}
   1029  1.54     pooka 
   1030   1.1     pooka 	pthread_spin_lock(&ukfs->ukfs_spin);
   1031  1.54     pooka 	oldpath = ukfs->ukfs_cwd;
   1032  1.54     pooka 	ukfs->ukfs_cwd = newpath;
   1033   1.1     pooka 	pthread_spin_unlock(&ukfs->ukfs_spin);
   1034  1.54     pooka 	free(oldpath);
   1035   1.1     pooka 
   1036   1.1     pooka  out:
   1037  1.54     pooka 	POSTCALL();
   1038  1.21     pooka 	return rv;
   1039   1.1     pooka }
   1040   1.1     pooka 
   1041   1.1     pooka int
   1042   1.1     pooka ukfs_stat(struct ukfs *ukfs, const char *filename, struct stat *file_stat)
   1043   1.1     pooka {
   1044  1.28     pooka 	int rv;
   1045   1.1     pooka 
   1046  1.54     pooka 	PRECALL();
   1047  1.52     pooka 	rv = rump_sys_stat(filename, file_stat);
   1048  1.54     pooka 	POSTCALL();
   1049  1.28     pooka 
   1050  1.28     pooka 	return rv;
   1051   1.1     pooka }
   1052   1.1     pooka 
   1053   1.1     pooka int
   1054   1.1     pooka ukfs_lstat(struct ukfs *ukfs, const char *filename, struct stat *file_stat)
   1055   1.1     pooka {
   1056  1.28     pooka 	int rv;
   1057   1.1     pooka 
   1058  1.54     pooka 	PRECALL();
   1059  1.52     pooka 	rv = rump_sys_lstat(filename, file_stat);
   1060  1.54     pooka 	POSTCALL();
   1061  1.28     pooka 
   1062  1.28     pooka 	return rv;
   1063   1.1     pooka }
   1064   1.1     pooka 
   1065   1.1     pooka int
   1066   1.1     pooka ukfs_chmod(struct ukfs *ukfs, const char *filename, mode_t mode)
   1067   1.1     pooka {
   1068   1.1     pooka 
   1069  1.21     pooka 	STDCALL(ukfs, rump_sys_chmod(filename, mode));
   1070   1.1     pooka }
   1071   1.1     pooka 
   1072   1.1     pooka int
   1073   1.1     pooka ukfs_lchmod(struct ukfs *ukfs, const char *filename, mode_t mode)
   1074   1.1     pooka {
   1075   1.1     pooka 
   1076  1.21     pooka 	STDCALL(ukfs, rump_sys_lchmod(filename, mode));
   1077   1.1     pooka }
   1078   1.1     pooka 
   1079   1.1     pooka int
   1080   1.1     pooka ukfs_chown(struct ukfs *ukfs, const char *filename, uid_t uid, gid_t gid)
   1081   1.1     pooka {
   1082   1.1     pooka 
   1083  1.21     pooka 	STDCALL(ukfs, rump_sys_chown(filename, uid, gid));
   1084   1.1     pooka }
   1085   1.1     pooka 
   1086   1.1     pooka int
   1087   1.1     pooka ukfs_lchown(struct ukfs *ukfs, const char *filename, uid_t uid, gid_t gid)
   1088   1.1     pooka {
   1089   1.1     pooka 
   1090  1.21     pooka 	STDCALL(ukfs, rump_sys_lchown(filename, uid, gid));
   1091   1.1     pooka }
   1092   1.1     pooka 
   1093   1.1     pooka int
   1094   1.1     pooka ukfs_chflags(struct ukfs *ukfs, const char *filename, u_long flags)
   1095   1.1     pooka {
   1096   1.1     pooka 
   1097  1.21     pooka 	STDCALL(ukfs, rump_sys_chflags(filename, flags));
   1098   1.1     pooka }
   1099   1.1     pooka 
   1100   1.1     pooka int
   1101   1.1     pooka ukfs_lchflags(struct ukfs *ukfs, const char *filename, u_long flags)
   1102   1.1     pooka {
   1103   1.1     pooka 
   1104  1.21     pooka 	STDCALL(ukfs, rump_sys_lchflags(filename, flags));
   1105   1.1     pooka }
   1106   1.1     pooka 
   1107   1.1     pooka int
   1108   1.1     pooka ukfs_utimes(struct ukfs *ukfs, const char *filename, const struct timeval *tptr)
   1109   1.1     pooka {
   1110   1.1     pooka 
   1111  1.21     pooka 	STDCALL(ukfs, rump_sys_utimes(filename, tptr));
   1112   1.1     pooka }
   1113   1.1     pooka 
   1114   1.1     pooka int
   1115   1.1     pooka ukfs_lutimes(struct ukfs *ukfs, const char *filename,
   1116   1.1     pooka 	      const struct timeval *tptr)
   1117   1.1     pooka {
   1118   1.1     pooka 
   1119  1.21     pooka 	STDCALL(ukfs, rump_sys_lutimes(filename, tptr));
   1120   1.1     pooka }
   1121   1.1     pooka 
   1122   1.3     pooka /*
   1123   1.3     pooka  * Dynamic module support
   1124   1.3     pooka  */
   1125   1.3     pooka 
   1126   1.3     pooka /* load one library */
   1127   1.3     pooka 
   1128   1.3     pooka /*
   1129   1.3     pooka  * XXX: the dlerror stuff isn't really threadsafe, but then again I
   1130   1.3     pooka  * can't protect against other threads calling dl*() outside of ukfs,
   1131   1.3     pooka  * so just live with it being flimsy
   1132   1.3     pooka  */
   1133   1.3     pooka int
   1134   1.3     pooka ukfs_modload(const char *fname)
   1135   1.3     pooka {
   1136  1.26     pooka 	void *handle;
   1137  1.48     pooka 	const struct modinfo *const *mi_start, *const *mi_end;
   1138   1.3     pooka 	int error;
   1139   1.3     pooka 
   1140  1.42     njoly 	handle = dlopen(fname, RTLD_LAZY|RTLD_GLOBAL);
   1141   1.3     pooka 	if (handle == NULL) {
   1142  1.13     pooka 		const char *dlmsg = dlerror();
   1143  1.13     pooka 		if (strstr(dlmsg, "Undefined symbol"))
   1144   1.3     pooka 			return 0;
   1145  1.13     pooka 		warnx("dlopen %s failed: %s\n", fname, dlmsg);
   1146   1.3     pooka 		/* XXXerrno */
   1147   1.3     pooka 		return -1;
   1148   1.3     pooka 	}
   1149   1.3     pooka 
   1150  1.48     pooka 	mi_start = dlsym(handle, "__start_link_set_modules");
   1151  1.48     pooka 	mi_end = dlsym(handle, "__stop_link_set_modules");
   1152  1.48     pooka 	if (mi_start && mi_end) {
   1153  1.48     pooka 		error = rump_pub_module_init(mi_start,
   1154  1.48     pooka 		    (size_t)(mi_end-mi_start));
   1155   1.3     pooka 		if (error)
   1156   1.3     pooka 			goto errclose;
   1157   1.3     pooka 		return 1;
   1158   1.3     pooka 	}
   1159   1.3     pooka 	error = EINVAL;
   1160   1.3     pooka 
   1161   1.3     pooka  errclose:
   1162   1.3     pooka 	dlclose(handle);
   1163   1.3     pooka 	errno = error;
   1164   1.3     pooka 	return -1;
   1165   1.3     pooka }
   1166   1.3     pooka 
   1167   1.3     pooka struct loadfail {
   1168   1.3     pooka 	char *pname;
   1169   1.3     pooka 
   1170   1.3     pooka 	LIST_ENTRY(loadfail) entries;
   1171   1.3     pooka };
   1172   1.3     pooka 
   1173   1.3     pooka #define RUMPFSMOD_PREFIX "librumpfs_"
   1174   1.3     pooka #define RUMPFSMOD_SUFFIX ".so"
   1175   1.3     pooka 
   1176   1.3     pooka int
   1177   1.3     pooka ukfs_modload_dir(const char *dir)
   1178   1.3     pooka {
   1179   1.3     pooka 	char nbuf[MAXPATHLEN+1], *p;
   1180   1.3     pooka 	struct dirent entry, *result;
   1181   1.3     pooka 	DIR *libdir;
   1182   1.3     pooka 	struct loadfail *lf, *nlf;
   1183   1.3     pooka 	int error, nloaded = 0, redo;
   1184   1.3     pooka 	LIST_HEAD(, loadfail) lfs;
   1185   1.3     pooka 
   1186   1.3     pooka 	libdir = opendir(dir);
   1187   1.3     pooka 	if (libdir == NULL)
   1188   1.3     pooka 		return -1;
   1189   1.3     pooka 
   1190   1.3     pooka 	LIST_INIT(&lfs);
   1191   1.3     pooka 	for (;;) {
   1192   1.3     pooka 		if ((error = readdir_r(libdir, &entry, &result)) != 0)
   1193   1.3     pooka 			break;
   1194   1.3     pooka 		if (!result)
   1195   1.3     pooka 			break;
   1196   1.3     pooka 		if (strncmp(result->d_name, RUMPFSMOD_PREFIX,
   1197   1.3     pooka 		    strlen(RUMPFSMOD_PREFIX)) != 0)
   1198   1.3     pooka 			continue;
   1199   1.3     pooka 		if (((p = strstr(result->d_name, RUMPFSMOD_SUFFIX)) == NULL)
   1200   1.3     pooka 		    || strlen(p) != strlen(RUMPFSMOD_SUFFIX))
   1201   1.3     pooka 			continue;
   1202   1.3     pooka 		strlcpy(nbuf, dir, sizeof(nbuf));
   1203   1.3     pooka 		strlcat(nbuf, "/", sizeof(nbuf));
   1204   1.3     pooka 		strlcat(nbuf, result->d_name, sizeof(nbuf));
   1205   1.3     pooka 		switch (ukfs_modload(nbuf)) {
   1206   1.3     pooka 		case 0:
   1207   1.3     pooka 			lf = malloc(sizeof(*lf));
   1208   1.3     pooka 			if (lf == NULL) {
   1209   1.3     pooka 				error = ENOMEM;
   1210   1.3     pooka 				break;
   1211   1.3     pooka 			}
   1212   1.3     pooka 			lf->pname = strdup(nbuf);
   1213   1.3     pooka 			if (lf->pname == NULL) {
   1214   1.3     pooka 				free(lf);
   1215   1.3     pooka 				error = ENOMEM;
   1216   1.3     pooka 				break;
   1217   1.3     pooka 			}
   1218   1.3     pooka 			LIST_INSERT_HEAD(&lfs, lf, entries);
   1219   1.3     pooka 			break;
   1220   1.3     pooka 		case 1:
   1221   1.3     pooka 			nloaded++;
   1222   1.3     pooka 			break;
   1223   1.3     pooka 		default:
   1224   1.3     pooka 			/* ignore errors */
   1225   1.3     pooka 			break;
   1226   1.3     pooka 		}
   1227   1.3     pooka 	}
   1228   1.3     pooka 	closedir(libdir);
   1229   1.3     pooka 	if (error && nloaded != 0)
   1230   1.3     pooka 		error = 0;
   1231   1.3     pooka 
   1232   1.3     pooka 	/*
   1233   1.3     pooka 	 * El-cheapo dependency calculator.  Just try to load the
   1234   1.3     pooka 	 * modules n times in a loop
   1235   1.3     pooka 	 */
   1236   1.3     pooka 	for (redo = 1; redo;) {
   1237   1.3     pooka 		redo = 0;
   1238   1.3     pooka 		nlf = LIST_FIRST(&lfs);
   1239   1.3     pooka 		while ((lf = nlf) != NULL) {
   1240   1.3     pooka 			nlf = LIST_NEXT(lf, entries);
   1241   1.3     pooka 			if (ukfs_modload(lf->pname) == 1) {
   1242   1.3     pooka 				nloaded++;
   1243   1.3     pooka 				redo = 1;
   1244   1.3     pooka 				LIST_REMOVE(lf, entries);
   1245   1.3     pooka 				free(lf->pname);
   1246   1.3     pooka 				free(lf);
   1247   1.3     pooka 			}
   1248   1.3     pooka 		}
   1249   1.3     pooka 	}
   1250   1.3     pooka 
   1251   1.3     pooka 	while ((lf = LIST_FIRST(&lfs)) != NULL) {
   1252   1.3     pooka 		LIST_REMOVE(lf, entries);
   1253   1.3     pooka 		free(lf->pname);
   1254   1.3     pooka 		free(lf);
   1255   1.3     pooka 	}
   1256   1.3     pooka 
   1257   1.3     pooka 	if (error && nloaded == 0) {
   1258   1.3     pooka 		errno = error;
   1259   1.3     pooka 		return -1;
   1260   1.3     pooka 	}
   1261   1.3     pooka 
   1262   1.3     pooka 	return nloaded;
   1263   1.3     pooka }
   1264   1.3     pooka 
   1265   1.4     pooka /* XXX: this code uses definitions from NetBSD, needs rumpdefs */
   1266   1.4     pooka ssize_t
   1267   1.4     pooka ukfs_vfstypes(char *buf, size_t buflen)
   1268   1.4     pooka {
   1269   1.4     pooka 	int mib[3];
   1270   1.4     pooka 	struct sysctlnode q, ans[128];
   1271   1.4     pooka 	size_t alen;
   1272  1.21     pooka 	int i;
   1273   1.4     pooka 
   1274   1.4     pooka 	mib[0] = CTL_VFS;
   1275   1.4     pooka 	mib[1] = VFS_GENERIC;
   1276   1.4     pooka 	mib[2] = CTL_QUERY;
   1277   1.4     pooka 	alen = sizeof(ans);
   1278   1.4     pooka 
   1279   1.4     pooka 	memset(&q, 0, sizeof(q));
   1280   1.4     pooka 	q.sysctl_flags = SYSCTL_VERSION;
   1281   1.4     pooka 
   1282  1.21     pooka 	if (rump_sys___sysctl(mib, 3, ans, &alen, &q, sizeof(q)) == -1) {
   1283   1.4     pooka 		return -1;
   1284   1.4     pooka 	}
   1285   1.4     pooka 
   1286   1.4     pooka 	for (i = 0; i < alen/sizeof(ans[0]); i++)
   1287   1.4     pooka 		if (strcmp("fstypes", ans[i].sysctl_name) == 0)
   1288   1.4     pooka 			break;
   1289   1.4     pooka 	if (i == alen/sizeof(ans[0])) {
   1290   1.4     pooka 		errno = ENXIO;
   1291   1.4     pooka 		return -1;
   1292   1.4     pooka 	}
   1293   1.4     pooka 
   1294   1.4     pooka 	mib[0] = CTL_VFS;
   1295   1.4     pooka 	mib[1] = VFS_GENERIC;
   1296   1.4     pooka 	mib[2] = ans[i].sysctl_num;
   1297   1.4     pooka 
   1298  1.21     pooka 	if (rump_sys___sysctl(mib, 3, buf, &buflen, NULL, 0) == -1) {
   1299   1.4     pooka 		return -1;
   1300   1.4     pooka 	}
   1301   1.4     pooka 
   1302   1.4     pooka 	return buflen;
   1303   1.4     pooka }
   1304   1.3     pooka 
   1305   1.3     pooka /*
   1306   1.3     pooka  * Utilities
   1307   1.3     pooka  */
   1308  1.30     pooka static int
   1309  1.30     pooka builddirs(const char *pathname, mode_t mode,
   1310  1.30     pooka 	int (*mkdirfn)(struct ukfs *, const char *, mode_t), struct ukfs *fs)
   1311   1.1     pooka {
   1312   1.1     pooka 	char *f1, *f2;
   1313   1.1     pooka 	int rv;
   1314   1.1     pooka 	mode_t mask;
   1315   1.1     pooka 	bool end;
   1316   1.1     pooka 
   1317   1.1     pooka 	/*ukfs_umask((mask = ukfs_umask(0)));*/
   1318   1.1     pooka 	umask((mask = umask(0)));
   1319   1.1     pooka 
   1320   1.1     pooka 	f1 = f2 = strdup(pathname);
   1321   1.1     pooka 	if (f1 == NULL) {
   1322   1.1     pooka 		errno = ENOMEM;
   1323   1.1     pooka 		return -1;
   1324   1.1     pooka 	}
   1325   1.1     pooka 
   1326   1.1     pooka 	end = false;
   1327   1.1     pooka 	for (;;) {
   1328   1.1     pooka 		/* find next component */
   1329   1.1     pooka 		f2 += strspn(f2, "/");
   1330   1.1     pooka 		f2 += strcspn(f2, "/");
   1331   1.1     pooka 		if (*f2 == '\0')
   1332   1.1     pooka 			end = true;
   1333   1.1     pooka 		else
   1334   1.1     pooka 			*f2 = '\0';
   1335   1.1     pooka 
   1336  1.30     pooka 		rv = mkdirfn(fs, f1, mode & ~mask);
   1337   1.1     pooka 		if (errno == EEXIST)
   1338   1.1     pooka 			rv = 0;
   1339   1.1     pooka 
   1340   1.1     pooka 		if (rv == -1 || *f2 != '\0' || end)
   1341   1.1     pooka 			break;
   1342   1.1     pooka 
   1343   1.1     pooka 		*f2 = '/';
   1344   1.1     pooka 	}
   1345   1.1     pooka 
   1346   1.1     pooka 	free(f1);
   1347   1.1     pooka 
   1348   1.1     pooka 	return rv;
   1349   1.1     pooka }
   1350  1.30     pooka 
   1351  1.30     pooka int
   1352  1.30     pooka ukfs_util_builddirs(struct ukfs *ukfs, const char *pathname, mode_t mode)
   1353  1.30     pooka {
   1354  1.30     pooka 
   1355  1.30     pooka 	return builddirs(pathname, mode, ukfs_mkdir, ukfs);
   1356  1.30     pooka }
   1357