Home | History | Annotate | Line # | Download | only in libpuffs
puffs_priv.h revision 1.31
      1 /*	$NetBSD: puffs_priv.h,v 1.31 2007/10/31 16:09:09 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2006 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 #ifndef _PUFFS_PRIVATE_H_
     29 #define _PUFFS_PRIVATE_H_
     30 
     31 #include <sys/types.h>
     32 #include <fs/puffs/puffs_msgif.h>
     33 
     34 #include <puffs.h>
     35 #include <ucontext.h>
     36 
     37 #ifdef PUFFS_WITH_THREADS
     38 #include <pthread.h>
     39 
     40 extern pthread_mutex_t pu_lock;
     41 #define PU_LOCK() pthread_mutex_lock(&pu_lock)
     42 #define PU_UNLOCK() pthread_mutex_unlock(&pu_lock)
     43 #else
     44 #define PU_LOCK()
     45 #define PU_UNLOCK()
     46 #endif
     47 
     48 #define PU_CMAP(pu, c)	(pu->pu_cmap ? pu->pu_cmap(c) : (struct puffs_node *)c)
     49 
     50 struct puffs_framectrl {
     51 	puffs_framev_readframe_fn rfb;
     52 	puffs_framev_writeframe_fn wfb;
     53 	puffs_framev_cmpframe_fn cmpfb;
     54 	puffs_framev_gotframe_fn gotfb;
     55 	puffs_framev_fdnotify_fn fdnotfn;
     56 
     57 	struct kevent *evs;
     58 	size_t nfds;
     59 
     60 	struct timespec timeout;
     61 	struct timespec *timp;
     62 
     63 	LIST_HEAD(, puffs_fctrl_io) fb_ios;
     64 	LIST_HEAD(, puffs_fctrl_io) fb_ios_rmlist;
     65 };
     66 
     67 struct puffs_fctrl_io {
     68 	int io_fd;
     69 	int stat;
     70 
     71 	int rwait;
     72 	int wwait;
     73 
     74 	struct puffs_framebuf *cur_in;
     75 
     76 	TAILQ_HEAD(, puffs_framebuf) snd_qing;	/* queueing to be sent */
     77 	TAILQ_HEAD(, puffs_framebuf) res_qing;	/* q'ing for rescue */
     78 	LIST_HEAD(, puffs_fbevent) ev_qing;	/* q'ing for events */
     79 
     80 	LIST_ENTRY(puffs_fctrl_io) fio_entries;
     81 };
     82 #define FIO_WR		0x01
     83 #define FIO_WRGONE	0x02
     84 #define FIO_RDGONE	0x04
     85 #define FIO_DEAD	0x08
     86 #define FIO_ENABLE_R	0x10
     87 #define FIO_ENABLE_W	0x20
     88 
     89 #define FIO_EN_WRITE(fio)				\
     90     (!(fio->stat & FIO_WR)				\
     91       && ((!TAILQ_EMPTY(&fio->snd_qing)			\
     92             && (fio->stat & FIO_ENABLE_W))		\
     93          || fio->wwait))
     94 
     95 #define FIO_RM_WRITE(fio)			\
     96     ((fio->stat & FIO_WR)			\
     97       && (((TAILQ_EMPTY(&fio->snd_qing)		\
     98         || (fio->stat & FIO_ENABLE_W) == 0))	\
     99 	&& (fio->wwait == 0)))
    100 
    101 struct puffs_executor {
    102 	struct puffs_req		*pex_preq;
    103 	TAILQ_ENTRY(puffs_executor)	pex_entries;
    104 };
    105 
    106 /*
    107  * usermount: describes one file system instance
    108  */
    109 struct puffs_usermount {
    110 	struct puffs_ops	pu_ops;
    111 
    112 	int			pu_fd;
    113 	size_t			pu_maxreqlen;
    114 
    115 	uint32_t		pu_flags;
    116 	int			pu_cc_stackshift;
    117 
    118 	int			pu_kq;
    119 	int			pu_state;
    120 #define PU_STATEMASK	0xff
    121 #define PU_INLOOP	0x100
    122 #define PU_ASYNCFD	0x200
    123 #define PU_HASKQ	0x400
    124 #define PU_SETSTATE(pu, s) (pu->pu_state = (s) | (pu->pu_state & ~PU_STATEMASK))
    125 
    126 	struct puffs_node	*pu_pn_root;
    127 
    128 	LIST_HEAD(, puffs_node)	pu_pnodelst;
    129 	LIST_HEAD(, puffs_cc)	pu_ccnukelst;
    130 	TAILQ_HEAD(, puffs_cc)	pu_sched;
    131 
    132 	TAILQ_HEAD(, puffs_executor) pu_exq;
    133 
    134 	struct puffs_node	*(*pu_cmap)(void *);
    135 
    136 	pu_pathbuild_fn		pu_pathbuild;
    137 	pu_pathtransform_fn	pu_pathtransform;
    138 	pu_pathcmp_fn		pu_pathcmp;
    139 	pu_pathfree_fn		pu_pathfree;
    140 	pu_namemod_fn		pu_namemod;
    141 
    142 	pu_errnotify_fn		pu_errnotify;
    143 
    144 	pu_prepost_fn		pu_oppre;
    145 	pu_prepost_fn		pu_oppost;
    146 
    147 	struct puffs_framectrl	pu_framectrl;
    148 
    149 	puffs_ml_loop_fn	pu_ml_lfn;
    150 	struct timespec		pu_ml_timeout;
    151 	struct timespec		*pu_ml_timep;
    152 
    153 	struct puffs_kargs	*pu_kargp;
    154 
    155 	void			*pu_privdata;
    156 };
    157 
    158 /* call context */
    159 
    160 struct puffs_cc {
    161 	struct puffs_usermount	*pcc_pu;
    162 	struct puffs_req	*pcc_preq;
    163 
    164 	ucontext_t		pcc_uc;		/* "continue" 		*/
    165 	ucontext_t		pcc_uc_ret;	/* "yield" 		*/
    166 	void			*pcc_stack;
    167 
    168 	pid_t			pcc_pid;
    169 	lwpid_t			pcc_lid;
    170 
    171 	int			pcc_flags;
    172 	struct puffs_putreq	*pcc_ppr;
    173 
    174 	TAILQ_ENTRY(puffs_cc)	entries;
    175 	LIST_ENTRY(puffs_cc)	nlst_entries;
    176 };
    177 #define PCC_FAKECC	0x01
    178 #define PCC_REALCC	0x02
    179 #define PCC_DONE	0x04
    180 #define PCC_BORROWED	0x08
    181 #define PCC_HASCALLER	0x10
    182 #define PCC_THREADED	0x20
    183 
    184 #define pcc_callstat(a)	   (a->pcc_flags & PCC_CALL_MASK)
    185 #define pcc_callset(a, b)  (a->pcc_flags = (a->pcc_flags & ~PCC_CALL_MASK) | b)
    186 
    187 #define pcc_init_unreal(ap, type) 					\
    188 do {									\
    189 	memset(ap, 0, sizeof(*ap));					\
    190 	(ap)->pcc_flags = type;						\
    191 } while (/*CONSTCOND*/0)
    192 
    193 /*
    194  * Reqs
    195  */
    196 
    197 struct puffs_getreq {
    198 	struct puffs_usermount	*pgr_pu;
    199 
    200 	void			*pgr_buf;
    201 };
    202 
    203 struct puffs_putreq {
    204 	struct puffs_usermount *ppr_pu;
    205 
    206 	TAILQ_HEAD(, puffs_cc)	ppr_pccq;
    207 };
    208 
    209 struct puffs_newinfo {
    210 	void		**pni_cookie;
    211 	enum vtype	*pni_vtype;
    212 	voff_t		*pni_size;
    213 	dev_t		*pni_rdev;
    214 };
    215 
    216 #define PUFFS_MAKEKCRED(to, from)					\
    217 	/*LINTED: tnilxnaht, the cast is ok */				\
    218 	const struct puffs_kcred *to = (const void *)from
    219 #define PUFFS_MAKECRED(to, from)					\
    220 	/*LINTED: tnilxnaht, the cast is ok */				\
    221 	const struct puffs_cred *to = (const void *)from
    222 #define PUFFS_KCREDTOCRED(to, from)					\
    223 	/*LINTED: tnilxnaht, the cast is ok */				\
    224 	to = (void *)from
    225 
    226 #define PUFFS_MAKEKCID(to, from)					\
    227 	/*LINTED: tnilxnaht, the cast is ok */				\
    228 	const struct puffs_kcid *to = (const void *)from
    229 #define PUFFS_MAKECID(to, from)						\
    230 	/*LINTED: tnilxnaht, the cast is ok */				\
    231 	const struct puffs_cid *to = (const void *)from
    232 #define PUFFS_KCIDTOCID(to, from)					\
    233 	/*LINTED: tnilxnaht, the cast is ok */				\
    234 	to = (void *)from
    235 
    236 __BEGIN_DECLS
    237 
    238 void	puffs_calldispatcher(struct puffs_cc *);
    239 
    240 void	puffs_framev_input(struct puffs_usermount *, struct puffs_framectrl *,
    241 			   struct puffs_fctrl_io *, struct puffs_putreq *);
    242 int	puffs_framev_output(struct puffs_usermount *, struct puffs_framectrl*,
    243 			    struct puffs_fctrl_io *, struct puffs_putreq *);
    244 void	puffs_framev_exit(struct puffs_usermount *);
    245 void	puffs_framev_readclose(struct puffs_usermount *,
    246 			       struct puffs_fctrl_io *, int);
    247 void	puffs_framev_writeclose(struct puffs_usermount *,
    248 				struct puffs_fctrl_io *, int);
    249 void	puffs_framev_notify(struct puffs_fctrl_io *, int);
    250 
    251 int	puffs_cc_create(struct puffs_usermount *, struct puffs_req *,
    252 			int, struct puffs_cc **);
    253 void	puffs_cc_destroy(struct puffs_cc *);
    254 void	puffs_cc_setcaller(struct puffs_cc *, pid_t, lwpid_t);
    255 void	puffs_goto(struct puffs_cc *);
    256 
    257 __END_DECLS
    258 
    259 #endif /* _PUFFS_PRIVATE_H_ */
    260