puffs_msgif.c revision 1.52 1 1.52 ad /* $NetBSD: puffs_msgif.c,v 1.52 2007/11/07 00:23:20 ad Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.16 pooka * Copyright (c) 2005, 2006, 2007 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Development of this software was supported by the
7 1.1 pooka * Google Summer of Code program and the Ulla Tuominen Foundation.
8 1.1 pooka * The Google SoC project was mentored by Bill Studenmund.
9 1.1 pooka *
10 1.1 pooka * Redistribution and use in source and binary forms, with or without
11 1.1 pooka * modification, are permitted provided that the following conditions
12 1.1 pooka * are met:
13 1.1 pooka * 1. Redistributions of source code must retain the above copyright
14 1.1 pooka * notice, this list of conditions and the following disclaimer.
15 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 pooka * notice, this list of conditions and the following disclaimer in the
17 1.1 pooka * documentation and/or other materials provided with the distribution.
18 1.1 pooka *
19 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 pooka * SUCH DAMAGE.
30 1.1 pooka */
31 1.1 pooka
32 1.1 pooka #include <sys/cdefs.h>
33 1.52 ad __KERNEL_RCSID(0, "$NetBSD: puffs_msgif.c,v 1.52 2007/11/07 00:23:20 ad Exp $");
34 1.1 pooka
35 1.1 pooka #include <sys/param.h>
36 1.16 pooka #include <sys/fstrans.h>
37 1.46 pooka #include <sys/kmem.h>
38 1.1 pooka #include <sys/malloc.h>
39 1.1 pooka #include <sys/mount.h>
40 1.1 pooka #include <sys/vnode.h>
41 1.1 pooka #include <sys/lock.h>
42 1.39 ad #include <sys/proc.h>
43 1.1 pooka
44 1.1 pooka #include <fs/puffs/puffs_msgif.h>
45 1.1 pooka #include <fs/puffs/puffs_sys.h>
46 1.1 pooka
47 1.22 pooka /*
48 1.22 pooka * waitq data structures
49 1.22 pooka */
50 1.22 pooka
51 1.22 pooka /*
52 1.22 pooka * While a request is going to userspace, park the caller within the
53 1.22 pooka * kernel. This is the kernel counterpart of "struct puffs_req".
54 1.22 pooka */
55 1.46 pooka struct puffs_msgpark {
56 1.22 pooka struct puffs_req *park_preq; /* req followed by buf */
57 1.22 pooka
58 1.22 pooka size_t park_copylen; /* userspace copylength */
59 1.22 pooka size_t park_maxlen; /* max size in comeback */
60 1.24 pooka
61 1.46 pooka parkdone_fn park_done; /* "biodone" a'la puffs */
62 1.24 pooka void *park_donearg;
63 1.22 pooka
64 1.22 pooka int park_flags;
65 1.26 pooka int park_refcount;
66 1.22 pooka
67 1.22 pooka kcondvar_t park_cv;
68 1.26 pooka kmutex_t park_mtx;
69 1.26 pooka
70 1.46 pooka TAILQ_ENTRY(puffs_msgpark) park_entries;
71 1.22 pooka };
72 1.22 pooka #define PARKFLAG_WAITERGONE 0x01
73 1.26 pooka #define PARKFLAG_DONE 0x02
74 1.26 pooka #define PARKFLAG_ONQUEUE1 0x04
75 1.26 pooka #define PARKFLAG_ONQUEUE2 0x08
76 1.26 pooka #define PARKFLAG_CALL 0x10
77 1.31 pooka #define PARKFLAG_WANTREPLY 0x20
78 1.22 pooka
79 1.52 ad static pool_cache_t parkpc;
80 1.22 pooka
81 1.22 pooka static int
82 1.22 pooka makepark(void *arg, void *obj, int flags)
83 1.22 pooka {
84 1.46 pooka struct puffs_msgpark *park = obj;
85 1.22 pooka
86 1.26 pooka mutex_init(&park->park_mtx, MUTEX_DEFAULT, IPL_NONE);
87 1.22 pooka cv_init(&park->park_cv, "puffsrpl");
88 1.22 pooka
89 1.22 pooka return 0;
90 1.22 pooka }
91 1.22 pooka
92 1.22 pooka static void
93 1.22 pooka nukepark(void *arg, void *obj)
94 1.22 pooka {
95 1.46 pooka struct puffs_msgpark *park = obj;
96 1.22 pooka
97 1.22 pooka cv_destroy(&park->park_cv);
98 1.26 pooka mutex_destroy(&park->park_mtx);
99 1.22 pooka }
100 1.22 pooka
101 1.22 pooka void
102 1.22 pooka puffs_msgif_init()
103 1.22 pooka {
104 1.22 pooka
105 1.52 ad parkpc = pool_cache_init(sizeof(struct puffs_msgpark), 0, 0, 0,
106 1.52 ad "puffprkl", NULL, IPL_NONE, makepark, nukepark, NULL);
107 1.22 pooka }
108 1.22 pooka
109 1.22 pooka void
110 1.22 pooka puffs_msgif_destroy()
111 1.22 pooka {
112 1.22 pooka
113 1.52 ad pool_cache_destroy(parkpc);
114 1.22 pooka }
115 1.22 pooka
116 1.46 pooka static int alloced;
117 1.46 pooka
118 1.46 pooka static struct puffs_msgpark *
119 1.46 pooka puffs_msgpark_alloc(int waitok)
120 1.26 pooka {
121 1.46 pooka struct puffs_msgpark *park;
122 1.26 pooka
123 1.52 ad park = pool_cache_get(parkpc, waitok ? PR_WAITOK : PR_NOWAIT);
124 1.46 pooka if (park == NULL)
125 1.46 pooka return park;
126 1.46 pooka
127 1.46 pooka park->park_refcount = 1;
128 1.46 pooka park->park_preq = NULL;
129 1.46 pooka park->park_flags = PARKFLAG_WANTREPLY;
130 1.26 pooka
131 1.26 pooka return park;
132 1.26 pooka }
133 1.26 pooka
134 1.26 pooka static void
135 1.46 pooka puffs_msgpark_reference(struct puffs_msgpark *park)
136 1.22 pooka {
137 1.22 pooka
138 1.46 pooka KASSERT(mutex_owned(&park->park_mtx));
139 1.26 pooka park->park_refcount++;
140 1.22 pooka }
141 1.22 pooka
142 1.46 pooka /*
143 1.46 pooka * Release reference to park structure.
144 1.46 pooka */
145 1.46 pooka static void
146 1.46 pooka puffs_msgpark_release1(struct puffs_msgpark *park, int howmany)
147 1.22 pooka {
148 1.46 pooka struct puffs_req *preq = park->park_preq;
149 1.46 pooka int refcnt;
150 1.22 pooka
151 1.26 pooka KASSERT(mutex_owned(&park->park_mtx));
152 1.46 pooka refcnt = park->park_refcount -= howmany;
153 1.46 pooka mutex_exit(&park->park_mtx);
154 1.46 pooka
155 1.46 pooka KASSERT(refcnt >= 0);
156 1.26 pooka
157 1.46 pooka if (refcnt == 0) {
158 1.46 pooka alloced--;
159 1.46 pooka if (preq)
160 1.46 pooka kmem_free(preq, park->park_maxlen);
161 1.52 ad pool_cache_put(parkpc, park);
162 1.46 pooka }
163 1.22 pooka }
164 1.46 pooka #define puffs_msgpark_release(a) puffs_msgpark_release1(a, 1)
165 1.22 pooka
166 1.26 pooka #ifdef PUFFSDEBUG
167 1.26 pooka static void
168 1.46 pooka parkdump(struct puffs_msgpark *park)
169 1.26 pooka {
170 1.26 pooka
171 1.26 pooka DPRINTF(("park %p, preq %p, id %" PRIu64 "\n"
172 1.26 pooka "\tcopy %zu, max %zu - done: %p/%p\n"
173 1.26 pooka "\tflags 0x%08x, refcount %d, cv/mtx: %p/%p\n",
174 1.46 pooka park, park->park_preq, park->park_preq->preq_id,
175 1.26 pooka park->park_copylen, park->park_maxlen,
176 1.26 pooka park->park_done, park->park_donearg,
177 1.26 pooka park->park_flags, park->park_refcount,
178 1.26 pooka &park->park_cv, &park->park_mtx));
179 1.26 pooka }
180 1.26 pooka
181 1.26 pooka static void
182 1.26 pooka parkqdump(struct puffs_wq *q, int dumpall)
183 1.26 pooka {
184 1.46 pooka struct puffs_msgpark *park;
185 1.26 pooka int total = 0;
186 1.26 pooka
187 1.26 pooka TAILQ_FOREACH(park, q, park_entries) {
188 1.26 pooka if (dumpall)
189 1.26 pooka parkdump(park);
190 1.26 pooka total++;
191 1.26 pooka }
192 1.29 pooka DPRINTF(("puffs waitqueue at %p dumped, %d total\n", q, total));
193 1.26 pooka
194 1.26 pooka }
195 1.26 pooka #endif /* PUFFSDEBUG */
196 1.22 pooka
197 1.22 pooka /*
198 1.46 pooka * A word about locking in the park structures: the lock protects the
199 1.46 pooka * fields of the *park* structure (not preq) and acts as an interlock
200 1.46 pooka * in cv operations. The lock is always internal to this module and
201 1.46 pooka * callers do not need to worry about it.
202 1.22 pooka */
203 1.46 pooka
204 1.46 pooka int
205 1.46 pooka puffs_msgmem_alloc(size_t len, struct puffs_msgpark **ppark, void **mem,
206 1.46 pooka int cansleep)
207 1.46 pooka {
208 1.46 pooka struct puffs_msgpark *park;
209 1.46 pooka void *m;
210 1.46 pooka
211 1.46 pooka m = kmem_zalloc(len, cansleep ? KM_SLEEP : KM_NOSLEEP);
212 1.46 pooka if (m == NULL) {
213 1.46 pooka KASSERT(cansleep == 0);
214 1.46 pooka return ENOMEM;
215 1.46 pooka }
216 1.46 pooka
217 1.46 pooka park = puffs_msgpark_alloc(cansleep);
218 1.46 pooka if (park == NULL) {
219 1.46 pooka KASSERT(cansleep == 0);
220 1.46 pooka kmem_free(m, len);
221 1.46 pooka return ENOMEM;
222 1.46 pooka }
223 1.46 pooka
224 1.46 pooka park->park_preq = m;
225 1.46 pooka park->park_maxlen = len;
226 1.46 pooka
227 1.46 pooka *ppark = park;
228 1.46 pooka *mem = m;
229 1.46 pooka
230 1.46 pooka return 0;
231 1.46 pooka }
232 1.46 pooka
233 1.46 pooka void
234 1.46 pooka puffs_msgmem_release(struct puffs_msgpark *park)
235 1.22 pooka {
236 1.22 pooka
237 1.46 pooka if (park == NULL)
238 1.46 pooka return;
239 1.22 pooka
240 1.46 pooka mutex_enter(&park->park_mtx);
241 1.46 pooka puffs_msgpark_release(park);
242 1.46 pooka }
243 1.22 pooka
244 1.46 pooka void
245 1.46 pooka puffs_msg_setfaf(struct puffs_msgpark *park)
246 1.46 pooka {
247 1.22 pooka
248 1.36 pooka park->park_flags &= ~PARKFLAG_WANTREPLY;
249 1.22 pooka }
250 1.22 pooka
251 1.1 pooka /*
252 1.1 pooka * kernel-user-kernel waitqueues
253 1.1 pooka */
254 1.1 pooka
255 1.46 pooka static int touser(struct puffs_mount *, struct puffs_msgpark *);
256 1.1 pooka
257 1.46 pooka static uint64_t
258 1.46 pooka puffs_getmsgid(struct puffs_mount *pmp)
259 1.1 pooka {
260 1.14 pooka uint64_t rv;
261 1.1 pooka
262 1.22 pooka mutex_enter(&pmp->pmp_lock);
263 1.46 pooka rv = pmp->pmp_nextmsgid++;
264 1.22 pooka mutex_exit(&pmp->pmp_lock);
265 1.1 pooka
266 1.1 pooka return rv;
267 1.1 pooka }
268 1.1 pooka
269 1.1 pooka /* vfs request */
270 1.1 pooka int
271 1.46 pooka puffs_msg_vfs(struct puffs_mount *pmp, struct puffs_msgpark *park, int optype)
272 1.1 pooka {
273 1.1 pooka
274 1.25 pooka park->park_preq->preq_opclass = PUFFSOP_VFS;
275 1.25 pooka park->park_preq->preq_optype = optype;
276 1.1 pooka
277 1.46 pooka park->park_copylen = park->park_maxlen;
278 1.1 pooka
279 1.45 pooka return touser(pmp, park);
280 1.1 pooka }
281 1.1 pooka
282 1.1 pooka /*
283 1.1 pooka * vnode level request
284 1.1 pooka */
285 1.1 pooka int
286 1.46 pooka puffs_msg_vn(struct puffs_mount *pmp, struct puffs_msgpark *park,
287 1.46 pooka int optype, size_t delta, struct vnode *vp_opc, struct vnode *vp_aux)
288 1.1 pooka {
289 1.35 pooka struct puffs_req *preq;
290 1.35 pooka void *cookie = VPTOPNC(vp_opc);
291 1.35 pooka struct puffs_node *pnode;
292 1.35 pooka int rv;
293 1.1 pooka
294 1.25 pooka park->park_preq->preq_opclass = PUFFSOP_VN;
295 1.25 pooka park->park_preq->preq_optype = optype;
296 1.25 pooka park->park_preq->preq_cookie = cookie;
297 1.25 pooka
298 1.46 pooka KASSERT(delta < park->park_maxlen); /* "<=" wouldn't make sense */
299 1.46 pooka park->park_copylen = park->park_maxlen - delta;
300 1.1 pooka
301 1.45 pooka rv = touser(pmp, park);
302 1.35 pooka
303 1.35 pooka /*
304 1.35 pooka * Check if the user server requests that inactive be called
305 1.35 pooka * when the time is right.
306 1.35 pooka */
307 1.35 pooka preq = park->park_preq;
308 1.35 pooka if (preq->preq_setbacks & PUFFS_SETBACK_INACT_N1) {
309 1.35 pooka pnode = vp_opc->v_data;
310 1.35 pooka pnode->pn_stat |= PNODE_DOINACT;
311 1.35 pooka }
312 1.35 pooka if (preq->preq_setbacks & PUFFS_SETBACK_INACT_N2) {
313 1.35 pooka /* if no vp_aux, just ignore */
314 1.35 pooka if (vp_aux) {
315 1.35 pooka pnode = vp_aux->v_data;
316 1.35 pooka pnode->pn_stat |= PNODE_DOINACT;
317 1.35 pooka }
318 1.35 pooka }
319 1.37 pooka if (preq->preq_setbacks & PUFFS_SETBACK_NOREF_N1) {
320 1.37 pooka pnode = vp_opc->v_data;
321 1.37 pooka pnode->pn_stat |= PNODE_NOREFS;
322 1.37 pooka }
323 1.37 pooka if (preq->preq_setbacks & PUFFS_SETBACK_NOREF_N2) {
324 1.37 pooka /* if no vp_aux, just ignore */
325 1.37 pooka if (vp_aux) {
326 1.37 pooka pnode = vp_aux->v_data;
327 1.37 pooka pnode->pn_stat |= PNODE_NOREFS;
328 1.37 pooka }
329 1.37 pooka }
330 1.35 pooka
331 1.35 pooka return rv;
332 1.1 pooka }
333 1.1 pooka
334 1.24 pooka void
335 1.46 pooka puffs_msg_vncall(struct puffs_mount *pmp, struct puffs_msgpark *park,
336 1.46 pooka int optype, size_t delta, parkdone_fn donefn, void *donearg,
337 1.46 pooka struct vnode *vp_opc)
338 1.1 pooka {
339 1.35 pooka void *cookie = VPTOPNC(vp_opc);
340 1.1 pooka
341 1.25 pooka park->park_preq->preq_opclass = PUFFSOP_VN;
342 1.25 pooka park->park_preq->preq_optype = optype;
343 1.25 pooka park->park_preq->preq_cookie = cookie;
344 1.25 pooka
345 1.46 pooka KASSERT(delta < park->park_maxlen);
346 1.46 pooka park->park_copylen = park->park_maxlen - delta;
347 1.25 pooka park->park_done = donefn;
348 1.25 pooka park->park_donearg = donearg;
349 1.46 pooka park->park_flags |= PARKFLAG_CALL;
350 1.1 pooka
351 1.45 pooka (void) touser(pmp, park);
352 1.20 pooka }
353 1.20 pooka
354 1.46 pooka int
355 1.46 pooka puffs_msg_raw(struct puffs_mount *pmp, struct puffs_msgpark *park)
356 1.4 pooka {
357 1.4 pooka
358 1.46 pooka park->park_copylen = park->park_maxlen;
359 1.4 pooka
360 1.46 pooka return touser(pmp, park);
361 1.4 pooka }
362 1.4 pooka
363 1.21 pooka void
364 1.46 pooka puffs_msg_errnotify(struct puffs_mount *pmp, uint8_t type, int error,
365 1.42 pooka const char *str, void *cookie)
366 1.41 pooka {
367 1.46 pooka struct puffs_msgpark *park;
368 1.41 pooka struct puffs_error *perr;
369 1.41 pooka
370 1.46 pooka puffs_msgmem_alloc(sizeof(struct puffs_error), &park, (void **)&perr,1);
371 1.41 pooka
372 1.41 pooka perr->perr_error = error;
373 1.42 pooka strlcpy(perr->perr_str, str, sizeof(perr->perr_str));
374 1.41 pooka
375 1.46 pooka park->park_preq->preq_opclass |= PUFFSOP_ERROR | PUFFSOPFLAG_FAF;
376 1.41 pooka park->park_preq->preq_optype = type;
377 1.41 pooka park->park_preq->preq_cookie = cookie;
378 1.41 pooka
379 1.46 pooka park->park_copylen = park->park_maxlen;
380 1.41 pooka
381 1.45 pooka (void)touser(pmp, park);
382 1.41 pooka }
383 1.41 pooka
384 1.4 pooka /*
385 1.51 pooka * Wait for the userspace ping-pong game in calling process context,
386 1.51 pooka * unless a FAF / async call, in which case just enqueues the request
387 1.51 pooka * and return immediately.
388 1.1 pooka */
389 1.1 pooka static int
390 1.46 pooka touser(struct puffs_mount *pmp, struct puffs_msgpark *park)
391 1.1 pooka {
392 1.26 pooka struct lwp *l = curlwp;
393 1.16 pooka struct mount *mp;
394 1.9 pooka struct puffs_req *preq;
395 1.19 pooka int rv = 0;
396 1.1 pooka
397 1.16 pooka mp = PMPTOMP(pmp);
398 1.25 pooka preq = park->park_preq;
399 1.46 pooka preq->preq_buflen = park->park_maxlen;
400 1.45 pooka KASSERT(preq->preq_id == 0);
401 1.46 pooka
402 1.46 pooka if ((park->park_flags & PARKFLAG_WANTREPLY) == 0)
403 1.46 pooka preq->preq_opclass |= PUFFSOPFLAG_FAF;
404 1.46 pooka else
405 1.46 pooka preq->preq_id = puffs_getmsgid(pmp);
406 1.31 pooka
407 1.49 pooka /* fill in caller information */
408 1.49 pooka preq->preq_pid = l->l_proc->p_pid;
409 1.49 pooka preq->preq_lid = l->l_lid;
410 1.49 pooka
411 1.19 pooka /*
412 1.51 pooka * To support cv_sig, yet another movie: check if there are signals
413 1.19 pooka * pending and we are issueing a non-FAF. If so, return an error
414 1.19 pooka * directly UNLESS we are issueing INACTIVE. In that case, convert
415 1.19 pooka * it to a FAF, fire off to the file server and return an error.
416 1.19 pooka * Yes, this is bordering disgusting. Barfbags are on me.
417 1.19 pooka */
418 1.31 pooka if ((park->park_flags & PARKFLAG_WANTREPLY)
419 1.25 pooka && (park->park_flags & PARKFLAG_CALL) == 0
420 1.19 pooka && (l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0)) {
421 1.19 pooka if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN
422 1.19 pooka && preq->preq_optype == PUFFS_VN_INACTIVE) {
423 1.46 pooka park->park_preq->preq_opclass |= PUFFSOPFLAG_FAF;
424 1.46 pooka park->park_flags &= ~PARKFLAG_WANTREPLY;
425 1.25 pooka DPRINTF(("puffs touser: converted to FAF %p\n", park));
426 1.19 pooka rv = EINTR;
427 1.19 pooka } else {
428 1.19 pooka return EINTR;
429 1.19 pooka }
430 1.19 pooka }
431 1.16 pooka
432 1.16 pooka /*
433 1.16 pooka * test for suspension lock.
434 1.16 pooka *
435 1.16 pooka * Note that we *DO NOT* keep the lock, since that might block
436 1.16 pooka * lock acquiring PLUS it would give userlandia control over
437 1.16 pooka * the lock. The operation queue enforces a strict ordering:
438 1.16 pooka * when the fs server gets in the op stream, it knows things
439 1.16 pooka * are in order. The kernel locks can't guarantee that for
440 1.16 pooka * userspace, in any case.
441 1.16 pooka *
442 1.16 pooka * BUT: this presents a problem for ops which have a consistency
443 1.16 pooka * clause based on more than one operation. Unfortunately such
444 1.16 pooka * operations (read, write) do not reliably work yet.
445 1.16 pooka *
446 1.16 pooka * Ya, Ya, it's wrong wong wrong, me be fixink this someday.
447 1.18 pooka *
448 1.18 pooka * XXX: and there is one more problem. We sometimes need to
449 1.18 pooka * take a lazy lock in case the fs is suspending and we are
450 1.18 pooka * executing as the fs server context. This might happen
451 1.18 pooka * e.g. in the case that the user server triggers a reclaim
452 1.18 pooka * in the kernel while the fs is suspending. It's not a very
453 1.18 pooka * likely event, but it needs to be fixed some day.
454 1.16 pooka */
455 1.22 pooka
456 1.22 pooka /*
457 1.22 pooka * MOREXXX: once PUFFS_WCACHEINFO is enabled, we can't take
458 1.22 pooka * the mutex here, since getpages() might be called locked.
459 1.22 pooka */
460 1.18 pooka fstrans_start(mp, FSTRANS_NORMAL);
461 1.22 pooka mutex_enter(&pmp->pmp_lock);
462 1.16 pooka fstrans_done(mp);
463 1.16 pooka
464 1.13 pooka if (pmp->pmp_status != PUFFSTAT_RUNNING) {
465 1.22 pooka mutex_exit(&pmp->pmp_lock);
466 1.1 pooka return ENXIO;
467 1.1 pooka }
468 1.1 pooka
469 1.26 pooka #ifdef PUFFSDEBUG
470 1.46 pooka parkqdump(&pmp->pmp_msg_touser, puffsdebug > 1);
471 1.46 pooka parkqdump(&pmp->pmp_msg_replywait, puffsdebug > 1);
472 1.26 pooka #endif
473 1.26 pooka
474 1.46 pooka mutex_enter(&park->park_mtx);
475 1.46 pooka TAILQ_INSERT_TAIL(&pmp->pmp_msg_touser, park, park_entries);
476 1.26 pooka park->park_flags |= PARKFLAG_ONQUEUE1;
477 1.34 pooka puffs_mp_reference(pmp);
478 1.46 pooka pmp->pmp_msg_touser_count++;
479 1.26 pooka mutex_exit(&pmp->pmp_lock);
480 1.1 pooka
481 1.20 pooka DPRINTF(("touser: req %" PRIu64 ", preq: %p, park: %p, "
482 1.25 pooka "c/t: 0x%x/0x%x, f: 0x%x\n", preq->preq_id, preq, park,
483 1.25 pooka preq->preq_opclass, preq->preq_optype, park->park_flags));
484 1.15 pooka
485 1.46 pooka cv_broadcast(&pmp->pmp_msg_waiter_cv);
486 1.1 pooka selnotify(pmp->pmp_sel, 0);
487 1.1 pooka
488 1.31 pooka if ((park->park_flags & PARKFLAG_WANTREPLY)
489 1.25 pooka && (park->park_flags & PARKFLAG_CALL) == 0) {
490 1.19 pooka int error;
491 1.19 pooka
492 1.26 pooka error = cv_wait_sig(&park->park_cv, &park->park_mtx);
493 1.46 pooka DPRINTF(("puffs_touser: waiter for %p woke up with %d\n",
494 1.46 pooka park, error));
495 1.19 pooka if (error) {
496 1.25 pooka park->park_flags |= PARKFLAG_WAITERGONE;
497 1.26 pooka if (park->park_flags & PARKFLAG_DONE) {
498 1.22 pooka rv = preq->preq_rv;
499 1.19 pooka } else {
500 1.26 pooka /*
501 1.26 pooka * ok, we marked it as going away, but
502 1.26 pooka * still need to do queue ops. take locks
503 1.26 pooka * in correct order.
504 1.26 pooka *
505 1.26 pooka * We don't want to release our reference
506 1.26 pooka * if it's on replywait queue to avoid error
507 1.26 pooka * to file server. putop() code will DTRT.
508 1.26 pooka */
509 1.26 pooka mutex_exit(&park->park_mtx);
510 1.26 pooka mutex_enter(&pmp->pmp_lock);
511 1.26 pooka mutex_enter(&park->park_mtx);
512 1.46 pooka
513 1.46 pooka /* remove from queue1 */
514 1.36 pooka if (park->park_flags & PARKFLAG_ONQUEUE1) {
515 1.46 pooka TAILQ_REMOVE(&pmp->pmp_msg_touser,
516 1.26 pooka park, park_entries);
517 1.46 pooka pmp->pmp_msg_touser_count--;
518 1.36 pooka park->park_flags &= ~PARKFLAG_ONQUEUE1;
519 1.36 pooka }
520 1.46 pooka
521 1.46 pooka /*
522 1.46 pooka * If it's waiting for a response already,
523 1.46 pooka * boost reference count. Park will get
524 1.46 pooka * nuked once the response arrives from
525 1.46 pooka * the file server.
526 1.46 pooka */
527 1.46 pooka if (park->park_flags & PARKFLAG_ONQUEUE2)
528 1.46 pooka puffs_msgpark_reference(park);
529 1.46 pooka
530 1.26 pooka mutex_exit(&pmp->pmp_lock);
531 1.26 pooka
532 1.22 pooka rv = error;
533 1.19 pooka }
534 1.22 pooka } else {
535 1.22 pooka rv = preq->preq_rv;
536 1.19 pooka }
537 1.22 pooka
538 1.16 pooka /*
539 1.16 pooka * retake the lock and release. This makes sure (haha,
540 1.16 pooka * I'm humorous) that we don't process the same vnode in
541 1.16 pooka * multiple threads due to the locks hacks we have in
542 1.16 pooka * puffs_lock(). In reality this is well protected by
543 1.16 pooka * the biglock, but once that's gone, well, hopefully
544 1.16 pooka * this will be fixed for real. (and when you read this
545 1.16 pooka * comment in 2017 and subsequently barf, my condolences ;).
546 1.16 pooka */
547 1.19 pooka if (rv == 0 && !fstrans_is_owner(mp)) {
548 1.17 hannken fstrans_start(mp, FSTRANS_NORMAL);
549 1.16 pooka fstrans_done(mp);
550 1.16 pooka }
551 1.51 pooka
552 1.22 pooka } else {
553 1.46 pooka /*
554 1.46 pooka * Take extra reference for FAF, i.e. don't free us
555 1.46 pooka * immediately upon return to the caller, but rather
556 1.46 pooka * only when the message has been transported.
557 1.46 pooka */
558 1.46 pooka puffs_msgpark_reference(park);
559 1.16 pooka }
560 1.16 pooka
561 1.46 pooka mutex_exit(&park->park_mtx);
562 1.46 pooka
563 1.22 pooka mutex_enter(&pmp->pmp_lock);
564 1.34 pooka puffs_mp_release(pmp);
565 1.22 pooka mutex_exit(&pmp->pmp_lock);
566 1.16 pooka
567 1.19 pooka return rv;
568 1.1 pooka }
569 1.1 pooka
570 1.9 pooka /*
571 1.46 pooka * Get next request in the outgoing queue. "maxsize" controls the
572 1.46 pooka * size the caller can accommodate and "nonblock" signals if this
573 1.46 pooka * should block while waiting for input. Handles all locking internally.
574 1.9 pooka */
575 1.10 pooka int
576 1.46 pooka puffs_msgif_getout(void *this, size_t maxsize, int nonblock,
577 1.46 pooka uint8_t **data, size_t *dlen, void **parkptr)
578 1.1 pooka {
579 1.46 pooka struct puffs_mount *pmp = this;
580 1.46 pooka struct puffs_msgpark *park;
581 1.9 pooka struct puffs_req *preq;
582 1.46 pooka int error;
583 1.1 pooka
584 1.46 pooka error = 0;
585 1.22 pooka mutex_enter(&pmp->pmp_lock);
586 1.50 pooka puffs_mp_reference(pmp);
587 1.46 pooka for (;;) {
588 1.46 pooka /* RIP? */
589 1.9 pooka if (pmp->pmp_status != PUFFSTAT_RUNNING) {
590 1.9 pooka error = ENXIO;
591 1.46 pooka break;
592 1.9 pooka }
593 1.12 pooka
594 1.46 pooka /* need platinum yendorian express card? */
595 1.46 pooka if (TAILQ_EMPTY(&pmp->pmp_msg_touser)) {
596 1.46 pooka DPRINTF(("puffs_getout: no outgoing op, "));
597 1.12 pooka if (nonblock) {
598 1.46 pooka DPRINTF(("returning EWOULDBLOCK\n"));
599 1.12 pooka error = EWOULDBLOCK;
600 1.46 pooka break;
601 1.9 pooka }
602 1.46 pooka DPRINTF(("waiting ...\n"));
603 1.9 pooka
604 1.46 pooka error = cv_wait_sig(&pmp->pmp_msg_waiter_cv,
605 1.22 pooka &pmp->pmp_lock);
606 1.11 pooka if (error)
607 1.46 pooka break;
608 1.11 pooka else
609 1.46 pooka continue;
610 1.9 pooka }
611 1.9 pooka
612 1.46 pooka park = TAILQ_FIRST(&pmp->pmp_msg_touser);
613 1.50 pooka if (park == NULL)
614 1.50 pooka continue;
615 1.50 pooka
616 1.46 pooka mutex_enter(&park->park_mtx);
617 1.46 pooka puffs_msgpark_reference(park);
618 1.46 pooka
619 1.46 pooka DPRINTF(("puffs_getout: found park at %p, ", park));
620 1.22 pooka
621 1.22 pooka /* If it's a goner, don't process any furher */
622 1.22 pooka if (park->park_flags & PARKFLAG_WAITERGONE) {
623 1.46 pooka DPRINTF(("waitergone!\n"));
624 1.46 pooka puffs_msgpark_release(park);
625 1.22 pooka continue;
626 1.22 pooka }
627 1.22 pooka
628 1.46 pooka /* check size */
629 1.26 pooka preq = park->park_preq;
630 1.46 pooka if (maxsize < preq->preq_frhdr.pfr_len) {
631 1.46 pooka DPRINTF(("buffer too small\n"));
632 1.46 pooka puffs_msgpark_release(park);
633 1.46 pooka error = E2BIG;
634 1.46 pooka break;
635 1.26 pooka }
636 1.28 pooka
637 1.46 pooka DPRINTF(("returning\n"));
638 1.46 pooka
639 1.46 pooka /*
640 1.46 pooka * Ok, we found what we came for. Release it from the
641 1.46 pooka * outgoing queue but do not unlock. We will unlock
642 1.46 pooka * only after we "releaseout" it to avoid complications:
643 1.46 pooka * otherwise it is (theoretically) possible for userland
644 1.46 pooka * to race us into "put" before we have a change to put
645 1.46 pooka * this baby on the receiving queue.
646 1.46 pooka */
647 1.46 pooka TAILQ_REMOVE(&pmp->pmp_msg_touser, park, park_entries);
648 1.28 pooka KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
649 1.28 pooka park->park_flags &= ~PARKFLAG_ONQUEUE1;
650 1.46 pooka mutex_exit(&park->park_mtx);
651 1.46 pooka
652 1.46 pooka pmp->pmp_msg_touser_count--;
653 1.46 pooka KASSERT(pmp->pmp_msg_touser_count >= 0);
654 1.26 pooka
655 1.46 pooka break;
656 1.46 pooka }
657 1.50 pooka puffs_mp_release(pmp);
658 1.46 pooka mutex_exit(&pmp->pmp_lock);
659 1.9 pooka
660 1.46 pooka if (error == 0) {
661 1.46 pooka *data = (uint8_t *)preq;
662 1.48 pooka preq->preq_frhdr.pfr_len = park->park_copylen;
663 1.48 pooka preq->preq_frhdr.pfr_alloclen = park->park_maxlen;
664 1.47 pooka preq->preq_frhdr.pfr_type = preq->preq_opclass; /* yay! */
665 1.46 pooka *dlen = preq->preq_frhdr.pfr_len;
666 1.46 pooka *parkptr = park;
667 1.46 pooka }
668 1.51 pooka
669 1.46 pooka return error;
670 1.46 pooka }
671 1.9 pooka
672 1.46 pooka /*
673 1.46 pooka * Release outgoing structure. Now, depending on the success of the
674 1.46 pooka * outgoing send, it is either going onto the result waiting queue
675 1.46 pooka * or the death chamber.
676 1.46 pooka */
677 1.46 pooka void
678 1.46 pooka puffs_msgif_releaseout(void *this, void *parkptr, int status)
679 1.46 pooka {
680 1.46 pooka struct puffs_mount *pmp = this;
681 1.46 pooka struct puffs_msgpark *park = parkptr;
682 1.32 pooka
683 1.46 pooka DPRINTF(("puffs_releaseout: returning park %p, errno %d: " ,
684 1.46 pooka park, status));
685 1.46 pooka mutex_enter(&pmp->pmp_lock);
686 1.46 pooka mutex_enter(&park->park_mtx);
687 1.46 pooka if (park->park_flags & PARKFLAG_WANTREPLY) {
688 1.46 pooka if (status == 0) {
689 1.46 pooka DPRINTF(("enqueue replywait\n"));
690 1.46 pooka TAILQ_INSERT_TAIL(&pmp->pmp_msg_replywait, park,
691 1.22 pooka park_entries);
692 1.26 pooka park->park_flags |= PARKFLAG_ONQUEUE2;
693 1.9 pooka } else {
694 1.46 pooka DPRINTF(("error path!\n"));
695 1.46 pooka park->park_preq->preq_rv = status;
696 1.46 pooka park->park_flags |= PARKFLAG_DONE;
697 1.46 pooka cv_signal(&park->park_cv);
698 1.1 pooka }
699 1.46 pooka puffs_msgpark_release(park);
700 1.46 pooka } else {
701 1.46 pooka DPRINTF(("release\n"));
702 1.46 pooka puffs_msgpark_release1(park, 2);
703 1.1 pooka }
704 1.22 pooka mutex_exit(&pmp->pmp_lock);
705 1.1 pooka }
706 1.1 pooka
707 1.50 pooka /*
708 1.50 pooka * XXX: locking with this one?
709 1.50 pooka */
710 1.46 pooka void
711 1.46 pooka puffs_msgif_incoming(void *this, void *buf)
712 1.1 pooka {
713 1.46 pooka struct puffs_mount *pmp = this;
714 1.46 pooka struct puffs_req *preq = buf;
715 1.46 pooka struct puffs_frame *pfr = &preq->preq_frhdr;
716 1.46 pooka struct puffs_msgpark *park;
717 1.46 pooka int release, wgone;
718 1.46 pooka
719 1.46 pooka /* XXX */
720 1.46 pooka if (PUFFSOP_OPCLASS(preq->preq_opclass) != PUFFSOP_VN
721 1.46 pooka && PUFFSOP_OPCLASS(preq->preq_opclass) != PUFFSOP_VFS)
722 1.46 pooka return;
723 1.1 pooka
724 1.22 pooka mutex_enter(&pmp->pmp_lock);
725 1.9 pooka
726 1.46 pooka /* Locate waiter */
727 1.46 pooka TAILQ_FOREACH(park, &pmp->pmp_msg_replywait, park_entries) {
728 1.46 pooka if (park->park_preq->preq_id == preq->preq_id)
729 1.1 pooka break;
730 1.46 pooka }
731 1.46 pooka if (park == NULL) {
732 1.46 pooka DPRINTF(("puffs_msgif_income: no request: %" PRIu64 "\n",
733 1.46 pooka preq->preq_id));
734 1.46 pooka mutex_exit(&pmp->pmp_lock);
735 1.46 pooka return; /* XXX send error */
736 1.46 pooka }
737 1.26 pooka
738 1.46 pooka mutex_enter(&park->park_mtx);
739 1.46 pooka puffs_msgpark_reference(park);
740 1.46 pooka if (pfr->pfr_len > park->park_maxlen) {
741 1.46 pooka DPRINTF(("puffs_msgif_income: invalid buffer length: "
742 1.46 pooka "%zu (req %" PRIu64 ", \n", pfr->pfr_len, preq->preq_id));
743 1.46 pooka park->park_preq->preq_rv = EPROTO;
744 1.46 pooka cv_signal(&park->park_cv);
745 1.46 pooka puffs_msgpark_release(park);
746 1.22 pooka mutex_exit(&pmp->pmp_lock);
747 1.46 pooka return; /* XXX: error */
748 1.46 pooka }
749 1.46 pooka wgone = park->park_flags & PARKFLAG_WAITERGONE;
750 1.9 pooka
751 1.46 pooka KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
752 1.46 pooka TAILQ_REMOVE(&pmp->pmp_msg_replywait, park, park_entries);
753 1.46 pooka park->park_flags &= ~PARKFLAG_ONQUEUE2;
754 1.46 pooka mutex_exit(&pmp->pmp_lock);
755 1.24 pooka
756 1.46 pooka if (wgone) {
757 1.46 pooka DPRINTF(("puffs_putop: bad service - waiter gone for "
758 1.46 pooka "park %p\n", park));
759 1.46 pooka release = 2;
760 1.46 pooka } else {
761 1.24 pooka if (park->park_flags & PARKFLAG_CALL) {
762 1.46 pooka DPRINTF(("puffs_msgif_income: call for %p, arg %p\n",
763 1.40 pooka park->park_preq, park->park_donearg));
764 1.46 pooka park->park_done(pmp, buf, park->park_donearg);
765 1.46 pooka release = 2;
766 1.46 pooka } else {
767 1.46 pooka /* XXX: yes, I know */
768 1.46 pooka memcpy(park->park_preq, buf, pfr->pfr_len);
769 1.26 pooka release = 1;
770 1.20 pooka }
771 1.46 pooka }
772 1.1 pooka
773 1.46 pooka if (!wgone) {
774 1.46 pooka DPRINTF(("puffs_putop: flagging done for "
775 1.46 pooka "park %p\n", park));
776 1.46 pooka cv_signal(&park->park_cv);
777 1.1 pooka }
778 1.1 pooka
779 1.46 pooka park->park_flags |= PARKFLAG_DONE;
780 1.46 pooka puffs_msgpark_release1(park, release);
781 1.1 pooka }
782 1.1 pooka
783 1.22 pooka /*
784 1.22 pooka * We're dead, kaput, RIP, slightly more than merely pining for the
785 1.22 pooka * fjords, belly-up, fallen, lifeless, finished, expired, gone to meet
786 1.22 pooka * our maker, ceased to be, etcetc. YASD. It's a dead FS!
787 1.22 pooka *
788 1.22 pooka * Caller must hold puffs mutex.
789 1.22 pooka */
790 1.22 pooka void
791 1.22 pooka puffs_userdead(struct puffs_mount *pmp)
792 1.22 pooka {
793 1.46 pooka struct puffs_msgpark *park, *park_next;
794 1.22 pooka
795 1.22 pooka /*
796 1.22 pooka * Mark filesystem status as dying so that operations don't
797 1.22 pooka * attempt to march to userspace any longer.
798 1.22 pooka */
799 1.22 pooka pmp->pmp_status = PUFFSTAT_DYING;
800 1.22 pooka
801 1.22 pooka /* signal waiters on REQUEST TO file server queue */
802 1.46 pooka for (park = TAILQ_FIRST(&pmp->pmp_msg_touser); park; park = park_next) {
803 1.24 pooka uint8_t opclass;
804 1.24 pooka
805 1.46 pooka mutex_enter(&park->park_mtx);
806 1.46 pooka puffs_msgpark_reference(park);
807 1.32 pooka park_next = TAILQ_NEXT(park, park_entries);
808 1.26 pooka
809 1.26 pooka KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
810 1.46 pooka TAILQ_REMOVE(&pmp->pmp_msg_touser, park, park_entries);
811 1.26 pooka park->park_flags &= ~PARKFLAG_ONQUEUE1;
812 1.46 pooka pmp->pmp_msg_touser_count--;
813 1.22 pooka
814 1.31 pooka /*
815 1.51 pooka * Even though waiters on QUEUE1 are removed in touser()
816 1.51 pooka * in case of WAITERGONE, it is still possible for us to
817 1.51 pooka * get raced here due to having to retake locks in said
818 1.51 pooka * touser(). In the race case simply "ignore" the item
819 1.51 pooka * on the queue and move on to the next one.
820 1.31 pooka */
821 1.31 pooka if (park->park_flags & PARKFLAG_WAITERGONE) {
822 1.31 pooka KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
823 1.31 pooka KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
824 1.46 pooka puffs_msgpark_release(park);
825 1.51 pooka
826 1.22 pooka } else {
827 1.31 pooka opclass = park->park_preq->preq_opclass;
828 1.23 pooka park->park_preq->preq_rv = ENXIO;
829 1.31 pooka
830 1.31 pooka if (park->park_flags & PARKFLAG_CALL) {
831 1.42 pooka park->park_done(pmp, park->park_preq,
832 1.31 pooka park->park_donearg);
833 1.46 pooka puffs_msgpark_release1(park, 2);
834 1.31 pooka } else if ((park->park_flags & PARKFLAG_WANTREPLY)==0) {
835 1.46 pooka puffs_msgpark_release1(park, 2);
836 1.31 pooka } else {
837 1.31 pooka park->park_preq->preq_rv = ENXIO;
838 1.31 pooka cv_signal(&park->park_cv);
839 1.46 pooka puffs_msgpark_release(park);
840 1.31 pooka }
841 1.22 pooka }
842 1.22 pooka }
843 1.22 pooka
844 1.22 pooka /* signal waiters on RESPONSE FROM file server queue */
845 1.46 pooka for (park=TAILQ_FIRST(&pmp->pmp_msg_replywait); park; park=park_next) {
846 1.46 pooka mutex_enter(&park->park_mtx);
847 1.46 pooka puffs_msgpark_reference(park);
848 1.32 pooka park_next = TAILQ_NEXT(park, park_entries);
849 1.26 pooka
850 1.26 pooka KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
851 1.31 pooka KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
852 1.26 pooka
853 1.46 pooka TAILQ_REMOVE(&pmp->pmp_msg_replywait, park, park_entries);
854 1.26 pooka park->park_flags &= ~PARKFLAG_ONQUEUE2;
855 1.26 pooka
856 1.31 pooka if (park->park_flags & PARKFLAG_WAITERGONE) {
857 1.31 pooka KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
858 1.46 pooka puffs_msgpark_release(park);
859 1.22 pooka } else {
860 1.31 pooka park->park_preq->preq_rv = ENXIO;
861 1.31 pooka if (park->park_flags & PARKFLAG_CALL) {
862 1.42 pooka park->park_done(pmp, park->park_preq,
863 1.31 pooka park->park_donearg);
864 1.46 pooka puffs_msgpark_release1(park, 2);
865 1.31 pooka } else {
866 1.31 pooka cv_signal(&park->park_cv);
867 1.46 pooka puffs_msgpark_release(park);
868 1.31 pooka }
869 1.22 pooka }
870 1.22 pooka }
871 1.50 pooka
872 1.50 pooka cv_broadcast(&pmp->pmp_msg_waiter_cv);
873 1.22 pooka }
874