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