Home | History | Annotate | Line # | Download | only in librefuse
refuse.c revision 1.77
      1 /*	$NetBSD: refuse.c,v 1.77 2007/10/21 16:46:52 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright  2007 Alistair Crooks.  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  * 3. The name of the author may not be used to endorse or promote
     15  *    products derived from this software without specific prior written
     16  *    permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     24  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 #if !defined(lint)
     33 __RCSID("$NetBSD: refuse.c,v 1.77 2007/10/21 16:46:52 pooka Exp $");
     34 #endif /* !lint */
     35 
     36 #include <assert.h>
     37 #include <err.h>
     38 #include <errno.h>
     39 #include <fuse.h>
     40 #include <paths.h>
     41 #include <pthread.h>
     42 #include <unistd.h>
     43 
     44 #include "defs.h"
     45 
     46 typedef uint64_t	 fuse_ino_t;
     47 
     48 struct fuse_config {
     49 	uid_t		uid;
     50 	gid_t		gid;
     51 	mode_t		umask;
     52 	double		entry_timeout;
     53 	double		negative_timeout;
     54 	double		attr_timeout;
     55 	double		ac_attr_timeout;
     56 	int		ac_attr_timeout_set;
     57 	int		debug;
     58 	int		hard_remove;
     59 	int		use_ino;
     60 	int		readdir_ino;
     61 	int		set_mode;
     62 	int		set_uid;
     63 	int		set_gid;
     64 	int		direct_io;
     65 	int		kernel_cache;
     66 	int		auto_cache;
     67 	int		intr;
     68 	int		intr_signal;
     69 };
     70 
     71 struct fuse_chan {
     72 	const char		*dir;
     73 	struct fuse_args	*args;
     74 	struct puffs_usermount	*pu;
     75 	int 			dead;
     76 };
     77 
     78 /* this is the private fuse structure */
     79 struct fuse {
     80 	struct fuse_chan	*fc;		/* fuse channel pointer */
     81 	struct fuse_operations	op;		/* switch table of operations */
     82 	int			compat;		/* compat level -
     83 						 * not used in puffs_fuse */
     84 	struct node		**name_table;
     85 	size_t			name_table_size;
     86 	struct node		**id_table;
     87 	size_t			id_table_size;
     88 	fuse_ino_t		ctr;
     89 	unsigned int		generation;
     90 	unsigned int		hidectr;
     91 	pthread_mutex_t		lock;
     92 	pthread_rwlock_t	tree_lock;
     93 	void			*user_data;
     94 	struct fuse_config	conf;
     95 	int			intr_installed;
     96 };
     97 
     98 struct puffs_fuse_dirh {
     99 	void *dbuf;
    100 	struct dirent *d;
    101 
    102 	size_t reslen;
    103 	size_t bufsize;
    104 };
    105 
    106 struct refusenode {
    107 	struct fuse_file_info	file_info;
    108 	struct puffs_fuse_dirh	dirh;
    109 	int opencount;
    110 	int flags;
    111 };
    112 #define RN_ROOT		0x01
    113 #define RN_OPEN		0x02	/* XXX: could just use opencount */
    114 
    115 static int fuse_setattr(struct fuse *, struct puffs_node *,
    116 			const char *, const struct vattr *);
    117 
    118 static struct puffs_node *
    119 newrn(struct puffs_usermount *pu)
    120 {
    121 	struct puffs_node *pn;
    122 	struct refusenode *rn;
    123 
    124 	if ((rn = calloc(1, sizeof(*rn))) == NULL) {
    125 		err(EXIT_FAILURE, "newrn");
    126 	}
    127 	pn = puffs_pn_new(pu, rn);
    128 
    129 	return pn;
    130 }
    131 
    132 static void
    133 nukern(struct puffs_node *pn)
    134 {
    135 	struct refusenode *rn = pn->pn_data;
    136 
    137 	free(rn->dirh.dbuf);
    138 	free(rn);
    139 	puffs_pn_put(pn);
    140 }
    141 
    142 /* XXX - not threadsafe */
    143 static ino_t fakeino = 3;
    144 
    145 /***************** start of pthread context routines ************************/
    146 
    147 /*
    148  * Notes on fuse_context:
    149  * we follow fuse's lead and use the pthread specific information to hold
    150  * a reference to the fuse_context structure for this thread.
    151  */
    152 static pthread_mutex_t		context_mutex = PTHREAD_MUTEX_INITIALIZER;
    153 static pthread_key_t		context_key;
    154 static uint64_t			context_refc;
    155 
    156 /* return the fuse_context struct related to this thread */
    157 struct fuse_context *
    158 fuse_get_context(void)
    159 {
    160 	struct fuse_context	*ctxt;
    161 
    162 	if ((ctxt = pthread_getspecific(context_key)) == NULL) {
    163 		if ((ctxt = calloc(1, sizeof(struct fuse_context))) == NULL) {
    164 			errx(EXIT_FAILURE, "fuse_get_context: no memory");
    165 		}
    166 		pthread_setspecific(context_key, ctxt);
    167 	}
    168 	return ctxt;
    169 }
    170 
    171 /* used as a callback function */
    172 static void
    173 free_context(void *ctxt)
    174 {
    175 	free(ctxt);
    176 }
    177 
    178 /* make the pthread key */
    179 static int
    180 create_context_key(void)
    181 {
    182 	if (pthread_mutex_lock(&context_mutex) == 0) {
    183 		/* we have the lock, attempt to create the key */
    184 		if (pthread_key_create(&context_key, free_context) != 0) {
    185 			warnx("create_context_key: pthread_key_create failed");
    186 			pthread_mutex_unlock(&context_mutex);
    187 			return 0;
    188 		}
    189 	}
    190 	context_refc += 1;
    191 	pthread_mutex_unlock(&context_mutex);
    192 	return 1;
    193 }
    194 
    195 /* delete the pthread key */
    196 static void
    197 delete_context_key(void)
    198 {
    199 	pthread_mutex_lock(&context_mutex);
    200 	if (--context_refc == 0) {
    201 		free(pthread_getspecific(context_key));
    202 		pthread_key_delete(context_key);
    203 	}
    204 	pthread_mutex_unlock(&context_mutex);
    205 }
    206 
    207 /* set the uid and gid of the calling process in the current fuse context */
    208 static void
    209 set_fuse_context_uid_gid(const struct puffs_cred *cred)
    210 {
    211 	struct fuse_context	*fusectx;
    212 	uid_t			 uid;
    213 	gid_t			 gid;
    214 
    215 	fusectx = fuse_get_context();
    216 	if (puffs_cred_getuid(cred, &uid) == 0) {
    217 		fusectx->uid = uid;
    218 	}
    219 	if (puffs_cred_getgid(cred, &gid) == 0) {
    220 		fusectx->gid = gid;
    221 	}
    222 }
    223 
    224 /* set the pid of the calling process in the current fuse context */
    225 static void
    226 set_fuse_context_pid(struct puffs_cc *pcc)
    227 {
    228 	struct fuse_context	*fusectx;
    229 
    230 	fusectx = fuse_get_context();
    231 	puffs_cc_getcaller(pcc, &fusectx->pid, NULL);
    232 }
    233 
    234 /***************** end of pthread context routines ************************/
    235 
    236 #define DIR_CHUNKSIZE 4096
    237 static int
    238 fill_dirbuf(struct puffs_fuse_dirh *dh, const char *name, ino_t dino,
    239 	uint8_t dtype)
    240 {
    241 
    242 	/* initial? */
    243 	if (dh->bufsize == 0) {
    244 		if ((dh->dbuf = calloc(1, DIR_CHUNKSIZE)) == NULL) {
    245 			err(EXIT_FAILURE, "fill_dirbuf");
    246 		}
    247 		dh->d = dh->dbuf;
    248 		dh->reslen = dh->bufsize = DIR_CHUNKSIZE;
    249 	}
    250 
    251 	if (puffs_nextdent(&dh->d, name, dino, dtype, &dh->reslen)) {
    252 		return 0;
    253 	}
    254 
    255 	/* try to increase buffer space */
    256 	dh->dbuf = realloc(dh->dbuf, dh->bufsize + DIR_CHUNKSIZE);
    257 	if (dh->dbuf == NULL) {
    258 		err(EXIT_FAILURE, "fill_dirbuf realloc");
    259 	}
    260 	dh->d = (void *)((uint8_t *)dh->dbuf + (dh->bufsize - dh->reslen));
    261 	dh->reslen += DIR_CHUNKSIZE;
    262 	dh->bufsize += DIR_CHUNKSIZE;
    263 
    264 	return !puffs_nextdent(&dh->d, name, dino, dtype, &dh->reslen);
    265 }
    266 
    267 /* ARGSUSED3 */
    268 /* XXX: I have no idea how "off" is supposed to be used */
    269 static int
    270 puffs_fuse_fill_dir(void *buf, const char *name,
    271 	const struct stat *stbuf, off_t off)
    272 {
    273 	struct puffs_fuse_dirh *deh = buf;
    274 	ino_t dino;
    275 	uint8_t dtype;
    276 
    277 	if (stbuf == NULL) {
    278 		dtype = DT_UNKNOWN;
    279 		dino = fakeino++;
    280 	} else {
    281 		dtype = puffs_vtype2dt(puffs_mode2vt(stbuf->st_mode));
    282 		dino = stbuf->st_ino;
    283 
    284 		/*
    285 		 * Some FUSE file systems like to always use 0 as the
    286 		 * inode number.   Our readdir() doesn't like to show
    287 		 * directory entries with inode number 0 ==> workaround.
    288 		 */
    289 		if (dino == 0) {
    290 			dino = fakeino++;
    291 		}
    292 	}
    293 
    294 	return fill_dirbuf(deh, name, dino, dtype);
    295 }
    296 
    297 static int
    298 puffs_fuse_dirfil(fuse_dirh_t h, const char *name, int type, ino_t ino)
    299 {
    300 	ino_t dino;
    301 	int dtype;
    302 
    303 	if ((dtype = type) == 0) {
    304 		dtype = DT_UNKNOWN;
    305 	}
    306 
    307 	dino = (ino) ? ino : fakeino++;
    308 
    309 	return fill_dirbuf(h, name, dino, dtype);
    310 }
    311 
    312 /* place the refuse file system name into `name' */
    313 static void
    314 set_refuse_mount_name(char **argv, char *name, size_t size)
    315 {
    316 	char	*slash;
    317 
    318 	if (argv == NULL || *argv == NULL) {
    319 		(void) strlcpy(name, "refuse", size);
    320 	} else {
    321 		if ((slash = strrchr(*argv, '/')) == NULL) {
    322 			slash = *argv;
    323 		} else {
    324 			slash += 1;
    325 		}
    326 		if (strncmp(*argv, "refuse:", 7) == 0) {
    327 			/* we've already done this */
    328 			(void) strlcpy(name, *argv, size);
    329 		} else {
    330 			(void) snprintf(name, size, "refuse:%s", slash);
    331 		}
    332 	}
    333 }
    334 
    335 
    336 /* this function exposes struct fuse to userland */
    337 struct fuse *
    338 fuse_setup(int argc, char **argv, const struct fuse_operations *ops,
    339 	size_t size, char **mountpoint, int *multithreaded, int *fd)
    340 {
    341 	struct fuse_chan	*fc;
    342 	struct fuse_args	*args;
    343 	struct fuse		*fuse;
    344 	char			 name[64];
    345 	int			 i;
    346 
    347 	/* set up the proper name */
    348 	set_refuse_mount_name(argv, name, sizeof(name));
    349 
    350 	/* grab the pthread context key */
    351 	if (!create_context_key()) {
    352 		err(EXIT_FAILURE, "fuse_setup: can't create context key");
    353 	}
    354 
    355 	/* stuff name into fuse_args */
    356 	args = fuse_opt_deep_copy_args(argc, argv);
    357 	if (args->argc > 0) {
    358 		free(args->argv[0]);
    359 	}
    360 	if ((args->argv[0] = strdup(name)) == NULL) {
    361 		err(EXIT_FAILURE, "fuse_setup: can't strdup memory");
    362 	}
    363 
    364 	/* count back from the end over arguments starting with '-' */
    365 	for (i = argc - 1 ; i > 0 && *argv[i] == '-' ; --i) {
    366 	}
    367 
    368 	fc = fuse_mount(*mountpoint = argv[i], args);
    369 	fuse = fuse_new(fc, args, ops, size, NULL);
    370 
    371 	fuse_opt_free_args(args);
    372 	free(args);
    373 
    374 	/* XXX - wait for puffs to become multi-threaded */
    375 	if (multithreaded) {
    376 		*multithreaded = 0;
    377 	}
    378 
    379 	/* XXX - this is unused */
    380 	if (fd) {
    381 		*fd = 0;
    382 	}
    383 
    384 	return fuse;
    385 }
    386 
    387 #define FUSE_ERR_UNLINK(fuse, file) if (fuse->op.unlink) fuse->op.unlink(file)
    388 #define FUSE_ERR_RMDIR(fuse, dir) if (fuse->op.rmdir) fuse->op.rmdir(dir)
    389 
    390 /* ARGSUSED1 */
    391 static int
    392 fuse_getattr(struct fuse *fuse, struct puffs_node *pn, const char *path,
    393 	struct vattr *va)
    394 {
    395 	struct stat		 st;
    396 	int			ret;
    397 
    398 	if (fuse->op.getattr == NULL) {
    399 		return ENOSYS;
    400 	}
    401 
    402 	/* wrap up return code */
    403 	memset(&st, 0, sizeof(st));
    404 	ret = (*fuse->op.getattr)(path, &st);
    405 
    406 	if (ret == 0) {
    407 		if (st.st_blksize == 0)
    408 			st.st_blksize = DEV_BSIZE;
    409 		puffs_stat2vattr(va, &st);
    410 	}
    411 
    412 	return -ret;
    413 }
    414 
    415 /* utility function to set various elements of the attribute */
    416 static int
    417 fuse_setattr(struct fuse *fuse, struct puffs_node *pn, const char *path,
    418 	const struct vattr *va)
    419 {
    420 	struct refusenode	*rn = pn->pn_data;
    421 	mode_t			mode;
    422 	uid_t			uid;
    423 	gid_t			gid;
    424 	int			error, ret;
    425 
    426 	error = 0;
    427 
    428 	mode = va->va_mode;
    429 	uid = va->va_uid;
    430 	gid = va->va_gid;
    431 
    432 	if (mode != (mode_t)PUFFS_VNOVAL) {
    433 		ret = 0;
    434 
    435 		if (fuse->op.chmod == NULL) {
    436 			error = -ENOSYS;
    437 		} else {
    438 			ret = fuse->op.chmod(path, mode);
    439 			if (ret)
    440 				error = ret;
    441 		}
    442 	}
    443 	if (uid != (uid_t)PUFFS_VNOVAL || gid != (gid_t)PUFFS_VNOVAL) {
    444 		ret = 0;
    445 
    446 		if (fuse->op.chown == NULL) {
    447 			error = -ENOSYS;
    448 		} else {
    449 			ret = fuse->op.chown(path, uid, gid);
    450 			if (ret)
    451 				error = ret;
    452 		}
    453 	}
    454 	if (va->va_atime.tv_sec != (time_t)PUFFS_VNOVAL
    455 	    || va->va_mtime.tv_sec != (long)PUFFS_VNOVAL) {
    456 		ret = 0;
    457 
    458 		if (fuse->op.utimens) {
    459 			struct timespec tv[2];
    460 
    461 			tv[0].tv_sec = va->va_atime.tv_sec;
    462 			tv[0].tv_nsec = va->va_atime.tv_nsec;
    463 			tv[1].tv_sec = va->va_mtime.tv_sec;
    464 			tv[1].tv_nsec = va->va_mtime.tv_nsec;
    465 
    466 			ret = fuse->op.utimens(path, tv);
    467 		} else if (fuse->op.utime) {
    468 			struct utimbuf timbuf;
    469 
    470 			timbuf.actime = va->va_atime.tv_sec;
    471 			timbuf.modtime = va->va_mtime.tv_sec;
    472 
    473 			ret = fuse->op.utime(path, &timbuf);
    474 		} else {
    475 			error = -ENOSYS;
    476 		}
    477 
    478 		if (ret)
    479 			error = ret;
    480 	}
    481 	if (va->va_size != (u_quad_t)PUFFS_VNOVAL) {
    482 		ret = 0;
    483 
    484 		if (fuse->op.truncate) {
    485 			ret = fuse->op.truncate(path, (off_t)va->va_size);
    486 		} else if (fuse->op.ftruncate) {
    487 			ret = fuse->op.ftruncate(path, (off_t)va->va_size,
    488 			    &rn->file_info);
    489 		} else {
    490 			error = -ENOSYS;
    491 		}
    492 
    493 		if (ret)
    494 			error = ret;
    495 	}
    496 	/* XXX: no reflection with reality */
    497 	puffs_setvattr(&pn->pn_va, va);
    498 
    499 	return -error;
    500 
    501 }
    502 
    503 static int
    504 fuse_newnode(struct puffs_usermount *pu, const char *path,
    505 	const struct vattr *va, struct fuse_file_info *fi,
    506 	struct puffs_newinfo *pni, struct puffs_node **pn_new)
    507 {
    508 	struct puffs_node	*pn;
    509 	struct refusenode	*rn;
    510 	struct vattr		 newva;
    511 	struct fuse		*fuse;
    512 
    513 	fuse = puffs_getspecific(pu);
    514 
    515 	/* fix up nodes */
    516 	pn = newrn(pu);
    517 	if (pn == NULL) {
    518 		if (va->va_type == VDIR) {
    519 			FUSE_ERR_RMDIR(fuse, path);
    520 		} else {
    521 			FUSE_ERR_UNLINK(fuse, path);
    522 		}
    523 		return ENOMEM;
    524 	}
    525 	fuse_setattr(fuse, pn, path, va);
    526 	if (fuse_getattr(fuse, pn, path, &newva) == 0)
    527 		puffs_setvattr(&pn->pn_va, &newva);
    528 
    529 	rn = pn->pn_data;
    530 	if (fi)
    531 		memcpy(&rn->file_info, fi, sizeof(struct fuse_file_info));
    532 
    533 	puffs_newinfo_setcookie(pni, pn);
    534 	if (pn_new)
    535 		*pn_new = pn;
    536 
    537 	return 0;
    538 }
    539 
    540 
    541 /* operation wrappers start here */
    542 
    543 /* lookup the path */
    544 /* ARGSUSED1 */
    545 static int
    546 puffs_fuse_node_lookup(struct puffs_cc *pcc, void *opc,
    547 	struct puffs_newinfo *pni, const struct puffs_cn *pcn)
    548 {
    549 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    550 	struct puffs_node	*pn_res;
    551 	struct stat		 st;
    552 	struct fuse		*fuse;
    553 	const char		*path = PCNPATH(pcn);
    554 	int			 ret;
    555 
    556 	fuse = puffs_getspecific(pu);
    557 
    558 	set_fuse_context_uid_gid(pcn->pcn_cred);
    559 	set_fuse_context_pid(pcc);
    560 
    561 	ret = fuse->op.getattr(path, &st);
    562 
    563 	if (ret != 0) {
    564 		return -ret;
    565 	}
    566 
    567 	/* XXX: fiXXXme unconst */
    568 	pn_res = puffs_pn_nodewalk(pu, puffs_path_walkcmp,
    569 	    __UNCONST(&pcn->pcn_po_full));
    570 	if (pn_res == NULL) {
    571 		pn_res = newrn(pu);
    572 		if (pn_res == NULL)
    573 			return errno;
    574 		puffs_stat2vattr(&pn_res->pn_va, &st);
    575 	}
    576 
    577 	puffs_newinfo_setcookie(pni, pn_res);
    578 	puffs_newinfo_setvtype(pni, pn_res->pn_va.va_type);
    579 	puffs_newinfo_setsize(pni, (voff_t)pn_res->pn_va.va_size);
    580 	puffs_newinfo_setrdev(pni, pn_res->pn_va.va_rdev);
    581 
    582 	return 0;
    583 }
    584 
    585 /* get attributes for the path name */
    586 /* ARGSUSED3 */
    587 static int
    588 puffs_fuse_node_getattr(struct puffs_cc *pcc, void *opc, struct vattr *va,
    589 	const struct puffs_cred *pcr, const struct puffs_cid *pcid)
    590 {
    591 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    592 	struct puffs_node	*pn = opc;
    593 	struct fuse		*fuse;
    594 	const char		*path = PNPATH(pn);
    595 
    596 	fuse = puffs_getspecific(pu);
    597 
    598 	set_fuse_context_uid_gid(pcr);
    599 	set_fuse_context_pid(pcc);
    600 
    601 	return fuse_getattr(fuse, pn, path, va);
    602 }
    603 
    604 /* read the contents of the symbolic link */
    605 /* ARGSUSED2 */
    606 static int
    607 puffs_fuse_node_readlink(struct puffs_cc *pcc, void *opc,
    608 	const struct puffs_cred *cred, char *linkname, size_t *linklen)
    609 {
    610 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    611 	struct puffs_node	*pn = opc;
    612 	struct fuse		*fuse;
    613 	const char		*path = PNPATH(pn), *p;
    614 	int			ret;
    615 
    616 	fuse = puffs_getspecific(pu);
    617 	if (fuse->op.readlink == NULL) {
    618 		return ENOSYS;
    619 	}
    620 
    621 	set_fuse_context_uid_gid(cred);
    622 	set_fuse_context_pid(pcc);
    623 
    624 	/* wrap up return code */
    625 	ret = (*fuse->op.readlink)(path, linkname, *linklen);
    626 
    627 	if (ret == 0) {
    628 		p = memchr(linkname, '\0', *linklen);
    629 		if (!p)
    630 			return EINVAL;
    631 
    632 		*linklen = p - linkname;
    633 	}
    634 
    635 	return -ret;
    636 }
    637 
    638 /* make the special node */
    639 /* ARGSUSED1 */
    640 static int
    641 puffs_fuse_node_mknod(struct puffs_cc *pcc, void *opc,
    642 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
    643 	const struct vattr *va)
    644 {
    645 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    646 	struct fuse		*fuse;
    647 	mode_t			 mode;
    648 	const char		*path = PCNPATH(pcn);
    649 	int			ret;
    650 
    651 	fuse = puffs_getspecific(pu);
    652 	if (fuse->op.mknod == NULL) {
    653 		return ENOSYS;
    654 	}
    655 
    656 	set_fuse_context_uid_gid(pcn->pcn_cred);
    657 	set_fuse_context_pid(pcc);
    658 
    659 	/* wrap up return code */
    660 	mode = puffs_addvtype2mode(va->va_mode, va->va_type);
    661 	ret = (*fuse->op.mknod)(path, mode, va->va_rdev);
    662 
    663 	if (ret == 0) {
    664 		ret = fuse_newnode(pu, path, va, NULL, pni, NULL);
    665 	}
    666 
    667 	return -ret;
    668 }
    669 
    670 /* make a directory */
    671 /* ARGSUSED1 */
    672 static int
    673 puffs_fuse_node_mkdir(struct puffs_cc *pcc, void *opc,
    674 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
    675 	const struct vattr *va)
    676 {
    677 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    678 	struct fuse		*fuse;
    679 	mode_t			 mode = va->va_mode;
    680 	const char		*path = PCNPATH(pcn);
    681 	int			ret;
    682 
    683 	fuse = puffs_getspecific(pu);
    684 
    685 	set_fuse_context_uid_gid(pcn->pcn_cred);
    686 	set_fuse_context_pid(pcc);
    687 
    688 	if (fuse->op.mkdir == NULL) {
    689 		return ENOSYS;
    690 	}
    691 
    692 	/* wrap up return code */
    693 	ret = (*fuse->op.mkdir)(path, mode);
    694 
    695 	if (ret == 0) {
    696 		ret = fuse_newnode(pu, path, va, NULL, pni, NULL);
    697 	}
    698 
    699 	return -ret;
    700 }
    701 
    702 /*
    703  * create a regular file
    704  *
    705  * since linux/fuse sports using mknod for creating regular files
    706  * instead of having a separate call for it in some versions, if
    707  * we don't have create, just jump to op->mknod.
    708  */
    709 /*ARGSUSED1*/
    710 static int
    711 puffs_fuse_node_create(struct puffs_cc *pcc, void *opc,
    712 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
    713 	const struct vattr *va)
    714 {
    715 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    716 	struct fuse		*fuse;
    717 	struct fuse_file_info	fi;
    718 	struct puffs_node	*pn;
    719 	mode_t			mode = va->va_mode;
    720 	const char		*path = PCNPATH(pcn);
    721 	int			ret, created;
    722 
    723 	fuse = puffs_getspecific(pu);
    724 
    725 	set_fuse_context_uid_gid(pcn->pcn_cred);
    726 	set_fuse_context_pid(pcc);
    727 
    728 	created = 0;
    729 	if (fuse->op.create) {
    730 		ret = fuse->op.create(path, mode, &fi);
    731 		if (ret == 0)
    732 			created = 1;
    733 
    734 	} else if (fuse->op.mknod) {
    735 		ret = fuse->op.mknod(path, mode | S_IFREG, 0);
    736 
    737 	} else {
    738 		ret = -ENOSYS;
    739 	}
    740 
    741 	if (ret == 0) {
    742 		ret = fuse_newnode(pu, path, va, &fi, pni, &pn);
    743 
    744 		/* sweet..  create also open the file */
    745 		if (created) {
    746 			struct refusenode *rn;
    747 
    748 			rn = pn->pn_data;
    749 			rn->flags |= RN_OPEN;
    750 			rn->opencount++;
    751 		}
    752 	}
    753 
    754 	return -ret;
    755 }
    756 
    757 /* remove the directory entry */
    758 /* ARGSUSED1 */
    759 static int
    760 puffs_fuse_node_remove(struct puffs_cc *pcc, void *opc, void *targ,
    761 	const struct puffs_cn *pcn)
    762 {
    763 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    764 	struct puffs_node	*pn_targ = targ;
    765 	struct fuse		*fuse;
    766 	const char		*path = PNPATH(pn_targ);
    767 	int			ret;
    768 
    769 	fuse = puffs_getspecific(pu);
    770 
    771 	set_fuse_context_uid_gid(pcn->pcn_cred);
    772 	set_fuse_context_pid(pcc);
    773 
    774 	if (fuse->op.unlink == NULL) {
    775 		return ENOSYS;
    776 	}
    777 
    778 	/* wrap up return code */
    779 	ret = (*fuse->op.unlink)(path);
    780 
    781 	return -ret;
    782 }
    783 
    784 /* remove the directory */
    785 /* ARGSUSED1 */
    786 static int
    787 puffs_fuse_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
    788 	const struct puffs_cn *pcn)
    789 {
    790 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    791 	struct puffs_node	*pn_targ = targ;
    792 	struct fuse		*fuse;
    793 	const char		*path = PNPATH(pn_targ);
    794 	int			ret;
    795 
    796 	fuse = puffs_getspecific(pu);
    797 
    798 	set_fuse_context_uid_gid(pcn->pcn_cred);
    799 	set_fuse_context_pid(pcc);
    800 
    801 	if (fuse->op.rmdir == NULL) {
    802 		return ENOSYS;
    803 	}
    804 
    805 	/* wrap up return code */
    806 	ret = (*fuse->op.rmdir)(path);
    807 
    808 	return -ret;
    809 }
    810 
    811 /* create a symbolic link */
    812 /* ARGSUSED1 */
    813 static int
    814 puffs_fuse_node_symlink(struct puffs_cc *pcc, void *opc,
    815 	struct puffs_newinfo *pni, const struct puffs_cn *pcn_src,
    816 	const struct vattr *va, const char *link_target)
    817 {
    818 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    819 	struct fuse		*fuse;
    820 	const char		*path = PCNPATH(pcn_src);
    821 	int			ret;
    822 
    823 	fuse = puffs_getspecific(pu);
    824 
    825 	set_fuse_context_uid_gid(pcn_src->pcn_cred);
    826 	set_fuse_context_pid(pcc);
    827 
    828 	if (fuse->op.symlink == NULL) {
    829 		return ENOSYS;
    830 	}
    831 
    832 	/* wrap up return code */
    833 	ret = fuse->op.symlink(link_target, path);
    834 
    835 	if (ret == 0) {
    836 		ret = fuse_newnode(pu, path, va, NULL, pni, NULL);
    837 	}
    838 
    839 	return -ret;
    840 }
    841 
    842 /* rename a directory entry */
    843 /* ARGSUSED1 */
    844 static int
    845 puffs_fuse_node_rename(struct puffs_cc *pcc, void *opc, void *src,
    846 	const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
    847 	const struct puffs_cn *pcn_targ)
    848 {
    849 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    850 	struct fuse		*fuse;
    851 	const char		*path_src = PCNPATH(pcn_src);
    852 	const char		*path_dest = PCNPATH(pcn_targ);
    853 	int			ret;
    854 
    855 	fuse = puffs_getspecific(pu);
    856 
    857 	set_fuse_context_uid_gid(pcn_targ->pcn_cred);
    858 	set_fuse_context_pid(pcc);
    859 
    860 	if (fuse->op.rename == NULL) {
    861 		return ENOSYS;
    862 	}
    863 
    864 	ret = fuse->op.rename(path_src, path_dest);
    865 
    866 	if (ret == 0) {
    867 	}
    868 
    869 	return -ret;
    870 }
    871 
    872 /* create a link in the file system */
    873 /* ARGSUSED1 */
    874 static int
    875 puffs_fuse_node_link(struct puffs_cc *pcc, void *opc, void *targ,
    876 	const struct puffs_cn *pcn)
    877 {
    878 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    879 	struct puffs_node	*pn = targ;
    880 	struct fuse		*fuse;
    881 	int			ret;
    882 
    883 	fuse = puffs_getspecific(pu);
    884 
    885 	set_fuse_context_uid_gid(pcn->pcn_cred);
    886 	set_fuse_context_pid(pcc);
    887 
    888 	if (fuse->op.link == NULL) {
    889 		return ENOSYS;
    890 	}
    891 
    892 	/* wrap up return code */
    893 	ret = (*fuse->op.link)(PNPATH(pn), PCNPATH(pcn));
    894 
    895 	return -ret;
    896 }
    897 
    898 /*
    899  * fuse's regular interface provides chmod(), chown(), utimes()
    900  * and truncate() + some variations, so try to fit the square block
    901  * in the circle hole and the circle block .... something like that
    902  */
    903 /* ARGSUSED3 */
    904 static int
    905 puffs_fuse_node_setattr(struct puffs_cc *pcc, void *opc,
    906 	const struct vattr *va, const struct puffs_cred *pcr,
    907 	const struct puffs_cid *pcid)
    908 {
    909 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    910 	struct puffs_node	*pn = opc;
    911 	struct fuse		*fuse;
    912 	const char		*path = PNPATH(pn);
    913 
    914 	fuse = puffs_getspecific(pu);
    915 
    916 	set_fuse_context_uid_gid(pcr);
    917 	set_fuse_context_pid(pcc);
    918 
    919 	return fuse_setattr(fuse, pn, path, va);
    920 }
    921 
    922 /* ARGSUSED2 */
    923 static int
    924 puffs_fuse_node_open(struct puffs_cc *pcc, void *opc, int mode,
    925 	const struct puffs_cred *cred, const struct puffs_cid *pcid)
    926 {
    927 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    928 	struct puffs_node	*pn = opc;
    929 	struct refusenode	*rn = pn->pn_data;
    930 	struct fuse_file_info	*fi = &rn->file_info;
    931 	struct fuse		*fuse;
    932 	const char		*path = PNPATH(pn);
    933 
    934 	fuse = puffs_getspecific(pu);
    935 
    936 	set_fuse_context_uid_gid(cred);
    937 	set_fuse_context_pid(pcc);
    938 
    939 	/* if open, don't open again, lest risk nuking file private info */
    940 	if (rn->flags & RN_OPEN) {
    941 		rn->opencount++;
    942 		return 0;
    943 	}
    944 
    945 	/* OFLAGS(), need to convert FREAD/FWRITE to O_RD/WR */
    946 	fi->flags = (mode & ~(O_CREAT | O_EXCL | O_TRUNC)) - 1;
    947 
    948 	if (pn->pn_va.va_type == VDIR) {
    949 		if (fuse->op.opendir)
    950 			fuse->op.opendir(path, fi);
    951 	} else {
    952 		if (fuse->op.open)
    953 			fuse->op.open(path, fi);
    954 	}
    955 
    956 	rn->flags |= RN_OPEN;
    957 	rn->opencount++;
    958 
    959 	return 0;
    960 }
    961 
    962 /* ARGSUSED2 */
    963 static int
    964 puffs_fuse_node_close(struct puffs_cc *pcc, void *opc, int fflag,
    965 	const struct puffs_cred *pcr, const struct puffs_cid *pcid)
    966 {
    967 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    968 	struct puffs_node	*pn = opc;
    969 	struct refusenode	*rn = pn->pn_data;
    970 	struct fuse		*fuse;
    971 	struct fuse_file_info	*fi;
    972 	const char		*path = PNPATH(pn);
    973 	int			ret;
    974 
    975 	fuse = puffs_getspecific(pu);
    976 	fi = &rn->file_info;
    977 	ret = 0;
    978 
    979 	set_fuse_context_uid_gid(pcr);
    980 	set_fuse_context_pid(pcc);
    981 
    982 	if (rn->flags & RN_OPEN) {
    983 		if (pn->pn_va.va_type == VDIR) {
    984 			if (fuse->op.releasedir)
    985 				ret = fuse->op.releasedir(path, fi);
    986 		} else {
    987 			if (fuse->op.release)
    988 				ret = fuse->op.release(path, fi);
    989 		}
    990 	}
    991 	rn->flags &= ~RN_OPEN;
    992 	rn->opencount--;
    993 
    994 	return ret;
    995 }
    996 
    997 /* read some more from the file */
    998 /* ARGSUSED5 */
    999 static int
   1000 puffs_fuse_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
   1001 	off_t offset, size_t *resid, const struct puffs_cred *pcr,
   1002 	int ioflag)
   1003 {
   1004 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
   1005 	struct puffs_node	*pn = opc;
   1006 	struct refusenode	*rn = pn->pn_data;
   1007 	struct fuse		*fuse;
   1008 	const char		*path = PNPATH(pn);
   1009 	size_t			maxread;
   1010 	int			ret;
   1011 
   1012 	fuse = puffs_getspecific(pu);
   1013 	if (fuse->op.read == NULL) {
   1014 		return ENOSYS;
   1015 	}
   1016 
   1017 	set_fuse_context_uid_gid(pcr);
   1018 	set_fuse_context_pid(pcc);
   1019 
   1020 	maxread = *resid;
   1021 	if (maxread > pn->pn_va.va_size - offset) {
   1022 		/*LINTED*/
   1023 		maxread = pn->pn_va.va_size - offset;
   1024 	}
   1025 	if (maxread == 0)
   1026 		return 0;
   1027 
   1028 	ret = (*fuse->op.read)(path, (char *)buf, maxread, offset,
   1029 	    &rn->file_info);
   1030 
   1031 	if (ret > 0) {
   1032 		*resid -= ret;
   1033 		ret = 0;
   1034 	}
   1035 
   1036 	return -ret;
   1037 }
   1038 
   1039 /* write to the file */
   1040 /* ARGSUSED0 */
   1041 static int
   1042 puffs_fuse_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
   1043 	off_t offset, size_t *resid, const struct puffs_cred *pcr,
   1044 	int ioflag)
   1045 {
   1046 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
   1047 	struct puffs_node	*pn = opc;
   1048 	struct refusenode	*rn = pn->pn_data;
   1049 	struct fuse		*fuse;
   1050 	const char		*path = PNPATH(pn);
   1051 	int			ret;
   1052 
   1053 	fuse = puffs_getspecific(pu);
   1054 	if (fuse->op.write == NULL) {
   1055 		return ENOSYS;
   1056 	}
   1057 
   1058 	set_fuse_context_uid_gid(pcr);
   1059 	set_fuse_context_pid(pcc);
   1060 
   1061 	if (ioflag & PUFFS_IO_APPEND)
   1062 		offset = pn->pn_va.va_size;
   1063 
   1064 	ret = (*fuse->op.write)(path, (char *)buf, *resid, offset,
   1065 	    &rn->file_info);
   1066 
   1067 	if (ret > 0) {
   1068 		if (offset + ret > pn->pn_va.va_size)
   1069 			pn->pn_va.va_size = offset + ret;
   1070 		*resid -= ret;
   1071 		ret = 0;
   1072 	}
   1073 
   1074 	return -ret;
   1075 }
   1076 
   1077 
   1078 /* ARGSUSED3 */
   1079 static int
   1080 puffs_fuse_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *dent,
   1081 	off_t *readoff, size_t *reslen, const struct puffs_cred *pcr,
   1082 	int *eofflag, off_t *cookies, size_t *ncookies)
   1083 {
   1084 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
   1085 	struct puffs_node	*pn = opc;
   1086 	struct refusenode	*rn = pn->pn_data;
   1087 	struct puffs_fuse_dirh	*dirh;
   1088 	struct fuse		*fuse;
   1089 	struct dirent		*fromdent;
   1090 	const char		*path = PNPATH(pn);
   1091 	int			ret;
   1092 
   1093 	fuse = puffs_getspecific(pu);
   1094 	if (fuse->op.readdir == NULL && fuse->op.getdir == NULL) {
   1095 		return ENOSYS;
   1096 	}
   1097 
   1098 	set_fuse_context_uid_gid(pcr);
   1099 	set_fuse_context_pid(pcc);
   1100 
   1101 	if (pn->pn_va.va_type != VDIR)
   1102 		return ENOTDIR;
   1103 
   1104 	dirh = &rn->dirh;
   1105 
   1106 	/*
   1107 	 * if we are starting from the beginning, slurp entire directory
   1108 	 * into our buffers
   1109 	 */
   1110 	if (*readoff == 0) {
   1111 		/* free old buffers */
   1112 		free(dirh->dbuf);
   1113 		memset(dirh, 0, sizeof(struct puffs_fuse_dirh));
   1114 
   1115 		if (fuse->op.readdir)
   1116 			ret = fuse->op.readdir(path, dirh, puffs_fuse_fill_dir,
   1117 			    0, &rn->file_info);
   1118 		else
   1119 			ret = fuse->op.getdir(path, dirh, puffs_fuse_dirfil);
   1120 		if (ret)
   1121 			return -ret;
   1122 	}
   1123 
   1124 	/* now, stuff results into the kernel buffers */
   1125 	while (*readoff < dirh->bufsize - dirh->reslen) {
   1126 		/*LINTED*/
   1127 		fromdent = (struct dirent *)((uint8_t *)dirh->dbuf + *readoff);
   1128 
   1129 		if (*reslen < _DIRENT_SIZE(fromdent))
   1130 			break;
   1131 
   1132 		memcpy(dent, fromdent, _DIRENT_SIZE(fromdent));
   1133 		*readoff += _DIRENT_SIZE(fromdent);
   1134 		*reslen -= _DIRENT_SIZE(fromdent);
   1135 
   1136 		dent = _DIRENT_NEXT(dent);
   1137 	}
   1138 
   1139 	return 0;
   1140 }
   1141 
   1142 /* ARGSUSED */
   1143 static int
   1144 puffs_fuse_node_reclaim(struct puffs_cc *pcc, void *opc,
   1145 	const struct puffs_cid *pcid)
   1146 {
   1147 	struct puffs_node	*pn = opc;
   1148 
   1149 	nukern(pn);
   1150 	return 0;
   1151 }
   1152 
   1153 /* ARGSUSED1 */
   1154 static int
   1155 puffs_fuse_fs_unmount(struct puffs_cc *pcc, int flags,
   1156 	const struct puffs_cid *pcid)
   1157 {
   1158         struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
   1159 	struct fuse		*fuse;
   1160 
   1161 	fuse = puffs_getspecific(pu);
   1162 	set_fuse_context_pid(pcc);
   1163 	if (fuse->op.destroy == NULL) {
   1164 		return 0;
   1165 	}
   1166 	(*fuse->op.destroy)(fuse);
   1167         return 0;
   1168 }
   1169 
   1170 /* ARGSUSED0 */
   1171 static int
   1172 puffs_fuse_fs_sync(struct puffs_cc *pcc, int flags,
   1173             const struct puffs_cred *cr, const struct puffs_cid *pcid)
   1174 {
   1175 	set_fuse_context_uid_gid(cr);
   1176 	set_fuse_context_pid(pcc);
   1177         return 0;
   1178 }
   1179 
   1180 /* ARGSUSED2 */
   1181 static int
   1182 puffs_fuse_fs_statvfs(struct puffs_cc *pcc, struct statvfs *svfsb,
   1183 	const struct puffs_cid *pcid)
   1184 {
   1185         struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
   1186 	struct fuse		*fuse;
   1187 	int			ret;
   1188 
   1189 	fuse = puffs_getspecific(pu);
   1190 	set_fuse_context_pid(pcc);
   1191 	if (fuse->op.statfs == NULL) {
   1192 		if ((ret = statvfs(PNPATH(puffs_getroot(pu)), svfsb)) == -1) {
   1193 			return errno;
   1194 		}
   1195 	} else {
   1196 		ret = fuse->op.statfs(PNPATH(puffs_getroot(pu)), svfsb);
   1197 	}
   1198 
   1199         return ret;
   1200 }
   1201 
   1202 
   1203 /* End of puffs_fuse operations */
   1204 /* ARGSUSED3 */
   1205 int
   1206 fuse_main_real(int argc, char **argv, const struct fuse_operations *ops,
   1207 	size_t size, void *userdata)
   1208 {
   1209 	struct fuse	*fuse;
   1210 	char		*mountpoint;
   1211 	int		 multithreaded;
   1212 	int		 fd;
   1213 
   1214 	fuse = fuse_setup(argc, argv, ops, size, &mountpoint, &multithreaded,
   1215 			&fd);
   1216 
   1217 	return fuse_loop(fuse);
   1218 }
   1219 
   1220 /*
   1221  * XXX: just defer the operation until fuse_new() when we have more
   1222  * info on our hands.  The real beef is why's this separate in fuse in
   1223  * the first place?
   1224  */
   1225 /* ARGSUSED1 */
   1226 struct fuse_chan *
   1227 fuse_mount(const char *dir, struct fuse_args *args)
   1228 {
   1229  	struct fuse_chan	*fc;
   1230 	char			 name[64];
   1231 
   1232 	if ((fc = calloc(1, sizeof(*fc))) == NULL) {
   1233 		err(EXIT_FAILURE, "fuse_mount");
   1234 	}
   1235 	fc->dead = 0;
   1236 
   1237 	if ((fc->dir = strdup(dir)) == NULL) {
   1238 		err(EXIT_FAILURE, "fuse_mount");
   1239 	}
   1240 
   1241 	/*
   1242 	 * we need to deep copy the args struct - some fuse file
   1243 	 * systems "clean up" the argument vector for "security
   1244 	 * reasons"
   1245 	 */
   1246 	fc->args = fuse_opt_deep_copy_args(args->argc, args->argv);
   1247 
   1248 	if (args->argc > 0) {
   1249 		set_refuse_mount_name(args->argv, name, sizeof(name));
   1250 		if ((args->argv[0] = strdup(name)) == NULL)
   1251 			err(1, "fuse_mount");
   1252 	}
   1253 
   1254 	return fc;
   1255 }
   1256 
   1257 /* ARGSUSED1 */
   1258 struct fuse *
   1259 fuse_new(struct fuse_chan *fc, struct fuse_args *args,
   1260 	const struct fuse_operations *ops, size_t size, void *userdata)
   1261 {
   1262 	struct puffs_usermount	*pu;
   1263 	struct fuse_context	*fusectx;
   1264 	struct puffs_pathobj	*po_root;
   1265 	struct puffs_node	*pn_root;
   1266 	struct puffs_ops	*pops;
   1267 	struct refusenode	*rn_root;
   1268 	struct statvfs		 svfsb;
   1269 	struct stat		 st;
   1270 	struct fuse		*fuse;
   1271 	extern int		 puffs_fakecc;
   1272 	char			 name[64];
   1273 	char			*argv0;
   1274 
   1275 	if ((fuse = calloc(1, sizeof(*fuse))) == NULL) {
   1276 		err(EXIT_FAILURE, "fuse_new");
   1277 	}
   1278 
   1279 	/* copy fuse ops to their own stucture */
   1280 	(void) memcpy(&fuse->op, ops, sizeof(fuse->op));
   1281 
   1282 	fusectx = fuse_get_context();
   1283 	fusectx->fuse = fuse;
   1284 	fusectx->uid = 0;
   1285 	fusectx->gid = 0;
   1286 	fusectx->pid = 0;
   1287 	fusectx->private_data = userdata;
   1288 
   1289 	fuse->fc = fc;
   1290 
   1291 	/* initialise the puffs operations structure */
   1292         PUFFSOP_INIT(pops);
   1293 
   1294         PUFFSOP_SET(pops, puffs_fuse, fs, sync);
   1295         PUFFSOP_SET(pops, puffs_fuse, fs, statvfs);
   1296         PUFFSOP_SET(pops, puffs_fuse, fs, unmount);
   1297 
   1298 	/*
   1299 	 * XXX: all of these don't possibly need to be
   1300 	 * unconditionally set
   1301 	 */
   1302         PUFFSOP_SET(pops, puffs_fuse, node, lookup);
   1303         PUFFSOP_SET(pops, puffs_fuse, node, getattr);
   1304         PUFFSOP_SET(pops, puffs_fuse, node, setattr);
   1305         PUFFSOP_SET(pops, puffs_fuse, node, readdir);
   1306         PUFFSOP_SET(pops, puffs_fuse, node, readlink);
   1307         PUFFSOP_SET(pops, puffs_fuse, node, mknod);
   1308         PUFFSOP_SET(pops, puffs_fuse, node, create);
   1309         PUFFSOP_SET(pops, puffs_fuse, node, remove);
   1310         PUFFSOP_SET(pops, puffs_fuse, node, mkdir);
   1311         PUFFSOP_SET(pops, puffs_fuse, node, rmdir);
   1312         PUFFSOP_SET(pops, puffs_fuse, node, symlink);
   1313         PUFFSOP_SET(pops, puffs_fuse, node, rename);
   1314         PUFFSOP_SET(pops, puffs_fuse, node, link);
   1315         PUFFSOP_SET(pops, puffs_fuse, node, open);
   1316         PUFFSOP_SET(pops, puffs_fuse, node, close);
   1317         PUFFSOP_SET(pops, puffs_fuse, node, read);
   1318         PUFFSOP_SET(pops, puffs_fuse, node, write);
   1319         PUFFSOP_SET(pops, puffs_fuse, node, reclaim);
   1320 
   1321 	argv0 = (*args->argv[0] == 0x0) ? fc->args->argv[0] : args->argv[0];
   1322 	set_refuse_mount_name(&argv0, name, sizeof(name));
   1323 
   1324 	puffs_fakecc = 1; /* XXX */
   1325 	pu = puffs_init(pops, _PATH_PUFFS, name, fuse,
   1326 			 PUFFS_FLAG_BUILDPATH
   1327 			   | PUFFS_FLAG_HASHPATH
   1328 			   | PUFFS_KFLAG_NOCACHE);
   1329 	if (pu == NULL) {
   1330 		err(EXIT_FAILURE, "puffs_init");
   1331 	}
   1332 	fc->pu = pu;
   1333 
   1334 	pn_root = newrn(pu);
   1335 	puffs_setroot(pu, pn_root);
   1336 	rn_root = pn_root->pn_data;
   1337 	rn_root->flags |= RN_ROOT;
   1338 
   1339 	po_root = puffs_getrootpathobj(pu);
   1340 	if ((po_root->po_path = strdup("/")) == NULL)
   1341 		err(1, "fuse_new");
   1342 	po_root->po_len = 1;
   1343 	puffs_path_buildhash(pu, po_root);
   1344 
   1345 	/* sane defaults */
   1346 	puffs_vattr_null(&pn_root->pn_va);
   1347 	pn_root->pn_va.va_type = VDIR;
   1348 	pn_root->pn_va.va_mode = 0755;
   1349 	if (fuse->op.getattr)
   1350 		if (fuse->op.getattr(po_root->po_path, &st) == 0)
   1351 			puffs_stat2vattr(&pn_root->pn_va, &st);
   1352 	assert(pn_root->pn_va.va_type == VDIR);
   1353 
   1354 	if (fuse->op.init)
   1355 		fusectx->private_data = fuse->op.init(NULL); /* XXX */
   1356 
   1357 	puffs_zerostatvfs(&svfsb);
   1358 	if (puffs_mount(pu, fc->dir, MNT_NODEV | MNT_NOSUID, pn_root) == -1) {
   1359 		err(EXIT_FAILURE, "puffs_mount: directory \"%s\"", fc->dir);
   1360 	}
   1361 
   1362 	return fuse;
   1363 }
   1364 
   1365 int
   1366 fuse_loop(struct fuse *fuse)
   1367 {
   1368 
   1369 	return puffs_mainloop(fuse->fc->pu, 0);
   1370 }
   1371 
   1372 void
   1373 fuse_destroy(struct fuse *fuse)
   1374 {
   1375 
   1376 	delete_context_key();
   1377 	/* XXXXXX: missing stuff */
   1378 	free(fuse);
   1379 }
   1380 
   1381 void
   1382 fuse_exit(struct fuse *fuse)
   1383 {
   1384 
   1385 	/* XXX: puffs_exit() is WRONG */
   1386 	if (fuse->fc->dead == 0)
   1387 		puffs_exit(fuse->fc->pu, 1);
   1388 	fuse->fc->dead = 1;
   1389 }
   1390 
   1391 /*
   1392  * XXX: obviously not the most perfect of functions, but needs some
   1393  * puffs tweaking for a better tomorrow
   1394  */
   1395 /*ARGSUSED*/
   1396 void
   1397 fuse_unmount(const char *mp, struct fuse_chan *fc)
   1398 {
   1399 
   1400 	/* XXX: puffs_exit() is WRONG */
   1401 	if (fc->dead == 0)
   1402 		puffs_exit(fc->pu, 1);
   1403 	fc->dead = 1;
   1404 }
   1405 
   1406 /*ARGSUSED*/
   1407 void
   1408 fuse_unmount_compat22(const char *mp)
   1409 {
   1410 
   1411 	return;
   1412 }
   1413 
   1414 /* The next function "exposes" struct fuse to userland.  Not much
   1415 * that we can do about this, as we're conforming to a defined
   1416 * interface.  */
   1417 
   1418 void
   1419 fuse_teardown(struct fuse *fuse, char *mountpoint)
   1420 {
   1421 	fuse_unmount(mountpoint, fuse->fc);
   1422 	fuse_destroy(fuse);
   1423 }
   1424