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