Home | History | Annotate | Line # | Download | only in libpuffs
puffs.h revision 1.24
      1 /*	$NetBSD: puffs.h,v 1.24 2007/01/11 17:48:21 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/stat.h>
     41 #include <sys/statvfs.h>
     42 #include <sys/vnode.h>
     43 
     44 #include <fs/puffs/puffs_msgif.h>
     45 
     46 #include <mntopts.h>
     47 #include <string.h>
     48 
     49 /* forwards */
     50 struct puffs_cc;
     51 
     52 struct puffs_getreq;
     53 struct puffs_putreq;
     54 
     55 /* XXX: might disappear from here */
     56 struct puffs_node {
     57 	off_t		pn_size;
     58 	int		pn_flag;
     59 	struct vattr	pn_va;
     60 
     61 	void		*pn_data;		/* private data		*/
     62 
     63 	char 		*pn_path;		/* PUFFS_FLAG_BUILDPATH */
     64 	size_t		pn_plen;
     65 
     66 	struct puffs_usermount *pn_mnt;
     67 
     68 	LIST_ENTRY(puffs_node) pn_entries;
     69 };
     70 
     71 
     72 struct puffs_usermount;
     73 
     74 /*
     75  * megaXXX: these are values from inside _KERNEL
     76  * need to work on the translation for ALL the necessary values.
     77  */
     78 #define PUFFS_VNOVAL (-1)
     79 #define PUFFS_ISDOTDOT 0x2000
     80 #define PUFFS_IO_APPEND 0x20
     81 
     82 #define PUFFS_FSYNC_DATAONLY 0x0002
     83 #define PUFFS_FSYNC_CACHE    0x0100
     84 
     85 /*
     86  * Magic constants
     87  */
     88 #define PUFFS_CC_STACKSIZE_DEFAULT (1024*1024)
     89 
     90 struct puffs_cn {
     91 	struct puffs_kcn	*pcn_pkcnp;	/* kernel input */
     92 
     93 	/* XXX: this will be a more complex object some day */
     94 	char			*pcn_fullpath;	/* if PUFFS_FLAG_BUILDPATH */
     95 	size_t			pcn_fullplen;
     96 };
     97 #define pcn_nameiop	pcn_pkcnp->pkcn_nameiop
     98 #define pcn_flags	pcn_pkcnp->pkcn_flags
     99 #define pcn_pid		pcn_pkcnp->pkcn_pid
    100 #define pcn_cred	pcn_pkcnp->pkcn_cred
    101 #define pcn_name	pcn_pkcnp->pkcn_name
    102 #define pcn_namelen	pcn_pkcnp->pkcn_namelen
    103 
    104 
    105 /*
    106  * Puffs options to mount
    107  */
    108 /* kernel */
    109 #define	PUFFSMOPT_CACHE		{ "cache", 1, PUFFS_KFLAG_NOCACHE, 1 }
    110 #define PUFFSMOPT_ALLOPS	{ "allops", 0, PUFFS_KFLAG_ALLOPS, 1 }
    111 
    112 /* libpuffs */
    113 #define PUFFSMOPT_DUMP		{ "dump", 0, PUFFS_FLAG_OPDUMP, 1 }
    114 
    115 #define PUFFSMOPT_STD							\
    116 	PUFFSMOPT_CACHE,						\
    117 	PUFFSMOPT_ALLOPS,						\
    118 	PUFFSMOPT_DUMP
    119 
    120 extern const struct mntopt puffsmopts[]; /* puffs.c */
    121 
    122 /* callbacks for operations */
    123 struct puffs_ops {
    124 	int (*puffs_fs_unmount)(struct puffs_cc *, int, pid_t);
    125 	int (*puffs_fs_statvfs)(struct puffs_cc *,
    126 	    struct statvfs *, pid_t);
    127 	int (*puffs_fs_sync)(struct puffs_cc *, int,
    128 	    const struct puffs_cred *, pid_t);
    129 
    130 	int (*puffs_node_lookup)(struct puffs_cc *,
    131 	    void *, void **, enum vtype *, voff_t *, dev_t *,
    132 	    const struct puffs_cn *);
    133 	int (*puffs_node_create)(struct puffs_cc *,
    134 	    void *, void **, const struct puffs_cn *, const struct vattr *);
    135 	int (*puffs_node_mknod)(struct puffs_cc *,
    136 	    void *, void **, const struct puffs_cn *, const struct vattr *);
    137 	int (*puffs_node_open)(struct puffs_cc *,
    138 	    void *, int, const struct puffs_cred *, pid_t);
    139 	int (*puffs_node_close)(struct puffs_cc *,
    140 	    void *, int, const struct puffs_cred *, pid_t);
    141 	int (*puffs_node_access)(struct puffs_cc *,
    142 	    void *, int, const struct puffs_cred *, pid_t);
    143 	int (*puffs_node_getattr)(struct puffs_cc *,
    144 	    void *, struct vattr *, const struct puffs_cred *, pid_t);
    145 	int (*puffs_node_setattr)(struct puffs_cc *,
    146 	    void *, const struct vattr *, const struct puffs_cred *, pid_t);
    147 	int (*puffs_node_poll)(struct puffs_cc *,
    148 	    void *, struct puffs_vnreq_poll *);
    149 	int (*puffs_node_revoke)(struct puffs_cc *, void *, int);
    150 	int (*puffs_node_mmap)(struct puffs_cc *,
    151 	    void *, int, const struct puffs_cred *, pid_t);
    152 	int (*puffs_node_fsync)(struct puffs_cc *,
    153 	    void *, const struct puffs_cred *, int, off_t, off_t, pid_t);
    154 	int (*puffs_node_seek)(struct puffs_cc *,
    155 	    void *, off_t, off_t, const struct puffs_cred *);
    156 	int (*puffs_node_remove)(struct puffs_cc *,
    157 	    void *, void *, const struct puffs_cn *);
    158 	int (*puffs_node_link)(struct puffs_cc *,
    159 	    void *, void *, const struct puffs_cn *);
    160 	int (*puffs_node_rename)(struct puffs_cc *,
    161 	    void *, void *, const struct puffs_cn *, void *, void *,
    162 	    const struct puffs_cn *);
    163 	int (*puffs_node_mkdir)(struct puffs_cc *,
    164 	    void *, void **, const struct puffs_cn *, const struct vattr *);
    165 	int (*puffs_node_rmdir)(struct puffs_cc *,
    166 	    void *, void *, const struct puffs_cn *);
    167 	int (*puffs_node_symlink)(struct puffs_cc *,
    168 	    void *, void **, const struct puffs_cn *, const struct vattr *,
    169 	    const char *);
    170 	int (*puffs_node_readdir)(struct puffs_cc *,
    171 	    void *, struct dirent *, const struct puffs_cred *,
    172 	    off_t *, size_t *);
    173 	int (*puffs_node_readlink)(struct puffs_cc *,
    174 	    void *, const struct puffs_cred *, char *, size_t *);
    175 	int (*puffs_node_reclaim)(struct puffs_cc *,
    176 	    void *, pid_t);
    177 	int (*puffs_node_inactive)(struct puffs_cc *,
    178 	    void *, pid_t, int *);
    179 	int (*puffs_node_print)(struct puffs_cc *,
    180 	    void *);
    181 	int (*puffs_node_pathconf)(struct puffs_cc *,
    182 	    void *, int, int *);
    183 	int (*puffs_node_advlock)(struct puffs_cc *,
    184 	    void *, void *, int, struct flock *, int);
    185 	int (*puffs_node_getextattr)(struct puffs_cc *,
    186 	    void *, struct puffs_vnreq_getextattr *);
    187 	int (*puffs_node_setextattr)(struct puffs_cc *,
    188 	    void *, struct puffs_vnreq_setextattr *);
    189 	int (*puffs_node_listextattr)(struct puffs_cc *,
    190 	    void *, struct puffs_vnreq_listextattr *);
    191 	int (*puffs_node_read)(struct puffs_cc *, void *,
    192 	    uint8_t *, off_t, size_t *, const struct puffs_cred *, int);
    193 	int (*puffs_node_write)(struct puffs_cc *, void *,
    194 	    uint8_t *, off_t, size_t *, const struct puffs_cred *, int);
    195 };
    196 
    197 struct puffs_usermount {
    198 	struct puffs_ops	pu_ops;
    199 
    200 	int			pu_fd;
    201 	uint32_t		pu_flags;
    202 	size_t			pu_maxreqlen;
    203 	size_t			pu_cc_stacksize;
    204 
    205 	int			pu_state;
    206 
    207 	struct puffs_node	*pu_pn_root;
    208 
    209 	LIST_HEAD(, puffs_node)	pu_pnodelst;
    210 
    211 	int	pu_wcnt;
    212 	void	*pu_privdata;
    213 };
    214 
    215 enum {
    216 	PUFFS_STATE_MOUNTING,	PUFFS_STATE_RUNNING,
    217 	PUFFS_STATE_UNMOUNTING,	PUFFS_STATE_UNMOUNTED
    218 };
    219 
    220 #define PUFFS_FLAG_KERN(a)	((a) & 0x0000ffff)
    221 #define PUFFS_FLAG_LIB(a)	((a) & 0xffff0000)
    222 
    223 #define PUFFS_FLAG_BUILDPATH	0x80000000	/* node paths in pnode */
    224 #define PUFFS_FLAG_OPDUMP	0x40000000	/* dump all operations */
    225 
    226 /* blocking mode argument */
    227 #define PUFFSDEV_BLOCK 0
    228 #define PUFFSDEV_NONBLOCK 1
    229 
    230 /* mainloop flags */
    231 #define PUFFSLOOP_NODAEMON 0x01
    232 
    233 int  puffs_fsnop_unmount(struct puffs_cc *, int, pid_t);
    234 int  puffs_fsnop_statvfs(struct puffs_cc *, struct statvfs *, pid_t);
    235 void puffs_zerostatvfs(struct statvfs *);
    236 int  puffs_fsnop_sync(struct puffs_cc *, int waitfor,
    237 		      const struct puffs_cred *, pid_t);
    238 
    239 #define		DENT_DOT	0
    240 #define		DENT_DOTDOT	1
    241 #define		DENT_ADJ(a)	((a)-2)	/* nth request means dir's n-2th */
    242 
    243 
    244 /*
    245  * Operation credentials
    246  */
    247 
    248 /* Credential fetch */
    249 int	puffs_cred_getuid(const struct puffs_cred *pcr, uid_t *);
    250 int	puffs_cred_getgid(const struct puffs_cred *pcr, gid_t *);
    251 int	puffs_cred_getgroups(const struct puffs_cred *pcr, gid_t *, short *);
    252 
    253 /* Credential check */
    254 int	puffs_cred_isuid(const struct puffs_cred *pcr, uid_t);
    255 int	puffs_cred_hasgroup(const struct puffs_cred *pcr, gid_t);
    256 /* kernel internal NOCRED */
    257 int	puffs_cred_iskernel(const struct puffs_cred *pcr);
    258 /* kernel internal FSCRED */
    259 int	puffs_cred_isfs(const struct puffs_cred *pcr);
    260 /* root || NOCRED || FSCRED */
    261 int	puffs_cred_isjuggernaut(const struct puffs_cred *pcr);
    262 
    263 
    264 /*
    265  * protos
    266  */
    267 
    268 #define PUFFSOP_PROTOS(fsname)						\
    269 	int fsname##_fs_mount(struct puffs_usermount *, void **,	\
    270 	    struct statvfs *);						\
    271 	int fsname##_fs_unmount(struct puffs_cc *, int, pid_t);		\
    272 	int fsname##_fs_statvfs(struct puffs_cc *,			\
    273 	    struct statvfs *, pid_t);					\
    274 	int fsname##_fs_sync(struct puffs_cc *, int,			\
    275 	    const struct puffs_cred *cred, pid_t);			\
    276 									\
    277 	int fsname##_node_lookup(struct puffs_cc *,			\
    278 	    void *, void **, enum vtype *, voff_t *, dev_t *,		\
    279 	    const struct puffs_cn *);					\
    280 	int fsname##_node_create(struct puffs_cc *,			\
    281 	    void *, void **, const struct puffs_cn *,			\
    282 	    const struct vattr *);					\
    283 	int fsname##_node_mknod(struct puffs_cc *,			\
    284 	    void *, void **, const struct puffs_cn *,			\
    285 	    const struct vattr *);					\
    286 	int fsname##_node_open(struct puffs_cc *,			\
    287 	    void *, int, const struct puffs_cred *, pid_t);		\
    288 	int fsname##_node_close(struct puffs_cc *,			\
    289 	    void *, int, const struct puffs_cred *, pid_t);		\
    290 	int fsname##_node_access(struct puffs_cc *,			\
    291 	    void *, int, const struct puffs_cred *, pid_t);		\
    292 	int fsname##_node_getattr(struct puffs_cc *,			\
    293 	    void *, struct vattr *, const struct puffs_cred *, pid_t);	\
    294 	int fsname##_node_setattr(struct puffs_cc *,			\
    295 	    void *, const struct vattr *, const struct puffs_cred *,	\
    296 	    pid_t);							\
    297 	int fsname##_node_poll(struct puffs_cc *,			\
    298 	    void *, struct puffs_vnreq_poll *);				\
    299 	int fsname##_node_revoke(struct puffs_cc *, void *, int);	\
    300 	int fsname##_node_mmap(struct puffs_cc *,			\
    301 	    void *, int, const struct puffs_cred *, pid_t);		\
    302 	int fsname##_node_fsync(struct puffs_cc *,			\
    303 	    void *, const struct puffs_cred *, int, off_t, off_t,	\
    304 	    pid_t);							\
    305 	int fsname##_node_seek(struct puffs_cc *,			\
    306 	    void *, off_t, off_t, const struct puffs_cred *);		\
    307 	int fsname##_node_remove(struct puffs_cc *,			\
    308 	    void *, void *, const struct puffs_cn *);			\
    309 	int fsname##_node_link(struct puffs_cc *,			\
    310 	    void *, void *, const struct puffs_cn *);			\
    311 	int fsname##_node_rename(struct puffs_cc *,			\
    312 	    void *, void *, const struct puffs_cn *, void *, void *,	\
    313 	    const struct puffs_cn *);					\
    314 	int fsname##_node_mkdir(struct puffs_cc *,			\
    315 	    void *, void **, const struct puffs_cn *,			\
    316 	    const struct vattr *);					\
    317 	int fsname##_node_rmdir(struct puffs_cc *,			\
    318 	    void *, void *, const struct puffs_cn *);			\
    319 	int fsname##_node_symlink(struct puffs_cc *,			\
    320 	    void *, void **, const struct puffs_cn *,			\
    321 	    const struct vattr *, const char *);			\
    322 	int fsname##_node_readdir(struct puffs_cc *,			\
    323 	    void *, struct dirent *, const struct puffs_cred *,		\
    324 	    off_t *, size_t *);						\
    325 	int fsname##_node_readlink(struct puffs_cc *,			\
    326 	    void *, const struct puffs_cred *, char *, size_t *);	\
    327 	int fsname##_node_reclaim(struct puffs_cc *,			\
    328 	    void *, pid_t);						\
    329 	int fsname##_node_inactive(struct puffs_cc *,			\
    330 	    void *, pid_t, int *);					\
    331 	int fsname##_node_print(struct puffs_cc *,			\
    332 	    void *);							\
    333 	int fsname##_node_pathconf(struct puffs_cc *,			\
    334 	    void *, int, int *);					\
    335 	int fsname##_node_advlock(struct puffs_cc *,			\
    336 	    void *, void *, int, struct flock *, int);			\
    337 	int fsname##_node_getextattr(struct puffs_cc *,			\
    338 	    void *, struct puffs_vnreq_getextattr *);			\
    339 	int fsname##_node_setextattr(struct puffs_cc *,			\
    340 	    void *, struct puffs_vnreq_setextattr *);			\
    341 	int fsname##_node_listextattr(struct puffs_cc *,		\
    342 	    void *, struct puffs_vnreq_listextattr *);			\
    343 	int fsname##_node_read(struct puffs_cc *, void *,		\
    344 	    uint8_t *, off_t, size_t *, const struct puffs_cred *, int);\
    345 	int fsname##_node_write(struct puffs_cc *, void *,		\
    346 	    uint8_t *, off_t, size_t *, const struct puffs_cred *, int);
    347 
    348 #define PUFFSOP_INIT(ops)						\
    349     ops = malloc(sizeof(struct puffs_ops));				\
    350     memset(ops, 0, sizeof(struct puffs_ops))
    351 #define PUFFSOP_SET(ops, fsname, fsornode, opname)			\
    352     (ops)->puffs_##fsornode##_##opname = fsname##_##fsornode##_##opname
    353 #define PUFFSOP_SETFSNOP(ops, opname)					\
    354     (ops)->puffs_fs_##opname = puffs_fsnop_##opname
    355 
    356 #define PUFFS_DEVEL_LIBVERSION 2
    357 #define puffs_mount(a,b,c,d,e,f,g) \
    358     _puffs_mount(PUFFS_DEVEL_LIBVERSION,a,b,c,d,e,f,g)
    359 
    360 __BEGIN_DECLS
    361 
    362 struct puffs_usermount *_puffs_mount(int, struct puffs_ops *, const char *, int,
    363 				    const char *, void *, uint32_t, size_t);
    364 int		puffs_start(struct puffs_usermount *, void *, struct statvfs *);
    365 int		puffs_exit(struct puffs_usermount *, int);
    366 int		puffs_mainloop(struct puffs_usermount *, int);
    367 
    368 
    369 int	puffs_getselectable(struct puffs_usermount *);
    370 int	puffs_setblockingmode(struct puffs_usermount *, int);
    371 int	puffs_getstate(struct puffs_usermount *);
    372 int	puffs_setrootpath(struct puffs_usermount *, const char *);
    373 void	puffs_setstacksize(struct puffs_usermount *, size_t);
    374 
    375 struct puffs_node	*puffs_pn_new(struct puffs_usermount *, void *);
    376 void			puffs_pn_put(struct puffs_node *);
    377 void			*puffs_pn_nodewalk(struct puffs_usermount *pu,
    378 					  void*(*fn)(struct puffs_node*,void*),
    379 					  void *);
    380 
    381 void			puffs_setvattr(struct vattr *, const struct vattr *);
    382 void			puffs_vattr_null(struct vattr *);
    383 
    384 /*
    385  * Subroutine stuff
    386  */
    387 
    388 int		puffs_gendotdent(struct dirent **, ino_t, int, size_t *);
    389 int		puffs_nextdent(struct dirent **, const char *, ino_t,
    390 			       uint8_t, size_t *);
    391 int		puffs_vtype2dt(enum vtype);
    392 enum vtype	puffs_mode2vt(mode_t);
    393 void		puffs_stat2vattr(struct vattr *va, const struct stat *);
    394 
    395 /*
    396  * Requests
    397  */
    398 
    399 struct puffs_getreq	*puffs_makegetreq(struct puffs_usermount *,
    400 					  size_t, int);
    401 int			puffs_loadgetreq(struct puffs_getreq *);
    402 struct puffs_req	*puffs_getreq(struct puffs_getreq *);
    403 int			puffs_remaininggetreq(struct puffs_getreq *);
    404 void			puffs_setmaxgetreq(struct puffs_getreq *, int);
    405 void			puffs_destroygetreq(struct puffs_getreq *);
    406 
    407 struct puffs_putreq	*puffs_makeputreq(struct puffs_usermount *);
    408 void			puffs_putreq(struct puffs_putreq *, struct puffs_req *);
    409 void			puffs_putreq_cc(struct puffs_putreq *,struct puffs_cc*);
    410 int			puffs_putputreq(struct puffs_putreq *);
    411 void			puffs_resetputreq(struct puffs_putreq *);
    412 void			puffs_destroyputreq(struct puffs_putreq *);
    413 
    414 /*
    415  * Call Context interfaces relevant for user.
    416  */
    417 
    418 void			puffs_cc_yield(struct puffs_cc *);
    419 void			puffs_cc_continue(struct puffs_cc *);
    420 struct puffs_usermount	*puffs_cc_getusermount(struct puffs_cc *);
    421 struct puffs_cc 	*puffs_cc_create(struct puffs_usermount *);
    422 void			puffs_cc_destroy(struct puffs_cc *);
    423 
    424 /*
    425  * Execute or continue a request
    426  */
    427 
    428 int	puffs_handlereqs(struct puffs_usermount *, struct puffs_getreq *,
    429 			 struct puffs_putreq *, int);
    430 
    431 int	puffs_dopreq(struct puffs_usermount *, struct puffs_putreq *,
    432 		     struct puffs_req *);
    433 int	puffs_docc(struct puffs_putreq *, struct puffs_cc *);
    434 
    435 /*
    436  * Flushing / invalidation routines
    437  */
    438 
    439 int	puffs_inval_namecache_dir(struct puffs_usermount *, void *);
    440 int	puffs_inval_namecache_all(struct puffs_usermount *);
    441 
    442 __END_DECLS
    443 
    444 #endif /* _PUFFS_H_ */
    445