Annotation of src/sys/fs/puffs/puffs_msgif.c, Revision 1.94.4.2
1.94.4.2! martin 1: /* $NetBSD: puffs_msgif.c,v 1.94.4.1 2014/08/29 11:55:34 martin Exp $ */
1.1 pooka 2:
3: /*
1.16 pooka 4: * Copyright (c) 2005, 2006, 2007 Antti Kantee. All Rights Reserved.
1.1 pooka 5: *
6: * Development of this software was supported by the
7: * Google Summer of Code program and the Ulla Tuominen Foundation.
8: * The Google SoC project was mentored by Bill Studenmund.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20: * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22: * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29: * SUCH DAMAGE.
30: */
31:
32: #include <sys/cdefs.h>
1.94.4.2! martin 33: __KERNEL_RCSID(0, "$NetBSD: puffs_msgif.c,v 1.94.4.1 2014/08/29 11:55:34 martin Exp $");
1.1 pooka 34:
35: #include <sys/param.h>
1.90 manu 36: #include <sys/kernel.h>
1.68 tnn 37: #include <sys/atomic.h>
1.46 pooka 38: #include <sys/kmem.h>
1.53 pooka 39: #include <sys/kthread.h>
40: #include <sys/lock.h>
1.1 pooka 41: #include <sys/mount.h>
1.53 pooka 42: #include <sys/namei.h>
43: #include <sys/proc.h>
1.1 pooka 44: #include <sys/vnode.h>
1.71 ad 45: #include <sys/atomic.h>
1.53 pooka 46:
1.88 manu 47: #include <uvm/uvm.h>
48:
1.55 pooka 49: #include <dev/putter/putter_sys.h>
1.1 pooka 50:
51: #include <fs/puffs/puffs_msgif.h>
52: #include <fs/puffs/puffs_sys.h>
53:
1.53 pooka 54: #include <miscfs/syncfs/syncfs.h> /* XXX: for syncer_mutex reference */
55:
1.22 pooka 56: /*
57: * waitq data structures
58: */
59:
60: /*
61: * While a request is going to userspace, park the caller within the
62: * kernel. This is the kernel counterpart of "struct puffs_req".
63: */
1.46 pooka 64: struct puffs_msgpark {
1.22 pooka 65: struct puffs_req *park_preq; /* req followed by buf */
66:
67: size_t park_copylen; /* userspace copylength */
68: size_t park_maxlen; /* max size in comeback */
1.24 pooka 69:
1.81 pooka 70: struct puffs_req *park_creq; /* non-compat preq */
71: size_t park_creqlen; /* non-compat preq len */
72:
1.46 pooka 73: parkdone_fn park_done; /* "biodone" a'la puffs */
1.24 pooka 74: void *park_donearg;
1.22 pooka 75:
76: int park_flags;
1.26 pooka 77: int park_refcount;
1.22 pooka 78:
79: kcondvar_t park_cv;
1.26 pooka 80: kmutex_t park_mtx;
81:
1.46 pooka 82: TAILQ_ENTRY(puffs_msgpark) park_entries;
1.22 pooka 83: };
84: #define PARKFLAG_WAITERGONE 0x01
1.26 pooka 85: #define PARKFLAG_DONE 0x02
86: #define PARKFLAG_ONQUEUE1 0x04
87: #define PARKFLAG_ONQUEUE2 0x08
88: #define PARKFLAG_CALL 0x10
1.31 pooka 89: #define PARKFLAG_WANTREPLY 0x20
1.57 pooka 90: #define PARKFLAG_HASERROR 0x40
1.22 pooka 91:
1.52 ad 92: static pool_cache_t parkpc;
1.57 pooka 93: #ifdef PUFFSDEBUG
94: static int totalpark;
95: #endif
1.22 pooka 96:
1.90 manu 97: int puffs_sopreq_expire_timeout = PUFFS_SOPREQ_EXPIRE_TIMEOUT;
98:
1.22 pooka 99: static int
100: makepark(void *arg, void *obj, int flags)
101: {
1.46 pooka 102: struct puffs_msgpark *park = obj;
1.22 pooka 103:
1.26 pooka 104: mutex_init(&park->park_mtx, MUTEX_DEFAULT, IPL_NONE);
1.22 pooka 105: cv_init(&park->park_cv, "puffsrpl");
106:
107: return 0;
108: }
109:
110: static void
111: nukepark(void *arg, void *obj)
112: {
1.46 pooka 113: struct puffs_msgpark *park = obj;
1.22 pooka 114:
115: cv_destroy(&park->park_cv);
1.26 pooka 116: mutex_destroy(&park->park_mtx);
1.22 pooka 117: }
118:
119: void
1.73 cegger 120: puffs_msgif_init(void)
1.22 pooka 121: {
122:
1.52 ad 123: parkpc = pool_cache_init(sizeof(struct puffs_msgpark), 0, 0, 0,
124: "puffprkl", NULL, IPL_NONE, makepark, nukepark, NULL);
1.22 pooka 125: }
126:
127: void
1.73 cegger 128: puffs_msgif_destroy(void)
1.22 pooka 129: {
130:
1.52 ad 131: pool_cache_destroy(parkpc);
1.22 pooka 132: }
133:
1.46 pooka 134: static struct puffs_msgpark *
135: puffs_msgpark_alloc(int waitok)
1.26 pooka 136: {
1.46 pooka 137: struct puffs_msgpark *park;
1.26 pooka 138:
1.89 manu 139: KASSERT(curlwp != uvm.pagedaemon_lwp || !waitok);
140:
1.52 ad 141: park = pool_cache_get(parkpc, waitok ? PR_WAITOK : PR_NOWAIT);
1.46 pooka 142: if (park == NULL)
143: return park;
144:
145: park->park_refcount = 1;
1.81 pooka 146: park->park_preq = park->park_creq = NULL;
1.46 pooka 147: park->park_flags = PARKFLAG_WANTREPLY;
1.26 pooka 148:
1.57 pooka 149: #ifdef PUFFSDEBUG
150: totalpark++;
151: #endif
152:
1.26 pooka 153: return park;
154: }
155:
156: static void
1.46 pooka 157: puffs_msgpark_reference(struct puffs_msgpark *park)
1.22 pooka 158: {
159:
1.46 pooka 160: KASSERT(mutex_owned(&park->park_mtx));
1.26 pooka 161: park->park_refcount++;
1.22 pooka 162: }
163:
1.46 pooka 164: /*
165: * Release reference to park structure.
166: */
167: static void
168: puffs_msgpark_release1(struct puffs_msgpark *park, int howmany)
1.22 pooka 169: {
1.46 pooka 170: struct puffs_req *preq = park->park_preq;
1.81 pooka 171: struct puffs_req *creq = park->park_creq;
1.46 pooka 172: int refcnt;
1.22 pooka 173:
1.26 pooka 174: KASSERT(mutex_owned(&park->park_mtx));
1.46 pooka 175: refcnt = park->park_refcount -= howmany;
176: mutex_exit(&park->park_mtx);
177:
178: KASSERT(refcnt >= 0);
1.26 pooka 179:
1.46 pooka 180: if (refcnt == 0) {
181: if (preq)
182: kmem_free(preq, park->park_maxlen);
1.81 pooka 183: #if 1
184: if (creq)
185: kmem_free(creq, park->park_creqlen);
186: #endif
1.52 ad 187: pool_cache_put(parkpc, park);
1.57 pooka 188:
189: #ifdef PUFFSDEBUG
190: totalpark--;
191: #endif
1.46 pooka 192: }
1.22 pooka 193: }
1.46 pooka 194: #define puffs_msgpark_release(a) puffs_msgpark_release1(a, 1)
1.22 pooka 195:
1.26 pooka 196: #ifdef PUFFSDEBUG
197: static void
1.46 pooka 198: parkdump(struct puffs_msgpark *park)
1.26 pooka 199: {
200:
201: DPRINTF(("park %p, preq %p, id %" PRIu64 "\n"
202: "\tcopy %zu, max %zu - done: %p/%p\n"
203: "\tflags 0x%08x, refcount %d, cv/mtx: %p/%p\n",
1.46 pooka 204: park, park->park_preq, park->park_preq->preq_id,
1.26 pooka 205: park->park_copylen, park->park_maxlen,
206: park->park_done, park->park_donearg,
207: park->park_flags, park->park_refcount,
208: &park->park_cv, &park->park_mtx));
209: }
210:
211: static void
212: parkqdump(struct puffs_wq *q, int dumpall)
213: {
1.46 pooka 214: struct puffs_msgpark *park;
1.26 pooka 215: int total = 0;
216:
217: TAILQ_FOREACH(park, q, park_entries) {
218: if (dumpall)
219: parkdump(park);
220: total++;
221: }
1.29 pooka 222: DPRINTF(("puffs waitqueue at %p dumped, %d total\n", q, total));
1.26 pooka 223:
224: }
225: #endif /* PUFFSDEBUG */
1.22 pooka 226:
227: /*
1.46 pooka 228: * A word about locking in the park structures: the lock protects the
229: * fields of the *park* structure (not preq) and acts as an interlock
230: * in cv operations. The lock is always internal to this module and
231: * callers do not need to worry about it.
1.22 pooka 232: */
1.46 pooka 233:
234: int
235: puffs_msgmem_alloc(size_t len, struct puffs_msgpark **ppark, void **mem,
236: int cansleep)
237: {
238: struct puffs_msgpark *park;
239: void *m;
240:
1.89 manu 241: KASSERT(curlwp != uvm.pagedaemon_lwp || !cansleep);
1.46 pooka 242: m = kmem_zalloc(len, cansleep ? KM_SLEEP : KM_NOSLEEP);
243: if (m == NULL) {
244: KASSERT(cansleep == 0);
245: return ENOMEM;
246: }
247:
248: park = puffs_msgpark_alloc(cansleep);
249: if (park == NULL) {
250: KASSERT(cansleep == 0);
251: kmem_free(m, len);
252: return ENOMEM;
253: }
254:
255: park->park_preq = m;
1.57 pooka 256: park->park_maxlen = park->park_copylen = len;
1.46 pooka 257:
258: *ppark = park;
259: *mem = m;
260:
261: return 0;
262: }
263:
264: void
265: puffs_msgmem_release(struct puffs_msgpark *park)
1.22 pooka 266: {
267:
1.46 pooka 268: if (park == NULL)
269: return;
1.22 pooka 270:
1.46 pooka 271: mutex_enter(&park->park_mtx);
272: puffs_msgpark_release(park);
273: }
1.22 pooka 274:
1.46 pooka 275: void
276: puffs_msg_setfaf(struct puffs_msgpark *park)
277: {
1.22 pooka 278:
1.57 pooka 279: KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
1.36 pooka 280: park->park_flags &= ~PARKFLAG_WANTREPLY;
1.22 pooka 281: }
282:
1.57 pooka 283: void
284: puffs_msg_setdelta(struct puffs_msgpark *park, size_t delta)
1.1 pooka 285: {
286:
1.57 pooka 287: KASSERT(delta < park->park_maxlen); /* "<=" wouldn't make sense */
288: park->park_copylen = park->park_maxlen - delta;
1.1 pooka 289: }
290:
1.57 pooka 291: void
1.64 pooka 292: puffs_msg_setinfo(struct puffs_msgpark *park, int class, int type,
293: puffs_cookie_t ck)
1.1 pooka 294: {
295:
1.57 pooka 296: park->park_preq->preq_opclass = PUFFSOP_OPCLASS(class);
297: park->park_preq->preq_optype = type;
1.64 pooka 298: park->park_preq->preq_cookie = ck;
1.1 pooka 299: }
300:
1.24 pooka 301: void
1.57 pooka 302: puffs_msg_setcall(struct puffs_msgpark *park, parkdone_fn donefn, void *donearg)
1.1 pooka 303: {
1.25 pooka 304:
1.57 pooka 305: KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
1.25 pooka 306: park->park_done = donefn;
307: park->park_donearg = donearg;
1.46 pooka 308: park->park_flags |= PARKFLAG_CALL;
1.20 pooka 309: }
310:
1.57 pooka 311: /*
312: * kernel-user-kernel waitqueues
313: */
1.4 pooka 314:
1.57 pooka 315: static uint64_t
316: puffs_getmsgid(struct puffs_mount *pmp)
1.41 pooka 317: {
1.57 pooka 318: uint64_t rv;
1.41 pooka 319:
1.57 pooka 320: mutex_enter(&pmp->pmp_lock);
321: rv = pmp->pmp_nextmsgid++;
322: mutex_exit(&pmp->pmp_lock);
1.41 pooka 323:
1.57 pooka 324: return rv;
1.41 pooka 325: }
326:
1.4 pooka 327: /*
1.57 pooka 328: * A word about reference counting of parks. A reference must be taken
329: * when accessing a park and additionally when it is on a queue. So
330: * when taking it off a queue and releasing the access reference, the
331: * reference count is generally decremented by 2.
1.1 pooka 332: */
1.57 pooka 333:
334: void
335: puffs_msg_enqueue(struct puffs_mount *pmp, struct puffs_msgpark *park)
1.1 pooka 336: {
1.26 pooka 337: struct lwp *l = curlwp;
1.81 pooka 338: struct puffs_req *preq, *creq;
339: ssize_t delta;
1.1 pooka 340:
1.83 pooka 341: /*
342: * Some clients reuse a park, so reset some flags. We might
343: * want to provide a caller-side interface for this and add
344: * a few more invariant checks here, but this will do for now.
345: */
346: park->park_flags &= ~(PARKFLAG_DONE | PARKFLAG_HASERROR);
347: KASSERT((park->park_flags & PARKFLAG_WAITERGONE) == 0);
348:
1.25 pooka 349: preq = park->park_preq;
1.81 pooka 350:
351: #if 1
352: /* check if we do compat adjustments */
353: if (pmp->pmp_docompat && puffs_compat_outgoing(preq, &creq, &delta)) {
354: park->park_creq = park->park_preq;
355: park->park_creqlen = park->park_maxlen;
356:
357: park->park_maxlen += delta;
358: park->park_copylen += delta;
359: park->park_preq = preq = creq;
360: }
361: #endif
362:
1.46 pooka 363: preq->preq_buflen = park->park_maxlen;
1.61 pooka 364: KASSERT(preq->preq_id == 0
365: || (preq->preq_opclass & PUFFSOPFLAG_ISRESPONSE));
1.46 pooka 366:
367: if ((park->park_flags & PARKFLAG_WANTREPLY) == 0)
368: preq->preq_opclass |= PUFFSOPFLAG_FAF;
369: else
370: preq->preq_id = puffs_getmsgid(pmp);
1.31 pooka 371:
1.49 pooka 372: /* fill in caller information */
373: preq->preq_pid = l->l_proc->p_pid;
374: preq->preq_lid = l->l_lid;
375:
1.19 pooka 376: /*
1.51 pooka 377: * To support cv_sig, yet another movie: check if there are signals
1.19 pooka 378: * pending and we are issueing a non-FAF. If so, return an error
1.57 pooka 379: * directly UNLESS we are issueing INACTIVE/RECLAIM. In that case,
380: * convert it to a FAF, fire off to the file server and return
381: * an error. Yes, this is bordering disgusting. Barfbags are on me.
1.19 pooka 382: */
1.57 pooka 383: if (__predict_false((park->park_flags & PARKFLAG_WANTREPLY)
1.25 pooka 384: && (park->park_flags & PARKFLAG_CALL) == 0
1.57 pooka 385: && (l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0))) {
1.84 pooka 386: sigset_t ss;
387:
388: /*
389: * see the comment about signals in puffs_msg_wait.
390: */
391: sigpending1(l, &ss);
392: if (sigismember(&ss, SIGINT) ||
393: sigismember(&ss, SIGTERM) ||
394: sigismember(&ss, SIGKILL) ||
395: sigismember(&ss, SIGHUP) ||
396: sigismember(&ss, SIGQUIT)) {
397: park->park_flags |= PARKFLAG_HASERROR;
398: preq->preq_rv = EINTR;
399: if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN
400: && (preq->preq_optype == PUFFS_VN_INACTIVE
401: || preq->preq_optype == PUFFS_VN_RECLAIM)) {
402: park->park_preq->preq_opclass |=
403: PUFFSOPFLAG_FAF;
404: park->park_flags &= ~PARKFLAG_WANTREPLY;
405: DPRINTF(("puffs_msg_enqueue: "
406: "converted to FAF %p\n", park));
407: } else {
408: return;
409: }
1.19 pooka 410: }
411: }
1.16 pooka 412:
1.22 pooka 413: mutex_enter(&pmp->pmp_lock);
1.13 pooka 414: if (pmp->pmp_status != PUFFSTAT_RUNNING) {
1.22 pooka 415: mutex_exit(&pmp->pmp_lock);
1.57 pooka 416: park->park_flags |= PARKFLAG_HASERROR;
417: preq->preq_rv = ENXIO;
418: return;
1.1 pooka 419: }
420:
1.26 pooka 421: #ifdef PUFFSDEBUG
1.46 pooka 422: parkqdump(&pmp->pmp_msg_touser, puffsdebug > 1);
423: parkqdump(&pmp->pmp_msg_replywait, puffsdebug > 1);
1.26 pooka 424: #endif
425:
1.57 pooka 426: /*
427: * Note: we don't need to lock park since we have the only
428: * reference to it at this point.
429: */
1.46 pooka 430: TAILQ_INSERT_TAIL(&pmp->pmp_msg_touser, park, park_entries);
1.26 pooka 431: park->park_flags |= PARKFLAG_ONQUEUE1;
1.46 pooka 432: pmp->pmp_msg_touser_count++;
1.57 pooka 433: park->park_refcount++;
1.26 pooka 434: mutex_exit(&pmp->pmp_lock);
1.1 pooka 435:
1.57 pooka 436: cv_broadcast(&pmp->pmp_msg_waiter_cv);
437: putter_notify(pmp->pmp_pi);
438:
1.20 pooka 439: DPRINTF(("touser: req %" PRIu64 ", preq: %p, park: %p, "
1.25 pooka 440: "c/t: 0x%x/0x%x, f: 0x%x\n", preq->preq_id, preq, park,
441: preq->preq_opclass, preq->preq_optype, park->park_flags));
1.57 pooka 442: }
1.15 pooka 443:
1.57 pooka 444: int
445: puffs_msg_wait(struct puffs_mount *pmp, struct puffs_msgpark *park)
446: {
1.84 pooka 447: lwp_t *l = curlwp;
448: proc_t *p = l->l_proc;
1.57 pooka 449: struct puffs_req *preq = park->park_preq; /* XXX: hmmm */
1.84 pooka 450: sigset_t ss;
451: sigset_t oss;
1.57 pooka 452: int error = 0;
453: int rv;
1.1 pooka 454:
1.84 pooka 455: /*
456: * block unimportant signals.
457: *
458: * The set of "important" signals here was chosen to be same as
459: * nfs interruptible mount.
460: */
461: sigfillset(&ss);
462: sigdelset(&ss, SIGINT);
463: sigdelset(&ss, SIGTERM);
464: sigdelset(&ss, SIGKILL);
465: sigdelset(&ss, SIGHUP);
466: sigdelset(&ss, SIGQUIT);
467: mutex_enter(p->p_lock);
468: sigprocmask1(l, SIG_BLOCK, &ss, &oss);
469: mutex_exit(p->p_lock);
470:
1.57 pooka 471: mutex_enter(&pmp->pmp_lock);
472: puffs_mp_reference(pmp);
473: mutex_exit(&pmp->pmp_lock);
1.46 pooka 474:
1.57 pooka 475: mutex_enter(&park->park_mtx);
1.85 yamt 476: /* did the response beat us to the wait? */
477: if (__predict_false((park->park_flags & PARKFLAG_DONE)
478: || (park->park_flags & PARKFLAG_HASERROR))) {
479: rv = park->park_preq->preq_rv;
1.57 pooka 480: mutex_exit(&park->park_mtx);
481: goto skipwait;
482: }
1.46 pooka 483:
1.85 yamt 484: if ((park->park_flags & PARKFLAG_WANTREPLY) == 0
485: || (park->park_flags & PARKFLAG_CALL)) {
1.57 pooka 486: mutex_exit(&park->park_mtx);
1.85 yamt 487: rv = 0;
1.57 pooka 488: goto skipwait;
489: }
1.26 pooka 490:
1.57 pooka 491: error = cv_wait_sig(&park->park_cv, &park->park_mtx);
492: DPRINTF(("puffs_touser: waiter for %p woke up with %d\n",
493: park, error));
494: if (error) {
495: park->park_flags |= PARKFLAG_WAITERGONE;
496: if (park->park_flags & PARKFLAG_DONE) {
497: rv = preq->preq_rv;
498: mutex_exit(&park->park_mtx);
499: } else {
500: /*
501: * ok, we marked it as going away, but
502: * still need to do queue ops. take locks
503: * in correct order.
504: *
505: * We don't want to release our reference
506: * if it's on replywait queue to avoid error
507: * to file server. putop() code will DTRT.
508: */
509: mutex_exit(&park->park_mtx);
510: mutex_enter(&pmp->pmp_lock);
511: mutex_enter(&park->park_mtx);
512:
513: /*
514: * Still on queue1? We can safely remove it
515: * without any consequences since the file
516: * server hasn't seen it. "else" we need to
517: * wait for the response and just ignore it
518: * to avoid signalling an incorrect error to
519: * the file server.
520: */
521: if (park->park_flags & PARKFLAG_ONQUEUE1) {
522: TAILQ_REMOVE(&pmp->pmp_msg_touser,
523: park, park_entries);
524: puffs_msgpark_release(park);
525: pmp->pmp_msg_touser_count--;
526: park->park_flags &= ~PARKFLAG_ONQUEUE1;
527: } else {
528: mutex_exit(&park->park_mtx);
1.19 pooka 529: }
1.57 pooka 530: mutex_exit(&pmp->pmp_lock);
1.22 pooka 531:
1.60 pooka 532: rv = EINTR;
1.16 pooka 533: }
1.22 pooka 534: } else {
1.57 pooka 535: rv = preq->preq_rv;
536: mutex_exit(&park->park_mtx);
1.16 pooka 537: }
538:
1.57 pooka 539: skipwait:
1.22 pooka 540: mutex_enter(&pmp->pmp_lock);
1.34 pooka 541: puffs_mp_release(pmp);
1.22 pooka 542: mutex_exit(&pmp->pmp_lock);
1.16 pooka 543:
1.84 pooka 544: mutex_enter(p->p_lock);
545: sigprocmask1(l, SIG_SETMASK, &oss, NULL);
546: mutex_exit(p->p_lock);
547:
1.19 pooka 548: return rv;
1.1 pooka 549: }
550:
1.9 pooka 551: /*
1.57 pooka 552: * XXX: this suuuucks. Hopefully I'll get rid of this lossage once
553: * the whole setback-nonsense gets fixed.
554: */
555: int
556: puffs_msg_wait2(struct puffs_mount *pmp, struct puffs_msgpark *park,
557: struct puffs_node *pn1, struct puffs_node *pn2)
558: {
559: struct puffs_req *preq;
560: int rv;
561:
562: rv = puffs_msg_wait(pmp, park);
563:
564: preq = park->park_preq;
565: if (pn1 && preq->preq_setbacks & PUFFS_SETBACK_INACT_N1)
566: pn1->pn_stat |= PNODE_DOINACT;
567: if (pn2 && preq->preq_setbacks & PUFFS_SETBACK_INACT_N2)
568: pn2->pn_stat |= PNODE_DOINACT;
569:
570: if (pn1 && preq->preq_setbacks & PUFFS_SETBACK_NOREF_N1)
571: pn1->pn_stat |= PNODE_NOREFS;
572: if (pn2 && preq->preq_setbacks & PUFFS_SETBACK_NOREF_N2)
573: pn2->pn_stat |= PNODE_NOREFS;
574:
575: return rv;
576:
577: }
578:
579: /*
1.61 pooka 580: * XXX: lazy bum. please, for the love of foie gras, fix me.
581: * This should *NOT* depend on setfaf. Also "memcpy" could
582: * be done more nicely.
583: */
584: void
585: puffs_msg_sendresp(struct puffs_mount *pmp, struct puffs_req *origpreq, int rv)
586: {
587: struct puffs_msgpark *park;
588: struct puffs_req *preq;
589:
1.63 pooka 590: puffs_msgmem_alloc(sizeof(struct puffs_req), &park, (void *)&preq, 1);
1.61 pooka 591: puffs_msg_setfaf(park); /* XXXXXX: avoids reqid override */
592:
593: memcpy(preq, origpreq, sizeof(struct puffs_req));
594: preq->preq_rv = rv;
595: preq->preq_opclass |= PUFFSOPFLAG_ISRESPONSE;
596:
597: puffs_msg_enqueue(pmp, park);
598: puffs_msgmem_release(park);
599: }
600:
601: /*
1.46 pooka 602: * Get next request in the outgoing queue. "maxsize" controls the
603: * size the caller can accommodate and "nonblock" signals if this
604: * should block while waiting for input. Handles all locking internally.
1.9 pooka 605: */
1.10 pooka 606: int
1.46 pooka 607: puffs_msgif_getout(void *this, size_t maxsize, int nonblock,
608: uint8_t **data, size_t *dlen, void **parkptr)
1.1 pooka 609: {
1.46 pooka 610: struct puffs_mount *pmp = this;
1.87 mrg 611: struct puffs_msgpark *park = NULL;
612: struct puffs_req *preq = NULL;
1.46 pooka 613: int error;
1.1 pooka 614:
1.46 pooka 615: error = 0;
1.22 pooka 616: mutex_enter(&pmp->pmp_lock);
1.50 pooka 617: puffs_mp_reference(pmp);
1.46 pooka 618: for (;;) {
619: /* RIP? */
1.9 pooka 620: if (pmp->pmp_status != PUFFSTAT_RUNNING) {
621: error = ENXIO;
1.46 pooka 622: break;
1.9 pooka 623: }
1.12 pooka 624:
1.46 pooka 625: /* need platinum yendorian express card? */
626: if (TAILQ_EMPTY(&pmp->pmp_msg_touser)) {
627: DPRINTF(("puffs_getout: no outgoing op, "));
1.12 pooka 628: if (nonblock) {
1.46 pooka 629: DPRINTF(("returning EWOULDBLOCK\n"));
1.12 pooka 630: error = EWOULDBLOCK;
1.46 pooka 631: break;
1.9 pooka 632: }
1.46 pooka 633: DPRINTF(("waiting ...\n"));
1.9 pooka 634:
1.46 pooka 635: error = cv_wait_sig(&pmp->pmp_msg_waiter_cv,
1.22 pooka 636: &pmp->pmp_lock);
1.11 pooka 637: if (error)
1.46 pooka 638: break;
1.11 pooka 639: else
1.46 pooka 640: continue;
1.9 pooka 641: }
642:
1.46 pooka 643: park = TAILQ_FIRST(&pmp->pmp_msg_touser);
1.50 pooka 644: if (park == NULL)
645: continue;
646:
1.46 pooka 647: mutex_enter(&park->park_mtx);
648: puffs_msgpark_reference(park);
649:
650: DPRINTF(("puffs_getout: found park at %p, ", park));
1.22 pooka 651:
652: /* If it's a goner, don't process any furher */
653: if (park->park_flags & PARKFLAG_WAITERGONE) {
1.46 pooka 654: DPRINTF(("waitergone!\n"));
655: puffs_msgpark_release(park);
1.22 pooka 656: continue;
657: }
1.55 pooka 658: preq = park->park_preq;
1.22 pooka 659:
1.55 pooka 660: #if 0
1.46 pooka 661: /* check size */
1.55 pooka 662: /*
663: * XXX: this check is not valid for now, we don't know
664: * the size of the caller's input buffer. i.e. this
665: * will most likely go away
666: */
1.46 pooka 667: if (maxsize < preq->preq_frhdr.pfr_len) {
668: DPRINTF(("buffer too small\n"));
669: puffs_msgpark_release(park);
670: error = E2BIG;
671: break;
1.26 pooka 672: }
1.55 pooka 673: #endif
1.28 pooka 674:
1.46 pooka 675: DPRINTF(("returning\n"));
676:
677: /*
678: * Ok, we found what we came for. Release it from the
679: * outgoing queue but do not unlock. We will unlock
680: * only after we "releaseout" it to avoid complications:
681: * otherwise it is (theoretically) possible for userland
682: * to race us into "put" before we have a change to put
683: * this baby on the receiving queue.
684: */
685: TAILQ_REMOVE(&pmp->pmp_msg_touser, park, park_entries);
1.28 pooka 686: KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
687: park->park_flags &= ~PARKFLAG_ONQUEUE1;
1.46 pooka 688: mutex_exit(&park->park_mtx);
689:
690: pmp->pmp_msg_touser_count--;
691: KASSERT(pmp->pmp_msg_touser_count >= 0);
1.26 pooka 692:
1.46 pooka 693: break;
694: }
1.50 pooka 695: puffs_mp_release(pmp);
1.46 pooka 696: mutex_exit(&pmp->pmp_lock);
1.9 pooka 697:
1.46 pooka 698: if (error == 0) {
699: *data = (uint8_t *)preq;
1.55 pooka 700: preq->preq_pth.pth_framelen = park->park_copylen;
701: *dlen = preq->preq_pth.pth_framelen;
1.46 pooka 702: *parkptr = park;
703: }
1.51 pooka 704:
1.46 pooka 705: return error;
706: }
1.9 pooka 707:
1.46 pooka 708: /*
709: * Release outgoing structure. Now, depending on the success of the
710: * outgoing send, it is either going onto the result waiting queue
711: * or the death chamber.
712: */
713: void
714: puffs_msgif_releaseout(void *this, void *parkptr, int status)
715: {
716: struct puffs_mount *pmp = this;
717: struct puffs_msgpark *park = parkptr;
1.32 pooka 718:
1.46 pooka 719: DPRINTF(("puffs_releaseout: returning park %p, errno %d: " ,
720: park, status));
721: mutex_enter(&pmp->pmp_lock);
722: mutex_enter(&park->park_mtx);
723: if (park->park_flags & PARKFLAG_WANTREPLY) {
724: if (status == 0) {
725: DPRINTF(("enqueue replywait\n"));
726: TAILQ_INSERT_TAIL(&pmp->pmp_msg_replywait, park,
1.22 pooka 727: park_entries);
1.26 pooka 728: park->park_flags |= PARKFLAG_ONQUEUE2;
1.9 pooka 729: } else {
1.46 pooka 730: DPRINTF(("error path!\n"));
731: park->park_preq->preq_rv = status;
732: park->park_flags |= PARKFLAG_DONE;
733: cv_signal(&park->park_cv);
1.1 pooka 734: }
1.46 pooka 735: puffs_msgpark_release(park);
736: } else {
737: DPRINTF(("release\n"));
738: puffs_msgpark_release1(park, 2);
1.1 pooka 739: }
1.22 pooka 740: mutex_exit(&pmp->pmp_lock);
1.1 pooka 741: }
742:
1.53 pooka 743: size_t
744: puffs_msgif_waitcount(void *this)
745: {
746: struct puffs_mount *pmp = this;
747: size_t rv;
748:
749: mutex_enter(&pmp->pmp_lock);
750: rv = pmp->pmp_msg_touser_count;
751: mutex_exit(&pmp->pmp_lock);
752:
753: return rv;
754: }
755:
1.50 pooka 756: /*
757: * XXX: locking with this one?
758: */
1.53 pooka 759: static void
1.56 pooka 760: puffsop_msg(void *this, struct puffs_req *preq)
1.1 pooka 761: {
1.46 pooka 762: struct puffs_mount *pmp = this;
1.55 pooka 763: struct putter_hdr *pth = &preq->preq_pth;
1.46 pooka 764: struct puffs_msgpark *park;
1.57 pooka 765: int wgone;
1.1 pooka 766:
1.22 pooka 767: mutex_enter(&pmp->pmp_lock);
1.9 pooka 768:
1.46 pooka 769: /* Locate waiter */
770: TAILQ_FOREACH(park, &pmp->pmp_msg_replywait, park_entries) {
771: if (park->park_preq->preq_id == preq->preq_id)
1.1 pooka 772: break;
1.46 pooka 773: }
774: if (park == NULL) {
1.58 pooka 775: DPRINTF(("puffsop_msg: no request: %" PRIu64 "\n",
1.46 pooka 776: preq->preq_id));
777: mutex_exit(&pmp->pmp_lock);
778: return; /* XXX send error */
779: }
1.26 pooka 780:
1.46 pooka 781: mutex_enter(&park->park_mtx);
782: puffs_msgpark_reference(park);
1.55 pooka 783: if (pth->pth_framelen > park->park_maxlen) {
1.58 pooka 784: DPRINTF(("puffsop_msg: invalid buffer length: "
1.55 pooka 785: "%" PRIu64 " (req %" PRIu64 ", \n", pth->pth_framelen,
786: preq->preq_id));
1.46 pooka 787: park->park_preq->preq_rv = EPROTO;
788: cv_signal(&park->park_cv);
1.57 pooka 789: puffs_msgpark_release1(park, 2);
1.22 pooka 790: mutex_exit(&pmp->pmp_lock);
1.46 pooka 791: return; /* XXX: error */
792: }
793: wgone = park->park_flags & PARKFLAG_WAITERGONE;
1.9 pooka 794:
1.46 pooka 795: KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
796: TAILQ_REMOVE(&pmp->pmp_msg_replywait, park, park_entries);
797: park->park_flags &= ~PARKFLAG_ONQUEUE2;
798: mutex_exit(&pmp->pmp_lock);
1.24 pooka 799:
1.46 pooka 800: if (wgone) {
1.58 pooka 801: DPRINTF(("puffsop_msg: bad service - waiter gone for "
1.46 pooka 802: "park %p\n", park));
803: } else {
1.81 pooka 804: #if 1
805: if (park->park_creq) {
806: struct puffs_req *creq;
807: size_t csize;
808:
809: KASSERT(pmp->pmp_docompat);
810: puffs_compat_incoming(preq, park->park_creq);
811: creq = park->park_creq;
812: csize = park->park_creqlen;
813: park->park_creq = park->park_preq;
814: park->park_creqlen = park->park_maxlen;
815:
816: park->park_preq = creq;
817: park->park_maxlen = csize;
818:
819: memcpy(park->park_creq, preq, pth->pth_framelen);
820: } else {
821: #endif
822: memcpy(park->park_preq, preq, pth->pth_framelen);
823: }
824:
1.24 pooka 825: if (park->park_flags & PARKFLAG_CALL) {
1.58 pooka 826: DPRINTF(("puffsop_msg: call for %p, arg %p\n",
1.40 pooka 827: park->park_preq, park->park_donearg));
1.55 pooka 828: park->park_done(pmp, preq, park->park_donearg);
1.20 pooka 829: }
1.46 pooka 830: }
1.1 pooka 831:
1.46 pooka 832: if (!wgone) {
833: DPRINTF(("puffs_putop: flagging done for "
834: "park %p\n", park));
835: cv_signal(&park->park_cv);
1.1 pooka 836: }
837:
1.46 pooka 838: park->park_flags |= PARKFLAG_DONE;
1.57 pooka 839: puffs_msgpark_release1(park, 2);
1.1 pooka 840: }
841:
1.90 manu 842: /*
843: * Node expiry. We come here after an inactive on an unexpired node.
844: * The expiry has been queued and is done in sop thread.
845: */
1.92 manu 846: static void
1.90 manu 847: puffsop_expire(struct puffs_mount *pmp, puffs_cookie_t cookie)
848: {
849: struct vnode *vp;
850:
851: KASSERT(PUFFS_USE_FS_TTL(pmp));
852:
853: /*
854: * If it still exists and has no reference,
855: * vrele should cause it to be reclaimed.
856: * Otherwise, we have nothing to do.
857: */
1.94.4.1 martin 858: if (puffs_cookie2vnode(pmp, cookie, &vp) == 0) {
1.90 manu 859: VPTOPP(vp)->pn_stat &= ~PNODE_SOPEXP;
860: vrele(vp);
861: }
862:
1.92 manu 863: return;
1.90 manu 864: }
865:
1.61 pooka 866: static void
1.53 pooka 867: puffsop_flush(struct puffs_mount *pmp, struct puffs_flush *pf)
868: {
869: struct vnode *vp;
870: voff_t offlo, offhi;
871: int rv, flags = 0;
872:
1.76 pooka 873: KASSERT(pf->pf_req.preq_pth.pth_framelen == sizeof(struct puffs_flush));
1.61 pooka 874:
1.53 pooka 875: /* XXX: slurry */
876: if (pf->pf_op == PUFFS_INVAL_NAMECACHE_ALL) {
877: cache_purgevfs(PMPTOMP(pmp));
1.61 pooka 878: rv = 0;
879: goto out;
1.53 pooka 880: }
881:
882: /*
883: * Get vnode, don't lock it. Namecache is protected by its own lock
884: * and we have a reference to protect against premature harvesting.
885: *
886: * The node we want here might be locked and the op is in
887: * userspace waiting for us to complete ==> deadlock. Another
888: * reason we need to eventually bump locking to userspace, as we
889: * will need to lock the node if we wish to do flushes.
890: */
1.94.4.1 martin 891: rv = puffs_cookie2vnode(pmp, pf->pf_cookie, &vp);
1.53 pooka 892: if (rv) {
893: if (rv == PUFFS_NOSUCHCOOKIE)
1.61 pooka 894: rv = ENOENT;
895: goto out;
1.53 pooka 896: }
897:
898: switch (pf->pf_op) {
899: #if 0
900: /* not quite ready, yet */
901: case PUFFS_INVAL_NAMECACHE_NODE:
902: struct componentname *pf_cn;
903: char *name;
904: /* get comfortab^Wcomponentname */
1.59 pooka 905: pf_cn = kmem_alloc(componentname);
1.53 pooka 906: memset(pf_cn, 0, sizeof(struct componentname));
907: break;
908:
909: #endif
910: case PUFFS_INVAL_NAMECACHE_DIR:
911: if (vp->v_type != VDIR) {
912: rv = EINVAL;
913: break;
914: }
1.93 dholland 915: cache_purge1(vp, NULL, 0, PURGE_CHILDREN);
1.53 pooka 916: break;
917:
918: case PUFFS_INVAL_PAGECACHE_NODE_RANGE:
919: flags = PGO_FREE;
920: /*FALLTHROUGH*/
921: case PUFFS_FLUSH_PAGECACHE_NODE_RANGE:
922: if (flags == 0)
923: flags = PGO_CLEANIT;
924:
925: if (pf->pf_end > vp->v_size || vp->v_type != VREG) {
926: rv = EINVAL;
927: break;
928: }
929:
930: offlo = trunc_page(pf->pf_start);
931: offhi = round_page(pf->pf_end);
932: if (offhi != 0 && offlo >= offhi) {
933: rv = EINVAL;
934: break;
935: }
936:
1.86 rmind 937: mutex_enter(vp->v_uobj.vmobjlock);
1.53 pooka 938: rv = VOP_PUTPAGES(vp, offlo, offhi, flags);
939: break;
940:
941: default:
942: rv = EINVAL;
943: }
944:
945: vrele(vp);
946:
1.61 pooka 947: out:
948: puffs_msg_sendresp(pmp, &pf->pf_req, rv);
1.53 pooka 949: }
950:
951: int
1.56 pooka 952: puffs_msgif_dispatch(void *this, struct putter_hdr *pth)
1.53 pooka 953: {
954: struct puffs_mount *pmp = this;
1.56 pooka 955: struct puffs_req *preq = (struct puffs_req *)pth;
1.76 pooka 956: struct puffs_sopreq *psopr;
1.56 pooka 957:
1.61 pooka 958: if (pth->pth_framelen < sizeof(struct puffs_req)) {
959: puffs_msg_sendresp(pmp, preq, EINVAL); /* E2SMALL */
960: return 0;
961: }
1.53 pooka 962:
1.55 pooka 963: switch (PUFFSOP_OPCLASS(preq->preq_opclass)) {
1.53 pooka 964: case PUFFSOP_VN:
965: case PUFFSOP_VFS:
1.61 pooka 966: DPRINTF(("dispatch: vn/vfs message 0x%x\n", preq->preq_optype));
1.56 pooka 967: puffsop_msg(pmp, preq);
1.53 pooka 968: break;
1.77 pooka 969:
1.76 pooka 970: case PUFFSOP_FLUSH: /* process in sop thread */
971: {
972: struct puffs_flush *pf;
973:
1.61 pooka 974: DPRINTF(("dispatch: flush 0x%x\n", preq->preq_optype));
1.76 pooka 975:
976: if (preq->preq_pth.pth_framelen != sizeof(struct puffs_flush)) {
977: puffs_msg_sendresp(pmp, preq, EINVAL); /* E2SMALL */
978: break;
979: }
980: pf = (struct puffs_flush *)preq;
981:
1.88 manu 982: KASSERT(curlwp != uvm.pagedaemon_lwp);
1.76 pooka 983: psopr = kmem_alloc(sizeof(*psopr), KM_SLEEP);
984: memcpy(&psopr->psopr_pf, pf, sizeof(*pf));
985: psopr->psopr_sopreq = PUFFS_SOPREQ_FLUSH;
986:
987: mutex_enter(&pmp->pmp_sopmtx);
1.80 pooka 988: if (pmp->pmp_sopthrcount == 0) {
989: mutex_exit(&pmp->pmp_sopmtx);
990: kmem_free(psopr, sizeof(*psopr));
991: puffs_msg_sendresp(pmp, preq, ENXIO);
992: } else {
1.90 manu 993: TAILQ_INSERT_TAIL(&pmp->pmp_sopfastreqs,
1.80 pooka 994: psopr, psopr_entries);
995: cv_signal(&pmp->pmp_sopcv);
996: mutex_exit(&pmp->pmp_sopmtx);
997: }
1.53 pooka 998: break;
1.76 pooka 999: }
1.77 pooka 1000:
1001: case PUFFSOP_UNMOUNT: /* process in sop thread */
1002: {
1003:
1004: DPRINTF(("dispatch: unmount 0x%x\n", preq->preq_optype));
1005:
1.88 manu 1006: KASSERT(curlwp != uvm.pagedaemon_lwp);
1.77 pooka 1007: psopr = kmem_alloc(sizeof(*psopr), KM_SLEEP);
1008: psopr->psopr_preq = *preq;
1009: psopr->psopr_sopreq = PUFFS_SOPREQ_UNMOUNT;
1010:
1011: mutex_enter(&pmp->pmp_sopmtx);
1.80 pooka 1012: if (pmp->pmp_sopthrcount == 0) {
1013: mutex_exit(&pmp->pmp_sopmtx);
1014: kmem_free(psopr, sizeof(*psopr));
1015: puffs_msg_sendresp(pmp, preq, ENXIO);
1016: } else {
1.90 manu 1017: TAILQ_INSERT_TAIL(&pmp->pmp_sopfastreqs,
1.80 pooka 1018: psopr, psopr_entries);
1019: cv_signal(&pmp->pmp_sopcv);
1020: mutex_exit(&pmp->pmp_sopmtx);
1021: }
1.77 pooka 1022: break;
1023: }
1024:
1.53 pooka 1025: default:
1.61 pooka 1026: DPRINTF(("dispatch: invalid class 0x%x\n", preq->preq_opclass));
1.75 pooka 1027: puffs_msg_sendresp(pmp, preq, EOPNOTSUPP);
1.53 pooka 1028: break;
1029: }
1030:
1.76 pooka 1031: return 0;
1032: }
1033:
1034: /*
1035: * Work loop for thread processing all ops from server which
1036: * cannot safely be handled in caller context. This includes
1037: * everything which might need a lock currently "held" by the file
1038: * server, i.e. a long-term kernel lock which will be released only
1039: * once the file server acknowledges a request
1040: */
1.90 manu 1041: #define TIMED_OUT(expire) \
1042: ((int)((unsigned int)hardclock_ticks - (unsigned int)expire) > 0)
1.76 pooka 1043: void
1044: puffs_sop_thread(void *arg)
1045: {
1046: struct puffs_mount *pmp = arg;
1.77 pooka 1047: struct mount *mp = PMPTOMP(pmp);
1.76 pooka 1048: struct puffs_sopreq *psopr;
1049: bool keeprunning;
1.77 pooka 1050: bool unmountme = false;
1.90 manu 1051: int timeo;
1052:
1053: timeo = PUFFS_USE_FS_TTL(pmp) ? puffs_sopreq_expire_timeout : 0;
1.76 pooka 1054:
1055: mutex_enter(&pmp->pmp_sopmtx);
1056: for (keeprunning = true; keeprunning; ) {
1.90 manu 1057: /*
1.92 manu 1058: * We have a fast queue for flush and umount, and a node
1059: * queue for delayes node reclaims. Requests on node queue * are not honoured before clock reaches psopr_at. This
1060: * code assumes that requests are ordered by psopr_at.
1.90 manu 1061: */
1062: do {
1063: psopr = TAILQ_FIRST(&pmp->pmp_sopfastreqs);
1.91 manu 1064: if (psopr != NULL) {
1.90 manu 1065: TAILQ_REMOVE(&pmp->pmp_sopfastreqs,
1066: psopr, psopr_entries);
1067: break;
1068: }
1069:
1.92 manu 1070: psopr = TAILQ_FIRST(&pmp->pmp_sopnodereqs);
1.90 manu 1071: if ((psopr != NULL) && TIMED_OUT(psopr->psopr_at)) {
1.92 manu 1072: TAILQ_REMOVE(&pmp->pmp_sopnodereqs,
1.90 manu 1073: psopr, psopr_entries);
1074: break;
1075: }
1076:
1077: cv_timedwait(&pmp->pmp_sopcv, &pmp->pmp_sopmtx, timeo);
1078: } while (1 /* CONSTCOND */);
1079:
1.76 pooka 1080: mutex_exit(&pmp->pmp_sopmtx);
1081:
1082: switch (psopr->psopr_sopreq) {
1.79 pooka 1083: case PUFFS_SOPREQSYS_EXIT:
1.76 pooka 1084: keeprunning = false;
1085: break;
1086: case PUFFS_SOPREQ_FLUSH:
1087: puffsop_flush(pmp, &psopr->psopr_pf);
1088: break;
1.90 manu 1089: case PUFFS_SOPREQ_EXPIRE:
1090: puffsop_expire(pmp, psopr->psopr_ck);
1091: break;
1.77 pooka 1092: case PUFFS_SOPREQ_UNMOUNT:
1.78 pooka 1093: puffs_msg_sendresp(pmp, &psopr->psopr_preq, 0);
1.77 pooka 1094:
1095: unmountme = true;
1096: keeprunning = false;
1097:
1098: /*
1099: * We know the mountpoint is still alive because
1100: * the thread that is us (poetic?) is still alive.
1101: */
1102: atomic_inc_uint((unsigned int*)&mp->mnt_refcnt);
1103: break;
1.76 pooka 1104: }
1105:
1106: kmem_free(psopr, sizeof(*psopr));
1107: mutex_enter(&pmp->pmp_sopmtx);
1108: }
1109:
1110: /*
1.80 pooka 1111: * Purge remaining ops.
1.76 pooka 1112: */
1.90 manu 1113: while ((psopr = TAILQ_FIRST(&pmp->pmp_sopfastreqs)) != NULL) {
1114: TAILQ_REMOVE(&pmp->pmp_sopfastreqs, psopr, psopr_entries);
1115: mutex_exit(&pmp->pmp_sopmtx);
1116: puffs_msg_sendresp(pmp, &psopr->psopr_preq, ENXIO);
1117: kmem_free(psopr, sizeof(*psopr));
1118: mutex_enter(&pmp->pmp_sopmtx);
1119: }
1120:
1.92 manu 1121: while ((psopr = TAILQ_FIRST(&pmp->pmp_sopnodereqs)) != NULL) {
1122: TAILQ_REMOVE(&pmp->pmp_sopnodereqs, psopr, psopr_entries);
1.76 pooka 1123: mutex_exit(&pmp->pmp_sopmtx);
1.92 manu 1124: KASSERT(psopr->psopr_sopreq == PUFFS_SOPREQ_EXPIRE);
1.76 pooka 1125: kmem_free(psopr, sizeof(*psopr));
1126: mutex_enter(&pmp->pmp_sopmtx);
1127: }
1128:
1129: pmp->pmp_sopthrcount--;
1.77 pooka 1130: cv_broadcast(&pmp->pmp_sopcv);
1.76 pooka 1131: mutex_exit(&pmp->pmp_sopmtx); /* not allowed to access fs after this */
1132:
1.77 pooka 1133: /*
1134: * If unmount was requested, we can now safely do it here, since
1135: * our context is dead from the point-of-view of puffs_unmount()
1136: * and we are just another thread. dounmount() makes internally
1137: * sure that VFS_UNMOUNT() isn't called reentrantly and that it
1138: * is eventually completed.
1139: */
1140: if (unmountme) {
1141: (void)dounmount(mp, MNT_FORCE, curlwp);
1142: vfs_destroy(mp);
1143: }
1144:
1.76 pooka 1145: kthread_exit(0);
1.53 pooka 1146: }
1147:
1148: int
1149: puffs_msgif_close(void *this)
1150: {
1151: struct puffs_mount *pmp = this;
1152: struct mount *mp = PMPTOMP(pmp);
1153:
1154: mutex_enter(&pmp->pmp_lock);
1155: puffs_mp_reference(pmp);
1156:
1157: /*
1158: * Free the waiting callers before proceeding any further.
1159: * The syncer might be jogging around in this file system
1160: * currently. If we allow it to go to the userspace of no
1161: * return while trying to get the syncer lock, well ...
1162: */
1163: puffs_userdead(pmp);
1164:
1165: /*
1166: * Make sure someone from puffs_unmount() isn't currently in
1167: * userspace. If we don't take this precautionary step,
1168: * they might notice that the mountpoint has disappeared
1169: * from under them once they return. Especially note that we
1170: * cannot simply test for an unmounter before calling
1171: * dounmount(), since it might be possible that that particular
1172: * invocation of unmount was called without MNT_FORCE. Here we
1173: * *must* make sure unmount succeeds. Also, restart is necessary
1174: * since pmp isn't locked. We might end up with PUTTER_DEAD after
1175: * restart and exit from there.
1176: */
1177: if (pmp->pmp_unmounting) {
1178: cv_wait(&pmp->pmp_unmounting_cv, &pmp->pmp_lock);
1179: puffs_mp_release(pmp);
1180: mutex_exit(&pmp->pmp_lock);
1181: DPRINTF(("puffs_fop_close: unmount was in progress for pmp %p, "
1182: "restart\n", pmp));
1183: return ERESTART;
1184: }
1185:
1186: /* Won't access pmp from here anymore */
1.72 ad 1187: atomic_inc_uint((unsigned int*)&mp->mnt_refcnt);
1.53 pooka 1188: puffs_mp_release(pmp);
1189: mutex_exit(&pmp->pmp_lock);
1190:
1.72 ad 1191: /* Detach from VFS. */
1.71 ad 1192: (void)dounmount(mp, MNT_FORCE, curlwp);
1.72 ad 1193: vfs_destroy(mp);
1.53 pooka 1194:
1195: return 0;
1196: }
1197:
1198: /*
1.22 pooka 1199: * We're dead, kaput, RIP, slightly more than merely pining for the
1200: * fjords, belly-up, fallen, lifeless, finished, expired, gone to meet
1201: * our maker, ceased to be, etcetc. YASD. It's a dead FS!
1202: *
1203: * Caller must hold puffs mutex.
1204: */
1205: void
1206: puffs_userdead(struct puffs_mount *pmp)
1207: {
1.46 pooka 1208: struct puffs_msgpark *park, *park_next;
1.22 pooka 1209:
1210: /*
1211: * Mark filesystem status as dying so that operations don't
1212: * attempt to march to userspace any longer.
1213: */
1214: pmp->pmp_status = PUFFSTAT_DYING;
1215:
1216: /* signal waiters on REQUEST TO file server queue */
1.46 pooka 1217: for (park = TAILQ_FIRST(&pmp->pmp_msg_touser); park; park = park_next) {
1.24 pooka 1218:
1.46 pooka 1219: mutex_enter(&park->park_mtx);
1220: puffs_msgpark_reference(park);
1.32 pooka 1221: park_next = TAILQ_NEXT(park, park_entries);
1.26 pooka 1222:
1223: KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
1.46 pooka 1224: TAILQ_REMOVE(&pmp->pmp_msg_touser, park, park_entries);
1.26 pooka 1225: park->park_flags &= ~PARKFLAG_ONQUEUE1;
1.46 pooka 1226: pmp->pmp_msg_touser_count--;
1.22 pooka 1227:
1.31 pooka 1228: /*
1.51 pooka 1229: * Even though waiters on QUEUE1 are removed in touser()
1230: * in case of WAITERGONE, it is still possible for us to
1231: * get raced here due to having to retake locks in said
1232: * touser(). In the race case simply "ignore" the item
1233: * on the queue and move on to the next one.
1.31 pooka 1234: */
1235: if (park->park_flags & PARKFLAG_WAITERGONE) {
1236: KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
1237: KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
1.46 pooka 1238: puffs_msgpark_release(park);
1.51 pooka 1239:
1.22 pooka 1240: } else {
1.23 pooka 1241: park->park_preq->preq_rv = ENXIO;
1.31 pooka 1242:
1243: if (park->park_flags & PARKFLAG_CALL) {
1.42 pooka 1244: park->park_done(pmp, park->park_preq,
1.31 pooka 1245: park->park_donearg);
1.46 pooka 1246: puffs_msgpark_release1(park, 2);
1.31 pooka 1247: } else if ((park->park_flags & PARKFLAG_WANTREPLY)==0) {
1.46 pooka 1248: puffs_msgpark_release1(park, 2);
1.31 pooka 1249: } else {
1250: park->park_preq->preq_rv = ENXIO;
1251: cv_signal(&park->park_cv);
1.46 pooka 1252: puffs_msgpark_release(park);
1.31 pooka 1253: }
1.22 pooka 1254: }
1255: }
1256:
1257: /* signal waiters on RESPONSE FROM file server queue */
1.46 pooka 1258: for (park=TAILQ_FIRST(&pmp->pmp_msg_replywait); park; park=park_next) {
1259: mutex_enter(&park->park_mtx);
1260: puffs_msgpark_reference(park);
1.32 pooka 1261: park_next = TAILQ_NEXT(park, park_entries);
1.26 pooka 1262:
1263: KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
1.31 pooka 1264: KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
1.26 pooka 1265:
1.46 pooka 1266: TAILQ_REMOVE(&pmp->pmp_msg_replywait, park, park_entries);
1.26 pooka 1267: park->park_flags &= ~PARKFLAG_ONQUEUE2;
1268:
1.31 pooka 1269: if (park->park_flags & PARKFLAG_WAITERGONE) {
1270: KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
1.46 pooka 1271: puffs_msgpark_release(park);
1.22 pooka 1272: } else {
1.31 pooka 1273: park->park_preq->preq_rv = ENXIO;
1274: if (park->park_flags & PARKFLAG_CALL) {
1.42 pooka 1275: park->park_done(pmp, park->park_preq,
1.31 pooka 1276: park->park_donearg);
1.46 pooka 1277: puffs_msgpark_release1(park, 2);
1.31 pooka 1278: } else {
1279: cv_signal(&park->park_cv);
1.46 pooka 1280: puffs_msgpark_release(park);
1.31 pooka 1281: }
1.22 pooka 1282: }
1283: }
1.50 pooka 1284:
1285: cv_broadcast(&pmp->pmp_msg_waiter_cv);
1.22 pooka 1286: }
CVSweb <webmaster@jp.NetBSD.org>