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