2018-06-30 19:05:44 +03:00
|
|
|
/* $NetBSD: puffs.c,v 1.124 2018/06/30 16:05:44 christos Exp $ */
|
2006-10-23 02:52:21 +04:00
|
|
|
|
|
|
|
/*
|
2007-05-12 01:27:13 +04:00
|
|
|
* Copyright (c) 2005, 2006, 2007 Antti Kantee. All Rights Reserved.
|
2006-10-23 02:52:21 +04:00
|
|
|
*
|
|
|
|
* Development of this software was supported by the
|
|
|
|
* Google Summer of Code program and the Ulla Tuominen Foundation.
|
|
|
|
* The Google SoC project was mentored by Bill Studenmund.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#if !defined(lint)
|
2018-06-30 19:05:44 +03:00
|
|
|
__RCSID("$NetBSD: puffs.c,v 1.124 2018/06/30 16:05:44 christos Exp $");
|
2006-10-23 02:52:21 +04:00
|
|
|
#endif /* !lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
|
2006-11-30 08:37:48 +03:00
|
|
|
#include <assert.h>
|
2006-12-06 02:04:21 +03:00
|
|
|
#include <err.h>
|
2006-10-23 02:52:21 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2007-01-06 21:22:09 +03:00
|
|
|
#include <mntopts.h>
|
2007-07-17 15:34:51 +04:00
|
|
|
#include <paths.h>
|
2010-12-06 17:50:34 +03:00
|
|
|
#include <pthread.h>
|
2006-10-23 02:52:21 +04:00
|
|
|
#include <puffs.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2006-12-29 18:28:11 +03:00
|
|
|
#include "puffs_priv.h"
|
|
|
|
|
2007-01-06 21:22:09 +03:00
|
|
|
/* Most file systems want this for opts, so just give it to them */
|
|
|
|
const struct mntopt puffsmopts[] = {
|
|
|
|
MOPT_STDOPTS,
|
|
|
|
PUFFSMOPT_STD,
|
|
|
|
MOPT_NULL,
|
|
|
|
};
|
|
|
|
|
2007-10-29 18:52:44 +03:00
|
|
|
pthread_mutex_t pu_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
2006-12-01 15:38:11 +03:00
|
|
|
#define FILLOP(lower, upper) \
|
|
|
|
do { \
|
2006-12-07 13:53:21 +03:00
|
|
|
if (pops->puffs_node_##lower) \
|
2006-12-01 15:38:11 +03:00
|
|
|
opmask[PUFFS_VN_##upper] = 1; \
|
|
|
|
} while (/*CONSTCOND*/0)
|
|
|
|
static void
|
2010-05-21 14:50:52 +04:00
|
|
|
fillvnopmask(struct puffs_ops *pops, struct puffs_kargs *pa)
|
2006-12-01 15:38:11 +03:00
|
|
|
{
|
2010-05-21 14:50:52 +04:00
|
|
|
uint8_t *opmask = pa->pa_vnopmask;
|
2006-12-01 15:38:11 +03:00
|
|
|
|
2010-05-21 14:50:52 +04:00
|
|
|
memset(opmask, 0, sizeof(pa->pa_vnopmask));
|
2006-12-01 15:38:11 +03:00
|
|
|
|
|
|
|
FILLOP(create, CREATE);
|
|
|
|
FILLOP(mknod, MKNOD);
|
|
|
|
FILLOP(open, OPEN);
|
|
|
|
FILLOP(close, CLOSE);
|
|
|
|
FILLOP(access, ACCESS);
|
|
|
|
FILLOP(getattr, GETATTR);
|
|
|
|
FILLOP(setattr, SETATTR);
|
2010-05-19 16:16:45 +04:00
|
|
|
FILLOP(poll, POLL);
|
2006-12-07 20:39:54 +03:00
|
|
|
FILLOP(mmap, MMAP);
|
2006-12-01 15:38:11 +03:00
|
|
|
FILLOP(fsync, FSYNC);
|
|
|
|
FILLOP(seek, SEEK);
|
|
|
|
FILLOP(remove, REMOVE);
|
|
|
|
FILLOP(link, LINK);
|
|
|
|
FILLOP(rename, RENAME);
|
|
|
|
FILLOP(mkdir, MKDIR);
|
|
|
|
FILLOP(rmdir, RMDIR);
|
|
|
|
FILLOP(symlink, SYMLINK);
|
|
|
|
FILLOP(readdir, READDIR);
|
|
|
|
FILLOP(readlink, READLINK);
|
|
|
|
FILLOP(reclaim, RECLAIM);
|
|
|
|
FILLOP(inactive, INACTIVE);
|
|
|
|
FILLOP(print, PRINT);
|
|
|
|
FILLOP(read, READ);
|
|
|
|
FILLOP(write, WRITE);
|
2011-05-03 17:16:47 +04:00
|
|
|
FILLOP(advlock, ADVLOCK);
|
2009-10-18 03:19:52 +04:00
|
|
|
FILLOP(abortop, ABORTOP);
|
2010-06-07 02:44:54 +04:00
|
|
|
FILLOP(pathconf, PATHCONF);
|
2010-05-21 14:50:52 +04:00
|
|
|
|
|
|
|
FILLOP(getextattr, GETEXTATTR);
|
|
|
|
FILLOP(setextattr, SETEXTATTR);
|
|
|
|
FILLOP(listextattr, LISTEXTATTR);
|
|
|
|
FILLOP(deleteextattr, DELETEEXTATTR);
|
2014-10-31 16:56:04 +03:00
|
|
|
FILLOP(fallocate, FALLOCATE);
|
|
|
|
FILLOP(fdiscard, FDISCARD);
|
2006-12-01 15:38:11 +03:00
|
|
|
}
|
|
|
|
#undef FILLOP
|
|
|
|
|
2007-12-05 00:24:10 +03:00
|
|
|
/*
|
|
|
|
* Go over all framev entries and write everything we can. This is
|
|
|
|
* mostly for the benefit of delivering "unmount" to the kernel.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
finalpush(struct puffs_usermount *pu)
|
|
|
|
{
|
|
|
|
struct puffs_fctrl_io *fio;
|
|
|
|
|
|
|
|
LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
|
|
|
|
if (fio->stat & FIO_WRGONE)
|
|
|
|
continue;
|
|
|
|
|
2008-01-28 21:35:49 +03:00
|
|
|
puffs__framev_output(pu, fio->fctrl, fio);
|
2007-12-05 00:24:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-28 03:11:41 +04:00
|
|
|
/*ARGSUSED*/
|
2009-12-05 23:54:10 +03:00
|
|
|
void
|
|
|
|
puffs_kernerr_abort(struct puffs_usermount *pu, uint8_t type,
|
2008-08-12 23:44:39 +04:00
|
|
|
int error, const char *str, puffs_cookie_t cookie)
|
2007-09-28 01:14:49 +04:00
|
|
|
{
|
|
|
|
|
2017-11-05 18:33:15 +03:00
|
|
|
warnx("%s: type %d, error %d, cookie %p (%s)", __func__,
|
2007-11-26 15:20:21 +03:00
|
|
|
type, error, cookie, str);
|
2007-09-28 01:14:49 +04:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2009-12-05 23:54:10 +03:00
|
|
|
/*ARGSUSED*/
|
|
|
|
void
|
|
|
|
puffs_kernerr_log(struct puffs_usermount *pu, uint8_t type,
|
|
|
|
int error, const char *str, puffs_cookie_t cookie)
|
|
|
|
{
|
|
|
|
|
2017-11-05 18:33:15 +03:00
|
|
|
syslog(LOG_WARNING, "%s: type %d, error %d, cookie %p (%s)", __func__,
|
2009-12-05 23:54:10 +03:00
|
|
|
type, error, cookie, str);
|
|
|
|
}
|
|
|
|
|
2006-12-29 18:28:11 +03:00
|
|
|
int
|
|
|
|
puffs_getselectable(struct puffs_usermount *pu)
|
|
|
|
{
|
|
|
|
|
2007-07-19 16:52:28 +04:00
|
|
|
return pu->pu_fd;
|
2006-12-29 18:28:11 +03:00
|
|
|
}
|
|
|
|
|
2007-12-05 15:11:56 +03:00
|
|
|
uint64_t
|
|
|
|
puffs__nextreq(struct puffs_usermount *pu)
|
|
|
|
{
|
|
|
|
uint64_t rv;
|
|
|
|
|
|
|
|
PU_LOCK();
|
2010-01-08 02:03:26 +03:00
|
|
|
rv = pu->pu_nextreq++ | (uint64_t)1<<63;
|
2007-12-05 15:11:56 +03:00
|
|
|
PU_UNLOCK();
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2006-12-29 18:28:11 +03:00
|
|
|
int
|
|
|
|
puffs_setblockingmode(struct puffs_usermount *pu, int mode)
|
|
|
|
{
|
2007-05-15 17:44:46 +04:00
|
|
|
int rv, x;
|
|
|
|
|
2007-11-06 18:09:07 +03:00
|
|
|
assert(puffs_getstate(pu) == PUFFS_STATE_RUNNING);
|
|
|
|
|
2007-05-15 17:44:46 +04:00
|
|
|
if (mode != PUFFSDEV_BLOCK && mode != PUFFSDEV_NONBLOCK) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2006-12-29 18:28:11 +03:00
|
|
|
|
|
|
|
x = mode;
|
2007-07-19 16:52:28 +04:00
|
|
|
rv = ioctl(pu->pu_fd, FIONBIO, &x);
|
2007-05-15 17:44:46 +04:00
|
|
|
|
|
|
|
if (rv == 0) {
|
|
|
|
if (mode == PUFFSDEV_BLOCK)
|
|
|
|
pu->pu_state &= ~PU_ASYNCFD;
|
|
|
|
else
|
|
|
|
pu->pu_state |= PU_ASYNCFD;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2006-12-29 18:28:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
puffs_getstate(struct puffs_usermount *pu)
|
|
|
|
{
|
|
|
|
|
2007-05-12 01:27:13 +04:00
|
|
|
return pu->pu_state & PU_STATEMASK;
|
2006-12-29 18:28:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
puffs_setstacksize(struct puffs_usermount *pu, size_t ss)
|
|
|
|
{
|
2008-01-14 16:57:26 +03:00
|
|
|
long psize, minsize;
|
2007-10-26 17:51:14 +04:00
|
|
|
int stackshift;
|
2008-01-16 03:29:42 +03:00
|
|
|
int bonus;
|
2007-11-29 20:47:54 +03:00
|
|
|
|
2008-01-14 16:57:26 +03:00
|
|
|
assert(puffs_getstate(pu) == PUFFS_STATE_BEFOREMOUNT);
|
|
|
|
|
2007-11-29 20:47:54 +03:00
|
|
|
psize = sysconf(_SC_PAGESIZE);
|
2008-01-16 03:29:42 +03:00
|
|
|
minsize = 4*psize;
|
2009-01-08 05:28:08 +03:00
|
|
|
if (ss < (size_t)minsize || ss == PUFFS_STACKSIZE_MIN) {
|
2008-01-14 16:57:26 +03:00
|
|
|
if (ss != PUFFS_STACKSIZE_MIN)
|
2015-06-17 03:15:26 +03:00
|
|
|
warnx("%s: adjusting " "stacksize to minimum %ld",
|
|
|
|
__func__, minsize);
|
2008-01-16 03:29:42 +03:00
|
|
|
ss = 4*psize;
|
2007-11-29 20:47:54 +03:00
|
|
|
}
|
2007-10-26 17:51:14 +04:00
|
|
|
|
|
|
|
stackshift = -1;
|
2008-01-16 03:29:42 +03:00
|
|
|
bonus = 0;
|
2007-10-26 17:51:14 +04:00
|
|
|
while (ss) {
|
2008-01-16 03:29:42 +03:00
|
|
|
if (ss & 0x1)
|
|
|
|
bonus++;
|
2007-10-26 17:51:14 +04:00
|
|
|
ss >>= 1;
|
|
|
|
stackshift++;
|
|
|
|
}
|
2008-01-16 03:29:42 +03:00
|
|
|
if (bonus > 1) {
|
|
|
|
stackshift++;
|
2015-06-17 03:15:26 +03:00
|
|
|
warnx("%s: using next power of two: %d", __func__,
|
|
|
|
1 << stackshift);
|
2008-01-16 03:29:42 +03:00
|
|
|
}
|
|
|
|
|
2007-10-26 17:51:14 +04:00
|
|
|
pu->pu_cc_stackshift = stackshift;
|
2006-12-29 18:28:11 +03:00
|
|
|
}
|
|
|
|
|
2007-01-15 03:39:02 +03:00
|
|
|
struct puffs_pathobj *
|
|
|
|
puffs_getrootpathobj(struct puffs_usermount *pu)
|
2006-12-29 18:28:11 +03:00
|
|
|
{
|
|
|
|
struct puffs_node *pnr;
|
|
|
|
|
|
|
|
pnr = pu->pu_pn_root;
|
|
|
|
if (pnr == NULL) {
|
|
|
|
errno = ENOENT;
|
2007-01-15 03:39:02 +03:00
|
|
|
return NULL;
|
2006-12-29 18:28:11 +03:00
|
|
|
}
|
|
|
|
|
2007-01-15 03:39:02 +03:00
|
|
|
return &pnr->pn_po;
|
|
|
|
}
|
2006-12-29 18:28:11 +03:00
|
|
|
|
2007-04-12 19:09:00 +04:00
|
|
|
void
|
|
|
|
puffs_setroot(struct puffs_usermount *pu, struct puffs_node *pn)
|
|
|
|
{
|
|
|
|
|
|
|
|
pu->pu_pn_root = pn;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct puffs_node *
|
|
|
|
puffs_getroot(struct puffs_usermount *pu)
|
|
|
|
{
|
|
|
|
|
|
|
|
return pu->pu_pn_root;
|
|
|
|
}
|
|
|
|
|
2007-05-17 18:03:13 +04:00
|
|
|
void
|
|
|
|
puffs_setrootinfo(struct puffs_usermount *pu, enum vtype vt,
|
|
|
|
vsize_t vsize, dev_t rdev)
|
|
|
|
{
|
2007-07-19 16:52:28 +04:00
|
|
|
struct puffs_kargs *pargs = pu->pu_kargp;
|
2007-05-17 18:03:13 +04:00
|
|
|
|
2007-07-19 16:52:28 +04:00
|
|
|
if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT) {
|
2015-06-17 03:15:26 +03:00
|
|
|
warnx("%s: call has effect only before mount", __func__);
|
2007-07-19 16:52:28 +04:00
|
|
|
return;
|
|
|
|
}
|
2007-05-17 18:03:13 +04:00
|
|
|
|
|
|
|
pargs->pa_root_vtype = vt;
|
|
|
|
pargs->pa_root_vsize = vsize;
|
|
|
|
pargs->pa_root_rdev = rdev;
|
|
|
|
}
|
|
|
|
|
2007-04-12 19:09:00 +04:00
|
|
|
void *
|
|
|
|
puffs_getspecific(struct puffs_usermount *pu)
|
|
|
|
{
|
|
|
|
|
|
|
|
return pu->pu_privdata;
|
2008-12-12 21:59:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
puffs_setspecific(struct puffs_usermount *pu, void *privdata)
|
|
|
|
{
|
|
|
|
|
|
|
|
pu->pu_privdata = privdata;
|
2007-04-12 19:09:00 +04:00
|
|
|
}
|
|
|
|
|
2009-10-18 23:09:20 +04:00
|
|
|
void
|
|
|
|
puffs_setmntinfo(struct puffs_usermount *pu,
|
|
|
|
const char *mntfromname, const char *puffsname)
|
|
|
|
{
|
|
|
|
struct puffs_kargs *pargs = pu->pu_kargp;
|
|
|
|
|
|
|
|
(void)strlcpy(pargs->pa_mntfromname, mntfromname,
|
|
|
|
sizeof(pargs->pa_mntfromname));
|
|
|
|
(void)strlcpy(pargs->pa_typename, puffsname,
|
|
|
|
sizeof(pargs->pa_typename));
|
|
|
|
}
|
|
|
|
|
2007-04-12 19:09:00 +04:00
|
|
|
size_t
|
|
|
|
puffs_getmaxreqlen(struct puffs_usermount *pu)
|
|
|
|
{
|
|
|
|
|
2007-07-19 16:52:28 +04:00
|
|
|
return pu->pu_maxreqlen;
|
2007-04-12 19:09:00 +04:00
|
|
|
}
|
2007-01-15 03:39:02 +03:00
|
|
|
|
2007-04-16 12:28:55 +04:00
|
|
|
void
|
|
|
|
puffs_setmaxreqlen(struct puffs_usermount *pu, size_t reqlen)
|
|
|
|
{
|
|
|
|
|
2007-05-12 01:27:13 +04:00
|
|
|
if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
|
2015-06-17 03:15:26 +03:00
|
|
|
warnx("%s: call has effect only before mount", __func__);
|
2007-04-16 12:28:55 +04:00
|
|
|
|
2007-10-11 23:41:13 +04:00
|
|
|
pu->pu_kargp->pa_maxmsglen = reqlen;
|
2007-04-16 12:28:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-04-16 17:04:49 +04:00
|
|
|
puffs_setfhsize(struct puffs_usermount *pu, size_t fhsize, int flags)
|
2007-04-16 12:28:55 +04:00
|
|
|
{
|
|
|
|
|
2007-05-12 01:27:13 +04:00
|
|
|
if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
|
2015-06-17 03:15:26 +03:00
|
|
|
warnx("%s: call has effect only before mount", __func__);
|
2007-04-16 12:28:55 +04:00
|
|
|
|
2007-07-19 16:52:28 +04:00
|
|
|
pu->pu_kargp->pa_fhsize = fhsize;
|
|
|
|
pu->pu_kargp->pa_fhflags = flags;
|
2007-04-16 12:28:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
puffs_setncookiehash(struct puffs_usermount *pu, int nhash)
|
|
|
|
{
|
|
|
|
|
2007-05-12 01:27:13 +04:00
|
|
|
if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
|
2015-06-17 03:15:26 +03:00
|
|
|
warnx("%s: call has effect only before mount", __func__);
|
2007-04-16 12:28:55 +04:00
|
|
|
|
2007-07-19 16:52:28 +04:00
|
|
|
pu->pu_kargp->pa_nhashbuckets = nhash;
|
2007-04-16 12:28:55 +04:00
|
|
|
}
|
|
|
|
|
2007-01-15 03:39:02 +03:00
|
|
|
void
|
|
|
|
puffs_set_pathbuild(struct puffs_usermount *pu, pu_pathbuild_fn fn)
|
|
|
|
{
|
|
|
|
|
|
|
|
pu->pu_pathbuild = fn;
|
2006-12-29 18:28:11 +03:00
|
|
|
}
|
|
|
|
|
2007-01-15 03:39:02 +03:00
|
|
|
void
|
|
|
|
puffs_set_pathtransform(struct puffs_usermount *pu, pu_pathtransform_fn fn)
|
|
|
|
{
|
|
|
|
|
|
|
|
pu->pu_pathtransform = fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
puffs_set_pathcmp(struct puffs_usermount *pu, pu_pathcmp_fn fn)
|
|
|
|
{
|
|
|
|
|
|
|
|
pu->pu_pathcmp = fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
puffs_set_pathfree(struct puffs_usermount *pu, pu_pathfree_fn fn)
|
|
|
|
{
|
|
|
|
|
|
|
|
pu->pu_pathfree = fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
puffs_set_namemod(struct puffs_usermount *pu, pu_namemod_fn fn)
|
|
|
|
{
|
|
|
|
|
|
|
|
pu->pu_namemod = fn;
|
|
|
|
}
|
2006-12-29 18:28:11 +03:00
|
|
|
|
2007-09-28 01:14:49 +04:00
|
|
|
void
|
|
|
|
puffs_set_errnotify(struct puffs_usermount *pu, pu_errnotify_fn fn)
|
|
|
|
{
|
|
|
|
|
|
|
|
pu->pu_errnotify = fn;
|
|
|
|
}
|
|
|
|
|
2007-12-25 23:38:01 +03:00
|
|
|
void
|
|
|
|
puffs_set_cmap(struct puffs_usermount *pu, pu_cmap_fn fn)
|
|
|
|
{
|
|
|
|
|
|
|
|
pu->pu_cmap = fn;
|
|
|
|
}
|
|
|
|
|
2007-05-15 17:44:46 +04:00
|
|
|
void
|
|
|
|
puffs_ml_setloopfn(struct puffs_usermount *pu, puffs_ml_loop_fn lfn)
|
|
|
|
{
|
|
|
|
|
|
|
|
pu->pu_ml_lfn = lfn;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
puffs_ml_settimeout(struct puffs_usermount *pu, struct timespec *ts)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (ts == NULL) {
|
|
|
|
pu->pu_ml_timep = NULL;
|
|
|
|
} else {
|
|
|
|
pu->pu_ml_timeout = *ts;
|
|
|
|
pu->pu_ml_timep = &pu->pu_ml_timeout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-28 21:40:30 +03:00
|
|
|
void
|
|
|
|
puffs_set_prepost(struct puffs_usermount *pu,
|
|
|
|
pu_prepost_fn pre, pu_prepost_fn pst)
|
|
|
|
{
|
|
|
|
|
|
|
|
pu->pu_oppre = pre;
|
|
|
|
pu->pu_oppost = pst;
|
|
|
|
}
|
|
|
|
|
2007-05-07 21:16:07 +04:00
|
|
|
void
|
|
|
|
puffs_setback(struct puffs_cc *pcc, int whatback)
|
|
|
|
{
|
2007-12-05 00:24:10 +03:00
|
|
|
struct puffs_req *preq = puffs__framebuf_getdataptr(pcc->pcc_pb);
|
2007-05-07 21:16:07 +04:00
|
|
|
|
|
|
|
assert(PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN && (
|
|
|
|
preq->preq_optype == PUFFS_VN_OPEN ||
|
|
|
|
preq->preq_optype == PUFFS_VN_MMAP ||
|
|
|
|
preq->preq_optype == PUFFS_VN_REMOVE ||
|
2007-07-02 14:24:17 +04:00
|
|
|
preq->preq_optype == PUFFS_VN_RMDIR ||
|
|
|
|
preq->preq_optype == PUFFS_VN_INACTIVE));
|
2007-05-07 21:16:07 +04:00
|
|
|
|
|
|
|
preq->preq_setbacks |= whatback & PUFFS_SETBACK_MASK;
|
|
|
|
}
|
|
|
|
|
2007-11-16 21:35:10 +03:00
|
|
|
int
|
|
|
|
puffs_daemon(struct puffs_usermount *pu, int nochdir, int noclose)
|
|
|
|
{
|
2008-12-13 14:53:25 +03:00
|
|
|
long int n;
|
2007-11-16 21:35:10 +03:00
|
|
|
int parent, value, fd;
|
|
|
|
|
|
|
|
if (pipe(pu->pu_dpipe) == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
switch (fork()) {
|
|
|
|
case -1:
|
|
|
|
return -1;
|
|
|
|
case 0:
|
|
|
|
parent = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
parent = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pu->pu_state |= PU_PUFFSDAEMON;
|
|
|
|
|
|
|
|
if (parent) {
|
2008-12-12 22:56:12 +03:00
|
|
|
close(pu->pu_dpipe[1]);
|
2007-11-16 21:35:10 +03:00
|
|
|
n = read(pu->pu_dpipe[0], &value, sizeof(int));
|
|
|
|
if (n == -1)
|
|
|
|
err(1, "puffs_daemon");
|
2008-12-12 22:56:12 +03:00
|
|
|
if (n != sizeof(value))
|
2008-12-13 14:53:25 +03:00
|
|
|
errx(1, "puffs_daemon got %ld bytes", n);
|
2007-11-16 21:35:10 +03:00
|
|
|
if (value) {
|
|
|
|
errno = value;
|
|
|
|
err(1, "puffs_daemon");
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
} else {
|
|
|
|
if (setsid() == -1)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (!nochdir)
|
|
|
|
chdir("/");
|
|
|
|
|
|
|
|
if (!noclose) {
|
|
|
|
fd = open(_PATH_DEVNULL, O_RDWR, 0);
|
|
|
|
if (fd == -1)
|
|
|
|
goto fail;
|
|
|
|
dup2(fd, STDIN_FILENO);
|
|
|
|
dup2(fd, STDOUT_FILENO);
|
|
|
|
dup2(fd, STDERR_FILENO);
|
|
|
|
if (fd > STDERR_FILENO)
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
n = write(pu->pu_dpipe[1], &errno, sizeof(int));
|
|
|
|
assert(n == 4);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-12-12 22:45:16 +03:00
|
|
|
static void
|
|
|
|
shutdaemon(struct puffs_usermount *pu, int error)
|
|
|
|
{
|
|
|
|
ssize_t n;
|
|
|
|
|
|
|
|
n = write(pu->pu_dpipe[1], &error, sizeof(int));
|
|
|
|
assert(n == 4);
|
|
|
|
close(pu->pu_dpipe[0]);
|
|
|
|
close(pu->pu_dpipe[1]);
|
|
|
|
pu->pu_state &= ~PU_PUFFSDAEMON;
|
|
|
|
}
|
|
|
|
|
2007-04-13 17:35:46 +04:00
|
|
|
int
|
2007-05-17 18:03:13 +04:00
|
|
|
puffs_mount(struct puffs_usermount *pu, const char *dir, int mntflags,
|
2008-08-12 23:44:39 +04:00
|
|
|
puffs_cookie_t cookie)
|
2007-04-13 17:35:46 +04:00
|
|
|
{
|
2007-11-16 21:35:10 +03:00
|
|
|
int rv, fd, sverrno;
|
2008-01-02 17:27:42 +03:00
|
|
|
char *comfd;
|
|
|
|
|
|
|
|
pu->pu_kargp->pa_root_cookie = cookie;
|
2007-04-13 17:35:46 +04:00
|
|
|
|
|
|
|
/* XXXkludgehere */
|
|
|
|
/* kauth doesn't provide this service any longer */
|
|
|
|
if (geteuid() != 0)
|
|
|
|
mntflags |= MNT_NOSUID | MNT_NODEV;
|
|
|
|
|
2008-01-02 17:27:42 +03:00
|
|
|
/*
|
|
|
|
* Undocumented... Well, documented only here.
|
|
|
|
*
|
|
|
|
* This is used for imaginative purposes. If the env variable is
|
|
|
|
* set, puffs_mount() doesn't do the regular mount procedure.
|
|
|
|
* Rather, it crams the mount data down the comfd and sets comfd as
|
|
|
|
* the puffs descriptor.
|
|
|
|
*
|
|
|
|
* This shouldn't be used unless you can read my mind ( ... or write
|
|
|
|
* it, not to mention execute it, but that's starting to get silly).
|
|
|
|
*/
|
|
|
|
if ((comfd = getenv("PUFFS_COMFD")) != NULL) {
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (sscanf(comfd, "%d", &pu->pu_fd) != 1) {
|
|
|
|
errno = EINVAL;
|
|
|
|
rv = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* check that what we got at least resembles an fd */
|
|
|
|
if (fcntl(pu->pu_fd, F_GETFL) == -1) {
|
|
|
|
rv = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define allwrite(buf, len) \
|
|
|
|
do { \
|
|
|
|
ssize_t al_rv; \
|
|
|
|
al_rv = write(pu->pu_fd, buf, len); \
|
2009-01-08 05:28:08 +03:00
|
|
|
if ((size_t)al_rv != len) { \
|
2008-01-02 17:27:42 +03:00
|
|
|
if (al_rv != -1) \
|
|
|
|
errno = EIO; \
|
|
|
|
rv = -1; \
|
|
|
|
goto out; \
|
|
|
|
} \
|
|
|
|
} while (/*CONSTCOND*/0)
|
2010-07-06 17:27:16 +04:00
|
|
|
len = strlen(dir)+1;
|
2008-01-02 17:27:42 +03:00
|
|
|
allwrite(&len, sizeof(len));
|
|
|
|
allwrite(dir, len);
|
2008-01-02 21:11:01 +03:00
|
|
|
len = strlen(pu->pu_kargp->pa_mntfromname)+1;
|
2008-01-02 20:57:51 +03:00
|
|
|
allwrite(&len, sizeof(len));
|
|
|
|
allwrite(pu->pu_kargp->pa_mntfromname, len);
|
2008-01-02 17:27:42 +03:00
|
|
|
allwrite(&mntflags, sizeof(mntflags));
|
2010-07-06 17:27:16 +04:00
|
|
|
len = sizeof(*pu->pu_kargp);
|
|
|
|
allwrite(&len, sizeof(len));
|
2008-01-02 17:27:42 +03:00
|
|
|
allwrite(pu->pu_kargp, sizeof(*pu->pu_kargp));
|
|
|
|
allwrite(&pu->pu_flags, sizeof(pu->pu_flags));
|
|
|
|
#undef allwrite
|
|
|
|
|
|
|
|
rv = 0;
|
|
|
|
} else {
|
2010-07-06 22:22:20 +04:00
|
|
|
char rp[MAXPATHLEN];
|
2014-12-22 11:16:21 +03:00
|
|
|
size_t rplen,dirlen;
|
2010-07-06 22:22:20 +04:00
|
|
|
|
|
|
|
if (realpath(dir, rp) == NULL) {
|
|
|
|
rv = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-12-22 11:16:21 +03:00
|
|
|
rplen = strlen(rp);
|
|
|
|
dirlen = strlen(dir);
|
|
|
|
if (strncmp(dir, rp, rplen) != 0 ||
|
|
|
|
strspn(dir + rplen, "/") != dirlen - rplen) {
|
2017-11-05 18:33:15 +03:00
|
|
|
warnx("%s: `%s' is a %s path.", __func__, dir,
|
|
|
|
dir[0] != '/' ? "relative" : "non canonical");
|
|
|
|
warnx("%s: using `%s' instead.", __func__, rp);
|
2010-07-06 22:22:20 +04:00
|
|
|
}
|
|
|
|
|
2008-01-02 17:27:42 +03:00
|
|
|
fd = open(_PATH_PUFFS, O_RDWR);
|
|
|
|
if (fd == -1) {
|
2017-11-05 18:33:15 +03:00
|
|
|
warnx("%s: cannot open `%s'", __func__, _PATH_PUFFS);
|
2008-01-02 17:27:42 +03:00
|
|
|
rv = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (fd <= 2)
|
2017-11-05 18:33:15 +03:00
|
|
|
warnx("%s: device fd %d (<= 2), sure this is "
|
|
|
|
"what you want?", __func__, fd);
|
2008-01-02 17:27:42 +03:00
|
|
|
|
|
|
|
pu->pu_kargp->pa_fd = pu->pu_fd = fd;
|
|
|
|
if ((rv = mount(MOUNT_PUFFS, rp, mntflags,
|
|
|
|
pu->pu_kargp, sizeof(struct puffs_kargs))) == -1)
|
|
|
|
goto out;
|
2007-11-06 18:09:07 +03:00
|
|
|
}
|
|
|
|
|
2007-05-17 18:03:13 +04:00
|
|
|
PU_SETSTATE(pu, PUFFS_STATE_RUNNING);
|
2007-04-13 17:35:46 +04:00
|
|
|
|
2007-07-19 16:52:28 +04:00
|
|
|
out:
|
2007-11-17 19:06:18 +03:00
|
|
|
if (rv != 0)
|
|
|
|
sverrno = errno;
|
|
|
|
else
|
|
|
|
sverrno = 0;
|
2007-07-19 16:52:28 +04:00
|
|
|
free(pu->pu_kargp);
|
|
|
|
pu->pu_kargp = NULL;
|
|
|
|
|
2008-12-12 22:45:16 +03:00
|
|
|
if (pu->pu_state & PU_PUFFSDAEMON)
|
|
|
|
shutdaemon(pu, sverrno);
|
2007-11-16 21:35:10 +03:00
|
|
|
|
|
|
|
errno = sverrno;
|
2007-07-19 16:52:28 +04:00
|
|
|
return rv;
|
2007-04-13 17:35:46 +04:00
|
|
|
}
|
2006-12-29 18:28:11 +03:00
|
|
|
|
2006-10-23 02:52:21 +04:00
|
|
|
struct puffs_usermount *
|
2010-05-21 14:50:52 +04:00
|
|
|
puffs_init(struct puffs_ops *pops, const char *mntfromname,
|
2007-07-17 15:34:51 +04:00
|
|
|
const char *puffsname, void *priv, uint32_t pflags)
|
2006-10-23 02:52:21 +04:00
|
|
|
{
|
|
|
|
struct puffs_usermount *pu;
|
2007-04-13 17:35:46 +04:00
|
|
|
struct puffs_kargs *pargs;
|
2007-11-06 18:09:07 +03:00
|
|
|
int sverrno;
|
2006-10-23 02:52:21 +04:00
|
|
|
|
2009-10-18 23:09:20 +04:00
|
|
|
if (puffsname == PUFFS_DEFER)
|
|
|
|
puffsname = "n/a";
|
|
|
|
if (mntfromname == PUFFS_DEFER)
|
|
|
|
mntfromname = "n/a";
|
|
|
|
if (priv == PUFFS_DEFER)
|
|
|
|
priv = NULL;
|
|
|
|
|
2006-10-23 02:52:21 +04:00
|
|
|
pu = malloc(sizeof(struct puffs_usermount));
|
2007-04-13 17:35:46 +04:00
|
|
|
if (pu == NULL)
|
|
|
|
goto failfree;
|
2007-05-15 20:45:22 +04:00
|
|
|
memset(pu, 0, sizeof(struct puffs_usermount));
|
2007-04-13 17:35:46 +04:00
|
|
|
|
2007-07-19 16:52:28 +04:00
|
|
|
pargs = pu->pu_kargp = malloc(sizeof(struct puffs_kargs));
|
|
|
|
if (pargs == NULL)
|
|
|
|
goto failfree;
|
|
|
|
memset(pargs, 0, sizeof(struct puffs_kargs));
|
|
|
|
|
2010-05-21 14:50:52 +04:00
|
|
|
pargs->pa_vers = PUFFSVERSION;
|
2007-04-13 17:35:46 +04:00
|
|
|
pargs->pa_flags = PUFFS_FLAG_KERN(pflags);
|
2010-05-21 14:50:52 +04:00
|
|
|
fillvnopmask(pops, pargs);
|
2009-10-18 23:09:20 +04:00
|
|
|
puffs_setmntinfo(pu, mntfromname, puffsname);
|
2006-10-23 02:52:21 +04:00
|
|
|
|
2007-05-17 18:03:13 +04:00
|
|
|
puffs_zerostatvfs(&pargs->pa_svfsb);
|
|
|
|
pargs->pa_root_cookie = NULL;
|
|
|
|
pargs->pa_root_vtype = VDIR;
|
|
|
|
pargs->pa_root_vsize = 0;
|
|
|
|
pargs->pa_root_rdev = 0;
|
2007-10-11 23:41:13 +04:00
|
|
|
pargs->pa_maxmsglen = 0;
|
2010-07-07 01:58:18 +04:00
|
|
|
if (/*CONSTCOND*/ sizeof(time_t) == 4)
|
2010-07-06 22:01:14 +04:00
|
|
|
pargs->pa_time32 = 1;
|
|
|
|
else
|
|
|
|
pargs->pa_time32 = 0;
|
2007-05-17 18:03:13 +04:00
|
|
|
|
2006-10-23 02:52:21 +04:00
|
|
|
pu->pu_flags = pflags;
|
2006-12-07 13:53:21 +03:00
|
|
|
pu->pu_ops = *pops;
|
2007-01-06 21:22:09 +03:00
|
|
|
free(pops); /* XXX */
|
2007-04-13 17:35:46 +04:00
|
|
|
|
2006-12-29 18:28:11 +03:00
|
|
|
pu->pu_privdata = priv;
|
2007-10-26 17:51:14 +04:00
|
|
|
pu->pu_cc_stackshift = PUFFS_CC_STACKSHIFT_DEFAULT;
|
2006-10-23 02:52:21 +04:00
|
|
|
LIST_INIT(&pu->pu_pnodelst);
|
2007-12-05 00:24:10 +03:00
|
|
|
LIST_INIT(&pu->pu_ios);
|
|
|
|
LIST_INIT(&pu->pu_ios_rmlist);
|
2008-01-17 00:29:59 +03:00
|
|
|
LIST_INIT(&pu->pu_ccmagazin);
|
2007-10-21 23:25:58 +04:00
|
|
|
TAILQ_INIT(&pu->pu_sched);
|
2006-10-23 02:52:21 +04:00
|
|
|
|
2008-01-28 21:35:49 +03:00
|
|
|
pu->pu_framectrl[PU_FRAMECTRL_FS].rfb = puffs__fsframe_read;
|
|
|
|
pu->pu_framectrl[PU_FRAMECTRL_FS].wfb = puffs__fsframe_write;
|
|
|
|
pu->pu_framectrl[PU_FRAMECTRL_FS].cmpfb = puffs__fsframe_cmp;
|
|
|
|
pu->pu_framectrl[PU_FRAMECTRL_FS].gotfb = puffs__fsframe_gotframe;
|
2007-12-05 00:24:10 +03:00
|
|
|
pu->pu_framectrl[PU_FRAMECTRL_FS].fdnotfn = puffs_framev_unmountonclose;
|
|
|
|
|
2007-01-15 03:39:02 +03:00
|
|
|
/* defaults for some user-settable translation functions */
|
|
|
|
pu->pu_cmap = NULL; /* identity translation */
|
|
|
|
|
2007-02-15 20:04:46 +03:00
|
|
|
pu->pu_pathbuild = puffs_stdpath_buildpath;
|
|
|
|
pu->pu_pathfree = puffs_stdpath_freepath;
|
|
|
|
pu->pu_pathcmp = puffs_stdpath_cmppath;
|
2007-01-15 03:39:02 +03:00
|
|
|
pu->pu_pathtransform = NULL;
|
|
|
|
pu->pu_namemod = NULL;
|
|
|
|
|
2009-12-05 23:54:10 +03:00
|
|
|
pu->pu_errnotify = puffs_kernerr_log;
|
2007-09-28 01:14:49 +04:00
|
|
|
|
2007-05-12 01:27:13 +04:00
|
|
|
PU_SETSTATE(pu, PUFFS_STATE_BEFOREMOUNT);
|
2006-10-23 02:52:21 +04:00
|
|
|
|
|
|
|
return pu;
|
2006-11-09 16:11:01 +03:00
|
|
|
|
2006-10-23 02:52:21 +04:00
|
|
|
failfree:
|
2006-11-09 16:11:01 +03:00
|
|
|
/* can't unmount() from here for obvious reasons */
|
2007-07-19 16:52:28 +04:00
|
|
|
sverrno = errno;
|
2006-10-23 02:52:21 +04:00
|
|
|
free(pu);
|
2007-07-19 16:52:28 +04:00
|
|
|
errno = sverrno;
|
2006-10-23 02:52:21 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-12-12 22:45:16 +03:00
|
|
|
void
|
|
|
|
puffs_cancel(struct puffs_usermount *pu, int error)
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(puffs_getstate(pu) < PUFFS_STATE_RUNNING);
|
|
|
|
shutdaemon(pu, error);
|
|
|
|
free(pu);
|
|
|
|
}
|
|
|
|
|
2007-04-13 17:35:46 +04:00
|
|
|
/*ARGSUSED1*/
|
2007-01-06 21:22:09 +03:00
|
|
|
int
|
2010-01-08 01:49:19 +03:00
|
|
|
puffs_exit(struct puffs_usermount *pu, int unused /* strict compat */)
|
2007-01-06 21:22:09 +03:00
|
|
|
{
|
2010-01-08 01:49:19 +03:00
|
|
|
struct puffs_framebuf *pb;
|
|
|
|
struct puffs_req *preq;
|
|
|
|
void *winp;
|
|
|
|
size_t winlen;
|
|
|
|
int sverrno;
|
2007-01-06 21:22:09 +03:00
|
|
|
|
2010-01-08 01:49:19 +03:00
|
|
|
pb = puffs_framebuf_make();
|
|
|
|
if (pb == NULL) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
2007-01-06 21:22:09 +03:00
|
|
|
|
2010-01-08 01:49:19 +03:00
|
|
|
winlen = sizeof(struct puffs_req);
|
|
|
|
if (puffs_framebuf_getwindow(pb, 0, &winp, &winlen) == -1) {
|
|
|
|
sverrno = errno;
|
|
|
|
puffs_framebuf_destroy(pb);
|
|
|
|
errno = sverrno;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
preq = winp;
|
2007-01-06 21:22:09 +03:00
|
|
|
|
2010-01-08 01:49:19 +03:00
|
|
|
preq->preq_buflen = sizeof(struct puffs_req);
|
|
|
|
preq->preq_opclass = PUFFSOP_UNMOUNT;
|
|
|
|
preq->preq_id = puffs__nextreq(pu);
|
2007-05-12 01:27:13 +04:00
|
|
|
|
2010-01-08 01:49:19 +03:00
|
|
|
puffs_framev_enqueue_justsend(pu, puffs_getselectable(pu), pb, 1, 0);
|
2007-01-06 21:22:09 +03:00
|
|
|
|
2010-01-08 01:49:19 +03:00
|
|
|
return 0;
|
2007-01-06 21:22:09 +03:00
|
|
|
}
|
|
|
|
|
2018-02-08 12:05:16 +03:00
|
|
|
/* no sigset_t static initializer */
|
2010-01-12 21:42:38 +03:00
|
|
|
static int sigs[NSIG] = { 0, };
|
|
|
|
static int sigcatch = 0;
|
|
|
|
|
|
|
|
int
|
|
|
|
puffs_unmountonsignal(int sig, bool sigignore)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (sig < 0 || sig >= (int)NSIG) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (sigignore)
|
|
|
|
if (signal(sig, SIG_IGN) == SIG_ERR)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!sigs[sig])
|
|
|
|
sigcatch++;
|
|
|
|
sigs[sig] = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-28 21:35:49 +03:00
|
|
|
/*
|
|
|
|
* Actual mainloop. This is called from a context which can block.
|
|
|
|
* It is called either from puffs_mainloop (indirectly, via
|
|
|
|
* puffs_cc_continue() or from puffs_cc_yield()).
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
puffs__theloop(struct puffs_cc *pcc)
|
2006-10-23 02:52:21 +04:00
|
|
|
{
|
2008-01-28 21:35:49 +03:00
|
|
|
struct puffs_usermount *pu = pcc->pcc_pu;
|
2007-12-05 00:24:10 +03:00
|
|
|
struct puffs_framectrl *pfctrl;
|
2007-05-12 01:27:13 +04:00
|
|
|
struct puffs_fctrl_io *fio;
|
2007-12-05 00:24:10 +03:00
|
|
|
struct kevent *curev;
|
2007-05-12 01:27:13 +04:00
|
|
|
size_t nchanges;
|
2008-01-28 21:35:49 +03:00
|
|
|
int ndone;
|
2007-12-05 00:24:10 +03:00
|
|
|
|
2007-05-12 01:27:13 +04:00
|
|
|
while (puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED) {
|
2010-01-12 21:42:38 +03:00
|
|
|
|
2008-01-28 21:35:49 +03:00
|
|
|
/*
|
|
|
|
* Schedule existing requests.
|
|
|
|
*/
|
|
|
|
while ((pcc = TAILQ_FIRST(&pu->pu_sched)) != NULL) {
|
|
|
|
TAILQ_REMOVE(&pu->pu_sched, pcc, pcc_schedent);
|
|
|
|
puffs__goto(pcc);
|
|
|
|
}
|
|
|
|
|
2007-05-15 17:44:46 +04:00
|
|
|
if (pu->pu_ml_lfn)
|
|
|
|
pu->pu_ml_lfn(pu);
|
|
|
|
|
2007-12-05 00:24:10 +03:00
|
|
|
/* XXX: can we still do these optimizations? */
|
|
|
|
#if 0
|
2007-05-18 17:24:23 +04:00
|
|
|
/*
|
|
|
|
* Do this here, because:
|
|
|
|
* a) loopfunc might generate some results
|
|
|
|
* b) it's still "after" event handling (except for round 1)
|
|
|
|
*/
|
|
|
|
if (puffs_req_putput(ppr) == -1)
|
|
|
|
goto out;
|
|
|
|
puffs_req_resetput(ppr);
|
|
|
|
|
2007-07-05 16:27:39 +04:00
|
|
|
/* micro optimization: skip kevent syscall if possible */
|
2007-12-05 00:24:10 +03:00
|
|
|
if (pu->pu_nfds == 1 && pu->pu_ml_timep == NULL
|
2007-07-05 16:27:39 +04:00
|
|
|
&& (pu->pu_state & PU_ASYNCFD) == 0) {
|
2007-12-05 00:24:10 +03:00
|
|
|
pfctrl = XXX->fctrl;
|
|
|
|
puffs_framev_input(pu, pfctrl, XXX);
|
2007-07-05 16:27:39 +04:00
|
|
|
continue;
|
|
|
|
}
|
2007-12-05 00:24:10 +03:00
|
|
|
#endif
|
2007-07-05 16:27:39 +04:00
|
|
|
|
|
|
|
/* else: do full processing */
|
2007-12-05 00:24:10 +03:00
|
|
|
/* Don't bother worrying about O(n) for now */
|
|
|
|
LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
|
2007-05-15 17:44:46 +04:00
|
|
|
if (fio->stat & FIO_WRGONE)
|
|
|
|
continue;
|
|
|
|
|
2007-12-05 00:24:10 +03:00
|
|
|
pfctrl = fio->fctrl;
|
|
|
|
|
2007-05-12 01:27:13 +04:00
|
|
|
/*
|
|
|
|
* Try to write out everything to avoid the
|
|
|
|
* need for enabling EVFILT_WRITE. The likely
|
|
|
|
* case is that we can fit everything into the
|
|
|
|
* socket buffer.
|
|
|
|
*/
|
2008-01-28 21:35:49 +03:00
|
|
|
puffs__framev_output(pu, pfctrl, fio);
|
2007-12-05 00:24:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build list of which to enable/disable in writecheck.
|
|
|
|
*/
|
2007-12-05 15:11:56 +03:00
|
|
|
nchanges = 0;
|
2007-12-05 00:24:10 +03:00
|
|
|
LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
|
|
|
|
if (fio->stat & FIO_WRGONE)
|
|
|
|
continue;
|
2007-05-12 01:27:13 +04:00
|
|
|
|
2007-05-15 17:44:46 +04:00
|
|
|
/* en/disable write checks for kqueue as needed */
|
2007-05-12 01:27:13 +04:00
|
|
|
assert((FIO_EN_WRITE(fio) && FIO_RM_WRITE(fio)) == 0);
|
|
|
|
if (FIO_EN_WRITE(fio)) {
|
2007-12-05 00:24:10 +03:00
|
|
|
EV_SET(&pu->pu_evs[nchanges], fio->io_fd,
|
2007-05-12 01:27:13 +04:00
|
|
|
EVFILT_WRITE, EV_ENABLE, 0, 0,
|
2017-05-31 20:56:00 +03:00
|
|
|
(intptr_t)fio);
|
2007-05-15 17:44:46 +04:00
|
|
|
fio->stat |= FIO_WR;
|
2007-05-12 01:27:13 +04:00
|
|
|
nchanges++;
|
|
|
|
}
|
|
|
|
if (FIO_RM_WRITE(fio)) {
|
2007-12-05 00:24:10 +03:00
|
|
|
EV_SET(&pu->pu_evs[nchanges], fio->io_fd,
|
2007-05-12 01:27:13 +04:00
|
|
|
EVFILT_WRITE, EV_DISABLE, 0, 0,
|
2017-05-31 20:56:00 +03:00
|
|
|
(intptr_t)fio);
|
2007-05-15 17:44:46 +04:00
|
|
|
fio->stat &= ~FIO_WR;
|
2007-05-12 01:27:13 +04:00
|
|
|
nchanges++;
|
|
|
|
}
|
|
|
|
}
|
2006-12-29 18:28:11 +03:00
|
|
|
|
2007-12-05 00:24:10 +03:00
|
|
|
ndone = kevent(pu->pu_kq, pu->pu_evs, nchanges,
|
2010-01-12 21:42:38 +03:00
|
|
|
pu->pu_evs, pu->pu_nevs, pu->pu_ml_timep);
|
2007-05-18 17:24:23 +04:00
|
|
|
|
|
|
|
if (ndone == -1) {
|
|
|
|
if (errno != EINTR)
|
2008-01-28 21:35:49 +03:00
|
|
|
break;
|
2007-05-18 17:24:23 +04:00
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* uoptimize */
|
2007-05-15 17:44:46 +04:00
|
|
|
if (ndone == 0)
|
|
|
|
continue;
|
2007-05-12 01:27:13 +04:00
|
|
|
|
|
|
|
/* iterate over the results */
|
2007-12-05 00:24:10 +03:00
|
|
|
for (curev = pu->pu_evs; ndone--; curev++) {
|
2007-07-20 17:14:55 +04:00
|
|
|
int what;
|
|
|
|
|
2007-12-05 00:24:10 +03:00
|
|
|
#if 0
|
2007-05-12 01:27:13 +04:00
|
|
|
/* get & possibly dispatch events from kernel */
|
|
|
|
if (curev->ident == puffsfd) {
|
|
|
|
if (puffs_req_handle(pgr, ppr, 0) == -1)
|
|
|
|
goto out;
|
|
|
|
continue;
|
|
|
|
}
|
2007-12-05 00:24:10 +03:00
|
|
|
#endif
|
2007-05-12 01:27:13 +04:00
|
|
|
|
2007-05-15 17:44:46 +04:00
|
|
|
fio = (void *)curev->udata;
|
2010-01-12 21:42:38 +03:00
|
|
|
if (__predict_true(fio))
|
|
|
|
pfctrl = fio->fctrl;
|
|
|
|
else
|
|
|
|
pfctrl = NULL;
|
2007-05-15 17:44:46 +04:00
|
|
|
if (curev->flags & EV_ERROR) {
|
|
|
|
assert(curev->filter == EVFILT_WRITE);
|
|
|
|
fio->stat &= ~FIO_WR;
|
|
|
|
|
|
|
|
/* XXX: how to know if it's a transient error */
|
2008-01-28 21:35:49 +03:00
|
|
|
puffs__framev_writeclose(pu, fio,
|
2007-05-15 17:44:46 +04:00
|
|
|
(int)curev->data);
|
2008-01-28 21:35:49 +03:00
|
|
|
puffs__framev_notify(fio, PUFFS_FBIO_ERROR);
|
2007-05-15 17:44:46 +04:00
|
|
|
continue;
|
2007-05-12 01:27:13 +04:00
|
|
|
}
|
|
|
|
|
2007-07-20 17:14:55 +04:00
|
|
|
what = 0;
|
2018-06-30 19:05:44 +03:00
|
|
|
switch (curev->filter) {
|
|
|
|
case EVFILT_READ:
|
2008-01-28 21:35:49 +03:00
|
|
|
puffs__framev_input(pu, pfctrl, fio);
|
2007-07-20 17:14:55 +04:00
|
|
|
what |= PUFFS_FBIO_READ;
|
2018-06-30 19:05:44 +03:00
|
|
|
break;
|
|
|
|
case EVFILT_WRITE:
|
2008-01-28 21:35:49 +03:00
|
|
|
puffs__framev_output(pu, pfctrl, fio);
|
2007-07-20 17:14:55 +04:00
|
|
|
what |= PUFFS_FBIO_WRITE;
|
2018-06-30 19:05:44 +03:00
|
|
|
break;
|
|
|
|
case EVFILT_SIGNAL:
|
2010-01-12 21:42:38 +03:00
|
|
|
if ((pu->pu_state & PU_DONEXIT) == 0) {
|
|
|
|
PU_SETSFLAG(pu, PU_DONEXIT);
|
|
|
|
puffs_exit(pu, 0);
|
|
|
|
}
|
2018-06-30 19:05:44 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
warn("unhandled filter %d", curev->filter);
|
2010-01-12 21:42:38 +03:00
|
|
|
}
|
2007-07-20 17:14:55 +04:00
|
|
|
if (what)
|
2008-01-28 21:35:49 +03:00
|
|
|
puffs__framev_notify(fio, what);
|
2007-10-21 23:25:58 +04:00
|
|
|
}
|
|
|
|
|
2007-05-15 17:44:46 +04:00
|
|
|
/*
|
|
|
|
* Really free fd's now that we don't have references
|
|
|
|
* to them.
|
|
|
|
*/
|
2007-12-05 00:24:10 +03:00
|
|
|
while ((fio = LIST_FIRST(&pu->pu_ios_rmlist)) != NULL) {
|
2007-05-15 17:44:46 +04:00
|
|
|
LIST_REMOVE(fio, fio_entries);
|
|
|
|
free(fio);
|
|
|
|
}
|
2006-12-29 18:28:11 +03:00
|
|
|
}
|
2008-08-11 19:59:01 +04:00
|
|
|
|
|
|
|
if (puffs__cc_restoremain(pu) == -1)
|
|
|
|
warn("cannot restore main context. impending doom");
|
2008-01-28 21:35:49 +03:00
|
|
|
}
|
|
|
|
int
|
|
|
|
puffs_mainloop(struct puffs_usermount *pu)
|
|
|
|
{
|
|
|
|
struct puffs_fctrl_io *fio;
|
|
|
|
struct puffs_cc *pcc;
|
|
|
|
struct kevent *curev;
|
2010-01-12 21:42:38 +03:00
|
|
|
size_t nevs;
|
|
|
|
int sverrno, i;
|
2008-01-28 21:35:49 +03:00
|
|
|
|
|
|
|
assert(puffs_getstate(pu) >= PUFFS_STATE_RUNNING);
|
|
|
|
|
|
|
|
pu->pu_kq = kqueue();
|
|
|
|
if (pu->pu_kq == -1)
|
|
|
|
goto out;
|
|
|
|
pu->pu_state |= PU_HASKQ;
|
|
|
|
|
|
|
|
puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK);
|
|
|
|
if (puffs__framev_addfd_ctrl(pu, puffs_getselectable(pu),
|
|
|
|
PUFFS_FBIO_READ | PUFFS_FBIO_WRITE,
|
|
|
|
&pu->pu_framectrl[PU_FRAMECTRL_FS]) == -1)
|
|
|
|
goto out;
|
|
|
|
|
2010-01-12 21:42:38 +03:00
|
|
|
nevs = pu->pu_nevs + sigcatch;
|
|
|
|
curev = realloc(pu->pu_evs, nevs * sizeof(struct kevent));
|
2008-01-28 21:35:49 +03:00
|
|
|
if (curev == NULL)
|
|
|
|
goto out;
|
|
|
|
pu->pu_evs = curev;
|
2010-01-12 21:42:38 +03:00
|
|
|
pu->pu_nevs = nevs;
|
2008-01-28 21:35:49 +03:00
|
|
|
|
|
|
|
LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
|
|
|
|
EV_SET(curev, fio->io_fd, EVFILT_READ, EV_ADD,
|
2017-05-31 20:56:00 +03:00
|
|
|
0, 0, (intptr_t)fio);
|
2008-01-28 21:35:49 +03:00
|
|
|
curev++;
|
|
|
|
EV_SET(curev, fio->io_fd, EVFILT_WRITE, EV_ADD | EV_DISABLE,
|
2017-05-31 20:56:00 +03:00
|
|
|
0, 0, (intptr_t)fio);
|
2008-01-28 21:35:49 +03:00
|
|
|
curev++;
|
|
|
|
}
|
2010-01-12 21:42:38 +03:00
|
|
|
for (i = 0; i < NSIG; i++) {
|
|
|
|
if (sigs[i]) {
|
|
|
|
EV_SET(curev, i, EVFILT_SIGNAL, EV_ADD | EV_ENABLE,
|
|
|
|
0, 0, 0);
|
|
|
|
curev++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(curev - pu->pu_evs == (ssize_t)pu->pu_nevs);
|
|
|
|
if (kevent(pu->pu_kq, pu->pu_evs, pu->pu_nevs, NULL, 0, NULL) == -1)
|
2008-01-28 21:35:49 +03:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
pu->pu_state |= PU_INLOOP;
|
|
|
|
|
2008-08-11 19:59:01 +04:00
|
|
|
/*
|
|
|
|
* Create alternate execution context and jump to it. Note
|
|
|
|
* that we come "out" of savemain twice. Where we come out
|
|
|
|
* of it depends on the architecture. If the return address is
|
|
|
|
* stored on the stack, we jump out from puffs_cc_continue(),
|
|
|
|
* for a register return address from puffs__cc_savemain().
|
|
|
|
* PU_MAINRESTORE makes sure we DTRT in both cases.
|
|
|
|
*/
|
|
|
|
if (puffs__cc_create(pu, puffs__theloop, &pcc) == -1) {
|
2008-01-28 21:35:49 +03:00
|
|
|
goto out;
|
2008-08-11 19:59:01 +04:00
|
|
|
}
|
2011-11-14 05:27:42 +04:00
|
|
|
|
|
|
|
#if 0
|
2008-08-11 19:59:01 +04:00
|
|
|
if (puffs__cc_savemain(pu) == -1) {
|
|
|
|
goto out;
|
|
|
|
}
|
2011-11-14 05:27:42 +04:00
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* XXX
|
|
|
|
* puffs__cc_savemain() uses getcontext() and then returns.
|
|
|
|
* the caller (this function) may overwrite the stack frame
|
|
|
|
* of puffs__cc_savemain(), so when we call setcontext() later and
|
|
|
|
* return from puffs__cc_savemain() again, the return address or
|
|
|
|
* saved stack pointer can be garbage.
|
|
|
|
* avoid this by calling getcontext() directly here.
|
|
|
|
*/
|
|
|
|
extern int puffs_fakecc;
|
|
|
|
if (!puffs_fakecc) {
|
|
|
|
PU_CLRSFLAG(pu, PU_MAINRESTORE);
|
|
|
|
if (getcontext(&pu->pu_mainctx) == -1) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-08-11 19:59:01 +04:00
|
|
|
if ((pu->pu_state & PU_MAINRESTORE) == 0)
|
|
|
|
puffs_cc_continue(pcc);
|
2008-01-28 21:35:49 +03:00
|
|
|
|
2007-12-05 00:24:10 +03:00
|
|
|
finalpush(pu);
|
2007-05-12 01:27:13 +04:00
|
|
|
errno = 0;
|
2006-12-29 18:28:11 +03:00
|
|
|
|
|
|
|
out:
|
2007-05-12 01:27:13 +04:00
|
|
|
/* store the real error for a while */
|
|
|
|
sverrno = errno;
|
|
|
|
|
|
|
|
errno = sverrno;
|
|
|
|
if (errno)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 0;
|
2006-10-23 02:52:21 +04:00
|
|
|
}
|