Home | History | Annotate | Line # | Download | only in librefuse
refuse.c revision 1.29
      1 /*	$NetBSD: refuse.c,v 1.29 2007/02/19 23:12:29 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.29 2007/02/19 23:12:29 pooka Exp $");
     34 #endif /* !lint */
     35 
     36 #include <assert.h>
     37 #include <err.h>
     38 #include <errno.h>
     39 #include <fuse.h>
     40 #include <unistd.h>
     41 
     42 #include "defs.h"
     43 
     44 typedef uint64_t	 fuse_ino_t;
     45 
     46 struct fuse_config {
     47 	uid_t		uid;
     48 	gid_t		gid;
     49 	mode_t		umask;
     50 	double		entry_timeout;
     51 	double		negative_timeout;
     52 	double		attr_timeout;
     53 	double		ac_attr_timeout;
     54 	int		ac_attr_timeout_set;
     55 	int		debug;
     56 	int		hard_remove;
     57 	int		use_ino;
     58 	int		readdir_ino;
     59 	int		set_mode;
     60 	int		set_uid;
     61 	int		set_gid;
     62 	int		direct_io;
     63 	int		kernel_cache;
     64 	int		auto_cache;
     65 	int		intr;
     66 	int		intr_signal;
     67 };
     68 
     69 /* this is the private fuse structure */
     70 struct fuse {
     71 	struct fuse_session	*se;		/* fuse session pointer */
     72 	struct fuse_operations	op;		/* switch table of operations */
     73 	int			compat;		/* compat level -
     74 						 * not used in puffs_fuse */
     75 	struct node		**name_table;
     76 	size_t			name_table_size;
     77 	struct node		**id_table;
     78 	size_t			id_table_size;
     79 	fuse_ino_t		ctr;
     80 	unsigned int		generation;
     81 	unsigned int		hidectr;
     82 	pthread_mutex_t		lock;
     83 	pthread_rwlock_t	tree_lock;
     84 	void			*user_data;
     85 	struct fuse_config	conf;
     86 	int			intr_installed;
     87 	struct puffs_usermount	*pu;
     88 };
     89 
     90 struct refusenode {
     91 	struct fuse_file_info	 file_info;
     92 	int flags;
     93 };
     94 #define RN_OPEN		0x01
     95 
     96 static int fuse_setattr(struct fuse *, struct puffs_node *,
     97 			const char *, const struct vattr *);
     98 
     99 static struct puffs_node *
    100 newrn(struct puffs_usermount *pu)
    101 {
    102 	struct puffs_node *pn;
    103 	struct refusenode *rn;
    104 
    105 	rn = malloc(sizeof(struct refusenode));
    106 	if (!rn)
    107 		abort(); /*XXX*/
    108 
    109 	memset(rn, 0, sizeof(struct refusenode));
    110 	pn = puffs_pn_new(pu, rn);
    111 
    112 	return pn;
    113 }
    114 
    115 static void
    116 nukern(struct puffs_node *pn)
    117 {
    118 
    119 	free(pn->pn_data);
    120 	puffs_pn_put(pn);
    121 }
    122 
    123 static ino_t fakeino = 3;
    124 
    125 /*
    126  * XXX: do this otherwise if/when we grow thread support
    127  *
    128  * XXX2: does not consistently supply uid, gid or pid currently
    129  */
    130 static struct fuse_context fcon;
    131 
    132 
    133 /* XXX: rethinkme */
    134 struct fuse_dirh {
    135 	struct dirent *dent;
    136 	size_t reslen;
    137 	off_t readoff;
    138 };
    139 
    140 /* ARGSUSED2 */
    141 static int
    142 puffs_fuse_fill_dir(void *buf, const char *name,
    143 	const struct stat *stbuf, off_t off)
    144 {
    145 	struct fuse_dirh *deh = buf;
    146 	ino_t dino;
    147 	uint8_t dtype;
    148 
    149 	if (stbuf == NULL) {
    150 		dtype = DT_UNKNOWN;
    151 		dino = fakeino++;
    152 	} else {
    153 		dtype = puffs_vtype2dt(puffs_mode2vt(stbuf->st_mode));
    154 		dino = stbuf->st_ino;
    155 	}
    156 
    157 	return !puffs_nextdent(&deh->dent, name, dino, dtype, &deh->reslen);
    158 }
    159 
    160 static int
    161 puffs_fuse_dirfil(fuse_dirh_t h, const char *name, int type, ino_t ino)
    162 {
    163 	ino_t dino;
    164 	int dtype;
    165 
    166 	if (type == 0)
    167 		dtype = DT_UNKNOWN;
    168 	else
    169 		dtype = type;
    170 
    171 	if (ino)
    172 		dino = ino;
    173 	else
    174 		dino = fakeino++;
    175 
    176 	return !puffs_nextdent(&h->dent, name, dino, dtype, &h->reslen);
    177 }
    178 
    179 int
    180 fuse_opt_add_arg(struct fuse_args *args, const char *arg)
    181 {
    182 	char	**oldargv;
    183 	int	oldargc;
    184 
    185 	if (args->allocated) {
    186 		RENEW(char *, args->argv, args->argc + 1,
    187 		    "fuse_opt_add_arg1", return 0);
    188 	} else {
    189 		oldargv = args->argv;
    190 		oldargc = args->argc;
    191 		NEWARRAY(char *, args->argv, oldargc + 1,
    192 		    "fuse_opt_add_arg2", return 0);
    193 		(void) memcpy(args->argv, oldargv, oldargc * sizeof(char *));
    194 		args->allocated = 1;
    195 	}
    196 	args->argv[args->argc++] = strdup(arg);
    197 	return 1;
    198 }
    199 
    200 void
    201 fuse_opt_free_args(struct fuse_args *args)
    202 {
    203 	if (args && args->argv) {
    204 		int i;
    205 		for (i = 0; i < args->argc; i++)
    206 			FREE(args->argv[i]);
    207 		FREE(args->argv);
    208 	}
    209 }
    210 
    211 #define FUSE_ERR_UNLINK(fuse, file) if (fuse->op.unlink) fuse->op.unlink(file)
    212 #define FUSE_ERR_RMDIR(fuse, dir) if (fuse->op.rmdir) fuse->op.rmdir(dir)
    213 
    214 /* ARGSUSED1 */
    215 static int
    216 fuse_getattr(struct fuse *fuse, struct puffs_node *pn, const char *path,
    217 	struct vattr *va)
    218 {
    219 	struct stat		 st;
    220 	int			ret;
    221 
    222 	if (fuse->op.getattr == NULL) {
    223 		return ENOSYS;
    224 	}
    225 
    226 	/* wrap up return code */
    227 	ret = (*fuse->op.getattr)(path, &st);
    228 
    229 	if (ret == 0) {
    230 		puffs_stat2vattr(va, &st);
    231 	}
    232 
    233 	return -ret;
    234 }
    235 
    236 static int
    237 fuse_setattr(struct fuse *fuse, struct puffs_node *pn, const char *path,
    238 	const struct vattr *va)
    239 {
    240 	struct refusenode	*rn = pn->pn_data;
    241 	mode_t			mode;
    242 	uid_t			uid;
    243 	gid_t			gid;
    244 	int			error, ret;
    245 
    246 	error = 0;
    247 
    248 	mode = va->va_mode;
    249 	uid = va->va_uid;
    250 	gid = va->va_gid;
    251 
    252 	if (mode != (mode_t)PUFFS_VNOVAL) {
    253 		ret = 0;
    254 
    255 		if (fuse->op.chmod == NULL) {
    256 			error = -ENOSYS;
    257 		} else {
    258 			ret = fuse->op.chmod(path, mode);
    259 			if (ret)
    260 				error = ret;
    261 		}
    262 	}
    263 	if (uid != (uid_t)PUFFS_VNOVAL || gid != (gid_t)PUFFS_VNOVAL) {
    264 		ret = 0;
    265 
    266 		if (fuse->op.chown == NULL) {
    267 			error = -ENOSYS;
    268 		} else {
    269 			ret = fuse->op.chown(path, uid, gid);
    270 			if (ret)
    271 				error = ret;
    272 		}
    273 	}
    274 	if (va->va_atime.tv_sec != (time_t)PUFFS_VNOVAL
    275 	    || va->va_mtime.tv_sec != (long)PUFFS_VNOVAL) {
    276 		ret = 0;
    277 
    278 		if (fuse->op.utimens) {
    279 			struct timespec tv[2];
    280 
    281 			tv[0].tv_sec = va->va_atime.tv_sec;
    282 			tv[0].tv_nsec = va->va_atime.tv_nsec;
    283 			tv[1].tv_sec = va->va_mtime.tv_sec;
    284 			tv[1].tv_nsec = va->va_mtime.tv_nsec;
    285 
    286 			ret = fuse->op.utimens(path, tv);
    287 		} else if (fuse->op.utime) {
    288 			struct utimbuf timbuf;
    289 
    290 			timbuf.actime = va->va_atime.tv_sec;
    291 			timbuf.modtime = va->va_mtime.tv_sec;
    292 
    293 			ret = fuse->op.utime(path, &timbuf);
    294 		} else {
    295 			error = -ENOSYS;
    296 		}
    297 
    298 		if (ret)
    299 			error = ret;
    300 	}
    301 	if (va->va_size != (u_quad_t)PUFFS_VNOVAL) {
    302 		ret = 0;
    303 
    304 		if (fuse->op.truncate) {
    305 			ret = fuse->op.truncate(path, (off_t)va->va_size);
    306 		} else if (fuse->op.ftruncate) {
    307 			ret = fuse->op.ftruncate(path, (off_t)va->va_size,
    308 			    &rn->file_info);
    309 		} else {
    310 			error = -ENOSYS;
    311 		}
    312 
    313 		if (ret)
    314 			error = ret;
    315 	}
    316 	/* XXX: no reflection with reality */
    317 	puffs_setvattr(&pn->pn_va, va);
    318 
    319 	return -error;
    320 
    321 }
    322 
    323 static int
    324 fuse_newnode(struct puffs_usermount *pu, const char *path,
    325 	const struct vattr *va, struct fuse_file_info *fi, void **newnode)
    326 {
    327 	struct vattr		newva;
    328 	struct fuse		*fuse;
    329 	struct puffs_node	*pn;
    330 	struct refusenode	*rn;
    331 
    332 	fuse = (struct fuse *)pu->pu_privdata;
    333 
    334 	/* fix up nodes */
    335 	pn = newrn(pu);
    336 	if (pn == NULL) {
    337 		if (va->va_type == VDIR) {
    338 			FUSE_ERR_RMDIR(fuse, path);
    339 		} else {
    340 			FUSE_ERR_UNLINK(fuse, path);
    341 		}
    342 		return ENOMEM;
    343 	}
    344 	fuse_setattr(fuse, pn, path, va);
    345 	if (fuse_getattr(fuse, pn, path, &newva) == 0)
    346 		puffs_setvattr(&pn->pn_va, &newva);
    347 
    348 	rn = pn->pn_data;
    349 	if (fi)
    350 		memcpy(&rn->file_info, fi, sizeof(struct fuse_file_info));
    351 
    352 	*newnode = pn;
    353 
    354 	return 0;
    355 }
    356 
    357 
    358 /* operation wrappers start here */
    359 
    360 /* lookup the path */
    361 /* ARGSUSED1 */
    362 static int
    363 puffs_fuse_node_lookup(struct puffs_cc *pcc, void *opc, void **newnode,
    364 	enum vtype *newtype, voff_t *newsize, dev_t *newrdev,
    365 	const struct puffs_cn *pcn)
    366 {
    367 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    368 	struct puffs_node	*pn_res;
    369 	struct stat		st;
    370 	struct fuse		*fuse;
    371 	const char		*path = PCNPATH(pcn);
    372 	int			ret;
    373 
    374 	fuse = (struct fuse *)pu->pu_privdata;
    375 	ret = fuse->op.getattr(path, &st);
    376 
    377 	if (ret != 0) {
    378 		return -ret;
    379 	}
    380 
    381 	/* XXX: fiXXXme unconst */
    382 	pn_res = puffs_pn_nodewalk(pu, puffs_path_walkcmp,
    383 	    __UNCONST(&pcn->pcn_po_full));
    384 	if (pn_res == NULL) {
    385 		pn_res = newrn(pu);
    386 		if (pn_res == NULL)
    387 			return errno;
    388 		puffs_stat2vattr(&pn_res->pn_va, &st);
    389 	}
    390 
    391 	*newnode = pn_res;
    392 	*newtype = pn_res->pn_va.va_type;
    393 	*newsize = pn_res->pn_va.va_size;
    394 	*newrdev = pn_res->pn_va.va_rdev;
    395 
    396 	return 0;
    397 }
    398 
    399 /* get attributes for the path name */
    400 /* ARGSUSED3 */
    401 static int
    402 puffs_fuse_node_getattr(struct puffs_cc *pcc, void *opc, struct vattr *va,
    403 	const struct puffs_cred *pcr, pid_t pid)
    404 {
    405 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    406 	struct puffs_node	*pn = opc;
    407 	struct fuse		*fuse;
    408 	const char		*path = PNPATH(pn);
    409 
    410 	fuse = (struct fuse *)pu->pu_privdata;
    411 	return fuse_getattr(fuse, pn, path, va);
    412 }
    413 
    414 /* read the contents of the symbolic link */
    415 /* ARGSUSED2 */
    416 static int
    417 puffs_fuse_node_readlink(struct puffs_cc *pcc, void *opc,
    418 	const struct puffs_cred *cred, char *linkname, size_t *linklen)
    419 {
    420 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    421 	struct puffs_node	*pn = opc;
    422 	struct fuse		*fuse;
    423 	const char		*path = PNPATH(pn), *p;
    424 	int			ret;
    425 
    426 	fuse = (struct fuse *)pu->pu_privdata;
    427 	if (fuse->op.readlink == NULL) {
    428 		return ENOSYS;
    429 	}
    430 
    431 	/* wrap up return code */
    432 	ret = (*fuse->op.readlink)(path, linkname, *linklen);
    433 
    434 	if (ret == 0) {
    435 		p = memchr(linkname, '\0', *linklen);
    436 		if (!p)
    437 			return EINVAL;
    438 
    439 		*linklen = p - linkname;
    440 	}
    441 
    442 	return -ret;
    443 }
    444 
    445 /* make the special node */
    446 /* ARGSUSED1 */
    447 static int
    448 puffs_fuse_node_mknod(struct puffs_cc *pcc, void *opc, void **newnode,
    449 	const struct puffs_cn *pcn, const struct vattr *va)
    450 {
    451 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    452 	struct fuse		*fuse;
    453 	mode_t			 mode = va->va_mode;
    454 	const char		*path = PCNPATH(pcn);
    455 	int			ret;
    456 
    457 	fuse = (struct fuse *)pu->pu_privdata;
    458 	if (fuse->op.mknod == NULL) {
    459 		return ENOSYS;
    460 	}
    461 
    462 	/* wrap up return code */
    463 	ret = (*fuse->op.mknod)(path, mode, va->va_rdev);
    464 
    465 	if (ret == 0) {
    466 		ret = fuse_newnode(pu, path, va, NULL, newnode);
    467 	}
    468 
    469 	return -ret;
    470 }
    471 
    472 /* make a directory */
    473 /* ARGSUSED1 */
    474 static int
    475 puffs_fuse_node_mkdir(struct puffs_cc *pcc, void *opc, void **newnode,
    476 	const struct puffs_cn *pcn, const struct vattr *va)
    477 {
    478 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    479 	struct fuse		*fuse;
    480 	mode_t			 mode = va->va_mode;
    481 	const char		*path = PCNPATH(pcn);
    482 	int			ret;
    483 
    484 	fuse = (struct fuse *)pu->pu_privdata;
    485 	if (fuse->op.mkdir == NULL) {
    486 		return ENOSYS;
    487 	}
    488 
    489 	/* wrap up return code */
    490 	ret = (*fuse->op.mkdir)(path, mode);
    491 
    492 	if (ret == 0) {
    493 		ret = fuse_newnode(pu, path, va, NULL, newnode);
    494 	}
    495 
    496 	return -ret;
    497 }
    498 
    499 /*
    500  * create a regular file
    501  *
    502  * since linux/fuse sports using mknod for creating regular files
    503  * instead of having a separate call for it in some versions, if
    504  * we don't have create, just jump to op->mknod.
    505  */
    506 /*ARGSUSED1*/
    507 static int
    508 puffs_fuse_node_create(struct puffs_cc *pcc, void *opc, void **newnode,
    509 	const struct puffs_cn *pcn, const struct vattr *va)
    510 {
    511 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    512 	struct fuse		*fuse;
    513 	struct fuse_file_info	fi;
    514 	mode_t			mode = va->va_mode;
    515 	const char		*path = PCNPATH(pcn);
    516 	int			ret;
    517 
    518 	fuse = (struct fuse *)pu->pu_privdata;
    519 
    520 	if (fuse->op.create) {
    521 		ret = fuse->op.create(path, mode, &fi);
    522 
    523 	} else if (fuse->op.mknod) {
    524 		fcon.uid = va->va_uid; /*XXX*/
    525 		fcon.gid = va->va_gid; /*XXX*/
    526 
    527 		ret = fuse->op.mknod(path, mode | S_IFREG, 0);
    528 
    529 	} else {
    530 		ret = -ENOSYS;
    531 	}
    532 
    533 	if (ret == 0) {
    534 		ret = fuse_newnode(pu, path, va, &fi, newnode);
    535 	}
    536 
    537 	return -ret;
    538 }
    539 
    540 /* remove the directory entry */
    541 /* ARGSUSED1 */
    542 static int
    543 puffs_fuse_node_remove(struct puffs_cc *pcc, void *opc, void *targ,
    544 	const struct puffs_cn *pcn)
    545 {
    546 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    547 	struct puffs_node	*pn_targ = targ;
    548 	struct fuse		*fuse;
    549 	const char		*path = PNPATH(pn_targ);
    550 	int			ret;
    551 
    552 	fuse = (struct fuse *)pu->pu_privdata;
    553 	if (fuse->op.unlink == NULL) {
    554 		return ENOSYS;
    555 	}
    556 
    557 	/* wrap up return code */
    558 	ret = (*fuse->op.unlink)(path);
    559 
    560 	return -ret;
    561 }
    562 
    563 /* remove the directory */
    564 /* ARGSUSED1 */
    565 static int
    566 puffs_fuse_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
    567 	const struct puffs_cn *pcn)
    568 {
    569 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    570 	struct puffs_node	*pn_targ = targ;
    571 	struct fuse		*fuse;
    572 	const char		*path = PNPATH(pn_targ);
    573 	int			ret;
    574 
    575 	fuse = (struct fuse *)pu->pu_privdata;
    576 	if (fuse->op.rmdir == NULL) {
    577 		return ENOSYS;
    578 	}
    579 
    580 	/* wrap up return code */
    581 	ret = (*fuse->op.rmdir)(path);
    582 
    583 	return -ret;
    584 }
    585 
    586 /* create a symbolic link */
    587 /* ARGSUSED1 */
    588 static int
    589 puffs_fuse_node_symlink(struct puffs_cc *pcc, void *opc, void **newnode,
    590 	const struct puffs_cn *pcn_src, const struct vattr *va,
    591 	const char *link_target)
    592 {
    593 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    594 	struct fuse		*fuse;
    595 	const char		*path = PCNPATH(pcn_src);
    596 	int			ret;
    597 
    598 	fuse = (struct fuse *)pu->pu_privdata;
    599 	if (fuse->op.symlink == NULL) {
    600 		return ENOSYS;
    601 	}
    602 
    603 	/* wrap up return code */
    604 	ret = (*fuse->op.symlink)(path, link_target);
    605 	/* XXX - check I haven't transposed these args */
    606 
    607 	if (ret == 0) {
    608 		ret = fuse_newnode(pu, path, va, NULL, newnode);
    609 	}
    610 
    611 	return -ret;
    612 }
    613 
    614 /* rename a directory entry */
    615 /* ARGSUSED1 */
    616 static int
    617 puffs_fuse_node_rename(struct puffs_cc *pcc, void *opc, void *src,
    618 	const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
    619 	const struct puffs_cn *pcn_targ)
    620 {
    621 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    622 	struct fuse		*fuse;
    623 	const char		*path_src = PCNPATH(pcn_src);
    624 	const char		*path_dest = PCNPATH(pcn_targ);
    625 	int			ret;
    626 
    627 	fuse = (struct fuse *)pu->pu_privdata;
    628 	if (fuse->op.rename == NULL) {
    629 		return ENOSYS;
    630 	}
    631 
    632 	ret = fuse->op.rename(path_src, path_dest);
    633 
    634 	if (ret == 0) {
    635 	}
    636 
    637 	return -ret;
    638 }
    639 
    640 /* create a link in the file system */
    641 /* ARGSUSED1 */
    642 static int
    643 puffs_fuse_node_link(struct puffs_cc *pcc, void *opc, void *targ,
    644 	const struct puffs_cn *pcn)
    645 {
    646 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    647 	struct puffs_node	*pn = targ;
    648 	struct fuse		*fuse;
    649 	int			ret;
    650 
    651 	fuse = (struct fuse *)pu->pu_privdata;
    652 	if (fuse->op.link == NULL) {
    653 		return ENOSYS;
    654 	}
    655 
    656 	/* wrap up return code */
    657 	ret = (*fuse->op.link)(PNPATH(pn), PCNPATH(pcn));
    658 
    659 	return -ret;
    660 }
    661 
    662 /*
    663  * fuse's regular interface provides chmod(), chown(), utimes()
    664  * and truncate() + some variations, so try to fit the square block
    665  * in the circle hole and the circle block .... something like that
    666  */
    667 /* ARGSUSED3 */
    668 static int
    669 puffs_fuse_node_setattr(struct puffs_cc *pcc, void *opc,
    670 	const struct vattr *va, const struct puffs_cred *pcr, pid_t pid)
    671 {
    672 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    673 	struct puffs_node	*pn = opc;
    674 	struct fuse		*fuse;
    675 	const char		*path = PNPATH(pn);
    676 
    677 	fuse = (struct fuse *)pu->pu_privdata;
    678 
    679 	return fuse_setattr(fuse, pn, path, va);
    680 }
    681 
    682 /* ARGSUSED2 */
    683 static int
    684 puffs_fuse_node_open(struct puffs_cc *pcc, void *opc, int flags,
    685 	const struct puffs_cred *cred, pid_t pid)
    686 {
    687 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    688 	struct puffs_node	*pn = opc;
    689 	struct refusenode	*rn = pn->pn_data;
    690 	struct fuse		*fuse;
    691 	const char		*path = PNPATH(pn);
    692 	int			 ret;
    693 
    694 	fuse = (struct fuse *)pu->pu_privdata;
    695 	if (fuse->op.open == NULL) {
    696 		return ENOSYS;
    697 	}
    698 
    699 	/*
    700 	 * examine type
    701 	 *  - if directory, return 0 rather than open
    702 	 *  - if open, don't open again, lest risk nuking file
    703 	 *    private info
    704 	 */
    705 	if (pn->pn_va.va_type == VDIR || rn->flags & RN_OPEN)
    706 		return 0;
    707 
    708 	ret = (*fuse->op.open)(path, &rn->file_info);
    709 
    710 	if (ret == 0) {
    711 		rn->flags |= RN_OPEN;
    712 	}
    713 
    714 	return -ret;
    715 }
    716 
    717 /* read some more from the file */
    718 /* ARGSUSED5 */
    719 static int
    720 puffs_fuse_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
    721 	off_t offset, size_t *resid, const struct puffs_cred *pcr,
    722 	int ioflag)
    723 {
    724 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    725 	struct puffs_node	*pn = opc;
    726 	struct refusenode	*rn = pn->pn_data;
    727 	struct fuse		*fuse;
    728 	const char		*path = PNPATH(pn);
    729 	size_t			maxread;
    730 	int			ret;
    731 
    732 	fuse = (struct fuse *)pu->pu_privdata;
    733 	if (fuse->op.read == NULL) {
    734 		return ENOSYS;
    735 	}
    736 
    737 	maxread = *resid;
    738 	if (maxread > pn->pn_va.va_size - offset) {
    739 		/*LINTED*/
    740 		maxread = pn->pn_va.va_size - offset;
    741 	}
    742 	if (maxread == 0)
    743 		return 0;
    744 
    745 	ret = (*fuse->op.read)(path, (char *)buf, maxread, offset,
    746 	    &rn->file_info);
    747 
    748 	if (ret > 0) {
    749 		*resid -= ret;
    750 		ret = 0;
    751 	}
    752 
    753 	return -ret;
    754 }
    755 
    756 /* write to the file */
    757 /* ARGSUSED0 */
    758 static int
    759 puffs_fuse_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
    760 	off_t offset, size_t *resid, const struct puffs_cred *pcr,
    761 	int ioflag)
    762 {
    763 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    764 	struct puffs_node	*pn = opc;
    765 	struct refusenode	*rn = pn->pn_data;
    766 	struct fuse		*fuse;
    767 	const char		*path = PNPATH(pn);
    768 	int			ret;
    769 
    770 	fuse = (struct fuse *)pu->pu_privdata;
    771 	if (fuse->op.write == NULL) {
    772 		return ENOSYS;
    773 	}
    774 
    775 	if (ioflag & PUFFS_IO_APPEND)
    776 		offset = pn->pn_va.va_size;
    777 
    778 	ret = (*fuse->op.write)(path, (char *)buf, *resid, offset,
    779 	    &rn->file_info);
    780 
    781 	if (ret > 0) {
    782 		if (offset + ret > pn->pn_va.va_size)
    783 			pn->pn_va.va_size = offset + ret;
    784 		*resid -= ret;
    785 		ret = 0;
    786 	}
    787 
    788 	return -ret;
    789 }
    790 
    791 
    792 /* ARGSUSED3 */
    793 static int
    794 puffs_fuse_node_readdir(struct puffs_cc *pcc, void *opc,
    795 	struct dirent *dent, const struct puffs_cred *pcr, off_t *readoff,
    796 	size_t *reslen)
    797 {
    798 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    799 	struct puffs_node	*pn = opc;
    800 	struct refusenode	*rn = pn->pn_data;
    801 	struct fuse		*fuse;
    802 	const char		*path = PNPATH(pn);
    803 	struct fuse_dirh	deh;
    804 	int			ret;
    805 
    806 	fuse = (struct fuse *)pu->pu_privdata;
    807 	if (fuse->op.readdir == NULL && fuse->op.getdir == NULL) {
    808 		return ENOSYS;
    809 	}
    810 
    811 	/* XXX: how to handle this??? */
    812 	if (*readoff != 0) {
    813 		return 0;
    814 	}
    815 
    816 	deh.dent = dent;
    817 	deh.reslen = *reslen;
    818 	deh.readoff = *readoff;
    819 
    820 	if (fuse->op.readdir)
    821 		ret = fuse->op.readdir(path, &deh, puffs_fuse_fill_dir,
    822 		    *readoff, &rn->file_info);
    823 	else
    824 		ret = fuse->op.getdir(path, &deh, puffs_fuse_dirfil);
    825 	*reslen = deh.reslen;
    826 	*readoff = 1;
    827 
    828 	if (ret == 0) {
    829 	}
    830 
    831 	return -ret;
    832 }
    833 
    834 /* ARGSUSED2 */
    835 static int
    836 puffs_fuse_node_inactive(struct puffs_cc *pcc, void *opc, pid_t pid,
    837 	int *refcount)
    838 {
    839 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    840 	struct puffs_node	*pn = opc;
    841 	struct refusenode	*rn = pn->pn_data;
    842 	struct fuse		*fuse;
    843 	const char		*path = PNPATH(pn);
    844 
    845 	*refcount = 1; /* safe default */
    846 	fuse = (struct fuse *)pu->pu_privdata;
    847 
    848 	if (rn && rn->flags & RN_OPEN) {
    849 		if (pn->pn_va.va_type == VDIR) {
    850 			if (fuse->op.releasedir)
    851 				fuse->op.releasedir(path, &rn->file_info);
    852 		} else {
    853 			if (fuse->op.release)
    854 				fuse->op.release(path, &rn->file_info);
    855 		}
    856 	}
    857 	if (rn)
    858 		rn->flags &= ~RN_OPEN;
    859 
    860 	return 0;
    861 }
    862 
    863 /* ARGSUSED */
    864 static int
    865 puffs_fuse_node_reclaim(struct puffs_cc *pcc, void *opc, pid_t pid)
    866 {
    867 	struct puffs_node	*pn = opc;
    868 
    869 	nukern(pn);
    870 
    871 	return 0;
    872 }
    873 
    874 /* ARGSUSED1 */
    875 static int
    876 puffs_fuse_fs_unmount(struct puffs_cc *pcc, int flags, pid_t pid)
    877 {
    878         struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    879 	struct fuse		*fuse;
    880 
    881 	fuse = (struct fuse *)pu->pu_privdata;
    882 	if (fuse->op.destroy == NULL) {
    883 		return 0;
    884 	}
    885 	(*fuse->op.destroy)(fuse);
    886         return 0;
    887 }
    888 
    889 /* ARGSUSED0 */
    890 static int
    891 puffs_fuse_fs_sync(struct puffs_cc *pcc, int flags,
    892             const struct puffs_cred *cr, pid_t pid)
    893 {
    894         return 0;
    895 }
    896 
    897 /* ARGSUSED2 */
    898 static int
    899 puffs_fuse_fs_statvfs(struct puffs_cc *pcc, struct statvfs *svfsb, pid_t pid)
    900 {
    901         struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    902 	struct fuse		*fuse;
    903 	int			ret;
    904 
    905 	fuse = (struct fuse *)pu->pu_privdata;
    906 	if (fuse->op.statfs == NULL) {
    907 		if ((ret = statvfs(PNPATH(pu->pu_pn_root), svfsb)) == -1) {
    908 			return errno;
    909 		}
    910 	} else {
    911 		ret = (*fuse->op.statfs)(PNPATH(pu->pu_pn_root), svfsb);
    912 	}
    913 
    914         return ret;
    915 }
    916 
    917 
    918 /* End of puffs_fuse operations */
    919 
    920 /* ARGSUSED3 */
    921 int
    922 fuse_main_real(int argc, char **argv, const struct fuse_operations *ops,
    923 	size_t size, void *userdata)
    924 {
    925 	struct puffs_usermount	*pu;
    926 	struct puffs_pathobj	*po_root;
    927 	struct puffs_ops	*pops;
    928 	struct statvfs		svfsb;
    929 	struct stat		st;
    930 	struct fuse		*fuse;
    931 	char			 name[64];
    932 	char			*slash;
    933 	int			 ret;
    934 
    935 	/* initialise the puffs operations structure */
    936         PUFFSOP_INIT(pops);
    937 
    938         PUFFSOP_SET(pops, puffs_fuse, fs, sync);
    939         PUFFSOP_SET(pops, puffs_fuse, fs, statvfs);
    940         PUFFSOP_SET(pops, puffs_fuse, fs, unmount);
    941 
    942 	/*
    943 	 * XXX: all of these don't possibly need to be
    944 	 * unconditionally set
    945 	 */
    946         PUFFSOP_SET(pops, puffs_fuse, node, lookup);
    947         PUFFSOP_SET(pops, puffs_fuse, node, getattr);
    948         PUFFSOP_SET(pops, puffs_fuse, node, setattr);
    949         PUFFSOP_SET(pops, puffs_fuse, node, readdir);
    950         PUFFSOP_SET(pops, puffs_fuse, node, readlink);
    951         PUFFSOP_SET(pops, puffs_fuse, node, mknod);
    952         PUFFSOP_SET(pops, puffs_fuse, node, create);
    953         PUFFSOP_SET(pops, puffs_fuse, node, remove);
    954         PUFFSOP_SET(pops, puffs_fuse, node, mkdir);
    955         PUFFSOP_SET(pops, puffs_fuse, node, rmdir);
    956         PUFFSOP_SET(pops, puffs_fuse, node, symlink);
    957         PUFFSOP_SET(pops, puffs_fuse, node, rename);
    958         PUFFSOP_SET(pops, puffs_fuse, node, link);
    959         PUFFSOP_SET(pops, puffs_fuse, node, open);
    960         PUFFSOP_SET(pops, puffs_fuse, node, read);
    961         PUFFSOP_SET(pops, puffs_fuse, node, write);
    962         PUFFSOP_SET(pops, puffs_fuse, node, inactive);
    963         PUFFSOP_SET(pops, puffs_fuse, node, reclaim);
    964 
    965 	NEW(struct fuse, fuse, "fuse_main_real", exit(EXIT_FAILURE));
    966 
    967 	/* copy fuse ops to their own stucture */
    968 	(void) memcpy(&fuse->op, ops, sizeof(fuse->op));
    969 
    970 	fcon.fuse = fuse;
    971 	fcon.private_data = userdata;
    972 
    973 	/* whilst this (assigning the pu_privdata in the puffs
    974 	 * usermount struct to be the fuse struct) might seem like
    975 	 * we are chasing our tail here, the logic is as follows:
    976 		+ the operation wrapper gets called with the puffs
    977 		  calling conventions
    978 		+ we need to fix up args first
    979 		+ then call the fuse user-supplied operation
    980 		+ then we fix up any values on return that we need to
    981 		+ and fix up any nodes, etc
    982 	 * so we need to be able to get at the fuse ops from within the
    983 	 * puffs_usermount struct
    984 	 */
    985 	if ((slash = strrchr(*argv, '/')) == NULL) {
    986 		slash = *argv;
    987 	} else {
    988 		slash += 1;
    989 	}
    990 	(void) snprintf(name, sizeof(name), "refuse:%s", slash);
    991 	pu = puffs_mount(pops, argv[argc - 1], MNT_NODEV | MNT_NOSUID,
    992 			name, fuse,
    993 			PUFFS_FLAG_BUILDPATH
    994 			  | PUFFS_FLAG_OPDUMP
    995 			  | PUFFS_KFLAG_NOCACHE,
    996 			0);
    997 	if (pu == NULL) {
    998 		err(EXIT_FAILURE, "puffs_mount");
    999 	}
   1000 
   1001 	fuse->pu = pu;
   1002 	pu->pu_pn_root = puffs_pn_new(pu, NULL);
   1003 	po_root = puffs_getrootpathobj(pu);
   1004 	po_root->po_path = strdup("/");
   1005 	po_root->po_len = 1;
   1006 
   1007 	/* sane defaults */
   1008 	puffs_vattr_null(&pu->pu_pn_root->pn_va);
   1009 	pu->pu_pn_root->pn_va.va_type = VDIR;
   1010 	pu->pu_pn_root->pn_va.va_mode = 0755;
   1011 	if (fuse->op.getattr)
   1012 		if (fuse->op.getattr(po_root->po_path, &st) == 0)
   1013 			puffs_stat2vattr(&pu->pu_pn_root->pn_va, &st);
   1014 	assert(pu->pu_pn_root->pn_va.va_type == VDIR);
   1015 
   1016 	if (fuse->op.init)
   1017 		fcon.private_data = fuse->op.init(NULL); /* XXX */
   1018 
   1019 	statvfs(argv[argc - 1], &svfsb); /* XXX - not really the correct dir */
   1020 	if (puffs_start(pu, pu->pu_pn_root, &svfsb) == -1) {
   1021 		err(EXIT_FAILURE, "puffs_start");
   1022 	}
   1023 
   1024 	ret = puffs_mainloop(fuse->pu, PUFFSLOOP_NODAEMON);
   1025 
   1026 	(void) free(po_root->po_path);
   1027 	FREE(fuse);
   1028 	return ret;
   1029 }
   1030 
   1031 /* ARGSUSED0 */
   1032 int
   1033 fuse_opt_parse(struct fuse_args *args, void *data,
   1034 	const struct fuse_opt *opts, fuse_opt_proc_t proc)
   1035 {
   1036 	return 0;
   1037 }
   1038 
   1039 /* XXX: threads */
   1040 struct fuse_context *
   1041 fuse_get_context()
   1042 {
   1043 
   1044 	return &fcon;
   1045 }
   1046 
   1047 void
   1048 fuse_exit(struct fuse *f)
   1049 {
   1050 
   1051 	puffs_exit(f->pu, 1);
   1052 }
   1053 
   1054 /*
   1055  * XXX: obviously not the most perfect of functions, but needs some
   1056  * puffs tweaking for a better tomorrow
   1057  */
   1058 void
   1059 fuse_unmount(const char *mp)
   1060 {
   1061 
   1062 	return;
   1063 }
   1064