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