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