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