Home | History | Annotate | Line # | Download | only in librefuse
refuse.c revision 1.14
      1 /*	$NetBSD: refuse.c,v 1.14 2007/02/15 19:33:52 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.14 2007/02/15 19:33:52 pooka Exp $");
     34 #endif /* !lint */
     35 
     36 #include <err.h>
     37 #include <errno.h>
     38 #include <fuse.h>
     39 #include <ucontext.h>
     40 #include <unistd.h>
     41 
     42 #include "defs.h"
     43 
     44 typedef uint64_t	 fuse_ino_t;
     45 
     46 struct fuse_config {
     47 	uid_t		uid;
     48 	gid_t		gid;
     49 	mode_t		umask;
     50 	double		entry_timeout;
     51 	double		negative_timeout;
     52 	double		attr_timeout;
     53 	double		ac_attr_timeout;
     54 	int		ac_attr_timeout_set;
     55 	int		debug;
     56 	int		hard_remove;
     57 	int		use_ino;
     58 	int		readdir_ino;
     59 	int		set_mode;
     60 	int		set_uid;
     61 	int		set_gid;
     62 	int		direct_io;
     63 	int		kernel_cache;
     64 	int		auto_cache;
     65 	int		intr;
     66 	int		intr_signal;
     67 };
     68 
     69 /* this is the private fuse structure */
     70 struct fuse {
     71 	struct fuse_session	*se;		/* fuse session pointer */
     72 	struct fuse_operations	op;		/* switch table of operations */
     73 	int			compat;		/* compat level -
     74 						 * not used in puffs_fuse */
     75 	struct node		**name_table;
     76 	size_t			name_table_size;
     77 	struct node		**id_table;
     78 	size_t			id_table_size;
     79 	fuse_ino_t		ctr;
     80 	unsigned int		generation;
     81 	unsigned int		hidectr;
     82 	pthread_mutex_t		lock;
     83 	pthread_rwlock_t	tree_lock;
     84 	void			*user_data;
     85 	struct fuse_config	conf;
     86 	int			intr_installed;
     87 	struct puffs_usermount	*pu;
     88 };
     89 
     90 struct refusenode {
     91 	struct fuse_file_info	 file_info;
     92 };
     93 
     94 static struct puffs_node *
     95 newrn(struct puffs_usermount *pu)
     96 {
     97 	struct puffs_node *pn;
     98 	struct refusenode *rn;
     99 
    100 	rn = malloc(sizeof(struct refusenode));
    101 	if (!rn)
    102 		abort(); /*XXX*/
    103 
    104 	memset(rn, 0, sizeof(struct refusenode));
    105 	pn = puffs_pn_new(pu, rn);
    106 
    107 	return pn;
    108 }
    109 
    110 static void
    111 nukern(struct puffs_node *pn)
    112 {
    113 
    114 	free(pn->pn_data);
    115 	puffs_pn_put(pn);
    116 }
    117 
    118 static ino_t fakeino = 3;
    119 
    120 /* XXX: rethinkme */
    121 struct fuse_dirh {
    122 	struct dirent *dent;
    123 	size_t reslen;
    124 	off_t readoff;
    125 };
    126 
    127 /* ARGSUSED2 */
    128 static int
    129 puffs_fuse_fill_dir(void *buf, const char *name,
    130 	const struct stat *stbuf, off_t off)
    131 {
    132 	struct fuse_dirh *deh = buf;
    133 	ino_t dino;
    134 	uint8_t dtype;
    135 
    136 	if (stbuf == NULL) {
    137 		dtype = DT_UNKNOWN;
    138 		dino = fakeino++;
    139 	} else {
    140 		dtype = puffs_vtype2dt(puffs_mode2vt(stbuf->st_mode));
    141 		dino = stbuf->st_ino;
    142 	}
    143 
    144 	return !puffs_nextdent(&deh->dent, name, dino, dtype, &deh->reslen);
    145 }
    146 
    147 static int
    148 puffs_fuse_dirfil(fuse_dirh_t h, const char *name, int type, ino_t ino)
    149 {
    150 	ino_t dino;
    151 	int dtype;
    152 
    153 	if (type == 0)
    154 		dtype = DT_UNKNOWN;
    155 	else
    156 		dtype = type;
    157 
    158 	if (ino)
    159 		dino = ino;
    160 	else
    161 		dino = fakeino++;
    162 
    163 	return !puffs_nextdent(&h->dent, name, dino, dtype, &h->reslen);
    164 }
    165 
    166 int
    167 fuse_opt_add_arg(struct fuse_args *args, const char *arg)
    168 {
    169 	char	**oldargv;
    170 	int	oldargc;
    171 
    172 	if (args->allocated) {
    173 		RENEW(char *, args->argv, args->argc + 1,
    174 		    "fuse_opt_add_arg1", return 0);
    175 	} else {
    176 		oldargv = args->argv;
    177 		oldargc = args->argc;
    178 		NEWARRAY(char *, args->argv, oldargc + 1,
    179 		    "fuse_opt_add_arg2", return 0);
    180 		(void) memcpy(args->argv, oldargv, oldargc * sizeof(char *));
    181 		args->allocated = 1;
    182 	}
    183 	args->argv[args->argc++] = strdup(arg);
    184 	return 1;
    185 }
    186 
    187 /* operation wrappers start here */
    188 
    189 /* lookup the path */
    190 /* ARGSUSED1 */
    191 static int
    192 puffs_fuse_node_lookup(struct puffs_cc *pcc, void *opc, void **newnode,
    193 	enum vtype *newtype, voff_t *newsize, dev_t *newrdev,
    194 	const struct puffs_cn *pcn)
    195 {
    196 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    197 	struct puffs_node	*pn_res;
    198 	struct stat		st;
    199 	struct fuse		*fuse;
    200 	const char		*path = PCNPATH(pcn);
    201 	int			ret;
    202 
    203 	fuse = (struct fuse *)pu->pu_privdata;
    204 	ret = fuse->op.getattr(path, &st);
    205 
    206 	if (ret != 0) {
    207 		return -ret; /* XXX: linux foo */
    208 	}
    209 
    210 	/* XXX: fiXXXme unconst */
    211 	pn_res = puffs_pn_nodewalk(pu, puffs_path_walkcmp,
    212 	    __UNCONST(&pcn->pcn_po_full));
    213 	if (pn_res == NULL) {
    214 		pn_res = newrn(pu);
    215 		if (pn_res == NULL)
    216 			return errno;
    217 		puffs_stat2vattr(&pn_res->pn_va, &st);
    218 	}
    219 
    220 	*newnode = pn_res;
    221 	*newtype = pn_res->pn_va.va_type;
    222 	*newsize = pn_res->pn_va.va_size;
    223 	*newrdev = pn_res->pn_va.va_rdev;
    224 
    225 	return 0;
    226 }
    227 
    228 /* get attributes for the path name */
    229 /* ARGSUSED3 */
    230 static int
    231 puffs_fuse_node_getattr(struct puffs_cc *pcc, void *opc, struct vattr *va,
    232 	const struct puffs_cred *pcr, pid_t pid)
    233 {
    234 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    235 	struct puffs_node	*pn = opc;
    236 	struct stat		 st;
    237 	struct fuse		*fuse;
    238 	const char		*path = PNPATH(pn);
    239 	int			ret;
    240 
    241 	fuse = (struct fuse *)pu->pu_privdata;
    242 	if (fuse->op.getattr == NULL) {
    243 		return ENOSYS;
    244 	}
    245 
    246 	/* wrap up return code */
    247 	ret = (*fuse->op.getattr)(path, &st);
    248 
    249 	if (ret == 0) {
    250 		/* fill in va from st */
    251 		va->va_mode = st.st_mode;
    252 		va->va_nlink = st.st_nlink;
    253 		va->va_uid = st.st_uid;
    254 		va->va_gid = st.st_gid;
    255 		va->va_fsid = st.st_rdev;
    256 		va->va_fileid = st.st_ino;
    257 		va->va_size = st.st_size;
    258 		va->va_blocksize = st.st_blksize;
    259 		va->va_atime = st.st_atimespec;
    260 		va->va_mtime = st.st_mtimespec;
    261 		va->va_ctime = st.st_ctimespec;
    262 		va->va_birthtime = st.st_birthtimespec;
    263 		va->va_gen = st.st_gen;
    264 		va->va_flags = st.st_flags;
    265 		va->va_rdev = st.st_rdev;
    266 		va->va_bytes = st.st_size;
    267 		va->va_filerev = st.st_gen;
    268 		va->va_vaflags = st.st_flags;
    269 
    270 	}
    271 
    272 	return ret;
    273 }
    274 
    275 /* read the contents of the symbolic link */
    276 /* ARGSUSED2 */
    277 static int
    278 puffs_fuse_node_readlink(struct puffs_cc *pcc, void *opc,
    279 	const struct puffs_cred *cred, char *linkname, size_t *linklen)
    280 {
    281 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    282 	struct puffs_node	*pn = opc;
    283 	struct fuse		*fuse;
    284 	const char		*path = PNPATH(pn), *p;
    285 	int			ret;
    286 
    287 	fuse = (struct fuse *)pu->pu_privdata;
    288 	if (fuse->op.readlink == NULL) {
    289 		return ENOSYS;
    290 	}
    291 
    292 	/* wrap up return code */
    293 	ret = (*fuse->op.readlink)(path, linkname, *linklen);
    294 
    295 	if (ret == 0) {
    296 		p = memchr(linkname, '\0', *linklen);
    297 		if (!p)
    298 			return EINVAL;
    299 
    300 		*linklen = p - linkname;
    301 	}
    302 
    303 	return -ret;
    304 }
    305 
    306 /* make the special node */
    307 /* ARGSUSED1 */
    308 static int
    309 puffs_fuse_node_mknod(struct puffs_cc *pcc, void *opc, void **newnode,
    310 	const struct puffs_cn *pcn, const struct vattr *va)
    311 {
    312 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    313 	struct puffs_node	*pn;
    314 	struct fuse		*fuse;
    315 	mode_t			 mode = va->va_mode;
    316 	const char		*path = PCNPATH(pcn);
    317 	int			ret;
    318 
    319 	fuse = (struct fuse *)pu->pu_privdata;
    320 	if (fuse->op.mknod == NULL) {
    321 		return ENOSYS;
    322 	}
    323 
    324 	/* wrap up return code */
    325 	ret = (*fuse->op.mknod)(path, mode, va->va_rdev);
    326 
    327 	if (ret == 0) {
    328 		/* fix up nodes */
    329 		pn = newrn(pu);
    330 		if (pn == NULL) {
    331 			unlink(PCNPATH(pcn));
    332 			return ENOMEM;
    333 		}
    334 		puffs_setvattr(&pn->pn_va, va);
    335 
    336 		*newnode = pn;
    337 	}
    338 
    339 	return ret;
    340 }
    341 
    342 /* make a directory */
    343 /* ARGSUSED1 */
    344 static int
    345 puffs_fuse_node_mkdir(struct puffs_cc *pcc, void *opc, void **newnode,
    346 	const struct puffs_cn *pcn, const struct vattr *va)
    347 {
    348 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    349 	struct puffs_node	*pn;
    350 	struct fuse		*fuse;
    351 	mode_t			 mode = va->va_mode;
    352 	const char		*path = PCNPATH(pcn);
    353 	int			ret;
    354 
    355 	fuse = (struct fuse *)pu->pu_privdata;
    356 	if (fuse->op.mkdir == NULL) {
    357 		return ENOSYS;
    358 	}
    359 
    360 	/* wrap up return code */
    361 	ret = (*fuse->op.mkdir)(path, mode);
    362 
    363 	if (ret == 0) {
    364 		/* fix up nodes */
    365 		pn = newrn(pu);
    366 		if (pn == NULL) {
    367 			rmdir(PCNPATH(pcn));
    368 			return ENOMEM;
    369 		}
    370 		puffs_setvattr(&pn->pn_va, va);
    371 
    372 		*newnode = pn;
    373 	}
    374 
    375 	return ret;
    376 }
    377 
    378 /* remove the directory entry */
    379 /* ARGSUSED1 */
    380 static int
    381 puffs_fuse_node_remove(struct puffs_cc *pcc, void *opc, void *targ,
    382 	const struct puffs_cn *pcn)
    383 {
    384 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    385 	struct fuse		*fuse;
    386 	const char		*path = PCNPATH(pcn);
    387 	int			ret;
    388 
    389 	fuse = (struct fuse *)pu->pu_privdata;
    390 	if (fuse->op.unlink == NULL) {
    391 		return ENOSYS;
    392 	}
    393 
    394 	/* wrap up return code */
    395 	ret = (*fuse->op.unlink)(path);
    396 
    397 	return ret;
    398 }
    399 
    400 /* remove the directory */
    401 /* ARGSUSED1 */
    402 static int
    403 puffs_fuse_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
    404 	const struct puffs_cn *pcn)
    405 {
    406 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    407 	struct fuse		*fuse;
    408 	const char		*path = PCNPATH(pcn);
    409 	int			ret;
    410 
    411 	fuse = (struct fuse *)pu->pu_privdata;
    412 	if (fuse->op.rmdir == NULL) {
    413 		return ENOSYS;
    414 	}
    415 
    416 	/* wrap up return code */
    417 	ret = (*fuse->op.rmdir)(path);
    418 
    419 	return ret;
    420 }
    421 
    422 /* create a symbolic link */
    423 /* ARGSUSED1 */
    424 static int
    425 puffs_fuse_node_symlink(struct puffs_cc *pcc, void *opc, void **newnode,
    426 	const struct puffs_cn *pcn_src, const struct vattr *va,
    427 	const char *link_target)
    428 {
    429 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    430 	struct puffs_node	*pn;
    431 	struct fuse		*fuse;
    432 	const char		*path = PCNPATH(pcn_src);
    433 	int			ret;
    434 
    435 	fuse = (struct fuse *)pu->pu_privdata;
    436 	if (fuse->op.symlink == NULL) {
    437 		return ENOSYS;
    438 	}
    439 
    440 	/* wrap up return code */
    441 	ret = (*fuse->op.symlink)(path, link_target);
    442 	/* XXX - check I haven't transposed these args */
    443 
    444 	if (ret == 0) {
    445 		/* fix up nodes */
    446 		pn = newrn(pu);
    447 		if (pn == NULL) {
    448 			unlink(link_target);
    449 			return ENOMEM;
    450 		}
    451 		puffs_setvattr(&pn->pn_va, va);
    452 
    453 		*newnode = pn;
    454 	}
    455 
    456 	return ret;
    457 }
    458 
    459 /* rename a directory entry */
    460 /* ARGSUSED1 */
    461 static int
    462 puffs_fuse_node_rename(struct puffs_cc *pcc, void *opc, void *src,
    463 	const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
    464 	const struct puffs_cn *pcn_targ)
    465 {
    466 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    467 	struct puffs_node	*pn = opc;
    468 	struct vattr		va;
    469 	struct fuse		*fuse;
    470 	const char		*path = PCNPATH(pcn_src);
    471 	int			ret;
    472 
    473 	fuse = (struct fuse *)pu->pu_privdata;
    474 	if (fuse->op.rename == NULL) {
    475 		return ENOSYS;
    476 	}
    477 
    478 	/* wrap up return code */
    479 	ret = (*fuse->op.rename)(path, PCNPATH(pcn_targ));
    480 
    481 	/* XXX: what's this guy doing??? */
    482 	if (ret == 0) {
    483 		(void) memcpy(&va, &pn->pn_va, sizeof(va));
    484 
    485 		puffs_pn_put(pn);
    486 
    487 		pn = puffs_pn_new(pu, NULL);
    488 		if (pn == NULL) {
    489 			return ENOMEM;
    490 		}
    491 		puffs_setvattr(&pn->pn_va, &va);
    492 
    493 	}
    494 
    495 	return ret;
    496 }
    497 
    498 /* create a link in the file system */
    499 /* ARGSUSED1 */
    500 static int
    501 puffs_fuse_node_link(struct puffs_cc *pcc, void *opc, void *targ,
    502 	const struct puffs_cn *pcn)
    503 {
    504 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    505 	struct puffs_node	*pn = targ;
    506 	struct fuse		*fuse;
    507 	int			ret;
    508 
    509 	fuse = (struct fuse *)pu->pu_privdata;
    510 	if (fuse->op.link == NULL) {
    511 		return ENOSYS;
    512 	}
    513 
    514 	/* wrap up return code */
    515 	ret = (*fuse->op.link)(PNPATH(pn), PCNPATH(pcn));
    516 
    517 	return ret;
    518 }
    519 
    520 /*
    521  * We run into a slight problemette here - puffs provides
    522  * setattr/getattr, whilst fuse provides all the usual chown/chmod/chgrp
    523  * functionality.  So that we don't miss out on anything when calling a
    524  * fuse operation, we have to get the vattr from the existing file,
    525  * find out what's changed, and then switch on that to call the fuse
    526  * function accordingly.
    527  */
    528 /* ARGSUSED3 */
    529 static int
    530 puffs_fuse_node_setattr(struct puffs_cc *pcc, void *opc,
    531 	const struct vattr *va, const struct puffs_cred *pcr, pid_t pid)
    532 {
    533 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    534 	struct puffs_node	*pn = opc;
    535 	struct fuse		*fuse;
    536 	const char		*path = PNPATH(pn);
    537 	mode_t			mode;
    538 	uid_t			uid;
    539 	gid_t			gid;
    540 	int			ret;
    541 
    542 	fuse = (struct fuse *)pu->pu_privdata;
    543 
    544 	ret = -1;
    545 
    546 	mode = va->va_mode;
    547 	uid = va->va_uid;
    548 	gid = va->va_gid;
    549 
    550 	if (mode != (mode_t)PUFFS_VNOVAL) {
    551 		if (fuse->op.chmod == NULL) {
    552 			return ENOSYS;
    553 		}
    554 		ret = (*fuse->op.chmod)(path, mode);
    555 	}
    556 	if (uid != (uid_t)PUFFS_VNOVAL || gid != (gid_t)PUFFS_VNOVAL) {
    557 		if (fuse->op.chown == NULL) {
    558 			return ENOSYS;
    559 		}
    560 		ret = (*fuse->op.chown)(path, uid, gid);
    561 	}
    562 
    563 	if (ret == 0) {
    564 	}
    565 
    566 	return ret;
    567 }
    568 
    569 /* ARGSUSED2 */
    570 static int
    571 puffs_fuse_node_open(struct puffs_cc *pcc, void *opc, int flags,
    572 	const struct puffs_cred *cred, pid_t pid)
    573 {
    574 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    575 	struct puffs_node	*pn = opc;
    576 	struct refusenode	*rn = pn->pn_data;
    577 	struct fuse		*fuse;
    578 	struct stat		 st;
    579 	const char		*path = PNPATH(pn);
    580 	int			 ret;
    581 
    582 	fuse = (struct fuse *)pu->pu_privdata;
    583 	if (fuse->op.open == NULL) {
    584 		return ENOSYS;
    585 	}
    586 
    587 	/* examine type - if directory, return 0 rather than open */
    588 	ret = (fuse->op.getattr == NULL) ?
    589 		stat(path, &st) :
    590 		(*fuse->op.getattr)(path, &st);
    591 	if (ret == 0 && (st.st_mode & S_IFMT) == S_IFDIR) {
    592 		return 0;
    593 	}
    594 
    595 	if (strcmp(path, "/") == 0) {
    596 		return 0;
    597 	}
    598 
    599 	ret = (*fuse->op.open)(path, &rn->file_info);
    600 
    601 	if (ret == 0) {
    602 	}
    603 
    604 	return ret;
    605 }
    606 
    607 /* read some more from the file */
    608 /* ARGSUSED5 */
    609 static int
    610 puffs_fuse_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
    611 	off_t offset, size_t *resid, const struct puffs_cred *pcr,
    612 	int ioflag)
    613 {
    614 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    615 	struct puffs_node	*pn = opc;
    616 	struct refusenode	*rn = pn->pn_data;
    617 	struct fuse		*fuse;
    618 	const char		*path = PNPATH(pn);
    619 	int			ret;
    620 
    621 	fuse = (struct fuse *)pu->pu_privdata;
    622 	if (fuse->op.read == NULL) {
    623 		return ENOSYS;
    624 	}
    625 
    626 	ret = (*fuse->op.read)(path, (char *)buf, *resid, offset,
    627 	    &rn->file_info);
    628 
    629 	if (ret > 0) {
    630 		*resid -= ret;
    631 	}
    632 
    633 	return 0;
    634 }
    635 
    636 /* write to the file */
    637 /* ARGSUSED0 */
    638 static int
    639 puffs_fuse_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
    640 	off_t offset, size_t *resid, const struct puffs_cred *pcr,
    641 	int ioflag)
    642 {
    643 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    644 	struct puffs_node	*pn = opc;
    645 	struct refusenode	*rn = pn->pn_data;
    646 	struct fuse		*fuse;
    647 	const char		*path = PNPATH(pn);
    648 	int			ret;
    649 
    650 	fuse = (struct fuse *)pu->pu_privdata;
    651 	if (fuse->op.write == NULL) {
    652 		return ENOSYS;
    653 	}
    654 
    655 	ret = (*fuse->op.write)(path, (char *)buf, *resid, offset,
    656 	    &rn->file_info);
    657 
    658 	if (ret > 0) {
    659 		*resid -= ret;
    660 	}
    661 
    662 	return ret;
    663 }
    664 
    665 
    666 /* ARGSUSED3 */
    667 static int
    668 puffs_fuse_node_readdir(struct puffs_cc *pcc, void *opc,
    669 	struct dirent *dent, const struct puffs_cred *pcr, off_t *readoff,
    670 	size_t *reslen)
    671 {
    672 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    673 	struct puffs_node	*pn = opc;
    674 	struct refusenode	*rn = pn->pn_data;
    675 	struct fuse		*fuse;
    676 	const char		*path = PNPATH(pn);
    677 	struct fuse_dirh	deh;
    678 	int			ret;
    679 
    680 	fuse = (struct fuse *)pu->pu_privdata;
    681 	if (fuse->op.readdir == NULL && fuse->op.getdir == NULL) {
    682 		return ENOSYS;
    683 	}
    684 
    685 	/* XXX: how to handle this??? */
    686 	if (*readoff != 0) {
    687 		return 0;
    688 	}
    689 
    690 	deh.dent = dent;
    691 	deh.reslen = *reslen;
    692 	deh.readoff = *readoff;
    693 
    694 	if (fuse->op.readdir)
    695 		ret = fuse->op.readdir(path, &deh, puffs_fuse_fill_dir,
    696 		    *readoff, &rn->file_info);
    697 	else
    698 		ret = fuse->op.getdir(path, &deh, puffs_fuse_dirfil);
    699 	*reslen = deh.reslen;
    700 	*readoff = 1;
    701 
    702 	if (ret == 0) {
    703 	}
    704 
    705 	return ret;
    706 }
    707 
    708 /* ARGSUSED */
    709 static int
    710 puffs_fuse_node_reclaim(struct puffs_cc *pcc, void *opc, pid_t pid)
    711 {
    712 	struct puffs_node	*pn = opc;
    713 
    714 	nukern(pn);
    715 
    716 	return 0;
    717 }
    718 
    719 /* ARGSUSED1 */
    720 static int
    721 puffs_fuse_fs_unmount(struct puffs_cc *pcc, int flags, pid_t pid)
    722 {
    723         struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    724 	struct fuse		*fuse;
    725 
    726 	fuse = (struct fuse *)pu->pu_privdata;
    727 	if (fuse->op.destroy == NULL) {
    728 		return 0;
    729 	}
    730 	(*fuse->op.destroy)(fuse);
    731         return 0;
    732 }
    733 
    734 /* ARGSUSED0 */
    735 static int
    736 puffs_fuse_fs_sync(struct puffs_cc *pcc, int flags,
    737             const struct puffs_cred *cr, pid_t pid)
    738 {
    739         return 0;
    740 }
    741 
    742 /* ARGSUSED2 */
    743 static int
    744 puffs_fuse_fs_statvfs(struct puffs_cc *pcc, struct statvfs *svfsb, pid_t pid)
    745 {
    746         struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    747 	struct fuse		*fuse;
    748 	int			ret;
    749 
    750 	fuse = (struct fuse *)pu->pu_privdata;
    751 	if (fuse->op.statfs == NULL) {
    752 		if ((ret = statvfs(PNPATH(pu->pu_pn_root), svfsb)) == -1) {
    753 			return errno;
    754 		}
    755 	} else {
    756 		ret = (*fuse->op.statfs)(PNPATH(pu->pu_pn_root), svfsb);
    757 	}
    758 
    759         return ret;
    760 }
    761 
    762 
    763 
    764 
    765 /* End of puffs_fuse operations */
    766 
    767 /*
    768  * XXX: do this otherwise if/when we grow thread support
    769  *
    770  * XXX2: does not supply uid, gid or pid currently
    771  */
    772 static struct fuse_context fcon;
    773 
    774 /* ARGSUSED3 */
    775 int
    776 fuse_main_real(int argc, char **argv, const struct fuse_operations *ops,
    777 	size_t size, void *userdata)
    778 {
    779 	struct puffs_usermount	*pu;
    780 	struct puffs_pathobj	*po_root;
    781 	struct puffs_ops	*pops;
    782 	struct statvfs		svfsb;
    783 	struct fuse		*fuse;
    784 	char			 name[64];
    785 	char			*slash;
    786 	int			 ret;
    787 
    788 	/* initialise the puffs operations structure */
    789         PUFFSOP_INIT(pops);
    790 
    791         PUFFSOP_SET(pops, puffs_fuse, fs, sync);
    792         PUFFSOP_SET(pops, puffs_fuse, fs, statvfs);
    793         PUFFSOP_SET(pops, puffs_fuse, fs, unmount);
    794 
    795 	/*
    796 	 * XXX: all of these don't possibly need to be
    797 	 * unconditionally set
    798 	 */
    799         PUFFSOP_SET(pops, puffs_fuse, node, lookup);
    800         PUFFSOP_SET(pops, puffs_fuse, node, getattr);
    801         PUFFSOP_SET(pops, puffs_fuse, node, readdir);
    802         PUFFSOP_SET(pops, puffs_fuse, node, readlink);
    803         PUFFSOP_SET(pops, puffs_fuse, node, mknod);
    804         PUFFSOP_SET(pops, puffs_fuse, node, mkdir);
    805         PUFFSOP_SET(pops, puffs_fuse, node, remove);
    806         PUFFSOP_SET(pops, puffs_fuse, node, rmdir);
    807         PUFFSOP_SET(pops, puffs_fuse, node, symlink);
    808         PUFFSOP_SET(pops, puffs_fuse, node, rename);
    809         PUFFSOP_SET(pops, puffs_fuse, node, link);
    810         PUFFSOP_SET(pops, puffs_fuse, node, setattr);
    811         PUFFSOP_SET(pops, puffs_fuse, node, open);
    812         PUFFSOP_SET(pops, puffs_fuse, node, read);
    813         PUFFSOP_SET(pops, puffs_fuse, node, write);
    814         PUFFSOP_SET(pops, puffs_fuse, node, readdir);
    815         PUFFSOP_SET(pops, puffs_fuse, node, read);
    816         PUFFSOP_SET(pops, puffs_fuse, node, write);
    817         PUFFSOP_SET(pops, puffs_fuse, node, reclaim);
    818 
    819 	NEW(struct fuse, fuse, "fuse_main_real", exit(EXIT_FAILURE));
    820 
    821 	/* copy fuse ops to their own stucture */
    822 	(void) memcpy(&fuse->op, ops, sizeof(fuse->op));
    823 
    824 	fcon.fuse = fuse;
    825 	fcon.private_data = userdata;
    826 
    827 	/* whilst this (assigning the pu_privdata in the puffs
    828 	 * usermount struct to be the fuse struct) might seem like
    829 	 * we are chasing our tail here, the logic is as follows:
    830 		+ the operation wrapper gets called with the puffs
    831 		  calling conventions
    832 		+ we need to fix up args first
    833 		+ then call the fuse user-supplied operation
    834 		+ then we fix up any values on return that we need to
    835 		+ and fix up any nodes, etc
    836 	 * so we need to be able to get at the fuse ops from within the
    837 	 * puffs_usermount struct
    838 	 */
    839 	if ((slash = strrchr(*argv, '/')) == NULL) {
    840 		slash = *argv;
    841 	} else {
    842 		slash += 1;
    843 	}
    844 	(void) snprintf(name, sizeof(name), "refuse:%s", slash);
    845 	pu = puffs_mount(pops, argv[argc - 1], MNT_NODEV | MNT_NOSUID,
    846 			name, fuse,
    847 			PUFFS_FLAG_BUILDPATH | PUFFS_FLAG_OPDUMP, 0);
    848 	if (pu == NULL) {
    849 		err(EXIT_FAILURE, "puffs_mount");
    850 	}
    851 
    852 	fuse->pu = pu;
    853 	pu->pu_pn_root = puffs_pn_new(pu, NULL);
    854 	po_root = puffs_getrootpathobj(pu);
    855 	po_root->po_path = strdup("/");
    856 	po_root->po_len = 1;
    857 
    858 	if (fuse->op.init)
    859 		fcon.private_data = fuse->op.init(NULL); /* XXX */
    860 
    861 	statvfs(argv[argc - 1], &svfsb); /* XXX - not really the correct dir */
    862 	if (puffs_start(pu, pu->pu_pn_root, &svfsb) == -1) {
    863 		err(EXIT_FAILURE, "puffs_start");
    864 	}
    865 
    866 	ret = puffs_mainloop(fuse->pu, PUFFSLOOP_NODAEMON);
    867 
    868 	(void) free(po_root->po_path);
    869 	FREE(fuse);
    870 	return ret;
    871 }
    872 
    873 /* ARGSUSED0 */
    874 int
    875 fuse_opt_parse(struct fuse_args *args, void *data,
    876 	const struct fuse_opt *opts, fuse_opt_proc_t proc)
    877 {
    878 	return 0;
    879 }
    880 
    881 /* XXX: threads */
    882 struct fuse_context *
    883 fuse_get_context()
    884 {
    885 
    886 	return &fcon;
    887 }
    888