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