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