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