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