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