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