Home | History | Annotate | Line # | Download | only in libpuffs
puffs.h revision 1.22
      1 /*	$NetBSD: puffs.h,v 1.22 2007/01/10 20:11:04 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2005, 2006  Antti Kantee.  All Rights Reserved.
      5  *
      6  * Development of this software was supported by the
      7  * Google Summer of Code program and the Ulla Tuominen Foundation.
      8  * The Google SoC project was mentored by Bill Studenmund.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. The name of the company nor the name of the author may be used to
     19  *    endorse or promote products derived from this software without specific
     20  *    prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     23  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     25  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #ifndef _PUFFS_H_
     36 #define _PUFFS_H_
     37 
     38 #include <sys/param.h>
     39 #include <sys/mount.h>
     40 #include <sys/statvfs.h>
     41 #include <sys/vnode.h>
     42 
     43 #include <fs/puffs/puffs_msgif.h>
     44 
     45 #include <mntopts.h>
     46 #include <string.h>
     47 
     48 /* forwards */
     49 struct puffs_cc;
     50 
     51 struct puffs_getreq;
     52 struct puffs_putreq;
     53 
     54 /* XXX: might disappear from here */
     55 struct puffs_node {
     56 	off_t		pn_size;
     57 	int		pn_flag;
     58 	struct vattr	pn_va;
     59 
     60 	void		*pn_data;		/* private data		*/
     61 
     62 	char 		*pn_path;		/* PUFFS_FLAG_BUILDPATH */
     63 	size_t		pn_plen;
     64 
     65 	struct puffs_usermount *pn_mnt;
     66 
     67 	LIST_ENTRY(puffs_node) pn_entries;
     68 };
     69 
     70 
     71 struct puffs_usermount;
     72 
     73 /*
     74  * megaXXX: these are values from inside _KERNEL
     75  * need to work on the translation for ALL the necessary values.
     76  */
     77 #define PUFFS_VNOVAL (-1)
     78 #define PUFFS_ISDOTDOT 0x2000
     79 #define PUFFS_IO_APPEND 0x20
     80 
     81 /*
     82  * Magic constants
     83  */
     84 #define PUFFS_CC_STACKSIZE_DEFAULT (1024*1024)
     85 
     86 struct puffs_cn {
     87 	struct puffs_kcn	*pcn_pkcnp;	/* kernel input */
     88 
     89 	/* XXX: this will be a more complex object some day */
     90 	char			*pcn_fullpath;	/* if PUFFS_FLAG_BUILDPATH */
     91 	size_t			pcn_fullplen;
     92 };
     93 #define pcn_nameiop	pcn_pkcnp->pkcn_nameiop
     94 #define pcn_flags	pcn_pkcnp->pkcn_flags
     95 #define pcn_pid		pcn_pkcnp->pkcn_pid
     96 #define pcn_cred	pcn_pkcnp->pkcn_cred
     97 #define pcn_name	pcn_pkcnp->pkcn_name
     98 #define pcn_namelen	pcn_pkcnp->pkcn_namelen
     99 
    100 
    101 /*
    102  * Puffs options to mount
    103  */
    104 /* kernel */
    105 #define	PUFFSMOPT_CACHE		{ "cache", 1, PUFFS_KFLAG_NOCACHE, 1 }
    106 #define PUFFSMOPT_ALLOPS	{ "allops", 0, PUFFS_KFLAG_ALLOPS, 1 }
    107 
    108 /* libpuffs */
    109 #define PUFFSMOPT_DUMP		{ "dump", 0, PUFFS_FLAG_OPDUMP, 1 }
    110 
    111 #define PUFFSMOPT_STD							\
    112 	PUFFSMOPT_CACHE,						\
    113 	PUFFSMOPT_ALLOPS,						\
    114 	PUFFSMOPT_DUMP
    115 
    116 extern const struct mntopt puffsmopts[]; /* puffs.c */
    117 
    118 /* callbacks for operations */
    119 struct puffs_ops {
    120 	int (*puffs_fs_unmount)(struct puffs_cc *, int, pid_t);
    121 	int (*puffs_fs_statvfs)(struct puffs_cc *,
    122 	    struct statvfs *, pid_t);
    123 	int (*puffs_fs_sync)(struct puffs_cc *, int,
    124 	    const struct puffs_cred *, pid_t);
    125 
    126 	int (*puffs_node_lookup)(struct puffs_cc *,
    127 	    void *, void **, enum vtype *, voff_t *, dev_t *,
    128 	    const struct puffs_cn *);
    129 	int (*puffs_node_create)(struct puffs_cc *,
    130 	    void *, void **, const struct puffs_cn *, const struct vattr *);
    131 	int (*puffs_node_mknod)(struct puffs_cc *,
    132 	    void *, void **, const struct puffs_cn *, const struct vattr *);
    133 	int (*puffs_node_open)(struct puffs_cc *,
    134 	    void *, int, const struct puffs_cred *, pid_t);
    135 	int (*puffs_node_close)(struct puffs_cc *,
    136 	    void *, int, const struct puffs_cred *, pid_t);
    137 	int (*puffs_node_access)(struct puffs_cc *,
    138 	    void *, int, const struct puffs_cred *, pid_t);
    139 	int (*puffs_node_getattr)(struct puffs_cc *,
    140 	    void *, struct vattr *, const struct puffs_cred *, pid_t);
    141 	int (*puffs_node_setattr)(struct puffs_cc *,
    142 	    void *, const struct vattr *, const struct puffs_cred *, pid_t);
    143 	int (*puffs_node_poll)(struct puffs_cc *,
    144 	    void *, struct puffs_vnreq_poll *);
    145 	int (*puffs_node_revoke)(struct puffs_cc *, void *, int);
    146 	int (*puffs_node_mmap)(struct puffs_cc *,
    147 	    void *, int, const struct puffs_cred *, pid_t);
    148 	int (*puffs_node_fsync)(struct puffs_cc *,
    149 	    void *, const struct puffs_cred *, int, off_t, off_t, pid_t);
    150 	int (*puffs_node_seek)(struct puffs_cc *,
    151 	    void *, off_t, off_t, const struct puffs_cred *);
    152 	int (*puffs_node_remove)(struct puffs_cc *,
    153 	    void *, void *, const struct puffs_cn *);
    154 	int (*puffs_node_link)(struct puffs_cc *,
    155 	    void *, void *, const struct puffs_cn *);
    156 	int (*puffs_node_rename)(struct puffs_cc *,
    157 	    void *, void *, const struct puffs_cn *, void *, void *,
    158 	    const struct puffs_cn *);
    159 	int (*puffs_node_mkdir)(struct puffs_cc *,
    160 	    void *, void **, const struct puffs_cn *, const struct vattr *);
    161 	int (*puffs_node_rmdir)(struct puffs_cc *,
    162 	    void *, void *, const struct puffs_cn *);
    163 	int (*puffs_node_symlink)(struct puffs_cc *,
    164 	    void *, void **, const struct puffs_cn *, const struct vattr *,
    165 	    const char *);
    166 	int (*puffs_node_readdir)(struct puffs_cc *,
    167 	    void *, struct dirent *, const struct puffs_cred *,
    168 	    off_t *, size_t *);
    169 	int (*puffs_node_readlink)(struct puffs_cc *,
    170 	    void *, const struct puffs_cred *, char *, size_t *);
    171 	int (*puffs_node_reclaim)(struct puffs_cc *,
    172 	    void *, pid_t);
    173 	int (*puffs_node_inactive)(struct puffs_cc *,
    174 	    void *, pid_t, int *);
    175 	int (*puffs_node_print)(struct puffs_cc *,
    176 	    void *);
    177 	int (*puffs_node_pathconf)(struct puffs_cc *,
    178 	    void *, int, int *);
    179 	int (*puffs_node_advlock)(struct puffs_cc *,
    180 	    void *, void *, int, struct flock *, int);
    181 	int (*puffs_node_getextattr)(struct puffs_cc *,
    182 	    void *, struct puffs_vnreq_getextattr *);
    183 	int (*puffs_node_setextattr)(struct puffs_cc *,
    184 	    void *, struct puffs_vnreq_setextattr *);
    185 	int (*puffs_node_listextattr)(struct puffs_cc *,
    186 	    void *, struct puffs_vnreq_listextattr *);
    187 	int (*puffs_node_read)(struct puffs_cc *, void *,
    188 	    uint8_t *, off_t, size_t *, const struct puffs_cred *, int);
    189 	int (*puffs_node_write)(struct puffs_cc *, void *,
    190 	    uint8_t *, off_t, size_t *, const struct puffs_cred *, int);
    191 };
    192 
    193 struct puffs_usermount {
    194 	struct puffs_ops	pu_ops;
    195 
    196 	int			pu_fd;
    197 	uint32_t		pu_flags;
    198 	size_t			pu_maxreqlen;
    199 	size_t			pu_cc_stacksize;
    200 
    201 	int			pu_state;
    202 
    203 	struct puffs_node	*pu_pn_root;
    204 
    205 	LIST_HEAD(, puffs_node)	pu_pnodelst;
    206 
    207 	int	pu_wcnt;
    208 	void	*pu_privdata;
    209 };
    210 
    211 enum {
    212 	PUFFS_STATE_MOUNTING,	PUFFS_STATE_RUNNING,
    213 	PUFFS_STATE_UNMOUNTING,	PUFFS_STATE_UNMOUNTED
    214 };
    215 
    216 #define PUFFS_FLAG_KERN(a)	((a) & 0x0000ffff)
    217 #define PUFFS_FLAG_LIB(a)	((a) & 0xffff0000)
    218 
    219 #define PUFFS_FLAG_BUILDPATH	0x80000000	/* node paths in pnode */
    220 #define PUFFS_FLAG_OPDUMP	0x40000000	/* dump all operations */
    221 
    222 /* blocking mode argument */
    223 #define PUFFSDEV_BLOCK 0
    224 #define PUFFSDEV_NONBLOCK 1
    225 
    226 /* mainloop flags */
    227 #define PUFFSLOOP_NODAEMON 0x01
    228 
    229 int  puffs_fsnop_unmount(struct puffs_cc *, int, pid_t);
    230 int  puffs_fsnop_statvfs(struct puffs_cc *, struct statvfs *, pid_t);
    231 void puffs_zerostatvfs(struct statvfs *);
    232 int  puffs_fsnop_sync(struct puffs_cc *, int waitfor,
    233 		      const struct puffs_cred *, pid_t);
    234 
    235 #define		DENT_DOT	0
    236 #define		DENT_DOTDOT	1
    237 #define		DENT_ADJ(a)	((a)-2)	/* nth request means dir's n-2th */
    238 int		puffs_gendotdent(struct dirent **, ino_t, int, size_t *);
    239 int		puffs_nextdent(struct dirent **, const char *, ino_t,
    240 			       uint8_t, size_t *);
    241 int		puffs_vtype2dt(enum vtype);
    242 enum vtype	puffs_mode2vt(mode_t);
    243 
    244 
    245 /*
    246  * Operation credentials
    247  */
    248 
    249 /* Credential fetch */
    250 int	puffs_cred_getuid(const struct puffs_cred *pcr, uid_t *);
    251 int	puffs_cred_getgid(const struct puffs_cred *pcr, gid_t *);
    252 int	puffs_cred_getgroups(const struct puffs_cred *pcr, gid_t *, short *);
    253 
    254 /* Credential check */
    255 int	puffs_cred_isuid(const struct puffs_cred *pcr, uid_t);
    256 int	puffs_cred_hasgroup(const struct puffs_cred *pcr, gid_t);
    257 /* kernel internal NOCRED */
    258 int	puffs_cred_iskernel(const struct puffs_cred *pcr);
    259 /* kernel internal FSCRED */
    260 int	puffs_cred_isfs(const struct puffs_cred *pcr);
    261 /* root || NOCRED || FSCRED */
    262 int	puffs_cred_isjuggernaut(const struct puffs_cred *pcr);
    263 
    264 
    265 /*
    266  * protos
    267  */
    268 
    269 #define PUFFSOP_PROTOS(fsname)						\
    270 	int fsname##_fs_mount(struct puffs_usermount *, void **,	\
    271 	    struct statvfs *);						\
    272 	int fsname##_fs_unmount(struct puffs_cc *, int, pid_t);		\
    273 	int fsname##_fs_statvfs(struct puffs_cc *,			\
    274 	    struct statvfs *, pid_t);					\
    275 	int fsname##_fs_sync(struct puffs_cc *, int,			\
    276 	    const struct puffs_cred *cred, pid_t);			\
    277 									\
    278 	int fsname##_node_lookup(struct puffs_cc *,			\
    279 	    void *, void **, enum vtype *, voff_t *, dev_t *,		\
    280 	    const struct puffs_cn *);					\
    281 	int fsname##_node_create(struct puffs_cc *,			\
    282 	    void *, void **, const struct puffs_cn *,			\
    283 	    const struct vattr *);					\
    284 	int fsname##_node_mknod(struct puffs_cc *,			\
    285 	    void *, void **, const struct puffs_cn *,			\
    286 	    const struct vattr *);					\
    287 	int fsname##_node_open(struct puffs_cc *,			\
    288 	    void *, int, const struct puffs_cred *, pid_t);		\
    289 	int fsname##_node_close(struct puffs_cc *,			\
    290 	    void *, int, const struct puffs_cred *, pid_t);		\
    291 	int fsname##_node_access(struct puffs_cc *,			\
    292 	    void *, int, const struct puffs_cred *, pid_t);		\
    293 	int fsname##_node_getattr(struct puffs_cc *,			\
    294 	    void *, struct vattr *, const struct puffs_cred *, pid_t);	\
    295 	int fsname##_node_setattr(struct puffs_cc *,			\
    296 	    void *, const struct vattr *, const struct puffs_cred *,	\
    297 	    pid_t);							\
    298 	int fsname##_node_poll(struct puffs_cc *,			\
    299 	    void *, struct puffs_vnreq_poll *);				\
    300 	int fsname##_node_revoke(struct puffs_cc *, void *, int);	\
    301 	int fsname##_node_mmap(struct puffs_cc *,			\
    302 	    void *, int, const struct puffs_cred *, pid_t);		\
    303 	int fsname##_node_fsync(struct puffs_cc *,			\
    304 	    void *, const struct puffs_cred *, int, off_t, off_t,	\
    305 	    pid_t);							\
    306 	int fsname##_node_seek(struct puffs_cc *,			\
    307 	    void *, off_t, off_t, const struct puffs_cred *);		\
    308 	int fsname##_node_remove(struct puffs_cc *,			\
    309 	    void *, void *, const struct puffs_cn *);			\
    310 	int fsname##_node_link(struct puffs_cc *,			\
    311 	    void *, void *, const struct puffs_cn *);			\
    312 	int fsname##_node_rename(struct puffs_cc *,			\
    313 	    void *, void *, const struct puffs_cn *, void *, void *,	\
    314 	    const struct puffs_cn *);					\
    315 	int fsname##_node_mkdir(struct puffs_cc *,			\
    316 	    void *, void **, const struct puffs_cn *,			\
    317 	    const struct vattr *);					\
    318 	int fsname##_node_rmdir(struct puffs_cc *,			\
    319 	    void *, void *, const struct puffs_cn *);			\
    320 	int fsname##_node_symlink(struct puffs_cc *,			\
    321 	    void *, void **, const struct puffs_cn *,			\
    322 	    const struct vattr *, const char *);			\
    323 	int fsname##_node_readdir(struct puffs_cc *,			\
    324 	    void *, struct dirent *, const struct puffs_cred *,		\
    325 	    off_t *, size_t *);						\
    326 	int fsname##_node_readlink(struct puffs_cc *,			\
    327 	    void *, const struct puffs_cred *, char *, size_t *);	\
    328 	int fsname##_node_reclaim(struct puffs_cc *,			\
    329 	    void *, pid_t);						\
    330 	int fsname##_node_inactive(struct puffs_cc *,			\
    331 	    void *, pid_t, int *);					\
    332 	int fsname##_node_print(struct puffs_cc *,			\
    333 	    void *);							\
    334 	int fsname##_node_pathconf(struct puffs_cc *,			\
    335 	    void *, int, int *);					\
    336 	int fsname##_node_advlock(struct puffs_cc *,			\
    337 	    void *, void *, int, struct flock *, int);			\
    338 	int fsname##_node_getextattr(struct puffs_cc *,			\
    339 	    void *, struct puffs_vnreq_getextattr *);			\
    340 	int fsname##_node_setextattr(struct puffs_cc *,			\
    341 	    void *, struct puffs_vnreq_setextattr *);			\
    342 	int fsname##_node_listextattr(struct puffs_cc *,		\
    343 	    void *, struct puffs_vnreq_listextattr *);			\
    344 	int fsname##_node_read(struct puffs_cc *, void *,		\
    345 	    uint8_t *, off_t, size_t *, const struct puffs_cred *, int);\
    346 	int fsname##_node_write(struct puffs_cc *, void *,		\
    347 	    uint8_t *, off_t, size_t *, const struct puffs_cred *, int);
    348 
    349 #define PUFFSOP_INIT(ops)						\
    350     ops = malloc(sizeof(struct puffs_ops));				\
    351     memset(ops, 0, sizeof(struct puffs_ops))
    352 #define PUFFSOP_SET(ops, fsname, fsornode, opname)			\
    353     (ops)->puffs_##fsornode##_##opname = fsname##_##fsornode##_##opname
    354 #define PUFFSOP_SETFSNOP(ops, opname)					\
    355     (ops)->puffs_fs_##opname = puffs_fsnop_##opname
    356 
    357 #define PUFFS_DEVEL_LIBVERSION 2
    358 #define puffs_mount(a,b,c,d,e,f,g) \
    359     _puffs_mount(PUFFS_DEVEL_LIBVERSION,a,b,c,d,e,f,g)
    360 
    361 __BEGIN_DECLS
    362 
    363 struct puffs_usermount *_puffs_mount(int, struct puffs_ops *, const char *, int,
    364 				    const char *, void *, uint32_t, size_t);
    365 int		puffs_start(struct puffs_usermount *, void *, struct statvfs *);
    366 int		puffs_exit(struct puffs_usermount *, int);
    367 int		puffs_mainloop(struct puffs_usermount *, int);
    368 
    369 
    370 int	puffs_getselectable(struct puffs_usermount *);
    371 int	puffs_setblockingmode(struct puffs_usermount *, int);
    372 int	puffs_getstate(struct puffs_usermount *);
    373 int	puffs_setrootpath(struct puffs_usermount *, const char *);
    374 void	puffs_setstacksize(struct puffs_usermount *, size_t);
    375 
    376 struct puffs_node *	puffs_pn_new(struct puffs_usermount *, void *);
    377 void			puffs_pn_put(struct puffs_node *);
    378 struct vattr 		*puffs_pn_getvattrp(struct puffs_node *);
    379 void			puffs_setvattr(struct vattr *, const struct vattr *);
    380 void			puffs_vattr_null(struct vattr *);
    381 
    382 /*
    383  * Requests
    384  */
    385 
    386 struct puffs_getreq	*puffs_makegetreq(struct puffs_usermount *,
    387 					  size_t, int);
    388 int			puffs_loadgetreq(struct puffs_getreq *);
    389 struct puffs_req	*puffs_getreq(struct puffs_getreq *);
    390 int			puffs_remaininggetreq(struct puffs_getreq *);
    391 void			puffs_setmaxgetreq(struct puffs_getreq *, int);
    392 void			puffs_destroygetreq(struct puffs_getreq *);
    393 
    394 struct puffs_putreq	*puffs_makeputreq(struct puffs_usermount *);
    395 void			puffs_putreq(struct puffs_putreq *, struct puffs_req *);
    396 void			puffs_putreq_cc(struct puffs_putreq *,struct puffs_cc*);
    397 int			puffs_putputreq(struct puffs_putreq *);
    398 void			puffs_resetputreq(struct puffs_putreq *);
    399 void			puffs_destroyputreq(struct puffs_putreq *);
    400 
    401 /*
    402  * Call Context interfaces relevant for user.
    403  */
    404 
    405 void			puffs_cc_yield(struct puffs_cc *);
    406 void			puffs_cc_continue(struct puffs_cc *);
    407 struct puffs_usermount	*puffs_cc_getusermount(struct puffs_cc *);
    408 struct puffs_cc 	*puffs_cc_create(struct puffs_usermount *);
    409 void			puffs_cc_destroy(struct puffs_cc *);
    410 
    411 /*
    412  * Execute or continue a request
    413  */
    414 
    415 int	puffs_handlereqs(struct puffs_usermount *, struct puffs_getreq *,
    416 			 struct puffs_putreq *, int);
    417 
    418 int	puffs_dopreq(struct puffs_usermount *, struct puffs_putreq *,
    419 		     struct puffs_req *);
    420 int	puffs_docc(struct puffs_putreq *, struct puffs_cc *);
    421 
    422 /*
    423  * Flushing / invalidation routines
    424  */
    425 
    426 int	puffs_inval_namecache_dir(struct puffs_usermount *, void *);
    427 int	puffs_inval_namecache_all(struct puffs_usermount *);
    428 
    429 __END_DECLS
    430 
    431 #endif /* _PUFFS_H_ */
    432