xref: /minix/lib/libpuffs/puffs_priv.h (revision 84d9c625)
1 /*	$NetBSD: puffs_priv.h,v 1.45 2012/04/18 00:57:22 manu Exp $	*/
2 
3 /*
4  * Copyright (c) 2006, 2007, 2008 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 #if !defined(__minix)
35 #include <pthread.h>
36 #endif /* !defined(__minix) */
37 #include <puffs.h>
38 #include <ucontext.h>
39 
40 #if defined(__minix)
41 
42 /* XXX: MINIX */
43 #define IGN_PERM            0
44 #define CHK_PERM            1
45 #define SU_UID          ((uid_t) 0)     /* super_user's uid_t */
46 
47 /* XXX: MINIX */
48 #define ATIME            002    /* set if atime field needs updating */
49 #define CTIME            004    /* set if ctime field needs updating */
50 #define MTIME            010    /* set if mtime field needs updating */
51 
52 #define REQ_READ_SUPER   28
53 
54 #define NUL(str,l,m) mfs_nul_f(__FILE__,__LINE__,(str), (l), (m))
55 
56 #else
57 extern pthread_mutex_t pu_lock;
58 #define PU_LOCK() pthread_mutex_lock(&pu_lock)
59 #define PU_UNLOCK() pthread_mutex_unlock(&pu_lock)
60 #endif /* defined(__minix) */
61 #if defined(__minix)
62 #define PU_LOCK() /* nothing */
63 #define PU_UNLOCK()  /* nothing */
64 #endif /* defined(__minix) */
65 
66 #define PU_CMAP(pu, c) (pu->pu_cmap ? pu->pu_cmap(pu,c) : (struct puffs_node*)c)
67 
68 struct puffs_framectrl {
69 	puffs_framev_readframe_fn rfb;
70 	puffs_framev_writeframe_fn wfb;
71 	puffs_framev_cmpframe_fn cmpfb;
72 	puffs_framev_gotframe_fn gotfb;
73 	puffs_framev_fdnotify_fn fdnotfn;
74 };
75 
76 struct puffs_fctrl_io {
77 	struct puffs_framectrl *fctrl;
78 
79 	int io_fd;
80 	int stat;
81 
82 	int rwait;
83 	int wwait;
84 
85 	struct puffs_framebuf *cur_in;
86 
87 	TAILQ_HEAD(, puffs_framebuf) snd_qing;	/* queueing to be sent */
88 	TAILQ_HEAD(, puffs_framebuf) res_qing;	/* q'ing for rescue */
89 	LIST_HEAD(, puffs_fbevent) ev_qing;	/* q'ing for events */
90 
91 	LIST_ENTRY(puffs_fctrl_io) fio_entries;
92 };
93 #define FIO_WR		0x01
94 #define FIO_WRGONE	0x02
95 #define FIO_RDGONE	0x04
96 #define FIO_DEAD	0x08
97 #define FIO_ENABLE_R	0x10
98 #define FIO_ENABLE_W	0x20
99 
100 #define FIO_EN_WRITE(fio)				\
101     (!(fio->stat & FIO_WR)				\
102       && ((!TAILQ_EMPTY(&fio->snd_qing)			\
103             && (fio->stat & FIO_ENABLE_W))		\
104          || fio->wwait))
105 
106 #define FIO_RM_WRITE(fio)			\
107     ((fio->stat & FIO_WR)			\
108       && (((TAILQ_EMPTY(&fio->snd_qing)		\
109         || (fio->stat & FIO_ENABLE_W) == 0))	\
110 	&& (fio->wwait == 0)))
111 
112 
113 /*
114  * usermount: describes one file system instance
115  */
116 struct puffs_usermount {
117 	struct puffs_ops	pu_ops;
118 
119 	int			pu_fd;
120 	size_t			pu_maxreqlen;
121 
122 	uint32_t		pu_flags;
123 	int			pu_cc_stackshift;
124 
125 	ucontext_t		pu_mainctx;
126 #define PUFFS_CCMAXSTORE 32
127 	int			pu_cc_nstored;
128 
129 	int			pu_kq;
130 	int			pu_state;
131 #define PU_STATEMASK	0x00ff
132 #define PU_INLOOP	0x0100
133 #define PU_ASYNCFD	0x0200
134 #define PU_HASKQ	0x0400
135 #define PU_PUFFSDAEMON	0x0800
136 #define PU_MAINRESTORE	0x1000
137 #define PU_DONEXIT	0x2000
138 #define PU_SETSTATE(pu, s) (pu->pu_state = (s) | (pu->pu_state & ~PU_STATEMASK))
139 #define PU_SETSFLAG(pu, s) (pu->pu_state |= (s))
140 #define PU_CLRSFLAG(pu, s) \
141     (pu->pu_state = ((pu->pu_state & ~(s)) | (pu->pu_state & PU_STATEMASK)))
142 	int			pu_dpipe[2];
143 
144 	struct puffs_node	*pu_pn_root;
145 
146 	LIST_HEAD(, puffs_node)	pu_pnodelst;
147 #if defined(__minix) // LSC TO KEEP??
148 	LIST_HEAD(, puffs_node)	pu_pnode_removed_lst;
149 #endif /* defined(__minix) */
150 
151 	LIST_HEAD(, puffs_cc)	pu_ccmagazin;
152 	TAILQ_HEAD(, puffs_cc)	pu_lazyctx;
153 	TAILQ_HEAD(, puffs_cc)	pu_sched;
154 
155 	pu_cmap_fn		pu_cmap;
156 
157 	pu_pathbuild_fn		pu_pathbuild;
158 	pu_pathtransform_fn	pu_pathtransform;
159 	pu_pathcmp_fn		pu_pathcmp;
160 	pu_pathfree_fn		pu_pathfree;
161 	pu_namemod_fn		pu_namemod;
162 
163 	pu_errnotify_fn		pu_errnotify;
164 
165 	pu_prepost_fn		pu_oppre;
166 	pu_prepost_fn		pu_oppost;
167 
168 	struct puffs_framectrl	pu_framectrl[2];
169 #define PU_FRAMECTRL_FS   0
170 #define PU_FRAMECTRL_USER 1
171 	LIST_HEAD(, puffs_fctrl_io) pu_ios;
172 	LIST_HEAD(, puffs_fctrl_io) pu_ios_rmlist;
173 	struct kevent		*pu_evs;
174 	size_t			pu_nevs;
175 
176 	puffs_ml_loop_fn	pu_ml_lfn;
177 	struct timespec		pu_ml_timeout;
178 	struct timespec		*pu_ml_timep;
179 
180 	struct puffs_kargs	*pu_kargp;
181 
182 	uint64_t		pu_nextreq;
183 	void			*pu_privdata;
184 };
185 
186 /* call context */
187 
188 struct puffs_cc;
189 typedef void (*puffs_ccfunc)(struct puffs_cc *);
190 
191 struct puffs_cc {
192 	struct puffs_usermount	*pcc_pu;
193 	struct puffs_framebuf	*pcc_pb;
194 
195 	/* real cc */
196 	union {
197 		struct {
198 			ucontext_t	uc;		/* "continue"	*/
199 			ucontext_t	uc_ret;		/* "yield" 	*/
200 		} real;
201 		struct {
202 			puffs_ccfunc	func;
203 			void		*farg;
204 		} fake;
205 	} pcc_u;
206 
207 	pid_t			pcc_pid;
208 	lwpid_t			pcc_lid;
209 
210 	int			pcc_flags;
211 
212 	TAILQ_ENTRY(puffs_cc)	pcc_schedent;
213 	LIST_ENTRY(puffs_cc)	pcc_rope;
214 };
215 #define pcc_uc		pcc_u.real.uc
216 #define pcc_uc_ret 	pcc_u.real.uc_ret
217 #define pcc_func	pcc_u.fake.func
218 #define pcc_farg	pcc_u.fake.farg
219 #define PCC_DONE	0x01
220 #define PCC_BORROWED	0x02
221 #define PCC_HASCALLER	0x04
222 #define PCC_MLCONT	0x08
223 
224 struct puffs_newinfo {
225 	void		**pni_cookie;
226 	enum vtype	*pni_vtype;
227 	voff_t		*pni_size;
228 	dev_t		*pni_rdev;
229 	struct vattr	*pni_va;
230 	struct timespec	*pni_va_ttl;
231 	struct timespec	*pni_cn_ttl;
232 };
233 
234 #define PUFFS_MAKEKCRED(to, from)					\
235 	/*LINTED: tnilxnaht, the cast is ok */				\
236 	const struct puffs_kcred *to = (const void *)from
237 #define PUFFS_MAKECRED(to, from)					\
238 	/*LINTED: tnilxnaht, the cast is ok */				\
239 	const struct puffs_cred *to = (const void *)from
240 #define PUFFS_KCREDTOCRED(to, from)					\
241 	/*LINTED: tnilxnaht, the cast is ok */				\
242 	to = (void *)from
243 
244 __BEGIN_DECLS
245 
246 void	puffs__framev_input(struct puffs_usermount *, struct puffs_framectrl *,
247 			   struct puffs_fctrl_io *);
248 int	puffs__framev_output(struct puffs_usermount *, struct puffs_framectrl*,
249 			    struct puffs_fctrl_io *);
250 void	puffs__framev_exit(struct puffs_usermount *);
251 void	puffs__framev_readclose(struct puffs_usermount *,
252 			       struct puffs_fctrl_io *, int);
253 void	puffs__framev_writeclose(struct puffs_usermount *,
254 				struct puffs_fctrl_io *, int);
255 void	puffs__framev_notify(struct puffs_fctrl_io *, int);
256 void	*puffs__framebuf_getdataptr(struct puffs_framebuf *);
257 int	puffs__framev_addfd_ctrl(struct puffs_usermount *, int, int,
258 				 struct puffs_framectrl *);
259 void	puffs__framebuf_moveinfo(struct puffs_framebuf *,
260 				 struct puffs_framebuf *);
261 
262 void	puffs__theloop(struct puffs_cc *);
263 void	puffs__ml_dispatch(struct puffs_usermount *, struct puffs_framebuf *);
264 
265 int	puffs__cc_create(struct puffs_usermount *, puffs_ccfunc,
266 			 struct puffs_cc **);
267 void	puffs__cc_cont(struct puffs_cc *);
268 void	puffs__cc_destroy(struct puffs_cc *, int);
269 void	puffs__cc_setcaller(struct puffs_cc *, pid_t, lwpid_t);
270 void	puffs__goto(struct puffs_cc *);
271 int	puffs__cc_savemain(struct puffs_usermount *);
272 int	puffs__cc_restoremain(struct puffs_usermount *);
273 void	puffs__cc_exit(struct puffs_usermount *);
274 
275 int	puffs__fsframe_read(struct puffs_usermount *, struct puffs_framebuf *,
276 			    int, int *);
277 int	puffs__fsframe_write(struct puffs_usermount *, struct puffs_framebuf *,
278 			    int, int *);
279 int	puffs__fsframe_cmp(struct puffs_usermount *, struct puffs_framebuf *,
280 			   struct puffs_framebuf *, int *);
281 void	puffs__fsframe_gotframe(struct puffs_usermount *,
282 			        struct puffs_framebuf *);
283 
284 __END_DECLS
285 
286 #endif /* _PUFFS_PRIVATE_H_ */
287