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