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