Home | History | Annotate | Line # | Download | only in librefuse
refuse.c revision 1.72
      1 /*	$NetBSD: refuse.c,v 1.72 2007/07/17 11:34:53 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.72 2007/07/17 11:34:53 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(const struct puffs_cid *pcid)
    245 {
    246 	struct fuse_context	*fusectx;
    247 
    248 	fusectx = fuse_get_context();
    249 	puffs_cid_getpid(pcid, &fusectx->pid);
    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 	ret = (*fuse->op.getattr)(path, &st);
    422 
    423 	if (ret == 0) {
    424 		puffs_stat2vattr(va, &st);
    425 	}
    426 
    427 	return -ret;
    428 }
    429 
    430 /* utility function to set various elements of the attribute */
    431 static int
    432 fuse_setattr(struct fuse *fuse, struct puffs_node *pn, const char *path,
    433 	const struct vattr *va)
    434 {
    435 	struct refusenode	*rn = pn->pn_data;
    436 	mode_t			mode;
    437 	uid_t			uid;
    438 	gid_t			gid;
    439 	int			error, ret;
    440 
    441 	error = 0;
    442 
    443 	mode = va->va_mode;
    444 	uid = va->va_uid;
    445 	gid = va->va_gid;
    446 
    447 	if (mode != (mode_t)PUFFS_VNOVAL) {
    448 		ret = 0;
    449 
    450 		if (fuse->op.chmod == NULL) {
    451 			error = -ENOSYS;
    452 		} else {
    453 			ret = fuse->op.chmod(path, mode);
    454 			if (ret)
    455 				error = ret;
    456 		}
    457 	}
    458 	if (uid != (uid_t)PUFFS_VNOVAL || gid != (gid_t)PUFFS_VNOVAL) {
    459 		ret = 0;
    460 
    461 		if (fuse->op.chown == NULL) {
    462 			error = -ENOSYS;
    463 		} else {
    464 			ret = fuse->op.chown(path, uid, gid);
    465 			if (ret)
    466 				error = ret;
    467 		}
    468 	}
    469 	if (va->va_atime.tv_sec != (time_t)PUFFS_VNOVAL
    470 	    || va->va_mtime.tv_sec != (long)PUFFS_VNOVAL) {
    471 		ret = 0;
    472 
    473 		if (fuse->op.utimens) {
    474 			struct timespec tv[2];
    475 
    476 			tv[0].tv_sec = va->va_atime.tv_sec;
    477 			tv[0].tv_nsec = va->va_atime.tv_nsec;
    478 			tv[1].tv_sec = va->va_mtime.tv_sec;
    479 			tv[1].tv_nsec = va->va_mtime.tv_nsec;
    480 
    481 			ret = fuse->op.utimens(path, tv);
    482 		} else if (fuse->op.utime) {
    483 			struct utimbuf timbuf;
    484 
    485 			timbuf.actime = va->va_atime.tv_sec;
    486 			timbuf.modtime = va->va_mtime.tv_sec;
    487 
    488 			ret = fuse->op.utime(path, &timbuf);
    489 		} else {
    490 			error = -ENOSYS;
    491 		}
    492 
    493 		if (ret)
    494 			error = ret;
    495 	}
    496 	if (va->va_size != (u_quad_t)PUFFS_VNOVAL) {
    497 		ret = 0;
    498 
    499 		if (fuse->op.truncate) {
    500 			ret = fuse->op.truncate(path, (off_t)va->va_size);
    501 		} else if (fuse->op.ftruncate) {
    502 			ret = fuse->op.ftruncate(path, (off_t)va->va_size,
    503 			    &rn->file_info);
    504 		} else {
    505 			error = -ENOSYS;
    506 		}
    507 
    508 		if (ret)
    509 			error = ret;
    510 	}
    511 	/* XXX: no reflection with reality */
    512 	puffs_setvattr(&pn->pn_va, va);
    513 
    514 	return -error;
    515 
    516 }
    517 
    518 static int
    519 fuse_newnode(struct puffs_usermount *pu, const char *path,
    520 	const struct vattr *va, struct fuse_file_info *fi,
    521 	struct puffs_newinfo *pni, struct puffs_node **pn_new)
    522 {
    523 	struct puffs_node	*pn;
    524 	struct refusenode	*rn;
    525 	struct vattr		 newva;
    526 	struct fuse		*fuse;
    527 
    528 	fuse = puffs_getspecific(pu);
    529 
    530 	/* fix up nodes */
    531 	pn = newrn(pu);
    532 	if (pn == NULL) {
    533 		if (va->va_type == VDIR) {
    534 			FUSE_ERR_RMDIR(fuse, path);
    535 		} else {
    536 			FUSE_ERR_UNLINK(fuse, path);
    537 		}
    538 		return ENOMEM;
    539 	}
    540 	fuse_setattr(fuse, pn, path, va);
    541 	if (fuse_getattr(fuse, pn, path, &newva) == 0)
    542 		puffs_setvattr(&pn->pn_va, &newva);
    543 
    544 	rn = pn->pn_data;
    545 	if (fi)
    546 		memcpy(&rn->file_info, fi, sizeof(struct fuse_file_info));
    547 
    548 	puffs_newinfo_setcookie(pni, pn);
    549 	if (pn_new)
    550 		*pn_new = pn;
    551 
    552 	return 0;
    553 }
    554 
    555 
    556 /* operation wrappers start here */
    557 
    558 /* lookup the path */
    559 /* ARGSUSED1 */
    560 static int
    561 puffs_fuse_node_lookup(struct puffs_cc *pcc, void *opc,
    562 	struct puffs_newinfo *pni, const struct puffs_cn *pcn)
    563 {
    564 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    565 	struct puffs_node	*pn_res;
    566 	struct stat		 st;
    567 	struct fuse		*fuse;
    568 	const char		*path = PCNPATH(pcn);
    569 	int			 ret;
    570 
    571 	fuse = puffs_getspecific(pu);
    572 
    573 	set_fuse_context_uid_gid(pcn->pcn_cred);
    574 
    575 	ret = fuse->op.getattr(path, &st);
    576 
    577 	if (ret != 0) {
    578 		return -ret;
    579 	}
    580 
    581 	/* XXX: fiXXXme unconst */
    582 	pn_res = puffs_pn_nodewalk(pu, puffs_path_walkcmp,
    583 	    __UNCONST(&pcn->pcn_po_full));
    584 	if (pn_res == NULL) {
    585 		pn_res = newrn(pu);
    586 		if (pn_res == NULL)
    587 			return errno;
    588 		puffs_stat2vattr(&pn_res->pn_va, &st);
    589 	}
    590 
    591 	puffs_newinfo_setcookie(pni, pn_res);
    592 	puffs_newinfo_setvtype(pni, pn_res->pn_va.va_type);
    593 	puffs_newinfo_setsize(pni, (voff_t)pn_res->pn_va.va_size);
    594 	puffs_newinfo_setrdev(pni, pn_res->pn_va.va_rdev);
    595 
    596 	return 0;
    597 }
    598 
    599 /* get attributes for the path name */
    600 /* ARGSUSED3 */
    601 static int
    602 puffs_fuse_node_getattr(struct puffs_cc *pcc, void *opc, struct vattr *va,
    603 	const struct puffs_cred *pcr, const struct puffs_cid *pcid)
    604 {
    605 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    606 	struct puffs_node	*pn = opc;
    607 	struct fuse		*fuse;
    608 	const char		*path = PNPATH(pn);
    609 
    610 	fuse = puffs_getspecific(pu);
    611 
    612 	set_fuse_context_uid_gid(pcr);
    613 	set_fuse_context_pid(pcid);
    614 
    615 	return fuse_getattr(fuse, pn, path, va);
    616 }
    617 
    618 /* read the contents of the symbolic link */
    619 /* ARGSUSED2 */
    620 static int
    621 puffs_fuse_node_readlink(struct puffs_cc *pcc, void *opc,
    622 	const struct puffs_cred *cred, char *linkname, size_t *linklen)
    623 {
    624 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    625 	struct puffs_node	*pn = opc;
    626 	struct fuse		*fuse;
    627 	const char		*path = PNPATH(pn), *p;
    628 	int			ret;
    629 
    630 	fuse = puffs_getspecific(pu);
    631 	if (fuse->op.readlink == NULL) {
    632 		return ENOSYS;
    633 	}
    634 
    635 	set_fuse_context_uid_gid(cred);
    636 
    637 	/* wrap up return code */
    638 	ret = (*fuse->op.readlink)(path, linkname, *linklen);
    639 
    640 	if (ret == 0) {
    641 		p = memchr(linkname, '\0', *linklen);
    642 		if (!p)
    643 			return EINVAL;
    644 
    645 		*linklen = p - linkname;
    646 	}
    647 
    648 	return -ret;
    649 }
    650 
    651 /* make the special node */
    652 /* ARGSUSED1 */
    653 static int
    654 puffs_fuse_node_mknod(struct puffs_cc *pcc, void *opc,
    655 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
    656 	const struct vattr *va)
    657 {
    658 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    659 	struct fuse		*fuse;
    660 	mode_t			 mode;
    661 	const char		*path = PCNPATH(pcn);
    662 	int			ret;
    663 
    664 	fuse = puffs_getspecific(pu);
    665 	if (fuse->op.mknod == NULL) {
    666 		return ENOSYS;
    667 	}
    668 
    669 	set_fuse_context_uid_gid(pcn->pcn_cred);
    670 	set_fuse_context_pid(pcn->pcn_cid);
    671 
    672 	/* wrap up return code */
    673 	mode = puffs_addvtype2mode(va->va_mode, va->va_type);
    674 	ret = (*fuse->op.mknod)(path, mode, va->va_rdev);
    675 
    676 	if (ret == 0) {
    677 		ret = fuse_newnode(pu, path, va, NULL, pni, NULL);
    678 	}
    679 
    680 	return -ret;
    681 }
    682 
    683 /* make a directory */
    684 /* ARGSUSED1 */
    685 static int
    686 puffs_fuse_node_mkdir(struct puffs_cc *pcc, void *opc,
    687 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
    688 	const struct vattr *va)
    689 {
    690 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    691 	struct fuse		*fuse;
    692 	mode_t			 mode = va->va_mode;
    693 	const char		*path = PCNPATH(pcn);
    694 	int			ret;
    695 
    696 	fuse = puffs_getspecific(pu);
    697 
    698 	set_fuse_context_uid_gid(pcn->pcn_cred);
    699 	set_fuse_context_pid(pcn->pcn_cid);
    700 
    701 	if (fuse->op.mkdir == NULL) {
    702 		return ENOSYS;
    703 	}
    704 
    705 	/* wrap up return code */
    706 	ret = (*fuse->op.mkdir)(path, mode);
    707 
    708 	if (ret == 0) {
    709 		ret = fuse_newnode(pu, path, va, NULL, pni, NULL);
    710 	}
    711 
    712 	return -ret;
    713 }
    714 
    715 /*
    716  * create a regular file
    717  *
    718  * since linux/fuse sports using mknod for creating regular files
    719  * instead of having a separate call for it in some versions, if
    720  * we don't have create, just jump to op->mknod.
    721  */
    722 /*ARGSUSED1*/
    723 static int
    724 puffs_fuse_node_create(struct puffs_cc *pcc, void *opc,
    725 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
    726 	const struct vattr *va)
    727 {
    728 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    729 	struct fuse		*fuse;
    730 	struct fuse_file_info	fi;
    731 	struct puffs_node	*pn;
    732 	mode_t			mode = va->va_mode;
    733 	const char		*path = PCNPATH(pcn);
    734 	int			ret, created;
    735 
    736 	fuse = puffs_getspecific(pu);
    737 
    738 	set_fuse_context_uid_gid(pcn->pcn_cred);
    739 	set_fuse_context_pid(pcn->pcn_cid);
    740 
    741 	created = 0;
    742 	if (fuse->op.create) {
    743 		ret = fuse->op.create(path, mode, &fi);
    744 		if (ret == 0)
    745 			created = 1;
    746 
    747 	} else if (fuse->op.mknod) {
    748 		ret = fuse->op.mknod(path, mode | S_IFREG, 0);
    749 
    750 	} else {
    751 		ret = -ENOSYS;
    752 	}
    753 
    754 	if (ret == 0) {
    755 		ret = fuse_newnode(pu, path, va, &fi, pni, &pn);
    756 
    757 		/* sweet..  create also open the file */
    758 		if (created) {
    759 			struct refusenode *rn;
    760 
    761 			rn = pn->pn_data;
    762 			rn->flags |= RN_OPEN;
    763 			rn->opencount++;
    764 		}
    765 	}
    766 
    767 	return -ret;
    768 }
    769 
    770 /* remove the directory entry */
    771 /* ARGSUSED1 */
    772 static int
    773 puffs_fuse_node_remove(struct puffs_cc *pcc, void *opc, void *targ,
    774 	const struct puffs_cn *pcn)
    775 {
    776 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    777 	struct puffs_node	*pn_targ = targ;
    778 	struct fuse		*fuse;
    779 	const char		*path = PNPATH(pn_targ);
    780 	int			ret;
    781 
    782 	fuse = puffs_getspecific(pu);
    783 
    784 	set_fuse_context_uid_gid(pcn->pcn_cred);
    785 	set_fuse_context_pid(pcn->pcn_cid);
    786 
    787 	if (fuse->op.unlink == NULL) {
    788 		return ENOSYS;
    789 	}
    790 
    791 	/* wrap up return code */
    792 	ret = (*fuse->op.unlink)(path);
    793 
    794 	return -ret;
    795 }
    796 
    797 /* remove the directory */
    798 /* ARGSUSED1 */
    799 static int
    800 puffs_fuse_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
    801 	const struct puffs_cn *pcn)
    802 {
    803 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    804 	struct puffs_node	*pn_targ = targ;
    805 	struct fuse		*fuse;
    806 	const char		*path = PNPATH(pn_targ);
    807 	int			ret;
    808 
    809 	fuse = puffs_getspecific(pu);
    810 
    811 	set_fuse_context_uid_gid(pcn->pcn_cred);
    812 	set_fuse_context_pid(pcn->pcn_cid);
    813 
    814 	if (fuse->op.rmdir == NULL) {
    815 		return ENOSYS;
    816 	}
    817 
    818 	/* wrap up return code */
    819 	ret = (*fuse->op.rmdir)(path);
    820 
    821 	return -ret;
    822 }
    823 
    824 /* create a symbolic link */
    825 /* ARGSUSED1 */
    826 static int
    827 puffs_fuse_node_symlink(struct puffs_cc *pcc, void *opc,
    828 	struct puffs_newinfo *pni, const struct puffs_cn *pcn_src,
    829 	const struct vattr *va, const char *link_target)
    830 {
    831 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    832 	struct fuse		*fuse;
    833 	const char		*path = PCNPATH(pcn_src);
    834 	int			ret;
    835 
    836 	fuse = puffs_getspecific(pu);
    837 
    838 	set_fuse_context_uid_gid(pcn_src->pcn_cred);
    839 	set_fuse_context_pid(pcn_src->pcn_cid);
    840 
    841 	if (fuse->op.symlink == NULL) {
    842 		return ENOSYS;
    843 	}
    844 
    845 	/* wrap up return code */
    846 	ret = fuse->op.symlink(link_target, path);
    847 
    848 	if (ret == 0) {
    849 		ret = fuse_newnode(pu, path, va, NULL, pni, NULL);
    850 	}
    851 
    852 	return -ret;
    853 }
    854 
    855 /* rename a directory entry */
    856 /* ARGSUSED1 */
    857 static int
    858 puffs_fuse_node_rename(struct puffs_cc *pcc, void *opc, void *src,
    859 	const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
    860 	const struct puffs_cn *pcn_targ)
    861 {
    862 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    863 	struct fuse		*fuse;
    864 	const char		*path_src = PCNPATH(pcn_src);
    865 	const char		*path_dest = PCNPATH(pcn_targ);
    866 	int			ret;
    867 
    868 	fuse = puffs_getspecific(pu);
    869 
    870 	set_fuse_context_uid_gid(pcn_targ->pcn_cred);
    871 	set_fuse_context_pid(pcn_targ->pcn_cid);
    872 
    873 	if (fuse->op.rename == NULL) {
    874 		return ENOSYS;
    875 	}
    876 
    877 	ret = fuse->op.rename(path_src, path_dest);
    878 
    879 	if (ret == 0) {
    880 	}
    881 
    882 	return -ret;
    883 }
    884 
    885 /* create a link in the file system */
    886 /* ARGSUSED1 */
    887 static int
    888 puffs_fuse_node_link(struct puffs_cc *pcc, void *opc, void *targ,
    889 	const struct puffs_cn *pcn)
    890 {
    891 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    892 	struct puffs_node	*pn = targ;
    893 	struct fuse		*fuse;
    894 	int			ret;
    895 
    896 	fuse = puffs_getspecific(pu);
    897 
    898 	set_fuse_context_uid_gid(pcn->pcn_cred);
    899 	set_fuse_context_pid(pcn->pcn_cid);
    900 
    901 	if (fuse->op.link == NULL) {
    902 		return ENOSYS;
    903 	}
    904 
    905 	/* wrap up return code */
    906 	ret = (*fuse->op.link)(PNPATH(pn), PCNPATH(pcn));
    907 
    908 	return -ret;
    909 }
    910 
    911 /*
    912  * fuse's regular interface provides chmod(), chown(), utimes()
    913  * and truncate() + some variations, so try to fit the square block
    914  * in the circle hole and the circle block .... something like that
    915  */
    916 /* ARGSUSED3 */
    917 static int
    918 puffs_fuse_node_setattr(struct puffs_cc *pcc, void *opc,
    919 	const struct vattr *va, const struct puffs_cred *pcr,
    920 	const struct puffs_cid *pcid)
    921 {
    922 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    923 	struct puffs_node	*pn = opc;
    924 	struct fuse		*fuse;
    925 	const char		*path = PNPATH(pn);
    926 
    927 	fuse = puffs_getspecific(pu);
    928 
    929 	set_fuse_context_uid_gid(pcr);
    930 	set_fuse_context_pid(pcid);
    931 
    932 	return fuse_setattr(fuse, pn, path, va);
    933 }
    934 
    935 /* ARGSUSED2 */
    936 static int
    937 puffs_fuse_node_open(struct puffs_cc *pcc, void *opc, int mode,
    938 	const struct puffs_cred *cred, const struct puffs_cid *pcid)
    939 {
    940 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    941 	struct puffs_node	*pn = opc;
    942 	struct refusenode	*rn = pn->pn_data;
    943 	struct fuse_file_info	*fi = &rn->file_info;
    944 	struct fuse		*fuse;
    945 	const char		*path = PNPATH(pn);
    946 
    947 	fuse = puffs_getspecific(pu);
    948 
    949 	set_fuse_context_uid_gid(cred);
    950 	set_fuse_context_pid(pcid);
    951 
    952 	/* if open, don't open again, lest risk nuking file private info */
    953 	if (rn->flags & RN_OPEN) {
    954 		rn->opencount++;
    955 		return 0;
    956 	}
    957 
    958 	/* OFLAGS(), need to convert FREAD/FWRITE to O_RD/WR */
    959 	fi->flags = (mode & ~(O_CREAT | O_EXCL | O_TRUNC)) - 1;
    960 
    961 	if (pn->pn_va.va_type == VDIR) {
    962 		if (fuse->op.opendir)
    963 			fuse->op.opendir(path, fi);
    964 	} else {
    965 		if (fuse->op.open)
    966 			fuse->op.open(path, fi);
    967 	}
    968 
    969 	rn->flags |= RN_OPEN;
    970 	rn->opencount++;
    971 
    972 	return 0;
    973 }
    974 
    975 /* ARGSUSED2 */
    976 static int
    977 puffs_fuse_node_close(struct puffs_cc *pcc, void *opc, int fflag,
    978 	const struct puffs_cred *pcr, const struct puffs_cid *pcid)
    979 {
    980 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    981 	struct puffs_node	*pn = opc;
    982 	struct refusenode	*rn = pn->pn_data;
    983 	struct fuse		*fuse;
    984 	struct fuse_file_info	*fi;
    985 	const char		*path = PNPATH(pn);
    986 	int			ret;
    987 
    988 	fuse = puffs_getspecific(pu);
    989 	fi = &rn->file_info;
    990 	ret = 0;
    991 
    992 	set_fuse_context_uid_gid(pcr);
    993 	set_fuse_context_pid(pcid);
    994 
    995 	if (rn->flags & RN_OPEN) {
    996 		if (pn->pn_va.va_type == VDIR) {
    997 			if (fuse->op.releasedir)
    998 				ret = fuse->op.releasedir(path, fi);
    999 		} else {
   1000 			if (fuse->op.release)
   1001 				ret = fuse->op.release(path, fi);
   1002 		}
   1003 	}
   1004 	rn->flags &= ~RN_OPEN;
   1005 	rn->opencount--;
   1006 
   1007 	return ret;
   1008 }
   1009 
   1010 /* read some more from the file */
   1011 /* ARGSUSED5 */
   1012 static int
   1013 puffs_fuse_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
   1014 	off_t offset, size_t *resid, const struct puffs_cred *pcr,
   1015 	int ioflag)
   1016 {
   1017 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
   1018 	struct puffs_node	*pn = opc;
   1019 	struct refusenode	*rn = pn->pn_data;
   1020 	struct fuse		*fuse;
   1021 	const char		*path = PNPATH(pn);
   1022 	size_t			maxread;
   1023 	int			ret;
   1024 
   1025 	fuse = puffs_getspecific(pu);
   1026 	if (fuse->op.read == NULL) {
   1027 		return ENOSYS;
   1028 	}
   1029 
   1030 	set_fuse_context_uid_gid(pcr);
   1031 
   1032 	maxread = *resid;
   1033 	if (maxread > pn->pn_va.va_size - offset) {
   1034 		/*LINTED*/
   1035 		maxread = pn->pn_va.va_size - offset;
   1036 	}
   1037 	if (maxread == 0)
   1038 		return 0;
   1039 
   1040 	ret = (*fuse->op.read)(path, (char *)buf, maxread, offset,
   1041 	    &rn->file_info);
   1042 
   1043 	if (ret > 0) {
   1044 		*resid -= ret;
   1045 		ret = 0;
   1046 	}
   1047 
   1048 	return -ret;
   1049 }
   1050 
   1051 /* write to the file */
   1052 /* ARGSUSED0 */
   1053 static int
   1054 puffs_fuse_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
   1055 	off_t offset, size_t *resid, const struct puffs_cred *pcr,
   1056 	int ioflag)
   1057 {
   1058 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
   1059 	struct puffs_node	*pn = opc;
   1060 	struct refusenode	*rn = pn->pn_data;
   1061 	struct fuse		*fuse;
   1062 	const char		*path = PNPATH(pn);
   1063 	int			ret;
   1064 
   1065 	fuse = puffs_getspecific(pu);
   1066 	if (fuse->op.write == NULL) {
   1067 		return ENOSYS;
   1068 	}
   1069 
   1070 	set_fuse_context_uid_gid(pcr);
   1071 
   1072 	if (ioflag & PUFFS_IO_APPEND)
   1073 		offset = pn->pn_va.va_size;
   1074 
   1075 	ret = (*fuse->op.write)(path, (char *)buf, *resid, offset,
   1076 	    &rn->file_info);
   1077 
   1078 	if (ret > 0) {
   1079 		if (offset + ret > pn->pn_va.va_size)
   1080 			pn->pn_va.va_size = offset + ret;
   1081 		*resid -= ret;
   1082 		ret = 0;
   1083 	}
   1084 
   1085 	return -ret;
   1086 }
   1087 
   1088 
   1089 /* ARGSUSED3 */
   1090 static int
   1091 puffs_fuse_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *dent,
   1092 	off_t *readoff, size_t *reslen, const struct puffs_cred *pcr,
   1093 	int *eofflag, off_t *cookies, size_t *ncookies)
   1094 {
   1095 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
   1096 	struct puffs_node	*pn = opc;
   1097 	struct refusenode	*rn = pn->pn_data;
   1098 	struct puffs_fuse_dirh	*dirh;
   1099 	struct fuse		*fuse;
   1100 	struct dirent		*fromdent;
   1101 	const char		*path = PNPATH(pn);
   1102 	int			ret;
   1103 
   1104 	fuse = puffs_getspecific(pu);
   1105 	if (fuse->op.readdir == NULL && fuse->op.getdir == NULL) {
   1106 		return ENOSYS;
   1107 	}
   1108 
   1109 	set_fuse_context_uid_gid(pcr);
   1110 
   1111 	if (pn->pn_va.va_type != VDIR)
   1112 		return ENOTDIR;
   1113 
   1114 	dirh = &rn->dirh;
   1115 
   1116 	/*
   1117 	 * if we are starting from the beginning, slurp entire directory
   1118 	 * into our buffers
   1119 	 */
   1120 	if (*readoff == 0) {
   1121 		/* free old buffers */
   1122 		free(dirh->dbuf);
   1123 		memset(dirh, 0, sizeof(struct puffs_fuse_dirh));
   1124 
   1125 		if (fuse->op.readdir)
   1126 			ret = fuse->op.readdir(path, dirh, puffs_fuse_fill_dir,
   1127 			    0, &rn->file_info);
   1128 		else
   1129 			ret = fuse->op.getdir(path, dirh, puffs_fuse_dirfil);
   1130 		if (ret)
   1131 			return -ret;
   1132 	}
   1133 
   1134 	/* now, stuff results into the kernel buffers */
   1135 	while (*readoff < dirh->bufsize - dirh->reslen) {
   1136 		/*LINTED*/
   1137 		fromdent = (struct dirent *)((uint8_t *)dirh->dbuf + *readoff);
   1138 
   1139 		if (*reslen < _DIRENT_SIZE(fromdent))
   1140 			break;
   1141 
   1142 		memcpy(dent, fromdent, _DIRENT_SIZE(fromdent));
   1143 		*readoff += _DIRENT_SIZE(fromdent);
   1144 		*reslen -= _DIRENT_SIZE(fromdent);
   1145 
   1146 		dent = _DIRENT_NEXT(dent);
   1147 	}
   1148 
   1149 	return 0;
   1150 }
   1151 
   1152 /* ARGSUSED */
   1153 static int
   1154 puffs_fuse_node_reclaim(struct puffs_cc *pcc, void *opc,
   1155 	const struct puffs_cid *pcid)
   1156 {
   1157 	struct puffs_node	*pn = opc;
   1158 
   1159 	nukern(pn);
   1160 
   1161 	set_fuse_context_pid(pcid);
   1162 
   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 	set_fuse_context_pid(pcid);
   1176 	if (fuse->op.destroy == NULL) {
   1177 		return 0;
   1178 	}
   1179 	(*fuse->op.destroy)(fuse);
   1180         return 0;
   1181 }
   1182 
   1183 /* ARGSUSED0 */
   1184 static int
   1185 puffs_fuse_fs_sync(struct puffs_cc *pcc, int flags,
   1186             const struct puffs_cred *cr, const struct puffs_cid *pcid)
   1187 {
   1188 	set_fuse_context_uid_gid(cr);
   1189 	set_fuse_context_pid(pcid);
   1190         return 0;
   1191 }
   1192 
   1193 /* ARGSUSED2 */
   1194 static int
   1195 puffs_fuse_fs_statvfs(struct puffs_cc *pcc, struct statvfs *svfsb,
   1196 	const struct puffs_cid *pcid)
   1197 {
   1198         struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
   1199 	struct fuse		*fuse;
   1200 	int			ret;
   1201 
   1202 	fuse = puffs_getspecific(pu);
   1203 	set_fuse_context_pid(pcid);
   1204 	if (fuse->op.statfs == NULL) {
   1205 		if ((ret = statvfs(PNPATH(puffs_getroot(pu)), svfsb)) == -1) {
   1206 			return errno;
   1207 		}
   1208 	} else {
   1209 		ret = fuse->op.statfs(PNPATH(puffs_getroot(pu)), svfsb);
   1210 	}
   1211 
   1212         return ret;
   1213 }
   1214 
   1215 
   1216 /* End of puffs_fuse operations */
   1217 /* ARGSUSED3 */
   1218 int
   1219 fuse_main_real(int argc, char **argv, const struct fuse_operations *ops,
   1220 	size_t size, void *userdata)
   1221 {
   1222 	struct fuse	*fuse;
   1223 	char		*mountpoint;
   1224 	int		 multithreaded;
   1225 	int		 fd;
   1226 
   1227 	fuse = fuse_setup(argc, argv, ops, size, &mountpoint, &multithreaded,
   1228 			&fd);
   1229 
   1230 	return fuse_loop(fuse);
   1231 }
   1232 
   1233 /*
   1234  * XXX: just defer the operation until fuse_new() when we have more
   1235  * info on our hands.  The real beef is why's this separate in fuse in
   1236  * the first place?
   1237  */
   1238 /* ARGSUSED1 */
   1239 struct fuse_chan *
   1240 fuse_mount(const char *dir, struct fuse_args *args)
   1241 {
   1242  	struct fuse_chan	*fc;
   1243 	char			 name[64];
   1244 
   1245 	if ((fc = calloc(1, sizeof(*fc))) == NULL) {
   1246 		err(EXIT_FAILURE, "fuse_mount");
   1247 	}
   1248 	fc->dead = 0;
   1249 
   1250 	if ((fc->dir = strdup(dir)) == NULL) {
   1251 		err(EXIT_FAILURE, "fuse_mount");
   1252 	}
   1253 
   1254 	/*
   1255 	 * we need to deep copy the args struct - some fuse file
   1256 	 * systems "clean up" the argument vector for "security
   1257 	 * reasons"
   1258 	 */
   1259 	fc->args = fuse_opt_deep_copy_args(args->argc, args->argv);
   1260 
   1261 	if (args->argc > 0) {
   1262 		set_refuse_mount_name(args->argv, name, sizeof(name));
   1263 		if ((args->argv[0] = strdup(name)) == NULL)
   1264 			err(1, "fuse_mount");
   1265 	}
   1266 
   1267 	return fc;
   1268 }
   1269 
   1270 /* ARGSUSED1 */
   1271 struct fuse *
   1272 fuse_new(struct fuse_chan *fc, struct fuse_args *args,
   1273 	const struct fuse_operations *ops, size_t size, void *userdata)
   1274 {
   1275 	struct puffs_usermount	*pu;
   1276 	struct fuse_context	*fusectx;
   1277 	struct puffs_pathobj	*po_root;
   1278 	struct puffs_node	*pn_root;
   1279 	struct puffs_ops	*pops;
   1280 	struct refusenode	*rn_root;
   1281 	struct statvfs		 svfsb;
   1282 	struct stat		 st;
   1283 	struct fuse		*fuse;
   1284 	extern int		 puffs_fakecc;
   1285 	char			 name[64];
   1286 	char			*argv0;
   1287 
   1288 	if ((fuse = calloc(1, sizeof(*fuse))) == NULL) {
   1289 		err(EXIT_FAILURE, "fuse_new");
   1290 	}
   1291 
   1292 	/* copy fuse ops to their own stucture */
   1293 	(void) memcpy(&fuse->op, ops, sizeof(fuse->op));
   1294 
   1295 	fusectx = fuse_get_context();
   1296 	fusectx->fuse = fuse;
   1297 	fusectx->uid = 0;
   1298 	fusectx->gid = 0;
   1299 	fusectx->pid = 0;
   1300 	fusectx->private_data = userdata;
   1301 
   1302 	fuse->fc = fc;
   1303 
   1304 	/* initialise the puffs operations structure */
   1305         PUFFSOP_INIT(pops);
   1306 
   1307         PUFFSOP_SET(pops, puffs_fuse, fs, sync);
   1308         PUFFSOP_SET(pops, puffs_fuse, fs, statvfs);
   1309         PUFFSOP_SET(pops, puffs_fuse, fs, unmount);
   1310 
   1311 	/*
   1312 	 * XXX: all of these don't possibly need to be
   1313 	 * unconditionally set
   1314 	 */
   1315         PUFFSOP_SET(pops, puffs_fuse, node, lookup);
   1316         PUFFSOP_SET(pops, puffs_fuse, node, getattr);
   1317         PUFFSOP_SET(pops, puffs_fuse, node, setattr);
   1318         PUFFSOP_SET(pops, puffs_fuse, node, readdir);
   1319         PUFFSOP_SET(pops, puffs_fuse, node, readlink);
   1320         PUFFSOP_SET(pops, puffs_fuse, node, mknod);
   1321         PUFFSOP_SET(pops, puffs_fuse, node, create);
   1322         PUFFSOP_SET(pops, puffs_fuse, node, remove);
   1323         PUFFSOP_SET(pops, puffs_fuse, node, mkdir);
   1324         PUFFSOP_SET(pops, puffs_fuse, node, rmdir);
   1325         PUFFSOP_SET(pops, puffs_fuse, node, symlink);
   1326         PUFFSOP_SET(pops, puffs_fuse, node, rename);
   1327         PUFFSOP_SET(pops, puffs_fuse, node, link);
   1328         PUFFSOP_SET(pops, puffs_fuse, node, open);
   1329         PUFFSOP_SET(pops, puffs_fuse, node, close);
   1330         PUFFSOP_SET(pops, puffs_fuse, node, read);
   1331         PUFFSOP_SET(pops, puffs_fuse, node, write);
   1332         PUFFSOP_SET(pops, puffs_fuse, node, reclaim);
   1333 
   1334 	argv0 = (*args->argv[0] == 0x0) ? fc->args->argv[0] : args->argv[0];
   1335 	set_refuse_mount_name(&argv0, name, sizeof(name));
   1336 
   1337 	puffs_fakecc = 1; /* XXX */
   1338 	pu = puffs_init(pops, _PATH_PUFFS, name, fuse,
   1339 			 PUFFS_FLAG_BUILDPATH
   1340 			   | PUFFS_FLAG_HASHPATH
   1341 			   | PUFFS_FLAG_OPDUMP
   1342 			   | PUFFS_KFLAG_NOCACHE);
   1343 	if (pu == NULL) {
   1344 		err(EXIT_FAILURE, "puffs_init");
   1345 	}
   1346 	fc->pu = pu;
   1347 
   1348 	pn_root = newrn(pu);
   1349 	puffs_setroot(pu, pn_root);
   1350 	rn_root = pn_root->pn_data;
   1351 	rn_root->flags |= RN_ROOT;
   1352 
   1353 	po_root = puffs_getrootpathobj(pu);
   1354 	if ((po_root->po_path = strdup("/")) == NULL)
   1355 		err(1, "fuse_new");
   1356 	po_root->po_len = 1;
   1357 	puffs_path_buildhash(pu, po_root);
   1358 
   1359 	/* sane defaults */
   1360 	puffs_vattr_null(&pn_root->pn_va);
   1361 	pn_root->pn_va.va_type = VDIR;
   1362 	pn_root->pn_va.va_mode = 0755;
   1363 	if (fuse->op.getattr)
   1364 		if (fuse->op.getattr(po_root->po_path, &st) == 0)
   1365 			puffs_stat2vattr(&pn_root->pn_va, &st);
   1366 	assert(pn_root->pn_va.va_type == VDIR);
   1367 
   1368 	if (fuse->op.init)
   1369 		fusectx->private_data = fuse->op.init(NULL); /* XXX */
   1370 
   1371 	puffs_zerostatvfs(&svfsb);
   1372 	if (puffs_mount(pu, fc->dir, MNT_NODEV | MNT_NOSUID, pn_root) == -1) {
   1373 		err(EXIT_FAILURE, "puffs_mount: directory \"%s\"", fc->dir);
   1374 	}
   1375 
   1376 	return fuse;
   1377 }
   1378 
   1379 int
   1380 fuse_loop(struct fuse *fuse)
   1381 {
   1382 
   1383 	return puffs_mainloop(fuse->fc->pu, PUFFSLOOP_NODAEMON);
   1384 }
   1385 
   1386 void
   1387 fuse_destroy(struct fuse *fuse)
   1388 {
   1389 
   1390 	delete_context_key();
   1391 	/* XXXXXX: missing stuff */
   1392 	free(fuse);
   1393 }
   1394 
   1395 void
   1396 fuse_exit(struct fuse *fuse)
   1397 {
   1398 
   1399 	/* XXX: puffs_exit() is WRONG */
   1400 	if (fuse->fc->dead == 0)
   1401 		puffs_exit(fuse->fc->pu, 1);
   1402 	fuse->fc->dead = 1;
   1403 }
   1404 
   1405 /*
   1406  * XXX: obviously not the most perfect of functions, but needs some
   1407  * puffs tweaking for a better tomorrow
   1408  */
   1409 /*ARGSUSED*/
   1410 void
   1411 fuse_unmount(const char *mp, struct fuse_chan *fc)
   1412 {
   1413 
   1414 	/* XXX: puffs_exit() is WRONG */
   1415 	if (fc->dead == 0)
   1416 		puffs_exit(fc->pu, 1);
   1417 	fc->dead = 1;
   1418 }
   1419 
   1420 /*ARGSUSED*/
   1421 void
   1422 fuse_unmount_compat22(const char *mp)
   1423 {
   1424 
   1425 	return;
   1426 }
   1427 
   1428 /* The next function "exposes" struct fuse to userland.  Not much
   1429 * that we can do about this, as we're conforming to a defined
   1430 * interface.  */
   1431 
   1432 void
   1433 fuse_teardown(struct fuse *fuse, char *mountpoint)
   1434 {
   1435 	fuse_unmount(mountpoint, fuse->fc);
   1436 	fuse_destroy(fuse);
   1437 }
   1438