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