1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 22 /* All Rights Reserved */ 23 24 25 /* 26 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 27 * Use is subject to license terms. 28 */ 29 30 #pragma ident "%Z%%M% %I% %E% SMI" 31 32 #include <sys/types.h> 33 #include <sys/sysmacros.h> 34 #include <sys/param.h> 35 #include <sys/errno.h> 36 #include <sys/signal.h> 37 #include <sys/proc.h> 38 #include <sys/conf.h> 39 #include <sys/cred.h> 40 #include <sys/user.h> 41 #include <sys/vnode.h> 42 #include <sys/file.h> 43 #include <sys/session.h> 44 #include <sys/stream.h> 45 #include <sys/strsubr.h> 46 #include <sys/stropts.h> 47 #include <sys/poll.h> 48 #include <sys/systm.h> 49 #include <sys/cpuvar.h> 50 #include <sys/uio.h> 51 #include <sys/cmn_err.h> 52 #include <sys/priocntl.h> 53 #include <sys/procset.h> 54 #include <sys/vmem.h> 55 #include <sys/bitmap.h> 56 #include <sys/kmem.h> 57 #include <sys/siginfo.h> 58 #include <sys/vtrace.h> 59 #include <sys/callb.h> 60 #include <sys/debug.h> 61 #include <sys/modctl.h> 62 #include <sys/vmsystm.h> 63 #include <vm/page.h> 64 #include <sys/atomic.h> 65 #include <sys/suntpi.h> 66 #include <sys/strlog.h> 67 #include <sys/promif.h> 68 #include <sys/project.h> 69 #include <sys/vm.h> 70 #include <sys/taskq.h> 71 #include <sys/sunddi.h> 72 #include <sys/sunldi_impl.h> 73 #include <sys/strsun.h> 74 #include <sys/isa_defs.h> 75 #include <sys/multidata.h> 76 #include <sys/pattr.h> 77 #include <sys/strft.h> 78 #include <sys/fs/snode.h> 79 #include <sys/zone.h> 80 #include <sys/open.h> 81 #include <sys/sunldi.h> 82 #include <sys/sad.h> 83 #include <sys/netstack.h> 84 85 #define O_SAMESTR(q) (((q)->q_next) && \ 86 (((q)->q_flag & QREADR) == ((q)->q_next->q_flag & QREADR))) 87 88 /* 89 * WARNING: 90 * The variables and routines in this file are private, belonging 91 * to the STREAMS subsystem. These should not be used by modules 92 * or drivers. Compatibility will not be guaranteed. 93 */ 94 95 /* 96 * Id value used to distinguish between different multiplexor links. 97 */ 98 static int32_t lnk_id = 0; 99 100 #define STREAMS_LOPRI MINCLSYSPRI 101 static pri_t streams_lopri = STREAMS_LOPRI; 102 103 #define STRSTAT(x) (str_statistics.x.value.ui64++) 104 typedef struct str_stat { 105 kstat_named_t sqenables; 106 kstat_named_t stenables; 107 kstat_named_t syncqservice; 108 kstat_named_t freebs; 109 kstat_named_t qwr_outer; 110 kstat_named_t rservice; 111 kstat_named_t strwaits; 112 kstat_named_t taskqfails; 113 kstat_named_t bufcalls; 114 kstat_named_t qhelps; 115 kstat_named_t qremoved; 116 kstat_named_t sqremoved; 117 kstat_named_t bcwaits; 118 kstat_named_t sqtoomany; 119 } str_stat_t; 120 121 static str_stat_t str_statistics = { 122 { "sqenables", KSTAT_DATA_UINT64 }, 123 { "stenables", KSTAT_DATA_UINT64 }, 124 { "syncqservice", KSTAT_DATA_UINT64 }, 125 { "freebs", KSTAT_DATA_UINT64 }, 126 { "qwr_outer", KSTAT_DATA_UINT64 }, 127 { "rservice", KSTAT_DATA_UINT64 }, 128 { "strwaits", KSTAT_DATA_UINT64 }, 129 { "taskqfails", KSTAT_DATA_UINT64 }, 130 { "bufcalls", KSTAT_DATA_UINT64 }, 131 { "qhelps", KSTAT_DATA_UINT64 }, 132 { "qremoved", KSTAT_DATA_UINT64 }, 133 { "sqremoved", KSTAT_DATA_UINT64 }, 134 { "bcwaits", KSTAT_DATA_UINT64 }, 135 { "sqtoomany", KSTAT_DATA_UINT64 }, 136 }; 137 138 static kstat_t *str_kstat; 139 140 /* 141 * qrunflag was used previously to control background scheduling of queues. It 142 * is not used anymore, but kept here in case some module still wants to access 143 * it via qready() and setqsched macros. 144 */ 145 char qrunflag; /* Unused */ 146 147 /* 148 * Most of the streams scheduling is done via task queues. Task queues may fail 149 * for non-sleep dispatches, so there are two backup threads servicing failed 150 * requests for queues and syncqs. Both of these threads also service failed 151 * dispatches freebs requests. Queues are put in the list specified by `qhead' 152 * and `qtail' pointers, syncqs use `sqhead' and `sqtail' pointers and freebs 153 * requests are put into `freebs_list' which has no tail pointer. All three 154 * lists are protected by a single `service_queue' lock and use 155 * `services_to_run' condition variable for signaling background threads. Use of 156 * a single lock should not be a problem because it is only used under heavy 157 * loads when task queues start to fail and at that time it may be a good idea 158 * to throttle scheduling requests. 159 * 160 * NOTE: queues and syncqs should be scheduled by two separate threads because 161 * queue servicing may be blocked waiting for a syncq which may be also 162 * scheduled for background execution. This may create a deadlock when only one 163 * thread is used for both. 164 */ 165 166 static taskq_t *streams_taskq; /* Used for most STREAMS scheduling */ 167 168 static kmutex_t service_queue; /* protects all of servicing vars */ 169 static kcondvar_t services_to_run; /* wake up background service thread */ 170 static kcondvar_t syncqs_to_run; /* wake up background service thread */ 171 172 /* 173 * List of queues scheduled for background processing dueue to lack of resources 174 * in the task queues. Protected by service_queue lock; 175 */ 176 static struct queue *qhead; 177 static struct queue *qtail; 178 179 /* 180 * Same list for syncqs 181 */ 182 static syncq_t *sqhead; 183 static syncq_t *sqtail; 184 185 static mblk_t *freebs_list; /* list of buffers to free */ 186 187 /* 188 * Backup threads for servicing queues and syncqs 189 */ 190 kthread_t *streams_qbkgrnd_thread; 191 kthread_t *streams_sqbkgrnd_thread; 192 193 /* 194 * Bufcalls related variables. 195 */ 196 struct bclist strbcalls; /* list of waiting bufcalls */ 197 kmutex_t strbcall_lock; /* protects bufcall list (strbcalls) */ 198 kcondvar_t strbcall_cv; /* Signaling when a bufcall is added */ 199 kmutex_t bcall_monitor; /* sleep/wakeup style monitor */ 200 kcondvar_t bcall_cv; /* wait 'till executing bufcall completes */ 201 kthread_t *bc_bkgrnd_thread; /* Thread to service bufcall requests */ 202 203 kmutex_t strresources; /* protects global resources */ 204 kmutex_t muxifier; /* single-threads multiplexor creation */ 205 206 static void *str_stack_init(netstackid_t stackid, netstack_t *ns); 207 static void str_stack_shutdown(netstackid_t stackid, void *arg); 208 static void str_stack_fini(netstackid_t stackid, void *arg); 209 210 extern void time_to_wait(clock_t *, clock_t); 211 212 /* 213 * run_queues is no longer used, but is kept in case some 3-d party 214 * module/driver decides to use it. 215 */ 216 int run_queues = 0; 217 218 /* 219 * sq_max_size is the depth of the syncq (in number of messages) before 220 * qfill_syncq() starts QFULL'ing destination queues. As its primary 221 * consumer - IP is no longer D_MTPERMOD, but there may be other 222 * modules/drivers depend on this syncq flow control, we prefer to 223 * choose a large number as the default value. For potential 224 * performance gain, this value is tunable in /etc/system. 225 */ 226 int sq_max_size = 10000; 227 228 /* 229 * the number of ciputctrl structures per syncq and stream we create when 230 * needed. 231 */ 232 int n_ciputctrl; 233 int max_n_ciputctrl = 16; 234 /* 235 * if n_ciputctrl is < min_n_ciputctrl don't even create ciputctrl_cache. 236 */ 237 int min_n_ciputctrl = 2; 238 239 /* 240 * Per-driver/module syncqs 241 * ======================== 242 * 243 * For drivers/modules that use PERMOD or outer syncqs we keep a list of 244 * perdm structures, new entries being added (and new syncqs allocated) when 245 * setq() encounters a module/driver with a streamtab that it hasn't seen 246 * before. 247 * The reason for this mechanism is that some modules and drivers share a 248 * common streamtab and it is necessary for those modules and drivers to also 249 * share a common PERMOD syncq. 250 * 251 * perdm_list --> dm_str == streamtab_1 252 * dm_sq == syncq_1 253 * dm_ref 254 * dm_next --> dm_str == streamtab_2 255 * dm_sq == syncq_2 256 * dm_ref 257 * dm_next --> ... NULL 258 * 259 * The dm_ref field is incremented for each new driver/module that takes 260 * a reference to the perdm structure and hence shares the syncq. 261 * References are held in the fmodsw_impl_t structure for each STREAMS module 262 * or the dev_impl array (indexed by device major number) for each driver. 263 * 264 * perdm_list -> [dm_ref == 1] -> [dm_ref == 2] -> [dm_ref == 1] -> NULL 265 * ^ ^ ^ ^ 266 * | ______________/ | | 267 * | / | | 268 * dev_impl: ...|x|y|... module A module B 269 * 270 * When a module/driver is unloaded the reference count is decremented and, 271 * when it falls to zero, the perdm structure is removed from the list and 272 * the syncq is freed (see rele_dm()). 273 */ 274 perdm_t *perdm_list = NULL; 275 static krwlock_t perdm_rwlock; 276 cdevsw_impl_t *devimpl; 277 278 extern struct qinit strdata; 279 extern struct qinit stwdata; 280 281 static void runservice(queue_t *); 282 static void streams_bufcall_service(void); 283 static void streams_qbkgrnd_service(void); 284 static void streams_sqbkgrnd_service(void); 285 static syncq_t *new_syncq(void); 286 static void free_syncq(syncq_t *); 287 static void outer_insert(syncq_t *, syncq_t *); 288 static void outer_remove(syncq_t *, syncq_t *); 289 static void write_now(syncq_t *); 290 static void clr_qfull(queue_t *); 291 static void enable_svc(queue_t *); 292 static void runbufcalls(void); 293 static void sqenable(syncq_t *); 294 static void sqfill_events(syncq_t *, queue_t *, mblk_t *, void (*)()); 295 static void wait_q_syncq(queue_t *); 296 static void backenable_insertedq(queue_t *); 297 298 static void queue_service(queue_t *); 299 static void stream_service(stdata_t *); 300 static void syncq_service(syncq_t *); 301 static void qwriter_outer_service(syncq_t *); 302 static void mblk_free(mblk_t *); 303 #ifdef DEBUG 304 static int qprocsareon(queue_t *); 305 #endif 306 307 static void set_nfsrv_ptr(queue_t *, queue_t *, queue_t *, queue_t *); 308 static void reset_nfsrv_ptr(queue_t *, queue_t *); 309 void set_qfull(queue_t *); 310 311 static void sq_run_events(syncq_t *); 312 static int propagate_syncq(queue_t *); 313 314 static void blocksq(syncq_t *, ushort_t, int); 315 static void unblocksq(syncq_t *, ushort_t, int); 316 static int dropsq(syncq_t *, uint16_t); 317 static void emptysq(syncq_t *); 318 static sqlist_t *sqlist_alloc(struct stdata *, int); 319 static void sqlist_free(sqlist_t *); 320 static sqlist_t *sqlist_build(queue_t *, struct stdata *, boolean_t); 321 static void sqlist_insert(sqlist_t *, syncq_t *); 322 static void sqlist_insertall(sqlist_t *, queue_t *); 323 324 static void strsetuio(stdata_t *); 325 326 struct kmem_cache *stream_head_cache; 327 struct kmem_cache *queue_cache; 328 struct kmem_cache *syncq_cache; 329 struct kmem_cache *qband_cache; 330 struct kmem_cache *linkinfo_cache; 331 struct kmem_cache *ciputctrl_cache = NULL; 332 333 static linkinfo_t *linkinfo_list; 334 335 /* global esballoc throttling queue */ 336 static esb_queue_t system_esbq; 337 338 /* 339 * esballoc tunable parameters. 340 */ 341 int esbq_max_qlen = 0x16; /* throttled queue length */ 342 clock_t esbq_timeout = 0x8; /* timeout to process esb queue */ 343 344 /* 345 * routines to handle esballoc queuing. 346 */ 347 static void esballoc_process_queue(esb_queue_t *); 348 static void esballoc_enqueue_mblk(mblk_t *); 349 static void esballoc_timer(void *); 350 static void esballoc_set_timer(esb_queue_t *, clock_t); 351 static void esballoc_mblk_free(mblk_t *); 352 353 /* 354 * Qinit structure and Module_info structures 355 * for passthru read and write queues 356 */ 357 358 static void pass_wput(queue_t *, mblk_t *); 359 static queue_t *link_addpassthru(stdata_t *); 360 static void link_rempassthru(queue_t *); 361 362 struct module_info passthru_info = { 363 0, 364 "passthru", 365 0, 366 INFPSZ, 367 STRHIGH, 368 STRLOW 369 }; 370 371 struct qinit passthru_rinit = { 372 (int (*)())putnext, 373 NULL, 374 NULL, 375 NULL, 376 NULL, 377 &passthru_info, 378 NULL 379 }; 380 381 struct qinit passthru_winit = { 382 (int (*)()) pass_wput, 383 NULL, 384 NULL, 385 NULL, 386 NULL, 387 &passthru_info, 388 NULL 389 }; 390 391 /* 392 * Special form of assertion: verify that X implies Y i.e. when X is true Y 393 * should also be true. 394 */ 395 #define IMPLY(X, Y) ASSERT(!(X) || (Y)) 396 397 /* 398 * Logical equivalence. Verify that both X and Y are either TRUE or FALSE. 399 */ 400 #define EQUIV(X, Y) { IMPLY(X, Y); IMPLY(Y, X); } 401 402 /* 403 * Verify correctness of list head/tail pointers. 404 */ 405 #define LISTCHECK(head, tail, link) { \ 406 EQUIV(head, tail); \ 407 IMPLY(tail != NULL, tail->link == NULL); \ 408 } 409 410 /* 411 * Enqueue a list element `el' in the end of a list denoted by `head' and `tail' 412 * using a `link' field. 413 */ 414 #define ENQUEUE(el, head, tail, link) { \ 415 ASSERT(el->link == NULL); \ 416 LISTCHECK(head, tail, link); \ 417 if (head == NULL) \ 418 head = el; \ 419 else \ 420 tail->link = el; \ 421 tail = el; \ 422 } 423 424 /* 425 * Dequeue the first element of the list denoted by `head' and `tail' pointers 426 * using a `link' field and put result into `el'. 427 */ 428 #define DQ(el, head, tail, link) { \ 429 LISTCHECK(head, tail, link); \ 430 el = head; \ 431 if (head != NULL) { \ 432 head = head->link; \ 433 if (head == NULL) \ 434 tail = NULL; \ 435 el->link = NULL; \ 436 } \ 437 } 438 439 /* 440 * Remove `el' from the list using `chase' and `curr' pointers and return result 441 * in `succeed'. 442 */ 443 #define RMQ(el, head, tail, link, chase, curr, succeed) { \ 444 LISTCHECK(head, tail, link); \ 445 chase = NULL; \ 446 succeed = 0; \ 447 for (curr = head; (curr != el) && (curr != NULL); curr = curr->link) \ 448 chase = curr; \ 449 if (curr != NULL) { \ 450 succeed = 1; \ 451 ASSERT(curr == el); \ 452 if (chase != NULL) \ 453 chase->link = curr->link; \ 454 else \ 455 head = curr->link; \ 456 curr->link = NULL; \ 457 if (curr == tail) \ 458 tail = chase; \ 459 } \ 460 LISTCHECK(head, tail, link); \ 461 } 462 463 /* Handling of delayed messages on the inner syncq. */ 464 465 /* 466 * DEBUG versions should use function versions (to simplify tracing) and 467 * non-DEBUG kernels should use macro versions. 468 */ 469 470 /* 471 * Put a queue on the syncq list of queues. 472 * Assumes SQLOCK held. 473 */ 474 #define SQPUT_Q(sq, qp) \ 475 { \ 476 ASSERT(MUTEX_HELD(SQLOCK(sq))); \ 477 if (!(qp->q_sqflags & Q_SQQUEUED)) { \ 478 /* The queue should not be linked anywhere */ \ 479 ASSERT((qp->q_sqprev == NULL) && (qp->q_sqnext == NULL)); \ 480 /* Head and tail may only be NULL simultaneously */ \ 481 EQUIV(sq->sq_head, sq->sq_tail); \ 482 /* Queue may be only enqueyed on its syncq */ \ 483 ASSERT(sq == qp->q_syncq); \ 484 /* Check the correctness of SQ_MESSAGES flag */ \ 485 EQUIV(sq->sq_head, (sq->sq_flags & SQ_MESSAGES)); \ 486 /* Sanity check first/last elements of the list */ \ 487 IMPLY(sq->sq_head != NULL, sq->sq_head->q_sqprev == NULL);\ 488 IMPLY(sq->sq_tail != NULL, sq->sq_tail->q_sqnext == NULL);\ 489 /* \ 490 * Sanity check of priority field: empty queue should \ 491 * have zero priority \ 492 * and nqueues equal to zero. \ 493 */ \ 494 IMPLY(sq->sq_head == NULL, sq->sq_pri == 0); \ 495 /* Sanity check of sq_nqueues field */ \ 496 EQUIV(sq->sq_head, sq->sq_nqueues); \ 497 if (sq->sq_head == NULL) { \ 498 sq->sq_head = sq->sq_tail = qp; \ 499 sq->sq_flags |= SQ_MESSAGES; \ 500 } else if (qp->q_spri == 0) { \ 501 qp->q_sqprev = sq->sq_tail; \ 502 sq->sq_tail->q_sqnext = qp; \ 503 sq->sq_tail = qp; \ 504 } else { \ 505 /* \ 506 * Put this queue in priority order: higher \ 507 * priority gets closer to the head. \ 508 */ \ 509 queue_t **qpp = &sq->sq_tail; \ 510 queue_t *qnext = NULL; \ 511 \ 512 while (*qpp != NULL && qp->q_spri > (*qpp)->q_spri) { \ 513 qnext = *qpp; \ 514 qpp = &(*qpp)->q_sqprev; \ 515 } \ 516 qp->q_sqnext = qnext; \ 517 qp->q_sqprev = *qpp; \ 518 if (*qpp != NULL) { \ 519 (*qpp)->q_sqnext = qp; \ 520 } else { \ 521 sq->sq_head = qp; \ 522 sq->sq_pri = sq->sq_head->q_spri; \ 523 } \ 524 *qpp = qp; \ 525 } \ 526 qp->q_sqflags |= Q_SQQUEUED; \ 527 qp->q_sqtstamp = lbolt; \ 528 sq->sq_nqueues++; \ 529 } \ 530 } 531 532 /* 533 * Remove a queue from the syncq list 534 * Assumes SQLOCK held. 535 */ 536 #define SQRM_Q(sq, qp) \ 537 { \ 538 ASSERT(MUTEX_HELD(SQLOCK(sq))); \ 539 ASSERT(qp->q_sqflags & Q_SQQUEUED); \ 540 ASSERT(sq->sq_head != NULL && sq->sq_tail != NULL); \ 541 ASSERT((sq->sq_flags & SQ_MESSAGES) != 0); \ 542 /* Check that the queue is actually in the list */ \ 543 ASSERT(qp->q_sqnext != NULL || sq->sq_tail == qp); \ 544 ASSERT(qp->q_sqprev != NULL || sq->sq_head == qp); \ 545 ASSERT(sq->sq_nqueues != 0); \ 546 if (qp->q_sqprev == NULL) { \ 547 /* First queue on list, make head q_sqnext */ \ 548 sq->sq_head = qp->q_sqnext; \ 549 } else { \ 550 /* Make prev->next == next */ \ 551 qp->q_sqprev->q_sqnext = qp->q_sqnext; \ 552 } \ 553 if (qp->q_sqnext == NULL) { \ 554 /* Last queue on list, make tail sqprev */ \ 555 sq->sq_tail = qp->q_sqprev; \ 556 } else { \ 557 /* Make next->prev == prev */ \ 558 qp->q_sqnext->q_sqprev = qp->q_sqprev; \ 559 } \ 560 /* clear out references on this queue */ \ 561 qp->q_sqprev = qp->q_sqnext = NULL; \ 562 qp->q_sqflags &= ~Q_SQQUEUED; \ 563 /* If there is nothing queued, clear SQ_MESSAGES */ \ 564 if (sq->sq_head != NULL) { \ 565 sq->sq_pri = sq->sq_head->q_spri; \ 566 } else { \ 567 sq->sq_flags &= ~SQ_MESSAGES; \ 568 sq->sq_pri = 0; \ 569 } \ 570 sq->sq_nqueues--; \ 571 ASSERT(sq->sq_head != NULL || sq->sq_evhead != NULL || \ 572 (sq->sq_flags & SQ_QUEUED) == 0); \ 573 } 574 575 /* Hide the definition from the header file. */ 576 #ifdef SQPUT_MP 577 #undef SQPUT_MP 578 #endif 579 580 /* 581 * Put a message on the queue syncq. 582 * Assumes QLOCK held. 583 */ 584 #define SQPUT_MP(qp, mp) \ 585 { \ 586 ASSERT(MUTEX_HELD(QLOCK(qp))); \ 587 ASSERT(qp->q_sqhead == NULL || \ 588 (qp->q_sqtail != NULL && \ 589 qp->q_sqtail->b_next == NULL)); \ 590 qp->q_syncqmsgs++; \ 591 ASSERT(qp->q_syncqmsgs != 0); /* Wraparound */ \ 592 if (qp->q_sqhead == NULL) { \ 593 qp->q_sqhead = qp->q_sqtail = mp; \ 594 } else { \ 595 qp->q_sqtail->b_next = mp; \ 596 qp->q_sqtail = mp; \ 597 } \ 598 ASSERT(qp->q_syncqmsgs > 0); \ 599 set_qfull(qp); \ 600 } 601 602 #define SQ_PUTCOUNT_SETFAST_LOCKED(sq) { \ 603 ASSERT(MUTEX_HELD(SQLOCK(sq))); \ 604 if ((sq)->sq_ciputctrl != NULL) { \ 605 int i; \ 606 int nlocks = (sq)->sq_nciputctrl; \ 607 ciputctrl_t *cip = (sq)->sq_ciputctrl; \ 608 ASSERT((sq)->sq_type & SQ_CIPUT); \ 609 for (i = 0; i <= nlocks; i++) { \ 610 ASSERT(MUTEX_HELD(&cip[i].ciputctrl_lock)); \ 611 cip[i].ciputctrl_count |= SQ_FASTPUT; \ 612 } \ 613 } \ 614 } 615 616 617 #define SQ_PUTCOUNT_CLRFAST_LOCKED(sq) { \ 618 ASSERT(MUTEX_HELD(SQLOCK(sq))); \ 619 if ((sq)->sq_ciputctrl != NULL) { \ 620 int i; \ 621 int nlocks = (sq)->sq_nciputctrl; \ 622 ciputctrl_t *cip = (sq)->sq_ciputctrl; \ 623 ASSERT((sq)->sq_type & SQ_CIPUT); \ 624 for (i = 0; i <= nlocks; i++) { \ 625 ASSERT(MUTEX_HELD(&cip[i].ciputctrl_lock)); \ 626 cip[i].ciputctrl_count &= ~SQ_FASTPUT; \ 627 } \ 628 } \ 629 } 630 631 /* 632 * Run service procedures for all queues in the stream head. 633 */ 634 #define STR_SERVICE(stp, q) { \ 635 ASSERT(MUTEX_HELD(&stp->sd_qlock)); \ 636 while (stp->sd_qhead != NULL) { \ 637 DQ(q, stp->sd_qhead, stp->sd_qtail, q_link); \ 638 ASSERT(stp->sd_nqueues > 0); \ 639 stp->sd_nqueues--; \ 640 ASSERT(!(q->q_flag & QINSERVICE)); \ 641 mutex_exit(&stp->sd_qlock); \ 642 queue_service(q); \ 643 mutex_enter(&stp->sd_qlock); \ 644 } \ 645 ASSERT(stp->sd_nqueues == 0); \ 646 ASSERT((stp->sd_qhead == NULL) && (stp->sd_qtail == NULL)); \ 647 } 648 649 /* 650 * constructor/destructor routines for the stream head cache 651 */ 652 /* ARGSUSED */ 653 static int 654 stream_head_constructor(void *buf, void *cdrarg, int kmflags) 655 { 656 stdata_t *stp = buf; 657 658 mutex_init(&stp->sd_lock, NULL, MUTEX_DEFAULT, NULL); 659 mutex_init(&stp->sd_reflock, NULL, MUTEX_DEFAULT, NULL); 660 mutex_init(&stp->sd_qlock, NULL, MUTEX_DEFAULT, NULL); 661 cv_init(&stp->sd_monitor, NULL, CV_DEFAULT, NULL); 662 cv_init(&stp->sd_iocmonitor, NULL, CV_DEFAULT, NULL); 663 cv_init(&stp->sd_refmonitor, NULL, CV_DEFAULT, NULL); 664 cv_init(&stp->sd_qcv, NULL, CV_DEFAULT, NULL); 665 cv_init(&stp->sd_zcopy_wait, NULL, CV_DEFAULT, NULL); 666 stp->sd_wrq = NULL; 667 668 return (0); 669 } 670 671 /* ARGSUSED */ 672 static void 673 stream_head_destructor(void *buf, void *cdrarg) 674 { 675 stdata_t *stp = buf; 676 677 mutex_destroy(&stp->sd_lock); 678 mutex_destroy(&stp->sd_reflock); 679 mutex_destroy(&stp->sd_qlock); 680 cv_destroy(&stp->sd_monitor); 681 cv_destroy(&stp->sd_iocmonitor); 682 cv_destroy(&stp->sd_refmonitor); 683 cv_destroy(&stp->sd_qcv); 684 cv_destroy(&stp->sd_zcopy_wait); 685 } 686 687 /* 688 * constructor/destructor routines for the queue cache 689 */ 690 /* ARGSUSED */ 691 static int 692 queue_constructor(void *buf, void *cdrarg, int kmflags) 693 { 694 queinfo_t *qip = buf; 695 queue_t *qp = &qip->qu_rqueue; 696 queue_t *wqp = &qip->qu_wqueue; 697 syncq_t *sq = &qip->qu_syncq; 698 699 qp->q_first = NULL; 700 qp->q_link = NULL; 701 qp->q_count = 0; 702 qp->q_mblkcnt = 0; 703 qp->q_sqhead = NULL; 704 qp->q_sqtail = NULL; 705 qp->q_sqnext = NULL; 706 qp->q_sqprev = NULL; 707 qp->q_sqflags = 0; 708 qp->q_rwcnt = 0; 709 qp->q_spri = 0; 710 711 mutex_init(QLOCK(qp), NULL, MUTEX_DEFAULT, NULL); 712 cv_init(&qp->q_wait, NULL, CV_DEFAULT, NULL); 713 714 wqp->q_first = NULL; 715 wqp->q_link = NULL; 716 wqp->q_count = 0; 717 wqp->q_mblkcnt = 0; 718 wqp->q_sqhead = NULL; 719 wqp->q_sqtail = NULL; 720 wqp->q_sqnext = NULL; 721 wqp->q_sqprev = NULL; 722 wqp->q_sqflags = 0; 723 wqp->q_rwcnt = 0; 724 wqp->q_spri = 0; 725 726 mutex_init(QLOCK(wqp), NULL, MUTEX_DEFAULT, NULL); 727 cv_init(&wqp->q_wait, NULL, CV_DEFAULT, NULL); 728 729 sq->sq_head = NULL; 730 sq->sq_tail = NULL; 731 sq->sq_evhead = NULL; 732 sq->sq_evtail = NULL; 733 sq->sq_callbpend = NULL; 734 sq->sq_outer = NULL; 735 sq->sq_onext = NULL; 736 sq->sq_oprev = NULL; 737 sq->sq_next = NULL; 738 sq->sq_svcflags = 0; 739 sq->sq_servcount = 0; 740 sq->sq_needexcl = 0; 741 sq->sq_nqueues = 0; 742 sq->sq_pri = 0; 743 744 mutex_init(&sq->sq_lock, NULL, MUTEX_DEFAULT, NULL); 745 cv_init(&sq->sq_wait, NULL, CV_DEFAULT, NULL); 746 cv_init(&sq->sq_exitwait, NULL, CV_DEFAULT, NULL); 747 748 return (0); 749 } 750 751 /* ARGSUSED */ 752 static void 753 queue_destructor(void *buf, void *cdrarg) 754 { 755 queinfo_t *qip = buf; 756 queue_t *qp = &qip->qu_rqueue; 757 queue_t *wqp = &qip->qu_wqueue; 758 syncq_t *sq = &qip->qu_syncq; 759 760 ASSERT(qp->q_sqhead == NULL); 761 ASSERT(wqp->q_sqhead == NULL); 762 ASSERT(qp->q_sqnext == NULL); 763 ASSERT(wqp->q_sqnext == NULL); 764 ASSERT(qp->q_rwcnt == 0); 765 ASSERT(wqp->q_rwcnt == 0); 766 767 mutex_destroy(&qp->q_lock); 768 cv_destroy(&qp->q_wait); 769 770 mutex_destroy(&wqp->q_lock); 771 cv_destroy(&wqp->q_wait); 772 773 mutex_destroy(&sq->sq_lock); 774 cv_destroy(&sq->sq_wait); 775 cv_destroy(&sq->sq_exitwait); 776 } 777 778 /* 779 * constructor/destructor routines for the syncq cache 780 */ 781 /* ARGSUSED */ 782 static int 783 syncq_constructor(void *buf, void *cdrarg, int kmflags) 784 { 785 syncq_t *sq = buf; 786 787 bzero(buf, sizeof (syncq_t)); 788 789 mutex_init(&sq->sq_lock, NULL, MUTEX_DEFAULT, NULL); 790 cv_init(&sq->sq_wait, NULL, CV_DEFAULT, NULL); 791 cv_init(&sq->sq_exitwait, NULL, CV_DEFAULT, NULL); 792 793 return (0); 794 } 795 796 /* ARGSUSED */ 797 static void 798 syncq_destructor(void *buf, void *cdrarg) 799 { 800 syncq_t *sq = buf; 801 802 ASSERT(sq->sq_head == NULL); 803 ASSERT(sq->sq_tail == NULL); 804 ASSERT(sq->sq_evhead == NULL); 805 ASSERT(sq->sq_evtail == NULL); 806 ASSERT(sq->sq_callbpend == NULL); 807 ASSERT(sq->sq_callbflags == 0); 808 ASSERT(sq->sq_outer == NULL); 809 ASSERT(sq->sq_onext == NULL); 810 ASSERT(sq->sq_oprev == NULL); 811 ASSERT(sq->sq_next == NULL); 812 ASSERT(sq->sq_needexcl == 0); 813 ASSERT(sq->sq_svcflags == 0); 814 ASSERT(sq->sq_servcount == 0); 815 ASSERT(sq->sq_nqueues == 0); 816 ASSERT(sq->sq_pri == 0); 817 ASSERT(sq->sq_count == 0); 818 ASSERT(sq->sq_rmqcount == 0); 819 ASSERT(sq->sq_cancelid == 0); 820 ASSERT(sq->sq_ciputctrl == NULL); 821 ASSERT(sq->sq_nciputctrl == 0); 822 ASSERT(sq->sq_type == 0); 823 ASSERT(sq->sq_flags == 0); 824 825 mutex_destroy(&sq->sq_lock); 826 cv_destroy(&sq->sq_wait); 827 cv_destroy(&sq->sq_exitwait); 828 } 829 830 /* ARGSUSED */ 831 static int 832 ciputctrl_constructor(void *buf, void *cdrarg, int kmflags) 833 { 834 ciputctrl_t *cip = buf; 835 int i; 836 837 for (i = 0; i < n_ciputctrl; i++) { 838 cip[i].ciputctrl_count = SQ_FASTPUT; 839 mutex_init(&cip[i].ciputctrl_lock, NULL, MUTEX_DEFAULT, NULL); 840 } 841 842 return (0); 843 } 844 845 /* ARGSUSED */ 846 static void 847 ciputctrl_destructor(void *buf, void *cdrarg) 848 { 849 ciputctrl_t *cip = buf; 850 int i; 851 852 for (i = 0; i < n_ciputctrl; i++) { 853 ASSERT(cip[i].ciputctrl_count & SQ_FASTPUT); 854 mutex_destroy(&cip[i].ciputctrl_lock); 855 } 856 } 857 858 /* 859 * Init routine run from main at boot time. 860 */ 861 void 862 strinit(void) 863 { 864 int ncpus = ((boot_max_ncpus == -1) ? max_ncpus : boot_max_ncpus); 865 866 stream_head_cache = kmem_cache_create("stream_head_cache", 867 sizeof (stdata_t), 0, 868 stream_head_constructor, stream_head_destructor, NULL, 869 NULL, NULL, 0); 870 871 queue_cache = kmem_cache_create("queue_cache", sizeof (queinfo_t), 0, 872 queue_constructor, queue_destructor, NULL, NULL, NULL, 0); 873 874 syncq_cache = kmem_cache_create("syncq_cache", sizeof (syncq_t), 0, 875 syncq_constructor, syncq_destructor, NULL, NULL, NULL, 0); 876 877 qband_cache = kmem_cache_create("qband_cache", 878 sizeof (qband_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 879 880 linkinfo_cache = kmem_cache_create("linkinfo_cache", 881 sizeof (linkinfo_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 882 883 n_ciputctrl = ncpus; 884 n_ciputctrl = 1 << highbit(n_ciputctrl - 1); 885 ASSERT(n_ciputctrl >= 1); 886 n_ciputctrl = MIN(n_ciputctrl, max_n_ciputctrl); 887 if (n_ciputctrl >= min_n_ciputctrl) { 888 ciputctrl_cache = kmem_cache_create("ciputctrl_cache", 889 sizeof (ciputctrl_t) * n_ciputctrl, 890 sizeof (ciputctrl_t), ciputctrl_constructor, 891 ciputctrl_destructor, NULL, NULL, NULL, 0); 892 } 893 894 streams_taskq = system_taskq; 895 896 if (streams_taskq == NULL) 897 panic("strinit: no memory for streams taskq!"); 898 899 bc_bkgrnd_thread = thread_create(NULL, 0, 900 streams_bufcall_service, NULL, 0, &p0, TS_RUN, streams_lopri); 901 902 streams_qbkgrnd_thread = thread_create(NULL, 0, 903 streams_qbkgrnd_service, NULL, 0, &p0, TS_RUN, streams_lopri); 904 905 streams_sqbkgrnd_thread = thread_create(NULL, 0, 906 streams_sqbkgrnd_service, NULL, 0, &p0, TS_RUN, streams_lopri); 907 908 /* 909 * Create STREAMS kstats. 910 */ 911 str_kstat = kstat_create("streams", 0, "strstat", 912 "net", KSTAT_TYPE_NAMED, 913 sizeof (str_statistics) / sizeof (kstat_named_t), 914 KSTAT_FLAG_VIRTUAL); 915 916 if (str_kstat != NULL) { 917 str_kstat->ks_data = &str_statistics; 918 kstat_install(str_kstat); 919 } 920 921 /* 922 * TPI support routine initialisation. 923 */ 924 tpi_init(); 925 926 /* 927 * Handle to have autopush and persistent link information per 928 * zone. 929 * Note: uses shutdown hook instead of destroy hook so that the 930 * persistent links can be torn down before the destroy hooks 931 * in the TCP/IP stack are called. 932 */ 933 netstack_register(NS_STR, str_stack_init, str_stack_shutdown, 934 str_stack_fini); 935 } 936 937 void 938 str_sendsig(vnode_t *vp, int event, uchar_t band, int error) 939 { 940 struct stdata *stp; 941 942 ASSERT(vp->v_stream); 943 stp = vp->v_stream; 944 /* Have to hold sd_lock to prevent siglist from changing */ 945 mutex_enter(&stp->sd_lock); 946 if (stp->sd_sigflags & event) 947 strsendsig(stp->sd_siglist, event, band, error); 948 mutex_exit(&stp->sd_lock); 949 } 950 951 /* 952 * Send the "sevent" set of signals to a process. 953 * This might send more than one signal if the process is registered 954 * for multiple events. The caller should pass in an sevent that only 955 * includes the events for which the process has registered. 956 */ 957 static void 958 dosendsig(proc_t *proc, int events, int sevent, k_siginfo_t *info, 959 uchar_t band, int error) 960 { 961 ASSERT(MUTEX_HELD(&proc->p_lock)); 962 963 info->si_band = 0; 964 info->si_errno = 0; 965 966 if (sevent & S_ERROR) { 967 sevent &= ~S_ERROR; 968 info->si_code = POLL_ERR; 969 info->si_errno = error; 970 TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG, 971 "strsendsig:proc %p info %p", proc, info); 972 sigaddq(proc, NULL, info, KM_NOSLEEP); 973 info->si_errno = 0; 974 } 975 if (sevent & S_HANGUP) { 976 sevent &= ~S_HANGUP; 977 info->si_code = POLL_HUP; 978 TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG, 979 "strsendsig:proc %p info %p", proc, info); 980 sigaddq(proc, NULL, info, KM_NOSLEEP); 981 } 982 if (sevent & S_HIPRI) { 983 sevent &= ~S_HIPRI; 984 info->si_code = POLL_PRI; 985 TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG, 986 "strsendsig:proc %p info %p", proc, info); 987 sigaddq(proc, NULL, info, KM_NOSLEEP); 988 } 989 if (sevent & S_RDBAND) { 990 sevent &= ~S_RDBAND; 991 if (events & S_BANDURG) 992 sigtoproc(proc, NULL, SIGURG); 993 else 994 sigtoproc(proc, NULL, SIGPOLL); 995 } 996 if (sevent & S_WRBAND) { 997 sevent &= ~S_WRBAND; 998 sigtoproc(proc, NULL, SIGPOLL); 999 } 1000 if (sevent & S_INPUT) { 1001 sevent &= ~S_INPUT; 1002 info->si_code = POLL_IN; 1003 info->si_band = band; 1004 TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG, 1005 "strsendsig:proc %p info %p", proc, info); 1006 sigaddq(proc, NULL, info, KM_NOSLEEP); 1007 info->si_band = 0; 1008 } 1009 if (sevent & S_OUTPUT) { 1010 sevent &= ~S_OUTPUT; 1011 info->si_code = POLL_OUT; 1012 info->si_band = band; 1013 TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG, 1014 "strsendsig:proc %p info %p", proc, info); 1015 sigaddq(proc, NULL, info, KM_NOSLEEP); 1016 info->si_band = 0; 1017 } 1018 if (sevent & S_MSG) { 1019 sevent &= ~S_MSG; 1020 info->si_code = POLL_MSG; 1021 info->si_band = band; 1022 TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG, 1023 "strsendsig:proc %p info %p", proc, info); 1024 sigaddq(proc, NULL, info, KM_NOSLEEP); 1025 info->si_band = 0; 1026 } 1027 if (sevent & S_RDNORM) { 1028 sevent &= ~S_RDNORM; 1029 sigtoproc(proc, NULL, SIGPOLL); 1030 } 1031 if (sevent != 0) { 1032 panic("strsendsig: unknown event(s) %x", sevent); 1033 } 1034 } 1035 1036 /* 1037 * Send SIGPOLL/SIGURG signal to all processes and process groups 1038 * registered on the given signal list that want a signal for at 1039 * least one of the specified events. 1040 * 1041 * Must be called with exclusive access to siglist (caller holding sd_lock). 1042 * 1043 * strioctl(I_SETSIG/I_ESETSIG) will only change siglist when holding 1044 * sd_lock and the ioctl code maintains a PID_HOLD on the pid structure 1045 * while it is in the siglist. 1046 * 1047 * For performance reasons (MP scalability) the code drops pidlock 1048 * when sending signals to a single process. 1049 * When sending to a process group the code holds 1050 * pidlock to prevent the membership in the process group from changing 1051 * while walking the p_pglink list. 1052 */ 1053 void 1054 strsendsig(strsig_t *siglist, int event, uchar_t band, int error) 1055 { 1056 strsig_t *ssp; 1057 k_siginfo_t info; 1058 struct pid *pidp; 1059 proc_t *proc; 1060 1061 info.si_signo = SIGPOLL; 1062 info.si_errno = 0; 1063 for (ssp = siglist; ssp; ssp = ssp->ss_next) { 1064 int sevent; 1065 1066 sevent = ssp->ss_events & event; 1067 if (sevent == 0) 1068 continue; 1069 1070 if ((pidp = ssp->ss_pidp) == NULL) { 1071 /* pid was released but still on event list */ 1072 continue; 1073 } 1074 1075 1076 if (ssp->ss_pid > 0) { 1077 /* 1078 * XXX This unfortunately still generates 1079 * a signal when a fd is closed but 1080 * the proc is active. 1081 */ 1082 ASSERT(ssp->ss_pid == pidp->pid_id); 1083 1084 mutex_enter(&pidlock); 1085 proc = prfind_zone(pidp->pid_id, ALL_ZONES); 1086 if (proc == NULL) { 1087 mutex_exit(&pidlock); 1088 continue; 1089 } 1090 mutex_enter(&proc->p_lock); 1091 mutex_exit(&pidlock); 1092 dosendsig(proc, ssp->ss_events, sevent, &info, 1093 band, error); 1094 mutex_exit(&proc->p_lock); 1095 } else { 1096 /* 1097 * Send to process group. Hold pidlock across 1098 * calls to dosendsig(). 1099 */ 1100 pid_t pgrp = -ssp->ss_pid; 1101 1102 mutex_enter(&pidlock); 1103 proc = pgfind_zone(pgrp, ALL_ZONES); 1104 while (proc != NULL) { 1105 mutex_enter(&proc->p_lock); 1106 dosendsig(proc, ssp->ss_events, sevent, 1107 &info, band, error); 1108 mutex_exit(&proc->p_lock); 1109 proc = proc->p_pglink; 1110 } 1111 mutex_exit(&pidlock); 1112 } 1113 } 1114 } 1115 1116 /* 1117 * Attach a stream device or module. 1118 * qp is a read queue; the new queue goes in so its next 1119 * read ptr is the argument, and the write queue corresponding 1120 * to the argument points to this queue. Return 0 on success, 1121 * or a non-zero errno on failure. 1122 */ 1123 int 1124 qattach(queue_t *qp, dev_t *devp, int oflag, cred_t *crp, fmodsw_impl_t *fp, 1125 boolean_t is_insert) 1126 { 1127 major_t major; 1128 cdevsw_impl_t *dp; 1129 struct streamtab *str; 1130 queue_t *rq; 1131 queue_t *wrq; 1132 uint32_t qflag; 1133 uint32_t sqtype; 1134 perdm_t *dmp; 1135 int error; 1136 int sflag; 1137 1138 rq = allocq(); 1139 wrq = _WR(rq); 1140 STREAM(rq) = STREAM(wrq) = STREAM(qp); 1141 1142 if (fp != NULL) { 1143 str = fp->f_str; 1144 qflag = fp->f_qflag; 1145 sqtype = fp->f_sqtype; 1146 dmp = fp->f_dmp; 1147 IMPLY((qflag & (QPERMOD | QMTOUTPERIM)), dmp != NULL); 1148 sflag = MODOPEN; 1149 1150 /* 1151 * stash away a pointer to the module structure so we can 1152 * unref it in qdetach. 1153 */ 1154 rq->q_fp = fp; 1155 } else { 1156 ASSERT(!is_insert); 1157 1158 major = getmajor(*devp); 1159 dp = &devimpl[major]; 1160 1161 str = dp->d_str; 1162 ASSERT(str == STREAMSTAB(major)); 1163 1164 qflag = dp->d_qflag; 1165 ASSERT(qflag & QISDRV); 1166 sqtype = dp->d_sqtype; 1167 1168 /* create perdm_t if needed */ 1169 if (NEED_DM(dp->d_dmp, qflag)) 1170 dp->d_dmp = hold_dm(str, qflag, sqtype); 1171 1172 dmp = dp->d_dmp; 1173 sflag = 0; 1174 } 1175 1176 TRACE_2(TR_FAC_STREAMS_FR, TR_QATTACH_FLAGS, 1177 "qattach:qflag == %X(%X)", qflag, *devp); 1178 1179 /* setq might sleep in allocator - avoid holding locks. */ 1180 setq(rq, str->st_rdinit, str->st_wrinit, dmp, qflag, sqtype, B_FALSE); 1181 1182 /* 1183 * Before calling the module's open routine, set up the q_next 1184 * pointer for inserting a module in the middle of a stream. 1185 * 1186 * Note that we can always set _QINSERTING and set up q_next 1187 * pointer for both inserting and pushing a module. Then there 1188 * is no need for the is_insert parameter. In insertq(), called 1189 * by qprocson(), assume that q_next of the new module always points 1190 * to the correct queue and use it for insertion. Everything should 1191 * work out fine. But in the first release of _I_INSERT, we 1192 * distinguish between inserting and pushing to make sure that 1193 * pushing a module follows the same code path as before. 1194 */ 1195 if (is_insert) { 1196 rq->q_flag |= _QINSERTING; 1197 rq->q_next = qp; 1198 } 1199 1200 /* 1201 * If there is an outer perimeter get exclusive access during 1202 * the open procedure. Bump up the reference count on the queue. 1203 */ 1204 entersq(rq->q_syncq, SQ_OPENCLOSE); 1205 error = (*rq->q_qinfo->qi_qopen)(rq, devp, oflag, sflag, crp); 1206 if (error != 0) 1207 goto failed; 1208 leavesq(rq->q_syncq, SQ_OPENCLOSE); 1209 ASSERT(qprocsareon(rq)); 1210 return (0); 1211 1212 failed: 1213 rq->q_flag &= ~_QINSERTING; 1214 if (backq(wrq) != NULL && backq(wrq)->q_next == wrq) 1215 qprocsoff(rq); 1216 leavesq(rq->q_syncq, SQ_OPENCLOSE); 1217 rq->q_next = wrq->q_next = NULL; 1218 qdetach(rq, 0, 0, crp, B_FALSE); 1219 return (error); 1220 } 1221 1222 /* 1223 * Handle second open of stream. For modules, set the 1224 * last argument to MODOPEN and do not pass any open flags. 1225 * Ignore dummydev since this is not the first open. 1226 */ 1227 int 1228 qreopen(queue_t *qp, dev_t *devp, int flag, cred_t *crp) 1229 { 1230 int error; 1231 dev_t dummydev; 1232 queue_t *wqp = _WR(qp); 1233 1234 ASSERT(qp->q_flag & QREADR); 1235 entersq(qp->q_syncq, SQ_OPENCLOSE); 1236 1237 dummydev = *devp; 1238 if (error = ((*qp->q_qinfo->qi_qopen)(qp, &dummydev, 1239 (wqp->q_next ? 0 : flag), (wqp->q_next ? MODOPEN : 0), crp))) { 1240 leavesq(qp->q_syncq, SQ_OPENCLOSE); 1241 mutex_enter(&STREAM(qp)->sd_lock); 1242 qp->q_stream->sd_flag |= STREOPENFAIL; 1243 mutex_exit(&STREAM(qp)->sd_lock); 1244 return (error); 1245 } 1246 leavesq(qp->q_syncq, SQ_OPENCLOSE); 1247 1248 /* 1249 * successful open should have done qprocson() 1250 */ 1251 ASSERT(qprocsareon(_RD(qp))); 1252 return (0); 1253 } 1254 1255 /* 1256 * Detach a stream module or device. 1257 * If clmode == 1 then the module or driver was opened and its 1258 * close routine must be called. If clmode == 0, the module 1259 * or driver was never opened or the open failed, and so its close 1260 * should not be called. 1261 */ 1262 void 1263 qdetach(queue_t *qp, int clmode, int flag, cred_t *crp, boolean_t is_remove) 1264 { 1265 queue_t *wqp = _WR(qp); 1266 ASSERT(STREAM(qp)->sd_flag & (STRCLOSE|STWOPEN|STRPLUMB)); 1267 1268 if (STREAM_NEEDSERVICE(STREAM(qp))) 1269 stream_runservice(STREAM(qp)); 1270 1271 if (clmode) { 1272 /* 1273 * Make sure that all the messages on the write side syncq are 1274 * processed and nothing is left. Since we are closing, no new 1275 * messages may appear there. 1276 */ 1277 wait_q_syncq(wqp); 1278 1279 entersq(qp->q_syncq, SQ_OPENCLOSE); 1280 if (is_remove) { 1281 mutex_enter(QLOCK(qp)); 1282 qp->q_flag |= _QREMOVING; 1283 mutex_exit(QLOCK(qp)); 1284 } 1285 (*qp->q_qinfo->qi_qclose)(qp, flag, crp); 1286 /* 1287 * Check that qprocsoff() was actually called. 1288 */ 1289 ASSERT((qp->q_flag & QWCLOSE) && (wqp->q_flag & QWCLOSE)); 1290 1291 leavesq(qp->q_syncq, SQ_OPENCLOSE); 1292 } else { 1293 disable_svc(qp); 1294 } 1295 1296 /* 1297 * Allow any threads blocked in entersq to proceed and discover 1298 * the QWCLOSE is set. 1299 * Note: This assumes that all users of entersq check QWCLOSE. 1300 * Currently runservice is the only entersq that can happen 1301 * after removeq has finished. 1302 * Removeq will have discarded all messages destined to the closing 1303 * pair of queues from the syncq. 1304 * NOTE: Calling a function inside an assert is unconventional. 1305 * However, it does not cause any problem since flush_syncq() does 1306 * not change any state except when it returns non-zero i.e. 1307 * when the assert will trigger. 1308 */ 1309 ASSERT(flush_syncq(qp->q_syncq, qp) == 0); 1310 ASSERT(flush_syncq(wqp->q_syncq, wqp) == 0); 1311 ASSERT((qp->q_flag & QPERMOD) || 1312 ((qp->q_syncq->sq_head == NULL) && 1313 (wqp->q_syncq->sq_head == NULL))); 1314 1315 /* release any fmodsw_impl_t structure held on behalf of the queue */ 1316 ASSERT(qp->q_fp != NULL || qp->q_flag & QISDRV); 1317 if (qp->q_fp != NULL) 1318 fmodsw_rele(qp->q_fp); 1319 1320 /* freeq removes us from the outer perimeter if any */ 1321 freeq(qp); 1322 } 1323 1324 /* Prevent service procedures from being called */ 1325 void 1326 disable_svc(queue_t *qp) 1327 { 1328 queue_t *wqp = _WR(qp); 1329 1330 ASSERT(qp->q_flag & QREADR); 1331 mutex_enter(QLOCK(qp)); 1332 qp->q_flag |= QWCLOSE; 1333 mutex_exit(QLOCK(qp)); 1334 mutex_enter(QLOCK(wqp)); 1335 wqp->q_flag |= QWCLOSE; 1336 mutex_exit(QLOCK(wqp)); 1337 } 1338 1339 /* allow service procedures to be called again */ 1340 void 1341 enable_svc(queue_t *qp) 1342 { 1343 queue_t *wqp = _WR(qp); 1344 1345 ASSERT(qp->q_flag & QREADR); 1346 mutex_enter(QLOCK(qp)); 1347 qp->q_flag &= ~QWCLOSE; 1348 mutex_exit(QLOCK(qp)); 1349 mutex_enter(QLOCK(wqp)); 1350 wqp->q_flag &= ~QWCLOSE; 1351 mutex_exit(QLOCK(wqp)); 1352 } 1353 1354 /* 1355 * Remove queue from qhead/qtail if it is enabled. 1356 * Only reset QENAB if the queue was removed from the runlist. 1357 * A queue goes through 3 stages: 1358 * It is on the service list and QENAB is set. 1359 * It is removed from the service list but QENAB is still set. 1360 * QENAB gets changed to QINSERVICE. 1361 * QINSERVICE is reset (when the service procedure is done) 1362 * Thus we can not reset QENAB unless we actually removed it from the service 1363 * queue. 1364 */ 1365 void 1366 remove_runlist(queue_t *qp) 1367 { 1368 if (qp->q_flag & QENAB && qhead != NULL) { 1369 queue_t *q_chase; 1370 queue_t *q_curr; 1371 int removed; 1372 1373 mutex_enter(&service_queue); 1374 RMQ(qp, qhead, qtail, q_link, q_chase, q_curr, removed); 1375 mutex_exit(&service_queue); 1376 if (removed) { 1377 STRSTAT(qremoved); 1378 qp->q_flag &= ~QENAB; 1379 } 1380 } 1381 } 1382 1383 1384 /* 1385 * wait for any pending service processing to complete. 1386 * The removal of queues from the runlist is not atomic with the 1387 * clearing of the QENABLED flag and setting the INSERVICE flag. 1388 * consequently it is possible for remove_runlist in strclose 1389 * to not find the queue on the runlist but for it to be QENABLED 1390 * and not yet INSERVICE -> hence wait_svc needs to check QENABLED 1391 * as well as INSERVICE. 1392 */ 1393 void 1394 wait_svc(queue_t *qp) 1395 { 1396 queue_t *wqp = _WR(qp); 1397 1398 ASSERT(qp->q_flag & QREADR); 1399 1400 /* 1401 * Try to remove queues from qhead/qtail list. 1402 */ 1403 if (qhead != NULL) { 1404 remove_runlist(qp); 1405 remove_runlist(wqp); 1406 } 1407 /* 1408 * Wait till the syncqs associated with the queue 1409 * will dissapear from background processing list. 1410 * This only needs to be done for non-PERMOD perimeters since 1411 * for PERMOD perimeters the syncq may be shared and will only be freed 1412 * when the last module/driver is unloaded. 1413 * If for PERMOD perimeters queue was on the syncq list, removeq() 1414 * should call propagate_syncq() or drain_syncq() for it. Both of these 1415 * function remove the queue from its syncq list, so sqthread will not 1416 * try to access the queue. 1417 */ 1418 if (!(qp->q_flag & QPERMOD)) { 1419 syncq_t *rsq = qp->q_syncq; 1420 syncq_t *wsq = wqp->q_syncq; 1421 1422 /* 1423 * Disable rsq and wsq and wait for any background processing of 1424 * syncq to complete. 1425 */ 1426 wait_sq_svc(rsq); 1427 if (wsq != rsq) 1428 wait_sq_svc(wsq); 1429 } 1430 1431 mutex_enter(QLOCK(qp)); 1432 while (qp->q_flag & (QINSERVICE|QENAB)) 1433 cv_wait(&qp->q_wait, QLOCK(qp)); 1434 mutex_exit(QLOCK(qp)); 1435 mutex_enter(QLOCK(wqp)); 1436 while (wqp->q_flag & (QINSERVICE|QENAB)) 1437 cv_wait(&wqp->q_wait, QLOCK(wqp)); 1438 mutex_exit(QLOCK(wqp)); 1439 } 1440 1441 /* 1442 * Put ioctl data from userland buffer `arg' into the mblk chain `bp'. 1443 * `flag' must always contain either K_TO_K or U_TO_K; STR_NOSIG may 1444 * also be set, and is passed through to allocb_cred_wait(). 1445 * 1446 * Returns errno on failure, zero on success. 1447 */ 1448 int 1449 putiocd(mblk_t *bp, char *arg, int flag, cred_t *cr) 1450 { 1451 mblk_t *tmp; 1452 ssize_t count; 1453 size_t n; 1454 int error = 0; 1455 1456 ASSERT((flag & (U_TO_K | K_TO_K)) == U_TO_K || 1457 (flag & (U_TO_K | K_TO_K)) == K_TO_K); 1458 1459 if (bp->b_datap->db_type == M_IOCTL) { 1460 count = ((struct iocblk *)bp->b_rptr)->ioc_count; 1461 } else { 1462 ASSERT(bp->b_datap->db_type == M_COPYIN); 1463 count = ((struct copyreq *)bp->b_rptr)->cq_size; 1464 } 1465 /* 1466 * strdoioctl validates ioc_count, so if this assert fails it 1467 * cannot be due to user error. 1468 */ 1469 ASSERT(count >= 0); 1470 1471 while (count > 0) { 1472 n = MIN(MAXIOCBSZ, count); 1473 if ((tmp = allocb_cred_wait(n, (flag & STR_NOSIG), &error, 1474 cr)) == NULL) { 1475 return (error); 1476 } 1477 error = strcopyin(arg, tmp->b_wptr, n, flag & (U_TO_K|K_TO_K)); 1478 if (error != 0) { 1479 freeb(tmp); 1480 return (error); 1481 } 1482 arg += n; 1483 DB_CPID(tmp) = curproc->p_pid; 1484 tmp->b_wptr += n; 1485 count -= n; 1486 bp = (bp->b_cont = tmp); 1487 } 1488 1489 return (0); 1490 } 1491 1492 /* 1493 * Copy ioctl data to user-land. Return non-zero errno on failure, 1494 * 0 for success. 1495 */ 1496 int 1497 getiocd(mblk_t *bp, char *arg, int copymode) 1498 { 1499 ssize_t count; 1500 size_t n; 1501 int error; 1502 1503 if (bp->b_datap->db_type == M_IOCACK) 1504 count = ((struct iocblk *)bp->b_rptr)->ioc_count; 1505 else { 1506 ASSERT(bp->b_datap->db_type == M_COPYOUT); 1507 count = ((struct copyreq *)bp->b_rptr)->cq_size; 1508 } 1509 ASSERT(count >= 0); 1510 1511 for (bp = bp->b_cont; bp && count; 1512 count -= n, bp = bp->b_cont, arg += n) { 1513 n = MIN(count, bp->b_wptr - bp->b_rptr); 1514 error = strcopyout(bp->b_rptr, arg, n, copymode); 1515 if (error) 1516 return (error); 1517 } 1518 ASSERT(count == 0); 1519 return (0); 1520 } 1521 1522 /* 1523 * Allocate a linkinfo entry given the write queue of the 1524 * bottom module of the top stream and the write queue of the 1525 * stream head of the bottom stream. 1526 */ 1527 linkinfo_t * 1528 alloclink(queue_t *qup, queue_t *qdown, file_t *fpdown) 1529 { 1530 linkinfo_t *linkp; 1531 1532 linkp = kmem_cache_alloc(linkinfo_cache, KM_SLEEP); 1533 1534 linkp->li_lblk.l_qtop = qup; 1535 linkp->li_lblk.l_qbot = qdown; 1536 linkp->li_fpdown = fpdown; 1537 1538 mutex_enter(&strresources); 1539 linkp->li_next = linkinfo_list; 1540 linkp->li_prev = NULL; 1541 if (linkp->li_next) 1542 linkp->li_next->li_prev = linkp; 1543 linkinfo_list = linkp; 1544 linkp->li_lblk.l_index = ++lnk_id; 1545 ASSERT(lnk_id != 0); /* this should never wrap in practice */ 1546 mutex_exit(&strresources); 1547 1548 return (linkp); 1549 } 1550 1551 /* 1552 * Free a linkinfo entry. 1553 */ 1554 void 1555 lbfree(linkinfo_t *linkp) 1556 { 1557 mutex_enter(&strresources); 1558 if (linkp->li_next) 1559 linkp->li_next->li_prev = linkp->li_prev; 1560 if (linkp->li_prev) 1561 linkp->li_prev->li_next = linkp->li_next; 1562 else 1563 linkinfo_list = linkp->li_next; 1564 mutex_exit(&strresources); 1565 1566 kmem_cache_free(linkinfo_cache, linkp); 1567 } 1568 1569 /* 1570 * Check for a potential linking cycle. 1571 * Return 1 if a link will result in a cycle, 1572 * and 0 otherwise. 1573 */ 1574 int 1575 linkcycle(stdata_t *upstp, stdata_t *lostp, str_stack_t *ss) 1576 { 1577 struct mux_node *np; 1578 struct mux_edge *ep; 1579 int i; 1580 major_t lomaj; 1581 major_t upmaj; 1582 /* 1583 * if the lower stream is a pipe/FIFO, return, since link 1584 * cycles can not happen on pipes/FIFOs 1585 */ 1586 if (lostp->sd_vnode->v_type == VFIFO) 1587 return (0); 1588 1589 for (i = 0; i < ss->ss_devcnt; i++) { 1590 np = &ss->ss_mux_nodes[i]; 1591 MUX_CLEAR(np); 1592 } 1593 lomaj = getmajor(lostp->sd_vnode->v_rdev); 1594 upmaj = getmajor(upstp->sd_vnode->v_rdev); 1595 np = &ss->ss_mux_nodes[lomaj]; 1596 for (;;) { 1597 if (!MUX_DIDVISIT(np)) { 1598 if (np->mn_imaj == upmaj) 1599 return (1); 1600 if (np->mn_outp == NULL) { 1601 MUX_VISIT(np); 1602 if (np->mn_originp == NULL) 1603 return (0); 1604 np = np->mn_originp; 1605 continue; 1606 } 1607 MUX_VISIT(np); 1608 np->mn_startp = np->mn_outp; 1609 } else { 1610 if (np->mn_startp == NULL) { 1611 if (np->mn_originp == NULL) 1612 return (0); 1613 else { 1614 np = np->mn_originp; 1615 continue; 1616 } 1617 } 1618 /* 1619 * If ep->me_nodep is a FIFO (me_nodep == NULL), 1620 * ignore the edge and move on. ep->me_nodep gets 1621 * set to NULL in mux_addedge() if it is a FIFO. 1622 * 1623 */ 1624 ep = np->mn_startp; 1625 np->mn_startp = ep->me_nextp; 1626 if (ep->me_nodep == NULL) 1627 continue; 1628 ep->me_nodep->mn_originp = np; 1629 np = ep->me_nodep; 1630 } 1631 } 1632 } 1633 1634 /* 1635 * Find linkinfo entry corresponding to the parameters. 1636 */ 1637 linkinfo_t * 1638 findlinks(stdata_t *stp, int index, int type, str_stack_t *ss) 1639 { 1640 linkinfo_t *linkp; 1641 struct mux_edge *mep; 1642 struct mux_node *mnp; 1643 queue_t *qup; 1644 1645 mutex_enter(&strresources); 1646 if ((type & LINKTYPEMASK) == LINKNORMAL) { 1647 qup = getendq(stp->sd_wrq); 1648 for (linkp = linkinfo_list; linkp; linkp = linkp->li_next) { 1649 if ((qup == linkp->li_lblk.l_qtop) && 1650 (!index || (index == linkp->li_lblk.l_index))) { 1651 mutex_exit(&strresources); 1652 return (linkp); 1653 } 1654 } 1655 } else { 1656 ASSERT((type & LINKTYPEMASK) == LINKPERSIST); 1657 mnp = &ss->ss_mux_nodes[getmajor(stp->sd_vnode->v_rdev)]; 1658 mep = mnp->mn_outp; 1659 while (mep) { 1660 if ((index == 0) || (index == mep->me_muxid)) 1661 break; 1662 mep = mep->me_nextp; 1663 } 1664 if (!mep) { 1665 mutex_exit(&strresources); 1666 return (NULL); 1667 } 1668 for (linkp = linkinfo_list; linkp; linkp = linkp->li_next) { 1669 if ((!linkp->li_lblk.l_qtop) && 1670 (mep->me_muxid == linkp->li_lblk.l_index)) { 1671 mutex_exit(&strresources); 1672 return (linkp); 1673 } 1674 } 1675 } 1676 mutex_exit(&strresources); 1677 return (NULL); 1678 } 1679 1680 /* 1681 * Given a queue ptr, follow the chain of q_next pointers until you reach the 1682 * last queue on the chain and return it. 1683 */ 1684 queue_t * 1685 getendq(queue_t *q) 1686 { 1687 ASSERT(q != NULL); 1688 while (_SAMESTR(q)) 1689 q = q->q_next; 1690 return (q); 1691 } 1692 1693 /* 1694 * wait for the syncq count to drop to zero. 1695 * sq could be either outer or inner. 1696 */ 1697 1698 static void 1699 wait_syncq(syncq_t *sq) 1700 { 1701 uint16_t count; 1702 1703 mutex_enter(SQLOCK(sq)); 1704 count = sq->sq_count; 1705 SQ_PUTLOCKS_ENTER(sq); 1706 SUM_SQ_PUTCOUNTS(sq, count); 1707 while (count != 0) { 1708 sq->sq_flags |= SQ_WANTWAKEUP; 1709 SQ_PUTLOCKS_EXIT(sq); 1710 cv_wait(&sq->sq_wait, SQLOCK(sq)); 1711 count = sq->sq_count; 1712 SQ_PUTLOCKS_ENTER(sq); 1713 SUM_SQ_PUTCOUNTS(sq, count); 1714 } 1715 SQ_PUTLOCKS_EXIT(sq); 1716 mutex_exit(SQLOCK(sq)); 1717 } 1718 1719 /* 1720 * Wait while there are any messages for the queue in its syncq. 1721 */ 1722 static void 1723 wait_q_syncq(queue_t *q) 1724 { 1725 if ((q->q_sqflags & Q_SQQUEUED) || (q->q_syncqmsgs > 0)) { 1726 syncq_t *sq = q->q_syncq; 1727 1728 mutex_enter(SQLOCK(sq)); 1729 while ((q->q_sqflags & Q_SQQUEUED) || (q->q_syncqmsgs > 0)) { 1730 sq->sq_flags |= SQ_WANTWAKEUP; 1731 cv_wait(&sq->sq_wait, SQLOCK(sq)); 1732 } 1733 mutex_exit(SQLOCK(sq)); 1734 } 1735 } 1736 1737 1738 int 1739 mlink_file(vnode_t *vp, int cmd, struct file *fpdown, cred_t *crp, int *rvalp, 1740 int lhlink) 1741 { 1742 struct stdata *stp; 1743 struct strioctl strioc; 1744 struct linkinfo *linkp; 1745 struct stdata *stpdown; 1746 struct streamtab *str; 1747 queue_t *passq; 1748 syncq_t *passyncq; 1749 queue_t *rq; 1750 cdevsw_impl_t *dp; 1751 uint32_t qflag; 1752 uint32_t sqtype; 1753 perdm_t *dmp; 1754 int error = 0; 1755 netstack_t *ns; 1756 str_stack_t *ss; 1757 1758 stp = vp->v_stream; 1759 TRACE_1(TR_FAC_STREAMS_FR, 1760 TR_I_LINK, "I_LINK/I_PLINK:stp %p", stp); 1761 /* 1762 * Test for invalid upper stream 1763 */ 1764 if (stp->sd_flag & STRHUP) { 1765 return (ENXIO); 1766 } 1767 if (vp->v_type == VFIFO) { 1768 return (EINVAL); 1769 } 1770 if (stp->sd_strtab == NULL) { 1771 return (EINVAL); 1772 } 1773 if (!stp->sd_strtab->st_muxwinit) { 1774 return (EINVAL); 1775 } 1776 if (fpdown == NULL) { 1777 return (EBADF); 1778 } 1779 ns = netstack_find_by_cred(crp); 1780 ASSERT(ns != NULL); 1781 ss = ns->netstack_str; 1782 ASSERT(ss != NULL); 1783 1784 if (getmajor(stp->sd_vnode->v_rdev) >= ss->ss_devcnt) { 1785 netstack_rele(ss->ss_netstack); 1786 return (EINVAL); 1787 } 1788 mutex_enter(&muxifier); 1789 if (stp->sd_flag & STPLEX) { 1790 mutex_exit(&muxifier); 1791 netstack_rele(ss->ss_netstack); 1792 return (ENXIO); 1793 } 1794 1795 /* 1796 * Test for invalid lower stream. 1797 * The check for the v_type != VFIFO and having a major 1798 * number not >= devcnt is done to avoid problems with 1799 * adding mux_node entry past the end of mux_nodes[]. 1800 * For FIFO's we don't add an entry so this isn't a 1801 * problem. 1802 */ 1803 if (((stpdown = fpdown->f_vnode->v_stream) == NULL) || 1804 (stpdown == stp) || (stpdown->sd_flag & 1805 (STPLEX|STRHUP|STRDERR|STWRERR|IOCWAIT|STRPLUMB)) || 1806 ((stpdown->sd_vnode->v_type != VFIFO) && 1807 (getmajor(stpdown->sd_vnode->v_rdev) >= ss->ss_devcnt)) || 1808 linkcycle(stp, stpdown, ss)) { 1809 mutex_exit(&muxifier); 1810 netstack_rele(ss->ss_netstack); 1811 return (EINVAL); 1812 } 1813 TRACE_1(TR_FAC_STREAMS_FR, 1814 TR_STPDOWN, "stpdown:%p", stpdown); 1815 rq = getendq(stp->sd_wrq); 1816 if (cmd == I_PLINK) 1817 rq = NULL; 1818 1819 linkp = alloclink(rq, stpdown->sd_wrq, fpdown); 1820 1821 strioc.ic_cmd = cmd; 1822 strioc.ic_timout = INFTIM; 1823 strioc.ic_len = sizeof (struct linkblk); 1824 strioc.ic_dp = (char *)&linkp->li_lblk; 1825 1826 /* 1827 * STRPLUMB protects plumbing changes and should be set before 1828 * link_addpassthru()/link_rempassthru() are called, so it is set here 1829 * and cleared in the end of mlink when passthru queue is removed. 1830 * Setting of STRPLUMB prevents reopens of the stream while passthru 1831 * queue is in-place (it is not a proper module and doesn't have open 1832 * entry point). 1833 * 1834 * STPLEX prevents any threads from entering the stream from above. It 1835 * can't be set before the call to link_addpassthru() because putnext 1836 * from below may cause stream head I/O routines to be called and these 1837 * routines assert that STPLEX is not set. After link_addpassthru() 1838 * nothing may come from below since the pass queue syncq is blocked. 1839 * Note also that STPLEX should be cleared before the call to 1840 * link_remmpassthru() since when messages start flowing to the stream 1841 * head (e.g. because of message propagation from the pass queue) stream 1842 * head I/O routines may be called with STPLEX flag set. 1843 * 1844 * When STPLEX is set, nothing may come into the stream from above and 1845 * it is safe to do a setq which will change stream head. So, the 1846 * correct sequence of actions is: 1847 * 1848 * 1) Set STRPLUMB 1849 * 2) Call link_addpassthru() 1850 * 3) Set STPLEX 1851 * 4) Call setq and update the stream state 1852 * 5) Clear STPLEX 1853 * 6) Call link_rempassthru() 1854 * 7) Clear STRPLUMB 1855 * 1856 * The same sequence applies to munlink() code. 1857 */ 1858 mutex_enter(&stpdown->sd_lock); 1859 stpdown->sd_flag |= STRPLUMB; 1860 mutex_exit(&stpdown->sd_lock); 1861 /* 1862 * Add passthru queue below lower mux. This will block 1863 * syncqs of lower muxs read queue during I_LINK/I_UNLINK. 1864 */ 1865 passq = link_addpassthru(stpdown); 1866 1867 mutex_enter(&stpdown->sd_lock); 1868 stpdown->sd_flag |= STPLEX; 1869 mutex_exit(&stpdown->sd_lock); 1870 1871 rq = _RD(stpdown->sd_wrq); 1872 /* 1873 * There may be messages in the streamhead's syncq due to messages 1874 * that arrived before link_addpassthru() was done. To avoid 1875 * background processing of the syncq happening simultaneous with 1876 * setq processing, we disable the streamhead syncq and wait until 1877 * existing background thread finishes working on it. 1878 */ 1879 wait_sq_svc(rq->q_syncq); 1880 passyncq = passq->q_syncq; 1881 if (!(passyncq->sq_flags & SQ_BLOCKED)) 1882 blocksq(passyncq, SQ_BLOCKED, 0); 1883 1884 ASSERT((rq->q_flag & QMT_TYPEMASK) == QMTSAFE); 1885 ASSERT(rq->q_syncq == SQ(rq) && _WR(rq)->q_syncq == SQ(rq)); 1886 rq->q_ptr = _WR(rq)->q_ptr = NULL; 1887 1888 /* setq might sleep in allocator - avoid holding locks. */ 1889 /* Note: we are holding muxifier here. */ 1890 1891 str = stp->sd_strtab; 1892 dp = &devimpl[getmajor(vp->v_rdev)]; 1893 ASSERT(dp->d_str == str); 1894 1895 qflag = dp->d_qflag; 1896 sqtype = dp->d_sqtype; 1897 1898 /* create perdm_t if needed */ 1899 if (NEED_DM(dp->d_dmp, qflag)) 1900 dp->d_dmp = hold_dm(str, qflag, sqtype); 1901 1902 dmp = dp->d_dmp; 1903 1904 setq(rq, str->st_muxrinit, str->st_muxwinit, dmp, qflag, sqtype, 1905 B_TRUE); 1906 1907 /* 1908 * XXX Remove any "odd" messages from the queue. 1909 * Keep only M_DATA, M_PROTO, M_PCPROTO. 1910 */ 1911 error = strdoioctl(stp, &strioc, FNATIVE, 1912 K_TO_K | STR_NOERROR | STR_NOSIG, crp, rvalp); 1913 if (error != 0) { 1914 lbfree(linkp); 1915 1916 if (!(passyncq->sq_flags & SQ_BLOCKED)) 1917 blocksq(passyncq, SQ_BLOCKED, 0); 1918 /* 1919 * Restore the stream head queue and then remove 1920 * the passq. Turn off STPLEX before we turn on 1921 * the stream by removing the passq. 1922 */ 1923 rq->q_ptr = _WR(rq)->q_ptr = stpdown; 1924 setq(rq, &strdata, &stwdata, NULL, QMTSAFE, SQ_CI|SQ_CO, 1925 B_TRUE); 1926 1927 mutex_enter(&stpdown->sd_lock); 1928 stpdown->sd_flag &= ~STPLEX; 1929 mutex_exit(&stpdown->sd_lock); 1930 1931 link_rempassthru(passq); 1932 1933 mutex_enter(&stpdown->sd_lock); 1934 stpdown->sd_flag &= ~STRPLUMB; 1935 /* Wakeup anyone waiting for STRPLUMB to clear. */ 1936 cv_broadcast(&stpdown->sd_monitor); 1937 mutex_exit(&stpdown->sd_lock); 1938 1939 mutex_exit(&muxifier); 1940 netstack_rele(ss->ss_netstack); 1941 return (error); 1942 } 1943 mutex_enter(&fpdown->f_tlock); 1944 fpdown->f_count++; 1945 mutex_exit(&fpdown->f_tlock); 1946 1947 /* 1948 * if we've made it here the linkage is all set up so we should also 1949 * set up the layered driver linkages 1950 */ 1951 1952 ASSERT((cmd == I_LINK) || (cmd == I_PLINK)); 1953 if (cmd == I_LINK) { 1954 ldi_mlink_fp(stp, fpdown, lhlink, LINKNORMAL); 1955 } else { 1956 ldi_mlink_fp(stp, fpdown, lhlink, LINKPERSIST); 1957 } 1958 1959 link_rempassthru(passq); 1960 1961 mux_addedge(stp, stpdown, linkp->li_lblk.l_index, ss); 1962 1963 /* 1964 * Mark the upper stream as having dependent links 1965 * so that strclose can clean it up. 1966 */ 1967 if (cmd == I_LINK) { 1968 mutex_enter(&stp->sd_lock); 1969 stp->sd_flag |= STRHASLINKS; 1970 mutex_exit(&stp->sd_lock); 1971 } 1972 /* 1973 * Wake up any other processes that may have been 1974 * waiting on the lower stream. These will all 1975 * error out. 1976 */ 1977 mutex_enter(&stpdown->sd_lock); 1978 /* The passthru module is removed so we may release STRPLUMB */ 1979 stpdown->sd_flag &= ~STRPLUMB; 1980 cv_broadcast(&rq->q_wait); 1981 cv_broadcast(&_WR(rq)->q_wait); 1982 cv_broadcast(&stpdown->sd_monitor); 1983 mutex_exit(&stpdown->sd_lock); 1984 mutex_exit(&muxifier); 1985 *rvalp = linkp->li_lblk.l_index; 1986 netstack_rele(ss->ss_netstack); 1987 return (0); 1988 } 1989 1990 int 1991 mlink(vnode_t *vp, int cmd, int arg, cred_t *crp, int *rvalp, int lhlink) 1992 { 1993 int ret; 1994 struct file *fpdown; 1995 1996 fpdown = getf(arg); 1997 ret = mlink_file(vp, cmd, fpdown, crp, rvalp, lhlink); 1998 if (fpdown != NULL) 1999 releasef(arg); 2000 return (ret); 2001 } 2002 2003 /* 2004 * Unlink a multiplexor link. Stp is the controlling stream for the 2005 * link, and linkp points to the link's entry in the linkinfo list. 2006 * The muxifier lock must be held on entry and is dropped on exit. 2007 * 2008 * NOTE : Currently it is assumed that mux would process all the messages 2009 * sitting on it's queue before ACKing the UNLINK. It is the responsibility 2010 * of the mux to handle all the messages that arrive before UNLINK. 2011 * If the mux has to send down messages on its lower stream before 2012 * ACKing I_UNLINK, then it *should* know to handle messages even 2013 * after the UNLINK is acked (actually it should be able to handle till we 2014 * re-block the read side of the pass queue here). If the mux does not 2015 * open up the lower stream, any messages that arrive during UNLINK 2016 * will be put in the stream head. In the case of lower stream opening 2017 * up, some messages might land in the stream head depending on when 2018 * the message arrived and when the read side of the pass queue was 2019 * re-blocked. 2020 */ 2021 int 2022 munlink(stdata_t *stp, linkinfo_t *linkp, int flag, cred_t *crp, int *rvalp, 2023 str_stack_t *ss) 2024 { 2025 struct strioctl strioc; 2026 struct stdata *stpdown; 2027 queue_t *rq, *wrq; 2028 queue_t *passq; 2029 syncq_t *passyncq; 2030 int error = 0; 2031 file_t *fpdown; 2032 2033 ASSERT(MUTEX_HELD(&muxifier)); 2034 2035 stpdown = linkp->li_fpdown->f_vnode->v_stream; 2036 2037 /* 2038 * See the comment in mlink() concerning STRPLUMB/STPLEX flags. 2039 */ 2040 mutex_enter(&stpdown->sd_lock); 2041 stpdown->sd_flag |= STRPLUMB; 2042 mutex_exit(&stpdown->sd_lock); 2043 2044 /* 2045 * Add passthru queue below lower mux. This will block 2046 * syncqs of lower muxs read queue during I_LINK/I_UNLINK. 2047 */ 2048 passq = link_addpassthru(stpdown); 2049 2050 if ((flag & LINKTYPEMASK) == LINKNORMAL) 2051 strioc.ic_cmd = I_UNLINK; 2052 else 2053 strioc.ic_cmd = I_PUNLINK; 2054 strioc.ic_timout = INFTIM; 2055 strioc.ic_len = sizeof (struct linkblk); 2056 strioc.ic_dp = (char *)&linkp->li_lblk; 2057 2058 error = strdoioctl(stp, &strioc, FNATIVE, 2059 K_TO_K | STR_NOERROR | STR_NOSIG, crp, rvalp); 2060 2061 /* 2062 * If there was an error and this is not called via strclose, 2063 * return to the user. Otherwise, pretend there was no error 2064 * and close the link. 2065 */ 2066 if (error) { 2067 if (flag & LINKCLOSE) { 2068 cmn_err(CE_WARN, "KERNEL: munlink: could not perform " 2069 "unlink ioctl, closing anyway (%d)\n", error); 2070 } else { 2071 link_rempassthru(passq); 2072 mutex_enter(&stpdown->sd_lock); 2073 stpdown->sd_flag &= ~STRPLUMB; 2074 cv_broadcast(&stpdown->sd_monitor); 2075 mutex_exit(&stpdown->sd_lock); 2076 mutex_exit(&muxifier); 2077 return (error); 2078 } 2079 } 2080 2081 mux_rmvedge(stp, linkp->li_lblk.l_index, ss); 2082 fpdown = linkp->li_fpdown; 2083 lbfree(linkp); 2084 2085 /* 2086 * We go ahead and drop muxifier here--it's a nasty global lock that 2087 * can slow others down. It's okay to since attempts to mlink() this 2088 * stream will be stopped because STPLEX is still set in the stdata 2089 * structure, and munlink() is stopped because mux_rmvedge() and 2090 * lbfree() have removed it from mux_nodes[] and linkinfo_list, 2091 * respectively. Note that we defer the closef() of fpdown until 2092 * after we drop muxifier since strclose() can call munlinkall(). 2093 */ 2094 mutex_exit(&muxifier); 2095 2096 wrq = stpdown->sd_wrq; 2097 rq = _RD(wrq); 2098 2099 /* 2100 * Get rid of outstanding service procedure runs, before we make 2101 * it a stream head, since a stream head doesn't have any service 2102 * procedure. 2103 */ 2104 disable_svc(rq); 2105 wait_svc(rq); 2106 2107 /* 2108 * Since we don't disable the syncq for QPERMOD, we wait for whatever 2109 * is queued up to be finished. mux should take care that nothing is 2110 * send down to this queue. We should do it now as we're going to block 2111 * passyncq if it was unblocked. 2112 */ 2113 if (wrq->q_flag & QPERMOD) { 2114 syncq_t *sq = wrq->q_syncq; 2115 2116 mutex_enter(SQLOCK(sq)); 2117 while (wrq->q_sqflags & Q_SQQUEUED) { 2118 sq->sq_flags |= SQ_WANTWAKEUP; 2119 cv_wait(&sq->sq_wait, SQLOCK(sq)); 2120 } 2121 mutex_exit(SQLOCK(sq)); 2122 } 2123 passyncq = passq->q_syncq; 2124 if (!(passyncq->sq_flags & SQ_BLOCKED)) { 2125 2126 syncq_t *sq, *outer; 2127 2128 /* 2129 * Messages could be flowing from underneath. We will 2130 * block the read side of the passq. This would be 2131 * sufficient for QPAIR and QPERQ muxes to ensure 2132 * that no data is flowing up into this queue 2133 * and hence no thread active in this instance of 2134 * lower mux. But for QPERMOD and QMTOUTPERIM there 2135 * could be messages on the inner and outer/inner 2136 * syncqs respectively. We will wait for them to drain. 2137 * Because passq is blocked messages end up in the syncq 2138 * And qfill_syncq could possibly end up setting QFULL 2139 * which will access the rq->q_flag. Hence, we have to 2140 * acquire the QLOCK in setq. 2141 * 2142 * XXX Messages can also flow from top into this 2143 * queue though the unlink is over (Ex. some instance 2144 * in putnext() called from top that has still not 2145 * accessed this queue. And also putq(lowerq) ?). 2146 * Solution : How about blocking the l_qtop queue ? 2147 * Do we really care about such pure D_MP muxes ? 2148 */ 2149 2150 blocksq(passyncq, SQ_BLOCKED, 0); 2151 2152 sq = rq->q_syncq; 2153 if ((outer = sq->sq_outer) != NULL) { 2154 2155 /* 2156 * We have to just wait for the outer sq_count 2157 * drop to zero. As this does not prevent new 2158 * messages to enter the outer perimeter, this 2159 * is subject to starvation. 2160 * 2161 * NOTE :Because of blocksq above, messages could 2162 * be in the inner syncq only because of some 2163 * thread holding the outer perimeter exclusively. 2164 * Hence it would be sufficient to wait for the 2165 * exclusive holder of the outer perimeter to drain 2166 * the inner and outer syncqs. But we will not depend 2167 * on this feature and hence check the inner syncqs 2168 * separately. 2169 */ 2170 wait_syncq(outer); 2171 } 2172 2173 2174 /* 2175 * There could be messages destined for 2176 * this queue. Let the exclusive holder 2177 * drain it. 2178 */ 2179 2180 wait_syncq(sq); 2181 ASSERT((rq->q_flag & QPERMOD) || 2182 ((rq->q_syncq->sq_head == NULL) && 2183 (_WR(rq)->q_syncq->sq_head == NULL))); 2184 } 2185 2186 /* 2187 * We haven't taken care of QPERMOD case yet. QPERMOD is a special 2188 * case as we don't disable its syncq or remove it off the syncq 2189 * service list. 2190 */ 2191 if (rq->q_flag & QPERMOD) { 2192 syncq_t *sq = rq->q_syncq; 2193 2194 mutex_enter(SQLOCK(sq)); 2195 while (rq->q_sqflags & Q_SQQUEUED) { 2196 sq->sq_flags |= SQ_WANTWAKEUP; 2197 cv_wait(&sq->sq_wait, SQLOCK(sq)); 2198 } 2199 mutex_exit(SQLOCK(sq)); 2200 } 2201 2202 /* 2203 * flush_syncq changes states only when there is some messages to 2204 * free. ie when it returns non-zero value to return. 2205 */ 2206 ASSERT(flush_syncq(rq->q_syncq, rq) == 0); 2207 ASSERT(flush_syncq(wrq->q_syncq, wrq) == 0); 2208 2209 /* 2210 * No body else should know about this queue now. 2211 * If the mux did not process the messages before 2212 * acking the I_UNLINK, free them now. 2213 */ 2214 2215 flushq(rq, FLUSHALL); 2216 flushq(_WR(rq), FLUSHALL); 2217 2218 /* 2219 * Convert the mux lower queue into a stream head queue. 2220 * Turn off STPLEX before we turn on the stream by removing the passq. 2221 */ 2222 rq->q_ptr = wrq->q_ptr = stpdown; 2223 setq(rq, &strdata, &stwdata, NULL, QMTSAFE, SQ_CI|SQ_CO, B_TRUE); 2224 2225 ASSERT((rq->q_flag & QMT_TYPEMASK) == QMTSAFE); 2226 ASSERT(rq->q_syncq == SQ(rq) && _WR(rq)->q_syncq == SQ(rq)); 2227 2228 enable_svc(rq); 2229 2230 /* 2231 * Now it is a proper stream, so STPLEX is cleared. But STRPLUMB still 2232 * needs to be set to prevent reopen() of the stream - such reopen may 2233 * try to call non-existent pass queue open routine and panic. 2234 */ 2235 mutex_enter(&stpdown->sd_lock); 2236 stpdown->sd_flag &= ~STPLEX; 2237 mutex_exit(&stpdown->sd_lock); 2238 2239 ASSERT(((flag & LINKTYPEMASK) == LINKNORMAL) || 2240 ((flag & LINKTYPEMASK) == LINKPERSIST)); 2241 2242 /* clean up the layered driver linkages */ 2243 if ((flag & LINKTYPEMASK) == LINKNORMAL) { 2244 ldi_munlink_fp(stp, fpdown, LINKNORMAL); 2245 } else { 2246 ldi_munlink_fp(stp, fpdown, LINKPERSIST); 2247 } 2248 2249 link_rempassthru(passq); 2250 2251 /* 2252 * Now all plumbing changes are finished and STRPLUMB is no 2253 * longer needed. 2254 */ 2255 mutex_enter(&stpdown->sd_lock); 2256 stpdown->sd_flag &= ~STRPLUMB; 2257 cv_broadcast(&stpdown->sd_monitor); 2258 mutex_exit(&stpdown->sd_lock); 2259 2260 (void) closef(fpdown); 2261 return (0); 2262 } 2263 2264 /* 2265 * Unlink all multiplexor links for which stp is the controlling stream. 2266 * Return 0, or a non-zero errno on failure. 2267 */ 2268 int 2269 munlinkall(stdata_t *stp, int flag, cred_t *crp, int *rvalp, str_stack_t *ss) 2270 { 2271 linkinfo_t *linkp; 2272 int error = 0; 2273 2274 mutex_enter(&muxifier); 2275 while (linkp = findlinks(stp, 0, flag, ss)) { 2276 /* 2277 * munlink() releases the muxifier lock. 2278 */ 2279 if (error = munlink(stp, linkp, flag, crp, rvalp, ss)) 2280 return (error); 2281 mutex_enter(&muxifier); 2282 } 2283 mutex_exit(&muxifier); 2284 return (0); 2285 } 2286 2287 /* 2288 * A multiplexor link has been made. Add an 2289 * edge to the directed graph. 2290 */ 2291 void 2292 mux_addedge(stdata_t *upstp, stdata_t *lostp, int muxid, str_stack_t *ss) 2293 { 2294 struct mux_node *np; 2295 struct mux_edge *ep; 2296 major_t upmaj; 2297 major_t lomaj; 2298 2299 upmaj = getmajor(upstp->sd_vnode->v_rdev); 2300 lomaj = getmajor(lostp->sd_vnode->v_rdev); 2301 np = &ss->ss_mux_nodes[upmaj]; 2302 if (np->mn_outp) { 2303 ep = np->mn_outp; 2304 while (ep->me_nextp) 2305 ep = ep->me_nextp; 2306 ep->me_nextp = kmem_alloc(sizeof (struct mux_edge), KM_SLEEP); 2307 ep = ep->me_nextp; 2308 } else { 2309 np->mn_outp = kmem_alloc(sizeof (struct mux_edge), KM_SLEEP); 2310 ep = np->mn_outp; 2311 } 2312 ep->me_nextp = NULL; 2313 ep->me_muxid = muxid; 2314 /* 2315 * Save the dev_t for the purposes of str_stack_shutdown. 2316 * str_stack_shutdown assumes that the device allows reopen, since 2317 * this dev_t is the one after any cloning by xx_open(). 2318 * Would prefer finding the dev_t from before any cloning, 2319 * but specfs doesn't retain that. 2320 */ 2321 ep->me_dev = upstp->sd_vnode->v_rdev; 2322 if (lostp->sd_vnode->v_type == VFIFO) 2323 ep->me_nodep = NULL; 2324 else 2325 ep->me_nodep = &ss->ss_mux_nodes[lomaj]; 2326 } 2327 2328 /* 2329 * A multiplexor link has been removed. Remove the 2330 * edge in the directed graph. 2331 */ 2332 void 2333 mux_rmvedge(stdata_t *upstp, int muxid, str_stack_t *ss) 2334 { 2335 struct mux_node *np; 2336 struct mux_edge *ep; 2337 struct mux_edge *pep = NULL; 2338 major_t upmaj; 2339 2340 upmaj = getmajor(upstp->sd_vnode->v_rdev); 2341 np = &ss->ss_mux_nodes[upmaj]; 2342 ASSERT(np->mn_outp != NULL); 2343 ep = np->mn_outp; 2344 while (ep) { 2345 if (ep->me_muxid == muxid) { 2346 if (pep) 2347 pep->me_nextp = ep->me_nextp; 2348 else 2349 np->mn_outp = ep->me_nextp; 2350 kmem_free(ep, sizeof (struct mux_edge)); 2351 return; 2352 } 2353 pep = ep; 2354 ep = ep->me_nextp; 2355 } 2356 ASSERT(0); /* should not reach here */ 2357 } 2358 2359 /* 2360 * Translate the device flags (from conf.h) to the corresponding 2361 * qflag and sq_flag (type) values. 2362 */ 2363 int 2364 devflg_to_qflag(struct streamtab *stp, uint32_t devflag, uint32_t *qflagp, 2365 uint32_t *sqtypep) 2366 { 2367 uint32_t qflag = 0; 2368 uint32_t sqtype = 0; 2369 2370 if (devflag & _D_OLD) 2371 goto bad; 2372 2373 /* Inner perimeter presence and scope */ 2374 switch (devflag & D_MTINNER_MASK) { 2375 case D_MP: 2376 qflag |= QMTSAFE; 2377 sqtype |= SQ_CI; 2378 break; 2379 case D_MTPERQ|D_MP: 2380 qflag |= QPERQ; 2381 break; 2382 case D_MTQPAIR|D_MP: 2383 qflag |= QPAIR; 2384 break; 2385 case D_MTPERMOD|D_MP: 2386 qflag |= QPERMOD; 2387 break; 2388 default: 2389 goto bad; 2390 } 2391 2392 /* Outer perimeter */ 2393 if (devflag & D_MTOUTPERIM) { 2394 switch (devflag & D_MTINNER_MASK) { 2395 case D_MP: 2396 case D_MTPERQ|D_MP: 2397 case D_MTQPAIR|D_MP: 2398 break; 2399 default: 2400 goto bad; 2401 } 2402 qflag |= QMTOUTPERIM; 2403 } 2404 2405 /* Inner perimeter modifiers */ 2406 if (devflag & D_MTINNER_MOD) { 2407 switch (devflag & D_MTINNER_MASK) { 2408 case D_MP: 2409 goto bad; 2410 default: 2411 break; 2412 } 2413 if (devflag & D_MTPUTSHARED) 2414 sqtype |= SQ_CIPUT; 2415 if (devflag & _D_MTOCSHARED) { 2416 /* 2417 * The code in putnext assumes that it has the 2418 * highest concurrency by not checking sq_count. 2419 * Thus _D_MTOCSHARED can only be supported when 2420 * D_MTPUTSHARED is set. 2421 */ 2422 if (!(devflag & D_MTPUTSHARED)) 2423 goto bad; 2424 sqtype |= SQ_CIOC; 2425 } 2426 if (devflag & _D_MTCBSHARED) { 2427 /* 2428 * The code in putnext assumes that it has the 2429 * highest concurrency by not checking sq_count. 2430 * Thus _D_MTCBSHARED can only be supported when 2431 * D_MTPUTSHARED is set. 2432 */ 2433 if (!(devflag & D_MTPUTSHARED)) 2434 goto bad; 2435 sqtype |= SQ_CICB; 2436 } 2437 if (devflag & _D_MTSVCSHARED) { 2438 /* 2439 * The code in putnext assumes that it has the 2440 * highest concurrency by not checking sq_count. 2441 * Thus _D_MTSVCSHARED can only be supported when 2442 * D_MTPUTSHARED is set. Also _D_MTSVCSHARED is 2443 * supported only for QPERMOD. 2444 */ 2445 if (!(devflag & D_MTPUTSHARED) || !(qflag & QPERMOD)) 2446 goto bad; 2447 sqtype |= SQ_CISVC; 2448 } 2449 } 2450 2451 /* Default outer perimeter concurrency */ 2452 sqtype |= SQ_CO; 2453 2454 /* Outer perimeter modifiers */ 2455 if (devflag & D_MTOCEXCL) { 2456 if (!(devflag & D_MTOUTPERIM)) { 2457 /* No outer perimeter */ 2458 goto bad; 2459 } 2460 sqtype &= ~SQ_COOC; 2461 } 2462 2463 /* Synchronous Streams extended qinit structure */ 2464 if (devflag & D_SYNCSTR) 2465 qflag |= QSYNCSTR; 2466 2467 /* 2468 * Private flag used by a transport module to indicate 2469 * to sockfs that it supports direct-access mode without 2470 * having to go through STREAMS. 2471 */ 2472 if (devflag & _D_DIRECT) { 2473 /* Reject unless the module is fully-MT (no perimeter) */ 2474 if ((qflag & QMT_TYPEMASK) != QMTSAFE) 2475 goto bad; 2476 qflag |= _QDIRECT; 2477 } 2478 2479 *qflagp = qflag; 2480 *sqtypep = sqtype; 2481 return (0); 2482 2483 bad: 2484 cmn_err(CE_WARN, 2485 "stropen: bad MT flags (0x%x) in driver '%s'", 2486 (int)(qflag & D_MTSAFETY_MASK), 2487 stp->st_rdinit->qi_minfo->mi_idname); 2488 2489 return (EINVAL); 2490 } 2491 2492 /* 2493 * Set the interface values for a pair of queues (qinit structure, 2494 * packet sizes, water marks). 2495 * setq assumes that the caller does not have a claim (entersq or claimq) 2496 * on the queue. 2497 */ 2498 void 2499 setq(queue_t *rq, struct qinit *rinit, struct qinit *winit, 2500 perdm_t *dmp, uint32_t qflag, uint32_t sqtype, boolean_t lock_needed) 2501 { 2502 queue_t *wq; 2503 syncq_t *sq, *outer; 2504 2505 ASSERT(rq->q_flag & QREADR); 2506 ASSERT((qflag & QMT_TYPEMASK) != 0); 2507 IMPLY((qflag & (QPERMOD | QMTOUTPERIM)), dmp != NULL); 2508 2509 wq = _WR(rq); 2510 rq->q_qinfo = rinit; 2511 rq->q_hiwat = rinit->qi_minfo->mi_hiwat; 2512 rq->q_lowat = rinit->qi_minfo->mi_lowat; 2513 rq->q_minpsz = rinit->qi_minfo->mi_minpsz; 2514 rq->q_maxpsz = rinit->qi_minfo->mi_maxpsz; 2515 wq->q_qinfo = winit; 2516 wq->q_hiwat = winit->qi_minfo->mi_hiwat; 2517 wq->q_lowat = winit->qi_minfo->mi_lowat; 2518 wq->q_minpsz = winit->qi_minfo->mi_minpsz; 2519 wq->q_maxpsz = winit->qi_minfo->mi_maxpsz; 2520 2521 /* Remove old syncqs */ 2522 sq = rq->q_syncq; 2523 outer = sq->sq_outer; 2524 if (outer != NULL) { 2525 ASSERT(wq->q_syncq->sq_outer == outer); 2526 outer_remove(outer, rq->q_syncq); 2527 if (wq->q_syncq != rq->q_syncq) 2528 outer_remove(outer, wq->q_syncq); 2529 } 2530 ASSERT(sq->sq_outer == NULL); 2531 ASSERT(sq->sq_onext == NULL && sq->sq_oprev == NULL); 2532 2533 if (sq != SQ(rq)) { 2534 if (!(rq->q_flag & QPERMOD)) 2535 free_syncq(sq); 2536 if (wq->q_syncq == rq->q_syncq) 2537 wq->q_syncq = NULL; 2538 rq->q_syncq = NULL; 2539 } 2540 if (wq->q_syncq != NULL && wq->q_syncq != sq && 2541 wq->q_syncq != SQ(rq)) { 2542 free_syncq(wq->q_syncq); 2543 wq->q_syncq = NULL; 2544 } 2545 ASSERT(rq->q_syncq == NULL || (rq->q_syncq->sq_head == NULL && 2546 rq->q_syncq->sq_tail == NULL)); 2547 ASSERT(wq->q_syncq == NULL || (wq->q_syncq->sq_head == NULL && 2548 wq->q_syncq->sq_tail == NULL)); 2549 2550 if (!(rq->q_flag & QPERMOD) && 2551 rq->q_syncq != NULL && rq->q_syncq->sq_ciputctrl != NULL) { 2552 ASSERT(rq->q_syncq->sq_nciputctrl == n_ciputctrl - 1); 2553 SUMCHECK_CIPUTCTRL_COUNTS(rq->q_syncq->sq_ciputctrl, 2554 rq->q_syncq->sq_nciputctrl, 0); 2555 ASSERT(ciputctrl_cache != NULL); 2556 kmem_cache_free(ciputctrl_cache, rq->q_syncq->sq_ciputctrl); 2557 rq->q_syncq->sq_ciputctrl = NULL; 2558 rq->q_syncq->sq_nciputctrl = 0; 2559 } 2560 2561 if (!(wq->q_flag & QPERMOD) && 2562 wq->q_syncq != NULL && wq->q_syncq->sq_ciputctrl != NULL) { 2563 ASSERT(wq->q_syncq->sq_nciputctrl == n_ciputctrl - 1); 2564 SUMCHECK_CIPUTCTRL_COUNTS(wq->q_syncq->sq_ciputctrl, 2565 wq->q_syncq->sq_nciputctrl, 0); 2566 ASSERT(ciputctrl_cache != NULL); 2567 kmem_cache_free(ciputctrl_cache, wq->q_syncq->sq_ciputctrl); 2568 wq->q_syncq->sq_ciputctrl = NULL; 2569 wq->q_syncq->sq_nciputctrl = 0; 2570 } 2571 2572 sq = SQ(rq); 2573 ASSERT(sq->sq_head == NULL && sq->sq_tail == NULL); 2574 ASSERT(sq->sq_outer == NULL); 2575 ASSERT(sq->sq_onext == NULL && sq->sq_oprev == NULL); 2576 2577 /* 2578 * Create syncqs based on qflag and sqtype. Set the SQ_TYPES_IN_FLAGS 2579 * bits in sq_flag based on the sqtype. 2580 */ 2581 ASSERT((sq->sq_flags & ~SQ_TYPES_IN_FLAGS) == 0); 2582 2583 rq->q_syncq = wq->q_syncq = sq; 2584 sq->sq_type = sqtype; 2585 sq->sq_flags = (sqtype & SQ_TYPES_IN_FLAGS); 2586 2587 /* 2588 * We are making sq_svcflags zero, 2589 * resetting SQ_DISABLED in case it was set by 2590 * wait_svc() in the munlink path. 2591 * 2592 */ 2593 ASSERT((sq->sq_svcflags & SQ_SERVICE) == 0); 2594 sq->sq_svcflags = 0; 2595 2596 /* 2597 * We need to acquire the lock here for the mlink and munlink case, 2598 * where canputnext, backenable, etc can access the q_flag. 2599 */ 2600 if (lock_needed) { 2601 mutex_enter(QLOCK(rq)); 2602 rq->q_flag = (rq->q_flag & ~QMT_TYPEMASK) | QWANTR | qflag; 2603 mutex_exit(QLOCK(rq)); 2604 mutex_enter(QLOCK(wq)); 2605 wq->q_flag = (wq->q_flag & ~QMT_TYPEMASK) | QWANTR | qflag; 2606 mutex_exit(QLOCK(wq)); 2607 } else { 2608 rq->q_flag = (rq->q_flag & ~QMT_TYPEMASK) | QWANTR | qflag; 2609 wq->q_flag = (wq->q_flag & ~QMT_TYPEMASK) | QWANTR | qflag; 2610 } 2611 2612 if (qflag & QPERQ) { 2613 /* Allocate a separate syncq for the write side */ 2614 sq = new_syncq(); 2615 sq->sq_type = rq->q_syncq->sq_type; 2616 sq->sq_flags = rq->q_syncq->sq_flags; 2617 ASSERT(sq->sq_outer == NULL && sq->sq_onext == NULL && 2618 sq->sq_oprev == NULL); 2619 wq->q_syncq = sq; 2620 } 2621 if (qflag & QPERMOD) { 2622 sq = dmp->dm_sq; 2623 2624 /* 2625 * Assert that we do have an inner perimeter syncq and that it 2626 * does not have an outer perimeter associated with it. 2627 */ 2628 ASSERT(sq->sq_outer == NULL && sq->sq_onext == NULL && 2629 sq->sq_oprev == NULL); 2630 rq->q_syncq = wq->q_syncq = sq; 2631 } 2632 if (qflag & QMTOUTPERIM) { 2633 outer = dmp->dm_sq; 2634 2635 ASSERT(outer->sq_outer == NULL); 2636 outer_insert(outer, rq->q_syncq); 2637 if (wq->q_syncq != rq->q_syncq) 2638 outer_insert(outer, wq->q_syncq); 2639 } 2640 ASSERT((rq->q_syncq->sq_flags & SQ_TYPES_IN_FLAGS) == 2641 (rq->q_syncq->sq_type & SQ_TYPES_IN_FLAGS)); 2642 ASSERT((wq->q_syncq->sq_flags & SQ_TYPES_IN_FLAGS) == 2643 (wq->q_syncq->sq_type & SQ_TYPES_IN_FLAGS)); 2644 ASSERT((rq->q_flag & QMT_TYPEMASK) == (qflag & QMT_TYPEMASK)); 2645 2646 /* 2647 * Initialize struio() types. 2648 */ 2649 rq->q_struiot = 2650 (rq->q_flag & QSYNCSTR) ? rinit->qi_struiot : STRUIOT_NONE; 2651 wq->q_struiot = 2652 (wq->q_flag & QSYNCSTR) ? winit->qi_struiot : STRUIOT_NONE; 2653 } 2654 2655 perdm_t * 2656 hold_dm(struct streamtab *str, uint32_t qflag, uint32_t sqtype) 2657 { 2658 syncq_t *sq; 2659 perdm_t **pp; 2660 perdm_t *p; 2661 perdm_t *dmp; 2662 2663 ASSERT(str != NULL); 2664 ASSERT(qflag & (QPERMOD | QMTOUTPERIM)); 2665 2666 rw_enter(&perdm_rwlock, RW_READER); 2667 for (p = perdm_list; p != NULL; p = p->dm_next) { 2668 if (p->dm_str == str) { /* found one */ 2669 atomic_add_32(&(p->dm_ref), 1); 2670 rw_exit(&perdm_rwlock); 2671 return (p); 2672 } 2673 } 2674 rw_exit(&perdm_rwlock); 2675 2676 sq = new_syncq(); 2677 if (qflag & QPERMOD) { 2678 sq->sq_type = sqtype | SQ_PERMOD; 2679 sq->sq_flags = sqtype & SQ_TYPES_IN_FLAGS; 2680 } else { 2681 ASSERT(qflag & QMTOUTPERIM); 2682 sq->sq_onext = sq->sq_oprev = sq; 2683 } 2684 2685 dmp = kmem_alloc(sizeof (perdm_t), KM_SLEEP); 2686 dmp->dm_sq = sq; 2687 dmp->dm_str = str; 2688 dmp->dm_ref = 1; 2689 dmp->dm_next = NULL; 2690 2691 rw_enter(&perdm_rwlock, RW_WRITER); 2692 for (pp = &perdm_list; (p = *pp) != NULL; pp = &(p->dm_next)) { 2693 if (p->dm_str == str) { /* already present */ 2694 p->dm_ref++; 2695 rw_exit(&perdm_rwlock); 2696 free_syncq(sq); 2697 kmem_free(dmp, sizeof (perdm_t)); 2698 return (p); 2699 } 2700 } 2701 2702 *pp = dmp; 2703 rw_exit(&perdm_rwlock); 2704 return (dmp); 2705 } 2706 2707 void 2708 rele_dm(perdm_t *dmp) 2709 { 2710 perdm_t **pp; 2711 perdm_t *p; 2712 2713 rw_enter(&perdm_rwlock, RW_WRITER); 2714 ASSERT(dmp->dm_ref > 0); 2715 2716 if (--dmp->dm_ref > 0) { 2717 rw_exit(&perdm_rwlock); 2718 return; 2719 } 2720 2721 for (pp = &perdm_list; (p = *pp) != NULL; pp = &(p->dm_next)) 2722 if (p == dmp) 2723 break; 2724 ASSERT(p == dmp); 2725 *pp = p->dm_next; 2726 rw_exit(&perdm_rwlock); 2727 2728 /* 2729 * Wait for any background processing that relies on the 2730 * syncq to complete before it is freed. 2731 */ 2732 wait_sq_svc(p->dm_sq); 2733 free_syncq(p->dm_sq); 2734 kmem_free(p, sizeof (perdm_t)); 2735 } 2736 2737 /* 2738 * Make a protocol message given control and data buffers. 2739 * n.b., this can block; be careful of what locks you hold when calling it. 2740 * 2741 * If sd_maxblk is less than *iosize this routine can fail part way through 2742 * (due to an allocation failure). In this case on return *iosize will contain 2743 * the amount that was consumed. Otherwise *iosize will not be modified 2744 * i.e. it will contain the amount that was consumed. 2745 */ 2746 int 2747 strmakemsg( 2748 struct strbuf *mctl, 2749 ssize_t *iosize, 2750 struct uio *uiop, 2751 stdata_t *stp, 2752 int32_t flag, 2753 mblk_t **mpp) 2754 { 2755 mblk_t *mpctl = NULL; 2756 mblk_t *mpdata = NULL; 2757 int error; 2758 2759 ASSERT(uiop != NULL); 2760 2761 *mpp = NULL; 2762 /* Create control part, if any */ 2763 if ((mctl != NULL) && (mctl->len >= 0)) { 2764 error = strmakectl(mctl, flag, uiop->uio_fmode, &mpctl); 2765 if (error) 2766 return (error); 2767 } 2768 /* Create data part, if any */ 2769 if (*iosize >= 0) { 2770 error = strmakedata(iosize, uiop, stp, flag, &mpdata); 2771 if (error) { 2772 freemsg(mpctl); 2773 return (error); 2774 } 2775 } 2776 if (mpctl != NULL) { 2777 if (mpdata != NULL) 2778 linkb(mpctl, mpdata); 2779 *mpp = mpctl; 2780 } else { 2781 *mpp = mpdata; 2782 } 2783 return (0); 2784 } 2785 2786 /* 2787 * Make the control part of a protocol message given a control buffer. 2788 * n.b., this can block; be careful of what locks you hold when calling it. 2789 */ 2790 int 2791 strmakectl( 2792 struct strbuf *mctl, 2793 int32_t flag, 2794 int32_t fflag, 2795 mblk_t **mpp) 2796 { 2797 mblk_t *bp = NULL; 2798 unsigned char msgtype; 2799 int error = 0; 2800 2801 *mpp = NULL; 2802 /* 2803 * Create control part of message, if any. 2804 */ 2805 if ((mctl != NULL) && (mctl->len >= 0)) { 2806 caddr_t base; 2807 int ctlcount; 2808 int allocsz; 2809 2810 if (flag & RS_HIPRI) 2811 msgtype = M_PCPROTO; 2812 else 2813 msgtype = M_PROTO; 2814 2815 ctlcount = mctl->len; 2816 base = mctl->buf; 2817 2818 /* 2819 * Give modules a better chance to reuse M_PROTO/M_PCPROTO 2820 * blocks by increasing the size to something more usable. 2821 */ 2822 allocsz = MAX(ctlcount, 64); 2823 2824 /* 2825 * Range checking has already been done; simply try 2826 * to allocate a message block for the ctl part. 2827 */ 2828 while (!(bp = allocb(allocsz, BPRI_MED))) { 2829 if (fflag & (FNDELAY|FNONBLOCK)) 2830 return (EAGAIN); 2831 if (error = strwaitbuf(allocsz, BPRI_MED)) 2832 return (error); 2833 } 2834 2835 bp->b_datap->db_type = msgtype; 2836 if (copyin(base, bp->b_wptr, ctlcount)) { 2837 freeb(bp); 2838 return (EFAULT); 2839 } 2840 bp->b_wptr += ctlcount; 2841 } 2842 *mpp = bp; 2843 return (0); 2844 } 2845 2846 /* 2847 * Make a protocol message given data buffers. 2848 * n.b., this can block; be careful of what locks you hold when calling it. 2849 * 2850 * If sd_maxblk is less than *iosize this routine can fail part way through 2851 * (due to an allocation failure). In this case on return *iosize will contain 2852 * the amount that was consumed. Otherwise *iosize will not be modified 2853 * i.e. it will contain the amount that was consumed. 2854 */ 2855 int 2856 strmakedata( 2857 ssize_t *iosize, 2858 struct uio *uiop, 2859 stdata_t *stp, 2860 int32_t flag, 2861 mblk_t **mpp) 2862 { 2863 mblk_t *mp = NULL; 2864 mblk_t *bp; 2865 int wroff = (int)stp->sd_wroff; 2866 int tail_len = (int)stp->sd_tail; 2867 int extra = wroff + tail_len; 2868 int error = 0; 2869 ssize_t maxblk; 2870 ssize_t count = *iosize; 2871 cred_t *cr = CRED(); 2872 2873 *mpp = NULL; 2874 if (count < 0) 2875 return (0); 2876 2877 maxblk = stp->sd_maxblk; 2878 if (maxblk == INFPSZ) 2879 maxblk = count; 2880 2881 /* 2882 * Create data part of message, if any. 2883 */ 2884 do { 2885 ssize_t size; 2886 dblk_t *dp; 2887 2888 ASSERT(uiop); 2889 2890 size = MIN(count, maxblk); 2891 2892 while ((bp = allocb_cred(size + extra, cr)) == NULL) { 2893 error = EAGAIN; 2894 if ((uiop->uio_fmode & (FNDELAY|FNONBLOCK)) || 2895 (error = strwaitbuf(size + extra, BPRI_MED)) != 0) { 2896 if (count == *iosize) { 2897 freemsg(mp); 2898 return (error); 2899 } else { 2900 *iosize -= count; 2901 *mpp = mp; 2902 return (0); 2903 } 2904 } 2905 } 2906 dp = bp->b_datap; 2907 dp->db_cpid = curproc->p_pid; 2908 ASSERT(wroff <= dp->db_lim - bp->b_wptr); 2909 bp->b_wptr = bp->b_rptr = bp->b_rptr + wroff; 2910 2911 if (flag & STRUIO_POSTPONE) { 2912 /* 2913 * Setup the stream uio portion of the 2914 * dblk for subsequent use by struioget(). 2915 */ 2916 dp->db_struioflag = STRUIO_SPEC; 2917 dp->db_cksumstart = 0; 2918 dp->db_cksumstuff = 0; 2919 dp->db_cksumend = size; 2920 *(long long *)dp->db_struioun.data = 0ll; 2921 bp->b_wptr += size; 2922 } else { 2923 if (stp->sd_copyflag & STRCOPYCACHED) 2924 uiop->uio_extflg |= UIO_COPY_CACHED; 2925 2926 if (size != 0) { 2927 error = uiomove(bp->b_wptr, size, UIO_WRITE, 2928 uiop); 2929 if (error != 0) { 2930 freeb(bp); 2931 freemsg(mp); 2932 return (error); 2933 } 2934 } 2935 bp->b_wptr += size; 2936 2937 if (stp->sd_wputdatafunc != NULL) { 2938 mblk_t *newbp; 2939 2940 newbp = (stp->sd_wputdatafunc)(stp->sd_vnode, 2941 bp, NULL, NULL, NULL, NULL); 2942 if (newbp == NULL) { 2943 freeb(bp); 2944 freemsg(mp); 2945 return (ECOMM); 2946 } 2947 bp = newbp; 2948 } 2949 } 2950 2951 count -= size; 2952 2953 if (mp == NULL) 2954 mp = bp; 2955 else 2956 linkb(mp, bp); 2957 } while (count > 0); 2958 2959 *mpp = mp; 2960 return (0); 2961 } 2962 2963 /* 2964 * Wait for a buffer to become available. Return non-zero errno 2965 * if not able to wait, 0 if buffer is probably there. 2966 */ 2967 int 2968 strwaitbuf(size_t size, int pri) 2969 { 2970 bufcall_id_t id; 2971 2972 mutex_enter(&bcall_monitor); 2973 if ((id = bufcall(size, pri, (void (*)(void *))cv_broadcast, 2974 &ttoproc(curthread)->p_flag_cv)) == 0) { 2975 mutex_exit(&bcall_monitor); 2976 return (ENOSR); 2977 } 2978 if (!cv_wait_sig(&(ttoproc(curthread)->p_flag_cv), &bcall_monitor)) { 2979 unbufcall(id); 2980 mutex_exit(&bcall_monitor); 2981 return (EINTR); 2982 } 2983 unbufcall(id); 2984 mutex_exit(&bcall_monitor); 2985 return (0); 2986 } 2987 2988 /* 2989 * This function waits for a read or write event to happen on a stream. 2990 * fmode can specify FNDELAY and/or FNONBLOCK. 2991 * The timeout is in ms with -1 meaning infinite. 2992 * The flag values work as follows: 2993 * READWAIT Check for read side errors, send M_READ 2994 * GETWAIT Check for read side errors, no M_READ 2995 * WRITEWAIT Check for write side errors. 2996 * NOINTR Do not return error if nonblocking or timeout. 2997 * STR_NOERROR Ignore all errors except STPLEX. 2998 * STR_NOSIG Ignore/hold signals during the duration of the call. 2999 * STR_PEEK Pass through the strgeterr(). 3000 */ 3001 int 3002 strwaitq(stdata_t *stp, int flag, ssize_t count, int fmode, clock_t timout, 3003 int *done) 3004 { 3005 int slpflg, errs; 3006 int error; 3007 kcondvar_t *sleepon; 3008 mblk_t *mp; 3009 ssize_t *rd_count; 3010 clock_t rval; 3011 3012 ASSERT(MUTEX_HELD(&stp->sd_lock)); 3013 if ((flag & READWAIT) || (flag & GETWAIT)) { 3014 slpflg = RSLEEP; 3015 sleepon = &_RD(stp->sd_wrq)->q_wait; 3016 errs = STRDERR|STPLEX; 3017 } else { 3018 slpflg = WSLEEP; 3019 sleepon = &stp->sd_wrq->q_wait; 3020 errs = STWRERR|STRHUP|STPLEX; 3021 } 3022 if (flag & STR_NOERROR) 3023 errs = STPLEX; 3024 3025 if (stp->sd_wakeq & slpflg) { 3026 /* 3027 * A strwakeq() is pending, no need to sleep. 3028 */ 3029 stp->sd_wakeq &= ~slpflg; 3030 *done = 0; 3031 return (0); 3032 } 3033 3034 if (fmode & (FNDELAY|FNONBLOCK)) { 3035 if (!(flag & NOINTR)) 3036 error = EAGAIN; 3037 else 3038 error = 0; 3039 *done = 1; 3040 return (error); 3041 } 3042 3043 if (stp->sd_flag & errs) { 3044 /* 3045 * Check for errors before going to sleep since the 3046 * caller might not have checked this while holding 3047 * sd_lock. 3048 */ 3049 error = strgeterr(stp, errs, (flag & STR_PEEK)); 3050 if (error != 0) { 3051 *done = 1; 3052 return (error); 3053 } 3054 } 3055 3056 /* 3057 * If any module downstream has requested read notification 3058 * by setting SNDMREAD flag using M_SETOPTS, send a message 3059 * down stream. 3060 */ 3061 if ((flag & READWAIT) && (stp->sd_flag & SNDMREAD)) { 3062 mutex_exit(&stp->sd_lock); 3063 if (!(mp = allocb_wait(sizeof (ssize_t), BPRI_MED, 3064 (flag & STR_NOSIG), &error))) { 3065 mutex_enter(&stp->sd_lock); 3066 *done = 1; 3067 return (error); 3068 } 3069 mp->b_datap->db_type = M_READ; 3070 rd_count = (ssize_t *)mp->b_wptr; 3071 *rd_count = count; 3072 mp->b_wptr += sizeof (ssize_t); 3073 /* 3074 * Send the number of bytes requested by the 3075 * read as the argument to M_READ. 3076 */ 3077 stream_willservice(stp); 3078 putnext(stp->sd_wrq, mp); 3079 stream_runservice(stp); 3080 mutex_enter(&stp->sd_lock); 3081 3082 /* 3083 * If any data arrived due to inline processing 3084 * of putnext(), don't sleep. 3085 */ 3086 if (_RD(stp->sd_wrq)->q_first != NULL) { 3087 *done = 0; 3088 return (0); 3089 } 3090 } 3091 3092 stp->sd_flag |= slpflg; 3093 TRACE_5(TR_FAC_STREAMS_FR, TR_STRWAITQ_WAIT2, 3094 "strwaitq sleeps (2):%p, %X, %lX, %X, %p", 3095 stp, flag, count, fmode, done); 3096 3097 rval = str_cv_wait(sleepon, &stp->sd_lock, timout, flag & STR_NOSIG); 3098 if (rval > 0) { 3099 /* EMPTY */ 3100 TRACE_5(TR_FAC_STREAMS_FR, TR_STRWAITQ_WAKE2, 3101 "strwaitq awakes(2):%X, %X, %X, %X, %X", 3102 stp, flag, count, fmode, done); 3103 } else if (rval == 0) { 3104 TRACE_5(TR_FAC_STREAMS_FR, TR_STRWAITQ_INTR2, 3105 "strwaitq interrupt #2:%p, %X, %lX, %X, %p", 3106 stp, flag, count, fmode, done); 3107 stp->sd_flag &= ~slpflg; 3108 cv_broadcast(sleepon); 3109 if (!(flag & NOINTR)) 3110 error = EINTR; 3111 else 3112 error = 0; 3113 *done = 1; 3114 return (error); 3115 } else { 3116 /* timeout */ 3117 TRACE_5(TR_FAC_STREAMS_FR, TR_STRWAITQ_TIME, 3118 "strwaitq timeout:%p, %X, %lX, %X, %p", 3119 stp, flag, count, fmode, done); 3120 *done = 1; 3121 if (!(flag & NOINTR)) 3122 return (ETIME); 3123 else 3124 return (0); 3125 } 3126 /* 3127 * If the caller implements delayed errors (i.e. queued after data) 3128 * we can not check for errors here since data as well as an 3129 * error might have arrived at the stream head. We return to 3130 * have the caller check the read queue before checking for errors. 3131 */ 3132 if ((stp->sd_flag & errs) && !(flag & STR_DELAYERR)) { 3133 error = strgeterr(stp, errs, (flag & STR_PEEK)); 3134 if (error != 0) { 3135 *done = 1; 3136 return (error); 3137 } 3138 } 3139 *done = 0; 3140 return (0); 3141 } 3142 3143 /* 3144 * Perform job control discipline access checks. 3145 * Return 0 for success and the errno for failure. 3146 */ 3147 3148 #define cantsend(p, t, sig) \ 3149 (sigismember(&(p)->p_ignore, sig) || signal_is_blocked((t), sig)) 3150 3151 int 3152 straccess(struct stdata *stp, enum jcaccess mode) 3153 { 3154 extern kcondvar_t lbolt_cv; /* XXX: should be in a header file */ 3155 kthread_t *t = curthread; 3156 proc_t *p = ttoproc(t); 3157 sess_t *sp; 3158 3159 ASSERT(mutex_owned(&stp->sd_lock)); 3160 3161 if (stp->sd_sidp == NULL || stp->sd_vnode->v_type == VFIFO) 3162 return (0); 3163 3164 mutex_enter(&p->p_lock); /* protects p_pgidp */ 3165 3166 for (;;) { 3167 mutex_enter(&p->p_splock); /* protects p->p_sessp */ 3168 sp = p->p_sessp; 3169 mutex_enter(&sp->s_lock); /* protects sp->* */ 3170 3171 /* 3172 * If this is not the calling process's controlling terminal 3173 * or if the calling process is already in the foreground 3174 * then allow access. 3175 */ 3176 if (sp->s_dev != stp->sd_vnode->v_rdev || 3177 p->p_pgidp == stp->sd_pgidp) { 3178 mutex_exit(&sp->s_lock); 3179 mutex_exit(&p->p_splock); 3180 mutex_exit(&p->p_lock); 3181 return (0); 3182 } 3183 3184 /* 3185 * Check to see if controlling terminal has been deallocated. 3186 */ 3187 if (sp->s_vp == NULL) { 3188 if (!cantsend(p, t, SIGHUP)) 3189 sigtoproc(p, t, SIGHUP); 3190 mutex_exit(&sp->s_lock); 3191 mutex_exit(&p->p_splock); 3192 mutex_exit(&p->p_lock); 3193 return (EIO); 3194 } 3195 3196 mutex_exit(&sp->s_lock); 3197 mutex_exit(&p->p_splock); 3198 3199 if (mode == JCGETP) { 3200 mutex_exit(&p->p_lock); 3201 return (0); 3202 } 3203 3204 if (mode == JCREAD) { 3205 if (p->p_detached || cantsend(p, t, SIGTTIN)) { 3206 mutex_exit(&p->p_lock); 3207 return (EIO); 3208 } 3209 mutex_exit(&p->p_lock); 3210 mutex_exit(&stp->sd_lock); 3211 pgsignal(p->p_pgidp, SIGTTIN); 3212 mutex_enter(&stp->sd_lock); 3213 mutex_enter(&p->p_lock); 3214 } else { /* mode == JCWRITE or JCSETP */ 3215 if ((mode == JCWRITE && !(stp->sd_flag & STRTOSTOP)) || 3216 cantsend(p, t, SIGTTOU)) { 3217 mutex_exit(&p->p_lock); 3218 return (0); 3219 } 3220 if (p->p_detached) { 3221 mutex_exit(&p->p_lock); 3222 return (EIO); 3223 } 3224 mutex_exit(&p->p_lock); 3225 mutex_exit(&stp->sd_lock); 3226 pgsignal(p->p_pgidp, SIGTTOU); 3227 mutex_enter(&stp->sd_lock); 3228 mutex_enter(&p->p_lock); 3229 } 3230 3231 /* 3232 * We call cv_wait_sig_swap() to cause the appropriate 3233 * action for the jobcontrol signal to take place. 3234 * If the signal is being caught, we will take the 3235 * EINTR error return. Otherwise, the default action 3236 * of causing the process to stop will take place. 3237 * In this case, we rely on the periodic cv_broadcast() on 3238 * &lbolt_cv to wake us up to loop around and test again. 3239 * We can't get here if the signal is ignored or 3240 * if the current thread is blocking the signal. 3241 */ 3242 mutex_exit(&stp->sd_lock); 3243 if (!cv_wait_sig_swap(&lbolt_cv, &p->p_lock)) { 3244 mutex_exit(&p->p_lock); 3245 mutex_enter(&stp->sd_lock); 3246 return (EINTR); 3247 } 3248 mutex_exit(&p->p_lock); 3249 mutex_enter(&stp->sd_lock); 3250 mutex_enter(&p->p_lock); 3251 } 3252 } 3253 3254 /* 3255 * Return size of message of block type (bp->b_datap->db_type) 3256 */ 3257 size_t 3258 xmsgsize(mblk_t *bp) 3259 { 3260 unsigned char type; 3261 size_t count = 0; 3262 3263 type = bp->b_datap->db_type; 3264 3265 for (; bp; bp = bp->b_cont) { 3266 if (type != bp->b_datap->db_type) 3267 break; 3268 ASSERT(bp->b_wptr >= bp->b_rptr); 3269 count += bp->b_wptr - bp->b_rptr; 3270 } 3271 return (count); 3272 } 3273 3274 /* 3275 * Allocate a stream head. 3276 */ 3277 struct stdata * 3278 shalloc(queue_t *qp) 3279 { 3280 stdata_t *stp; 3281 3282 stp = kmem_cache_alloc(stream_head_cache, KM_SLEEP); 3283 3284 stp->sd_wrq = _WR(qp); 3285 stp->sd_strtab = NULL; 3286 stp->sd_iocid = 0; 3287 stp->sd_mate = NULL; 3288 stp->sd_freezer = NULL; 3289 stp->sd_refcnt = 0; 3290 stp->sd_wakeq = 0; 3291 stp->sd_anchor = 0; 3292 stp->sd_struiowrq = NULL; 3293 stp->sd_struiordq = NULL; 3294 stp->sd_struiodnak = 0; 3295 stp->sd_struionak = NULL; 3296 stp->sd_t_audit_data = NULL; 3297 stp->sd_rput_opt = 0; 3298 stp->sd_wput_opt = 0; 3299 stp->sd_read_opt = 0; 3300 stp->sd_rprotofunc = strrput_proto; 3301 stp->sd_rmiscfunc = strrput_misc; 3302 stp->sd_rderrfunc = stp->sd_wrerrfunc = NULL; 3303 stp->sd_rputdatafunc = stp->sd_wputdatafunc = NULL; 3304 stp->sd_ciputctrl = NULL; 3305 stp->sd_nciputctrl = 0; 3306 stp->sd_qhead = NULL; 3307 stp->sd_qtail = NULL; 3308 stp->sd_servid = NULL; 3309 stp->sd_nqueues = 0; 3310 stp->sd_svcflags = 0; 3311 stp->sd_copyflag = 0; 3312 3313 return (stp); 3314 } 3315 3316 /* 3317 * Free a stream head. 3318 */ 3319 void 3320 shfree(stdata_t *stp) 3321 { 3322 ASSERT(MUTEX_NOT_HELD(&stp->sd_lock)); 3323 3324 stp->sd_wrq = NULL; 3325 3326 mutex_enter(&stp->sd_qlock); 3327 while (stp->sd_svcflags & STRS_SCHEDULED) { 3328 STRSTAT(strwaits); 3329 cv_wait(&stp->sd_qcv, &stp->sd_qlock); 3330 } 3331 mutex_exit(&stp->sd_qlock); 3332 3333 if (stp->sd_ciputctrl != NULL) { 3334 ASSERT(stp->sd_nciputctrl == n_ciputctrl - 1); 3335 SUMCHECK_CIPUTCTRL_COUNTS(stp->sd_ciputctrl, 3336 stp->sd_nciputctrl, 0); 3337 ASSERT(ciputctrl_cache != NULL); 3338 kmem_cache_free(ciputctrl_cache, stp->sd_ciputctrl); 3339 stp->sd_ciputctrl = NULL; 3340 stp->sd_nciputctrl = 0; 3341 } 3342 ASSERT(stp->sd_qhead == NULL); 3343 ASSERT(stp->sd_qtail == NULL); 3344 ASSERT(stp->sd_nqueues == 0); 3345 kmem_cache_free(stream_head_cache, stp); 3346 } 3347 3348 /* 3349 * Allocate a pair of queues and a syncq for the pair 3350 */ 3351 queue_t * 3352 allocq(void) 3353 { 3354 queinfo_t *qip; 3355 queue_t *qp, *wqp; 3356 syncq_t *sq; 3357 3358 qip = kmem_cache_alloc(queue_cache, KM_SLEEP); 3359 3360 qp = &qip->qu_rqueue; 3361 wqp = &qip->qu_wqueue; 3362 sq = &qip->qu_syncq; 3363 3364 qp->q_last = NULL; 3365 qp->q_next = NULL; 3366 qp->q_ptr = NULL; 3367 qp->q_flag = QUSE | QREADR; 3368 qp->q_bandp = NULL; 3369 qp->q_stream = NULL; 3370 qp->q_syncq = sq; 3371 qp->q_nband = 0; 3372 qp->q_nfsrv = NULL; 3373 qp->q_draining = 0; 3374 qp->q_syncqmsgs = 0; 3375 qp->q_spri = 0; 3376 qp->q_qtstamp = 0; 3377 qp->q_sqtstamp = 0; 3378 qp->q_fp = NULL; 3379 3380 wqp->q_last = NULL; 3381 wqp->q_next = NULL; 3382 wqp->q_ptr = NULL; 3383 wqp->q_flag = QUSE; 3384 wqp->q_bandp = NULL; 3385 wqp->q_stream = NULL; 3386 wqp->q_syncq = sq; 3387 wqp->q_nband = 0; 3388 wqp->q_nfsrv = NULL; 3389 wqp->q_draining = 0; 3390 wqp->q_syncqmsgs = 0; 3391 wqp->q_qtstamp = 0; 3392 wqp->q_sqtstamp = 0; 3393 wqp->q_spri = 0; 3394 3395 sq->sq_count = 0; 3396 sq->sq_rmqcount = 0; 3397 sq->sq_flags = 0; 3398 sq->sq_type = 0; 3399 sq->sq_callbflags = 0; 3400 sq->sq_cancelid = 0; 3401 sq->sq_ciputctrl = NULL; 3402 sq->sq_nciputctrl = 0; 3403 sq->sq_needexcl = 0; 3404 sq->sq_svcflags = 0; 3405 3406 return (qp); 3407 } 3408 3409 /* 3410 * Free a pair of queues and the "attached" syncq. 3411 * Discard any messages left on the syncq(s), remove the syncq(s) from the 3412 * outer perimeter, and free the syncq(s) if they are not the "attached" syncq. 3413 */ 3414 void 3415 freeq(queue_t *qp) 3416 { 3417 qband_t *qbp, *nqbp; 3418 syncq_t *sq, *outer; 3419 queue_t *wqp = _WR(qp); 3420 3421 ASSERT(qp->q_flag & QREADR); 3422 3423 /* 3424 * If a previously dispatched taskq job is scheduled to run 3425 * sync_service() or a service routine is scheduled for the 3426 * queues about to be freed, wait here until all service is 3427 * done on the queue and all associated queues and syncqs. 3428 */ 3429 wait_svc(qp); 3430 3431 (void) flush_syncq(qp->q_syncq, qp); 3432 (void) flush_syncq(wqp->q_syncq, wqp); 3433 ASSERT(qp->q_syncqmsgs == 0 && wqp->q_syncqmsgs == 0); 3434 3435 /* 3436 * Flush the queues before q_next is set to NULL This is needed 3437 * in order to backenable any downstream queue before we go away. 3438 * Note: we are already removed from the stream so that the 3439 * backenabling will not cause any messages to be delivered to our 3440 * put procedures. 3441 */ 3442 flushq(qp, FLUSHALL); 3443 flushq(wqp, FLUSHALL); 3444 3445 /* Tidy up - removeq only does a half-remove from stream */ 3446 qp->q_next = wqp->q_next = NULL; 3447 ASSERT(!(qp->q_flag & QENAB)); 3448 ASSERT(!(wqp->q_flag & QENAB)); 3449 3450 outer = qp->q_syncq->sq_outer; 3451 if (outer != NULL) { 3452 outer_remove(outer, qp->q_syncq); 3453 if (wqp->q_syncq != qp->q_syncq) 3454 outer_remove(outer, wqp->q_syncq); 3455 } 3456 /* 3457 * Free any syncqs that are outside what allocq returned. 3458 */ 3459 if (qp->q_syncq != SQ(qp) && !(qp->q_flag & QPERMOD)) 3460 free_syncq(qp->q_syncq); 3461 if (qp->q_syncq != wqp->q_syncq && wqp->q_syncq != SQ(qp)) 3462 free_syncq(wqp->q_syncq); 3463 3464 ASSERT((qp->q_sqflags & (Q_SQQUEUED | Q_SQDRAINING)) == 0); 3465 ASSERT((wqp->q_sqflags & (Q_SQQUEUED | Q_SQDRAINING)) == 0); 3466 ASSERT(MUTEX_NOT_HELD(QLOCK(qp))); 3467 ASSERT(MUTEX_NOT_HELD(QLOCK(wqp))); 3468 sq = SQ(qp); 3469 ASSERT(MUTEX_NOT_HELD(SQLOCK(sq))); 3470 ASSERT(sq->sq_head == NULL && sq->sq_tail == NULL); 3471 ASSERT(sq->sq_outer == NULL); 3472 ASSERT(sq->sq_onext == NULL && sq->sq_oprev == NULL); 3473 ASSERT(sq->sq_callbpend == NULL); 3474 ASSERT(sq->sq_needexcl == 0); 3475 3476 if (sq->sq_ciputctrl != NULL) { 3477 ASSERT(sq->sq_nciputctrl == n_ciputctrl - 1); 3478 SUMCHECK_CIPUTCTRL_COUNTS(sq->sq_ciputctrl, 3479 sq->sq_nciputctrl, 0); 3480 ASSERT(ciputctrl_cache != NULL); 3481 kmem_cache_free(ciputctrl_cache, sq->sq_ciputctrl); 3482 sq->sq_ciputctrl = NULL; 3483 sq->sq_nciputctrl = 0; 3484 } 3485 3486 ASSERT(qp->q_first == NULL && wqp->q_first == NULL); 3487 ASSERT(qp->q_count == 0 && wqp->q_count == 0); 3488 ASSERT(qp->q_mblkcnt == 0 && wqp->q_mblkcnt == 0); 3489 3490 qp->q_flag &= ~QUSE; 3491 wqp->q_flag &= ~QUSE; 3492 3493 /* NOTE: Uncomment the assert below once bugid 1159635 is fixed. */ 3494 /* ASSERT((qp->q_flag & QWANTW) == 0 && (wqp->q_flag & QWANTW) == 0); */ 3495 3496 qbp = qp->q_bandp; 3497 while (qbp) { 3498 nqbp = qbp->qb_next; 3499 freeband(qbp); 3500 qbp = nqbp; 3501 } 3502 qbp = wqp->q_bandp; 3503 while (qbp) { 3504 nqbp = qbp->qb_next; 3505 freeband(qbp); 3506 qbp = nqbp; 3507 } 3508 kmem_cache_free(queue_cache, qp); 3509 } 3510 3511 /* 3512 * Allocate a qband structure. 3513 */ 3514 qband_t * 3515 allocband(void) 3516 { 3517 qband_t *qbp; 3518 3519 qbp = kmem_cache_alloc(qband_cache, KM_NOSLEEP); 3520 if (qbp == NULL) 3521 return (NULL); 3522 3523 qbp->qb_next = NULL; 3524 qbp->qb_count = 0; 3525 qbp->qb_mblkcnt = 0; 3526 qbp->qb_first = NULL; 3527 qbp->qb_last = NULL; 3528 qbp->qb_flag = 0; 3529 3530 return (qbp); 3531 } 3532 3533 /* 3534 * Free a qband structure. 3535 */ 3536 void 3537 freeband(qband_t *qbp) 3538 { 3539 kmem_cache_free(qband_cache, qbp); 3540 } 3541 3542 /* 3543 * Just like putnextctl(9F), except that allocb_wait() is used. 3544 * 3545 * Consolidation Private, and of course only callable from the stream head or 3546 * routines that may block. 3547 */ 3548 int 3549 putnextctl_wait(queue_t *q, int type) 3550 { 3551 mblk_t *bp; 3552 int error; 3553 3554 if ((datamsg(type) && (type != M_DELAY)) || 3555 (bp = allocb_wait(0, BPRI_HI, 0, &error)) == NULL) 3556 return (0); 3557 3558 bp->b_datap->db_type = (unsigned char)type; 3559 putnext(q, bp); 3560 return (1); 3561 } 3562 3563 /* 3564 * run any possible bufcalls. 3565 */ 3566 void 3567 runbufcalls(void) 3568 { 3569 strbufcall_t *bcp; 3570 3571 mutex_enter(&bcall_monitor); 3572 mutex_enter(&strbcall_lock); 3573 3574 if (strbcalls.bc_head) { 3575 size_t count; 3576 int nevent; 3577 3578 /* 3579 * count how many events are on the list 3580 * now so we can check to avoid looping 3581 * in low memory situations 3582 */ 3583 nevent = 0; 3584 for (bcp = strbcalls.bc_head; bcp; bcp = bcp->bc_next) 3585 nevent++; 3586 3587 /* 3588 * get estimate of available memory from kmem_avail(). 3589 * awake all bufcall functions waiting for 3590 * memory whose request could be satisfied 3591 * by 'count' memory and let 'em fight for it. 3592 */ 3593 count = kmem_avail(); 3594 while ((bcp = strbcalls.bc_head) != NULL && nevent) { 3595 STRSTAT(bufcalls); 3596 --nevent; 3597 if (bcp->bc_size <= count) { 3598 bcp->bc_executor = curthread; 3599 mutex_exit(&strbcall_lock); 3600 (*bcp->bc_func)(bcp->bc_arg); 3601 mutex_enter(&strbcall_lock); 3602 bcp->bc_executor = NULL; 3603 cv_broadcast(&bcall_cv); 3604 strbcalls.bc_head = bcp->bc_next; 3605 kmem_free(bcp, sizeof (strbufcall_t)); 3606 } else { 3607 /* 3608 * too big, try again later - note 3609 * that nevent was decremented above 3610 * so we won't retry this one on this 3611 * iteration of the loop 3612 */ 3613 if (bcp->bc_next != NULL) { 3614 strbcalls.bc_head = bcp->bc_next; 3615 bcp->bc_next = NULL; 3616 strbcalls.bc_tail->bc_next = bcp; 3617 strbcalls.bc_tail = bcp; 3618 } 3619 } 3620 } 3621 if (strbcalls.bc_head == NULL) 3622 strbcalls.bc_tail = NULL; 3623 } 3624 3625 mutex_exit(&strbcall_lock); 3626 mutex_exit(&bcall_monitor); 3627 } 3628 3629 3630 /* 3631 * actually run queue's service routine. 3632 */ 3633 static void 3634 runservice(queue_t *q) 3635 { 3636 qband_t *qbp; 3637 3638 ASSERT(q->q_qinfo->qi_srvp); 3639 again: 3640 entersq(q->q_syncq, SQ_SVC); 3641 TRACE_1(TR_FAC_STREAMS_FR, TR_QRUNSERVICE_START, 3642 "runservice starts:%p", q); 3643 3644 if (!(q->q_flag & QWCLOSE)) 3645 (*q->q_qinfo->qi_srvp)(q); 3646 3647 TRACE_1(TR_FAC_STREAMS_FR, TR_QRUNSERVICE_END, 3648 "runservice ends:(%p)", q); 3649 3650 leavesq(q->q_syncq, SQ_SVC); 3651 3652 mutex_enter(QLOCK(q)); 3653 if (q->q_flag & QENAB) { 3654 q->q_flag &= ~QENAB; 3655 mutex_exit(QLOCK(q)); 3656 goto again; 3657 } 3658 q->q_flag &= ~QINSERVICE; 3659 q->q_flag &= ~QBACK; 3660 for (qbp = q->q_bandp; qbp; qbp = qbp->qb_next) 3661 qbp->qb_flag &= ~QB_BACK; 3662 /* 3663 * Wakeup thread waiting for the service procedure 3664 * to be run (strclose and qdetach). 3665 */ 3666 cv_broadcast(&q->q_wait); 3667 3668 mutex_exit(QLOCK(q)); 3669 } 3670 3671 /* 3672 * Background processing of bufcalls. 3673 */ 3674 void 3675 streams_bufcall_service(void) 3676 { 3677 callb_cpr_t cprinfo; 3678 3679 CALLB_CPR_INIT(&cprinfo, &strbcall_lock, callb_generic_cpr, 3680 "streams_bufcall_service"); 3681 3682 mutex_enter(&strbcall_lock); 3683 3684 for (;;) { 3685 if (strbcalls.bc_head != NULL && kmem_avail() > 0) { 3686 mutex_exit(&strbcall_lock); 3687 runbufcalls(); 3688 mutex_enter(&strbcall_lock); 3689 } 3690 if (strbcalls.bc_head != NULL) { 3691 clock_t wt, tick; 3692 3693 STRSTAT(bcwaits); 3694 /* Wait for memory to become available */ 3695 CALLB_CPR_SAFE_BEGIN(&cprinfo); 3696 tick = SEC_TO_TICK(60); 3697 time_to_wait(&wt, tick); 3698 (void) cv_timedwait(&memavail_cv, &strbcall_lock, wt); 3699 CALLB_CPR_SAFE_END(&cprinfo, &strbcall_lock); 3700 } 3701 3702 /* Wait for new work to arrive */ 3703 if (strbcalls.bc_head == NULL) { 3704 CALLB_CPR_SAFE_BEGIN(&cprinfo); 3705 cv_wait(&strbcall_cv, &strbcall_lock); 3706 CALLB_CPR_SAFE_END(&cprinfo, &strbcall_lock); 3707 } 3708 } 3709 } 3710 3711 /* 3712 * Background processing of streams background tasks which failed 3713 * taskq_dispatch. 3714 */ 3715 static void 3716 streams_qbkgrnd_service(void) 3717 { 3718 callb_cpr_t cprinfo; 3719 queue_t *q; 3720 3721 CALLB_CPR_INIT(&cprinfo, &service_queue, callb_generic_cpr, 3722 "streams_bkgrnd_service"); 3723 3724 mutex_enter(&service_queue); 3725 3726 for (;;) { 3727 /* 3728 * Wait for work to arrive. 3729 */ 3730 while ((freebs_list == NULL) && (qhead == NULL)) { 3731 CALLB_CPR_SAFE_BEGIN(&cprinfo); 3732 cv_wait(&services_to_run, &service_queue); 3733 CALLB_CPR_SAFE_END(&cprinfo, &service_queue); 3734 } 3735 /* 3736 * Handle all pending freebs requests to free memory. 3737 */ 3738 while (freebs_list != NULL) { 3739 mblk_t *mp = freebs_list; 3740 freebs_list = mp->b_next; 3741 mutex_exit(&service_queue); 3742 mblk_free(mp); 3743 mutex_enter(&service_queue); 3744 } 3745 /* 3746 * Run pending queues. 3747 */ 3748 while (qhead != NULL) { 3749 DQ(q, qhead, qtail, q_link); 3750 ASSERT(q != NULL); 3751 mutex_exit(&service_queue); 3752 queue_service(q); 3753 mutex_enter(&service_queue); 3754 } 3755 ASSERT(qhead == NULL && qtail == NULL); 3756 } 3757 } 3758 3759 /* 3760 * Background processing of streams background tasks which failed 3761 * taskq_dispatch. 3762 */ 3763 static void 3764 streams_sqbkgrnd_service(void) 3765 { 3766 callb_cpr_t cprinfo; 3767 syncq_t *sq; 3768 3769 CALLB_CPR_INIT(&cprinfo, &service_queue, callb_generic_cpr, 3770 "streams_sqbkgrnd_service"); 3771 3772 mutex_enter(&service_queue); 3773 3774 for (;;) { 3775 /* 3776 * Wait for work to arrive. 3777 */ 3778 while (sqhead == NULL) { 3779 CALLB_CPR_SAFE_BEGIN(&cprinfo); 3780 cv_wait(&syncqs_to_run, &service_queue); 3781 CALLB_CPR_SAFE_END(&cprinfo, &service_queue); 3782 } 3783 3784 /* 3785 * Run pending syncqs. 3786 */ 3787 while (sqhead != NULL) { 3788 DQ(sq, sqhead, sqtail, sq_next); 3789 ASSERT(sq != NULL); 3790 ASSERT(sq->sq_svcflags & SQ_BGTHREAD); 3791 mutex_exit(&service_queue); 3792 syncq_service(sq); 3793 mutex_enter(&service_queue); 3794 } 3795 } 3796 } 3797 3798 /* 3799 * Disable the syncq and wait for background syncq processing to complete. 3800 * If the syncq is placed on the sqhead/sqtail queue, try to remove it from the 3801 * list. 3802 */ 3803 void 3804 wait_sq_svc(syncq_t *sq) 3805 { 3806 mutex_enter(SQLOCK(sq)); 3807 sq->sq_svcflags |= SQ_DISABLED; 3808 if (sq->sq_svcflags & SQ_BGTHREAD) { 3809 syncq_t *sq_chase; 3810 syncq_t *sq_curr; 3811 int removed; 3812 3813 ASSERT(sq->sq_servcount == 1); 3814 mutex_enter(&service_queue); 3815 RMQ(sq, sqhead, sqtail, sq_next, sq_chase, sq_curr, removed); 3816 mutex_exit(&service_queue); 3817 if (removed) { 3818 sq->sq_svcflags &= ~SQ_BGTHREAD; 3819 sq->sq_servcount = 0; 3820 STRSTAT(sqremoved); 3821 goto done; 3822 } 3823 } 3824 while (sq->sq_servcount != 0) { 3825 sq->sq_flags |= SQ_WANTWAKEUP; 3826 cv_wait(&sq->sq_wait, SQLOCK(sq)); 3827 } 3828 done: 3829 mutex_exit(SQLOCK(sq)); 3830 } 3831 3832 /* 3833 * Put a syncq on the list of syncq's to be serviced by the sqthread. 3834 * Add the argument to the end of the sqhead list and set the flag 3835 * indicating this syncq has been enabled. If it has already been 3836 * enabled, don't do anything. 3837 * This routine assumes that SQLOCK is held. 3838 * NOTE that the lock order is to have the SQLOCK first, 3839 * so if the service_syncq lock is held, we need to release it 3840 * before aquiring the SQLOCK (mostly relevant for the background 3841 * thread, and this seems to be common among the STREAMS global locks). 3842 * Note the the sq_svcflags are protected by the SQLOCK. 3843 */ 3844 void 3845 sqenable(syncq_t *sq) 3846 { 3847 /* 3848 * This is probably not important except for where I believe it 3849 * is being called. At that point, it should be held (and it 3850 * is a pain to release it just for this routine, so don't do 3851 * it). 3852 */ 3853 ASSERT(MUTEX_HELD(SQLOCK(sq))); 3854 3855 IMPLY(sq->sq_servcount == 0, sq->sq_next == NULL); 3856 IMPLY(sq->sq_next != NULL, sq->sq_svcflags & SQ_BGTHREAD); 3857 3858 /* 3859 * Do not put on list if background thread is scheduled or 3860 * syncq is disabled. 3861 */ 3862 if (sq->sq_svcflags & (SQ_DISABLED | SQ_BGTHREAD)) 3863 return; 3864 3865 /* 3866 * Check whether we should enable sq at all. 3867 * Non PERMOD syncqs may be drained by at most one thread. 3868 * PERMOD syncqs may be drained by several threads but we limit the 3869 * total amount to the lesser of 3870 * Number of queues on the squeue and 3871 * Number of CPUs. 3872 */ 3873 if (sq->sq_servcount != 0) { 3874 if (((sq->sq_type & SQ_PERMOD) == 0) || 3875 (sq->sq_servcount >= MIN(sq->sq_nqueues, ncpus_online))) { 3876 STRSTAT(sqtoomany); 3877 return; 3878 } 3879 } 3880 3881 sq->sq_tstamp = lbolt; 3882 STRSTAT(sqenables); 3883 3884 /* Attempt a taskq dispatch */ 3885 sq->sq_servid = (void *)taskq_dispatch(streams_taskq, 3886 (task_func_t *)syncq_service, sq, TQ_NOSLEEP | TQ_NOQUEUE); 3887 if (sq->sq_servid != NULL) { 3888 sq->sq_servcount++; 3889 return; 3890 } 3891 3892 /* 3893 * This taskq dispatch failed, but a previous one may have succeeded. 3894 * Don't try to schedule on the background thread whilst there is 3895 * outstanding taskq processing. 3896 */ 3897 if (sq->sq_servcount != 0) 3898 return; 3899 3900 /* 3901 * System is low on resources and can't perform a non-sleeping 3902 * dispatch. Schedule the syncq for a background thread and mark the 3903 * syncq to avoid any further taskq dispatch attempts. 3904 */ 3905 mutex_enter(&service_queue); 3906 STRSTAT(taskqfails); 3907 ENQUEUE(sq, sqhead, sqtail, sq_next); 3908 sq->sq_svcflags |= SQ_BGTHREAD; 3909 sq->sq_servcount = 1; 3910 cv_signal(&syncqs_to_run); 3911 mutex_exit(&service_queue); 3912 } 3913 3914 /* 3915 * Note: fifo_close() depends on the mblk_t on the queue being freed 3916 * asynchronously. The asynchronous freeing of messages breaks the 3917 * recursive call chain of fifo_close() while there are I_SENDFD type of 3918 * messages refering other file pointers on the queue. Then when 3919 * closing pipes it can avoid stack overflow in case of daisy-chained 3920 * pipes, and also avoid deadlock in case of fifonode_t pairs (which 3921 * share the same fifolock_t). 3922 */ 3923 3924 void 3925 freebs_enqueue(mblk_t *mp, dblk_t *dbp) 3926 { 3927 esb_queue_t *eqp = &system_esbq; 3928 3929 ASSERT(dbp->db_mblk == mp); 3930 3931 /* 3932 * Check data sanity. The dblock should have non-empty free function. 3933 * It is better to panic here then later when the dblock is freed 3934 * asynchronously when the context is lost. 3935 */ 3936 if (dbp->db_frtnp->free_func == NULL) { 3937 panic("freebs_enqueue: dblock %p has a NULL free callback", 3938 (void *)dbp); 3939 } 3940 3941 mutex_enter(&eqp->eq_lock); 3942 /* queue the new mblk on the esballoc queue */ 3943 if (eqp->eq_head == NULL) { 3944 eqp->eq_head = eqp->eq_tail = mp; 3945 } else { 3946 eqp->eq_tail->b_next = mp; 3947 eqp->eq_tail = mp; 3948 } 3949 eqp->eq_len++; 3950 3951 /* If we're the first thread to reach the threshold, process */ 3952 if (eqp->eq_len >= esbq_max_qlen && 3953 !(eqp->eq_flags & ESBQ_PROCESSING)) 3954 esballoc_process_queue(eqp); 3955 3956 esballoc_set_timer(eqp, esbq_timeout); 3957 mutex_exit(&eqp->eq_lock); 3958 } 3959 3960 static void 3961 esballoc_process_queue(esb_queue_t *eqp) 3962 { 3963 mblk_t *mp; 3964 3965 ASSERT(MUTEX_HELD(&eqp->eq_lock)); 3966 3967 eqp->eq_flags |= ESBQ_PROCESSING; 3968 3969 do { 3970 /* 3971 * Detach the message chain for processing. 3972 */ 3973 mp = eqp->eq_head; 3974 eqp->eq_tail->b_next = NULL; 3975 eqp->eq_head = eqp->eq_tail = NULL; 3976 eqp->eq_len = 0; 3977 mutex_exit(&eqp->eq_lock); 3978 3979 /* 3980 * Process the message chain. 3981 */ 3982 esballoc_enqueue_mblk(mp); 3983 mutex_enter(&eqp->eq_lock); 3984 } while ((eqp->eq_len >= esbq_max_qlen) && (eqp->eq_len > 0)); 3985 3986 eqp->eq_flags &= ~ESBQ_PROCESSING; 3987 } 3988 3989 /* 3990 * taskq callback routine to free esballoced mblk's 3991 */ 3992 static void 3993 esballoc_mblk_free(mblk_t *mp) 3994 { 3995 mblk_t *nextmp; 3996 3997 for (; mp != NULL; mp = nextmp) { 3998 nextmp = mp->b_next; 3999 mp->b_next = NULL; 4000 mblk_free(mp); 4001 } 4002 } 4003 4004 static void 4005 esballoc_enqueue_mblk(mblk_t *mp) 4006 { 4007 4008 if (taskq_dispatch(system_taskq, (task_func_t *)esballoc_mblk_free, mp, 4009 TQ_NOSLEEP) == NULL) { 4010 mblk_t *first_mp = mp; 4011 /* 4012 * System is low on resources and can't perform a non-sleeping 4013 * dispatch. Schedule for a background thread. 4014 */ 4015 mutex_enter(&service_queue); 4016 STRSTAT(taskqfails); 4017 4018 while (mp->b_next != NULL) 4019 mp = mp->b_next; 4020 4021 mp->b_next = freebs_list; 4022 freebs_list = first_mp; 4023 cv_signal(&services_to_run); 4024 mutex_exit(&service_queue); 4025 } 4026 } 4027 4028 static void 4029 esballoc_timer(void *arg) 4030 { 4031 esb_queue_t *eqp = arg; 4032 4033 mutex_enter(&eqp->eq_lock); 4034 eqp->eq_flags &= ~ESBQ_TIMER; 4035 4036 if (!(eqp->eq_flags & ESBQ_PROCESSING) && 4037 eqp->eq_len > 0) 4038 esballoc_process_queue(eqp); 4039 4040 esballoc_set_timer(eqp, esbq_timeout); 4041 mutex_exit(&eqp->eq_lock); 4042 } 4043 4044 static void 4045 esballoc_set_timer(esb_queue_t *eqp, clock_t eq_timeout) 4046 { 4047 ASSERT(MUTEX_HELD(&eqp->eq_lock)); 4048 4049 if (eqp->eq_len > 0 && !(eqp->eq_flags & ESBQ_TIMER)) { 4050 (void) timeout(esballoc_timer, eqp, eq_timeout); 4051 eqp->eq_flags |= ESBQ_TIMER; 4052 } 4053 } 4054 4055 void 4056 esballoc_queue_init(void) 4057 { 4058 system_esbq.eq_len = 0; 4059 system_esbq.eq_head = system_esbq.eq_tail = NULL; 4060 system_esbq.eq_flags = 0; 4061 } 4062 4063 /* 4064 * Set the QBACK or QB_BACK flag in the given queue for 4065 * the given priority band. 4066 */ 4067 void 4068 setqback(queue_t *q, unsigned char pri) 4069 { 4070 int i; 4071 qband_t *qbp; 4072 qband_t **qbpp; 4073 4074 ASSERT(MUTEX_HELD(QLOCK(q))); 4075 if (pri != 0) { 4076 if (pri > q->q_nband) { 4077 qbpp = &q->q_bandp; 4078 while (*qbpp) 4079 qbpp = &(*qbpp)->qb_next; 4080 while (pri > q->q_nband) { 4081 if ((*qbpp = allocband()) == NULL) { 4082 cmn_err(CE_WARN, 4083 "setqback: can't allocate qband\n"); 4084 return; 4085 } 4086 (*qbpp)->qb_hiwat = q->q_hiwat; 4087 (*qbpp)->qb_lowat = q->q_lowat; 4088 q->q_nband++; 4089 qbpp = &(*qbpp)->qb_next; 4090 } 4091 } 4092 qbp = q->q_bandp; 4093 i = pri; 4094 while (--i) 4095 qbp = qbp->qb_next; 4096 qbp->qb_flag |= QB_BACK; 4097 } else { 4098 q->q_flag |= QBACK; 4099 } 4100 } 4101 4102 int 4103 strcopyin(void *from, void *to, size_t len, int copyflag) 4104 { 4105 if (copyflag & U_TO_K) { 4106 ASSERT((copyflag & K_TO_K) == 0); 4107 if (copyin(from, to, len)) 4108 return (EFAULT); 4109 } else { 4110 ASSERT(copyflag & K_TO_K); 4111 bcopy(from, to, len); 4112 } 4113 return (0); 4114 } 4115 4116 int 4117 strcopyout(void *from, void *to, size_t len, int copyflag) 4118 { 4119 if (copyflag & U_TO_K) { 4120 if (copyout(from, to, len)) 4121 return (EFAULT); 4122 } else { 4123 ASSERT(copyflag & K_TO_K); 4124 bcopy(from, to, len); 4125 } 4126 return (0); 4127 } 4128 4129 /* 4130 * strsignal_nolock() posts a signal to the process(es) at the stream head. 4131 * It assumes that the stream head lock is already held, whereas strsignal() 4132 * acquires the lock first. This routine was created because a few callers 4133 * release the stream head lock before calling only to re-acquire it after 4134 * it returns. 4135 */ 4136 void 4137 strsignal_nolock(stdata_t *stp, int sig, int32_t band) 4138 { 4139 ASSERT(MUTEX_HELD(&stp->sd_lock)); 4140 switch (sig) { 4141 case SIGPOLL: 4142 if (stp->sd_sigflags & S_MSG) 4143 strsendsig(stp->sd_siglist, S_MSG, (uchar_t)band, 0); 4144 break; 4145 4146 default: 4147 if (stp->sd_pgidp) { 4148 pgsignal(stp->sd_pgidp, sig); 4149 } 4150 break; 4151 } 4152 } 4153 4154 void 4155 strsignal(stdata_t *stp, int sig, int32_t band) 4156 { 4157 TRACE_3(TR_FAC_STREAMS_FR, TR_SENDSIG, 4158 "strsignal:%p, %X, %X", stp, sig, band); 4159 4160 mutex_enter(&stp->sd_lock); 4161 switch (sig) { 4162 case SIGPOLL: 4163 if (stp->sd_sigflags & S_MSG) 4164 strsendsig(stp->sd_siglist, S_MSG, (uchar_t)band, 0); 4165 break; 4166 4167 default: 4168 if (stp->sd_pgidp) { 4169 pgsignal(stp->sd_pgidp, sig); 4170 } 4171 break; 4172 } 4173 mutex_exit(&stp->sd_lock); 4174 } 4175 4176 void 4177 strhup(stdata_t *stp) 4178 { 4179 ASSERT(mutex_owned(&stp->sd_lock)); 4180 pollwakeup(&stp->sd_pollist, POLLHUP); 4181 if (stp->sd_sigflags & S_HANGUP) 4182 strsendsig(stp->sd_siglist, S_HANGUP, 0, 0); 4183 } 4184 4185 /* 4186 * Backenable the first queue upstream from `q' with a service procedure. 4187 */ 4188 void 4189 backenable(queue_t *q, uchar_t pri) 4190 { 4191 queue_t *nq; 4192 4193 /* 4194 * our presence might not prevent other modules in our own 4195 * stream from popping/pushing since the caller of getq might not 4196 * have a claim on the queue (some drivers do a getq on somebody 4197 * else's queue - they know that the queue itself is not going away 4198 * but the framework has to guarantee q_next in that stream.) 4199 */ 4200 claimstr(q); 4201 4202 /* find nearest back queue with service proc */ 4203 for (nq = backq(q); nq && !nq->q_qinfo->qi_srvp; nq = backq(nq)) { 4204 ASSERT(STRMATED(q->q_stream) || STREAM(q) == STREAM(nq)); 4205 } 4206 4207 if (nq) { 4208 kthread_t *freezer; 4209 /* 4210 * backenable can be called either with no locks held 4211 * or with the stream frozen (the latter occurs when a module 4212 * calls rmvq with the stream frozen.) If the stream is frozen 4213 * by the caller the caller will hold all qlocks in the stream. 4214 * Note that a frozen stream doesn't freeze a mated stream, 4215 * so we explicitly check for that. 4216 */ 4217 freezer = STREAM(q)->sd_freezer; 4218 if (freezer != curthread || STREAM(q) != STREAM(nq)) { 4219 mutex_enter(QLOCK(nq)); 4220 } 4221 #ifdef DEBUG 4222 else { 4223 ASSERT(frozenstr(q)); 4224 ASSERT(MUTEX_HELD(QLOCK(q))); 4225 ASSERT(MUTEX_HELD(QLOCK(nq))); 4226 } 4227 #endif 4228 setqback(nq, pri); 4229 qenable_locked(nq); 4230 if (freezer != curthread || STREAM(q) != STREAM(nq)) 4231 mutex_exit(QLOCK(nq)); 4232 } 4233 releasestr(q); 4234 } 4235 4236 /* 4237 * Return the appropriate errno when one of flags_to_check is set 4238 * in sd_flags. Uses the exported error routines if they are set. 4239 * Will return 0 if non error is set (or if the exported error routines 4240 * do not return an error). 4241 * 4242 * If there is both a read and write error to check we prefer the read error. 4243 * Also, give preference to recorded errno's over the error functions. 4244 * The flags that are handled are: 4245 * STPLEX return EINVAL 4246 * STRDERR return sd_rerror (and clear if STRDERRNONPERSIST) 4247 * STWRERR return sd_werror (and clear if STWRERRNONPERSIST) 4248 * STRHUP return sd_werror 4249 * 4250 * If the caller indicates that the operation is a peek a nonpersistent error 4251 * is not cleared. 4252 */ 4253 int 4254 strgeterr(stdata_t *stp, int32_t flags_to_check, int ispeek) 4255 { 4256 int32_t sd_flag = stp->sd_flag & flags_to_check; 4257 int error = 0; 4258 4259 ASSERT(MUTEX_HELD(&stp->sd_lock)); 4260 ASSERT((flags_to_check & ~(STRDERR|STWRERR|STRHUP|STPLEX)) == 0); 4261 if (sd_flag & STPLEX) 4262 error = EINVAL; 4263 else if (sd_flag & STRDERR) { 4264 error = stp->sd_rerror; 4265 if ((stp->sd_flag & STRDERRNONPERSIST) && !ispeek) { 4266 /* 4267 * Read errors are non-persistent i.e. discarded once 4268 * returned to a non-peeking caller, 4269 */ 4270 stp->sd_rerror = 0; 4271 stp->sd_flag &= ~STRDERR; 4272 } 4273 if (error == 0 && stp->sd_rderrfunc != NULL) { 4274 int clearerr = 0; 4275 4276 error = (*stp->sd_rderrfunc)(stp->sd_vnode, ispeek, 4277 &clearerr); 4278 if (clearerr) { 4279 stp->sd_flag &= ~STRDERR; 4280 stp->sd_rderrfunc = NULL; 4281 } 4282 } 4283 } else if (sd_flag & STWRERR) { 4284 error = stp->sd_werror; 4285 if ((stp->sd_flag & STWRERRNONPERSIST) && !ispeek) { 4286 /* 4287 * Write errors are non-persistent i.e. discarded once 4288 * returned to a non-peeking caller, 4289 */ 4290 stp->sd_werror = 0; 4291 stp->sd_flag &= ~STWRERR; 4292 } 4293 if (error == 0 && stp->sd_wrerrfunc != NULL) { 4294 int clearerr = 0; 4295 4296 error = (*stp->sd_wrerrfunc)(stp->sd_vnode, ispeek, 4297 &clearerr); 4298 if (clearerr) { 4299 stp->sd_flag &= ~STWRERR; 4300 stp->sd_wrerrfunc = NULL; 4301 } 4302 } 4303 } else if (sd_flag & STRHUP) { 4304 /* sd_werror set when STRHUP */ 4305 error = stp->sd_werror; 4306 } 4307 return (error); 4308 } 4309 4310 4311 /* 4312 * single-thread open/close/push/pop 4313 * for twisted streams also 4314 */ 4315 int 4316 strstartplumb(stdata_t *stp, int flag, int cmd) 4317 { 4318 int waited = 1; 4319 int error = 0; 4320 4321 if (STRMATED(stp)) { 4322 struct stdata *stmatep = stp->sd_mate; 4323 4324 STRLOCKMATES(stp); 4325 while (waited) { 4326 waited = 0; 4327 while (stmatep->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) { 4328 if ((cmd == I_POP) && 4329 (flag & (FNDELAY|FNONBLOCK))) { 4330 STRUNLOCKMATES(stp); 4331 return (EAGAIN); 4332 } 4333 waited = 1; 4334 mutex_exit(&stp->sd_lock); 4335 if (!cv_wait_sig(&stmatep->sd_monitor, 4336 &stmatep->sd_lock)) { 4337 mutex_exit(&stmatep->sd_lock); 4338 return (EINTR); 4339 } 4340 mutex_exit(&stmatep->sd_lock); 4341 STRLOCKMATES(stp); 4342 } 4343 while (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) { 4344 if ((cmd == I_POP) && 4345 (flag & (FNDELAY|FNONBLOCK))) { 4346 STRUNLOCKMATES(stp); 4347 return (EAGAIN); 4348 } 4349 waited = 1; 4350 mutex_exit(&stmatep->sd_lock); 4351 if (!cv_wait_sig(&stp->sd_monitor, 4352 &stp->sd_lock)) { 4353 mutex_exit(&stp->sd_lock); 4354 return (EINTR); 4355 } 4356 mutex_exit(&stp->sd_lock); 4357 STRLOCKMATES(stp); 4358 } 4359 if (stp->sd_flag & (STRDERR|STWRERR|STRHUP|STPLEX)) { 4360 error = strgeterr(stp, 4361 STRDERR|STWRERR|STRHUP|STPLEX, 0); 4362 if (error != 0) { 4363 STRUNLOCKMATES(stp); 4364 return (error); 4365 } 4366 } 4367 } 4368 stp->sd_flag |= STRPLUMB; 4369 STRUNLOCKMATES(stp); 4370 } else { 4371 mutex_enter(&stp->sd_lock); 4372 while (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) { 4373 if (((cmd == I_POP) || (cmd == _I_REMOVE)) && 4374 (flag & (FNDELAY|FNONBLOCK))) { 4375 mutex_exit(&stp->sd_lock); 4376 return (EAGAIN); 4377 } 4378 if (!cv_wait_sig(&stp->sd_monitor, &stp->sd_lock)) { 4379 mutex_exit(&stp->sd_lock); 4380 return (EINTR); 4381 } 4382 if (stp->sd_flag & (STRDERR|STWRERR|STRHUP|STPLEX)) { 4383 error = strgeterr(stp, 4384 STRDERR|STWRERR|STRHUP|STPLEX, 0); 4385 if (error != 0) { 4386 mutex_exit(&stp->sd_lock); 4387 return (error); 4388 } 4389 } 4390 } 4391 stp->sd_flag |= STRPLUMB; 4392 mutex_exit(&stp->sd_lock); 4393 } 4394 return (0); 4395 } 4396 4397 /* 4398 * Complete the plumbing operation associated with stream `stp'. 4399 */ 4400 void 4401 strendplumb(stdata_t *stp) 4402 { 4403 ASSERT(MUTEX_HELD(&stp->sd_lock)); 4404 ASSERT(stp->sd_flag & STRPLUMB); 4405 stp->sd_flag &= ~STRPLUMB; 4406 cv_broadcast(&stp->sd_monitor); 4407 } 4408 4409 /* 4410 * This describes how the STREAMS framework handles synchronization 4411 * during open/push and close/pop. 4412 * The key interfaces for open and close are qprocson and qprocsoff, 4413 * respectively. While the close case in general is harder both open 4414 * have close have significant similarities. 4415 * 4416 * During close the STREAMS framework has to both ensure that there 4417 * are no stale references to the queue pair (and syncq) that 4418 * are being closed and also provide the guarantees that are documented 4419 * in qprocsoff(9F). 4420 * If there are stale references to the queue that is closing it can 4421 * result in kernel memory corruption or kernel panics. 4422 * 4423 * Note that is it up to the module/driver to ensure that it itself 4424 * does not have any stale references to the closing queues once its close 4425 * routine returns. This includes: 4426 * - Cancelling any timeout/bufcall/qtimeout/qbufcall callback routines 4427 * associated with the queues. For timeout and bufcall callbacks the 4428 * module/driver also has to ensure (or wait for) any callbacks that 4429 * are in progress. 4430 * - If the module/driver is using esballoc it has to ensure that any 4431 * esballoc free functions do not refer to a queue that has closed. 4432 * (Note that in general the close routine can not wait for the esballoc'ed 4433 * messages to be freed since that can cause a deadlock.) 4434 * - Cancelling any interrupts that refer to the closing queues and 4435 * also ensuring that there are no interrupts in progress that will 4436 * refer to the closing queues once the close routine returns. 4437 * - For multiplexors removing any driver global state that refers to 4438 * the closing queue and also ensuring that there are no threads in 4439 * the multiplexor that has picked up a queue pointer but not yet 4440 * finished using it. 4441 * 4442 * In addition, a driver/module can only reference the q_next pointer 4443 * in its open, close, put, or service procedures or in a 4444 * qtimeout/qbufcall callback procedure executing "on" the correct 4445 * stream. Thus it can not reference the q_next pointer in an interrupt 4446 * routine or a timeout, bufcall or esballoc callback routine. Likewise 4447 * it can not reference q_next of a different queue e.g. in a mux that 4448 * passes messages from one queues put/service procedure to another queue. 4449 * In all the cases when the driver/module can not access the q_next 4450 * field it must use the *next* versions e.g. canputnext instead of 4451 * canput(q->q_next) and putnextctl instead of putctl(q->q_next, ...). 4452 * 4453 * 4454 * Assuming that the driver/module conforms to the above constraints 4455 * the STREAMS framework has to avoid stale references to q_next for all 4456 * the framework internal cases which include (but are not limited to): 4457 * - Threads in canput/canputnext/backenable and elsewhere that are 4458 * walking q_next. 4459 * - Messages on a syncq that have a reference to the queue through b_queue. 4460 * - Messages on an outer perimeter (syncq) that have a reference to the 4461 * queue through b_queue. 4462 * - Threads that use q_nfsrv (e.g. canput) to find a queue. 4463 * Note that only canput and bcanput use q_nfsrv without any locking. 4464 * 4465 * The STREAMS framework providing the qprocsoff(9F) guarantees means that 4466 * after qprocsoff returns, the framework has to ensure that no threads can 4467 * enter the put or service routines for the closing read or write-side queue. 4468 * In addition to preventing "direct" entry into the put procedures 4469 * the framework also has to prevent messages being drained from 4470 * the syncq or the outer perimeter. 4471 * XXX Note that currently qdetach does relies on D_MTOCEXCL as the only 4472 * mechanism to prevent qwriter(PERIM_OUTER) from running after 4473 * qprocsoff has returned. 4474 * Note that if a module/driver uses put(9F) on one of its own queues 4475 * it is up to the module/driver to ensure that the put() doesn't 4476 * get called when the queue is closing. 4477 * 4478 * 4479 * The framework aspects of the above "contract" is implemented by 4480 * qprocsoff, removeq, and strlock: 4481 * - qprocsoff (disable_svc) sets QWCLOSE to prevent runservice from 4482 * entering the service procedures. 4483 * - strlock acquires the sd_lock and sd_reflock to prevent putnext, 4484 * canputnext, backenable etc from dereferencing the q_next that will 4485 * soon change. 4486 * - strlock waits for sd_refcnt to be zero to wait for e.g. any canputnext 4487 * or other q_next walker that uses claimstr/releasestr to finish. 4488 * - optionally for every syncq in the stream strlock acquires all the 4489 * sq_lock's and waits for all sq_counts to drop to a value that indicates 4490 * that no thread executes in the put or service procedures and that no 4491 * thread is draining into the module/driver. This ensures that no 4492 * open, close, put, service, or qtimeout/qbufcall callback procedure is 4493 * currently executing hence no such thread can end up with the old stale 4494 * q_next value and no canput/backenable can have the old stale 4495 * q_nfsrv/q_next. 4496 * - qdetach (wait_svc) makes sure that any scheduled or running threads 4497 * have either finished or observed the QWCLOSE flag and gone away. 4498 */ 4499 4500 4501 /* 4502 * Get all the locks necessary to change q_next. 4503 * 4504 * Wait for sd_refcnt to reach 0 and, if sqlist is present, wait for the 4505 * sq_count of each syncq in the list to drop to sq_rmqcount, indicating that 4506 * the only threads inside the sqncq are threads currently calling removeq(). 4507 * Since threads calling removeq() are in the process of removing their queues 4508 * from the stream, we do not need to worry about them accessing a stale q_next 4509 * pointer and thus we do not need to wait for them to exit (in fact, waiting 4510 * for them can cause deadlock). 4511 * 4512 * This routine is subject to starvation since it does not set any flag to 4513 * prevent threads from entering a module in the stream(i.e. sq_count can 4514 * increase on some syncq while it is waiting on some other syncq.) 4515 * 4516 * Assumes that only one thread attempts to call strlock for a given 4517 * stream. If this is not the case the two threads would deadlock. 4518 * This assumption is guaranteed since strlock is only called by insertq 4519 * and removeq and streams plumbing changes are single-threaded for 4520 * a given stream using the STWOPEN, STRCLOSE, and STRPLUMB flags. 4521 * 4522 * For pipes, it is not difficult to atomically designate a pair of streams 4523 * to be mated. Once mated atomically by the framework the twisted pair remain 4524 * configured that way until dismantled atomically by the framework. 4525 * When plumbing takes place on a twisted stream it is necessary to ensure that 4526 * this operation is done exclusively on the twisted stream since two such 4527 * operations, each initiated on different ends of the pipe will deadlock 4528 * waiting for each other to complete. 4529 * 4530 * On entry, no locks should be held. 4531 * The locks acquired and held by strlock depends on a few factors. 4532 * - If sqlist is non-NULL all the syncq locks in the sqlist will be acquired 4533 * and held on exit and all sq_count are at an acceptable level. 4534 * - In all cases, sd_lock and sd_reflock are acquired and held on exit with 4535 * sd_refcnt being zero. 4536 */ 4537 4538 static void 4539 strlock(struct stdata *stp, sqlist_t *sqlist) 4540 { 4541 syncql_t *sql, *sql2; 4542 retry: 4543 /* 4544 * Wait for any claimstr to go away. 4545 */ 4546 if (STRMATED(stp)) { 4547 struct stdata *stp1, *stp2; 4548 4549 STRLOCKMATES(stp); 4550 /* 4551 * Note that the selection of locking order is not 4552 * important, just that they are always aquired in 4553 * the same order. To assure this, we choose this 4554 * order based on the value of the pointer, and since 4555 * the pointer will not change for the life of this 4556 * pair, we will always grab the locks in the same 4557 * order (and hence, prevent deadlocks). 4558 */ 4559 if (&(stp->sd_lock) > &((stp->sd_mate)->sd_lock)) { 4560 stp1 = stp; 4561 stp2 = stp->sd_mate; 4562 } else { 4563 stp2 = stp; 4564 stp1 = stp->sd_mate; 4565 } 4566 mutex_enter(&stp1->sd_reflock); 4567 if (stp1->sd_refcnt > 0) { 4568 STRUNLOCKMATES(stp); 4569 cv_wait(&stp1->sd_refmonitor, &stp1->sd_reflock); 4570 mutex_exit(&stp1->sd_reflock); 4571 goto retry; 4572 } 4573 mutex_enter(&stp2->sd_reflock); 4574 if (stp2->sd_refcnt > 0) { 4575 STRUNLOCKMATES(stp); 4576 mutex_exit(&stp1->sd_reflock); 4577 cv_wait(&stp2->sd_refmonitor, &stp2->sd_reflock); 4578 mutex_exit(&stp2->sd_reflock); 4579 goto retry; 4580 } 4581 STREAM_PUTLOCKS_ENTER(stp1); 4582 STREAM_PUTLOCKS_ENTER(stp2); 4583 } else { 4584 mutex_enter(&stp->sd_lock); 4585 mutex_enter(&stp->sd_reflock); 4586 while (stp->sd_refcnt > 0) { 4587 mutex_exit(&stp->sd_lock); 4588 cv_wait(&stp->sd_refmonitor, &stp->sd_reflock); 4589 if (mutex_tryenter(&stp->sd_lock) == 0) { 4590 mutex_exit(&stp->sd_reflock); 4591 mutex_enter(&stp->sd_lock); 4592 mutex_enter(&stp->sd_reflock); 4593 } 4594 } 4595 STREAM_PUTLOCKS_ENTER(stp); 4596 } 4597 4598 if (sqlist == NULL) 4599 return; 4600 4601 for (sql = sqlist->sqlist_head; sql; sql = sql->sql_next) { 4602 syncq_t *sq = sql->sql_sq; 4603 uint16_t count; 4604 4605 mutex_enter(SQLOCK(sq)); 4606 count = sq->sq_count; 4607 ASSERT(sq->sq_rmqcount <= count); 4608 SQ_PUTLOCKS_ENTER(sq); 4609 SUM_SQ_PUTCOUNTS(sq, count); 4610 if (count == sq->sq_rmqcount) 4611 continue; 4612 4613 /* Failed - drop all locks that we have acquired so far */ 4614 if (STRMATED(stp)) { 4615 STREAM_PUTLOCKS_EXIT(stp); 4616 STREAM_PUTLOCKS_EXIT(stp->sd_mate); 4617 STRUNLOCKMATES(stp); 4618 mutex_exit(&stp->sd_reflock); 4619 mutex_exit(&stp->sd_mate->sd_reflock); 4620 } else { 4621 STREAM_PUTLOCKS_EXIT(stp); 4622 mutex_exit(&stp->sd_lock); 4623 mutex_exit(&stp->sd_reflock); 4624 } 4625 for (sql2 = sqlist->sqlist_head; sql2 != sql; 4626 sql2 = sql2->sql_next) { 4627 SQ_PUTLOCKS_EXIT(sql2->sql_sq); 4628 mutex_exit(SQLOCK(sql2->sql_sq)); 4629 } 4630 4631 /* 4632 * The wait loop below may starve when there are many threads 4633 * claiming the syncq. This is especially a problem with permod 4634 * syncqs (IP). To lessen the impact of the problem we increment 4635 * sq_needexcl and clear fastbits so that putnexts will slow 4636 * down and call sqenable instead of draining right away. 4637 */ 4638 sq->sq_needexcl++; 4639 SQ_PUTCOUNT_CLRFAST_LOCKED(sq); 4640 while (count > sq->sq_rmqcount) { 4641 sq->sq_flags |= SQ_WANTWAKEUP; 4642 SQ_PUTLOCKS_EXIT(sq); 4643 cv_wait(&sq->sq_wait, SQLOCK(sq)); 4644 count = sq->sq_count; 4645 SQ_PUTLOCKS_ENTER(sq); 4646 SUM_SQ_PUTCOUNTS(sq, count); 4647 } 4648 sq->sq_needexcl--; 4649 if (sq->sq_needexcl == 0) 4650 SQ_PUTCOUNT_SETFAST_LOCKED(sq); 4651 SQ_PUTLOCKS_EXIT(sq); 4652 ASSERT(count == sq->sq_rmqcount); 4653 mutex_exit(SQLOCK(sq)); 4654 goto retry; 4655 } 4656 } 4657 4658 /* 4659 * Drop all the locks that strlock acquired. 4660 */ 4661 static void 4662 strunlock(struct stdata *stp, sqlist_t *sqlist) 4663 { 4664 syncql_t *sql; 4665 4666 if (STRMATED(stp)) { 4667 STREAM_PUTLOCKS_EXIT(stp); 4668 STREAM_PUTLOCKS_EXIT(stp->sd_mate); 4669 STRUNLOCKMATES(stp); 4670 mutex_exit(&stp->sd_reflock); 4671 mutex_exit(&stp->sd_mate->sd_reflock); 4672 } else { 4673 STREAM_PUTLOCKS_EXIT(stp); 4674 mutex_exit(&stp->sd_lock); 4675 mutex_exit(&stp->sd_reflock); 4676 } 4677 4678 if (sqlist == NULL) 4679 return; 4680 4681 for (sql = sqlist->sqlist_head; sql; sql = sql->sql_next) { 4682 SQ_PUTLOCKS_EXIT(sql->sql_sq); 4683 mutex_exit(SQLOCK(sql->sql_sq)); 4684 } 4685 } 4686 4687 /* 4688 * When the module has service procedure, we need check if the next 4689 * module which has service procedure is in flow control to trigger 4690 * the backenable. 4691 */ 4692 static void 4693 backenable_insertedq(queue_t *q) 4694 { 4695 qband_t *qbp; 4696 4697 claimstr(q); 4698 if (q->q_qinfo->qi_srvp != NULL && q->q_next != NULL) { 4699 if (q->q_next->q_nfsrv->q_flag & QWANTW) 4700 backenable(q, 0); 4701 4702 qbp = q->q_next->q_nfsrv->q_bandp; 4703 for (; qbp != NULL; qbp = qbp->qb_next) 4704 if ((qbp->qb_flag & QB_WANTW) && qbp->qb_first != NULL) 4705 backenable(q, qbp->qb_first->b_band); 4706 } 4707 releasestr(q); 4708 } 4709 4710 /* 4711 * Given two read queues, insert a new single one after another. 4712 * 4713 * This routine acquires all the necessary locks in order to change 4714 * q_next and related pointer using strlock(). 4715 * It depends on the stream head ensuring that there are no concurrent 4716 * insertq or removeq on the same stream. The stream head ensures this 4717 * using the flags STWOPEN, STRCLOSE, and STRPLUMB. 4718 * 4719 * Note that no syncq locks are held during the q_next change. This is 4720 * applied to all streams since, unlike removeq, there is no problem of stale 4721 * pointers when adding a module to the stream. Thus drivers/modules that do a 4722 * canput(rq->q_next) would never get a closed/freed queue pointer even if we 4723 * applied this optimization to all streams. 4724 */ 4725 void 4726 insertq(struct stdata *stp, queue_t *new) 4727 { 4728 queue_t *after; 4729 queue_t *wafter; 4730 queue_t *wnew = _WR(new); 4731 boolean_t have_fifo = B_FALSE; 4732 4733 if (new->q_flag & _QINSERTING) { 4734 ASSERT(stp->sd_vnode->v_type != VFIFO); 4735 after = new->q_next; 4736 wafter = _WR(new->q_next); 4737 } else { 4738 after = _RD(stp->sd_wrq); 4739 wafter = stp->sd_wrq; 4740 } 4741 4742 TRACE_2(TR_FAC_STREAMS_FR, TR_INSERTQ, 4743 "insertq:%p, %p", after, new); 4744 ASSERT(after->q_flag & QREADR); 4745 ASSERT(new->q_flag & QREADR); 4746 4747 strlock(stp, NULL); 4748 4749 /* Do we have a FIFO? */ 4750 if (wafter->q_next == after) { 4751 have_fifo = B_TRUE; 4752 wnew->q_next = new; 4753 } else { 4754 wnew->q_next = wafter->q_next; 4755 } 4756 new->q_next = after; 4757 4758 set_nfsrv_ptr(new, wnew, after, wafter); 4759 /* 4760 * set_nfsrv_ptr() needs to know if this is an insertion or not, 4761 * so only reset this flag after calling it. 4762 */ 4763 new->q_flag &= ~_QINSERTING; 4764 4765 if (have_fifo) { 4766 wafter->q_next = wnew; 4767 } else { 4768 if (wafter->q_next) 4769 _OTHERQ(wafter->q_next)->q_next = new; 4770 wafter->q_next = wnew; 4771 } 4772 4773 set_qend(new); 4774 /* The QEND flag might have to be updated for the upstream guy */ 4775 set_qend(after); 4776 4777 ASSERT(_SAMESTR(new) == O_SAMESTR(new)); 4778 ASSERT(_SAMESTR(wnew) == O_SAMESTR(wnew)); 4779 ASSERT(_SAMESTR(after) == O_SAMESTR(after)); 4780 ASSERT(_SAMESTR(wafter) == O_SAMESTR(wafter)); 4781 strsetuio(stp); 4782 4783 /* 4784 * If this was a module insertion, bump the push count. 4785 */ 4786 if (!(new->q_flag & QISDRV)) 4787 stp->sd_pushcnt++; 4788 4789 strunlock(stp, NULL); 4790 4791 /* check if the write Q needs backenable */ 4792 backenable_insertedq(wnew); 4793 4794 /* check if the read Q needs backenable */ 4795 backenable_insertedq(new); 4796 } 4797 4798 /* 4799 * Given a read queue, unlink it from any neighbors. 4800 * 4801 * This routine acquires all the necessary locks in order to 4802 * change q_next and related pointers and also guard against 4803 * stale references (e.g. through q_next) to the queue that 4804 * is being removed. It also plays part of the role in ensuring 4805 * that the module's/driver's put procedure doesn't get called 4806 * after qprocsoff returns. 4807 * 4808 * Removeq depends on the stream head ensuring that there are 4809 * no concurrent insertq or removeq on the same stream. The 4810 * stream head ensures this using the flags STWOPEN, STRCLOSE and 4811 * STRPLUMB. 4812 * 4813 * The set of locks needed to remove the queue is different in 4814 * different cases: 4815 * 4816 * Acquire sd_lock, sd_reflock, and all the syncq locks in the stream after 4817 * waiting for the syncq reference count to drop to 0 indicating that no 4818 * non-close threads are present anywhere in the stream. This ensures that any 4819 * module/driver can reference q_next in its open, close, put, or service 4820 * procedures. 4821 * 4822 * The sq_rmqcount counter tracks the number of threads inside removeq(). 4823 * strlock() ensures that there is either no threads executing inside perimeter 4824 * or there is only a thread calling qprocsoff(). 4825 * 4826 * strlock() compares the value of sq_count with the number of threads inside 4827 * removeq() and waits until sq_count is equal to sq_rmqcount. We need to wakeup 4828 * any threads waiting in strlock() when the sq_rmqcount increases. 4829 */ 4830 4831 void 4832 removeq(queue_t *qp) 4833 { 4834 queue_t *wqp = _WR(qp); 4835 struct stdata *stp = STREAM(qp); 4836 sqlist_t *sqlist = NULL; 4837 boolean_t isdriver; 4838 int moved; 4839 syncq_t *sq = qp->q_syncq; 4840 syncq_t *wsq = wqp->q_syncq; 4841 4842 ASSERT(stp); 4843 4844 TRACE_2(TR_FAC_STREAMS_FR, TR_REMOVEQ, 4845 "removeq:%p %p", qp, wqp); 4846 ASSERT(qp->q_flag&QREADR); 4847 4848 /* 4849 * For queues using Synchronous streams, we must wait for all threads in 4850 * rwnext() to drain out before proceeding. 4851 */ 4852 if (qp->q_flag & QSYNCSTR) { 4853 /* First, we need wakeup any threads blocked in rwnext() */ 4854 mutex_enter(SQLOCK(sq)); 4855 if (sq->sq_flags & SQ_WANTWAKEUP) { 4856 sq->sq_flags &= ~SQ_WANTWAKEUP; 4857 cv_broadcast(&sq->sq_wait); 4858 } 4859 mutex_exit(SQLOCK(sq)); 4860 4861 if (wsq != sq) { 4862 mutex_enter(SQLOCK(wsq)); 4863 if (wsq->sq_flags & SQ_WANTWAKEUP) { 4864 wsq->sq_flags &= ~SQ_WANTWAKEUP; 4865 cv_broadcast(&wsq->sq_wait); 4866 } 4867 mutex_exit(SQLOCK(wsq)); 4868 } 4869 4870 mutex_enter(QLOCK(qp)); 4871 while (qp->q_rwcnt > 0) { 4872 qp->q_flag |= QWANTRMQSYNC; 4873 cv_wait(&qp->q_wait, QLOCK(qp)); 4874 } 4875 mutex_exit(QLOCK(qp)); 4876 4877 mutex_enter(QLOCK(wqp)); 4878 while (wqp->q_rwcnt > 0) { 4879 wqp->q_flag |= QWANTRMQSYNC; 4880 cv_wait(&wqp->q_wait, QLOCK(wqp)); 4881 } 4882 mutex_exit(QLOCK(wqp)); 4883 } 4884 4885 mutex_enter(SQLOCK(sq)); 4886 sq->sq_rmqcount++; 4887 if (sq->sq_flags & SQ_WANTWAKEUP) { 4888 sq->sq_flags &= ~SQ_WANTWAKEUP; 4889 cv_broadcast(&sq->sq_wait); 4890 } 4891 mutex_exit(SQLOCK(sq)); 4892 4893 isdriver = (qp->q_flag & QISDRV); 4894 4895 sqlist = sqlist_build(qp, stp, STRMATED(stp)); 4896 strlock(stp, sqlist); 4897 4898 reset_nfsrv_ptr(qp, wqp); 4899 4900 ASSERT(wqp->q_next == NULL || backq(qp)->q_next == qp); 4901 ASSERT(qp->q_next == NULL || backq(wqp)->q_next == wqp); 4902 /* Do we have a FIFO? */ 4903 if (wqp->q_next == qp) { 4904 stp->sd_wrq->q_next = _RD(stp->sd_wrq); 4905 } else { 4906 if (wqp->q_next) 4907 backq(qp)->q_next = qp->q_next; 4908 if (qp->q_next) 4909 backq(wqp)->q_next = wqp->q_next; 4910 } 4911 4912 /* The QEND flag might have to be updated for the upstream guy */ 4913 if (qp->q_next) 4914 set_qend(qp->q_next); 4915 4916 ASSERT(_SAMESTR(stp->sd_wrq) == O_SAMESTR(stp->sd_wrq)); 4917 ASSERT(_SAMESTR(_RD(stp->sd_wrq)) == O_SAMESTR(_RD(stp->sd_wrq))); 4918 4919 /* 4920 * Move any messages destined for the put procedures to the next 4921 * syncq in line. Otherwise free them. 4922 */ 4923 moved = 0; 4924 /* 4925 * Quick check to see whether there are any messages or events. 4926 */ 4927 if (qp->q_syncqmsgs != 0 || (qp->q_syncq->sq_flags & SQ_EVENTS)) 4928 moved += propagate_syncq(qp); 4929 if (wqp->q_syncqmsgs != 0 || 4930 (wqp->q_syncq->sq_flags & SQ_EVENTS)) 4931 moved += propagate_syncq(wqp); 4932 4933 strsetuio(stp); 4934 4935 /* 4936 * If this was a module removal, decrement the push count. 4937 */ 4938 if (!isdriver) 4939 stp->sd_pushcnt--; 4940 4941 strunlock(stp, sqlist); 4942 sqlist_free(sqlist); 4943 4944 /* 4945 * Make sure any messages that were propagated are drained. 4946 * Also clear any QFULL bit caused by messages that were propagated. 4947 */ 4948 4949 if (qp->q_next != NULL) { 4950 clr_qfull(qp); 4951 /* 4952 * For the driver calling qprocsoff, propagate_syncq 4953 * frees all the messages instead of putting it in 4954 * the stream head 4955 */ 4956 if (!isdriver && (moved > 0)) 4957 emptysq(qp->q_next->q_syncq); 4958 } 4959 if (wqp->q_next != NULL) { 4960 clr_qfull(wqp); 4961 /* 4962 * We come here for any pop of a module except for the 4963 * case of driver being removed. We don't call emptysq 4964 * if we did not move any messages. This will avoid holding 4965 * PERMOD syncq locks in emptysq 4966 */ 4967 if (moved > 0) 4968 emptysq(wqp->q_next->q_syncq); 4969 } 4970 4971 mutex_enter(SQLOCK(sq)); 4972 sq->sq_rmqcount--; 4973 mutex_exit(SQLOCK(sq)); 4974 } 4975 4976 /* 4977 * Prevent further entry by setting a flag (like SQ_FROZEN, SQ_BLOCKED or 4978 * SQ_WRITER) on a syncq. 4979 * If maxcnt is not -1 it assumes that caller has "maxcnt" claim(s) on the 4980 * sync queue and waits until sq_count reaches maxcnt. 4981 * 4982 * if maxcnt is -1 there's no need to grab sq_putlocks since the caller 4983 * does not care about putnext threads that are in the middle of calling put 4984 * entry points. 4985 * 4986 * This routine is used for both inner and outer syncqs. 4987 */ 4988 static void 4989 blocksq(syncq_t *sq, ushort_t flag, int maxcnt) 4990 { 4991 uint16_t count = 0; 4992 4993 mutex_enter(SQLOCK(sq)); 4994 /* 4995 * Wait for SQ_FROZEN/SQ_BLOCKED to be reset. 4996 * SQ_FROZEN will be set if there is a frozen stream that has a 4997 * queue which also refers to this "shared" syncq. 4998 * SQ_BLOCKED will be set if there is "off" queue which also 4999 * refers to this "shared" syncq. 5000 */ 5001 if (maxcnt != -1) { 5002 count = sq->sq_count; 5003 SQ_PUTLOCKS_ENTER(sq); 5004 SQ_PUTCOUNT_CLRFAST_LOCKED(sq); 5005 SUM_SQ_PUTCOUNTS(sq, count); 5006 } 5007 sq->sq_needexcl++; 5008 ASSERT(sq->sq_needexcl != 0); /* wraparound */ 5009 5010 while ((sq->sq_flags & flag) || 5011 (maxcnt != -1 && count > (unsigned)maxcnt)) { 5012 sq->sq_flags |= SQ_WANTWAKEUP; 5013 if (maxcnt != -1) { 5014 SQ_PUTLOCKS_EXIT(sq); 5015 } 5016 cv_wait(&sq->sq_wait, SQLOCK(sq)); 5017 if (maxcnt != -1) { 5018 count = sq->sq_count; 5019 SQ_PUTLOCKS_ENTER(sq); 5020 SUM_SQ_PUTCOUNTS(sq, count); 5021 } 5022 } 5023 sq->sq_needexcl--; 5024 sq->sq_flags |= flag; 5025 ASSERT(maxcnt == -1 || count == maxcnt); 5026 if (maxcnt != -1) { 5027 if (sq->sq_needexcl == 0) { 5028 SQ_PUTCOUNT_SETFAST_LOCKED(sq); 5029 } 5030 SQ_PUTLOCKS_EXIT(sq); 5031 } else if (sq->sq_needexcl == 0) { 5032 SQ_PUTCOUNT_SETFAST(sq); 5033 } 5034 5035 mutex_exit(SQLOCK(sq)); 5036 } 5037 5038 /* 5039 * Reset a flag that was set with blocksq. 5040 * 5041 * Can not use this routine to reset SQ_WRITER. 5042 * 5043 * If "isouter" is set then the syncq is assumed to be an outer perimeter 5044 * and drain_syncq is not called. Instead we rely on the qwriter_outer thread 5045 * to handle the queued qwriter operations. 5046 * 5047 * no need to grab sq_putlocks here. See comment in strsubr.h that explains when 5048 * sq_putlocks are used. 5049 */ 5050 static void 5051 unblocksq(syncq_t *sq, uint16_t resetflag, int isouter) 5052 { 5053 uint16_t flags; 5054 5055 mutex_enter(SQLOCK(sq)); 5056 ASSERT(resetflag != SQ_WRITER); 5057 ASSERT(sq->sq_flags & resetflag); 5058 flags = sq->sq_flags & ~resetflag; 5059 sq->sq_flags = flags; 5060 if (flags & (SQ_QUEUED | SQ_WANTWAKEUP)) { 5061 if (flags & SQ_WANTWAKEUP) { 5062 flags &= ~SQ_WANTWAKEUP; 5063 cv_broadcast(&sq->sq_wait); 5064 } 5065 sq->sq_flags = flags; 5066 if ((flags & SQ_QUEUED) && !(flags & (SQ_STAYAWAY|SQ_EXCL))) { 5067 if (!isouter) { 5068 /* drain_syncq drops SQLOCK */ 5069 drain_syncq(sq); 5070 return; 5071 } 5072 } 5073 } 5074 mutex_exit(SQLOCK(sq)); 5075 } 5076 5077 /* 5078 * Reset a flag that was set with blocksq. 5079 * Does not drain the syncq. Use emptysq() for that. 5080 * Returns 1 if SQ_QUEUED is set. Otherwise 0. 5081 * 5082 * no need to grab sq_putlocks here. See comment in strsubr.h that explains when 5083 * sq_putlocks are used. 5084 */ 5085 static int 5086 dropsq(syncq_t *sq, uint16_t resetflag) 5087 { 5088 uint16_t flags; 5089 5090 mutex_enter(SQLOCK(sq)); 5091 ASSERT(sq->sq_flags & resetflag); 5092 flags = sq->sq_flags & ~resetflag; 5093 if (flags & SQ_WANTWAKEUP) { 5094 flags &= ~SQ_WANTWAKEUP; 5095 cv_broadcast(&sq->sq_wait); 5096 } 5097 sq->sq_flags = flags; 5098 mutex_exit(SQLOCK(sq)); 5099 if (flags & SQ_QUEUED) 5100 return (1); 5101 return (0); 5102 } 5103 5104 /* 5105 * Empty all the messages on a syncq. 5106 * 5107 * no need to grab sq_putlocks here. See comment in strsubr.h that explains when 5108 * sq_putlocks are used. 5109 */ 5110 static void 5111 emptysq(syncq_t *sq) 5112 { 5113 uint16_t flags; 5114 5115 mutex_enter(SQLOCK(sq)); 5116 flags = sq->sq_flags; 5117 if ((flags & SQ_QUEUED) && !(flags & (SQ_STAYAWAY|SQ_EXCL))) { 5118 /* 5119 * To prevent potential recursive invocation of drain_syncq we 5120 * do not call drain_syncq if count is non-zero. 5121 */ 5122 if (sq->sq_count == 0) { 5123 /* drain_syncq() drops SQLOCK */ 5124 drain_syncq(sq); 5125 return; 5126 } else 5127 sqenable(sq); 5128 } 5129 mutex_exit(SQLOCK(sq)); 5130 } 5131 5132 /* 5133 * Ordered insert while removing duplicates. 5134 */ 5135 static void 5136 sqlist_insert(sqlist_t *sqlist, syncq_t *sqp) 5137 { 5138 syncql_t *sqlp, **prev_sqlpp, *new_sqlp; 5139 5140 prev_sqlpp = &sqlist->sqlist_head; 5141 while ((sqlp = *prev_sqlpp) != NULL) { 5142 if (sqlp->sql_sq >= sqp) { 5143 if (sqlp->sql_sq == sqp) /* duplicate */ 5144 return; 5145 break; 5146 } 5147 prev_sqlpp = &sqlp->sql_next; 5148 } 5149 new_sqlp = &sqlist->sqlist_array[sqlist->sqlist_index++]; 5150 ASSERT((char *)new_sqlp < (char *)sqlist + sqlist->sqlist_size); 5151 new_sqlp->sql_next = sqlp; 5152 new_sqlp->sql_sq = sqp; 5153 *prev_sqlpp = new_sqlp; 5154 } 5155 5156 /* 5157 * Walk the write side queues until we hit either the driver 5158 * or a twist in the stream (_SAMESTR will return false in both 5159 * these cases) then turn around and walk the read side queues 5160 * back up to the stream head. 5161 */ 5162 static void 5163 sqlist_insertall(sqlist_t *sqlist, queue_t *q) 5164 { 5165 while (q != NULL) { 5166 sqlist_insert(sqlist, q->q_syncq); 5167 5168 if (_SAMESTR(q)) 5169 q = q->q_next; 5170 else if (!(q->q_flag & QREADR)) 5171 q = _RD(q); 5172 else 5173 q = NULL; 5174 } 5175 } 5176 5177 /* 5178 * Allocate and build a list of all syncqs in a stream and the syncq(s) 5179 * associated with the "q" parameter. The resulting list is sorted in a 5180 * canonical order and is free of duplicates. 5181 * Assumes the passed queue is a _RD(q). 5182 */ 5183 static sqlist_t * 5184 sqlist_build(queue_t *q, struct stdata *stp, boolean_t do_twist) 5185 { 5186 sqlist_t *sqlist = sqlist_alloc(stp, KM_SLEEP); 5187 5188 /* 5189 * start with the current queue/qpair 5190 */ 5191 ASSERT(q->q_flag & QREADR); 5192 5193 sqlist_insert(sqlist, q->q_syncq); 5194 sqlist_insert(sqlist, _WR(q)->q_syncq); 5195 5196 sqlist_insertall(sqlist, stp->sd_wrq); 5197 if (do_twist) 5198 sqlist_insertall(sqlist, stp->sd_mate->sd_wrq); 5199 5200 return (sqlist); 5201 } 5202 5203 static sqlist_t * 5204 sqlist_alloc(struct stdata *stp, int kmflag) 5205 { 5206 size_t sqlist_size; 5207 sqlist_t *sqlist; 5208 5209 /* 5210 * Allocate 2 syncql_t's for each pushed module. Note that 5211 * the sqlist_t structure already has 4 syncql_t's built in: 5212 * 2 for the stream head, and 2 for the driver/other stream head. 5213 */ 5214 sqlist_size = 2 * sizeof (syncql_t) * stp->sd_pushcnt + 5215 sizeof (sqlist_t); 5216 if (STRMATED(stp)) 5217 sqlist_size += 2 * sizeof (syncql_t) * stp->sd_mate->sd_pushcnt; 5218 sqlist = kmem_alloc(sqlist_size, kmflag); 5219 5220 sqlist->sqlist_head = NULL; 5221 sqlist->sqlist_size = sqlist_size; 5222 sqlist->sqlist_index = 0; 5223 5224 return (sqlist); 5225 } 5226 5227 /* 5228 * Free the list created by sqlist_alloc() 5229 */ 5230 static void 5231 sqlist_free(sqlist_t *sqlist) 5232 { 5233 kmem_free(sqlist, sqlist->sqlist_size); 5234 } 5235 5236 /* 5237 * Prevent any new entries into any syncq in this stream. 5238 * Used by freezestr. 5239 */ 5240 void 5241 strblock(queue_t *q) 5242 { 5243 struct stdata *stp; 5244 syncql_t *sql; 5245 sqlist_t *sqlist; 5246 5247 q = _RD(q); 5248 5249 stp = STREAM(q); 5250 ASSERT(stp != NULL); 5251 5252 /* 5253 * Get a sorted list with all the duplicates removed containing 5254 * all the syncqs referenced by this stream. 5255 */ 5256 sqlist = sqlist_build(q, stp, B_FALSE); 5257 for (sql = sqlist->sqlist_head; sql != NULL; sql = sql->sql_next) 5258 blocksq(sql->sql_sq, SQ_FROZEN, -1); 5259 sqlist_free(sqlist); 5260 } 5261 5262 /* 5263 * Release the block on new entries into this stream 5264 */ 5265 void 5266 strunblock(queue_t *q) 5267 { 5268 struct stdata *stp; 5269 syncql_t *sql; 5270 sqlist_t *sqlist; 5271 int drain_needed; 5272 5273 q = _RD(q); 5274 5275 /* 5276 * Get a sorted list with all the duplicates removed containing 5277 * all the syncqs referenced by this stream. 5278 * Have to drop the SQ_FROZEN flag on all the syncqs before 5279 * starting to drain them; otherwise the draining might 5280 * cause a freezestr in some module on the stream (which 5281 * would deadlock.) 5282 */ 5283 stp = STREAM(q); 5284 ASSERT(stp != NULL); 5285 sqlist = sqlist_build(q, stp, B_FALSE); 5286 drain_needed = 0; 5287 for (sql = sqlist->sqlist_head; sql != NULL; sql = sql->sql_next) 5288 drain_needed += dropsq(sql->sql_sq, SQ_FROZEN); 5289 if (drain_needed) { 5290 for (sql = sqlist->sqlist_head; sql != NULL; 5291 sql = sql->sql_next) 5292 emptysq(sql->sql_sq); 5293 } 5294 sqlist_free(sqlist); 5295 } 5296 5297 #ifdef DEBUG 5298 static int 5299 qprocsareon(queue_t *rq) 5300 { 5301 if (rq->q_next == NULL) 5302 return (0); 5303 return (_WR(rq->q_next)->q_next == _WR(rq)); 5304 } 5305 5306 int 5307 qclaimed(queue_t *q) 5308 { 5309 uint_t count; 5310 5311 count = q->q_syncq->sq_count; 5312 SUM_SQ_PUTCOUNTS(q->q_syncq, count); 5313 return (count != 0); 5314 } 5315 5316 /* 5317 * Check if anyone has frozen this stream with freezestr 5318 */ 5319 int 5320 frozenstr(queue_t *q) 5321 { 5322 return ((q->q_syncq->sq_flags & SQ_FROZEN) != 0); 5323 } 5324 #endif /* DEBUG */ 5325 5326 /* 5327 * Enter a queue. 5328 * Obsoleted interface. Should not be used. 5329 */ 5330 void 5331 enterq(queue_t *q) 5332 { 5333 entersq(q->q_syncq, SQ_CALLBACK); 5334 } 5335 5336 void 5337 leaveq(queue_t *q) 5338 { 5339 leavesq(q->q_syncq, SQ_CALLBACK); 5340 } 5341 5342 /* 5343 * Enter a perimeter. c_inner and c_outer specifies which concurrency bits 5344 * to check. 5345 * Wait if SQ_QUEUED is set to preserve ordering between messages and qwriter 5346 * calls and the running of open, close and service procedures. 5347 * 5348 * if c_inner bit is set no need to grab sq_putlocks since we don't care 5349 * if other threads have entered or are entering put entry point. 5350 * 5351 * if c_inner bit is set it might have been posible to use 5352 * sq_putlocks/sq_putcounts instead of SQLOCK/sq_count (e.g. to optimize 5353 * open/close path for IP) but since the count may need to be decremented in 5354 * qwait() we wouldn't know which counter to decrement. Currently counter is 5355 * selected by current cpu_seqid and current CPU can change at any moment. XXX 5356 * in the future we might use curthread id bits to select the counter and this 5357 * would stay constant across routine calls. 5358 */ 5359 void 5360 entersq(syncq_t *sq, int entrypoint) 5361 { 5362 uint16_t count = 0; 5363 uint16_t flags; 5364 uint16_t waitflags = SQ_STAYAWAY | SQ_EVENTS | SQ_EXCL; 5365 uint16_t type; 5366 uint_t c_inner = entrypoint & SQ_CI; 5367 uint_t c_outer = entrypoint & SQ_CO; 5368 5369 /* 5370 * Increment ref count to keep closes out of this queue. 5371 */ 5372 ASSERT(sq); 5373 ASSERT(c_inner && c_outer); 5374 mutex_enter(SQLOCK(sq)); 5375 flags = sq->sq_flags; 5376 type = sq->sq_type; 5377 if (!(type & c_inner)) { 5378 /* Make sure all putcounts now use slowlock. */ 5379 count = sq->sq_count; 5380 SQ_PUTLOCKS_ENTER(sq); 5381 SQ_PUTCOUNT_CLRFAST_LOCKED(sq); 5382 SUM_SQ_PUTCOUNTS(sq, count); 5383 sq->sq_needexcl++; 5384 ASSERT(sq->sq_needexcl != 0); /* wraparound */ 5385 waitflags |= SQ_MESSAGES; 5386 } 5387 /* 5388 * Wait until we can enter the inner perimeter. 5389 * If we want exclusive access we wait until sq_count is 0. 5390 * We have to do this before entering the outer perimeter in order 5391 * to preserve put/close message ordering. 5392 */ 5393 while ((flags & waitflags) || (!(type & c_inner) && count != 0)) { 5394 sq->sq_flags = flags | SQ_WANTWAKEUP; 5395 if (!(type & c_inner)) { 5396 SQ_PUTLOCKS_EXIT(sq); 5397 } 5398 cv_wait(&sq->sq_wait, SQLOCK(sq)); 5399 if (!(type & c_inner)) { 5400 count = sq->sq_count; 5401 SQ_PUTLOCKS_ENTER(sq); 5402 SUM_SQ_PUTCOUNTS(sq, count); 5403 } 5404 flags = sq->sq_flags; 5405 } 5406 5407 if (!(type & c_inner)) { 5408 ASSERT(sq->sq_needexcl > 0); 5409 sq->sq_needexcl--; 5410 if (sq->sq_needexcl == 0) { 5411 SQ_PUTCOUNT_SETFAST_LOCKED(sq); 5412 } 5413 } 5414 5415 /* Check if we need to enter the outer perimeter */ 5416 if (!(type & c_outer)) { 5417 /* 5418 * We have to enter the outer perimeter exclusively before 5419 * we can increment sq_count to avoid deadlock. This implies 5420 * that we have to re-check sq_flags and sq_count. 5421 * 5422 * is it possible to have c_inner set when c_outer is not set? 5423 */ 5424 if (!(type & c_inner)) { 5425 SQ_PUTLOCKS_EXIT(sq); 5426 } 5427 mutex_exit(SQLOCK(sq)); 5428 outer_enter(sq->sq_outer, SQ_GOAWAY); 5429 mutex_enter(SQLOCK(sq)); 5430 flags = sq->sq_flags; 5431 /* 5432 * there should be no need to recheck sq_putcounts 5433 * because outer_enter() has already waited for them to clear 5434 * after setting SQ_WRITER. 5435 */ 5436 count = sq->sq_count; 5437 #ifdef DEBUG 5438 /* 5439 * SUMCHECK_SQ_PUTCOUNTS should return the sum instead 5440 * of doing an ASSERT internally. Others should do 5441 * something like 5442 * ASSERT(SUMCHECK_SQ_PUTCOUNTS(sq) == 0); 5443 * without the need to #ifdef DEBUG it. 5444 */ 5445 SUMCHECK_SQ_PUTCOUNTS(sq, 0); 5446 #endif 5447 while ((flags & (SQ_EXCL|SQ_BLOCKED|SQ_FROZEN)) || 5448 (!(type & c_inner) && count != 0)) { 5449 sq->sq_flags = flags | SQ_WANTWAKEUP; 5450 cv_wait(&sq->sq_wait, SQLOCK(sq)); 5451 count = sq->sq_count; 5452 flags = sq->sq_flags; 5453 } 5454 } 5455 5456 sq->sq_count++; 5457 ASSERT(sq->sq_count != 0); /* Wraparound */ 5458 if (!(type & c_inner)) { 5459 /* Exclusive entry */ 5460 ASSERT(sq->sq_count == 1); 5461 sq->sq_flags |= SQ_EXCL; 5462 if (type & c_outer) { 5463 SQ_PUTLOCKS_EXIT(sq); 5464 } 5465 } 5466 mutex_exit(SQLOCK(sq)); 5467 } 5468 5469 /* 5470 * leave a syncq. announce to framework that closes may proceed. 5471 * c_inner and c_outer specifies which concurrency bits 5472 * to check. 5473 * 5474 * must never be called from driver or module put entry point. 5475 * 5476 * no need to grab sq_putlocks here. See comment in strsubr.h that explains when 5477 * sq_putlocks are used. 5478 */ 5479 void 5480 leavesq(syncq_t *sq, int entrypoint) 5481 { 5482 uint16_t flags; 5483 uint16_t type; 5484 uint_t c_outer = entrypoint & SQ_CO; 5485 #ifdef DEBUG 5486 uint_t c_inner = entrypoint & SQ_CI; 5487 #endif 5488 5489 /* 5490 * decrement ref count, drain the syncq if possible, and wake up 5491 * any waiting close. 5492 */ 5493 ASSERT(sq); 5494 ASSERT(c_inner && c_outer); 5495 mutex_enter(SQLOCK(sq)); 5496 flags = sq->sq_flags; 5497 type = sq->sq_type; 5498 if (flags & (SQ_QUEUED|SQ_WANTWAKEUP|SQ_WANTEXWAKEUP)) { 5499 5500 if (flags & SQ_WANTWAKEUP) { 5501 flags &= ~SQ_WANTWAKEUP; 5502 cv_broadcast(&sq->sq_wait); 5503 } 5504 if (flags & SQ_WANTEXWAKEUP) { 5505 flags &= ~SQ_WANTEXWAKEUP; 5506 cv_broadcast(&sq->sq_exitwait); 5507 } 5508 5509 if ((flags & SQ_QUEUED) && !(flags & SQ_STAYAWAY)) { 5510 /* 5511 * The syncq needs to be drained. "Exit" the syncq 5512 * before calling drain_syncq. 5513 */ 5514 ASSERT(sq->sq_count != 0); 5515 sq->sq_count--; 5516 ASSERT((flags & SQ_EXCL) || (type & c_inner)); 5517 sq->sq_flags = flags & ~SQ_EXCL; 5518 drain_syncq(sq); 5519 ASSERT(MUTEX_NOT_HELD(SQLOCK(sq))); 5520 /* Check if we need to exit the outer perimeter */ 5521 /* XXX will this ever be true? */ 5522 if (!(type & c_outer)) 5523 outer_exit(sq->sq_outer); 5524 return; 5525 } 5526 } 5527 ASSERT(sq->sq_count != 0); 5528 sq->sq_count--; 5529 ASSERT((flags & SQ_EXCL) || (type & c_inner)); 5530 sq->sq_flags = flags & ~SQ_EXCL; 5531 mutex_exit(SQLOCK(sq)); 5532 5533 /* Check if we need to exit the outer perimeter */ 5534 if (!(sq->sq_type & c_outer)) 5535 outer_exit(sq->sq_outer); 5536 } 5537 5538 /* 5539 * Prevent q_next from changing in this stream by incrementing sq_count. 5540 * 5541 * no need to grab sq_putlocks here. See comment in strsubr.h that explains when 5542 * sq_putlocks are used. 5543 */ 5544 void 5545 claimq(queue_t *qp) 5546 { 5547 syncq_t *sq = qp->q_syncq; 5548 5549 mutex_enter(SQLOCK(sq)); 5550 sq->sq_count++; 5551 ASSERT(sq->sq_count != 0); /* Wraparound */ 5552 mutex_exit(SQLOCK(sq)); 5553 } 5554 5555 /* 5556 * Undo claimq. 5557 * 5558 * no need to grab sq_putlocks here. See comment in strsubr.h that explains when 5559 * sq_putlocks are used. 5560 */ 5561 void 5562 releaseq(queue_t *qp) 5563 { 5564 syncq_t *sq = qp->q_syncq; 5565 uint16_t flags; 5566 5567 mutex_enter(SQLOCK(sq)); 5568 ASSERT(sq->sq_count > 0); 5569 sq->sq_count--; 5570 5571 flags = sq->sq_flags; 5572 if (flags & (SQ_WANTWAKEUP|SQ_QUEUED)) { 5573 if (flags & SQ_WANTWAKEUP) { 5574 flags &= ~SQ_WANTWAKEUP; 5575 cv_broadcast(&sq->sq_wait); 5576 } 5577 sq->sq_flags = flags; 5578 if ((flags & SQ_QUEUED) && !(flags & (SQ_STAYAWAY|SQ_EXCL))) { 5579 /* 5580 * To prevent potential recursive invocation of 5581 * drain_syncq we do not call drain_syncq if count is 5582 * non-zero. 5583 */ 5584 if (sq->sq_count == 0) { 5585 drain_syncq(sq); 5586 return; 5587 } else 5588 sqenable(sq); 5589 } 5590 } 5591 mutex_exit(SQLOCK(sq)); 5592 } 5593 5594 /* 5595 * Prevent q_next from changing in this stream by incrementing sd_refcnt. 5596 */ 5597 void 5598 claimstr(queue_t *qp) 5599 { 5600 struct stdata *stp = STREAM(qp); 5601 5602 mutex_enter(&stp->sd_reflock); 5603 stp->sd_refcnt++; 5604 ASSERT(stp->sd_refcnt != 0); /* Wraparound */ 5605 mutex_exit(&stp->sd_reflock); 5606 } 5607 5608 /* 5609 * Undo claimstr. 5610 */ 5611 void 5612 releasestr(queue_t *qp) 5613 { 5614 struct stdata *stp = STREAM(qp); 5615 5616 mutex_enter(&stp->sd_reflock); 5617 ASSERT(stp->sd_refcnt != 0); 5618 if (--stp->sd_refcnt == 0) 5619 cv_broadcast(&stp->sd_refmonitor); 5620 mutex_exit(&stp->sd_reflock); 5621 } 5622 5623 static syncq_t * 5624 new_syncq(void) 5625 { 5626 return (kmem_cache_alloc(syncq_cache, KM_SLEEP)); 5627 } 5628 5629 static void 5630 free_syncq(syncq_t *sq) 5631 { 5632 ASSERT(sq->sq_head == NULL); 5633 ASSERT(sq->sq_outer == NULL); 5634 ASSERT(sq->sq_callbpend == NULL); 5635 ASSERT((sq->sq_onext == NULL && sq->sq_oprev == NULL) || 5636 (sq->sq_onext == sq && sq->sq_oprev == sq)); 5637 5638 if (sq->sq_ciputctrl != NULL) { 5639 ASSERT(sq->sq_nciputctrl == n_ciputctrl - 1); 5640 SUMCHECK_CIPUTCTRL_COUNTS(sq->sq_ciputctrl, 5641 sq->sq_nciputctrl, 0); 5642 ASSERT(ciputctrl_cache != NULL); 5643 kmem_cache_free(ciputctrl_cache, sq->sq_ciputctrl); 5644 } 5645 5646 sq->sq_tail = NULL; 5647 sq->sq_evhead = NULL; 5648 sq->sq_evtail = NULL; 5649 sq->sq_ciputctrl = NULL; 5650 sq->sq_nciputctrl = 0; 5651 sq->sq_count = 0; 5652 sq->sq_rmqcount = 0; 5653 sq->sq_callbflags = 0; 5654 sq->sq_cancelid = 0; 5655 sq->sq_next = NULL; 5656 sq->sq_needexcl = 0; 5657 sq->sq_svcflags = 0; 5658 sq->sq_nqueues = 0; 5659 sq->sq_pri = 0; 5660 sq->sq_onext = NULL; 5661 sq->sq_oprev = NULL; 5662 sq->sq_flags = 0; 5663 sq->sq_type = 0; 5664 sq->sq_servcount = 0; 5665 5666 kmem_cache_free(syncq_cache, sq); 5667 } 5668 5669 /* Outer perimeter code */ 5670 5671 /* 5672 * The outer syncq uses the fields and flags in the syncq slightly 5673 * differently from the inner syncqs. 5674 * sq_count Incremented when there are pending or running 5675 * writers at the outer perimeter to prevent the set of 5676 * inner syncqs that belong to the outer perimeter from 5677 * changing. 5678 * sq_head/tail List of deferred qwriter(OUTER) operations. 5679 * 5680 * SQ_BLOCKED Set to prevent traversing of sq_next,sq_prev while 5681 * inner syncqs are added to or removed from the 5682 * outer perimeter. 5683 * SQ_QUEUED sq_head/tail has messages or eventsqueued. 5684 * 5685 * SQ_WRITER A thread is currently traversing all the inner syncqs 5686 * setting the SQ_WRITER flag. 5687 */ 5688 5689 /* 5690 * Get write access at the outer perimeter. 5691 * Note that read access is done by entersq, putnext, and put by simply 5692 * incrementing sq_count in the inner syncq. 5693 * 5694 * Waits until "flags" is no longer set in the outer to prevent multiple 5695 * threads from having write access at the same time. SQ_WRITER has to be part 5696 * of "flags". 5697 * 5698 * Increases sq_count on the outer syncq to keep away outer_insert/remove 5699 * until the outer_exit is finished. 5700 * 5701 * outer_enter is vulnerable to starvation since it does not prevent new 5702 * threads from entering the inner syncqs while it is waiting for sq_count to 5703 * go to zero. 5704 */ 5705 void 5706 outer_enter(syncq_t *outer, uint16_t flags) 5707 { 5708 syncq_t *sq; 5709 int wait_needed; 5710 uint16_t count; 5711 5712 ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL && 5713 outer->sq_oprev != NULL); 5714 ASSERT(flags & SQ_WRITER); 5715 5716 retry: 5717 mutex_enter(SQLOCK(outer)); 5718 while (outer->sq_flags & flags) { 5719 outer->sq_flags |= SQ_WANTWAKEUP; 5720 cv_wait(&outer->sq_wait, SQLOCK(outer)); 5721 } 5722 5723 ASSERT(!(outer->sq_flags & SQ_WRITER)); 5724 outer->sq_flags |= SQ_WRITER; 5725 outer->sq_count++; 5726 ASSERT(outer->sq_count != 0); /* wraparound */ 5727 wait_needed = 0; 5728 /* 5729 * Set SQ_WRITER on all the inner syncqs while holding 5730 * the SQLOCK on the outer syncq. This ensures that the changing 5731 * of SQ_WRITER is atomic under the outer SQLOCK. 5732 */ 5733 for (sq = outer->sq_onext; sq != outer; sq = sq->sq_onext) { 5734 mutex_enter(SQLOCK(sq)); 5735 count = sq->sq_count; 5736 SQ_PUTLOCKS_ENTER(sq); 5737 sq->sq_flags |= SQ_WRITER; 5738 SUM_SQ_PUTCOUNTS(sq, count); 5739 if (count != 0) 5740 wait_needed = 1; 5741 SQ_PUTLOCKS_EXIT(sq); 5742 mutex_exit(SQLOCK(sq)); 5743 } 5744 mutex_exit(SQLOCK(outer)); 5745 5746 /* 5747 * Get everybody out of the syncqs sequentially. 5748 * Note that we don't actually need to aqiure the PUTLOCKS, since 5749 * we have already cleared the fastbit, and set QWRITER. By 5750 * definition, the count can not increase since putnext will 5751 * take the slowlock path (and the purpose of aquiring the 5752 * putlocks was to make sure it didn't increase while we were 5753 * waiting). 5754 * 5755 * Note that we still aquire the PUTLOCKS to be safe. 5756 */ 5757 if (wait_needed) { 5758 for (sq = outer->sq_onext; sq != outer; sq = sq->sq_onext) { 5759 mutex_enter(SQLOCK(sq)); 5760 count = sq->sq_count; 5761 SQ_PUTLOCKS_ENTER(sq); 5762 SUM_SQ_PUTCOUNTS(sq, count); 5763 while (count != 0) { 5764 sq->sq_flags |= SQ_WANTWAKEUP; 5765 SQ_PUTLOCKS_EXIT(sq); 5766 cv_wait(&sq->sq_wait, SQLOCK(sq)); 5767 count = sq->sq_count; 5768 SQ_PUTLOCKS_ENTER(sq); 5769 SUM_SQ_PUTCOUNTS(sq, count); 5770 } 5771 SQ_PUTLOCKS_EXIT(sq); 5772 mutex_exit(SQLOCK(sq)); 5773 } 5774 /* 5775 * Verify that none of the flags got set while we 5776 * were waiting for the sq_counts to drop. 5777 * If this happens we exit and retry entering the 5778 * outer perimeter. 5779 */ 5780 mutex_enter(SQLOCK(outer)); 5781 if (outer->sq_flags & (flags & ~SQ_WRITER)) { 5782 mutex_exit(SQLOCK(outer)); 5783 outer_exit(outer); 5784 goto retry; 5785 } 5786 mutex_exit(SQLOCK(outer)); 5787 } 5788 } 5789 5790 /* 5791 * Drop the write access at the outer perimeter. 5792 * Read access is dropped implicitly (by putnext, put, and leavesq) by 5793 * decrementing sq_count. 5794 */ 5795 void 5796 outer_exit(syncq_t *outer) 5797 { 5798 syncq_t *sq; 5799 int drain_needed; 5800 uint16_t flags; 5801 5802 ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL && 5803 outer->sq_oprev != NULL); 5804 ASSERT(MUTEX_NOT_HELD(SQLOCK(outer))); 5805 5806 /* 5807 * Atomically (from the perspective of threads calling become_writer) 5808 * drop the write access at the outer perimeter by holding 5809 * SQLOCK(outer) across all the dropsq calls and the resetting of 5810 * SQ_WRITER. 5811 * This defines a locking order between the outer perimeter 5812 * SQLOCK and the inner perimeter SQLOCKs. 5813 */ 5814 mutex_enter(SQLOCK(outer)); 5815 flags = outer->sq_flags; 5816 ASSERT(outer->sq_flags & SQ_WRITER); 5817 if (flags & SQ_QUEUED) { 5818 write_now(outer); 5819 flags = outer->sq_flags; 5820 } 5821 5822 /* 5823 * sq_onext is stable since sq_count has not yet been decreased. 5824 * Reset the SQ_WRITER flags in all syncqs. 5825 * After dropping SQ_WRITER on the outer syncq we empty all the 5826 * inner syncqs. 5827 */ 5828 drain_needed = 0; 5829 for (sq = outer->sq_onext; sq != outer; sq = sq->sq_onext) 5830 drain_needed += dropsq(sq, SQ_WRITER); 5831 ASSERT(!(outer->sq_flags & SQ_QUEUED)); 5832 flags &= ~SQ_WRITER; 5833 if (drain_needed) { 5834 outer->sq_flags = flags; 5835 mutex_exit(SQLOCK(outer)); 5836 for (sq = outer->sq_onext; sq != outer; sq = sq->sq_onext) 5837 emptysq(sq); 5838 mutex_enter(SQLOCK(outer)); 5839 flags = outer->sq_flags; 5840 } 5841 if (flags & SQ_WANTWAKEUP) { 5842 flags &= ~SQ_WANTWAKEUP; 5843 cv_broadcast(&outer->sq_wait); 5844 } 5845 outer->sq_flags = flags; 5846 ASSERT(outer->sq_count > 0); 5847 outer->sq_count--; 5848 mutex_exit(SQLOCK(outer)); 5849 } 5850 5851 /* 5852 * Add another syncq to an outer perimeter. 5853 * Block out all other access to the outer perimeter while it is being 5854 * changed using blocksq. 5855 * Assumes that the caller has *not* done an outer_enter. 5856 * 5857 * Vulnerable to starvation in blocksq. 5858 */ 5859 static void 5860 outer_insert(syncq_t *outer, syncq_t *sq) 5861 { 5862 ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL && 5863 outer->sq_oprev != NULL); 5864 ASSERT(sq->sq_outer == NULL && sq->sq_onext == NULL && 5865 sq->sq_oprev == NULL); /* Can't be in an outer perimeter */ 5866 5867 /* Get exclusive access to the outer perimeter list */ 5868 blocksq(outer, SQ_BLOCKED, 0); 5869 ASSERT(outer->sq_flags & SQ_BLOCKED); 5870 ASSERT(!(outer->sq_flags & SQ_WRITER)); 5871 5872 mutex_enter(SQLOCK(sq)); 5873 sq->sq_outer = outer; 5874 outer->sq_onext->sq_oprev = sq; 5875 sq->sq_onext = outer->sq_onext; 5876 outer->sq_onext = sq; 5877 sq->sq_oprev = outer; 5878 mutex_exit(SQLOCK(sq)); 5879 unblocksq(outer, SQ_BLOCKED, 1); 5880 } 5881 5882 /* 5883 * Remove a syncq from an outer perimeter. 5884 * Block out all other access to the outer perimeter while it is being 5885 * changed using blocksq. 5886 * Assumes that the caller has *not* done an outer_enter. 5887 * 5888 * Vulnerable to starvation in blocksq. 5889 */ 5890 static void 5891 outer_remove(syncq_t *outer, syncq_t *sq) 5892 { 5893 ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL && 5894 outer->sq_oprev != NULL); 5895 ASSERT(sq->sq_outer == outer); 5896 5897 /* Get exclusive access to the outer perimeter list */ 5898 blocksq(outer, SQ_BLOCKED, 0); 5899 ASSERT(outer->sq_flags & SQ_BLOCKED); 5900 ASSERT(!(outer->sq_flags & SQ_WRITER)); 5901 5902 mutex_enter(SQLOCK(sq)); 5903 sq->sq_outer = NULL; 5904 sq->sq_onext->sq_oprev = sq->sq_oprev; 5905 sq->sq_oprev->sq_onext = sq->sq_onext; 5906 sq->sq_oprev = sq->sq_onext = NULL; 5907 mutex_exit(SQLOCK(sq)); 5908 unblocksq(outer, SQ_BLOCKED, 1); 5909 } 5910 5911 /* 5912 * Queue a deferred qwriter(OUTER) callback for this outer perimeter. 5913 * If this is the first callback for this outer perimeter then add 5914 * this outer perimeter to the list of outer perimeters that 5915 * the qwriter_outer_thread will process. 5916 * 5917 * Increments sq_count in the outer syncq to prevent the membership 5918 * of the outer perimeter (in terms of inner syncqs) to change while 5919 * the callback is pending. 5920 */ 5921 static void 5922 queue_writer(syncq_t *outer, void (*func)(), queue_t *q, mblk_t *mp) 5923 { 5924 ASSERT(MUTEX_HELD(SQLOCK(outer))); 5925 5926 mp->b_prev = (mblk_t *)func; 5927 mp->b_queue = q; 5928 mp->b_next = NULL; 5929 outer->sq_count++; /* Decremented when dequeued */ 5930 ASSERT(outer->sq_count != 0); /* Wraparound */ 5931 if (outer->sq_evhead == NULL) { 5932 /* First message. */ 5933 outer->sq_evhead = outer->sq_evtail = mp; 5934 outer->sq_flags |= SQ_EVENTS; 5935 mutex_exit(SQLOCK(outer)); 5936 STRSTAT(qwr_outer); 5937 (void) taskq_dispatch(streams_taskq, 5938 (task_func_t *)qwriter_outer_service, outer, TQ_SLEEP); 5939 } else { 5940 ASSERT(outer->sq_flags & SQ_EVENTS); 5941 outer->sq_evtail->b_next = mp; 5942 outer->sq_evtail = mp; 5943 mutex_exit(SQLOCK(outer)); 5944 } 5945 } 5946 5947 /* 5948 * Try and upgrade to write access at the outer perimeter. If this can 5949 * not be done without blocking then queue the callback to be done 5950 * by the qwriter_outer_thread. 5951 * 5952 * This routine can only be called from put or service procedures plus 5953 * asynchronous callback routines that have properly entered to 5954 * queue (with entersq.) Thus qwriter(OUTER) assumes the caller has one claim 5955 * on the syncq associated with q. 5956 */ 5957 void 5958 qwriter_outer(queue_t *q, mblk_t *mp, void (*func)()) 5959 { 5960 syncq_t *osq, *sq, *outer; 5961 int failed; 5962 uint16_t flags; 5963 5964 osq = q->q_syncq; 5965 outer = osq->sq_outer; 5966 if (outer == NULL) 5967 panic("qwriter(PERIM_OUTER): no outer perimeter"); 5968 ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL && 5969 outer->sq_oprev != NULL); 5970 5971 mutex_enter(SQLOCK(outer)); 5972 flags = outer->sq_flags; 5973 /* 5974 * If some thread is traversing sq_next, or if we are blocked by 5975 * outer_insert or outer_remove, or if the we already have queued 5976 * callbacks, then queue this callback for later processing. 5977 * 5978 * Also queue the qwriter for an interrupt thread in order 5979 * to reduce the time spent running at high IPL. 5980 * to identify there are events. 5981 */ 5982 if ((flags & SQ_GOAWAY) || (curthread->t_pri >= kpreemptpri)) { 5983 /* 5984 * Queue the become_writer request. 5985 * The queueing is atomic under SQLOCK(outer) in order 5986 * to synchronize with outer_exit. 5987 * queue_writer will drop the outer SQLOCK 5988 */ 5989 if (flags & SQ_BLOCKED) { 5990 /* Must set SQ_WRITER on inner perimeter */ 5991 mutex_enter(SQLOCK(osq)); 5992 osq->sq_flags |= SQ_WRITER; 5993 mutex_exit(SQLOCK(osq)); 5994 } else { 5995 if (!(flags & SQ_WRITER)) { 5996 /* 5997 * The outer could have been SQ_BLOCKED thus 5998 * SQ_WRITER might not be set on the inner. 5999 */ 6000 mutex_enter(SQLOCK(osq)); 6001 osq->sq_flags |= SQ_WRITER; 6002 mutex_exit(SQLOCK(osq)); 6003 } 6004 ASSERT(osq->sq_flags & SQ_WRITER); 6005 } 6006 queue_writer(outer, func, q, mp); 6007 return; 6008 } 6009 /* 6010 * We are half-way to exclusive access to the outer perimeter. 6011 * Prevent any outer_enter, qwriter(OUTER), or outer_insert/remove 6012 * while the inner syncqs are traversed. 6013 */ 6014 outer->sq_count++; 6015 ASSERT(outer->sq_count != 0); /* wraparound */ 6016 flags |= SQ_WRITER; 6017 /* 6018 * Check if we can run the function immediately. Mark all 6019 * syncqs with the writer flag to prevent new entries into 6020 * put and service procedures. 6021 * 6022 * Set SQ_WRITER on all the inner syncqs while holding 6023 * the SQLOCK on the outer syncq. This ensures that the changing 6024 * of SQ_WRITER is atomic under the outer SQLOCK. 6025 */ 6026 failed = 0; 6027 for (sq = outer->sq_onext; sq != outer; sq = sq->sq_onext) { 6028 uint16_t count; 6029 uint_t maxcnt = (sq == osq) ? 1 : 0; 6030 6031 mutex_enter(SQLOCK(sq)); 6032 count = sq->sq_count; 6033 SQ_PUTLOCKS_ENTER(sq); 6034 SUM_SQ_PUTCOUNTS(sq, count); 6035 if (sq->sq_count > maxcnt) 6036 failed = 1; 6037 sq->sq_flags |= SQ_WRITER; 6038 SQ_PUTLOCKS_EXIT(sq); 6039 mutex_exit(SQLOCK(sq)); 6040 } 6041 if (failed) { 6042 /* 6043 * Some other thread has a read claim on the outer perimeter. 6044 * Queue the callback for deferred processing. 6045 * 6046 * queue_writer will set SQ_QUEUED before we drop SQ_WRITER 6047 * so that other qwriter(OUTER) calls will queue their 6048 * callbacks as well. queue_writer increments sq_count so we 6049 * decrement to compensate for the our increment. 6050 * 6051 * Dropping SQ_WRITER enables the writer thread to work 6052 * on this outer perimeter. 6053 */ 6054 outer->sq_flags = flags; 6055 queue_writer(outer, func, q, mp); 6056 /* queue_writer dropper the lock */ 6057 mutex_enter(SQLOCK(outer)); 6058 ASSERT(outer->sq_count > 0); 6059 outer->sq_count--; 6060 ASSERT(outer->sq_flags & SQ_WRITER); 6061 flags = outer->sq_flags; 6062 flags &= ~SQ_WRITER; 6063 if (flags & SQ_WANTWAKEUP) { 6064 flags &= ~SQ_WANTWAKEUP; 6065 cv_broadcast(&outer->sq_wait); 6066 } 6067 outer->sq_flags = flags; 6068 mutex_exit(SQLOCK(outer)); 6069 return; 6070 } else { 6071 outer->sq_flags = flags; 6072 mutex_exit(SQLOCK(outer)); 6073 } 6074 6075 /* Can run it immediately */ 6076 (*func)(q, mp); 6077 6078 outer_exit(outer); 6079 } 6080 6081 /* 6082 * Dequeue all writer callbacks from the outer perimeter and run them. 6083 */ 6084 static void 6085 write_now(syncq_t *outer) 6086 { 6087 mblk_t *mp; 6088 queue_t *q; 6089 void (*func)(); 6090 6091 ASSERT(MUTEX_HELD(SQLOCK(outer))); 6092 ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL && 6093 outer->sq_oprev != NULL); 6094 while ((mp = outer->sq_evhead) != NULL) { 6095 /* 6096 * queues cannot be placed on the queuelist on the outer 6097 * perimiter. 6098 */ 6099 ASSERT(!(outer->sq_flags & SQ_MESSAGES)); 6100 ASSERT((outer->sq_flags & SQ_EVENTS)); 6101 6102 outer->sq_evhead = mp->b_next; 6103 if (outer->sq_evhead == NULL) { 6104 outer->sq_evtail = NULL; 6105 outer->sq_flags &= ~SQ_EVENTS; 6106 } 6107 ASSERT(outer->sq_count != 0); 6108 outer->sq_count--; /* Incremented when enqueued. */ 6109 mutex_exit(SQLOCK(outer)); 6110 /* 6111 * Drop the message if the queue is closing. 6112 * Make sure that the queue is "claimed" when the callback 6113 * is run in order to satisfy various ASSERTs. 6114 */ 6115 q = mp->b_queue; 6116 func = (void (*)())mp->b_prev; 6117 ASSERT(func != NULL); 6118 mp->b_next = mp->b_prev = NULL; 6119 if (q->q_flag & QWCLOSE) { 6120 freemsg(mp); 6121 } else { 6122 claimq(q); 6123 (*func)(q, mp); 6124 releaseq(q); 6125 } 6126 mutex_enter(SQLOCK(outer)); 6127 } 6128 ASSERT(MUTEX_HELD(SQLOCK(outer))); 6129 } 6130 6131 /* 6132 * The list of messages on the inner syncq is effectively hashed 6133 * by destination queue. These destination queues are doubly 6134 * linked lists (hopefully) in priority order. Messages are then 6135 * put on the queue referenced by the q_sqhead/q_sqtail elements. 6136 * Additional messages are linked together by the b_next/b_prev 6137 * elements in the mblk, with (similar to putq()) the first message 6138 * having a NULL b_prev and the last message having a NULL b_next. 6139 * 6140 * Events, such as qwriter callbacks, are put onto a list in FIFO 6141 * order referenced by sq_evhead, and sq_evtail. This is a singly 6142 * linked list, and messages here MUST be processed in the order queued. 6143 */ 6144 6145 /* 6146 * Run the events on the syncq event list (sq_evhead). 6147 * Assumes there is only one claim on the syncq, it is 6148 * already exclusive (SQ_EXCL set), and the SQLOCK held. 6149 * Messages here are processed in order, with the SQ_EXCL bit 6150 * held all the way through till the last message is processed. 6151 */ 6152 void 6153 sq_run_events(syncq_t *sq) 6154 { 6155 mblk_t *bp; 6156 queue_t *qp; 6157 uint16_t flags = sq->sq_flags; 6158 void (*func)(); 6159 6160 ASSERT(MUTEX_HELD(SQLOCK(sq))); 6161 ASSERT((sq->sq_outer == NULL && sq->sq_onext == NULL && 6162 sq->sq_oprev == NULL) || 6163 (sq->sq_outer != NULL && sq->sq_onext != NULL && 6164 sq->sq_oprev != NULL)); 6165 6166 ASSERT(flags & SQ_EXCL); 6167 ASSERT(sq->sq_count == 1); 6168 6169 /* 6170 * We need to process all of the events on this list. It 6171 * is possible that new events will be added while we are 6172 * away processing a callback, so on every loop, we start 6173 * back at the beginning of the list. 6174 */ 6175 /* 6176 * We have to reaccess sq_evhead since there is a 6177 * possibility of a new entry while we were running 6178 * the callback. 6179 */ 6180 for (bp = sq->sq_evhead; bp != NULL; bp = sq->sq_evhead) { 6181 ASSERT(bp->b_queue->q_syncq == sq); 6182 ASSERT(sq->sq_flags & SQ_EVENTS); 6183 6184 qp = bp->b_queue; 6185 func = (void (*)())bp->b_prev; 6186 ASSERT(func != NULL); 6187 6188 /* 6189 * Messages from the event queue must be taken off in 6190 * FIFO order. 6191 */ 6192 ASSERT(sq->sq_evhead == bp); 6193 sq->sq_evhead = bp->b_next; 6194 6195 if (bp->b_next == NULL) { 6196 /* Deleting last */ 6197 ASSERT(sq->sq_evtail == bp); 6198 sq->sq_evtail = NULL; 6199 sq->sq_flags &= ~SQ_EVENTS; 6200 } 6201 bp->b_prev = bp->b_next = NULL; 6202 ASSERT(bp->b_datap->db_ref != 0); 6203 6204 mutex_exit(SQLOCK(sq)); 6205 6206 (*func)(qp, bp); 6207 6208 mutex_enter(SQLOCK(sq)); 6209 /* 6210 * re-read the flags, since they could have changed. 6211 */ 6212 flags = sq->sq_flags; 6213 ASSERT(flags & SQ_EXCL); 6214 } 6215 ASSERT(sq->sq_evhead == NULL && sq->sq_evtail == NULL); 6216 ASSERT(!(sq->sq_flags & SQ_EVENTS)); 6217 6218 if (flags & SQ_WANTWAKEUP) { 6219 flags &= ~SQ_WANTWAKEUP; 6220 cv_broadcast(&sq->sq_wait); 6221 } 6222 if (flags & SQ_WANTEXWAKEUP) { 6223 flags &= ~SQ_WANTEXWAKEUP; 6224 cv_broadcast(&sq->sq_exitwait); 6225 } 6226 sq->sq_flags = flags; 6227 } 6228 6229 /* 6230 * Put messages on the event list. 6231 * If we can go exclusive now, do so and process the event list, otherwise 6232 * let the last claim service this list (or wake the sqthread). 6233 * This procedure assumes SQLOCK is held. To run the event list, it 6234 * must be called with no claims. 6235 */ 6236 static void 6237 sqfill_events(syncq_t *sq, queue_t *q, mblk_t *mp, void (*func)()) 6238 { 6239 uint16_t count; 6240 6241 ASSERT(MUTEX_HELD(SQLOCK(sq))); 6242 ASSERT(func != NULL); 6243 6244 /* 6245 * This is a callback. Add it to the list of callbacks 6246 * and see about upgrading. 6247 */ 6248 mp->b_prev = (mblk_t *)func; 6249 mp->b_queue = q; 6250 mp->b_next = NULL; 6251 if (sq->sq_evhead == NULL) { 6252 sq->sq_evhead = sq->sq_evtail = mp; 6253 sq->sq_flags |= SQ_EVENTS; 6254 } else { 6255 ASSERT(sq->sq_evtail != NULL); 6256 ASSERT(sq->sq_evtail->b_next == NULL); 6257 ASSERT(sq->sq_flags & SQ_EVENTS); 6258 sq->sq_evtail->b_next = mp; 6259 sq->sq_evtail = mp; 6260 } 6261 /* 6262 * We have set SQ_EVENTS, so threads will have to 6263 * unwind out of the perimiter, and new entries will 6264 * not grab a putlock. But we still need to know 6265 * how many threads have already made a claim to the 6266 * syncq, so grab the putlocks, and sum the counts. 6267 * If there are no claims on the syncq, we can upgrade 6268 * to exclusive, and run the event list. 6269 * NOTE: We hold the SQLOCK, so we can just grab the 6270 * putlocks. 6271 */ 6272 count = sq->sq_count; 6273 SQ_PUTLOCKS_ENTER(sq); 6274 SUM_SQ_PUTCOUNTS(sq, count); 6275 /* 6276 * We have no claim, so we need to check if there 6277 * are no others, then we can upgrade. 6278 */ 6279 /* 6280 * There are currently no claims on 6281 * the syncq by this thread (at least on this entry). The thread who has 6282 * the claim should drain syncq. 6283 */ 6284 if (count > 0) { 6285 /* 6286 * Can't upgrade - other threads inside. 6287 */ 6288 SQ_PUTLOCKS_EXIT(sq); 6289 mutex_exit(SQLOCK(sq)); 6290 return; 6291 } 6292 /* 6293 * Need to set SQ_EXCL and make a claim on the syncq. 6294 */ 6295 ASSERT((sq->sq_flags & SQ_EXCL) == 0); 6296 sq->sq_flags |= SQ_EXCL; 6297 ASSERT(sq->sq_count == 0); 6298 sq->sq_count++; 6299 SQ_PUTLOCKS_EXIT(sq); 6300 6301 /* Process the events list */ 6302 sq_run_events(sq); 6303 6304 /* 6305 * Release our claim... 6306 */ 6307 sq->sq_count--; 6308 6309 /* 6310 * And release SQ_EXCL. 6311 * We don't need to acquire the putlocks to release 6312 * SQ_EXCL, since we are exclusive, and hold the SQLOCK. 6313 */ 6314 sq->sq_flags &= ~SQ_EXCL; 6315 6316 /* 6317 * sq_run_events should have released SQ_EXCL 6318 */ 6319 ASSERT(!(sq->sq_flags & SQ_EXCL)); 6320 6321 /* 6322 * If anything happened while we were running the 6323 * events (or was there before), we need to process 6324 * them now. We shouldn't be exclusive sine we 6325 * released the perimiter above (plus, we asserted 6326 * for it). 6327 */ 6328 if (!(sq->sq_flags & SQ_STAYAWAY) && (sq->sq_flags & SQ_QUEUED)) 6329 drain_syncq(sq); 6330 else 6331 mutex_exit(SQLOCK(sq)); 6332 } 6333 6334 /* 6335 * Perform delayed processing. The caller has to make sure that it is safe 6336 * to enter the syncq (e.g. by checking that none of the SQ_STAYAWAY bits are 6337 * set.) 6338 * 6339 * Assume that the caller has NO claims on the syncq. However, a claim 6340 * on the syncq does not indicate that a thread is draining the syncq. 6341 * There may be more claims on the syncq than there are threads draining 6342 * (i.e. #_threads_draining <= sq_count) 6343 * 6344 * drain_syncq has to terminate when one of the SQ_STAYAWAY bits gets set 6345 * in order to preserve qwriter(OUTER) ordering constraints. 6346 * 6347 * sq_putcount only needs to be checked when dispatching the queued 6348 * writer call for CIPUT sync queue, but this is handled in sq_run_events. 6349 */ 6350 void 6351 drain_syncq(syncq_t *sq) 6352 { 6353 queue_t *qp; 6354 uint16_t count; 6355 uint16_t type = sq->sq_type; 6356 uint16_t flags = sq->sq_flags; 6357 boolean_t bg_service = sq->sq_svcflags & SQ_SERVICE; 6358 6359 TRACE_1(TR_FAC_STREAMS_FR, TR_DRAIN_SYNCQ_START, 6360 "drain_syncq start:%p", sq); 6361 ASSERT(MUTEX_HELD(SQLOCK(sq))); 6362 ASSERT((sq->sq_outer == NULL && sq->sq_onext == NULL && 6363 sq->sq_oprev == NULL) || 6364 (sq->sq_outer != NULL && sq->sq_onext != NULL && 6365 sq->sq_oprev != NULL)); 6366 6367 /* 6368 * Drop SQ_SERVICE flag. 6369 */ 6370 if (bg_service) 6371 sq->sq_svcflags &= ~SQ_SERVICE; 6372 6373 /* 6374 * If SQ_EXCL is set, someone else is processing this syncq - let him 6375 * finish the job. 6376 */ 6377 if (flags & SQ_EXCL) { 6378 if (bg_service) { 6379 ASSERT(sq->sq_servcount != 0); 6380 sq->sq_servcount--; 6381 } 6382 mutex_exit(SQLOCK(sq)); 6383 return; 6384 } 6385 6386 /* 6387 * This routine can be called by a background thread if 6388 * it was scheduled by a hi-priority thread. SO, if there are 6389 * NOT messages queued, return (remember, we have the SQLOCK, 6390 * and it cannot change until we release it). Wakeup any waiters also. 6391 */ 6392 if (!(flags & SQ_QUEUED)) { 6393 if (flags & SQ_WANTWAKEUP) { 6394 flags &= ~SQ_WANTWAKEUP; 6395 cv_broadcast(&sq->sq_wait); 6396 } 6397 if (flags & SQ_WANTEXWAKEUP) { 6398 flags &= ~SQ_WANTEXWAKEUP; 6399 cv_broadcast(&sq->sq_exitwait); 6400 } 6401 sq->sq_flags = flags; 6402 if (bg_service) { 6403 ASSERT(sq->sq_servcount != 0); 6404 sq->sq_servcount--; 6405 } 6406 mutex_exit(SQLOCK(sq)); 6407 return; 6408 } 6409 6410 /* 6411 * If this is not a concurrent put perimiter, we need to 6412 * become exclusive to drain. Also, if not CIPUT, we would 6413 * not have acquired a putlock, so we don't need to check 6414 * the putcounts. If not entering with a claim, we test 6415 * for sq_count == 0. 6416 */ 6417 type = sq->sq_type; 6418 if (!(type & SQ_CIPUT)) { 6419 if (sq->sq_count > 1) { 6420 if (bg_service) { 6421 ASSERT(sq->sq_servcount != 0); 6422 sq->sq_servcount--; 6423 } 6424 mutex_exit(SQLOCK(sq)); 6425 return; 6426 } 6427 sq->sq_flags |= SQ_EXCL; 6428 } 6429 6430 /* 6431 * This is where we make a claim to the syncq. 6432 * This can either be done by incrementing a putlock, or 6433 * the sq_count. But since we already have the SQLOCK 6434 * here, we just bump the sq_count. 6435 * 6436 * Note that after we make a claim, we need to let the code 6437 * fall through to the end of this routine to clean itself 6438 * up. A return in the while loop will put the syncq in a 6439 * very bad state. 6440 */ 6441 sq->sq_count++; 6442 ASSERT(sq->sq_count != 0); /* wraparound */ 6443 6444 while ((flags = sq->sq_flags) & SQ_QUEUED) { 6445 /* 6446 * If we are told to stayaway or went exclusive, 6447 * we are done. 6448 */ 6449 if (flags & (SQ_STAYAWAY)) { 6450 break; 6451 } 6452 6453 /* 6454 * If there are events to run, do so. 6455 * We have one claim to the syncq, so if there are 6456 * more than one, other threads are running. 6457 */ 6458 if (sq->sq_evhead != NULL) { 6459 ASSERT(sq->sq_flags & SQ_EVENTS); 6460 6461 count = sq->sq_count; 6462 SQ_PUTLOCKS_ENTER(sq); 6463 SUM_SQ_PUTCOUNTS(sq, count); 6464 if (count > 1) { 6465 SQ_PUTLOCKS_EXIT(sq); 6466 /* Can't upgrade - other threads inside */ 6467 break; 6468 } 6469 ASSERT((flags & SQ_EXCL) == 0); 6470 sq->sq_flags = flags | SQ_EXCL; 6471 SQ_PUTLOCKS_EXIT(sq); 6472 /* 6473 * we have the only claim, run the events, 6474 * sq_run_events will clear the SQ_EXCL flag. 6475 */ 6476 sq_run_events(sq); 6477 6478 /* 6479 * If this is a CIPUT perimiter, we need 6480 * to drop the SQ_EXCL flag so we can properly 6481 * continue draining the syncq. 6482 */ 6483 if (type & SQ_CIPUT) { 6484 ASSERT(sq->sq_flags & SQ_EXCL); 6485 sq->sq_flags &= ~SQ_EXCL; 6486 } 6487 6488 /* 6489 * And go back to the beginning just in case 6490 * anything changed while we were away. 6491 */ 6492 ASSERT((sq->sq_flags & SQ_EXCL) || (type & SQ_CIPUT)); 6493 continue; 6494 } 6495 6496 ASSERT(sq->sq_evhead == NULL); 6497 ASSERT(!(sq->sq_flags & SQ_EVENTS)); 6498 6499 /* 6500 * Find the queue that is not draining. 6501 * 6502 * q_draining is protected by QLOCK which we do not hold. 6503 * But if it was set, then a thread was draining, and if it gets 6504 * cleared, then it was because the thread has successfully 6505 * drained the syncq, or a GOAWAY state occured. For the GOAWAY 6506 * state to happen, a thread needs the SQLOCK which we hold, and 6507 * if there was such a flag, we whould have already seen it. 6508 */ 6509 6510 for (qp = sq->sq_head; 6511 qp != NULL && (qp->q_draining || 6512 (qp->q_sqflags & Q_SQDRAINING)); 6513 qp = qp->q_sqnext) 6514 ; 6515 6516 if (qp == NULL) 6517 break; 6518 6519 /* 6520 * We have a queue to work on, and we hold the 6521 * SQLOCK and one claim, call qdrain_syncq. 6522 * This means we need to release the SQLOCK and 6523 * aquire the QLOCK (OK since we have a claim). 6524 * Note that qdrain_syncq will actually dequeue 6525 * this queue from the sq_head list when it is 6526 * convinced all the work is done and release 6527 * the QLOCK before returning. 6528 */ 6529 qp->q_sqflags |= Q_SQDRAINING; 6530 mutex_exit(SQLOCK(sq)); 6531 mutex_enter(QLOCK(qp)); 6532 qdrain_syncq(sq, qp); 6533 mutex_enter(SQLOCK(sq)); 6534 6535 /* The queue is drained */ 6536 ASSERT(qp->q_sqflags & Q_SQDRAINING); 6537 qp->q_sqflags &= ~Q_SQDRAINING; 6538 /* 6539 * NOTE: After this point qp should not be used since it may be 6540 * closed. 6541 */ 6542 } 6543 6544 ASSERT(MUTEX_HELD(SQLOCK(sq))); 6545 flags = sq->sq_flags; 6546 6547 /* 6548 * sq->sq_head cannot change because we hold the 6549 * sqlock. However, a thread CAN decide that it is no longer 6550 * going to drain that queue. However, this should be due to 6551 * a GOAWAY state, and we should see that here. 6552 * 6553 * This loop is not very efficient. One solution may be adding a second 6554 * pointer to the "draining" queue, but it is difficult to do when 6555 * queues are inserted in the middle due to priority ordering. Another 6556 * possibility is to yank the queue out of the sq list and put it onto 6557 * the "draining list" and then put it back if it can't be drained. 6558 */ 6559 6560 ASSERT((sq->sq_head == NULL) || (flags & SQ_GOAWAY) || 6561 (type & SQ_CI) || sq->sq_head->q_draining); 6562 6563 /* Drop SQ_EXCL for non-CIPUT perimiters */ 6564 if (!(type & SQ_CIPUT)) 6565 flags &= ~SQ_EXCL; 6566 ASSERT((flags & SQ_EXCL) == 0); 6567 6568 /* Wake up any waiters. */ 6569 if (flags & SQ_WANTWAKEUP) { 6570 flags &= ~SQ_WANTWAKEUP; 6571 cv_broadcast(&sq->sq_wait); 6572 } 6573 if (flags & SQ_WANTEXWAKEUP) { 6574 flags &= ~SQ_WANTEXWAKEUP; 6575 cv_broadcast(&sq->sq_exitwait); 6576 } 6577 sq->sq_flags = flags; 6578 6579 ASSERT(sq->sq_count != 0); 6580 /* Release our claim. */ 6581 sq->sq_count--; 6582 6583 if (bg_service) { 6584 ASSERT(sq->sq_servcount != 0); 6585 sq->sq_servcount--; 6586 } 6587 6588 mutex_exit(SQLOCK(sq)); 6589 6590 TRACE_1(TR_FAC_STREAMS_FR, TR_DRAIN_SYNCQ_END, 6591 "drain_syncq end:%p", sq); 6592 } 6593 6594 6595 /* 6596 * 6597 * qdrain_syncq can be called (currently) from only one of two places: 6598 * drain_syncq 6599 * putnext (or some variation of it). 6600 * and eventually 6601 * qwait(_sig) 6602 * 6603 * If called from drain_syncq, we found it in the list 6604 * of queue's needing service, so there is work to be done (or it 6605 * wouldn't be on the list). 6606 * 6607 * If called from some putnext variation, it was because the 6608 * perimiter is open, but messages are blocking a putnext and 6609 * there is not a thread working on it. Now a thread could start 6610 * working on it while we are getting ready to do so ourself, but 6611 * the thread would set the q_draining flag, and we can spin out. 6612 * 6613 * As for qwait(_sig), I think I shall let it continue to call 6614 * drain_syncq directly (after all, it will get here eventually). 6615 * 6616 * qdrain_syncq has to terminate when: 6617 * - one of the SQ_STAYAWAY bits gets set to preserve qwriter(OUTER) ordering 6618 * - SQ_EVENTS gets set to preserve qwriter(INNER) ordering 6619 * 6620 * ASSUMES: 6621 * One claim 6622 * QLOCK held 6623 * SQLOCK not held 6624 * Will release QLOCK before returning 6625 */ 6626 void 6627 qdrain_syncq(syncq_t *sq, queue_t *q) 6628 { 6629 mblk_t *bp; 6630 boolean_t do_clr; 6631 #ifdef DEBUG 6632 uint16_t count; 6633 #endif 6634 6635 TRACE_1(TR_FAC_STREAMS_FR, TR_DRAIN_SYNCQ_START, 6636 "drain_syncq start:%p", sq); 6637 ASSERT(q->q_syncq == sq); 6638 ASSERT(MUTEX_HELD(QLOCK(q))); 6639 ASSERT(MUTEX_NOT_HELD(SQLOCK(sq))); 6640 /* 6641 * For non-CIPUT perimiters, we should be called with the 6642 * exclusive bit set already. For non-CIPUT perimiters we 6643 * will be doing a concurrent drain, so it better not be set. 6644 */ 6645 ASSERT((sq->sq_flags & (SQ_EXCL|SQ_CIPUT))); 6646 ASSERT(!((sq->sq_type & SQ_CIPUT) && (sq->sq_flags & SQ_EXCL))); 6647 ASSERT((sq->sq_type & SQ_CIPUT) || (sq->sq_flags & SQ_EXCL)); 6648 /* 6649 * All outer pointers are set, or none of them are 6650 */ 6651 ASSERT((sq->sq_outer == NULL && sq->sq_onext == NULL && 6652 sq->sq_oprev == NULL) || 6653 (sq->sq_outer != NULL && sq->sq_onext != NULL && 6654 sq->sq_oprev != NULL)); 6655 #ifdef DEBUG 6656 count = sq->sq_count; 6657 /* 6658 * This is OK without the putlocks, because we have one 6659 * claim either from the sq_count, or a putcount. We could 6660 * get an erroneous value from other counts, but ours won't 6661 * change, so one way or another, we will have at least a 6662 * value of one. 6663 */ 6664 SUM_SQ_PUTCOUNTS(sq, count); 6665 ASSERT(count >= 1); 6666 #endif /* DEBUG */ 6667 6668 /* 6669 * The first thing to do here, is find out if a thread is already 6670 * draining this queue or the queue is closing. If so, we are done, 6671 * just return. Also, if there are no messages, we are done as well. 6672 * Note that we check the q_sqhead since there is s window of 6673 * opportunity for us to enter here because Q_SQQUEUED was set, but is 6674 * not anymore. 6675 */ 6676 if (q->q_draining || (q->q_sqhead == NULL)) { 6677 mutex_exit(QLOCK(q)); 6678 return; 6679 } 6680 6681 /* 6682 * If the perimiter is exclusive, there is nothing we can 6683 * do right now, go away. 6684 * Note that there is nothing to prevent this case from changing 6685 * right after this check, but the spin-out will catch it. 6686 */ 6687 6688 /* Tell other threads that we are draining this queue */ 6689 q->q_draining = 1; /* Protected by QLOCK */ 6690 6691 for (bp = q->q_sqhead; bp != NULL; bp = q->q_sqhead) { 6692 6693 /* 6694 * Because we can enter this routine just because 6695 * a putnext is blocked, we need to spin out if 6696 * the perimiter wants to go exclusive as well 6697 * as just blocked. We need to spin out also if 6698 * events are queued on the syncq. 6699 * Don't check for SQ_EXCL, because non-CIPUT 6700 * perimiters would set it, and it can't become 6701 * exclusive while we hold a claim. 6702 */ 6703 if (sq->sq_flags & (SQ_STAYAWAY | SQ_EVENTS)) { 6704 break; 6705 } 6706 6707 #ifdef DEBUG 6708 /* 6709 * Since we are in qdrain_syncq, we already know the queue, 6710 * but for sanity, we want to check this against the qp that 6711 * was passed in by bp->b_queue. 6712 */ 6713 6714 ASSERT(bp->b_queue == q); 6715 ASSERT(bp->b_queue->q_syncq == sq); 6716 bp->b_queue = NULL; 6717 6718 /* 6719 * We would have the following check in the DEBUG code: 6720 * 6721 * if (bp->b_prev != NULL) { 6722 * ASSERT(bp->b_prev == (void (*)())q->q_qinfo->qi_putp); 6723 * } 6724 * 6725 * This can't be done, however, since IP modifies qinfo 6726 * structure at run-time (switching between IPv4 qinfo and IPv6 6727 * qinfo), invalidating the check. 6728 * So the assignment to func is left here, but the ASSERT itself 6729 * is removed until the whole issue is resolved. 6730 */ 6731 #endif 6732 ASSERT(q->q_sqhead == bp); 6733 q->q_sqhead = bp->b_next; 6734 bp->b_prev = bp->b_next = NULL; 6735 ASSERT(q->q_syncqmsgs > 0); 6736 mutex_exit(QLOCK(q)); 6737 6738 ASSERT(bp->b_datap->db_ref != 0); 6739 6740 (void) (*q->q_qinfo->qi_putp)(q, bp); 6741 6742 mutex_enter(QLOCK(q)); 6743 /* 6744 * We should decrement q_syncqmsgs only after executing the 6745 * put procedure to avoid a possible race with putnext(). 6746 * In putnext() though it sees Q_SQQUEUED is set, there is 6747 * an optimization which allows putnext to call the put 6748 * procedure directly if (q_syncqmsgs == 0) and thus 6749 * a message reodering could otherwise occur. 6750 */ 6751 q->q_syncqmsgs--; 6752 6753 /* 6754 * Clear QFULL in the next service procedure queue if 6755 * this is the last message destined to that queue. 6756 * 6757 * It would make better sense to have some sort of 6758 * tunable for the low water mark, but these symantics 6759 * are not yet defined. So, alas, we use a constant. 6760 */ 6761 do_clr = (q->q_syncqmsgs == 0); 6762 mutex_exit(QLOCK(q)); 6763 6764 if (do_clr) 6765 clr_qfull(q); 6766 6767 mutex_enter(QLOCK(q)); 6768 /* 6769 * Always clear SQ_EXCL when CIPUT in order to handle 6770 * qwriter(INNER). 6771 */ 6772 /* 6773 * The putp() can call qwriter and get exclusive access 6774 * IFF this is the only claim. So, we need to test for 6775 * this possibility so we can aquire the mutex and clear 6776 * the bit. 6777 */ 6778 if ((sq->sq_type & SQ_CIPUT) && (sq->sq_flags & SQ_EXCL)) { 6779 mutex_enter(SQLOCK(sq)); 6780 sq->sq_flags &= ~SQ_EXCL; 6781 mutex_exit(SQLOCK(sq)); 6782 } 6783 } 6784 6785 /* 6786 * We should either have no queues on the syncq, or we were 6787 * told to goaway by a waiter (which we will wake up at the 6788 * end of this function). 6789 */ 6790 ASSERT((q->q_sqhead == NULL) || 6791 (sq->sq_flags & (SQ_STAYAWAY | SQ_EVENTS))); 6792 6793 ASSERT(MUTEX_HELD(QLOCK(q))); 6794 ASSERT(MUTEX_NOT_HELD(SQLOCK(sq))); 6795 6796 /* 6797 * Remove the q from the syncq list if all the messages are 6798 * drained. 6799 */ 6800 if (q->q_sqhead == NULL) { 6801 mutex_enter(SQLOCK(sq)); 6802 if (q->q_sqflags & Q_SQQUEUED) 6803 SQRM_Q(sq, q); 6804 mutex_exit(SQLOCK(sq)); 6805 /* 6806 * Since the queue is removed from the list, reset its priority. 6807 */ 6808 q->q_spri = 0; 6809 } 6810 6811 /* 6812 * Remember, the q_draining flag is used to let another 6813 * thread know that there is a thread currently draining 6814 * the messages for a queue. Since we are now done with 6815 * this queue (even if there may be messages still there), 6816 * we need to clear this flag so some thread will work 6817 * on it if needed. 6818 */ 6819 ASSERT(q->q_draining); 6820 q->q_draining = 0; 6821 6822 /* called with a claim, so OK to drop all locks. */ 6823 mutex_exit(QLOCK(q)); 6824 6825 TRACE_1(TR_FAC_STREAMS_FR, TR_DRAIN_SYNCQ_END, 6826 "drain_syncq end:%p", sq); 6827 } 6828 /* END OF QDRAIN_SYNCQ */ 6829 6830 6831 /* 6832 * This is the mate to qdrain_syncq, except that it is putting the 6833 * message onto the the queue instead draining. Since the 6834 * message is destined for the queue that is selected, there is 6835 * no need to identify the function because the message is 6836 * intended for the put routine for the queue. But this 6837 * routine will do it anyway just in case (but only for debug kernels). 6838 * 6839 * After the message is enqueued on the syncq, it calls putnext_tail() 6840 * which will schedule a background thread to actually process the message. 6841 * 6842 * Assumes that there is a claim on the syncq (sq->sq_count > 0) and 6843 * SQLOCK(sq) and QLOCK(q) are not held. 6844 */ 6845 void 6846 qfill_syncq(syncq_t *sq, queue_t *q, mblk_t *mp) 6847 { 6848 ASSERT(MUTEX_NOT_HELD(SQLOCK(sq))); 6849 ASSERT(MUTEX_NOT_HELD(QLOCK(q))); 6850 ASSERT(sq->sq_count > 0); 6851 ASSERT(q->q_syncq == sq); 6852 ASSERT((sq->sq_outer == NULL && sq->sq_onext == NULL && 6853 sq->sq_oprev == NULL) || 6854 (sq->sq_outer != NULL && sq->sq_onext != NULL && 6855 sq->sq_oprev != NULL)); 6856 6857 mutex_enter(QLOCK(q)); 6858 6859 #ifdef DEBUG 6860 /* 6861 * This is used for debug in the qfill_syncq/qdrain_syncq case 6862 * to trace the queue that the message is intended for. Note 6863 * that the original use was to identify the queue and function 6864 * to call on the drain. In the new syncq, we have the context 6865 * of the queue that we are draining, so call it's putproc and 6866 * don't rely on the saved values. But for debug this is still 6867 * usefull information. 6868 */ 6869 mp->b_prev = (mblk_t *)q->q_qinfo->qi_putp; 6870 mp->b_queue = q; 6871 mp->b_next = NULL; 6872 #endif 6873 ASSERT(q->q_syncq == sq); 6874 /* 6875 * Enqueue the message on the list. 6876 * SQPUT_MP() accesses q_syncqmsgs. We are already holding QLOCK to 6877 * protect it. So its ok to acquire SQLOCK after SQPUT_MP(). 6878 */ 6879 SQPUT_MP(q, mp); 6880 mutex_enter(SQLOCK(sq)); 6881 6882 /* 6883 * And queue on syncq for scheduling, if not already queued. 6884 * Note that we need the SQLOCK for this, and for testing flags 6885 * at the end to see if we will drain. So grab it now, and 6886 * release it before we call qdrain_syncq or return. 6887 */ 6888 if (!(q->q_sqflags & Q_SQQUEUED)) { 6889 q->q_spri = curthread->t_pri; 6890 SQPUT_Q(sq, q); 6891 } 6892 #ifdef DEBUG 6893 else { 6894 /* 6895 * All of these conditions MUST be true! 6896 */ 6897 ASSERT(sq->sq_tail != NULL); 6898 if (sq->sq_tail == sq->sq_head) { 6899 ASSERT((q->q_sqprev == NULL) && 6900 (q->q_sqnext == NULL)); 6901 } else { 6902 ASSERT((q->q_sqprev != NULL) || 6903 (q->q_sqnext != NULL)); 6904 } 6905 ASSERT(sq->sq_flags & SQ_QUEUED); 6906 ASSERT(q->q_syncqmsgs != 0); 6907 ASSERT(q->q_sqflags & Q_SQQUEUED); 6908 } 6909 #endif 6910 mutex_exit(QLOCK(q)); 6911 /* 6912 * SQLOCK is still held, so sq_count can be safely decremented. 6913 */ 6914 sq->sq_count--; 6915 6916 putnext_tail(sq, q, 0); 6917 /* Should not reference sq or q after this point. */ 6918 } 6919 6920 /* End of qfill_syncq */ 6921 6922 /* 6923 * Remove all messages from a syncq (if qp is NULL) or remove all messages 6924 * that would be put into qp by drain_syncq. 6925 * Used when deleting the syncq (qp == NULL) or when detaching 6926 * a queue (qp != NULL). 6927 * Return non-zero if one or more messages were freed. 6928 * 6929 * no need to grab sq_putlocks here. See comment in strsubr.h that explains when 6930 * sq_putlocks are used. 6931 * 6932 * NOTE: This function assumes that it is called from the close() context and 6933 * that all the queues in the syncq are going aay. For this reason it doesn't 6934 * acquire QLOCK for modifying q_sqhead/q_sqtail fields. This assumption is 6935 * currently valid, but it is useful to rethink this function to behave properly 6936 * in other cases. 6937 */ 6938 int 6939 flush_syncq(syncq_t *sq, queue_t *qp) 6940 { 6941 mblk_t *bp, *mp_head, *mp_next, *mp_prev; 6942 queue_t *q; 6943 int ret = 0; 6944 6945 mutex_enter(SQLOCK(sq)); 6946 6947 /* 6948 * Before we leave, we need to make sure there are no 6949 * events listed for this queue. All events for this queue 6950 * will just be freed. 6951 */ 6952 if (qp != NULL && sq->sq_evhead != NULL) { 6953 ASSERT(sq->sq_flags & SQ_EVENTS); 6954 6955 mp_prev = NULL; 6956 for (bp = sq->sq_evhead; bp != NULL; bp = mp_next) { 6957 mp_next = bp->b_next; 6958 if (bp->b_queue == qp) { 6959 /* Delete this message */ 6960 if (mp_prev != NULL) { 6961 mp_prev->b_next = mp_next; 6962 /* 6963 * Update sq_evtail if the last element 6964 * is removed. 6965 */ 6966 if (bp == sq->sq_evtail) { 6967 ASSERT(mp_next == NULL); 6968 sq->sq_evtail = mp_prev; 6969 } 6970 } else 6971 sq->sq_evhead = mp_next; 6972 if (sq->sq_evhead == NULL) 6973 sq->sq_flags &= ~SQ_EVENTS; 6974 bp->b_prev = bp->b_next = NULL; 6975 freemsg(bp); 6976 ret++; 6977 } else { 6978 mp_prev = bp; 6979 } 6980 } 6981 } 6982 6983 /* 6984 * Walk sq_head and: 6985 * - match qp if qp is set, remove it's messages 6986 * - all if qp is not set 6987 */ 6988 q = sq->sq_head; 6989 while (q != NULL) { 6990 ASSERT(q->q_syncq == sq); 6991 if ((qp == NULL) || (qp == q)) { 6992 /* 6993 * Yank the messages as a list off the queue 6994 */ 6995 mp_head = q->q_sqhead; 6996 /* 6997 * We do not have QLOCK(q) here (which is safe due to 6998 * assumptions mentioned above). To obtain the lock we 6999 * need to release SQLOCK which may allow lots of things 7000 * to change upon us. This place requires more analysis. 7001 */ 7002 q->q_sqhead = q->q_sqtail = NULL; 7003 ASSERT(mp_head->b_queue && 7004 mp_head->b_queue->q_syncq == sq); 7005 7006 /* 7007 * Free each of the messages. 7008 */ 7009 for (bp = mp_head; bp != NULL; bp = mp_next) { 7010 mp_next = bp->b_next; 7011 bp->b_prev = bp->b_next = NULL; 7012 freemsg(bp); 7013 ret++; 7014 } 7015 /* 7016 * Now remove the queue from the syncq. 7017 */ 7018 ASSERT(q->q_sqflags & Q_SQQUEUED); 7019 SQRM_Q(sq, q); 7020 q->q_spri = 0; 7021 q->q_syncqmsgs = 0; 7022 7023 /* 7024 * If qp was specified, we are done with it and are 7025 * going to drop SQLOCK(sq) and return. We wakeup syncq 7026 * waiters while we still have the SQLOCK. 7027 */ 7028 if ((qp != NULL) && (sq->sq_flags & SQ_WANTWAKEUP)) { 7029 sq->sq_flags &= ~SQ_WANTWAKEUP; 7030 cv_broadcast(&sq->sq_wait); 7031 } 7032 /* Drop SQLOCK across clr_qfull */ 7033 mutex_exit(SQLOCK(sq)); 7034 7035 /* 7036 * We avoid doing the test that drain_syncq does and 7037 * unconditionally clear qfull for every flushed 7038 * message. Since flush_syncq is only called during 7039 * close this should not be a problem. 7040 */ 7041 clr_qfull(q); 7042 if (qp != NULL) { 7043 return (ret); 7044 } else { 7045 mutex_enter(SQLOCK(sq)); 7046 /* 7047 * The head was removed by SQRM_Q above. 7048 * reread the new head and flush it. 7049 */ 7050 q = sq->sq_head; 7051 } 7052 } else { 7053 q = q->q_sqnext; 7054 } 7055 ASSERT(MUTEX_HELD(SQLOCK(sq))); 7056 } 7057 7058 if (sq->sq_flags & SQ_WANTWAKEUP) { 7059 sq->sq_flags &= ~SQ_WANTWAKEUP; 7060 cv_broadcast(&sq->sq_wait); 7061 } 7062 7063 mutex_exit(SQLOCK(sq)); 7064 return (ret); 7065 } 7066 7067 /* 7068 * Propagate all messages from a syncq to the next syncq that are associated 7069 * with the specified queue. If the queue is attached to a driver or if the 7070 * messages have been added due to a qwriter(PERIM_INNER), free the messages. 7071 * 7072 * Assumes that the stream is strlock()'ed. We don't come here if there 7073 * are no messages to propagate. 7074 * 7075 * NOTE : If the queue is attached to a driver, all the messages are freed 7076 * as there is no point in propagating the messages from the driver syncq 7077 * to the closing stream head which will in turn get freed later. 7078 */ 7079 static int 7080 propagate_syncq(queue_t *qp) 7081 { 7082 mblk_t *bp, *head, *tail, *prev, *next; 7083 syncq_t *sq; 7084 queue_t *nqp; 7085 syncq_t *nsq; 7086 boolean_t isdriver; 7087 int moved = 0; 7088 uint16_t flags; 7089 pri_t priority = curthread->t_pri; 7090 #ifdef DEBUG 7091 void (*func)(); 7092 #endif 7093 7094 sq = qp->q_syncq; 7095 ASSERT(MUTEX_HELD(SQLOCK(sq))); 7096 /* debug macro */ 7097 SQ_PUTLOCKS_HELD(sq); 7098 /* 7099 * As entersq() does not increment the sq_count for 7100 * the write side, check sq_count for non-QPERQ 7101 * perimeters alone. 7102 */ 7103 ASSERT((qp->q_flag & QPERQ) || (sq->sq_count >= 1)); 7104 7105 /* 7106 * propagate_syncq() can be called because of either messages on the 7107 * queue syncq or because on events on the queue syncq. Do actual 7108 * message propagations if there are any messages. 7109 */ 7110 if (qp->q_syncqmsgs) { 7111 isdriver = (qp->q_flag & QISDRV); 7112 7113 if (!isdriver) { 7114 nqp = qp->q_next; 7115 nsq = nqp->q_syncq; 7116 ASSERT(MUTEX_HELD(SQLOCK(nsq))); 7117 /* debug macro */ 7118 SQ_PUTLOCKS_HELD(nsq); 7119 #ifdef DEBUG 7120 func = (void (*)())nqp->q_qinfo->qi_putp; 7121 #endif 7122 } 7123 7124 SQRM_Q(sq, qp); 7125 priority = MAX(qp->q_spri, priority); 7126 qp->q_spri = 0; 7127 head = qp->q_sqhead; 7128 tail = qp->q_sqtail; 7129 qp->q_sqhead = qp->q_sqtail = NULL; 7130 qp->q_syncqmsgs = 0; 7131 7132 /* 7133 * Walk the list of messages, and free them if this is a driver, 7134 * otherwise reset the b_prev and b_queue value to the new putp. 7135 * Afterward, we will just add the head to the end of the next 7136 * syncq, and point the tail to the end of this one. 7137 */ 7138 7139 for (bp = head; bp != NULL; bp = next) { 7140 next = bp->b_next; 7141 if (isdriver) { 7142 bp->b_prev = bp->b_next = NULL; 7143 freemsg(bp); 7144 continue; 7145 } 7146 /* Change the q values for this message */ 7147 bp->b_queue = nqp; 7148 #ifdef DEBUG 7149 bp->b_prev = (mblk_t *)func; 7150 #endif 7151 moved++; 7152 } 7153 /* 7154 * Attach list of messages to the end of the new queue (if there 7155 * is a list of messages). 7156 */ 7157 7158 if (!isdriver && head != NULL) { 7159 ASSERT(tail != NULL); 7160 if (nqp->q_sqhead == NULL) { 7161 nqp->q_sqhead = head; 7162 } else { 7163 ASSERT(nqp->q_sqtail != NULL); 7164 nqp->q_sqtail->b_next = head; 7165 } 7166 nqp->q_sqtail = tail; 7167 /* 7168 * When messages are moved from high priority queue to 7169 * another queue, the destination queue priority is 7170 * upgraded. 7171 */ 7172 7173 if (priority > nqp->q_spri) 7174 nqp->q_spri = priority; 7175 7176 SQPUT_Q(nsq, nqp); 7177 7178 nqp->q_syncqmsgs += moved; 7179 ASSERT(nqp->q_syncqmsgs != 0); 7180 } 7181 } 7182 7183 /* 7184 * Before we leave, we need to make sure there are no 7185 * events listed for this queue. All events for this queue 7186 * will just be freed. 7187 */ 7188 if (sq->sq_evhead != NULL) { 7189 ASSERT(sq->sq_flags & SQ_EVENTS); 7190 prev = NULL; 7191 for (bp = sq->sq_evhead; bp != NULL; bp = next) { 7192 next = bp->b_next; 7193 if (bp->b_queue == qp) { 7194 /* Delete this message */ 7195 if (prev != NULL) { 7196 prev->b_next = next; 7197 /* 7198 * Update sq_evtail if the last element 7199 * is removed. 7200 */ 7201 if (bp == sq->sq_evtail) { 7202 ASSERT(next == NULL); 7203 sq->sq_evtail = prev; 7204 } 7205 } else 7206 sq->sq_evhead = next; 7207 if (sq->sq_evhead == NULL) 7208 sq->sq_flags &= ~SQ_EVENTS; 7209 bp->b_prev = bp->b_next = NULL; 7210 freemsg(bp); 7211 } else { 7212 prev = bp; 7213 } 7214 } 7215 } 7216 7217 flags = sq->sq_flags; 7218 7219 /* Wake up any waiter before leaving. */ 7220 if (flags & SQ_WANTWAKEUP) { 7221 flags &= ~SQ_WANTWAKEUP; 7222 cv_broadcast(&sq->sq_wait); 7223 } 7224 sq->sq_flags = flags; 7225 7226 return (moved); 7227 } 7228 7229 /* 7230 * Try and upgrade to exclusive access at the inner perimeter. If this can 7231 * not be done without blocking then request will be queued on the syncq 7232 * and drain_syncq will run it later. 7233 * 7234 * This routine can only be called from put or service procedures plus 7235 * asynchronous callback routines that have properly entered to 7236 * queue (with entersq.) Thus qwriter_inner assumes the caller has one claim 7237 * on the syncq associated with q. 7238 */ 7239 void 7240 qwriter_inner(queue_t *q, mblk_t *mp, void (*func)()) 7241 { 7242 syncq_t *sq = q->q_syncq; 7243 uint16_t count; 7244 7245 mutex_enter(SQLOCK(sq)); 7246 count = sq->sq_count; 7247 SQ_PUTLOCKS_ENTER(sq); 7248 SUM_SQ_PUTCOUNTS(sq, count); 7249 ASSERT(count >= 1); 7250 ASSERT(sq->sq_type & (SQ_CIPUT|SQ_CISVC)); 7251 7252 if (count == 1) { 7253 /* 7254 * Can upgrade. This case also handles nested qwriter calls 7255 * (when the qwriter callback function calls qwriter). In that 7256 * case SQ_EXCL is already set. 7257 */ 7258 sq->sq_flags |= SQ_EXCL; 7259 SQ_PUTLOCKS_EXIT(sq); 7260 mutex_exit(SQLOCK(sq)); 7261 (*func)(q, mp); 7262 /* 7263 * Assumes that leavesq, putnext, and drain_syncq will reset 7264 * SQ_EXCL for SQ_CIPUT/SQ_CISVC queues. We leave SQ_EXCL on 7265 * until putnext, leavesq, or drain_syncq drops it. 7266 * That way we handle nested qwriter(INNER) without dropping 7267 * SQ_EXCL until the outermost qwriter callback routine is 7268 * done. 7269 */ 7270 return; 7271 } 7272 SQ_PUTLOCKS_EXIT(sq); 7273 sqfill_events(sq, q, mp, func); 7274 } 7275 7276 /* 7277 * Synchronous callback support functions 7278 */ 7279 7280 /* 7281 * Allocate a callback parameter structure. 7282 * Assumes that caller initializes the flags and the id. 7283 * Acquires SQLOCK(sq) if non-NULL is returned. 7284 */ 7285 callbparams_t * 7286 callbparams_alloc(syncq_t *sq, void (*func)(void *), void *arg, int kmflags) 7287 { 7288 callbparams_t *cbp; 7289 size_t size = sizeof (callbparams_t); 7290 7291 cbp = kmem_alloc(size, kmflags & ~KM_PANIC); 7292 7293 /* 7294 * Only try tryhard allocation if the caller is ready to panic. 7295 * Otherwise just fail. 7296 */ 7297 if (cbp == NULL) { 7298 if (kmflags & KM_PANIC) 7299 cbp = kmem_alloc_tryhard(sizeof (callbparams_t), 7300 &size, kmflags); 7301 else 7302 return (NULL); 7303 } 7304 7305 ASSERT(size >= sizeof (callbparams_t)); 7306 cbp->cbp_size = size; 7307 cbp->cbp_sq = sq; 7308 cbp->cbp_func = func; 7309 cbp->cbp_arg = arg; 7310 mutex_enter(SQLOCK(sq)); 7311 cbp->cbp_next = sq->sq_callbpend; 7312 sq->sq_callbpend = cbp; 7313 return (cbp); 7314 } 7315 7316 void 7317 callbparams_free(syncq_t *sq, callbparams_t *cbp) 7318 { 7319 callbparams_t **pp, *p; 7320 7321 ASSERT(MUTEX_HELD(SQLOCK(sq))); 7322 7323 for (pp = &sq->sq_callbpend; (p = *pp) != NULL; pp = &p->cbp_next) { 7324 if (p == cbp) { 7325 *pp = p->cbp_next; 7326 kmem_free(p, p->cbp_size); 7327 return; 7328 } 7329 } 7330 (void) (STRLOG(0, 0, 0, SL_CONSOLE, 7331 "callbparams_free: not found\n")); 7332 } 7333 7334 void 7335 callbparams_free_id(syncq_t *sq, callbparams_id_t id, int32_t flag) 7336 { 7337 callbparams_t **pp, *p; 7338 7339 ASSERT(MUTEX_HELD(SQLOCK(sq))); 7340 7341 for (pp = &sq->sq_callbpend; (p = *pp) != NULL; pp = &p->cbp_next) { 7342 if (p->cbp_id == id && p->cbp_flags == flag) { 7343 *pp = p->cbp_next; 7344 kmem_free(p, p->cbp_size); 7345 return; 7346 } 7347 } 7348 (void) (STRLOG(0, 0, 0, SL_CONSOLE, 7349 "callbparams_free_id: not found\n")); 7350 } 7351 7352 /* 7353 * Callback wrapper function used by once-only callbacks that can be 7354 * cancelled (qtimeout and qbufcall) 7355 * Contains inline version of entersq(sq, SQ_CALLBACK) that can be 7356 * cancelled by the qun* functions. 7357 */ 7358 void 7359 qcallbwrapper(void *arg) 7360 { 7361 callbparams_t *cbp = arg; 7362 syncq_t *sq; 7363 uint16_t count = 0; 7364 uint16_t waitflags = SQ_STAYAWAY | SQ_EVENTS | SQ_EXCL; 7365 uint16_t type; 7366 7367 sq = cbp->cbp_sq; 7368 mutex_enter(SQLOCK(sq)); 7369 type = sq->sq_type; 7370 if (!(type & SQ_CICB)) { 7371 count = sq->sq_count; 7372 SQ_PUTLOCKS_ENTER(sq); 7373 SQ_PUTCOUNT_CLRFAST_LOCKED(sq); 7374 SUM_SQ_PUTCOUNTS(sq, count); 7375 sq->sq_needexcl++; 7376 ASSERT(sq->sq_needexcl != 0); /* wraparound */ 7377 waitflags |= SQ_MESSAGES; 7378 } 7379 /* Can not handle exlusive entry at outer perimeter */ 7380 ASSERT(type & SQ_COCB); 7381 7382 while ((sq->sq_flags & waitflags) || (!(type & SQ_CICB) &&count != 0)) { 7383 if ((sq->sq_callbflags & cbp->cbp_flags) && 7384 (sq->sq_cancelid == cbp->cbp_id)) { 7385 /* timeout has been cancelled */ 7386 sq->sq_callbflags |= SQ_CALLB_BYPASSED; 7387 callbparams_free(sq, cbp); 7388 if (!(type & SQ_CICB)) { 7389 ASSERT(sq->sq_needexcl > 0); 7390 sq->sq_needexcl--; 7391 if (sq->sq_needexcl == 0) { 7392 SQ_PUTCOUNT_SETFAST_LOCKED(sq); 7393 } 7394 SQ_PUTLOCKS_EXIT(sq); 7395 } 7396 mutex_exit(SQLOCK(sq)); 7397 return; 7398 } 7399 sq->sq_flags |= SQ_WANTWAKEUP; 7400 if (!(type & SQ_CICB)) { 7401 SQ_PUTLOCKS_EXIT(sq); 7402 } 7403 cv_wait(&sq->sq_wait, SQLOCK(sq)); 7404 if (!(type & SQ_CICB)) { 7405 count = sq->sq_count; 7406 SQ_PUTLOCKS_ENTER(sq); 7407 SUM_SQ_PUTCOUNTS(sq, count); 7408 } 7409 } 7410 7411 sq->sq_count++; 7412 ASSERT(sq->sq_count != 0); /* Wraparound */ 7413 if (!(type & SQ_CICB)) { 7414 ASSERT(count == 0); 7415 sq->sq_flags |= SQ_EXCL; 7416 ASSERT(sq->sq_needexcl > 0); 7417 sq->sq_needexcl--; 7418 if (sq->sq_needexcl == 0) { 7419 SQ_PUTCOUNT_SETFAST_LOCKED(sq); 7420 } 7421 SQ_PUTLOCKS_EXIT(sq); 7422 } 7423 7424 mutex_exit(SQLOCK(sq)); 7425 7426 cbp->cbp_func(cbp->cbp_arg); 7427 7428 /* 7429 * We drop the lock only for leavesq to re-acquire it. 7430 * Possible optimization is inline of leavesq. 7431 */ 7432 mutex_enter(SQLOCK(sq)); 7433 callbparams_free(sq, cbp); 7434 mutex_exit(SQLOCK(sq)); 7435 leavesq(sq, SQ_CALLBACK); 7436 } 7437 7438 /* 7439 * no need to grab sq_putlocks here. See comment in strsubr.h that 7440 * explains when sq_putlocks are used. 7441 * 7442 * sq_count (or one of the sq_putcounts) has already been 7443 * decremented by the caller, and if SQ_QUEUED, we need to call 7444 * drain_syncq (the global syncq drain). 7445 * If putnext_tail is called with the SQ_EXCL bit set, we are in 7446 * one of two states, non-CIPUT perimiter, and we need to clear 7447 * it, or we went exclusive in the put procedure. In any case, 7448 * we want to clear the bit now, and it is probably easier to do 7449 * this at the beginning of this function (remember, we hold 7450 * the SQLOCK). Lastly, if there are other messages queued 7451 * on the syncq (and not for our destination), enable the syncq 7452 * for background work. 7453 */ 7454 7455 /* ARGSUSED */ 7456 void 7457 putnext_tail(syncq_t *sq, queue_t *qp, uint32_t passflags) 7458 { 7459 uint16_t flags = sq->sq_flags; 7460 7461 ASSERT(MUTEX_HELD(SQLOCK(sq))); 7462 ASSERT(MUTEX_NOT_HELD(QLOCK(qp))); 7463 7464 /* Clear SQ_EXCL if set in passflags */ 7465 if (passflags & SQ_EXCL) { 7466 flags &= ~SQ_EXCL; 7467 } 7468 if (flags & SQ_WANTWAKEUP) { 7469 flags &= ~SQ_WANTWAKEUP; 7470 cv_broadcast(&sq->sq_wait); 7471 } 7472 if (flags & SQ_WANTEXWAKEUP) { 7473 flags &= ~SQ_WANTEXWAKEUP; 7474 cv_broadcast(&sq->sq_exitwait); 7475 } 7476 sq->sq_flags = flags; 7477 7478 /* 7479 * We have cleared SQ_EXCL if we were asked to, and started 7480 * the wakeup process for waiters. If there are no writers 7481 * then we need to drain the syncq if we were told to, or 7482 * enable the background thread to do it. 7483 */ 7484 if (!(flags & (SQ_STAYAWAY|SQ_EXCL))) { 7485 if ((passflags & SQ_QUEUED) || 7486 (sq->sq_svcflags & SQ_DISABLED)) { 7487 /* drain_syncq will take care of events in the list */ 7488 drain_syncq(sq); 7489 return; 7490 } else if (flags & SQ_QUEUED) { 7491 sqenable(sq); 7492 } 7493 } 7494 /* Drop the SQLOCK on exit */ 7495 mutex_exit(SQLOCK(sq)); 7496 TRACE_3(TR_FAC_STREAMS_FR, TR_PUTNEXT_END, 7497 "putnext_end:(%p, %p, %p) done", NULL, qp, sq); 7498 } 7499 7500 void 7501 set_qend(queue_t *q) 7502 { 7503 mutex_enter(QLOCK(q)); 7504 if (!O_SAMESTR(q)) 7505 q->q_flag |= QEND; 7506 else 7507 q->q_flag &= ~QEND; 7508 mutex_exit(QLOCK(q)); 7509 q = _OTHERQ(q); 7510 mutex_enter(QLOCK(q)); 7511 if (!O_SAMESTR(q)) 7512 q->q_flag |= QEND; 7513 else 7514 q->q_flag &= ~QEND; 7515 mutex_exit(QLOCK(q)); 7516 } 7517 7518 /* 7519 * Set QFULL in next service procedure queue (that cares) if not already 7520 * set and if there are already more messages on the syncq than 7521 * sq_max_size. If sq_max_size is 0, no flow control will be asserted on 7522 * any syncq. 7523 * 7524 * The fq here is the next queue with a service procedure. This is where 7525 * we would fail canputnext, so this is where we need to set QFULL. 7526 * In the case when fq != q we need to take QLOCK(fq) to set QFULL flag. 7527 * 7528 * We already have QLOCK at this point. To avoid cross-locks with 7529 * freezestr() which grabs all QLOCKs and with strlock() which grabs both 7530 * SQLOCK and sd_reflock, we need to drop respective locks first. 7531 */ 7532 void 7533 set_qfull(queue_t *q) 7534 { 7535 queue_t *fq = NULL; 7536 7537 ASSERT(MUTEX_HELD(QLOCK(q))); 7538 if ((sq_max_size != 0) && (!(q->q_nfsrv->q_flag & QFULL)) && 7539 (q->q_syncqmsgs > sq_max_size)) { 7540 if ((fq = q->q_nfsrv) == q) { 7541 fq->q_flag |= QFULL; 7542 } else { 7543 mutex_exit(QLOCK(q)); 7544 mutex_enter(QLOCK(fq)); 7545 fq->q_flag |= QFULL; 7546 mutex_exit(QLOCK(fq)); 7547 mutex_enter(QLOCK(q)); 7548 } 7549 } 7550 } 7551 7552 void 7553 clr_qfull(queue_t *q) 7554 { 7555 queue_t *oq = q; 7556 7557 q = q->q_nfsrv; 7558 /* Fast check if there is any work to do before getting the lock. */ 7559 if ((q->q_flag & (QFULL|QWANTW)) == 0) { 7560 return; 7561 } 7562 7563 /* 7564 * Do not reset QFULL (and backenable) if the q_count is the reason 7565 * for QFULL being set. 7566 */ 7567 mutex_enter(QLOCK(q)); 7568 /* 7569 * If queue is empty i.e q_mblkcnt is zero, queue can not be full. 7570 * Hence clear the QFULL. 7571 * If both q_count and q_mblkcnt are less than the hiwat mark, 7572 * clear the QFULL. 7573 */ 7574 if (q->q_mblkcnt == 0 || ((q->q_count < q->q_hiwat) && 7575 (q->q_mblkcnt < q->q_hiwat))) { 7576 q->q_flag &= ~QFULL; 7577 /* 7578 * A little more confusing, how about this way: 7579 * if someone wants to write, 7580 * AND 7581 * both counts are less than the lowat mark 7582 * OR 7583 * the lowat mark is zero 7584 * THEN 7585 * backenable 7586 */ 7587 if ((q->q_flag & QWANTW) && 7588 (((q->q_count < q->q_lowat) && 7589 (q->q_mblkcnt < q->q_lowat)) || q->q_lowat == 0)) { 7590 q->q_flag &= ~QWANTW; 7591 mutex_exit(QLOCK(q)); 7592 backenable(oq, 0); 7593 } else 7594 mutex_exit(QLOCK(q)); 7595 } else 7596 mutex_exit(QLOCK(q)); 7597 } 7598 7599 /* 7600 * Set the forward service procedure pointer. 7601 * 7602 * Called at insert-time to cache a queue's next forward service procedure in 7603 * q_nfsrv; used by canput() and canputnext(). If the queue to be inserted 7604 * has a service procedure then q_nfsrv points to itself. If the queue to be 7605 * inserted does not have a service procedure, then q_nfsrv points to the next 7606 * queue forward that has a service procedure. If the queue is at the logical 7607 * end of the stream (driver for write side, stream head for the read side) 7608 * and does not have a service procedure, then q_nfsrv also points to itself. 7609 */ 7610 void 7611 set_nfsrv_ptr( 7612 queue_t *rnew, /* read queue pointer to new module */ 7613 queue_t *wnew, /* write queue pointer to new module */ 7614 queue_t *prev_rq, /* read queue pointer to the module above */ 7615 queue_t *prev_wq) /* write queue pointer to the module above */ 7616 { 7617 queue_t *qp; 7618 7619 if (prev_wq->q_next == NULL) { 7620 /* 7621 * Insert the driver, initialize the driver and stream head. 7622 * In this case, prev_rq/prev_wq should be the stream head. 7623 * _I_INSERT does not allow inserting a driver. Make sure 7624 * that it is not an insertion. 7625 */ 7626 ASSERT(!(rnew->q_flag & _QINSERTING)); 7627 wnew->q_nfsrv = wnew; 7628 if (rnew->q_qinfo->qi_srvp) 7629 rnew->q_nfsrv = rnew; 7630 else 7631 rnew->q_nfsrv = prev_rq; 7632 prev_rq->q_nfsrv = prev_rq; 7633 prev_wq->q_nfsrv = prev_wq; 7634 } else { 7635 /* 7636 * set up read side q_nfsrv pointer. This MUST be done 7637 * before setting the write side, because the setting of 7638 * the write side for a fifo may depend on it. 7639 * 7640 * Suppose we have a fifo that only has pipemod pushed. 7641 * pipemod has no read or write service procedures, so 7642 * nfsrv for both pipemod queues points to prev_rq (the 7643 * stream read head). Now push bufmod (which has only a 7644 * read service procedure). Doing the write side first, 7645 * wnew->q_nfsrv is set to pipemod's writeq nfsrv, which 7646 * is WRONG; the next queue forward from wnew with a 7647 * service procedure will be rnew, not the stream read head. 7648 * Since the downstream queue (which in the case of a fifo 7649 * is the read queue rnew) can affect upstream queues, it 7650 * needs to be done first. Setting up the read side first 7651 * sets nfsrv for both pipemod queues to rnew and then 7652 * when the write side is set up, wnew-q_nfsrv will also 7653 * point to rnew. 7654 */ 7655 if (rnew->q_qinfo->qi_srvp) { 7656 /* 7657 * use _OTHERQ() because, if this is a pipe, next 7658 * module may have been pushed from other end and 7659 * q_next could be a read queue. 7660 */ 7661 qp = _OTHERQ(prev_wq->q_next); 7662 while (qp && qp->q_nfsrv != qp) { 7663 qp->q_nfsrv = rnew; 7664 qp = backq(qp); 7665 } 7666 rnew->q_nfsrv = rnew; 7667 } else 7668 rnew->q_nfsrv = prev_rq->q_nfsrv; 7669 7670 /* set up write side q_nfsrv pointer */ 7671 if (wnew->q_qinfo->qi_srvp) { 7672 wnew->q_nfsrv = wnew; 7673 7674 /* 7675 * For insertion, need to update nfsrv of the modules 7676 * above which do not have a service routine. 7677 */ 7678 if (rnew->q_flag & _QINSERTING) { 7679 for (qp = prev_wq; 7680 qp != NULL && qp->q_nfsrv != qp; 7681 qp = backq(qp)) { 7682 qp->q_nfsrv = wnew->q_nfsrv; 7683 } 7684 } 7685 } else { 7686 if (prev_wq->q_next == prev_rq) 7687 /* 7688 * Since prev_wq/prev_rq are the middle of a 7689 * fifo, wnew/rnew will also be the middle of 7690 * a fifo and wnew's nfsrv is same as rnew's. 7691 */ 7692 wnew->q_nfsrv = rnew->q_nfsrv; 7693 else 7694 wnew->q_nfsrv = prev_wq->q_next->q_nfsrv; 7695 } 7696 } 7697 } 7698 7699 /* 7700 * Reset the forward service procedure pointer; called at remove-time. 7701 */ 7702 void 7703 reset_nfsrv_ptr(queue_t *rqp, queue_t *wqp) 7704 { 7705 queue_t *tmp_qp; 7706 7707 /* Reset the write side q_nfsrv pointer for _I_REMOVE */ 7708 if ((rqp->q_flag & _QREMOVING) && (wqp->q_qinfo->qi_srvp != NULL)) { 7709 for (tmp_qp = backq(wqp); 7710 tmp_qp != NULL && tmp_qp->q_nfsrv == wqp; 7711 tmp_qp = backq(tmp_qp)) { 7712 tmp_qp->q_nfsrv = wqp->q_nfsrv; 7713 } 7714 } 7715 7716 /* reset the read side q_nfsrv pointer */ 7717 if (rqp->q_qinfo->qi_srvp) { 7718 if (wqp->q_next) { /* non-driver case */ 7719 tmp_qp = _OTHERQ(wqp->q_next); 7720 while (tmp_qp && tmp_qp->q_nfsrv == rqp) { 7721 /* Note that rqp->q_next cannot be NULL */ 7722 ASSERT(rqp->q_next != NULL); 7723 tmp_qp->q_nfsrv = rqp->q_next->q_nfsrv; 7724 tmp_qp = backq(tmp_qp); 7725 } 7726 } 7727 } 7728 } 7729 7730 /* 7731 * This routine should be called after all stream geometry changes to update 7732 * the stream head cached struio() rd/wr queue pointers. Note must be called 7733 * with the streamlock()ed. 7734 * 7735 * Note: only enables Synchronous STREAMS for a side of a Stream which has 7736 * an explicit synchronous barrier module queue. That is, a queue that 7737 * has specified a struio() type. 7738 */ 7739 static void 7740 strsetuio(stdata_t *stp) 7741 { 7742 queue_t *wrq; 7743 7744 if (stp->sd_flag & STPLEX) { 7745 /* 7746 * Not stremahead, but a mux, so no Synchronous STREAMS. 7747 */ 7748 stp->sd_struiowrq = NULL; 7749 stp->sd_struiordq = NULL; 7750 return; 7751 } 7752 /* 7753 * Scan the write queue(s) while synchronous 7754 * until we find a qinfo uio type specified. 7755 */ 7756 wrq = stp->sd_wrq->q_next; 7757 while (wrq) { 7758 if (wrq->q_struiot == STRUIOT_NONE) { 7759 wrq = 0; 7760 break; 7761 } 7762 if (wrq->q_struiot != STRUIOT_DONTCARE) 7763 break; 7764 if (! _SAMESTR(wrq)) { 7765 wrq = 0; 7766 break; 7767 } 7768 wrq = wrq->q_next; 7769 } 7770 stp->sd_struiowrq = wrq; 7771 /* 7772 * Scan the read queue(s) while synchronous 7773 * until we find a qinfo uio type specified. 7774 */ 7775 wrq = stp->sd_wrq->q_next; 7776 while (wrq) { 7777 if (_RD(wrq)->q_struiot == STRUIOT_NONE) { 7778 wrq = 0; 7779 break; 7780 } 7781 if (_RD(wrq)->q_struiot != STRUIOT_DONTCARE) 7782 break; 7783 if (! _SAMESTR(wrq)) { 7784 wrq = 0; 7785 break; 7786 } 7787 wrq = wrq->q_next; 7788 } 7789 stp->sd_struiordq = wrq ? _RD(wrq) : 0; 7790 } 7791 7792 /* 7793 * pass_wput, unblocks the passthru queues, so that 7794 * messages can arrive at muxs lower read queue, before 7795 * I_LINK/I_UNLINK is acked/nacked. 7796 */ 7797 static void 7798 pass_wput(queue_t *q, mblk_t *mp) 7799 { 7800 syncq_t *sq; 7801 7802 sq = _RD(q)->q_syncq; 7803 if (sq->sq_flags & SQ_BLOCKED) 7804 unblocksq(sq, SQ_BLOCKED, 0); 7805 putnext(q, mp); 7806 } 7807 7808 /* 7809 * Set up queues for the link/unlink. 7810 * Create a new queue and block it and then insert it 7811 * below the stream head on the lower stream. 7812 * This prevents any messages from arriving during the setq 7813 * as well as while the mux is processing the LINK/I_UNLINK. 7814 * The blocked passq is unblocked once the LINK/I_UNLINK has 7815 * been acked or nacked or if a message is generated and sent 7816 * down muxs write put procedure. 7817 * see pass_wput(). 7818 * 7819 * After the new queue is inserted, all messages coming from below are 7820 * blocked. The call to strlock will ensure that all activity in the stream head 7821 * read queue syncq is stopped (sq_count drops to zero). 7822 */ 7823 static queue_t * 7824 link_addpassthru(stdata_t *stpdown) 7825 { 7826 queue_t *passq; 7827 sqlist_t sqlist; 7828 7829 passq = allocq(); 7830 STREAM(passq) = STREAM(_WR(passq)) = stpdown; 7831 /* setq might sleep in allocator - avoid holding locks. */ 7832 setq(passq, &passthru_rinit, &passthru_winit, NULL, QPERQ, 7833 SQ_CI|SQ_CO, B_FALSE); 7834 claimq(passq); 7835 blocksq(passq->q_syncq, SQ_BLOCKED, 1); 7836 insertq(STREAM(passq), passq); 7837 7838 /* 7839 * Use strlock() to wait for the stream head sq_count to drop to zero 7840 * since we are going to change q_ptr in the stream head. Note that 7841 * insertq() doesn't wait for any syncq counts to drop to zero. 7842 */ 7843 sqlist.sqlist_head = NULL; 7844 sqlist.sqlist_index = 0; 7845 sqlist.sqlist_size = sizeof (sqlist_t); 7846 sqlist_insert(&sqlist, _RD(stpdown->sd_wrq)->q_syncq); 7847 strlock(stpdown, &sqlist); 7848 strunlock(stpdown, &sqlist); 7849 7850 releaseq(passq); 7851 return (passq); 7852 } 7853 7854 /* 7855 * Let messages flow up into the mux by removing 7856 * the passq. 7857 */ 7858 static void 7859 link_rempassthru(queue_t *passq) 7860 { 7861 claimq(passq); 7862 removeq(passq); 7863 releaseq(passq); 7864 freeq(passq); 7865 } 7866 7867 /* 7868 * Wait for the condition variable pointed to by `cvp' to be signaled, 7869 * or for `tim' milliseconds to elapse, whichever comes first. If `tim' 7870 * is negative, then there is no time limit. If `nosigs' is non-zero, 7871 * then the wait will be non-interruptible. 7872 * 7873 * Returns >0 if signaled, 0 if interrupted, or -1 upon timeout. 7874 */ 7875 clock_t 7876 str_cv_wait(kcondvar_t *cvp, kmutex_t *mp, clock_t tim, int nosigs) 7877 { 7878 clock_t ret, now, tick; 7879 7880 if (tim < 0) { 7881 if (nosigs) { 7882 cv_wait(cvp, mp); 7883 ret = 1; 7884 } else { 7885 ret = cv_wait_sig(cvp, mp); 7886 } 7887 } else if (tim > 0) { 7888 /* 7889 * convert milliseconds to clock ticks 7890 */ 7891 tick = MSEC_TO_TICK_ROUNDUP(tim); 7892 time_to_wait(&now, tick); 7893 if (nosigs) { 7894 ret = cv_timedwait(cvp, mp, now); 7895 } else { 7896 ret = cv_timedwait_sig(cvp, mp, now); 7897 } 7898 } else { 7899 ret = -1; 7900 } 7901 return (ret); 7902 } 7903 7904 /* 7905 * Wait until the stream head can determine if it is at the mark but 7906 * don't wait forever to prevent a race condition between the "mark" state 7907 * in the stream head and any mark state in the caller/user of this routine. 7908 * 7909 * This is used by sockets and for a socket it would be incorrect 7910 * to return a failure for SIOCATMARK when there is no data in the receive 7911 * queue and the marked urgent data is traveling up the stream. 7912 * 7913 * This routine waits until the mark is known by waiting for one of these 7914 * three events: 7915 * The stream head read queue becoming non-empty (including an EOF) 7916 * The STRATMARK flag being set. (Due to a MSGMARKNEXT message.) 7917 * The STRNOTATMARK flag being set (which indicates that the transport 7918 * has sent a MSGNOTMARKNEXT message to indicate that it is not at 7919 * the mark). 7920 * 7921 * The routine returns 1 if the stream is at the mark; 0 if it can 7922 * be determined that the stream is not at the mark. 7923 * If the wait times out and it can't determine 7924 * whether or not the stream might be at the mark the routine will return -1. 7925 * 7926 * Note: This routine should only be used when a mark is pending i.e., 7927 * in the socket case the SIGURG has been posted. 7928 * Note2: This can not wakeup just because synchronous streams indicate 7929 * that data is available since it is not possible to use the synchronous 7930 * streams interfaces to determine the b_flag value for the data queued below 7931 * the stream head. 7932 */ 7933 int 7934 strwaitmark(vnode_t *vp) 7935 { 7936 struct stdata *stp = vp->v_stream; 7937 queue_t *rq = _RD(stp->sd_wrq); 7938 int mark; 7939 7940 mutex_enter(&stp->sd_lock); 7941 while (rq->q_first == NULL && 7942 !(stp->sd_flag & (STRATMARK|STRNOTATMARK|STREOF))) { 7943 stp->sd_flag |= RSLEEP; 7944 7945 /* Wait for 100 milliseconds for any state change. */ 7946 if (str_cv_wait(&rq->q_wait, &stp->sd_lock, 100, 1) == -1) { 7947 mutex_exit(&stp->sd_lock); 7948 return (-1); 7949 } 7950 } 7951 if (stp->sd_flag & STRATMARK) 7952 mark = 1; 7953 else if (rq->q_first != NULL && (rq->q_first->b_flag & MSGMARK)) 7954 mark = 1; 7955 else 7956 mark = 0; 7957 7958 mutex_exit(&stp->sd_lock); 7959 return (mark); 7960 } 7961 7962 /* 7963 * Set a read side error. If persist is set change the socket error 7964 * to persistent. If errfunc is set install the function as the exported 7965 * error handler. 7966 */ 7967 void 7968 strsetrerror(vnode_t *vp, int error, int persist, errfunc_t errfunc) 7969 { 7970 struct stdata *stp = vp->v_stream; 7971 7972 mutex_enter(&stp->sd_lock); 7973 stp->sd_rerror = error; 7974 if (error == 0 && errfunc == NULL) 7975 stp->sd_flag &= ~STRDERR; 7976 else 7977 stp->sd_flag |= STRDERR; 7978 if (persist) { 7979 stp->sd_flag &= ~STRDERRNONPERSIST; 7980 } else { 7981 stp->sd_flag |= STRDERRNONPERSIST; 7982 } 7983 stp->sd_rderrfunc = errfunc; 7984 if (error != 0 || errfunc != NULL) { 7985 cv_broadcast(&_RD(stp->sd_wrq)->q_wait); /* readers */ 7986 cv_broadcast(&stp->sd_wrq->q_wait); /* writers */ 7987 cv_broadcast(&stp->sd_monitor); /* ioctllers */ 7988 7989 mutex_exit(&stp->sd_lock); 7990 pollwakeup(&stp->sd_pollist, POLLERR); 7991 mutex_enter(&stp->sd_lock); 7992 7993 if (stp->sd_sigflags & S_ERROR) 7994 strsendsig(stp->sd_siglist, S_ERROR, 0, error); 7995 } 7996 mutex_exit(&stp->sd_lock); 7997 } 7998 7999 /* 8000 * Set a write side error. If persist is set change the socket error 8001 * to persistent. 8002 */ 8003 void 8004 strsetwerror(vnode_t *vp, int error, int persist, errfunc_t errfunc) 8005 { 8006 struct stdata *stp = vp->v_stream; 8007 8008 mutex_enter(&stp->sd_lock); 8009 stp->sd_werror = error; 8010 if (error == 0 && errfunc == NULL) 8011 stp->sd_flag &= ~STWRERR; 8012 else 8013 stp->sd_flag |= STWRERR; 8014 if (persist) { 8015 stp->sd_flag &= ~STWRERRNONPERSIST; 8016 } else { 8017 stp->sd_flag |= STWRERRNONPERSIST; 8018 } 8019 stp->sd_wrerrfunc = errfunc; 8020 if (error != 0 || errfunc != NULL) { 8021 cv_broadcast(&_RD(stp->sd_wrq)->q_wait); /* readers */ 8022 cv_broadcast(&stp->sd_wrq->q_wait); /* writers */ 8023 cv_broadcast(&stp->sd_monitor); /* ioctllers */ 8024 8025 mutex_exit(&stp->sd_lock); 8026 pollwakeup(&stp->sd_pollist, POLLERR); 8027 mutex_enter(&stp->sd_lock); 8028 8029 if (stp->sd_sigflags & S_ERROR) 8030 strsendsig(stp->sd_siglist, S_ERROR, 0, error); 8031 } 8032 mutex_exit(&stp->sd_lock); 8033 } 8034 8035 /* 8036 * Make the stream return 0 (EOF) when all data has been read. 8037 * No effect on write side. 8038 */ 8039 void 8040 strseteof(vnode_t *vp, int eof) 8041 { 8042 struct stdata *stp = vp->v_stream; 8043 8044 mutex_enter(&stp->sd_lock); 8045 if (!eof) { 8046 stp->sd_flag &= ~STREOF; 8047 mutex_exit(&stp->sd_lock); 8048 return; 8049 } 8050 stp->sd_flag |= STREOF; 8051 if (stp->sd_flag & RSLEEP) { 8052 stp->sd_flag &= ~RSLEEP; 8053 cv_broadcast(&_RD(stp->sd_wrq)->q_wait); 8054 } 8055 8056 mutex_exit(&stp->sd_lock); 8057 pollwakeup(&stp->sd_pollist, POLLIN|POLLRDNORM); 8058 mutex_enter(&stp->sd_lock); 8059 8060 if (stp->sd_sigflags & (S_INPUT|S_RDNORM)) 8061 strsendsig(stp->sd_siglist, S_INPUT|S_RDNORM, 0, 0); 8062 mutex_exit(&stp->sd_lock); 8063 } 8064 8065 void 8066 strflushrq(vnode_t *vp, int flag) 8067 { 8068 struct stdata *stp = vp->v_stream; 8069 8070 mutex_enter(&stp->sd_lock); 8071 flushq(_RD(stp->sd_wrq), flag); 8072 mutex_exit(&stp->sd_lock); 8073 } 8074 8075 void 8076 strsetrputhooks(vnode_t *vp, uint_t flags, 8077 msgfunc_t protofunc, msgfunc_t miscfunc) 8078 { 8079 struct stdata *stp = vp->v_stream; 8080 8081 mutex_enter(&stp->sd_lock); 8082 8083 if (protofunc == NULL) 8084 stp->sd_rprotofunc = strrput_proto; 8085 else 8086 stp->sd_rprotofunc = protofunc; 8087 8088 if (miscfunc == NULL) 8089 stp->sd_rmiscfunc = strrput_misc; 8090 else 8091 stp->sd_rmiscfunc = miscfunc; 8092 8093 if (flags & SH_CONSOL_DATA) 8094 stp->sd_rput_opt |= SR_CONSOL_DATA; 8095 else 8096 stp->sd_rput_opt &= ~SR_CONSOL_DATA; 8097 8098 if (flags & SH_SIGALLDATA) 8099 stp->sd_rput_opt |= SR_SIGALLDATA; 8100 else 8101 stp->sd_rput_opt &= ~SR_SIGALLDATA; 8102 8103 if (flags & SH_IGN_ZEROLEN) 8104 stp->sd_rput_opt |= SR_IGN_ZEROLEN; 8105 else 8106 stp->sd_rput_opt &= ~SR_IGN_ZEROLEN; 8107 8108 mutex_exit(&stp->sd_lock); 8109 } 8110 8111 void 8112 strsetwputhooks(vnode_t *vp, uint_t flags, clock_t closetime) 8113 { 8114 struct stdata *stp = vp->v_stream; 8115 8116 mutex_enter(&stp->sd_lock); 8117 stp->sd_closetime = closetime; 8118 8119 if (flags & SH_SIGPIPE) 8120 stp->sd_wput_opt |= SW_SIGPIPE; 8121 else 8122 stp->sd_wput_opt &= ~SW_SIGPIPE; 8123 if (flags & SH_RECHECK_ERR) 8124 stp->sd_wput_opt |= SW_RECHECK_ERR; 8125 else 8126 stp->sd_wput_opt &= ~SW_RECHECK_ERR; 8127 8128 mutex_exit(&stp->sd_lock); 8129 } 8130 8131 void 8132 strsetrwputdatahooks(vnode_t *vp, msgfunc_t rdatafunc, msgfunc_t wdatafunc) 8133 { 8134 struct stdata *stp = vp->v_stream; 8135 8136 mutex_enter(&stp->sd_lock); 8137 8138 stp->sd_rputdatafunc = rdatafunc; 8139 stp->sd_wputdatafunc = wdatafunc; 8140 8141 mutex_exit(&stp->sd_lock); 8142 } 8143 8144 /* Used within framework when the queue is already locked */ 8145 void 8146 qenable_locked(queue_t *q) 8147 { 8148 stdata_t *stp = STREAM(q); 8149 8150 ASSERT(MUTEX_HELD(QLOCK(q))); 8151 8152 if (!q->q_qinfo->qi_srvp) 8153 return; 8154 8155 /* 8156 * Do not place on run queue if already enabled or closing. 8157 */ 8158 if (q->q_flag & (QWCLOSE|QENAB)) 8159 return; 8160 8161 /* 8162 * mark queue enabled and place on run list if it is not already being 8163 * serviced. If it is serviced, the runservice() function will detect 8164 * that QENAB is set and call service procedure before clearing 8165 * QINSERVICE flag. 8166 */ 8167 q->q_flag |= QENAB; 8168 if (q->q_flag & QINSERVICE) 8169 return; 8170 8171 /* Record the time of qenable */ 8172 q->q_qtstamp = lbolt; 8173 8174 /* 8175 * Put the queue in the stp list and schedule it for background 8176 * processing if it is not already scheduled or if stream head does not 8177 * intent to process it in the foreground later by setting 8178 * STRS_WILLSERVICE flag. 8179 */ 8180 mutex_enter(&stp->sd_qlock); 8181 /* 8182 * If there are already something on the list, stp flags should show 8183 * intention to drain it. 8184 */ 8185 IMPLY(STREAM_NEEDSERVICE(stp), 8186 (stp->sd_svcflags & (STRS_WILLSERVICE | STRS_SCHEDULED))); 8187 8188 ENQUEUE(q, stp->sd_qhead, stp->sd_qtail, q_link); 8189 stp->sd_nqueues++; 8190 8191 /* 8192 * If no one will drain this stream we are the first producer and 8193 * need to schedule it for background thread. 8194 */ 8195 if (!(stp->sd_svcflags & (STRS_WILLSERVICE | STRS_SCHEDULED))) { 8196 /* 8197 * No one will service this stream later, so we have to 8198 * schedule it now. 8199 */ 8200 STRSTAT(stenables); 8201 stp->sd_svcflags |= STRS_SCHEDULED; 8202 stp->sd_servid = (void *)taskq_dispatch(streams_taskq, 8203 (task_func_t *)stream_service, stp, TQ_NOSLEEP|TQ_NOQUEUE); 8204 8205 if (stp->sd_servid == NULL) { 8206 /* 8207 * Task queue failed so fail over to the backup 8208 * servicing thread. 8209 */ 8210 STRSTAT(taskqfails); 8211 /* 8212 * It is safe to clear STRS_SCHEDULED flag because it 8213 * was set by this thread above. 8214 */ 8215 stp->sd_svcflags &= ~STRS_SCHEDULED; 8216 8217 /* 8218 * Failover scheduling is protected by service_queue 8219 * lock. 8220 */ 8221 mutex_enter(&service_queue); 8222 ASSERT((stp->sd_qhead == q) && (stp->sd_qtail == q)); 8223 ASSERT(q->q_link == NULL); 8224 /* 8225 * Append the queue to qhead/qtail list. 8226 */ 8227 if (qhead == NULL) 8228 qhead = q; 8229 else 8230 qtail->q_link = q; 8231 qtail = q; 8232 /* 8233 * Clear stp queue list. 8234 */ 8235 stp->sd_qhead = stp->sd_qtail = NULL; 8236 stp->sd_nqueues = 0; 8237 /* 8238 * Wakeup background queue processing thread. 8239 */ 8240 cv_signal(&services_to_run); 8241 mutex_exit(&service_queue); 8242 } 8243 } 8244 mutex_exit(&stp->sd_qlock); 8245 } 8246 8247 static void 8248 queue_service(queue_t *q) 8249 { 8250 /* 8251 * The queue in the list should have 8252 * QENAB flag set and should not have 8253 * QINSERVICE flag set. QINSERVICE is 8254 * set when the queue is dequeued and 8255 * qenable_locked doesn't enqueue a 8256 * queue with QINSERVICE set. 8257 */ 8258 8259 ASSERT(!(q->q_flag & QINSERVICE)); 8260 ASSERT((q->q_flag & QENAB)); 8261 mutex_enter(QLOCK(q)); 8262 q->q_flag &= ~QENAB; 8263 q->q_flag |= QINSERVICE; 8264 mutex_exit(QLOCK(q)); 8265 runservice(q); 8266 } 8267 8268 static void 8269 syncq_service(syncq_t *sq) 8270 { 8271 STRSTAT(syncqservice); 8272 mutex_enter(SQLOCK(sq)); 8273 ASSERT(!(sq->sq_svcflags & SQ_SERVICE)); 8274 ASSERT(sq->sq_servcount != 0); 8275 ASSERT(sq->sq_next == NULL); 8276 8277 /* if we came here from the background thread, clear the flag */ 8278 if (sq->sq_svcflags & SQ_BGTHREAD) 8279 sq->sq_svcflags &= ~SQ_BGTHREAD; 8280 8281 /* let drain_syncq know that it's being called in the background */ 8282 sq->sq_svcflags |= SQ_SERVICE; 8283 drain_syncq(sq); 8284 } 8285 8286 static void 8287 qwriter_outer_service(syncq_t *outer) 8288 { 8289 /* 8290 * Note that SQ_WRITER is used on the outer perimeter 8291 * to signal that a qwriter(OUTER) is either investigating 8292 * running or that it is actually running a function. 8293 */ 8294 outer_enter(outer, SQ_BLOCKED|SQ_WRITER); 8295 8296 /* 8297 * All inner syncq are empty and have SQ_WRITER set 8298 * to block entering the outer perimeter. 8299 * 8300 * We do not need to explicitly call write_now since 8301 * outer_exit does it for us. 8302 */ 8303 outer_exit(outer); 8304 } 8305 8306 static void 8307 mblk_free(mblk_t *mp) 8308 { 8309 dblk_t *dbp = mp->b_datap; 8310 frtn_t *frp = dbp->db_frtnp; 8311 8312 mp->b_next = NULL; 8313 if (dbp->db_fthdr != NULL) 8314 str_ftfree(dbp); 8315 8316 ASSERT(dbp->db_fthdr == NULL); 8317 frp->free_func(frp->free_arg); 8318 ASSERT(dbp->db_mblk == mp); 8319 8320 if (dbp->db_credp != NULL) { 8321 crfree(dbp->db_credp); 8322 dbp->db_credp = NULL; 8323 } 8324 dbp->db_cpid = -1; 8325 dbp->db_struioflag = 0; 8326 dbp->db_struioun.cksum.flags = 0; 8327 8328 kmem_cache_free(dbp->db_cache, dbp); 8329 } 8330 8331 /* 8332 * Background processing of the stream queue list. 8333 */ 8334 static void 8335 stream_service(stdata_t *stp) 8336 { 8337 queue_t *q; 8338 8339 mutex_enter(&stp->sd_qlock); 8340 8341 STR_SERVICE(stp, q); 8342 8343 stp->sd_svcflags &= ~STRS_SCHEDULED; 8344 stp->sd_servid = NULL; 8345 cv_signal(&stp->sd_qcv); 8346 mutex_exit(&stp->sd_qlock); 8347 } 8348 8349 /* 8350 * Foreground processing of the stream queue list. 8351 */ 8352 void 8353 stream_runservice(stdata_t *stp) 8354 { 8355 queue_t *q; 8356 8357 mutex_enter(&stp->sd_qlock); 8358 STRSTAT(rservice); 8359 /* 8360 * We are going to drain this stream queue list, so qenable_locked will 8361 * not schedule it until we finish. 8362 */ 8363 stp->sd_svcflags |= STRS_WILLSERVICE; 8364 8365 STR_SERVICE(stp, q); 8366 8367 stp->sd_svcflags &= ~STRS_WILLSERVICE; 8368 mutex_exit(&stp->sd_qlock); 8369 /* 8370 * Help backup background thread to drain the qhead/qtail list. 8371 */ 8372 while (qhead != NULL) { 8373 STRSTAT(qhelps); 8374 mutex_enter(&service_queue); 8375 DQ(q, qhead, qtail, q_link); 8376 mutex_exit(&service_queue); 8377 if (q != NULL) 8378 queue_service(q); 8379 } 8380 } 8381 8382 void 8383 stream_willservice(stdata_t *stp) 8384 { 8385 mutex_enter(&stp->sd_qlock); 8386 stp->sd_svcflags |= STRS_WILLSERVICE; 8387 mutex_exit(&stp->sd_qlock); 8388 } 8389 8390 /* 8391 * Replace the cred currently in the mblk with a different one. 8392 */ 8393 void 8394 mblk_setcred(mblk_t *mp, cred_t *cr) 8395 { 8396 cred_t *ocr = DB_CRED(mp); 8397 8398 ASSERT(cr != NULL); 8399 8400 if (cr != ocr) { 8401 crhold(mp->b_datap->db_credp = cr); 8402 if (ocr != NULL) 8403 crfree(ocr); 8404 } 8405 } 8406 8407 int 8408 hcksum_assoc(mblk_t *mp, multidata_t *mmd, pdesc_t *pd, 8409 uint32_t start, uint32_t stuff, uint32_t end, uint32_t value, 8410 uint32_t flags, int km_flags) 8411 { 8412 int rc = 0; 8413 8414 ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_MULTIDATA); 8415 if (mp->b_datap->db_type == M_DATA) { 8416 /* Associate values for M_DATA type */ 8417 DB_CKSUMSTART(mp) = (intptr_t)start; 8418 DB_CKSUMSTUFF(mp) = (intptr_t)stuff; 8419 DB_CKSUMEND(mp) = (intptr_t)end; 8420 DB_CKSUMFLAGS(mp) = flags; 8421 DB_CKSUM16(mp) = (uint16_t)value; 8422 8423 } else { 8424 pattrinfo_t pa_info; 8425 8426 ASSERT(mmd != NULL); 8427 8428 pa_info.type = PATTR_HCKSUM; 8429 pa_info.len = sizeof (pattr_hcksum_t); 8430 8431 if (mmd_addpattr(mmd, pd, &pa_info, B_TRUE, km_flags) != NULL) { 8432 pattr_hcksum_t *hck = (pattr_hcksum_t *)pa_info.buf; 8433 8434 hck->hcksum_start_offset = start; 8435 hck->hcksum_stuff_offset = stuff; 8436 hck->hcksum_end_offset = end; 8437 hck->hcksum_cksum_val.inet_cksum = (uint16_t)value; 8438 hck->hcksum_flags = flags; 8439 } else { 8440 rc = -1; 8441 } 8442 } 8443 return (rc); 8444 } 8445 8446 void 8447 hcksum_retrieve(mblk_t *mp, multidata_t *mmd, pdesc_t *pd, 8448 uint32_t *start, uint32_t *stuff, uint32_t *end, 8449 uint32_t *value, uint32_t *flags) 8450 { 8451 ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_MULTIDATA); 8452 if (mp->b_datap->db_type == M_DATA) { 8453 if (flags != NULL) { 8454 *flags = DB_CKSUMFLAGS(mp); 8455 if (*flags & HCK_PARTIALCKSUM) { 8456 if (start != NULL) 8457 *start = (uint32_t)DB_CKSUMSTART(mp); 8458 if (stuff != NULL) 8459 *stuff = (uint32_t)DB_CKSUMSTUFF(mp); 8460 if (end != NULL) 8461 *end = (uint32_t)DB_CKSUMEND(mp); 8462 if (value != NULL) 8463 *value = (uint32_t)DB_CKSUM16(mp); 8464 } else if ((*flags & HW_LSO) && (value != NULL)) 8465 *value = (uint32_t)DB_LSOMSS(mp); 8466 } 8467 } else { 8468 pattrinfo_t hck_attr = {PATTR_HCKSUM}; 8469 8470 ASSERT(mmd != NULL); 8471 8472 /* get hardware checksum attribute */ 8473 if (mmd_getpattr(mmd, pd, &hck_attr) != NULL) { 8474 pattr_hcksum_t *hck = (pattr_hcksum_t *)hck_attr.buf; 8475 8476 ASSERT(hck_attr.len >= sizeof (pattr_hcksum_t)); 8477 if (flags != NULL) 8478 *flags = hck->hcksum_flags; 8479 if (start != NULL) 8480 *start = hck->hcksum_start_offset; 8481 if (stuff != NULL) 8482 *stuff = hck->hcksum_stuff_offset; 8483 if (end != NULL) 8484 *end = hck->hcksum_end_offset; 8485 if (value != NULL) 8486 *value = (uint32_t) 8487 hck->hcksum_cksum_val.inet_cksum; 8488 } 8489 } 8490 } 8491 8492 /* 8493 * Checksum buffer *bp for len bytes with psum partial checksum, 8494 * or 0 if none, and return the 16 bit partial checksum. 8495 */ 8496 unsigned 8497 bcksum(uchar_t *bp, int len, unsigned int psum) 8498 { 8499 int odd = len & 1; 8500 extern unsigned int ip_ocsum(); 8501 8502 if (((intptr_t)bp & 1) == 0 && !odd) { 8503 /* 8504 * Bp is 16 bit aligned and len is multiple of 16 bit word. 8505 */ 8506 return (ip_ocsum((ushort_t *)bp, len >> 1, psum)); 8507 } 8508 if (((intptr_t)bp & 1) != 0) { 8509 /* 8510 * Bp isn't 16 bit aligned. 8511 */ 8512 unsigned int tsum; 8513 8514 #ifdef _LITTLE_ENDIAN 8515 psum += *bp; 8516 #else 8517 psum += *bp << 8; 8518 #endif 8519 len--; 8520 bp++; 8521 tsum = ip_ocsum((ushort_t *)bp, len >> 1, 0); 8522 psum += (tsum << 8) & 0xffff | (tsum >> 8); 8523 if (len & 1) { 8524 bp += len - 1; 8525 #ifdef _LITTLE_ENDIAN 8526 psum += *bp << 8; 8527 #else 8528 psum += *bp; 8529 #endif 8530 } 8531 } else { 8532 /* 8533 * Bp is 16 bit aligned. 8534 */ 8535 psum = ip_ocsum((ushort_t *)bp, len >> 1, psum); 8536 if (odd) { 8537 bp += len - 1; 8538 #ifdef _LITTLE_ENDIAN 8539 psum += *bp; 8540 #else 8541 psum += *bp << 8; 8542 #endif 8543 } 8544 } 8545 /* 8546 * Normalize psum to 16 bits before returning the new partial 8547 * checksum. The max psum value before normalization is 0x3FDFE. 8548 */ 8549 return ((psum >> 16) + (psum & 0xFFFF)); 8550 } 8551 8552 boolean_t 8553 is_vmloaned_mblk(mblk_t *mp, multidata_t *mmd, pdesc_t *pd) 8554 { 8555 boolean_t rc; 8556 8557 ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_MULTIDATA); 8558 if (DB_TYPE(mp) == M_DATA) { 8559 rc = (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0); 8560 } else { 8561 pattrinfo_t zcopy_attr = {PATTR_ZCOPY}; 8562 8563 ASSERT(mmd != NULL); 8564 rc = (mmd_getpattr(mmd, pd, &zcopy_attr) != NULL); 8565 } 8566 return (rc); 8567 } 8568 8569 void 8570 freemsgchain(mblk_t *mp) 8571 { 8572 mblk_t *next; 8573 8574 while (mp != NULL) { 8575 next = mp->b_next; 8576 mp->b_next = NULL; 8577 8578 freemsg(mp); 8579 mp = next; 8580 } 8581 } 8582 8583 mblk_t * 8584 copymsgchain(mblk_t *mp) 8585 { 8586 mblk_t *nmp = NULL; 8587 mblk_t **nmpp = &nmp; 8588 8589 for (; mp != NULL; mp = mp->b_next) { 8590 if ((*nmpp = copymsg(mp)) == NULL) { 8591 freemsgchain(nmp); 8592 return (NULL); 8593 } 8594 8595 nmpp = &((*nmpp)->b_next); 8596 } 8597 8598 return (nmp); 8599 } 8600 8601 /* NOTE: Do not add code after this point. */ 8602 #undef QLOCK 8603 8604 /* 8605 * replacement for QLOCK macro for those that can't use it. 8606 */ 8607 kmutex_t * 8608 QLOCK(queue_t *q) 8609 { 8610 return (&(q)->q_lock); 8611 } 8612 8613 /* 8614 * Dummy runqueues/queuerun functions functions for backwards compatibility. 8615 */ 8616 #undef runqueues 8617 void 8618 runqueues(void) 8619 { 8620 } 8621 8622 #undef queuerun 8623 void 8624 queuerun(void) 8625 { 8626 } 8627 8628 /* 8629 * Initialize the STR stack instance, which tracks autopush and persistent 8630 * links. 8631 */ 8632 /* ARGSUSED */ 8633 static void * 8634 str_stack_init(netstackid_t stackid, netstack_t *ns) 8635 { 8636 str_stack_t *ss; 8637 int i; 8638 8639 ss = (str_stack_t *)kmem_zalloc(sizeof (*ss), KM_SLEEP); 8640 ss->ss_netstack = ns; 8641 8642 /* 8643 * set up autopush 8644 */ 8645 sad_initspace(ss); 8646 8647 /* 8648 * set up mux_node structures. 8649 */ 8650 ss->ss_devcnt = devcnt; /* In case it should change before free */ 8651 ss->ss_mux_nodes = kmem_zalloc((sizeof (struct mux_node) * 8652 ss->ss_devcnt), KM_SLEEP); 8653 for (i = 0; i < ss->ss_devcnt; i++) 8654 ss->ss_mux_nodes[i].mn_imaj = i; 8655 return (ss); 8656 } 8657 8658 /* 8659 * Note: run at zone shutdown and not destroy so that the PLINKs are 8660 * gone by the time other cleanup happens from the destroy callbacks. 8661 */ 8662 static void 8663 str_stack_shutdown(netstackid_t stackid, void *arg) 8664 { 8665 str_stack_t *ss = (str_stack_t *)arg; 8666 int i; 8667 cred_t *cr; 8668 8669 cr = zone_get_kcred(netstackid_to_zoneid(stackid)); 8670 ASSERT(cr != NULL); 8671 8672 /* Undo all the I_PLINKs for this zone */ 8673 for (i = 0; i < ss->ss_devcnt; i++) { 8674 struct mux_edge *ep; 8675 ldi_handle_t lh; 8676 ldi_ident_t li; 8677 int ret; 8678 int rval; 8679 dev_t rdev; 8680 8681 ep = ss->ss_mux_nodes[i].mn_outp; 8682 if (ep == NULL) 8683 continue; 8684 ret = ldi_ident_from_major((major_t)i, &li); 8685 if (ret != 0) { 8686 continue; 8687 } 8688 rdev = ep->me_dev; 8689 ret = ldi_open_by_dev(&rdev, OTYP_CHR, FREAD|FWRITE, 8690 cr, &lh, li); 8691 if (ret != 0) { 8692 ldi_ident_release(li); 8693 continue; 8694 } 8695 8696 ret = ldi_ioctl(lh, I_PUNLINK, (intptr_t)MUXID_ALL, FKIOCTL, 8697 cr, &rval); 8698 if (ret) { 8699 (void) ldi_close(lh, FREAD|FWRITE, cr); 8700 ldi_ident_release(li); 8701 continue; 8702 } 8703 (void) ldi_close(lh, FREAD|FWRITE, cr); 8704 8705 /* Close layered handles */ 8706 ldi_ident_release(li); 8707 } 8708 crfree(cr); 8709 8710 sad_freespace(ss); 8711 8712 kmem_free(ss->ss_mux_nodes, sizeof (struct mux_node) * ss->ss_devcnt); 8713 ss->ss_mux_nodes = NULL; 8714 } 8715 8716 /* 8717 * Free the structure; str_stack_shutdown did the other cleanup work. 8718 */ 8719 /* ARGSUSED */ 8720 static void 8721 str_stack_fini(netstackid_t stackid, void *arg) 8722 { 8723 str_stack_t *ss = (str_stack_t *)arg; 8724 8725 kmem_free(ss, sizeof (*ss)); 8726 } 8727