Home | History | Annotate | Line # | Download | only in libukfs
ukfs.c revision 1.4
      1  1.4  pooka /*	$NetBSD: ukfs.c,v 1.4 2008/08/01 19:52:10 pooka Exp $	*/
      2  1.1  pooka 
      3  1.1  pooka /*
      4  1.3  pooka  * Copyright (c) 2007, 2008  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.1  pooka #include <pthread.h>
     54  1.1  pooka #include <stdio.h>
     55  1.1  pooka #include <stdlib.h>
     56  1.1  pooka #include <string.h>
     57  1.1  pooka #include <unistd.h>
     58  1.1  pooka #include <stdint.h>
     59  1.1  pooka 
     60  1.1  pooka #include <rump/ukfs.h>
     61  1.1  pooka 
     62  1.1  pooka #include <rump/rump.h>
     63  1.1  pooka #include <rump/rump_syscalls.h>
     64  1.1  pooka 
     65  1.1  pooka #define UKFS_MODE_DEFAULT 0555
     66  1.1  pooka 
     67  1.1  pooka struct ukfs {
     68  1.1  pooka 	struct mount *ukfs_mp;
     69  1.1  pooka 	struct vnode *ukfs_rvp;
     70  1.1  pooka 
     71  1.1  pooka 	pthread_spinlock_t ukfs_spin;
     72  1.1  pooka 	pid_t ukfs_nextpid;
     73  1.1  pooka 	struct vnode *ukfs_cdir;
     74  1.1  pooka };
     75  1.1  pooka 
     76  1.1  pooka struct mount *
     77  1.1  pooka ukfs_getmp(struct ukfs *ukfs)
     78  1.1  pooka {
     79  1.1  pooka 
     80  1.1  pooka 	return ukfs->ukfs_mp;
     81  1.1  pooka }
     82  1.1  pooka 
     83  1.1  pooka struct vnode *
     84  1.1  pooka ukfs_getrvp(struct ukfs *ukfs)
     85  1.1  pooka {
     86  1.1  pooka 	struct vnode *rvp;
     87  1.1  pooka 
     88  1.1  pooka 	rvp = ukfs->ukfs_rvp;
     89  1.1  pooka 	rump_vp_incref(rvp);
     90  1.1  pooka 
     91  1.1  pooka 	return rvp;
     92  1.1  pooka }
     93  1.1  pooka 
     94  1.1  pooka static pid_t
     95  1.1  pooka nextpid(struct ukfs *ukfs)
     96  1.1  pooka {
     97  1.1  pooka 	pid_t npid;
     98  1.1  pooka 
     99  1.1  pooka 	pthread_spin_lock(&ukfs->ukfs_spin);
    100  1.1  pooka 	npid = ukfs->ukfs_nextpid++;
    101  1.1  pooka 	pthread_spin_unlock(&ukfs->ukfs_spin);
    102  1.1  pooka 
    103  1.1  pooka 	return npid;
    104  1.1  pooka }
    105  1.1  pooka 
    106  1.1  pooka static void
    107  1.1  pooka precall(struct ukfs *ukfs)
    108  1.1  pooka {
    109  1.1  pooka 	struct vnode *rvp, *cvp;
    110  1.1  pooka 
    111  1.1  pooka 	rump_setup_curlwp(nextpid(ukfs), 1, 1);
    112  1.1  pooka 	rvp = ukfs_getrvp(ukfs);
    113  1.1  pooka 	pthread_spin_lock(&ukfs->ukfs_spin);
    114  1.1  pooka 	cvp = ukfs->ukfs_cdir;
    115  1.1  pooka 	pthread_spin_unlock(&ukfs->ukfs_spin);
    116  1.1  pooka 	rump_rcvp_set(rvp, cvp); /* takes refs */
    117  1.1  pooka 	rump_vp_rele(rvp);
    118  1.1  pooka }
    119  1.1  pooka 
    120  1.1  pooka static void
    121  1.1  pooka postcall(struct ukfs *ukfs)
    122  1.1  pooka {
    123  1.1  pooka 	struct vnode *rvp;
    124  1.1  pooka 
    125  1.1  pooka 	rvp = ukfs_getrvp(ukfs);
    126  1.1  pooka 	rump_rcvp_set(NULL, rvp);
    127  1.1  pooka 	rump_vp_rele(rvp);
    128  1.1  pooka 	rump_clear_curlwp();
    129  1.1  pooka }
    130  1.1  pooka 
    131  1.1  pooka int
    132  1.1  pooka ukfs_init()
    133  1.1  pooka {
    134  1.1  pooka 
    135  1.1  pooka 	rump_init();
    136  1.1  pooka 
    137  1.1  pooka 	return 0;
    138  1.1  pooka }
    139  1.1  pooka 
    140  1.1  pooka struct ukfs *
    141  1.1  pooka ukfs_mount(const char *vfsname, const char *devpath, const char *mountpath,
    142  1.1  pooka 	int mntflags, void *arg, size_t alen)
    143  1.1  pooka {
    144  1.1  pooka 	struct ukfs *fs = NULL;
    145  1.1  pooka 	struct vfsops *vfsops;
    146  1.1  pooka 	struct mount *mp;
    147  1.1  pooka 	int rv = 0;
    148  1.1  pooka 
    149  1.1  pooka 	vfsops = rump_vfs_getopsbyname(vfsname);
    150  1.1  pooka 	if (vfsops == NULL) {
    151  1.1  pooka 		rv = ENOENT;
    152  1.1  pooka 		goto out;
    153  1.1  pooka 	}
    154  1.1  pooka 
    155  1.1  pooka 	mp = rump_mnt_init(vfsops, mntflags);
    156  1.1  pooka 
    157  1.1  pooka 	fs = malloc(sizeof(struct ukfs));
    158  1.1  pooka 	if (fs == NULL) {
    159  1.1  pooka 		rv = ENOMEM;
    160  1.1  pooka 		goto out;
    161  1.1  pooka 	}
    162  1.1  pooka 	memset(fs, 0, sizeof(struct ukfs));
    163  1.1  pooka 	pthread_spin_init(&fs->ukfs_spin, PTHREAD_PROCESS_SHARED);
    164  1.1  pooka 
    165  1.1  pooka 	rump_fakeblk_register(devpath);
    166  1.1  pooka 	rv = rump_mnt_mount(mp, mountpath, arg, &alen);
    167  1.1  pooka 	rump_fakeblk_deregister(devpath);
    168  1.1  pooka 	if (rv) {
    169  1.1  pooka 		warnx("VFS_MOUNT %d", rv);
    170  1.1  pooka 		goto out;
    171  1.1  pooka 	}
    172  1.1  pooka 	fs->ukfs_mp = mp;
    173  1.1  pooka 
    174  1.1  pooka 	rv = rump_vfs_root(fs->ukfs_mp, &fs->ukfs_rvp, 0);
    175  1.1  pooka 	fs->ukfs_cdir = ukfs_getrvp(fs);
    176  1.1  pooka 
    177  1.1  pooka  out:
    178  1.1  pooka 	if (rv) {
    179  1.1  pooka 		if (fs && fs->ukfs_mp)
    180  1.1  pooka 			rump_mnt_destroy(fs->ukfs_mp);
    181  1.1  pooka 		if (fs)
    182  1.1  pooka 			free(fs);
    183  1.1  pooka 		errno = rv;
    184  1.1  pooka 		fs = NULL;
    185  1.1  pooka 	}
    186  1.1  pooka 
    187  1.1  pooka 	return fs;
    188  1.1  pooka }
    189  1.1  pooka 
    190  1.1  pooka void
    191  1.1  pooka ukfs_release(struct ukfs *fs, int flags)
    192  1.1  pooka {
    193  1.1  pooka 	int rv;
    194  1.1  pooka 
    195  1.1  pooka 	if ((flags & UKFS_RELFLAG_NOUNMOUNT) == 0) {
    196  1.1  pooka 		rump_vp_rele(fs->ukfs_cdir);
    197  1.1  pooka 		rv = rump_vfs_sync(fs->ukfs_mp, 1, NULL);
    198  1.1  pooka 		rump_vp_recycle_nokidding(ukfs_getrvp(fs));
    199  1.1  pooka 		rv |= rump_vfs_unmount(fs->ukfs_mp, 0);
    200  1.1  pooka 		assert(rv == 0);
    201  1.1  pooka 	}
    202  1.1  pooka 
    203  1.1  pooka 	rump_vfs_syncwait(fs->ukfs_mp);
    204  1.1  pooka 	rump_mnt_destroy(fs->ukfs_mp);
    205  1.1  pooka 
    206  1.1  pooka 	pthread_spin_destroy(&fs->ukfs_spin);
    207  1.1  pooka 	free(fs);
    208  1.1  pooka }
    209  1.1  pooka 
    210  1.1  pooka /* don't need vn_lock(), since we don't have VXLOCK */
    211  1.1  pooka #define VLE(a) rump_vp_lock_exclusive(a)
    212  1.1  pooka #define VLS(a) rump_vp_lock_shared(a)
    213  1.1  pooka #define VUL(a) rump_vp_unlock(a)
    214  1.1  pooka #define AUL(a) assert(rump_vp_islocked(a) == 0)
    215  1.1  pooka 
    216  1.1  pooka #define STDCALL(ukfs, thecall)						\
    217  1.1  pooka 	int rv = 0;							\
    218  1.1  pooka 									\
    219  1.1  pooka 	precall(ukfs);							\
    220  1.1  pooka 	thecall;							\
    221  1.1  pooka 	postcall(ukfs);							\
    222  1.1  pooka 	if (rv) {							\
    223  1.1  pooka 		errno = rv;						\
    224  1.1  pooka 		return -1;						\
    225  1.1  pooka 	}								\
    226  1.1  pooka 	return 0;
    227  1.1  pooka 
    228  1.1  pooka int
    229  1.1  pooka ukfs_getdents(struct ukfs *ukfs, const char *dirname, off_t *off,
    230  1.1  pooka 	uint8_t *buf, size_t bufsize)
    231  1.1  pooka {
    232  1.1  pooka 	struct uio *uio;
    233  1.1  pooka 	struct vnode *vp;
    234  1.1  pooka 	size_t resid;
    235  1.1  pooka 	int rv, eofflag;
    236  1.1  pooka 
    237  1.1  pooka 	precall(ukfs);
    238  1.1  pooka 	rv = rump_namei(RUMP_NAMEI_LOOKUP, RUMP_NAMEI_LOCKLEAF, dirname,
    239  1.1  pooka 	    NULL, &vp, NULL);
    240  1.1  pooka 	postcall(ukfs);
    241  1.1  pooka 	if (rv)
    242  1.1  pooka 		goto out;
    243  1.1  pooka 
    244  1.1  pooka 	uio = rump_uio_setup(buf, bufsize, *off, RUMPUIO_READ);
    245  1.1  pooka 	rv = RUMP_VOP_READDIR(vp, uio, NULL, &eofflag, NULL, NULL);
    246  1.1  pooka 	VUL(vp);
    247  1.1  pooka 	*off = rump_uio_getoff(uio);
    248  1.1  pooka 	resid = rump_uio_free(uio);
    249  1.1  pooka 	rump_vp_rele(vp);
    250  1.1  pooka 
    251  1.1  pooka  out:
    252  1.1  pooka 	if (rv) {
    253  1.1  pooka 		errno = rv;
    254  1.1  pooka 		return -1;
    255  1.1  pooka 	}
    256  1.1  pooka 
    257  1.1  pooka 	/* LINTED: not totally correct return type, but follows syscall */
    258  1.1  pooka 	return bufsize - resid;
    259  1.1  pooka }
    260  1.1  pooka 
    261  1.1  pooka ssize_t
    262  1.1  pooka ukfs_read(struct ukfs *ukfs, const char *filename, off_t off,
    263  1.1  pooka 	uint8_t *buf, size_t bufsize)
    264  1.1  pooka {
    265  1.1  pooka 	int fd, rv = 0, dummy;
    266  1.1  pooka 	ssize_t xfer = -1; /* XXXgcc */
    267  1.1  pooka 
    268  1.1  pooka 	precall(ukfs);
    269  1.1  pooka 	fd = rump_sys_open(filename, RUMP_O_RDONLY, 0, &rv);
    270  1.1  pooka 	if (rv)
    271  1.1  pooka 		goto out;
    272  1.1  pooka 
    273  1.1  pooka 	xfer = rump_sys_pread(fd, buf, bufsize, 0, off, &rv);
    274  1.1  pooka 	rump_sys_close(fd, &dummy);
    275  1.1  pooka 
    276  1.1  pooka  out:
    277  1.1  pooka 	postcall(ukfs);
    278  1.1  pooka 	if (rv) {
    279  1.1  pooka 		errno = rv;
    280  1.1  pooka 		return -1;
    281  1.1  pooka 	}
    282  1.1  pooka 	return xfer;
    283  1.1  pooka }
    284  1.1  pooka 
    285  1.1  pooka ssize_t
    286  1.1  pooka ukfs_write(struct ukfs *ukfs, const char *filename, off_t off,
    287  1.1  pooka 	uint8_t *buf, size_t bufsize)
    288  1.1  pooka {
    289  1.1  pooka 	int fd, rv = 0, dummy;
    290  1.1  pooka 	ssize_t xfer = -1; /* XXXgcc */
    291  1.1  pooka 
    292  1.1  pooka 	precall(ukfs);
    293  1.1  pooka 	fd = rump_sys_open(filename, RUMP_O_WRONLY, 0, &rv);
    294  1.1  pooka 	if (rv)
    295  1.1  pooka 		goto out;
    296  1.1  pooka 
    297  1.1  pooka 	/* write and commit */
    298  1.1  pooka 	xfer = rump_sys_pwrite(fd, buf, bufsize, 0, off, &rv);
    299  1.1  pooka 	if (rv == 0)
    300  1.1  pooka 		rump_sys_fsync(fd, &dummy);
    301  1.1  pooka 
    302  1.1  pooka 	rump_sys_close(fd, &dummy);
    303  1.1  pooka 
    304  1.1  pooka  out:
    305  1.1  pooka 	postcall(ukfs);
    306  1.1  pooka 	if (rv) {
    307  1.1  pooka 		errno = rv;
    308  1.1  pooka 		return -1;
    309  1.1  pooka 	}
    310  1.1  pooka 	return xfer;
    311  1.1  pooka }
    312  1.1  pooka 
    313  1.1  pooka int
    314  1.1  pooka ukfs_create(struct ukfs *ukfs, const char *filename, mode_t mode)
    315  1.1  pooka {
    316  1.1  pooka 	int rv, fd, dummy;
    317  1.1  pooka 
    318  1.1  pooka 	precall(ukfs);
    319  1.1  pooka 	fd = rump_sys_open(filename, RUMP_O_WRONLY | RUMP_O_CREAT, mode, &rv);
    320  1.1  pooka 	rump_sys_close(fd, &dummy);
    321  1.1  pooka 
    322  1.1  pooka 	postcall(ukfs);
    323  1.1  pooka 	if (rv) {
    324  1.1  pooka 		errno = rv;
    325  1.1  pooka 		return -1;
    326  1.1  pooka 	}
    327  1.1  pooka 	return 0;
    328  1.1  pooka }
    329  1.1  pooka 
    330  1.1  pooka int
    331  1.1  pooka ukfs_mknod(struct ukfs *ukfs, const char *path, mode_t mode, dev_t dev)
    332  1.1  pooka {
    333  1.1  pooka 
    334  1.1  pooka 	STDCALL(ukfs, rump_sys_mknod(path, mode, dev, &rv));
    335  1.1  pooka }
    336  1.1  pooka 
    337  1.1  pooka int
    338  1.1  pooka ukfs_mkfifo(struct ukfs *ukfs, const char *path, mode_t mode)
    339  1.1  pooka {
    340  1.1  pooka 
    341  1.1  pooka 	STDCALL(ukfs, rump_sys_mkfifo(path, mode, &rv));
    342  1.1  pooka }
    343  1.1  pooka 
    344  1.1  pooka int
    345  1.1  pooka ukfs_mkdir(struct ukfs *ukfs, const char *filename, mode_t mode)
    346  1.1  pooka {
    347  1.1  pooka 
    348  1.1  pooka 	STDCALL(ukfs, rump_sys_mkdir(filename, mode, &rv));
    349  1.1  pooka }
    350  1.1  pooka 
    351  1.1  pooka int
    352  1.1  pooka ukfs_remove(struct ukfs *ukfs, const char *filename)
    353  1.1  pooka {
    354  1.1  pooka 
    355  1.1  pooka 	STDCALL(ukfs, rump_sys_unlink(filename, &rv));
    356  1.1  pooka }
    357  1.1  pooka 
    358  1.1  pooka int
    359  1.1  pooka ukfs_rmdir(struct ukfs *ukfs, const char *filename)
    360  1.1  pooka {
    361  1.1  pooka 
    362  1.1  pooka 	STDCALL(ukfs, rump_sys_rmdir(filename, &rv));
    363  1.1  pooka }
    364  1.1  pooka 
    365  1.1  pooka int
    366  1.1  pooka ukfs_link(struct ukfs *ukfs, const char *filename, const char *f_create)
    367  1.1  pooka {
    368  1.1  pooka 
    369  1.1  pooka 	STDCALL(ukfs, rump_sys_link(filename, f_create, &rv));
    370  1.1  pooka }
    371  1.1  pooka 
    372  1.1  pooka int
    373  1.1  pooka ukfs_symlink(struct ukfs *ukfs, const char *filename, const char *linkname)
    374  1.1  pooka {
    375  1.1  pooka 
    376  1.1  pooka 	STDCALL(ukfs, rump_sys_symlink(filename, linkname, &rv));
    377  1.1  pooka }
    378  1.1  pooka 
    379  1.1  pooka ssize_t
    380  1.1  pooka ukfs_readlink(struct ukfs *ukfs, const char *filename,
    381  1.1  pooka 	char *linkbuf, size_t buflen)
    382  1.1  pooka {
    383  1.1  pooka 	ssize_t rv;
    384  1.1  pooka 	int myerr = 0;
    385  1.1  pooka 
    386  1.1  pooka 	precall(ukfs);
    387  1.1  pooka 	rv = rump_sys_readlink(filename, linkbuf, buflen, &myerr);
    388  1.1  pooka 	postcall(ukfs);
    389  1.1  pooka 	if (myerr) {
    390  1.2  pooka 		errno = myerr;
    391  1.1  pooka 		return -1;
    392  1.1  pooka 	}
    393  1.1  pooka 	return rv;
    394  1.1  pooka }
    395  1.1  pooka 
    396  1.1  pooka int
    397  1.1  pooka ukfs_rename(struct ukfs *ukfs, const char *from, const char *to)
    398  1.1  pooka {
    399  1.1  pooka 
    400  1.1  pooka 	STDCALL(ukfs, rump_sys_rename(from, to, &rv));
    401  1.1  pooka }
    402  1.1  pooka 
    403  1.1  pooka int
    404  1.1  pooka ukfs_chdir(struct ukfs *ukfs, const char *path)
    405  1.1  pooka {
    406  1.1  pooka 	struct vnode *newvp, *oldvp;
    407  1.1  pooka 	int rv;
    408  1.1  pooka 
    409  1.1  pooka 	precall(ukfs);
    410  1.1  pooka 	rump_sys_chdir(path, &rv);
    411  1.1  pooka 	if (rv)
    412  1.1  pooka 		goto out;
    413  1.1  pooka 
    414  1.1  pooka 	newvp = rump_cdir_get();
    415  1.1  pooka 	pthread_spin_lock(&ukfs->ukfs_spin);
    416  1.1  pooka 	oldvp = ukfs->ukfs_cdir;
    417  1.1  pooka 	ukfs->ukfs_cdir = newvp;
    418  1.1  pooka 	pthread_spin_unlock(&ukfs->ukfs_spin);
    419  1.1  pooka 	if (oldvp)
    420  1.1  pooka 		rump_vp_rele(oldvp);
    421  1.1  pooka 
    422  1.1  pooka  out:
    423  1.1  pooka 	postcall(ukfs);
    424  1.1  pooka 	if (rv) {
    425  1.1  pooka 		errno = rv;
    426  1.1  pooka 		return -1;
    427  1.1  pooka 	}
    428  1.1  pooka 	return 0;
    429  1.1  pooka }
    430  1.1  pooka 
    431  1.1  pooka int
    432  1.1  pooka ukfs_stat(struct ukfs *ukfs, const char *filename, struct stat *file_stat)
    433  1.1  pooka {
    434  1.1  pooka 
    435  1.1  pooka 	STDCALL(ukfs, rump_sys___stat30(filename, file_stat, &rv));
    436  1.1  pooka }
    437  1.1  pooka 
    438  1.1  pooka int
    439  1.1  pooka ukfs_lstat(struct ukfs *ukfs, const char *filename, struct stat *file_stat)
    440  1.1  pooka {
    441  1.1  pooka 
    442  1.1  pooka 	STDCALL(ukfs, rump_sys___lstat30(filename, file_stat, &rv));
    443  1.1  pooka }
    444  1.1  pooka 
    445  1.1  pooka int
    446  1.1  pooka ukfs_chmod(struct ukfs *ukfs, const char *filename, mode_t mode)
    447  1.1  pooka {
    448  1.1  pooka 
    449  1.1  pooka 	STDCALL(ukfs, rump_sys_chmod(filename, mode, &rv));
    450  1.1  pooka }
    451  1.1  pooka 
    452  1.1  pooka int
    453  1.1  pooka ukfs_lchmod(struct ukfs *ukfs, const char *filename, mode_t mode)
    454  1.1  pooka {
    455  1.1  pooka 
    456  1.1  pooka 	STDCALL(ukfs, rump_sys_lchmod(filename, mode, &rv));
    457  1.1  pooka }
    458  1.1  pooka 
    459  1.1  pooka int
    460  1.1  pooka ukfs_chown(struct ukfs *ukfs, const char *filename, uid_t uid, gid_t gid)
    461  1.1  pooka {
    462  1.1  pooka 
    463  1.1  pooka 	STDCALL(ukfs, rump_sys_chown(filename, uid, gid, &rv));
    464  1.1  pooka }
    465  1.1  pooka 
    466  1.1  pooka int
    467  1.1  pooka ukfs_lchown(struct ukfs *ukfs, const char *filename, uid_t uid, gid_t gid)
    468  1.1  pooka {
    469  1.1  pooka 
    470  1.1  pooka 	STDCALL(ukfs, rump_sys_lchown(filename, uid, gid, &rv));
    471  1.1  pooka }
    472  1.1  pooka 
    473  1.1  pooka int
    474  1.1  pooka ukfs_chflags(struct ukfs *ukfs, const char *filename, u_long flags)
    475  1.1  pooka {
    476  1.1  pooka 
    477  1.1  pooka 	STDCALL(ukfs, rump_sys_chflags(filename, flags, &rv));
    478  1.1  pooka }
    479  1.1  pooka 
    480  1.1  pooka int
    481  1.1  pooka ukfs_lchflags(struct ukfs *ukfs, const char *filename, u_long flags)
    482  1.1  pooka {
    483  1.1  pooka 
    484  1.1  pooka 	STDCALL(ukfs, rump_sys_lchflags(filename, flags, &rv));
    485  1.1  pooka }
    486  1.1  pooka 
    487  1.1  pooka int
    488  1.1  pooka ukfs_utimes(struct ukfs *ukfs, const char *filename, const struct timeval *tptr)
    489  1.1  pooka {
    490  1.1  pooka 
    491  1.1  pooka 	STDCALL(ukfs, rump_sys_utimes(filename, tptr, &rv));
    492  1.1  pooka }
    493  1.1  pooka 
    494  1.1  pooka int
    495  1.1  pooka ukfs_lutimes(struct ukfs *ukfs, const char *filename,
    496  1.1  pooka 	      const struct timeval *tptr)
    497  1.1  pooka {
    498  1.1  pooka 
    499  1.1  pooka 	STDCALL(ukfs, rump_sys_lutimes(filename, tptr, &rv));
    500  1.1  pooka }
    501  1.1  pooka 
    502  1.3  pooka /*
    503  1.3  pooka  * Dynamic module support
    504  1.3  pooka  */
    505  1.3  pooka 
    506  1.3  pooka /* load one library */
    507  1.3  pooka 
    508  1.3  pooka /*
    509  1.3  pooka  * XXX: the dlerror stuff isn't really threadsafe, but then again I
    510  1.3  pooka  * can't protect against other threads calling dl*() outside of ukfs,
    511  1.3  pooka  * so just live with it being flimsy
    512  1.3  pooka  */
    513  1.3  pooka #define UFSLIB "librumpfs_ufs.so"
    514  1.3  pooka int
    515  1.3  pooka ukfs_modload(const char *fname)
    516  1.3  pooka {
    517  1.3  pooka 	void *handle, *thesym;
    518  1.3  pooka 	struct stat sb;
    519  1.3  pooka 	const char *p;
    520  1.3  pooka 	int error;
    521  1.3  pooka 
    522  1.3  pooka 	if (stat(fname, &sb) == -1)
    523  1.3  pooka 		return -1;
    524  1.3  pooka 
    525  1.3  pooka 	handle = dlopen(fname, RTLD_GLOBAL);
    526  1.3  pooka 	if (handle == NULL) {
    527  1.3  pooka 		if (strstr(dlerror(), "Undefined symbol"))
    528  1.3  pooka 			return 0;
    529  1.3  pooka 		warnx("dlopen %s failed: %s\n", fname, dlerror());
    530  1.3  pooka 		/* XXXerrno */
    531  1.3  pooka 		return -1;
    532  1.3  pooka 	}
    533  1.3  pooka 
    534  1.3  pooka 	/*
    535  1.3  pooka 	 * XXX: the ufs module is not loaded in the same fashion as the
    536  1.3  pooka 	 * others.  But we can't do dlclose() for it, since that would
    537  1.3  pooka 	 * lead to not being able to load ffs/ext2fs/lfs.  Hence hardcode
    538  1.3  pooka 	 * and kludge around the issue for now.  But this should really
    539  1.3  pooka 	 * be fixed by fixing sys/ufs/ufs to be a kernel module.
    540  1.3  pooka 	 */
    541  1.3  pooka 	if ((p = strrchr(fname, '/')) != NULL)
    542  1.3  pooka 		p++;
    543  1.3  pooka 	else
    544  1.3  pooka 		p = fname;
    545  1.3  pooka 	if (strcmp(p, UFSLIB) == 0)
    546  1.3  pooka 		return 1;
    547  1.3  pooka 
    548  1.3  pooka 	thesym = dlsym(handle, "__start_link_set_modules");
    549  1.3  pooka 	if (thesym) {
    550  1.3  pooka 		error = rump_vfs_load(thesym);
    551  1.3  pooka 		if (error)
    552  1.3  pooka 			goto errclose;
    553  1.3  pooka 		return 1;
    554  1.3  pooka 	}
    555  1.3  pooka 	error = EINVAL;
    556  1.3  pooka 
    557  1.3  pooka  errclose:
    558  1.3  pooka 	dlclose(handle);
    559  1.3  pooka 	errno = error;
    560  1.3  pooka 	return -1;
    561  1.3  pooka }
    562  1.3  pooka 
    563  1.3  pooka struct loadfail {
    564  1.3  pooka 	char *pname;
    565  1.3  pooka 
    566  1.3  pooka 	LIST_ENTRY(loadfail) entries;
    567  1.3  pooka };
    568  1.3  pooka 
    569  1.3  pooka #define RUMPFSMOD_PREFIX "librumpfs_"
    570  1.3  pooka #define RUMPFSMOD_SUFFIX ".so"
    571  1.3  pooka 
    572  1.3  pooka int
    573  1.3  pooka ukfs_modload_dir(const char *dir)
    574  1.3  pooka {
    575  1.3  pooka 	char nbuf[MAXPATHLEN+1], *p;
    576  1.3  pooka 	struct dirent entry, *result;
    577  1.3  pooka 	DIR *libdir;
    578  1.3  pooka 	struct loadfail *lf, *nlf;
    579  1.3  pooka 	int error, nloaded = 0, redo;
    580  1.3  pooka 	LIST_HEAD(, loadfail) lfs;
    581  1.3  pooka 
    582  1.3  pooka 	libdir = opendir(dir);
    583  1.3  pooka 	if (libdir == NULL)
    584  1.3  pooka 		return -1;
    585  1.3  pooka 
    586  1.3  pooka 	LIST_INIT(&lfs);
    587  1.3  pooka 	for (;;) {
    588  1.3  pooka 		if ((error = readdir_r(libdir, &entry, &result)) != 0)
    589  1.3  pooka 			break;
    590  1.3  pooka 		if (!result)
    591  1.3  pooka 			break;
    592  1.3  pooka 		if (strncmp(result->d_name, RUMPFSMOD_PREFIX,
    593  1.3  pooka 		    strlen(RUMPFSMOD_PREFIX)) != 0)
    594  1.3  pooka 			continue;
    595  1.3  pooka 		if (((p = strstr(result->d_name, RUMPFSMOD_SUFFIX)) == NULL)
    596  1.3  pooka 		    || strlen(p) != strlen(RUMPFSMOD_SUFFIX))
    597  1.3  pooka 			continue;
    598  1.3  pooka 		strlcpy(nbuf, dir, sizeof(nbuf));
    599  1.3  pooka 		strlcat(nbuf, "/", sizeof(nbuf));
    600  1.3  pooka 		strlcat(nbuf, result->d_name, sizeof(nbuf));
    601  1.3  pooka 		switch (ukfs_modload(nbuf)) {
    602  1.3  pooka 		case 0:
    603  1.3  pooka 			lf = malloc(sizeof(*lf));
    604  1.3  pooka 			if (lf == NULL) {
    605  1.3  pooka 				error = ENOMEM;
    606  1.3  pooka 				break;
    607  1.3  pooka 			}
    608  1.3  pooka 			lf->pname = strdup(nbuf);
    609  1.3  pooka 			if (lf->pname == NULL) {
    610  1.3  pooka 				free(lf);
    611  1.3  pooka 				error = ENOMEM;
    612  1.3  pooka 				break;
    613  1.3  pooka 			}
    614  1.3  pooka 			LIST_INSERT_HEAD(&lfs, lf, entries);
    615  1.3  pooka 			break;
    616  1.3  pooka 		case 1:
    617  1.3  pooka 			nloaded++;
    618  1.3  pooka 			break;
    619  1.3  pooka 		default:
    620  1.3  pooka 			/* ignore errors */
    621  1.3  pooka 			break;
    622  1.3  pooka 		}
    623  1.3  pooka 	}
    624  1.3  pooka 	closedir(libdir);
    625  1.3  pooka 	if (error && nloaded != 0)
    626  1.3  pooka 		error = 0;
    627  1.3  pooka 
    628  1.3  pooka 	/*
    629  1.3  pooka 	 * El-cheapo dependency calculator.  Just try to load the
    630  1.3  pooka 	 * modules n times in a loop
    631  1.3  pooka 	 */
    632  1.3  pooka 	for (redo = 1; redo;) {
    633  1.3  pooka 		redo = 0;
    634  1.3  pooka 		nlf = LIST_FIRST(&lfs);
    635  1.3  pooka 		while ((lf = nlf) != NULL) {
    636  1.3  pooka 			nlf = LIST_NEXT(lf, entries);
    637  1.3  pooka 			if (ukfs_modload(lf->pname) == 1) {
    638  1.3  pooka 				nloaded++;
    639  1.3  pooka 				redo = 1;
    640  1.3  pooka 				LIST_REMOVE(lf, entries);
    641  1.3  pooka 				free(lf->pname);
    642  1.3  pooka 				free(lf);
    643  1.3  pooka 			}
    644  1.3  pooka 		}
    645  1.3  pooka 	}
    646  1.3  pooka 
    647  1.3  pooka 	while ((lf = LIST_FIRST(&lfs)) != NULL) {
    648  1.3  pooka 		LIST_REMOVE(lf, entries);
    649  1.3  pooka 		free(lf->pname);
    650  1.3  pooka 		free(lf);
    651  1.3  pooka 	}
    652  1.3  pooka 
    653  1.3  pooka 	if (error && nloaded == 0) {
    654  1.3  pooka 		errno = error;
    655  1.3  pooka 		return -1;
    656  1.3  pooka 	}
    657  1.3  pooka 
    658  1.3  pooka 	return nloaded;
    659  1.3  pooka }
    660  1.3  pooka 
    661  1.4  pooka /* XXX: this code uses definitions from NetBSD, needs rumpdefs */
    662  1.4  pooka ssize_t
    663  1.4  pooka ukfs_vfstypes(char *buf, size_t buflen)
    664  1.4  pooka {
    665  1.4  pooka 	int mib[3];
    666  1.4  pooka 	struct sysctlnode q, ans[128];
    667  1.4  pooka 	size_t alen;
    668  1.4  pooka 	int error, i;
    669  1.4  pooka 
    670  1.4  pooka 	mib[0] = CTL_VFS;
    671  1.4  pooka 	mib[1] = VFS_GENERIC;
    672  1.4  pooka 	mib[2] = CTL_QUERY;
    673  1.4  pooka 	alen = sizeof(ans);
    674  1.4  pooka 
    675  1.4  pooka 	memset(&q, 0, sizeof(q));
    676  1.4  pooka 	q.sysctl_flags = SYSCTL_VERSION;
    677  1.4  pooka 
    678  1.4  pooka 	if (rump_sys___sysctl(mib, 3, ans, &alen, &q, sizeof(q), &error) == -1){
    679  1.4  pooka 		errno = error;
    680  1.4  pooka 		return -1;
    681  1.4  pooka 	}
    682  1.4  pooka 
    683  1.4  pooka 	for (i = 0; i < alen/sizeof(ans[0]); i++)
    684  1.4  pooka 		if (strcmp("fstypes", ans[i].sysctl_name) == 0)
    685  1.4  pooka 			break;
    686  1.4  pooka 	if (i == alen/sizeof(ans[0])) {
    687  1.4  pooka 		errno = ENXIO;
    688  1.4  pooka 		return -1;
    689  1.4  pooka 	}
    690  1.4  pooka 
    691  1.4  pooka 	mib[0] = CTL_VFS;
    692  1.4  pooka 	mib[1] = VFS_GENERIC;
    693  1.4  pooka 	mib[2] = ans[i].sysctl_num;
    694  1.4  pooka 
    695  1.4  pooka 	if (rump_sys___sysctl(mib, 3, buf, &buflen, NULL, 0, &error) == -1) {
    696  1.4  pooka 		errno = error;
    697  1.4  pooka 		return -1;
    698  1.4  pooka 	}
    699  1.4  pooka 
    700  1.4  pooka 	return buflen;
    701  1.4  pooka }
    702  1.3  pooka 
    703  1.3  pooka /*
    704  1.3  pooka  * Utilities
    705  1.3  pooka  */
    706  1.1  pooka int
    707  1.1  pooka ukfs_util_builddirs(struct ukfs *ukfs, const char *pathname, mode_t mode)
    708  1.1  pooka {
    709  1.1  pooka 	char *f1, *f2;
    710  1.1  pooka 	int rv;
    711  1.1  pooka 	mode_t mask;
    712  1.1  pooka 	bool end;
    713  1.1  pooka 
    714  1.1  pooka 	/*ukfs_umask((mask = ukfs_umask(0)));*/
    715  1.1  pooka 	umask((mask = umask(0)));
    716  1.1  pooka 
    717  1.1  pooka 	f1 = f2 = strdup(pathname);
    718  1.1  pooka 	if (f1 == NULL) {
    719  1.1  pooka 		errno = ENOMEM;
    720  1.1  pooka 		return -1;
    721  1.1  pooka 	}
    722  1.1  pooka 
    723  1.1  pooka 	end = false;
    724  1.1  pooka 	for (;;) {
    725  1.1  pooka 		/* find next component */
    726  1.1  pooka 		f2 += strspn(f2, "/");
    727  1.1  pooka 		f2 += strcspn(f2, "/");
    728  1.1  pooka 		if (*f2 == '\0')
    729  1.1  pooka 			end = true;
    730  1.1  pooka 		else
    731  1.1  pooka 			*f2 = '\0';
    732  1.1  pooka 
    733  1.1  pooka 		rv = ukfs_mkdir(ukfs, f1, mode & ~mask);
    734  1.1  pooka 		if (errno == EEXIST)
    735  1.1  pooka 			rv = 0;
    736  1.1  pooka 
    737  1.1  pooka 		if (rv == -1 || *f2 != '\0' || end)
    738  1.1  pooka 			break;
    739  1.1  pooka 
    740  1.1  pooka 		*f2 = '/';
    741  1.1  pooka 	}
    742  1.1  pooka 
    743  1.1  pooka 	free(f1);
    744  1.1  pooka 
    745  1.1  pooka 	return rv;
    746  1.1  pooka }
    747