Home | History | Annotate | Line # | Download | only in librefuse
refuse.c revision 1.80
      1  1.80     pooka /*	$NetBSD: refuse.c,v 1.80 2007/11/05 13:38:27 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.80     pooka __RCSID("$NetBSD: refuse.c,v 1.80 2007/11/05 13:38:27 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.72     pooka #include <paths.h>
     41  1.78     pooka #include <unistd.h>
     42  1.78     pooka #ifdef MULTITHREADED_REFUSE
     43  1.77     pooka #include <pthread.h>
     44  1.78     pooka #endif
     45   1.1       agc 
     46   1.1       agc #include "defs.h"
     47   1.1       agc 
     48   1.1       agc typedef uint64_t	 fuse_ino_t;
     49   1.1       agc 
     50   1.1       agc struct fuse_config {
     51   1.1       agc 	uid_t		uid;
     52   1.1       agc 	gid_t		gid;
     53   1.1       agc 	mode_t		umask;
     54   1.1       agc 	double		entry_timeout;
     55   1.1       agc 	double		negative_timeout;
     56   1.1       agc 	double		attr_timeout;
     57   1.1       agc 	double		ac_attr_timeout;
     58   1.1       agc 	int		ac_attr_timeout_set;
     59   1.1       agc 	int		debug;
     60   1.1       agc 	int		hard_remove;
     61   1.1       agc 	int		use_ino;
     62   1.1       agc 	int		readdir_ino;
     63   1.1       agc 	int		set_mode;
     64   1.1       agc 	int		set_uid;
     65   1.1       agc 	int		set_gid;
     66   1.1       agc 	int		direct_io;
     67   1.1       agc 	int		kernel_cache;
     68   1.1       agc 	int		auto_cache;
     69   1.1       agc 	int		intr;
     70   1.1       agc 	int		intr_signal;
     71   1.1       agc };
     72   1.1       agc 
     73  1.38     pooka struct fuse_chan {
     74  1.51       agc 	const char		*dir;
     75  1.51       agc 	struct fuse_args	*args;
     76  1.51       agc 	struct puffs_usermount	*pu;
     77  1.60     pooka 	int 			dead;
     78  1.38     pooka };
     79  1.38     pooka 
     80   1.1       agc /* this is the private fuse structure */
     81   1.1       agc struct fuse {
     82  1.38     pooka 	struct fuse_chan	*fc;		/* fuse channel pointer */
     83   1.1       agc 	struct fuse_operations	op;		/* switch table of operations */
     84   1.7     pooka 	int			compat;		/* compat level -
     85   1.7     pooka 						 * not used in puffs_fuse */
     86   1.1       agc 	struct node		**name_table;
     87   1.1       agc 	size_t			name_table_size;
     88   1.1       agc 	struct node		**id_table;
     89   1.1       agc 	size_t			id_table_size;
     90   1.1       agc 	fuse_ino_t		ctr;
     91   1.1       agc 	unsigned int		generation;
     92   1.1       agc 	unsigned int		hidectr;
     93   1.1       agc 	pthread_mutex_t		lock;
     94   1.1       agc 	pthread_rwlock_t	tree_lock;
     95   1.1       agc 	void			*user_data;
     96   1.1       agc 	struct fuse_config	conf;
     97   1.1       agc 	int			intr_installed;
     98   1.1       agc };
     99   1.1       agc 
    100  1.36     pooka struct puffs_fuse_dirh {
    101  1.43       agc 	void *dbuf;
    102  1.43       agc 	struct dirent *d;
    103  1.43       agc 
    104  1.43       agc 	size_t reslen;
    105  1.43       agc 	size_t bufsize;
    106  1.36     pooka };
    107  1.36     pooka 
    108  1.43       agc struct refusenode {
    109  1.43       agc 	struct fuse_file_info	file_info;
    110  1.43       agc 	struct puffs_fuse_dirh	dirh;
    111  1.43       agc 	int opencount;
    112  1.43       agc 	int flags;
    113  1.43       agc };
    114  1.30     pooka #define RN_ROOT		0x01
    115  1.31     pooka #define RN_OPEN		0x02	/* XXX: could just use opencount */
    116   1.5     pooka 
    117  1.43       agc static int fuse_setattr(struct fuse *, struct puffs_node *,
    118  1.43       agc 			const char *, const struct vattr *);
    119  1.26     pooka 
    120   1.5     pooka static struct puffs_node *
    121   1.5     pooka newrn(struct puffs_usermount *pu)
    122   1.5     pooka {
    123   1.5     pooka 	struct puffs_node *pn;
    124   1.5     pooka 	struct refusenode *rn;
    125   1.5     pooka 
    126  1.62       agc 	if ((rn = calloc(1, sizeof(*rn))) == NULL) {
    127  1.62       agc 		err(EXIT_FAILURE, "newrn");
    128  1.62       agc 	}
    129   1.5     pooka 	pn = puffs_pn_new(pu, rn);
    130   1.5     pooka 
    131   1.5     pooka 	return pn;
    132   1.5     pooka }
    133   1.5     pooka 
    134   1.5     pooka static void
    135   1.5     pooka nukern(struct puffs_node *pn)
    136   1.5     pooka {
    137  1.36     pooka 	struct refusenode *rn = pn->pn_data;
    138   1.5     pooka 
    139  1.36     pooka 	free(rn->dirh.dbuf);
    140  1.36     pooka 	free(rn);
    141   1.5     pooka 	puffs_pn_put(pn);
    142   1.5     pooka }
    143   1.5     pooka 
    144  1.66       agc /* XXX - not threadsafe */
    145   1.1       agc static ino_t fakeino = 3;
    146   1.1       agc 
    147  1.66       agc /***************** start of pthread context routines ************************/
    148  1.66       agc 
    149  1.21     pooka /*
    150  1.66       agc  * Notes on fuse_context:
    151  1.66       agc  * we follow fuse's lead and use the pthread specific information to hold
    152  1.66       agc  * a reference to the fuse_context structure for this thread.
    153  1.21     pooka  */
    154  1.78     pooka #ifdef MULTITHREADED_REFUSE
    155  1.66       agc static pthread_mutex_t		context_mutex = PTHREAD_MUTEX_INITIALIZER;
    156  1.66       agc static pthread_key_t		context_key;
    157  1.80     pooka static unsigned long		context_refc;
    158  1.78     pooka #endif
    159  1.66       agc 
    160  1.66       agc /* return the fuse_context struct related to this thread */
    161  1.66       agc struct fuse_context *
    162  1.66       agc fuse_get_context(void)
    163  1.66       agc {
    164  1.78     pooka #ifdef MULTITHREADED_REFUSE
    165  1.66       agc 	struct fuse_context	*ctxt;
    166  1.66       agc 
    167  1.66       agc 	if ((ctxt = pthread_getspecific(context_key)) == NULL) {
    168  1.66       agc 		if ((ctxt = calloc(1, sizeof(struct fuse_context))) == NULL) {
    169  1.80     pooka 			abort();
    170  1.66       agc 		}
    171  1.66       agc 		pthread_setspecific(context_key, ctxt);
    172  1.66       agc 	}
    173  1.66       agc 	return ctxt;
    174  1.78     pooka #else
    175  1.78     pooka 	static struct fuse_context	fcon;
    176  1.78     pooka 
    177  1.78     pooka 	return &fcon;
    178  1.78     pooka #endif
    179  1.66       agc }
    180  1.66       agc 
    181  1.66       agc /* used as a callback function */
    182  1.78     pooka #ifdef MULTITHREADED_REFUSE
    183  1.66       agc static void
    184  1.66       agc free_context(void *ctxt)
    185  1.66       agc {
    186  1.66       agc 	free(ctxt);
    187  1.66       agc }
    188  1.78     pooka #endif
    189  1.66       agc 
    190  1.80     pooka /*
    191  1.80     pooka  * Create the pthread key.  The reason for the complexity is to
    192  1.80     pooka  * enable use of multiple fuse instances within a single process.
    193  1.80     pooka  */
    194  1.66       agc static int
    195  1.66       agc create_context_key(void)
    196  1.66       agc {
    197  1.78     pooka #ifdef MULTITHREADED_REFUSE
    198  1.80     pooka 	int rv;
    199  1.80     pooka 
    200  1.80     pooka 	rv = pthread_mutex_lock(&context_mutex);
    201  1.80     pooka 	assert(rv == 0);
    202  1.80     pooka 
    203  1.80     pooka 	if (context_refc == 0) {
    204  1.66       agc 		if (pthread_key_create(&context_key, free_context) != 0) {
    205  1.66       agc 			warnx("create_context_key: pthread_key_create failed");
    206  1.66       agc 			pthread_mutex_unlock(&context_mutex);
    207  1.66       agc 			return 0;
    208  1.66       agc 		}
    209  1.66       agc 	}
    210  1.66       agc 	context_refc += 1;
    211  1.66       agc 	pthread_mutex_unlock(&context_mutex);
    212  1.66       agc 	return 1;
    213  1.78     pooka #else
    214  1.78     pooka 	return 1;
    215  1.78     pooka #endif
    216  1.66       agc }
    217  1.66       agc 
    218  1.66       agc static void
    219  1.66       agc delete_context_key(void)
    220  1.66       agc {
    221  1.78     pooka #ifdef MULTITHREADED_REFUSE
    222  1.66       agc 	pthread_mutex_lock(&context_mutex);
    223  1.80     pooka 	/* If we are the last fuse instances using the key, delete it */
    224  1.66       agc 	if (--context_refc == 0) {
    225  1.66       agc 		free(pthread_getspecific(context_key));
    226  1.66       agc 		pthread_key_delete(context_key);
    227  1.66       agc 	}
    228  1.66       agc 	pthread_mutex_unlock(&context_mutex);
    229  1.78     pooka #endif
    230  1.66       agc }
    231  1.66       agc 
    232  1.66       agc /* set the uid and gid of the calling process in the current fuse context */
    233  1.66       agc static void
    234  1.66       agc set_fuse_context_uid_gid(const struct puffs_cred *cred)
    235  1.66       agc {
    236  1.66       agc 	struct fuse_context	*fusectx;
    237  1.66       agc 	uid_t			 uid;
    238  1.66       agc 	gid_t			 gid;
    239  1.66       agc 
    240  1.66       agc 	fusectx = fuse_get_context();
    241  1.66       agc 	if (puffs_cred_getuid(cred, &uid) == 0) {
    242  1.66       agc 		fusectx->uid = uid;
    243  1.66       agc 	}
    244  1.66       agc 	if (puffs_cred_getgid(cred, &gid) == 0) {
    245  1.66       agc 		fusectx->gid = gid;
    246  1.66       agc 	}
    247  1.66       agc }
    248  1.66       agc 
    249  1.66       agc /* set the pid of the calling process in the current fuse context */
    250  1.66       agc static void
    251  1.76     pooka set_fuse_context_pid(struct puffs_cc *pcc)
    252  1.66       agc {
    253  1.66       agc 	struct fuse_context	*fusectx;
    254  1.21     pooka 
    255  1.66       agc 	fusectx = fuse_get_context();
    256  1.76     pooka 	puffs_cc_getcaller(pcc, &fusectx->pid, NULL);
    257  1.66       agc }
    258  1.66       agc 
    259  1.66       agc /***************** end of pthread context routines ************************/
    260  1.62       agc 
    261  1.36     pooka #define DIR_CHUNKSIZE 4096
    262  1.36     pooka static int
    263  1.36     pooka fill_dirbuf(struct puffs_fuse_dirh *dh, const char *name, ino_t dino,
    264  1.36     pooka 	uint8_t dtype)
    265  1.36     pooka {
    266  1.36     pooka 
    267  1.36     pooka 	/* initial? */
    268  1.36     pooka 	if (dh->bufsize == 0) {
    269  1.65       agc 		if ((dh->dbuf = calloc(1, DIR_CHUNKSIZE)) == NULL) {
    270  1.80     pooka 			abort();
    271  1.62       agc 		}
    272  1.36     pooka 		dh->d = dh->dbuf;
    273  1.36     pooka 		dh->reslen = dh->bufsize = DIR_CHUNKSIZE;
    274  1.36     pooka 	}
    275  1.21     pooka 
    276  1.62       agc 	if (puffs_nextdent(&dh->d, name, dino, dtype, &dh->reslen)) {
    277  1.36     pooka 		return 0;
    278  1.62       agc 	}
    279  1.36     pooka 
    280  1.36     pooka 	/* try to increase buffer space */
    281  1.36     pooka 	dh->dbuf = realloc(dh->dbuf, dh->bufsize + DIR_CHUNKSIZE);
    282  1.62       agc 	if (dh->dbuf == NULL) {
    283  1.80     pooka 		abort();
    284  1.62       agc 	}
    285  1.36     pooka 	dh->d = (void *)((uint8_t *)dh->dbuf + (dh->bufsize - dh->reslen));
    286  1.36     pooka 	dh->reslen += DIR_CHUNKSIZE;
    287  1.36     pooka 	dh->bufsize += DIR_CHUNKSIZE;
    288  1.36     pooka 
    289  1.36     pooka 	return !puffs_nextdent(&dh->d, name, dino, dtype, &dh->reslen);
    290  1.36     pooka }
    291   1.1       agc 
    292  1.36     pooka /* ARGSUSED3 */
    293  1.36     pooka /* XXX: I have no idea how "off" is supposed to be used */
    294   1.1       agc static int
    295   1.4     pooka puffs_fuse_fill_dir(void *buf, const char *name,
    296   1.1       agc 	const struct stat *stbuf, off_t off)
    297   1.1       agc {
    298  1.36     pooka 	struct puffs_fuse_dirh *deh = buf;
    299  1.13     pooka 	ino_t dino;
    300   1.1       agc 	uint8_t dtype;
    301   1.1       agc 
    302  1.13     pooka 	if (stbuf == NULL) {
    303  1.13     pooka 		dtype = DT_UNKNOWN;
    304  1.13     pooka 		dino = fakeino++;
    305  1.13     pooka 	} else {
    306  1.13     pooka 		dtype = puffs_vtype2dt(puffs_mode2vt(stbuf->st_mode));
    307  1.13     pooka 		dino = stbuf->st_ino;
    308  1.50     pooka 
    309  1.50     pooka 		/*
    310  1.50     pooka 		 * Some FUSE file systems like to always use 0 as the
    311  1.50     pooka 		 * inode number.   Our readdir() doesn't like to show
    312  1.50     pooka 		 * directory entries with inode number 0 ==> workaround.
    313  1.50     pooka 		 */
    314  1.62       agc 		if (dino == 0) {
    315  1.50     pooka 			dino = fakeino++;
    316  1.62       agc 		}
    317  1.13     pooka 	}
    318   1.1       agc 
    319  1.36     pooka 	return fill_dirbuf(deh, name, dino, dtype);
    320   1.4     pooka }
    321   1.4     pooka 
    322   1.4     pooka static int
    323   1.4     pooka puffs_fuse_dirfil(fuse_dirh_t h, const char *name, int type, ino_t ino)
    324   1.4     pooka {
    325  1.43       agc 	ino_t dino;
    326  1.43       agc 	int dtype;
    327   1.4     pooka 
    328  1.62       agc 	if ((dtype = type) == 0) {
    329  1.13     pooka 		dtype = DT_UNKNOWN;
    330  1.62       agc 	}
    331  1.43       agc 
    332  1.62       agc 	dino = (ino) ? ino : fakeino++;
    333   1.4     pooka 
    334  1.36     pooka 	return fill_dirbuf(h, name, dino, dtype);
    335   1.1       agc }
    336   1.1       agc 
    337  1.62       agc /* place the refuse file system name into `name' */
    338  1.53       agc static void
    339  1.53       agc set_refuse_mount_name(char **argv, char *name, size_t size)
    340  1.53       agc {
    341  1.53       agc 	char	*slash;
    342  1.53       agc 
    343  1.53       agc 	if (argv == NULL || *argv == NULL) {
    344  1.53       agc 		(void) strlcpy(name, "refuse", size);
    345  1.53       agc 	} else {
    346  1.53       agc 		if ((slash = strrchr(*argv, '/')) == NULL) {
    347  1.53       agc 			slash = *argv;
    348  1.53       agc 		} else {
    349  1.53       agc 			slash += 1;
    350  1.53       agc 		}
    351  1.53       agc 		if (strncmp(*argv, "refuse:", 7) == 0) {
    352  1.53       agc 			/* we've already done this */
    353  1.53       agc 			(void) strlcpy(name, *argv, size);
    354  1.53       agc 		} else {
    355  1.53       agc 			(void) snprintf(name, size, "refuse:%s", slash);
    356  1.53       agc 		}
    357  1.53       agc 	}
    358  1.53       agc }
    359  1.53       agc 
    360  1.53       agc 
    361  1.51       agc /* this function exposes struct fuse to userland */
    362  1.51       agc struct fuse *
    363  1.51       agc fuse_setup(int argc, char **argv, const struct fuse_operations *ops,
    364  1.51       agc 	size_t size, char **mountpoint, int *multithreaded, int *fd)
    365  1.51       agc {
    366  1.51       agc 	struct fuse_chan	*fc;
    367  1.51       agc 	struct fuse_args	*args;
    368  1.51       agc 	struct fuse		*fuse;
    369  1.51       agc 	char			 name[64];
    370  1.53       agc 	int			 i;
    371  1.51       agc 
    372  1.66       agc 	/* set up the proper name */
    373  1.53       agc 	set_refuse_mount_name(argv, name, sizeof(name));
    374  1.51       agc 
    375  1.66       agc 	/* grab the pthread context key */
    376  1.66       agc 	if (!create_context_key()) {
    377  1.80     pooka 		return NULL;
    378  1.66       agc 	}
    379  1.66       agc 
    380  1.51       agc 	/* stuff name into fuse_args */
    381  1.56  christos 	args = fuse_opt_deep_copy_args(argc, argv);
    382  1.51       agc 	if (args->argc > 0) {
    383  1.55  christos 		free(args->argv[0]);
    384  1.51       agc 	}
    385  1.66       agc 	if ((args->argv[0] = strdup(name)) == NULL) {
    386  1.80     pooka 		fuse_opt_free_args(args);
    387  1.80     pooka 		return NULL;
    388  1.66       agc 	}
    389  1.51       agc 
    390  1.62       agc 	/* count back from the end over arguments starting with '-' */
    391  1.53       agc 	for (i = argc - 1 ; i > 0 && *argv[i] == '-' ; --i) {
    392  1.53       agc 	}
    393  1.53       agc 
    394  1.53       agc 	fc = fuse_mount(*mountpoint = argv[i], args);
    395  1.51       agc 	fuse = fuse_new(fc, args, ops, size, NULL);
    396  1.51       agc 
    397  1.56  christos 	fuse_opt_free_args(args);
    398  1.56  christos 	free(args);
    399  1.51       agc 
    400  1.51       agc 	/* XXX - wait for puffs to become multi-threaded */
    401  1.51       agc 	if (multithreaded) {
    402  1.51       agc 		*multithreaded = 0;
    403  1.51       agc 	}
    404  1.51       agc 
    405  1.51       agc 	/* XXX - this is unused */
    406  1.51       agc 	if (fd) {
    407  1.51       agc 		*fd = 0;
    408  1.51       agc 	}
    409  1.51       agc 
    410  1.51       agc 	return fuse;
    411  1.51       agc }
    412  1.51       agc 
    413  1.18     pooka #define FUSE_ERR_UNLINK(fuse, file) if (fuse->op.unlink) fuse->op.unlink(file)
    414  1.18     pooka #define FUSE_ERR_RMDIR(fuse, dir) if (fuse->op.rmdir) fuse->op.rmdir(dir)
    415  1.18     pooka 
    416  1.26     pooka /* ARGSUSED1 */
    417  1.26     pooka static int
    418  1.26     pooka fuse_getattr(struct fuse *fuse, struct puffs_node *pn, const char *path,
    419  1.26     pooka 	struct vattr *va)
    420  1.26     pooka {
    421  1.26     pooka 	struct stat		 st;
    422  1.26     pooka 	int			ret;
    423  1.26     pooka 
    424  1.26     pooka 	if (fuse->op.getattr == NULL) {
    425  1.26     pooka 		return ENOSYS;
    426  1.26     pooka 	}
    427  1.26     pooka 
    428  1.26     pooka 	/* wrap up return code */
    429  1.74     pooka 	memset(&st, 0, sizeof(st));
    430  1.26     pooka 	ret = (*fuse->op.getattr)(path, &st);
    431  1.26     pooka 
    432  1.26     pooka 	if (ret == 0) {
    433  1.74     pooka 		if (st.st_blksize == 0)
    434  1.74     pooka 			st.st_blksize = DEV_BSIZE;
    435  1.26     pooka 		puffs_stat2vattr(va, &st);
    436  1.26     pooka 	}
    437  1.26     pooka 
    438  1.26     pooka 	return -ret;
    439  1.26     pooka }
    440  1.26     pooka 
    441  1.66       agc /* utility function to set various elements of the attribute */
    442  1.26     pooka static int
    443  1.26     pooka fuse_setattr(struct fuse *fuse, struct puffs_node *pn, const char *path,
    444  1.26     pooka 	const struct vattr *va)
    445  1.26     pooka {
    446  1.26     pooka 	struct refusenode	*rn = pn->pn_data;
    447  1.26     pooka 	mode_t			mode;
    448  1.26     pooka 	uid_t			uid;
    449  1.26     pooka 	gid_t			gid;
    450  1.26     pooka 	int			error, ret;
    451  1.26     pooka 
    452  1.26     pooka 	error = 0;
    453  1.26     pooka 
    454  1.26     pooka 	mode = va->va_mode;
    455  1.26     pooka 	uid = va->va_uid;
    456  1.26     pooka 	gid = va->va_gid;
    457  1.26     pooka 
    458  1.26     pooka 	if (mode != (mode_t)PUFFS_VNOVAL) {
    459  1.26     pooka 		ret = 0;
    460  1.26     pooka 
    461  1.26     pooka 		if (fuse->op.chmod == NULL) {
    462  1.26     pooka 			error = -ENOSYS;
    463  1.26     pooka 		} else {
    464  1.26     pooka 			ret = fuse->op.chmod(path, mode);
    465  1.26     pooka 			if (ret)
    466  1.26     pooka 				error = ret;
    467  1.26     pooka 		}
    468  1.26     pooka 	}
    469  1.26     pooka 	if (uid != (uid_t)PUFFS_VNOVAL || gid != (gid_t)PUFFS_VNOVAL) {
    470  1.26     pooka 		ret = 0;
    471  1.26     pooka 
    472  1.26     pooka 		if (fuse->op.chown == NULL) {
    473  1.26     pooka 			error = -ENOSYS;
    474  1.26     pooka 		} else {
    475  1.26     pooka 			ret = fuse->op.chown(path, uid, gid);
    476  1.26     pooka 			if (ret)
    477  1.26     pooka 				error = ret;
    478  1.26     pooka 		}
    479  1.26     pooka 	}
    480  1.26     pooka 	if (va->va_atime.tv_sec != (time_t)PUFFS_VNOVAL
    481  1.26     pooka 	    || va->va_mtime.tv_sec != (long)PUFFS_VNOVAL) {
    482  1.26     pooka 		ret = 0;
    483  1.26     pooka 
    484  1.26     pooka 		if (fuse->op.utimens) {
    485  1.26     pooka 			struct timespec tv[2];
    486  1.26     pooka 
    487  1.26     pooka 			tv[0].tv_sec = va->va_atime.tv_sec;
    488  1.26     pooka 			tv[0].tv_nsec = va->va_atime.tv_nsec;
    489  1.26     pooka 			tv[1].tv_sec = va->va_mtime.tv_sec;
    490  1.26     pooka 			tv[1].tv_nsec = va->va_mtime.tv_nsec;
    491  1.26     pooka 
    492  1.26     pooka 			ret = fuse->op.utimens(path, tv);
    493  1.26     pooka 		} else if (fuse->op.utime) {
    494  1.26     pooka 			struct utimbuf timbuf;
    495  1.26     pooka 
    496  1.26     pooka 			timbuf.actime = va->va_atime.tv_sec;
    497  1.26     pooka 			timbuf.modtime = va->va_mtime.tv_sec;
    498  1.26     pooka 
    499  1.26     pooka 			ret = fuse->op.utime(path, &timbuf);
    500  1.26     pooka 		} else {
    501  1.26     pooka 			error = -ENOSYS;
    502  1.26     pooka 		}
    503  1.26     pooka 
    504  1.26     pooka 		if (ret)
    505  1.26     pooka 			error = ret;
    506  1.26     pooka 	}
    507  1.26     pooka 	if (va->va_size != (u_quad_t)PUFFS_VNOVAL) {
    508  1.26     pooka 		ret = 0;
    509  1.26     pooka 
    510  1.26     pooka 		if (fuse->op.truncate) {
    511  1.26     pooka 			ret = fuse->op.truncate(path, (off_t)va->va_size);
    512  1.26     pooka 		} else if (fuse->op.ftruncate) {
    513  1.26     pooka 			ret = fuse->op.ftruncate(path, (off_t)va->va_size,
    514  1.26     pooka 			    &rn->file_info);
    515  1.26     pooka 		} else {
    516  1.26     pooka 			error = -ENOSYS;
    517  1.26     pooka 		}
    518  1.26     pooka 
    519  1.26     pooka 		if (ret)
    520  1.26     pooka 			error = ret;
    521  1.26     pooka 	}
    522  1.26     pooka 	/* XXX: no reflection with reality */
    523  1.26     pooka 	puffs_setvattr(&pn->pn_va, va);
    524  1.26     pooka 
    525  1.26     pooka 	return -error;
    526  1.26     pooka 
    527  1.26     pooka }
    528  1.26     pooka 
    529  1.26     pooka static int
    530  1.26     pooka fuse_newnode(struct puffs_usermount *pu, const char *path,
    531  1.71     pooka 	const struct vattr *va, struct fuse_file_info *fi,
    532  1.71     pooka 	struct puffs_newinfo *pni, struct puffs_node **pn_new)
    533  1.26     pooka {
    534  1.26     pooka 	struct puffs_node	*pn;
    535  1.26     pooka 	struct refusenode	*rn;
    536  1.66       agc 	struct vattr		 newva;
    537  1.66       agc 	struct fuse		*fuse;
    538  1.26     pooka 
    539  1.46     pooka 	fuse = puffs_getspecific(pu);
    540  1.26     pooka 
    541  1.26     pooka 	/* fix up nodes */
    542  1.26     pooka 	pn = newrn(pu);
    543  1.26     pooka 	if (pn == NULL) {
    544  1.26     pooka 		if (va->va_type == VDIR) {
    545  1.26     pooka 			FUSE_ERR_RMDIR(fuse, path);
    546  1.26     pooka 		} else {
    547  1.26     pooka 			FUSE_ERR_UNLINK(fuse, path);
    548  1.26     pooka 		}
    549  1.26     pooka 		return ENOMEM;
    550  1.26     pooka 	}
    551  1.26     pooka 	fuse_setattr(fuse, pn, path, va);
    552  1.26     pooka 	if (fuse_getattr(fuse, pn, path, &newva) == 0)
    553  1.26     pooka 		puffs_setvattr(&pn->pn_va, &newva);
    554  1.26     pooka 
    555  1.26     pooka 	rn = pn->pn_data;
    556  1.26     pooka 	if (fi)
    557  1.26     pooka 		memcpy(&rn->file_info, fi, sizeof(struct fuse_file_info));
    558  1.26     pooka 
    559  1.71     pooka 	puffs_newinfo_setcookie(pni, pn);
    560  1.71     pooka 	if (pn_new)
    561  1.71     pooka 		*pn_new = pn;
    562  1.26     pooka 
    563  1.26     pooka 	return 0;
    564  1.26     pooka }
    565  1.26     pooka 
    566  1.26     pooka 
    567   1.1       agc /* operation wrappers start here */
    568   1.1       agc 
    569   1.1       agc /* lookup the path */
    570   1.1       agc /* ARGSUSED1 */
    571   1.1       agc static int
    572  1.71     pooka puffs_fuse_node_lookup(struct puffs_cc *pcc, void *opc,
    573  1.71     pooka 	struct puffs_newinfo *pni, const struct puffs_cn *pcn)
    574   1.1       agc {
    575   1.1       agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    576  1.12     pooka 	struct puffs_node	*pn_res;
    577  1.66       agc 	struct stat		 st;
    578   1.1       agc 	struct fuse		*fuse;
    579   1.1       agc 	const char		*path = PCNPATH(pcn);
    580  1.66       agc 	int			 ret;
    581   1.1       agc 
    582  1.46     pooka 	fuse = puffs_getspecific(pu);
    583  1.62       agc 
    584  1.69     pooka 	set_fuse_context_uid_gid(pcn->pcn_cred);
    585  1.62       agc 
    586   1.1       agc 	ret = fuse->op.getattr(path, &st);
    587  1.12     pooka 
    588   1.1       agc 	if (ret != 0) {
    589  1.18     pooka 		return -ret;
    590  1.12     pooka 	}
    591  1.12     pooka 
    592  1.12     pooka 	/* XXX: fiXXXme unconst */
    593  1.12     pooka 	pn_res = puffs_pn_nodewalk(pu, puffs_path_walkcmp,
    594  1.12     pooka 	    __UNCONST(&pcn->pcn_po_full));
    595  1.12     pooka 	if (pn_res == NULL) {
    596  1.12     pooka 		pn_res = newrn(pu);
    597  1.12     pooka 		if (pn_res == NULL)
    598  1.12     pooka 			return errno;
    599  1.26     pooka 		puffs_stat2vattr(&pn_res->pn_va, &st);
    600   1.1       agc 	}
    601  1.12     pooka 
    602  1.71     pooka 	puffs_newinfo_setcookie(pni, pn_res);
    603  1.71     pooka 	puffs_newinfo_setvtype(pni, pn_res->pn_va.va_type);
    604  1.71     pooka 	puffs_newinfo_setsize(pni, (voff_t)pn_res->pn_va.va_size);
    605  1.71     pooka 	puffs_newinfo_setrdev(pni, pn_res->pn_va.va_rdev);
    606  1.12     pooka 
    607  1.12     pooka 	return 0;
    608   1.1       agc }
    609   1.1       agc 
    610   1.1       agc /* get attributes for the path name */
    611   1.1       agc /* ARGSUSED3 */
    612   1.1       agc static int
    613   1.1       agc puffs_fuse_node_getattr(struct puffs_cc *pcc, void *opc, struct vattr *va,
    614  1.70     pooka 	const struct puffs_cred *pcr, const struct puffs_cid *pcid)
    615   1.1       agc {
    616   1.1       agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    617   1.1       agc 	struct puffs_node	*pn = opc;
    618   1.1       agc 	struct fuse		*fuse;
    619   1.1       agc 	const char		*path = PNPATH(pn);
    620   1.1       agc 
    621  1.46     pooka 	fuse = puffs_getspecific(pu);
    622  1.62       agc 
    623  1.66       agc 	set_fuse_context_uid_gid(pcr);
    624  1.62       agc 
    625  1.26     pooka 	return fuse_getattr(fuse, pn, path, va);
    626   1.1       agc }
    627   1.1       agc 
    628   1.1       agc /* read the contents of the symbolic link */
    629   1.1       agc /* ARGSUSED2 */
    630   1.1       agc static int
    631   1.1       agc puffs_fuse_node_readlink(struct puffs_cc *pcc, void *opc,
    632   1.1       agc 	const struct puffs_cred *cred, char *linkname, size_t *linklen)
    633   1.1       agc {
    634   1.1       agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    635   1.1       agc 	struct puffs_node	*pn = opc;
    636   1.1       agc 	struct fuse		*fuse;
    637  1.13     pooka 	const char		*path = PNPATH(pn), *p;
    638   1.1       agc 	int			ret;
    639   1.1       agc 
    640  1.46     pooka 	fuse = puffs_getspecific(pu);
    641   1.1       agc 	if (fuse->op.readlink == NULL) {
    642   1.1       agc 		return ENOSYS;
    643   1.1       agc 	}
    644   1.1       agc 
    645  1.66       agc 	set_fuse_context_uid_gid(cred);
    646  1.62       agc 
    647   1.1       agc 	/* wrap up return code */
    648   1.1       agc 	ret = (*fuse->op.readlink)(path, linkname, *linklen);
    649   1.1       agc 
    650   1.1       agc 	if (ret == 0) {
    651  1.43       agc 		p = memchr(linkname, '\0', *linklen);
    652  1.13     pooka 		if (!p)
    653  1.13     pooka 			return EINVAL;
    654  1.13     pooka 
    655  1.14     pooka 		*linklen = p - linkname;
    656   1.1       agc 	}
    657   1.1       agc 
    658  1.13     pooka 	return -ret;
    659   1.1       agc }
    660   1.1       agc 
    661   1.1       agc /* make the special node */
    662   1.1       agc /* ARGSUSED1 */
    663   1.1       agc static int
    664  1.71     pooka puffs_fuse_node_mknod(struct puffs_cc *pcc, void *opc,
    665  1.71     pooka 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
    666  1.71     pooka 	const struct vattr *va)
    667   1.1       agc {
    668   1.1       agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    669   1.1       agc 	struct fuse		*fuse;
    670  1.44     pooka 	mode_t			 mode;
    671   1.1       agc 	const char		*path = PCNPATH(pcn);
    672   1.1       agc 	int			ret;
    673   1.1       agc 
    674  1.46     pooka 	fuse = puffs_getspecific(pu);
    675   1.1       agc 	if (fuse->op.mknod == NULL) {
    676   1.1       agc 		return ENOSYS;
    677   1.1       agc 	}
    678   1.1       agc 
    679  1.69     pooka 	set_fuse_context_uid_gid(pcn->pcn_cred);
    680  1.62       agc 
    681   1.1       agc 	/* wrap up return code */
    682  1.44     pooka 	mode = puffs_addvtype2mode(va->va_mode, va->va_type);
    683   1.1       agc 	ret = (*fuse->op.mknod)(path, mode, va->va_rdev);
    684   1.1       agc 
    685   1.1       agc 	if (ret == 0) {
    686  1.71     pooka 		ret = fuse_newnode(pu, path, va, NULL, pni, NULL);
    687   1.1       agc 	}
    688   1.1       agc 
    689  1.18     pooka 	return -ret;
    690   1.1       agc }
    691   1.1       agc 
    692   1.1       agc /* make a directory */
    693   1.1       agc /* ARGSUSED1 */
    694   1.1       agc static int
    695  1.71     pooka puffs_fuse_node_mkdir(struct puffs_cc *pcc, void *opc,
    696  1.71     pooka 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
    697  1.71     pooka 	const struct vattr *va)
    698   1.1       agc {
    699   1.1       agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    700   1.1       agc 	struct fuse		*fuse;
    701   1.1       agc 	mode_t			 mode = va->va_mode;
    702   1.1       agc 	const char		*path = PCNPATH(pcn);
    703   1.1       agc 	int			ret;
    704   1.1       agc 
    705  1.46     pooka 	fuse = puffs_getspecific(pu);
    706  1.62       agc 
    707  1.69     pooka 	set_fuse_context_uid_gid(pcn->pcn_cred);
    708  1.62       agc 
    709   1.1       agc 	if (fuse->op.mkdir == NULL) {
    710   1.1       agc 		return ENOSYS;
    711   1.1       agc 	}
    712   1.1       agc 
    713   1.1       agc 	/* wrap up return code */
    714   1.1       agc 	ret = (*fuse->op.mkdir)(path, mode);
    715   1.1       agc 
    716   1.1       agc 	if (ret == 0) {
    717  1.71     pooka 		ret = fuse_newnode(pu, path, va, NULL, pni, NULL);
    718   1.1       agc 	}
    719   1.1       agc 
    720  1.18     pooka 	return -ret;
    721   1.1       agc }
    722   1.1       agc 
    723  1.21     pooka /*
    724  1.21     pooka  * create a regular file
    725  1.21     pooka  *
    726  1.21     pooka  * since linux/fuse sports using mknod for creating regular files
    727  1.21     pooka  * instead of having a separate call for it in some versions, if
    728  1.21     pooka  * we don't have create, just jump to op->mknod.
    729  1.21     pooka  */
    730  1.16     pooka /*ARGSUSED1*/
    731  1.16     pooka static int
    732  1.71     pooka puffs_fuse_node_create(struct puffs_cc *pcc, void *opc,
    733  1.71     pooka 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
    734  1.71     pooka 	const struct vattr *va)
    735  1.16     pooka {
    736  1.16     pooka 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    737  1.16     pooka 	struct fuse		*fuse;
    738  1.16     pooka 	struct fuse_file_info	fi;
    739  1.71     pooka 	struct puffs_node	*pn;
    740  1.16     pooka 	mode_t			mode = va->va_mode;
    741  1.16     pooka 	const char		*path = PCNPATH(pcn);
    742  1.31     pooka 	int			ret, created;
    743  1.16     pooka 
    744  1.46     pooka 	fuse = puffs_getspecific(pu);
    745  1.21     pooka 
    746  1.69     pooka 	set_fuse_context_uid_gid(pcn->pcn_cred);
    747  1.62       agc 
    748  1.31     pooka 	created = 0;
    749  1.21     pooka 	if (fuse->op.create) {
    750  1.21     pooka 		ret = fuse->op.create(path, mode, &fi);
    751  1.31     pooka 		if (ret == 0)
    752  1.31     pooka 			created = 1;
    753  1.21     pooka 
    754  1.21     pooka 	} else if (fuse->op.mknod) {
    755  1.21     pooka 		ret = fuse->op.mknod(path, mode | S_IFREG, 0);
    756  1.21     pooka 
    757  1.21     pooka 	} else {
    758  1.21     pooka 		ret = -ENOSYS;
    759  1.16     pooka 	}
    760  1.16     pooka 
    761  1.16     pooka 	if (ret == 0) {
    762  1.71     pooka 		ret = fuse_newnode(pu, path, va, &fi, pni, &pn);
    763  1.31     pooka 
    764  1.31     pooka 		/* sweet..  create also open the file */
    765  1.31     pooka 		if (created) {
    766  1.31     pooka 			struct refusenode *rn;
    767  1.31     pooka 
    768  1.31     pooka 			rn = pn->pn_data;
    769  1.31     pooka 			rn->flags |= RN_OPEN;
    770  1.31     pooka 			rn->opencount++;
    771  1.31     pooka 		}
    772  1.16     pooka 	}
    773  1.16     pooka 
    774  1.18     pooka 	return -ret;
    775  1.16     pooka }
    776  1.16     pooka 
    777   1.1       agc /* remove the directory entry */
    778  1.23     pooka /* ARGSUSED1 */
    779   1.1       agc static int
    780   1.1       agc puffs_fuse_node_remove(struct puffs_cc *pcc, void *opc, void *targ,
    781   1.1       agc 	const struct puffs_cn *pcn)
    782   1.1       agc {
    783   1.1       agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    784  1.23     pooka 	struct puffs_node	*pn_targ = targ;
    785   1.1       agc 	struct fuse		*fuse;
    786  1.23     pooka 	const char		*path = PNPATH(pn_targ);
    787   1.1       agc 	int			ret;
    788   1.1       agc 
    789  1.46     pooka 	fuse = puffs_getspecific(pu);
    790  1.62       agc 
    791  1.69     pooka 	set_fuse_context_uid_gid(pcn->pcn_cred);
    792  1.62       agc 
    793   1.1       agc 	if (fuse->op.unlink == NULL) {
    794   1.1       agc 		return ENOSYS;
    795   1.1       agc 	}
    796   1.1       agc 
    797   1.1       agc 	/* wrap up return code */
    798   1.1       agc 	ret = (*fuse->op.unlink)(path);
    799   1.1       agc 
    800  1.18     pooka 	return -ret;
    801   1.1       agc }
    802   1.1       agc 
    803   1.1       agc /* remove the directory */
    804   1.1       agc /* ARGSUSED1 */
    805   1.1       agc static int
    806   1.1       agc puffs_fuse_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
    807   1.1       agc 	const struct puffs_cn *pcn)
    808   1.1       agc {
    809   1.1       agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    810  1.23     pooka 	struct puffs_node	*pn_targ = targ;
    811   1.1       agc 	struct fuse		*fuse;
    812  1.23     pooka 	const char		*path = PNPATH(pn_targ);
    813   1.1       agc 	int			ret;
    814   1.1       agc 
    815  1.46     pooka 	fuse = puffs_getspecific(pu);
    816  1.62       agc 
    817  1.69     pooka 	set_fuse_context_uid_gid(pcn->pcn_cred);
    818  1.62       agc 
    819   1.1       agc 	if (fuse->op.rmdir == NULL) {
    820   1.1       agc 		return ENOSYS;
    821   1.1       agc 	}
    822   1.1       agc 
    823   1.1       agc 	/* wrap up return code */
    824   1.1       agc 	ret = (*fuse->op.rmdir)(path);
    825   1.1       agc 
    826  1.18     pooka 	return -ret;
    827   1.1       agc }
    828   1.1       agc 
    829   1.1       agc /* create a symbolic link */
    830   1.1       agc /* ARGSUSED1 */
    831   1.1       agc static int
    832  1.71     pooka puffs_fuse_node_symlink(struct puffs_cc *pcc, void *opc,
    833  1.71     pooka 	struct puffs_newinfo *pni, const struct puffs_cn *pcn_src,
    834  1.71     pooka 	const struct vattr *va, const char *link_target)
    835   1.1       agc {
    836   1.1       agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    837   1.1       agc 	struct fuse		*fuse;
    838   1.1       agc 	const char		*path = PCNPATH(pcn_src);
    839   1.1       agc 	int			ret;
    840   1.1       agc 
    841  1.46     pooka 	fuse = puffs_getspecific(pu);
    842  1.62       agc 
    843  1.69     pooka 	set_fuse_context_uid_gid(pcn_src->pcn_cred);
    844  1.62       agc 
    845   1.1       agc 	if (fuse->op.symlink == NULL) {
    846   1.1       agc 		return ENOSYS;
    847   1.1       agc 	}
    848   1.1       agc 
    849   1.1       agc 	/* wrap up return code */
    850  1.32     pooka 	ret = fuse->op.symlink(link_target, path);
    851   1.1       agc 
    852   1.1       agc 	if (ret == 0) {
    853  1.71     pooka 		ret = fuse_newnode(pu, path, va, NULL, pni, NULL);
    854   1.1       agc 	}
    855   1.1       agc 
    856  1.18     pooka 	return -ret;
    857   1.1       agc }
    858   1.1       agc 
    859   1.1       agc /* rename a directory entry */
    860   1.1       agc /* ARGSUSED1 */
    861   1.1       agc static int
    862   1.1       agc puffs_fuse_node_rename(struct puffs_cc *pcc, void *opc, void *src,
    863   1.1       agc 	const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
    864   1.1       agc 	const struct puffs_cn *pcn_targ)
    865   1.1       agc {
    866   1.1       agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    867   1.1       agc 	struct fuse		*fuse;
    868  1.24     pooka 	const char		*path_src = PCNPATH(pcn_src);
    869  1.24     pooka 	const char		*path_dest = PCNPATH(pcn_targ);
    870   1.1       agc 	int			ret;
    871   1.1       agc 
    872  1.46     pooka 	fuse = puffs_getspecific(pu);
    873  1.62       agc 
    874  1.69     pooka 	set_fuse_context_uid_gid(pcn_targ->pcn_cred);
    875  1.62       agc 
    876   1.1       agc 	if (fuse->op.rename == NULL) {
    877   1.1       agc 		return ENOSYS;
    878   1.1       agc 	}
    879   1.1       agc 
    880  1.24     pooka 	ret = fuse->op.rename(path_src, path_dest);
    881   1.1       agc 
    882   1.1       agc 	if (ret == 0) {
    883   1.1       agc 	}
    884   1.1       agc 
    885  1.18     pooka 	return -ret;
    886   1.1       agc }
    887   1.1       agc 
    888   1.1       agc /* create a link in the file system */
    889   1.1       agc /* ARGSUSED1 */
    890   1.1       agc static int
    891   1.1       agc puffs_fuse_node_link(struct puffs_cc *pcc, void *opc, void *targ,
    892   1.1       agc 	const struct puffs_cn *pcn)
    893   1.1       agc {
    894   1.1       agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    895   1.1       agc 	struct puffs_node	*pn = targ;
    896   1.1       agc 	struct fuse		*fuse;
    897   1.1       agc 	int			ret;
    898   1.1       agc 
    899  1.46     pooka 	fuse = puffs_getspecific(pu);
    900  1.62       agc 
    901  1.69     pooka 	set_fuse_context_uid_gid(pcn->pcn_cred);
    902  1.62       agc 
    903   1.1       agc 	if (fuse->op.link == NULL) {
    904   1.1       agc 		return ENOSYS;
    905   1.1       agc 	}
    906   1.1       agc 
    907   1.1       agc 	/* wrap up return code */
    908   1.1       agc 	ret = (*fuse->op.link)(PNPATH(pn), PCNPATH(pcn));
    909   1.1       agc 
    910  1.18     pooka 	return -ret;
    911   1.1       agc }
    912   1.1       agc 
    913   1.1       agc /*
    914  1.19     pooka  * fuse's regular interface provides chmod(), chown(), utimes()
    915  1.19     pooka  * and truncate() + some variations, so try to fit the square block
    916  1.19     pooka  * in the circle hole and the circle block .... something like that
    917   1.7     pooka  */
    918   1.1       agc /* ARGSUSED3 */
    919   1.1       agc static int
    920   1.1       agc puffs_fuse_node_setattr(struct puffs_cc *pcc, void *opc,
    921  1.70     pooka 	const struct vattr *va, const struct puffs_cred *pcr,
    922  1.70     pooka 	const struct puffs_cid *pcid)
    923   1.1       agc {
    924   1.1       agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    925   1.1       agc 	struct puffs_node	*pn = opc;
    926   1.1       agc 	struct fuse		*fuse;
    927   1.1       agc 	const char		*path = PNPATH(pn);
    928   1.1       agc 
    929  1.46     pooka 	fuse = puffs_getspecific(pu);
    930   1.1       agc 
    931  1.66       agc 	set_fuse_context_uid_gid(pcr);
    932  1.62       agc 
    933  1.26     pooka 	return fuse_setattr(fuse, pn, path, va);
    934   1.1       agc }
    935   1.1       agc 
    936   1.1       agc /* ARGSUSED2 */
    937   1.1       agc static int
    938  1.31     pooka puffs_fuse_node_open(struct puffs_cc *pcc, void *opc, int mode,
    939  1.70     pooka 	const struct puffs_cred *cred, const struct puffs_cid *pcid)
    940   1.1       agc {
    941   1.1       agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    942   1.1       agc 	struct puffs_node	*pn = opc;
    943   1.5     pooka 	struct refusenode	*rn = pn->pn_data;
    944  1.31     pooka 	struct fuse_file_info	*fi = &rn->file_info;
    945   1.1       agc 	struct fuse		*fuse;
    946   1.1       agc 	const char		*path = PNPATH(pn);
    947   1.1       agc 
    948  1.46     pooka 	fuse = puffs_getspecific(pu);
    949   1.1       agc 
    950  1.66       agc 	set_fuse_context_uid_gid(cred);
    951  1.62       agc 
    952  1.30     pooka 	/* if open, don't open again, lest risk nuking file private info */
    953  1.31     pooka 	if (rn->flags & RN_OPEN) {
    954  1.31     pooka 		rn->opencount++;
    955   1.1       agc 		return 0;
    956  1.31     pooka 	}
    957   1.1       agc 
    958  1.37     pooka 	/* OFLAGS(), need to convert FREAD/FWRITE to O_RD/WR */
    959  1.37     pooka 	fi->flags = (mode & ~(O_CREAT | O_EXCL | O_TRUNC)) - 1;
    960  1.37     pooka 
    961  1.30     pooka 	if (pn->pn_va.va_type == VDIR) {
    962  1.30     pooka 		if (fuse->op.opendir)
    963  1.33     pooka 			fuse->op.opendir(path, fi);
    964  1.30     pooka 	} else {
    965  1.30     pooka 		if (fuse->op.open)
    966  1.33     pooka 			fuse->op.open(path, fi);
    967  1.30     pooka 	}
    968   1.1       agc 
    969  1.33     pooka 	rn->flags |= RN_OPEN;
    970  1.33     pooka 	rn->opencount++;
    971   1.1       agc 
    972  1.33     pooka 	return 0;
    973   1.1       agc }
    974   1.1       agc 
    975  1.31     pooka /* ARGSUSED2 */
    976  1.31     pooka static int
    977  1.31     pooka puffs_fuse_node_close(struct puffs_cc *pcc, void *opc, int fflag,
    978  1.70     pooka 	const struct puffs_cred *pcr, const struct puffs_cid *pcid)
    979  1.31     pooka {
    980  1.31     pooka 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    981  1.31     pooka 	struct puffs_node	*pn = opc;
    982  1.31     pooka 	struct refusenode	*rn = pn->pn_data;
    983  1.31     pooka 	struct fuse		*fuse;
    984  1.31     pooka 	struct fuse_file_info	*fi;
    985  1.31     pooka 	const char		*path = PNPATH(pn);
    986  1.31     pooka 	int			ret;
    987  1.31     pooka 
    988  1.46     pooka 	fuse = puffs_getspecific(pu);
    989  1.31     pooka 	fi = &rn->file_info;
    990  1.31     pooka 	ret = 0;
    991  1.31     pooka 
    992  1.66       agc 	set_fuse_context_uid_gid(pcr);
    993  1.62       agc 
    994  1.31     pooka 	if (rn->flags & RN_OPEN) {
    995  1.31     pooka 		if (pn->pn_va.va_type == VDIR) {
    996  1.31     pooka 			if (fuse->op.releasedir)
    997  1.31     pooka 				ret = fuse->op.releasedir(path, fi);
    998  1.31     pooka 		} else {
    999  1.31     pooka 			if (fuse->op.release)
   1000  1.31     pooka 				ret = fuse->op.release(path, fi);
   1001  1.31     pooka 		}
   1002  1.31     pooka 	}
   1003  1.31     pooka 	rn->flags &= ~RN_OPEN;
   1004  1.31     pooka 	rn->opencount--;
   1005  1.31     pooka 
   1006  1.31     pooka 	return ret;
   1007  1.31     pooka }
   1008  1.31     pooka 
   1009   1.1       agc /* read some more from the file */
   1010   1.1       agc /* ARGSUSED5 */
   1011   1.1       agc static int
   1012   1.1       agc puffs_fuse_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
   1013   1.1       agc 	off_t offset, size_t *resid, const struct puffs_cred *pcr,
   1014   1.1       agc 	int ioflag)
   1015   1.1       agc {
   1016   1.1       agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
   1017   1.1       agc 	struct puffs_node	*pn = opc;
   1018   1.5     pooka 	struct refusenode	*rn = pn->pn_data;
   1019   1.1       agc 	struct fuse		*fuse;
   1020   1.1       agc 	const char		*path = PNPATH(pn);
   1021  1.25     pooka 	size_t			maxread;
   1022   1.1       agc 	int			ret;
   1023   1.1       agc 
   1024  1.46     pooka 	fuse = puffs_getspecific(pu);
   1025   1.1       agc 	if (fuse->op.read == NULL) {
   1026   1.1       agc 		return ENOSYS;
   1027   1.1       agc 	}
   1028   1.1       agc 
   1029  1.66       agc 	set_fuse_context_uid_gid(pcr);
   1030  1.62       agc 
   1031  1.25     pooka 	maxread = *resid;
   1032  1.26     pooka 	if (maxread > pn->pn_va.va_size - offset) {
   1033  1.26     pooka 		/*LINTED*/
   1034  1.25     pooka 		maxread = pn->pn_va.va_size - offset;
   1035  1.26     pooka 	}
   1036  1.25     pooka 	if (maxread == 0)
   1037  1.25     pooka 		return 0;
   1038  1.25     pooka 
   1039  1.25     pooka 	ret = (*fuse->op.read)(path, (char *)buf, maxread, offset,
   1040   1.7     pooka 	    &rn->file_info);
   1041   1.1       agc 
   1042   1.1       agc 	if (ret > 0) {
   1043   1.1       agc 		*resid -= ret;
   1044  1.16     pooka 		ret = 0;
   1045   1.1       agc 	}
   1046   1.1       agc 
   1047  1.16     pooka 	return -ret;
   1048   1.1       agc }
   1049   1.1       agc 
   1050   1.1       agc /* write to the file */
   1051   1.1       agc /* ARGSUSED0 */
   1052   1.1       agc static int
   1053   1.1       agc puffs_fuse_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
   1054   1.1       agc 	off_t offset, size_t *resid, const struct puffs_cred *pcr,
   1055   1.1       agc 	int ioflag)
   1056   1.1       agc {
   1057   1.1       agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
   1058   1.1       agc 	struct puffs_node	*pn = opc;
   1059   1.8     pooka 	struct refusenode	*rn = pn->pn_data;
   1060   1.1       agc 	struct fuse		*fuse;
   1061   1.1       agc 	const char		*path = PNPATH(pn);
   1062   1.1       agc 	int			ret;
   1063   1.1       agc 
   1064  1.46     pooka 	fuse = puffs_getspecific(pu);
   1065   1.1       agc 	if (fuse->op.write == NULL) {
   1066   1.1       agc 		return ENOSYS;
   1067   1.1       agc 	}
   1068   1.1       agc 
   1069  1.66       agc 	set_fuse_context_uid_gid(pcr);
   1070  1.62       agc 
   1071  1.17     pooka 	if (ioflag & PUFFS_IO_APPEND)
   1072  1.17     pooka 		offset = pn->pn_va.va_size;
   1073  1.17     pooka 
   1074   1.8     pooka 	ret = (*fuse->op.write)(path, (char *)buf, *resid, offset,
   1075   1.8     pooka 	    &rn->file_info);
   1076   1.1       agc 
   1077   1.1       agc 	if (ret > 0) {
   1078  1.23     pooka 		if (offset + ret > pn->pn_va.va_size)
   1079  1.23     pooka 			pn->pn_va.va_size = offset + ret;
   1080  1.26     pooka 		*resid -= ret;
   1081  1.16     pooka 		ret = 0;
   1082   1.1       agc 	}
   1083   1.1       agc 
   1084  1.16     pooka 	return -ret;
   1085   1.1       agc }
   1086   1.1       agc 
   1087   1.1       agc 
   1088   1.1       agc /* ARGSUSED3 */
   1089   1.1       agc static int
   1090  1.45     pooka puffs_fuse_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *dent,
   1091  1.45     pooka 	off_t *readoff, size_t *reslen, const struct puffs_cred *pcr,
   1092  1.45     pooka 	int *eofflag, off_t *cookies, size_t *ncookies)
   1093   1.1       agc {
   1094   1.1       agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
   1095   1.1       agc 	struct puffs_node	*pn = opc;
   1096   1.8     pooka 	struct refusenode	*rn = pn->pn_data;
   1097  1.43       agc 	struct puffs_fuse_dirh	*dirh;
   1098  1.43       agc 	struct fuse		*fuse;
   1099  1.41       agc 	struct dirent		*fromdent;
   1100   1.1       agc 	const char		*path = PNPATH(pn);
   1101   1.1       agc 	int			ret;
   1102   1.1       agc 
   1103  1.46     pooka 	fuse = puffs_getspecific(pu);
   1104   1.4     pooka 	if (fuse->op.readdir == NULL && fuse->op.getdir == NULL) {
   1105   1.1       agc 		return ENOSYS;
   1106   1.1       agc 	}
   1107   1.1       agc 
   1108  1.66       agc 	set_fuse_context_uid_gid(pcr);
   1109  1.62       agc 
   1110  1.36     pooka 	if (pn->pn_va.va_type != VDIR)
   1111  1.36     pooka 		return ENOTDIR;
   1112  1.36     pooka 
   1113  1.36     pooka 	dirh = &rn->dirh;
   1114  1.36     pooka 
   1115  1.36     pooka 	/*
   1116  1.36     pooka 	 * if we are starting from the beginning, slurp entire directory
   1117  1.36     pooka 	 * into our buffers
   1118  1.36     pooka 	 */
   1119  1.36     pooka 	if (*readoff == 0) {
   1120  1.36     pooka 		/* free old buffers */
   1121  1.36     pooka 		free(dirh->dbuf);
   1122  1.36     pooka 		memset(dirh, 0, sizeof(struct puffs_fuse_dirh));
   1123  1.36     pooka 
   1124  1.36     pooka 		if (fuse->op.readdir)
   1125  1.36     pooka 			ret = fuse->op.readdir(path, dirh, puffs_fuse_fill_dir,
   1126  1.36     pooka 			    0, &rn->file_info);
   1127  1.36     pooka 		else
   1128  1.36     pooka 			ret = fuse->op.getdir(path, dirh, puffs_fuse_dirfil);
   1129  1.36     pooka 		if (ret)
   1130  1.36     pooka 			return -ret;
   1131  1.35     pooka 	}
   1132  1.35     pooka 
   1133  1.36     pooka 	/* now, stuff results into the kernel buffers */
   1134  1.36     pooka 	while (*readoff < dirh->bufsize - dirh->reslen) {
   1135  1.36     pooka 		/*LINTED*/
   1136  1.36     pooka 		fromdent = (struct dirent *)((uint8_t *)dirh->dbuf + *readoff);
   1137  1.36     pooka 
   1138  1.36     pooka 		if (*reslen < _DIRENT_SIZE(fromdent))
   1139  1.36     pooka 			break;
   1140  1.36     pooka 
   1141  1.36     pooka 		memcpy(dent, fromdent, _DIRENT_SIZE(fromdent));
   1142  1.36     pooka 		*readoff += _DIRENT_SIZE(fromdent);
   1143  1.36     pooka 		*reslen -= _DIRENT_SIZE(fromdent);
   1144   1.1       agc 
   1145  1.36     pooka 		dent = _DIRENT_NEXT(dent);
   1146   1.1       agc 	}
   1147   1.1       agc 
   1148  1.36     pooka 	return 0;
   1149   1.1       agc }
   1150   1.1       agc 
   1151  1.26     pooka /* ARGSUSED */
   1152  1.26     pooka static int
   1153  1.70     pooka puffs_fuse_node_reclaim(struct puffs_cc *pcc, void *opc,
   1154  1.70     pooka 	const struct puffs_cid *pcid)
   1155  1.26     pooka {
   1156  1.26     pooka 	struct puffs_node	*pn = opc;
   1157   1.3     pooka 
   1158   1.5     pooka 	nukern(pn);
   1159  1.26     pooka 	return 0;
   1160   1.3     pooka }
   1161   1.3     pooka 
   1162   1.1       agc /* ARGSUSED1 */
   1163   1.1       agc static int
   1164  1.70     pooka puffs_fuse_fs_unmount(struct puffs_cc *pcc, int flags,
   1165  1.70     pooka 	const struct puffs_cid *pcid)
   1166   1.1       agc {
   1167   1.1       agc         struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
   1168   1.1       agc 	struct fuse		*fuse;
   1169   1.1       agc 
   1170  1.46     pooka 	fuse = puffs_getspecific(pu);
   1171   1.1       agc 	if (fuse->op.destroy == NULL) {
   1172   1.2     pooka 		return 0;
   1173   1.1       agc 	}
   1174   1.1       agc 	(*fuse->op.destroy)(fuse);
   1175   1.1       agc         return 0;
   1176   1.1       agc }
   1177   1.1       agc 
   1178   1.1       agc /* ARGSUSED0 */
   1179   1.1       agc static int
   1180   1.1       agc puffs_fuse_fs_sync(struct puffs_cc *pcc, int flags,
   1181  1.70     pooka             const struct puffs_cred *cr, const struct puffs_cid *pcid)
   1182   1.1       agc {
   1183  1.66       agc 	set_fuse_context_uid_gid(cr);
   1184   1.1       agc         return 0;
   1185   1.1       agc }
   1186   1.1       agc 
   1187   1.1       agc /* ARGSUSED2 */
   1188   1.1       agc static int
   1189  1.70     pooka puffs_fuse_fs_statvfs(struct puffs_cc *pcc, struct statvfs *svfsb,
   1190  1.70     pooka 	const struct puffs_cid *pcid)
   1191   1.1       agc {
   1192   1.1       agc         struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
   1193   1.1       agc 	struct fuse		*fuse;
   1194   1.1       agc 	int			ret;
   1195   1.1       agc 
   1196  1.46     pooka 	fuse = puffs_getspecific(pu);
   1197   1.1       agc 	if (fuse->op.statfs == NULL) {
   1198  1.46     pooka 		if ((ret = statvfs(PNPATH(puffs_getroot(pu)), svfsb)) == -1) {
   1199   1.1       agc 			return errno;
   1200   1.1       agc 		}
   1201   1.1       agc 	} else {
   1202  1.46     pooka 		ret = fuse->op.statfs(PNPATH(puffs_getroot(pu)), svfsb);
   1203   1.1       agc 	}
   1204   1.1       agc 
   1205   1.1       agc         return ret;
   1206   1.1       agc }
   1207   1.1       agc 
   1208   1.1       agc 
   1209   1.1       agc /* End of puffs_fuse operations */
   1210   1.1       agc /* ARGSUSED3 */
   1211   1.1       agc int
   1212   1.1       agc fuse_main_real(int argc, char **argv, const struct fuse_operations *ops,
   1213   1.1       agc 	size_t size, void *userdata)
   1214   1.1       agc {
   1215  1.51       agc 	struct fuse	*fuse;
   1216  1.51       agc 	char		*mountpoint;
   1217  1.51       agc 	int		 multithreaded;
   1218  1.51       agc 	int		 fd;
   1219  1.38     pooka 
   1220  1.51       agc 	fuse = fuse_setup(argc, argv, ops, size, &mountpoint, &multithreaded,
   1221  1.51       agc 			&fd);
   1222  1.38     pooka 
   1223  1.51       agc 	return fuse_loop(fuse);
   1224  1.38     pooka }
   1225  1.38     pooka 
   1226  1.38     pooka /*
   1227  1.38     pooka  * XXX: just defer the operation until fuse_new() when we have more
   1228  1.38     pooka  * info on our hands.  The real beef is why's this separate in fuse in
   1229  1.38     pooka  * the first place?
   1230  1.38     pooka  */
   1231  1.38     pooka /* ARGSUSED1 */
   1232  1.38     pooka struct fuse_chan *
   1233  1.38     pooka fuse_mount(const char *dir, struct fuse_args *args)
   1234  1.38     pooka {
   1235  1.48       agc  	struct fuse_chan	*fc;
   1236  1.53       agc 	char			 name[64];
   1237  1.38     pooka 
   1238  1.65       agc 	if ((fc = calloc(1, sizeof(*fc))) == NULL) {
   1239  1.65       agc 		err(EXIT_FAILURE, "fuse_mount");
   1240  1.65       agc 	}
   1241  1.60     pooka 	fc->dead = 0;
   1242  1.38     pooka 
   1243  1.65       agc 	if ((fc->dir = strdup(dir)) == NULL) {
   1244  1.65       agc 		err(EXIT_FAILURE, "fuse_mount");
   1245  1.65       agc 	}
   1246  1.48       agc 
   1247  1.48       agc 	/*
   1248  1.48       agc 	 * we need to deep copy the args struct - some fuse file
   1249  1.48       agc 	 * systems "clean up" the argument vector for "security
   1250  1.48       agc 	 * reasons"
   1251  1.48       agc 	 */
   1252  1.56  christos 	fc->args = fuse_opt_deep_copy_args(args->argc, args->argv);
   1253  1.53       agc 
   1254  1.53       agc 	if (args->argc > 0) {
   1255  1.53       agc 		set_refuse_mount_name(args->argv, name, sizeof(name));
   1256  1.55  christos 		if ((args->argv[0] = strdup(name)) == NULL)
   1257  1.55  christos 			err(1, "fuse_mount");
   1258  1.53       agc 	}
   1259  1.53       agc 
   1260  1.38     pooka 	return fc;
   1261  1.38     pooka }
   1262  1.38     pooka 
   1263  1.38     pooka /* ARGSUSED1 */
   1264  1.38     pooka struct fuse *
   1265  1.38     pooka fuse_new(struct fuse_chan *fc, struct fuse_args *args,
   1266  1.38     pooka 	const struct fuse_operations *ops, size_t size, void *userdata)
   1267  1.38     pooka {
   1268   1.1       agc 	struct puffs_usermount	*pu;
   1269  1.66       agc 	struct fuse_context	*fusectx;
   1270   1.1       agc 	struct puffs_pathobj	*po_root;
   1271  1.46     pooka 	struct puffs_node	*pn_root;
   1272  1.43       agc 	struct puffs_ops	*pops;
   1273  1.41       agc 	struct refusenode	*rn_root;
   1274  1.62       agc 	struct statvfs		 svfsb;
   1275  1.62       agc 	struct stat		 st;
   1276   1.1       agc 	struct fuse		*fuse;
   1277  1.62       agc 	extern int		 puffs_fakecc;
   1278  1.53       agc 	char			 name[64];
   1279  1.53       agc 	char			*argv0;
   1280  1.38     pooka 
   1281  1.65       agc 	if ((fuse = calloc(1, sizeof(*fuse))) == NULL) {
   1282  1.65       agc 		err(EXIT_FAILURE, "fuse_new");
   1283  1.65       agc 	}
   1284  1.38     pooka 
   1285  1.38     pooka 	/* copy fuse ops to their own stucture */
   1286  1.38     pooka 	(void) memcpy(&fuse->op, ops, sizeof(fuse->op));
   1287  1.38     pooka 
   1288  1.66       agc 	fusectx = fuse_get_context();
   1289  1.66       agc 	fusectx->fuse = fuse;
   1290  1.66       agc 	fusectx->uid = 0;
   1291  1.66       agc 	fusectx->gid = 0;
   1292  1.66       agc 	fusectx->pid = 0;
   1293  1.66       agc 	fusectx->private_data = userdata;
   1294  1.38     pooka 
   1295  1.38     pooka 	fuse->fc = fc;
   1296   1.1       agc 
   1297   1.1       agc 	/* initialise the puffs operations structure */
   1298   1.1       agc         PUFFSOP_INIT(pops);
   1299   1.1       agc 
   1300   1.1       agc         PUFFSOP_SET(pops, puffs_fuse, fs, sync);
   1301   1.1       agc         PUFFSOP_SET(pops, puffs_fuse, fs, statvfs);
   1302   1.1       agc         PUFFSOP_SET(pops, puffs_fuse, fs, unmount);
   1303   1.1       agc 
   1304   1.2     pooka 	/*
   1305   1.2     pooka 	 * XXX: all of these don't possibly need to be
   1306   1.2     pooka 	 * unconditionally set
   1307   1.2     pooka 	 */
   1308   1.1       agc         PUFFSOP_SET(pops, puffs_fuse, node, lookup);
   1309   1.1       agc         PUFFSOP_SET(pops, puffs_fuse, node, getattr);
   1310  1.15     pooka         PUFFSOP_SET(pops, puffs_fuse, node, setattr);
   1311   1.1       agc         PUFFSOP_SET(pops, puffs_fuse, node, readdir);
   1312   1.1       agc         PUFFSOP_SET(pops, puffs_fuse, node, readlink);
   1313   1.1       agc         PUFFSOP_SET(pops, puffs_fuse, node, mknod);
   1314  1.16     pooka         PUFFSOP_SET(pops, puffs_fuse, node, create);
   1315  1.15     pooka         PUFFSOP_SET(pops, puffs_fuse, node, remove);
   1316   1.1       agc         PUFFSOP_SET(pops, puffs_fuse, node, mkdir);
   1317   1.1       agc         PUFFSOP_SET(pops, puffs_fuse, node, rmdir);
   1318   1.1       agc         PUFFSOP_SET(pops, puffs_fuse, node, symlink);
   1319   1.1       agc         PUFFSOP_SET(pops, puffs_fuse, node, rename);
   1320   1.1       agc         PUFFSOP_SET(pops, puffs_fuse, node, link);
   1321   1.1       agc         PUFFSOP_SET(pops, puffs_fuse, node, open);
   1322  1.31     pooka         PUFFSOP_SET(pops, puffs_fuse, node, close);
   1323   1.1       agc         PUFFSOP_SET(pops, puffs_fuse, node, read);
   1324   1.1       agc         PUFFSOP_SET(pops, puffs_fuse, node, write);
   1325   1.3     pooka         PUFFSOP_SET(pops, puffs_fuse, node, reclaim);
   1326   1.1       agc 
   1327  1.53       agc 	argv0 = (*args->argv[0] == 0x0) ? fc->args->argv[0] : args->argv[0];
   1328  1.53       agc 	set_refuse_mount_name(&argv0, name, sizeof(name));
   1329  1.53       agc 
   1330  1.54     pooka 	puffs_fakecc = 1; /* XXX */
   1331  1.72     pooka 	pu = puffs_init(pops, _PATH_PUFFS, name, fuse,
   1332  1.38     pooka 			 PUFFS_FLAG_BUILDPATH
   1333  1.49     pooka 			   | PUFFS_FLAG_HASHPATH
   1334  1.68     pooka 			   | PUFFS_KFLAG_NOCACHE);
   1335   1.1       agc 	if (pu == NULL) {
   1336  1.57     pooka 		err(EXIT_FAILURE, "puffs_init");
   1337   1.1       agc 	}
   1338  1.38     pooka 	fc->pu = pu;
   1339   1.1       agc 
   1340  1.46     pooka 	pn_root = newrn(pu);
   1341  1.46     pooka 	puffs_setroot(pu, pn_root);
   1342  1.46     pooka 	rn_root = pn_root->pn_data;
   1343  1.30     pooka 	rn_root->flags |= RN_ROOT;
   1344  1.30     pooka 
   1345   1.1       agc 	po_root = puffs_getrootpathobj(pu);
   1346  1.55  christos 	if ((po_root->po_path = strdup("/")) == NULL)
   1347  1.55  christos 		err(1, "fuse_new");
   1348   1.1       agc 	po_root->po_len = 1;
   1349  1.59     pooka 	puffs_path_buildhash(pu, po_root);
   1350   1.1       agc 
   1351  1.25     pooka 	/* sane defaults */
   1352  1.46     pooka 	puffs_vattr_null(&pn_root->pn_va);
   1353  1.46     pooka 	pn_root->pn_va.va_type = VDIR;
   1354  1.46     pooka 	pn_root->pn_va.va_mode = 0755;
   1355  1.25     pooka 	if (fuse->op.getattr)
   1356  1.25     pooka 		if (fuse->op.getattr(po_root->po_path, &st) == 0)
   1357  1.46     pooka 			puffs_stat2vattr(&pn_root->pn_va, &st);
   1358  1.46     pooka 	assert(pn_root->pn_va.va_type == VDIR);
   1359  1.25     pooka 
   1360  1.11     pooka 	if (fuse->op.init)
   1361  1.66       agc 		fusectx->private_data = fuse->op.init(NULL); /* XXX */
   1362  1.11     pooka 
   1363  1.79     pooka 	puffs_set_prepost(pu, set_fuse_context_pid, NULL);
   1364  1.79     pooka 
   1365  1.38     pooka 	puffs_zerostatvfs(&svfsb);
   1366  1.57     pooka 	if (puffs_mount(pu, fc->dir, MNT_NODEV | MNT_NOSUID, pn_root) == -1) {
   1367  1.57     pooka 		err(EXIT_FAILURE, "puffs_mount: directory \"%s\"", fc->dir);
   1368   1.1       agc 	}
   1369   1.1       agc 
   1370  1.38     pooka 	return fuse;
   1371  1.38     pooka }
   1372  1.38     pooka 
   1373  1.38     pooka int
   1374  1.38     pooka fuse_loop(struct fuse *fuse)
   1375  1.38     pooka {
   1376  1.38     pooka 
   1377  1.75     pooka 	return puffs_mainloop(fuse->fc->pu, 0);
   1378  1.38     pooka }
   1379  1.38     pooka 
   1380  1.38     pooka void
   1381  1.38     pooka fuse_destroy(struct fuse *fuse)
   1382  1.38     pooka {
   1383  1.38     pooka 
   1384  1.80     pooka 	/*
   1385  1.80     pooka 	 * TODO: needs to assert the fs is quiescent, i.e. no other
   1386  1.80     pooka 	 * threads exist
   1387  1.80     pooka 	 */
   1388  1.80     pooka 
   1389  1.66       agc 	delete_context_key();
   1390  1.38     pooka 	/* XXXXXX: missing stuff */
   1391  1.55  christos 	free(fuse);
   1392   1.1       agc }
   1393   1.1       agc 
   1394  1.20       agc void
   1395  1.38     pooka fuse_exit(struct fuse *fuse)
   1396  1.20       agc {
   1397  1.20       agc 
   1398  1.60     pooka 	/* XXX: puffs_exit() is WRONG */
   1399  1.60     pooka 	if (fuse->fc->dead == 0)
   1400  1.60     pooka 		puffs_exit(fuse->fc->pu, 1);
   1401  1.60     pooka 	fuse->fc->dead = 1;
   1402  1.20       agc }
   1403  1.29     pooka 
   1404  1.29     pooka /*
   1405  1.29     pooka  * XXX: obviously not the most perfect of functions, but needs some
   1406  1.29     pooka  * puffs tweaking for a better tomorrow
   1407  1.29     pooka  */
   1408  1.31     pooka /*ARGSUSED*/
   1409  1.29     pooka void
   1410  1.38     pooka fuse_unmount(const char *mp, struct fuse_chan *fc)
   1411  1.38     pooka {
   1412  1.38     pooka 
   1413  1.60     pooka 	/* XXX: puffs_exit() is WRONG */
   1414  1.60     pooka 	if (fc->dead == 0)
   1415  1.60     pooka 		puffs_exit(fc->pu, 1);
   1416  1.60     pooka 	fc->dead = 1;
   1417  1.38     pooka }
   1418  1.38     pooka 
   1419  1.38     pooka /*ARGSUSED*/
   1420  1.38     pooka void
   1421  1.38     pooka fuse_unmount_compat22(const char *mp)
   1422  1.29     pooka {
   1423  1.29     pooka 
   1424  1.29     pooka 	return;
   1425  1.29     pooka }
   1426  1.51       agc 
   1427  1.51       agc /* The next function "exposes" struct fuse to userland.  Not much
   1428  1.51       agc * that we can do about this, as we're conforming to a defined
   1429  1.51       agc * interface.  */
   1430  1.51       agc 
   1431  1.51       agc void
   1432  1.51       agc fuse_teardown(struct fuse *fuse, char *mountpoint)
   1433  1.51       agc {
   1434  1.51       agc 	fuse_unmount(mountpoint, fuse->fc);
   1435  1.51       agc 	fuse_destroy(fuse);
   1436  1.51       agc }
   1437