Home | History | Annotate | Line # | Download | only in librefuse
refuse.c revision 1.93
      1 /*	$NetBSD: refuse.c,v 1.93 2011/07/09 14:50:00 tron 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.93 2011/07/09 14:50:00 tron 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_usermount *pu)
    256 {
    257 	struct puffs_cc		*pcc = puffs_cc_getcc(pu);
    258 	struct fuse_context	*fusectx;
    259 
    260 	fusectx = fuse_get_context();
    261 	puffs_cc_getcaller(pcc, &fusectx->pid, NULL);
    262 }
    263 
    264 /***************** end of pthread context routines ************************/
    265 
    266 #define DIR_CHUNKSIZE 4096
    267 static int
    268 fill_dirbuf(struct puffs_fuse_dirh *dh, const char *name, ino_t dino,
    269 	uint8_t dtype)
    270 {
    271 
    272 	/* initial? */
    273 	if (dh->bufsize == 0) {
    274 		if ((dh->dbuf = calloc(1, DIR_CHUNKSIZE)) == NULL) {
    275 			abort();
    276 		}
    277 		dh->d = dh->dbuf;
    278 		dh->reslen = dh->bufsize = DIR_CHUNKSIZE;
    279 	}
    280 
    281 	if (puffs_nextdent(&dh->d, name, dino, dtype, &dh->reslen)) {
    282 		return 0;
    283 	}
    284 
    285 	/* try to increase buffer space */
    286 	dh->dbuf = realloc(dh->dbuf, dh->bufsize + DIR_CHUNKSIZE);
    287 	if (dh->dbuf == NULL) {
    288 		abort();
    289 	}
    290 	dh->d = (void *)((uint8_t *)dh->dbuf + (dh->bufsize - dh->reslen));
    291 	dh->reslen += DIR_CHUNKSIZE;
    292 	dh->bufsize += DIR_CHUNKSIZE;
    293 
    294 	return !puffs_nextdent(&dh->d, name, dino, dtype, &dh->reslen);
    295 }
    296 
    297 /* ARGSUSED3 */
    298 /* XXX: I have no idea how "off" is supposed to be used */
    299 static int
    300 puffs_fuse_fill_dir(void *buf, const char *name,
    301 	const struct stat *stbuf, off_t off)
    302 {
    303 	struct puffs_fuse_dirh *deh = buf;
    304 	ino_t dino;
    305 	uint8_t dtype;
    306 
    307 	if (stbuf == NULL) {
    308 		dtype = DT_UNKNOWN;
    309 		dino = fakeino++;
    310 	} else {
    311 		dtype = puffs_vtype2dt(puffs_mode2vt(stbuf->st_mode));
    312 		dino = stbuf->st_ino;
    313 
    314 		/*
    315 		 * Some FUSE file systems like to always use 0 as the
    316 		 * inode number.   Our readdir() doesn't like to show
    317 		 * directory entries with inode number 0 ==> workaround.
    318 		 */
    319 		if (dino == 0) {
    320 			dino = fakeino++;
    321 		}
    322 	}
    323 
    324 	return fill_dirbuf(deh, name, dino, dtype);
    325 }
    326 
    327 static int
    328 puffs_fuse_dirfil(fuse_dirh_t h, const char *name, int type, ino_t ino)
    329 {
    330 	ino_t dino;
    331 	int dtype;
    332 
    333 	if ((dtype = type) == 0) {
    334 		dtype = DT_UNKNOWN;
    335 	}
    336 
    337 	dino = (ino) ? ino : fakeino++;
    338 
    339 	return fill_dirbuf(h, name, dino, dtype);
    340 }
    341 
    342 /* place the refuse file system name into `name' */
    343 static void
    344 set_refuse_mount_name(char **argv, char *name, size_t size)
    345 {
    346 	char	*slash;
    347 
    348 	if (argv == NULL || *argv == NULL) {
    349 		(void) strlcpy(name, "refuse", size);
    350 	} else {
    351 		if ((slash = strrchr(*argv, '/')) == NULL) {
    352 			slash = *argv;
    353 		} else {
    354 			slash += 1;
    355 		}
    356 		if (strncmp(*argv, "refuse:", 7) == 0) {
    357 			/* we've already done this */
    358 			(void) strlcpy(name, *argv, size);
    359 		} else {
    360 			(void) snprintf(name, size, "refuse:%s", slash);
    361 		}
    362 	}
    363 }
    364 
    365 
    366 /* this function exposes struct fuse to userland */
    367 static struct fuse *
    368 fuse_setup_real(int argc, char **argv, const struct fuse_operations *ops,
    369 	size_t size, char **mountpoint, int *multithreaded, int *fd,
    370         void *user_data)
    371 {
    372 	struct fuse_chan	*fc;
    373 	struct fuse_args	*args;
    374 	struct fuse		*fuse;
    375 	char			 name[64];
    376 	int			 i;
    377 
    378 	/* set up the proper name */
    379 	set_refuse_mount_name(argv, name, sizeof(name));
    380 
    381 	/* grab the pthread context key */
    382 	if (!create_context_key()) {
    383 		return NULL;
    384 	}
    385 
    386 	/* stuff name into fuse_args */
    387 	args = fuse_opt_deep_copy_args(argc, argv);
    388 	if (args->argc > 0) {
    389 		free(args->argv[0]);
    390 	}
    391 	if ((args->argv[0] = strdup(name)) == NULL) {
    392 		fuse_opt_free_args(args);
    393 		return NULL;
    394 	}
    395 
    396 	/* count back from the end over arguments starting with '-' */
    397 	for (i = argc - 1 ; i > 0 && *argv[i] == '-' ; --i) {
    398 	}
    399 
    400 	fc = fuse_mount(*mountpoint = argv[i], args);
    401 	fuse = fuse_new(fc, args, ops, size, user_data);
    402 
    403 	fuse_opt_free_args(args);
    404 	free(args);
    405 
    406 	/* XXX - wait for puffs to become multi-threaded */
    407 	if (multithreaded) {
    408 		*multithreaded = 0;
    409 	}
    410 
    411 	/* XXX - this is unused */
    412 	if (fd) {
    413 		*fd = 0;
    414 	}
    415 
    416 	return fuse;
    417 }
    418 
    419 #ifdef fuse_setup
    420 #undef fuse_setup
    421 #endif
    422 
    423 struct fuse *
    424 fuse_setup(int argc, char **argv, const struct fuse_operations *ops,
    425 	size_t size, char **mountpoint, int *multithreaded, int *fd)
    426 {
    427     return fuse_setup_real(argc, argv, ops, size, mountpoint,
    428 	multithreaded, fd, NULL);
    429 }
    430 
    431 struct fuse *
    432 fuse_setup26(int argc, char **argv, const struct fuse_operations *ops,
    433 	size_t size, char **mountpoint, int *multithreaded, void *user_data)
    434 {
    435     return fuse_setup_real(argc, argv, ops, size, mountpoint,
    436 	multithreaded, NULL, user_data);
    437 }
    438 
    439 #define FUSE_ERR_UNLINK(fuse, file) if (fuse->op.unlink) fuse->op.unlink(file)
    440 #define FUSE_ERR_RMDIR(fuse, dir) if (fuse->op.rmdir) fuse->op.rmdir(dir)
    441 
    442 /* ARGSUSED1 */
    443 static int
    444 fuse_getattr(struct fuse *fuse, struct puffs_node *pn, const char *path,
    445 	struct vattr *va)
    446 {
    447 	struct stat		 st;
    448 	int			ret;
    449 
    450 	if (fuse->op.getattr == NULL) {
    451 		return ENOSYS;
    452 	}
    453 
    454 	/* wrap up return code */
    455 	memset(&st, 0, sizeof(st));
    456 	ret = (*fuse->op.getattr)(path, &st);
    457 
    458 	if (ret == 0) {
    459 		if (st.st_blksize == 0)
    460 			st.st_blksize = DEV_BSIZE;
    461 		puffs_stat2vattr(va, &st);
    462 	}
    463 
    464 	return -ret;
    465 }
    466 
    467 /* utility function to set various elements of the attribute */
    468 static int
    469 fuse_setattr(struct fuse *fuse, struct puffs_node *pn, const char *path,
    470 	const struct vattr *va)
    471 {
    472 	struct refusenode	*rn = pn->pn_data;
    473 	mode_t			mode;
    474 	uid_t			uid;
    475 	gid_t			gid;
    476 	int			error, ret;
    477 
    478 	error = 0;
    479 
    480 	mode = va->va_mode;
    481 	uid = va->va_uid;
    482 	gid = va->va_gid;
    483 
    484 	if (mode != (mode_t)PUFFS_VNOVAL) {
    485 		ret = 0;
    486 
    487 		if (fuse->op.chmod == NULL) {
    488 			error = -ENOSYS;
    489 		} else {
    490 			ret = fuse->op.chmod(path, mode);
    491 			if (ret)
    492 				error = ret;
    493 		}
    494 	}
    495 	if (uid != (uid_t)PUFFS_VNOVAL || gid != (gid_t)PUFFS_VNOVAL) {
    496 		ret = 0;
    497 
    498 		if (fuse->op.chown == NULL) {
    499 			error = -ENOSYS;
    500 		} else {
    501 			ret = fuse->op.chown(path, uid, gid);
    502 			if (ret)
    503 				error = ret;
    504 		}
    505 	}
    506 	if (va->va_atime.tv_sec != (time_t)PUFFS_VNOVAL
    507 	    || va->va_mtime.tv_sec != (long)PUFFS_VNOVAL) {
    508 		ret = 0;
    509 
    510 		if (fuse->op.utimens) {
    511 			struct timespec tv[2];
    512 
    513 			tv[0].tv_sec = va->va_atime.tv_sec;
    514 			tv[0].tv_nsec = va->va_atime.tv_nsec;
    515 			tv[1].tv_sec = va->va_mtime.tv_sec;
    516 			tv[1].tv_nsec = va->va_mtime.tv_nsec;
    517 
    518 			ret = fuse->op.utimens(path, tv);
    519 		} else if (fuse->op.utime) {
    520 			struct utimbuf timbuf;
    521 
    522 			timbuf.actime = va->va_atime.tv_sec;
    523 			timbuf.modtime = va->va_mtime.tv_sec;
    524 
    525 			ret = fuse->op.utime(path, &timbuf);
    526 		} else {
    527 			error = -ENOSYS;
    528 		}
    529 
    530 		if (ret)
    531 			error = ret;
    532 	}
    533 	if (va->va_size != (u_quad_t)PUFFS_VNOVAL) {
    534 		ret = 0;
    535 
    536 		if (fuse->op.truncate) {
    537 			ret = fuse->op.truncate(path, (off_t)va->va_size);
    538 		} else if (fuse->op.ftruncate) {
    539 			ret = fuse->op.ftruncate(path, (off_t)va->va_size,
    540 			    &rn->file_info);
    541 		} else {
    542 			error = -ENOSYS;
    543 		}
    544 
    545 		if (ret)
    546 			error = ret;
    547 	}
    548 	/* XXX: no reflection with reality */
    549 	puffs_setvattr(&pn->pn_va, va);
    550 
    551 	return -error;
    552 
    553 }
    554 
    555 static int
    556 fuse_newnode(struct puffs_usermount *pu, const char *path,
    557 	const struct vattr *va, struct fuse_file_info *fi,
    558 	struct puffs_newinfo *pni, struct puffs_node **pn_new)
    559 {
    560 	struct puffs_node	*pn;
    561 	struct refusenode	*rn;
    562 	struct vattr		 newva;
    563 	struct fuse		*fuse;
    564 
    565 	fuse = puffs_getspecific(pu);
    566 
    567 	/* fix up nodes */
    568 	pn = newrn(pu);
    569 	if (pn == NULL) {
    570 		if (va->va_type == VDIR) {
    571 			FUSE_ERR_RMDIR(fuse, path);
    572 		} else {
    573 			FUSE_ERR_UNLINK(fuse, path);
    574 		}
    575 		return ENOMEM;
    576 	}
    577 	fuse_setattr(fuse, pn, path, va);
    578 	if (fuse_getattr(fuse, pn, path, &newva) == 0)
    579 		puffs_setvattr(&pn->pn_va, &newva);
    580 
    581 	rn = pn->pn_data;
    582 	if (fi)
    583 		memcpy(&rn->file_info, fi, sizeof(struct fuse_file_info));
    584 
    585 	puffs_newinfo_setcookie(pni, pn);
    586 	if (pn_new)
    587 		*pn_new = pn;
    588 
    589 	return 0;
    590 }
    591 
    592 
    593 /* operation wrappers start here */
    594 
    595 /* lookup the path */
    596 /* ARGSUSED1 */
    597 static int
    598 puffs_fuse_node_lookup(struct puffs_usermount *pu, void *opc,
    599 	struct puffs_newinfo *pni, const struct puffs_cn *pcn)
    600 {
    601 	struct puffs_node	*pn_res;
    602 	struct stat		 st;
    603 	struct fuse		*fuse;
    604 	const char		*path = PCNPATH(pcn);
    605 	int			 ret;
    606 
    607 	fuse = puffs_getspecific(pu);
    608 
    609 	set_fuse_context_uid_gid(pcn->pcn_cred);
    610 
    611 	ret = fuse->op.getattr(path, &st);
    612 
    613 	if (ret != 0) {
    614 		return -ret;
    615 	}
    616 
    617 	/* XXX: fiXXXme unconst */
    618 	pn_res = puffs_pn_nodewalk(pu, puffs_path_walkcmp,
    619 	    __UNCONST(&pcn->pcn_po_full));
    620 	if (pn_res == NULL) {
    621 		pn_res = newrn(pu);
    622 		if (pn_res == NULL)
    623 			return errno;
    624 		puffs_stat2vattr(&pn_res->pn_va, &st);
    625 	}
    626 
    627 	puffs_newinfo_setcookie(pni, pn_res);
    628 	puffs_newinfo_setvtype(pni, pn_res->pn_va.va_type);
    629 	puffs_newinfo_setsize(pni, (voff_t)pn_res->pn_va.va_size);
    630 	puffs_newinfo_setrdev(pni, pn_res->pn_va.va_rdev);
    631 
    632 	return 0;
    633 }
    634 
    635 /* get attributes for the path name */
    636 /* ARGSUSED3 */
    637 static int
    638 puffs_fuse_node_getattr(struct puffs_usermount *pu, void *opc, struct vattr *va,
    639 	const struct puffs_cred *pcr)
    640 {
    641 	struct puffs_node	*pn = opc;
    642 	struct fuse		*fuse;
    643 	const char		*path = PNPATH(pn);
    644 
    645 	fuse = puffs_getspecific(pu);
    646 
    647 	set_fuse_context_uid_gid(pcr);
    648 
    649 	return fuse_getattr(fuse, pn, path, va);
    650 }
    651 
    652 /* read the contents of the symbolic link */
    653 /* ARGSUSED2 */
    654 static int
    655 puffs_fuse_node_readlink(struct puffs_usermount *pu, void *opc,
    656 	const struct puffs_cred *cred, char *linkname, size_t *linklen)
    657 {
    658 	struct puffs_node	*pn = opc;
    659 	struct fuse		*fuse;
    660 	const char		*path = PNPATH(pn), *p;
    661 	int			ret;
    662 
    663 	fuse = puffs_getspecific(pu);
    664 	if (fuse->op.readlink == NULL) {
    665 		return ENOSYS;
    666 	}
    667 
    668 	set_fuse_context_uid_gid(cred);
    669 
    670 	/* wrap up return code */
    671 	ret = (*fuse->op.readlink)(path, linkname, *linklen);
    672 
    673 	if (ret == 0) {
    674 		p = memchr(linkname, '\0', *linklen);
    675 		if (!p)
    676 			return EINVAL;
    677 
    678 		*linklen = p - linkname;
    679 	}
    680 
    681 	return -ret;
    682 }
    683 
    684 /* make the special node */
    685 /* ARGSUSED1 */
    686 static int
    687 puffs_fuse_node_mknod(struct puffs_usermount *pu, void *opc,
    688 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
    689 	const struct vattr *va)
    690 {
    691 	struct fuse		*fuse;
    692 	mode_t			 mode;
    693 	const char		*path = PCNPATH(pcn);
    694 	int			ret;
    695 
    696 	fuse = puffs_getspecific(pu);
    697 	if (fuse->op.mknod == NULL) {
    698 		return ENOSYS;
    699 	}
    700 
    701 	set_fuse_context_uid_gid(pcn->pcn_cred);
    702 
    703 	/* wrap up return code */
    704 	mode = puffs_addvtype2mode(va->va_mode, va->va_type);
    705 	ret = (*fuse->op.mknod)(path, mode, va->va_rdev);
    706 
    707 	if (ret == 0) {
    708 		ret = fuse_newnode(pu, path, va, NULL, pni, NULL);
    709 	}
    710 
    711 	return -ret;
    712 }
    713 
    714 /* make a directory */
    715 /* ARGSUSED1 */
    716 static int
    717 puffs_fuse_node_mkdir(struct puffs_usermount *pu, void *opc,
    718 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
    719 	const struct vattr *va)
    720 {
    721 	struct fuse		*fuse;
    722 	mode_t			 mode = va->va_mode;
    723 	const char		*path = PCNPATH(pcn);
    724 	int			ret;
    725 
    726 	fuse = puffs_getspecific(pu);
    727 
    728 	set_fuse_context_uid_gid(pcn->pcn_cred);
    729 
    730 	if (fuse->op.mkdir == NULL) {
    731 		return ENOSYS;
    732 	}
    733 
    734 	/* wrap up return code */
    735 	ret = (*fuse->op.mkdir)(path, mode);
    736 
    737 	if (ret == 0) {
    738 		ret = fuse_newnode(pu, path, va, NULL, pni, NULL);
    739 	}
    740 
    741 	return -ret;
    742 }
    743 
    744 /*
    745  * create a regular file
    746  *
    747  * since linux/fuse sports using mknod for creating regular files
    748  * instead of having a separate call for it in some versions, if
    749  * we don't have create, just jump to op->mknod.
    750  */
    751 /*ARGSUSED1*/
    752 static int
    753 puffs_fuse_node_create(struct puffs_usermount *pu, void *opc,
    754 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
    755 	const struct vattr *va)
    756 {
    757 	struct fuse		*fuse;
    758 	struct fuse_file_info	fi;
    759 	struct puffs_node	*pn;
    760 	mode_t			mode = va->va_mode;
    761 	const char		*path = PCNPATH(pcn);
    762 	int			ret, created;
    763 
    764 	fuse = puffs_getspecific(pu);
    765 
    766 	set_fuse_context_uid_gid(pcn->pcn_cred);
    767 
    768 	created = 0;
    769 	if (fuse->op.create) {
    770 		ret = fuse->op.create(path, mode | S_IFREG, &fi);
    771 		if (ret == 0)
    772 			created = 1;
    773 
    774 	} else if (fuse->op.mknod) {
    775 		ret = fuse->op.mknod(path, mode | S_IFREG, 0);
    776 
    777 	} else {
    778 		ret = -ENOSYS;
    779 	}
    780 
    781 	if (ret == 0) {
    782 		ret = fuse_newnode(pu, path, va, &fi, pni, &pn);
    783 
    784 		/* sweet..  create also open the file */
    785 		if (created) {
    786 			struct refusenode *rn;
    787 
    788 			rn = pn->pn_data;
    789 			rn->flags |= RN_OPEN;
    790 			rn->opencount++;
    791 		}
    792 	}
    793 
    794 	return -ret;
    795 }
    796 
    797 /* remove the directory entry */
    798 /* ARGSUSED1 */
    799 static int
    800 puffs_fuse_node_remove(struct puffs_usermount *pu, void *opc, void *targ,
    801 	const struct puffs_cn *pcn)
    802 {
    803 	struct puffs_node	*pn_targ = targ;
    804 	struct fuse		*fuse;
    805 	const char		*path = PNPATH(pn_targ);
    806 	int			ret;
    807 
    808 	fuse = puffs_getspecific(pu);
    809 
    810 	set_fuse_context_uid_gid(pcn->pcn_cred);
    811 
    812 	if (fuse->op.unlink == NULL) {
    813 		return ENOSYS;
    814 	}
    815 
    816 	/* wrap up return code */
    817 	ret = (*fuse->op.unlink)(path);
    818 
    819 	return -ret;
    820 }
    821 
    822 /* remove the directory */
    823 /* ARGSUSED1 */
    824 static int
    825 puffs_fuse_node_rmdir(struct puffs_usermount *pu, void *opc, void *targ,
    826 	const struct puffs_cn *pcn)
    827 {
    828 	struct puffs_node	*pn_targ = targ;
    829 	struct fuse		*fuse;
    830 	const char		*path = PNPATH(pn_targ);
    831 	int			ret;
    832 
    833 	fuse = puffs_getspecific(pu);
    834 
    835 	set_fuse_context_uid_gid(pcn->pcn_cred);
    836 
    837 	if (fuse->op.rmdir == NULL) {
    838 		return ENOSYS;
    839 	}
    840 
    841 	/* wrap up return code */
    842 	ret = (*fuse->op.rmdir)(path);
    843 
    844 	return -ret;
    845 }
    846 
    847 /* create a symbolic link */
    848 /* ARGSUSED1 */
    849 static int
    850 puffs_fuse_node_symlink(struct puffs_usermount *pu, void *opc,
    851 	struct puffs_newinfo *pni, const struct puffs_cn *pcn_src,
    852 	const struct vattr *va, const char *link_target)
    853 {
    854 	struct fuse		*fuse;
    855 	const char		*path = PCNPATH(pcn_src);
    856 	int			ret;
    857 
    858 	fuse = puffs_getspecific(pu);
    859 
    860 	set_fuse_context_uid_gid(pcn_src->pcn_cred);
    861 
    862 	if (fuse->op.symlink == NULL) {
    863 		return ENOSYS;
    864 	}
    865 
    866 	/* wrap up return code */
    867 	ret = fuse->op.symlink(link_target, path);
    868 
    869 	if (ret == 0) {
    870 		ret = fuse_newnode(pu, path, va, NULL, pni, NULL);
    871 	}
    872 
    873 	return -ret;
    874 }
    875 
    876 /* rename a directory entry */
    877 /* ARGSUSED1 */
    878 static int
    879 puffs_fuse_node_rename(struct puffs_usermount *pu, void *opc, void *src,
    880 	const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
    881 	const struct puffs_cn *pcn_targ)
    882 {
    883 	struct fuse		*fuse;
    884 	const char		*path_src = PCNPATH(pcn_src);
    885 	const char		*path_dest = PCNPATH(pcn_targ);
    886 	int			ret;
    887 
    888 	fuse = puffs_getspecific(pu);
    889 
    890 	set_fuse_context_uid_gid(pcn_targ->pcn_cred);
    891 
    892 	if (fuse->op.rename == NULL) {
    893 		return ENOSYS;
    894 	}
    895 
    896 	ret = fuse->op.rename(path_src, path_dest);
    897 
    898 	if (ret == 0) {
    899 	}
    900 
    901 	return -ret;
    902 }
    903 
    904 /* create a link in the file system */
    905 /* ARGSUSED1 */
    906 static int
    907 puffs_fuse_node_link(struct puffs_usermount *pu, void *opc, void *targ,
    908 	const struct puffs_cn *pcn)
    909 {
    910 	struct puffs_node	*pn = targ;
    911 	struct fuse		*fuse;
    912 	int			ret;
    913 
    914 	fuse = puffs_getspecific(pu);
    915 
    916 	set_fuse_context_uid_gid(pcn->pcn_cred);
    917 
    918 	if (fuse->op.link == NULL) {
    919 		return ENOSYS;
    920 	}
    921 
    922 	/* wrap up return code */
    923 	ret = (*fuse->op.link)(PNPATH(pn), PCNPATH(pcn));
    924 
    925 	return -ret;
    926 }
    927 
    928 /*
    929  * fuse's regular interface provides chmod(), chown(), utimes()
    930  * and truncate() + some variations, so try to fit the square block
    931  * in the circle hole and the circle block .... something like that
    932  */
    933 /* ARGSUSED3 */
    934 static int
    935 puffs_fuse_node_setattr(struct puffs_usermount *pu, void *opc,
    936 	const struct vattr *va, const struct puffs_cred *pcr)
    937 {
    938 	struct puffs_node	*pn = opc;
    939 	struct fuse		*fuse;
    940 	const char		*path = PNPATH(pn);
    941 
    942 	fuse = puffs_getspecific(pu);
    943 
    944 	set_fuse_context_uid_gid(pcr);
    945 
    946 	return fuse_setattr(fuse, pn, path, va);
    947 }
    948 
    949 /* ARGSUSED2 */
    950 static int
    951 puffs_fuse_node_open(struct puffs_usermount *pu, void *opc, int mode,
    952 	const struct puffs_cred *cred)
    953 {
    954 	struct puffs_node	*pn = opc;
    955 	struct refusenode	*rn = pn->pn_data;
    956 	struct fuse_file_info	*fi = &rn->file_info;
    957 	struct fuse		*fuse;
    958 	const char		*path = PNPATH(pn);
    959 
    960 	fuse = puffs_getspecific(pu);
    961 
    962 	set_fuse_context_uid_gid(cred);
    963 
    964 	/* if open, don't open again, lest risk nuking file private info */
    965 	if (rn->flags & RN_OPEN) {
    966 		rn->opencount++;
    967 		return 0;
    968 	}
    969 
    970 	/* OFLAGS(), need to convert FREAD/FWRITE to O_RD/WR */
    971 	fi->flags = (mode & ~(O_CREAT | O_EXCL | O_TRUNC)) - 1;
    972 
    973 	if (pn->pn_va.va_type == VDIR) {
    974 		if (fuse->op.opendir)
    975 			fuse->op.opendir(path, fi);
    976 	} else {
    977 		if (fuse->op.open)
    978 			fuse->op.open(path, fi);
    979 	}
    980 
    981 	rn->flags |= RN_OPEN;
    982 	rn->opencount++;
    983 
    984 	return 0;
    985 }
    986 
    987 /* ARGSUSED2 */
    988 static int
    989 puffs_fuse_node_close(struct puffs_usermount *pu, void *opc, int fflag,
    990 	const struct puffs_cred *pcr)
    991 {
    992 	struct puffs_node	*pn = opc;
    993 	struct refusenode	*rn = pn->pn_data;
    994 	struct fuse		*fuse;
    995 	struct fuse_file_info	*fi;
    996 	const char		*path = PNPATH(pn);
    997 	int			ret;
    998 
    999 	fuse = puffs_getspecific(pu);
   1000 	fi = &rn->file_info;
   1001 	ret = 0;
   1002 
   1003 	set_fuse_context_uid_gid(pcr);
   1004 
   1005 	if (rn->flags & RN_OPEN) {
   1006 		if (pn->pn_va.va_type == VDIR) {
   1007 			if (fuse->op.releasedir)
   1008 				ret = fuse->op.releasedir(path, fi);
   1009 		} else {
   1010 			if (fuse->op.release)
   1011 				ret = fuse->op.release(path, fi);
   1012 		}
   1013 	}
   1014 	rn->flags &= ~RN_OPEN;
   1015 	rn->opencount--;
   1016 
   1017 	return ret;
   1018 }
   1019 
   1020 /* read some more from the file */
   1021 /* ARGSUSED5 */
   1022 static int
   1023 puffs_fuse_node_read(struct puffs_usermount *pu, void *opc, uint8_t *buf,
   1024 	off_t offset, size_t *resid, const struct puffs_cred *pcr,
   1025 	int ioflag)
   1026 {
   1027 	struct puffs_node	*pn = opc;
   1028 	struct refusenode	*rn = pn->pn_data;
   1029 	struct fuse		*fuse;
   1030 	const char		*path = PNPATH(pn);
   1031 	size_t			maxread;
   1032 	int			ret;
   1033 
   1034 	fuse = puffs_getspecific(pu);
   1035 	if (fuse->op.read == NULL) {
   1036 		return ENOSYS;
   1037 	}
   1038 
   1039 	set_fuse_context_uid_gid(pcr);
   1040 
   1041 	maxread = *resid;
   1042 	if (maxread > pn->pn_va.va_size - offset) {
   1043 		/*LINTED*/
   1044 		maxread = pn->pn_va.va_size - offset;
   1045 	}
   1046 	if (maxread == 0)
   1047 		return 0;
   1048 
   1049 	ret = (*fuse->op.read)(path, (char *)buf, maxread, offset,
   1050 	    &rn->file_info);
   1051 
   1052 	if (ret > 0) {
   1053 		*resid -= ret;
   1054 		ret = 0;
   1055 	}
   1056 
   1057 	return -ret;
   1058 }
   1059 
   1060 /* write to the file */
   1061 /* ARGSUSED0 */
   1062 static int
   1063 puffs_fuse_node_write(struct puffs_usermount *pu, void *opc, uint8_t *buf,
   1064 	off_t offset, size_t *resid, const struct puffs_cred *pcr,
   1065 	int ioflag)
   1066 {
   1067 	struct puffs_node	*pn = opc;
   1068 	struct refusenode	*rn = pn->pn_data;
   1069 	struct fuse		*fuse;
   1070 	const char		*path = PNPATH(pn);
   1071 	int			ret;
   1072 
   1073 	fuse = puffs_getspecific(pu);
   1074 	if (fuse->op.write == NULL) {
   1075 		return ENOSYS;
   1076 	}
   1077 
   1078 	set_fuse_context_uid_gid(pcr);
   1079 
   1080 	if (ioflag & PUFFS_IO_APPEND)
   1081 		offset = pn->pn_va.va_size;
   1082 
   1083 	ret = (*fuse->op.write)(path, (char *)buf, *resid, offset,
   1084 	    &rn->file_info);
   1085 
   1086 	if (ret > 0) {
   1087 		if ((uint64_t)(offset + ret) > pn->pn_va.va_size)
   1088 			pn->pn_va.va_size = offset + ret;
   1089 		*resid -= ret;
   1090 		ret = 0;
   1091 	}
   1092 
   1093 	return -ret;
   1094 }
   1095 
   1096 
   1097 /* ARGSUSED3 */
   1098 static int
   1099 puffs_fuse_node_readdir(struct puffs_usermount *pu, void *opc,
   1100 	struct dirent *dent, off_t *readoff, size_t *reslen,
   1101 	const struct puffs_cred *pcr, int *eofflag,
   1102 	off_t *cookies, size_t *ncookies)
   1103 {
   1104 	struct puffs_node	*pn = opc;
   1105 	struct refusenode	*rn = pn->pn_data;
   1106 	struct puffs_fuse_dirh	*dirh;
   1107 	struct fuse		*fuse;
   1108 	struct dirent		*fromdent;
   1109 	const char		*path = PNPATH(pn);
   1110 	int			ret;
   1111 
   1112 	fuse = puffs_getspecific(pu);
   1113 	if (fuse->op.readdir == NULL && fuse->op.getdir == NULL) {
   1114 		return ENOSYS;
   1115 	}
   1116 
   1117 	set_fuse_context_uid_gid(pcr);
   1118 
   1119 	if (pn->pn_va.va_type != VDIR)
   1120 		return ENOTDIR;
   1121 
   1122 	dirh = &rn->dirh;
   1123 
   1124 	/*
   1125 	 * if we are starting from the beginning, slurp entire directory
   1126 	 * into our buffers
   1127 	 */
   1128 	if (*readoff == 0) {
   1129 		/* free old buffers */
   1130 		free(dirh->dbuf);
   1131 		memset(dirh, 0, sizeof(struct puffs_fuse_dirh));
   1132 
   1133 		if (fuse->op.readdir)
   1134 			ret = fuse->op.readdir(path, dirh, puffs_fuse_fill_dir,
   1135 			    0, &rn->file_info);
   1136 		else
   1137 			ret = fuse->op.getdir(path, dirh, puffs_fuse_dirfil);
   1138 		if (ret)
   1139 			return -ret;
   1140 	}
   1141 
   1142 	/* now, stuff results into the kernel buffers */
   1143 	while (*readoff < (off_t)(dirh->bufsize - dirh->reslen)) {
   1144 		/*LINTED*/
   1145 		fromdent = (struct dirent *)((uint8_t *)dirh->dbuf + *readoff);
   1146 
   1147 		if (*reslen < _DIRENT_SIZE(fromdent))
   1148 			break;
   1149 
   1150 		memcpy(dent, fromdent, _DIRENT_SIZE(fromdent));
   1151 		*readoff += _DIRENT_SIZE(fromdent);
   1152 		*reslen -= _DIRENT_SIZE(fromdent);
   1153 
   1154 		dent = _DIRENT_NEXT(dent);
   1155 	}
   1156 
   1157 	return 0;
   1158 }
   1159 
   1160 /* ARGSUSED */
   1161 static int
   1162 puffs_fuse_node_reclaim(struct puffs_usermount *pu, void *opc)
   1163 {
   1164 	struct puffs_node	*pn = opc;
   1165 
   1166 	nukern(pn);
   1167 	return 0;
   1168 }
   1169 
   1170 /* ARGSUSED1 */
   1171 static int
   1172 puffs_fuse_fs_unmount(struct puffs_usermount *pu, int flags)
   1173 {
   1174 	struct fuse		*fuse;
   1175 
   1176 	fuse = puffs_getspecific(pu);
   1177 	if (fuse->op.destroy == NULL) {
   1178 		return 0;
   1179 	}
   1180 	(*fuse->op.destroy)(fuse);
   1181         return 0;
   1182 }
   1183 
   1184 /* ARGSUSED0 */
   1185 static int
   1186 puffs_fuse_fs_sync(struct puffs_usermount *pu, int flags,
   1187             const struct puffs_cred *cr)
   1188 {
   1189 	set_fuse_context_uid_gid(cr);
   1190         return 0;
   1191 }
   1192 
   1193 /* ARGSUSED2 */
   1194 static int
   1195 puffs_fuse_fs_statvfs(struct puffs_usermount *pu, struct statvfs *svfsb)
   1196 {
   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_real(argc, argv, ops, size, &mountpoint,
   1225 	    &multithreaded, &fd, userdata);
   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 structure */
   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