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