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