Home | History | Annotate | Line # | Download | only in libukfs
ukfs.c revision 1.30
      1  1.30     pooka /*	$NetBSD: ukfs.c,v 1.30 2009/07/22 20:46:34 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.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.1     pooka #define UKFS_MODE_DEFAULT 0555
     67   1.1     pooka 
     68   1.1     pooka struct ukfs {
     69   1.1     pooka 	struct mount *ukfs_mp;
     70   1.1     pooka 	struct vnode *ukfs_rvp;
     71   1.1     pooka 
     72   1.1     pooka 	pthread_spinlock_t ukfs_spin;
     73   1.1     pooka 	pid_t ukfs_nextpid;
     74   1.1     pooka 	struct vnode *ukfs_cdir;
     75  1.11     pooka 	int ukfs_devfd;
     76  1.30     pooka 	char *ukfs_devpath;
     77  1.30     pooka 	char *ukfs_mountpath;
     78   1.1     pooka };
     79   1.1     pooka 
     80  1.30     pooka static int builddirs(const char *, mode_t,
     81  1.30     pooka     int (*mkdirfn)(struct ukfs *, const char *, mode_t), struct ukfs *);
     82  1.30     pooka 
     83   1.1     pooka struct mount *
     84   1.1     pooka ukfs_getmp(struct ukfs *ukfs)
     85   1.1     pooka {
     86   1.1     pooka 
     87   1.1     pooka 	return ukfs->ukfs_mp;
     88   1.1     pooka }
     89   1.1     pooka 
     90   1.1     pooka struct vnode *
     91   1.1     pooka ukfs_getrvp(struct ukfs *ukfs)
     92   1.1     pooka {
     93   1.1     pooka 	struct vnode *rvp;
     94   1.1     pooka 
     95   1.1     pooka 	rvp = ukfs->ukfs_rvp;
     96   1.1     pooka 	rump_vp_incref(rvp);
     97   1.1     pooka 
     98   1.1     pooka 	return rvp;
     99   1.1     pooka }
    100   1.1     pooka 
    101  1.20     pooka #ifdef DONT_WANT_PTHREAD_LINKAGE
    102  1.20     pooka #define pthread_spin_lock(a)
    103  1.20     pooka #define pthread_spin_unlock(a)
    104  1.20     pooka #define pthread_spin_init(a,b)
    105  1.20     pooka #define pthread_spin_destroy(a)
    106  1.20     pooka #endif
    107  1.20     pooka 
    108   1.1     pooka static pid_t
    109   1.1     pooka nextpid(struct ukfs *ukfs)
    110   1.1     pooka {
    111   1.1     pooka 	pid_t npid;
    112   1.1     pooka 
    113   1.1     pooka 	pthread_spin_lock(&ukfs->ukfs_spin);
    114   1.5     pooka 	if (ukfs->ukfs_nextpid == 0)
    115   1.5     pooka 		ukfs->ukfs_nextpid++;
    116   1.1     pooka 	npid = ukfs->ukfs_nextpid++;
    117   1.1     pooka 	pthread_spin_unlock(&ukfs->ukfs_spin);
    118   1.1     pooka 
    119   1.1     pooka 	return npid;
    120   1.1     pooka }
    121   1.1     pooka 
    122   1.1     pooka static void
    123   1.1     pooka precall(struct ukfs *ukfs)
    124   1.1     pooka {
    125   1.1     pooka 	struct vnode *rvp, *cvp;
    126   1.1     pooka 
    127   1.1     pooka 	rump_setup_curlwp(nextpid(ukfs), 1, 1);
    128   1.1     pooka 	rvp = ukfs_getrvp(ukfs);
    129   1.1     pooka 	pthread_spin_lock(&ukfs->ukfs_spin);
    130   1.1     pooka 	cvp = ukfs->ukfs_cdir;
    131   1.1     pooka 	pthread_spin_unlock(&ukfs->ukfs_spin);
    132   1.1     pooka 	rump_rcvp_set(rvp, cvp); /* takes refs */
    133   1.1     pooka 	rump_vp_rele(rvp);
    134   1.1     pooka }
    135   1.1     pooka 
    136   1.1     pooka static void
    137   1.1     pooka postcall(struct ukfs *ukfs)
    138   1.1     pooka {
    139   1.1     pooka 	struct vnode *rvp;
    140   1.1     pooka 
    141   1.1     pooka 	rvp = ukfs_getrvp(ukfs);
    142   1.1     pooka 	rump_rcvp_set(NULL, rvp);
    143   1.1     pooka 	rump_vp_rele(rvp);
    144   1.1     pooka 	rump_clear_curlwp();
    145   1.1     pooka }
    146   1.1     pooka 
    147   1.1     pooka int
    148  1.10     pooka _ukfs_init(int version)
    149   1.1     pooka {
    150  1.10     pooka 	int rv;
    151  1.10     pooka 
    152  1.10     pooka 	if (version != UKFS_VERSION) {
    153  1.10     pooka 		printf("incompatible ukfs version, %d vs. %d\n",
    154  1.10     pooka 		    version, UKFS_VERSION);
    155  1.10     pooka 		errno = EPROGMISMATCH;
    156  1.10     pooka 		return -1;
    157  1.10     pooka 	}
    158   1.1     pooka 
    159  1.10     pooka 	if ((rv = rump_init()) != 0) {
    160  1.10     pooka 		errno = rv;
    161  1.10     pooka 		return -1;
    162  1.10     pooka 	}
    163   1.1     pooka 
    164   1.1     pooka 	return 0;
    165   1.1     pooka }
    166   1.1     pooka 
    167  1.30     pooka static int
    168  1.30     pooka rumpmkdir(struct ukfs *dummy, const char *path, mode_t mode)
    169  1.30     pooka {
    170  1.30     pooka 
    171  1.30     pooka 	return rump_sys_mkdir(path, mode);
    172  1.30     pooka }
    173  1.30     pooka 
    174   1.1     pooka struct ukfs *
    175   1.1     pooka ukfs_mount(const char *vfsname, const char *devpath, const char *mountpath,
    176   1.1     pooka 	int mntflags, void *arg, size_t alen)
    177   1.1     pooka {
    178  1.22     pooka 	struct stat sb;
    179   1.1     pooka 	struct ukfs *fs = NULL;
    180  1.11     pooka 	int rv = 0, devfd = -1, rdonly;
    181  1.30     pooka 	int mounted = 0;
    182   1.1     pooka 
    183  1.11     pooka 	/*
    184  1.11     pooka 	 * Try open and lock the device.  if we can't open it, assume
    185  1.11     pooka 	 * it's a file system which doesn't use a real device and let
    186  1.11     pooka 	 * it slide.  The mount will fail anyway if the fs requires a
    187  1.11     pooka 	 * device.
    188  1.11     pooka 	 *
    189  1.11     pooka 	 * XXX: strictly speaking this is not 100% correct, as virtual
    190  1.11     pooka 	 * file systems can use a device path which does exist and can
    191  1.11     pooka 	 * be opened.  E.g. tmpfs should be mountable multiple times
    192  1.11     pooka 	 * with "device" path "/swap", but now isn't.  But I think the
    193  1.11     pooka 	 * chances are so low that it's currently acceptable to let
    194  1.11     pooka 	 * this one slip.
    195  1.11     pooka 	 */
    196  1.11     pooka 	rdonly = mntflags & MNT_RDONLY;
    197  1.11     pooka 	devfd = open(devpath, rdonly ? O_RDONLY : O_RDWR);
    198  1.11     pooka 	if (devfd != -1) {
    199  1.22     pooka 		if (fstat(devfd, &sb) == -1) {
    200  1.11     pooka 			close(devfd);
    201  1.11     pooka 			devfd = -1;
    202  1.11     pooka 			rv = errno;
    203  1.11     pooka 			goto out;
    204  1.11     pooka 		}
    205  1.22     pooka 
    206  1.22     pooka 		/*
    207  1.22     pooka 		 * We do this only for non-block device since the
    208  1.22     pooka 		 * (NetBSD) kernel allows block device open only once.
    209  1.22     pooka 		 */
    210  1.22     pooka 		if (!S_ISBLK(sb.st_mode)) {
    211  1.22     pooka 			if (flock(devfd, LOCK_NB | (rdonly ? LOCK_SH:LOCK_EX))
    212  1.22     pooka 			    == -1) {
    213  1.22     pooka 				warnx("ukfs_mount: cannot get %s lock on "
    214  1.22     pooka 				    "device", rdonly ? "shared" : "exclusive");
    215  1.22     pooka 				close(devfd);
    216  1.22     pooka 				devfd = -1;
    217  1.22     pooka 				rv = errno;
    218  1.22     pooka 				goto out;
    219  1.22     pooka 			}
    220  1.22     pooka 		} else {
    221  1.22     pooka 			close(devfd);
    222  1.22     pooka 			devfd = -1;
    223  1.22     pooka 		}
    224  1.11     pooka 	}
    225   1.1     pooka 
    226   1.1     pooka 	fs = malloc(sizeof(struct ukfs));
    227   1.1     pooka 	if (fs == NULL) {
    228   1.1     pooka 		rv = ENOMEM;
    229   1.1     pooka 		goto out;
    230   1.1     pooka 	}
    231   1.1     pooka 	memset(fs, 0, sizeof(struct ukfs));
    232  1.30     pooka 
    233  1.30     pooka 	/* create our mountpoint.  this is never removed. */
    234  1.30     pooka 	if (builddirs(mountpath, 0777, rumpmkdir, NULL) == -1) {
    235  1.30     pooka 		if (errno != EEXIST) {
    236  1.30     pooka 			rv = errno;
    237  1.30     pooka 			goto out;
    238  1.30     pooka 		}
    239  1.30     pooka 	}
    240   1.1     pooka 
    241   1.1     pooka 	rump_fakeblk_register(devpath);
    242  1.30     pooka 	rv = rump_sys_mount(vfsname, mountpath, mntflags, arg, alen);
    243   1.1     pooka 	if (rv) {
    244   1.1     pooka 		goto out;
    245   1.1     pooka 	}
    246  1.30     pooka 	mounted = 1;
    247  1.30     pooka 	rv = rump_vfs_getmp(mountpath, &fs->ukfs_mp);
    248  1.11     pooka 	if (rv) {
    249  1.11     pooka 		goto out;
    250  1.11     pooka 	}
    251  1.30     pooka 	rv = rump_vfs_root(fs->ukfs_mp, &fs->ukfs_rvp, 0);
    252  1.30     pooka 	if (rv) {
    253  1.30     pooka 		goto out;
    254  1.30     pooka 	}
    255  1.30     pooka 
    256  1.30     pooka 	fs->ukfs_devpath = strdup(devpath);
    257  1.30     pooka 	fs->ukfs_mountpath = strdup(mountpath);
    258  1.11     pooka 	fs->ukfs_cdir = ukfs_getrvp(fs);
    259  1.11     pooka 	pthread_spin_init(&fs->ukfs_spin, PTHREAD_PROCESS_SHARED);
    260  1.11     pooka 	fs->ukfs_devfd = devfd;
    261  1.11     pooka 	assert(rv == 0);
    262   1.1     pooka 
    263   1.1     pooka  out:
    264   1.1     pooka 	if (rv) {
    265  1.30     pooka 		if (fs) {
    266  1.30     pooka 			if (fs->ukfs_rvp)
    267  1.30     pooka 				rump_vp_rele(fs->ukfs_rvp);
    268   1.1     pooka 			free(fs);
    269  1.30     pooka 			fs = NULL;
    270  1.30     pooka 		}
    271  1.30     pooka 		if (mounted)
    272  1.30     pooka 			rump_sys_unmount(mountpath, MNT_FORCE);
    273  1.11     pooka 		if (devfd != -1) {
    274  1.11     pooka 			flock(devfd, LOCK_UN);
    275  1.11     pooka 			close(devfd);
    276  1.11     pooka 		}
    277  1.30     pooka 		errno = rv;
    278   1.1     pooka 	}
    279   1.1     pooka 
    280   1.1     pooka 	return fs;
    281   1.1     pooka }
    282   1.1     pooka 
    283  1.30     pooka int
    284   1.1     pooka ukfs_release(struct ukfs *fs, int flags)
    285   1.1     pooka {
    286   1.1     pooka 
    287   1.1     pooka 	if ((flags & UKFS_RELFLAG_NOUNMOUNT) == 0) {
    288  1.30     pooka 		int rv, mntflag;
    289   1.9     pooka 
    290  1.30     pooka 		ukfs_chdir(fs, "/");
    291  1.30     pooka 		mntflag = 0;
    292  1.30     pooka 		if (flags & UKFS_RELFLAG_FORCE)
    293  1.30     pooka 			mntflag = MNT_FORCE;
    294  1.30     pooka 		rv = rump_sys_unmount(fs->ukfs_mountpath, mntflag);
    295  1.30     pooka 		if (rv) {
    296  1.30     pooka 			ukfs_chdir(fs, fs->ukfs_mountpath);
    297  1.30     pooka 			errno = rv;
    298  1.30     pooka 			return -1;
    299  1.30     pooka 		}
    300   1.1     pooka 	}
    301   1.1     pooka 
    302  1.30     pooka 	rump_fakeblk_deregister(fs->ukfs_devpath);
    303  1.30     pooka 	free(fs->ukfs_devpath);
    304  1.30     pooka 	free(fs->ukfs_mountpath);
    305   1.1     pooka 
    306   1.1     pooka 	pthread_spin_destroy(&fs->ukfs_spin);
    307  1.16  stacktic 	if (fs->ukfs_devfd != -1) {
    308  1.11     pooka 		flock(fs->ukfs_devfd, LOCK_UN);
    309  1.16  stacktic 		close(fs->ukfs_devfd);
    310  1.16  stacktic 	}
    311   1.1     pooka 	free(fs);
    312  1.30     pooka 
    313  1.30     pooka 	return 0;
    314   1.1     pooka }
    315   1.1     pooka 
    316   1.1     pooka #define STDCALL(ukfs, thecall)						\
    317   1.1     pooka 	int rv = 0;							\
    318   1.1     pooka 									\
    319   1.1     pooka 	precall(ukfs);							\
    320  1.21     pooka 	rv = thecall;							\
    321   1.1     pooka 	postcall(ukfs);							\
    322  1.21     pooka 	return rv;
    323   1.1     pooka 
    324   1.1     pooka int
    325  1.24     pooka ukfs_opendir(struct ukfs *ukfs, const char *dirname, struct ukfs_dircookie **c)
    326   1.1     pooka {
    327   1.1     pooka 	struct vnode *vp;
    328  1.24     pooka 	int rv;
    329   1.1     pooka 
    330   1.1     pooka 	precall(ukfs);
    331   1.1     pooka 	rv = rump_namei(RUMP_NAMEI_LOOKUP, RUMP_NAMEI_LOCKLEAF, dirname,
    332   1.1     pooka 	    NULL, &vp, NULL);
    333   1.1     pooka 	postcall(ukfs);
    334  1.24     pooka 
    335  1.24     pooka 	if (rv == 0) {
    336  1.24     pooka 		RUMP_VOP_UNLOCK(vp, 0);
    337  1.24     pooka 	} else {
    338  1.24     pooka 		errno = rv;
    339  1.24     pooka 		rv = -1;
    340  1.24     pooka 	}
    341  1.24     pooka 
    342  1.24     pooka 	/*LINTED*/
    343  1.24     pooka 	*c = (struct ukfs_dircookie *)vp;
    344  1.24     pooka 	return rv;
    345  1.24     pooka }
    346  1.24     pooka 
    347  1.24     pooka static int
    348  1.24     pooka getmydents(struct vnode *vp, off_t *off, uint8_t *buf, size_t bufsize)
    349  1.24     pooka {
    350  1.24     pooka 	struct uio *uio;
    351  1.24     pooka 	size_t resid;
    352  1.24     pooka 	int rv, eofflag;
    353  1.24     pooka 	kauth_cred_t cred;
    354  1.24     pooka 
    355   1.1     pooka 	uio = rump_uio_setup(buf, bufsize, *off, RUMPUIO_READ);
    356   1.9     pooka 	cred = rump_cred_suserget();
    357   1.9     pooka 	rv = RUMP_VOP_READDIR(vp, uio, cred, &eofflag, NULL, NULL);
    358   1.9     pooka 	rump_cred_suserput(cred);
    359  1.17     pooka 	RUMP_VOP_UNLOCK(vp, 0);
    360   1.1     pooka 	*off = rump_uio_getoff(uio);
    361   1.1     pooka 	resid = rump_uio_free(uio);
    362   1.1     pooka 
    363   1.1     pooka 	if (rv) {
    364   1.1     pooka 		errno = rv;
    365   1.1     pooka 		return -1;
    366   1.1     pooka 	}
    367   1.1     pooka 
    368   1.1     pooka 	/* LINTED: not totally correct return type, but follows syscall */
    369   1.1     pooka 	return bufsize - resid;
    370   1.1     pooka }
    371   1.1     pooka 
    372  1.24     pooka /*ARGSUSED*/
    373  1.24     pooka int
    374  1.24     pooka ukfs_getdents_cookie(struct ukfs *ukfs, struct ukfs_dircookie *c, off_t *off,
    375  1.24     pooka 	uint8_t *buf, size_t bufsize)
    376  1.24     pooka {
    377  1.24     pooka 	/*LINTED*/
    378  1.24     pooka 	struct vnode *vp = (struct vnode *)c;
    379  1.24     pooka 
    380  1.24     pooka 	RUMP_VOP_LOCK(vp, RUMP_LK_SHARED);
    381  1.24     pooka 	return getmydents(vp, off, buf, bufsize);
    382  1.24     pooka }
    383  1.24     pooka 
    384  1.24     pooka int
    385  1.24     pooka ukfs_getdents(struct ukfs *ukfs, const char *dirname, off_t *off,
    386  1.24     pooka 	uint8_t *buf, size_t bufsize)
    387  1.24     pooka {
    388  1.24     pooka 	struct vnode *vp;
    389  1.24     pooka 	int rv;
    390  1.24     pooka 
    391  1.24     pooka 	precall(ukfs);
    392  1.24     pooka 	rv = rump_namei(RUMP_NAMEI_LOOKUP, RUMP_NAMEI_LOCKLEAF, dirname,
    393  1.24     pooka 	    NULL, &vp, NULL);
    394  1.24     pooka 	postcall(ukfs);
    395  1.24     pooka 	if (rv) {
    396  1.24     pooka 		errno = rv;
    397  1.24     pooka 		return -1;
    398  1.24     pooka 	}
    399  1.24     pooka 
    400  1.24     pooka 	rv = getmydents(vp, off, buf, bufsize);
    401  1.24     pooka 	rump_vp_rele(vp);
    402  1.24     pooka 	return rv;
    403  1.24     pooka }
    404  1.24     pooka 
    405  1.24     pooka /*ARGSUSED*/
    406  1.24     pooka int
    407  1.24     pooka ukfs_closedir(struct ukfs *ukfs, struct ukfs_dircookie *c)
    408  1.24     pooka {
    409  1.24     pooka 
    410  1.24     pooka 	/*LINTED*/
    411  1.24     pooka 	rump_vp_rele((struct vnode *)c);
    412  1.24     pooka 	return 0;
    413  1.24     pooka }
    414  1.24     pooka 
    415  1.24     pooka int
    416  1.24     pooka ukfs_open(struct ukfs *ukfs, const char *filename, int flags)
    417  1.24     pooka {
    418  1.24     pooka 	int fd;
    419  1.24     pooka 
    420  1.24     pooka 	precall(ukfs);
    421  1.24     pooka 	fd = rump_sys_open(filename, flags, 0);
    422  1.24     pooka 	postcall(ukfs);
    423  1.24     pooka 	if (fd == -1)
    424  1.24     pooka 		return -1;
    425  1.24     pooka 
    426  1.24     pooka 	return fd;
    427  1.24     pooka }
    428  1.24     pooka 
    429   1.1     pooka ssize_t
    430   1.1     pooka ukfs_read(struct ukfs *ukfs, const char *filename, off_t off,
    431   1.1     pooka 	uint8_t *buf, size_t bufsize)
    432   1.1     pooka {
    433  1.21     pooka 	int fd;
    434   1.1     pooka 	ssize_t xfer = -1; /* XXXgcc */
    435   1.1     pooka 
    436   1.1     pooka 	precall(ukfs);
    437  1.21     pooka 	fd = rump_sys_open(filename, RUMP_O_RDONLY, 0);
    438  1.21     pooka 	if (fd == -1)
    439   1.1     pooka 		goto out;
    440   1.1     pooka 
    441  1.27     pooka 	xfer = rump_sys_pread(fd, buf, bufsize, off);
    442  1.21     pooka 	rump_sys_close(fd);
    443   1.1     pooka 
    444   1.1     pooka  out:
    445   1.1     pooka 	postcall(ukfs);
    446  1.21     pooka 	if (fd == -1) {
    447   1.1     pooka 		return -1;
    448   1.1     pooka 	}
    449   1.1     pooka 	return xfer;
    450   1.1     pooka }
    451   1.1     pooka 
    452  1.24     pooka /*ARGSUSED*/
    453  1.24     pooka ssize_t
    454  1.24     pooka ukfs_read_fd(struct ukfs *ukfs, int fd, off_t off, uint8_t *buf, size_t buflen)
    455  1.24     pooka {
    456  1.24     pooka 
    457  1.27     pooka 	return rump_sys_pread(fd, buf, buflen, off);
    458  1.24     pooka }
    459  1.24     pooka 
    460   1.1     pooka ssize_t
    461   1.1     pooka ukfs_write(struct ukfs *ukfs, const char *filename, off_t off,
    462   1.1     pooka 	uint8_t *buf, size_t bufsize)
    463   1.1     pooka {
    464  1.21     pooka 	int fd;
    465   1.1     pooka 	ssize_t xfer = -1; /* XXXgcc */
    466   1.1     pooka 
    467   1.1     pooka 	precall(ukfs);
    468  1.21     pooka 	fd = rump_sys_open(filename, RUMP_O_WRONLY, 0);
    469  1.21     pooka 	if (fd == -1)
    470   1.1     pooka 		goto out;
    471   1.1     pooka 
    472   1.1     pooka 	/* write and commit */
    473  1.27     pooka 	xfer = rump_sys_pwrite(fd, buf, bufsize, off);
    474  1.21     pooka 	if (xfer > 0)
    475  1.21     pooka 		rump_sys_fsync(fd);
    476   1.1     pooka 
    477  1.21     pooka 	rump_sys_close(fd);
    478   1.1     pooka 
    479   1.1     pooka  out:
    480   1.1     pooka 	postcall(ukfs);
    481  1.21     pooka 	if (fd == -1) {
    482   1.1     pooka 		return -1;
    483   1.1     pooka 	}
    484   1.1     pooka 	return xfer;
    485   1.1     pooka }
    486   1.1     pooka 
    487  1.24     pooka /*ARGSUSED*/
    488  1.24     pooka ssize_t
    489  1.24     pooka ukfs_write_fd(struct ukfs *ukfs, int fd, off_t off, uint8_t *buf, size_t buflen,
    490  1.24     pooka 	int dosync)
    491  1.24     pooka {
    492  1.24     pooka 	ssize_t xfer;
    493  1.24     pooka 
    494  1.27     pooka 	xfer = rump_sys_pwrite(fd, buf, buflen, off);
    495  1.24     pooka 	if (xfer > 0 && dosync)
    496  1.24     pooka 		rump_sys_fsync(fd);
    497  1.24     pooka 
    498  1.24     pooka 	return xfer;
    499  1.24     pooka }
    500  1.24     pooka 
    501  1.24     pooka /*ARGSUSED*/
    502  1.24     pooka int
    503  1.24     pooka ukfs_close(struct ukfs *ukfs, int fd)
    504  1.24     pooka {
    505  1.24     pooka 
    506  1.24     pooka 	rump_sys_close(fd);
    507  1.24     pooka 	return 0;
    508  1.24     pooka }
    509  1.24     pooka 
    510   1.1     pooka int
    511   1.1     pooka ukfs_create(struct ukfs *ukfs, const char *filename, mode_t mode)
    512   1.1     pooka {
    513  1.21     pooka 	int fd;
    514   1.1     pooka 
    515   1.1     pooka 	precall(ukfs);
    516  1.21     pooka 	fd = rump_sys_open(filename, RUMP_O_WRONLY | RUMP_O_CREAT, mode);
    517  1.21     pooka 	if (fd == -1)
    518  1.21     pooka 		return -1;
    519  1.21     pooka 	rump_sys_close(fd);
    520   1.1     pooka 
    521   1.1     pooka 	postcall(ukfs);
    522   1.1     pooka 	return 0;
    523   1.1     pooka }
    524   1.1     pooka 
    525   1.1     pooka int
    526   1.1     pooka ukfs_mknod(struct ukfs *ukfs, const char *path, mode_t mode, dev_t dev)
    527   1.1     pooka {
    528   1.1     pooka 
    529  1.21     pooka 	STDCALL(ukfs, rump_sys_mknod(path, mode, dev));
    530   1.1     pooka }
    531   1.1     pooka 
    532   1.1     pooka int
    533   1.1     pooka ukfs_mkfifo(struct ukfs *ukfs, const char *path, mode_t mode)
    534   1.1     pooka {
    535   1.1     pooka 
    536  1.21     pooka 	STDCALL(ukfs, rump_sys_mkfifo(path, mode));
    537   1.1     pooka }
    538   1.1     pooka 
    539   1.1     pooka int
    540   1.1     pooka ukfs_mkdir(struct ukfs *ukfs, const char *filename, mode_t mode)
    541   1.1     pooka {
    542   1.1     pooka 
    543  1.21     pooka 	STDCALL(ukfs, rump_sys_mkdir(filename, mode));
    544   1.1     pooka }
    545   1.1     pooka 
    546   1.1     pooka int
    547   1.1     pooka ukfs_remove(struct ukfs *ukfs, const char *filename)
    548   1.1     pooka {
    549   1.1     pooka 
    550  1.21     pooka 	STDCALL(ukfs, rump_sys_unlink(filename));
    551   1.1     pooka }
    552   1.1     pooka 
    553   1.1     pooka int
    554   1.1     pooka ukfs_rmdir(struct ukfs *ukfs, const char *filename)
    555   1.1     pooka {
    556   1.1     pooka 
    557  1.21     pooka 	STDCALL(ukfs, rump_sys_rmdir(filename));
    558   1.1     pooka }
    559   1.1     pooka 
    560   1.1     pooka int
    561   1.1     pooka ukfs_link(struct ukfs *ukfs, const char *filename, const char *f_create)
    562   1.1     pooka {
    563   1.1     pooka 
    564  1.21     pooka 	STDCALL(ukfs, rump_sys_link(filename, f_create));
    565   1.1     pooka }
    566   1.1     pooka 
    567   1.1     pooka int
    568   1.1     pooka ukfs_symlink(struct ukfs *ukfs, const char *filename, const char *linkname)
    569   1.1     pooka {
    570   1.1     pooka 
    571  1.21     pooka 	STDCALL(ukfs, rump_sys_symlink(filename, linkname));
    572   1.1     pooka }
    573   1.1     pooka 
    574   1.1     pooka ssize_t
    575   1.1     pooka ukfs_readlink(struct ukfs *ukfs, const char *filename,
    576   1.1     pooka 	char *linkbuf, size_t buflen)
    577   1.1     pooka {
    578   1.1     pooka 	ssize_t rv;
    579   1.1     pooka 
    580   1.1     pooka 	precall(ukfs);
    581  1.21     pooka 	rv = rump_sys_readlink(filename, linkbuf, buflen);
    582   1.1     pooka 	postcall(ukfs);
    583   1.1     pooka 	return rv;
    584   1.1     pooka }
    585   1.1     pooka 
    586   1.1     pooka int
    587   1.1     pooka ukfs_rename(struct ukfs *ukfs, const char *from, const char *to)
    588   1.1     pooka {
    589   1.1     pooka 
    590  1.21     pooka 	STDCALL(ukfs, rump_sys_rename(from, to));
    591   1.1     pooka }
    592   1.1     pooka 
    593   1.1     pooka int
    594   1.1     pooka ukfs_chdir(struct ukfs *ukfs, const char *path)
    595   1.1     pooka {
    596   1.1     pooka 	struct vnode *newvp, *oldvp;
    597   1.1     pooka 	int rv;
    598   1.1     pooka 
    599   1.1     pooka 	precall(ukfs);
    600  1.21     pooka 	rv = rump_sys_chdir(path);
    601  1.21     pooka 	if (rv == -1)
    602   1.1     pooka 		goto out;
    603   1.1     pooka 
    604   1.1     pooka 	newvp = rump_cdir_get();
    605   1.1     pooka 	pthread_spin_lock(&ukfs->ukfs_spin);
    606   1.1     pooka 	oldvp = ukfs->ukfs_cdir;
    607   1.1     pooka 	ukfs->ukfs_cdir = newvp;
    608   1.1     pooka 	pthread_spin_unlock(&ukfs->ukfs_spin);
    609   1.1     pooka 	if (oldvp)
    610   1.1     pooka 		rump_vp_rele(oldvp);
    611   1.1     pooka 
    612   1.1     pooka  out:
    613   1.1     pooka 	postcall(ukfs);
    614  1.21     pooka 	return rv;
    615   1.1     pooka }
    616   1.1     pooka 
    617  1.28     pooka /*
    618  1.28     pooka  * If we want to use post-time_t file systems on pre-time_t hosts,
    619  1.28     pooka  * we must translate the stat structure.  Since we don't currently
    620  1.28     pooka  * have a general method for making compat calls in rump, special-case
    621  1.28     pooka  * this one.
    622  1.28     pooka  *
    623  1.28     pooka  * Note that this does not allow making system calls to older rump
    624  1.28     pooka  * kernels from newer hosts.
    625  1.28     pooka  */
    626  1.28     pooka #define VERS_TIMECHANGE 599000700
    627  1.28     pooka 
    628  1.28     pooka static int
    629  1.28     pooka needcompat(void)
    630  1.28     pooka {
    631  1.28     pooka 
    632  1.28     pooka #ifdef __NetBSD__
    633  1.30     pooka 	/*LINTED*/
    634  1.28     pooka 	return __NetBSD_Version__ < VERS_TIMECHANGE
    635  1.28     pooka 	    && rump_getversion() >= VERS_TIMECHANGE;
    636  1.28     pooka #else
    637  1.28     pooka 	return 0;
    638  1.28     pooka #endif
    639  1.28     pooka }
    640  1.28     pooka 
    641   1.1     pooka int
    642   1.1     pooka ukfs_stat(struct ukfs *ukfs, const char *filename, struct stat *file_stat)
    643   1.1     pooka {
    644  1.28     pooka 	int rv;
    645   1.1     pooka 
    646  1.28     pooka 	precall(ukfs);
    647  1.28     pooka 	if (needcompat())
    648  1.28     pooka 		rv = rump_sys___stat30(filename, file_stat);
    649  1.28     pooka 	else
    650  1.28     pooka 		rv = rump_sys_stat(filename, file_stat);
    651  1.28     pooka 	postcall(ukfs);
    652  1.28     pooka 
    653  1.28     pooka 	return rv;
    654   1.1     pooka }
    655   1.1     pooka 
    656   1.1     pooka int
    657   1.1     pooka ukfs_lstat(struct ukfs *ukfs, const char *filename, struct stat *file_stat)
    658   1.1     pooka {
    659  1.28     pooka 	int rv;
    660   1.1     pooka 
    661  1.28     pooka 	precall(ukfs);
    662  1.28     pooka 	if (needcompat())
    663  1.28     pooka 		rv = rump_sys___lstat30(filename, file_stat);
    664  1.28     pooka 	else
    665  1.28     pooka 		rv = rump_sys_lstat(filename, file_stat);
    666  1.28     pooka 	postcall(ukfs);
    667  1.28     pooka 
    668  1.28     pooka 	return rv;
    669   1.1     pooka }
    670   1.1     pooka 
    671   1.1     pooka int
    672   1.1     pooka ukfs_chmod(struct ukfs *ukfs, const char *filename, mode_t mode)
    673   1.1     pooka {
    674   1.1     pooka 
    675  1.21     pooka 	STDCALL(ukfs, rump_sys_chmod(filename, mode));
    676   1.1     pooka }
    677   1.1     pooka 
    678   1.1     pooka int
    679   1.1     pooka ukfs_lchmod(struct ukfs *ukfs, const char *filename, mode_t mode)
    680   1.1     pooka {
    681   1.1     pooka 
    682  1.21     pooka 	STDCALL(ukfs, rump_sys_lchmod(filename, mode));
    683   1.1     pooka }
    684   1.1     pooka 
    685   1.1     pooka int
    686   1.1     pooka ukfs_chown(struct ukfs *ukfs, const char *filename, uid_t uid, gid_t gid)
    687   1.1     pooka {
    688   1.1     pooka 
    689  1.21     pooka 	STDCALL(ukfs, rump_sys_chown(filename, uid, gid));
    690   1.1     pooka }
    691   1.1     pooka 
    692   1.1     pooka int
    693   1.1     pooka ukfs_lchown(struct ukfs *ukfs, const char *filename, uid_t uid, gid_t gid)
    694   1.1     pooka {
    695   1.1     pooka 
    696  1.21     pooka 	STDCALL(ukfs, rump_sys_lchown(filename, uid, gid));
    697   1.1     pooka }
    698   1.1     pooka 
    699   1.1     pooka int
    700   1.1     pooka ukfs_chflags(struct ukfs *ukfs, const char *filename, u_long flags)
    701   1.1     pooka {
    702   1.1     pooka 
    703  1.21     pooka 	STDCALL(ukfs, rump_sys_chflags(filename, flags));
    704   1.1     pooka }
    705   1.1     pooka 
    706   1.1     pooka int
    707   1.1     pooka ukfs_lchflags(struct ukfs *ukfs, const char *filename, u_long flags)
    708   1.1     pooka {
    709   1.1     pooka 
    710  1.21     pooka 	STDCALL(ukfs, rump_sys_lchflags(filename, flags));
    711   1.1     pooka }
    712   1.1     pooka 
    713   1.1     pooka int
    714   1.1     pooka ukfs_utimes(struct ukfs *ukfs, const char *filename, const struct timeval *tptr)
    715   1.1     pooka {
    716   1.1     pooka 
    717  1.21     pooka 	STDCALL(ukfs, rump_sys_utimes(filename, tptr));
    718   1.1     pooka }
    719   1.1     pooka 
    720   1.1     pooka int
    721   1.1     pooka ukfs_lutimes(struct ukfs *ukfs, const char *filename,
    722   1.1     pooka 	      const struct timeval *tptr)
    723   1.1     pooka {
    724   1.1     pooka 
    725  1.21     pooka 	STDCALL(ukfs, rump_sys_lutimes(filename, tptr));
    726   1.1     pooka }
    727   1.1     pooka 
    728   1.3     pooka /*
    729   1.3     pooka  * Dynamic module support
    730   1.3     pooka  */
    731   1.3     pooka 
    732   1.3     pooka /* load one library */
    733   1.3     pooka 
    734   1.3     pooka /*
    735   1.3     pooka  * XXX: the dlerror stuff isn't really threadsafe, but then again I
    736   1.3     pooka  * can't protect against other threads calling dl*() outside of ukfs,
    737   1.3     pooka  * so just live with it being flimsy
    738   1.3     pooka  */
    739   1.3     pooka int
    740   1.3     pooka ukfs_modload(const char *fname)
    741   1.3     pooka {
    742  1.26     pooka 	void *handle;
    743  1.26     pooka 	struct modinfo **mi;
    744   1.3     pooka 	int error;
    745   1.3     pooka 
    746   1.3     pooka 	handle = dlopen(fname, RTLD_GLOBAL);
    747   1.3     pooka 	if (handle == NULL) {
    748  1.13     pooka 		const char *dlmsg = dlerror();
    749  1.13     pooka 		if (strstr(dlmsg, "Undefined symbol"))
    750   1.3     pooka 			return 0;
    751  1.13     pooka 		warnx("dlopen %s failed: %s\n", fname, dlmsg);
    752   1.3     pooka 		/* XXXerrno */
    753   1.3     pooka 		return -1;
    754   1.3     pooka 	}
    755   1.3     pooka 
    756  1.26     pooka 	mi = dlsym(handle, "__start_link_set_modules");
    757  1.26     pooka 	if (mi) {
    758  1.26     pooka 		error = rump_module_init(*mi, NULL);
    759   1.3     pooka 		if (error)
    760   1.3     pooka 			goto errclose;
    761   1.3     pooka 		return 1;
    762   1.3     pooka 	}
    763   1.3     pooka 	error = EINVAL;
    764   1.3     pooka 
    765   1.3     pooka  errclose:
    766   1.3     pooka 	dlclose(handle);
    767   1.3     pooka 	errno = error;
    768   1.3     pooka 	return -1;
    769   1.3     pooka }
    770   1.3     pooka 
    771   1.3     pooka struct loadfail {
    772   1.3     pooka 	char *pname;
    773   1.3     pooka 
    774   1.3     pooka 	LIST_ENTRY(loadfail) entries;
    775   1.3     pooka };
    776   1.3     pooka 
    777   1.3     pooka #define RUMPFSMOD_PREFIX "librumpfs_"
    778   1.3     pooka #define RUMPFSMOD_SUFFIX ".so"
    779   1.3     pooka 
    780   1.3     pooka int
    781   1.3     pooka ukfs_modload_dir(const char *dir)
    782   1.3     pooka {
    783   1.3     pooka 	char nbuf[MAXPATHLEN+1], *p;
    784   1.3     pooka 	struct dirent entry, *result;
    785   1.3     pooka 	DIR *libdir;
    786   1.3     pooka 	struct loadfail *lf, *nlf;
    787   1.3     pooka 	int error, nloaded = 0, redo;
    788   1.3     pooka 	LIST_HEAD(, loadfail) lfs;
    789   1.3     pooka 
    790   1.3     pooka 	libdir = opendir(dir);
    791   1.3     pooka 	if (libdir == NULL)
    792   1.3     pooka 		return -1;
    793   1.3     pooka 
    794   1.3     pooka 	LIST_INIT(&lfs);
    795   1.3     pooka 	for (;;) {
    796   1.3     pooka 		if ((error = readdir_r(libdir, &entry, &result)) != 0)
    797   1.3     pooka 			break;
    798   1.3     pooka 		if (!result)
    799   1.3     pooka 			break;
    800   1.3     pooka 		if (strncmp(result->d_name, RUMPFSMOD_PREFIX,
    801   1.3     pooka 		    strlen(RUMPFSMOD_PREFIX)) != 0)
    802   1.3     pooka 			continue;
    803   1.3     pooka 		if (((p = strstr(result->d_name, RUMPFSMOD_SUFFIX)) == NULL)
    804   1.3     pooka 		    || strlen(p) != strlen(RUMPFSMOD_SUFFIX))
    805   1.3     pooka 			continue;
    806   1.3     pooka 		strlcpy(nbuf, dir, sizeof(nbuf));
    807   1.3     pooka 		strlcat(nbuf, "/", sizeof(nbuf));
    808   1.3     pooka 		strlcat(nbuf, result->d_name, sizeof(nbuf));
    809   1.3     pooka 		switch (ukfs_modload(nbuf)) {
    810   1.3     pooka 		case 0:
    811   1.3     pooka 			lf = malloc(sizeof(*lf));
    812   1.3     pooka 			if (lf == NULL) {
    813   1.3     pooka 				error = ENOMEM;
    814   1.3     pooka 				break;
    815   1.3     pooka 			}
    816   1.3     pooka 			lf->pname = strdup(nbuf);
    817   1.3     pooka 			if (lf->pname == NULL) {
    818   1.3     pooka 				free(lf);
    819   1.3     pooka 				error = ENOMEM;
    820   1.3     pooka 				break;
    821   1.3     pooka 			}
    822   1.3     pooka 			LIST_INSERT_HEAD(&lfs, lf, entries);
    823   1.3     pooka 			break;
    824   1.3     pooka 		case 1:
    825   1.3     pooka 			nloaded++;
    826   1.3     pooka 			break;
    827   1.3     pooka 		default:
    828   1.3     pooka 			/* ignore errors */
    829   1.3     pooka 			break;
    830   1.3     pooka 		}
    831   1.3     pooka 	}
    832   1.3     pooka 	closedir(libdir);
    833   1.3     pooka 	if (error && nloaded != 0)
    834   1.3     pooka 		error = 0;
    835   1.3     pooka 
    836   1.3     pooka 	/*
    837   1.3     pooka 	 * El-cheapo dependency calculator.  Just try to load the
    838   1.3     pooka 	 * modules n times in a loop
    839   1.3     pooka 	 */
    840   1.3     pooka 	for (redo = 1; redo;) {
    841   1.3     pooka 		redo = 0;
    842   1.3     pooka 		nlf = LIST_FIRST(&lfs);
    843   1.3     pooka 		while ((lf = nlf) != NULL) {
    844   1.3     pooka 			nlf = LIST_NEXT(lf, entries);
    845   1.3     pooka 			if (ukfs_modload(lf->pname) == 1) {
    846   1.3     pooka 				nloaded++;
    847   1.3     pooka 				redo = 1;
    848   1.3     pooka 				LIST_REMOVE(lf, entries);
    849   1.3     pooka 				free(lf->pname);
    850   1.3     pooka 				free(lf);
    851   1.3     pooka 			}
    852   1.3     pooka 		}
    853   1.3     pooka 	}
    854   1.3     pooka 
    855   1.3     pooka 	while ((lf = LIST_FIRST(&lfs)) != NULL) {
    856   1.3     pooka 		LIST_REMOVE(lf, entries);
    857   1.3     pooka 		free(lf->pname);
    858   1.3     pooka 		free(lf);
    859   1.3     pooka 	}
    860   1.3     pooka 
    861   1.3     pooka 	if (error && nloaded == 0) {
    862   1.3     pooka 		errno = error;
    863   1.3     pooka 		return -1;
    864   1.3     pooka 	}
    865   1.3     pooka 
    866   1.3     pooka 	return nloaded;
    867   1.3     pooka }
    868   1.3     pooka 
    869   1.4     pooka /* XXX: this code uses definitions from NetBSD, needs rumpdefs */
    870   1.4     pooka ssize_t
    871   1.4     pooka ukfs_vfstypes(char *buf, size_t buflen)
    872   1.4     pooka {
    873   1.4     pooka 	int mib[3];
    874   1.4     pooka 	struct sysctlnode q, ans[128];
    875   1.4     pooka 	size_t alen;
    876  1.21     pooka 	int i;
    877   1.4     pooka 
    878   1.4     pooka 	mib[0] = CTL_VFS;
    879   1.4     pooka 	mib[1] = VFS_GENERIC;
    880   1.4     pooka 	mib[2] = CTL_QUERY;
    881   1.4     pooka 	alen = sizeof(ans);
    882   1.4     pooka 
    883   1.4     pooka 	memset(&q, 0, sizeof(q));
    884   1.4     pooka 	q.sysctl_flags = SYSCTL_VERSION;
    885   1.4     pooka 
    886  1.21     pooka 	if (rump_sys___sysctl(mib, 3, ans, &alen, &q, sizeof(q)) == -1) {
    887   1.4     pooka 		return -1;
    888   1.4     pooka 	}
    889   1.4     pooka 
    890   1.4     pooka 	for (i = 0; i < alen/sizeof(ans[0]); i++)
    891   1.4     pooka 		if (strcmp("fstypes", ans[i].sysctl_name) == 0)
    892   1.4     pooka 			break;
    893   1.4     pooka 	if (i == alen/sizeof(ans[0])) {
    894   1.4     pooka 		errno = ENXIO;
    895   1.4     pooka 		return -1;
    896   1.4     pooka 	}
    897   1.4     pooka 
    898   1.4     pooka 	mib[0] = CTL_VFS;
    899   1.4     pooka 	mib[1] = VFS_GENERIC;
    900   1.4     pooka 	mib[2] = ans[i].sysctl_num;
    901   1.4     pooka 
    902  1.21     pooka 	if (rump_sys___sysctl(mib, 3, buf, &buflen, NULL, 0) == -1) {
    903   1.4     pooka 		return -1;
    904   1.4     pooka 	}
    905   1.4     pooka 
    906   1.4     pooka 	return buflen;
    907   1.4     pooka }
    908   1.3     pooka 
    909   1.3     pooka /*
    910   1.3     pooka  * Utilities
    911   1.3     pooka  */
    912  1.30     pooka static int
    913  1.30     pooka builddirs(const char *pathname, mode_t mode,
    914  1.30     pooka 	int (*mkdirfn)(struct ukfs *, const char *, mode_t), struct ukfs *fs)
    915   1.1     pooka {
    916   1.1     pooka 	char *f1, *f2;
    917   1.1     pooka 	int rv;
    918   1.1     pooka 	mode_t mask;
    919   1.1     pooka 	bool end;
    920   1.1     pooka 
    921   1.1     pooka 	/*ukfs_umask((mask = ukfs_umask(0)));*/
    922   1.1     pooka 	umask((mask = umask(0)));
    923   1.1     pooka 
    924   1.1     pooka 	f1 = f2 = strdup(pathname);
    925   1.1     pooka 	if (f1 == NULL) {
    926   1.1     pooka 		errno = ENOMEM;
    927   1.1     pooka 		return -1;
    928   1.1     pooka 	}
    929   1.1     pooka 
    930   1.1     pooka 	end = false;
    931   1.1     pooka 	for (;;) {
    932   1.1     pooka 		/* find next component */
    933   1.1     pooka 		f2 += strspn(f2, "/");
    934   1.1     pooka 		f2 += strcspn(f2, "/");
    935   1.1     pooka 		if (*f2 == '\0')
    936   1.1     pooka 			end = true;
    937   1.1     pooka 		else
    938   1.1     pooka 			*f2 = '\0';
    939   1.1     pooka 
    940  1.30     pooka 		rv = mkdirfn(fs, f1, mode & ~mask);
    941   1.1     pooka 		if (errno == EEXIST)
    942   1.1     pooka 			rv = 0;
    943   1.1     pooka 
    944   1.1     pooka 		if (rv == -1 || *f2 != '\0' || end)
    945   1.1     pooka 			break;
    946   1.1     pooka 
    947   1.1     pooka 		*f2 = '/';
    948   1.1     pooka 	}
    949   1.1     pooka 
    950   1.1     pooka 	free(f1);
    951   1.1     pooka 
    952   1.1     pooka 	return rv;
    953   1.1     pooka }
    954  1.30     pooka 
    955  1.30     pooka int
    956  1.30     pooka ukfs_util_builddirs(struct ukfs *ukfs, const char *pathname, mode_t mode)
    957  1.30     pooka {
    958  1.30     pooka 
    959  1.30     pooka 	return builddirs(pathname, mode, ukfs_mkdir, ukfs);
    960  1.30     pooka }
    961