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