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