Home | History | Annotate | Line # | Download | only in libperfuse
perfuse.c revision 1.37
      1 /*  $NetBSD: perfuse.c,v 1.37 2015/06/19 17:33:20 christos Exp $ */
      2 
      3 /*-
      4  *  Copyright (c) 2010-2011 Emmanuel Dreyfus. All rights reserved.
      5  *
      6  *  Redistribution and use in source and binary forms, with or without
      7  *  modification, are permitted provided that the following conditions
      8  *  are met:
      9  *  1. Redistributions of source code must retain the above copyright
     10  *     notice, this list of conditions and the following disclaimer.
     11  *  2. Redistributions in binary form must reproduce the above copyright
     12  *     notice, this list of conditions and the following disclaimer in the
     13  *     documentation and/or other materials provided with the distribution.
     14  *
     15  *  THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     16  *  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     17  *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18  *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     19  *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     20  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     21  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     22  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     23  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     24  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  *  POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include <stdio.h>
     29 #include <unistd.h>
     30 #include <stdlib.h>
     31 #include <fcntl.h>
     32 #include <string.h>
     33 #include <errno.h>
     34 #include <puffs.h>
     35 #include <sys/types.h>
     36 #include <sys/mman.h>
     37 #include <sys/resource.h>
     38 #include <sys/socket.h>
     39 #include <sys/extattr.h>
     40 #include <sys/hash.h>
     41 #include <sys/un.h>
     42 #include <machine/vmparam.h>
     43 
     44 #define LIBPERFUSE
     45 #include "perfuse.h"
     46 #include "perfuse_if.h"
     47 #include "perfuse_priv.h"
     48 
     49 int perfuse_diagflags = 0; /* global used only in DPRINTF/DERR/DWARN */
     50 extern char **environ;
     51 
     52 static struct perfuse_state *init_state(void);
     53 static int get_fd(const char *);
     54 
     55 
     56 static struct perfuse_state *
     57 init_state(void)
     58 {
     59 	struct perfuse_state *ps;
     60 	size_t len;
     61 	char opts[1024];
     62 	int i;
     63 
     64 	if ((ps = malloc(sizeof(*ps))) == NULL)
     65 		DERR(EX_OSERR, "%s:%d malloc failed", __func__, __LINE__);
     66 
     67 	(void)memset(ps, 0, sizeof(*ps));
     68 	ps->ps_max_write = UINT_MAX;
     69 	ps->ps_max_readahead = UINT_MAX;
     70 	TAILQ_INIT(&ps->ps_trace);
     71 
     72 	ps->ps_nnidhash = PUFFS_PNODEBUCKETS;
     73 	len = sizeof(*ps->ps_nidhash) * ps->ps_nnidhash;
     74 	if ((ps->ps_nidhash = malloc(len)) == NULL)
     75 		DERR(EX_OSERR, "%s:%d malloc failed", __func__, __LINE__);
     76 	for (i = 0; i < ps->ps_nnidhash; i++)
     77 		LIST_INIT(&ps->ps_nidhash[i]);
     78 
     79 	/*
     80 	 * Most of the time, access() is broken because the filesystem
     81 	 * performs the check with root privileges. glusterfs will do that
     82 	 * if the Linux-specific setfsuid() is missing, for instance.
     83 	 */
     84 	ps->ps_flags |= PS_NO_ACCESS;
     85 
     86 	/*
     87 	 * This is a temporary way to toggle access and creat usage.
     88 	 * It would be nice if that could be provided as mount options,
     89 	 * but that will not be obvious to do.
     90  	 */
     91 	if (getenv_r("PERFUSE_OPTIONS", opts, sizeof(opts)) != -1) {
     92 		char *optname;
     93 		char *last;
     94 
     95 		 for ((optname = strtok_r(opts, ",", &last));
     96 		      optname != NULL;
     97 		      (optname = strtok_r(NULL, ",", &last))) {
     98 			if (strcmp(optname, "enable_access") == 0)
     99 				ps->ps_flags &= ~PS_NO_ACCESS;
    100 
    101 			if (strcmp(optname, "disable_access") == 0)
    102 				ps->ps_flags |= PS_NO_ACCESS;
    103 
    104 			if (strcmp(optname, "enable_creat") == 0)
    105 				ps->ps_flags &= ~PS_NO_CREAT;
    106 
    107 			if (strcmp(optname, "disable_creat") == 0)
    108 				ps->ps_flags |= PS_NO_CREAT;
    109 		}
    110 	}
    111 
    112 
    113 	return ps;
    114 }
    115 
    116 
    117 static int
    118 get_fd(const char *data)
    119 {
    120 	char *string;
    121 	const char fdopt[] = "fd=";
    122 	char *lastp;
    123 	char *opt;
    124 	int fd = -1;
    125 
    126 	if ((string = strdup(data)) == NULL)
    127 		return -1;
    128 
    129 	for (opt = strtok_r(string, ",", &lastp);
    130 	     opt != NULL;
    131 	     opt = strtok_r(NULL, ",", &lastp)) {
    132 		if (strncmp(opt, fdopt, strlen(fdopt)) == 0) {
    133 			fd =  atoi(opt + strlen(fdopt));
    134 			break;
    135 		}
    136 	}
    137 
    138 	/*
    139 	 * No file descriptor found
    140 	 */
    141 	if (fd == -1)
    142 		errno = EINVAL;
    143 
    144 	free(string);
    145 	return fd;
    146 
    147 }
    148 
    149 int
    150 perfuse_open(const char *path, int flags, mode_t mode)
    151 {
    152 	int sv[2];
    153 	struct sockaddr_un sun;
    154 	struct sockaddr *sa;
    155 	char progname[] = _PATH_PERFUSED;
    156 	char minus_i[] = "-i";
    157 	char fdstr[16];
    158 	char *const argv[] = { progname, minus_i, fdstr, NULL};
    159 	uint32_t opt;
    160 	uint32_t optlen;
    161 	int sock_type = SOCK_SEQPACKET;
    162 
    163 	if (strcmp(path, _PATH_FUSE) != 0)
    164 		return open(path, flags, mode);
    165 
    166 	/*
    167 	 * Try SOCK_SEQPACKET then SOCK_DGRAM if unavailable
    168 	 */
    169 	if ((sv[0] = socket(PF_LOCAL, SOCK_SEQPACKET, 0)) == -1) {
    170 		sock_type = SOCK_DGRAM;
    171                 DWARNX("SEQPACKET local sockets unavailable, using less "
    172 		       "reliable DGRAM sockets. Expect file operation hangs.");
    173 
    174 		if ((sv[0] = socket(PF_LOCAL, SOCK_DGRAM, 0)) == -1) {
    175 #ifdef PERFUSE_DEBUG
    176 			DWARN("%s: %d socket failed", __func__, __LINE__);
    177 #endif
    178 			return -1;
    179 		}
    180 	}
    181 
    182 	/*
    183 	 * Set a buffer lentgh large enough so that any FUSE packet
    184 	 * will fit.
    185 	 */
    186 	opt = (uint32_t)FUSE_BUFSIZE;
    187 	optlen = sizeof(opt);
    188 	if (setsockopt(sv[0], SOL_SOCKET, SO_SNDBUF, &opt, optlen) != 0)
    189 		DWARN("%s: setsockopt SO_SNDBUF to %d failed", __func__, opt);
    190 
    191 	if (setsockopt(sv[0], SOL_SOCKET, SO_RCVBUF, &opt, optlen) != 0)
    192 		DWARN("%s: setsockopt SO_RCVBUF to %d failed", __func__, opt);
    193 
    194 	sa = (struct sockaddr *)(void *)&sun;
    195 	sun.sun_len = sizeof(sun);
    196 	sun.sun_family = AF_LOCAL;
    197 	(void)strcpy(sun.sun_path, path);
    198 
    199 	if (connect(sv[0], sa, (socklen_t)sun.sun_len) == 0)
    200 		return sv[0];
    201 
    202 	/*
    203 	 * Attempt to run perfused on our own
    204 	 * if it does not run yet; In that case
    205 	 * we will talk using a socketpair
    206 	 * instead of /dev/fuse.
    207 	 */
    208 	if (socketpair(PF_LOCAL, sock_type, 0, sv) != 0) {
    209 		DWARN("%s:%d: socketpair failed", __func__, __LINE__);
    210 		return -1;
    211 	}
    212 
    213 	/*
    214 	 * Set a buffer lentgh large enough so that any FUSE packet
    215 	 * will fit.
    216 	 */
    217 	opt = (uint32_t)(4 * FUSE_BUFSIZE);
    218 	optlen = sizeof(opt);
    219 	if (setsockopt(sv[0], SOL_SOCKET, SO_SNDBUF, &opt, optlen) != 0)
    220 		DWARN("%s: setsockopt SO_SNDBUF to %d failed", __func__, opt);
    221 
    222 	if (setsockopt(sv[0], SOL_SOCKET, SO_RCVBUF, &opt, optlen) != 0)
    223 		DWARN("%s: setsockopt SO_RCVBUF to %d failed", __func__, opt);
    224 
    225 	if (setsockopt(sv[1], SOL_SOCKET, SO_SNDBUF, &opt, optlen) != 0)
    226 		DWARN("%s: setsockopt SO_SNDBUF to %d failed", __func__, opt);
    227 
    228 	if (setsockopt(sv[1], SOL_SOCKET, SO_RCVBUF, &opt, optlen) != 0)
    229 		DWARN("%s: setsockopt SO_RCVBUF to %d failed", __func__, opt);
    230 
    231 	/*
    232 	 * Request peer credentials. This musr be done before first
    233 	 * frame is sent.
    234 	 */
    235 	opt = 1;
    236 	optlen = sizeof(opt);
    237 	if (setsockopt(sv[1], 0, LOCAL_CREDS, &opt, optlen) != 0)
    238 		DWARN("%s: setsockopt LOCAL_CREDS failed", __func__);
    239 
    240 	(void)sprintf(fdstr, "%d", sv[1]);
    241 
    242 	switch(fork()) {
    243 	case -1:
    244 #ifdef PERFUSE_DEBUG
    245 		DWARN("%s:%d: fork failed", __func__, __LINE__);
    246 #endif
    247 		return -1;
    248 		/* NOTREACHED */
    249 		break;
    250 	case 0:
    251 		(void)close(sv[0]);
    252 		(void)execve(argv[0], argv, environ);
    253 #ifdef PERFUSE_DEBUG
    254 		DWARN("%s:%d: execve failed", __func__, __LINE__);
    255 #endif
    256 		return -1;
    257 		/* NOTREACHED */
    258 		break;
    259 	default:
    260 		break;
    261 	}
    262 
    263 	(void)close(sv[1]);
    264 	return sv[0];
    265 }
    266 
    267 int
    268 perfuse_mount(const char *source, const char *target,
    269 	const char *filesystemtype, long mountflags, const void *data)
    270 {
    271 	int s;
    272 	size_t len;
    273 	struct perfuse_mount_out *pmo;
    274 	struct sockaddr_storage ss;
    275 	struct sockaddr_un *sun;
    276 	struct sockaddr *sa;
    277 	socklen_t sa_len;
    278 	size_t sock_len;
    279 	char *frame;
    280 	char *cp;
    281 
    282 #ifdef PERFUSE_DEBUG
    283 	if (perfuse_diagflags & PDF_MISC)
    284 		DPRINTF("%s(\"%s\", \"%s\", \"%s\", 0x%lx, \"%s\")\n",
    285 			__func__, source, target, filesystemtype,
    286 			mountflags, (const char *)data);
    287 #endif
    288 
    289 	if ((s = get_fd(data)) == -1)
    290 		return -1;
    291 
    292 	/*
    293 	 * If we are connected to /dev/fuse, we need a second
    294 	 * socket to get replies from perfused.
    295 	 * XXX This socket is not removed at exit time yet
    296 	 */
    297 	sock_len = 0;
    298 	sa = (struct sockaddr *)(void *)&ss;
    299 	sun = (struct sockaddr_un *)(void *)&ss;
    300 	sa_len = sizeof(ss);
    301 	if ((getpeername(s, sa, &sa_len) == 0) &&
    302 	    (sa->sa_family = AF_LOCAL) &&
    303 	    (strcmp(sun->sun_path, _PATH_FUSE) == 0)) {
    304 
    305 		sun->sun_len = sizeof(*sun);
    306 		sun->sun_family = AF_LOCAL;
    307 		(void)sprintf(sun->sun_path, "%s/%s-%d",
    308 			      _PATH_TMP, getprogname(), getpid());
    309 
    310 		if (bind(s, sa, (socklen_t)sa->sa_len) != 0)
    311 			DERR(EX_OSERR, "%s:%d bind to \"%s\" failed",
    312 			     __func__, __LINE__, sun->sun_path);
    313 
    314 		sock_len = strlen(sun->sun_path) + 1;
    315 	}
    316 
    317 	len = sizeof(*pmo);
    318 	len += source ? (uint32_t)strlen(source) + 1 : 0;
    319 	len += target ? (uint32_t)strlen(target) + 1 : 0;
    320 	len += filesystemtype ? (uint32_t)strlen(filesystemtype) + 1 : 0;
    321 	len += data ? (uint32_t)strlen(data) + 1 : 0;
    322 	len += sock_len;
    323 
    324 	if ((frame = malloc(len)) == NULL) {
    325 #ifdef PERFUSE_DEBUG
    326 		if (perfuse_diagflags & PDF_MISC)
    327 			DWARN("%s:%d malloc failed", __func__, __LINE__);
    328 #endif
    329 		return -1;
    330 	}
    331 
    332 	pmo = (struct perfuse_mount_out *)(void *)frame;
    333 	pmo->pmo_len = (uint32_t)len;
    334 	pmo->pmo_error = 0;
    335 	pmo->pmo_unique = (uint64_t)-1;
    336 	(void)strcpy(pmo->pmo_magic, PERFUSE_MOUNT_MAGIC);
    337 
    338 	pmo->pmo_source_len = source ? (uint32_t)strlen(source) + 1 : 0;
    339 	pmo->pmo_target_len = target ? (uint32_t)strlen(target) + 1: 0;
    340 	pmo->pmo_filesystemtype_len =
    341 	    filesystemtype ? (uint32_t)strlen(filesystemtype) + 1 : 0;
    342 	pmo->pmo_mountflags = (uint32_t)mountflags;
    343 	pmo->pmo_data_len = data ? (uint32_t)strlen(data) + 1 : 0;
    344 	pmo->pmo_sock_len = (uint32_t)sock_len;
    345 
    346 	cp = (char *)(void *)(pmo + 1);
    347 
    348 	if (source) {
    349 		(void)strcpy(cp, source);
    350 		cp += pmo->pmo_source_len;
    351 	}
    352 
    353 	if (target) {
    354 		(void)strcpy(cp, target);
    355 		cp += pmo->pmo_target_len;
    356 	}
    357 
    358 	if (filesystemtype) {
    359 		(void)strcpy(cp, filesystemtype);
    360 		cp += pmo->pmo_filesystemtype_len;
    361 	}
    362 
    363 	if (data) {
    364 		(void)strcpy(cp, data);
    365 		cp += pmo->pmo_data_len;
    366 	}
    367 
    368 	if (sock_len != 0) {
    369 		(void)strcpy(cp, sun->sun_path);
    370 		cp += pmo->pmo_sock_len;
    371 	}
    372 
    373 	if (send(s, frame, len, MSG_NOSIGNAL) != (ssize_t)len) {
    374 #ifdef PERFUSE_DEBUG
    375 		DWARN("%s:%d sendto failed", __func__, __LINE__);
    376 #endif
    377 		return -1;
    378 	}
    379 
    380 	return 0;
    381 }
    382 
    383 
    384 uint64_t
    385 perfuse_next_unique(struct puffs_usermount *pu)
    386 {
    387 	struct perfuse_state *ps;
    388 
    389 	ps = puffs_getspecific(pu);
    390 
    391 	return ps->ps_unique++;
    392 }
    393 
    394 static void
    395 updatelimit(const char *func, int lim, const char *name)
    396 {
    397 	struct rlimit rl;
    398 
    399 	/* Try infinity but that will fail unless we are root */
    400 	rl.rlim_cur = RLIM_INFINITY;
    401 	rl.rlim_max = RLIM_INFINITY;
    402 	if (setrlimit(lim, &rl) != -1)
    403 		return;
    404 
    405 	/* Get and set to the maximum allowed */
    406 	if (getrlimit(lim, &rl) == -1)
    407 		DERR(EX_OSERR, "%s: getrlimit %s failed", func, name);
    408 
    409 	rl.rlim_cur = rl.rlim_max;
    410 	if (setrlimit(lim, &rl) == -1)
    411 		DERR(EX_OSERR, "%s: setrlimit %s to %ju failed", func,
    412 		    name, (uintmax_t)rl.rlim_cur);
    413 }
    414 
    415 struct puffs_usermount *
    416 perfuse_init(struct perfuse_callbacks *pc, struct perfuse_mount_info *pmi)
    417 {
    418 	struct perfuse_state *ps;
    419 	struct puffs_usermount *pu;
    420 	struct puffs_ops *pops;
    421 	const char *source = _PATH_PUFFS;
    422 	char *fstype;
    423 	unsigned int puffs_flags;
    424 	struct puffs_node *pn_root;
    425 	struct puffs_pathobj *po_root;
    426 
    427 	/*
    428 	 * perfused can grow quite large, let assume there's enough ram ...
    429 	 */
    430 	updatelimit(__func__, RLIMIT_DATA, "RLIMIT_DATA");
    431 	updatelimit(__func__, RLIMIT_AS, "RLIMIT_AS");
    432 
    433 	ps = init_state();
    434 	ps->ps_owner_uid = pmi->pmi_uid;
    435 
    436 	if (pmi->pmi_source) {
    437 		if ((ps->ps_source = strdup(pmi->pmi_source)) == NULL)
    438 			DERR(EX_OSERR, "%s: strdup failed", __func__);
    439 
    440 		source = ps->ps_source;
    441 	}
    442 
    443 	if (pmi->pmi_filesystemtype) {
    444 		size_t len;
    445 
    446 		ps->ps_filesystemtype = strdup(pmi->pmi_filesystemtype);
    447 		if (ps->ps_filesystemtype == NULL)
    448 			DERR(EX_OSERR, "%s: strdup failed", __func__);
    449 
    450 		len = sizeof("perfuse|") + strlen(ps->ps_filesystemtype) + 1;
    451 		if ((fstype = malloc(len)) == NULL)
    452 			DERR(EX_OSERR, "%s: malloc failed", __func__);
    453 
    454 		(void)sprintf(fstype, "perfuse|%s", ps->ps_filesystemtype);
    455 	} else {
    456 		if ((fstype = strdup("perfuse")) == NULL)
    457 			DERR(EX_OSERR, "%s: strdup failed", __func__);
    458 	}
    459 
    460 	if ((ps->ps_target = strdup(pmi->pmi_target)) == NULL)
    461 		DERR(EX_OSERR, "%s: strdup failed", __func__);
    462 
    463 	ps->ps_mountflags = pmi->pmi_mountflags;
    464 
    465 	/*
    466 	 * Some options are forbidden for non root users
    467 	 */
    468 	if (ps->ps_owner_uid != 0)
    469 	    ps->ps_mountflags |= MNT_NOSUID|MNT_NODEV;
    470 
    471 	PUFFSOP_INIT(pops);
    472 	PUFFSOP_SET(pops, perfuse, fs, unmount);
    473 	PUFFSOP_SET(pops, perfuse, fs, statvfs);
    474 	PUFFSOP_SET(pops, perfuse, fs, sync);
    475 	PUFFSOP_SET(pops, perfuse, node, lookup);
    476 	PUFFSOP_SET(pops, perfuse, node, create);
    477 	PUFFSOP_SET(pops, perfuse, node, mknod);
    478 	PUFFSOP_SET(pops, perfuse, node, open);
    479 	PUFFSOP_SET(pops, perfuse, node, close);
    480 	PUFFSOP_SET(pops, perfuse, node, access);
    481 	PUFFSOP_SET(pops, perfuse, node, getattr);
    482 	PUFFSOP_SET(pops, perfuse, node, setattr);
    483 	PUFFSOP_SET(pops, perfuse, node, poll);
    484 	PUFFSOP_SET(pops, perfuse, node, fsync);
    485 	PUFFSOP_SET(pops, perfuse, node, remove);
    486 	PUFFSOP_SET(pops, perfuse, node, link);
    487 	PUFFSOP_SET(pops, perfuse, node, rename);
    488 	PUFFSOP_SET(pops, perfuse, node, mkdir);
    489 	PUFFSOP_SET(pops, perfuse, node, rmdir);
    490 	PUFFSOP_SET(pops, perfuse, node, symlink);
    491 	PUFFSOP_SET(pops, perfuse, node, readdir);
    492 	PUFFSOP_SET(pops, perfuse, node, readlink);
    493 	PUFFSOP_SET(pops, perfuse, node, reclaim);
    494 	PUFFSOP_SET(pops, perfuse, node, inactive);
    495 	PUFFSOP_SET(pops, perfuse, node, print);
    496 	PUFFSOP_SET(pops, perfuse, node, pathconf);
    497 	PUFFSOP_SET(pops, perfuse, node, advlock);
    498 	PUFFSOP_SET(pops, perfuse, node, read);
    499 	PUFFSOP_SET(pops, perfuse, node, write);
    500 #ifdef PUFFS_EXTNAMELEN
    501 	PUFFSOP_SET(pops, perfuse, node, getextattr);
    502 	PUFFSOP_SET(pops, perfuse, node, setextattr);
    503 	PUFFSOP_SET(pops, perfuse, node, listextattr);
    504 	PUFFSOP_SET(pops, perfuse, node, deleteextattr);
    505 #endif /* PUFFS_EXTNAMELEN */
    506 #ifdef PUFFS_KFLAG_CACHE_FS_TTL
    507 	PUFFSOP_SET(pops, perfuse, node, getattr_ttl);
    508 	PUFFSOP_SET(pops, perfuse, node, setattr_ttl);
    509 #endif /* PUFFS_KFLAG_CACHE_FS_TTL */
    510 #ifdef PUFFS_SETATTR_FAF
    511 	PUFFSOP_SET(pops, perfuse, node, write2);
    512 #endif /* PUFFS_SETATTR_FAF */
    513 #ifdef PUFFS_OPEN_IO_DIRECT
    514 	PUFFSOP_SET(pops, perfuse, node, open2);
    515 #endif /* PUFFS_OPEN_IO_DIRECT */
    516 #ifdef PUFFS_HAVE_FALLOCATE
    517 	PUFFSOP_SET(pops, perfuse, node, fallocate);
    518 #endif /* PUFFS_HAVE_FALLOCATE */
    519 
    520 	/*
    521 	 * PUFFS_KFLAG_NOCACHE_NAME is required so that we can see changes
    522 	 * done by other machines in networked filesystems. In later
    523 	 * NetBSD releases we use the alternative PUFFS_KFLAG_CACHE_FS_TTL,
    524 	 * which implement name cache with a filesystem-provided TTL.
    525 	 */
    526 #ifdef PUFFS_KFLAG_CACHE_FS_TTL
    527 	puffs_flags = PUFFS_KFLAG_CACHE_FS_TTL;
    528 #else
    529 	puffs_flags = PUFFS_KFLAG_NOCACHE_NAME;
    530 #endif
    531 
    532 	/*
    533 	 * Do not lookuo ..
    534 	 * That means we keep all parent vnode active
    535 	 */
    536 #ifdef PUFFS_KFLAG_CACHE_DOTDOT
    537 	puffs_flags |= PUFFS_KFLAG_CACHE_DOTDOT;
    538 #endif
    539 
    540 	/*
    541 	 * It would be nice to avoid useless inactive, and only
    542 	 * get them on file open for writing (PUFFS does
    543 	 * CLOSE/WRITE/INACTIVE, therefore actual close must be
    544 	 * done at INACTIVE time). Unfortunatley, puffs_setback
    545 	 * crashes when called on OPEN, therefore leave it for
    546 	 * another day.
    547 	 */
    548 #ifdef notyet
    549 	puffs_flags |= PUFFS_FLAG_IAONDEMAND;
    550 #endif
    551 
    552 	/*
    553 	 * FUSE filesystem do not expect [amc]time and size
    554 	 * updates to be sent by the kernel, they do the
    555 	 * updates on their own after other operations.
    556 	 */
    557 #ifdef PUFFS_KFLAG_NOFLUSH_META
    558 	puffs_flags |= PUFFS_KFLAG_NOFLUSH_META;
    559 #endif
    560 
    561 	if (perfuse_diagflags & PDF_PUFFS)
    562 		puffs_flags |= PUFFS_FLAG_OPDUMP;
    563 
    564 	if ((pu = puffs_init(pops, source, fstype, ps, puffs_flags)) == NULL)
    565 		DERR(EX_OSERR, "%s: puffs_init failed", __func__);
    566 
    567 	puffs_setncookiehash(pu, PUFFS_PNODEBUCKETS);
    568 
    569 	ps->ps_pu = pu;
    570 
    571 	/*
    572 	 * Setup filesystem root
    573 	 */
    574 	pn_root = perfuse_new_pn(pu, "", NULL);
    575 	PERFUSE_NODE_DATA(pn_root)->pnd_nodeid = FUSE_ROOT_ID;
    576 	PERFUSE_NODE_DATA(pn_root)->pnd_parent_nodeid = FUSE_ROOT_ID;
    577 	perfuse_node_cache(ps, pn_root);
    578 	puffs_setroot(pu, pn_root);
    579 	ps->ps_fsid = pn_root->pn_va.va_fsid;
    580 
    581 	po_root = puffs_getrootpathobj(pu);
    582 	if ((po_root->po_path = strdup("/")) == NULL)
    583 		DERRX(EX_OSERR, "perfuse_mount_start() failed");
    584 
    585 	po_root->po_len = 1;
    586 	puffs_path_buildhash(pu, po_root);
    587 
    588 	puffs_vattr_null(&pn_root->pn_va);
    589 	pn_root->pn_va.va_type = VDIR;
    590 	pn_root->pn_va.va_mode = 0755;
    591 	pn_root->pn_va.va_fileid = FUSE_ROOT_ID;
    592 
    593 	ps->ps_root = pn_root;
    594 
    595 	/*
    596 	 *  Callbacks
    597 	 */
    598 	ps->ps_new_msg = pc->pc_new_msg;
    599 	ps->ps_xchg_msg = pc->pc_xchg_msg;
    600 	ps->ps_destroy_msg = pc->pc_destroy_msg;
    601 	ps->ps_get_inhdr = pc->pc_get_inhdr;
    602 	ps->ps_get_inpayload = pc->pc_get_inpayload;
    603 	ps->ps_get_outhdr = pc->pc_get_outhdr;
    604 	ps->ps_get_outpayload = pc->pc_get_outpayload;
    605 	ps->ps_umount = pc->pc_umount;
    606 
    607 	pc->pc_fsreq = *perfuse_fsreq;
    608 
    609 	return pu;
    610 }
    611 
    612 void
    613 perfuse_setspecific(struct puffs_usermount *pu, void *priv)
    614 {
    615 	struct perfuse_state *ps;
    616 
    617 	ps = puffs_getspecific(pu);
    618 	ps->ps_private = priv;
    619 
    620 	return;
    621 }
    622 
    623 void *
    624 perfuse_getspecific(struct puffs_usermount *pu)
    625 {
    626 	struct perfuse_state *ps;
    627 
    628 	ps = puffs_getspecific(pu);
    629 
    630 	return ps->ps_private;
    631 }
    632 
    633 int
    634 perfuse_inloop(struct puffs_usermount *pu)
    635 {
    636 	struct perfuse_state *ps;
    637 
    638 	ps = puffs_getspecific(pu);
    639 
    640 	return ps->ps_flags & PS_INLOOP;
    641 }
    642 
    643 int
    644 perfuse_mainloop(struct puffs_usermount *pu)
    645 {
    646 	struct perfuse_state *ps;
    647 
    648 	ps = puffs_getspecific(pu);
    649 
    650 	ps->ps_flags |= PS_INLOOP;
    651 	if (puffs_mainloop(ps->ps_pu) != 0) {
    652 		DERR(EX_OSERR, "%s: failed", __func__);
    653 		return -1;
    654 	}
    655 
    656 	/*
    657 	 * Normal exit after unmount
    658 	 */
    659 	return 0;
    660 }
    661 
    662 /* ARGSUSED0 */
    663 uint64_t
    664 perfuse_get_nodeid(struct puffs_usermount *pu, puffs_cookie_t opc)
    665 {
    666 	return PERFUSE_NODE_DATA(opc)->pnd_nodeid;
    667 }
    668 
    669 int
    670 perfuse_unmount(struct puffs_usermount *pu)
    671 {
    672 	struct perfuse_state *ps;
    673 
    674 	ps = puffs_getspecific(pu);
    675 
    676 	return unmount(ps->ps_target, MNT_FORCE);
    677 }
    678 
    679 void
    680 perfuse_fsreq(struct puffs_usermount *pu, perfuse_msg_t *pm)
    681 {
    682 	struct perfuse_state *ps;
    683 	struct fuse_out_header *foh;
    684 
    685 	ps = puffs_getspecific(pu);
    686 	foh = GET_OUTHDR(ps, pm);
    687 
    688 	/*
    689 	 * There are some operations we may use in a  Fire and Forget wey,
    690 	 * because the kernel does not await a reply, but FUSE still
    691 	 * sends a reply. This happens for fsyc, setattr (for metadata
    692 	 * associated with a fsync) and write (for VOP_PUTPAGES). Ignore
    693 	 * if it was fine, warn or abort otherwise.
    694 	 */
    695 	switch (foh->error) {
    696 	case 0:
    697 		break;
    698 	case -ENOENT:
    699 		/* File disapeared during a FAF operation */
    700 		break;
    701 	case -ENOTCONN: /* FALLTHROUGH */
    702 	case -EAGAIN: /* FALLTHROUGH */
    703 	case -EMSGSIZE:
    704 		DWARN("operation unique = %"PRId64" failed", foh->unique);
    705 		break;
    706 	default:
    707 		DWARNX("Unexpected frame: unique = %"PRId64", error = %d",
    708 		        foh->unique, foh->error);
    709 		/* NOTREACHED */
    710 		break;
    711 	}
    712 
    713 	ps->ps_destroy_msg(pm);
    714 
    715 	return;
    716 }
    717