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