Home | History | Annotate | Line # | Download | only in librefuse
refuse.c revision 1.19
      1  1.19  pooka /*	$NetBSD: refuse.c,v 1.19 2007/02/18 00:01:18 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.19  pooka __RCSID("$NetBSD: refuse.c,v 1.19 2007/02/18 00:01:18 pooka Exp $");
     34   1.7  pooka #endif /* !lint */
     35   1.7  pooka 
     36   1.1    agc #include <err.h>
     37   1.1    agc #include <errno.h>
     38   1.1    agc #include <fuse.h>
     39   1.1    agc #include <ucontext.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.1    agc /* this is the private fuse structure */
     70   1.1    agc struct fuse {
     71   1.1    agc 	struct fuse_session	*se;		/* fuse session pointer */
     72   1.1    agc 	struct fuse_operations	op;		/* switch table of operations */
     73   1.7  pooka 	int			compat;		/* compat level -
     74   1.7  pooka 						 * not used in puffs_fuse */
     75   1.1    agc 	struct node		**name_table;
     76   1.1    agc 	size_t			name_table_size;
     77   1.1    agc 	struct node		**id_table;
     78   1.1    agc 	size_t			id_table_size;
     79   1.1    agc 	fuse_ino_t		ctr;
     80   1.1    agc 	unsigned int		generation;
     81   1.1    agc 	unsigned int		hidectr;
     82   1.1    agc 	pthread_mutex_t		lock;
     83   1.1    agc 	pthread_rwlock_t	tree_lock;
     84   1.1    agc 	void			*user_data;
     85   1.1    agc 	struct fuse_config	conf;
     86   1.1    agc 	int			intr_installed;
     87   1.1    agc 	struct puffs_usermount	*pu;
     88   1.1    agc };
     89   1.1    agc 
     90   1.5  pooka struct refusenode {
     91   1.5  pooka 	struct fuse_file_info	 file_info;
     92   1.5  pooka };
     93   1.5  pooka 
     94   1.5  pooka static struct puffs_node *
     95   1.5  pooka newrn(struct puffs_usermount *pu)
     96   1.5  pooka {
     97   1.5  pooka 	struct puffs_node *pn;
     98   1.5  pooka 	struct refusenode *rn;
     99   1.5  pooka 
    100   1.5  pooka 	rn = malloc(sizeof(struct refusenode));
    101   1.5  pooka 	if (!rn)
    102   1.5  pooka 		abort(); /*XXX*/
    103   1.5  pooka 
    104   1.5  pooka 	memset(rn, 0, sizeof(struct refusenode));
    105   1.5  pooka 	pn = puffs_pn_new(pu, rn);
    106   1.5  pooka 
    107   1.5  pooka 	return pn;
    108   1.5  pooka }
    109   1.5  pooka 
    110   1.5  pooka static void
    111   1.5  pooka nukern(struct puffs_node *pn)
    112   1.5  pooka {
    113   1.5  pooka 
    114   1.5  pooka 	free(pn->pn_data);
    115   1.5  pooka 	puffs_pn_put(pn);
    116   1.5  pooka }
    117   1.5  pooka 
    118   1.1    agc static ino_t fakeino = 3;
    119   1.1    agc 
    120   1.4  pooka /* XXX: rethinkme */
    121   1.4  pooka struct fuse_dirh {
    122   1.1    agc 	struct dirent *dent;
    123   1.1    agc 	size_t reslen;
    124   1.4  pooka 	off_t readoff;
    125   1.1    agc };
    126   1.1    agc 
    127   1.1    agc /* ARGSUSED2 */
    128   1.1    agc static int
    129   1.4  pooka puffs_fuse_fill_dir(void *buf, const char *name,
    130   1.1    agc 	const struct stat *stbuf, off_t off)
    131   1.1    agc {
    132   1.4  pooka 	struct fuse_dirh *deh = buf;
    133  1.13  pooka 	ino_t dino;
    134   1.1    agc 	uint8_t dtype;
    135   1.1    agc 
    136  1.13  pooka 	if (stbuf == NULL) {
    137  1.13  pooka 		dtype = DT_UNKNOWN;
    138  1.13  pooka 		dino = fakeino++;
    139  1.13  pooka 	} else {
    140  1.13  pooka 		dtype = puffs_vtype2dt(puffs_mode2vt(stbuf->st_mode));
    141  1.13  pooka 		dino = stbuf->st_ino;
    142  1.13  pooka 	}
    143   1.1    agc 
    144  1.13  pooka 	return !puffs_nextdent(&deh->dent, name, dino, dtype, &deh->reslen);
    145   1.4  pooka }
    146   1.4  pooka 
    147   1.4  pooka static int
    148   1.4  pooka puffs_fuse_dirfil(fuse_dirh_t h, const char *name, int type, ino_t ino)
    149   1.4  pooka {
    150   1.4  pooka 	ino_t dino;
    151   1.4  pooka 	int dtype;
    152   1.4  pooka 
    153  1.13  pooka 	if (type == 0)
    154  1.13  pooka 		dtype = DT_UNKNOWN;
    155  1.13  pooka 	else
    156   1.4  pooka 		dtype = type;
    157   1.4  pooka 
    158   1.4  pooka 	if (ino)
    159   1.4  pooka 		dino = ino;
    160   1.4  pooka 	else
    161   1.4  pooka 		dino = fakeino++;
    162   1.4  pooka 
    163   1.6  pooka 	return !puffs_nextdent(&h->dent, name, dino, dtype, &h->reslen);
    164   1.1    agc }
    165   1.1    agc 
    166   1.1    agc int
    167   1.1    agc fuse_opt_add_arg(struct fuse_args *args, const char *arg)
    168   1.1    agc {
    169   1.1    agc 	char	**oldargv;
    170   1.1    agc 	int	oldargc;
    171   1.1    agc 
    172   1.1    agc 	if (args->allocated) {
    173   1.7  pooka 		RENEW(char *, args->argv, args->argc + 1,
    174   1.7  pooka 		    "fuse_opt_add_arg1", return 0);
    175   1.1    agc 	} else {
    176   1.1    agc 		oldargv = args->argv;
    177   1.1    agc 		oldargc = args->argc;
    178   1.7  pooka 		NEWARRAY(char *, args->argv, oldargc + 1,
    179   1.7  pooka 		    "fuse_opt_add_arg2", return 0);
    180   1.1    agc 		(void) memcpy(args->argv, oldargv, oldargc * sizeof(char *));
    181   1.1    agc 		args->allocated = 1;
    182   1.1    agc 	}
    183   1.1    agc 	args->argv[args->argc++] = strdup(arg);
    184   1.1    agc 	return 1;
    185   1.1    agc }
    186   1.1    agc 
    187  1.18  pooka #define FUSE_ERR_UNLINK(fuse, file) if (fuse->op.unlink) fuse->op.unlink(file)
    188  1.18  pooka #define FUSE_ERR_RMDIR(fuse, dir) if (fuse->op.rmdir) fuse->op.rmdir(dir)
    189  1.18  pooka 
    190   1.1    agc /* operation wrappers start here */
    191   1.1    agc 
    192   1.1    agc /* lookup the path */
    193   1.1    agc /* ARGSUSED1 */
    194   1.1    agc static int
    195   1.1    agc puffs_fuse_node_lookup(struct puffs_cc *pcc, void *opc, void **newnode,
    196   1.1    agc 	enum vtype *newtype, voff_t *newsize, dev_t *newrdev,
    197   1.1    agc 	const struct puffs_cn *pcn)
    198   1.1    agc {
    199   1.1    agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    200  1.12  pooka 	struct puffs_node	*pn_res;
    201   1.1    agc 	struct stat		st;
    202   1.1    agc 	struct fuse		*fuse;
    203   1.1    agc 	const char		*path = PCNPATH(pcn);
    204   1.1    agc 	int			ret;
    205   1.1    agc 
    206   1.1    agc 	fuse = (struct fuse *)pu->pu_privdata;
    207   1.1    agc 	ret = fuse->op.getattr(path, &st);
    208  1.12  pooka 
    209   1.1    agc 	if (ret != 0) {
    210  1.18  pooka 		return -ret;
    211  1.12  pooka 	}
    212  1.12  pooka 
    213  1.12  pooka 	/* XXX: fiXXXme unconst */
    214  1.12  pooka 	pn_res = puffs_pn_nodewalk(pu, puffs_path_walkcmp,
    215  1.12  pooka 	    __UNCONST(&pcn->pcn_po_full));
    216  1.12  pooka 	if (pn_res == NULL) {
    217  1.12  pooka 		pn_res = newrn(pu);
    218  1.12  pooka 		if (pn_res == NULL)
    219  1.12  pooka 			return errno;
    220  1.12  pooka 		puffs_stat2vattr(&pn_res->pn_va, &st);
    221   1.1    agc 	}
    222  1.12  pooka 
    223  1.12  pooka 	*newnode = pn_res;
    224  1.12  pooka 	*newtype = pn_res->pn_va.va_type;
    225  1.12  pooka 	*newsize = pn_res->pn_va.va_size;
    226  1.12  pooka 	*newrdev = pn_res->pn_va.va_rdev;
    227  1.12  pooka 
    228  1.12  pooka 	return 0;
    229   1.1    agc }
    230   1.1    agc 
    231   1.1    agc /* get attributes for the path name */
    232   1.1    agc /* ARGSUSED3 */
    233   1.1    agc static int
    234   1.1    agc puffs_fuse_node_getattr(struct puffs_cc *pcc, void *opc, struct vattr *va,
    235   1.1    agc 	const struct puffs_cred *pcr, pid_t pid)
    236   1.1    agc {
    237   1.1    agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    238   1.1    agc 	struct puffs_node	*pn = opc;
    239   1.1    agc 	struct stat		 st;
    240   1.1    agc 	struct fuse		*fuse;
    241   1.1    agc 	const char		*path = PNPATH(pn);
    242   1.1    agc 	int			ret;
    243   1.1    agc 
    244   1.1    agc 	fuse = (struct fuse *)pu->pu_privdata;
    245   1.1    agc 	if (fuse->op.getattr == NULL) {
    246   1.1    agc 		return ENOSYS;
    247   1.1    agc 	}
    248   1.1    agc 
    249   1.1    agc 	/* wrap up return code */
    250   1.1    agc 	ret = (*fuse->op.getattr)(path, &st);
    251   1.1    agc 
    252   1.1    agc 	if (ret == 0) {
    253   1.1    agc 		/* fill in va from st */
    254   1.1    agc 		va->va_mode = st.st_mode;
    255   1.1    agc 		va->va_nlink = st.st_nlink;
    256   1.1    agc 		va->va_uid = st.st_uid;
    257   1.1    agc 		va->va_gid = st.st_gid;
    258   1.1    agc 		va->va_fsid = st.st_rdev;
    259   1.1    agc 		va->va_fileid = st.st_ino;
    260   1.1    agc 		va->va_size = st.st_size;
    261   1.1    agc 		va->va_blocksize = st.st_blksize;
    262   1.1    agc 		va->va_atime = st.st_atimespec;
    263   1.1    agc 		va->va_mtime = st.st_mtimespec;
    264   1.1    agc 		va->va_ctime = st.st_ctimespec;
    265   1.1    agc 		va->va_birthtime = st.st_birthtimespec;
    266   1.1    agc 		va->va_gen = st.st_gen;
    267   1.1    agc 		va->va_flags = st.st_flags;
    268   1.1    agc 		va->va_rdev = st.st_rdev;
    269   1.1    agc 		va->va_bytes = st.st_size;
    270   1.1    agc 		va->va_filerev = st.st_gen;
    271   1.1    agc 		va->va_vaflags = st.st_flags;
    272   1.1    agc 
    273   1.1    agc 	}
    274   1.1    agc 
    275  1.18  pooka 	return -ret;
    276   1.1    agc }
    277   1.1    agc 
    278   1.1    agc /* read the contents of the symbolic link */
    279   1.1    agc /* ARGSUSED2 */
    280   1.1    agc static int
    281   1.1    agc puffs_fuse_node_readlink(struct puffs_cc *pcc, void *opc,
    282   1.1    agc 	const struct puffs_cred *cred, char *linkname, size_t *linklen)
    283   1.1    agc {
    284   1.1    agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    285   1.1    agc 	struct puffs_node	*pn = opc;
    286   1.1    agc 	struct fuse		*fuse;
    287  1.13  pooka 	const char		*path = PNPATH(pn), *p;
    288   1.1    agc 	int			ret;
    289   1.1    agc 
    290   1.1    agc 	fuse = (struct fuse *)pu->pu_privdata;
    291   1.1    agc 	if (fuse->op.readlink == NULL) {
    292   1.1    agc 		return ENOSYS;
    293   1.1    agc 	}
    294   1.1    agc 
    295   1.1    agc 	/* wrap up return code */
    296   1.1    agc 	ret = (*fuse->op.readlink)(path, linkname, *linklen);
    297   1.1    agc 
    298   1.1    agc 	if (ret == 0) {
    299  1.13  pooka 		p = memchr(linkname, '\0', *linklen);
    300  1.13  pooka 		if (!p)
    301  1.13  pooka 			return EINVAL;
    302  1.13  pooka 
    303  1.14  pooka 		*linklen = p - linkname;
    304   1.1    agc 	}
    305   1.1    agc 
    306  1.13  pooka 	return -ret;
    307   1.1    agc }
    308   1.1    agc 
    309   1.1    agc /* make the special node */
    310   1.1    agc /* ARGSUSED1 */
    311   1.1    agc static int
    312   1.1    agc puffs_fuse_node_mknod(struct puffs_cc *pcc, void *opc, void **newnode,
    313   1.1    agc 	const struct puffs_cn *pcn, const struct vattr *va)
    314   1.1    agc {
    315   1.1    agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    316   1.1    agc 	struct puffs_node	*pn;
    317   1.1    agc 	struct fuse		*fuse;
    318   1.1    agc 	mode_t			 mode = va->va_mode;
    319   1.1    agc 	const char		*path = PCNPATH(pcn);
    320   1.1    agc 	int			ret;
    321   1.1    agc 
    322   1.1    agc 	fuse = (struct fuse *)pu->pu_privdata;
    323   1.1    agc 	if (fuse->op.mknod == NULL) {
    324   1.1    agc 		return ENOSYS;
    325   1.1    agc 	}
    326   1.1    agc 
    327   1.1    agc 	/* wrap up return code */
    328   1.1    agc 	ret = (*fuse->op.mknod)(path, mode, va->va_rdev);
    329   1.1    agc 
    330   1.1    agc 	if (ret == 0) {
    331   1.1    agc 		/* fix up nodes */
    332   1.5  pooka 		pn = newrn(pu);
    333   1.1    agc 		if (pn == NULL) {
    334  1.18  pooka 			FUSE_ERR_UNLINK(fuse, path);
    335   1.1    agc 			return ENOMEM;
    336   1.1    agc 		}
    337   1.1    agc 		puffs_setvattr(&pn->pn_va, va);
    338   1.1    agc 
    339   1.1    agc 		*newnode = pn;
    340   1.1    agc 	}
    341   1.1    agc 
    342  1.18  pooka 	return -ret;
    343   1.1    agc }
    344   1.1    agc 
    345   1.1    agc /* make a directory */
    346   1.1    agc /* ARGSUSED1 */
    347   1.1    agc static int
    348   1.1    agc puffs_fuse_node_mkdir(struct puffs_cc *pcc, void *opc, void **newnode,
    349   1.1    agc 	const struct puffs_cn *pcn, const struct vattr *va)
    350   1.1    agc {
    351   1.1    agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    352   1.1    agc 	struct puffs_node	*pn;
    353   1.1    agc 	struct fuse		*fuse;
    354   1.1    agc 	mode_t			 mode = va->va_mode;
    355   1.1    agc 	const char		*path = PCNPATH(pcn);
    356   1.1    agc 	int			ret;
    357   1.1    agc 
    358   1.1    agc 	fuse = (struct fuse *)pu->pu_privdata;
    359   1.1    agc 	if (fuse->op.mkdir == NULL) {
    360   1.1    agc 		return ENOSYS;
    361   1.1    agc 	}
    362   1.1    agc 
    363   1.1    agc 	/* wrap up return code */
    364   1.1    agc 	ret = (*fuse->op.mkdir)(path, mode);
    365   1.1    agc 
    366   1.1    agc 	if (ret == 0) {
    367   1.1    agc 		/* fix up nodes */
    368   1.5  pooka 		pn = newrn(pu);
    369   1.1    agc 		if (pn == NULL) {
    370  1.18  pooka 			FUSE_ERR_RMDIR(fuse, path);
    371   1.1    agc 			return ENOMEM;
    372   1.1    agc 		}
    373   1.1    agc 		puffs_setvattr(&pn->pn_va, va);
    374   1.1    agc 
    375   1.1    agc 		*newnode = pn;
    376   1.1    agc 	}
    377   1.1    agc 
    378  1.18  pooka 	return -ret;
    379   1.1    agc }
    380   1.1    agc 
    381  1.16  pooka /* create a regular file */
    382  1.16  pooka /*ARGSUSED1*/
    383  1.16  pooka static int
    384  1.16  pooka puffs_fuse_node_create(struct puffs_cc *pcc, void *opc, void **newnode,
    385  1.16  pooka 	const struct puffs_cn *pcn, const struct vattr *va)
    386  1.16  pooka {
    387  1.16  pooka 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    388  1.16  pooka 	struct puffs_node	*pn;
    389  1.16  pooka 	struct refusenode	*rn;
    390  1.16  pooka 	struct fuse		*fuse;
    391  1.16  pooka 	struct fuse_file_info	fi;
    392  1.16  pooka 	mode_t			mode = va->va_mode;
    393  1.16  pooka 	const char		*path = PCNPATH(pcn);
    394  1.16  pooka 	int			ret;
    395  1.16  pooka 
    396  1.16  pooka 	fuse = (struct fuse *)pu->pu_privdata;
    397  1.16  pooka 	if (fuse->op.create == NULL) {
    398  1.16  pooka 		return ENOSYS;
    399  1.16  pooka 	}
    400  1.16  pooka 
    401  1.16  pooka 	/* wrap up return code */
    402  1.16  pooka 	ret = (*fuse->op.create)(path, mode, &fi);
    403  1.16  pooka 
    404  1.16  pooka 	if (ret == 0) {
    405  1.16  pooka 		/* fix up nodes */
    406  1.16  pooka 		pn = newrn(pu);
    407  1.16  pooka 		if (pn == NULL) {
    408  1.18  pooka 			FUSE_ERR_UNLINK(fuse, path);
    409  1.16  pooka 			return ENOMEM;
    410  1.16  pooka 		}
    411  1.16  pooka 		puffs_setvattr(&pn->pn_va, va);
    412  1.16  pooka 
    413  1.16  pooka 		rn = pn->pn_data;
    414  1.16  pooka 		memcpy(&rn->file_info, &fi, sizeof(struct fuse_file_info));
    415  1.16  pooka 
    416  1.16  pooka 		*newnode = pn;
    417  1.16  pooka 	}
    418  1.16  pooka 
    419  1.18  pooka 	return -ret;
    420  1.16  pooka }
    421  1.16  pooka 
    422   1.1    agc /* remove the directory entry */
    423   1.1    agc /* ARGSUSED1 */
    424   1.1    agc static int
    425   1.1    agc puffs_fuse_node_remove(struct puffs_cc *pcc, void *opc, void *targ,
    426   1.1    agc 	const struct puffs_cn *pcn)
    427   1.1    agc {
    428   1.1    agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    429   1.1    agc 	struct fuse		*fuse;
    430   1.1    agc 	const char		*path = PCNPATH(pcn);
    431   1.1    agc 	int			ret;
    432   1.1    agc 
    433   1.1    agc 	fuse = (struct fuse *)pu->pu_privdata;
    434   1.1    agc 	if (fuse->op.unlink == NULL) {
    435   1.1    agc 		return ENOSYS;
    436   1.1    agc 	}
    437   1.1    agc 
    438   1.1    agc 	/* wrap up return code */
    439   1.1    agc 	ret = (*fuse->op.unlink)(path);
    440   1.1    agc 
    441  1.18  pooka 	return -ret;
    442   1.1    agc }
    443   1.1    agc 
    444   1.1    agc /* remove the directory */
    445   1.1    agc /* ARGSUSED1 */
    446   1.1    agc static int
    447   1.1    agc puffs_fuse_node_rmdir(struct puffs_cc *pcc, void *opc, void *targ,
    448   1.1    agc 	const struct puffs_cn *pcn)
    449   1.1    agc {
    450   1.1    agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    451   1.1    agc 	struct fuse		*fuse;
    452   1.1    agc 	const char		*path = PCNPATH(pcn);
    453   1.1    agc 	int			ret;
    454   1.1    agc 
    455   1.1    agc 	fuse = (struct fuse *)pu->pu_privdata;
    456   1.1    agc 	if (fuse->op.rmdir == NULL) {
    457   1.1    agc 		return ENOSYS;
    458   1.1    agc 	}
    459   1.1    agc 
    460   1.1    agc 	/* wrap up return code */
    461   1.1    agc 	ret = (*fuse->op.rmdir)(path);
    462   1.1    agc 
    463  1.18  pooka 	return -ret;
    464   1.1    agc }
    465   1.1    agc 
    466   1.1    agc /* create a symbolic link */
    467   1.1    agc /* ARGSUSED1 */
    468   1.1    agc static int
    469   1.1    agc puffs_fuse_node_symlink(struct puffs_cc *pcc, void *opc, void **newnode,
    470   1.1    agc 	const struct puffs_cn *pcn_src, const struct vattr *va,
    471   1.1    agc 	const char *link_target)
    472   1.1    agc {
    473   1.1    agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    474   1.1    agc 	struct puffs_node	*pn;
    475   1.1    agc 	struct fuse		*fuse;
    476   1.1    agc 	const char		*path = PCNPATH(pcn_src);
    477   1.1    agc 	int			ret;
    478   1.1    agc 
    479   1.1    agc 	fuse = (struct fuse *)pu->pu_privdata;
    480   1.1    agc 	if (fuse->op.symlink == NULL) {
    481   1.1    agc 		return ENOSYS;
    482   1.1    agc 	}
    483   1.1    agc 
    484   1.1    agc 	/* wrap up return code */
    485   1.1    agc 	ret = (*fuse->op.symlink)(path, link_target);
    486   1.1    agc 	/* XXX - check I haven't transposed these args */
    487   1.1    agc 
    488   1.1    agc 	if (ret == 0) {
    489   1.1    agc 		/* fix up nodes */
    490   1.5  pooka 		pn = newrn(pu);
    491   1.1    agc 		if (pn == NULL) {
    492  1.18  pooka 			FUSE_ERR_UNLINK(fuse, path);
    493   1.1    agc 			return ENOMEM;
    494   1.1    agc 		}
    495   1.1    agc 		puffs_setvattr(&pn->pn_va, va);
    496   1.1    agc 
    497   1.1    agc 		*newnode = pn;
    498   1.1    agc 	}
    499   1.1    agc 
    500  1.18  pooka 	return -ret;
    501   1.1    agc }
    502   1.1    agc 
    503   1.1    agc /* rename a directory entry */
    504   1.1    agc /* ARGSUSED1 */
    505   1.1    agc static int
    506   1.1    agc puffs_fuse_node_rename(struct puffs_cc *pcc, void *opc, void *src,
    507   1.1    agc 	const struct puffs_cn *pcn_src, void *targ_dir, void *targ,
    508   1.1    agc 	const struct puffs_cn *pcn_targ)
    509   1.1    agc {
    510   1.1    agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    511   1.1    agc 	struct puffs_node	*pn = opc;
    512   1.1    agc 	struct vattr		va;
    513   1.1    agc 	struct fuse		*fuse;
    514   1.1    agc 	const char		*path = PCNPATH(pcn_src);
    515   1.1    agc 	int			ret;
    516   1.1    agc 
    517   1.1    agc 	fuse = (struct fuse *)pu->pu_privdata;
    518   1.1    agc 	if (fuse->op.rename == NULL) {
    519   1.1    agc 		return ENOSYS;
    520   1.1    agc 	}
    521   1.1    agc 
    522   1.1    agc 	/* wrap up return code */
    523   1.1    agc 	ret = (*fuse->op.rename)(path, PCNPATH(pcn_targ));
    524   1.1    agc 
    525   1.5  pooka 	/* XXX: what's this guy doing??? */
    526   1.1    agc 	if (ret == 0) {
    527   1.1    agc 		(void) memcpy(&va, &pn->pn_va, sizeof(va));
    528   1.1    agc 
    529   1.1    agc 		puffs_pn_put(pn);
    530   1.1    agc 
    531   1.1    agc 		pn = puffs_pn_new(pu, NULL);
    532   1.1    agc 		if (pn == NULL) {
    533   1.1    agc 			return ENOMEM;
    534   1.1    agc 		}
    535   1.1    agc 		puffs_setvattr(&pn->pn_va, &va);
    536   1.1    agc 
    537   1.1    agc 	}
    538   1.1    agc 
    539  1.18  pooka 	return -ret;
    540   1.1    agc }
    541   1.1    agc 
    542   1.1    agc /* create a link in the file system */
    543   1.1    agc /* ARGSUSED1 */
    544   1.1    agc static int
    545   1.1    agc puffs_fuse_node_link(struct puffs_cc *pcc, void *opc, void *targ,
    546   1.1    agc 	const struct puffs_cn *pcn)
    547   1.1    agc {
    548   1.1    agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    549   1.1    agc 	struct puffs_node	*pn = targ;
    550   1.1    agc 	struct fuse		*fuse;
    551   1.1    agc 	int			ret;
    552   1.1    agc 
    553   1.1    agc 	fuse = (struct fuse *)pu->pu_privdata;
    554   1.1    agc 	if (fuse->op.link == NULL) {
    555   1.1    agc 		return ENOSYS;
    556   1.1    agc 	}
    557   1.1    agc 
    558   1.1    agc 	/* wrap up return code */
    559   1.1    agc 	ret = (*fuse->op.link)(PNPATH(pn), PCNPATH(pcn));
    560   1.1    agc 
    561  1.18  pooka 	return -ret;
    562   1.1    agc }
    563   1.1    agc 
    564   1.1    agc /*
    565  1.19  pooka  * fuse's regular interface provides chmod(), chown(), utimes()
    566  1.19  pooka  * and truncate() + some variations, so try to fit the square block
    567  1.19  pooka  * in the circle hole and the circle block .... something like that
    568   1.7  pooka  */
    569   1.1    agc /* ARGSUSED3 */
    570   1.1    agc static int
    571   1.1    agc puffs_fuse_node_setattr(struct puffs_cc *pcc, void *opc,
    572   1.1    agc 	const struct vattr *va, const struct puffs_cred *pcr, pid_t pid)
    573   1.1    agc {
    574   1.1    agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    575   1.1    agc 	struct puffs_node	*pn = opc;
    576  1.19  pooka 	struct refusenode	*rn = pn->pn_data;
    577   1.1    agc 	struct fuse		*fuse;
    578   1.1    agc 	const char		*path = PNPATH(pn);
    579   1.1    agc 	mode_t			mode;
    580   1.1    agc 	uid_t			uid;
    581   1.1    agc 	gid_t			gid;
    582  1.19  pooka 	int			error, ret;
    583   1.1    agc 
    584   1.1    agc 	fuse = (struct fuse *)pu->pu_privdata;
    585   1.1    agc 
    586  1.19  pooka 	error = 0;
    587   1.1    agc 
    588   1.1    agc 	mode = va->va_mode;
    589   1.1    agc 	uid = va->va_uid;
    590   1.1    agc 	gid = va->va_gid;
    591   1.1    agc 
    592   1.9  pooka 	if (mode != (mode_t)PUFFS_VNOVAL) {
    593  1.19  pooka 		ret = 0;
    594  1.19  pooka 
    595   1.1    agc 		if (fuse->op.chmod == NULL) {
    596  1.19  pooka 			error = -ENOSYS;
    597  1.19  pooka 		} else {
    598  1.19  pooka 			ret = fuse->op.chmod(path, mode);
    599  1.19  pooka 			if (ret)
    600  1.19  pooka 				error = ret;
    601   1.1    agc 		}
    602   1.1    agc 	}
    603   1.9  pooka 	if (uid != (uid_t)PUFFS_VNOVAL || gid != (gid_t)PUFFS_VNOVAL) {
    604  1.19  pooka 		ret = 0;
    605  1.19  pooka 
    606   1.1    agc 		if (fuse->op.chown == NULL) {
    607  1.19  pooka 			error = -ENOSYS;
    608  1.19  pooka 		} else {
    609  1.19  pooka 			ret = fuse->op.chown(path, uid, gid);
    610  1.19  pooka 			if (ret)
    611  1.19  pooka 				error = ret;
    612   1.1    agc 		}
    613   1.1    agc 	}
    614  1.19  pooka 	if (va->va_atime.tv_sec != (time_t)PUFFS_VNOVAL
    615  1.19  pooka 	    || va->va_mtime.tv_sec != (long)PUFFS_VNOVAL) {
    616  1.19  pooka 		ret = 0;
    617   1.1    agc 
    618  1.19  pooka 		if (fuse->op.utimens) {
    619  1.19  pooka 			struct timespec tv[2];
    620  1.19  pooka 
    621  1.19  pooka 			tv[0].tv_sec = va->va_atime.tv_sec;
    622  1.19  pooka 			tv[0].tv_nsec = va->va_atime.tv_nsec;
    623  1.19  pooka 			tv[1].tv_sec = va->va_mtime.tv_sec;
    624  1.19  pooka 			tv[1].tv_nsec = va->va_mtime.tv_nsec;
    625  1.19  pooka 
    626  1.19  pooka 			ret = fuse->op.utimens(path, tv);
    627  1.19  pooka 		} else if (fuse->op.utime) {
    628  1.19  pooka 			struct utimbuf timbuf;
    629  1.19  pooka 
    630  1.19  pooka 			timbuf.actime = va->va_atime.tv_sec;
    631  1.19  pooka 			timbuf.modtime = va->va_mtime.tv_sec;
    632  1.19  pooka 
    633  1.19  pooka 			ret = fuse->op.utime(path, &timbuf);
    634  1.19  pooka 		} else {
    635  1.19  pooka 			error = -ENOSYS;
    636  1.19  pooka 		}
    637  1.19  pooka 
    638  1.19  pooka 		if (ret)
    639  1.19  pooka 			error = ret;
    640  1.19  pooka 	}
    641  1.19  pooka 	if (va->va_size != (u_quad_t)PUFFS_VNOVAL) {
    642  1.19  pooka 		ret = 0;
    643  1.19  pooka 
    644  1.19  pooka 		if (fuse->op.truncate) {
    645  1.19  pooka 			ret = fuse->op.truncate(path, (off_t)va->va_size);
    646  1.19  pooka 		} else if (fuse->op.ftruncate) {
    647  1.19  pooka 			ret = fuse->op.ftruncate(path, (off_t)va->va_size,
    648  1.19  pooka 			    &rn->file_info);
    649  1.19  pooka 		} else {
    650  1.19  pooka 			error = -ENOSYS;
    651  1.19  pooka 		}
    652  1.19  pooka 
    653  1.19  pooka 		if (ret)
    654  1.19  pooka 			error = ret;
    655   1.1    agc 	}
    656   1.1    agc 
    657  1.19  pooka 	return -error;
    658   1.1    agc }
    659   1.1    agc 
    660   1.1    agc /* ARGSUSED2 */
    661   1.1    agc static int
    662   1.1    agc puffs_fuse_node_open(struct puffs_cc *pcc, void *opc, int flags,
    663   1.1    agc 	const struct puffs_cred *cred, pid_t pid)
    664   1.1    agc {
    665   1.1    agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    666   1.1    agc 	struct puffs_node	*pn = opc;
    667   1.5  pooka 	struct refusenode	*rn = pn->pn_data;
    668   1.1    agc 	struct fuse		*fuse;
    669   1.1    agc 	struct stat		 st;
    670   1.1    agc 	const char		*path = PNPATH(pn);
    671   1.1    agc 	int			 ret;
    672   1.1    agc 
    673   1.1    agc 	fuse = (struct fuse *)pu->pu_privdata;
    674   1.1    agc 	if (fuse->op.open == NULL) {
    675   1.1    agc 		return ENOSYS;
    676   1.1    agc 	}
    677   1.1    agc 
    678   1.1    agc 	/* examine type - if directory, return 0 rather than open */
    679   1.1    agc 	ret = (fuse->op.getattr == NULL) ?
    680   1.1    agc 		stat(path, &st) :
    681   1.1    agc 		(*fuse->op.getattr)(path, &st);
    682   1.1    agc 	if (ret == 0 && (st.st_mode & S_IFMT) == S_IFDIR) {
    683   1.1    agc 		return 0;
    684   1.1    agc 	}
    685   1.1    agc 
    686   1.1    agc 	if (strcmp(path, "/") == 0) {
    687   1.1    agc 		return 0;
    688   1.1    agc 	}
    689   1.1    agc 
    690   1.5  pooka 	ret = (*fuse->op.open)(path, &rn->file_info);
    691   1.1    agc 
    692   1.1    agc 	if (ret == 0) {
    693   1.1    agc 	}
    694   1.1    agc 
    695  1.18  pooka 	return -ret;
    696   1.1    agc }
    697   1.1    agc 
    698   1.1    agc /* read some more from the file */
    699   1.1    agc /* ARGSUSED5 */
    700   1.1    agc static int
    701   1.1    agc puffs_fuse_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
    702   1.1    agc 	off_t offset, size_t *resid, const struct puffs_cred *pcr,
    703   1.1    agc 	int ioflag)
    704   1.1    agc {
    705   1.1    agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    706   1.1    agc 	struct puffs_node	*pn = opc;
    707   1.5  pooka 	struct refusenode	*rn = pn->pn_data;
    708   1.1    agc 	struct fuse		*fuse;
    709   1.1    agc 	const char		*path = PNPATH(pn);
    710   1.1    agc 	int			ret;
    711   1.1    agc 
    712   1.1    agc 	fuse = (struct fuse *)pu->pu_privdata;
    713   1.1    agc 	if (fuse->op.read == NULL) {
    714   1.1    agc 		return ENOSYS;
    715   1.1    agc 	}
    716   1.1    agc 
    717   1.7  pooka 	ret = (*fuse->op.read)(path, (char *)buf, *resid, offset,
    718   1.7  pooka 	    &rn->file_info);
    719   1.1    agc 
    720   1.1    agc 	if (ret > 0) {
    721   1.1    agc 		*resid -= ret;
    722  1.16  pooka 		ret = 0;
    723   1.1    agc 	}
    724   1.1    agc 
    725  1.16  pooka 	return -ret;
    726   1.1    agc }
    727   1.1    agc 
    728   1.1    agc /* write to the file */
    729   1.1    agc /* ARGSUSED0 */
    730   1.1    agc static int
    731   1.1    agc puffs_fuse_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
    732   1.1    agc 	off_t offset, size_t *resid, const struct puffs_cred *pcr,
    733   1.1    agc 	int ioflag)
    734   1.1    agc {
    735   1.1    agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    736   1.1    agc 	struct puffs_node	*pn = opc;
    737   1.8  pooka 	struct refusenode	*rn = pn->pn_data;
    738   1.1    agc 	struct fuse		*fuse;
    739   1.1    agc 	const char		*path = PNPATH(pn);
    740   1.1    agc 	int			ret;
    741   1.1    agc 
    742   1.1    agc 	fuse = (struct fuse *)pu->pu_privdata;
    743   1.1    agc 	if (fuse->op.write == NULL) {
    744   1.1    agc 		return ENOSYS;
    745   1.1    agc 	}
    746   1.1    agc 
    747  1.17  pooka 	if (ioflag & PUFFS_IO_APPEND)
    748  1.17  pooka 		offset = pn->pn_va.va_size;
    749  1.17  pooka 
    750   1.8  pooka 	ret = (*fuse->op.write)(path, (char *)buf, *resid, offset,
    751   1.8  pooka 	    &rn->file_info);
    752   1.1    agc 
    753   1.1    agc 	if (ret > 0) {
    754   1.1    agc 		*resid -= ret;
    755  1.16  pooka 		ret = 0;
    756   1.1    agc 	}
    757   1.1    agc 
    758  1.16  pooka 	return -ret;
    759   1.1    agc }
    760   1.1    agc 
    761   1.1    agc 
    762   1.1    agc /* ARGSUSED3 */
    763   1.1    agc static int
    764   1.1    agc puffs_fuse_node_readdir(struct puffs_cc *pcc, void *opc,
    765   1.1    agc 	struct dirent *dent, const struct puffs_cred *pcr, off_t *readoff,
    766   1.1    agc 	size_t *reslen)
    767   1.1    agc {
    768   1.1    agc 	struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    769   1.1    agc 	struct puffs_node	*pn = opc;
    770   1.8  pooka 	struct refusenode	*rn = pn->pn_data;
    771   1.1    agc 	struct fuse		*fuse;
    772   1.1    agc 	const char		*path = PNPATH(pn);
    773   1.4  pooka 	struct fuse_dirh	deh;
    774   1.1    agc 	int			ret;
    775   1.1    agc 
    776   1.1    agc 	fuse = (struct fuse *)pu->pu_privdata;
    777   1.4  pooka 	if (fuse->op.readdir == NULL && fuse->op.getdir == NULL) {
    778   1.1    agc 		return ENOSYS;
    779   1.1    agc 	}
    780   1.1    agc 
    781   1.1    agc 	/* XXX: how to handle this??? */
    782   1.1    agc 	if (*readoff != 0) {
    783   1.1    agc 		return 0;
    784   1.1    agc 	}
    785   1.1    agc 
    786   1.4  pooka 	deh.dent = dent;
    787   1.4  pooka 	deh.reslen = *reslen;
    788   1.4  pooka 	deh.readoff = *readoff;
    789   1.4  pooka 
    790   1.4  pooka 	if (fuse->op.readdir)
    791   1.7  pooka 		ret = fuse->op.readdir(path, &deh, puffs_fuse_fill_dir,
    792   1.8  pooka 		    *readoff, &rn->file_info);
    793   1.4  pooka 	else
    794   1.4  pooka 		ret = fuse->op.getdir(path, &deh, puffs_fuse_dirfil);
    795   1.4  pooka 	*reslen = deh.reslen;
    796   1.1    agc 	*readoff = 1;
    797   1.1    agc 
    798   1.1    agc 	if (ret == 0) {
    799   1.1    agc 	}
    800   1.1    agc 
    801  1.18  pooka 	return -ret;
    802   1.1    agc }
    803   1.1    agc 
    804   1.3  pooka /* ARGSUSED */
    805   1.3  pooka static int
    806   1.3  pooka puffs_fuse_node_reclaim(struct puffs_cc *pcc, void *opc, pid_t pid)
    807   1.3  pooka {
    808   1.3  pooka 	struct puffs_node	*pn = opc;
    809   1.3  pooka 
    810   1.5  pooka 	nukern(pn);
    811   1.3  pooka 
    812   1.3  pooka 	return 0;
    813   1.3  pooka }
    814   1.3  pooka 
    815   1.1    agc /* ARGSUSED1 */
    816   1.1    agc static int
    817   1.1    agc puffs_fuse_fs_unmount(struct puffs_cc *pcc, int flags, pid_t pid)
    818   1.1    agc {
    819   1.1    agc         struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    820   1.1    agc 	struct fuse		*fuse;
    821   1.1    agc 
    822   1.1    agc 	fuse = (struct fuse *)pu->pu_privdata;
    823   1.1    agc 	if (fuse->op.destroy == NULL) {
    824   1.2  pooka 		return 0;
    825   1.1    agc 	}
    826   1.1    agc 	(*fuse->op.destroy)(fuse);
    827   1.1    agc         return 0;
    828   1.1    agc }
    829   1.1    agc 
    830   1.1    agc /* ARGSUSED0 */
    831   1.1    agc static int
    832   1.1    agc puffs_fuse_fs_sync(struct puffs_cc *pcc, int flags,
    833   1.1    agc             const struct puffs_cred *cr, pid_t pid)
    834   1.1    agc {
    835   1.1    agc         return 0;
    836   1.1    agc }
    837   1.1    agc 
    838   1.1    agc /* ARGSUSED2 */
    839   1.1    agc static int
    840   1.1    agc puffs_fuse_fs_statvfs(struct puffs_cc *pcc, struct statvfs *svfsb, pid_t pid)
    841   1.1    agc {
    842   1.1    agc         struct puffs_usermount	*pu = puffs_cc_getusermount(pcc);
    843   1.1    agc 	struct fuse		*fuse;
    844   1.1    agc 	int			ret;
    845   1.1    agc 
    846   1.1    agc 	fuse = (struct fuse *)pu->pu_privdata;
    847   1.1    agc 	if (fuse->op.statfs == NULL) {
    848   1.1    agc 		if ((ret = statvfs(PNPATH(pu->pu_pn_root), svfsb)) == -1) {
    849   1.1    agc 			return errno;
    850   1.1    agc 		}
    851   1.1    agc 	} else {
    852   1.1    agc 		ret = (*fuse->op.statfs)(PNPATH(pu->pu_pn_root), svfsb);
    853   1.1    agc 	}
    854   1.1    agc 
    855   1.1    agc         return ret;
    856   1.1    agc }
    857   1.1    agc 
    858   1.1    agc 
    859   1.1    agc 
    860   1.1    agc 
    861   1.1    agc /* End of puffs_fuse operations */
    862   1.1    agc 
    863  1.11  pooka /*
    864  1.11  pooka  * XXX: do this otherwise if/when we grow thread support
    865  1.11  pooka  *
    866  1.11  pooka  * XXX2: does not supply uid, gid or pid currently
    867  1.11  pooka  */
    868  1.11  pooka static struct fuse_context fcon;
    869  1.11  pooka 
    870   1.1    agc /* ARGSUSED3 */
    871   1.1    agc int
    872   1.1    agc fuse_main_real(int argc, char **argv, const struct fuse_operations *ops,
    873   1.1    agc 	size_t size, void *userdata)
    874   1.1    agc {
    875   1.1    agc 	struct puffs_usermount	*pu;
    876   1.1    agc 	struct puffs_pathobj	*po_root;
    877   1.1    agc 	struct puffs_ops	*pops;
    878   1.1    agc 	struct statvfs		svfsb;
    879   1.1    agc 	struct fuse		*fuse;
    880   1.1    agc 	char			 name[64];
    881   1.1    agc 	char			*slash;
    882   1.1    agc 	int			 ret;
    883   1.1    agc 
    884   1.1    agc 	/* initialise the puffs operations structure */
    885   1.1    agc         PUFFSOP_INIT(pops);
    886   1.1    agc 
    887   1.1    agc         PUFFSOP_SET(pops, puffs_fuse, fs, sync);
    888   1.1    agc         PUFFSOP_SET(pops, puffs_fuse, fs, statvfs);
    889   1.1    agc         PUFFSOP_SET(pops, puffs_fuse, fs, unmount);
    890   1.1    agc 
    891   1.2  pooka 	/*
    892   1.2  pooka 	 * XXX: all of these don't possibly need to be
    893   1.2  pooka 	 * unconditionally set
    894   1.2  pooka 	 */
    895   1.1    agc         PUFFSOP_SET(pops, puffs_fuse, node, lookup);
    896   1.1    agc         PUFFSOP_SET(pops, puffs_fuse, node, getattr);
    897  1.15  pooka         PUFFSOP_SET(pops, puffs_fuse, node, setattr);
    898   1.1    agc         PUFFSOP_SET(pops, puffs_fuse, node, readdir);
    899   1.1    agc         PUFFSOP_SET(pops, puffs_fuse, node, readlink);
    900   1.1    agc         PUFFSOP_SET(pops, puffs_fuse, node, mknod);
    901  1.16  pooka         PUFFSOP_SET(pops, puffs_fuse, node, create);
    902  1.15  pooka         PUFFSOP_SET(pops, puffs_fuse, node, remove);
    903   1.1    agc         PUFFSOP_SET(pops, puffs_fuse, node, mkdir);
    904   1.1    agc         PUFFSOP_SET(pops, puffs_fuse, node, rmdir);
    905   1.1    agc         PUFFSOP_SET(pops, puffs_fuse, node, symlink);
    906   1.1    agc         PUFFSOP_SET(pops, puffs_fuse, node, rename);
    907   1.1    agc         PUFFSOP_SET(pops, puffs_fuse, node, link);
    908   1.1    agc         PUFFSOP_SET(pops, puffs_fuse, node, open);
    909   1.1    agc         PUFFSOP_SET(pops, puffs_fuse, node, read);
    910   1.1    agc         PUFFSOP_SET(pops, puffs_fuse, node, write);
    911   1.3  pooka         PUFFSOP_SET(pops, puffs_fuse, node, reclaim);
    912   1.1    agc 
    913   1.1    agc 	NEW(struct fuse, fuse, "fuse_main_real", exit(EXIT_FAILURE));
    914   1.1    agc 
    915   1.1    agc 	/* copy fuse ops to their own stucture */
    916   1.1    agc 	(void) memcpy(&fuse->op, ops, sizeof(fuse->op));
    917   1.1    agc 
    918  1.11  pooka 	fcon.fuse = fuse;
    919  1.11  pooka 	fcon.private_data = userdata;
    920  1.11  pooka 
    921   1.1    agc 	/* whilst this (assigning the pu_privdata in the puffs
    922   1.1    agc 	 * usermount struct to be the fuse struct) might seem like
    923   1.1    agc 	 * we are chasing our tail here, the logic is as follows:
    924   1.1    agc 		+ the operation wrapper gets called with the puffs
    925   1.1    agc 		  calling conventions
    926   1.1    agc 		+ we need to fix up args first
    927   1.1    agc 		+ then call the fuse user-supplied operation
    928   1.1    agc 		+ then we fix up any values on return that we need to
    929   1.1    agc 		+ and fix up any nodes, etc
    930   1.7  pooka 	 * so we need to be able to get at the fuse ops from within the
    931   1.7  pooka 	 * puffs_usermount struct
    932   1.7  pooka 	 */
    933   1.1    agc 	if ((slash = strrchr(*argv, '/')) == NULL) {
    934   1.1    agc 		slash = *argv;
    935   1.1    agc 	} else {
    936   1.1    agc 		slash += 1;
    937   1.1    agc 	}
    938   1.1    agc 	(void) snprintf(name, sizeof(name), "refuse:%s", slash);
    939   1.1    agc 	pu = puffs_mount(pops, argv[argc - 1], MNT_NODEV | MNT_NOSUID,
    940   1.1    agc 			name, fuse,
    941   1.1    agc 			PUFFS_FLAG_BUILDPATH | PUFFS_FLAG_OPDUMP, 0);
    942   1.1    agc 	if (pu == NULL) {
    943   1.1    agc 		err(EXIT_FAILURE, "puffs_mount");
    944   1.1    agc 	}
    945   1.1    agc 
    946   1.1    agc 	fuse->pu = pu;
    947   1.1    agc 	pu->pu_pn_root = puffs_pn_new(pu, NULL);
    948   1.1    agc 	po_root = puffs_getrootpathobj(pu);
    949   1.1    agc 	po_root->po_path = strdup("/");
    950   1.1    agc 	po_root->po_len = 1;
    951   1.1    agc 
    952  1.11  pooka 	if (fuse->op.init)
    953  1.11  pooka 		fcon.private_data = fuse->op.init(NULL); /* XXX */
    954  1.11  pooka 
    955   1.1    agc 	statvfs(argv[argc - 1], &svfsb); /* XXX - not really the correct dir */
    956   1.1    agc 	if (puffs_start(pu, pu->pu_pn_root, &svfsb) == -1) {
    957   1.1    agc 		err(EXIT_FAILURE, "puffs_start");
    958   1.1    agc 	}
    959   1.1    agc 
    960   1.1    agc 	ret = puffs_mainloop(fuse->pu, PUFFSLOOP_NODAEMON);
    961   1.1    agc 
    962   1.1    agc 	(void) free(po_root->po_path);
    963   1.1    agc 	FREE(fuse);
    964   1.1    agc 	return ret;
    965   1.1    agc }
    966   1.1    agc 
    967   1.1    agc /* ARGSUSED0 */
    968   1.1    agc int
    969   1.1    agc fuse_opt_parse(struct fuse_args *args, void *data,
    970   1.1    agc 	const struct fuse_opt *opts, fuse_opt_proc_t proc)
    971   1.1    agc {
    972   1.1    agc 	return 0;
    973   1.1    agc }
    974  1.11  pooka 
    975  1.11  pooka /* XXX: threads */
    976  1.11  pooka struct fuse_context *
    977  1.11  pooka fuse_get_context()
    978  1.11  pooka {
    979  1.11  pooka 
    980  1.11  pooka 	return &fcon;
    981  1.11  pooka }
    982