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