10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
52497Sgeorges * Common Development and Distribution License (the "License").
62497Sgeorges * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
220Sstevel@tonic-gate /* All Rights Reserved */
230Sstevel@tonic-gate
240Sstevel@tonic-gate
250Sstevel@tonic-gate /*
2611469SRoamer@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
270Sstevel@tonic-gate * Use is subject to license terms.
280Sstevel@tonic-gate */
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/sysmacros.h>
320Sstevel@tonic-gate #include <sys/param.h>
330Sstevel@tonic-gate #include <sys/errno.h>
340Sstevel@tonic-gate #include <sys/signal.h>
350Sstevel@tonic-gate #include <sys/proc.h>
360Sstevel@tonic-gate #include <sys/conf.h>
370Sstevel@tonic-gate #include <sys/cred.h>
380Sstevel@tonic-gate #include <sys/user.h>
390Sstevel@tonic-gate #include <sys/vnode.h>
400Sstevel@tonic-gate #include <sys/file.h>
410Sstevel@tonic-gate #include <sys/session.h>
420Sstevel@tonic-gate #include <sys/stream.h>
430Sstevel@tonic-gate #include <sys/strsubr.h>
440Sstevel@tonic-gate #include <sys/stropts.h>
450Sstevel@tonic-gate #include <sys/poll.h>
460Sstevel@tonic-gate #include <sys/systm.h>
470Sstevel@tonic-gate #include <sys/cpuvar.h>
480Sstevel@tonic-gate #include <sys/uio.h>
490Sstevel@tonic-gate #include <sys/cmn_err.h>
500Sstevel@tonic-gate #include <sys/priocntl.h>
510Sstevel@tonic-gate #include <sys/procset.h>
520Sstevel@tonic-gate #include <sys/vmem.h>
530Sstevel@tonic-gate #include <sys/bitmap.h>
540Sstevel@tonic-gate #include <sys/kmem.h>
550Sstevel@tonic-gate #include <sys/siginfo.h>
560Sstevel@tonic-gate #include <sys/vtrace.h>
570Sstevel@tonic-gate #include <sys/callb.h>
580Sstevel@tonic-gate #include <sys/debug.h>
590Sstevel@tonic-gate #include <sys/modctl.h>
600Sstevel@tonic-gate #include <sys/vmsystm.h>
610Sstevel@tonic-gate #include <vm/page.h>
620Sstevel@tonic-gate #include <sys/atomic.h>
630Sstevel@tonic-gate #include <sys/suntpi.h>
640Sstevel@tonic-gate #include <sys/strlog.h>
650Sstevel@tonic-gate #include <sys/promif.h>
660Sstevel@tonic-gate #include <sys/project.h>
670Sstevel@tonic-gate #include <sys/vm.h>
680Sstevel@tonic-gate #include <sys/taskq.h>
690Sstevel@tonic-gate #include <sys/sunddi.h>
700Sstevel@tonic-gate #include <sys/sunldi_impl.h>
710Sstevel@tonic-gate #include <sys/strsun.h>
720Sstevel@tonic-gate #include <sys/isa_defs.h>
730Sstevel@tonic-gate #include <sys/multidata.h>
740Sstevel@tonic-gate #include <sys/pattr.h>
750Sstevel@tonic-gate #include <sys/strft.h>
76560Smeem #include <sys/fs/snode.h>
770Sstevel@tonic-gate #include <sys/zone.h>
783448Sdh155122 #include <sys/open.h>
793448Sdh155122 #include <sys/sunldi.h>
803448Sdh155122 #include <sys/sad.h>
813448Sdh155122 #include <sys/netstack.h>
820Sstevel@tonic-gate
830Sstevel@tonic-gate #define O_SAMESTR(q) (((q)->q_next) && \
840Sstevel@tonic-gate (((q)->q_flag & QREADR) == ((q)->q_next->q_flag & QREADR)))
850Sstevel@tonic-gate
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate * WARNING:
880Sstevel@tonic-gate * The variables and routines in this file are private, belonging
890Sstevel@tonic-gate * to the STREAMS subsystem. These should not be used by modules
900Sstevel@tonic-gate * or drivers. Compatibility will not be guaranteed.
910Sstevel@tonic-gate */
920Sstevel@tonic-gate
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate * Id value used to distinguish between different multiplexor links.
950Sstevel@tonic-gate */
960Sstevel@tonic-gate static int32_t lnk_id = 0;
970Sstevel@tonic-gate
980Sstevel@tonic-gate #define STREAMS_LOPRI MINCLSYSPRI
990Sstevel@tonic-gate static pri_t streams_lopri = STREAMS_LOPRI;
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate #define STRSTAT(x) (str_statistics.x.value.ui64++)
1020Sstevel@tonic-gate typedef struct str_stat {
1030Sstevel@tonic-gate kstat_named_t sqenables;
1040Sstevel@tonic-gate kstat_named_t stenables;
1050Sstevel@tonic-gate kstat_named_t syncqservice;
1060Sstevel@tonic-gate kstat_named_t freebs;
1070Sstevel@tonic-gate kstat_named_t qwr_outer;
1080Sstevel@tonic-gate kstat_named_t rservice;
1090Sstevel@tonic-gate kstat_named_t strwaits;
1100Sstevel@tonic-gate kstat_named_t taskqfails;
1110Sstevel@tonic-gate kstat_named_t bufcalls;
1120Sstevel@tonic-gate kstat_named_t qhelps;
1130Sstevel@tonic-gate kstat_named_t qremoved;
1140Sstevel@tonic-gate kstat_named_t sqremoved;
1150Sstevel@tonic-gate kstat_named_t bcwaits;
1160Sstevel@tonic-gate kstat_named_t sqtoomany;
1170Sstevel@tonic-gate } str_stat_t;
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate static str_stat_t str_statistics = {
1200Sstevel@tonic-gate { "sqenables", KSTAT_DATA_UINT64 },
1210Sstevel@tonic-gate { "stenables", KSTAT_DATA_UINT64 },
1220Sstevel@tonic-gate { "syncqservice", KSTAT_DATA_UINT64 },
1230Sstevel@tonic-gate { "freebs", KSTAT_DATA_UINT64 },
1240Sstevel@tonic-gate { "qwr_outer", KSTAT_DATA_UINT64 },
1250Sstevel@tonic-gate { "rservice", KSTAT_DATA_UINT64 },
1260Sstevel@tonic-gate { "strwaits", KSTAT_DATA_UINT64 },
1270Sstevel@tonic-gate { "taskqfails", KSTAT_DATA_UINT64 },
1280Sstevel@tonic-gate { "bufcalls", KSTAT_DATA_UINT64 },
1290Sstevel@tonic-gate { "qhelps", KSTAT_DATA_UINT64 },
1300Sstevel@tonic-gate { "qremoved", KSTAT_DATA_UINT64 },
1310Sstevel@tonic-gate { "sqremoved", KSTAT_DATA_UINT64 },
1320Sstevel@tonic-gate { "bcwaits", KSTAT_DATA_UINT64 },
1330Sstevel@tonic-gate { "sqtoomany", KSTAT_DATA_UINT64 },
1340Sstevel@tonic-gate };
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate static kstat_t *str_kstat;
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate /*
1390Sstevel@tonic-gate * qrunflag was used previously to control background scheduling of queues. It
1400Sstevel@tonic-gate * is not used anymore, but kept here in case some module still wants to access
1410Sstevel@tonic-gate * it via qready() and setqsched macros.
1420Sstevel@tonic-gate */
1430Sstevel@tonic-gate char qrunflag; /* Unused */
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate /*
1460Sstevel@tonic-gate * Most of the streams scheduling is done via task queues. Task queues may fail
1470Sstevel@tonic-gate * for non-sleep dispatches, so there are two backup threads servicing failed
1480Sstevel@tonic-gate * requests for queues and syncqs. Both of these threads also service failed
1490Sstevel@tonic-gate * dispatches freebs requests. Queues are put in the list specified by `qhead'
1500Sstevel@tonic-gate * and `qtail' pointers, syncqs use `sqhead' and `sqtail' pointers and freebs
1510Sstevel@tonic-gate * requests are put into `freebs_list' which has no tail pointer. All three
1520Sstevel@tonic-gate * lists are protected by a single `service_queue' lock and use
1530Sstevel@tonic-gate * `services_to_run' condition variable for signaling background threads. Use of
1540Sstevel@tonic-gate * a single lock should not be a problem because it is only used under heavy
1550Sstevel@tonic-gate * loads when task queues start to fail and at that time it may be a good idea
1560Sstevel@tonic-gate * to throttle scheduling requests.
1570Sstevel@tonic-gate *
1580Sstevel@tonic-gate * NOTE: queues and syncqs should be scheduled by two separate threads because
1590Sstevel@tonic-gate * queue servicing may be blocked waiting for a syncq which may be also
1600Sstevel@tonic-gate * scheduled for background execution. This may create a deadlock when only one
1610Sstevel@tonic-gate * thread is used for both.
1620Sstevel@tonic-gate */
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate static taskq_t *streams_taskq; /* Used for most STREAMS scheduling */
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate static kmutex_t service_queue; /* protects all of servicing vars */
1670Sstevel@tonic-gate static kcondvar_t services_to_run; /* wake up background service thread */
1680Sstevel@tonic-gate static kcondvar_t syncqs_to_run; /* wake up background service thread */
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate /*
1719671SBrian.Ruthven@Sun.COM * List of queues scheduled for background processing due to lack of resources
1720Sstevel@tonic-gate * in the task queues. Protected by service_queue lock;
1730Sstevel@tonic-gate */
1740Sstevel@tonic-gate static struct queue *qhead;
1750Sstevel@tonic-gate static struct queue *qtail;
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate /*
1780Sstevel@tonic-gate * Same list for syncqs
1790Sstevel@tonic-gate */
1800Sstevel@tonic-gate static syncq_t *sqhead;
1810Sstevel@tonic-gate static syncq_t *sqtail;
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate static mblk_t *freebs_list; /* list of buffers to free */
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate /*
1860Sstevel@tonic-gate * Backup threads for servicing queues and syncqs
1870Sstevel@tonic-gate */
1880Sstevel@tonic-gate kthread_t *streams_qbkgrnd_thread;
1890Sstevel@tonic-gate kthread_t *streams_sqbkgrnd_thread;
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate /*
1920Sstevel@tonic-gate * Bufcalls related variables.
1930Sstevel@tonic-gate */
1940Sstevel@tonic-gate struct bclist strbcalls; /* list of waiting bufcalls */
1950Sstevel@tonic-gate kmutex_t strbcall_lock; /* protects bufcall list (strbcalls) */
1960Sstevel@tonic-gate kcondvar_t strbcall_cv; /* Signaling when a bufcall is added */
1970Sstevel@tonic-gate kmutex_t bcall_monitor; /* sleep/wakeup style monitor */
1980Sstevel@tonic-gate kcondvar_t bcall_cv; /* wait 'till executing bufcall completes */
1990Sstevel@tonic-gate kthread_t *bc_bkgrnd_thread; /* Thread to service bufcall requests */
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate kmutex_t strresources; /* protects global resources */
2020Sstevel@tonic-gate kmutex_t muxifier; /* single-threads multiplexor creation */
2030Sstevel@tonic-gate
2043448Sdh155122 static void *str_stack_init(netstackid_t stackid, netstack_t *ns);
2053448Sdh155122 static void str_stack_shutdown(netstackid_t stackid, void *arg);
2063448Sdh155122 static void str_stack_fini(netstackid_t stackid, void *arg);
2073448Sdh155122
2080Sstevel@tonic-gate /*
2099671SBrian.Ruthven@Sun.COM * run_queues is no longer used, but is kept in case some 3rd party
2100Sstevel@tonic-gate * module/driver decides to use it.
2110Sstevel@tonic-gate */
2120Sstevel@tonic-gate int run_queues = 0;
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate /*
2150Sstevel@tonic-gate * sq_max_size is the depth of the syncq (in number of messages) before
2160Sstevel@tonic-gate * qfill_syncq() starts QFULL'ing destination queues. As its primary
2170Sstevel@tonic-gate * consumer - IP is no longer D_MTPERMOD, but there may be other
2180Sstevel@tonic-gate * modules/drivers depend on this syncq flow control, we prefer to
2190Sstevel@tonic-gate * choose a large number as the default value. For potential
2200Sstevel@tonic-gate * performance gain, this value is tunable in /etc/system.
2210Sstevel@tonic-gate */
2220Sstevel@tonic-gate int sq_max_size = 10000;
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate /*
2259671SBrian.Ruthven@Sun.COM * The number of ciputctrl structures per syncq and stream we create when
2260Sstevel@tonic-gate * needed.
2270Sstevel@tonic-gate */
2280Sstevel@tonic-gate int n_ciputctrl;
2290Sstevel@tonic-gate int max_n_ciputctrl = 16;
2300Sstevel@tonic-gate /*
2319671SBrian.Ruthven@Sun.COM * If n_ciputctrl is < min_n_ciputctrl don't even create ciputctrl_cache.
2320Sstevel@tonic-gate */
2330Sstevel@tonic-gate int min_n_ciputctrl = 2;
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate /*
2360Sstevel@tonic-gate * Per-driver/module syncqs
2370Sstevel@tonic-gate * ========================
2380Sstevel@tonic-gate *
2390Sstevel@tonic-gate * For drivers/modules that use PERMOD or outer syncqs we keep a list of
2400Sstevel@tonic-gate * perdm structures, new entries being added (and new syncqs allocated) when
2410Sstevel@tonic-gate * setq() encounters a module/driver with a streamtab that it hasn't seen
2420Sstevel@tonic-gate * before.
2430Sstevel@tonic-gate * The reason for this mechanism is that some modules and drivers share a
2440Sstevel@tonic-gate * common streamtab and it is necessary for those modules and drivers to also
2450Sstevel@tonic-gate * share a common PERMOD syncq.
2460Sstevel@tonic-gate *
2470Sstevel@tonic-gate * perdm_list --> dm_str == streamtab_1
2480Sstevel@tonic-gate * dm_sq == syncq_1
2490Sstevel@tonic-gate * dm_ref
2500Sstevel@tonic-gate * dm_next --> dm_str == streamtab_2
2510Sstevel@tonic-gate * dm_sq == syncq_2
2520Sstevel@tonic-gate * dm_ref
2530Sstevel@tonic-gate * dm_next --> ... NULL
2540Sstevel@tonic-gate *
2550Sstevel@tonic-gate * The dm_ref field is incremented for each new driver/module that takes
2560Sstevel@tonic-gate * a reference to the perdm structure and hence shares the syncq.
2570Sstevel@tonic-gate * References are held in the fmodsw_impl_t structure for each STREAMS module
2580Sstevel@tonic-gate * or the dev_impl array (indexed by device major number) for each driver.
2590Sstevel@tonic-gate *
2600Sstevel@tonic-gate * perdm_list -> [dm_ref == 1] -> [dm_ref == 2] -> [dm_ref == 1] -> NULL
2610Sstevel@tonic-gate * ^ ^ ^ ^
2620Sstevel@tonic-gate * | ______________/ | |
2630Sstevel@tonic-gate * | / | |
2640Sstevel@tonic-gate * dev_impl: ...|x|y|... module A module B
2650Sstevel@tonic-gate *
2660Sstevel@tonic-gate * When a module/driver is unloaded the reference count is decremented and,
2670Sstevel@tonic-gate * when it falls to zero, the perdm structure is removed from the list and
2680Sstevel@tonic-gate * the syncq is freed (see rele_dm()).
2690Sstevel@tonic-gate */
2700Sstevel@tonic-gate perdm_t *perdm_list = NULL;
2710Sstevel@tonic-gate static krwlock_t perdm_rwlock;
2720Sstevel@tonic-gate cdevsw_impl_t *devimpl;
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate extern struct qinit strdata;
2750Sstevel@tonic-gate extern struct qinit stwdata;
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate static void runservice(queue_t *);
2780Sstevel@tonic-gate static void streams_bufcall_service(void);
2790Sstevel@tonic-gate static void streams_qbkgrnd_service(void);
2800Sstevel@tonic-gate static void streams_sqbkgrnd_service(void);
2810Sstevel@tonic-gate static syncq_t *new_syncq(void);
2820Sstevel@tonic-gate static void free_syncq(syncq_t *);
2830Sstevel@tonic-gate static void outer_insert(syncq_t *, syncq_t *);
2840Sstevel@tonic-gate static void outer_remove(syncq_t *, syncq_t *);
2850Sstevel@tonic-gate static void write_now(syncq_t *);
2860Sstevel@tonic-gate static void clr_qfull(queue_t *);
2870Sstevel@tonic-gate static void runbufcalls(void);
2880Sstevel@tonic-gate static void sqenable(syncq_t *);
2890Sstevel@tonic-gate static void sqfill_events(syncq_t *, queue_t *, mblk_t *, void (*)());
2900Sstevel@tonic-gate static void wait_q_syncq(queue_t *);
291235Smicheng static void backenable_insertedq(queue_t *);
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate static void queue_service(queue_t *);
2940Sstevel@tonic-gate static void stream_service(stdata_t *);
2950Sstevel@tonic-gate static void syncq_service(syncq_t *);
2960Sstevel@tonic-gate static void qwriter_outer_service(syncq_t *);
2970Sstevel@tonic-gate static void mblk_free(mblk_t *);
2980Sstevel@tonic-gate #ifdef DEBUG
2990Sstevel@tonic-gate static int qprocsareon(queue_t *);
3000Sstevel@tonic-gate #endif
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate static void set_nfsrv_ptr(queue_t *, queue_t *, queue_t *, queue_t *);
3030Sstevel@tonic-gate static void reset_nfsrv_ptr(queue_t *, queue_t *);
3046380Sjk115741 void set_qfull(queue_t *);
3050Sstevel@tonic-gate
3060Sstevel@tonic-gate static void sq_run_events(syncq_t *);
3070Sstevel@tonic-gate static int propagate_syncq(queue_t *);
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate static void blocksq(syncq_t *, ushort_t, int);
3100Sstevel@tonic-gate static void unblocksq(syncq_t *, ushort_t, int);
3110Sstevel@tonic-gate static int dropsq(syncq_t *, uint16_t);
3120Sstevel@tonic-gate static void emptysq(syncq_t *);
3130Sstevel@tonic-gate static sqlist_t *sqlist_alloc(struct stdata *, int);
3140Sstevel@tonic-gate static void sqlist_free(sqlist_t *);
3150Sstevel@tonic-gate static sqlist_t *sqlist_build(queue_t *, struct stdata *, boolean_t);
3160Sstevel@tonic-gate static void sqlist_insert(sqlist_t *, syncq_t *);
3170Sstevel@tonic-gate static void sqlist_insertall(sqlist_t *, queue_t *);
3180Sstevel@tonic-gate
3190Sstevel@tonic-gate static void strsetuio(stdata_t *);
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate struct kmem_cache *stream_head_cache;
3220Sstevel@tonic-gate struct kmem_cache *queue_cache;
3230Sstevel@tonic-gate struct kmem_cache *syncq_cache;
3240Sstevel@tonic-gate struct kmem_cache *qband_cache;
3250Sstevel@tonic-gate struct kmem_cache *linkinfo_cache;
3260Sstevel@tonic-gate struct kmem_cache *ciputctrl_cache = NULL;
3270Sstevel@tonic-gate
3280Sstevel@tonic-gate static linkinfo_t *linkinfo_list;
3290Sstevel@tonic-gate
3309671SBrian.Ruthven@Sun.COM /* Global esballoc throttling queue */
331*11785SDave.Plauger@Sun.COM static esb_queue_t system_esbq;
332*11785SDave.Plauger@Sun.COM
333*11785SDave.Plauger@Sun.COM /* Array of esballoc throttling queues, of length esbq_nelem */
334*11785SDave.Plauger@Sun.COM static esb_queue_t *volatile system_esbq_array;
335*11785SDave.Plauger@Sun.COM static int esbq_nelem;
336*11785SDave.Plauger@Sun.COM static kmutex_t esbq_lock;
337*11785SDave.Plauger@Sun.COM static int esbq_log2_cpus_per_q = 0;
338*11785SDave.Plauger@Sun.COM
339*11785SDave.Plauger@Sun.COM /* Scale the system_esbq length by setting number of CPUs per queue. */
340*11785SDave.Plauger@Sun.COM uint_t esbq_cpus_per_q = 1;
3413932Sss146032
3423932Sss146032 /*
3433932Sss146032 * esballoc tunable parameters.
3443932Sss146032 */
3453932Sss146032 int esbq_max_qlen = 0x16; /* throttled queue length */
3463932Sss146032 clock_t esbq_timeout = 0x8; /* timeout to process esb queue */
3473932Sss146032
3483932Sss146032 /*
3499671SBrian.Ruthven@Sun.COM * Routines to handle esballoc queueing.
3503932Sss146032 */
3513932Sss146032 static void esballoc_process_queue(esb_queue_t *);
3523932Sss146032 static void esballoc_enqueue_mblk(mblk_t *);
3533932Sss146032 static void esballoc_timer(void *);
3543932Sss146032 static void esballoc_set_timer(esb_queue_t *, clock_t);
3553932Sss146032 static void esballoc_mblk_free(mblk_t *);
3563932Sss146032
3570Sstevel@tonic-gate /*
3580Sstevel@tonic-gate * Qinit structure and Module_info structures
3590Sstevel@tonic-gate * for passthru read and write queues
3600Sstevel@tonic-gate */
3610Sstevel@tonic-gate
3620Sstevel@tonic-gate static void pass_wput(queue_t *, mblk_t *);
3630Sstevel@tonic-gate static queue_t *link_addpassthru(stdata_t *);
3640Sstevel@tonic-gate static void link_rempassthru(queue_t *);
3650Sstevel@tonic-gate
3660Sstevel@tonic-gate struct module_info passthru_info = {
3670Sstevel@tonic-gate 0,
3680Sstevel@tonic-gate "passthru",
3690Sstevel@tonic-gate 0,
3700Sstevel@tonic-gate INFPSZ,
3710Sstevel@tonic-gate STRHIGH,
3720Sstevel@tonic-gate STRLOW
3730Sstevel@tonic-gate };
3740Sstevel@tonic-gate
3750Sstevel@tonic-gate struct qinit passthru_rinit = {
3760Sstevel@tonic-gate (int (*)())putnext,
3770Sstevel@tonic-gate NULL,
3780Sstevel@tonic-gate NULL,
3790Sstevel@tonic-gate NULL,
3800Sstevel@tonic-gate NULL,
3810Sstevel@tonic-gate &passthru_info,
3820Sstevel@tonic-gate NULL
3830Sstevel@tonic-gate };
3840Sstevel@tonic-gate
3850Sstevel@tonic-gate struct qinit passthru_winit = {
3860Sstevel@tonic-gate (int (*)()) pass_wput,
3870Sstevel@tonic-gate NULL,
3880Sstevel@tonic-gate NULL,
3890Sstevel@tonic-gate NULL,
3900Sstevel@tonic-gate NULL,
3910Sstevel@tonic-gate &passthru_info,
3920Sstevel@tonic-gate NULL
3930Sstevel@tonic-gate };
3940Sstevel@tonic-gate
3950Sstevel@tonic-gate /*
3960Sstevel@tonic-gate * Verify correctness of list head/tail pointers.
3970Sstevel@tonic-gate */
3980Sstevel@tonic-gate #define LISTCHECK(head, tail, link) { \
3990Sstevel@tonic-gate EQUIV(head, tail); \
4000Sstevel@tonic-gate IMPLY(tail != NULL, tail->link == NULL); \
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate
4030Sstevel@tonic-gate /*
4040Sstevel@tonic-gate * Enqueue a list element `el' in the end of a list denoted by `head' and `tail'
4050Sstevel@tonic-gate * using a `link' field.
4060Sstevel@tonic-gate */
4070Sstevel@tonic-gate #define ENQUEUE(el, head, tail, link) { \
4080Sstevel@tonic-gate ASSERT(el->link == NULL); \
4090Sstevel@tonic-gate LISTCHECK(head, tail, link); \
4100Sstevel@tonic-gate if (head == NULL) \
4110Sstevel@tonic-gate head = el; \
4120Sstevel@tonic-gate else \
4130Sstevel@tonic-gate tail->link = el; \
4140Sstevel@tonic-gate tail = el; \
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate
4170Sstevel@tonic-gate /*
4180Sstevel@tonic-gate * Dequeue the first element of the list denoted by `head' and `tail' pointers
4190Sstevel@tonic-gate * using a `link' field and put result into `el'.
4200Sstevel@tonic-gate */
4210Sstevel@tonic-gate #define DQ(el, head, tail, link) { \
4220Sstevel@tonic-gate LISTCHECK(head, tail, link); \
4230Sstevel@tonic-gate el = head; \
4240Sstevel@tonic-gate if (head != NULL) { \
4250Sstevel@tonic-gate head = head->link; \
4260Sstevel@tonic-gate if (head == NULL) \
4270Sstevel@tonic-gate tail = NULL; \
4280Sstevel@tonic-gate el->link = NULL; \
4290Sstevel@tonic-gate } \
4300Sstevel@tonic-gate }
4310Sstevel@tonic-gate
4320Sstevel@tonic-gate /*
4330Sstevel@tonic-gate * Remove `el' from the list using `chase' and `curr' pointers and return result
4340Sstevel@tonic-gate * in `succeed'.
4350Sstevel@tonic-gate */
4360Sstevel@tonic-gate #define RMQ(el, head, tail, link, chase, curr, succeed) { \
4370Sstevel@tonic-gate LISTCHECK(head, tail, link); \
4380Sstevel@tonic-gate chase = NULL; \
4390Sstevel@tonic-gate succeed = 0; \
4400Sstevel@tonic-gate for (curr = head; (curr != el) && (curr != NULL); curr = curr->link) \
4410Sstevel@tonic-gate chase = curr; \
4420Sstevel@tonic-gate if (curr != NULL) { \
4430Sstevel@tonic-gate succeed = 1; \
4440Sstevel@tonic-gate ASSERT(curr == el); \
4450Sstevel@tonic-gate if (chase != NULL) \
4460Sstevel@tonic-gate chase->link = curr->link; \
4470Sstevel@tonic-gate else \
4480Sstevel@tonic-gate head = curr->link; \
4490Sstevel@tonic-gate curr->link = NULL; \
4500Sstevel@tonic-gate if (curr == tail) \
4510Sstevel@tonic-gate tail = chase; \
4520Sstevel@tonic-gate } \
4530Sstevel@tonic-gate LISTCHECK(head, tail, link); \
4540Sstevel@tonic-gate }
4550Sstevel@tonic-gate
4560Sstevel@tonic-gate /* Handling of delayed messages on the inner syncq. */
4570Sstevel@tonic-gate
4580Sstevel@tonic-gate /*
4590Sstevel@tonic-gate * DEBUG versions should use function versions (to simplify tracing) and
4600Sstevel@tonic-gate * non-DEBUG kernels should use macro versions.
4610Sstevel@tonic-gate */
4620Sstevel@tonic-gate
4630Sstevel@tonic-gate /*
4640Sstevel@tonic-gate * Put a queue on the syncq list of queues.
4650Sstevel@tonic-gate * Assumes SQLOCK held.
4660Sstevel@tonic-gate */
4670Sstevel@tonic-gate #define SQPUT_Q(sq, qp) \
4680Sstevel@tonic-gate { \
4690Sstevel@tonic-gate ASSERT(MUTEX_HELD(SQLOCK(sq))); \
4700Sstevel@tonic-gate if (!(qp->q_sqflags & Q_SQQUEUED)) { \
4710Sstevel@tonic-gate /* The queue should not be linked anywhere */ \
4720Sstevel@tonic-gate ASSERT((qp->q_sqprev == NULL) && (qp->q_sqnext == NULL)); \
4730Sstevel@tonic-gate /* Head and tail may only be NULL simultaneously */ \
4740Sstevel@tonic-gate EQUIV(sq->sq_head, sq->sq_tail); \
4759671SBrian.Ruthven@Sun.COM /* Queue may be only enqueued on its syncq */ \
4760Sstevel@tonic-gate ASSERT(sq == qp->q_syncq); \
4770Sstevel@tonic-gate /* Check the correctness of SQ_MESSAGES flag */ \
4780Sstevel@tonic-gate EQUIV(sq->sq_head, (sq->sq_flags & SQ_MESSAGES)); \
4790Sstevel@tonic-gate /* Sanity check first/last elements of the list */ \
4800Sstevel@tonic-gate IMPLY(sq->sq_head != NULL, sq->sq_head->q_sqprev == NULL);\
4810Sstevel@tonic-gate IMPLY(sq->sq_tail != NULL, sq->sq_tail->q_sqnext == NULL);\
4820Sstevel@tonic-gate /* \
4830Sstevel@tonic-gate * Sanity check of priority field: empty queue should \
4840Sstevel@tonic-gate * have zero priority \
4850Sstevel@tonic-gate * and nqueues equal to zero. \
4860Sstevel@tonic-gate */ \
4870Sstevel@tonic-gate IMPLY(sq->sq_head == NULL, sq->sq_pri == 0); \
4880Sstevel@tonic-gate /* Sanity check of sq_nqueues field */ \
4890Sstevel@tonic-gate EQUIV(sq->sq_head, sq->sq_nqueues); \
4900Sstevel@tonic-gate if (sq->sq_head == NULL) { \
4910Sstevel@tonic-gate sq->sq_head = sq->sq_tail = qp; \
4920Sstevel@tonic-gate sq->sq_flags |= SQ_MESSAGES; \
4930Sstevel@tonic-gate } else if (qp->q_spri == 0) { \
4940Sstevel@tonic-gate qp->q_sqprev = sq->sq_tail; \
4950Sstevel@tonic-gate sq->sq_tail->q_sqnext = qp; \
4960Sstevel@tonic-gate sq->sq_tail = qp; \
4970Sstevel@tonic-gate } else { \
4980Sstevel@tonic-gate /* \
4990Sstevel@tonic-gate * Put this queue in priority order: higher \
5000Sstevel@tonic-gate * priority gets closer to the head. \
5010Sstevel@tonic-gate */ \
5020Sstevel@tonic-gate queue_t **qpp = &sq->sq_tail; \
5030Sstevel@tonic-gate queue_t *qnext = NULL; \
5040Sstevel@tonic-gate \
5050Sstevel@tonic-gate while (*qpp != NULL && qp->q_spri > (*qpp)->q_spri) { \
5060Sstevel@tonic-gate qnext = *qpp; \
5070Sstevel@tonic-gate qpp = &(*qpp)->q_sqprev; \
5080Sstevel@tonic-gate } \
5090Sstevel@tonic-gate qp->q_sqnext = qnext; \
5100Sstevel@tonic-gate qp->q_sqprev = *qpp; \
5110Sstevel@tonic-gate if (*qpp != NULL) { \
5120Sstevel@tonic-gate (*qpp)->q_sqnext = qp; \
5130Sstevel@tonic-gate } else { \
5140Sstevel@tonic-gate sq->sq_head = qp; \
5150Sstevel@tonic-gate sq->sq_pri = sq->sq_head->q_spri; \
5160Sstevel@tonic-gate } \
5170Sstevel@tonic-gate *qpp = qp; \
5180Sstevel@tonic-gate } \
5190Sstevel@tonic-gate qp->q_sqflags |= Q_SQQUEUED; \
52011066Srafael.vanoni@sun.com qp->q_sqtstamp = ddi_get_lbolt(); \
5210Sstevel@tonic-gate sq->sq_nqueues++; \
5220Sstevel@tonic-gate } \
5230Sstevel@tonic-gate }
5240Sstevel@tonic-gate
5250Sstevel@tonic-gate /*
5260Sstevel@tonic-gate * Remove a queue from the syncq list
5270Sstevel@tonic-gate * Assumes SQLOCK held.
5280Sstevel@tonic-gate */
5290Sstevel@tonic-gate #define SQRM_Q(sq, qp) \
5300Sstevel@tonic-gate { \
5310Sstevel@tonic-gate ASSERT(MUTEX_HELD(SQLOCK(sq))); \
5320Sstevel@tonic-gate ASSERT(qp->q_sqflags & Q_SQQUEUED); \
5330Sstevel@tonic-gate ASSERT(sq->sq_head != NULL && sq->sq_tail != NULL); \
5340Sstevel@tonic-gate ASSERT((sq->sq_flags & SQ_MESSAGES) != 0); \
5350Sstevel@tonic-gate /* Check that the queue is actually in the list */ \
5360Sstevel@tonic-gate ASSERT(qp->q_sqnext != NULL || sq->sq_tail == qp); \
5370Sstevel@tonic-gate ASSERT(qp->q_sqprev != NULL || sq->sq_head == qp); \
5380Sstevel@tonic-gate ASSERT(sq->sq_nqueues != 0); \
5390Sstevel@tonic-gate if (qp->q_sqprev == NULL) { \
5400Sstevel@tonic-gate /* First queue on list, make head q_sqnext */ \
5410Sstevel@tonic-gate sq->sq_head = qp->q_sqnext; \
5420Sstevel@tonic-gate } else { \
5430Sstevel@tonic-gate /* Make prev->next == next */ \
5440Sstevel@tonic-gate qp->q_sqprev->q_sqnext = qp->q_sqnext; \
5450Sstevel@tonic-gate } \
5460Sstevel@tonic-gate if (qp->q_sqnext == NULL) { \
5470Sstevel@tonic-gate /* Last queue on list, make tail sqprev */ \
5480Sstevel@tonic-gate sq->sq_tail = qp->q_sqprev; \
5490Sstevel@tonic-gate } else { \
5500Sstevel@tonic-gate /* Make next->prev == prev */ \
5510Sstevel@tonic-gate qp->q_sqnext->q_sqprev = qp->q_sqprev; \
5520Sstevel@tonic-gate } \
5530Sstevel@tonic-gate /* clear out references on this queue */ \
5540Sstevel@tonic-gate qp->q_sqprev = qp->q_sqnext = NULL; \
5550Sstevel@tonic-gate qp->q_sqflags &= ~Q_SQQUEUED; \
5560Sstevel@tonic-gate /* If there is nothing queued, clear SQ_MESSAGES */ \
5570Sstevel@tonic-gate if (sq->sq_head != NULL) { \
5580Sstevel@tonic-gate sq->sq_pri = sq->sq_head->q_spri; \
5590Sstevel@tonic-gate } else { \
5600Sstevel@tonic-gate sq->sq_flags &= ~SQ_MESSAGES; \
5610Sstevel@tonic-gate sq->sq_pri = 0; \
5620Sstevel@tonic-gate } \
5630Sstevel@tonic-gate sq->sq_nqueues--; \
5640Sstevel@tonic-gate ASSERT(sq->sq_head != NULL || sq->sq_evhead != NULL || \
5650Sstevel@tonic-gate (sq->sq_flags & SQ_QUEUED) == 0); \
5660Sstevel@tonic-gate }
5670Sstevel@tonic-gate
5680Sstevel@tonic-gate /* Hide the definition from the header file. */
5690Sstevel@tonic-gate #ifdef SQPUT_MP
5700Sstevel@tonic-gate #undef SQPUT_MP
5710Sstevel@tonic-gate #endif
5720Sstevel@tonic-gate
5730Sstevel@tonic-gate /*
5740Sstevel@tonic-gate * Put a message on the queue syncq.
5750Sstevel@tonic-gate * Assumes QLOCK held.
5760Sstevel@tonic-gate */
5770Sstevel@tonic-gate #define SQPUT_MP(qp, mp) \
5780Sstevel@tonic-gate { \
5790Sstevel@tonic-gate ASSERT(MUTEX_HELD(QLOCK(qp))); \
5800Sstevel@tonic-gate ASSERT(qp->q_sqhead == NULL || \
5810Sstevel@tonic-gate (qp->q_sqtail != NULL && \
5820Sstevel@tonic-gate qp->q_sqtail->b_next == NULL)); \
5830Sstevel@tonic-gate qp->q_syncqmsgs++; \
5840Sstevel@tonic-gate ASSERT(qp->q_syncqmsgs != 0); /* Wraparound */ \
5850Sstevel@tonic-gate if (qp->q_sqhead == NULL) { \
5860Sstevel@tonic-gate qp->q_sqhead = qp->q_sqtail = mp; \
5870Sstevel@tonic-gate } else { \
5880Sstevel@tonic-gate qp->q_sqtail->b_next = mp; \
5890Sstevel@tonic-gate qp->q_sqtail = mp; \
5900Sstevel@tonic-gate } \
5910Sstevel@tonic-gate ASSERT(qp->q_syncqmsgs > 0); \
5926380Sjk115741 set_qfull(qp); \
5930Sstevel@tonic-gate }
5940Sstevel@tonic-gate
5950Sstevel@tonic-gate #define SQ_PUTCOUNT_SETFAST_LOCKED(sq) { \
5960Sstevel@tonic-gate ASSERT(MUTEX_HELD(SQLOCK(sq))); \
5970Sstevel@tonic-gate if ((sq)->sq_ciputctrl != NULL) { \
5980Sstevel@tonic-gate int i; \
5990Sstevel@tonic-gate int nlocks = (sq)->sq_nciputctrl; \
6000Sstevel@tonic-gate ciputctrl_t *cip = (sq)->sq_ciputctrl; \
6010Sstevel@tonic-gate ASSERT((sq)->sq_type & SQ_CIPUT); \
6020Sstevel@tonic-gate for (i = 0; i <= nlocks; i++) { \
6030Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cip[i].ciputctrl_lock)); \
6040Sstevel@tonic-gate cip[i].ciputctrl_count |= SQ_FASTPUT; \
6050Sstevel@tonic-gate } \
6060Sstevel@tonic-gate } \
6070Sstevel@tonic-gate }
6080Sstevel@tonic-gate
6090Sstevel@tonic-gate
6100Sstevel@tonic-gate #define SQ_PUTCOUNT_CLRFAST_LOCKED(sq) { \
6110Sstevel@tonic-gate ASSERT(MUTEX_HELD(SQLOCK(sq))); \
6120Sstevel@tonic-gate if ((sq)->sq_ciputctrl != NULL) { \
6130Sstevel@tonic-gate int i; \
6140Sstevel@tonic-gate int nlocks = (sq)->sq_nciputctrl; \
6150Sstevel@tonic-gate ciputctrl_t *cip = (sq)->sq_ciputctrl; \
6160Sstevel@tonic-gate ASSERT((sq)->sq_type & SQ_CIPUT); \
6170Sstevel@tonic-gate for (i = 0; i <= nlocks; i++) { \
6180Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cip[i].ciputctrl_lock)); \
6190Sstevel@tonic-gate cip[i].ciputctrl_count &= ~SQ_FASTPUT; \
6200Sstevel@tonic-gate } \
6210Sstevel@tonic-gate } \
6220Sstevel@tonic-gate }
6230Sstevel@tonic-gate
6240Sstevel@tonic-gate /*
6250Sstevel@tonic-gate * Run service procedures for all queues in the stream head.
6260Sstevel@tonic-gate */
6270Sstevel@tonic-gate #define STR_SERVICE(stp, q) { \
6280Sstevel@tonic-gate ASSERT(MUTEX_HELD(&stp->sd_qlock)); \
6290Sstevel@tonic-gate while (stp->sd_qhead != NULL) { \
6300Sstevel@tonic-gate DQ(q, stp->sd_qhead, stp->sd_qtail, q_link); \
6310Sstevel@tonic-gate ASSERT(stp->sd_nqueues > 0); \
6320Sstevel@tonic-gate stp->sd_nqueues--; \
6330Sstevel@tonic-gate ASSERT(!(q->q_flag & QINSERVICE)); \
6340Sstevel@tonic-gate mutex_exit(&stp->sd_qlock); \
6350Sstevel@tonic-gate queue_service(q); \
6360Sstevel@tonic-gate mutex_enter(&stp->sd_qlock); \
6370Sstevel@tonic-gate } \
6380Sstevel@tonic-gate ASSERT(stp->sd_nqueues == 0); \
6390Sstevel@tonic-gate ASSERT((stp->sd_qhead == NULL) && (stp->sd_qtail == NULL)); \
6400Sstevel@tonic-gate }
6410Sstevel@tonic-gate
6420Sstevel@tonic-gate /*
6439671SBrian.Ruthven@Sun.COM * Constructor/destructor routines for the stream head cache
6440Sstevel@tonic-gate */
6450Sstevel@tonic-gate /* ARGSUSED */
6460Sstevel@tonic-gate static int
stream_head_constructor(void * buf,void * cdrarg,int kmflags)6470Sstevel@tonic-gate stream_head_constructor(void *buf, void *cdrarg, int kmflags)
6480Sstevel@tonic-gate {
6490Sstevel@tonic-gate stdata_t *stp = buf;
6500Sstevel@tonic-gate
6510Sstevel@tonic-gate mutex_init(&stp->sd_lock, NULL, MUTEX_DEFAULT, NULL);
6520Sstevel@tonic-gate mutex_init(&stp->sd_reflock, NULL, MUTEX_DEFAULT, NULL);
6530Sstevel@tonic-gate mutex_init(&stp->sd_qlock, NULL, MUTEX_DEFAULT, NULL);
6540Sstevel@tonic-gate cv_init(&stp->sd_monitor, NULL, CV_DEFAULT, NULL);
6550Sstevel@tonic-gate cv_init(&stp->sd_iocmonitor, NULL, CV_DEFAULT, NULL);
656166Sxy158873 cv_init(&stp->sd_refmonitor, NULL, CV_DEFAULT, NULL);
6570Sstevel@tonic-gate cv_init(&stp->sd_qcv, NULL, CV_DEFAULT, NULL);
6580Sstevel@tonic-gate cv_init(&stp->sd_zcopy_wait, NULL, CV_DEFAULT, NULL);
6590Sstevel@tonic-gate stp->sd_wrq = NULL;
6600Sstevel@tonic-gate
6610Sstevel@tonic-gate return (0);
6620Sstevel@tonic-gate }
6630Sstevel@tonic-gate
6640Sstevel@tonic-gate /* ARGSUSED */
6650Sstevel@tonic-gate static void
stream_head_destructor(void * buf,void * cdrarg)6660Sstevel@tonic-gate stream_head_destructor(void *buf, void *cdrarg)
6670Sstevel@tonic-gate {
6680Sstevel@tonic-gate stdata_t *stp = buf;
6690Sstevel@tonic-gate
6700Sstevel@tonic-gate mutex_destroy(&stp->sd_lock);
6710Sstevel@tonic-gate mutex_destroy(&stp->sd_reflock);
6720Sstevel@tonic-gate mutex_destroy(&stp->sd_qlock);
6730Sstevel@tonic-gate cv_destroy(&stp->sd_monitor);
6740Sstevel@tonic-gate cv_destroy(&stp->sd_iocmonitor);
675166Sxy158873 cv_destroy(&stp->sd_refmonitor);
6760Sstevel@tonic-gate cv_destroy(&stp->sd_qcv);
6770Sstevel@tonic-gate cv_destroy(&stp->sd_zcopy_wait);
6780Sstevel@tonic-gate }
6790Sstevel@tonic-gate
6800Sstevel@tonic-gate /*
6819671SBrian.Ruthven@Sun.COM * Constructor/destructor routines for the queue cache
6820Sstevel@tonic-gate */
6830Sstevel@tonic-gate /* ARGSUSED */
6840Sstevel@tonic-gate static int
queue_constructor(void * buf,void * cdrarg,int kmflags)6850Sstevel@tonic-gate queue_constructor(void *buf, void *cdrarg, int kmflags)
6860Sstevel@tonic-gate {
6870Sstevel@tonic-gate queinfo_t *qip = buf;
6880Sstevel@tonic-gate queue_t *qp = &qip->qu_rqueue;
6890Sstevel@tonic-gate queue_t *wqp = &qip->qu_wqueue;
6900Sstevel@tonic-gate syncq_t *sq = &qip->qu_syncq;
6910Sstevel@tonic-gate
6920Sstevel@tonic-gate qp->q_first = NULL;
6930Sstevel@tonic-gate qp->q_link = NULL;
6940Sstevel@tonic-gate qp->q_count = 0;
6950Sstevel@tonic-gate qp->q_mblkcnt = 0;
6960Sstevel@tonic-gate qp->q_sqhead = NULL;
6970Sstevel@tonic-gate qp->q_sqtail = NULL;
6980Sstevel@tonic-gate qp->q_sqnext = NULL;
6990Sstevel@tonic-gate qp->q_sqprev = NULL;
7000Sstevel@tonic-gate qp->q_sqflags = 0;
7010Sstevel@tonic-gate qp->q_rwcnt = 0;
7020Sstevel@tonic-gate qp->q_spri = 0;
7030Sstevel@tonic-gate
7040Sstevel@tonic-gate mutex_init(QLOCK(qp), NULL, MUTEX_DEFAULT, NULL);
7050Sstevel@tonic-gate cv_init(&qp->q_wait, NULL, CV_DEFAULT, NULL);
7060Sstevel@tonic-gate
7070Sstevel@tonic-gate wqp->q_first = NULL;
7080Sstevel@tonic-gate wqp->q_link = NULL;
7090Sstevel@tonic-gate wqp->q_count = 0;
7100Sstevel@tonic-gate wqp->q_mblkcnt = 0;
7110Sstevel@tonic-gate wqp->q_sqhead = NULL;
7120Sstevel@tonic-gate wqp->q_sqtail = NULL;
7130Sstevel@tonic-gate wqp->q_sqnext = NULL;
7140Sstevel@tonic-gate wqp->q_sqprev = NULL;
7150Sstevel@tonic-gate wqp->q_sqflags = 0;
7160Sstevel@tonic-gate wqp->q_rwcnt = 0;
7170Sstevel@tonic-gate wqp->q_spri = 0;
7180Sstevel@tonic-gate
7190Sstevel@tonic-gate mutex_init(QLOCK(wqp), NULL, MUTEX_DEFAULT, NULL);
7200Sstevel@tonic-gate cv_init(&wqp->q_wait, NULL, CV_DEFAULT, NULL);
7210Sstevel@tonic-gate
7220Sstevel@tonic-gate sq->sq_head = NULL;
7230Sstevel@tonic-gate sq->sq_tail = NULL;
7240Sstevel@tonic-gate sq->sq_evhead = NULL;
7250Sstevel@tonic-gate sq->sq_evtail = NULL;
7260Sstevel@tonic-gate sq->sq_callbpend = NULL;
7270Sstevel@tonic-gate sq->sq_outer = NULL;
7280Sstevel@tonic-gate sq->sq_onext = NULL;
7290Sstevel@tonic-gate sq->sq_oprev = NULL;
7300Sstevel@tonic-gate sq->sq_next = NULL;
7310Sstevel@tonic-gate sq->sq_svcflags = 0;
7320Sstevel@tonic-gate sq->sq_servcount = 0;
7330Sstevel@tonic-gate sq->sq_needexcl = 0;
7340Sstevel@tonic-gate sq->sq_nqueues = 0;
7350Sstevel@tonic-gate sq->sq_pri = 0;
7360Sstevel@tonic-gate
7370Sstevel@tonic-gate mutex_init(&sq->sq_lock, NULL, MUTEX_DEFAULT, NULL);
7380Sstevel@tonic-gate cv_init(&sq->sq_wait, NULL, CV_DEFAULT, NULL);
7390Sstevel@tonic-gate cv_init(&sq->sq_exitwait, NULL, CV_DEFAULT, NULL);
7400Sstevel@tonic-gate
7410Sstevel@tonic-gate return (0);
7420Sstevel@tonic-gate }
7430Sstevel@tonic-gate
7440Sstevel@tonic-gate /* ARGSUSED */
7450Sstevel@tonic-gate static void
queue_destructor(void * buf,void * cdrarg)7460Sstevel@tonic-gate queue_destructor(void *buf, void *cdrarg)
7470Sstevel@tonic-gate {
7480Sstevel@tonic-gate queinfo_t *qip = buf;
7490Sstevel@tonic-gate queue_t *qp = &qip->qu_rqueue;
7500Sstevel@tonic-gate queue_t *wqp = &qip->qu_wqueue;
7510Sstevel@tonic-gate syncq_t *sq = &qip->qu_syncq;
7520Sstevel@tonic-gate
7530Sstevel@tonic-gate ASSERT(qp->q_sqhead == NULL);
7540Sstevel@tonic-gate ASSERT(wqp->q_sqhead == NULL);
7550Sstevel@tonic-gate ASSERT(qp->q_sqnext == NULL);
7560Sstevel@tonic-gate ASSERT(wqp->q_sqnext == NULL);
7570Sstevel@tonic-gate ASSERT(qp->q_rwcnt == 0);
7580Sstevel@tonic-gate ASSERT(wqp->q_rwcnt == 0);
7590Sstevel@tonic-gate
7600Sstevel@tonic-gate mutex_destroy(&qp->q_lock);
7610Sstevel@tonic-gate cv_destroy(&qp->q_wait);
7620Sstevel@tonic-gate
7630Sstevel@tonic-gate mutex_destroy(&wqp->q_lock);
7640Sstevel@tonic-gate cv_destroy(&wqp->q_wait);
7650Sstevel@tonic-gate
7660Sstevel@tonic-gate mutex_destroy(&sq->sq_lock);
7670Sstevel@tonic-gate cv_destroy(&sq->sq_wait);
7680Sstevel@tonic-gate cv_destroy(&sq->sq_exitwait);
7690Sstevel@tonic-gate }
7700Sstevel@tonic-gate
7710Sstevel@tonic-gate /*
7729671SBrian.Ruthven@Sun.COM * Constructor/destructor routines for the syncq cache
7730Sstevel@tonic-gate */
7740Sstevel@tonic-gate /* ARGSUSED */
7750Sstevel@tonic-gate static int
syncq_constructor(void * buf,void * cdrarg,int kmflags)7760Sstevel@tonic-gate syncq_constructor(void *buf, void *cdrarg, int kmflags)
7770Sstevel@tonic-gate {
7780Sstevel@tonic-gate syncq_t *sq = buf;
7790Sstevel@tonic-gate
7800Sstevel@tonic-gate bzero(buf, sizeof (syncq_t));
7810Sstevel@tonic-gate
7820Sstevel@tonic-gate mutex_init(&sq->sq_lock, NULL, MUTEX_DEFAULT, NULL);
7830Sstevel@tonic-gate cv_init(&sq->sq_wait, NULL, CV_DEFAULT, NULL);
7840Sstevel@tonic-gate cv_init(&sq->sq_exitwait, NULL, CV_DEFAULT, NULL);
7850Sstevel@tonic-gate
7860Sstevel@tonic-gate return (0);
7870Sstevel@tonic-gate }
7880Sstevel@tonic-gate
7890Sstevel@tonic-gate /* ARGSUSED */
7900Sstevel@tonic-gate static void
syncq_destructor(void * buf,void * cdrarg)7910Sstevel@tonic-gate syncq_destructor(void *buf, void *cdrarg)
7920Sstevel@tonic-gate {
7930Sstevel@tonic-gate syncq_t *sq = buf;
7940Sstevel@tonic-gate
7950Sstevel@tonic-gate ASSERT(sq->sq_head == NULL);
7960Sstevel@tonic-gate ASSERT(sq->sq_tail == NULL);
7970Sstevel@tonic-gate ASSERT(sq->sq_evhead == NULL);
7980Sstevel@tonic-gate ASSERT(sq->sq_evtail == NULL);
7990Sstevel@tonic-gate ASSERT(sq->sq_callbpend == NULL);
8000Sstevel@tonic-gate ASSERT(sq->sq_callbflags == 0);
8010Sstevel@tonic-gate ASSERT(sq->sq_outer == NULL);
8020Sstevel@tonic-gate ASSERT(sq->sq_onext == NULL);
8030Sstevel@tonic-gate ASSERT(sq->sq_oprev == NULL);
8040Sstevel@tonic-gate ASSERT(sq->sq_next == NULL);
8050Sstevel@tonic-gate ASSERT(sq->sq_needexcl == 0);
8060Sstevel@tonic-gate ASSERT(sq->sq_svcflags == 0);
8070Sstevel@tonic-gate ASSERT(sq->sq_servcount == 0);
8080Sstevel@tonic-gate ASSERT(sq->sq_nqueues == 0);
8090Sstevel@tonic-gate ASSERT(sq->sq_pri == 0);
8100Sstevel@tonic-gate ASSERT(sq->sq_count == 0);
8110Sstevel@tonic-gate ASSERT(sq->sq_rmqcount == 0);
8120Sstevel@tonic-gate ASSERT(sq->sq_cancelid == 0);
8130Sstevel@tonic-gate ASSERT(sq->sq_ciputctrl == NULL);
8140Sstevel@tonic-gate ASSERT(sq->sq_nciputctrl == 0);
8150Sstevel@tonic-gate ASSERT(sq->sq_type == 0);
8160Sstevel@tonic-gate ASSERT(sq->sq_flags == 0);
8170Sstevel@tonic-gate
8180Sstevel@tonic-gate mutex_destroy(&sq->sq_lock);
8190Sstevel@tonic-gate cv_destroy(&sq->sq_wait);
8200Sstevel@tonic-gate cv_destroy(&sq->sq_exitwait);
8210Sstevel@tonic-gate }
8220Sstevel@tonic-gate
8230Sstevel@tonic-gate /* ARGSUSED */
8240Sstevel@tonic-gate static int
ciputctrl_constructor(void * buf,void * cdrarg,int kmflags)8250Sstevel@tonic-gate ciputctrl_constructor(void *buf, void *cdrarg, int kmflags)
8260Sstevel@tonic-gate {
8270Sstevel@tonic-gate ciputctrl_t *cip = buf;
8280Sstevel@tonic-gate int i;
8290Sstevel@tonic-gate
8300Sstevel@tonic-gate for (i = 0; i < n_ciputctrl; i++) {
8310Sstevel@tonic-gate cip[i].ciputctrl_count = SQ_FASTPUT;
8320Sstevel@tonic-gate mutex_init(&cip[i].ciputctrl_lock, NULL, MUTEX_DEFAULT, NULL);
8330Sstevel@tonic-gate }
8340Sstevel@tonic-gate
8350Sstevel@tonic-gate return (0);
8360Sstevel@tonic-gate }
8370Sstevel@tonic-gate
8380Sstevel@tonic-gate /* ARGSUSED */
8390Sstevel@tonic-gate static void
ciputctrl_destructor(void * buf,void * cdrarg)8400Sstevel@tonic-gate ciputctrl_destructor(void *buf, void *cdrarg)
8410Sstevel@tonic-gate {
8420Sstevel@tonic-gate ciputctrl_t *cip = buf;
8430Sstevel@tonic-gate int i;
8440Sstevel@tonic-gate
8450Sstevel@tonic-gate for (i = 0; i < n_ciputctrl; i++) {
8460Sstevel@tonic-gate ASSERT(cip[i].ciputctrl_count & SQ_FASTPUT);
8470Sstevel@tonic-gate mutex_destroy(&cip[i].ciputctrl_lock);
8480Sstevel@tonic-gate }
8490Sstevel@tonic-gate }
8500Sstevel@tonic-gate
8510Sstevel@tonic-gate /*
8520Sstevel@tonic-gate * Init routine run from main at boot time.
8530Sstevel@tonic-gate */
8540Sstevel@tonic-gate void
strinit(void)8550Sstevel@tonic-gate strinit(void)
8560Sstevel@tonic-gate {
8570Sstevel@tonic-gate int ncpus = ((boot_max_ncpus == -1) ? max_ncpus : boot_max_ncpus);
8580Sstevel@tonic-gate
8590Sstevel@tonic-gate stream_head_cache = kmem_cache_create("stream_head_cache",
8605753Sgww sizeof (stdata_t), 0,
8615753Sgww stream_head_constructor, stream_head_destructor, NULL,
8625753Sgww NULL, NULL, 0);
8630Sstevel@tonic-gate
8640Sstevel@tonic-gate queue_cache = kmem_cache_create("queue_cache", sizeof (queinfo_t), 0,
8655753Sgww queue_constructor, queue_destructor, NULL, NULL, NULL, 0);
8660Sstevel@tonic-gate
8670Sstevel@tonic-gate syncq_cache = kmem_cache_create("syncq_cache", sizeof (syncq_t), 0,
8685753Sgww syncq_constructor, syncq_destructor, NULL, NULL, NULL, 0);
8690Sstevel@tonic-gate
8700Sstevel@tonic-gate qband_cache = kmem_cache_create("qband_cache",
8715753Sgww sizeof (qband_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
8720Sstevel@tonic-gate
8730Sstevel@tonic-gate linkinfo_cache = kmem_cache_create("linkinfo_cache",
8745753Sgww sizeof (linkinfo_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
8750Sstevel@tonic-gate
8760Sstevel@tonic-gate n_ciputctrl = ncpus;
8770Sstevel@tonic-gate n_ciputctrl = 1 << highbit(n_ciputctrl - 1);
8780Sstevel@tonic-gate ASSERT(n_ciputctrl >= 1);
8790Sstevel@tonic-gate n_ciputctrl = MIN(n_ciputctrl, max_n_ciputctrl);
8800Sstevel@tonic-gate if (n_ciputctrl >= min_n_ciputctrl) {
8810Sstevel@tonic-gate ciputctrl_cache = kmem_cache_create("ciputctrl_cache",
8825753Sgww sizeof (ciputctrl_t) * n_ciputctrl,
8835753Sgww sizeof (ciputctrl_t), ciputctrl_constructor,
8845753Sgww ciputctrl_destructor, NULL, NULL, NULL, 0);
8850Sstevel@tonic-gate }
8860Sstevel@tonic-gate
8870Sstevel@tonic-gate streams_taskq = system_taskq;
8880Sstevel@tonic-gate
8890Sstevel@tonic-gate if (streams_taskq == NULL)
8900Sstevel@tonic-gate panic("strinit: no memory for streams taskq!");
8910Sstevel@tonic-gate
8920Sstevel@tonic-gate bc_bkgrnd_thread = thread_create(NULL, 0,
8930Sstevel@tonic-gate streams_bufcall_service, NULL, 0, &p0, TS_RUN, streams_lopri);
8940Sstevel@tonic-gate
8950Sstevel@tonic-gate streams_qbkgrnd_thread = thread_create(NULL, 0,
8960Sstevel@tonic-gate streams_qbkgrnd_service, NULL, 0, &p0, TS_RUN, streams_lopri);
8970Sstevel@tonic-gate
8980Sstevel@tonic-gate streams_sqbkgrnd_thread = thread_create(NULL, 0,
8990Sstevel@tonic-gate streams_sqbkgrnd_service, NULL, 0, &p0, TS_RUN, streams_lopri);
9000Sstevel@tonic-gate
9010Sstevel@tonic-gate /*
9020Sstevel@tonic-gate * Create STREAMS kstats.
9030Sstevel@tonic-gate */
9040Sstevel@tonic-gate str_kstat = kstat_create("streams", 0, "strstat",
9050Sstevel@tonic-gate "net", KSTAT_TYPE_NAMED,
9060Sstevel@tonic-gate sizeof (str_statistics) / sizeof (kstat_named_t),
9070Sstevel@tonic-gate KSTAT_FLAG_VIRTUAL);
9080Sstevel@tonic-gate
9090Sstevel@tonic-gate if (str_kstat != NULL) {
9100Sstevel@tonic-gate str_kstat->ks_data = &str_statistics;
9110Sstevel@tonic-gate kstat_install(str_kstat);
9120Sstevel@tonic-gate }
9130Sstevel@tonic-gate
9140Sstevel@tonic-gate /*
9150Sstevel@tonic-gate * TPI support routine initialisation.
9160Sstevel@tonic-gate */
9170Sstevel@tonic-gate tpi_init();
9183448Sdh155122
9193448Sdh155122 /*
9203448Sdh155122 * Handle to have autopush and persistent link information per
9213448Sdh155122 * zone.
9223448Sdh155122 * Note: uses shutdown hook instead of destroy hook so that the
9233448Sdh155122 * persistent links can be torn down before the destroy hooks
9243448Sdh155122 * in the TCP/IP stack are called.
9253448Sdh155122 */
9263448Sdh155122 netstack_register(NS_STR, str_stack_init, str_stack_shutdown,
9273448Sdh155122 str_stack_fini);
9280Sstevel@tonic-gate }
9290Sstevel@tonic-gate
9300Sstevel@tonic-gate void
str_sendsig(vnode_t * vp,int event,uchar_t band,int error)9310Sstevel@tonic-gate str_sendsig(vnode_t *vp, int event, uchar_t band, int error)
9320Sstevel@tonic-gate {
9330Sstevel@tonic-gate struct stdata *stp;
9340Sstevel@tonic-gate
9350Sstevel@tonic-gate ASSERT(vp->v_stream);
9360Sstevel@tonic-gate stp = vp->v_stream;
9370Sstevel@tonic-gate /* Have to hold sd_lock to prevent siglist from changing */
9380Sstevel@tonic-gate mutex_enter(&stp->sd_lock);
9390Sstevel@tonic-gate if (stp->sd_sigflags & event)
9400Sstevel@tonic-gate strsendsig(stp->sd_siglist, event, band, error);
9410Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
9420Sstevel@tonic-gate }
9430Sstevel@tonic-gate
9440Sstevel@tonic-gate /*
9450Sstevel@tonic-gate * Send the "sevent" set of signals to a process.
9460Sstevel@tonic-gate * This might send more than one signal if the process is registered
9470Sstevel@tonic-gate * for multiple events. The caller should pass in an sevent that only
9480Sstevel@tonic-gate * includes the events for which the process has registered.
9490Sstevel@tonic-gate */
9500Sstevel@tonic-gate static void
dosendsig(proc_t * proc,int events,int sevent,k_siginfo_t * info,uchar_t band,int error)9510Sstevel@tonic-gate dosendsig(proc_t *proc, int events, int sevent, k_siginfo_t *info,
9520Sstevel@tonic-gate uchar_t band, int error)
9530Sstevel@tonic-gate {
9540Sstevel@tonic-gate ASSERT(MUTEX_HELD(&proc->p_lock));
9550Sstevel@tonic-gate
9560Sstevel@tonic-gate info->si_band = 0;
9570Sstevel@tonic-gate info->si_errno = 0;
9580Sstevel@tonic-gate
9590Sstevel@tonic-gate if (sevent & S_ERROR) {
9600Sstevel@tonic-gate sevent &= ~S_ERROR;
9610Sstevel@tonic-gate info->si_code = POLL_ERR;
9620Sstevel@tonic-gate info->si_errno = error;
9630Sstevel@tonic-gate TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG,
9645753Sgww "strsendsig:proc %p info %p", proc, info);
9650Sstevel@tonic-gate sigaddq(proc, NULL, info, KM_NOSLEEP);
9660Sstevel@tonic-gate info->si_errno = 0;
9670Sstevel@tonic-gate }
9680Sstevel@tonic-gate if (sevent & S_HANGUP) {
9690Sstevel@tonic-gate sevent &= ~S_HANGUP;
9700Sstevel@tonic-gate info->si_code = POLL_HUP;
9710Sstevel@tonic-gate TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG,
9725753Sgww "strsendsig:proc %p info %p", proc, info);
9730Sstevel@tonic-gate sigaddq(proc, NULL, info, KM_NOSLEEP);
9740Sstevel@tonic-gate }
9750Sstevel@tonic-gate if (sevent & S_HIPRI) {
9760Sstevel@tonic-gate sevent &= ~S_HIPRI;
9770Sstevel@tonic-gate info->si_code = POLL_PRI;
9780Sstevel@tonic-gate TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG,
9795753Sgww "strsendsig:proc %p info %p", proc, info);
9800Sstevel@tonic-gate sigaddq(proc, NULL, info, KM_NOSLEEP);
9810Sstevel@tonic-gate }
9820Sstevel@tonic-gate if (sevent & S_RDBAND) {
9830Sstevel@tonic-gate sevent &= ~S_RDBAND;
9840Sstevel@tonic-gate if (events & S_BANDURG)
9850Sstevel@tonic-gate sigtoproc(proc, NULL, SIGURG);
9860Sstevel@tonic-gate else
9870Sstevel@tonic-gate sigtoproc(proc, NULL, SIGPOLL);
9880Sstevel@tonic-gate }
9890Sstevel@tonic-gate if (sevent & S_WRBAND) {
9900Sstevel@tonic-gate sevent &= ~S_WRBAND;
9910Sstevel@tonic-gate sigtoproc(proc, NULL, SIGPOLL);
9920Sstevel@tonic-gate }
9930Sstevel@tonic-gate if (sevent & S_INPUT) {
9940Sstevel@tonic-gate sevent &= ~S_INPUT;
9950Sstevel@tonic-gate info->si_code = POLL_IN;
9960Sstevel@tonic-gate info->si_band = band;
9970Sstevel@tonic-gate TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG,
9985753Sgww "strsendsig:proc %p info %p", proc, info);
9990Sstevel@tonic-gate sigaddq(proc, NULL, info, KM_NOSLEEP);
10000Sstevel@tonic-gate info->si_band = 0;
10010Sstevel@tonic-gate }
10020Sstevel@tonic-gate if (sevent & S_OUTPUT) {
10030Sstevel@tonic-gate sevent &= ~S_OUTPUT;
10040Sstevel@tonic-gate info->si_code = POLL_OUT;
10050Sstevel@tonic-gate info->si_band = band;
10060Sstevel@tonic-gate TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG,
10075753Sgww "strsendsig:proc %p info %p", proc, info);
10080Sstevel@tonic-gate sigaddq(proc, NULL, info, KM_NOSLEEP);
10090Sstevel@tonic-gate info->si_band = 0;
10100Sstevel@tonic-gate }
10110Sstevel@tonic-gate if (sevent & S_MSG) {
10120Sstevel@tonic-gate sevent &= ~S_MSG;
10130Sstevel@tonic-gate info->si_code = POLL_MSG;
10140Sstevel@tonic-gate info->si_band = band;
10150Sstevel@tonic-gate TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG,
10165753Sgww "strsendsig:proc %p info %p", proc, info);
10170Sstevel@tonic-gate sigaddq(proc, NULL, info, KM_NOSLEEP);
10180Sstevel@tonic-gate info->si_band = 0;
10190Sstevel@tonic-gate }
10200Sstevel@tonic-gate if (sevent & S_RDNORM) {
10210Sstevel@tonic-gate sevent &= ~S_RDNORM;
10220Sstevel@tonic-gate sigtoproc(proc, NULL, SIGPOLL);
10230Sstevel@tonic-gate }
10240Sstevel@tonic-gate if (sevent != 0) {
10250Sstevel@tonic-gate panic("strsendsig: unknown event(s) %x", sevent);
10260Sstevel@tonic-gate }
10270Sstevel@tonic-gate }
10280Sstevel@tonic-gate
10290Sstevel@tonic-gate /*
10300Sstevel@tonic-gate * Send SIGPOLL/SIGURG signal to all processes and process groups
10310Sstevel@tonic-gate * registered on the given signal list that want a signal for at
10320Sstevel@tonic-gate * least one of the specified events.
10330Sstevel@tonic-gate *
10340Sstevel@tonic-gate * Must be called with exclusive access to siglist (caller holding sd_lock).
10350Sstevel@tonic-gate *
10360Sstevel@tonic-gate * strioctl(I_SETSIG/I_ESETSIG) will only change siglist when holding
10370Sstevel@tonic-gate * sd_lock and the ioctl code maintains a PID_HOLD on the pid structure
10380Sstevel@tonic-gate * while it is in the siglist.
10390Sstevel@tonic-gate *
10400Sstevel@tonic-gate * For performance reasons (MP scalability) the code drops pidlock
10410Sstevel@tonic-gate * when sending signals to a single process.
10420Sstevel@tonic-gate * When sending to a process group the code holds
10430Sstevel@tonic-gate * pidlock to prevent the membership in the process group from changing
10440Sstevel@tonic-gate * while walking the p_pglink list.
10450Sstevel@tonic-gate */
10460Sstevel@tonic-gate void
strsendsig(strsig_t * siglist,int event,uchar_t band,int error)10470Sstevel@tonic-gate strsendsig(strsig_t *siglist, int event, uchar_t band, int error)
10480Sstevel@tonic-gate {
10490Sstevel@tonic-gate strsig_t *ssp;
10500Sstevel@tonic-gate k_siginfo_t info;
10510Sstevel@tonic-gate struct pid *pidp;
10520Sstevel@tonic-gate proc_t *proc;
10530Sstevel@tonic-gate
10540Sstevel@tonic-gate info.si_signo = SIGPOLL;
10550Sstevel@tonic-gate info.si_errno = 0;
10560Sstevel@tonic-gate for (ssp = siglist; ssp; ssp = ssp->ss_next) {
10570Sstevel@tonic-gate int sevent;
10580Sstevel@tonic-gate
10590Sstevel@tonic-gate sevent = ssp->ss_events & event;
10600Sstevel@tonic-gate if (sevent == 0)
10610Sstevel@tonic-gate continue;
10620Sstevel@tonic-gate
10630Sstevel@tonic-gate if ((pidp = ssp->ss_pidp) == NULL) {
10640Sstevel@tonic-gate /* pid was released but still on event list */
10650Sstevel@tonic-gate continue;
10660Sstevel@tonic-gate }
10670Sstevel@tonic-gate
10680Sstevel@tonic-gate
10690Sstevel@tonic-gate if (ssp->ss_pid > 0) {
10700Sstevel@tonic-gate /*
10710Sstevel@tonic-gate * XXX This unfortunately still generates
10720Sstevel@tonic-gate * a signal when a fd is closed but
10730Sstevel@tonic-gate * the proc is active.
10740Sstevel@tonic-gate */
10750Sstevel@tonic-gate ASSERT(ssp->ss_pid == pidp->pid_id);
10760Sstevel@tonic-gate
10770Sstevel@tonic-gate mutex_enter(&pidlock);
10780Sstevel@tonic-gate proc = prfind_zone(pidp->pid_id, ALL_ZONES);
10790Sstevel@tonic-gate if (proc == NULL) {
10800Sstevel@tonic-gate mutex_exit(&pidlock);
10810Sstevel@tonic-gate continue;
10820Sstevel@tonic-gate }
10830Sstevel@tonic-gate mutex_enter(&proc->p_lock);
10840Sstevel@tonic-gate mutex_exit(&pidlock);
10850Sstevel@tonic-gate dosendsig(proc, ssp->ss_events, sevent, &info,
10865753Sgww band, error);
10870Sstevel@tonic-gate mutex_exit(&proc->p_lock);
10880Sstevel@tonic-gate } else {
10890Sstevel@tonic-gate /*
10900Sstevel@tonic-gate * Send to process group. Hold pidlock across
10910Sstevel@tonic-gate * calls to dosendsig().
10920Sstevel@tonic-gate */
10930Sstevel@tonic-gate pid_t pgrp = -ssp->ss_pid;
10940Sstevel@tonic-gate
10950Sstevel@tonic-gate mutex_enter(&pidlock);
10960Sstevel@tonic-gate proc = pgfind_zone(pgrp, ALL_ZONES);
10970Sstevel@tonic-gate while (proc != NULL) {
10980Sstevel@tonic-gate mutex_enter(&proc->p_lock);
10990Sstevel@tonic-gate dosendsig(proc, ssp->ss_events, sevent,
11005753Sgww &info, band, error);
11010Sstevel@tonic-gate mutex_exit(&proc->p_lock);
11020Sstevel@tonic-gate proc = proc->p_pglink;
11030Sstevel@tonic-gate }
11040Sstevel@tonic-gate mutex_exit(&pidlock);
11050Sstevel@tonic-gate }
11060Sstevel@tonic-gate }
11070Sstevel@tonic-gate }
11080Sstevel@tonic-gate
11090Sstevel@tonic-gate /*
11100Sstevel@tonic-gate * Attach a stream device or module.
11110Sstevel@tonic-gate * qp is a read queue; the new queue goes in so its next
11120Sstevel@tonic-gate * read ptr is the argument, and the write queue corresponding
11130Sstevel@tonic-gate * to the argument points to this queue. Return 0 on success,
11140Sstevel@tonic-gate * or a non-zero errno on failure.
11150Sstevel@tonic-gate */
11160Sstevel@tonic-gate int
qattach(queue_t * qp,dev_t * devp,int oflag,cred_t * crp,fmodsw_impl_t * fp,boolean_t is_insert)11170Sstevel@tonic-gate qattach(queue_t *qp, dev_t *devp, int oflag, cred_t *crp, fmodsw_impl_t *fp,
11180Sstevel@tonic-gate boolean_t is_insert)
11190Sstevel@tonic-gate {
11200Sstevel@tonic-gate major_t major;
11210Sstevel@tonic-gate cdevsw_impl_t *dp;
11220Sstevel@tonic-gate struct streamtab *str;
11230Sstevel@tonic-gate queue_t *rq;
11240Sstevel@tonic-gate queue_t *wrq;
11250Sstevel@tonic-gate uint32_t qflag;
11260Sstevel@tonic-gate uint32_t sqtype;
11270Sstevel@tonic-gate perdm_t *dmp;
11280Sstevel@tonic-gate int error;
11290Sstevel@tonic-gate int sflag;
11300Sstevel@tonic-gate
11310Sstevel@tonic-gate rq = allocq();
11320Sstevel@tonic-gate wrq = _WR(rq);
11330Sstevel@tonic-gate STREAM(rq) = STREAM(wrq) = STREAM(qp);
11340Sstevel@tonic-gate
11350Sstevel@tonic-gate if (fp != NULL) {
11360Sstevel@tonic-gate str = fp->f_str;
11370Sstevel@tonic-gate qflag = fp->f_qflag;
11380Sstevel@tonic-gate sqtype = fp->f_sqtype;
11390Sstevel@tonic-gate dmp = fp->f_dmp;
11400Sstevel@tonic-gate IMPLY((qflag & (QPERMOD | QMTOUTPERIM)), dmp != NULL);
11410Sstevel@tonic-gate sflag = MODOPEN;
11420Sstevel@tonic-gate
11430Sstevel@tonic-gate /*
11440Sstevel@tonic-gate * stash away a pointer to the module structure so we can
11450Sstevel@tonic-gate * unref it in qdetach.
11460Sstevel@tonic-gate */
11470Sstevel@tonic-gate rq->q_fp = fp;
11480Sstevel@tonic-gate } else {
11490Sstevel@tonic-gate ASSERT(!is_insert);
11500Sstevel@tonic-gate
11510Sstevel@tonic-gate major = getmajor(*devp);
11520Sstevel@tonic-gate dp = &devimpl[major];
11530Sstevel@tonic-gate
11540Sstevel@tonic-gate str = dp->d_str;
11550Sstevel@tonic-gate ASSERT(str == STREAMSTAB(major));
11560Sstevel@tonic-gate
11570Sstevel@tonic-gate qflag = dp->d_qflag;
11580Sstevel@tonic-gate ASSERT(qflag & QISDRV);
11590Sstevel@tonic-gate sqtype = dp->d_sqtype;
11600Sstevel@tonic-gate
11610Sstevel@tonic-gate /* create perdm_t if needed */
11620Sstevel@tonic-gate if (NEED_DM(dp->d_dmp, qflag))
11630Sstevel@tonic-gate dp->d_dmp = hold_dm(str, qflag, sqtype);
11640Sstevel@tonic-gate
11650Sstevel@tonic-gate dmp = dp->d_dmp;
11660Sstevel@tonic-gate sflag = 0;
11670Sstevel@tonic-gate }
11680Sstevel@tonic-gate
11690Sstevel@tonic-gate TRACE_2(TR_FAC_STREAMS_FR, TR_QATTACH_FLAGS,
11700Sstevel@tonic-gate "qattach:qflag == %X(%X)", qflag, *devp);
11710Sstevel@tonic-gate
11720Sstevel@tonic-gate /* setq might sleep in allocator - avoid holding locks. */
11730Sstevel@tonic-gate setq(rq, str->st_rdinit, str->st_wrinit, dmp, qflag, sqtype, B_FALSE);
11740Sstevel@tonic-gate
11750Sstevel@tonic-gate /*
11760Sstevel@tonic-gate * Before calling the module's open routine, set up the q_next
11770Sstevel@tonic-gate * pointer for inserting a module in the middle of a stream.
11780Sstevel@tonic-gate *
11790Sstevel@tonic-gate * Note that we can always set _QINSERTING and set up q_next
11800Sstevel@tonic-gate * pointer for both inserting and pushing a module. Then there
11810Sstevel@tonic-gate * is no need for the is_insert parameter. In insertq(), called
11820Sstevel@tonic-gate * by qprocson(), assume that q_next of the new module always points
11830Sstevel@tonic-gate * to the correct queue and use it for insertion. Everything should
11840Sstevel@tonic-gate * work out fine. But in the first release of _I_INSERT, we
11850Sstevel@tonic-gate * distinguish between inserting and pushing to make sure that
11860Sstevel@tonic-gate * pushing a module follows the same code path as before.
11870Sstevel@tonic-gate */
11880Sstevel@tonic-gate if (is_insert) {
11890Sstevel@tonic-gate rq->q_flag |= _QINSERTING;
11900Sstevel@tonic-gate rq->q_next = qp;
11910Sstevel@tonic-gate }
11920Sstevel@tonic-gate
11930Sstevel@tonic-gate /*
11940Sstevel@tonic-gate * If there is an outer perimeter get exclusive access during
11950Sstevel@tonic-gate * the open procedure. Bump up the reference count on the queue.
11960Sstevel@tonic-gate */
11970Sstevel@tonic-gate entersq(rq->q_syncq, SQ_OPENCLOSE);
11980Sstevel@tonic-gate error = (*rq->q_qinfo->qi_qopen)(rq, devp, oflag, sflag, crp);
11990Sstevel@tonic-gate if (error != 0)
12000Sstevel@tonic-gate goto failed;
12010Sstevel@tonic-gate leavesq(rq->q_syncq, SQ_OPENCLOSE);
12020Sstevel@tonic-gate ASSERT(qprocsareon(rq));
12030Sstevel@tonic-gate return (0);
12040Sstevel@tonic-gate
12050Sstevel@tonic-gate failed:
12060Sstevel@tonic-gate rq->q_flag &= ~_QINSERTING;
12070Sstevel@tonic-gate if (backq(wrq) != NULL && backq(wrq)->q_next == wrq)
12080Sstevel@tonic-gate qprocsoff(rq);
12090Sstevel@tonic-gate leavesq(rq->q_syncq, SQ_OPENCLOSE);
12100Sstevel@tonic-gate rq->q_next = wrq->q_next = NULL;
12110Sstevel@tonic-gate qdetach(rq, 0, 0, crp, B_FALSE);
12120Sstevel@tonic-gate return (error);
12130Sstevel@tonic-gate }
12140Sstevel@tonic-gate
12150Sstevel@tonic-gate /*
12160Sstevel@tonic-gate * Handle second open of stream. For modules, set the
12170Sstevel@tonic-gate * last argument to MODOPEN and do not pass any open flags.
12180Sstevel@tonic-gate * Ignore dummydev since this is not the first open.
12190Sstevel@tonic-gate */
12200Sstevel@tonic-gate int
qreopen(queue_t * qp,dev_t * devp,int flag,cred_t * crp)12210Sstevel@tonic-gate qreopen(queue_t *qp, dev_t *devp, int flag, cred_t *crp)
12220Sstevel@tonic-gate {
12230Sstevel@tonic-gate int error;
12240Sstevel@tonic-gate dev_t dummydev;
12250Sstevel@tonic-gate queue_t *wqp = _WR(qp);
12260Sstevel@tonic-gate
12270Sstevel@tonic-gate ASSERT(qp->q_flag & QREADR);
12280Sstevel@tonic-gate entersq(qp->q_syncq, SQ_OPENCLOSE);
12290Sstevel@tonic-gate
12300Sstevel@tonic-gate dummydev = *devp;
12310Sstevel@tonic-gate if (error = ((*qp->q_qinfo->qi_qopen)(qp, &dummydev,
12320Sstevel@tonic-gate (wqp->q_next ? 0 : flag), (wqp->q_next ? MODOPEN : 0), crp))) {
12330Sstevel@tonic-gate leavesq(qp->q_syncq, SQ_OPENCLOSE);
12340Sstevel@tonic-gate mutex_enter(&STREAM(qp)->sd_lock);
12350Sstevel@tonic-gate qp->q_stream->sd_flag |= STREOPENFAIL;
12360Sstevel@tonic-gate mutex_exit(&STREAM(qp)->sd_lock);
12370Sstevel@tonic-gate return (error);
12380Sstevel@tonic-gate }
12390Sstevel@tonic-gate leavesq(qp->q_syncq, SQ_OPENCLOSE);
12400Sstevel@tonic-gate
12410Sstevel@tonic-gate /*
12420Sstevel@tonic-gate * successful open should have done qprocson()
12430Sstevel@tonic-gate */
12440Sstevel@tonic-gate ASSERT(qprocsareon(_RD(qp)));
12450Sstevel@tonic-gate return (0);
12460Sstevel@tonic-gate }
12470Sstevel@tonic-gate
12480Sstevel@tonic-gate /*
12490Sstevel@tonic-gate * Detach a stream module or device.
12500Sstevel@tonic-gate * If clmode == 1 then the module or driver was opened and its
12510Sstevel@tonic-gate * close routine must be called. If clmode == 0, the module
12520Sstevel@tonic-gate * or driver was never opened or the open failed, and so its close
12530Sstevel@tonic-gate * should not be called.
12540Sstevel@tonic-gate */
12550Sstevel@tonic-gate void
qdetach(queue_t * qp,int clmode,int flag,cred_t * crp,boolean_t is_remove)12560Sstevel@tonic-gate qdetach(queue_t *qp, int clmode, int flag, cred_t *crp, boolean_t is_remove)
12570Sstevel@tonic-gate {
12580Sstevel@tonic-gate queue_t *wqp = _WR(qp);
12590Sstevel@tonic-gate ASSERT(STREAM(qp)->sd_flag & (STRCLOSE|STWOPEN|STRPLUMB));
12600Sstevel@tonic-gate
12610Sstevel@tonic-gate if (STREAM_NEEDSERVICE(STREAM(qp)))
12620Sstevel@tonic-gate stream_runservice(STREAM(qp));
12630Sstevel@tonic-gate
12640Sstevel@tonic-gate if (clmode) {
12650Sstevel@tonic-gate /*
12660Sstevel@tonic-gate * Make sure that all the messages on the write side syncq are
12670Sstevel@tonic-gate * processed and nothing is left. Since we are closing, no new
12680Sstevel@tonic-gate * messages may appear there.
12690Sstevel@tonic-gate */
12700Sstevel@tonic-gate wait_q_syncq(wqp);
12710Sstevel@tonic-gate
12720Sstevel@tonic-gate entersq(qp->q_syncq, SQ_OPENCLOSE);
12730Sstevel@tonic-gate if (is_remove) {
12740Sstevel@tonic-gate mutex_enter(QLOCK(qp));
12750Sstevel@tonic-gate qp->q_flag |= _QREMOVING;
12760Sstevel@tonic-gate mutex_exit(QLOCK(qp));
12770Sstevel@tonic-gate }
12780Sstevel@tonic-gate (*qp->q_qinfo->qi_qclose)(qp, flag, crp);
12790Sstevel@tonic-gate /*
12800Sstevel@tonic-gate * Check that qprocsoff() was actually called.
12810Sstevel@tonic-gate */
12820Sstevel@tonic-gate ASSERT((qp->q_flag & QWCLOSE) && (wqp->q_flag & QWCLOSE));
12830Sstevel@tonic-gate
12840Sstevel@tonic-gate leavesq(qp->q_syncq, SQ_OPENCLOSE);
12850Sstevel@tonic-gate } else {
12860Sstevel@tonic-gate disable_svc(qp);
12870Sstevel@tonic-gate }
12880Sstevel@tonic-gate
12890Sstevel@tonic-gate /*
12900Sstevel@tonic-gate * Allow any threads blocked in entersq to proceed and discover
12910Sstevel@tonic-gate * the QWCLOSE is set.
12920Sstevel@tonic-gate * Note: This assumes that all users of entersq check QWCLOSE.
12930Sstevel@tonic-gate * Currently runservice is the only entersq that can happen
12940Sstevel@tonic-gate * after removeq has finished.
12950Sstevel@tonic-gate * Removeq will have discarded all messages destined to the closing
12960Sstevel@tonic-gate * pair of queues from the syncq.
12970Sstevel@tonic-gate * NOTE: Calling a function inside an assert is unconventional.
12980Sstevel@tonic-gate * However, it does not cause any problem since flush_syncq() does
12990Sstevel@tonic-gate * not change any state except when it returns non-zero i.e.
13000Sstevel@tonic-gate * when the assert will trigger.
13010Sstevel@tonic-gate */
13020Sstevel@tonic-gate ASSERT(flush_syncq(qp->q_syncq, qp) == 0);
13030Sstevel@tonic-gate ASSERT(flush_syncq(wqp->q_syncq, wqp) == 0);
13040Sstevel@tonic-gate ASSERT((qp->q_flag & QPERMOD) ||
13055753Sgww ((qp->q_syncq->sq_head == NULL) &&
13065753Sgww (wqp->q_syncq->sq_head == NULL)));
13070Sstevel@tonic-gate
13080Sstevel@tonic-gate /* release any fmodsw_impl_t structure held on behalf of the queue */
13090Sstevel@tonic-gate ASSERT(qp->q_fp != NULL || qp->q_flag & QISDRV);
13100Sstevel@tonic-gate if (qp->q_fp != NULL)
13110Sstevel@tonic-gate fmodsw_rele(qp->q_fp);
13120Sstevel@tonic-gate
13130Sstevel@tonic-gate /* freeq removes us from the outer perimeter if any */
13140Sstevel@tonic-gate freeq(qp);
13150Sstevel@tonic-gate }
13160Sstevel@tonic-gate
13170Sstevel@tonic-gate /* Prevent service procedures from being called */
13180Sstevel@tonic-gate void
disable_svc(queue_t * qp)13190Sstevel@tonic-gate disable_svc(queue_t *qp)
13200Sstevel@tonic-gate {
13210Sstevel@tonic-gate queue_t *wqp = _WR(qp);
13220Sstevel@tonic-gate
13230Sstevel@tonic-gate ASSERT(qp->q_flag & QREADR);
13240Sstevel@tonic-gate mutex_enter(QLOCK(qp));
13250Sstevel@tonic-gate qp->q_flag |= QWCLOSE;
13260Sstevel@tonic-gate mutex_exit(QLOCK(qp));
13270Sstevel@tonic-gate mutex_enter(QLOCK(wqp));
13280Sstevel@tonic-gate wqp->q_flag |= QWCLOSE;
13290Sstevel@tonic-gate mutex_exit(QLOCK(wqp));
13300Sstevel@tonic-gate }
13310Sstevel@tonic-gate
13329671SBrian.Ruthven@Sun.COM /* Allow service procedures to be called again */
13330Sstevel@tonic-gate void
enable_svc(queue_t * qp)13340Sstevel@tonic-gate enable_svc(queue_t *qp)
13350Sstevel@tonic-gate {
13360Sstevel@tonic-gate queue_t *wqp = _WR(qp);
13370Sstevel@tonic-gate
13380Sstevel@tonic-gate ASSERT(qp->q_flag & QREADR);
13390Sstevel@tonic-gate mutex_enter(QLOCK(qp));
13400Sstevel@tonic-gate qp->q_flag &= ~QWCLOSE;
13410Sstevel@tonic-gate mutex_exit(QLOCK(qp));
13420Sstevel@tonic-gate mutex_enter(QLOCK(wqp));
13430Sstevel@tonic-gate wqp->q_flag &= ~QWCLOSE;
13440Sstevel@tonic-gate mutex_exit(QLOCK(wqp));
13450Sstevel@tonic-gate }
13460Sstevel@tonic-gate
13470Sstevel@tonic-gate /*
13480Sstevel@tonic-gate * Remove queue from qhead/qtail if it is enabled.
13490Sstevel@tonic-gate * Only reset QENAB if the queue was removed from the runlist.
13500Sstevel@tonic-gate * A queue goes through 3 stages:
13510Sstevel@tonic-gate * It is on the service list and QENAB is set.
13520Sstevel@tonic-gate * It is removed from the service list but QENAB is still set.
13530Sstevel@tonic-gate * QENAB gets changed to QINSERVICE.
13540Sstevel@tonic-gate * QINSERVICE is reset (when the service procedure is done)
13550Sstevel@tonic-gate * Thus we can not reset QENAB unless we actually removed it from the service
13560Sstevel@tonic-gate * queue.
13570Sstevel@tonic-gate */
13580Sstevel@tonic-gate void
remove_runlist(queue_t * qp)13590Sstevel@tonic-gate remove_runlist(queue_t *qp)
13600Sstevel@tonic-gate {
13610Sstevel@tonic-gate if (qp->q_flag & QENAB && qhead != NULL) {
13620Sstevel@tonic-gate queue_t *q_chase;
13630Sstevel@tonic-gate queue_t *q_curr;
13640Sstevel@tonic-gate int removed;
13650Sstevel@tonic-gate
13660Sstevel@tonic-gate mutex_enter(&service_queue);
13670Sstevel@tonic-gate RMQ(qp, qhead, qtail, q_link, q_chase, q_curr, removed);
13680Sstevel@tonic-gate mutex_exit(&service_queue);
13690Sstevel@tonic-gate if (removed) {
13700Sstevel@tonic-gate STRSTAT(qremoved);
13710Sstevel@tonic-gate qp->q_flag &= ~QENAB;
13720Sstevel@tonic-gate }
13730Sstevel@tonic-gate }
13740Sstevel@tonic-gate }
13750Sstevel@tonic-gate
13760Sstevel@tonic-gate
13770Sstevel@tonic-gate /*
13789671SBrian.Ruthven@Sun.COM * Wait for any pending service processing to complete.
13790Sstevel@tonic-gate * The removal of queues from the runlist is not atomic with the
13800Sstevel@tonic-gate * clearing of the QENABLED flag and setting the INSERVICE flag.
13810Sstevel@tonic-gate * consequently it is possible for remove_runlist in strclose
13820Sstevel@tonic-gate * to not find the queue on the runlist but for it to be QENABLED
13830Sstevel@tonic-gate * and not yet INSERVICE -> hence wait_svc needs to check QENABLED
13840Sstevel@tonic-gate * as well as INSERVICE.
13850Sstevel@tonic-gate */
13860Sstevel@tonic-gate void
wait_svc(queue_t * qp)13870Sstevel@tonic-gate wait_svc(queue_t *qp)
13880Sstevel@tonic-gate {
13890Sstevel@tonic-gate queue_t *wqp = _WR(qp);
13900Sstevel@tonic-gate
13910Sstevel@tonic-gate ASSERT(qp->q_flag & QREADR);
13920Sstevel@tonic-gate
13930Sstevel@tonic-gate /*
13940Sstevel@tonic-gate * Try to remove queues from qhead/qtail list.
13950Sstevel@tonic-gate */
13960Sstevel@tonic-gate if (qhead != NULL) {
13970Sstevel@tonic-gate remove_runlist(qp);
13980Sstevel@tonic-gate remove_runlist(wqp);
13990Sstevel@tonic-gate }
14000Sstevel@tonic-gate /*
14019671SBrian.Ruthven@Sun.COM * Wait till the syncqs associated with the queue disappear from the
14029671SBrian.Ruthven@Sun.COM * background processing list.
14030Sstevel@tonic-gate * This only needs to be done for non-PERMOD perimeters since
14040Sstevel@tonic-gate * for PERMOD perimeters the syncq may be shared and will only be freed
14050Sstevel@tonic-gate * when the last module/driver is unloaded.
14060Sstevel@tonic-gate * If for PERMOD perimeters queue was on the syncq list, removeq()
14070Sstevel@tonic-gate * should call propagate_syncq() or drain_syncq() for it. Both of these
14089671SBrian.Ruthven@Sun.COM * functions remove the queue from its syncq list, so sqthread will not
14090Sstevel@tonic-gate * try to access the queue.
14100Sstevel@tonic-gate */
14110Sstevel@tonic-gate if (!(qp->q_flag & QPERMOD)) {
14120Sstevel@tonic-gate syncq_t *rsq = qp->q_syncq;
14130Sstevel@tonic-gate syncq_t *wsq = wqp->q_syncq;
14140Sstevel@tonic-gate
14150Sstevel@tonic-gate /*
14160Sstevel@tonic-gate * Disable rsq and wsq and wait for any background processing of
14170Sstevel@tonic-gate * syncq to complete.
14180Sstevel@tonic-gate */
14190Sstevel@tonic-gate wait_sq_svc(rsq);
14200Sstevel@tonic-gate if (wsq != rsq)
14210Sstevel@tonic-gate wait_sq_svc(wsq);
14220Sstevel@tonic-gate }
14230Sstevel@tonic-gate
14240Sstevel@tonic-gate mutex_enter(QLOCK(qp));
14250Sstevel@tonic-gate while (qp->q_flag & (QINSERVICE|QENAB))
14260Sstevel@tonic-gate cv_wait(&qp->q_wait, QLOCK(qp));
14270Sstevel@tonic-gate mutex_exit(QLOCK(qp));
14280Sstevel@tonic-gate mutex_enter(QLOCK(wqp));
14290Sstevel@tonic-gate while (wqp->q_flag & (QINSERVICE|QENAB))
14300Sstevel@tonic-gate cv_wait(&wqp->q_wait, QLOCK(wqp));
14310Sstevel@tonic-gate mutex_exit(QLOCK(wqp));
14320Sstevel@tonic-gate }
14330Sstevel@tonic-gate
14340Sstevel@tonic-gate /*
14350Sstevel@tonic-gate * Put ioctl data from userland buffer `arg' into the mblk chain `bp'.
14360Sstevel@tonic-gate * `flag' must always contain either K_TO_K or U_TO_K; STR_NOSIG may
14370Sstevel@tonic-gate * also be set, and is passed through to allocb_cred_wait().
14380Sstevel@tonic-gate *
14390Sstevel@tonic-gate * Returns errno on failure, zero on success.
14400Sstevel@tonic-gate */
14410Sstevel@tonic-gate int
putiocd(mblk_t * bp,char * arg,int flag,cred_t * cr)14420Sstevel@tonic-gate putiocd(mblk_t *bp, char *arg, int flag, cred_t *cr)
14430Sstevel@tonic-gate {
14440Sstevel@tonic-gate mblk_t *tmp;
14450Sstevel@tonic-gate ssize_t count;
14460Sstevel@tonic-gate int error = 0;
14470Sstevel@tonic-gate
14480Sstevel@tonic-gate ASSERT((flag & (U_TO_K | K_TO_K)) == U_TO_K ||
14495753Sgww (flag & (U_TO_K | K_TO_K)) == K_TO_K);
14500Sstevel@tonic-gate
14510Sstevel@tonic-gate if (bp->b_datap->db_type == M_IOCTL) {
14520Sstevel@tonic-gate count = ((struct iocblk *)bp->b_rptr)->ioc_count;
14530Sstevel@tonic-gate } else {
14540Sstevel@tonic-gate ASSERT(bp->b_datap->db_type == M_COPYIN);
14550Sstevel@tonic-gate count = ((struct copyreq *)bp->b_rptr)->cq_size;
14560Sstevel@tonic-gate }
14570Sstevel@tonic-gate /*
14580Sstevel@tonic-gate * strdoioctl validates ioc_count, so if this assert fails it
14590Sstevel@tonic-gate * cannot be due to user error.
14600Sstevel@tonic-gate */
14610Sstevel@tonic-gate ASSERT(count >= 0);
14620Sstevel@tonic-gate
14638778SErik.Nordmark@Sun.COM if ((tmp = allocb_cred_wait(count, (flag & STR_NOSIG), &error, cr,
14648778SErik.Nordmark@Sun.COM curproc->p_pid)) == NULL) {
14656974Sgd78059 return (error);
14666974Sgd78059 }
14676974Sgd78059 error = strcopyin(arg, tmp->b_wptr, count, flag & (U_TO_K|K_TO_K));
14686974Sgd78059 if (error != 0) {
14696974Sgd78059 freeb(tmp);
14706974Sgd78059 return (error);
14716974Sgd78059 }
14726974Sgd78059 DB_CPID(tmp) = curproc->p_pid;
14736974Sgd78059 tmp->b_wptr += count;
14746974Sgd78059 bp->b_cont = tmp;
14750Sstevel@tonic-gate
14760Sstevel@tonic-gate return (0);
14770Sstevel@tonic-gate }
14780Sstevel@tonic-gate
14790Sstevel@tonic-gate /*
14800Sstevel@tonic-gate * Copy ioctl data to user-land. Return non-zero errno on failure,
14810Sstevel@tonic-gate * 0 for success.
14820Sstevel@tonic-gate */
14830Sstevel@tonic-gate int
getiocd(mblk_t * bp,char * arg,int copymode)14840Sstevel@tonic-gate getiocd(mblk_t *bp, char *arg, int copymode)
14850Sstevel@tonic-gate {
14860Sstevel@tonic-gate ssize_t count;
14870Sstevel@tonic-gate size_t n;
14880Sstevel@tonic-gate int error;
14890Sstevel@tonic-gate
14900Sstevel@tonic-gate if (bp->b_datap->db_type == M_IOCACK)
14910Sstevel@tonic-gate count = ((struct iocblk *)bp->b_rptr)->ioc_count;
14920Sstevel@tonic-gate else {
14930Sstevel@tonic-gate ASSERT(bp->b_datap->db_type == M_COPYOUT);
14940Sstevel@tonic-gate count = ((struct copyreq *)bp->b_rptr)->cq_size;
14950Sstevel@tonic-gate }
14960Sstevel@tonic-gate ASSERT(count >= 0);
14970Sstevel@tonic-gate
14980Sstevel@tonic-gate for (bp = bp->b_cont; bp && count;
14990Sstevel@tonic-gate count -= n, bp = bp->b_cont, arg += n) {
15000Sstevel@tonic-gate n = MIN(count, bp->b_wptr - bp->b_rptr);
15010Sstevel@tonic-gate error = strcopyout(bp->b_rptr, arg, n, copymode);
15020Sstevel@tonic-gate if (error)
15030Sstevel@tonic-gate return (error);
15040Sstevel@tonic-gate }
15050Sstevel@tonic-gate ASSERT(count == 0);
15060Sstevel@tonic-gate return (0);
15070Sstevel@tonic-gate }
15080Sstevel@tonic-gate
15090Sstevel@tonic-gate /*
15100Sstevel@tonic-gate * Allocate a linkinfo entry given the write queue of the
15110Sstevel@tonic-gate * bottom module of the top stream and the write queue of the
15120Sstevel@tonic-gate * stream head of the bottom stream.
15130Sstevel@tonic-gate */
15140Sstevel@tonic-gate linkinfo_t *
alloclink(queue_t * qup,queue_t * qdown,file_t * fpdown)15150Sstevel@tonic-gate alloclink(queue_t *qup, queue_t *qdown, file_t *fpdown)
15160Sstevel@tonic-gate {
15170Sstevel@tonic-gate linkinfo_t *linkp;
15180Sstevel@tonic-gate
15190Sstevel@tonic-gate linkp = kmem_cache_alloc(linkinfo_cache, KM_SLEEP);
15200Sstevel@tonic-gate
15210Sstevel@tonic-gate linkp->li_lblk.l_qtop = qup;
15220Sstevel@tonic-gate linkp->li_lblk.l_qbot = qdown;
15230Sstevel@tonic-gate linkp->li_fpdown = fpdown;
15240Sstevel@tonic-gate
15250Sstevel@tonic-gate mutex_enter(&strresources);
15260Sstevel@tonic-gate linkp->li_next = linkinfo_list;
15270Sstevel@tonic-gate linkp->li_prev = NULL;
15280Sstevel@tonic-gate if (linkp->li_next)
15290Sstevel@tonic-gate linkp->li_next->li_prev = linkp;
15300Sstevel@tonic-gate linkinfo_list = linkp;
15310Sstevel@tonic-gate linkp->li_lblk.l_index = ++lnk_id;
15320Sstevel@tonic-gate ASSERT(lnk_id != 0); /* this should never wrap in practice */
15330Sstevel@tonic-gate mutex_exit(&strresources);
15340Sstevel@tonic-gate
15350Sstevel@tonic-gate return (linkp);
15360Sstevel@tonic-gate }
15370Sstevel@tonic-gate
15380Sstevel@tonic-gate /*
15390Sstevel@tonic-gate * Free a linkinfo entry.
15400Sstevel@tonic-gate */
15410Sstevel@tonic-gate void
lbfree(linkinfo_t * linkp)15420Sstevel@tonic-gate lbfree(linkinfo_t *linkp)
15430Sstevel@tonic-gate {
15440Sstevel@tonic-gate mutex_enter(&strresources);
15450Sstevel@tonic-gate if (linkp->li_next)
15460Sstevel@tonic-gate linkp->li_next->li_prev = linkp->li_prev;
15470Sstevel@tonic-gate if (linkp->li_prev)
15480Sstevel@tonic-gate linkp->li_prev->li_next = linkp->li_next;
15490Sstevel@tonic-gate else
15500Sstevel@tonic-gate linkinfo_list = linkp->li_next;
15510Sstevel@tonic-gate mutex_exit(&strresources);
15520Sstevel@tonic-gate
15530Sstevel@tonic-gate kmem_cache_free(linkinfo_cache, linkp);
15540Sstevel@tonic-gate }
15550Sstevel@tonic-gate
15560Sstevel@tonic-gate /*
15570Sstevel@tonic-gate * Check for a potential linking cycle.
15580Sstevel@tonic-gate * Return 1 if a link will result in a cycle,
15590Sstevel@tonic-gate * and 0 otherwise.
15600Sstevel@tonic-gate */
15610Sstevel@tonic-gate int
linkcycle(stdata_t * upstp,stdata_t * lostp,str_stack_t * ss)15623448Sdh155122 linkcycle(stdata_t *upstp, stdata_t *lostp, str_stack_t *ss)
15630Sstevel@tonic-gate {
15640Sstevel@tonic-gate struct mux_node *np;
15650Sstevel@tonic-gate struct mux_edge *ep;
15660Sstevel@tonic-gate int i;
15670Sstevel@tonic-gate major_t lomaj;
15680Sstevel@tonic-gate major_t upmaj;
15690Sstevel@tonic-gate /*
15700Sstevel@tonic-gate * if the lower stream is a pipe/FIFO, return, since link
15710Sstevel@tonic-gate * cycles can not happen on pipes/FIFOs
15720Sstevel@tonic-gate */
15730Sstevel@tonic-gate if (lostp->sd_vnode->v_type == VFIFO)
15740Sstevel@tonic-gate return (0);
15750Sstevel@tonic-gate
15763448Sdh155122 for (i = 0; i < ss->ss_devcnt; i++) {
15773448Sdh155122 np = &ss->ss_mux_nodes[i];
15780Sstevel@tonic-gate MUX_CLEAR(np);
15790Sstevel@tonic-gate }
15800Sstevel@tonic-gate lomaj = getmajor(lostp->sd_vnode->v_rdev);
15810Sstevel@tonic-gate upmaj = getmajor(upstp->sd_vnode->v_rdev);
15823448Sdh155122 np = &ss->ss_mux_nodes[lomaj];
15830Sstevel@tonic-gate for (;;) {
15840Sstevel@tonic-gate if (!MUX_DIDVISIT(np)) {
15850Sstevel@tonic-gate if (np->mn_imaj == upmaj)
15860Sstevel@tonic-gate return (1);
15870Sstevel@tonic-gate if (np->mn_outp == NULL) {
15880Sstevel@tonic-gate MUX_VISIT(np);
15890Sstevel@tonic-gate if (np->mn_originp == NULL)
15900Sstevel@tonic-gate return (0);
15910Sstevel@tonic-gate np = np->mn_originp;
15920Sstevel@tonic-gate continue;
15930Sstevel@tonic-gate }
15940Sstevel@tonic-gate MUX_VISIT(np);
15950Sstevel@tonic-gate np->mn_startp = np->mn_outp;
15960Sstevel@tonic-gate } else {
15970Sstevel@tonic-gate if (np->mn_startp == NULL) {
15980Sstevel@tonic-gate if (np->mn_originp == NULL)
15990Sstevel@tonic-gate return (0);
16000Sstevel@tonic-gate else {
16010Sstevel@tonic-gate np = np->mn_originp;
16020Sstevel@tonic-gate continue;
16030Sstevel@tonic-gate }
16040Sstevel@tonic-gate }
16050Sstevel@tonic-gate /*
16060Sstevel@tonic-gate * If ep->me_nodep is a FIFO (me_nodep == NULL),
16070Sstevel@tonic-gate * ignore the edge and move on. ep->me_nodep gets
16080Sstevel@tonic-gate * set to NULL in mux_addedge() if it is a FIFO.
16090Sstevel@tonic-gate *
16100Sstevel@tonic-gate */
16110Sstevel@tonic-gate ep = np->mn_startp;
16120Sstevel@tonic-gate np->mn_startp = ep->me_nextp;
16130Sstevel@tonic-gate if (ep->me_nodep == NULL)
16140Sstevel@tonic-gate continue;
16150Sstevel@tonic-gate ep->me_nodep->mn_originp = np;
16160Sstevel@tonic-gate np = ep->me_nodep;
16170Sstevel@tonic-gate }
16180Sstevel@tonic-gate }
16190Sstevel@tonic-gate }
16200Sstevel@tonic-gate
16210Sstevel@tonic-gate /*
16220Sstevel@tonic-gate * Find linkinfo entry corresponding to the parameters.
16230Sstevel@tonic-gate */
16240Sstevel@tonic-gate linkinfo_t *
findlinks(stdata_t * stp,int index,int type,str_stack_t * ss)16253448Sdh155122 findlinks(stdata_t *stp, int index, int type, str_stack_t *ss)
16260Sstevel@tonic-gate {
16270Sstevel@tonic-gate linkinfo_t *linkp;
16280Sstevel@tonic-gate struct mux_edge *mep;
16290Sstevel@tonic-gate struct mux_node *mnp;
16300Sstevel@tonic-gate queue_t *qup;
16310Sstevel@tonic-gate
16320Sstevel@tonic-gate mutex_enter(&strresources);
16330Sstevel@tonic-gate if ((type & LINKTYPEMASK) == LINKNORMAL) {
16340Sstevel@tonic-gate qup = getendq(stp->sd_wrq);
16350Sstevel@tonic-gate for (linkp = linkinfo_list; linkp; linkp = linkp->li_next) {
16360Sstevel@tonic-gate if ((qup == linkp->li_lblk.l_qtop) &&
16370Sstevel@tonic-gate (!index || (index == linkp->li_lblk.l_index))) {
16380Sstevel@tonic-gate mutex_exit(&strresources);
16390Sstevel@tonic-gate return (linkp);
16400Sstevel@tonic-gate }
16410Sstevel@tonic-gate }
16420Sstevel@tonic-gate } else {
16430Sstevel@tonic-gate ASSERT((type & LINKTYPEMASK) == LINKPERSIST);
16443448Sdh155122 mnp = &ss->ss_mux_nodes[getmajor(stp->sd_vnode->v_rdev)];
16450Sstevel@tonic-gate mep = mnp->mn_outp;
16460Sstevel@tonic-gate while (mep) {
16470Sstevel@tonic-gate if ((index == 0) || (index == mep->me_muxid))
16480Sstevel@tonic-gate break;
16490Sstevel@tonic-gate mep = mep->me_nextp;
16500Sstevel@tonic-gate }
16510Sstevel@tonic-gate if (!mep) {
16520Sstevel@tonic-gate mutex_exit(&strresources);
16530Sstevel@tonic-gate return (NULL);
16540Sstevel@tonic-gate }
16550Sstevel@tonic-gate for (linkp = linkinfo_list; linkp; linkp = linkp->li_next) {
16560Sstevel@tonic-gate if ((!linkp->li_lblk.l_qtop) &&
16570Sstevel@tonic-gate (mep->me_muxid == linkp->li_lblk.l_index)) {
16580Sstevel@tonic-gate mutex_exit(&strresources);
16590Sstevel@tonic-gate return (linkp);
16600Sstevel@tonic-gate }
16610Sstevel@tonic-gate }
16620Sstevel@tonic-gate }
16630Sstevel@tonic-gate mutex_exit(&strresources);
16640Sstevel@tonic-gate return (NULL);
16650Sstevel@tonic-gate }
16660Sstevel@tonic-gate
16670Sstevel@tonic-gate /*
16680Sstevel@tonic-gate * Given a queue ptr, follow the chain of q_next pointers until you reach the
16690Sstevel@tonic-gate * last queue on the chain and return it.
16700Sstevel@tonic-gate */
16710Sstevel@tonic-gate queue_t *
getendq(queue_t * q)16720Sstevel@tonic-gate getendq(queue_t *q)
16730Sstevel@tonic-gate {
16740Sstevel@tonic-gate ASSERT(q != NULL);
16750Sstevel@tonic-gate while (_SAMESTR(q))
16760Sstevel@tonic-gate q = q->q_next;
16770Sstevel@tonic-gate return (q);
16780Sstevel@tonic-gate }
16790Sstevel@tonic-gate
16800Sstevel@tonic-gate /*
16819671SBrian.Ruthven@Sun.COM * Wait for the syncq count to drop to zero.
16820Sstevel@tonic-gate * sq could be either outer or inner.
16830Sstevel@tonic-gate */
16840Sstevel@tonic-gate
16850Sstevel@tonic-gate static void
wait_syncq(syncq_t * sq)16860Sstevel@tonic-gate wait_syncq(syncq_t *sq)
16870Sstevel@tonic-gate {
16880Sstevel@tonic-gate uint16_t count;
16890Sstevel@tonic-gate
16900Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
16910Sstevel@tonic-gate count = sq->sq_count;
16920Sstevel@tonic-gate SQ_PUTLOCKS_ENTER(sq);
16930Sstevel@tonic-gate SUM_SQ_PUTCOUNTS(sq, count);
16940Sstevel@tonic-gate while (count != 0) {
16950Sstevel@tonic-gate sq->sq_flags |= SQ_WANTWAKEUP;
16960Sstevel@tonic-gate SQ_PUTLOCKS_EXIT(sq);
16970Sstevel@tonic-gate cv_wait(&sq->sq_wait, SQLOCK(sq));
16980Sstevel@tonic-gate count = sq->sq_count;
16990Sstevel@tonic-gate SQ_PUTLOCKS_ENTER(sq);
17000Sstevel@tonic-gate SUM_SQ_PUTCOUNTS(sq, count);
17010Sstevel@tonic-gate }
17020Sstevel@tonic-gate SQ_PUTLOCKS_EXIT(sq);
17030Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
17040Sstevel@tonic-gate }
17050Sstevel@tonic-gate
17060Sstevel@tonic-gate /*
17070Sstevel@tonic-gate * Wait while there are any messages for the queue in its syncq.
17080Sstevel@tonic-gate */
17090Sstevel@tonic-gate static void
wait_q_syncq(queue_t * q)17100Sstevel@tonic-gate wait_q_syncq(queue_t *q)
17110Sstevel@tonic-gate {
17120Sstevel@tonic-gate if ((q->q_sqflags & Q_SQQUEUED) || (q->q_syncqmsgs > 0)) {
17130Sstevel@tonic-gate syncq_t *sq = q->q_syncq;
17140Sstevel@tonic-gate
17150Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
17160Sstevel@tonic-gate while ((q->q_sqflags & Q_SQQUEUED) || (q->q_syncqmsgs > 0)) {
17170Sstevel@tonic-gate sq->sq_flags |= SQ_WANTWAKEUP;
17180Sstevel@tonic-gate cv_wait(&sq->sq_wait, SQLOCK(sq));
17190Sstevel@tonic-gate }
17200Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
17210Sstevel@tonic-gate }
17220Sstevel@tonic-gate }
17230Sstevel@tonic-gate
17240Sstevel@tonic-gate
17250Sstevel@tonic-gate int
mlink_file(vnode_t * vp,int cmd,struct file * fpdown,cred_t * crp,int * rvalp,int lhlink)17260Sstevel@tonic-gate mlink_file(vnode_t *vp, int cmd, struct file *fpdown, cred_t *crp, int *rvalp,
17270Sstevel@tonic-gate int lhlink)
17280Sstevel@tonic-gate {
17290Sstevel@tonic-gate struct stdata *stp;
17300Sstevel@tonic-gate struct strioctl strioc;
17310Sstevel@tonic-gate struct linkinfo *linkp;
17320Sstevel@tonic-gate struct stdata *stpdown;
17330Sstevel@tonic-gate struct streamtab *str;
17340Sstevel@tonic-gate queue_t *passq;
17350Sstevel@tonic-gate syncq_t *passyncq;
17360Sstevel@tonic-gate queue_t *rq;
17370Sstevel@tonic-gate cdevsw_impl_t *dp;
17380Sstevel@tonic-gate uint32_t qflag;
17390Sstevel@tonic-gate uint32_t sqtype;
17400Sstevel@tonic-gate perdm_t *dmp;
17410Sstevel@tonic-gate int error = 0;
17423448Sdh155122 netstack_t *ns;
17433448Sdh155122 str_stack_t *ss;
17440Sstevel@tonic-gate
17450Sstevel@tonic-gate stp = vp->v_stream;
17460Sstevel@tonic-gate TRACE_1(TR_FAC_STREAMS_FR,
17475753Sgww TR_I_LINK, "I_LINK/I_PLINK:stp %p", stp);
17480Sstevel@tonic-gate /*
17490Sstevel@tonic-gate * Test for invalid upper stream
17500Sstevel@tonic-gate */
17510Sstevel@tonic-gate if (stp->sd_flag & STRHUP) {
17520Sstevel@tonic-gate return (ENXIO);
17530Sstevel@tonic-gate }
17540Sstevel@tonic-gate if (vp->v_type == VFIFO) {
17550Sstevel@tonic-gate return (EINVAL);
17560Sstevel@tonic-gate }
17570Sstevel@tonic-gate if (stp->sd_strtab == NULL) {
17580Sstevel@tonic-gate return (EINVAL);
17590Sstevel@tonic-gate }
17600Sstevel@tonic-gate if (!stp->sd_strtab->st_muxwinit) {
17610Sstevel@tonic-gate return (EINVAL);
17620Sstevel@tonic-gate }
17630Sstevel@tonic-gate if (fpdown == NULL) {
17640Sstevel@tonic-gate return (EBADF);
17650Sstevel@tonic-gate }
17663448Sdh155122 ns = netstack_find_by_cred(crp);
17673448Sdh155122 ASSERT(ns != NULL);
17683448Sdh155122 ss = ns->netstack_str;
17693448Sdh155122 ASSERT(ss != NULL);
17703448Sdh155122
17713448Sdh155122 if (getmajor(stp->sd_vnode->v_rdev) >= ss->ss_devcnt) {
17723448Sdh155122 netstack_rele(ss->ss_netstack);
17730Sstevel@tonic-gate return (EINVAL);
17740Sstevel@tonic-gate }
17750Sstevel@tonic-gate mutex_enter(&muxifier);
17760Sstevel@tonic-gate if (stp->sd_flag & STPLEX) {
17770Sstevel@tonic-gate mutex_exit(&muxifier);
17783448Sdh155122 netstack_rele(ss->ss_netstack);
17790Sstevel@tonic-gate return (ENXIO);
17800Sstevel@tonic-gate }
17810Sstevel@tonic-gate
17820Sstevel@tonic-gate /*
17830Sstevel@tonic-gate * Test for invalid lower stream.
17840Sstevel@tonic-gate * The check for the v_type != VFIFO and having a major
17850Sstevel@tonic-gate * number not >= devcnt is done to avoid problems with
17860Sstevel@tonic-gate * adding mux_node entry past the end of mux_nodes[].
17870Sstevel@tonic-gate * For FIFO's we don't add an entry so this isn't a
17880Sstevel@tonic-gate * problem.
17890Sstevel@tonic-gate */
17900Sstevel@tonic-gate if (((stpdown = fpdown->f_vnode->v_stream) == NULL) ||
17910Sstevel@tonic-gate (stpdown == stp) || (stpdown->sd_flag &
17920Sstevel@tonic-gate (STPLEX|STRHUP|STRDERR|STWRERR|IOCWAIT|STRPLUMB)) ||
17930Sstevel@tonic-gate ((stpdown->sd_vnode->v_type != VFIFO) &&
17943448Sdh155122 (getmajor(stpdown->sd_vnode->v_rdev) >= ss->ss_devcnt)) ||
17953448Sdh155122 linkcycle(stp, stpdown, ss)) {
17960Sstevel@tonic-gate mutex_exit(&muxifier);
17973448Sdh155122 netstack_rele(ss->ss_netstack);
17980Sstevel@tonic-gate return (EINVAL);
17990Sstevel@tonic-gate }
18000Sstevel@tonic-gate TRACE_1(TR_FAC_STREAMS_FR,
18015753Sgww TR_STPDOWN, "stpdown:%p", stpdown);
18020Sstevel@tonic-gate rq = getendq(stp->sd_wrq);
18030Sstevel@tonic-gate if (cmd == I_PLINK)
18040Sstevel@tonic-gate rq = NULL;
18050Sstevel@tonic-gate
18060Sstevel@tonic-gate linkp = alloclink(rq, stpdown->sd_wrq, fpdown);
18070Sstevel@tonic-gate
18080Sstevel@tonic-gate strioc.ic_cmd = cmd;
18090Sstevel@tonic-gate strioc.ic_timout = INFTIM;
18100Sstevel@tonic-gate strioc.ic_len = sizeof (struct linkblk);
18110Sstevel@tonic-gate strioc.ic_dp = (char *)&linkp->li_lblk;
18120Sstevel@tonic-gate
18130Sstevel@tonic-gate /*
18140Sstevel@tonic-gate * STRPLUMB protects plumbing changes and should be set before
18150Sstevel@tonic-gate * link_addpassthru()/link_rempassthru() are called, so it is set here
18160Sstevel@tonic-gate * and cleared in the end of mlink when passthru queue is removed.
18170Sstevel@tonic-gate * Setting of STRPLUMB prevents reopens of the stream while passthru
18180Sstevel@tonic-gate * queue is in-place (it is not a proper module and doesn't have open
18190Sstevel@tonic-gate * entry point).
18200Sstevel@tonic-gate *
18210Sstevel@tonic-gate * STPLEX prevents any threads from entering the stream from above. It
18220Sstevel@tonic-gate * can't be set before the call to link_addpassthru() because putnext
18230Sstevel@tonic-gate * from below may cause stream head I/O routines to be called and these
18240Sstevel@tonic-gate * routines assert that STPLEX is not set. After link_addpassthru()
18250Sstevel@tonic-gate * nothing may come from below since the pass queue syncq is blocked.
18260Sstevel@tonic-gate * Note also that STPLEX should be cleared before the call to
18279671SBrian.Ruthven@Sun.COM * link_rempassthru() since when messages start flowing to the stream
18280Sstevel@tonic-gate * head (e.g. because of message propagation from the pass queue) stream
18290Sstevel@tonic-gate * head I/O routines may be called with STPLEX flag set.
18300Sstevel@tonic-gate *
18310Sstevel@tonic-gate * When STPLEX is set, nothing may come into the stream from above and
18320Sstevel@tonic-gate * it is safe to do a setq which will change stream head. So, the
18330Sstevel@tonic-gate * correct sequence of actions is:
18340Sstevel@tonic-gate *
18350Sstevel@tonic-gate * 1) Set STRPLUMB
18360Sstevel@tonic-gate * 2) Call link_addpassthru()
18370Sstevel@tonic-gate * 3) Set STPLEX
18380Sstevel@tonic-gate * 4) Call setq and update the stream state
18390Sstevel@tonic-gate * 5) Clear STPLEX
18400Sstevel@tonic-gate * 6) Call link_rempassthru()
18410Sstevel@tonic-gate * 7) Clear STRPLUMB
18420Sstevel@tonic-gate *
18430Sstevel@tonic-gate * The same sequence applies to munlink() code.
18440Sstevel@tonic-gate */
18450Sstevel@tonic-gate mutex_enter(&stpdown->sd_lock);
18460Sstevel@tonic-gate stpdown->sd_flag |= STRPLUMB;
18470Sstevel@tonic-gate mutex_exit(&stpdown->sd_lock);
18480Sstevel@tonic-gate /*
18490Sstevel@tonic-gate * Add passthru queue below lower mux. This will block
18500Sstevel@tonic-gate * syncqs of lower muxs read queue during I_LINK/I_UNLINK.
18510Sstevel@tonic-gate */
18520Sstevel@tonic-gate passq = link_addpassthru(stpdown);
18530Sstevel@tonic-gate
18540Sstevel@tonic-gate mutex_enter(&stpdown->sd_lock);
18550Sstevel@tonic-gate stpdown->sd_flag |= STPLEX;
18560Sstevel@tonic-gate mutex_exit(&stpdown->sd_lock);
18570Sstevel@tonic-gate
18580Sstevel@tonic-gate rq = _RD(stpdown->sd_wrq);
18590Sstevel@tonic-gate /*
18600Sstevel@tonic-gate * There may be messages in the streamhead's syncq due to messages
18610Sstevel@tonic-gate * that arrived before link_addpassthru() was done. To avoid
18620Sstevel@tonic-gate * background processing of the syncq happening simultaneous with
18630Sstevel@tonic-gate * setq processing, we disable the streamhead syncq and wait until
18640Sstevel@tonic-gate * existing background thread finishes working on it.
18650Sstevel@tonic-gate */
18660Sstevel@tonic-gate wait_sq_svc(rq->q_syncq);
18670Sstevel@tonic-gate passyncq = passq->q_syncq;
18680Sstevel@tonic-gate if (!(passyncq->sq_flags & SQ_BLOCKED))
18690Sstevel@tonic-gate blocksq(passyncq, SQ_BLOCKED, 0);
18700Sstevel@tonic-gate
18710Sstevel@tonic-gate ASSERT((rq->q_flag & QMT_TYPEMASK) == QMTSAFE);
18720Sstevel@tonic-gate ASSERT(rq->q_syncq == SQ(rq) && _WR(rq)->q_syncq == SQ(rq));
18730Sstevel@tonic-gate rq->q_ptr = _WR(rq)->q_ptr = NULL;
18740Sstevel@tonic-gate
18750Sstevel@tonic-gate /* setq might sleep in allocator - avoid holding locks. */
18760Sstevel@tonic-gate /* Note: we are holding muxifier here. */
18770Sstevel@tonic-gate
18780Sstevel@tonic-gate str = stp->sd_strtab;
18790Sstevel@tonic-gate dp = &devimpl[getmajor(vp->v_rdev)];
18800Sstevel@tonic-gate ASSERT(dp->d_str == str);
18810Sstevel@tonic-gate
18820Sstevel@tonic-gate qflag = dp->d_qflag;
18830Sstevel@tonic-gate sqtype = dp->d_sqtype;
18840Sstevel@tonic-gate
18850Sstevel@tonic-gate /* create perdm_t if needed */
18860Sstevel@tonic-gate if (NEED_DM(dp->d_dmp, qflag))
18870Sstevel@tonic-gate dp->d_dmp = hold_dm(str, qflag, sqtype);
18880Sstevel@tonic-gate
18890Sstevel@tonic-gate dmp = dp->d_dmp;
18900Sstevel@tonic-gate
18910Sstevel@tonic-gate setq(rq, str->st_muxrinit, str->st_muxwinit, dmp, qflag, sqtype,
18920Sstevel@tonic-gate B_TRUE);
18930Sstevel@tonic-gate
18940Sstevel@tonic-gate /*
18950Sstevel@tonic-gate * XXX Remove any "odd" messages from the queue.
18960Sstevel@tonic-gate * Keep only M_DATA, M_PROTO, M_PCPROTO.
18970Sstevel@tonic-gate */
18980Sstevel@tonic-gate error = strdoioctl(stp, &strioc, FNATIVE,
18990Sstevel@tonic-gate K_TO_K | STR_NOERROR | STR_NOSIG, crp, rvalp);
19000Sstevel@tonic-gate if (error != 0) {
19010Sstevel@tonic-gate lbfree(linkp);
19020Sstevel@tonic-gate
19030Sstevel@tonic-gate if (!(passyncq->sq_flags & SQ_BLOCKED))
19040Sstevel@tonic-gate blocksq(passyncq, SQ_BLOCKED, 0);
19050Sstevel@tonic-gate /*
19060Sstevel@tonic-gate * Restore the stream head queue and then remove
19070Sstevel@tonic-gate * the passq. Turn off STPLEX before we turn on
19080Sstevel@tonic-gate * the stream by removing the passq.
19090Sstevel@tonic-gate */
19100Sstevel@tonic-gate rq->q_ptr = _WR(rq)->q_ptr = stpdown;
19110Sstevel@tonic-gate setq(rq, &strdata, &stwdata, NULL, QMTSAFE, SQ_CI|SQ_CO,
19120Sstevel@tonic-gate B_TRUE);
19130Sstevel@tonic-gate
19140Sstevel@tonic-gate mutex_enter(&stpdown->sd_lock);
19150Sstevel@tonic-gate stpdown->sd_flag &= ~STPLEX;
19160Sstevel@tonic-gate mutex_exit(&stpdown->sd_lock);
19170Sstevel@tonic-gate
19180Sstevel@tonic-gate link_rempassthru(passq);
19190Sstevel@tonic-gate
19200Sstevel@tonic-gate mutex_enter(&stpdown->sd_lock);
19210Sstevel@tonic-gate stpdown->sd_flag &= ~STRPLUMB;
19220Sstevel@tonic-gate /* Wakeup anyone waiting for STRPLUMB to clear. */
19230Sstevel@tonic-gate cv_broadcast(&stpdown->sd_monitor);
19240Sstevel@tonic-gate mutex_exit(&stpdown->sd_lock);
19250Sstevel@tonic-gate
19260Sstevel@tonic-gate mutex_exit(&muxifier);
19273448Sdh155122 netstack_rele(ss->ss_netstack);
19280Sstevel@tonic-gate return (error);
19290Sstevel@tonic-gate }
19300Sstevel@tonic-gate mutex_enter(&fpdown->f_tlock);
19310Sstevel@tonic-gate fpdown->f_count++;
19320Sstevel@tonic-gate mutex_exit(&fpdown->f_tlock);
19330Sstevel@tonic-gate
19340Sstevel@tonic-gate /*
19350Sstevel@tonic-gate * if we've made it here the linkage is all set up so we should also
19360Sstevel@tonic-gate * set up the layered driver linkages
19370Sstevel@tonic-gate */
19380Sstevel@tonic-gate
19390Sstevel@tonic-gate ASSERT((cmd == I_LINK) || (cmd == I_PLINK));
19400Sstevel@tonic-gate if (cmd == I_LINK) {
19410Sstevel@tonic-gate ldi_mlink_fp(stp, fpdown, lhlink, LINKNORMAL);
19420Sstevel@tonic-gate } else {
19430Sstevel@tonic-gate ldi_mlink_fp(stp, fpdown, lhlink, LINKPERSIST);
19440Sstevel@tonic-gate }
19450Sstevel@tonic-gate
19460Sstevel@tonic-gate link_rempassthru(passq);
19470Sstevel@tonic-gate
19483448Sdh155122 mux_addedge(stp, stpdown, linkp->li_lblk.l_index, ss);
19490Sstevel@tonic-gate
19500Sstevel@tonic-gate /*
19510Sstevel@tonic-gate * Mark the upper stream as having dependent links
19520Sstevel@tonic-gate * so that strclose can clean it up.
19530Sstevel@tonic-gate */
19540Sstevel@tonic-gate if (cmd == I_LINK) {
19550Sstevel@tonic-gate mutex_enter(&stp->sd_lock);
19560Sstevel@tonic-gate stp->sd_flag |= STRHASLINKS;
19570Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
19580Sstevel@tonic-gate }
19590Sstevel@tonic-gate /*
19600Sstevel@tonic-gate * Wake up any other processes that may have been
19610Sstevel@tonic-gate * waiting on the lower stream. These will all
19620Sstevel@tonic-gate * error out.
19630Sstevel@tonic-gate */
19640Sstevel@tonic-gate mutex_enter(&stpdown->sd_lock);
19650Sstevel@tonic-gate /* The passthru module is removed so we may release STRPLUMB */
19660Sstevel@tonic-gate stpdown->sd_flag &= ~STRPLUMB;
19670Sstevel@tonic-gate cv_broadcast(&rq->q_wait);
19680Sstevel@tonic-gate cv_broadcast(&_WR(rq)->q_wait);
19690Sstevel@tonic-gate cv_broadcast(&stpdown->sd_monitor);
19700Sstevel@tonic-gate mutex_exit(&stpdown->sd_lock);
19710Sstevel@tonic-gate mutex_exit(&muxifier);
19720Sstevel@tonic-gate *rvalp = linkp->li_lblk.l_index;
19733448Sdh155122 netstack_rele(ss->ss_netstack);
19740Sstevel@tonic-gate return (0);
19750Sstevel@tonic-gate }
19760Sstevel@tonic-gate
19770Sstevel@tonic-gate int
mlink(vnode_t * vp,int cmd,int arg,cred_t * crp,int * rvalp,int lhlink)19780Sstevel@tonic-gate mlink(vnode_t *vp, int cmd, int arg, cred_t *crp, int *rvalp, int lhlink)
19790Sstevel@tonic-gate {
19800Sstevel@tonic-gate int ret;
19810Sstevel@tonic-gate struct file *fpdown;
19820Sstevel@tonic-gate
19830Sstevel@tonic-gate fpdown = getf(arg);
19840Sstevel@tonic-gate ret = mlink_file(vp, cmd, fpdown, crp, rvalp, lhlink);
19850Sstevel@tonic-gate if (fpdown != NULL)
19860Sstevel@tonic-gate releasef(arg);
19870Sstevel@tonic-gate return (ret);
19880Sstevel@tonic-gate }
19890Sstevel@tonic-gate
19900Sstevel@tonic-gate /*
19910Sstevel@tonic-gate * Unlink a multiplexor link. Stp is the controlling stream for the
19920Sstevel@tonic-gate * link, and linkp points to the link's entry in the linkinfo list.
19930Sstevel@tonic-gate * The muxifier lock must be held on entry and is dropped on exit.
19940Sstevel@tonic-gate *
19950Sstevel@tonic-gate * NOTE : Currently it is assumed that mux would process all the messages
19960Sstevel@tonic-gate * sitting on it's queue before ACKing the UNLINK. It is the responsibility
19970Sstevel@tonic-gate * of the mux to handle all the messages that arrive before UNLINK.
19980Sstevel@tonic-gate * If the mux has to send down messages on its lower stream before
19990Sstevel@tonic-gate * ACKing I_UNLINK, then it *should* know to handle messages even
20000Sstevel@tonic-gate * after the UNLINK is acked (actually it should be able to handle till we
20010Sstevel@tonic-gate * re-block the read side of the pass queue here). If the mux does not
20020Sstevel@tonic-gate * open up the lower stream, any messages that arrive during UNLINK
20030Sstevel@tonic-gate * will be put in the stream head. In the case of lower stream opening
20040Sstevel@tonic-gate * up, some messages might land in the stream head depending on when
20050Sstevel@tonic-gate * the message arrived and when the read side of the pass queue was
20060Sstevel@tonic-gate * re-blocked.
20070Sstevel@tonic-gate */
20080Sstevel@tonic-gate int
munlink(stdata_t * stp,linkinfo_t * linkp,int flag,cred_t * crp,int * rvalp,str_stack_t * ss)20093448Sdh155122 munlink(stdata_t *stp, linkinfo_t *linkp, int flag, cred_t *crp, int *rvalp,
20103448Sdh155122 str_stack_t *ss)
20110Sstevel@tonic-gate {
20120Sstevel@tonic-gate struct strioctl strioc;
20130Sstevel@tonic-gate struct stdata *stpdown;
20140Sstevel@tonic-gate queue_t *rq, *wrq;
20150Sstevel@tonic-gate queue_t *passq;
20160Sstevel@tonic-gate syncq_t *passyncq;
20170Sstevel@tonic-gate int error = 0;
20180Sstevel@tonic-gate file_t *fpdown;
20190Sstevel@tonic-gate
20200Sstevel@tonic-gate ASSERT(MUTEX_HELD(&muxifier));
20210Sstevel@tonic-gate
20220Sstevel@tonic-gate stpdown = linkp->li_fpdown->f_vnode->v_stream;
20230Sstevel@tonic-gate
20240Sstevel@tonic-gate /*
20250Sstevel@tonic-gate * See the comment in mlink() concerning STRPLUMB/STPLEX flags.
20260Sstevel@tonic-gate */
20270Sstevel@tonic-gate mutex_enter(&stpdown->sd_lock);
20280Sstevel@tonic-gate stpdown->sd_flag |= STRPLUMB;
20290Sstevel@tonic-gate mutex_exit(&stpdown->sd_lock);
20300Sstevel@tonic-gate
20310Sstevel@tonic-gate /*
20320Sstevel@tonic-gate * Add passthru queue below lower mux. This will block
20330Sstevel@tonic-gate * syncqs of lower muxs read queue during I_LINK/I_UNLINK.
20340Sstevel@tonic-gate */
20350Sstevel@tonic-gate passq = link_addpassthru(stpdown);
20360Sstevel@tonic-gate
20370Sstevel@tonic-gate if ((flag & LINKTYPEMASK) == LINKNORMAL)
20380Sstevel@tonic-gate strioc.ic_cmd = I_UNLINK;
20390Sstevel@tonic-gate else
20400Sstevel@tonic-gate strioc.ic_cmd = I_PUNLINK;
20410Sstevel@tonic-gate strioc.ic_timout = INFTIM;
20420Sstevel@tonic-gate strioc.ic_len = sizeof (struct linkblk);
20430Sstevel@tonic-gate strioc.ic_dp = (char *)&linkp->li_lblk;
20440Sstevel@tonic-gate
20450Sstevel@tonic-gate error = strdoioctl(stp, &strioc, FNATIVE,
20460Sstevel@tonic-gate K_TO_K | STR_NOERROR | STR_NOSIG, crp, rvalp);
20470Sstevel@tonic-gate
20480Sstevel@tonic-gate /*
20490Sstevel@tonic-gate * If there was an error and this is not called via strclose,
20500Sstevel@tonic-gate * return to the user. Otherwise, pretend there was no error
20510Sstevel@tonic-gate * and close the link.
20520Sstevel@tonic-gate */
20530Sstevel@tonic-gate if (error) {
20540Sstevel@tonic-gate if (flag & LINKCLOSE) {
20550Sstevel@tonic-gate cmn_err(CE_WARN, "KERNEL: munlink: could not perform "
20560Sstevel@tonic-gate "unlink ioctl, closing anyway (%d)\n", error);
20570Sstevel@tonic-gate } else {
20580Sstevel@tonic-gate link_rempassthru(passq);
20590Sstevel@tonic-gate mutex_enter(&stpdown->sd_lock);
20600Sstevel@tonic-gate stpdown->sd_flag &= ~STRPLUMB;
20610Sstevel@tonic-gate cv_broadcast(&stpdown->sd_monitor);
20620Sstevel@tonic-gate mutex_exit(&stpdown->sd_lock);
20630Sstevel@tonic-gate mutex_exit(&muxifier);
20640Sstevel@tonic-gate return (error);
20650Sstevel@tonic-gate }
20660Sstevel@tonic-gate }
20670Sstevel@tonic-gate
20683448Sdh155122 mux_rmvedge(stp, linkp->li_lblk.l_index, ss);
20690Sstevel@tonic-gate fpdown = linkp->li_fpdown;
20700Sstevel@tonic-gate lbfree(linkp);
20710Sstevel@tonic-gate
20720Sstevel@tonic-gate /*
20730Sstevel@tonic-gate * We go ahead and drop muxifier here--it's a nasty global lock that
20740Sstevel@tonic-gate * can slow others down. It's okay to since attempts to mlink() this
20750Sstevel@tonic-gate * stream will be stopped because STPLEX is still set in the stdata
20760Sstevel@tonic-gate * structure, and munlink() is stopped because mux_rmvedge() and
20770Sstevel@tonic-gate * lbfree() have removed it from mux_nodes[] and linkinfo_list,
20780Sstevel@tonic-gate * respectively. Note that we defer the closef() of fpdown until
20790Sstevel@tonic-gate * after we drop muxifier since strclose() can call munlinkall().
20800Sstevel@tonic-gate */
20810Sstevel@tonic-gate mutex_exit(&muxifier);
20820Sstevel@tonic-gate
20830Sstevel@tonic-gate wrq = stpdown->sd_wrq;
20840Sstevel@tonic-gate rq = _RD(wrq);
20850Sstevel@tonic-gate
20860Sstevel@tonic-gate /*
20870Sstevel@tonic-gate * Get rid of outstanding service procedure runs, before we make
20880Sstevel@tonic-gate * it a stream head, since a stream head doesn't have any service
20890Sstevel@tonic-gate * procedure.
20900Sstevel@tonic-gate */
20910Sstevel@tonic-gate disable_svc(rq);
20920Sstevel@tonic-gate wait_svc(rq);
20930Sstevel@tonic-gate
20940Sstevel@tonic-gate /*
20950Sstevel@tonic-gate * Since we don't disable the syncq for QPERMOD, we wait for whatever
20960Sstevel@tonic-gate * is queued up to be finished. mux should take care that nothing is
20970Sstevel@tonic-gate * send down to this queue. We should do it now as we're going to block
20980Sstevel@tonic-gate * passyncq if it was unblocked.
20990Sstevel@tonic-gate */
21000Sstevel@tonic-gate if (wrq->q_flag & QPERMOD) {
21010Sstevel@tonic-gate syncq_t *sq = wrq->q_syncq;
21020Sstevel@tonic-gate
21030Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
21040Sstevel@tonic-gate while (wrq->q_sqflags & Q_SQQUEUED) {
21050Sstevel@tonic-gate sq->sq_flags |= SQ_WANTWAKEUP;
21060Sstevel@tonic-gate cv_wait(&sq->sq_wait, SQLOCK(sq));
21070Sstevel@tonic-gate }
21080Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
21090Sstevel@tonic-gate }
21100Sstevel@tonic-gate passyncq = passq->q_syncq;
21110Sstevel@tonic-gate if (!(passyncq->sq_flags & SQ_BLOCKED)) {
21120Sstevel@tonic-gate
21130Sstevel@tonic-gate syncq_t *sq, *outer;
21140Sstevel@tonic-gate
21150Sstevel@tonic-gate /*
21160Sstevel@tonic-gate * Messages could be flowing from underneath. We will
21170Sstevel@tonic-gate * block the read side of the passq. This would be
21180Sstevel@tonic-gate * sufficient for QPAIR and QPERQ muxes to ensure
21190Sstevel@tonic-gate * that no data is flowing up into this queue
21200Sstevel@tonic-gate * and hence no thread active in this instance of
21210Sstevel@tonic-gate * lower mux. But for QPERMOD and QMTOUTPERIM there
21220Sstevel@tonic-gate * could be messages on the inner and outer/inner
21230Sstevel@tonic-gate * syncqs respectively. We will wait for them to drain.
21240Sstevel@tonic-gate * Because passq is blocked messages end up in the syncq
21250Sstevel@tonic-gate * And qfill_syncq could possibly end up setting QFULL
21260Sstevel@tonic-gate * which will access the rq->q_flag. Hence, we have to
21270Sstevel@tonic-gate * acquire the QLOCK in setq.
21280Sstevel@tonic-gate *
21290Sstevel@tonic-gate * XXX Messages can also flow from top into this
21300Sstevel@tonic-gate * queue though the unlink is over (Ex. some instance
21310Sstevel@tonic-gate * in putnext() called from top that has still not
21320Sstevel@tonic-gate * accessed this queue. And also putq(lowerq) ?).
21330Sstevel@tonic-gate * Solution : How about blocking the l_qtop queue ?
21340Sstevel@tonic-gate * Do we really care about such pure D_MP muxes ?
21350Sstevel@tonic-gate */
21360Sstevel@tonic-gate
21370Sstevel@tonic-gate blocksq(passyncq, SQ_BLOCKED, 0);
21380Sstevel@tonic-gate
21390Sstevel@tonic-gate sq = rq->q_syncq;
21400Sstevel@tonic-gate if ((outer = sq->sq_outer) != NULL) {
21410Sstevel@tonic-gate
21420Sstevel@tonic-gate /*
21430Sstevel@tonic-gate * We have to just wait for the outer sq_count
21440Sstevel@tonic-gate * drop to zero. As this does not prevent new
21450Sstevel@tonic-gate * messages to enter the outer perimeter, this
21460Sstevel@tonic-gate * is subject to starvation.
21470Sstevel@tonic-gate *
21480Sstevel@tonic-gate * NOTE :Because of blocksq above, messages could
21490Sstevel@tonic-gate * be in the inner syncq only because of some
21500Sstevel@tonic-gate * thread holding the outer perimeter exclusively.
21510Sstevel@tonic-gate * Hence it would be sufficient to wait for the
21520Sstevel@tonic-gate * exclusive holder of the outer perimeter to drain
21530Sstevel@tonic-gate * the inner and outer syncqs. But we will not depend
21540Sstevel@tonic-gate * on this feature and hence check the inner syncqs
21550Sstevel@tonic-gate * separately.
21560Sstevel@tonic-gate */
21570Sstevel@tonic-gate wait_syncq(outer);
21580Sstevel@tonic-gate }
21590Sstevel@tonic-gate
21600Sstevel@tonic-gate
21610Sstevel@tonic-gate /*
21620Sstevel@tonic-gate * There could be messages destined for
21630Sstevel@tonic-gate * this queue. Let the exclusive holder
21640Sstevel@tonic-gate * drain it.
21650Sstevel@tonic-gate */
21660Sstevel@tonic-gate
21670Sstevel@tonic-gate wait_syncq(sq);
21680Sstevel@tonic-gate ASSERT((rq->q_flag & QPERMOD) ||
21695753Sgww ((rq->q_syncq->sq_head == NULL) &&
21705753Sgww (_WR(rq)->q_syncq->sq_head == NULL)));
21710Sstevel@tonic-gate }
21720Sstevel@tonic-gate
21730Sstevel@tonic-gate /*
21740Sstevel@tonic-gate * We haven't taken care of QPERMOD case yet. QPERMOD is a special
21750Sstevel@tonic-gate * case as we don't disable its syncq or remove it off the syncq
21760Sstevel@tonic-gate * service list.
21770Sstevel@tonic-gate */
21780Sstevel@tonic-gate if (rq->q_flag & QPERMOD) {
21790Sstevel@tonic-gate syncq_t *sq = rq->q_syncq;
21800Sstevel@tonic-gate
21810Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
21820Sstevel@tonic-gate while (rq->q_sqflags & Q_SQQUEUED) {
21830Sstevel@tonic-gate sq->sq_flags |= SQ_WANTWAKEUP;
21840Sstevel@tonic-gate cv_wait(&sq->sq_wait, SQLOCK(sq));
21850Sstevel@tonic-gate }
21860Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
21870Sstevel@tonic-gate }
21880Sstevel@tonic-gate
21890Sstevel@tonic-gate /*
21909671SBrian.Ruthven@Sun.COM * flush_syncq changes states only when there are some messages to
21919671SBrian.Ruthven@Sun.COM * free, i.e. when it returns non-zero value to return.
21920Sstevel@tonic-gate */
21930Sstevel@tonic-gate ASSERT(flush_syncq(rq->q_syncq, rq) == 0);
21940Sstevel@tonic-gate ASSERT(flush_syncq(wrq->q_syncq, wrq) == 0);
21950Sstevel@tonic-gate
21960Sstevel@tonic-gate /*
21979671SBrian.Ruthven@Sun.COM * Nobody else should know about this queue now.
21980Sstevel@tonic-gate * If the mux did not process the messages before
21990Sstevel@tonic-gate * acking the I_UNLINK, free them now.
22000Sstevel@tonic-gate */
22010Sstevel@tonic-gate
22020Sstevel@tonic-gate flushq(rq, FLUSHALL);
22030Sstevel@tonic-gate flushq(_WR(rq), FLUSHALL);
22040Sstevel@tonic-gate
22050Sstevel@tonic-gate /*
22060Sstevel@tonic-gate * Convert the mux lower queue into a stream head queue.
22070Sstevel@tonic-gate * Turn off STPLEX before we turn on the stream by removing the passq.
22080Sstevel@tonic-gate */
22090Sstevel@tonic-gate rq->q_ptr = wrq->q_ptr = stpdown;
22100Sstevel@tonic-gate setq(rq, &strdata, &stwdata, NULL, QMTSAFE, SQ_CI|SQ_CO, B_TRUE);
22110Sstevel@tonic-gate
22120Sstevel@tonic-gate ASSERT((rq->q_flag & QMT_TYPEMASK) == QMTSAFE);
22130Sstevel@tonic-gate ASSERT(rq->q_syncq == SQ(rq) && _WR(rq)->q_syncq == SQ(rq));
22140Sstevel@tonic-gate
22150Sstevel@tonic-gate enable_svc(rq);
22160Sstevel@tonic-gate
22170Sstevel@tonic-gate /*
22180Sstevel@tonic-gate * Now it is a proper stream, so STPLEX is cleared. But STRPLUMB still
22190Sstevel@tonic-gate * needs to be set to prevent reopen() of the stream - such reopen may
22200Sstevel@tonic-gate * try to call non-existent pass queue open routine and panic.
22210Sstevel@tonic-gate */
22220Sstevel@tonic-gate mutex_enter(&stpdown->sd_lock);
22230Sstevel@tonic-gate stpdown->sd_flag &= ~STPLEX;
22240Sstevel@tonic-gate mutex_exit(&stpdown->sd_lock);
22250Sstevel@tonic-gate
22260Sstevel@tonic-gate ASSERT(((flag & LINKTYPEMASK) == LINKNORMAL) ||
22270Sstevel@tonic-gate ((flag & LINKTYPEMASK) == LINKPERSIST));
22280Sstevel@tonic-gate
22290Sstevel@tonic-gate /* clean up the layered driver linkages */
22300Sstevel@tonic-gate if ((flag & LINKTYPEMASK) == LINKNORMAL) {
22310Sstevel@tonic-gate ldi_munlink_fp(stp, fpdown, LINKNORMAL);
22320Sstevel@tonic-gate } else {
22330Sstevel@tonic-gate ldi_munlink_fp(stp, fpdown, LINKPERSIST);
22340Sstevel@tonic-gate }
22350Sstevel@tonic-gate
22360Sstevel@tonic-gate link_rempassthru(passq);
22370Sstevel@tonic-gate
22380Sstevel@tonic-gate /*
22390Sstevel@tonic-gate * Now all plumbing changes are finished and STRPLUMB is no
22400Sstevel@tonic-gate * longer needed.
22410Sstevel@tonic-gate */
22420Sstevel@tonic-gate mutex_enter(&stpdown->sd_lock);
22430Sstevel@tonic-gate stpdown->sd_flag &= ~STRPLUMB;
22440Sstevel@tonic-gate cv_broadcast(&stpdown->sd_monitor);
22450Sstevel@tonic-gate mutex_exit(&stpdown->sd_lock);
22460Sstevel@tonic-gate
22470Sstevel@tonic-gate (void) closef(fpdown);
22480Sstevel@tonic-gate return (0);
22490Sstevel@tonic-gate }
22500Sstevel@tonic-gate
22510Sstevel@tonic-gate /*
22520Sstevel@tonic-gate * Unlink all multiplexor links for which stp is the controlling stream.
22530Sstevel@tonic-gate * Return 0, or a non-zero errno on failure.
22540Sstevel@tonic-gate */
22550Sstevel@tonic-gate int
munlinkall(stdata_t * stp,int flag,cred_t * crp,int * rvalp,str_stack_t * ss)22563448Sdh155122 munlinkall(stdata_t *stp, int flag, cred_t *crp, int *rvalp, str_stack_t *ss)
22570Sstevel@tonic-gate {
22580Sstevel@tonic-gate linkinfo_t *linkp;
22590Sstevel@tonic-gate int error = 0;
22600Sstevel@tonic-gate
22610Sstevel@tonic-gate mutex_enter(&muxifier);
22623448Sdh155122 while (linkp = findlinks(stp, 0, flag, ss)) {
22630Sstevel@tonic-gate /*
22640Sstevel@tonic-gate * munlink() releases the muxifier lock.
22650Sstevel@tonic-gate */
22663448Sdh155122 if (error = munlink(stp, linkp, flag, crp, rvalp, ss))
22670Sstevel@tonic-gate return (error);
22680Sstevel@tonic-gate mutex_enter(&muxifier);
22690Sstevel@tonic-gate }
22700Sstevel@tonic-gate mutex_exit(&muxifier);
22710Sstevel@tonic-gate return (0);
22720Sstevel@tonic-gate }
22730Sstevel@tonic-gate
22740Sstevel@tonic-gate /*
22750Sstevel@tonic-gate * A multiplexor link has been made. Add an
22760Sstevel@tonic-gate * edge to the directed graph.
22770Sstevel@tonic-gate */
22780Sstevel@tonic-gate void
mux_addedge(stdata_t * upstp,stdata_t * lostp,int muxid,str_stack_t * ss)22793448Sdh155122 mux_addedge(stdata_t *upstp, stdata_t *lostp, int muxid, str_stack_t *ss)
22800Sstevel@tonic-gate {
22810Sstevel@tonic-gate struct mux_node *np;
22820Sstevel@tonic-gate struct mux_edge *ep;
22830Sstevel@tonic-gate major_t upmaj;
22840Sstevel@tonic-gate major_t lomaj;
22850Sstevel@tonic-gate
22860Sstevel@tonic-gate upmaj = getmajor(upstp->sd_vnode->v_rdev);
22870Sstevel@tonic-gate lomaj = getmajor(lostp->sd_vnode->v_rdev);
22883448Sdh155122 np = &ss->ss_mux_nodes[upmaj];
22890Sstevel@tonic-gate if (np->mn_outp) {
22900Sstevel@tonic-gate ep = np->mn_outp;
22910Sstevel@tonic-gate while (ep->me_nextp)
22920Sstevel@tonic-gate ep = ep->me_nextp;
22930Sstevel@tonic-gate ep->me_nextp = kmem_alloc(sizeof (struct mux_edge), KM_SLEEP);
22940Sstevel@tonic-gate ep = ep->me_nextp;
22950Sstevel@tonic-gate } else {
22960Sstevel@tonic-gate np->mn_outp = kmem_alloc(sizeof (struct mux_edge), KM_SLEEP);
22970Sstevel@tonic-gate ep = np->mn_outp;
22980Sstevel@tonic-gate }
22990Sstevel@tonic-gate ep->me_nextp = NULL;
23000Sstevel@tonic-gate ep->me_muxid = muxid;
23013448Sdh155122 /*
23023448Sdh155122 * Save the dev_t for the purposes of str_stack_shutdown.
23033448Sdh155122 * str_stack_shutdown assumes that the device allows reopen, since
23043448Sdh155122 * this dev_t is the one after any cloning by xx_open().
23053448Sdh155122 * Would prefer finding the dev_t from before any cloning,
23063448Sdh155122 * but specfs doesn't retain that.
23073448Sdh155122 */
23083448Sdh155122 ep->me_dev = upstp->sd_vnode->v_rdev;
23090Sstevel@tonic-gate if (lostp->sd_vnode->v_type == VFIFO)
23100Sstevel@tonic-gate ep->me_nodep = NULL;
23110Sstevel@tonic-gate else
23123448Sdh155122 ep->me_nodep = &ss->ss_mux_nodes[lomaj];
23130Sstevel@tonic-gate }
23140Sstevel@tonic-gate
23150Sstevel@tonic-gate /*
23160Sstevel@tonic-gate * A multiplexor link has been removed. Remove the
23170Sstevel@tonic-gate * edge in the directed graph.
23180Sstevel@tonic-gate */
23190Sstevel@tonic-gate void
mux_rmvedge(stdata_t * upstp,int muxid,str_stack_t * ss)23203448Sdh155122 mux_rmvedge(stdata_t *upstp, int muxid, str_stack_t *ss)
23210Sstevel@tonic-gate {
23220Sstevel@tonic-gate struct mux_node *np;
23230Sstevel@tonic-gate struct mux_edge *ep;
23240Sstevel@tonic-gate struct mux_edge *pep = NULL;
23250Sstevel@tonic-gate major_t upmaj;
23260Sstevel@tonic-gate
23270Sstevel@tonic-gate upmaj = getmajor(upstp->sd_vnode->v_rdev);
23283448Sdh155122 np = &ss->ss_mux_nodes[upmaj];
23290Sstevel@tonic-gate ASSERT(np->mn_outp != NULL);
23300Sstevel@tonic-gate ep = np->mn_outp;
23310Sstevel@tonic-gate while (ep) {
23320Sstevel@tonic-gate if (ep->me_muxid == muxid) {
23330Sstevel@tonic-gate if (pep)
23340Sstevel@tonic-gate pep->me_nextp = ep->me_nextp;
23350Sstevel@tonic-gate else
23360Sstevel@tonic-gate np->mn_outp = ep->me_nextp;
23370Sstevel@tonic-gate kmem_free(ep, sizeof (struct mux_edge));
23380Sstevel@tonic-gate return;
23390Sstevel@tonic-gate }
23400Sstevel@tonic-gate pep = ep;
23410Sstevel@tonic-gate ep = ep->me_nextp;
23420Sstevel@tonic-gate }
23430Sstevel@tonic-gate ASSERT(0); /* should not reach here */
23440Sstevel@tonic-gate }
23450Sstevel@tonic-gate
23460Sstevel@tonic-gate /*
23470Sstevel@tonic-gate * Translate the device flags (from conf.h) to the corresponding
23480Sstevel@tonic-gate * qflag and sq_flag (type) values.
23490Sstevel@tonic-gate */
23500Sstevel@tonic-gate int
devflg_to_qflag(struct streamtab * stp,uint32_t devflag,uint32_t * qflagp,uint32_t * sqtypep)23510Sstevel@tonic-gate devflg_to_qflag(struct streamtab *stp, uint32_t devflag, uint32_t *qflagp,
23520Sstevel@tonic-gate uint32_t *sqtypep)
23530Sstevel@tonic-gate {
23540Sstevel@tonic-gate uint32_t qflag = 0;
23550Sstevel@tonic-gate uint32_t sqtype = 0;
23560Sstevel@tonic-gate
23570Sstevel@tonic-gate if (devflag & _D_OLD)
23580Sstevel@tonic-gate goto bad;
23590Sstevel@tonic-gate
23600Sstevel@tonic-gate /* Inner perimeter presence and scope */
23610Sstevel@tonic-gate switch (devflag & D_MTINNER_MASK) {
23620Sstevel@tonic-gate case D_MP:
23630Sstevel@tonic-gate qflag |= QMTSAFE;
23640Sstevel@tonic-gate sqtype |= SQ_CI;
23650Sstevel@tonic-gate break;
23660Sstevel@tonic-gate case D_MTPERQ|D_MP:
23670Sstevel@tonic-gate qflag |= QPERQ;
23680Sstevel@tonic-gate break;
23690Sstevel@tonic-gate case D_MTQPAIR|D_MP:
23700Sstevel@tonic-gate qflag |= QPAIR;
23710Sstevel@tonic-gate break;
23720Sstevel@tonic-gate case D_MTPERMOD|D_MP:
23730Sstevel@tonic-gate qflag |= QPERMOD;
23740Sstevel@tonic-gate break;
23750Sstevel@tonic-gate default:
23760Sstevel@tonic-gate goto bad;
23770Sstevel@tonic-gate }
23780Sstevel@tonic-gate
23790Sstevel@tonic-gate /* Outer perimeter */
23800Sstevel@tonic-gate if (devflag & D_MTOUTPERIM) {
23810Sstevel@tonic-gate switch (devflag & D_MTINNER_MASK) {
23820Sstevel@tonic-gate case D_MP:
23830Sstevel@tonic-gate case D_MTPERQ|D_MP:
23840Sstevel@tonic-gate case D_MTQPAIR|D_MP:
23850Sstevel@tonic-gate break;
23860Sstevel@tonic-gate default:
23870Sstevel@tonic-gate goto bad;
23880Sstevel@tonic-gate }
23890Sstevel@tonic-gate qflag |= QMTOUTPERIM;
23900Sstevel@tonic-gate }
23910Sstevel@tonic-gate
23920Sstevel@tonic-gate /* Inner perimeter modifiers */
23930Sstevel@tonic-gate if (devflag & D_MTINNER_MOD) {
23940Sstevel@tonic-gate switch (devflag & D_MTINNER_MASK) {
23950Sstevel@tonic-gate case D_MP:
23960Sstevel@tonic-gate goto bad;
23970Sstevel@tonic-gate default:
23980Sstevel@tonic-gate break;
23990Sstevel@tonic-gate }
24000Sstevel@tonic-gate if (devflag & D_MTPUTSHARED)
24010Sstevel@tonic-gate sqtype |= SQ_CIPUT;
24020Sstevel@tonic-gate if (devflag & _D_MTOCSHARED) {
24030Sstevel@tonic-gate /*
24040Sstevel@tonic-gate * The code in putnext assumes that it has the
24050Sstevel@tonic-gate * highest concurrency by not checking sq_count.
24060Sstevel@tonic-gate * Thus _D_MTOCSHARED can only be supported when
24070Sstevel@tonic-gate * D_MTPUTSHARED is set.
24080Sstevel@tonic-gate */
24090Sstevel@tonic-gate if (!(devflag & D_MTPUTSHARED))
24100Sstevel@tonic-gate goto bad;
24110Sstevel@tonic-gate sqtype |= SQ_CIOC;
24120Sstevel@tonic-gate }
24130Sstevel@tonic-gate if (devflag & _D_MTCBSHARED) {
24140Sstevel@tonic-gate /*
24150Sstevel@tonic-gate * The code in putnext assumes that it has the
24160Sstevel@tonic-gate * highest concurrency by not checking sq_count.
24170Sstevel@tonic-gate * Thus _D_MTCBSHARED can only be supported when
24180Sstevel@tonic-gate * D_MTPUTSHARED is set.
24190Sstevel@tonic-gate */
24200Sstevel@tonic-gate if (!(devflag & D_MTPUTSHARED))
24210Sstevel@tonic-gate goto bad;
24220Sstevel@tonic-gate sqtype |= SQ_CICB;
24230Sstevel@tonic-gate }
24240Sstevel@tonic-gate if (devflag & _D_MTSVCSHARED) {
24250Sstevel@tonic-gate /*
24260Sstevel@tonic-gate * The code in putnext assumes that it has the
24270Sstevel@tonic-gate * highest concurrency by not checking sq_count.
24280Sstevel@tonic-gate * Thus _D_MTSVCSHARED can only be supported when
24290Sstevel@tonic-gate * D_MTPUTSHARED is set. Also _D_MTSVCSHARED is
24300Sstevel@tonic-gate * supported only for QPERMOD.
24310Sstevel@tonic-gate */
24320Sstevel@tonic-gate if (!(devflag & D_MTPUTSHARED) || !(qflag & QPERMOD))
24330Sstevel@tonic-gate goto bad;
24340Sstevel@tonic-gate sqtype |= SQ_CISVC;
24350Sstevel@tonic-gate }
24360Sstevel@tonic-gate }
24370Sstevel@tonic-gate
24380Sstevel@tonic-gate /* Default outer perimeter concurrency */
24390Sstevel@tonic-gate sqtype |= SQ_CO;
24400Sstevel@tonic-gate
24410Sstevel@tonic-gate /* Outer perimeter modifiers */
24420Sstevel@tonic-gate if (devflag & D_MTOCEXCL) {
24430Sstevel@tonic-gate if (!(devflag & D_MTOUTPERIM)) {
24440Sstevel@tonic-gate /* No outer perimeter */
24450Sstevel@tonic-gate goto bad;
24460Sstevel@tonic-gate }
24470Sstevel@tonic-gate sqtype &= ~SQ_COOC;
24480Sstevel@tonic-gate }
24490Sstevel@tonic-gate
24500Sstevel@tonic-gate /* Synchronous Streams extended qinit structure */
24510Sstevel@tonic-gate if (devflag & D_SYNCSTR)
24520Sstevel@tonic-gate qflag |= QSYNCSTR;
24530Sstevel@tonic-gate
2454741Smasputra /*
2455741Smasputra * Private flag used by a transport module to indicate
2456741Smasputra * to sockfs that it supports direct-access mode without
24579491SAnders.Persson@Sun.COM * having to go through STREAMS.
24589491SAnders.Persson@Sun.COM */
24599491SAnders.Persson@Sun.COM if (devflag & _D_DIRECT) {
2460741Smasputra /* Reject unless the module is fully-MT (no perimeter) */
2461741Smasputra if ((qflag & QMT_TYPEMASK) != QMTSAFE)
2462741Smasputra goto bad;
24639491SAnders.Persson@Sun.COM qflag |= _QDIRECT;
2464741Smasputra }
2465741Smasputra
24660Sstevel@tonic-gate *qflagp = qflag;
24670Sstevel@tonic-gate *sqtypep = sqtype;
24680Sstevel@tonic-gate return (0);
24690Sstevel@tonic-gate
24700Sstevel@tonic-gate bad:
24710Sstevel@tonic-gate cmn_err(CE_WARN,
24720Sstevel@tonic-gate "stropen: bad MT flags (0x%x) in driver '%s'",
24730Sstevel@tonic-gate (int)(qflag & D_MTSAFETY_MASK),
24740Sstevel@tonic-gate stp->st_rdinit->qi_minfo->mi_idname);
24750Sstevel@tonic-gate
24760Sstevel@tonic-gate return (EINVAL);
24770Sstevel@tonic-gate }
24780Sstevel@tonic-gate
24790Sstevel@tonic-gate /*
24800Sstevel@tonic-gate * Set the interface values for a pair of queues (qinit structure,
24810Sstevel@tonic-gate * packet sizes, water marks).
24820Sstevel@tonic-gate * setq assumes that the caller does not have a claim (entersq or claimq)
24830Sstevel@tonic-gate * on the queue.
24840Sstevel@tonic-gate */
24850Sstevel@tonic-gate void
setq(queue_t * rq,struct qinit * rinit,struct qinit * winit,perdm_t * dmp,uint32_t qflag,uint32_t sqtype,boolean_t lock_needed)24860Sstevel@tonic-gate setq(queue_t *rq, struct qinit *rinit, struct qinit *winit,
24870Sstevel@tonic-gate perdm_t *dmp, uint32_t qflag, uint32_t sqtype, boolean_t lock_needed)
24880Sstevel@tonic-gate {
24890Sstevel@tonic-gate queue_t *wq;
24900Sstevel@tonic-gate syncq_t *sq, *outer;
24910Sstevel@tonic-gate
24920Sstevel@tonic-gate ASSERT(rq->q_flag & QREADR);
24930Sstevel@tonic-gate ASSERT((qflag & QMT_TYPEMASK) != 0);
24940Sstevel@tonic-gate IMPLY((qflag & (QPERMOD | QMTOUTPERIM)), dmp != NULL);
24950Sstevel@tonic-gate
24960Sstevel@tonic-gate wq = _WR(rq);
24970Sstevel@tonic-gate rq->q_qinfo = rinit;
24980Sstevel@tonic-gate rq->q_hiwat = rinit->qi_minfo->mi_hiwat;
24990Sstevel@tonic-gate rq->q_lowat = rinit->qi_minfo->mi_lowat;
25000Sstevel@tonic-gate rq->q_minpsz = rinit->qi_minfo->mi_minpsz;
25010Sstevel@tonic-gate rq->q_maxpsz = rinit->qi_minfo->mi_maxpsz;
25020Sstevel@tonic-gate wq->q_qinfo = winit;
25030Sstevel@tonic-gate wq->q_hiwat = winit->qi_minfo->mi_hiwat;
25040Sstevel@tonic-gate wq->q_lowat = winit->qi_minfo->mi_lowat;
25050Sstevel@tonic-gate wq->q_minpsz = winit->qi_minfo->mi_minpsz;
25060Sstevel@tonic-gate wq->q_maxpsz = winit->qi_minfo->mi_maxpsz;
25070Sstevel@tonic-gate
25080Sstevel@tonic-gate /* Remove old syncqs */
25090Sstevel@tonic-gate sq = rq->q_syncq;
25100Sstevel@tonic-gate outer = sq->sq_outer;
25110Sstevel@tonic-gate if (outer != NULL) {
25120Sstevel@tonic-gate ASSERT(wq->q_syncq->sq_outer == outer);
25130Sstevel@tonic-gate outer_remove(outer, rq->q_syncq);
25140Sstevel@tonic-gate if (wq->q_syncq != rq->q_syncq)
25150Sstevel@tonic-gate outer_remove(outer, wq->q_syncq);
25160Sstevel@tonic-gate }
25170Sstevel@tonic-gate ASSERT(sq->sq_outer == NULL);
25180Sstevel@tonic-gate ASSERT(sq->sq_onext == NULL && sq->sq_oprev == NULL);
25190Sstevel@tonic-gate
25200Sstevel@tonic-gate if (sq != SQ(rq)) {
25210Sstevel@tonic-gate if (!(rq->q_flag & QPERMOD))
25220Sstevel@tonic-gate free_syncq(sq);
25230Sstevel@tonic-gate if (wq->q_syncq == rq->q_syncq)
25240Sstevel@tonic-gate wq->q_syncq = NULL;
25250Sstevel@tonic-gate rq->q_syncq = NULL;
25260Sstevel@tonic-gate }
25270Sstevel@tonic-gate if (wq->q_syncq != NULL && wq->q_syncq != sq &&
25280Sstevel@tonic-gate wq->q_syncq != SQ(rq)) {
25290Sstevel@tonic-gate free_syncq(wq->q_syncq);
25300Sstevel@tonic-gate wq->q_syncq = NULL;
25310Sstevel@tonic-gate }
25320Sstevel@tonic-gate ASSERT(rq->q_syncq == NULL || (rq->q_syncq->sq_head == NULL &&
25335753Sgww rq->q_syncq->sq_tail == NULL));
25340Sstevel@tonic-gate ASSERT(wq->q_syncq == NULL || (wq->q_syncq->sq_head == NULL &&
25355753Sgww wq->q_syncq->sq_tail == NULL));
25360Sstevel@tonic-gate
25370Sstevel@tonic-gate if (!(rq->q_flag & QPERMOD) &&
25380Sstevel@tonic-gate rq->q_syncq != NULL && rq->q_syncq->sq_ciputctrl != NULL) {
25390Sstevel@tonic-gate ASSERT(rq->q_syncq->sq_nciputctrl == n_ciputctrl - 1);
25400Sstevel@tonic-gate SUMCHECK_CIPUTCTRL_COUNTS(rq->q_syncq->sq_ciputctrl,
25410Sstevel@tonic-gate rq->q_syncq->sq_nciputctrl, 0);
25420Sstevel@tonic-gate ASSERT(ciputctrl_cache != NULL);
25430Sstevel@tonic-gate kmem_cache_free(ciputctrl_cache, rq->q_syncq->sq_ciputctrl);
25440Sstevel@tonic-gate rq->q_syncq->sq_ciputctrl = NULL;
25450Sstevel@tonic-gate rq->q_syncq->sq_nciputctrl = 0;
25460Sstevel@tonic-gate }
25470Sstevel@tonic-gate
25480Sstevel@tonic-gate if (!(wq->q_flag & QPERMOD) &&
25490Sstevel@tonic-gate wq->q_syncq != NULL && wq->q_syncq->sq_ciputctrl != NULL) {
25500Sstevel@tonic-gate ASSERT(wq->q_syncq->sq_nciputctrl == n_ciputctrl - 1);
25510Sstevel@tonic-gate SUMCHECK_CIPUTCTRL_COUNTS(wq->q_syncq->sq_ciputctrl,
25520Sstevel@tonic-gate wq->q_syncq->sq_nciputctrl, 0);
25530Sstevel@tonic-gate ASSERT(ciputctrl_cache != NULL);
25540Sstevel@tonic-gate kmem_cache_free(ciputctrl_cache, wq->q_syncq->sq_ciputctrl);
25550Sstevel@tonic-gate wq->q_syncq->sq_ciputctrl = NULL;
25560Sstevel@tonic-gate wq->q_syncq->sq_nciputctrl = 0;
25570Sstevel@tonic-gate }
25580Sstevel@tonic-gate
25590Sstevel@tonic-gate sq = SQ(rq);
25600Sstevel@tonic-gate ASSERT(sq->sq_head == NULL && sq->sq_tail == NULL);
25610Sstevel@tonic-gate ASSERT(sq->sq_outer == NULL);
25620Sstevel@tonic-gate ASSERT(sq->sq_onext == NULL && sq->sq_oprev == NULL);
25630Sstevel@tonic-gate
25640Sstevel@tonic-gate /*
25650Sstevel@tonic-gate * Create syncqs based on qflag and sqtype. Set the SQ_TYPES_IN_FLAGS
25660Sstevel@tonic-gate * bits in sq_flag based on the sqtype.
25670Sstevel@tonic-gate */
25680Sstevel@tonic-gate ASSERT((sq->sq_flags & ~SQ_TYPES_IN_FLAGS) == 0);
25690Sstevel@tonic-gate
25700Sstevel@tonic-gate rq->q_syncq = wq->q_syncq = sq;
25710Sstevel@tonic-gate sq->sq_type = sqtype;
25720Sstevel@tonic-gate sq->sq_flags = (sqtype & SQ_TYPES_IN_FLAGS);
25730Sstevel@tonic-gate
25740Sstevel@tonic-gate /*
25750Sstevel@tonic-gate * We are making sq_svcflags zero,
25760Sstevel@tonic-gate * resetting SQ_DISABLED in case it was set by
25770Sstevel@tonic-gate * wait_svc() in the munlink path.
25780Sstevel@tonic-gate *
25790Sstevel@tonic-gate */
25800Sstevel@tonic-gate ASSERT((sq->sq_svcflags & SQ_SERVICE) == 0);
25810Sstevel@tonic-gate sq->sq_svcflags = 0;
25820Sstevel@tonic-gate
25830Sstevel@tonic-gate /*
25840Sstevel@tonic-gate * We need to acquire the lock here for the mlink and munlink case,
25850Sstevel@tonic-gate * where canputnext, backenable, etc can access the q_flag.
25860Sstevel@tonic-gate */
25870Sstevel@tonic-gate if (lock_needed) {
25880Sstevel@tonic-gate mutex_enter(QLOCK(rq));
25890Sstevel@tonic-gate rq->q_flag = (rq->q_flag & ~QMT_TYPEMASK) | QWANTR | qflag;
25900Sstevel@tonic-gate mutex_exit(QLOCK(rq));
25910Sstevel@tonic-gate mutex_enter(QLOCK(wq));
25920Sstevel@tonic-gate wq->q_flag = (wq->q_flag & ~QMT_TYPEMASK) | QWANTR | qflag;
25930Sstevel@tonic-gate mutex_exit(QLOCK(wq));
25940Sstevel@tonic-gate } else {
25950Sstevel@tonic-gate rq->q_flag = (rq->q_flag & ~QMT_TYPEMASK) | QWANTR | qflag;
25960Sstevel@tonic-gate wq->q_flag = (wq->q_flag & ~QMT_TYPEMASK) | QWANTR | qflag;
25970Sstevel@tonic-gate }
25980Sstevel@tonic-gate
25990Sstevel@tonic-gate if (qflag & QPERQ) {
26000Sstevel@tonic-gate /* Allocate a separate syncq for the write side */
26010Sstevel@tonic-gate sq = new_syncq();
26020Sstevel@tonic-gate sq->sq_type = rq->q_syncq->sq_type;
26030Sstevel@tonic-gate sq->sq_flags = rq->q_syncq->sq_flags;
26040Sstevel@tonic-gate ASSERT(sq->sq_outer == NULL && sq->sq_onext == NULL &&
26050Sstevel@tonic-gate sq->sq_oprev == NULL);
26060Sstevel@tonic-gate wq->q_syncq = sq;
26070Sstevel@tonic-gate }
26080Sstevel@tonic-gate if (qflag & QPERMOD) {
26090Sstevel@tonic-gate sq = dmp->dm_sq;
26100Sstevel@tonic-gate
26110Sstevel@tonic-gate /*
26120Sstevel@tonic-gate * Assert that we do have an inner perimeter syncq and that it
26130Sstevel@tonic-gate * does not have an outer perimeter associated with it.
26140Sstevel@tonic-gate */
26150Sstevel@tonic-gate ASSERT(sq->sq_outer == NULL && sq->sq_onext == NULL &&
26160Sstevel@tonic-gate sq->sq_oprev == NULL);
26170Sstevel@tonic-gate rq->q_syncq = wq->q_syncq = sq;
26180Sstevel@tonic-gate }
26190Sstevel@tonic-gate if (qflag & QMTOUTPERIM) {
26200Sstevel@tonic-gate outer = dmp->dm_sq;
26210Sstevel@tonic-gate
26220Sstevel@tonic-gate ASSERT(outer->sq_outer == NULL);
26230Sstevel@tonic-gate outer_insert(outer, rq->q_syncq);
26240Sstevel@tonic-gate if (wq->q_syncq != rq->q_syncq)
26250Sstevel@tonic-gate outer_insert(outer, wq->q_syncq);
26260Sstevel@tonic-gate }
26270Sstevel@tonic-gate ASSERT((rq->q_syncq->sq_flags & SQ_TYPES_IN_FLAGS) ==
26285753Sgww (rq->q_syncq->sq_type & SQ_TYPES_IN_FLAGS));
26290Sstevel@tonic-gate ASSERT((wq->q_syncq->sq_flags & SQ_TYPES_IN_FLAGS) ==
26305753Sgww (wq->q_syncq->sq_type & SQ_TYPES_IN_FLAGS));
26310Sstevel@tonic-gate ASSERT((rq->q_flag & QMT_TYPEMASK) == (qflag & QMT_TYPEMASK));
26320Sstevel@tonic-gate
26330Sstevel@tonic-gate /*
26340Sstevel@tonic-gate * Initialize struio() types.
26350Sstevel@tonic-gate */
26360Sstevel@tonic-gate rq->q_struiot =
26370Sstevel@tonic-gate (rq->q_flag & QSYNCSTR) ? rinit->qi_struiot : STRUIOT_NONE;
26380Sstevel@tonic-gate wq->q_struiot =
26390Sstevel@tonic-gate (wq->q_flag & QSYNCSTR) ? winit->qi_struiot : STRUIOT_NONE;
26400Sstevel@tonic-gate }
26410Sstevel@tonic-gate
26420Sstevel@tonic-gate perdm_t *
hold_dm(struct streamtab * str,uint32_t qflag,uint32_t sqtype)26430Sstevel@tonic-gate hold_dm(struct streamtab *str, uint32_t qflag, uint32_t sqtype)
26440Sstevel@tonic-gate {
26450Sstevel@tonic-gate syncq_t *sq;
26460Sstevel@tonic-gate perdm_t **pp;
26470Sstevel@tonic-gate perdm_t *p;
26480Sstevel@tonic-gate perdm_t *dmp;
26490Sstevel@tonic-gate
26500Sstevel@tonic-gate ASSERT(str != NULL);
26510Sstevel@tonic-gate ASSERT(qflag & (QPERMOD | QMTOUTPERIM));
26520Sstevel@tonic-gate
26530Sstevel@tonic-gate rw_enter(&perdm_rwlock, RW_READER);
26540Sstevel@tonic-gate for (p = perdm_list; p != NULL; p = p->dm_next) {
26550Sstevel@tonic-gate if (p->dm_str == str) { /* found one */
26560Sstevel@tonic-gate atomic_add_32(&(p->dm_ref), 1);
26570Sstevel@tonic-gate rw_exit(&perdm_rwlock);
26580Sstevel@tonic-gate return (p);
26590Sstevel@tonic-gate }
26600Sstevel@tonic-gate }
26610Sstevel@tonic-gate rw_exit(&perdm_rwlock);
26620Sstevel@tonic-gate
26630Sstevel@tonic-gate sq = new_syncq();
26640Sstevel@tonic-gate if (qflag & QPERMOD) {
26650Sstevel@tonic-gate sq->sq_type = sqtype | SQ_PERMOD;
26660Sstevel@tonic-gate sq->sq_flags = sqtype & SQ_TYPES_IN_FLAGS;
26670Sstevel@tonic-gate } else {
26680Sstevel@tonic-gate ASSERT(qflag & QMTOUTPERIM);
26690Sstevel@tonic-gate sq->sq_onext = sq->sq_oprev = sq;
26700Sstevel@tonic-gate }
26710Sstevel@tonic-gate
26720Sstevel@tonic-gate dmp = kmem_alloc(sizeof (perdm_t), KM_SLEEP);
26730Sstevel@tonic-gate dmp->dm_sq = sq;
26740Sstevel@tonic-gate dmp->dm_str = str;
26750Sstevel@tonic-gate dmp->dm_ref = 1;
26760Sstevel@tonic-gate dmp->dm_next = NULL;
26770Sstevel@tonic-gate
26780Sstevel@tonic-gate rw_enter(&perdm_rwlock, RW_WRITER);
26790Sstevel@tonic-gate for (pp = &perdm_list; (p = *pp) != NULL; pp = &(p->dm_next)) {
26800Sstevel@tonic-gate if (p->dm_str == str) { /* already present */
26810Sstevel@tonic-gate p->dm_ref++;
26820Sstevel@tonic-gate rw_exit(&perdm_rwlock);
26830Sstevel@tonic-gate free_syncq(sq);
26840Sstevel@tonic-gate kmem_free(dmp, sizeof (perdm_t));
26850Sstevel@tonic-gate return (p);
26860Sstevel@tonic-gate }
26870Sstevel@tonic-gate }
26880Sstevel@tonic-gate
26890Sstevel@tonic-gate *pp = dmp;
26900Sstevel@tonic-gate rw_exit(&perdm_rwlock);
26910Sstevel@tonic-gate return (dmp);
26920Sstevel@tonic-gate }
26930Sstevel@tonic-gate
26940Sstevel@tonic-gate void
rele_dm(perdm_t * dmp)26950Sstevel@tonic-gate rele_dm(perdm_t *dmp)
26960Sstevel@tonic-gate {
26970Sstevel@tonic-gate perdm_t **pp;
26980Sstevel@tonic-gate perdm_t *p;
26990Sstevel@tonic-gate
27000Sstevel@tonic-gate rw_enter(&perdm_rwlock, RW_WRITER);
27010Sstevel@tonic-gate ASSERT(dmp->dm_ref > 0);
27020Sstevel@tonic-gate
27030Sstevel@tonic-gate if (--dmp->dm_ref > 0) {
27040Sstevel@tonic-gate rw_exit(&perdm_rwlock);
27050Sstevel@tonic-gate return;
27060Sstevel@tonic-gate }
27070Sstevel@tonic-gate
27080Sstevel@tonic-gate for (pp = &perdm_list; (p = *pp) != NULL; pp = &(p->dm_next))
27090Sstevel@tonic-gate if (p == dmp)
27100Sstevel@tonic-gate break;
27110Sstevel@tonic-gate ASSERT(p == dmp);
27120Sstevel@tonic-gate *pp = p->dm_next;
27130Sstevel@tonic-gate rw_exit(&perdm_rwlock);
27140Sstevel@tonic-gate
27150Sstevel@tonic-gate /*
27160Sstevel@tonic-gate * Wait for any background processing that relies on the
27170Sstevel@tonic-gate * syncq to complete before it is freed.
27180Sstevel@tonic-gate */
27190Sstevel@tonic-gate wait_sq_svc(p->dm_sq);
27200Sstevel@tonic-gate free_syncq(p->dm_sq);
27210Sstevel@tonic-gate kmem_free(p, sizeof (perdm_t));
27220Sstevel@tonic-gate }
27230Sstevel@tonic-gate
27240Sstevel@tonic-gate /*
27250Sstevel@tonic-gate * Make a protocol message given control and data buffers.
27260Sstevel@tonic-gate * n.b., this can block; be careful of what locks you hold when calling it.
27270Sstevel@tonic-gate *
27280Sstevel@tonic-gate * If sd_maxblk is less than *iosize this routine can fail part way through
27290Sstevel@tonic-gate * (due to an allocation failure). In this case on return *iosize will contain
27300Sstevel@tonic-gate * the amount that was consumed. Otherwise *iosize will not be modified
27310Sstevel@tonic-gate * i.e. it will contain the amount that was consumed.
27320Sstevel@tonic-gate */
27330Sstevel@tonic-gate int
strmakemsg(struct strbuf * mctl,ssize_t * iosize,struct uio * uiop,stdata_t * stp,int32_t flag,mblk_t ** mpp)27340Sstevel@tonic-gate strmakemsg(
27350Sstevel@tonic-gate struct strbuf *mctl,
27360Sstevel@tonic-gate ssize_t *iosize,
27370Sstevel@tonic-gate struct uio *uiop,
27380Sstevel@tonic-gate stdata_t *stp,
27390Sstevel@tonic-gate int32_t flag,
27400Sstevel@tonic-gate mblk_t **mpp)
27410Sstevel@tonic-gate {
27420Sstevel@tonic-gate mblk_t *mpctl = NULL;
27430Sstevel@tonic-gate mblk_t *mpdata = NULL;
27440Sstevel@tonic-gate int error;
27450Sstevel@tonic-gate
27460Sstevel@tonic-gate ASSERT(uiop != NULL);
27470Sstevel@tonic-gate
27480Sstevel@tonic-gate *mpp = NULL;
27490Sstevel@tonic-gate /* Create control part, if any */
27500Sstevel@tonic-gate if ((mctl != NULL) && (mctl->len >= 0)) {
27510Sstevel@tonic-gate error = strmakectl(mctl, flag, uiop->uio_fmode, &mpctl);
27520Sstevel@tonic-gate if (error)
27530Sstevel@tonic-gate return (error);
27540Sstevel@tonic-gate }
27550Sstevel@tonic-gate /* Create data part, if any */
27560Sstevel@tonic-gate if (*iosize >= 0) {
27570Sstevel@tonic-gate error = strmakedata(iosize, uiop, stp, flag, &mpdata);
27580Sstevel@tonic-gate if (error) {
27590Sstevel@tonic-gate freemsg(mpctl);
27600Sstevel@tonic-gate return (error);
27610Sstevel@tonic-gate }
27620Sstevel@tonic-gate }
27630Sstevel@tonic-gate if (mpctl != NULL) {
27640Sstevel@tonic-gate if (mpdata != NULL)
27650Sstevel@tonic-gate linkb(mpctl, mpdata);
27660Sstevel@tonic-gate *mpp = mpctl;
27670Sstevel@tonic-gate } else {
27680Sstevel@tonic-gate *mpp = mpdata;
27690Sstevel@tonic-gate }
27700Sstevel@tonic-gate return (0);
27710Sstevel@tonic-gate }
27720Sstevel@tonic-gate
27730Sstevel@tonic-gate /*
27740Sstevel@tonic-gate * Make the control part of a protocol message given a control buffer.
27750Sstevel@tonic-gate * n.b., this can block; be careful of what locks you hold when calling it.
27760Sstevel@tonic-gate */
27770Sstevel@tonic-gate int
strmakectl(struct strbuf * mctl,int32_t flag,int32_t fflag,mblk_t ** mpp)27780Sstevel@tonic-gate strmakectl(
27790Sstevel@tonic-gate struct strbuf *mctl,
27800Sstevel@tonic-gate int32_t flag,
27810Sstevel@tonic-gate int32_t fflag,
27820Sstevel@tonic-gate mblk_t **mpp)
27830Sstevel@tonic-gate {
27840Sstevel@tonic-gate mblk_t *bp = NULL;
27850Sstevel@tonic-gate unsigned char msgtype;
27860Sstevel@tonic-gate int error = 0;
27878778SErik.Nordmark@Sun.COM cred_t *cr = CRED();
27888778SErik.Nordmark@Sun.COM
27898778SErik.Nordmark@Sun.COM /* We do not support interrupt threads using the stream head to send */
27908778SErik.Nordmark@Sun.COM ASSERT(cr != NULL);
27910Sstevel@tonic-gate
27920Sstevel@tonic-gate *mpp = NULL;
27930Sstevel@tonic-gate /*
27940Sstevel@tonic-gate * Create control part of message, if any.
27950Sstevel@tonic-gate */
27960Sstevel@tonic-gate if ((mctl != NULL) && (mctl->len >= 0)) {
27970Sstevel@tonic-gate caddr_t base;
27980Sstevel@tonic-gate int ctlcount;
27990Sstevel@tonic-gate int allocsz;
28000Sstevel@tonic-gate
28010Sstevel@tonic-gate if (flag & RS_HIPRI)
28020Sstevel@tonic-gate msgtype = M_PCPROTO;
28030Sstevel@tonic-gate else
28040Sstevel@tonic-gate msgtype = M_PROTO;
28050Sstevel@tonic-gate
28060Sstevel@tonic-gate ctlcount = mctl->len;
28070Sstevel@tonic-gate base = mctl->buf;
28080Sstevel@tonic-gate
28090Sstevel@tonic-gate /*
28100Sstevel@tonic-gate * Give modules a better chance to reuse M_PROTO/M_PCPROTO
28110Sstevel@tonic-gate * blocks by increasing the size to something more usable.
28120Sstevel@tonic-gate */
28130Sstevel@tonic-gate allocsz = MAX(ctlcount, 64);
28140Sstevel@tonic-gate
28150Sstevel@tonic-gate /*
28160Sstevel@tonic-gate * Range checking has already been done; simply try
28170Sstevel@tonic-gate * to allocate a message block for the ctl part.
28180Sstevel@tonic-gate */
28198778SErik.Nordmark@Sun.COM while ((bp = allocb_cred(allocsz, cr,
28208778SErik.Nordmark@Sun.COM curproc->p_pid)) == NULL) {
28210Sstevel@tonic-gate if (fflag & (FNDELAY|FNONBLOCK))
28220Sstevel@tonic-gate return (EAGAIN);
28230Sstevel@tonic-gate if (error = strwaitbuf(allocsz, BPRI_MED))
28240Sstevel@tonic-gate return (error);
28250Sstevel@tonic-gate }
28260Sstevel@tonic-gate
28270Sstevel@tonic-gate bp->b_datap->db_type = msgtype;
28280Sstevel@tonic-gate if (copyin(base, bp->b_wptr, ctlcount)) {
28290Sstevel@tonic-gate freeb(bp);
28300Sstevel@tonic-gate return (EFAULT);
28310Sstevel@tonic-gate }
28320Sstevel@tonic-gate bp->b_wptr += ctlcount;
28330Sstevel@tonic-gate }
28340Sstevel@tonic-gate *mpp = bp;
28350Sstevel@tonic-gate return (0);
28360Sstevel@tonic-gate }
28370Sstevel@tonic-gate
28380Sstevel@tonic-gate /*
28390Sstevel@tonic-gate * Make a protocol message given data buffers.
28400Sstevel@tonic-gate * n.b., this can block; be careful of what locks you hold when calling it.
28410Sstevel@tonic-gate *
28420Sstevel@tonic-gate * If sd_maxblk is less than *iosize this routine can fail part way through
28430Sstevel@tonic-gate * (due to an allocation failure). In this case on return *iosize will contain
28440Sstevel@tonic-gate * the amount that was consumed. Otherwise *iosize will not be modified
28450Sstevel@tonic-gate * i.e. it will contain the amount that was consumed.
28460Sstevel@tonic-gate */
28470Sstevel@tonic-gate int
strmakedata(ssize_t * iosize,struct uio * uiop,stdata_t * stp,int32_t flag,mblk_t ** mpp)28480Sstevel@tonic-gate strmakedata(
28490Sstevel@tonic-gate ssize_t *iosize,
28500Sstevel@tonic-gate struct uio *uiop,
28510Sstevel@tonic-gate stdata_t *stp,
28520Sstevel@tonic-gate int32_t flag,
28530Sstevel@tonic-gate mblk_t **mpp)
28540Sstevel@tonic-gate {
28550Sstevel@tonic-gate mblk_t *mp = NULL;
28560Sstevel@tonic-gate mblk_t *bp;
28570Sstevel@tonic-gate int wroff = (int)stp->sd_wroff;
2858898Skais int tail_len = (int)stp->sd_tail;
2859898Skais int extra = wroff + tail_len;
28600Sstevel@tonic-gate int error = 0;
28610Sstevel@tonic-gate ssize_t maxblk;
28620Sstevel@tonic-gate ssize_t count = *iosize;
28638778SErik.Nordmark@Sun.COM cred_t *cr;
28640Sstevel@tonic-gate
28650Sstevel@tonic-gate *mpp = NULL;
28660Sstevel@tonic-gate if (count < 0)
28670Sstevel@tonic-gate return (0);
28680Sstevel@tonic-gate
28698778SErik.Nordmark@Sun.COM /* We do not support interrupt threads using the stream head to send */
28708778SErik.Nordmark@Sun.COM cr = CRED();
28718778SErik.Nordmark@Sun.COM ASSERT(cr != NULL);
28728778SErik.Nordmark@Sun.COM
28730Sstevel@tonic-gate maxblk = stp->sd_maxblk;
28740Sstevel@tonic-gate if (maxblk == INFPSZ)
28750Sstevel@tonic-gate maxblk = count;
28760Sstevel@tonic-gate
28770Sstevel@tonic-gate /*
28780Sstevel@tonic-gate * Create data part of message, if any.
28790Sstevel@tonic-gate */
28800Sstevel@tonic-gate do {
28810Sstevel@tonic-gate ssize_t size;
28820Sstevel@tonic-gate dblk_t *dp;
28830Sstevel@tonic-gate
28840Sstevel@tonic-gate ASSERT(uiop);
28850Sstevel@tonic-gate
28860Sstevel@tonic-gate size = MIN(count, maxblk);
28870Sstevel@tonic-gate
28888778SErik.Nordmark@Sun.COM while ((bp = allocb_cred(size + extra, cr,
28898778SErik.Nordmark@Sun.COM curproc->p_pid)) == NULL) {
28900Sstevel@tonic-gate error = EAGAIN;
28910Sstevel@tonic-gate if ((uiop->uio_fmode & (FNDELAY|FNONBLOCK)) ||
2892898Skais (error = strwaitbuf(size + extra, BPRI_MED)) != 0) {
28930Sstevel@tonic-gate if (count == *iosize) {
28940Sstevel@tonic-gate freemsg(mp);
28950Sstevel@tonic-gate return (error);
28960Sstevel@tonic-gate } else {
28970Sstevel@tonic-gate *iosize -= count;
28980Sstevel@tonic-gate *mpp = mp;
28990Sstevel@tonic-gate return (0);
29000Sstevel@tonic-gate }
29010Sstevel@tonic-gate }
29020Sstevel@tonic-gate }
29030Sstevel@tonic-gate dp = bp->b_datap;
29040Sstevel@tonic-gate dp->db_cpid = curproc->p_pid;
29050Sstevel@tonic-gate ASSERT(wroff <= dp->db_lim - bp->b_wptr);
29060Sstevel@tonic-gate bp->b_wptr = bp->b_rptr = bp->b_rptr + wroff;
29070Sstevel@tonic-gate
29080Sstevel@tonic-gate if (flag & STRUIO_POSTPONE) {
29090Sstevel@tonic-gate /*
29100Sstevel@tonic-gate * Setup the stream uio portion of the
29110Sstevel@tonic-gate * dblk for subsequent use by struioget().
29120Sstevel@tonic-gate */
29130Sstevel@tonic-gate dp->db_struioflag = STRUIO_SPEC;
29140Sstevel@tonic-gate dp->db_cksumstart = 0;
29150Sstevel@tonic-gate dp->db_cksumstuff = 0;
29160Sstevel@tonic-gate dp->db_cksumend = size;
29170Sstevel@tonic-gate *(long long *)dp->db_struioun.data = 0ll;
2918898Skais bp->b_wptr += size;
29190Sstevel@tonic-gate } else {
29200Sstevel@tonic-gate if (stp->sd_copyflag & STRCOPYCACHED)
29210Sstevel@tonic-gate uiop->uio_extflg |= UIO_COPY_CACHED;
29220Sstevel@tonic-gate
29230Sstevel@tonic-gate if (size != 0) {
29240Sstevel@tonic-gate error = uiomove(bp->b_wptr, size, UIO_WRITE,
29250Sstevel@tonic-gate uiop);
29260Sstevel@tonic-gate if (error != 0) {
29270Sstevel@tonic-gate freeb(bp);
29280Sstevel@tonic-gate freemsg(mp);
29290Sstevel@tonic-gate return (error);
29300Sstevel@tonic-gate }
29310Sstevel@tonic-gate }
2932898Skais bp->b_wptr += size;
2933898Skais
2934898Skais if (stp->sd_wputdatafunc != NULL) {
2935898Skais mblk_t *newbp;
2936898Skais
2937898Skais newbp = (stp->sd_wputdatafunc)(stp->sd_vnode,
2938898Skais bp, NULL, NULL, NULL, NULL);
2939898Skais if (newbp == NULL) {
2940898Skais freeb(bp);
2941898Skais freemsg(mp);
2942898Skais return (ECOMM);
2943898Skais }
2944898Skais bp = newbp;
2945898Skais }
2946898Skais }
2947898Skais
29480Sstevel@tonic-gate count -= size;
29490Sstevel@tonic-gate
29500Sstevel@tonic-gate if (mp == NULL)
29510Sstevel@tonic-gate mp = bp;
29520Sstevel@tonic-gate else
29530Sstevel@tonic-gate linkb(mp, bp);
29540Sstevel@tonic-gate } while (count > 0);
29550Sstevel@tonic-gate
29560Sstevel@tonic-gate *mpp = mp;
29570Sstevel@tonic-gate return (0);
29580Sstevel@tonic-gate }
29590Sstevel@tonic-gate
29600Sstevel@tonic-gate /*
29610Sstevel@tonic-gate * Wait for a buffer to become available. Return non-zero errno
29620Sstevel@tonic-gate * if not able to wait, 0 if buffer is probably there.
29630Sstevel@tonic-gate */
29640Sstevel@tonic-gate int
strwaitbuf(size_t size,int pri)29650Sstevel@tonic-gate strwaitbuf(size_t size, int pri)
29660Sstevel@tonic-gate {
29670Sstevel@tonic-gate bufcall_id_t id;
29680Sstevel@tonic-gate
29690Sstevel@tonic-gate mutex_enter(&bcall_monitor);
29700Sstevel@tonic-gate if ((id = bufcall(size, pri, (void (*)(void *))cv_broadcast,
29710Sstevel@tonic-gate &ttoproc(curthread)->p_flag_cv)) == 0) {
29720Sstevel@tonic-gate mutex_exit(&bcall_monitor);
29730Sstevel@tonic-gate return (ENOSR);
29740Sstevel@tonic-gate }
29750Sstevel@tonic-gate if (!cv_wait_sig(&(ttoproc(curthread)->p_flag_cv), &bcall_monitor)) {
29760Sstevel@tonic-gate unbufcall(id);
29770Sstevel@tonic-gate mutex_exit(&bcall_monitor);
29780Sstevel@tonic-gate return (EINTR);
29790Sstevel@tonic-gate }
29800Sstevel@tonic-gate unbufcall(id);
29810Sstevel@tonic-gate mutex_exit(&bcall_monitor);
29820Sstevel@tonic-gate return (0);
29830Sstevel@tonic-gate }
29840Sstevel@tonic-gate
29850Sstevel@tonic-gate /*
29860Sstevel@tonic-gate * This function waits for a read or write event to happen on a stream.
29870Sstevel@tonic-gate * fmode can specify FNDELAY and/or FNONBLOCK.
29880Sstevel@tonic-gate * The timeout is in ms with -1 meaning infinite.
29890Sstevel@tonic-gate * The flag values work as follows:
29900Sstevel@tonic-gate * READWAIT Check for read side errors, send M_READ
29910Sstevel@tonic-gate * GETWAIT Check for read side errors, no M_READ
29920Sstevel@tonic-gate * WRITEWAIT Check for write side errors.
29930Sstevel@tonic-gate * NOINTR Do not return error if nonblocking or timeout.
29940Sstevel@tonic-gate * STR_NOERROR Ignore all errors except STPLEX.
29950Sstevel@tonic-gate * STR_NOSIG Ignore/hold signals during the duration of the call.
29960Sstevel@tonic-gate * STR_PEEK Pass through the strgeterr().
29970Sstevel@tonic-gate */
29980Sstevel@tonic-gate int
strwaitq(stdata_t * stp,int flag,ssize_t count,int fmode,clock_t timout,int * done)29990Sstevel@tonic-gate strwaitq(stdata_t *stp, int flag, ssize_t count, int fmode, clock_t timout,
30000Sstevel@tonic-gate int *done)
30010Sstevel@tonic-gate {
30020Sstevel@tonic-gate int slpflg, errs;
30030Sstevel@tonic-gate int error;
30040Sstevel@tonic-gate kcondvar_t *sleepon;
30050Sstevel@tonic-gate mblk_t *mp;
30060Sstevel@tonic-gate ssize_t *rd_count;
30070Sstevel@tonic-gate clock_t rval;
30080Sstevel@tonic-gate
30090Sstevel@tonic-gate ASSERT(MUTEX_HELD(&stp->sd_lock));
30100Sstevel@tonic-gate if ((flag & READWAIT) || (flag & GETWAIT)) {
30110Sstevel@tonic-gate slpflg = RSLEEP;
30120Sstevel@tonic-gate sleepon = &_RD(stp->sd_wrq)->q_wait;
30130Sstevel@tonic-gate errs = STRDERR|STPLEX;
30140Sstevel@tonic-gate } else {
30150Sstevel@tonic-gate slpflg = WSLEEP;
30160Sstevel@tonic-gate sleepon = &stp->sd_wrq->q_wait;
30170Sstevel@tonic-gate errs = STWRERR|STRHUP|STPLEX;
30180Sstevel@tonic-gate }
30190Sstevel@tonic-gate if (flag & STR_NOERROR)
30200Sstevel@tonic-gate errs = STPLEX;
30210Sstevel@tonic-gate
30220Sstevel@tonic-gate if (stp->sd_wakeq & slpflg) {
30230Sstevel@tonic-gate /*
30240Sstevel@tonic-gate * A strwakeq() is pending, no need to sleep.
30250Sstevel@tonic-gate */
30260Sstevel@tonic-gate stp->sd_wakeq &= ~slpflg;
30270Sstevel@tonic-gate *done = 0;
30280Sstevel@tonic-gate return (0);
30290Sstevel@tonic-gate }
30300Sstevel@tonic-gate
30310Sstevel@tonic-gate if (stp->sd_flag & errs) {
30320Sstevel@tonic-gate /*
30330Sstevel@tonic-gate * Check for errors before going to sleep since the
30340Sstevel@tonic-gate * caller might not have checked this while holding
30350Sstevel@tonic-gate * sd_lock.
30360Sstevel@tonic-gate */
30370Sstevel@tonic-gate error = strgeterr(stp, errs, (flag & STR_PEEK));
30380Sstevel@tonic-gate if (error != 0) {
30390Sstevel@tonic-gate *done = 1;
30400Sstevel@tonic-gate return (error);
30410Sstevel@tonic-gate }
30420Sstevel@tonic-gate }
30430Sstevel@tonic-gate
30440Sstevel@tonic-gate /*
30450Sstevel@tonic-gate * If any module downstream has requested read notification
30460Sstevel@tonic-gate * by setting SNDMREAD flag using M_SETOPTS, send a message
30470Sstevel@tonic-gate * down stream.
30480Sstevel@tonic-gate */
30490Sstevel@tonic-gate if ((flag & READWAIT) && (stp->sd_flag & SNDMREAD)) {
30500Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
30510Sstevel@tonic-gate if (!(mp = allocb_wait(sizeof (ssize_t), BPRI_MED,
30520Sstevel@tonic-gate (flag & STR_NOSIG), &error))) {
30530Sstevel@tonic-gate mutex_enter(&stp->sd_lock);
30540Sstevel@tonic-gate *done = 1;
30550Sstevel@tonic-gate return (error);
30560Sstevel@tonic-gate }
30570Sstevel@tonic-gate mp->b_datap->db_type = M_READ;
30580Sstevel@tonic-gate rd_count = (ssize_t *)mp->b_wptr;
30590Sstevel@tonic-gate *rd_count = count;
30600Sstevel@tonic-gate mp->b_wptr += sizeof (ssize_t);
30610Sstevel@tonic-gate /*
30620Sstevel@tonic-gate * Send the number of bytes requested by the
30630Sstevel@tonic-gate * read as the argument to M_READ.
30640Sstevel@tonic-gate */
30650Sstevel@tonic-gate stream_willservice(stp);
30660Sstevel@tonic-gate putnext(stp->sd_wrq, mp);
30670Sstevel@tonic-gate stream_runservice(stp);
30680Sstevel@tonic-gate mutex_enter(&stp->sd_lock);
30690Sstevel@tonic-gate
30700Sstevel@tonic-gate /*
30710Sstevel@tonic-gate * If any data arrived due to inline processing
30720Sstevel@tonic-gate * of putnext(), don't sleep.
30730Sstevel@tonic-gate */
30740Sstevel@tonic-gate if (_RD(stp->sd_wrq)->q_first != NULL) {
30750Sstevel@tonic-gate *done = 0;
30760Sstevel@tonic-gate return (0);
30770Sstevel@tonic-gate }
30780Sstevel@tonic-gate }
30790Sstevel@tonic-gate
30808581Sgdamore@opensolaris.org if (fmode & (FNDELAY|FNONBLOCK)) {
30818581Sgdamore@opensolaris.org if (!(flag & NOINTR))
30828581Sgdamore@opensolaris.org error = EAGAIN;
30838581Sgdamore@opensolaris.org else
30848581Sgdamore@opensolaris.org error = 0;
30858581Sgdamore@opensolaris.org *done = 1;
30868581Sgdamore@opensolaris.org return (error);
30878581Sgdamore@opensolaris.org }
30888581Sgdamore@opensolaris.org
30890Sstevel@tonic-gate stp->sd_flag |= slpflg;
30900Sstevel@tonic-gate TRACE_5(TR_FAC_STREAMS_FR, TR_STRWAITQ_WAIT2,
30915753Sgww "strwaitq sleeps (2):%p, %X, %lX, %X, %p",
30925753Sgww stp, flag, count, fmode, done);
30930Sstevel@tonic-gate
30940Sstevel@tonic-gate rval = str_cv_wait(sleepon, &stp->sd_lock, timout, flag & STR_NOSIG);
30950Sstevel@tonic-gate if (rval > 0) {
30960Sstevel@tonic-gate /* EMPTY */
30970Sstevel@tonic-gate TRACE_5(TR_FAC_STREAMS_FR, TR_STRWAITQ_WAKE2,
30985753Sgww "strwaitq awakes(2):%X, %X, %X, %X, %X",
30995753Sgww stp, flag, count, fmode, done);
31000Sstevel@tonic-gate } else if (rval == 0) {
31010Sstevel@tonic-gate TRACE_5(TR_FAC_STREAMS_FR, TR_STRWAITQ_INTR2,
31025753Sgww "strwaitq interrupt #2:%p, %X, %lX, %X, %p",
31035753Sgww stp, flag, count, fmode, done);
31040Sstevel@tonic-gate stp->sd_flag &= ~slpflg;
31050Sstevel@tonic-gate cv_broadcast(sleepon);
31060Sstevel@tonic-gate if (!(flag & NOINTR))
31070Sstevel@tonic-gate error = EINTR;
31080Sstevel@tonic-gate else
31090Sstevel@tonic-gate error = 0;
31100Sstevel@tonic-gate *done = 1;
31110Sstevel@tonic-gate return (error);
31120Sstevel@tonic-gate } else {
31130Sstevel@tonic-gate /* timeout */
31140Sstevel@tonic-gate TRACE_5(TR_FAC_STREAMS_FR, TR_STRWAITQ_TIME,
31155753Sgww "strwaitq timeout:%p, %X, %lX, %X, %p",
31165753Sgww stp, flag, count, fmode, done);
31170Sstevel@tonic-gate *done = 1;
31180Sstevel@tonic-gate if (!(flag & NOINTR))
31190Sstevel@tonic-gate return (ETIME);
31200Sstevel@tonic-gate else
31210Sstevel@tonic-gate return (0);
31220Sstevel@tonic-gate }
31230Sstevel@tonic-gate /*
31240Sstevel@tonic-gate * If the caller implements delayed errors (i.e. queued after data)
31250Sstevel@tonic-gate * we can not check for errors here since data as well as an
31260Sstevel@tonic-gate * error might have arrived at the stream head. We return to
31270Sstevel@tonic-gate * have the caller check the read queue before checking for errors.
31280Sstevel@tonic-gate */
31290Sstevel@tonic-gate if ((stp->sd_flag & errs) && !(flag & STR_DELAYERR)) {
31300Sstevel@tonic-gate error = strgeterr(stp, errs, (flag & STR_PEEK));
31310Sstevel@tonic-gate if (error != 0) {
31320Sstevel@tonic-gate *done = 1;
31330Sstevel@tonic-gate return (error);
31340Sstevel@tonic-gate }
31350Sstevel@tonic-gate }
31360Sstevel@tonic-gate *done = 0;
31370Sstevel@tonic-gate return (0);
31380Sstevel@tonic-gate }
31390Sstevel@tonic-gate
31400Sstevel@tonic-gate /*
31410Sstevel@tonic-gate * Perform job control discipline access checks.
31420Sstevel@tonic-gate * Return 0 for success and the errno for failure.
31430Sstevel@tonic-gate */
31440Sstevel@tonic-gate
31450Sstevel@tonic-gate #define cantsend(p, t, sig) \
31460Sstevel@tonic-gate (sigismember(&(p)->p_ignore, sig) || signal_is_blocked((t), sig))
31470Sstevel@tonic-gate
31480Sstevel@tonic-gate int
straccess(struct stdata * stp,enum jcaccess mode)31490Sstevel@tonic-gate straccess(struct stdata *stp, enum jcaccess mode)
31500Sstevel@tonic-gate {
31510Sstevel@tonic-gate extern kcondvar_t lbolt_cv; /* XXX: should be in a header file */
31520Sstevel@tonic-gate kthread_t *t = curthread;
31530Sstevel@tonic-gate proc_t *p = ttoproc(t);
31540Sstevel@tonic-gate sess_t *sp;
31550Sstevel@tonic-gate
31562712Snn35248 ASSERT(mutex_owned(&stp->sd_lock));
31572712Snn35248
31580Sstevel@tonic-gate if (stp->sd_sidp == NULL || stp->sd_vnode->v_type == VFIFO)
31590Sstevel@tonic-gate return (0);
31600Sstevel@tonic-gate
31612712Snn35248 mutex_enter(&p->p_lock); /* protects p_pgidp */
31620Sstevel@tonic-gate
31630Sstevel@tonic-gate for (;;) {
31642712Snn35248 mutex_enter(&p->p_splock); /* protects p->p_sessp */
31652712Snn35248 sp = p->p_sessp;
31662712Snn35248 mutex_enter(&sp->s_lock); /* protects sp->* */
31672712Snn35248
31680Sstevel@tonic-gate /*
31690Sstevel@tonic-gate * If this is not the calling process's controlling terminal
31700Sstevel@tonic-gate * or if the calling process is already in the foreground
31710Sstevel@tonic-gate * then allow access.
31720Sstevel@tonic-gate */
31730Sstevel@tonic-gate if (sp->s_dev != stp->sd_vnode->v_rdev ||
31740Sstevel@tonic-gate p->p_pgidp == stp->sd_pgidp) {
31752712Snn35248 mutex_exit(&sp->s_lock);
31762712Snn35248 mutex_exit(&p->p_splock);
31770Sstevel@tonic-gate mutex_exit(&p->p_lock);
31780Sstevel@tonic-gate return (0);
31790Sstevel@tonic-gate }
31800Sstevel@tonic-gate
31810Sstevel@tonic-gate /*
31820Sstevel@tonic-gate * Check to see if controlling terminal has been deallocated.
31830Sstevel@tonic-gate */
31840Sstevel@tonic-gate if (sp->s_vp == NULL) {
31850Sstevel@tonic-gate if (!cantsend(p, t, SIGHUP))
31860Sstevel@tonic-gate sigtoproc(p, t, SIGHUP);
31872712Snn35248 mutex_exit(&sp->s_lock);
31882712Snn35248 mutex_exit(&p->p_splock);
31890Sstevel@tonic-gate mutex_exit(&p->p_lock);
31900Sstevel@tonic-gate return (EIO);
31910Sstevel@tonic-gate }
31920Sstevel@tonic-gate
31932712Snn35248 mutex_exit(&sp->s_lock);
31942712Snn35248 mutex_exit(&p->p_splock);
31952712Snn35248
31960Sstevel@tonic-gate if (mode == JCGETP) {
31970Sstevel@tonic-gate mutex_exit(&p->p_lock);
31980Sstevel@tonic-gate return (0);
31990Sstevel@tonic-gate }
32000Sstevel@tonic-gate
32010Sstevel@tonic-gate if (mode == JCREAD) {
32020Sstevel@tonic-gate if (p->p_detached || cantsend(p, t, SIGTTIN)) {
32030Sstevel@tonic-gate mutex_exit(&p->p_lock);
32040Sstevel@tonic-gate return (EIO);
32050Sstevel@tonic-gate }
32060Sstevel@tonic-gate mutex_exit(&p->p_lock);
32072712Snn35248 mutex_exit(&stp->sd_lock);
32080Sstevel@tonic-gate pgsignal(p->p_pgidp, SIGTTIN);
32092712Snn35248 mutex_enter(&stp->sd_lock);
32100Sstevel@tonic-gate mutex_enter(&p->p_lock);
32110Sstevel@tonic-gate } else { /* mode == JCWRITE or JCSETP */
32120Sstevel@tonic-gate if ((mode == JCWRITE && !(stp->sd_flag & STRTOSTOP)) ||
32130Sstevel@tonic-gate cantsend(p, t, SIGTTOU)) {
32140Sstevel@tonic-gate mutex_exit(&p->p_lock);
32150Sstevel@tonic-gate return (0);
32160Sstevel@tonic-gate }
32170Sstevel@tonic-gate if (p->p_detached) {
32180Sstevel@tonic-gate mutex_exit(&p->p_lock);
32190Sstevel@tonic-gate return (EIO);
32200Sstevel@tonic-gate }
32210Sstevel@tonic-gate mutex_exit(&p->p_lock);
32222712Snn35248 mutex_exit(&stp->sd_lock);
32230Sstevel@tonic-gate pgsignal(p->p_pgidp, SIGTTOU);
32242712Snn35248 mutex_enter(&stp->sd_lock);
32250Sstevel@tonic-gate mutex_enter(&p->p_lock);
32260Sstevel@tonic-gate }
32270Sstevel@tonic-gate
32280Sstevel@tonic-gate /*
32290Sstevel@tonic-gate * We call cv_wait_sig_swap() to cause the appropriate
32300Sstevel@tonic-gate * action for the jobcontrol signal to take place.
32310Sstevel@tonic-gate * If the signal is being caught, we will take the
32320Sstevel@tonic-gate * EINTR error return. Otherwise, the default action
32330Sstevel@tonic-gate * of causing the process to stop will take place.
32340Sstevel@tonic-gate * In this case, we rely on the periodic cv_broadcast() on
32350Sstevel@tonic-gate * &lbolt_cv to wake us up to loop around and test again.
32360Sstevel@tonic-gate * We can't get here if the signal is ignored or
32370Sstevel@tonic-gate * if the current thread is blocking the signal.
32380Sstevel@tonic-gate */
32392712Snn35248 mutex_exit(&stp->sd_lock);
32400Sstevel@tonic-gate if (!cv_wait_sig_swap(&lbolt_cv, &p->p_lock)) {
32410Sstevel@tonic-gate mutex_exit(&p->p_lock);
32422712Snn35248 mutex_enter(&stp->sd_lock);
32430Sstevel@tonic-gate return (EINTR);
32440Sstevel@tonic-gate }
32452712Snn35248 mutex_exit(&p->p_lock);
32462712Snn35248 mutex_enter(&stp->sd_lock);
32472712Snn35248 mutex_enter(&p->p_lock);
32480Sstevel@tonic-gate }
32490Sstevel@tonic-gate }
32500Sstevel@tonic-gate
32510Sstevel@tonic-gate /*
32520Sstevel@tonic-gate * Return size of message of block type (bp->b_datap->db_type)
32530Sstevel@tonic-gate */
32540Sstevel@tonic-gate size_t
xmsgsize(mblk_t * bp)32550Sstevel@tonic-gate xmsgsize(mblk_t *bp)
32560Sstevel@tonic-gate {
32570Sstevel@tonic-gate unsigned char type;
32580Sstevel@tonic-gate size_t count = 0;
32590Sstevel@tonic-gate
32600Sstevel@tonic-gate type = bp->b_datap->db_type;
32610Sstevel@tonic-gate
32620Sstevel@tonic-gate for (; bp; bp = bp->b_cont) {
32630Sstevel@tonic-gate if (type != bp->b_datap->db_type)
32640Sstevel@tonic-gate break;
32650Sstevel@tonic-gate ASSERT(bp->b_wptr >= bp->b_rptr);
32660Sstevel@tonic-gate count += bp->b_wptr - bp->b_rptr;
32670Sstevel@tonic-gate }
32680Sstevel@tonic-gate return (count);
32690Sstevel@tonic-gate }
32700Sstevel@tonic-gate
32710Sstevel@tonic-gate /*
32720Sstevel@tonic-gate * Allocate a stream head.
32730Sstevel@tonic-gate */
32740Sstevel@tonic-gate struct stdata *
shalloc(queue_t * qp)32750Sstevel@tonic-gate shalloc(queue_t *qp)
32760Sstevel@tonic-gate {
32770Sstevel@tonic-gate stdata_t *stp;
32780Sstevel@tonic-gate
32790Sstevel@tonic-gate stp = kmem_cache_alloc(stream_head_cache, KM_SLEEP);
32800Sstevel@tonic-gate
32810Sstevel@tonic-gate stp->sd_wrq = _WR(qp);
32820Sstevel@tonic-gate stp->sd_strtab = NULL;
32830Sstevel@tonic-gate stp->sd_iocid = 0;
32840Sstevel@tonic-gate stp->sd_mate = NULL;
32850Sstevel@tonic-gate stp->sd_freezer = NULL;
32860Sstevel@tonic-gate stp->sd_refcnt = 0;
32870Sstevel@tonic-gate stp->sd_wakeq = 0;
32880Sstevel@tonic-gate stp->sd_anchor = 0;
32890Sstevel@tonic-gate stp->sd_struiowrq = NULL;
32900Sstevel@tonic-gate stp->sd_struiordq = NULL;
32910Sstevel@tonic-gate stp->sd_struiodnak = 0;
32920Sstevel@tonic-gate stp->sd_struionak = NULL;
32930Sstevel@tonic-gate stp->sd_t_audit_data = NULL;
32940Sstevel@tonic-gate stp->sd_rput_opt = 0;
32950Sstevel@tonic-gate stp->sd_wput_opt = 0;
32960Sstevel@tonic-gate stp->sd_read_opt = 0;
32970Sstevel@tonic-gate stp->sd_rprotofunc = strrput_proto;
32980Sstevel@tonic-gate stp->sd_rmiscfunc = strrput_misc;
32990Sstevel@tonic-gate stp->sd_rderrfunc = stp->sd_wrerrfunc = NULL;
3300898Skais stp->sd_rputdatafunc = stp->sd_wputdatafunc = NULL;
33010Sstevel@tonic-gate stp->sd_ciputctrl = NULL;
33020Sstevel@tonic-gate stp->sd_nciputctrl = 0;
33030Sstevel@tonic-gate stp->sd_qhead = NULL;
33040Sstevel@tonic-gate stp->sd_qtail = NULL;
33050Sstevel@tonic-gate stp->sd_servid = NULL;
33060Sstevel@tonic-gate stp->sd_nqueues = 0;
33070Sstevel@tonic-gate stp->sd_svcflags = 0;
33080Sstevel@tonic-gate stp->sd_copyflag = 0;
3309898Skais
33100Sstevel@tonic-gate return (stp);
33110Sstevel@tonic-gate }
33120Sstevel@tonic-gate
33130Sstevel@tonic-gate /*
33140Sstevel@tonic-gate * Free a stream head.
33150Sstevel@tonic-gate */
33160Sstevel@tonic-gate void
shfree(stdata_t * stp)33170Sstevel@tonic-gate shfree(stdata_t *stp)
33180Sstevel@tonic-gate {
33190Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&stp->sd_lock));
33200Sstevel@tonic-gate
33210Sstevel@tonic-gate stp->sd_wrq = NULL;
33220Sstevel@tonic-gate
33230Sstevel@tonic-gate mutex_enter(&stp->sd_qlock);
33240Sstevel@tonic-gate while (stp->sd_svcflags & STRS_SCHEDULED) {
33250Sstevel@tonic-gate STRSTAT(strwaits);
33260Sstevel@tonic-gate cv_wait(&stp->sd_qcv, &stp->sd_qlock);
33270Sstevel@tonic-gate }
33280Sstevel@tonic-gate mutex_exit(&stp->sd_qlock);
33290Sstevel@tonic-gate
33300Sstevel@tonic-gate if (stp->sd_ciputctrl != NULL) {
33310Sstevel@tonic-gate ASSERT(stp->sd_nciputctrl == n_ciputctrl - 1);
33320Sstevel@tonic-gate SUMCHECK_CIPUTCTRL_COUNTS(stp->sd_ciputctrl,
33330Sstevel@tonic-gate stp->sd_nciputctrl, 0);
33340Sstevel@tonic-gate ASSERT(ciputctrl_cache != NULL);
33350Sstevel@tonic-gate kmem_cache_free(ciputctrl_cache, stp->sd_ciputctrl);
33360Sstevel@tonic-gate stp->sd_ciputctrl = NULL;
33370Sstevel@tonic-gate stp->sd_nciputctrl = 0;
33380Sstevel@tonic-gate }
33390Sstevel@tonic-gate ASSERT(stp->sd_qhead == NULL);
33400Sstevel@tonic-gate ASSERT(stp->sd_qtail == NULL);
33410Sstevel@tonic-gate ASSERT(stp->sd_nqueues == 0);
33420Sstevel@tonic-gate kmem_cache_free(stream_head_cache, stp);
33430Sstevel@tonic-gate }
33440Sstevel@tonic-gate
33450Sstevel@tonic-gate /*
33460Sstevel@tonic-gate * Allocate a pair of queues and a syncq for the pair
33470Sstevel@tonic-gate */
33480Sstevel@tonic-gate queue_t *
allocq(void)33490Sstevel@tonic-gate allocq(void)
33500Sstevel@tonic-gate {
33510Sstevel@tonic-gate queinfo_t *qip;
33520Sstevel@tonic-gate queue_t *qp, *wqp;
33530Sstevel@tonic-gate syncq_t *sq;
33540Sstevel@tonic-gate
33550Sstevel@tonic-gate qip = kmem_cache_alloc(queue_cache, KM_SLEEP);
33560Sstevel@tonic-gate
33570Sstevel@tonic-gate qp = &qip->qu_rqueue;
33580Sstevel@tonic-gate wqp = &qip->qu_wqueue;
33590Sstevel@tonic-gate sq = &qip->qu_syncq;
33600Sstevel@tonic-gate
33610Sstevel@tonic-gate qp->q_last = NULL;
33620Sstevel@tonic-gate qp->q_next = NULL;
33630Sstevel@tonic-gate qp->q_ptr = NULL;
33640Sstevel@tonic-gate qp->q_flag = QUSE | QREADR;
33650Sstevel@tonic-gate qp->q_bandp = NULL;
33660Sstevel@tonic-gate qp->q_stream = NULL;
33670Sstevel@tonic-gate qp->q_syncq = sq;
33680Sstevel@tonic-gate qp->q_nband = 0;
33690Sstevel@tonic-gate qp->q_nfsrv = NULL;
33700Sstevel@tonic-gate qp->q_draining = 0;
33710Sstevel@tonic-gate qp->q_syncqmsgs = 0;
33720Sstevel@tonic-gate qp->q_spri = 0;
33730Sstevel@tonic-gate qp->q_qtstamp = 0;
33740Sstevel@tonic-gate qp->q_sqtstamp = 0;
33750Sstevel@tonic-gate qp->q_fp = NULL;
33760Sstevel@tonic-gate
33770Sstevel@tonic-gate wqp->q_last = NULL;
33780Sstevel@tonic-gate wqp->q_next = NULL;
33790Sstevel@tonic-gate wqp->q_ptr = NULL;
33800Sstevel@tonic-gate wqp->q_flag = QUSE;
33810Sstevel@tonic-gate wqp->q_bandp = NULL;
33820Sstevel@tonic-gate wqp->q_stream = NULL;
33830Sstevel@tonic-gate wqp->q_syncq = sq;
33840Sstevel@tonic-gate wqp->q_nband = 0;
33850Sstevel@tonic-gate wqp->q_nfsrv = NULL;
33860Sstevel@tonic-gate wqp->q_draining = 0;
33870Sstevel@tonic-gate wqp->q_syncqmsgs = 0;
33880Sstevel@tonic-gate wqp->q_qtstamp = 0;
33890Sstevel@tonic-gate wqp->q_sqtstamp = 0;
33900Sstevel@tonic-gate wqp->q_spri = 0;
33910Sstevel@tonic-gate
33920Sstevel@tonic-gate sq->sq_count = 0;
33930Sstevel@tonic-gate sq->sq_rmqcount = 0;
33940Sstevel@tonic-gate sq->sq_flags = 0;
33950Sstevel@tonic-gate sq->sq_type = 0;
33960Sstevel@tonic-gate sq->sq_callbflags = 0;
33970Sstevel@tonic-gate sq->sq_cancelid = 0;
33980Sstevel@tonic-gate sq->sq_ciputctrl = NULL;
33990Sstevel@tonic-gate sq->sq_nciputctrl = 0;
34000Sstevel@tonic-gate sq->sq_needexcl = 0;
34010Sstevel@tonic-gate sq->sq_svcflags = 0;
34020Sstevel@tonic-gate
34030Sstevel@tonic-gate return (qp);
34040Sstevel@tonic-gate }
34050Sstevel@tonic-gate
34060Sstevel@tonic-gate /*
34070Sstevel@tonic-gate * Free a pair of queues and the "attached" syncq.
34080Sstevel@tonic-gate * Discard any messages left on the syncq(s), remove the syncq(s) from the
34090Sstevel@tonic-gate * outer perimeter, and free the syncq(s) if they are not the "attached" syncq.
34100Sstevel@tonic-gate */
34110Sstevel@tonic-gate void
freeq(queue_t * qp)34120Sstevel@tonic-gate freeq(queue_t *qp)
34130Sstevel@tonic-gate {
34140Sstevel@tonic-gate qband_t *qbp, *nqbp;
34150Sstevel@tonic-gate syncq_t *sq, *outer;
34160Sstevel@tonic-gate queue_t *wqp = _WR(qp);
34170Sstevel@tonic-gate
34180Sstevel@tonic-gate ASSERT(qp->q_flag & QREADR);
34190Sstevel@tonic-gate
34202497Sgeorges /*
34212497Sgeorges * If a previously dispatched taskq job is scheduled to run
34222497Sgeorges * sync_service() or a service routine is scheduled for the
34232497Sgeorges * queues about to be freed, wait here until all service is
34242497Sgeorges * done on the queue and all associated queues and syncqs.
34252497Sgeorges */
34262497Sgeorges wait_svc(qp);
34272497Sgeorges
34280Sstevel@tonic-gate (void) flush_syncq(qp->q_syncq, qp);
34290Sstevel@tonic-gate (void) flush_syncq(wqp->q_syncq, wqp);
34300Sstevel@tonic-gate ASSERT(qp->q_syncqmsgs == 0 && wqp->q_syncqmsgs == 0);
34310Sstevel@tonic-gate
34322497Sgeorges /*
34332497Sgeorges * Flush the queues before q_next is set to NULL This is needed
34342497Sgeorges * in order to backenable any downstream queue before we go away.
34352497Sgeorges * Note: we are already removed from the stream so that the
34362497Sgeorges * backenabling will not cause any messages to be delivered to our
34372497Sgeorges * put procedures.
34382497Sgeorges */
34392497Sgeorges flushq(qp, FLUSHALL);
34402497Sgeorges flushq(wqp, FLUSHALL);
34412497Sgeorges
34422497Sgeorges /* Tidy up - removeq only does a half-remove from stream */
34432497Sgeorges qp->q_next = wqp->q_next = NULL;
34442497Sgeorges ASSERT(!(qp->q_flag & QENAB));
34452497Sgeorges ASSERT(!(wqp->q_flag & QENAB));
34462497Sgeorges
34470Sstevel@tonic-gate outer = qp->q_syncq->sq_outer;
34480Sstevel@tonic-gate if (outer != NULL) {
34490Sstevel@tonic-gate outer_remove(outer, qp->q_syncq);
34500Sstevel@tonic-gate if (wqp->q_syncq != qp->q_syncq)
34510Sstevel@tonic-gate outer_remove(outer, wqp->q_syncq);
34520Sstevel@tonic-gate }
34530Sstevel@tonic-gate /*
34540Sstevel@tonic-gate * Free any syncqs that are outside what allocq returned.
34550Sstevel@tonic-gate */
34560Sstevel@tonic-gate if (qp->q_syncq != SQ(qp) && !(qp->q_flag & QPERMOD))
34570Sstevel@tonic-gate free_syncq(qp->q_syncq);
34580Sstevel@tonic-gate if (qp->q_syncq != wqp->q_syncq && wqp->q_syncq != SQ(qp))
34590Sstevel@tonic-gate free_syncq(wqp->q_syncq);
34600Sstevel@tonic-gate
34610Sstevel@tonic-gate ASSERT((qp->q_sqflags & (Q_SQQUEUED | Q_SQDRAINING)) == 0);
34620Sstevel@tonic-gate ASSERT((wqp->q_sqflags & (Q_SQQUEUED | Q_SQDRAINING)) == 0);
34630Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(QLOCK(qp)));
34640Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(QLOCK(wqp)));
34650Sstevel@tonic-gate sq = SQ(qp);
34660Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(SQLOCK(sq)));
34670Sstevel@tonic-gate ASSERT(sq->sq_head == NULL && sq->sq_tail == NULL);
34680Sstevel@tonic-gate ASSERT(sq->sq_outer == NULL);
34690Sstevel@tonic-gate ASSERT(sq->sq_onext == NULL && sq->sq_oprev == NULL);
34700Sstevel@tonic-gate ASSERT(sq->sq_callbpend == NULL);
34710Sstevel@tonic-gate ASSERT(sq->sq_needexcl == 0);
34720Sstevel@tonic-gate
34730Sstevel@tonic-gate if (sq->sq_ciputctrl != NULL) {
34740Sstevel@tonic-gate ASSERT(sq->sq_nciputctrl == n_ciputctrl - 1);
34750Sstevel@tonic-gate SUMCHECK_CIPUTCTRL_COUNTS(sq->sq_ciputctrl,
34760Sstevel@tonic-gate sq->sq_nciputctrl, 0);
34770Sstevel@tonic-gate ASSERT(ciputctrl_cache != NULL);
34780Sstevel@tonic-gate kmem_cache_free(ciputctrl_cache, sq->sq_ciputctrl);
34790Sstevel@tonic-gate sq->sq_ciputctrl = NULL;
34800Sstevel@tonic-gate sq->sq_nciputctrl = 0;
34810Sstevel@tonic-gate }
34820Sstevel@tonic-gate
34830Sstevel@tonic-gate ASSERT(qp->q_first == NULL && wqp->q_first == NULL);
34840Sstevel@tonic-gate ASSERT(qp->q_count == 0 && wqp->q_count == 0);
34850Sstevel@tonic-gate ASSERT(qp->q_mblkcnt == 0 && wqp->q_mblkcnt == 0);
34860Sstevel@tonic-gate
34870Sstevel@tonic-gate qp->q_flag &= ~QUSE;
34880Sstevel@tonic-gate wqp->q_flag &= ~QUSE;
34890Sstevel@tonic-gate
34900Sstevel@tonic-gate /* NOTE: Uncomment the assert below once bugid 1159635 is fixed. */
34910Sstevel@tonic-gate /* ASSERT((qp->q_flag & QWANTW) == 0 && (wqp->q_flag & QWANTW) == 0); */
34920Sstevel@tonic-gate
34930Sstevel@tonic-gate qbp = qp->q_bandp;
34940Sstevel@tonic-gate while (qbp) {
34950Sstevel@tonic-gate nqbp = qbp->qb_next;
34960Sstevel@tonic-gate freeband(qbp);
34970Sstevel@tonic-gate qbp = nqbp;
34980Sstevel@tonic-gate }
34990Sstevel@tonic-gate qbp = wqp->q_bandp;
35000Sstevel@tonic-gate while (qbp) {
35010Sstevel@tonic-gate nqbp = qbp->qb_next;
35020Sstevel@tonic-gate freeband(qbp);
35030Sstevel@tonic-gate qbp = nqbp;
35040Sstevel@tonic-gate }
35050Sstevel@tonic-gate kmem_cache_free(queue_cache, qp);
35060Sstevel@tonic-gate }
35070Sstevel@tonic-gate
35080Sstevel@tonic-gate /*
35090Sstevel@tonic-gate * Allocate a qband structure.
35100Sstevel@tonic-gate */
35110Sstevel@tonic-gate qband_t *
allocband(void)35120Sstevel@tonic-gate allocband(void)
35130Sstevel@tonic-gate {
35140Sstevel@tonic-gate qband_t *qbp;
35150Sstevel@tonic-gate
35160Sstevel@tonic-gate qbp = kmem_cache_alloc(qband_cache, KM_NOSLEEP);
35170Sstevel@tonic-gate if (qbp == NULL)
35180Sstevel@tonic-gate return (NULL);
35190Sstevel@tonic-gate
35200Sstevel@tonic-gate qbp->qb_next = NULL;
35210Sstevel@tonic-gate qbp->qb_count = 0;
35220Sstevel@tonic-gate qbp->qb_mblkcnt = 0;
35230Sstevel@tonic-gate qbp->qb_first = NULL;
35240Sstevel@tonic-gate qbp->qb_last = NULL;
35250Sstevel@tonic-gate qbp->qb_flag = 0;
35260Sstevel@tonic-gate
35270Sstevel@tonic-gate return (qbp);
35280Sstevel@tonic-gate }
35290Sstevel@tonic-gate
35300Sstevel@tonic-gate /*
35310Sstevel@tonic-gate * Free a qband structure.
35320Sstevel@tonic-gate */
35330Sstevel@tonic-gate void
freeband(qband_t * qbp)35340Sstevel@tonic-gate freeband(qband_t *qbp)
35350Sstevel@tonic-gate {
35360Sstevel@tonic-gate kmem_cache_free(qband_cache, qbp);
35370Sstevel@tonic-gate }
35380Sstevel@tonic-gate
35390Sstevel@tonic-gate /*
35400Sstevel@tonic-gate * Just like putnextctl(9F), except that allocb_wait() is used.
35410Sstevel@tonic-gate *
35420Sstevel@tonic-gate * Consolidation Private, and of course only callable from the stream head or
35430Sstevel@tonic-gate * routines that may block.
35440Sstevel@tonic-gate */
35450Sstevel@tonic-gate int
putnextctl_wait(queue_t * q,int type)35460Sstevel@tonic-gate putnextctl_wait(queue_t *q, int type)
35470Sstevel@tonic-gate {
35480Sstevel@tonic-gate mblk_t *bp;
35490Sstevel@tonic-gate int error;
35500Sstevel@tonic-gate
35510Sstevel@tonic-gate if ((datamsg(type) && (type != M_DELAY)) ||
35520Sstevel@tonic-gate (bp = allocb_wait(0, BPRI_HI, 0, &error)) == NULL)
35530Sstevel@tonic-gate return (0);
35540Sstevel@tonic-gate
35550Sstevel@tonic-gate bp->b_datap->db_type = (unsigned char)type;
35560Sstevel@tonic-gate putnext(q, bp);
35570Sstevel@tonic-gate return (1);
35580Sstevel@tonic-gate }
35590Sstevel@tonic-gate
35600Sstevel@tonic-gate /*
35619671SBrian.Ruthven@Sun.COM * Run any possible bufcalls.
35620Sstevel@tonic-gate */
35630Sstevel@tonic-gate void
runbufcalls(void)35640Sstevel@tonic-gate runbufcalls(void)
35650Sstevel@tonic-gate {
35660Sstevel@tonic-gate strbufcall_t *bcp;
35670Sstevel@tonic-gate
35680Sstevel@tonic-gate mutex_enter(&bcall_monitor);
35690Sstevel@tonic-gate mutex_enter(&strbcall_lock);
35700Sstevel@tonic-gate
35710Sstevel@tonic-gate if (strbcalls.bc_head) {
35720Sstevel@tonic-gate size_t count;
35730Sstevel@tonic-gate int nevent;
35740Sstevel@tonic-gate
35750Sstevel@tonic-gate /*
35760Sstevel@tonic-gate * count how many events are on the list
35770Sstevel@tonic-gate * now so we can check to avoid looping
35780Sstevel@tonic-gate * in low memory situations
35790Sstevel@tonic-gate */
35800Sstevel@tonic-gate nevent = 0;
35810Sstevel@tonic-gate for (bcp = strbcalls.bc_head; bcp; bcp = bcp->bc_next)
35820Sstevel@tonic-gate nevent++;
35830Sstevel@tonic-gate
35840Sstevel@tonic-gate /*
35850Sstevel@tonic-gate * get estimate of available memory from kmem_avail().
35860Sstevel@tonic-gate * awake all bufcall functions waiting for
35870Sstevel@tonic-gate * memory whose request could be satisfied
35880Sstevel@tonic-gate * by 'count' memory and let 'em fight for it.
35890Sstevel@tonic-gate */
35900Sstevel@tonic-gate count = kmem_avail();
35910Sstevel@tonic-gate while ((bcp = strbcalls.bc_head) != NULL && nevent) {
35920Sstevel@tonic-gate STRSTAT(bufcalls);
35930Sstevel@tonic-gate --nevent;
35940Sstevel@tonic-gate if (bcp->bc_size <= count) {
35950Sstevel@tonic-gate bcp->bc_executor = curthread;
35960Sstevel@tonic-gate mutex_exit(&strbcall_lock);
35970Sstevel@tonic-gate (*bcp->bc_func)(bcp->bc_arg);
35980Sstevel@tonic-gate mutex_enter(&strbcall_lock);
35990Sstevel@tonic-gate bcp->bc_executor = NULL;
36000Sstevel@tonic-gate cv_broadcast(&bcall_cv);
36010Sstevel@tonic-gate strbcalls.bc_head = bcp->bc_next;
36020Sstevel@tonic-gate kmem_free(bcp, sizeof (strbufcall_t));
36030Sstevel@tonic-gate } else {
36040Sstevel@tonic-gate /*
36050Sstevel@tonic-gate * too big, try again later - note
36060Sstevel@tonic-gate * that nevent was decremented above
36070Sstevel@tonic-gate * so we won't retry this one on this
36080Sstevel@tonic-gate * iteration of the loop
36090Sstevel@tonic-gate */
36100Sstevel@tonic-gate if (bcp->bc_next != NULL) {
36110Sstevel@tonic-gate strbcalls.bc_head = bcp->bc_next;
36120Sstevel@tonic-gate bcp->bc_next = NULL;
36130Sstevel@tonic-gate strbcalls.bc_tail->bc_next = bcp;
36140Sstevel@tonic-gate strbcalls.bc_tail = bcp;
36150Sstevel@tonic-gate }
36160Sstevel@tonic-gate }
36170Sstevel@tonic-gate }
36180Sstevel@tonic-gate if (strbcalls.bc_head == NULL)
36190Sstevel@tonic-gate strbcalls.bc_tail = NULL;
36200Sstevel@tonic-gate }
36210Sstevel@tonic-gate
36220Sstevel@tonic-gate mutex_exit(&strbcall_lock);
36230Sstevel@tonic-gate mutex_exit(&bcall_monitor);
36240Sstevel@tonic-gate }
36250Sstevel@tonic-gate
36260Sstevel@tonic-gate
36270Sstevel@tonic-gate /*
36289671SBrian.Ruthven@Sun.COM * Actually run queue's service routine.
36290Sstevel@tonic-gate */
36300Sstevel@tonic-gate static void
runservice(queue_t * q)36310Sstevel@tonic-gate runservice(queue_t *q)
36320Sstevel@tonic-gate {
36330Sstevel@tonic-gate qband_t *qbp;
36340Sstevel@tonic-gate
36350Sstevel@tonic-gate ASSERT(q->q_qinfo->qi_srvp);
36360Sstevel@tonic-gate again:
36370Sstevel@tonic-gate entersq(q->q_syncq, SQ_SVC);
36380Sstevel@tonic-gate TRACE_1(TR_FAC_STREAMS_FR, TR_QRUNSERVICE_START,
36395753Sgww "runservice starts:%p", q);
36400Sstevel@tonic-gate
36410Sstevel@tonic-gate if (!(q->q_flag & QWCLOSE))
36420Sstevel@tonic-gate (*q->q_qinfo->qi_srvp)(q);
36430Sstevel@tonic-gate
36440Sstevel@tonic-gate TRACE_1(TR_FAC_STREAMS_FR, TR_QRUNSERVICE_END,
36455753Sgww "runservice ends:(%p)", q);
36460Sstevel@tonic-gate
36470Sstevel@tonic-gate leavesq(q->q_syncq, SQ_SVC);
36480Sstevel@tonic-gate
36490Sstevel@tonic-gate mutex_enter(QLOCK(q));
36500Sstevel@tonic-gate if (q->q_flag & QENAB) {
36510Sstevel@tonic-gate q->q_flag &= ~QENAB;
36520Sstevel@tonic-gate mutex_exit(QLOCK(q));
36530Sstevel@tonic-gate goto again;
36540Sstevel@tonic-gate }
36550Sstevel@tonic-gate q->q_flag &= ~QINSERVICE;
36560Sstevel@tonic-gate q->q_flag &= ~QBACK;
36570Sstevel@tonic-gate for (qbp = q->q_bandp; qbp; qbp = qbp->qb_next)
36580Sstevel@tonic-gate qbp->qb_flag &= ~QB_BACK;
36590Sstevel@tonic-gate /*
36600Sstevel@tonic-gate * Wakeup thread waiting for the service procedure
36610Sstevel@tonic-gate * to be run (strclose and qdetach).
36620Sstevel@tonic-gate */
36630Sstevel@tonic-gate cv_broadcast(&q->q_wait);
36640Sstevel@tonic-gate
36650Sstevel@tonic-gate mutex_exit(QLOCK(q));
36660Sstevel@tonic-gate }
36670Sstevel@tonic-gate
36680Sstevel@tonic-gate /*
36690Sstevel@tonic-gate * Background processing of bufcalls.
36700Sstevel@tonic-gate */
36710Sstevel@tonic-gate void
streams_bufcall_service(void)36720Sstevel@tonic-gate streams_bufcall_service(void)
36730Sstevel@tonic-gate {
36740Sstevel@tonic-gate callb_cpr_t cprinfo;
36750Sstevel@tonic-gate
36760Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, &strbcall_lock, callb_generic_cpr,
36770Sstevel@tonic-gate "streams_bufcall_service");
36780Sstevel@tonic-gate
36790Sstevel@tonic-gate mutex_enter(&strbcall_lock);
36800Sstevel@tonic-gate
36810Sstevel@tonic-gate for (;;) {
36820Sstevel@tonic-gate if (strbcalls.bc_head != NULL && kmem_avail() > 0) {
36830Sstevel@tonic-gate mutex_exit(&strbcall_lock);
36840Sstevel@tonic-gate runbufcalls();
36850Sstevel@tonic-gate mutex_enter(&strbcall_lock);
36860Sstevel@tonic-gate }
36870Sstevel@tonic-gate if (strbcalls.bc_head != NULL) {
36880Sstevel@tonic-gate STRSTAT(bcwaits);
36890Sstevel@tonic-gate /* Wait for memory to become available */
36900Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cprinfo);
369111066Srafael.vanoni@sun.com (void) cv_reltimedwait(&memavail_cv, &strbcall_lock,
369211066Srafael.vanoni@sun.com SEC_TO_TICK(60), TR_CLOCK_TICK);
36930Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cprinfo, &strbcall_lock);
36940Sstevel@tonic-gate }
36950Sstevel@tonic-gate
36960Sstevel@tonic-gate /* Wait for new work to arrive */
36970Sstevel@tonic-gate if (strbcalls.bc_head == NULL) {
36980Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cprinfo);
36990Sstevel@tonic-gate cv_wait(&strbcall_cv, &strbcall_lock);
37000Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cprinfo, &strbcall_lock);
37010Sstevel@tonic-gate }
37020Sstevel@tonic-gate }
37030Sstevel@tonic-gate }
37040Sstevel@tonic-gate
37050Sstevel@tonic-gate /*
37060Sstevel@tonic-gate * Background processing of streams background tasks which failed
37070Sstevel@tonic-gate * taskq_dispatch.
37080Sstevel@tonic-gate */
37090Sstevel@tonic-gate static void
streams_qbkgrnd_service(void)37100Sstevel@tonic-gate streams_qbkgrnd_service(void)
37110Sstevel@tonic-gate {
37120Sstevel@tonic-gate callb_cpr_t cprinfo;
37130Sstevel@tonic-gate queue_t *q;
37140Sstevel@tonic-gate
37150Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, &service_queue, callb_generic_cpr,
37160Sstevel@tonic-gate "streams_bkgrnd_service");
37170Sstevel@tonic-gate
37180Sstevel@tonic-gate mutex_enter(&service_queue);
37190Sstevel@tonic-gate
37200Sstevel@tonic-gate for (;;) {
37210Sstevel@tonic-gate /*
37220Sstevel@tonic-gate * Wait for work to arrive.
37230Sstevel@tonic-gate */
37240Sstevel@tonic-gate while ((freebs_list == NULL) && (qhead == NULL)) {
37250Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cprinfo);
37260Sstevel@tonic-gate cv_wait(&services_to_run, &service_queue);
37270Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cprinfo, &service_queue);
37280Sstevel@tonic-gate }
37290Sstevel@tonic-gate /*
37300Sstevel@tonic-gate * Handle all pending freebs requests to free memory.
37310Sstevel@tonic-gate */
37320Sstevel@tonic-gate while (freebs_list != NULL) {
37330Sstevel@tonic-gate mblk_t *mp = freebs_list;
37340Sstevel@tonic-gate freebs_list = mp->b_next;
37350Sstevel@tonic-gate mutex_exit(&service_queue);
37360Sstevel@tonic-gate mblk_free(mp);
37370Sstevel@tonic-gate mutex_enter(&service_queue);
37380Sstevel@tonic-gate }
37390Sstevel@tonic-gate /*
37400Sstevel@tonic-gate * Run pending queues.
37410Sstevel@tonic-gate */
37420Sstevel@tonic-gate while (qhead != NULL) {
37430Sstevel@tonic-gate DQ(q, qhead, qtail, q_link);
37440Sstevel@tonic-gate ASSERT(q != NULL);
37450Sstevel@tonic-gate mutex_exit(&service_queue);
37460Sstevel@tonic-gate queue_service(q);
37470Sstevel@tonic-gate mutex_enter(&service_queue);
37480Sstevel@tonic-gate }
37490Sstevel@tonic-gate ASSERT(qhead == NULL && qtail == NULL);
37500Sstevel@tonic-gate }
37510Sstevel@tonic-gate }
37520Sstevel@tonic-gate
37530Sstevel@tonic-gate /*
37540Sstevel@tonic-gate * Background processing of streams background tasks which failed
37550Sstevel@tonic-gate * taskq_dispatch.
37560Sstevel@tonic-gate */
37570Sstevel@tonic-gate static void
streams_sqbkgrnd_service(void)37580Sstevel@tonic-gate streams_sqbkgrnd_service(void)
37590Sstevel@tonic-gate {
37600Sstevel@tonic-gate callb_cpr_t cprinfo;
37610Sstevel@tonic-gate syncq_t *sq;
37620Sstevel@tonic-gate
37630Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, &service_queue, callb_generic_cpr,
37640Sstevel@tonic-gate "streams_sqbkgrnd_service");
37650Sstevel@tonic-gate
37660Sstevel@tonic-gate mutex_enter(&service_queue);
37670Sstevel@tonic-gate
37680Sstevel@tonic-gate for (;;) {
37690Sstevel@tonic-gate /*
37700Sstevel@tonic-gate * Wait for work to arrive.
37710Sstevel@tonic-gate */
37720Sstevel@tonic-gate while (sqhead == NULL) {
37730Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cprinfo);
37740Sstevel@tonic-gate cv_wait(&syncqs_to_run, &service_queue);
37750Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cprinfo, &service_queue);
37760Sstevel@tonic-gate }
37770Sstevel@tonic-gate
37780Sstevel@tonic-gate /*
37790Sstevel@tonic-gate * Run pending syncqs.
37800Sstevel@tonic-gate */
37810Sstevel@tonic-gate while (sqhead != NULL) {
37820Sstevel@tonic-gate DQ(sq, sqhead, sqtail, sq_next);
37830Sstevel@tonic-gate ASSERT(sq != NULL);
37840Sstevel@tonic-gate ASSERT(sq->sq_svcflags & SQ_BGTHREAD);
37850Sstevel@tonic-gate mutex_exit(&service_queue);
37860Sstevel@tonic-gate syncq_service(sq);
37870Sstevel@tonic-gate mutex_enter(&service_queue);
37880Sstevel@tonic-gate }
37890Sstevel@tonic-gate }
37900Sstevel@tonic-gate }
37910Sstevel@tonic-gate
37920Sstevel@tonic-gate /*
37930Sstevel@tonic-gate * Disable the syncq and wait for background syncq processing to complete.
37940Sstevel@tonic-gate * If the syncq is placed on the sqhead/sqtail queue, try to remove it from the
37950Sstevel@tonic-gate * list.
37960Sstevel@tonic-gate */
37970Sstevel@tonic-gate void
wait_sq_svc(syncq_t * sq)37980Sstevel@tonic-gate wait_sq_svc(syncq_t *sq)
37990Sstevel@tonic-gate {
38000Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
38010Sstevel@tonic-gate sq->sq_svcflags |= SQ_DISABLED;
38020Sstevel@tonic-gate if (sq->sq_svcflags & SQ_BGTHREAD) {
38030Sstevel@tonic-gate syncq_t *sq_chase;
38040Sstevel@tonic-gate syncq_t *sq_curr;
38050Sstevel@tonic-gate int removed;
38060Sstevel@tonic-gate
38070Sstevel@tonic-gate ASSERT(sq->sq_servcount == 1);
38080Sstevel@tonic-gate mutex_enter(&service_queue);
38090Sstevel@tonic-gate RMQ(sq, sqhead, sqtail, sq_next, sq_chase, sq_curr, removed);
38100Sstevel@tonic-gate mutex_exit(&service_queue);
38110Sstevel@tonic-gate if (removed) {
38120Sstevel@tonic-gate sq->sq_svcflags &= ~SQ_BGTHREAD;
38130Sstevel@tonic-gate sq->sq_servcount = 0;
38140Sstevel@tonic-gate STRSTAT(sqremoved);
38150Sstevel@tonic-gate goto done;
38160Sstevel@tonic-gate }
38170Sstevel@tonic-gate }
38180Sstevel@tonic-gate while (sq->sq_servcount != 0) {
38190Sstevel@tonic-gate sq->sq_flags |= SQ_WANTWAKEUP;
38200Sstevel@tonic-gate cv_wait(&sq->sq_wait, SQLOCK(sq));
38210Sstevel@tonic-gate }
38220Sstevel@tonic-gate done:
38230Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
38240Sstevel@tonic-gate }
38250Sstevel@tonic-gate
38260Sstevel@tonic-gate /*
38270Sstevel@tonic-gate * Put a syncq on the list of syncq's to be serviced by the sqthread.
38280Sstevel@tonic-gate * Add the argument to the end of the sqhead list and set the flag
38290Sstevel@tonic-gate * indicating this syncq has been enabled. If it has already been
38300Sstevel@tonic-gate * enabled, don't do anything.
38310Sstevel@tonic-gate * This routine assumes that SQLOCK is held.
38320Sstevel@tonic-gate * NOTE that the lock order is to have the SQLOCK first,
38330Sstevel@tonic-gate * so if the service_syncq lock is held, we need to release it
38349671SBrian.Ruthven@Sun.COM * before acquiring the SQLOCK (mostly relevant for the background
38350Sstevel@tonic-gate * thread, and this seems to be common among the STREAMS global locks).
38369671SBrian.Ruthven@Sun.COM * Note that the sq_svcflags are protected by the SQLOCK.
38370Sstevel@tonic-gate */
38380Sstevel@tonic-gate void
sqenable(syncq_t * sq)38390Sstevel@tonic-gate sqenable(syncq_t *sq)
38400Sstevel@tonic-gate {
38410Sstevel@tonic-gate /*
38420Sstevel@tonic-gate * This is probably not important except for where I believe it
38430Sstevel@tonic-gate * is being called. At that point, it should be held (and it
38440Sstevel@tonic-gate * is a pain to release it just for this routine, so don't do
38450Sstevel@tonic-gate * it).
38460Sstevel@tonic-gate */
38470Sstevel@tonic-gate ASSERT(MUTEX_HELD(SQLOCK(sq)));
38480Sstevel@tonic-gate
38490Sstevel@tonic-gate IMPLY(sq->sq_servcount == 0, sq->sq_next == NULL);
38500Sstevel@tonic-gate IMPLY(sq->sq_next != NULL, sq->sq_svcflags & SQ_BGTHREAD);
38510Sstevel@tonic-gate
38520Sstevel@tonic-gate /*
38530Sstevel@tonic-gate * Do not put on list if background thread is scheduled or
38540Sstevel@tonic-gate * syncq is disabled.
38550Sstevel@tonic-gate */
38560Sstevel@tonic-gate if (sq->sq_svcflags & (SQ_DISABLED | SQ_BGTHREAD))
38570Sstevel@tonic-gate return;
38580Sstevel@tonic-gate
38590Sstevel@tonic-gate /*
38600Sstevel@tonic-gate * Check whether we should enable sq at all.
38610Sstevel@tonic-gate * Non PERMOD syncqs may be drained by at most one thread.
38620Sstevel@tonic-gate * PERMOD syncqs may be drained by several threads but we limit the
38630Sstevel@tonic-gate * total amount to the lesser of
38640Sstevel@tonic-gate * Number of queues on the squeue and
38650Sstevel@tonic-gate * Number of CPUs.
38660Sstevel@tonic-gate */
38670Sstevel@tonic-gate if (sq->sq_servcount != 0) {
38680Sstevel@tonic-gate if (((sq->sq_type & SQ_PERMOD) == 0) ||
38690Sstevel@tonic-gate (sq->sq_servcount >= MIN(sq->sq_nqueues, ncpus_online))) {
38700Sstevel@tonic-gate STRSTAT(sqtoomany);
38710Sstevel@tonic-gate return;
38720Sstevel@tonic-gate }
38730Sstevel@tonic-gate }
38740Sstevel@tonic-gate
387511066Srafael.vanoni@sun.com sq->sq_tstamp = ddi_get_lbolt();
38760Sstevel@tonic-gate STRSTAT(sqenables);
38770Sstevel@tonic-gate
38780Sstevel@tonic-gate /* Attempt a taskq dispatch */
38790Sstevel@tonic-gate sq->sq_servid = (void *)taskq_dispatch(streams_taskq,
38800Sstevel@tonic-gate (task_func_t *)syncq_service, sq, TQ_NOSLEEP | TQ_NOQUEUE);
38810Sstevel@tonic-gate if (sq->sq_servid != NULL) {
38820Sstevel@tonic-gate sq->sq_servcount++;
38830Sstevel@tonic-gate return;
38840Sstevel@tonic-gate }
38850Sstevel@tonic-gate
38860Sstevel@tonic-gate /*
38870Sstevel@tonic-gate * This taskq dispatch failed, but a previous one may have succeeded.
38880Sstevel@tonic-gate * Don't try to schedule on the background thread whilst there is
38890Sstevel@tonic-gate * outstanding taskq processing.
38900Sstevel@tonic-gate */
38910Sstevel@tonic-gate if (sq->sq_servcount != 0)
38920Sstevel@tonic-gate return;
38930Sstevel@tonic-gate
38940Sstevel@tonic-gate /*
38950Sstevel@tonic-gate * System is low on resources and can't perform a non-sleeping
38960Sstevel@tonic-gate * dispatch. Schedule the syncq for a background thread and mark the
38970Sstevel@tonic-gate * syncq to avoid any further taskq dispatch attempts.
38980Sstevel@tonic-gate */
38990Sstevel@tonic-gate mutex_enter(&service_queue);
39000Sstevel@tonic-gate STRSTAT(taskqfails);
39010Sstevel@tonic-gate ENQUEUE(sq, sqhead, sqtail, sq_next);
39020Sstevel@tonic-gate sq->sq_svcflags |= SQ_BGTHREAD;
39030Sstevel@tonic-gate sq->sq_servcount = 1;
39040Sstevel@tonic-gate cv_signal(&syncqs_to_run);
39050Sstevel@tonic-gate mutex_exit(&service_queue);
39060Sstevel@tonic-gate }
39070Sstevel@tonic-gate
39080Sstevel@tonic-gate /*
39090Sstevel@tonic-gate * Note: fifo_close() depends on the mblk_t on the queue being freed
39100Sstevel@tonic-gate * asynchronously. The asynchronous freeing of messages breaks the
39110Sstevel@tonic-gate * recursive call chain of fifo_close() while there are I_SENDFD type of
39129671SBrian.Ruthven@Sun.COM * messages referring to other file pointers on the queue. Then when
39130Sstevel@tonic-gate * closing pipes it can avoid stack overflow in case of daisy-chained
39140Sstevel@tonic-gate * pipes, and also avoid deadlock in case of fifonode_t pairs (which
39150Sstevel@tonic-gate * share the same fifolock_t).
3916*11785SDave.Plauger@Sun.COM *
3917*11785SDave.Plauger@Sun.COM * No need to kpreempt_disable to access cpu_seqid. If we migrate and
3918*11785SDave.Plauger@Sun.COM * the esb queue does not match the new CPU, that is OK.
3919*11785SDave.Plauger@Sun.COM */
39200Sstevel@tonic-gate void
freebs_enqueue(mblk_t * mp,dblk_t * dbp)39210Sstevel@tonic-gate freebs_enqueue(mblk_t *mp, dblk_t *dbp)
39220Sstevel@tonic-gate {
3923*11785SDave.Plauger@Sun.COM int qindex = CPU->cpu_seqid >> esbq_log2_cpus_per_q;
3924*11785SDave.Plauger@Sun.COM esb_queue_t *eqp;
39253932Sss146032
39260Sstevel@tonic-gate ASSERT(dbp->db_mblk == mp);
3927*11785SDave.Plauger@Sun.COM ASSERT(qindex < esbq_nelem);
3928*11785SDave.Plauger@Sun.COM
3929*11785SDave.Plauger@Sun.COM eqp = system_esbq_array;
3930*11785SDave.Plauger@Sun.COM if (eqp != NULL) {
3931*11785SDave.Plauger@Sun.COM eqp += qindex;
3932*11785SDave.Plauger@Sun.COM } else {
3933*11785SDave.Plauger@Sun.COM mutex_enter(&esbq_lock);
3934*11785SDave.Plauger@Sun.COM if (kmem_ready && system_esbq_array == NULL)
3935*11785SDave.Plauger@Sun.COM system_esbq_array = (esb_queue_t *)kmem_zalloc(
3936*11785SDave.Plauger@Sun.COM esbq_nelem * sizeof (esb_queue_t), KM_NOSLEEP);
3937*11785SDave.Plauger@Sun.COM mutex_exit(&esbq_lock);
3938*11785SDave.Plauger@Sun.COM eqp = system_esbq_array;
3939*11785SDave.Plauger@Sun.COM if (eqp != NULL)
3940*11785SDave.Plauger@Sun.COM eqp += qindex;
3941*11785SDave.Plauger@Sun.COM else
3942*11785SDave.Plauger@Sun.COM eqp = &system_esbq;
3943*11785SDave.Plauger@Sun.COM }
39440Sstevel@tonic-gate
39450Sstevel@tonic-gate /*
39460Sstevel@tonic-gate * Check data sanity. The dblock should have non-empty free function.
39470Sstevel@tonic-gate * It is better to panic here then later when the dblock is freed
39480Sstevel@tonic-gate * asynchronously when the context is lost.
39490Sstevel@tonic-gate */
39500Sstevel@tonic-gate if (dbp->db_frtnp->free_func == NULL) {
39510Sstevel@tonic-gate panic("freebs_enqueue: dblock %p has a NULL free callback",
39523932Sss146032 (void *)dbp);
39533932Sss146032 }
39543932Sss146032
39553932Sss146032 mutex_enter(&eqp->eq_lock);
39563932Sss146032 /* queue the new mblk on the esballoc queue */
39573932Sss146032 if (eqp->eq_head == NULL) {
39583932Sss146032 eqp->eq_head = eqp->eq_tail = mp;
39593932Sss146032 } else {
39603932Sss146032 eqp->eq_tail->b_next = mp;
39613932Sss146032 eqp->eq_tail = mp;
39623932Sss146032 }
39633932Sss146032 eqp->eq_len++;
39643932Sss146032
39653932Sss146032 /* If we're the first thread to reach the threshold, process */
39663932Sss146032 if (eqp->eq_len >= esbq_max_qlen &&
39673932Sss146032 !(eqp->eq_flags & ESBQ_PROCESSING))
39683932Sss146032 esballoc_process_queue(eqp);
39693932Sss146032
39703932Sss146032 esballoc_set_timer(eqp, esbq_timeout);
39713932Sss146032 mutex_exit(&eqp->eq_lock);
39723932Sss146032 }
39733932Sss146032
39743932Sss146032 static void
esballoc_process_queue(esb_queue_t * eqp)39753932Sss146032 esballoc_process_queue(esb_queue_t *eqp)
39763932Sss146032 {
39773932Sss146032 mblk_t *mp;
39783932Sss146032
39793932Sss146032 ASSERT(MUTEX_HELD(&eqp->eq_lock));
39803932Sss146032
39813932Sss146032 eqp->eq_flags |= ESBQ_PROCESSING;
39823932Sss146032
39833932Sss146032 do {
39843932Sss146032 /*
39853932Sss146032 * Detach the message chain for processing.
39863932Sss146032 */
39873932Sss146032 mp = eqp->eq_head;
39883932Sss146032 eqp->eq_tail->b_next = NULL;
39893932Sss146032 eqp->eq_head = eqp->eq_tail = NULL;
39903932Sss146032 eqp->eq_len = 0;
39913932Sss146032 mutex_exit(&eqp->eq_lock);
39923932Sss146032
39933932Sss146032 /*
39943932Sss146032 * Process the message chain.
39953932Sss146032 */
39963932Sss146032 esballoc_enqueue_mblk(mp);
39973932Sss146032 mutex_enter(&eqp->eq_lock);
39983932Sss146032 } while ((eqp->eq_len >= esbq_max_qlen) && (eqp->eq_len > 0));
39993932Sss146032
40003932Sss146032 eqp->eq_flags &= ~ESBQ_PROCESSING;
40013932Sss146032 }
40023932Sss146032
40033932Sss146032 /*
40043932Sss146032 * taskq callback routine to free esballoced mblk's
40053932Sss146032 */
40063932Sss146032 static void
esballoc_mblk_free(mblk_t * mp)40073932Sss146032 esballoc_mblk_free(mblk_t *mp)
40083932Sss146032 {
40093932Sss146032 mblk_t *nextmp;
40103932Sss146032
40113932Sss146032 for (; mp != NULL; mp = nextmp) {
40123932Sss146032 nextmp = mp->b_next;
40133932Sss146032 mp->b_next = NULL;
40143932Sss146032 mblk_free(mp);
40153932Sss146032 }
40163932Sss146032 }
40173932Sss146032
40183932Sss146032 static void
esballoc_enqueue_mblk(mblk_t * mp)40193932Sss146032 esballoc_enqueue_mblk(mblk_t *mp)
40203932Sss146032 {
40213932Sss146032
40223932Sss146032 if (taskq_dispatch(system_taskq, (task_func_t *)esballoc_mblk_free, mp,
40230Sstevel@tonic-gate TQ_NOSLEEP) == NULL) {
40243932Sss146032 mblk_t *first_mp = mp;
40250Sstevel@tonic-gate /*
40260Sstevel@tonic-gate * System is low on resources and can't perform a non-sleeping
40270Sstevel@tonic-gate * dispatch. Schedule for a background thread.
40280Sstevel@tonic-gate */
40290Sstevel@tonic-gate mutex_enter(&service_queue);
40300Sstevel@tonic-gate STRSTAT(taskqfails);
40313932Sss146032
40323932Sss146032 while (mp->b_next != NULL)
40333932Sss146032 mp = mp->b_next;
40343932Sss146032
40350Sstevel@tonic-gate mp->b_next = freebs_list;
40363932Sss146032 freebs_list = first_mp;
40370Sstevel@tonic-gate cv_signal(&services_to_run);
40380Sstevel@tonic-gate mutex_exit(&service_queue);
40390Sstevel@tonic-gate }
40400Sstevel@tonic-gate }
40410Sstevel@tonic-gate
40423932Sss146032 static void
esballoc_timer(void * arg)40433932Sss146032 esballoc_timer(void *arg)
40443932Sss146032 {
40453932Sss146032 esb_queue_t *eqp = arg;
40463932Sss146032
40473932Sss146032 mutex_enter(&eqp->eq_lock);
40483932Sss146032 eqp->eq_flags &= ~ESBQ_TIMER;
40493932Sss146032
40503932Sss146032 if (!(eqp->eq_flags & ESBQ_PROCESSING) &&
40513932Sss146032 eqp->eq_len > 0)
40523932Sss146032 esballoc_process_queue(eqp);
40533932Sss146032
40543932Sss146032 esballoc_set_timer(eqp, esbq_timeout);
40553932Sss146032 mutex_exit(&eqp->eq_lock);
40563932Sss146032 }
40573932Sss146032
40583932Sss146032 static void
esballoc_set_timer(esb_queue_t * eqp,clock_t eq_timeout)40593932Sss146032 esballoc_set_timer(esb_queue_t *eqp, clock_t eq_timeout)
40603932Sss146032 {
40613932Sss146032 ASSERT(MUTEX_HELD(&eqp->eq_lock));
40623932Sss146032
40633932Sss146032 if (eqp->eq_len > 0 && !(eqp->eq_flags & ESBQ_TIMER)) {
40643932Sss146032 (void) timeout(esballoc_timer, eqp, eq_timeout);
40653932Sss146032 eqp->eq_flags |= ESBQ_TIMER;
40663932Sss146032 }
40673932Sss146032 }
40683932Sss146032
4069*11785SDave.Plauger@Sun.COM /*
4070*11785SDave.Plauger@Sun.COM * Setup esbq array length based upon NCPU scaled by CPUs per
4071*11785SDave.Plauger@Sun.COM * queue. Use static system_esbq until kmem_ready and we can
4072*11785SDave.Plauger@Sun.COM * create an array in freebs_enqueue().
4073*11785SDave.Plauger@Sun.COM */
40743932Sss146032 void
esballoc_queue_init(void)40753932Sss146032 esballoc_queue_init(void)
40763932Sss146032 {
4077*11785SDave.Plauger@Sun.COM esbq_log2_cpus_per_q = highbit(esbq_cpus_per_q - 1);
4078*11785SDave.Plauger@Sun.COM esbq_cpus_per_q = 1 << esbq_log2_cpus_per_q;
4079*11785SDave.Plauger@Sun.COM esbq_nelem = howmany(NCPU, esbq_cpus_per_q);
40803932Sss146032 system_esbq.eq_len = 0;
40813932Sss146032 system_esbq.eq_head = system_esbq.eq_tail = NULL;
40823932Sss146032 system_esbq.eq_flags = 0;
40833932Sss146032 }
40843932Sss146032
40850Sstevel@tonic-gate /*
40860Sstevel@tonic-gate * Set the QBACK or QB_BACK flag in the given queue for
40870Sstevel@tonic-gate * the given priority band.
40880Sstevel@tonic-gate */
40890Sstevel@tonic-gate void
setqback(queue_t * q,unsigned char pri)40900Sstevel@tonic-gate setqback(queue_t *q, unsigned char pri)
40910Sstevel@tonic-gate {
40920Sstevel@tonic-gate int i;
40930Sstevel@tonic-gate qband_t *qbp;
40940Sstevel@tonic-gate qband_t **qbpp;
40950Sstevel@tonic-gate
40960Sstevel@tonic-gate ASSERT(MUTEX_HELD(QLOCK(q)));
40970Sstevel@tonic-gate if (pri != 0) {
40980Sstevel@tonic-gate if (pri > q->q_nband) {
40990Sstevel@tonic-gate qbpp = &q->q_bandp;
41000Sstevel@tonic-gate while (*qbpp)
41010Sstevel@tonic-gate qbpp = &(*qbpp)->qb_next;
41020Sstevel@tonic-gate while (pri > q->q_nband) {
41030Sstevel@tonic-gate if ((*qbpp = allocband()) == NULL) {
41040Sstevel@tonic-gate cmn_err(CE_WARN,
41050Sstevel@tonic-gate "setqback: can't allocate qband\n");
41060Sstevel@tonic-gate return;
41070Sstevel@tonic-gate }
41080Sstevel@tonic-gate (*qbpp)->qb_hiwat = q->q_hiwat;
41090Sstevel@tonic-gate (*qbpp)->qb_lowat = q->q_lowat;
41100Sstevel@tonic-gate q->q_nband++;
41110Sstevel@tonic-gate qbpp = &(*qbpp)->qb_next;
41120Sstevel@tonic-gate }
41130Sstevel@tonic-gate }
41140Sstevel@tonic-gate qbp = q->q_bandp;
41150Sstevel@tonic-gate i = pri;
41160Sstevel@tonic-gate while (--i)
41170Sstevel@tonic-gate qbp = qbp->qb_next;
41180Sstevel@tonic-gate qbp->qb_flag |= QB_BACK;
41190Sstevel@tonic-gate } else {
41200Sstevel@tonic-gate q->q_flag |= QBACK;
41210Sstevel@tonic-gate }
41220Sstevel@tonic-gate }
41230Sstevel@tonic-gate
41240Sstevel@tonic-gate int
strcopyin(void * from,void * to,size_t len,int copyflag)41250Sstevel@tonic-gate strcopyin(void *from, void *to, size_t len, int copyflag)
41260Sstevel@tonic-gate {
41270Sstevel@tonic-gate if (copyflag & U_TO_K) {
41280Sstevel@tonic-gate ASSERT((copyflag & K_TO_K) == 0);
41290Sstevel@tonic-gate if (copyin(from, to, len))
41300Sstevel@tonic-gate return (EFAULT);
41310Sstevel@tonic-gate } else {
41320Sstevel@tonic-gate ASSERT(copyflag & K_TO_K);
41330Sstevel@tonic-gate bcopy(from, to, len);
41340Sstevel@tonic-gate }
41350Sstevel@tonic-gate return (0);
41360Sstevel@tonic-gate }
41370Sstevel@tonic-gate
41380Sstevel@tonic-gate int
strcopyout(void * from,void * to,size_t len,int copyflag)41390Sstevel@tonic-gate strcopyout(void *from, void *to, size_t len, int copyflag)
41400Sstevel@tonic-gate {
41410Sstevel@tonic-gate if (copyflag & U_TO_K) {
41420Sstevel@tonic-gate if (copyout(from, to, len))
41430Sstevel@tonic-gate return (EFAULT);
41440Sstevel@tonic-gate } else {
41450Sstevel@tonic-gate ASSERT(copyflag & K_TO_K);
41460Sstevel@tonic-gate bcopy(from, to, len);
41470Sstevel@tonic-gate }
41480Sstevel@tonic-gate return (0);
41490Sstevel@tonic-gate }
41500Sstevel@tonic-gate
41510Sstevel@tonic-gate /*
41520Sstevel@tonic-gate * strsignal_nolock() posts a signal to the process(es) at the stream head.
41530Sstevel@tonic-gate * It assumes that the stream head lock is already held, whereas strsignal()
41540Sstevel@tonic-gate * acquires the lock first. This routine was created because a few callers
41550Sstevel@tonic-gate * release the stream head lock before calling only to re-acquire it after
41560Sstevel@tonic-gate * it returns.
41570Sstevel@tonic-gate */
41580Sstevel@tonic-gate void
strsignal_nolock(stdata_t * stp,int sig,uchar_t band)41598752SPeter.Memishian@Sun.COM strsignal_nolock(stdata_t *stp, int sig, uchar_t band)
41600Sstevel@tonic-gate {
41610Sstevel@tonic-gate ASSERT(MUTEX_HELD(&stp->sd_lock));
41620Sstevel@tonic-gate switch (sig) {
41630Sstevel@tonic-gate case SIGPOLL:
41640Sstevel@tonic-gate if (stp->sd_sigflags & S_MSG)
41658752SPeter.Memishian@Sun.COM strsendsig(stp->sd_siglist, S_MSG, band, 0);
41660Sstevel@tonic-gate break;
41670Sstevel@tonic-gate default:
41688752SPeter.Memishian@Sun.COM if (stp->sd_pgidp)
41690Sstevel@tonic-gate pgsignal(stp->sd_pgidp, sig);
41700Sstevel@tonic-gate break;
41710Sstevel@tonic-gate }
41720Sstevel@tonic-gate }
41730Sstevel@tonic-gate
41740Sstevel@tonic-gate void
strsignal(stdata_t * stp,int sig,int32_t band)41750Sstevel@tonic-gate strsignal(stdata_t *stp, int sig, int32_t band)
41760Sstevel@tonic-gate {
41770Sstevel@tonic-gate TRACE_3(TR_FAC_STREAMS_FR, TR_SENDSIG,
41785753Sgww "strsignal:%p, %X, %X", stp, sig, band);
41790Sstevel@tonic-gate
41800Sstevel@tonic-gate mutex_enter(&stp->sd_lock);
41810Sstevel@tonic-gate switch (sig) {
41820Sstevel@tonic-gate case SIGPOLL:
41830Sstevel@tonic-gate if (stp->sd_sigflags & S_MSG)
41840Sstevel@tonic-gate strsendsig(stp->sd_siglist, S_MSG, (uchar_t)band, 0);
41850Sstevel@tonic-gate break;
41860Sstevel@tonic-gate
41870Sstevel@tonic-gate default:
41880Sstevel@tonic-gate if (stp->sd_pgidp) {
41890Sstevel@tonic-gate pgsignal(stp->sd_pgidp, sig);
41900Sstevel@tonic-gate }
41910Sstevel@tonic-gate break;
41920Sstevel@tonic-gate }
41930Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
41940Sstevel@tonic-gate }
41950Sstevel@tonic-gate
41960Sstevel@tonic-gate void
strhup(stdata_t * stp)41970Sstevel@tonic-gate strhup(stdata_t *stp)
41980Sstevel@tonic-gate {
41992712Snn35248 ASSERT(mutex_owned(&stp->sd_lock));
42000Sstevel@tonic-gate pollwakeup(&stp->sd_pollist, POLLHUP);
42010Sstevel@tonic-gate if (stp->sd_sigflags & S_HANGUP)
42020Sstevel@tonic-gate strsendsig(stp->sd_siglist, S_HANGUP, 0, 0);
42032712Snn35248 }
42042712Snn35248
4205560Smeem /*
4206560Smeem * Backenable the first queue upstream from `q' with a service procedure.
42070Sstevel@tonic-gate */
42080Sstevel@tonic-gate void
backenable(queue_t * q,uchar_t pri)4209235Smicheng backenable(queue_t *q, uchar_t pri)
42100Sstevel@tonic-gate {
42110Sstevel@tonic-gate queue_t *nq;
42120Sstevel@tonic-gate
42130Sstevel@tonic-gate /*
42149671SBrian.Ruthven@Sun.COM * Our presence might not prevent other modules in our own
42150Sstevel@tonic-gate * stream from popping/pushing since the caller of getq might not
42160Sstevel@tonic-gate * have a claim on the queue (some drivers do a getq on somebody
42170Sstevel@tonic-gate * else's queue - they know that the queue itself is not going away
42189671SBrian.Ruthven@Sun.COM * but the framework has to guarantee q_next in that stream).
42190Sstevel@tonic-gate */
42200Sstevel@tonic-gate claimstr(q);
42210Sstevel@tonic-gate
42229671SBrian.Ruthven@Sun.COM /* Find nearest back queue with service proc */
42230Sstevel@tonic-gate for (nq = backq(q); nq && !nq->q_qinfo->qi_srvp; nq = backq(nq)) {
42240Sstevel@tonic-gate ASSERT(STRMATED(q->q_stream) || STREAM(q) == STREAM(nq));
42250Sstevel@tonic-gate }
42260Sstevel@tonic-gate
42270Sstevel@tonic-gate if (nq) {
42280Sstevel@tonic-gate kthread_t *freezer;
42290Sstevel@tonic-gate /*
42300Sstevel@tonic-gate * backenable can be called either with no locks held
42310Sstevel@tonic-gate * or with the stream frozen (the latter occurs when a module
42329671SBrian.Ruthven@Sun.COM * calls rmvq with the stream frozen). If the stream is frozen
42330Sstevel@tonic-gate * by the caller the caller will hold all qlocks in the stream.
42343448Sdh155122 * Note that a frozen stream doesn't freeze a mated stream,
42353448Sdh155122 * so we explicitly check for that.
42360Sstevel@tonic-gate */
42370Sstevel@tonic-gate freezer = STREAM(q)->sd_freezer;
42383448Sdh155122 if (freezer != curthread || STREAM(q) != STREAM(nq)) {
42390Sstevel@tonic-gate mutex_enter(QLOCK(nq));
42400Sstevel@tonic-gate }
42410Sstevel@tonic-gate #ifdef DEBUG
42420Sstevel@tonic-gate else {
42430Sstevel@tonic-gate ASSERT(frozenstr(q));
42440Sstevel@tonic-gate ASSERT(MUTEX_HELD(QLOCK(q)));
42450Sstevel@tonic-gate ASSERT(MUTEX_HELD(QLOCK(nq)));
42460Sstevel@tonic-gate }
42470Sstevel@tonic-gate #endif
4248235Smicheng setqback(nq, pri);
42490Sstevel@tonic-gate qenable_locked(nq);
42503448Sdh155122 if (freezer != curthread || STREAM(q) != STREAM(nq))
42510Sstevel@tonic-gate mutex_exit(QLOCK(nq));
42520Sstevel@tonic-gate }
42530Sstevel@tonic-gate releasestr(q);
42540Sstevel@tonic-gate }
42550Sstevel@tonic-gate
42560Sstevel@tonic-gate /*
42570Sstevel@tonic-gate * Return the appropriate errno when one of flags_to_check is set
42580Sstevel@tonic-gate * in sd_flags. Uses the exported error routines if they are set.
42590Sstevel@tonic-gate * Will return 0 if non error is set (or if the exported error routines
42600Sstevel@tonic-gate * do not return an error).
42610Sstevel@tonic-gate *
42629671SBrian.Ruthven@Sun.COM * If there is both a read and write error to check, we prefer the read error.
42630Sstevel@tonic-gate * Also, give preference to recorded errno's over the error functions.
42640Sstevel@tonic-gate * The flags that are handled are:
42650Sstevel@tonic-gate * STPLEX return EINVAL
42660Sstevel@tonic-gate * STRDERR return sd_rerror (and clear if STRDERRNONPERSIST)
42670Sstevel@tonic-gate * STWRERR return sd_werror (and clear if STWRERRNONPERSIST)
42680Sstevel@tonic-gate * STRHUP return sd_werror
42690Sstevel@tonic-gate *
42709671SBrian.Ruthven@Sun.COM * If the caller indicates that the operation is a peek, a nonpersistent error
42710Sstevel@tonic-gate * is not cleared.
42720Sstevel@tonic-gate */
42730Sstevel@tonic-gate int
strgeterr(stdata_t * stp,int32_t flags_to_check,int ispeek)42740Sstevel@tonic-gate strgeterr(stdata_t *stp, int32_t flags_to_check, int ispeek)
42750Sstevel@tonic-gate {
42760Sstevel@tonic-gate int32_t sd_flag = stp->sd_flag & flags_to_check;
42770Sstevel@tonic-gate int error = 0;
42780Sstevel@tonic-gate
42790Sstevel@tonic-gate ASSERT(MUTEX_HELD(&stp->sd_lock));
42800Sstevel@tonic-gate ASSERT((flags_to_check & ~(STRDERR|STWRERR|STRHUP|STPLEX)) == 0);
42810Sstevel@tonic-gate if (sd_flag & STPLEX)
42820Sstevel@tonic-gate error = EINVAL;
42830Sstevel@tonic-gate else if (sd_flag & STRDERR) {
42840Sstevel@tonic-gate error = stp->sd_rerror;
42850Sstevel@tonic-gate if ((stp->sd_flag & STRDERRNONPERSIST) && !ispeek) {
42860Sstevel@tonic-gate /*
42870Sstevel@tonic-gate * Read errors are non-persistent i.e. discarded once
42880Sstevel@tonic-gate * returned to a non-peeking caller,
42890Sstevel@tonic-gate */
42900Sstevel@tonic-gate stp->sd_rerror = 0;
42910Sstevel@tonic-gate stp->sd_flag &= ~STRDERR;
42920Sstevel@tonic-gate }
42930Sstevel@tonic-gate if (error == 0 && stp->sd_rderrfunc != NULL) {
42940Sstevel@tonic-gate int clearerr = 0;
42950Sstevel@tonic-gate
42960Sstevel@tonic-gate error = (*stp->sd_rderrfunc)(stp->sd_vnode, ispeek,
42975753Sgww &clearerr);
42980Sstevel@tonic-gate if (clearerr) {
42990Sstevel@tonic-gate stp->sd_flag &= ~STRDERR;
43000Sstevel@tonic-gate stp->sd_rderrfunc = NULL;
43010Sstevel@tonic-gate }
43020Sstevel@tonic-gate }
43030Sstevel@tonic-gate } else if (sd_flag & STWRERR) {
43040Sstevel@tonic-gate error = stp->sd_werror;
43050Sstevel@tonic-gate if ((stp->sd_flag & STWRERRNONPERSIST) && !ispeek) {
43060Sstevel@tonic-gate /*
43070Sstevel@tonic-gate * Write errors are non-persistent i.e. discarded once
43080Sstevel@tonic-gate * returned to a non-peeking caller,
43090Sstevel@tonic-gate */
43100Sstevel@tonic-gate stp->sd_werror = 0;
43110Sstevel@tonic-gate stp->sd_flag &= ~STWRERR;
43120Sstevel@tonic-gate }
43130Sstevel@tonic-gate if (error == 0 && stp->sd_wrerrfunc != NULL) {
43140Sstevel@tonic-gate int clearerr = 0;
43150Sstevel@tonic-gate
43160Sstevel@tonic-gate error = (*stp->sd_wrerrfunc)(stp->sd_vnode, ispeek,
43175753Sgww &clearerr);
43180Sstevel@tonic-gate if (clearerr) {
43190Sstevel@tonic-gate stp->sd_flag &= ~STWRERR;
43200Sstevel@tonic-gate stp->sd_wrerrfunc = NULL;
43210Sstevel@tonic-gate }
43220Sstevel@tonic-gate }
43230Sstevel@tonic-gate } else if (sd_flag & STRHUP) {
43240Sstevel@tonic-gate /* sd_werror set when STRHUP */
43250Sstevel@tonic-gate error = stp->sd_werror;
43260Sstevel@tonic-gate }
43270Sstevel@tonic-gate return (error);
43280Sstevel@tonic-gate }
43290Sstevel@tonic-gate
43300Sstevel@tonic-gate
43310Sstevel@tonic-gate /*
43329671SBrian.Ruthven@Sun.COM * Single-thread open/close/push/pop
43330Sstevel@tonic-gate * for twisted streams also
43340Sstevel@tonic-gate */
43350Sstevel@tonic-gate int
strstartplumb(stdata_t * stp,int flag,int cmd)43360Sstevel@tonic-gate strstartplumb(stdata_t *stp, int flag, int cmd)
43370Sstevel@tonic-gate {
43380Sstevel@tonic-gate int waited = 1;
43390Sstevel@tonic-gate int error = 0;
43400Sstevel@tonic-gate
43410Sstevel@tonic-gate if (STRMATED(stp)) {
43420Sstevel@tonic-gate struct stdata *stmatep = stp->sd_mate;
43430Sstevel@tonic-gate
43440Sstevel@tonic-gate STRLOCKMATES(stp);
43450Sstevel@tonic-gate while (waited) {
43460Sstevel@tonic-gate waited = 0;
43470Sstevel@tonic-gate while (stmatep->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
43480Sstevel@tonic-gate if ((cmd == I_POP) &&
43490Sstevel@tonic-gate (flag & (FNDELAY|FNONBLOCK))) {
43500Sstevel@tonic-gate STRUNLOCKMATES(stp);
43510Sstevel@tonic-gate return (EAGAIN);
43520Sstevel@tonic-gate }
43530Sstevel@tonic-gate waited = 1;
43540Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
43550Sstevel@tonic-gate if (!cv_wait_sig(&stmatep->sd_monitor,
43560Sstevel@tonic-gate &stmatep->sd_lock)) {
43570Sstevel@tonic-gate mutex_exit(&stmatep->sd_lock);
43580Sstevel@tonic-gate return (EINTR);
43590Sstevel@tonic-gate }
43600Sstevel@tonic-gate mutex_exit(&stmatep->sd_lock);
43610Sstevel@tonic-gate STRLOCKMATES(stp);
43620Sstevel@tonic-gate }
43630Sstevel@tonic-gate while (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
43640Sstevel@tonic-gate if ((cmd == I_POP) &&
43655753Sgww (flag & (FNDELAY|FNONBLOCK))) {
43660Sstevel@tonic-gate STRUNLOCKMATES(stp);
43670Sstevel@tonic-gate return (EAGAIN);
43680Sstevel@tonic-gate }
43690Sstevel@tonic-gate waited = 1;
43700Sstevel@tonic-gate mutex_exit(&stmatep->sd_lock);
43710Sstevel@tonic-gate if (!cv_wait_sig(&stp->sd_monitor,
43720Sstevel@tonic-gate &stp->sd_lock)) {
43730Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
43740Sstevel@tonic-gate return (EINTR);
43750Sstevel@tonic-gate }
43760Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
43770Sstevel@tonic-gate STRLOCKMATES(stp);
43780Sstevel@tonic-gate }
43790Sstevel@tonic-gate if (stp->sd_flag & (STRDERR|STWRERR|STRHUP|STPLEX)) {
43800Sstevel@tonic-gate error = strgeterr(stp,
43815753Sgww STRDERR|STWRERR|STRHUP|STPLEX, 0);
43820Sstevel@tonic-gate if (error != 0) {
43830Sstevel@tonic-gate STRUNLOCKMATES(stp);
43840Sstevel@tonic-gate return (error);
43850Sstevel@tonic-gate }
43860Sstevel@tonic-gate }
43870Sstevel@tonic-gate }
43880Sstevel@tonic-gate stp->sd_flag |= STRPLUMB;
43890Sstevel@tonic-gate STRUNLOCKMATES(stp);
43900Sstevel@tonic-gate } else {
43910Sstevel@tonic-gate mutex_enter(&stp->sd_lock);
43920Sstevel@tonic-gate while (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
43930Sstevel@tonic-gate if (((cmd == I_POP) || (cmd == _I_REMOVE)) &&
43940Sstevel@tonic-gate (flag & (FNDELAY|FNONBLOCK))) {
43950Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
43960Sstevel@tonic-gate return (EAGAIN);
43970Sstevel@tonic-gate }
43980Sstevel@tonic-gate if (!cv_wait_sig(&stp->sd_monitor, &stp->sd_lock)) {
43990Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
44000Sstevel@tonic-gate return (EINTR);
44010Sstevel@tonic-gate }
44020Sstevel@tonic-gate if (stp->sd_flag & (STRDERR|STWRERR|STRHUP|STPLEX)) {
44030Sstevel@tonic-gate error = strgeterr(stp,
44045753Sgww STRDERR|STWRERR|STRHUP|STPLEX, 0);
44050Sstevel@tonic-gate if (error != 0) {
44060Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
44070Sstevel@tonic-gate return (error);
44080Sstevel@tonic-gate }
44090Sstevel@tonic-gate }
44100Sstevel@tonic-gate }
44110Sstevel@tonic-gate stp->sd_flag |= STRPLUMB;
44120Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
44130Sstevel@tonic-gate }
44140Sstevel@tonic-gate return (0);
44150Sstevel@tonic-gate }
44160Sstevel@tonic-gate
44170Sstevel@tonic-gate /*
44180Sstevel@tonic-gate * Complete the plumbing operation associated with stream `stp'.
44190Sstevel@tonic-gate */
44200Sstevel@tonic-gate void
strendplumb(stdata_t * stp)44210Sstevel@tonic-gate strendplumb(stdata_t *stp)
44220Sstevel@tonic-gate {
44230Sstevel@tonic-gate ASSERT(MUTEX_HELD(&stp->sd_lock));
44240Sstevel@tonic-gate ASSERT(stp->sd_flag & STRPLUMB);
44250Sstevel@tonic-gate stp->sd_flag &= ~STRPLUMB;
44260Sstevel@tonic-gate cv_broadcast(&stp->sd_monitor);
44270Sstevel@tonic-gate }
44280Sstevel@tonic-gate
44290Sstevel@tonic-gate /*
44300Sstevel@tonic-gate * This describes how the STREAMS framework handles synchronization
44310Sstevel@tonic-gate * during open/push and close/pop.
44320Sstevel@tonic-gate * The key interfaces for open and close are qprocson and qprocsoff,
44330Sstevel@tonic-gate * respectively. While the close case in general is harder both open
44340Sstevel@tonic-gate * have close have significant similarities.
44350Sstevel@tonic-gate *
44360Sstevel@tonic-gate * During close the STREAMS framework has to both ensure that there
44370Sstevel@tonic-gate * are no stale references to the queue pair (and syncq) that
44380Sstevel@tonic-gate * are being closed and also provide the guarantees that are documented
44390Sstevel@tonic-gate * in qprocsoff(9F).
44400Sstevel@tonic-gate * If there are stale references to the queue that is closing it can
44410Sstevel@tonic-gate * result in kernel memory corruption or kernel panics.
44420Sstevel@tonic-gate *
44430Sstevel@tonic-gate * Note that is it up to the module/driver to ensure that it itself
44440Sstevel@tonic-gate * does not have any stale references to the closing queues once its close
44450Sstevel@tonic-gate * routine returns. This includes:
44460Sstevel@tonic-gate * - Cancelling any timeout/bufcall/qtimeout/qbufcall callback routines
44470Sstevel@tonic-gate * associated with the queues. For timeout and bufcall callbacks the
44480Sstevel@tonic-gate * module/driver also has to ensure (or wait for) any callbacks that
44490Sstevel@tonic-gate * are in progress.
44500Sstevel@tonic-gate * - If the module/driver is using esballoc it has to ensure that any
44510Sstevel@tonic-gate * esballoc free functions do not refer to a queue that has closed.
44520Sstevel@tonic-gate * (Note that in general the close routine can not wait for the esballoc'ed
44530Sstevel@tonic-gate * messages to be freed since that can cause a deadlock.)
44540Sstevel@tonic-gate * - Cancelling any interrupts that refer to the closing queues and
44550Sstevel@tonic-gate * also ensuring that there are no interrupts in progress that will
44560Sstevel@tonic-gate * refer to the closing queues once the close routine returns.
44570Sstevel@tonic-gate * - For multiplexors removing any driver global state that refers to
44580Sstevel@tonic-gate * the closing queue and also ensuring that there are no threads in
44590Sstevel@tonic-gate * the multiplexor that has picked up a queue pointer but not yet
44600Sstevel@tonic-gate * finished using it.
44610Sstevel@tonic-gate *
44620Sstevel@tonic-gate * In addition, a driver/module can only reference the q_next pointer
44630Sstevel@tonic-gate * in its open, close, put, or service procedures or in a
44640Sstevel@tonic-gate * qtimeout/qbufcall callback procedure executing "on" the correct
44650Sstevel@tonic-gate * stream. Thus it can not reference the q_next pointer in an interrupt
44660Sstevel@tonic-gate * routine or a timeout, bufcall or esballoc callback routine. Likewise
44670Sstevel@tonic-gate * it can not reference q_next of a different queue e.g. in a mux that
44680Sstevel@tonic-gate * passes messages from one queues put/service procedure to another queue.
44690Sstevel@tonic-gate * In all the cases when the driver/module can not access the q_next
44700Sstevel@tonic-gate * field it must use the *next* versions e.g. canputnext instead of
44710Sstevel@tonic-gate * canput(q->q_next) and putnextctl instead of putctl(q->q_next, ...).
44720Sstevel@tonic-gate *
44730Sstevel@tonic-gate *
44740Sstevel@tonic-gate * Assuming that the driver/module conforms to the above constraints
44750Sstevel@tonic-gate * the STREAMS framework has to avoid stale references to q_next for all
44760Sstevel@tonic-gate * the framework internal cases which include (but are not limited to):
44770Sstevel@tonic-gate * - Threads in canput/canputnext/backenable and elsewhere that are
44780Sstevel@tonic-gate * walking q_next.
44790Sstevel@tonic-gate * - Messages on a syncq that have a reference to the queue through b_queue.
44800Sstevel@tonic-gate * - Messages on an outer perimeter (syncq) that have a reference to the
44810Sstevel@tonic-gate * queue through b_queue.
44820Sstevel@tonic-gate * - Threads that use q_nfsrv (e.g. canput) to find a queue.
44830Sstevel@tonic-gate * Note that only canput and bcanput use q_nfsrv without any locking.
44840Sstevel@tonic-gate *
44850Sstevel@tonic-gate * The STREAMS framework providing the qprocsoff(9F) guarantees means that
44860Sstevel@tonic-gate * after qprocsoff returns, the framework has to ensure that no threads can
44870Sstevel@tonic-gate * enter the put or service routines for the closing read or write-side queue.
44880Sstevel@tonic-gate * In addition to preventing "direct" entry into the put procedures
44890Sstevel@tonic-gate * the framework also has to prevent messages being drained from
44900Sstevel@tonic-gate * the syncq or the outer perimeter.
44910Sstevel@tonic-gate * XXX Note that currently qdetach does relies on D_MTOCEXCL as the only
44920Sstevel@tonic-gate * mechanism to prevent qwriter(PERIM_OUTER) from running after
44930Sstevel@tonic-gate * qprocsoff has returned.
44940Sstevel@tonic-gate * Note that if a module/driver uses put(9F) on one of its own queues
44950Sstevel@tonic-gate * it is up to the module/driver to ensure that the put() doesn't
44960Sstevel@tonic-gate * get called when the queue is closing.
44970Sstevel@tonic-gate *
44980Sstevel@tonic-gate *
44990Sstevel@tonic-gate * The framework aspects of the above "contract" is implemented by
45000Sstevel@tonic-gate * qprocsoff, removeq, and strlock:
45010Sstevel@tonic-gate * - qprocsoff (disable_svc) sets QWCLOSE to prevent runservice from
45020Sstevel@tonic-gate * entering the service procedures.
45030Sstevel@tonic-gate * - strlock acquires the sd_lock and sd_reflock to prevent putnext,
45040Sstevel@tonic-gate * canputnext, backenable etc from dereferencing the q_next that will
45050Sstevel@tonic-gate * soon change.
45060Sstevel@tonic-gate * - strlock waits for sd_refcnt to be zero to wait for e.g. any canputnext
45070Sstevel@tonic-gate * or other q_next walker that uses claimstr/releasestr to finish.
45080Sstevel@tonic-gate * - optionally for every syncq in the stream strlock acquires all the
45090Sstevel@tonic-gate * sq_lock's and waits for all sq_counts to drop to a value that indicates
45100Sstevel@tonic-gate * that no thread executes in the put or service procedures and that no
45110Sstevel@tonic-gate * thread is draining into the module/driver. This ensures that no
45120Sstevel@tonic-gate * open, close, put, service, or qtimeout/qbufcall callback procedure is
45130Sstevel@tonic-gate * currently executing hence no such thread can end up with the old stale
45140Sstevel@tonic-gate * q_next value and no canput/backenable can have the old stale
45150Sstevel@tonic-gate * q_nfsrv/q_next.
45160Sstevel@tonic-gate * - qdetach (wait_svc) makes sure that any scheduled or running threads
45170Sstevel@tonic-gate * have either finished or observed the QWCLOSE flag and gone away.
45180Sstevel@tonic-gate */
45190Sstevel@tonic-gate
45200Sstevel@tonic-gate
45210Sstevel@tonic-gate /*
45220Sstevel@tonic-gate * Get all the locks necessary to change q_next.
45230Sstevel@tonic-gate *
45249671SBrian.Ruthven@Sun.COM * Wait for sd_refcnt to reach 0 and, if sqlist is present, wait for the
45250Sstevel@tonic-gate * sq_count of each syncq in the list to drop to sq_rmqcount, indicating that
45269671SBrian.Ruthven@Sun.COM * the only threads inside the syncq are threads currently calling removeq().
45270Sstevel@tonic-gate * Since threads calling removeq() are in the process of removing their queues
45280Sstevel@tonic-gate * from the stream, we do not need to worry about them accessing a stale q_next
45290Sstevel@tonic-gate * pointer and thus we do not need to wait for them to exit (in fact, waiting
45300Sstevel@tonic-gate * for them can cause deadlock).
45310Sstevel@tonic-gate *
45320Sstevel@tonic-gate * This routine is subject to starvation since it does not set any flag to
45339671SBrian.Ruthven@Sun.COM * prevent threads from entering a module in the stream (i.e. sq_count can
45349671SBrian.Ruthven@Sun.COM * increase on some syncq while it is waiting on some other syncq).
45350Sstevel@tonic-gate *
45360Sstevel@tonic-gate * Assumes that only one thread attempts to call strlock for a given
45370Sstevel@tonic-gate * stream. If this is not the case the two threads would deadlock.
45380Sstevel@tonic-gate * This assumption is guaranteed since strlock is only called by insertq
45390Sstevel@tonic-gate * and removeq and streams plumbing changes are single-threaded for
45400Sstevel@tonic-gate * a given stream using the STWOPEN, STRCLOSE, and STRPLUMB flags.
45410Sstevel@tonic-gate *
45420Sstevel@tonic-gate * For pipes, it is not difficult to atomically designate a pair of streams
45430Sstevel@tonic-gate * to be mated. Once mated atomically by the framework the twisted pair remain
45440Sstevel@tonic-gate * configured that way until dismantled atomically by the framework.
45450Sstevel@tonic-gate * When plumbing takes place on a twisted stream it is necessary to ensure that
45460Sstevel@tonic-gate * this operation is done exclusively on the twisted stream since two such
45470Sstevel@tonic-gate * operations, each initiated on different ends of the pipe will deadlock
45480Sstevel@tonic-gate * waiting for each other to complete.
45490Sstevel@tonic-gate *
45500Sstevel@tonic-gate * On entry, no locks should be held.
45510Sstevel@tonic-gate * The locks acquired and held by strlock depends on a few factors.
45520Sstevel@tonic-gate * - If sqlist is non-NULL all the syncq locks in the sqlist will be acquired
45530Sstevel@tonic-gate * and held on exit and all sq_count are at an acceptable level.
45540Sstevel@tonic-gate * - In all cases, sd_lock and sd_reflock are acquired and held on exit with
45550Sstevel@tonic-gate * sd_refcnt being zero.
45560Sstevel@tonic-gate */
45570Sstevel@tonic-gate
45580Sstevel@tonic-gate static void
strlock(struct stdata * stp,sqlist_t * sqlist)45590Sstevel@tonic-gate strlock(struct stdata *stp, sqlist_t *sqlist)
45600Sstevel@tonic-gate {
45610Sstevel@tonic-gate syncql_t *sql, *sql2;
45620Sstevel@tonic-gate retry:
45630Sstevel@tonic-gate /*
45640Sstevel@tonic-gate * Wait for any claimstr to go away.
45650Sstevel@tonic-gate */
45660Sstevel@tonic-gate if (STRMATED(stp)) {
45670Sstevel@tonic-gate struct stdata *stp1, *stp2;
45680Sstevel@tonic-gate
45690Sstevel@tonic-gate STRLOCKMATES(stp);
45700Sstevel@tonic-gate /*
45710Sstevel@tonic-gate * Note that the selection of locking order is not
45729671SBrian.Ruthven@Sun.COM * important, just that they are always acquired in
45730Sstevel@tonic-gate * the same order. To assure this, we choose this
45740Sstevel@tonic-gate * order based on the value of the pointer, and since
45750Sstevel@tonic-gate * the pointer will not change for the life of this
45760Sstevel@tonic-gate * pair, we will always grab the locks in the same
45770Sstevel@tonic-gate * order (and hence, prevent deadlocks).
45780Sstevel@tonic-gate */
45790Sstevel@tonic-gate if (&(stp->sd_lock) > &((stp->sd_mate)->sd_lock)) {
45800Sstevel@tonic-gate stp1 = stp;
45810Sstevel@tonic-gate stp2 = stp->sd_mate;
45820Sstevel@tonic-gate } else {
45830Sstevel@tonic-gate stp2 = stp;
45840Sstevel@tonic-gate stp1 = stp->sd_mate;
45850Sstevel@tonic-gate }
45860Sstevel@tonic-gate mutex_enter(&stp1->sd_reflock);
45870Sstevel@tonic-gate if (stp1->sd_refcnt > 0) {
45880Sstevel@tonic-gate STRUNLOCKMATES(stp);
4589166Sxy158873 cv_wait(&stp1->sd_refmonitor, &stp1->sd_reflock);
45900Sstevel@tonic-gate mutex_exit(&stp1->sd_reflock);
45910Sstevel@tonic-gate goto retry;
45920Sstevel@tonic-gate }
45930Sstevel@tonic-gate mutex_enter(&stp2->sd_reflock);
45940Sstevel@tonic-gate if (stp2->sd_refcnt > 0) {
45950Sstevel@tonic-gate STRUNLOCKMATES(stp);
45960Sstevel@tonic-gate mutex_exit(&stp1->sd_reflock);
4597166Sxy158873 cv_wait(&stp2->sd_refmonitor, &stp2->sd_reflock);
45980Sstevel@tonic-gate mutex_exit(&stp2->sd_reflock);
45990Sstevel@tonic-gate goto retry;
46000Sstevel@tonic-gate }
46010Sstevel@tonic-gate STREAM_PUTLOCKS_ENTER(stp1);
46020Sstevel@tonic-gate STREAM_PUTLOCKS_ENTER(stp2);
46030Sstevel@tonic-gate } else {
46040Sstevel@tonic-gate mutex_enter(&stp->sd_lock);
46050Sstevel@tonic-gate mutex_enter(&stp->sd_reflock);
46060Sstevel@tonic-gate while (stp->sd_refcnt > 0) {
46070Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
4608166Sxy158873 cv_wait(&stp->sd_refmonitor, &stp->sd_reflock);
46090Sstevel@tonic-gate if (mutex_tryenter(&stp->sd_lock) == 0) {
46100Sstevel@tonic-gate mutex_exit(&stp->sd_reflock);
46110Sstevel@tonic-gate mutex_enter(&stp->sd_lock);
46120Sstevel@tonic-gate mutex_enter(&stp->sd_reflock);
46130Sstevel@tonic-gate }
46140Sstevel@tonic-gate }
46150Sstevel@tonic-gate STREAM_PUTLOCKS_ENTER(stp);
46160Sstevel@tonic-gate }
46170Sstevel@tonic-gate
46180Sstevel@tonic-gate if (sqlist == NULL)
46190Sstevel@tonic-gate return;
46200Sstevel@tonic-gate
46210Sstevel@tonic-gate for (sql = sqlist->sqlist_head; sql; sql = sql->sql_next) {
46220Sstevel@tonic-gate syncq_t *sq = sql->sql_sq;
46230Sstevel@tonic-gate uint16_t count;
46240Sstevel@tonic-gate
46250Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
46260Sstevel@tonic-gate count = sq->sq_count;
46270Sstevel@tonic-gate ASSERT(sq->sq_rmqcount <= count);
46280Sstevel@tonic-gate SQ_PUTLOCKS_ENTER(sq);
46290Sstevel@tonic-gate SUM_SQ_PUTCOUNTS(sq, count);
46300Sstevel@tonic-gate if (count == sq->sq_rmqcount)
46310Sstevel@tonic-gate continue;
46320Sstevel@tonic-gate
46330Sstevel@tonic-gate /* Failed - drop all locks that we have acquired so far */
46340Sstevel@tonic-gate if (STRMATED(stp)) {
46350Sstevel@tonic-gate STREAM_PUTLOCKS_EXIT(stp);
46360Sstevel@tonic-gate STREAM_PUTLOCKS_EXIT(stp->sd_mate);
46370Sstevel@tonic-gate STRUNLOCKMATES(stp);
46380Sstevel@tonic-gate mutex_exit(&stp->sd_reflock);
46390Sstevel@tonic-gate mutex_exit(&stp->sd_mate->sd_reflock);
46400Sstevel@tonic-gate } else {
46410Sstevel@tonic-gate STREAM_PUTLOCKS_EXIT(stp);
46420Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
46430Sstevel@tonic-gate mutex_exit(&stp->sd_reflock);
46440Sstevel@tonic-gate }
46450Sstevel@tonic-gate for (sql2 = sqlist->sqlist_head; sql2 != sql;
46460Sstevel@tonic-gate sql2 = sql2->sql_next) {
46470Sstevel@tonic-gate SQ_PUTLOCKS_EXIT(sql2->sql_sq);
46480Sstevel@tonic-gate mutex_exit(SQLOCK(sql2->sql_sq));
46490Sstevel@tonic-gate }
46500Sstevel@tonic-gate
46510Sstevel@tonic-gate /*
46520Sstevel@tonic-gate * The wait loop below may starve when there are many threads
46530Sstevel@tonic-gate * claiming the syncq. This is especially a problem with permod
46540Sstevel@tonic-gate * syncqs (IP). To lessen the impact of the problem we increment
46550Sstevel@tonic-gate * sq_needexcl and clear fastbits so that putnexts will slow
46560Sstevel@tonic-gate * down and call sqenable instead of draining right away.
46570Sstevel@tonic-gate */
46580Sstevel@tonic-gate sq->sq_needexcl++;
46590Sstevel@tonic-gate SQ_PUTCOUNT_CLRFAST_LOCKED(sq);
46600Sstevel@tonic-gate while (count > sq->sq_rmqcount) {
46610Sstevel@tonic-gate sq->sq_flags |= SQ_WANTWAKEUP;
46620Sstevel@tonic-gate SQ_PUTLOCKS_EXIT(sq);
46630Sstevel@tonic-gate cv_wait(&sq->sq_wait, SQLOCK(sq));
46640Sstevel@tonic-gate count = sq->sq_count;
46650Sstevel@tonic-gate SQ_PUTLOCKS_ENTER(sq);
46660Sstevel@tonic-gate SUM_SQ_PUTCOUNTS(sq, count);
46670Sstevel@tonic-gate }
46680Sstevel@tonic-gate sq->sq_needexcl--;
46690Sstevel@tonic-gate if (sq->sq_needexcl == 0)
46700Sstevel@tonic-gate SQ_PUTCOUNT_SETFAST_LOCKED(sq);
46710Sstevel@tonic-gate SQ_PUTLOCKS_EXIT(sq);
46720Sstevel@tonic-gate ASSERT(count == sq->sq_rmqcount);
46730Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
46740Sstevel@tonic-gate goto retry;
46750Sstevel@tonic-gate }
46760Sstevel@tonic-gate }
46770Sstevel@tonic-gate
46780Sstevel@tonic-gate /*
46790Sstevel@tonic-gate * Drop all the locks that strlock acquired.
46800Sstevel@tonic-gate */
46810Sstevel@tonic-gate static void
strunlock(struct stdata * stp,sqlist_t * sqlist)46820Sstevel@tonic-gate strunlock(struct stdata *stp, sqlist_t *sqlist)
46830Sstevel@tonic-gate {
46840Sstevel@tonic-gate syncql_t *sql;
46850Sstevel@tonic-gate
46860Sstevel@tonic-gate if (STRMATED(stp)) {
46870Sstevel@tonic-gate STREAM_PUTLOCKS_EXIT(stp);
46880Sstevel@tonic-gate STREAM_PUTLOCKS_EXIT(stp->sd_mate);
46890Sstevel@tonic-gate STRUNLOCKMATES(stp);
46900Sstevel@tonic-gate mutex_exit(&stp->sd_reflock);
46910Sstevel@tonic-gate mutex_exit(&stp->sd_mate->sd_reflock);
46920Sstevel@tonic-gate } else {
46930Sstevel@tonic-gate STREAM_PUTLOCKS_EXIT(stp);
46940Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
46950Sstevel@tonic-gate mutex_exit(&stp->sd_reflock);
46960Sstevel@tonic-gate }
46970Sstevel@tonic-gate
46980Sstevel@tonic-gate if (sqlist == NULL)
46990Sstevel@tonic-gate return;
47000Sstevel@tonic-gate
47010Sstevel@tonic-gate for (sql = sqlist->sqlist_head; sql; sql = sql->sql_next) {
47020Sstevel@tonic-gate SQ_PUTLOCKS_EXIT(sql->sql_sq);
47030Sstevel@tonic-gate mutex_exit(SQLOCK(sql->sql_sq));
47040Sstevel@tonic-gate }
47050Sstevel@tonic-gate }
47060Sstevel@tonic-gate
4707235Smicheng /*
4708235Smicheng * When the module has service procedure, we need check if the next
4709235Smicheng * module which has service procedure is in flow control to trigger
4710235Smicheng * the backenable.
4711235Smicheng */
4712235Smicheng static void
backenable_insertedq(queue_t * q)4713235Smicheng backenable_insertedq(queue_t *q)
4714235Smicheng {
4715235Smicheng qband_t *qbp;
4716235Smicheng
4717235Smicheng claimstr(q);
4718235Smicheng if (q->q_qinfo->qi_srvp != NULL && q->q_next != NULL) {
4719235Smicheng if (q->q_next->q_nfsrv->q_flag & QWANTW)
4720235Smicheng backenable(q, 0);
4721235Smicheng
4722235Smicheng qbp = q->q_next->q_nfsrv->q_bandp;
4723235Smicheng for (; qbp != NULL; qbp = qbp->qb_next)
4724235Smicheng if ((qbp->qb_flag & QB_WANTW) && qbp->qb_first != NULL)
4725235Smicheng backenable(q, qbp->qb_first->b_band);
4726235Smicheng }
4727235Smicheng releasestr(q);
4728235Smicheng }
47290Sstevel@tonic-gate
47300Sstevel@tonic-gate /*
47310Sstevel@tonic-gate * Given two read queues, insert a new single one after another.
47320Sstevel@tonic-gate *
47330Sstevel@tonic-gate * This routine acquires all the necessary locks in order to change
47340Sstevel@tonic-gate * q_next and related pointer using strlock().
47350Sstevel@tonic-gate * It depends on the stream head ensuring that there are no concurrent
47360Sstevel@tonic-gate * insertq or removeq on the same stream. The stream head ensures this
47370Sstevel@tonic-gate * using the flags STWOPEN, STRCLOSE, and STRPLUMB.
47380Sstevel@tonic-gate *
47390Sstevel@tonic-gate * Note that no syncq locks are held during the q_next change. This is
47400Sstevel@tonic-gate * applied to all streams since, unlike removeq, there is no problem of stale
47410Sstevel@tonic-gate * pointers when adding a module to the stream. Thus drivers/modules that do a
47420Sstevel@tonic-gate * canput(rq->q_next) would never get a closed/freed queue pointer even if we
47430Sstevel@tonic-gate * applied this optimization to all streams.
47440Sstevel@tonic-gate */
47450Sstevel@tonic-gate void
insertq(struct stdata * stp,queue_t * new)47460Sstevel@tonic-gate insertq(struct stdata *stp, queue_t *new)
47470Sstevel@tonic-gate {
47480Sstevel@tonic-gate queue_t *after;
47490Sstevel@tonic-gate queue_t *wafter;
47500Sstevel@tonic-gate queue_t *wnew = _WR(new);
47510Sstevel@tonic-gate boolean_t have_fifo = B_FALSE;
47520Sstevel@tonic-gate
47530Sstevel@tonic-gate if (new->q_flag & _QINSERTING) {
47540Sstevel@tonic-gate ASSERT(stp->sd_vnode->v_type != VFIFO);
47550Sstevel@tonic-gate after = new->q_next;
47560Sstevel@tonic-gate wafter = _WR(new->q_next);
47570Sstevel@tonic-gate } else {
47580Sstevel@tonic-gate after = _RD(stp->sd_wrq);
47590Sstevel@tonic-gate wafter = stp->sd_wrq;
47600Sstevel@tonic-gate }
47610Sstevel@tonic-gate
47620Sstevel@tonic-gate TRACE_2(TR_FAC_STREAMS_FR, TR_INSERTQ,
47635753Sgww "insertq:%p, %p", after, new);
47640Sstevel@tonic-gate ASSERT(after->q_flag & QREADR);
47650Sstevel@tonic-gate ASSERT(new->q_flag & QREADR);
47660Sstevel@tonic-gate
47670Sstevel@tonic-gate strlock(stp, NULL);
47680Sstevel@tonic-gate
47690Sstevel@tonic-gate /* Do we have a FIFO? */
47700Sstevel@tonic-gate if (wafter->q_next == after) {
47710Sstevel@tonic-gate have_fifo = B_TRUE;
47720Sstevel@tonic-gate wnew->q_next = new;
47730Sstevel@tonic-gate } else {
47740Sstevel@tonic-gate wnew->q_next = wafter->q_next;
47750Sstevel@tonic-gate }
47760Sstevel@tonic-gate new->q_next = after;
47770Sstevel@tonic-gate
47780Sstevel@tonic-gate set_nfsrv_ptr(new, wnew, after, wafter);
47790Sstevel@tonic-gate /*
47800Sstevel@tonic-gate * set_nfsrv_ptr() needs to know if this is an insertion or not,
47810Sstevel@tonic-gate * so only reset this flag after calling it.
47820Sstevel@tonic-gate */
47830Sstevel@tonic-gate new->q_flag &= ~_QINSERTING;
47840Sstevel@tonic-gate
47850Sstevel@tonic-gate if (have_fifo) {
47860Sstevel@tonic-gate wafter->q_next = wnew;
47870Sstevel@tonic-gate } else {
47880Sstevel@tonic-gate if (wafter->q_next)
47890Sstevel@tonic-gate _OTHERQ(wafter->q_next)->q_next = new;
47900Sstevel@tonic-gate wafter->q_next = wnew;
47910Sstevel@tonic-gate }
47920Sstevel@tonic-gate
47930Sstevel@tonic-gate set_qend(new);
47940Sstevel@tonic-gate /* The QEND flag might have to be updated for the upstream guy */
47950Sstevel@tonic-gate set_qend(after);
47960Sstevel@tonic-gate
47970Sstevel@tonic-gate ASSERT(_SAMESTR(new) == O_SAMESTR(new));
47980Sstevel@tonic-gate ASSERT(_SAMESTR(wnew) == O_SAMESTR(wnew));
47990Sstevel@tonic-gate ASSERT(_SAMESTR(after) == O_SAMESTR(after));
48000Sstevel@tonic-gate ASSERT(_SAMESTR(wafter) == O_SAMESTR(wafter));
48010Sstevel@tonic-gate strsetuio(stp);
48020Sstevel@tonic-gate
48030Sstevel@tonic-gate /*
48040Sstevel@tonic-gate * If this was a module insertion, bump the push count.
48050Sstevel@tonic-gate */
48060Sstevel@tonic-gate if (!(new->q_flag & QISDRV))
48070Sstevel@tonic-gate stp->sd_pushcnt++;
48080Sstevel@tonic-gate
48090Sstevel@tonic-gate strunlock(stp, NULL);
4810235Smicheng
4811235Smicheng /* check if the write Q needs backenable */
4812235Smicheng backenable_insertedq(wnew);
4813235Smicheng
4814235Smicheng /* check if the read Q needs backenable */
4815235Smicheng backenable_insertedq(new);
48160Sstevel@tonic-gate }
48170Sstevel@tonic-gate
48180Sstevel@tonic-gate /*
48190Sstevel@tonic-gate * Given a read queue, unlink it from any neighbors.
48200Sstevel@tonic-gate *
48210Sstevel@tonic-gate * This routine acquires all the necessary locks in order to
48220Sstevel@tonic-gate * change q_next and related pointers and also guard against
48230Sstevel@tonic-gate * stale references (e.g. through q_next) to the queue that
48240Sstevel@tonic-gate * is being removed. It also plays part of the role in ensuring
48250Sstevel@tonic-gate * that the module's/driver's put procedure doesn't get called
48260Sstevel@tonic-gate * after qprocsoff returns.
48270Sstevel@tonic-gate *
48280Sstevel@tonic-gate * Removeq depends on the stream head ensuring that there are
48290Sstevel@tonic-gate * no concurrent insertq or removeq on the same stream. The
48300Sstevel@tonic-gate * stream head ensures this using the flags STWOPEN, STRCLOSE and
48310Sstevel@tonic-gate * STRPLUMB.
48320Sstevel@tonic-gate *
48330Sstevel@tonic-gate * The set of locks needed to remove the queue is different in
48340Sstevel@tonic-gate * different cases:
48350Sstevel@tonic-gate *
48360Sstevel@tonic-gate * Acquire sd_lock, sd_reflock, and all the syncq locks in the stream after
48370Sstevel@tonic-gate * waiting for the syncq reference count to drop to 0 indicating that no
48380Sstevel@tonic-gate * non-close threads are present anywhere in the stream. This ensures that any
48390Sstevel@tonic-gate * module/driver can reference q_next in its open, close, put, or service
48400Sstevel@tonic-gate * procedures.
48410Sstevel@tonic-gate *
48420Sstevel@tonic-gate * The sq_rmqcount counter tracks the number of threads inside removeq().
48430Sstevel@tonic-gate * strlock() ensures that there is either no threads executing inside perimeter
48440Sstevel@tonic-gate * or there is only a thread calling qprocsoff().
48450Sstevel@tonic-gate *
48460Sstevel@tonic-gate * strlock() compares the value of sq_count with the number of threads inside
48470Sstevel@tonic-gate * removeq() and waits until sq_count is equal to sq_rmqcount. We need to wakeup
48480Sstevel@tonic-gate * any threads waiting in strlock() when the sq_rmqcount increases.
48490Sstevel@tonic-gate */
48500Sstevel@tonic-gate
48510Sstevel@tonic-gate void
removeq(queue_t * qp)48520Sstevel@tonic-gate removeq(queue_t *qp)
48530Sstevel@tonic-gate {
48540Sstevel@tonic-gate queue_t *wqp = _WR(qp);
48550Sstevel@tonic-gate struct stdata *stp = STREAM(qp);
48560Sstevel@tonic-gate sqlist_t *sqlist = NULL;
48570Sstevel@tonic-gate boolean_t isdriver;
48580Sstevel@tonic-gate int moved;
48590Sstevel@tonic-gate syncq_t *sq = qp->q_syncq;
48600Sstevel@tonic-gate syncq_t *wsq = wqp->q_syncq;
48610Sstevel@tonic-gate
48620Sstevel@tonic-gate ASSERT(stp);
48630Sstevel@tonic-gate
48640Sstevel@tonic-gate TRACE_2(TR_FAC_STREAMS_FR, TR_REMOVEQ,
48655753Sgww "removeq:%p %p", qp, wqp);
48660Sstevel@tonic-gate ASSERT(qp->q_flag&QREADR);
48670Sstevel@tonic-gate
48680Sstevel@tonic-gate /*
48690Sstevel@tonic-gate * For queues using Synchronous streams, we must wait for all threads in
48700Sstevel@tonic-gate * rwnext() to drain out before proceeding.
48710Sstevel@tonic-gate */
48720Sstevel@tonic-gate if (qp->q_flag & QSYNCSTR) {
48730Sstevel@tonic-gate /* First, we need wakeup any threads blocked in rwnext() */
48740Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
48750Sstevel@tonic-gate if (sq->sq_flags & SQ_WANTWAKEUP) {
48760Sstevel@tonic-gate sq->sq_flags &= ~SQ_WANTWAKEUP;
48770Sstevel@tonic-gate cv_broadcast(&sq->sq_wait);
48780Sstevel@tonic-gate }
48790Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
48800Sstevel@tonic-gate
48810Sstevel@tonic-gate if (wsq != sq) {
48820Sstevel@tonic-gate mutex_enter(SQLOCK(wsq));
48830Sstevel@tonic-gate if (wsq->sq_flags & SQ_WANTWAKEUP) {
48840Sstevel@tonic-gate wsq->sq_flags &= ~SQ_WANTWAKEUP;
48850Sstevel@tonic-gate cv_broadcast(&wsq->sq_wait);
48860Sstevel@tonic-gate }
48870Sstevel@tonic-gate mutex_exit(SQLOCK(wsq));
48880Sstevel@tonic-gate }
48890Sstevel@tonic-gate
48900Sstevel@tonic-gate mutex_enter(QLOCK(qp));
48910Sstevel@tonic-gate while (qp->q_rwcnt > 0) {
48920Sstevel@tonic-gate qp->q_flag |= QWANTRMQSYNC;
48930Sstevel@tonic-gate cv_wait(&qp->q_wait, QLOCK(qp));
48940Sstevel@tonic-gate }
48950Sstevel@tonic-gate mutex_exit(QLOCK(qp));
48960Sstevel@tonic-gate
48970Sstevel@tonic-gate mutex_enter(QLOCK(wqp));
48980Sstevel@tonic-gate while (wqp->q_rwcnt > 0) {
48990Sstevel@tonic-gate wqp->q_flag |= QWANTRMQSYNC;
49000Sstevel@tonic-gate cv_wait(&wqp->q_wait, QLOCK(wqp));
49010Sstevel@tonic-gate }
49020Sstevel@tonic-gate mutex_exit(QLOCK(wqp));
49030Sstevel@tonic-gate }
49040Sstevel@tonic-gate
49050Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
49060Sstevel@tonic-gate sq->sq_rmqcount++;
49070Sstevel@tonic-gate if (sq->sq_flags & SQ_WANTWAKEUP) {
49080Sstevel@tonic-gate sq->sq_flags &= ~SQ_WANTWAKEUP;
49090Sstevel@tonic-gate cv_broadcast(&sq->sq_wait);
49100Sstevel@tonic-gate }
49110Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
49120Sstevel@tonic-gate
49130Sstevel@tonic-gate isdriver = (qp->q_flag & QISDRV);
49140Sstevel@tonic-gate
49150Sstevel@tonic-gate sqlist = sqlist_build(qp, stp, STRMATED(stp));
49160Sstevel@tonic-gate strlock(stp, sqlist);
49170Sstevel@tonic-gate
49180Sstevel@tonic-gate reset_nfsrv_ptr(qp, wqp);
49190Sstevel@tonic-gate
49200Sstevel@tonic-gate ASSERT(wqp->q_next == NULL || backq(qp)->q_next == qp);
49210Sstevel@tonic-gate ASSERT(qp->q_next == NULL || backq(wqp)->q_next == wqp);
49220Sstevel@tonic-gate /* Do we have a FIFO? */
49230Sstevel@tonic-gate if (wqp->q_next == qp) {
49240Sstevel@tonic-gate stp->sd_wrq->q_next = _RD(stp->sd_wrq);
49250Sstevel@tonic-gate } else {
49260Sstevel@tonic-gate if (wqp->q_next)
49270Sstevel@tonic-gate backq(qp)->q_next = qp->q_next;
49280Sstevel@tonic-gate if (qp->q_next)
49290Sstevel@tonic-gate backq(wqp)->q_next = wqp->q_next;
49300Sstevel@tonic-gate }
49310Sstevel@tonic-gate
49320Sstevel@tonic-gate /* The QEND flag might have to be updated for the upstream guy */
49330Sstevel@tonic-gate if (qp->q_next)
49340Sstevel@tonic-gate set_qend(qp->q_next);
49350Sstevel@tonic-gate
49360Sstevel@tonic-gate ASSERT(_SAMESTR(stp->sd_wrq) == O_SAMESTR(stp->sd_wrq));
49370Sstevel@tonic-gate ASSERT(_SAMESTR(_RD(stp->sd_wrq)) == O_SAMESTR(_RD(stp->sd_wrq)));
49380Sstevel@tonic-gate
49390Sstevel@tonic-gate /*
49400Sstevel@tonic-gate * Move any messages destined for the put procedures to the next
49410Sstevel@tonic-gate * syncq in line. Otherwise free them.
49420Sstevel@tonic-gate */
49430Sstevel@tonic-gate moved = 0;
49440Sstevel@tonic-gate /*
49450Sstevel@tonic-gate * Quick check to see whether there are any messages or events.
49460Sstevel@tonic-gate */
49470Sstevel@tonic-gate if (qp->q_syncqmsgs != 0 || (qp->q_syncq->sq_flags & SQ_EVENTS))
49480Sstevel@tonic-gate moved += propagate_syncq(qp);
49490Sstevel@tonic-gate if (wqp->q_syncqmsgs != 0 ||
49500Sstevel@tonic-gate (wqp->q_syncq->sq_flags & SQ_EVENTS))
49510Sstevel@tonic-gate moved += propagate_syncq(wqp);
49520Sstevel@tonic-gate
49530Sstevel@tonic-gate strsetuio(stp);
49540Sstevel@tonic-gate
49550Sstevel@tonic-gate /*
49560Sstevel@tonic-gate * If this was a module removal, decrement the push count.
49570Sstevel@tonic-gate */
49580Sstevel@tonic-gate if (!isdriver)
49590Sstevel@tonic-gate stp->sd_pushcnt--;
49600Sstevel@tonic-gate
49610Sstevel@tonic-gate strunlock(stp, sqlist);
49620Sstevel@tonic-gate sqlist_free(sqlist);
49630Sstevel@tonic-gate
49640Sstevel@tonic-gate /*
49650Sstevel@tonic-gate * Make sure any messages that were propagated are drained.
49660Sstevel@tonic-gate * Also clear any QFULL bit caused by messages that were propagated.
49670Sstevel@tonic-gate */
49680Sstevel@tonic-gate
49690Sstevel@tonic-gate if (qp->q_next != NULL) {
49700Sstevel@tonic-gate clr_qfull(qp);
49710Sstevel@tonic-gate /*
49720Sstevel@tonic-gate * For the driver calling qprocsoff, propagate_syncq
49730Sstevel@tonic-gate * frees all the messages instead of putting it in
49740Sstevel@tonic-gate * the stream head
49750Sstevel@tonic-gate */
49760Sstevel@tonic-gate if (!isdriver && (moved > 0))
49770Sstevel@tonic-gate emptysq(qp->q_next->q_syncq);
49780Sstevel@tonic-gate }
49790Sstevel@tonic-gate if (wqp->q_next != NULL) {
49800Sstevel@tonic-gate clr_qfull(wqp);
49810Sstevel@tonic-gate /*
49820Sstevel@tonic-gate * We come here for any pop of a module except for the
49830Sstevel@tonic-gate * case of driver being removed. We don't call emptysq
49840Sstevel@tonic-gate * if we did not move any messages. This will avoid holding
49850Sstevel@tonic-gate * PERMOD syncq locks in emptysq
49860Sstevel@tonic-gate */
49870Sstevel@tonic-gate if (moved > 0)
49880Sstevel@tonic-gate emptysq(wqp->q_next->q_syncq);
49890Sstevel@tonic-gate }
49900Sstevel@tonic-gate
49910Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
49920Sstevel@tonic-gate sq->sq_rmqcount--;
49930Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
49940Sstevel@tonic-gate }
49950Sstevel@tonic-gate
49960Sstevel@tonic-gate /*
49970Sstevel@tonic-gate * Prevent further entry by setting a flag (like SQ_FROZEN, SQ_BLOCKED or
49980Sstevel@tonic-gate * SQ_WRITER) on a syncq.
49990Sstevel@tonic-gate * If maxcnt is not -1 it assumes that caller has "maxcnt" claim(s) on the
50000Sstevel@tonic-gate * sync queue and waits until sq_count reaches maxcnt.
50010Sstevel@tonic-gate *
50029671SBrian.Ruthven@Sun.COM * If maxcnt is -1 there's no need to grab sq_putlocks since the caller
50030Sstevel@tonic-gate * does not care about putnext threads that are in the middle of calling put
50040Sstevel@tonic-gate * entry points.
50050Sstevel@tonic-gate *
50060Sstevel@tonic-gate * This routine is used for both inner and outer syncqs.
50070Sstevel@tonic-gate */
50080Sstevel@tonic-gate static void
blocksq(syncq_t * sq,ushort_t flag,int maxcnt)50090Sstevel@tonic-gate blocksq(syncq_t *sq, ushort_t flag, int maxcnt)
50100Sstevel@tonic-gate {
50110Sstevel@tonic-gate uint16_t count = 0;
50120Sstevel@tonic-gate
50130Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
50140Sstevel@tonic-gate /*
50150Sstevel@tonic-gate * Wait for SQ_FROZEN/SQ_BLOCKED to be reset.
50160Sstevel@tonic-gate * SQ_FROZEN will be set if there is a frozen stream that has a
50170Sstevel@tonic-gate * queue which also refers to this "shared" syncq.
50180Sstevel@tonic-gate * SQ_BLOCKED will be set if there is "off" queue which also
50190Sstevel@tonic-gate * refers to this "shared" syncq.
50200Sstevel@tonic-gate */
50210Sstevel@tonic-gate if (maxcnt != -1) {
50220Sstevel@tonic-gate count = sq->sq_count;
50230Sstevel@tonic-gate SQ_PUTLOCKS_ENTER(sq);
50240Sstevel@tonic-gate SQ_PUTCOUNT_CLRFAST_LOCKED(sq);
50250Sstevel@tonic-gate SUM_SQ_PUTCOUNTS(sq, count);
50260Sstevel@tonic-gate }
50270Sstevel@tonic-gate sq->sq_needexcl++;
50280Sstevel@tonic-gate ASSERT(sq->sq_needexcl != 0); /* wraparound */
50290Sstevel@tonic-gate
50300Sstevel@tonic-gate while ((sq->sq_flags & flag) ||
50310Sstevel@tonic-gate (maxcnt != -1 && count > (unsigned)maxcnt)) {
50320Sstevel@tonic-gate sq->sq_flags |= SQ_WANTWAKEUP;
50330Sstevel@tonic-gate if (maxcnt != -1) {
50340Sstevel@tonic-gate SQ_PUTLOCKS_EXIT(sq);
50350Sstevel@tonic-gate }
50360Sstevel@tonic-gate cv_wait(&sq->sq_wait, SQLOCK(sq));
50370Sstevel@tonic-gate if (maxcnt != -1) {
50380Sstevel@tonic-gate count = sq->sq_count;
50390Sstevel@tonic-gate SQ_PUTLOCKS_ENTER(sq);
50400Sstevel@tonic-gate SUM_SQ_PUTCOUNTS(sq, count);
50410Sstevel@tonic-gate }
50420Sstevel@tonic-gate }
50430Sstevel@tonic-gate sq->sq_needexcl--;
50440Sstevel@tonic-gate sq->sq_flags |= flag;
50450Sstevel@tonic-gate ASSERT(maxcnt == -1 || count == maxcnt);
50460Sstevel@tonic-gate if (maxcnt != -1) {
50470Sstevel@tonic-gate if (sq->sq_needexcl == 0) {
50480Sstevel@tonic-gate SQ_PUTCOUNT_SETFAST_LOCKED(sq);
50490Sstevel@tonic-gate }
50500Sstevel@tonic-gate SQ_PUTLOCKS_EXIT(sq);
50510Sstevel@tonic-gate } else if (sq->sq_needexcl == 0) {
50520Sstevel@tonic-gate SQ_PUTCOUNT_SETFAST(sq);
50530Sstevel@tonic-gate }
50540Sstevel@tonic-gate
50550Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
50560Sstevel@tonic-gate }
50570Sstevel@tonic-gate
50580Sstevel@tonic-gate /*
50590Sstevel@tonic-gate * Reset a flag that was set with blocksq.
50600Sstevel@tonic-gate *
50610Sstevel@tonic-gate * Can not use this routine to reset SQ_WRITER.
50620Sstevel@tonic-gate *
50630Sstevel@tonic-gate * If "isouter" is set then the syncq is assumed to be an outer perimeter
50640Sstevel@tonic-gate * and drain_syncq is not called. Instead we rely on the qwriter_outer thread
50650Sstevel@tonic-gate * to handle the queued qwriter operations.
50660Sstevel@tonic-gate *
50679671SBrian.Ruthven@Sun.COM * No need to grab sq_putlocks here. See comment in strsubr.h that explains when
50680Sstevel@tonic-gate * sq_putlocks are used.
50690Sstevel@tonic-gate */
50700Sstevel@tonic-gate static void
unblocksq(syncq_t * sq,uint16_t resetflag,int isouter)50710Sstevel@tonic-gate unblocksq(syncq_t *sq, uint16_t resetflag, int isouter)
50720Sstevel@tonic-gate {
50730Sstevel@tonic-gate uint16_t flags;
50740Sstevel@tonic-gate
50750Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
50760Sstevel@tonic-gate ASSERT(resetflag != SQ_WRITER);
50770Sstevel@tonic-gate ASSERT(sq->sq_flags & resetflag);
50780Sstevel@tonic-gate flags = sq->sq_flags & ~resetflag;
50790Sstevel@tonic-gate sq->sq_flags = flags;
50800Sstevel@tonic-gate if (flags & (SQ_QUEUED | SQ_WANTWAKEUP)) {
50810Sstevel@tonic-gate if (flags & SQ_WANTWAKEUP) {
50820Sstevel@tonic-gate flags &= ~SQ_WANTWAKEUP;
50830Sstevel@tonic-gate cv_broadcast(&sq->sq_wait);
50840Sstevel@tonic-gate }
50850Sstevel@tonic-gate sq->sq_flags = flags;
50860Sstevel@tonic-gate if ((flags & SQ_QUEUED) && !(flags & (SQ_STAYAWAY|SQ_EXCL))) {
50870Sstevel@tonic-gate if (!isouter) {
50880Sstevel@tonic-gate /* drain_syncq drops SQLOCK */
50890Sstevel@tonic-gate drain_syncq(sq);
50900Sstevel@tonic-gate return;
50910Sstevel@tonic-gate }
50920Sstevel@tonic-gate }
50930Sstevel@tonic-gate }
50940Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
50950Sstevel@tonic-gate }
50960Sstevel@tonic-gate
50970Sstevel@tonic-gate /*
50980Sstevel@tonic-gate * Reset a flag that was set with blocksq.
50990Sstevel@tonic-gate * Does not drain the syncq. Use emptysq() for that.
51000Sstevel@tonic-gate * Returns 1 if SQ_QUEUED is set. Otherwise 0.
51010Sstevel@tonic-gate *
51029671SBrian.Ruthven@Sun.COM * No need to grab sq_putlocks here. See comment in strsubr.h that explains when
51030Sstevel@tonic-gate * sq_putlocks are used.
51040Sstevel@tonic-gate */
51050Sstevel@tonic-gate static int
dropsq(syncq_t * sq,uint16_t resetflag)51060Sstevel@tonic-gate dropsq(syncq_t *sq, uint16_t resetflag)
51070Sstevel@tonic-gate {
51080Sstevel@tonic-gate uint16_t flags;
51090Sstevel@tonic-gate
51100Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
51110Sstevel@tonic-gate ASSERT(sq->sq_flags & resetflag);
51120Sstevel@tonic-gate flags = sq->sq_flags & ~resetflag;
51130Sstevel@tonic-gate if (flags & SQ_WANTWAKEUP) {
51140Sstevel@tonic-gate flags &= ~SQ_WANTWAKEUP;
51150Sstevel@tonic-gate cv_broadcast(&sq->sq_wait);
51160Sstevel@tonic-gate }
51170Sstevel@tonic-gate sq->sq_flags = flags;
51180Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
51190Sstevel@tonic-gate if (flags & SQ_QUEUED)
51200Sstevel@tonic-gate return (1);
51210Sstevel@tonic-gate return (0);
51220Sstevel@tonic-gate }
51230Sstevel@tonic-gate
51240Sstevel@tonic-gate /*
51250Sstevel@tonic-gate * Empty all the messages on a syncq.
51260Sstevel@tonic-gate *
51279671SBrian.Ruthven@Sun.COM * No need to grab sq_putlocks here. See comment in strsubr.h that explains when
51280Sstevel@tonic-gate * sq_putlocks are used.
51290Sstevel@tonic-gate */
51300Sstevel@tonic-gate static void
emptysq(syncq_t * sq)51310Sstevel@tonic-gate emptysq(syncq_t *sq)
51320Sstevel@tonic-gate {
51330Sstevel@tonic-gate uint16_t flags;
51340Sstevel@tonic-gate
51350Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
51360Sstevel@tonic-gate flags = sq->sq_flags;
51370Sstevel@tonic-gate if ((flags & SQ_QUEUED) && !(flags & (SQ_STAYAWAY|SQ_EXCL))) {
51380Sstevel@tonic-gate /*
51390Sstevel@tonic-gate * To prevent potential recursive invocation of drain_syncq we
51400Sstevel@tonic-gate * do not call drain_syncq if count is non-zero.
51410Sstevel@tonic-gate */
51420Sstevel@tonic-gate if (sq->sq_count == 0) {
51430Sstevel@tonic-gate /* drain_syncq() drops SQLOCK */
51440Sstevel@tonic-gate drain_syncq(sq);
51450Sstevel@tonic-gate return;
51460Sstevel@tonic-gate } else
51470Sstevel@tonic-gate sqenable(sq);
51480Sstevel@tonic-gate }
51490Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
51500Sstevel@tonic-gate }
51510Sstevel@tonic-gate
51520Sstevel@tonic-gate /*
51530Sstevel@tonic-gate * Ordered insert while removing duplicates.
51540Sstevel@tonic-gate */
51550Sstevel@tonic-gate static void
sqlist_insert(sqlist_t * sqlist,syncq_t * sqp)51560Sstevel@tonic-gate sqlist_insert(sqlist_t *sqlist, syncq_t *sqp)
51570Sstevel@tonic-gate {
51580Sstevel@tonic-gate syncql_t *sqlp, **prev_sqlpp, *new_sqlp;
51590Sstevel@tonic-gate
51600Sstevel@tonic-gate prev_sqlpp = &sqlist->sqlist_head;
51610Sstevel@tonic-gate while ((sqlp = *prev_sqlpp) != NULL) {
51620Sstevel@tonic-gate if (sqlp->sql_sq >= sqp) {
51630Sstevel@tonic-gate if (sqlp->sql_sq == sqp) /* duplicate */
51640Sstevel@tonic-gate return;
51650Sstevel@tonic-gate break;
51660Sstevel@tonic-gate }
51670Sstevel@tonic-gate prev_sqlpp = &sqlp->sql_next;
51680Sstevel@tonic-gate }
51690Sstevel@tonic-gate new_sqlp = &sqlist->sqlist_array[sqlist->sqlist_index++];
51700Sstevel@tonic-gate ASSERT((char *)new_sqlp < (char *)sqlist + sqlist->sqlist_size);
51710Sstevel@tonic-gate new_sqlp->sql_next = sqlp;
51720Sstevel@tonic-gate new_sqlp->sql_sq = sqp;
51730Sstevel@tonic-gate *prev_sqlpp = new_sqlp;
51740Sstevel@tonic-gate }
51750Sstevel@tonic-gate
51760Sstevel@tonic-gate /*
51770Sstevel@tonic-gate * Walk the write side queues until we hit either the driver
51780Sstevel@tonic-gate * or a twist in the stream (_SAMESTR will return false in both
51790Sstevel@tonic-gate * these cases) then turn around and walk the read side queues
51800Sstevel@tonic-gate * back up to the stream head.
51810Sstevel@tonic-gate */
51820Sstevel@tonic-gate static void
sqlist_insertall(sqlist_t * sqlist,queue_t * q)51830Sstevel@tonic-gate sqlist_insertall(sqlist_t *sqlist, queue_t *q)
51840Sstevel@tonic-gate {
51850Sstevel@tonic-gate while (q != NULL) {
51860Sstevel@tonic-gate sqlist_insert(sqlist, q->q_syncq);
51870Sstevel@tonic-gate
51880Sstevel@tonic-gate if (_SAMESTR(q))
51890Sstevel@tonic-gate q = q->q_next;
51900Sstevel@tonic-gate else if (!(q->q_flag & QREADR))
51910Sstevel@tonic-gate q = _RD(q);
51920Sstevel@tonic-gate else
51930Sstevel@tonic-gate q = NULL;
51940Sstevel@tonic-gate }
51950Sstevel@tonic-gate }
51960Sstevel@tonic-gate
51970Sstevel@tonic-gate /*
51980Sstevel@tonic-gate * Allocate and build a list of all syncqs in a stream and the syncq(s)
51990Sstevel@tonic-gate * associated with the "q" parameter. The resulting list is sorted in a
52000Sstevel@tonic-gate * canonical order and is free of duplicates.
52010Sstevel@tonic-gate * Assumes the passed queue is a _RD(q).
52020Sstevel@tonic-gate */
52030Sstevel@tonic-gate static sqlist_t *
sqlist_build(queue_t * q,struct stdata * stp,boolean_t do_twist)52040Sstevel@tonic-gate sqlist_build(queue_t *q, struct stdata *stp, boolean_t do_twist)
52050Sstevel@tonic-gate {
52060Sstevel@tonic-gate sqlist_t *sqlist = sqlist_alloc(stp, KM_SLEEP);
52070Sstevel@tonic-gate
52080Sstevel@tonic-gate /*
52090Sstevel@tonic-gate * start with the current queue/qpair
52100Sstevel@tonic-gate */
52110Sstevel@tonic-gate ASSERT(q->q_flag & QREADR);
52120Sstevel@tonic-gate
52130Sstevel@tonic-gate sqlist_insert(sqlist, q->q_syncq);
52140Sstevel@tonic-gate sqlist_insert(sqlist, _WR(q)->q_syncq);
52150Sstevel@tonic-gate
52160Sstevel@tonic-gate sqlist_insertall(sqlist, stp->sd_wrq);
52170Sstevel@tonic-gate if (do_twist)
52180Sstevel@tonic-gate sqlist_insertall(sqlist, stp->sd_mate->sd_wrq);
52190Sstevel@tonic-gate
52200Sstevel@tonic-gate return (sqlist);
52210Sstevel@tonic-gate }
52220Sstevel@tonic-gate
52230Sstevel@tonic-gate static sqlist_t *
sqlist_alloc(struct stdata * stp,int kmflag)52240Sstevel@tonic-gate sqlist_alloc(struct stdata *stp, int kmflag)
52250Sstevel@tonic-gate {
52260Sstevel@tonic-gate size_t sqlist_size;
52270Sstevel@tonic-gate sqlist_t *sqlist;
52280Sstevel@tonic-gate
52290Sstevel@tonic-gate /*
52300Sstevel@tonic-gate * Allocate 2 syncql_t's for each pushed module. Note that
52310Sstevel@tonic-gate * the sqlist_t structure already has 4 syncql_t's built in:
52320Sstevel@tonic-gate * 2 for the stream head, and 2 for the driver/other stream head.
52330Sstevel@tonic-gate */
52340Sstevel@tonic-gate sqlist_size = 2 * sizeof (syncql_t) * stp->sd_pushcnt +
52355753Sgww sizeof (sqlist_t);
52360Sstevel@tonic-gate if (STRMATED(stp))
52370Sstevel@tonic-gate sqlist_size += 2 * sizeof (syncql_t) * stp->sd_mate->sd_pushcnt;
52380Sstevel@tonic-gate sqlist = kmem_alloc(sqlist_size, kmflag);
52390Sstevel@tonic-gate
52400Sstevel@tonic-gate sqlist->sqlist_head = NULL;
52410Sstevel@tonic-gate sqlist->sqlist_size = sqlist_size;
52420Sstevel@tonic-gate sqlist->sqlist_index = 0;
52430Sstevel@tonic-gate
52440Sstevel@tonic-gate return (sqlist);
52450Sstevel@tonic-gate }
52460Sstevel@tonic-gate
52470Sstevel@tonic-gate /*
52480Sstevel@tonic-gate * Free the list created by sqlist_alloc()
52490Sstevel@tonic-gate */
52500Sstevel@tonic-gate static void
sqlist_free(sqlist_t * sqlist)52510Sstevel@tonic-gate sqlist_free(sqlist_t *sqlist)
52520Sstevel@tonic-gate {
52530Sstevel@tonic-gate kmem_free(sqlist, sqlist->sqlist_size);
52540Sstevel@tonic-gate }
52550Sstevel@tonic-gate
52560Sstevel@tonic-gate /*
52570Sstevel@tonic-gate * Prevent any new entries into any syncq in this stream.
52580Sstevel@tonic-gate * Used by freezestr.
52590Sstevel@tonic-gate */
52600Sstevel@tonic-gate void
strblock(queue_t * q)52610Sstevel@tonic-gate strblock(queue_t *q)
52620Sstevel@tonic-gate {
52630Sstevel@tonic-gate struct stdata *stp;
52640Sstevel@tonic-gate syncql_t *sql;
52650Sstevel@tonic-gate sqlist_t *sqlist;
52660Sstevel@tonic-gate
52670Sstevel@tonic-gate q = _RD(q);
52680Sstevel@tonic-gate
52690Sstevel@tonic-gate stp = STREAM(q);
52700Sstevel@tonic-gate ASSERT(stp != NULL);
52710Sstevel@tonic-gate
52720Sstevel@tonic-gate /*
52730Sstevel@tonic-gate * Get a sorted list with all the duplicates removed containing
52740Sstevel@tonic-gate * all the syncqs referenced by this stream.
52750Sstevel@tonic-gate */
52760Sstevel@tonic-gate sqlist = sqlist_build(q, stp, B_FALSE);
52770Sstevel@tonic-gate for (sql = sqlist->sqlist_head; sql != NULL; sql = sql->sql_next)
52780Sstevel@tonic-gate blocksq(sql->sql_sq, SQ_FROZEN, -1);
52790Sstevel@tonic-gate sqlist_free(sqlist);
52800Sstevel@tonic-gate }
52810Sstevel@tonic-gate
52820Sstevel@tonic-gate /*
52830Sstevel@tonic-gate * Release the block on new entries into this stream
52840Sstevel@tonic-gate */
52850Sstevel@tonic-gate void
strunblock(queue_t * q)52860Sstevel@tonic-gate strunblock(queue_t *q)
52870Sstevel@tonic-gate {
52880Sstevel@tonic-gate struct stdata *stp;
52890Sstevel@tonic-gate syncql_t *sql;
52900Sstevel@tonic-gate sqlist_t *sqlist;
52910Sstevel@tonic-gate int drain_needed;
52920Sstevel@tonic-gate
52930Sstevel@tonic-gate q = _RD(q);
52940Sstevel@tonic-gate
52950Sstevel@tonic-gate /*
52960Sstevel@tonic-gate * Get a sorted list with all the duplicates removed containing
52970Sstevel@tonic-gate * all the syncqs referenced by this stream.
52980Sstevel@tonic-gate * Have to drop the SQ_FROZEN flag on all the syncqs before
52990Sstevel@tonic-gate * starting to drain them; otherwise the draining might
53000Sstevel@tonic-gate * cause a freezestr in some module on the stream (which
53019671SBrian.Ruthven@Sun.COM * would deadlock).
53020Sstevel@tonic-gate */
53030Sstevel@tonic-gate stp = STREAM(q);
53040Sstevel@tonic-gate ASSERT(stp != NULL);
53050Sstevel@tonic-gate sqlist = sqlist_build(q, stp, B_FALSE);
53060Sstevel@tonic-gate drain_needed = 0;
53070Sstevel@tonic-gate for (sql = sqlist->sqlist_head; sql != NULL; sql = sql->sql_next)
53080Sstevel@tonic-gate drain_needed += dropsq(sql->sql_sq, SQ_FROZEN);
53090Sstevel@tonic-gate if (drain_needed) {
53100Sstevel@tonic-gate for (sql = sqlist->sqlist_head; sql != NULL;
53110Sstevel@tonic-gate sql = sql->sql_next)
53120Sstevel@tonic-gate emptysq(sql->sql_sq);
53130Sstevel@tonic-gate }
53140Sstevel@tonic-gate sqlist_free(sqlist);
53150Sstevel@tonic-gate }
53160Sstevel@tonic-gate
53170Sstevel@tonic-gate #ifdef DEBUG
53180Sstevel@tonic-gate static int
qprocsareon(queue_t * rq)53190Sstevel@tonic-gate qprocsareon(queue_t *rq)
53200Sstevel@tonic-gate {
53210Sstevel@tonic-gate if (rq->q_next == NULL)
53220Sstevel@tonic-gate return (0);
53230Sstevel@tonic-gate return (_WR(rq->q_next)->q_next == _WR(rq));
53240Sstevel@tonic-gate }
53250Sstevel@tonic-gate
53260Sstevel@tonic-gate int
qclaimed(queue_t * q)53270Sstevel@tonic-gate qclaimed(queue_t *q)
53280Sstevel@tonic-gate {
53290Sstevel@tonic-gate uint_t count;
53300Sstevel@tonic-gate
53310Sstevel@tonic-gate count = q->q_syncq->sq_count;
53320Sstevel@tonic-gate SUM_SQ_PUTCOUNTS(q->q_syncq, count);
53330Sstevel@tonic-gate return (count != 0);
53340Sstevel@tonic-gate }
53350Sstevel@tonic-gate
53360Sstevel@tonic-gate /*
53370Sstevel@tonic-gate * Check if anyone has frozen this stream with freezestr
53380Sstevel@tonic-gate */
53390Sstevel@tonic-gate int
frozenstr(queue_t * q)53400Sstevel@tonic-gate frozenstr(queue_t *q)
53410Sstevel@tonic-gate {
53420Sstevel@tonic-gate return ((q->q_syncq->sq_flags & SQ_FROZEN) != 0);
53430Sstevel@tonic-gate }
53440Sstevel@tonic-gate #endif /* DEBUG */
53450Sstevel@tonic-gate
53460Sstevel@tonic-gate /*
53470Sstevel@tonic-gate * Enter a queue.
53480Sstevel@tonic-gate * Obsoleted interface. Should not be used.
53490Sstevel@tonic-gate */
53500Sstevel@tonic-gate void
enterq(queue_t * q)53510Sstevel@tonic-gate enterq(queue_t *q)
53520Sstevel@tonic-gate {
53530Sstevel@tonic-gate entersq(q->q_syncq, SQ_CALLBACK);
53540Sstevel@tonic-gate }
53550Sstevel@tonic-gate
53560Sstevel@tonic-gate void
leaveq(queue_t * q)53570Sstevel@tonic-gate leaveq(queue_t *q)
53580Sstevel@tonic-gate {
53590Sstevel@tonic-gate leavesq(q->q_syncq, SQ_CALLBACK);
53600Sstevel@tonic-gate }
53610Sstevel@tonic-gate
53620Sstevel@tonic-gate /*
53630Sstevel@tonic-gate * Enter a perimeter. c_inner and c_outer specifies which concurrency bits
53640Sstevel@tonic-gate * to check.
53650Sstevel@tonic-gate * Wait if SQ_QUEUED is set to preserve ordering between messages and qwriter
53660Sstevel@tonic-gate * calls and the running of open, close and service procedures.
53670Sstevel@tonic-gate *
53689671SBrian.Ruthven@Sun.COM * If c_inner bit is set no need to grab sq_putlocks since we don't care
53690Sstevel@tonic-gate * if other threads have entered or are entering put entry point.
53700Sstevel@tonic-gate *
53719671SBrian.Ruthven@Sun.COM * If c_inner bit is set it might have been possible to use
53720Sstevel@tonic-gate * sq_putlocks/sq_putcounts instead of SQLOCK/sq_count (e.g. to optimize
53730Sstevel@tonic-gate * open/close path for IP) but since the count may need to be decremented in
53740Sstevel@tonic-gate * qwait() we wouldn't know which counter to decrement. Currently counter is
53750Sstevel@tonic-gate * selected by current cpu_seqid and current CPU can change at any moment. XXX
53760Sstevel@tonic-gate * in the future we might use curthread id bits to select the counter and this
53770Sstevel@tonic-gate * would stay constant across routine calls.
53780Sstevel@tonic-gate */
53790Sstevel@tonic-gate void
entersq(syncq_t * sq,int entrypoint)53800Sstevel@tonic-gate entersq(syncq_t *sq, int entrypoint)
53810Sstevel@tonic-gate {
53820Sstevel@tonic-gate uint16_t count = 0;
53830Sstevel@tonic-gate uint16_t flags;
53840Sstevel@tonic-gate uint16_t waitflags = SQ_STAYAWAY | SQ_EVENTS | SQ_EXCL;
53850Sstevel@tonic-gate uint16_t type;
53860Sstevel@tonic-gate uint_t c_inner = entrypoint & SQ_CI;
53870Sstevel@tonic-gate uint_t c_outer = entrypoint & SQ_CO;
53880Sstevel@tonic-gate
53890Sstevel@tonic-gate /*
53900Sstevel@tonic-gate * Increment ref count to keep closes out of this queue.
53910Sstevel@tonic-gate */
53920Sstevel@tonic-gate ASSERT(sq);
53930Sstevel@tonic-gate ASSERT(c_inner && c_outer);
53940Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
53950Sstevel@tonic-gate flags = sq->sq_flags;
53960Sstevel@tonic-gate type = sq->sq_type;
53970Sstevel@tonic-gate if (!(type & c_inner)) {
53980Sstevel@tonic-gate /* Make sure all putcounts now use slowlock. */
53990Sstevel@tonic-gate count = sq->sq_count;
54000Sstevel@tonic-gate SQ_PUTLOCKS_ENTER(sq);
54010Sstevel@tonic-gate SQ_PUTCOUNT_CLRFAST_LOCKED(sq);
54020Sstevel@tonic-gate SUM_SQ_PUTCOUNTS(sq, count);
54030Sstevel@tonic-gate sq->sq_needexcl++;
54040Sstevel@tonic-gate ASSERT(sq->sq_needexcl != 0); /* wraparound */
54050Sstevel@tonic-gate waitflags |= SQ_MESSAGES;
54060Sstevel@tonic-gate }
54070Sstevel@tonic-gate /*
54080Sstevel@tonic-gate * Wait until we can enter the inner perimeter.
54090Sstevel@tonic-gate * If we want exclusive access we wait until sq_count is 0.
54100Sstevel@tonic-gate * We have to do this before entering the outer perimeter in order
54110Sstevel@tonic-gate * to preserve put/close message ordering.
54120Sstevel@tonic-gate */
54130Sstevel@tonic-gate while ((flags & waitflags) || (!(type & c_inner) && count != 0)) {
54140Sstevel@tonic-gate sq->sq_flags = flags | SQ_WANTWAKEUP;
54150Sstevel@tonic-gate if (!(type & c_inner)) {
54160Sstevel@tonic-gate SQ_PUTLOCKS_EXIT(sq);
54170Sstevel@tonic-gate }
54180Sstevel@tonic-gate cv_wait(&sq->sq_wait, SQLOCK(sq));
54190Sstevel@tonic-gate if (!(type & c_inner)) {
54200Sstevel@tonic-gate count = sq->sq_count;
54210Sstevel@tonic-gate SQ_PUTLOCKS_ENTER(sq);
54220Sstevel@tonic-gate SUM_SQ_PUTCOUNTS(sq, count);
54230Sstevel@tonic-gate }
54240Sstevel@tonic-gate flags = sq->sq_flags;
54250Sstevel@tonic-gate }
54260Sstevel@tonic-gate
54270Sstevel@tonic-gate if (!(type & c_inner)) {
54280Sstevel@tonic-gate ASSERT(sq->sq_needexcl > 0);
54290Sstevel@tonic-gate sq->sq_needexcl--;
54300Sstevel@tonic-gate if (sq->sq_needexcl == 0) {
54310Sstevel@tonic-gate SQ_PUTCOUNT_SETFAST_LOCKED(sq);
54320Sstevel@tonic-gate }
54330Sstevel@tonic-gate }
54340Sstevel@tonic-gate
54350Sstevel@tonic-gate /* Check if we need to enter the outer perimeter */
54360Sstevel@tonic-gate if (!(type & c_outer)) {
54370Sstevel@tonic-gate /*
54380Sstevel@tonic-gate * We have to enter the outer perimeter exclusively before
54390Sstevel@tonic-gate * we can increment sq_count to avoid deadlock. This implies
54400Sstevel@tonic-gate * that we have to re-check sq_flags and sq_count.
54410Sstevel@tonic-gate *
54420Sstevel@tonic-gate * is it possible to have c_inner set when c_outer is not set?
54430Sstevel@tonic-gate */
54440Sstevel@tonic-gate if (!(type & c_inner)) {
54450Sstevel@tonic-gate SQ_PUTLOCKS_EXIT(sq);
54460Sstevel@tonic-gate }
54470Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
54480Sstevel@tonic-gate outer_enter(sq->sq_outer, SQ_GOAWAY);
54490Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
54500Sstevel@tonic-gate flags = sq->sq_flags;
54510Sstevel@tonic-gate /*
54520Sstevel@tonic-gate * there should be no need to recheck sq_putcounts
54530Sstevel@tonic-gate * because outer_enter() has already waited for them to clear
54540Sstevel@tonic-gate * after setting SQ_WRITER.
54550Sstevel@tonic-gate */
54560Sstevel@tonic-gate count = sq->sq_count;
54570Sstevel@tonic-gate #ifdef DEBUG
54580Sstevel@tonic-gate /*
54590Sstevel@tonic-gate * SUMCHECK_SQ_PUTCOUNTS should return the sum instead
54600Sstevel@tonic-gate * of doing an ASSERT internally. Others should do
54610Sstevel@tonic-gate * something like
54620Sstevel@tonic-gate * ASSERT(SUMCHECK_SQ_PUTCOUNTS(sq) == 0);
54630Sstevel@tonic-gate * without the need to #ifdef DEBUG it.
54640Sstevel@tonic-gate */
54650Sstevel@tonic-gate SUMCHECK_SQ_PUTCOUNTS(sq, 0);
54660Sstevel@tonic-gate #endif
54670Sstevel@tonic-gate while ((flags & (SQ_EXCL|SQ_BLOCKED|SQ_FROZEN)) ||
54680Sstevel@tonic-gate (!(type & c_inner) && count != 0)) {
54690Sstevel@tonic-gate sq->sq_flags = flags | SQ_WANTWAKEUP;
54700Sstevel@tonic-gate cv_wait(&sq->sq_wait, SQLOCK(sq));
54710Sstevel@tonic-gate count = sq->sq_count;
54720Sstevel@tonic-gate flags = sq->sq_flags;
54730Sstevel@tonic-gate }
54740Sstevel@tonic-gate }
54750Sstevel@tonic-gate
54760Sstevel@tonic-gate sq->sq_count++;
54770Sstevel@tonic-gate ASSERT(sq->sq_count != 0); /* Wraparound */
54780Sstevel@tonic-gate if (!(type & c_inner)) {
54790Sstevel@tonic-gate /* Exclusive entry */
54800Sstevel@tonic-gate ASSERT(sq->sq_count == 1);
54810Sstevel@tonic-gate sq->sq_flags |= SQ_EXCL;
54820Sstevel@tonic-gate if (type & c_outer) {
54830Sstevel@tonic-gate SQ_PUTLOCKS_EXIT(sq);
54840Sstevel@tonic-gate }
54850Sstevel@tonic-gate }
54860Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
54870Sstevel@tonic-gate }
54880Sstevel@tonic-gate
54890Sstevel@tonic-gate /*
54909671SBrian.Ruthven@Sun.COM * Leave a syncq. Announce to framework that closes may proceed.
54919671SBrian.Ruthven@Sun.COM * c_inner and c_outer specify which concurrency bits to check.
54920Sstevel@tonic-gate *
54939671SBrian.Ruthven@Sun.COM * Must never be called from driver or module put entry point.
54940Sstevel@tonic-gate *
54959671SBrian.Ruthven@Sun.COM * No need to grab sq_putlocks here. See comment in strsubr.h that explains when
54960Sstevel@tonic-gate * sq_putlocks are used.
54970Sstevel@tonic-gate */
54980Sstevel@tonic-gate void
leavesq(syncq_t * sq,int entrypoint)54990Sstevel@tonic-gate leavesq(syncq_t *sq, int entrypoint)
55000Sstevel@tonic-gate {
55010Sstevel@tonic-gate uint16_t flags;
55020Sstevel@tonic-gate uint16_t type;
55030Sstevel@tonic-gate uint_t c_outer = entrypoint & SQ_CO;
55040Sstevel@tonic-gate #ifdef DEBUG
55050Sstevel@tonic-gate uint_t c_inner = entrypoint & SQ_CI;
55060Sstevel@tonic-gate #endif
55070Sstevel@tonic-gate
55080Sstevel@tonic-gate /*
55099671SBrian.Ruthven@Sun.COM * Decrement ref count, drain the syncq if possible, and wake up
55100Sstevel@tonic-gate * any waiting close.
55110Sstevel@tonic-gate */
55120Sstevel@tonic-gate ASSERT(sq);
55130Sstevel@tonic-gate ASSERT(c_inner && c_outer);
55140Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
55150Sstevel@tonic-gate flags = sq->sq_flags;
55160Sstevel@tonic-gate type = sq->sq_type;
55170Sstevel@tonic-gate if (flags & (SQ_QUEUED|SQ_WANTWAKEUP|SQ_WANTEXWAKEUP)) {
55180Sstevel@tonic-gate
55190Sstevel@tonic-gate if (flags & SQ_WANTWAKEUP) {
55200Sstevel@tonic-gate flags &= ~SQ_WANTWAKEUP;
55210Sstevel@tonic-gate cv_broadcast(&sq->sq_wait);
55220Sstevel@tonic-gate }
55230Sstevel@tonic-gate if (flags & SQ_WANTEXWAKEUP) {
55240Sstevel@tonic-gate flags &= ~SQ_WANTEXWAKEUP;
55250Sstevel@tonic-gate cv_broadcast(&sq->sq_exitwait);
55260Sstevel@tonic-gate }
55270Sstevel@tonic-gate
55280Sstevel@tonic-gate if ((flags & SQ_QUEUED) && !(flags & SQ_STAYAWAY)) {
55290Sstevel@tonic-gate /*
55300Sstevel@tonic-gate * The syncq needs to be drained. "Exit" the syncq
55310Sstevel@tonic-gate * before calling drain_syncq.
55320Sstevel@tonic-gate */
55330Sstevel@tonic-gate ASSERT(sq->sq_count != 0);
55340Sstevel@tonic-gate sq->sq_count--;
55350Sstevel@tonic-gate ASSERT((flags & SQ_EXCL) || (type & c_inner));
55360Sstevel@tonic-gate sq->sq_flags = flags & ~SQ_EXCL;
55370Sstevel@tonic-gate drain_syncq(sq);
55380Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(SQLOCK(sq)));
55390Sstevel@tonic-gate /* Check if we need to exit the outer perimeter */
55400Sstevel@tonic-gate /* XXX will this ever be true? */
55410Sstevel@tonic-gate if (!(type & c_outer))
55420Sstevel@tonic-gate outer_exit(sq->sq_outer);
55430Sstevel@tonic-gate return;
55440Sstevel@tonic-gate }
55450Sstevel@tonic-gate }
55460Sstevel@tonic-gate ASSERT(sq->sq_count != 0);
55470Sstevel@tonic-gate sq->sq_count--;
55480Sstevel@tonic-gate ASSERT((flags & SQ_EXCL) || (type & c_inner));
55490Sstevel@tonic-gate sq->sq_flags = flags & ~SQ_EXCL;
55500Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
55510Sstevel@tonic-gate
55520Sstevel@tonic-gate /* Check if we need to exit the outer perimeter */
55530Sstevel@tonic-gate if (!(sq->sq_type & c_outer))
55540Sstevel@tonic-gate outer_exit(sq->sq_outer);
55550Sstevel@tonic-gate }
55560Sstevel@tonic-gate
55570Sstevel@tonic-gate /*
55580Sstevel@tonic-gate * Prevent q_next from changing in this stream by incrementing sq_count.
55590Sstevel@tonic-gate *
55609671SBrian.Ruthven@Sun.COM * No need to grab sq_putlocks here. See comment in strsubr.h that explains when
55610Sstevel@tonic-gate * sq_putlocks are used.
55620Sstevel@tonic-gate */
55630Sstevel@tonic-gate void
claimq(queue_t * qp)55640Sstevel@tonic-gate claimq(queue_t *qp)
55650Sstevel@tonic-gate {
55660Sstevel@tonic-gate syncq_t *sq = qp->q_syncq;
55670Sstevel@tonic-gate
55680Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
55690Sstevel@tonic-gate sq->sq_count++;
55700Sstevel@tonic-gate ASSERT(sq->sq_count != 0); /* Wraparound */
55710Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
55720Sstevel@tonic-gate }
55730Sstevel@tonic-gate
55740Sstevel@tonic-gate /*
55750Sstevel@tonic-gate * Undo claimq.
55760Sstevel@tonic-gate *
55779671SBrian.Ruthven@Sun.COM * No need to grab sq_putlocks here. See comment in strsubr.h that explains when
55780Sstevel@tonic-gate * sq_putlocks are used.
55790Sstevel@tonic-gate */
55800Sstevel@tonic-gate void
releaseq(queue_t * qp)55810Sstevel@tonic-gate releaseq(queue_t *qp)
55820Sstevel@tonic-gate {
55830Sstevel@tonic-gate syncq_t *sq = qp->q_syncq;
55840Sstevel@tonic-gate uint16_t flags;
55850Sstevel@tonic-gate
55860Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
55870Sstevel@tonic-gate ASSERT(sq->sq_count > 0);
55880Sstevel@tonic-gate sq->sq_count--;
55890Sstevel@tonic-gate
55900Sstevel@tonic-gate flags = sq->sq_flags;
55910Sstevel@tonic-gate if (flags & (SQ_WANTWAKEUP|SQ_QUEUED)) {
55920Sstevel@tonic-gate if (flags & SQ_WANTWAKEUP) {
55930Sstevel@tonic-gate flags &= ~SQ_WANTWAKEUP;
55940Sstevel@tonic-gate cv_broadcast(&sq->sq_wait);
55950Sstevel@tonic-gate }
55960Sstevel@tonic-gate sq->sq_flags = flags;
55970Sstevel@tonic-gate if ((flags & SQ_QUEUED) && !(flags & (SQ_STAYAWAY|SQ_EXCL))) {
55980Sstevel@tonic-gate /*
55990Sstevel@tonic-gate * To prevent potential recursive invocation of
56000Sstevel@tonic-gate * drain_syncq we do not call drain_syncq if count is
56010Sstevel@tonic-gate * non-zero.
56020Sstevel@tonic-gate */
56030Sstevel@tonic-gate if (sq->sq_count == 0) {
56040Sstevel@tonic-gate drain_syncq(sq);
56050Sstevel@tonic-gate return;
56060Sstevel@tonic-gate } else
56070Sstevel@tonic-gate sqenable(sq);
56080Sstevel@tonic-gate }
56090Sstevel@tonic-gate }
56100Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
56110Sstevel@tonic-gate }
56120Sstevel@tonic-gate
56130Sstevel@tonic-gate /*
56140Sstevel@tonic-gate * Prevent q_next from changing in this stream by incrementing sd_refcnt.
56150Sstevel@tonic-gate */
56160Sstevel@tonic-gate void
claimstr(queue_t * qp)56170Sstevel@tonic-gate claimstr(queue_t *qp)
56180Sstevel@tonic-gate {
56190Sstevel@tonic-gate struct stdata *stp = STREAM(qp);
56200Sstevel@tonic-gate
56210Sstevel@tonic-gate mutex_enter(&stp->sd_reflock);
56220Sstevel@tonic-gate stp->sd_refcnt++;
56230Sstevel@tonic-gate ASSERT(stp->sd_refcnt != 0); /* Wraparound */
56240Sstevel@tonic-gate mutex_exit(&stp->sd_reflock);
56250Sstevel@tonic-gate }
56260Sstevel@tonic-gate
56270Sstevel@tonic-gate /*
56280Sstevel@tonic-gate * Undo claimstr.
56290Sstevel@tonic-gate */
56300Sstevel@tonic-gate void
releasestr(queue_t * qp)56310Sstevel@tonic-gate releasestr(queue_t *qp)
56320Sstevel@tonic-gate {
56330Sstevel@tonic-gate struct stdata *stp = STREAM(qp);
56340Sstevel@tonic-gate
56350Sstevel@tonic-gate mutex_enter(&stp->sd_reflock);
56360Sstevel@tonic-gate ASSERT(stp->sd_refcnt != 0);
5637166Sxy158873 if (--stp->sd_refcnt == 0)
5638166Sxy158873 cv_broadcast(&stp->sd_refmonitor);
56390Sstevel@tonic-gate mutex_exit(&stp->sd_reflock);
56400Sstevel@tonic-gate }
56410Sstevel@tonic-gate
56420Sstevel@tonic-gate static syncq_t *
new_syncq(void)56430Sstevel@tonic-gate new_syncq(void)
56440Sstevel@tonic-gate {
56450Sstevel@tonic-gate return (kmem_cache_alloc(syncq_cache, KM_SLEEP));
56460Sstevel@tonic-gate }
56470Sstevel@tonic-gate
56480Sstevel@tonic-gate static void
free_syncq(syncq_t * sq)56490Sstevel@tonic-gate free_syncq(syncq_t *sq)
56500Sstevel@tonic-gate {
56510Sstevel@tonic-gate ASSERT(sq->sq_head == NULL);
56520Sstevel@tonic-gate ASSERT(sq->sq_outer == NULL);
56530Sstevel@tonic-gate ASSERT(sq->sq_callbpend == NULL);
56540Sstevel@tonic-gate ASSERT((sq->sq_onext == NULL && sq->sq_oprev == NULL) ||
56550Sstevel@tonic-gate (sq->sq_onext == sq && sq->sq_oprev == sq));
56560Sstevel@tonic-gate
56570Sstevel@tonic-gate if (sq->sq_ciputctrl != NULL) {
56580Sstevel@tonic-gate ASSERT(sq->sq_nciputctrl == n_ciputctrl - 1);
56590Sstevel@tonic-gate SUMCHECK_CIPUTCTRL_COUNTS(sq->sq_ciputctrl,
56600Sstevel@tonic-gate sq->sq_nciputctrl, 0);
56610Sstevel@tonic-gate ASSERT(ciputctrl_cache != NULL);
56620Sstevel@tonic-gate kmem_cache_free(ciputctrl_cache, sq->sq_ciputctrl);
56630Sstevel@tonic-gate }
56640Sstevel@tonic-gate
56650Sstevel@tonic-gate sq->sq_tail = NULL;
56660Sstevel@tonic-gate sq->sq_evhead = NULL;
56670Sstevel@tonic-gate sq->sq_evtail = NULL;
56680Sstevel@tonic-gate sq->sq_ciputctrl = NULL;
56690Sstevel@tonic-gate sq->sq_nciputctrl = 0;
56700Sstevel@tonic-gate sq->sq_count = 0;
56710Sstevel@tonic-gate sq->sq_rmqcount = 0;
56720Sstevel@tonic-gate sq->sq_callbflags = 0;
56730Sstevel@tonic-gate sq->sq_cancelid = 0;
56740Sstevel@tonic-gate sq->sq_next = NULL;
56750Sstevel@tonic-gate sq->sq_needexcl = 0;
56760Sstevel@tonic-gate sq->sq_svcflags = 0;
56770Sstevel@tonic-gate sq->sq_nqueues = 0;
56780Sstevel@tonic-gate sq->sq_pri = 0;
56790Sstevel@tonic-gate sq->sq_onext = NULL;
56800Sstevel@tonic-gate sq->sq_oprev = NULL;
56810Sstevel@tonic-gate sq->sq_flags = 0;
56820Sstevel@tonic-gate sq->sq_type = 0;
56830Sstevel@tonic-gate sq->sq_servcount = 0;
56840Sstevel@tonic-gate
56850Sstevel@tonic-gate kmem_cache_free(syncq_cache, sq);
56860Sstevel@tonic-gate }
56870Sstevel@tonic-gate
56880Sstevel@tonic-gate /* Outer perimeter code */
56890Sstevel@tonic-gate
56900Sstevel@tonic-gate /*
56910Sstevel@tonic-gate * The outer syncq uses the fields and flags in the syncq slightly
56920Sstevel@tonic-gate * differently from the inner syncqs.
56930Sstevel@tonic-gate * sq_count Incremented when there are pending or running
56940Sstevel@tonic-gate * writers at the outer perimeter to prevent the set of
56950Sstevel@tonic-gate * inner syncqs that belong to the outer perimeter from
56960Sstevel@tonic-gate * changing.
56970Sstevel@tonic-gate * sq_head/tail List of deferred qwriter(OUTER) operations.
56980Sstevel@tonic-gate *
56990Sstevel@tonic-gate * SQ_BLOCKED Set to prevent traversing of sq_next,sq_prev while
57000Sstevel@tonic-gate * inner syncqs are added to or removed from the
57010Sstevel@tonic-gate * outer perimeter.
57029671SBrian.Ruthven@Sun.COM * SQ_QUEUED sq_head/tail has messages or events queued.
57030Sstevel@tonic-gate *
57040Sstevel@tonic-gate * SQ_WRITER A thread is currently traversing all the inner syncqs
57050Sstevel@tonic-gate * setting the SQ_WRITER flag.
57060Sstevel@tonic-gate */
57070Sstevel@tonic-gate
57080Sstevel@tonic-gate /*
57090Sstevel@tonic-gate * Get write access at the outer perimeter.
57100Sstevel@tonic-gate * Note that read access is done by entersq, putnext, and put by simply
57110Sstevel@tonic-gate * incrementing sq_count in the inner syncq.
57120Sstevel@tonic-gate *
57130Sstevel@tonic-gate * Waits until "flags" is no longer set in the outer to prevent multiple
57140Sstevel@tonic-gate * threads from having write access at the same time. SQ_WRITER has to be part
57150Sstevel@tonic-gate * of "flags".
57160Sstevel@tonic-gate *
57170Sstevel@tonic-gate * Increases sq_count on the outer syncq to keep away outer_insert/remove
57180Sstevel@tonic-gate * until the outer_exit is finished.
57190Sstevel@tonic-gate *
57200Sstevel@tonic-gate * outer_enter is vulnerable to starvation since it does not prevent new
57210Sstevel@tonic-gate * threads from entering the inner syncqs while it is waiting for sq_count to
57220Sstevel@tonic-gate * go to zero.
57230Sstevel@tonic-gate */
57240Sstevel@tonic-gate void
outer_enter(syncq_t * outer,uint16_t flags)57250Sstevel@tonic-gate outer_enter(syncq_t *outer, uint16_t flags)
57260Sstevel@tonic-gate {
57270Sstevel@tonic-gate syncq_t *sq;
57280Sstevel@tonic-gate int wait_needed;
57290Sstevel@tonic-gate uint16_t count;
57300Sstevel@tonic-gate
57310Sstevel@tonic-gate ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL &&
57320Sstevel@tonic-gate outer->sq_oprev != NULL);
57330Sstevel@tonic-gate ASSERT(flags & SQ_WRITER);
57340Sstevel@tonic-gate
57350Sstevel@tonic-gate retry:
57360Sstevel@tonic-gate mutex_enter(SQLOCK(outer));
57370Sstevel@tonic-gate while (outer->sq_flags & flags) {
57380Sstevel@tonic-gate outer->sq_flags |= SQ_WANTWAKEUP;
57390Sstevel@tonic-gate cv_wait(&outer->sq_wait, SQLOCK(outer));
57400Sstevel@tonic-gate }
57410Sstevel@tonic-gate
57420Sstevel@tonic-gate ASSERT(!(outer->sq_flags & SQ_WRITER));
57430Sstevel@tonic-gate outer->sq_flags |= SQ_WRITER;
57440Sstevel@tonic-gate outer->sq_count++;
57450Sstevel@tonic-gate ASSERT(outer->sq_count != 0); /* wraparound */
57460Sstevel@tonic-gate wait_needed = 0;
57470Sstevel@tonic-gate /*
57480Sstevel@tonic-gate * Set SQ_WRITER on all the inner syncqs while holding
57490Sstevel@tonic-gate * the SQLOCK on the outer syncq. This ensures that the changing
57500Sstevel@tonic-gate * of SQ_WRITER is atomic under the outer SQLOCK.
57510Sstevel@tonic-gate */
57520Sstevel@tonic-gate for (sq = outer->sq_onext; sq != outer; sq = sq->sq_onext) {
57530Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
57540Sstevel@tonic-gate count = sq->sq_count;
57550Sstevel@tonic-gate SQ_PUTLOCKS_ENTER(sq);
57560Sstevel@tonic-gate sq->sq_flags |= SQ_WRITER;
57570Sstevel@tonic-gate SUM_SQ_PUTCOUNTS(sq, count);
57580Sstevel@tonic-gate if (count != 0)
57590Sstevel@tonic-gate wait_needed = 1;
57600Sstevel@tonic-gate SQ_PUTLOCKS_EXIT(sq);
57610Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
57620Sstevel@tonic-gate }
57630Sstevel@tonic-gate mutex_exit(SQLOCK(outer));
57640Sstevel@tonic-gate
57650Sstevel@tonic-gate /*
57660Sstevel@tonic-gate * Get everybody out of the syncqs sequentially.
57679671SBrian.Ruthven@Sun.COM * Note that we don't actually need to acquire the PUTLOCKS, since
57680Sstevel@tonic-gate * we have already cleared the fastbit, and set QWRITER. By
57690Sstevel@tonic-gate * definition, the count can not increase since putnext will
57709671SBrian.Ruthven@Sun.COM * take the slowlock path (and the purpose of acquiring the
57710Sstevel@tonic-gate * putlocks was to make sure it didn't increase while we were
57720Sstevel@tonic-gate * waiting).
57730Sstevel@tonic-gate *
57749671SBrian.Ruthven@Sun.COM * Note that we still acquire the PUTLOCKS to be safe.
57750Sstevel@tonic-gate */
57760Sstevel@tonic-gate if (wait_needed) {
57770Sstevel@tonic-gate for (sq = outer->sq_onext; sq != outer; sq = sq->sq_onext) {
57780Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
57790Sstevel@tonic-gate count = sq->sq_count;
57800Sstevel@tonic-gate SQ_PUTLOCKS_ENTER(sq);
57810Sstevel@tonic-gate SUM_SQ_PUTCOUNTS(sq, count);
57820Sstevel@tonic-gate while (count != 0) {
57830Sstevel@tonic-gate sq->sq_flags |= SQ_WANTWAKEUP;
57840Sstevel@tonic-gate SQ_PUTLOCKS_EXIT(sq);
57850Sstevel@tonic-gate cv_wait(&sq->sq_wait, SQLOCK(sq));
57860Sstevel@tonic-gate count = sq->sq_count;
57870Sstevel@tonic-gate SQ_PUTLOCKS_ENTER(sq);
57880Sstevel@tonic-gate SUM_SQ_PUTCOUNTS(sq, count);
57890Sstevel@tonic-gate }
57900Sstevel@tonic-gate SQ_PUTLOCKS_EXIT(sq);
57910Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
57920Sstevel@tonic-gate }
57930Sstevel@tonic-gate /*
57940Sstevel@tonic-gate * Verify that none of the flags got set while we
57950Sstevel@tonic-gate * were waiting for the sq_counts to drop.
57960Sstevel@tonic-gate * If this happens we exit and retry entering the
57970Sstevel@tonic-gate * outer perimeter.
57980Sstevel@tonic-gate */
57990Sstevel@tonic-gate mutex_enter(SQLOCK(outer));
58000Sstevel@tonic-gate if (outer->sq_flags & (flags & ~SQ_WRITER)) {
58010Sstevel@tonic-gate mutex_exit(SQLOCK(outer));
58020Sstevel@tonic-gate outer_exit(outer);
58030Sstevel@tonic-gate goto retry;
58040Sstevel@tonic-gate }
58050Sstevel@tonic-gate mutex_exit(SQLOCK(outer));
58060Sstevel@tonic-gate }
58070Sstevel@tonic-gate }
58080Sstevel@tonic-gate
58090Sstevel@tonic-gate /*
58100Sstevel@tonic-gate * Drop the write access at the outer perimeter.
58110Sstevel@tonic-gate * Read access is dropped implicitly (by putnext, put, and leavesq) by
58120Sstevel@tonic-gate * decrementing sq_count.
58130Sstevel@tonic-gate */
58140Sstevel@tonic-gate void
outer_exit(syncq_t * outer)58150Sstevel@tonic-gate outer_exit(syncq_t *outer)
58160Sstevel@tonic-gate {
58170Sstevel@tonic-gate syncq_t *sq;
58180Sstevel@tonic-gate int drain_needed;
58190Sstevel@tonic-gate uint16_t flags;
58200Sstevel@tonic-gate
58210Sstevel@tonic-gate ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL &&
58220Sstevel@tonic-gate outer->sq_oprev != NULL);
58230Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(SQLOCK(outer)));
58240Sstevel@tonic-gate
58250Sstevel@tonic-gate /*
58260Sstevel@tonic-gate * Atomically (from the perspective of threads calling become_writer)
58270Sstevel@tonic-gate * drop the write access at the outer perimeter by holding
58280Sstevel@tonic-gate * SQLOCK(outer) across all the dropsq calls and the resetting of
58290Sstevel@tonic-gate * SQ_WRITER.
58300Sstevel@tonic-gate * This defines a locking order between the outer perimeter
58310Sstevel@tonic-gate * SQLOCK and the inner perimeter SQLOCKs.
58320Sstevel@tonic-gate */
58330Sstevel@tonic-gate mutex_enter(SQLOCK(outer));
58340Sstevel@tonic-gate flags = outer->sq_flags;
58350Sstevel@tonic-gate ASSERT(outer->sq_flags & SQ_WRITER);
58360Sstevel@tonic-gate if (flags & SQ_QUEUED) {
58370Sstevel@tonic-gate write_now(outer);
58380Sstevel@tonic-gate flags = outer->sq_flags;
58390Sstevel@tonic-gate }
58400Sstevel@tonic-gate
58410Sstevel@tonic-gate /*
58420Sstevel@tonic-gate * sq_onext is stable since sq_count has not yet been decreased.
58430Sstevel@tonic-gate * Reset the SQ_WRITER flags in all syncqs.
58440Sstevel@tonic-gate * After dropping SQ_WRITER on the outer syncq we empty all the
58450Sstevel@tonic-gate * inner syncqs.
58460Sstevel@tonic-gate */
58470Sstevel@tonic-gate drain_needed = 0;
58480Sstevel@tonic-gate for (sq = outer->sq_onext; sq != outer; sq = sq->sq_onext)
58490Sstevel@tonic-gate drain_needed += dropsq(sq, SQ_WRITER);
58500Sstevel@tonic-gate ASSERT(!(outer->sq_flags & SQ_QUEUED));
58510Sstevel@tonic-gate flags &= ~SQ_WRITER;
58520Sstevel@tonic-gate if (drain_needed) {
58530Sstevel@tonic-gate outer->sq_flags = flags;
58540Sstevel@tonic-gate mutex_exit(SQLOCK(outer));
58550Sstevel@tonic-gate for (sq = outer->sq_onext; sq != outer; sq = sq->sq_onext)
58560Sstevel@tonic-gate emptysq(sq);
58570Sstevel@tonic-gate mutex_enter(SQLOCK(outer));
58580Sstevel@tonic-gate flags = outer->sq_flags;
58590Sstevel@tonic-gate }
58600Sstevel@tonic-gate if (flags & SQ_WANTWAKEUP) {
58610Sstevel@tonic-gate flags &= ~SQ_WANTWAKEUP;
58620Sstevel@tonic-gate cv_broadcast(&outer->sq_wait);
58630Sstevel@tonic-gate }
58640Sstevel@tonic-gate outer->sq_flags = flags;
58650Sstevel@tonic-gate ASSERT(outer->sq_count > 0);
58660Sstevel@tonic-gate outer->sq_count--;
58670Sstevel@tonic-gate mutex_exit(SQLOCK(outer));
58680Sstevel@tonic-gate }
58690Sstevel@tonic-gate
58700Sstevel@tonic-gate /*
58710Sstevel@tonic-gate * Add another syncq to an outer perimeter.
58720Sstevel@tonic-gate * Block out all other access to the outer perimeter while it is being
58730Sstevel@tonic-gate * changed using blocksq.
58740Sstevel@tonic-gate * Assumes that the caller has *not* done an outer_enter.
58750Sstevel@tonic-gate *
58760Sstevel@tonic-gate * Vulnerable to starvation in blocksq.
58770Sstevel@tonic-gate */
58780Sstevel@tonic-gate static void
outer_insert(syncq_t * outer,syncq_t * sq)58790Sstevel@tonic-gate outer_insert(syncq_t *outer, syncq_t *sq)
58800Sstevel@tonic-gate {
58810Sstevel@tonic-gate ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL &&
58820Sstevel@tonic-gate outer->sq_oprev != NULL);
58830Sstevel@tonic-gate ASSERT(sq->sq_outer == NULL && sq->sq_onext == NULL &&
58840Sstevel@tonic-gate sq->sq_oprev == NULL); /* Can't be in an outer perimeter */
58850Sstevel@tonic-gate
58860Sstevel@tonic-gate /* Get exclusive access to the outer perimeter list */
58870Sstevel@tonic-gate blocksq(outer, SQ_BLOCKED, 0);
58880Sstevel@tonic-gate ASSERT(outer->sq_flags & SQ_BLOCKED);
58890Sstevel@tonic-gate ASSERT(!(outer->sq_flags & SQ_WRITER));
58900Sstevel@tonic-gate
58910Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
58920Sstevel@tonic-gate sq->sq_outer = outer;
58930Sstevel@tonic-gate outer->sq_onext->sq_oprev = sq;
58940Sstevel@tonic-gate sq->sq_onext = outer->sq_onext;
58950Sstevel@tonic-gate outer->sq_onext = sq;
58960Sstevel@tonic-gate sq->sq_oprev = outer;
58970Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
58980Sstevel@tonic-gate unblocksq(outer, SQ_BLOCKED, 1);
58990Sstevel@tonic-gate }
59000Sstevel@tonic-gate
59010Sstevel@tonic-gate /*
59020Sstevel@tonic-gate * Remove a syncq from an outer perimeter.
59030Sstevel@tonic-gate * Block out all other access to the outer perimeter while it is being
59040Sstevel@tonic-gate * changed using blocksq.
59050Sstevel@tonic-gate * Assumes that the caller has *not* done an outer_enter.
59060Sstevel@tonic-gate *
59070Sstevel@tonic-gate * Vulnerable to starvation in blocksq.
59080Sstevel@tonic-gate */
59090Sstevel@tonic-gate static void
outer_remove(syncq_t * outer,syncq_t * sq)59100Sstevel@tonic-gate outer_remove(syncq_t *outer, syncq_t *sq)
59110Sstevel@tonic-gate {
59120Sstevel@tonic-gate ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL &&
59130Sstevel@tonic-gate outer->sq_oprev != NULL);
59140Sstevel@tonic-gate ASSERT(sq->sq_outer == outer);
59150Sstevel@tonic-gate
59160Sstevel@tonic-gate /* Get exclusive access to the outer perimeter list */
59170Sstevel@tonic-gate blocksq(outer, SQ_BLOCKED, 0);
59180Sstevel@tonic-gate ASSERT(outer->sq_flags & SQ_BLOCKED);
59190Sstevel@tonic-gate ASSERT(!(outer->sq_flags & SQ_WRITER));
59200Sstevel@tonic-gate
59210Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
59220Sstevel@tonic-gate sq->sq_outer = NULL;
59230Sstevel@tonic-gate sq->sq_onext->sq_oprev = sq->sq_oprev;
59240Sstevel@tonic-gate sq->sq_oprev->sq_onext = sq->sq_onext;
59250Sstevel@tonic-gate sq->sq_oprev = sq->sq_onext = NULL;
59260Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
59270Sstevel@tonic-gate unblocksq(outer, SQ_BLOCKED, 1);
59280Sstevel@tonic-gate }
59290Sstevel@tonic-gate
59300Sstevel@tonic-gate /*
59310Sstevel@tonic-gate * Queue a deferred qwriter(OUTER) callback for this outer perimeter.
59320Sstevel@tonic-gate * If this is the first callback for this outer perimeter then add
59330Sstevel@tonic-gate * this outer perimeter to the list of outer perimeters that
59340Sstevel@tonic-gate * the qwriter_outer_thread will process.
59350Sstevel@tonic-gate *
59360Sstevel@tonic-gate * Increments sq_count in the outer syncq to prevent the membership
59370Sstevel@tonic-gate * of the outer perimeter (in terms of inner syncqs) to change while
59380Sstevel@tonic-gate * the callback is pending.
59390Sstevel@tonic-gate */
59400Sstevel@tonic-gate static void
queue_writer(syncq_t * outer,void (* func)(),queue_t * q,mblk_t * mp)59410Sstevel@tonic-gate queue_writer(syncq_t *outer, void (*func)(), queue_t *q, mblk_t *mp)
59420Sstevel@tonic-gate {
59430Sstevel@tonic-gate ASSERT(MUTEX_HELD(SQLOCK(outer)));
59440Sstevel@tonic-gate
59450Sstevel@tonic-gate mp->b_prev = (mblk_t *)func;
59460Sstevel@tonic-gate mp->b_queue = q;
59470Sstevel@tonic-gate mp->b_next = NULL;
59480Sstevel@tonic-gate outer->sq_count++; /* Decremented when dequeued */
59490Sstevel@tonic-gate ASSERT(outer->sq_count != 0); /* Wraparound */
59500Sstevel@tonic-gate if (outer->sq_evhead == NULL) {
59510Sstevel@tonic-gate /* First message. */
59520Sstevel@tonic-gate outer->sq_evhead = outer->sq_evtail = mp;
59530Sstevel@tonic-gate outer->sq_flags |= SQ_EVENTS;
59540Sstevel@tonic-gate mutex_exit(SQLOCK(outer));
59550Sstevel@tonic-gate STRSTAT(qwr_outer);
59560Sstevel@tonic-gate (void) taskq_dispatch(streams_taskq,
59570Sstevel@tonic-gate (task_func_t *)qwriter_outer_service, outer, TQ_SLEEP);
59580Sstevel@tonic-gate } else {
59590Sstevel@tonic-gate ASSERT(outer->sq_flags & SQ_EVENTS);
59600Sstevel@tonic-gate outer->sq_evtail->b_next = mp;
59610Sstevel@tonic-gate outer->sq_evtail = mp;
59620Sstevel@tonic-gate mutex_exit(SQLOCK(outer));
59630Sstevel@tonic-gate }
59640Sstevel@tonic-gate }
59650Sstevel@tonic-gate
59660Sstevel@tonic-gate /*
59670Sstevel@tonic-gate * Try and upgrade to write access at the outer perimeter. If this can
59680Sstevel@tonic-gate * not be done without blocking then queue the callback to be done
59690Sstevel@tonic-gate * by the qwriter_outer_thread.
59700Sstevel@tonic-gate *
59710Sstevel@tonic-gate * This routine can only be called from put or service procedures plus
59729671SBrian.Ruthven@Sun.COM * asynchronous callback routines that have properly entered the queue (with
59739671SBrian.Ruthven@Sun.COM * entersq). Thus qwriter(OUTER) assumes the caller has one claim on the syncq
59749671SBrian.Ruthven@Sun.COM * associated with q.
59750Sstevel@tonic-gate */
59760Sstevel@tonic-gate void
qwriter_outer(queue_t * q,mblk_t * mp,void (* func)())59770Sstevel@tonic-gate qwriter_outer(queue_t *q, mblk_t *mp, void (*func)())
59780Sstevel@tonic-gate {
59790Sstevel@tonic-gate syncq_t *osq, *sq, *outer;
59800Sstevel@tonic-gate int failed;
59810Sstevel@tonic-gate uint16_t flags;
59820Sstevel@tonic-gate
59830Sstevel@tonic-gate osq = q->q_syncq;
59840Sstevel@tonic-gate outer = osq->sq_outer;
59850Sstevel@tonic-gate if (outer == NULL)
59860Sstevel@tonic-gate panic("qwriter(PERIM_OUTER): no outer perimeter");
59870Sstevel@tonic-gate ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL &&
59880Sstevel@tonic-gate outer->sq_oprev != NULL);
59890Sstevel@tonic-gate
59900Sstevel@tonic-gate mutex_enter(SQLOCK(outer));
59910Sstevel@tonic-gate flags = outer->sq_flags;
59920Sstevel@tonic-gate /*
59930Sstevel@tonic-gate * If some thread is traversing sq_next, or if we are blocked by
59940Sstevel@tonic-gate * outer_insert or outer_remove, or if the we already have queued
59950Sstevel@tonic-gate * callbacks, then queue this callback for later processing.
59960Sstevel@tonic-gate *
59970Sstevel@tonic-gate * Also queue the qwriter for an interrupt thread in order
59980Sstevel@tonic-gate * to reduce the time spent running at high IPL.
59990Sstevel@tonic-gate * to identify there are events.
60000Sstevel@tonic-gate */
60010Sstevel@tonic-gate if ((flags & SQ_GOAWAY) || (curthread->t_pri >= kpreemptpri)) {
60020Sstevel@tonic-gate /*
60030Sstevel@tonic-gate * Queue the become_writer request.
60040Sstevel@tonic-gate * The queueing is atomic under SQLOCK(outer) in order
60050Sstevel@tonic-gate * to synchronize with outer_exit.
60060Sstevel@tonic-gate * queue_writer will drop the outer SQLOCK
60070Sstevel@tonic-gate */
60080Sstevel@tonic-gate if (flags & SQ_BLOCKED) {
60090Sstevel@tonic-gate /* Must set SQ_WRITER on inner perimeter */
60100Sstevel@tonic-gate mutex_enter(SQLOCK(osq));
60110Sstevel@tonic-gate osq->sq_flags |= SQ_WRITER;
60120Sstevel@tonic-gate mutex_exit(SQLOCK(osq));
60130Sstevel@tonic-gate } else {
60140Sstevel@tonic-gate if (!(flags & SQ_WRITER)) {
60150Sstevel@tonic-gate /*
60160Sstevel@tonic-gate * The outer could have been SQ_BLOCKED thus
60170Sstevel@tonic-gate * SQ_WRITER might not be set on the inner.
60180Sstevel@tonic-gate */
60190Sstevel@tonic-gate mutex_enter(SQLOCK(osq));
60200Sstevel@tonic-gate osq->sq_flags |= SQ_WRITER;
60210Sstevel@tonic-gate mutex_exit(SQLOCK(osq));
60220Sstevel@tonic-gate }
60230Sstevel@tonic-gate ASSERT(osq->sq_flags & SQ_WRITER);
60240Sstevel@tonic-gate }
60250Sstevel@tonic-gate queue_writer(outer, func, q, mp);
60260Sstevel@tonic-gate return;
60270Sstevel@tonic-gate }
60280Sstevel@tonic-gate /*
60290Sstevel@tonic-gate * We are half-way to exclusive access to the outer perimeter.
60300Sstevel@tonic-gate * Prevent any outer_enter, qwriter(OUTER), or outer_insert/remove
60310Sstevel@tonic-gate * while the inner syncqs are traversed.
60320Sstevel@tonic-gate */
60330Sstevel@tonic-gate outer->sq_count++;
60340Sstevel@tonic-gate ASSERT(outer->sq_count != 0); /* wraparound */
60350Sstevel@tonic-gate flags |= SQ_WRITER;
60360Sstevel@tonic-gate /*
60370Sstevel@tonic-gate * Check if we can run the function immediately. Mark all
60380Sstevel@tonic-gate * syncqs with the writer flag to prevent new entries into
60390Sstevel@tonic-gate * put and service procedures.
60400Sstevel@tonic-gate *
60410Sstevel@tonic-gate * Set SQ_WRITER on all the inner syncqs while holding
60420Sstevel@tonic-gate * the SQLOCK on the outer syncq. This ensures that the changing
60430Sstevel@tonic-gate * of SQ_WRITER is atomic under the outer SQLOCK.
60440Sstevel@tonic-gate */
60450Sstevel@tonic-gate failed = 0;
60460Sstevel@tonic-gate for (sq = outer->sq_onext; sq != outer; sq = sq->sq_onext) {
60470Sstevel@tonic-gate uint16_t count;
60480Sstevel@tonic-gate uint_t maxcnt = (sq == osq) ? 1 : 0;
60490Sstevel@tonic-gate
60500Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
60510Sstevel@tonic-gate count = sq->sq_count;
60520Sstevel@tonic-gate SQ_PUTLOCKS_ENTER(sq);
60530Sstevel@tonic-gate SUM_SQ_PUTCOUNTS(sq, count);
60540Sstevel@tonic-gate if (sq->sq_count > maxcnt)
60550Sstevel@tonic-gate failed = 1;
60560Sstevel@tonic-gate sq->sq_flags |= SQ_WRITER;
60570Sstevel@tonic-gate SQ_PUTLOCKS_EXIT(sq);
60580Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
60590Sstevel@tonic-gate }
60600Sstevel@tonic-gate if (failed) {
60610Sstevel@tonic-gate /*
60620Sstevel@tonic-gate * Some other thread has a read claim on the outer perimeter.
60630Sstevel@tonic-gate * Queue the callback for deferred processing.
60640Sstevel@tonic-gate *
60650Sstevel@tonic-gate * queue_writer will set SQ_QUEUED before we drop SQ_WRITER
60660Sstevel@tonic-gate * so that other qwriter(OUTER) calls will queue their
60670Sstevel@tonic-gate * callbacks as well. queue_writer increments sq_count so we
60680Sstevel@tonic-gate * decrement to compensate for the our increment.
60690Sstevel@tonic-gate *
60700Sstevel@tonic-gate * Dropping SQ_WRITER enables the writer thread to work
60710Sstevel@tonic-gate * on this outer perimeter.
60720Sstevel@tonic-gate */
60730Sstevel@tonic-gate outer->sq_flags = flags;
60740Sstevel@tonic-gate queue_writer(outer, func, q, mp);
60750Sstevel@tonic-gate /* queue_writer dropper the lock */
60760Sstevel@tonic-gate mutex_enter(SQLOCK(outer));
60770Sstevel@tonic-gate ASSERT(outer->sq_count > 0);
60780Sstevel@tonic-gate outer->sq_count--;
60790Sstevel@tonic-gate ASSERT(outer->sq_flags & SQ_WRITER);
60800Sstevel@tonic-gate flags = outer->sq_flags;
60810Sstevel@tonic-gate flags &= ~SQ_WRITER;
60820Sstevel@tonic-gate if (flags & SQ_WANTWAKEUP) {
60830Sstevel@tonic-gate flags &= ~SQ_WANTWAKEUP;
60840Sstevel@tonic-gate cv_broadcast(&outer->sq_wait);
60850Sstevel@tonic-gate }
60860Sstevel@tonic-gate outer->sq_flags = flags;
60870Sstevel@tonic-gate mutex_exit(SQLOCK(outer));
60880Sstevel@tonic-gate return;
60890Sstevel@tonic-gate } else {
60900Sstevel@tonic-gate outer->sq_flags = flags;
60910Sstevel@tonic-gate mutex_exit(SQLOCK(outer));
60920Sstevel@tonic-gate }
60930Sstevel@tonic-gate
60940Sstevel@tonic-gate /* Can run it immediately */
60950Sstevel@tonic-gate (*func)(q, mp);
60960Sstevel@tonic-gate
60970Sstevel@tonic-gate outer_exit(outer);
60980Sstevel@tonic-gate }
60990Sstevel@tonic-gate
61000Sstevel@tonic-gate /*
61010Sstevel@tonic-gate * Dequeue all writer callbacks from the outer perimeter and run them.
61020Sstevel@tonic-gate */
61030Sstevel@tonic-gate static void
write_now(syncq_t * outer)61040Sstevel@tonic-gate write_now(syncq_t *outer)
61050Sstevel@tonic-gate {
61060Sstevel@tonic-gate mblk_t *mp;
61070Sstevel@tonic-gate queue_t *q;
61080Sstevel@tonic-gate void (*func)();
61090Sstevel@tonic-gate
61100Sstevel@tonic-gate ASSERT(MUTEX_HELD(SQLOCK(outer)));
61110Sstevel@tonic-gate ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL &&
61120Sstevel@tonic-gate outer->sq_oprev != NULL);
61130Sstevel@tonic-gate while ((mp = outer->sq_evhead) != NULL) {
61140Sstevel@tonic-gate /*
61150Sstevel@tonic-gate * queues cannot be placed on the queuelist on the outer
61169671SBrian.Ruthven@Sun.COM * perimeter.
61170Sstevel@tonic-gate */
61180Sstevel@tonic-gate ASSERT(!(outer->sq_flags & SQ_MESSAGES));
61190Sstevel@tonic-gate ASSERT((outer->sq_flags & SQ_EVENTS));
61200Sstevel@tonic-gate
61210Sstevel@tonic-gate outer->sq_evhead = mp->b_next;
61220Sstevel@tonic-gate if (outer->sq_evhead == NULL) {
61230Sstevel@tonic-gate outer->sq_evtail = NULL;
61240Sstevel@tonic-gate outer->sq_flags &= ~SQ_EVENTS;
61250Sstevel@tonic-gate }
61260Sstevel@tonic-gate ASSERT(outer->sq_count != 0);
61270Sstevel@tonic-gate outer->sq_count--; /* Incremented when enqueued. */
61280Sstevel@tonic-gate mutex_exit(SQLOCK(outer));
61290Sstevel@tonic-gate /*
61300Sstevel@tonic-gate * Drop the message if the queue is closing.
61310Sstevel@tonic-gate * Make sure that the queue is "claimed" when the callback
61320Sstevel@tonic-gate * is run in order to satisfy various ASSERTs.
61330Sstevel@tonic-gate */
61340Sstevel@tonic-gate q = mp->b_queue;
61350Sstevel@tonic-gate func = (void (*)())mp->b_prev;
61360Sstevel@tonic-gate ASSERT(func != NULL);
61370Sstevel@tonic-gate mp->b_next = mp->b_prev = NULL;
61380Sstevel@tonic-gate if (q->q_flag & QWCLOSE) {
61390Sstevel@tonic-gate freemsg(mp);
61400Sstevel@tonic-gate } else {
61410Sstevel@tonic-gate claimq(q);
61420Sstevel@tonic-gate (*func)(q, mp);
61430Sstevel@tonic-gate releaseq(q);
61440Sstevel@tonic-gate }
61450Sstevel@tonic-gate mutex_enter(SQLOCK(outer));
61460Sstevel@tonic-gate }
61470Sstevel@tonic-gate ASSERT(MUTEX_HELD(SQLOCK(outer)));
61480Sstevel@tonic-gate }
61490Sstevel@tonic-gate
61500Sstevel@tonic-gate /*
61510Sstevel@tonic-gate * The list of messages on the inner syncq is effectively hashed
61520Sstevel@tonic-gate * by destination queue. These destination queues are doubly
61530Sstevel@tonic-gate * linked lists (hopefully) in priority order. Messages are then
61540Sstevel@tonic-gate * put on the queue referenced by the q_sqhead/q_sqtail elements.
61550Sstevel@tonic-gate * Additional messages are linked together by the b_next/b_prev
61560Sstevel@tonic-gate * elements in the mblk, with (similar to putq()) the first message
61570Sstevel@tonic-gate * having a NULL b_prev and the last message having a NULL b_next.
61580Sstevel@tonic-gate *
61590Sstevel@tonic-gate * Events, such as qwriter callbacks, are put onto a list in FIFO
61600Sstevel@tonic-gate * order referenced by sq_evhead, and sq_evtail. This is a singly
61610Sstevel@tonic-gate * linked list, and messages here MUST be processed in the order queued.
61620Sstevel@tonic-gate */
61630Sstevel@tonic-gate
61640Sstevel@tonic-gate /*
61650Sstevel@tonic-gate * Run the events on the syncq event list (sq_evhead).
61660Sstevel@tonic-gate * Assumes there is only one claim on the syncq, it is
61670Sstevel@tonic-gate * already exclusive (SQ_EXCL set), and the SQLOCK held.
61680Sstevel@tonic-gate * Messages here are processed in order, with the SQ_EXCL bit
61690Sstevel@tonic-gate * held all the way through till the last message is processed.
61700Sstevel@tonic-gate */
61710Sstevel@tonic-gate void
sq_run_events(syncq_t * sq)61720Sstevel@tonic-gate sq_run_events(syncq_t *sq)
61730Sstevel@tonic-gate {
61740Sstevel@tonic-gate mblk_t *bp;
61750Sstevel@tonic-gate queue_t *qp;
61760Sstevel@tonic-gate uint16_t flags = sq->sq_flags;
61770Sstevel@tonic-gate void (*func)();
61780Sstevel@tonic-gate
61790Sstevel@tonic-gate ASSERT(MUTEX_HELD(SQLOCK(sq)));
61800Sstevel@tonic-gate ASSERT((sq->sq_outer == NULL && sq->sq_onext == NULL &&
61815753Sgww sq->sq_oprev == NULL) ||
61825753Sgww (sq->sq_outer != NULL && sq->sq_onext != NULL &&
61835753Sgww sq->sq_oprev != NULL));
61840Sstevel@tonic-gate
61850Sstevel@tonic-gate ASSERT(flags & SQ_EXCL);
61860Sstevel@tonic-gate ASSERT(sq->sq_count == 1);
61870Sstevel@tonic-gate
61880Sstevel@tonic-gate /*
61890Sstevel@tonic-gate * We need to process all of the events on this list. It
61900Sstevel@tonic-gate * is possible that new events will be added while we are
61910Sstevel@tonic-gate * away processing a callback, so on every loop, we start
61920Sstevel@tonic-gate * back at the beginning of the list.
61930Sstevel@tonic-gate */
61940Sstevel@tonic-gate /*
61950Sstevel@tonic-gate * We have to reaccess sq_evhead since there is a
61960Sstevel@tonic-gate * possibility of a new entry while we were running
61970Sstevel@tonic-gate * the callback.
61980Sstevel@tonic-gate */
61990Sstevel@tonic-gate for (bp = sq->sq_evhead; bp != NULL; bp = sq->sq_evhead) {
62000Sstevel@tonic-gate ASSERT(bp->b_queue->q_syncq == sq);
62010Sstevel@tonic-gate ASSERT(sq->sq_flags & SQ_EVENTS);
62020Sstevel@tonic-gate
62030Sstevel@tonic-gate qp = bp->b_queue;
62040Sstevel@tonic-gate func = (void (*)())bp->b_prev;
62050Sstevel@tonic-gate ASSERT(func != NULL);
62060Sstevel@tonic-gate
62070Sstevel@tonic-gate /*
62080Sstevel@tonic-gate * Messages from the event queue must be taken off in
62090Sstevel@tonic-gate * FIFO order.
62100Sstevel@tonic-gate */
62110Sstevel@tonic-gate ASSERT(sq->sq_evhead == bp);
62120Sstevel@tonic-gate sq->sq_evhead = bp->b_next;
62130Sstevel@tonic-gate
62140Sstevel@tonic-gate if (bp->b_next == NULL) {
62150Sstevel@tonic-gate /* Deleting last */
62160Sstevel@tonic-gate ASSERT(sq->sq_evtail == bp);
62170Sstevel@tonic-gate sq->sq_evtail = NULL;
62180Sstevel@tonic-gate sq->sq_flags &= ~SQ_EVENTS;
62190Sstevel@tonic-gate }
62200Sstevel@tonic-gate bp->b_prev = bp->b_next = NULL;
62210Sstevel@tonic-gate ASSERT(bp->b_datap->db_ref != 0);
62220Sstevel@tonic-gate
62230Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
62240Sstevel@tonic-gate
62250Sstevel@tonic-gate (*func)(qp, bp);
62260Sstevel@tonic-gate
62270Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
62280Sstevel@tonic-gate /*
62290Sstevel@tonic-gate * re-read the flags, since they could have changed.
62300Sstevel@tonic-gate */
62310Sstevel@tonic-gate flags = sq->sq_flags;
62320Sstevel@tonic-gate ASSERT(flags & SQ_EXCL);
62330Sstevel@tonic-gate }
62340Sstevel@tonic-gate ASSERT(sq->sq_evhead == NULL && sq->sq_evtail == NULL);
62350Sstevel@tonic-gate ASSERT(!(sq->sq_flags & SQ_EVENTS));
62360Sstevel@tonic-gate
62370Sstevel@tonic-gate if (flags & SQ_WANTWAKEUP) {
62380Sstevel@tonic-gate flags &= ~SQ_WANTWAKEUP;
62390Sstevel@tonic-gate cv_broadcast(&sq->sq_wait);
62400Sstevel@tonic-gate }
62410Sstevel@tonic-gate if (flags & SQ_WANTEXWAKEUP) {
62420Sstevel@tonic-gate flags &= ~SQ_WANTEXWAKEUP;
62430Sstevel@tonic-gate cv_broadcast(&sq->sq_exitwait);
62440Sstevel@tonic-gate }
62450Sstevel@tonic-gate sq->sq_flags = flags;
62460Sstevel@tonic-gate }
62470Sstevel@tonic-gate
62480Sstevel@tonic-gate /*
62490Sstevel@tonic-gate * Put messages on the event list.
62500Sstevel@tonic-gate * If we can go exclusive now, do so and process the event list, otherwise
62510Sstevel@tonic-gate * let the last claim service this list (or wake the sqthread).
62520Sstevel@tonic-gate * This procedure assumes SQLOCK is held. To run the event list, it
62530Sstevel@tonic-gate * must be called with no claims.
62540Sstevel@tonic-gate */
62550Sstevel@tonic-gate static void
sqfill_events(syncq_t * sq,queue_t * q,mblk_t * mp,void (* func)())62560Sstevel@tonic-gate sqfill_events(syncq_t *sq, queue_t *q, mblk_t *mp, void (*func)())
62570Sstevel@tonic-gate {
62580Sstevel@tonic-gate uint16_t count;
62590Sstevel@tonic-gate
62600Sstevel@tonic-gate ASSERT(MUTEX_HELD(SQLOCK(sq)));
62610Sstevel@tonic-gate ASSERT(func != NULL);
62620Sstevel@tonic-gate
62630Sstevel@tonic-gate /*
62640Sstevel@tonic-gate * This is a callback. Add it to the list of callbacks
62650Sstevel@tonic-gate * and see about upgrading.
62660Sstevel@tonic-gate */
62670Sstevel@tonic-gate mp->b_prev = (mblk_t *)func;
62680Sstevel@tonic-gate mp->b_queue = q;
62690Sstevel@tonic-gate mp->b_next = NULL;
62700Sstevel@tonic-gate if (sq->sq_evhead == NULL) {
62710Sstevel@tonic-gate sq->sq_evhead = sq->sq_evtail = mp;
62720Sstevel@tonic-gate sq->sq_flags |= SQ_EVENTS;
62730Sstevel@tonic-gate } else {
62740Sstevel@tonic-gate ASSERT(sq->sq_evtail != NULL);
62750Sstevel@tonic-gate ASSERT(sq->sq_evtail->b_next == NULL);
62760Sstevel@tonic-gate ASSERT(sq->sq_flags & SQ_EVENTS);
62770Sstevel@tonic-gate sq->sq_evtail->b_next = mp;
62780Sstevel@tonic-gate sq->sq_evtail = mp;
62790Sstevel@tonic-gate }
62800Sstevel@tonic-gate /*
62810Sstevel@tonic-gate * We have set SQ_EVENTS, so threads will have to
62829671SBrian.Ruthven@Sun.COM * unwind out of the perimeter, and new entries will
62830Sstevel@tonic-gate * not grab a putlock. But we still need to know
62840Sstevel@tonic-gate * how many threads have already made a claim to the
62850Sstevel@tonic-gate * syncq, so grab the putlocks, and sum the counts.
62860Sstevel@tonic-gate * If there are no claims on the syncq, we can upgrade
62870Sstevel@tonic-gate * to exclusive, and run the event list.
62880Sstevel@tonic-gate * NOTE: We hold the SQLOCK, so we can just grab the
62890Sstevel@tonic-gate * putlocks.
62900Sstevel@tonic-gate */
62910Sstevel@tonic-gate count = sq->sq_count;
62920Sstevel@tonic-gate SQ_PUTLOCKS_ENTER(sq);
62930Sstevel@tonic-gate SUM_SQ_PUTCOUNTS(sq, count);
62940Sstevel@tonic-gate /*
62950Sstevel@tonic-gate * We have no claim, so we need to check if there
62960Sstevel@tonic-gate * are no others, then we can upgrade.
62970Sstevel@tonic-gate */
62980Sstevel@tonic-gate /*
62990Sstevel@tonic-gate * There are currently no claims on
63000Sstevel@tonic-gate * the syncq by this thread (at least on this entry). The thread who has
63010Sstevel@tonic-gate * the claim should drain syncq.
63020Sstevel@tonic-gate */
63030Sstevel@tonic-gate if (count > 0) {
63040Sstevel@tonic-gate /*
63050Sstevel@tonic-gate * Can't upgrade - other threads inside.
63060Sstevel@tonic-gate */
63070Sstevel@tonic-gate SQ_PUTLOCKS_EXIT(sq);
63080Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
63090Sstevel@tonic-gate return;
63100Sstevel@tonic-gate }
63110Sstevel@tonic-gate /*
63120Sstevel@tonic-gate * Need to set SQ_EXCL and make a claim on the syncq.
63130Sstevel@tonic-gate */
63140Sstevel@tonic-gate ASSERT((sq->sq_flags & SQ_EXCL) == 0);
63150Sstevel@tonic-gate sq->sq_flags |= SQ_EXCL;
63160Sstevel@tonic-gate ASSERT(sq->sq_count == 0);
63170Sstevel@tonic-gate sq->sq_count++;
63180Sstevel@tonic-gate SQ_PUTLOCKS_EXIT(sq);
63190Sstevel@tonic-gate
63200Sstevel@tonic-gate /* Process the events list */
63210Sstevel@tonic-gate sq_run_events(sq);
63220Sstevel@tonic-gate
63230Sstevel@tonic-gate /*
63240Sstevel@tonic-gate * Release our claim...
63250Sstevel@tonic-gate */
63260Sstevel@tonic-gate sq->sq_count--;
63270Sstevel@tonic-gate
63280Sstevel@tonic-gate /*
63290Sstevel@tonic-gate * And release SQ_EXCL.
63300Sstevel@tonic-gate * We don't need to acquire the putlocks to release
63310Sstevel@tonic-gate * SQ_EXCL, since we are exclusive, and hold the SQLOCK.
63320Sstevel@tonic-gate */
63330Sstevel@tonic-gate sq->sq_flags &= ~SQ_EXCL;
63340Sstevel@tonic-gate
63350Sstevel@tonic-gate /*
63360Sstevel@tonic-gate * sq_run_events should have released SQ_EXCL
63370Sstevel@tonic-gate */
63380Sstevel@tonic-gate ASSERT(!(sq->sq_flags & SQ_EXCL));
63390Sstevel@tonic-gate
63400Sstevel@tonic-gate /*
63410Sstevel@tonic-gate * If anything happened while we were running the
63420Sstevel@tonic-gate * events (or was there before), we need to process
63430Sstevel@tonic-gate * them now. We shouldn't be exclusive sine we
63449671SBrian.Ruthven@Sun.COM * released the perimeter above (plus, we asserted
63450Sstevel@tonic-gate * for it).
63460Sstevel@tonic-gate */
63470Sstevel@tonic-gate if (!(sq->sq_flags & SQ_STAYAWAY) && (sq->sq_flags & SQ_QUEUED))
63480Sstevel@tonic-gate drain_syncq(sq);
63490Sstevel@tonic-gate else
63500Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
63510Sstevel@tonic-gate }
63520Sstevel@tonic-gate
63530Sstevel@tonic-gate /*
63540Sstevel@tonic-gate * Perform delayed processing. The caller has to make sure that it is safe
63550Sstevel@tonic-gate * to enter the syncq (e.g. by checking that none of the SQ_STAYAWAY bits are
63569671SBrian.Ruthven@Sun.COM * set).
63570Sstevel@tonic-gate *
63580Sstevel@tonic-gate * Assume that the caller has NO claims on the syncq. However, a claim
63590Sstevel@tonic-gate * on the syncq does not indicate that a thread is draining the syncq.
63600Sstevel@tonic-gate * There may be more claims on the syncq than there are threads draining
63610Sstevel@tonic-gate * (i.e. #_threads_draining <= sq_count)
63620Sstevel@tonic-gate *
63630Sstevel@tonic-gate * drain_syncq has to terminate when one of the SQ_STAYAWAY bits gets set
63640Sstevel@tonic-gate * in order to preserve qwriter(OUTER) ordering constraints.
63650Sstevel@tonic-gate *
63660Sstevel@tonic-gate * sq_putcount only needs to be checked when dispatching the queued
63670Sstevel@tonic-gate * writer call for CIPUT sync queue, but this is handled in sq_run_events.
63680Sstevel@tonic-gate */
63690Sstevel@tonic-gate void
drain_syncq(syncq_t * sq)63700Sstevel@tonic-gate drain_syncq(syncq_t *sq)
63710Sstevel@tonic-gate {
63720Sstevel@tonic-gate queue_t *qp;
63730Sstevel@tonic-gate uint16_t count;
63740Sstevel@tonic-gate uint16_t type = sq->sq_type;
63750Sstevel@tonic-gate uint16_t flags = sq->sq_flags;
63760Sstevel@tonic-gate boolean_t bg_service = sq->sq_svcflags & SQ_SERVICE;
63770Sstevel@tonic-gate
63780Sstevel@tonic-gate TRACE_1(TR_FAC_STREAMS_FR, TR_DRAIN_SYNCQ_START,
63795753Sgww "drain_syncq start:%p", sq);
63800Sstevel@tonic-gate ASSERT(MUTEX_HELD(SQLOCK(sq)));
63810Sstevel@tonic-gate ASSERT((sq->sq_outer == NULL && sq->sq_onext == NULL &&
63825753Sgww sq->sq_oprev == NULL) ||
63835753Sgww (sq->sq_outer != NULL && sq->sq_onext != NULL &&
63845753Sgww sq->sq_oprev != NULL));
63850Sstevel@tonic-gate
63860Sstevel@tonic-gate /*
63870Sstevel@tonic-gate * Drop SQ_SERVICE flag.
63880Sstevel@tonic-gate */
63890Sstevel@tonic-gate if (bg_service)
63900Sstevel@tonic-gate sq->sq_svcflags &= ~SQ_SERVICE;
63910Sstevel@tonic-gate
63920Sstevel@tonic-gate /*
63930Sstevel@tonic-gate * If SQ_EXCL is set, someone else is processing this syncq - let him
63940Sstevel@tonic-gate * finish the job.
63950Sstevel@tonic-gate */
63960Sstevel@tonic-gate if (flags & SQ_EXCL) {
63970Sstevel@tonic-gate if (bg_service) {
63980Sstevel@tonic-gate ASSERT(sq->sq_servcount != 0);
63990Sstevel@tonic-gate sq->sq_servcount--;
64000Sstevel@tonic-gate }
64010Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
64020Sstevel@tonic-gate return;
64030Sstevel@tonic-gate }
64040Sstevel@tonic-gate
64050Sstevel@tonic-gate /*
64060Sstevel@tonic-gate * This routine can be called by a background thread if
64070Sstevel@tonic-gate * it was scheduled by a hi-priority thread. SO, if there are
64080Sstevel@tonic-gate * NOT messages queued, return (remember, we have the SQLOCK,
64090Sstevel@tonic-gate * and it cannot change until we release it). Wakeup any waiters also.
64100Sstevel@tonic-gate */
64110Sstevel@tonic-gate if (!(flags & SQ_QUEUED)) {
64120Sstevel@tonic-gate if (flags & SQ_WANTWAKEUP) {
64130Sstevel@tonic-gate flags &= ~SQ_WANTWAKEUP;
64140Sstevel@tonic-gate cv_broadcast(&sq->sq_wait);
64150Sstevel@tonic-gate }
64160Sstevel@tonic-gate if (flags & SQ_WANTEXWAKEUP) {
64170Sstevel@tonic-gate flags &= ~SQ_WANTEXWAKEUP;
64180Sstevel@tonic-gate cv_broadcast(&sq->sq_exitwait);
64190Sstevel@tonic-gate }
64200Sstevel@tonic-gate sq->sq_flags = flags;
64210Sstevel@tonic-gate if (bg_service) {
64220Sstevel@tonic-gate ASSERT(sq->sq_servcount != 0);
64230Sstevel@tonic-gate sq->sq_servcount--;
64240Sstevel@tonic-gate }
64250Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
64260Sstevel@tonic-gate return;
64270Sstevel@tonic-gate }
64280Sstevel@tonic-gate
64290Sstevel@tonic-gate /*
64309671SBrian.Ruthven@Sun.COM * If this is not a concurrent put perimeter, we need to
64310Sstevel@tonic-gate * become exclusive to drain. Also, if not CIPUT, we would
64320Sstevel@tonic-gate * not have acquired a putlock, so we don't need to check
64330Sstevel@tonic-gate * the putcounts. If not entering with a claim, we test
64340Sstevel@tonic-gate * for sq_count == 0.
64350Sstevel@tonic-gate */
64360Sstevel@tonic-gate type = sq->sq_type;
64370Sstevel@tonic-gate if (!(type & SQ_CIPUT)) {
64380Sstevel@tonic-gate if (sq->sq_count > 1) {
64390Sstevel@tonic-gate if (bg_service) {
64400Sstevel@tonic-gate ASSERT(sq->sq_servcount != 0);
64410Sstevel@tonic-gate sq->sq_servcount--;
64420Sstevel@tonic-gate }
64430Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
64440Sstevel@tonic-gate return;
64450Sstevel@tonic-gate }
64460Sstevel@tonic-gate sq->sq_flags |= SQ_EXCL;
64470Sstevel@tonic-gate }
64480Sstevel@tonic-gate
64490Sstevel@tonic-gate /*
64500Sstevel@tonic-gate * This is where we make a claim to the syncq.
64510Sstevel@tonic-gate * This can either be done by incrementing a putlock, or
64520Sstevel@tonic-gate * the sq_count. But since we already have the SQLOCK
64530Sstevel@tonic-gate * here, we just bump the sq_count.
64540Sstevel@tonic-gate *
64550Sstevel@tonic-gate * Note that after we make a claim, we need to let the code
64560Sstevel@tonic-gate * fall through to the end of this routine to clean itself
64570Sstevel@tonic-gate * up. A return in the while loop will put the syncq in a
64580Sstevel@tonic-gate * very bad state.
64590Sstevel@tonic-gate */
64600Sstevel@tonic-gate sq->sq_count++;
64610Sstevel@tonic-gate ASSERT(sq->sq_count != 0); /* wraparound */
64620Sstevel@tonic-gate
64630Sstevel@tonic-gate while ((flags = sq->sq_flags) & SQ_QUEUED) {
64640Sstevel@tonic-gate /*
64650Sstevel@tonic-gate * If we are told to stayaway or went exclusive,
64660Sstevel@tonic-gate * we are done.
64670Sstevel@tonic-gate */
64680Sstevel@tonic-gate if (flags & (SQ_STAYAWAY)) {
64690Sstevel@tonic-gate break;
64700Sstevel@tonic-gate }
64710Sstevel@tonic-gate
64720Sstevel@tonic-gate /*
64730Sstevel@tonic-gate * If there are events to run, do so.
64740Sstevel@tonic-gate * We have one claim to the syncq, so if there are
64750Sstevel@tonic-gate * more than one, other threads are running.
64760Sstevel@tonic-gate */
64770Sstevel@tonic-gate if (sq->sq_evhead != NULL) {
64780Sstevel@tonic-gate ASSERT(sq->sq_flags & SQ_EVENTS);
64790Sstevel@tonic-gate
64800Sstevel@tonic-gate count = sq->sq_count;
64810Sstevel@tonic-gate SQ_PUTLOCKS_ENTER(sq);
64820Sstevel@tonic-gate SUM_SQ_PUTCOUNTS(sq, count);
64830Sstevel@tonic-gate if (count > 1) {
64840Sstevel@tonic-gate SQ_PUTLOCKS_EXIT(sq);
64850Sstevel@tonic-gate /* Can't upgrade - other threads inside */
64860Sstevel@tonic-gate break;
64870Sstevel@tonic-gate }
64880Sstevel@tonic-gate ASSERT((flags & SQ_EXCL) == 0);
64890Sstevel@tonic-gate sq->sq_flags = flags | SQ_EXCL;
64900Sstevel@tonic-gate SQ_PUTLOCKS_EXIT(sq);
64910Sstevel@tonic-gate /*
64920Sstevel@tonic-gate * we have the only claim, run the events,
64930Sstevel@tonic-gate * sq_run_events will clear the SQ_EXCL flag.
64940Sstevel@tonic-gate */
64950Sstevel@tonic-gate sq_run_events(sq);
64960Sstevel@tonic-gate
64970Sstevel@tonic-gate /*
64989671SBrian.Ruthven@Sun.COM * If this is a CIPUT perimeter, we need
64990Sstevel@tonic-gate * to drop the SQ_EXCL flag so we can properly
65000Sstevel@tonic-gate * continue draining the syncq.
65010Sstevel@tonic-gate */
65020Sstevel@tonic-gate if (type & SQ_CIPUT) {
65030Sstevel@tonic-gate ASSERT(sq->sq_flags & SQ_EXCL);
65040Sstevel@tonic-gate sq->sq_flags &= ~SQ_EXCL;
65050Sstevel@tonic-gate }
65060Sstevel@tonic-gate
65070Sstevel@tonic-gate /*
65080Sstevel@tonic-gate * And go back to the beginning just in case
65090Sstevel@tonic-gate * anything changed while we were away.
65100Sstevel@tonic-gate */
65110Sstevel@tonic-gate ASSERT((sq->sq_flags & SQ_EXCL) || (type & SQ_CIPUT));
65120Sstevel@tonic-gate continue;
65130Sstevel@tonic-gate }
65140Sstevel@tonic-gate
65150Sstevel@tonic-gate ASSERT(sq->sq_evhead == NULL);
65160Sstevel@tonic-gate ASSERT(!(sq->sq_flags & SQ_EVENTS));
65170Sstevel@tonic-gate
65180Sstevel@tonic-gate /*
65190Sstevel@tonic-gate * Find the queue that is not draining.
65200Sstevel@tonic-gate *
65210Sstevel@tonic-gate * q_draining is protected by QLOCK which we do not hold.
65220Sstevel@tonic-gate * But if it was set, then a thread was draining, and if it gets
65230Sstevel@tonic-gate * cleared, then it was because the thread has successfully
65249671SBrian.Ruthven@Sun.COM * drained the syncq, or a GOAWAY state occurred. For the GOAWAY
65250Sstevel@tonic-gate * state to happen, a thread needs the SQLOCK which we hold, and
65269671SBrian.Ruthven@Sun.COM * if there was such a flag, we would have already seen it.
65270Sstevel@tonic-gate */
65280Sstevel@tonic-gate
65290Sstevel@tonic-gate for (qp = sq->sq_head;
65300Sstevel@tonic-gate qp != NULL && (qp->q_draining ||
65315753Sgww (qp->q_sqflags & Q_SQDRAINING));
65320Sstevel@tonic-gate qp = qp->q_sqnext)
65330Sstevel@tonic-gate ;
65340Sstevel@tonic-gate
65350Sstevel@tonic-gate if (qp == NULL)
65360Sstevel@tonic-gate break;
65370Sstevel@tonic-gate
65380Sstevel@tonic-gate /*
65390Sstevel@tonic-gate * We have a queue to work on, and we hold the
65400Sstevel@tonic-gate * SQLOCK and one claim, call qdrain_syncq.
65410Sstevel@tonic-gate * This means we need to release the SQLOCK and
65429671SBrian.Ruthven@Sun.COM * acquire the QLOCK (OK since we have a claim).
65430Sstevel@tonic-gate * Note that qdrain_syncq will actually dequeue
65440Sstevel@tonic-gate * this queue from the sq_head list when it is
65450Sstevel@tonic-gate * convinced all the work is done and release
65460Sstevel@tonic-gate * the QLOCK before returning.
65470Sstevel@tonic-gate */
65480Sstevel@tonic-gate qp->q_sqflags |= Q_SQDRAINING;
65490Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
65500Sstevel@tonic-gate mutex_enter(QLOCK(qp));
65510Sstevel@tonic-gate qdrain_syncq(sq, qp);
65520Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
65530Sstevel@tonic-gate
65540Sstevel@tonic-gate /* The queue is drained */
65550Sstevel@tonic-gate ASSERT(qp->q_sqflags & Q_SQDRAINING);
65560Sstevel@tonic-gate qp->q_sqflags &= ~Q_SQDRAINING;
65570Sstevel@tonic-gate /*
65580Sstevel@tonic-gate * NOTE: After this point qp should not be used since it may be
65590Sstevel@tonic-gate * closed.
65600Sstevel@tonic-gate */
65610Sstevel@tonic-gate }
65620Sstevel@tonic-gate
65630Sstevel@tonic-gate ASSERT(MUTEX_HELD(SQLOCK(sq)));
65640Sstevel@tonic-gate flags = sq->sq_flags;
65650Sstevel@tonic-gate
65660Sstevel@tonic-gate /*
65670Sstevel@tonic-gate * sq->sq_head cannot change because we hold the
65680Sstevel@tonic-gate * sqlock. However, a thread CAN decide that it is no longer
65690Sstevel@tonic-gate * going to drain that queue. However, this should be due to
65700Sstevel@tonic-gate * a GOAWAY state, and we should see that here.
65710Sstevel@tonic-gate *
65720Sstevel@tonic-gate * This loop is not very efficient. One solution may be adding a second
65730Sstevel@tonic-gate * pointer to the "draining" queue, but it is difficult to do when
65740Sstevel@tonic-gate * queues are inserted in the middle due to priority ordering. Another
65750Sstevel@tonic-gate * possibility is to yank the queue out of the sq list and put it onto
65760Sstevel@tonic-gate * the "draining list" and then put it back if it can't be drained.
65770Sstevel@tonic-gate */
65780Sstevel@tonic-gate
65790Sstevel@tonic-gate ASSERT((sq->sq_head == NULL) || (flags & SQ_GOAWAY) ||
65805753Sgww (type & SQ_CI) || sq->sq_head->q_draining);
65810Sstevel@tonic-gate
65829671SBrian.Ruthven@Sun.COM /* Drop SQ_EXCL for non-CIPUT perimeters */
65830Sstevel@tonic-gate if (!(type & SQ_CIPUT))
65840Sstevel@tonic-gate flags &= ~SQ_EXCL;
65850Sstevel@tonic-gate ASSERT((flags & SQ_EXCL) == 0);
65860Sstevel@tonic-gate
65870Sstevel@tonic-gate /* Wake up any waiters. */
65880Sstevel@tonic-gate if (flags & SQ_WANTWAKEUP) {
65890Sstevel@tonic-gate flags &= ~SQ_WANTWAKEUP;
65900Sstevel@tonic-gate cv_broadcast(&sq->sq_wait);
65910Sstevel@tonic-gate }
65920Sstevel@tonic-gate if (flags & SQ_WANTEXWAKEUP) {
65930Sstevel@tonic-gate flags &= ~SQ_WANTEXWAKEUP;
65940Sstevel@tonic-gate cv_broadcast(&sq->sq_exitwait);
65950Sstevel@tonic-gate }
65960Sstevel@tonic-gate sq->sq_flags = flags;
65970Sstevel@tonic-gate
65980Sstevel@tonic-gate ASSERT(sq->sq_count != 0);
65990Sstevel@tonic-gate /* Release our claim. */
66000Sstevel@tonic-gate sq->sq_count--;
66010Sstevel@tonic-gate
66020Sstevel@tonic-gate if (bg_service) {
66030Sstevel@tonic-gate ASSERT(sq->sq_servcount != 0);
66040Sstevel@tonic-gate sq->sq_servcount--;
66050Sstevel@tonic-gate }
66060Sstevel@tonic-gate
66070Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
66080Sstevel@tonic-gate
66090Sstevel@tonic-gate TRACE_1(TR_FAC_STREAMS_FR, TR_DRAIN_SYNCQ_END,
66105753Sgww "drain_syncq end:%p", sq);
66110Sstevel@tonic-gate }
66120Sstevel@tonic-gate
66130Sstevel@tonic-gate
66140Sstevel@tonic-gate /*
66150Sstevel@tonic-gate *
66160Sstevel@tonic-gate * qdrain_syncq can be called (currently) from only one of two places:
66170Sstevel@tonic-gate * drain_syncq
66180Sstevel@tonic-gate * putnext (or some variation of it).
66190Sstevel@tonic-gate * and eventually
66200Sstevel@tonic-gate * qwait(_sig)
66210Sstevel@tonic-gate *
66229671SBrian.Ruthven@Sun.COM * If called from drain_syncq, we found it in the list of queues needing
66239671SBrian.Ruthven@Sun.COM * service, so there is work to be done (or it wouldn't be in the list).
66240Sstevel@tonic-gate *
66250Sstevel@tonic-gate * If called from some putnext variation, it was because the
66269671SBrian.Ruthven@Sun.COM * perimeter is open, but messages are blocking a putnext and
66270Sstevel@tonic-gate * there is not a thread working on it. Now a thread could start
66280Sstevel@tonic-gate * working on it while we are getting ready to do so ourself, but
66290Sstevel@tonic-gate * the thread would set the q_draining flag, and we can spin out.
66300Sstevel@tonic-gate *
66310Sstevel@tonic-gate * As for qwait(_sig), I think I shall let it continue to call
66320Sstevel@tonic-gate * drain_syncq directly (after all, it will get here eventually).
66330Sstevel@tonic-gate *
66340Sstevel@tonic-gate * qdrain_syncq has to terminate when:
66350Sstevel@tonic-gate * - one of the SQ_STAYAWAY bits gets set to preserve qwriter(OUTER) ordering
66360Sstevel@tonic-gate * - SQ_EVENTS gets set to preserve qwriter(INNER) ordering
66370Sstevel@tonic-gate *
66380Sstevel@tonic-gate * ASSUMES:
66390Sstevel@tonic-gate * One claim
66400Sstevel@tonic-gate * QLOCK held
66410Sstevel@tonic-gate * SQLOCK not held
66420Sstevel@tonic-gate * Will release QLOCK before returning
66430Sstevel@tonic-gate */
66440Sstevel@tonic-gate void
qdrain_syncq(syncq_t * sq,queue_t * q)66450Sstevel@tonic-gate qdrain_syncq(syncq_t *sq, queue_t *q)
66460Sstevel@tonic-gate {
66470Sstevel@tonic-gate mblk_t *bp;
66480Sstevel@tonic-gate #ifdef DEBUG
66490Sstevel@tonic-gate uint16_t count;
66500Sstevel@tonic-gate #endif
66510Sstevel@tonic-gate
66520Sstevel@tonic-gate TRACE_1(TR_FAC_STREAMS_FR, TR_DRAIN_SYNCQ_START,
66535753Sgww "drain_syncq start:%p", sq);
66540Sstevel@tonic-gate ASSERT(q->q_syncq == sq);
66550Sstevel@tonic-gate ASSERT(MUTEX_HELD(QLOCK(q)));
66560Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(SQLOCK(sq)));
66570Sstevel@tonic-gate /*
66589671SBrian.Ruthven@Sun.COM * For non-CIPUT perimeters, we should be called with the exclusive bit
66599671SBrian.Ruthven@Sun.COM * set already. For CIPUT perimeters, we will be doing a concurrent
66609671SBrian.Ruthven@Sun.COM * drain, so it better not be set.
66610Sstevel@tonic-gate */
66620Sstevel@tonic-gate ASSERT((sq->sq_flags & (SQ_EXCL|SQ_CIPUT)));
66630Sstevel@tonic-gate ASSERT(!((sq->sq_type & SQ_CIPUT) && (sq->sq_flags & SQ_EXCL)));
66640Sstevel@tonic-gate ASSERT((sq->sq_type & SQ_CIPUT) || (sq->sq_flags & SQ_EXCL));
66650Sstevel@tonic-gate /*
66660Sstevel@tonic-gate * All outer pointers are set, or none of them are
66670Sstevel@tonic-gate */
66680Sstevel@tonic-gate ASSERT((sq->sq_outer == NULL && sq->sq_onext == NULL &&
66695753Sgww sq->sq_oprev == NULL) ||
66705753Sgww (sq->sq_outer != NULL && sq->sq_onext != NULL &&
66715753Sgww sq->sq_oprev != NULL));
66720Sstevel@tonic-gate #ifdef DEBUG
66730Sstevel@tonic-gate count = sq->sq_count;
66740Sstevel@tonic-gate /*
66750Sstevel@tonic-gate * This is OK without the putlocks, because we have one
66760Sstevel@tonic-gate * claim either from the sq_count, or a putcount. We could
66770Sstevel@tonic-gate * get an erroneous value from other counts, but ours won't
66780Sstevel@tonic-gate * change, so one way or another, we will have at least a
66790Sstevel@tonic-gate * value of one.
66800Sstevel@tonic-gate */
66810Sstevel@tonic-gate SUM_SQ_PUTCOUNTS(sq, count);
66820Sstevel@tonic-gate ASSERT(count >= 1);
66830Sstevel@tonic-gate #endif /* DEBUG */
66840Sstevel@tonic-gate
66850Sstevel@tonic-gate /*
66869671SBrian.Ruthven@Sun.COM * The first thing to do is find out if a thread is already draining
66879671SBrian.Ruthven@Sun.COM * this queue. If so, we are done, just return.
66889671SBrian.Ruthven@Sun.COM */
66899671SBrian.Ruthven@Sun.COM if (q->q_draining) {
66900Sstevel@tonic-gate mutex_exit(QLOCK(q));
66910Sstevel@tonic-gate return;
66920Sstevel@tonic-gate }
66930Sstevel@tonic-gate
66940Sstevel@tonic-gate /*
66959671SBrian.Ruthven@Sun.COM * If the perimeter is exclusive, there is nothing we can do right now,
66969671SBrian.Ruthven@Sun.COM * go away. Note that there is nothing to prevent this case from
66979671SBrian.Ruthven@Sun.COM * changing right after this check, but the spin-out will catch it.
66980Sstevel@tonic-gate */
66990Sstevel@tonic-gate
67000Sstevel@tonic-gate /* Tell other threads that we are draining this queue */
67010Sstevel@tonic-gate q->q_draining = 1; /* Protected by QLOCK */
67020Sstevel@tonic-gate
67039671SBrian.Ruthven@Sun.COM /*
67049671SBrian.Ruthven@Sun.COM * If there is nothing to do, clear QFULL as necessary. This caters for
67059671SBrian.Ruthven@Sun.COM * the case where an empty queue was enqueued onto the syncq.
67069671SBrian.Ruthven@Sun.COM */
67079671SBrian.Ruthven@Sun.COM if (q->q_sqhead == NULL) {
67089671SBrian.Ruthven@Sun.COM ASSERT(q->q_syncqmsgs == 0);
67099671SBrian.Ruthven@Sun.COM mutex_exit(QLOCK(q));
67109671SBrian.Ruthven@Sun.COM clr_qfull(q);
67119671SBrian.Ruthven@Sun.COM mutex_enter(QLOCK(q));
67129671SBrian.Ruthven@Sun.COM }
67139671SBrian.Ruthven@Sun.COM
67149671SBrian.Ruthven@Sun.COM /*
67159671SBrian.Ruthven@Sun.COM * Note that q_sqhead must be re-checked here in case another message
67169671SBrian.Ruthven@Sun.COM * was enqueued whilst QLOCK was dropped during the call to clr_qfull.
67179671SBrian.Ruthven@Sun.COM */
67180Sstevel@tonic-gate for (bp = q->q_sqhead; bp != NULL; bp = q->q_sqhead) {
67190Sstevel@tonic-gate /*
67209671SBrian.Ruthven@Sun.COM * Because we can enter this routine just because a putnext is
67219671SBrian.Ruthven@Sun.COM * blocked, we need to spin out if the perimeter wants to go
67229671SBrian.Ruthven@Sun.COM * exclusive as well as just blocked. We need to spin out also
67239671SBrian.Ruthven@Sun.COM * if events are queued on the syncq.
67249671SBrian.Ruthven@Sun.COM * Don't check for SQ_EXCL, because non-CIPUT perimeters would
67259671SBrian.Ruthven@Sun.COM * set it, and it can't become exclusive while we hold a claim.
67260Sstevel@tonic-gate */
67270Sstevel@tonic-gate if (sq->sq_flags & (SQ_STAYAWAY | SQ_EVENTS)) {
67280Sstevel@tonic-gate break;
67290Sstevel@tonic-gate }
67300Sstevel@tonic-gate
67310Sstevel@tonic-gate #ifdef DEBUG
67320Sstevel@tonic-gate /*
67330Sstevel@tonic-gate * Since we are in qdrain_syncq, we already know the queue,
67340Sstevel@tonic-gate * but for sanity, we want to check this against the qp that
67350Sstevel@tonic-gate * was passed in by bp->b_queue.
67360Sstevel@tonic-gate */
67370Sstevel@tonic-gate
67380Sstevel@tonic-gate ASSERT(bp->b_queue == q);
67390Sstevel@tonic-gate ASSERT(bp->b_queue->q_syncq == sq);
67400Sstevel@tonic-gate bp->b_queue = NULL;
67410Sstevel@tonic-gate
67420Sstevel@tonic-gate /*
67430Sstevel@tonic-gate * We would have the following check in the DEBUG code:
67440Sstevel@tonic-gate *
67450Sstevel@tonic-gate * if (bp->b_prev != NULL) {
67460Sstevel@tonic-gate * ASSERT(bp->b_prev == (void (*)())q->q_qinfo->qi_putp);
67470Sstevel@tonic-gate * }
67480Sstevel@tonic-gate *
67490Sstevel@tonic-gate * This can't be done, however, since IP modifies qinfo
67500Sstevel@tonic-gate * structure at run-time (switching between IPv4 qinfo and IPv6
67510Sstevel@tonic-gate * qinfo), invalidating the check.
67520Sstevel@tonic-gate * So the assignment to func is left here, but the ASSERT itself
67530Sstevel@tonic-gate * is removed until the whole issue is resolved.
67540Sstevel@tonic-gate */
67550Sstevel@tonic-gate #endif
67560Sstevel@tonic-gate ASSERT(q->q_sqhead == bp);
67570Sstevel@tonic-gate q->q_sqhead = bp->b_next;
67580Sstevel@tonic-gate bp->b_prev = bp->b_next = NULL;
67590Sstevel@tonic-gate ASSERT(q->q_syncqmsgs > 0);
67600Sstevel@tonic-gate mutex_exit(QLOCK(q));
67610Sstevel@tonic-gate
67620Sstevel@tonic-gate ASSERT(bp->b_datap->db_ref != 0);
67630Sstevel@tonic-gate
67640Sstevel@tonic-gate (void) (*q->q_qinfo->qi_putp)(q, bp);
67650Sstevel@tonic-gate
67660Sstevel@tonic-gate mutex_enter(QLOCK(q));
67670Sstevel@tonic-gate
67680Sstevel@tonic-gate /*
67699671SBrian.Ruthven@Sun.COM * q_syncqmsgs should only be decremented after executing the
67709671SBrian.Ruthven@Sun.COM * put procedure to avoid message re-ordering. This is due to an
67719671SBrian.Ruthven@Sun.COM * optimisation in putnext() which can call the put procedure
67729671SBrian.Ruthven@Sun.COM * directly if it sees q_syncqmsgs == 0 (despite Q_SQQUEUED
67739671SBrian.Ruthven@Sun.COM * being set).
67740Sstevel@tonic-gate *
67759671SBrian.Ruthven@Sun.COM * We also need to clear QFULL in the next service procedure
67769671SBrian.Ruthven@Sun.COM * queue if this is the last message destined for that queue.
67779671SBrian.Ruthven@Sun.COM *
67789671SBrian.Ruthven@Sun.COM * It would make better sense to have some sort of tunable for
67799671SBrian.Ruthven@Sun.COM * the low water mark, but these semantics are not yet defined.
67809671SBrian.Ruthven@Sun.COM * So, alas, we use a constant.
67810Sstevel@tonic-gate */
67829671SBrian.Ruthven@Sun.COM if (--q->q_syncqmsgs == 0) {
67839671SBrian.Ruthven@Sun.COM mutex_exit(QLOCK(q));
67840Sstevel@tonic-gate clr_qfull(q);
67859671SBrian.Ruthven@Sun.COM mutex_enter(QLOCK(q));
67869671SBrian.Ruthven@Sun.COM }
67879671SBrian.Ruthven@Sun.COM
67880Sstevel@tonic-gate /*
67890Sstevel@tonic-gate * Always clear SQ_EXCL when CIPUT in order to handle
67909671SBrian.Ruthven@Sun.COM * qwriter(INNER). The putp() can call qwriter and get exclusive
67919671SBrian.Ruthven@Sun.COM * access IFF this is the only claim. So, we need to test for
67929671SBrian.Ruthven@Sun.COM * this possibility, acquire the mutex and clear the bit.
67930Sstevel@tonic-gate */
67940Sstevel@tonic-gate if ((sq->sq_type & SQ_CIPUT) && (sq->sq_flags & SQ_EXCL)) {
67950Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
67960Sstevel@tonic-gate sq->sq_flags &= ~SQ_EXCL;
67970Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
67980Sstevel@tonic-gate }
67990Sstevel@tonic-gate }
68000Sstevel@tonic-gate
68010Sstevel@tonic-gate /*
68029671SBrian.Ruthven@Sun.COM * We should either have no messages on this queue, or we were told to
68039671SBrian.Ruthven@Sun.COM * goaway by a waiter (which we will wake up at the end of this
68049671SBrian.Ruthven@Sun.COM * function).
68050Sstevel@tonic-gate */
68060Sstevel@tonic-gate ASSERT((q->q_sqhead == NULL) ||
68070Sstevel@tonic-gate (sq->sq_flags & (SQ_STAYAWAY | SQ_EVENTS)));
68080Sstevel@tonic-gate
68090Sstevel@tonic-gate ASSERT(MUTEX_HELD(QLOCK(q)));
68100Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(SQLOCK(sq)));
68110Sstevel@tonic-gate
68129671SBrian.Ruthven@Sun.COM /* Remove the q from the syncq list if all the messages are drained. */
68130Sstevel@tonic-gate if (q->q_sqhead == NULL) {
68149671SBrian.Ruthven@Sun.COM ASSERT(q->q_syncqmsgs == 0);
68150Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
68160Sstevel@tonic-gate if (q->q_sqflags & Q_SQQUEUED)
68170Sstevel@tonic-gate SQRM_Q(sq, q);
68180Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
68190Sstevel@tonic-gate /*
68200Sstevel@tonic-gate * Since the queue is removed from the list, reset its priority.
68210Sstevel@tonic-gate */
68220Sstevel@tonic-gate q->q_spri = 0;
68230Sstevel@tonic-gate }
68240Sstevel@tonic-gate
68250Sstevel@tonic-gate /*
68269671SBrian.Ruthven@Sun.COM * Remember, the q_draining flag is used to let another thread know
68279671SBrian.Ruthven@Sun.COM * that there is a thread currently draining the messages for a queue.
68289671SBrian.Ruthven@Sun.COM * Since we are now done with this queue (even if there may be messages
68299671SBrian.Ruthven@Sun.COM * still there), we need to clear this flag so some thread will work on
68309671SBrian.Ruthven@Sun.COM * it if needed.
68310Sstevel@tonic-gate */
68320Sstevel@tonic-gate ASSERT(q->q_draining);
68330Sstevel@tonic-gate q->q_draining = 0;
68340Sstevel@tonic-gate
68359671SBrian.Ruthven@Sun.COM /* Called with a claim, so OK to drop all locks. */
68360Sstevel@tonic-gate mutex_exit(QLOCK(q));
68370Sstevel@tonic-gate
68380Sstevel@tonic-gate TRACE_1(TR_FAC_STREAMS_FR, TR_DRAIN_SYNCQ_END,
68395753Sgww "drain_syncq end:%p", sq);
68400Sstevel@tonic-gate }
68410Sstevel@tonic-gate /* END OF QDRAIN_SYNCQ */
68420Sstevel@tonic-gate
68430Sstevel@tonic-gate
68440Sstevel@tonic-gate /*
68459671SBrian.Ruthven@Sun.COM * This is the mate to qdrain_syncq, except that it is putting the message onto
68469671SBrian.Ruthven@Sun.COM * the queue instead of draining. Since the message is destined for the queue
68479671SBrian.Ruthven@Sun.COM * that is selected, there is no need to identify the function because the
68489671SBrian.Ruthven@Sun.COM * message is intended for the put routine for the queue. For debug kernels,
68499671SBrian.Ruthven@Sun.COM * this routine will do it anyway just in case.
68500Sstevel@tonic-gate *
68510Sstevel@tonic-gate * After the message is enqueued on the syncq, it calls putnext_tail()
68520Sstevel@tonic-gate * which will schedule a background thread to actually process the message.
68530Sstevel@tonic-gate *
68540Sstevel@tonic-gate * Assumes that there is a claim on the syncq (sq->sq_count > 0) and
68550Sstevel@tonic-gate * SQLOCK(sq) and QLOCK(q) are not held.
68560Sstevel@tonic-gate */
68570Sstevel@tonic-gate void
qfill_syncq(syncq_t * sq,queue_t * q,mblk_t * mp)68580Sstevel@tonic-gate qfill_syncq(syncq_t *sq, queue_t *q, mblk_t *mp)
68590Sstevel@tonic-gate {
68600Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(SQLOCK(sq)));
68610Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(QLOCK(q)));
68620Sstevel@tonic-gate ASSERT(sq->sq_count > 0);
68630Sstevel@tonic-gate ASSERT(q->q_syncq == sq);
68640Sstevel@tonic-gate ASSERT((sq->sq_outer == NULL && sq->sq_onext == NULL &&
68655753Sgww sq->sq_oprev == NULL) ||
68665753Sgww (sq->sq_outer != NULL && sq->sq_onext != NULL &&
68675753Sgww sq->sq_oprev != NULL));
68680Sstevel@tonic-gate
68690Sstevel@tonic-gate mutex_enter(QLOCK(q));
68700Sstevel@tonic-gate
68710Sstevel@tonic-gate #ifdef DEBUG
68720Sstevel@tonic-gate /*
68730Sstevel@tonic-gate * This is used for debug in the qfill_syncq/qdrain_syncq case
68740Sstevel@tonic-gate * to trace the queue that the message is intended for. Note
68750Sstevel@tonic-gate * that the original use was to identify the queue and function
68760Sstevel@tonic-gate * to call on the drain. In the new syncq, we have the context
68770Sstevel@tonic-gate * of the queue that we are draining, so call it's putproc and
68780Sstevel@tonic-gate * don't rely on the saved values. But for debug this is still
68799671SBrian.Ruthven@Sun.COM * useful information.
68800Sstevel@tonic-gate */
68810Sstevel@tonic-gate mp->b_prev = (mblk_t *)q->q_qinfo->qi_putp;
68820Sstevel@tonic-gate mp->b_queue = q;
68830Sstevel@tonic-gate mp->b_next = NULL;
68840Sstevel@tonic-gate #endif
68850Sstevel@tonic-gate ASSERT(q->q_syncq == sq);
68860Sstevel@tonic-gate /*
68870Sstevel@tonic-gate * Enqueue the message on the list.
68880Sstevel@tonic-gate * SQPUT_MP() accesses q_syncqmsgs. We are already holding QLOCK to
68899671SBrian.Ruthven@Sun.COM * protect it. So it's ok to acquire SQLOCK after SQPUT_MP().
68900Sstevel@tonic-gate */
68910Sstevel@tonic-gate SQPUT_MP(q, mp);
68920Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
68930Sstevel@tonic-gate
68940Sstevel@tonic-gate /*
68950Sstevel@tonic-gate * And queue on syncq for scheduling, if not already queued.
68960Sstevel@tonic-gate * Note that we need the SQLOCK for this, and for testing flags
68970Sstevel@tonic-gate * at the end to see if we will drain. So grab it now, and
68980Sstevel@tonic-gate * release it before we call qdrain_syncq or return.
68990Sstevel@tonic-gate */
69000Sstevel@tonic-gate if (!(q->q_sqflags & Q_SQQUEUED)) {
69010Sstevel@tonic-gate q->q_spri = curthread->t_pri;
69020Sstevel@tonic-gate SQPUT_Q(sq, q);
69030Sstevel@tonic-gate }
69040Sstevel@tonic-gate #ifdef DEBUG
69050Sstevel@tonic-gate else {
69060Sstevel@tonic-gate /*
69070Sstevel@tonic-gate * All of these conditions MUST be true!
69080Sstevel@tonic-gate */
69090Sstevel@tonic-gate ASSERT(sq->sq_tail != NULL);
69100Sstevel@tonic-gate if (sq->sq_tail == sq->sq_head) {
69110Sstevel@tonic-gate ASSERT((q->q_sqprev == NULL) &&
69120Sstevel@tonic-gate (q->q_sqnext == NULL));
69130Sstevel@tonic-gate } else {
69140Sstevel@tonic-gate ASSERT((q->q_sqprev != NULL) ||
69150Sstevel@tonic-gate (q->q_sqnext != NULL));
69160Sstevel@tonic-gate }
69170Sstevel@tonic-gate ASSERT(sq->sq_flags & SQ_QUEUED);
69180Sstevel@tonic-gate ASSERT(q->q_syncqmsgs != 0);
69190Sstevel@tonic-gate ASSERT(q->q_sqflags & Q_SQQUEUED);
69200Sstevel@tonic-gate }
69210Sstevel@tonic-gate #endif
69220Sstevel@tonic-gate mutex_exit(QLOCK(q));
69230Sstevel@tonic-gate /*
69240Sstevel@tonic-gate * SQLOCK is still held, so sq_count can be safely decremented.
69250Sstevel@tonic-gate */
69260Sstevel@tonic-gate sq->sq_count--;
69270Sstevel@tonic-gate
69280Sstevel@tonic-gate putnext_tail(sq, q, 0);
69290Sstevel@tonic-gate /* Should not reference sq or q after this point. */
69300Sstevel@tonic-gate }
69310Sstevel@tonic-gate
69320Sstevel@tonic-gate /* End of qfill_syncq */
69330Sstevel@tonic-gate
69340Sstevel@tonic-gate /*
69350Sstevel@tonic-gate * Remove all messages from a syncq (if qp is NULL) or remove all messages
69360Sstevel@tonic-gate * that would be put into qp by drain_syncq.
69370Sstevel@tonic-gate * Used when deleting the syncq (qp == NULL) or when detaching
69380Sstevel@tonic-gate * a queue (qp != NULL).
69390Sstevel@tonic-gate * Return non-zero if one or more messages were freed.
69400Sstevel@tonic-gate *
69419671SBrian.Ruthven@Sun.COM * No need to grab sq_putlocks here. See comment in strsubr.h that explains when
69420Sstevel@tonic-gate * sq_putlocks are used.
69430Sstevel@tonic-gate *
69440Sstevel@tonic-gate * NOTE: This function assumes that it is called from the close() context and
69459671SBrian.Ruthven@Sun.COM * that all the queues in the syncq are going away. For this reason it doesn't
69460Sstevel@tonic-gate * acquire QLOCK for modifying q_sqhead/q_sqtail fields. This assumption is
69470Sstevel@tonic-gate * currently valid, but it is useful to rethink this function to behave properly
69480Sstevel@tonic-gate * in other cases.
69490Sstevel@tonic-gate */
69500Sstevel@tonic-gate int
flush_syncq(syncq_t * sq,queue_t * qp)69510Sstevel@tonic-gate flush_syncq(syncq_t *sq, queue_t *qp)
69520Sstevel@tonic-gate {
69530Sstevel@tonic-gate mblk_t *bp, *mp_head, *mp_next, *mp_prev;
69540Sstevel@tonic-gate queue_t *q;
69550Sstevel@tonic-gate int ret = 0;
69560Sstevel@tonic-gate
69570Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
69580Sstevel@tonic-gate
69590Sstevel@tonic-gate /*
69600Sstevel@tonic-gate * Before we leave, we need to make sure there are no
69610Sstevel@tonic-gate * events listed for this queue. All events for this queue
69620Sstevel@tonic-gate * will just be freed.
69630Sstevel@tonic-gate */
69640Sstevel@tonic-gate if (qp != NULL && sq->sq_evhead != NULL) {
69650Sstevel@tonic-gate ASSERT(sq->sq_flags & SQ_EVENTS);
69660Sstevel@tonic-gate
69670Sstevel@tonic-gate mp_prev = NULL;
69680Sstevel@tonic-gate for (bp = sq->sq_evhead; bp != NULL; bp = mp_next) {
69690Sstevel@tonic-gate mp_next = bp->b_next;
69700Sstevel@tonic-gate if (bp->b_queue == qp) {
69710Sstevel@tonic-gate /* Delete this message */
69720Sstevel@tonic-gate if (mp_prev != NULL) {
69730Sstevel@tonic-gate mp_prev->b_next = mp_next;
69740Sstevel@tonic-gate /*
69750Sstevel@tonic-gate * Update sq_evtail if the last element
69760Sstevel@tonic-gate * is removed.
69770Sstevel@tonic-gate */
69780Sstevel@tonic-gate if (bp == sq->sq_evtail) {
69790Sstevel@tonic-gate ASSERT(mp_next == NULL);
69800Sstevel@tonic-gate sq->sq_evtail = mp_prev;
69810Sstevel@tonic-gate }
69820Sstevel@tonic-gate } else
69830Sstevel@tonic-gate sq->sq_evhead = mp_next;
69840Sstevel@tonic-gate if (sq->sq_evhead == NULL)
69850Sstevel@tonic-gate sq->sq_flags &= ~SQ_EVENTS;
69860Sstevel@tonic-gate bp->b_prev = bp->b_next = NULL;
69870Sstevel@tonic-gate freemsg(bp);
69880Sstevel@tonic-gate ret++;
69890Sstevel@tonic-gate } else {
69900Sstevel@tonic-gate mp_prev = bp;
69910Sstevel@tonic-gate }
69920Sstevel@tonic-gate }
69930Sstevel@tonic-gate }
69940Sstevel@tonic-gate
69950Sstevel@tonic-gate /*
69960Sstevel@tonic-gate * Walk sq_head and:
69970Sstevel@tonic-gate * - match qp if qp is set, remove it's messages
69980Sstevel@tonic-gate * - all if qp is not set
69990Sstevel@tonic-gate */
70000Sstevel@tonic-gate q = sq->sq_head;
70010Sstevel@tonic-gate while (q != NULL) {
70020Sstevel@tonic-gate ASSERT(q->q_syncq == sq);
70030Sstevel@tonic-gate if ((qp == NULL) || (qp == q)) {
70040Sstevel@tonic-gate /*
70050Sstevel@tonic-gate * Yank the messages as a list off the queue
70060Sstevel@tonic-gate */
70070Sstevel@tonic-gate mp_head = q->q_sqhead;
70080Sstevel@tonic-gate /*
70090Sstevel@tonic-gate * We do not have QLOCK(q) here (which is safe due to
70100Sstevel@tonic-gate * assumptions mentioned above). To obtain the lock we
70110Sstevel@tonic-gate * need to release SQLOCK which may allow lots of things
70120Sstevel@tonic-gate * to change upon us. This place requires more analysis.
70130Sstevel@tonic-gate */
70140Sstevel@tonic-gate q->q_sqhead = q->q_sqtail = NULL;
70150Sstevel@tonic-gate ASSERT(mp_head->b_queue &&
70160Sstevel@tonic-gate mp_head->b_queue->q_syncq == sq);
70170Sstevel@tonic-gate
70180Sstevel@tonic-gate /*
70190Sstevel@tonic-gate * Free each of the messages.
70200Sstevel@tonic-gate */
70210Sstevel@tonic-gate for (bp = mp_head; bp != NULL; bp = mp_next) {
70220Sstevel@tonic-gate mp_next = bp->b_next;
70230Sstevel@tonic-gate bp->b_prev = bp->b_next = NULL;
70240Sstevel@tonic-gate freemsg(bp);
70250Sstevel@tonic-gate ret++;
70260Sstevel@tonic-gate }
70270Sstevel@tonic-gate /*
70280Sstevel@tonic-gate * Now remove the queue from the syncq.
70290Sstevel@tonic-gate */
70300Sstevel@tonic-gate ASSERT(q->q_sqflags & Q_SQQUEUED);
70310Sstevel@tonic-gate SQRM_Q(sq, q);
70320Sstevel@tonic-gate q->q_spri = 0;
70330Sstevel@tonic-gate q->q_syncqmsgs = 0;
70340Sstevel@tonic-gate
70350Sstevel@tonic-gate /*
70360Sstevel@tonic-gate * If qp was specified, we are done with it and are
70370Sstevel@tonic-gate * going to drop SQLOCK(sq) and return. We wakeup syncq
70380Sstevel@tonic-gate * waiters while we still have the SQLOCK.
70390Sstevel@tonic-gate */
70400Sstevel@tonic-gate if ((qp != NULL) && (sq->sq_flags & SQ_WANTWAKEUP)) {
70410Sstevel@tonic-gate sq->sq_flags &= ~SQ_WANTWAKEUP;
70420Sstevel@tonic-gate cv_broadcast(&sq->sq_wait);
70430Sstevel@tonic-gate }
70440Sstevel@tonic-gate /* Drop SQLOCK across clr_qfull */
70450Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
70460Sstevel@tonic-gate
70470Sstevel@tonic-gate /*
70480Sstevel@tonic-gate * We avoid doing the test that drain_syncq does and
70490Sstevel@tonic-gate * unconditionally clear qfull for every flushed
70500Sstevel@tonic-gate * message. Since flush_syncq is only called during
70510Sstevel@tonic-gate * close this should not be a problem.
70520Sstevel@tonic-gate */
70530Sstevel@tonic-gate clr_qfull(q);
70540Sstevel@tonic-gate if (qp != NULL) {
70550Sstevel@tonic-gate return (ret);
70560Sstevel@tonic-gate } else {
70570Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
70580Sstevel@tonic-gate /*
70590Sstevel@tonic-gate * The head was removed by SQRM_Q above.
70600Sstevel@tonic-gate * reread the new head and flush it.
70610Sstevel@tonic-gate */
70620Sstevel@tonic-gate q = sq->sq_head;
70630Sstevel@tonic-gate }
70640Sstevel@tonic-gate } else {
70650Sstevel@tonic-gate q = q->q_sqnext;
70660Sstevel@tonic-gate }
70670Sstevel@tonic-gate ASSERT(MUTEX_HELD(SQLOCK(sq)));
70680Sstevel@tonic-gate }
70690Sstevel@tonic-gate
70700Sstevel@tonic-gate if (sq->sq_flags & SQ_WANTWAKEUP) {
70710Sstevel@tonic-gate sq->sq_flags &= ~SQ_WANTWAKEUP;
70720Sstevel@tonic-gate cv_broadcast(&sq->sq_wait);
70730Sstevel@tonic-gate }
70740Sstevel@tonic-gate
70750Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
70760Sstevel@tonic-gate return (ret);
70770Sstevel@tonic-gate }
70780Sstevel@tonic-gate
70790Sstevel@tonic-gate /*
70800Sstevel@tonic-gate * Propagate all messages from a syncq to the next syncq that are associated
70810Sstevel@tonic-gate * with the specified queue. If the queue is attached to a driver or if the
70820Sstevel@tonic-gate * messages have been added due to a qwriter(PERIM_INNER), free the messages.
70830Sstevel@tonic-gate *
70840Sstevel@tonic-gate * Assumes that the stream is strlock()'ed. We don't come here if there
70850Sstevel@tonic-gate * are no messages to propagate.
70860Sstevel@tonic-gate *
70870Sstevel@tonic-gate * NOTE : If the queue is attached to a driver, all the messages are freed
70880Sstevel@tonic-gate * as there is no point in propagating the messages from the driver syncq
70890Sstevel@tonic-gate * to the closing stream head which will in turn get freed later.
70900Sstevel@tonic-gate */
70910Sstevel@tonic-gate static int
propagate_syncq(queue_t * qp)70920Sstevel@tonic-gate propagate_syncq(queue_t *qp)
70930Sstevel@tonic-gate {
70940Sstevel@tonic-gate mblk_t *bp, *head, *tail, *prev, *next;
70950Sstevel@tonic-gate syncq_t *sq;
70960Sstevel@tonic-gate queue_t *nqp;
70970Sstevel@tonic-gate syncq_t *nsq;
70980Sstevel@tonic-gate boolean_t isdriver;
70990Sstevel@tonic-gate int moved = 0;
71000Sstevel@tonic-gate uint16_t flags;
71010Sstevel@tonic-gate pri_t priority = curthread->t_pri;
71020Sstevel@tonic-gate #ifdef DEBUG
71030Sstevel@tonic-gate void (*func)();
71040Sstevel@tonic-gate #endif
71050Sstevel@tonic-gate
71060Sstevel@tonic-gate sq = qp->q_syncq;
71070Sstevel@tonic-gate ASSERT(MUTEX_HELD(SQLOCK(sq)));
71080Sstevel@tonic-gate /* debug macro */
71090Sstevel@tonic-gate SQ_PUTLOCKS_HELD(sq);
71100Sstevel@tonic-gate /*
71110Sstevel@tonic-gate * As entersq() does not increment the sq_count for
71120Sstevel@tonic-gate * the write side, check sq_count for non-QPERQ
71130Sstevel@tonic-gate * perimeters alone.
71140Sstevel@tonic-gate */
71150Sstevel@tonic-gate ASSERT((qp->q_flag & QPERQ) || (sq->sq_count >= 1));
71160Sstevel@tonic-gate
71170Sstevel@tonic-gate /*
71180Sstevel@tonic-gate * propagate_syncq() can be called because of either messages on the
71190Sstevel@tonic-gate * queue syncq or because on events on the queue syncq. Do actual
71200Sstevel@tonic-gate * message propagations if there are any messages.
71210Sstevel@tonic-gate */
71220Sstevel@tonic-gate if (qp->q_syncqmsgs) {
71230Sstevel@tonic-gate isdriver = (qp->q_flag & QISDRV);
71240Sstevel@tonic-gate
71250Sstevel@tonic-gate if (!isdriver) {
71260Sstevel@tonic-gate nqp = qp->q_next;
71270Sstevel@tonic-gate nsq = nqp->q_syncq;
71280Sstevel@tonic-gate ASSERT(MUTEX_HELD(SQLOCK(nsq)));
71290Sstevel@tonic-gate /* debug macro */
71300Sstevel@tonic-gate SQ_PUTLOCKS_HELD(nsq);
71310Sstevel@tonic-gate #ifdef DEBUG
71320Sstevel@tonic-gate func = (void (*)())nqp->q_qinfo->qi_putp;
71330Sstevel@tonic-gate #endif
71340Sstevel@tonic-gate }
71350Sstevel@tonic-gate
71360Sstevel@tonic-gate SQRM_Q(sq, qp);
71370Sstevel@tonic-gate priority = MAX(qp->q_spri, priority);
71380Sstevel@tonic-gate qp->q_spri = 0;
71390Sstevel@tonic-gate head = qp->q_sqhead;
71400Sstevel@tonic-gate tail = qp->q_sqtail;
71410Sstevel@tonic-gate qp->q_sqhead = qp->q_sqtail = NULL;
71420Sstevel@tonic-gate qp->q_syncqmsgs = 0;
71430Sstevel@tonic-gate
71440Sstevel@tonic-gate /*
71450Sstevel@tonic-gate * Walk the list of messages, and free them if this is a driver,
71460Sstevel@tonic-gate * otherwise reset the b_prev and b_queue value to the new putp.
71470Sstevel@tonic-gate * Afterward, we will just add the head to the end of the next
71480Sstevel@tonic-gate * syncq, and point the tail to the end of this one.
71490Sstevel@tonic-gate */
71500Sstevel@tonic-gate
71510Sstevel@tonic-gate for (bp = head; bp != NULL; bp = next) {
71520Sstevel@tonic-gate next = bp->b_next;
71530Sstevel@tonic-gate if (isdriver) {
71540Sstevel@tonic-gate bp->b_prev = bp->b_next = NULL;
71550Sstevel@tonic-gate freemsg(bp);
71560Sstevel@tonic-gate continue;
71570Sstevel@tonic-gate }
71580Sstevel@tonic-gate /* Change the q values for this message */
71590Sstevel@tonic-gate bp->b_queue = nqp;
71600Sstevel@tonic-gate #ifdef DEBUG
71610Sstevel@tonic-gate bp->b_prev = (mblk_t *)func;
71620Sstevel@tonic-gate #endif
71630Sstevel@tonic-gate moved++;
71640Sstevel@tonic-gate }
71650Sstevel@tonic-gate /*
71660Sstevel@tonic-gate * Attach list of messages to the end of the new queue (if there
71670Sstevel@tonic-gate * is a list of messages).
71680Sstevel@tonic-gate */
71690Sstevel@tonic-gate
71700Sstevel@tonic-gate if (!isdriver && head != NULL) {
71710Sstevel@tonic-gate ASSERT(tail != NULL);
71720Sstevel@tonic-gate if (nqp->q_sqhead == NULL) {
71730Sstevel@tonic-gate nqp->q_sqhead = head;
71740Sstevel@tonic-gate } else {
71750Sstevel@tonic-gate ASSERT(nqp->q_sqtail != NULL);
71760Sstevel@tonic-gate nqp->q_sqtail->b_next = head;
71770Sstevel@tonic-gate }
71780Sstevel@tonic-gate nqp->q_sqtail = tail;
71790Sstevel@tonic-gate /*
71800Sstevel@tonic-gate * When messages are moved from high priority queue to
71810Sstevel@tonic-gate * another queue, the destination queue priority is
71820Sstevel@tonic-gate * upgraded.
71830Sstevel@tonic-gate */
71840Sstevel@tonic-gate
71850Sstevel@tonic-gate if (priority > nqp->q_spri)
71860Sstevel@tonic-gate nqp->q_spri = priority;
71870Sstevel@tonic-gate
71880Sstevel@tonic-gate SQPUT_Q(nsq, nqp);
71890Sstevel@tonic-gate
71900Sstevel@tonic-gate nqp->q_syncqmsgs += moved;
71910Sstevel@tonic-gate ASSERT(nqp->q_syncqmsgs != 0);
71920Sstevel@tonic-gate }
71930Sstevel@tonic-gate }
71940Sstevel@tonic-gate
71950Sstevel@tonic-gate /*
71960Sstevel@tonic-gate * Before we leave, we need to make sure there are no
71970Sstevel@tonic-gate * events listed for this queue. All events for this queue
71980Sstevel@tonic-gate * will just be freed.
71990Sstevel@tonic-gate */
72000Sstevel@tonic-gate if (sq->sq_evhead != NULL) {
72010Sstevel@tonic-gate ASSERT(sq->sq_flags & SQ_EVENTS);
72020Sstevel@tonic-gate prev = NULL;
72030Sstevel@tonic-gate for (bp = sq->sq_evhead; bp != NULL; bp = next) {
72040Sstevel@tonic-gate next = bp->b_next;
72050Sstevel@tonic-gate if (bp->b_queue == qp) {
72060Sstevel@tonic-gate /* Delete this message */
72070Sstevel@tonic-gate if (prev != NULL) {
72080Sstevel@tonic-gate prev->b_next = next;
72090Sstevel@tonic-gate /*
72100Sstevel@tonic-gate * Update sq_evtail if the last element
72110Sstevel@tonic-gate * is removed.
72120Sstevel@tonic-gate */
72130Sstevel@tonic-gate if (bp == sq->sq_evtail) {
72140Sstevel@tonic-gate ASSERT(next == NULL);
72150Sstevel@tonic-gate sq->sq_evtail = prev;
72160Sstevel@tonic-gate }
72170Sstevel@tonic-gate } else
72180Sstevel@tonic-gate sq->sq_evhead = next;
72190Sstevel@tonic-gate if (sq->sq_evhead == NULL)
72200Sstevel@tonic-gate sq->sq_flags &= ~SQ_EVENTS;
72210Sstevel@tonic-gate bp->b_prev = bp->b_next = NULL;
72220Sstevel@tonic-gate freemsg(bp);
72230Sstevel@tonic-gate } else {
72240Sstevel@tonic-gate prev = bp;
72250Sstevel@tonic-gate }
72260Sstevel@tonic-gate }
72270Sstevel@tonic-gate }
72280Sstevel@tonic-gate
72290Sstevel@tonic-gate flags = sq->sq_flags;
72300Sstevel@tonic-gate
72310Sstevel@tonic-gate /* Wake up any waiter before leaving. */
72320Sstevel@tonic-gate if (flags & SQ_WANTWAKEUP) {
72330Sstevel@tonic-gate flags &= ~SQ_WANTWAKEUP;
72340Sstevel@tonic-gate cv_broadcast(&sq->sq_wait);
72350Sstevel@tonic-gate }
72360Sstevel@tonic-gate sq->sq_flags = flags;
72370Sstevel@tonic-gate
72380Sstevel@tonic-gate return (moved);
72390Sstevel@tonic-gate }
72400Sstevel@tonic-gate
72410Sstevel@tonic-gate /*
72420Sstevel@tonic-gate * Try and upgrade to exclusive access at the inner perimeter. If this can
72430Sstevel@tonic-gate * not be done without blocking then request will be queued on the syncq
72440Sstevel@tonic-gate * and drain_syncq will run it later.
72450Sstevel@tonic-gate *
72460Sstevel@tonic-gate * This routine can only be called from put or service procedures plus
72479671SBrian.Ruthven@Sun.COM * asynchronous callback routines that have properly entered the queue (with
72489671SBrian.Ruthven@Sun.COM * entersq). Thus qwriter_inner assumes the caller has one claim on the syncq
72499671SBrian.Ruthven@Sun.COM * associated with q.
72500Sstevel@tonic-gate */
72510Sstevel@tonic-gate void
qwriter_inner(queue_t * q,mblk_t * mp,void (* func)())72520Sstevel@tonic-gate qwriter_inner(queue_t *q, mblk_t *mp, void (*func)())
72530Sstevel@tonic-gate {
72540Sstevel@tonic-gate syncq_t *sq = q->q_syncq;
72550Sstevel@tonic-gate uint16_t count;
72560Sstevel@tonic-gate
72570Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
72580Sstevel@tonic-gate count = sq->sq_count;
72590Sstevel@tonic-gate SQ_PUTLOCKS_ENTER(sq);
72600Sstevel@tonic-gate SUM_SQ_PUTCOUNTS(sq, count);
72610Sstevel@tonic-gate ASSERT(count >= 1);
72620Sstevel@tonic-gate ASSERT(sq->sq_type & (SQ_CIPUT|SQ_CISVC));
72630Sstevel@tonic-gate
72640Sstevel@tonic-gate if (count == 1) {
72650Sstevel@tonic-gate /*
72660Sstevel@tonic-gate * Can upgrade. This case also handles nested qwriter calls
72670Sstevel@tonic-gate * (when the qwriter callback function calls qwriter). In that
72680Sstevel@tonic-gate * case SQ_EXCL is already set.
72690Sstevel@tonic-gate */
72700Sstevel@tonic-gate sq->sq_flags |= SQ_EXCL;
72710Sstevel@tonic-gate SQ_PUTLOCKS_EXIT(sq);
72720Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
72730Sstevel@tonic-gate (*func)(q, mp);
72740Sstevel@tonic-gate /*
72750Sstevel@tonic-gate * Assumes that leavesq, putnext, and drain_syncq will reset
72760Sstevel@tonic-gate * SQ_EXCL for SQ_CIPUT/SQ_CISVC queues. We leave SQ_EXCL on
72770Sstevel@tonic-gate * until putnext, leavesq, or drain_syncq drops it.
72780Sstevel@tonic-gate * That way we handle nested qwriter(INNER) without dropping
72790Sstevel@tonic-gate * SQ_EXCL until the outermost qwriter callback routine is
72800Sstevel@tonic-gate * done.
72810Sstevel@tonic-gate */
72820Sstevel@tonic-gate return;
72830Sstevel@tonic-gate }
72840Sstevel@tonic-gate SQ_PUTLOCKS_EXIT(sq);
72850Sstevel@tonic-gate sqfill_events(sq, q, mp, func);
72860Sstevel@tonic-gate }
72870Sstevel@tonic-gate
72880Sstevel@tonic-gate /*
72890Sstevel@tonic-gate * Synchronous callback support functions
72900Sstevel@tonic-gate */
72910Sstevel@tonic-gate
72920Sstevel@tonic-gate /*
72930Sstevel@tonic-gate * Allocate a callback parameter structure.
72940Sstevel@tonic-gate * Assumes that caller initializes the flags and the id.
72950Sstevel@tonic-gate * Acquires SQLOCK(sq) if non-NULL is returned.
72960Sstevel@tonic-gate */
72970Sstevel@tonic-gate callbparams_t *
callbparams_alloc(syncq_t * sq,void (* func)(void *),void * arg,int kmflags)72980Sstevel@tonic-gate callbparams_alloc(syncq_t *sq, void (*func)(void *), void *arg, int kmflags)
72990Sstevel@tonic-gate {
73000Sstevel@tonic-gate callbparams_t *cbp;
73010Sstevel@tonic-gate size_t size = sizeof (callbparams_t);
73020Sstevel@tonic-gate
73030Sstevel@tonic-gate cbp = kmem_alloc(size, kmflags & ~KM_PANIC);
73040Sstevel@tonic-gate
73050Sstevel@tonic-gate /*
73060Sstevel@tonic-gate * Only try tryhard allocation if the caller is ready to panic.
73070Sstevel@tonic-gate * Otherwise just fail.
73080Sstevel@tonic-gate */
73090Sstevel@tonic-gate if (cbp == NULL) {
73100Sstevel@tonic-gate if (kmflags & KM_PANIC)
73110Sstevel@tonic-gate cbp = kmem_alloc_tryhard(sizeof (callbparams_t),
73120Sstevel@tonic-gate &size, kmflags);
73130Sstevel@tonic-gate else
73140Sstevel@tonic-gate return (NULL);
73150Sstevel@tonic-gate }
73160Sstevel@tonic-gate
73170Sstevel@tonic-gate ASSERT(size >= sizeof (callbparams_t));
73180Sstevel@tonic-gate cbp->cbp_size = size;
73190Sstevel@tonic-gate cbp->cbp_sq = sq;
73200Sstevel@tonic-gate cbp->cbp_func = func;
73210Sstevel@tonic-gate cbp->cbp_arg = arg;
73220Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
73230Sstevel@tonic-gate cbp->cbp_next = sq->sq_callbpend;
73240Sstevel@tonic-gate sq->sq_callbpend = cbp;
73250Sstevel@tonic-gate return (cbp);
73260Sstevel@tonic-gate }
73270Sstevel@tonic-gate
73280Sstevel@tonic-gate void
callbparams_free(syncq_t * sq,callbparams_t * cbp)73290Sstevel@tonic-gate callbparams_free(syncq_t *sq, callbparams_t *cbp)
73300Sstevel@tonic-gate {
73310Sstevel@tonic-gate callbparams_t **pp, *p;
73320Sstevel@tonic-gate
73330Sstevel@tonic-gate ASSERT(MUTEX_HELD(SQLOCK(sq)));
73340Sstevel@tonic-gate
73350Sstevel@tonic-gate for (pp = &sq->sq_callbpend; (p = *pp) != NULL; pp = &p->cbp_next) {
73360Sstevel@tonic-gate if (p == cbp) {
73370Sstevel@tonic-gate *pp = p->cbp_next;
73380Sstevel@tonic-gate kmem_free(p, p->cbp_size);
73390Sstevel@tonic-gate return;
73400Sstevel@tonic-gate }
73410Sstevel@tonic-gate }
73420Sstevel@tonic-gate (void) (STRLOG(0, 0, 0, SL_CONSOLE,
73430Sstevel@tonic-gate "callbparams_free: not found\n"));
73440Sstevel@tonic-gate }
73450Sstevel@tonic-gate
73460Sstevel@tonic-gate void
callbparams_free_id(syncq_t * sq,callbparams_id_t id,int32_t flag)73470Sstevel@tonic-gate callbparams_free_id(syncq_t *sq, callbparams_id_t id, int32_t flag)
73480Sstevel@tonic-gate {
73490Sstevel@tonic-gate callbparams_t **pp, *p;
73500Sstevel@tonic-gate
73510Sstevel@tonic-gate ASSERT(MUTEX_HELD(SQLOCK(sq)));
73520Sstevel@tonic-gate
73530Sstevel@tonic-gate for (pp = &sq->sq_callbpend; (p = *pp) != NULL; pp = &p->cbp_next) {
73540Sstevel@tonic-gate if (p->cbp_id == id && p->cbp_flags == flag) {
73550Sstevel@tonic-gate *pp = p->cbp_next;
73560Sstevel@tonic-gate kmem_free(p, p->cbp_size);
73570Sstevel@tonic-gate return;
73580Sstevel@tonic-gate }
73590Sstevel@tonic-gate }
73600Sstevel@tonic-gate (void) (STRLOG(0, 0, 0, SL_CONSOLE,
73610Sstevel@tonic-gate "callbparams_free_id: not found\n"));
73620Sstevel@tonic-gate }
73630Sstevel@tonic-gate
73640Sstevel@tonic-gate /*
73650Sstevel@tonic-gate * Callback wrapper function used by once-only callbacks that can be
73660Sstevel@tonic-gate * cancelled (qtimeout and qbufcall)
73670Sstevel@tonic-gate * Contains inline version of entersq(sq, SQ_CALLBACK) that can be
73680Sstevel@tonic-gate * cancelled by the qun* functions.
73690Sstevel@tonic-gate */
73700Sstevel@tonic-gate void
qcallbwrapper(void * arg)73710Sstevel@tonic-gate qcallbwrapper(void *arg)
73720Sstevel@tonic-gate {
73730Sstevel@tonic-gate callbparams_t *cbp = arg;
73740Sstevel@tonic-gate syncq_t *sq;
73750Sstevel@tonic-gate uint16_t count = 0;
73760Sstevel@tonic-gate uint16_t waitflags = SQ_STAYAWAY | SQ_EVENTS | SQ_EXCL;
73770Sstevel@tonic-gate uint16_t type;
73780Sstevel@tonic-gate
73790Sstevel@tonic-gate sq = cbp->cbp_sq;
73800Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
73810Sstevel@tonic-gate type = sq->sq_type;
73820Sstevel@tonic-gate if (!(type & SQ_CICB)) {
73830Sstevel@tonic-gate count = sq->sq_count;
73840Sstevel@tonic-gate SQ_PUTLOCKS_ENTER(sq);
73850Sstevel@tonic-gate SQ_PUTCOUNT_CLRFAST_LOCKED(sq);
73860Sstevel@tonic-gate SUM_SQ_PUTCOUNTS(sq, count);
73870Sstevel@tonic-gate sq->sq_needexcl++;
73880Sstevel@tonic-gate ASSERT(sq->sq_needexcl != 0); /* wraparound */
73890Sstevel@tonic-gate waitflags |= SQ_MESSAGES;
73900Sstevel@tonic-gate }
73919671SBrian.Ruthven@Sun.COM /* Can not handle exclusive entry at outer perimeter */
73920Sstevel@tonic-gate ASSERT(type & SQ_COCB);
73930Sstevel@tonic-gate
73940Sstevel@tonic-gate while ((sq->sq_flags & waitflags) || (!(type & SQ_CICB) &&count != 0)) {
73950Sstevel@tonic-gate if ((sq->sq_callbflags & cbp->cbp_flags) &&
73960Sstevel@tonic-gate (sq->sq_cancelid == cbp->cbp_id)) {
73970Sstevel@tonic-gate /* timeout has been cancelled */
73980Sstevel@tonic-gate sq->sq_callbflags |= SQ_CALLB_BYPASSED;
73990Sstevel@tonic-gate callbparams_free(sq, cbp);
74000Sstevel@tonic-gate if (!(type & SQ_CICB)) {
74010Sstevel@tonic-gate ASSERT(sq->sq_needexcl > 0);
74020Sstevel@tonic-gate sq->sq_needexcl--;
74030Sstevel@tonic-gate if (sq->sq_needexcl == 0) {
74040Sstevel@tonic-gate SQ_PUTCOUNT_SETFAST_LOCKED(sq);
74050Sstevel@tonic-gate }
74060Sstevel@tonic-gate SQ_PUTLOCKS_EXIT(sq);
74070Sstevel@tonic-gate }
74080Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
74090Sstevel@tonic-gate return;
74100Sstevel@tonic-gate }
74110Sstevel@tonic-gate sq->sq_flags |= SQ_WANTWAKEUP;
74120Sstevel@tonic-gate if (!(type & SQ_CICB)) {
74130Sstevel@tonic-gate SQ_PUTLOCKS_EXIT(sq);
74140Sstevel@tonic-gate }
74150Sstevel@tonic-gate cv_wait(&sq->sq_wait, SQLOCK(sq));
74160Sstevel@tonic-gate if (!(type & SQ_CICB)) {
74170Sstevel@tonic-gate count = sq->sq_count;
74180Sstevel@tonic-gate SQ_PUTLOCKS_ENTER(sq);
74190Sstevel@tonic-gate SUM_SQ_PUTCOUNTS(sq, count);
74200Sstevel@tonic-gate }
74210Sstevel@tonic-gate }
74220Sstevel@tonic-gate
74230Sstevel@tonic-gate sq->sq_count++;
74240Sstevel@tonic-gate ASSERT(sq->sq_count != 0); /* Wraparound */
74250Sstevel@tonic-gate if (!(type & SQ_CICB)) {
74260Sstevel@tonic-gate ASSERT(count == 0);
74270Sstevel@tonic-gate sq->sq_flags |= SQ_EXCL;
74280Sstevel@tonic-gate ASSERT(sq->sq_needexcl > 0);
74290Sstevel@tonic-gate sq->sq_needexcl--;
74300Sstevel@tonic-gate if (sq->sq_needexcl == 0) {
74310Sstevel@tonic-gate SQ_PUTCOUNT_SETFAST_LOCKED(sq);
74320Sstevel@tonic-gate }
74330Sstevel@tonic-gate SQ_PUTLOCKS_EXIT(sq);
74340Sstevel@tonic-gate }
74350Sstevel@tonic-gate
74360Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
74370Sstevel@tonic-gate
74380Sstevel@tonic-gate cbp->cbp_func(cbp->cbp_arg);
74390Sstevel@tonic-gate
74400Sstevel@tonic-gate /*
74410Sstevel@tonic-gate * We drop the lock only for leavesq to re-acquire it.
74420Sstevel@tonic-gate * Possible optimization is inline of leavesq.
74430Sstevel@tonic-gate */
74440Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
74450Sstevel@tonic-gate callbparams_free(sq, cbp);
74460Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
74470Sstevel@tonic-gate leavesq(sq, SQ_CALLBACK);
74480Sstevel@tonic-gate }
74490Sstevel@tonic-gate
74500Sstevel@tonic-gate /*
74519671SBrian.Ruthven@Sun.COM * No need to grab sq_putlocks here. See comment in strsubr.h that
74520Sstevel@tonic-gate * explains when sq_putlocks are used.
74530Sstevel@tonic-gate *
74540Sstevel@tonic-gate * sq_count (or one of the sq_putcounts) has already been
74550Sstevel@tonic-gate * decremented by the caller, and if SQ_QUEUED, we need to call
74560Sstevel@tonic-gate * drain_syncq (the global syncq drain).
74570Sstevel@tonic-gate * If putnext_tail is called with the SQ_EXCL bit set, we are in
74589671SBrian.Ruthven@Sun.COM * one of two states, non-CIPUT perimeter, and we need to clear
74590Sstevel@tonic-gate * it, or we went exclusive in the put procedure. In any case,
74600Sstevel@tonic-gate * we want to clear the bit now, and it is probably easier to do
74610Sstevel@tonic-gate * this at the beginning of this function (remember, we hold
74620Sstevel@tonic-gate * the SQLOCK). Lastly, if there are other messages queued
74630Sstevel@tonic-gate * on the syncq (and not for our destination), enable the syncq
74640Sstevel@tonic-gate * for background work.
74650Sstevel@tonic-gate */
74660Sstevel@tonic-gate
74670Sstevel@tonic-gate /* ARGSUSED */
74680Sstevel@tonic-gate void
putnext_tail(syncq_t * sq,queue_t * qp,uint32_t passflags)74690Sstevel@tonic-gate putnext_tail(syncq_t *sq, queue_t *qp, uint32_t passflags)
74700Sstevel@tonic-gate {
74710Sstevel@tonic-gate uint16_t flags = sq->sq_flags;
74720Sstevel@tonic-gate
74730Sstevel@tonic-gate ASSERT(MUTEX_HELD(SQLOCK(sq)));
74740Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(QLOCK(qp)));
74750Sstevel@tonic-gate
74760Sstevel@tonic-gate /* Clear SQ_EXCL if set in passflags */
74770Sstevel@tonic-gate if (passflags & SQ_EXCL) {
74780Sstevel@tonic-gate flags &= ~SQ_EXCL;
74790Sstevel@tonic-gate }
74800Sstevel@tonic-gate if (flags & SQ_WANTWAKEUP) {
74810Sstevel@tonic-gate flags &= ~SQ_WANTWAKEUP;
74820Sstevel@tonic-gate cv_broadcast(&sq->sq_wait);
74830Sstevel@tonic-gate }
74840Sstevel@tonic-gate if (flags & SQ_WANTEXWAKEUP) {
74850Sstevel@tonic-gate flags &= ~SQ_WANTEXWAKEUP;
74860Sstevel@tonic-gate cv_broadcast(&sq->sq_exitwait);
74870Sstevel@tonic-gate }
74880Sstevel@tonic-gate sq->sq_flags = flags;
74890Sstevel@tonic-gate
74900Sstevel@tonic-gate /*
74910Sstevel@tonic-gate * We have cleared SQ_EXCL if we were asked to, and started
74920Sstevel@tonic-gate * the wakeup process for waiters. If there are no writers
74930Sstevel@tonic-gate * then we need to drain the syncq if we were told to, or
74940Sstevel@tonic-gate * enable the background thread to do it.
74950Sstevel@tonic-gate */
74960Sstevel@tonic-gate if (!(flags & (SQ_STAYAWAY|SQ_EXCL))) {
74970Sstevel@tonic-gate if ((passflags & SQ_QUEUED) ||
74980Sstevel@tonic-gate (sq->sq_svcflags & SQ_DISABLED)) {
74990Sstevel@tonic-gate /* drain_syncq will take care of events in the list */
75000Sstevel@tonic-gate drain_syncq(sq);
75010Sstevel@tonic-gate return;
75020Sstevel@tonic-gate } else if (flags & SQ_QUEUED) {
75030Sstevel@tonic-gate sqenable(sq);
75040Sstevel@tonic-gate }
75050Sstevel@tonic-gate }
75060Sstevel@tonic-gate /* Drop the SQLOCK on exit */
75070Sstevel@tonic-gate mutex_exit(SQLOCK(sq));
75080Sstevel@tonic-gate TRACE_3(TR_FAC_STREAMS_FR, TR_PUTNEXT_END,
75095753Sgww "putnext_end:(%p, %p, %p) done", NULL, qp, sq);
75100Sstevel@tonic-gate }
75110Sstevel@tonic-gate
75120Sstevel@tonic-gate void
set_qend(queue_t * q)75130Sstevel@tonic-gate set_qend(queue_t *q)
75140Sstevel@tonic-gate {
75150Sstevel@tonic-gate mutex_enter(QLOCK(q));
75160Sstevel@tonic-gate if (!O_SAMESTR(q))
75170Sstevel@tonic-gate q->q_flag |= QEND;
75180Sstevel@tonic-gate else
75190Sstevel@tonic-gate q->q_flag &= ~QEND;
75200Sstevel@tonic-gate mutex_exit(QLOCK(q));
75210Sstevel@tonic-gate q = _OTHERQ(q);
75220Sstevel@tonic-gate mutex_enter(QLOCK(q));
75230Sstevel@tonic-gate if (!O_SAMESTR(q))
75240Sstevel@tonic-gate q->q_flag |= QEND;
75250Sstevel@tonic-gate else
75260Sstevel@tonic-gate q->q_flag &= ~QEND;
75270Sstevel@tonic-gate mutex_exit(QLOCK(q));
75280Sstevel@tonic-gate }
75290Sstevel@tonic-gate
75306380Sjk115741 /*
75316380Sjk115741 * Set QFULL in next service procedure queue (that cares) if not already
75326380Sjk115741 * set and if there are already more messages on the syncq than
75336380Sjk115741 * sq_max_size. If sq_max_size is 0, no flow control will be asserted on
75346380Sjk115741 * any syncq.
75356380Sjk115741 *
75366380Sjk115741 * The fq here is the next queue with a service procedure. This is where
75376380Sjk115741 * we would fail canputnext, so this is where we need to set QFULL.
75386380Sjk115741 * In the case when fq != q we need to take QLOCK(fq) to set QFULL flag.
75396380Sjk115741 *
75406380Sjk115741 * We already have QLOCK at this point. To avoid cross-locks with
75416380Sjk115741 * freezestr() which grabs all QLOCKs and with strlock() which grabs both
75426380Sjk115741 * SQLOCK and sd_reflock, we need to drop respective locks first.
75436380Sjk115741 */
75446380Sjk115741 void
set_qfull(queue_t * q)75456380Sjk115741 set_qfull(queue_t *q)
75466380Sjk115741 {
75476380Sjk115741 queue_t *fq = NULL;
75486380Sjk115741
75496380Sjk115741 ASSERT(MUTEX_HELD(QLOCK(q)));
75506380Sjk115741 if ((sq_max_size != 0) && (!(q->q_nfsrv->q_flag & QFULL)) &&
75516380Sjk115741 (q->q_syncqmsgs > sq_max_size)) {
75526380Sjk115741 if ((fq = q->q_nfsrv) == q) {
75536380Sjk115741 fq->q_flag |= QFULL;
75546380Sjk115741 } else {
75556380Sjk115741 mutex_exit(QLOCK(q));
75566380Sjk115741 mutex_enter(QLOCK(fq));
75576380Sjk115741 fq->q_flag |= QFULL;
75586380Sjk115741 mutex_exit(QLOCK(fq));
75596380Sjk115741 mutex_enter(QLOCK(q));
75606380Sjk115741 }
75616380Sjk115741 }
75626380Sjk115741 }
75630Sstevel@tonic-gate
75640Sstevel@tonic-gate void
clr_qfull(queue_t * q)75650Sstevel@tonic-gate clr_qfull(queue_t *q)
75660Sstevel@tonic-gate {
75670Sstevel@tonic-gate queue_t *oq = q;
75680Sstevel@tonic-gate
75690Sstevel@tonic-gate q = q->q_nfsrv;
75700Sstevel@tonic-gate /* Fast check if there is any work to do before getting the lock. */
75710Sstevel@tonic-gate if ((q->q_flag & (QFULL|QWANTW)) == 0) {
75720Sstevel@tonic-gate return;
75730Sstevel@tonic-gate }
75740Sstevel@tonic-gate
75750Sstevel@tonic-gate /*
75760Sstevel@tonic-gate * Do not reset QFULL (and backenable) if the q_count is the reason
75770Sstevel@tonic-gate * for QFULL being set.
75780Sstevel@tonic-gate */
75790Sstevel@tonic-gate mutex_enter(QLOCK(q));
75800Sstevel@tonic-gate /*
75815360Srk129064 * If queue is empty i.e q_mblkcnt is zero, queue can not be full.
75825360Srk129064 * Hence clear the QFULL.
75835360Srk129064 * If both q_count and q_mblkcnt are less than the hiwat mark,
75845360Srk129064 * clear the QFULL.
75855360Srk129064 */
75865360Srk129064 if (q->q_mblkcnt == 0 || ((q->q_count < q->q_hiwat) &&
75875360Srk129064 (q->q_mblkcnt < q->q_hiwat))) {
75880Sstevel@tonic-gate q->q_flag &= ~QFULL;
75890Sstevel@tonic-gate /*
75900Sstevel@tonic-gate * A little more confusing, how about this way:
75910Sstevel@tonic-gate * if someone wants to write,
75920Sstevel@tonic-gate * AND
75930Sstevel@tonic-gate * both counts are less than the lowat mark
75940Sstevel@tonic-gate * OR
75950Sstevel@tonic-gate * the lowat mark is zero
75960Sstevel@tonic-gate * THEN
75970Sstevel@tonic-gate * backenable
75980Sstevel@tonic-gate */
75990Sstevel@tonic-gate if ((q->q_flag & QWANTW) &&
76000Sstevel@tonic-gate (((q->q_count < q->q_lowat) &&
76010Sstevel@tonic-gate (q->q_mblkcnt < q->q_lowat)) || q->q_lowat == 0)) {
76020Sstevel@tonic-gate q->q_flag &= ~QWANTW;
76030Sstevel@tonic-gate mutex_exit(QLOCK(q));
76040Sstevel@tonic-gate backenable(oq, 0);
76050Sstevel@tonic-gate } else
76060Sstevel@tonic-gate mutex_exit(QLOCK(q));
76070Sstevel@tonic-gate } else
76080Sstevel@tonic-gate mutex_exit(QLOCK(q));
76090Sstevel@tonic-gate }
76100Sstevel@tonic-gate
76110Sstevel@tonic-gate /*
76120Sstevel@tonic-gate * Set the forward service procedure pointer.
76130Sstevel@tonic-gate *
76140Sstevel@tonic-gate * Called at insert-time to cache a queue's next forward service procedure in
76150Sstevel@tonic-gate * q_nfsrv; used by canput() and canputnext(). If the queue to be inserted
76160Sstevel@tonic-gate * has a service procedure then q_nfsrv points to itself. If the queue to be
76170Sstevel@tonic-gate * inserted does not have a service procedure, then q_nfsrv points to the next
76180Sstevel@tonic-gate * queue forward that has a service procedure. If the queue is at the logical
76190Sstevel@tonic-gate * end of the stream (driver for write side, stream head for the read side)
76200Sstevel@tonic-gate * and does not have a service procedure, then q_nfsrv also points to itself.
76210Sstevel@tonic-gate */
76220Sstevel@tonic-gate void
set_nfsrv_ptr(queue_t * rnew,queue_t * wnew,queue_t * prev_rq,queue_t * prev_wq)76230Sstevel@tonic-gate set_nfsrv_ptr(
76240Sstevel@tonic-gate queue_t *rnew, /* read queue pointer to new module */
76250Sstevel@tonic-gate queue_t *wnew, /* write queue pointer to new module */
76260Sstevel@tonic-gate queue_t *prev_rq, /* read queue pointer to the module above */
76270Sstevel@tonic-gate queue_t *prev_wq) /* write queue pointer to the module above */
76280Sstevel@tonic-gate {
76290Sstevel@tonic-gate queue_t *qp;
76300Sstevel@tonic-gate
76310Sstevel@tonic-gate if (prev_wq->q_next == NULL) {
76320Sstevel@tonic-gate /*
76330Sstevel@tonic-gate * Insert the driver, initialize the driver and stream head.
76340Sstevel@tonic-gate * In this case, prev_rq/prev_wq should be the stream head.
76350Sstevel@tonic-gate * _I_INSERT does not allow inserting a driver. Make sure
76360Sstevel@tonic-gate * that it is not an insertion.
76370Sstevel@tonic-gate */
76380Sstevel@tonic-gate ASSERT(!(rnew->q_flag & _QINSERTING));
76390Sstevel@tonic-gate wnew->q_nfsrv = wnew;
76400Sstevel@tonic-gate if (rnew->q_qinfo->qi_srvp)
76410Sstevel@tonic-gate rnew->q_nfsrv = rnew;
76420Sstevel@tonic-gate else
76430Sstevel@tonic-gate rnew->q_nfsrv = prev_rq;
76440Sstevel@tonic-gate prev_rq->q_nfsrv = prev_rq;
76450Sstevel@tonic-gate prev_wq->q_nfsrv = prev_wq;
76460Sstevel@tonic-gate } else {
76470Sstevel@tonic-gate /*
76480Sstevel@tonic-gate * set up read side q_nfsrv pointer. This MUST be done
76490Sstevel@tonic-gate * before setting the write side, because the setting of
76500Sstevel@tonic-gate * the write side for a fifo may depend on it.
76510Sstevel@tonic-gate *
76520Sstevel@tonic-gate * Suppose we have a fifo that only has pipemod pushed.
76530Sstevel@tonic-gate * pipemod has no read or write service procedures, so
76540Sstevel@tonic-gate * nfsrv for both pipemod queues points to prev_rq (the
76550Sstevel@tonic-gate * stream read head). Now push bufmod (which has only a
76560Sstevel@tonic-gate * read service procedure). Doing the write side first,
76570Sstevel@tonic-gate * wnew->q_nfsrv is set to pipemod's writeq nfsrv, which
76580Sstevel@tonic-gate * is WRONG; the next queue forward from wnew with a
76590Sstevel@tonic-gate * service procedure will be rnew, not the stream read head.
76600Sstevel@tonic-gate * Since the downstream queue (which in the case of a fifo
76610Sstevel@tonic-gate * is the read queue rnew) can affect upstream queues, it
76620Sstevel@tonic-gate * needs to be done first. Setting up the read side first
76630Sstevel@tonic-gate * sets nfsrv for both pipemod queues to rnew and then
76640Sstevel@tonic-gate * when the write side is set up, wnew-q_nfsrv will also
76650Sstevel@tonic-gate * point to rnew.
76660Sstevel@tonic-gate */
76670Sstevel@tonic-gate if (rnew->q_qinfo->qi_srvp) {
76680Sstevel@tonic-gate /*
76690Sstevel@tonic-gate * use _OTHERQ() because, if this is a pipe, next
76700Sstevel@tonic-gate * module may have been pushed from other end and
76710Sstevel@tonic-gate * q_next could be a read queue.
76720Sstevel@tonic-gate */
76730Sstevel@tonic-gate qp = _OTHERQ(prev_wq->q_next);
76740Sstevel@tonic-gate while (qp && qp->q_nfsrv != qp) {
76750Sstevel@tonic-gate qp->q_nfsrv = rnew;
76760Sstevel@tonic-gate qp = backq(qp);
76770Sstevel@tonic-gate }
76780Sstevel@tonic-gate rnew->q_nfsrv = rnew;
76790Sstevel@tonic-gate } else
76800Sstevel@tonic-gate rnew->q_nfsrv = prev_rq->q_nfsrv;
76810Sstevel@tonic-gate
76820Sstevel@tonic-gate /* set up write side q_nfsrv pointer */
76830Sstevel@tonic-gate if (wnew->q_qinfo->qi_srvp) {
76840Sstevel@tonic-gate wnew->q_nfsrv = wnew;
76850Sstevel@tonic-gate
76860Sstevel@tonic-gate /*
76870Sstevel@tonic-gate * For insertion, need to update nfsrv of the modules
76880Sstevel@tonic-gate * above which do not have a service routine.
76890Sstevel@tonic-gate */
76900Sstevel@tonic-gate if (rnew->q_flag & _QINSERTING) {
76910Sstevel@tonic-gate for (qp = prev_wq;
76920Sstevel@tonic-gate qp != NULL && qp->q_nfsrv != qp;
76930Sstevel@tonic-gate qp = backq(qp)) {
76940Sstevel@tonic-gate qp->q_nfsrv = wnew->q_nfsrv;
76950Sstevel@tonic-gate }
76960Sstevel@tonic-gate }
76970Sstevel@tonic-gate } else {
76980Sstevel@tonic-gate if (prev_wq->q_next == prev_rq)
76990Sstevel@tonic-gate /*
77000Sstevel@tonic-gate * Since prev_wq/prev_rq are the middle of a
77010Sstevel@tonic-gate * fifo, wnew/rnew will also be the middle of
77020Sstevel@tonic-gate * a fifo and wnew's nfsrv is same as rnew's.
77030Sstevel@tonic-gate */
77040Sstevel@tonic-gate wnew->q_nfsrv = rnew->q_nfsrv;
77050Sstevel@tonic-gate else
77060Sstevel@tonic-gate wnew->q_nfsrv = prev_wq->q_next->q_nfsrv;
77070Sstevel@tonic-gate }
77080Sstevel@tonic-gate }
77090Sstevel@tonic-gate }
77100Sstevel@tonic-gate
77110Sstevel@tonic-gate /*
77120Sstevel@tonic-gate * Reset the forward service procedure pointer; called at remove-time.
77130Sstevel@tonic-gate */
77140Sstevel@tonic-gate void
reset_nfsrv_ptr(queue_t * rqp,queue_t * wqp)77150Sstevel@tonic-gate reset_nfsrv_ptr(queue_t *rqp, queue_t *wqp)
77160Sstevel@tonic-gate {
77170Sstevel@tonic-gate queue_t *tmp_qp;
77180Sstevel@tonic-gate
77190Sstevel@tonic-gate /* Reset the write side q_nfsrv pointer for _I_REMOVE */
77200Sstevel@tonic-gate if ((rqp->q_flag & _QREMOVING) && (wqp->q_qinfo->qi_srvp != NULL)) {
77210Sstevel@tonic-gate for (tmp_qp = backq(wqp);
77220Sstevel@tonic-gate tmp_qp != NULL && tmp_qp->q_nfsrv == wqp;
77230Sstevel@tonic-gate tmp_qp = backq(tmp_qp)) {
77240Sstevel@tonic-gate tmp_qp->q_nfsrv = wqp->q_nfsrv;
77250Sstevel@tonic-gate }
77260Sstevel@tonic-gate }
77270Sstevel@tonic-gate
77280Sstevel@tonic-gate /* reset the read side q_nfsrv pointer */
77290Sstevel@tonic-gate if (rqp->q_qinfo->qi_srvp) {
77300Sstevel@tonic-gate if (wqp->q_next) { /* non-driver case */
77310Sstevel@tonic-gate tmp_qp = _OTHERQ(wqp->q_next);
77320Sstevel@tonic-gate while (tmp_qp && tmp_qp->q_nfsrv == rqp) {
77330Sstevel@tonic-gate /* Note that rqp->q_next cannot be NULL */
77340Sstevel@tonic-gate ASSERT(rqp->q_next != NULL);
77350Sstevel@tonic-gate tmp_qp->q_nfsrv = rqp->q_next->q_nfsrv;
77360Sstevel@tonic-gate tmp_qp = backq(tmp_qp);
77370Sstevel@tonic-gate }
77380Sstevel@tonic-gate }
77390Sstevel@tonic-gate }
77400Sstevel@tonic-gate }
77410Sstevel@tonic-gate
77420Sstevel@tonic-gate /*
77430Sstevel@tonic-gate * This routine should be called after all stream geometry changes to update
77440Sstevel@tonic-gate * the stream head cached struio() rd/wr queue pointers. Note must be called
77450Sstevel@tonic-gate * with the streamlock()ed.
77460Sstevel@tonic-gate *
77470Sstevel@tonic-gate * Note: only enables Synchronous STREAMS for a side of a Stream which has
77480Sstevel@tonic-gate * an explicit synchronous barrier module queue. That is, a queue that
77490Sstevel@tonic-gate * has specified a struio() type.
77500Sstevel@tonic-gate */
77510Sstevel@tonic-gate static void
strsetuio(stdata_t * stp)77520Sstevel@tonic-gate strsetuio(stdata_t *stp)
77530Sstevel@tonic-gate {
77540Sstevel@tonic-gate queue_t *wrq;
77550Sstevel@tonic-gate
77560Sstevel@tonic-gate if (stp->sd_flag & STPLEX) {
77570Sstevel@tonic-gate /*
77589671SBrian.Ruthven@Sun.COM * Not streamhead, but a mux, so no Synchronous STREAMS.
77590Sstevel@tonic-gate */
77600Sstevel@tonic-gate stp->sd_struiowrq = NULL;
77610Sstevel@tonic-gate stp->sd_struiordq = NULL;
77620Sstevel@tonic-gate return;
77630Sstevel@tonic-gate }
77640Sstevel@tonic-gate /*
77650Sstevel@tonic-gate * Scan the write queue(s) while synchronous
77660Sstevel@tonic-gate * until we find a qinfo uio type specified.
77670Sstevel@tonic-gate */
77680Sstevel@tonic-gate wrq = stp->sd_wrq->q_next;
77690Sstevel@tonic-gate while (wrq) {
77700Sstevel@tonic-gate if (wrq->q_struiot == STRUIOT_NONE) {
77710Sstevel@tonic-gate wrq = 0;
77720Sstevel@tonic-gate break;
77730Sstevel@tonic-gate }
77740Sstevel@tonic-gate if (wrq->q_struiot != STRUIOT_DONTCARE)
77750Sstevel@tonic-gate break;
77760Sstevel@tonic-gate if (! _SAMESTR(wrq)) {
77770Sstevel@tonic-gate wrq = 0;
77780Sstevel@tonic-gate break;
77790Sstevel@tonic-gate }
77800Sstevel@tonic-gate wrq = wrq->q_next;
77810Sstevel@tonic-gate }
77820Sstevel@tonic-gate stp->sd_struiowrq = wrq;
77830Sstevel@tonic-gate /*
77840Sstevel@tonic-gate * Scan the read queue(s) while synchronous
77850Sstevel@tonic-gate * until we find a qinfo uio type specified.
77860Sstevel@tonic-gate */
77870Sstevel@tonic-gate wrq = stp->sd_wrq->q_next;
77880Sstevel@tonic-gate while (wrq) {
77890Sstevel@tonic-gate if (_RD(wrq)->q_struiot == STRUIOT_NONE) {
77900Sstevel@tonic-gate wrq = 0;
77910Sstevel@tonic-gate break;
77920Sstevel@tonic-gate }
77930Sstevel@tonic-gate if (_RD(wrq)->q_struiot != STRUIOT_DONTCARE)
77940Sstevel@tonic-gate break;
77950Sstevel@tonic-gate if (! _SAMESTR(wrq)) {
77960Sstevel@tonic-gate wrq = 0;
77970Sstevel@tonic-gate break;
77980Sstevel@tonic-gate }
77990Sstevel@tonic-gate wrq = wrq->q_next;
78000Sstevel@tonic-gate }
78010Sstevel@tonic-gate stp->sd_struiordq = wrq ? _RD(wrq) : 0;
78020Sstevel@tonic-gate }
78030Sstevel@tonic-gate
78040Sstevel@tonic-gate /*
78050Sstevel@tonic-gate * pass_wput, unblocks the passthru queues, so that
78060Sstevel@tonic-gate * messages can arrive at muxs lower read queue, before
78070Sstevel@tonic-gate * I_LINK/I_UNLINK is acked/nacked.
78080Sstevel@tonic-gate */
78090Sstevel@tonic-gate static void
pass_wput(queue_t * q,mblk_t * mp)78100Sstevel@tonic-gate pass_wput(queue_t *q, mblk_t *mp)
78110Sstevel@tonic-gate {
78120Sstevel@tonic-gate syncq_t *sq;
78130Sstevel@tonic-gate
78140Sstevel@tonic-gate sq = _RD(q)->q_syncq;
78150Sstevel@tonic-gate if (sq->sq_flags & SQ_BLOCKED)
78160Sstevel@tonic-gate unblocksq(sq, SQ_BLOCKED, 0);
78170Sstevel@tonic-gate putnext(q, mp);
78180Sstevel@tonic-gate }
78190Sstevel@tonic-gate
78200Sstevel@tonic-gate /*
78210Sstevel@tonic-gate * Set up queues for the link/unlink.
78220Sstevel@tonic-gate * Create a new queue and block it and then insert it
78230Sstevel@tonic-gate * below the stream head on the lower stream.
78240Sstevel@tonic-gate * This prevents any messages from arriving during the setq
78250Sstevel@tonic-gate * as well as while the mux is processing the LINK/I_UNLINK.
78260Sstevel@tonic-gate * The blocked passq is unblocked once the LINK/I_UNLINK has
78270Sstevel@tonic-gate * been acked or nacked or if a message is generated and sent
78280Sstevel@tonic-gate * down muxs write put procedure.
78299671SBrian.Ruthven@Sun.COM * See pass_wput().
78300Sstevel@tonic-gate *
78310Sstevel@tonic-gate * After the new queue is inserted, all messages coming from below are
78320Sstevel@tonic-gate * blocked. The call to strlock will ensure that all activity in the stream head
78330Sstevel@tonic-gate * read queue syncq is stopped (sq_count drops to zero).
78340Sstevel@tonic-gate */
78350Sstevel@tonic-gate static queue_t *
link_addpassthru(stdata_t * stpdown)78360Sstevel@tonic-gate link_addpassthru(stdata_t *stpdown)
78370Sstevel@tonic-gate {
78380Sstevel@tonic-gate queue_t *passq;
78390Sstevel@tonic-gate sqlist_t sqlist;
78400Sstevel@tonic-gate
78410Sstevel@tonic-gate passq = allocq();
78420Sstevel@tonic-gate STREAM(passq) = STREAM(_WR(passq)) = stpdown;
78430Sstevel@tonic-gate /* setq might sleep in allocator - avoid holding locks. */
78440Sstevel@tonic-gate setq(passq, &passthru_rinit, &passthru_winit, NULL, QPERQ,
78450Sstevel@tonic-gate SQ_CI|SQ_CO, B_FALSE);
78460Sstevel@tonic-gate claimq(passq);
78470Sstevel@tonic-gate blocksq(passq->q_syncq, SQ_BLOCKED, 1);
78480Sstevel@tonic-gate insertq(STREAM(passq), passq);
78490Sstevel@tonic-gate
78500Sstevel@tonic-gate /*
78510Sstevel@tonic-gate * Use strlock() to wait for the stream head sq_count to drop to zero
78520Sstevel@tonic-gate * since we are going to change q_ptr in the stream head. Note that
78530Sstevel@tonic-gate * insertq() doesn't wait for any syncq counts to drop to zero.
78540Sstevel@tonic-gate */
78550Sstevel@tonic-gate sqlist.sqlist_head = NULL;
78560Sstevel@tonic-gate sqlist.sqlist_index = 0;
78570Sstevel@tonic-gate sqlist.sqlist_size = sizeof (sqlist_t);
78580Sstevel@tonic-gate sqlist_insert(&sqlist, _RD(stpdown->sd_wrq)->q_syncq);
78590Sstevel@tonic-gate strlock(stpdown, &sqlist);
78600Sstevel@tonic-gate strunlock(stpdown, &sqlist);
78610Sstevel@tonic-gate
78620Sstevel@tonic-gate releaseq(passq);
78630Sstevel@tonic-gate return (passq);
78640Sstevel@tonic-gate }
78650Sstevel@tonic-gate
78660Sstevel@tonic-gate /*
78670Sstevel@tonic-gate * Let messages flow up into the mux by removing
78680Sstevel@tonic-gate * the passq.
78690Sstevel@tonic-gate */
78700Sstevel@tonic-gate static void
link_rempassthru(queue_t * passq)78710Sstevel@tonic-gate link_rempassthru(queue_t *passq)
78720Sstevel@tonic-gate {
78730Sstevel@tonic-gate claimq(passq);
78740Sstevel@tonic-gate removeq(passq);
78750Sstevel@tonic-gate releaseq(passq);
78760Sstevel@tonic-gate freeq(passq);
78770Sstevel@tonic-gate }
78780Sstevel@tonic-gate
78790Sstevel@tonic-gate /*
7880577Smeem * Wait for the condition variable pointed to by `cvp' to be signaled,
7881577Smeem * or for `tim' milliseconds to elapse, whichever comes first. If `tim'
7882577Smeem * is negative, then there is no time limit. If `nosigs' is non-zero,
7883577Smeem * then the wait will be non-interruptible.
7884577Smeem *
7885577Smeem * Returns >0 if signaled, 0 if interrupted, or -1 upon timeout.
78860Sstevel@tonic-gate */
78870Sstevel@tonic-gate clock_t
str_cv_wait(kcondvar_t * cvp,kmutex_t * mp,clock_t tim,int nosigs)78880Sstevel@tonic-gate str_cv_wait(kcondvar_t *cvp, kmutex_t *mp, clock_t tim, int nosigs)
78890Sstevel@tonic-gate {
789011066Srafael.vanoni@sun.com clock_t ret;
78910Sstevel@tonic-gate
78920Sstevel@tonic-gate if (tim < 0) {
78930Sstevel@tonic-gate if (nosigs) {
78940Sstevel@tonic-gate cv_wait(cvp, mp);
78950Sstevel@tonic-gate ret = 1;
78960Sstevel@tonic-gate } else {
78970Sstevel@tonic-gate ret = cv_wait_sig(cvp, mp);
78980Sstevel@tonic-gate }
78990Sstevel@tonic-gate } else if (tim > 0) {
79000Sstevel@tonic-gate /*
79010Sstevel@tonic-gate * convert milliseconds to clock ticks
79020Sstevel@tonic-gate */
79030Sstevel@tonic-gate if (nosigs) {
790411066Srafael.vanoni@sun.com ret = cv_reltimedwait(cvp, mp,
790511066Srafael.vanoni@sun.com MSEC_TO_TICK_ROUNDUP(tim), TR_CLOCK_TICK);
79060Sstevel@tonic-gate } else {
790711066Srafael.vanoni@sun.com ret = cv_reltimedwait_sig(cvp, mp,
790811066Srafael.vanoni@sun.com MSEC_TO_TICK_ROUNDUP(tim), TR_CLOCK_TICK);
79090Sstevel@tonic-gate }
79100Sstevel@tonic-gate } else {
79110Sstevel@tonic-gate ret = -1;
79120Sstevel@tonic-gate }
79130Sstevel@tonic-gate return (ret);
79140Sstevel@tonic-gate }
79150Sstevel@tonic-gate
79160Sstevel@tonic-gate /*
79170Sstevel@tonic-gate * Wait until the stream head can determine if it is at the mark but
79180Sstevel@tonic-gate * don't wait forever to prevent a race condition between the "mark" state
79190Sstevel@tonic-gate * in the stream head and any mark state in the caller/user of this routine.
79200Sstevel@tonic-gate *
79210Sstevel@tonic-gate * This is used by sockets and for a socket it would be incorrect
79220Sstevel@tonic-gate * to return a failure for SIOCATMARK when there is no data in the receive
79230Sstevel@tonic-gate * queue and the marked urgent data is traveling up the stream.
79240Sstevel@tonic-gate *
79250Sstevel@tonic-gate * This routine waits until the mark is known by waiting for one of these
79260Sstevel@tonic-gate * three events:
79279671SBrian.Ruthven@Sun.COM * The stream head read queue becoming non-empty (including an EOF).
79289671SBrian.Ruthven@Sun.COM * The STRATMARK flag being set (due to a MSGMARKNEXT message).
79290Sstevel@tonic-gate * The STRNOTATMARK flag being set (which indicates that the transport
79300Sstevel@tonic-gate * has sent a MSGNOTMARKNEXT message to indicate that it is not at
79310Sstevel@tonic-gate * the mark).
79320Sstevel@tonic-gate *
79330Sstevel@tonic-gate * The routine returns 1 if the stream is at the mark; 0 if it can
79340Sstevel@tonic-gate * be determined that the stream is not at the mark.
79350Sstevel@tonic-gate * If the wait times out and it can't determine
79360Sstevel@tonic-gate * whether or not the stream might be at the mark the routine will return -1.
79370Sstevel@tonic-gate *
79380Sstevel@tonic-gate * Note: This routine should only be used when a mark is pending i.e.,
79390Sstevel@tonic-gate * in the socket case the SIGURG has been posted.
79400Sstevel@tonic-gate * Note2: This can not wakeup just because synchronous streams indicate
79410Sstevel@tonic-gate * that data is available since it is not possible to use the synchronous
79420Sstevel@tonic-gate * streams interfaces to determine the b_flag value for the data queued below
79430Sstevel@tonic-gate * the stream head.
79440Sstevel@tonic-gate */
79450Sstevel@tonic-gate int
strwaitmark(vnode_t * vp)79460Sstevel@tonic-gate strwaitmark(vnode_t *vp)
79470Sstevel@tonic-gate {
79480Sstevel@tonic-gate struct stdata *stp = vp->v_stream;
79490Sstevel@tonic-gate queue_t *rq = _RD(stp->sd_wrq);
79500Sstevel@tonic-gate int mark;
79510Sstevel@tonic-gate
79520Sstevel@tonic-gate mutex_enter(&stp->sd_lock);
79530Sstevel@tonic-gate while (rq->q_first == NULL &&
79540Sstevel@tonic-gate !(stp->sd_flag & (STRATMARK|STRNOTATMARK|STREOF))) {
79550Sstevel@tonic-gate stp->sd_flag |= RSLEEP;
79560Sstevel@tonic-gate
79570Sstevel@tonic-gate /* Wait for 100 milliseconds for any state change. */
79580Sstevel@tonic-gate if (str_cv_wait(&rq->q_wait, &stp->sd_lock, 100, 1) == -1) {
79590Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
79600Sstevel@tonic-gate return (-1);
79610Sstevel@tonic-gate }
79620Sstevel@tonic-gate }
79630Sstevel@tonic-gate if (stp->sd_flag & STRATMARK)
79640Sstevel@tonic-gate mark = 1;
79650Sstevel@tonic-gate else if (rq->q_first != NULL && (rq->q_first->b_flag & MSGMARK))
79660Sstevel@tonic-gate mark = 1;
79670Sstevel@tonic-gate else
79680Sstevel@tonic-gate mark = 0;
79690Sstevel@tonic-gate
79700Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
79710Sstevel@tonic-gate return (mark);
79720Sstevel@tonic-gate }
79730Sstevel@tonic-gate
79740Sstevel@tonic-gate /*
79750Sstevel@tonic-gate * Set a read side error. If persist is set change the socket error
79760Sstevel@tonic-gate * to persistent. If errfunc is set install the function as the exported
79770Sstevel@tonic-gate * error handler.
79780Sstevel@tonic-gate */
79790Sstevel@tonic-gate void
strsetrerror(vnode_t * vp,int error,int persist,errfunc_t errfunc)79800Sstevel@tonic-gate strsetrerror(vnode_t *vp, int error, int persist, errfunc_t errfunc)
79810Sstevel@tonic-gate {
79820Sstevel@tonic-gate struct stdata *stp = vp->v_stream;
79830Sstevel@tonic-gate
79840Sstevel@tonic-gate mutex_enter(&stp->sd_lock);
79850Sstevel@tonic-gate stp->sd_rerror = error;
79860Sstevel@tonic-gate if (error == 0 && errfunc == NULL)
79870Sstevel@tonic-gate stp->sd_flag &= ~STRDERR;
79880Sstevel@tonic-gate else
79890Sstevel@tonic-gate stp->sd_flag |= STRDERR;
79900Sstevel@tonic-gate if (persist) {
79910Sstevel@tonic-gate stp->sd_flag &= ~STRDERRNONPERSIST;
79920Sstevel@tonic-gate } else {
79930Sstevel@tonic-gate stp->sd_flag |= STRDERRNONPERSIST;
79940Sstevel@tonic-gate }
79950Sstevel@tonic-gate stp->sd_rderrfunc = errfunc;
79960Sstevel@tonic-gate if (error != 0 || errfunc != NULL) {
79970Sstevel@tonic-gate cv_broadcast(&_RD(stp->sd_wrq)->q_wait); /* readers */
79980Sstevel@tonic-gate cv_broadcast(&stp->sd_wrq->q_wait); /* writers */
79990Sstevel@tonic-gate cv_broadcast(&stp->sd_monitor); /* ioctllers */
80000Sstevel@tonic-gate
80010Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
80020Sstevel@tonic-gate pollwakeup(&stp->sd_pollist, POLLERR);
80030Sstevel@tonic-gate mutex_enter(&stp->sd_lock);
80040Sstevel@tonic-gate
80050Sstevel@tonic-gate if (stp->sd_sigflags & S_ERROR)
80060Sstevel@tonic-gate strsendsig(stp->sd_siglist, S_ERROR, 0, error);
80070Sstevel@tonic-gate }
80080Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
80090Sstevel@tonic-gate }
80100Sstevel@tonic-gate
80110Sstevel@tonic-gate /*
80120Sstevel@tonic-gate * Set a write side error. If persist is set change the socket error
80130Sstevel@tonic-gate * to persistent.
80140Sstevel@tonic-gate */
80150Sstevel@tonic-gate void
strsetwerror(vnode_t * vp,int error,int persist,errfunc_t errfunc)80160Sstevel@tonic-gate strsetwerror(vnode_t *vp, int error, int persist, errfunc_t errfunc)
80170Sstevel@tonic-gate {
80180Sstevel@tonic-gate struct stdata *stp = vp->v_stream;
80190Sstevel@tonic-gate
80200Sstevel@tonic-gate mutex_enter(&stp->sd_lock);
80210Sstevel@tonic-gate stp->sd_werror = error;
80220Sstevel@tonic-gate if (error == 0 && errfunc == NULL)
80230Sstevel@tonic-gate stp->sd_flag &= ~STWRERR;
80240Sstevel@tonic-gate else
80250Sstevel@tonic-gate stp->sd_flag |= STWRERR;
80260Sstevel@tonic-gate if (persist) {
80270Sstevel@tonic-gate stp->sd_flag &= ~STWRERRNONPERSIST;
80280Sstevel@tonic-gate } else {
80290Sstevel@tonic-gate stp->sd_flag |= STWRERRNONPERSIST;
80300Sstevel@tonic-gate }
80310Sstevel@tonic-gate stp->sd_wrerrfunc = errfunc;
80320Sstevel@tonic-gate if (error != 0 || errfunc != NULL) {
80330Sstevel@tonic-gate cv_broadcast(&_RD(stp->sd_wrq)->q_wait); /* readers */
80340Sstevel@tonic-gate cv_broadcast(&stp->sd_wrq->q_wait); /* writers */
80350Sstevel@tonic-gate cv_broadcast(&stp->sd_monitor); /* ioctllers */
80360Sstevel@tonic-gate
80370Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
80380Sstevel@tonic-gate pollwakeup(&stp->sd_pollist, POLLERR);
80390Sstevel@tonic-gate mutex_enter(&stp->sd_lock);
80400Sstevel@tonic-gate
80410Sstevel@tonic-gate if (stp->sd_sigflags & S_ERROR)
80420Sstevel@tonic-gate strsendsig(stp->sd_siglist, S_ERROR, 0, error);
80430Sstevel@tonic-gate }
80440Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
80450Sstevel@tonic-gate }
80460Sstevel@tonic-gate
80470Sstevel@tonic-gate /*
80480Sstevel@tonic-gate * Make the stream return 0 (EOF) when all data has been read.
80490Sstevel@tonic-gate * No effect on write side.
80500Sstevel@tonic-gate */
80510Sstevel@tonic-gate void
strseteof(vnode_t * vp,int eof)80520Sstevel@tonic-gate strseteof(vnode_t *vp, int eof)
80530Sstevel@tonic-gate {
80540Sstevel@tonic-gate struct stdata *stp = vp->v_stream;
80550Sstevel@tonic-gate
80560Sstevel@tonic-gate mutex_enter(&stp->sd_lock);
80570Sstevel@tonic-gate if (!eof) {
80580Sstevel@tonic-gate stp->sd_flag &= ~STREOF;
80590Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
80600Sstevel@tonic-gate return;
80610Sstevel@tonic-gate }
80620Sstevel@tonic-gate stp->sd_flag |= STREOF;
80630Sstevel@tonic-gate if (stp->sd_flag & RSLEEP) {
80640Sstevel@tonic-gate stp->sd_flag &= ~RSLEEP;
80650Sstevel@tonic-gate cv_broadcast(&_RD(stp->sd_wrq)->q_wait);
80660Sstevel@tonic-gate }
80670Sstevel@tonic-gate
80680Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
80690Sstevel@tonic-gate pollwakeup(&stp->sd_pollist, POLLIN|POLLRDNORM);
80700Sstevel@tonic-gate mutex_enter(&stp->sd_lock);
80710Sstevel@tonic-gate
80720Sstevel@tonic-gate if (stp->sd_sigflags & (S_INPUT|S_RDNORM))
80730Sstevel@tonic-gate strsendsig(stp->sd_siglist, S_INPUT|S_RDNORM, 0, 0);
80740Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
80750Sstevel@tonic-gate }
80760Sstevel@tonic-gate
80770Sstevel@tonic-gate void
strflushrq(vnode_t * vp,int flag)80780Sstevel@tonic-gate strflushrq(vnode_t *vp, int flag)
80790Sstevel@tonic-gate {
80800Sstevel@tonic-gate struct stdata *stp = vp->v_stream;
80810Sstevel@tonic-gate
80820Sstevel@tonic-gate mutex_enter(&stp->sd_lock);
80830Sstevel@tonic-gate flushq(_RD(stp->sd_wrq), flag);
80840Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
80850Sstevel@tonic-gate }
80860Sstevel@tonic-gate
80870Sstevel@tonic-gate void
strsetrputhooks(vnode_t * vp,uint_t flags,msgfunc_t protofunc,msgfunc_t miscfunc)80880Sstevel@tonic-gate strsetrputhooks(vnode_t *vp, uint_t flags,
80890Sstevel@tonic-gate msgfunc_t protofunc, msgfunc_t miscfunc)
80900Sstevel@tonic-gate {
80910Sstevel@tonic-gate struct stdata *stp = vp->v_stream;
80920Sstevel@tonic-gate
80930Sstevel@tonic-gate mutex_enter(&stp->sd_lock);
80940Sstevel@tonic-gate
80950Sstevel@tonic-gate if (protofunc == NULL)
80960Sstevel@tonic-gate stp->sd_rprotofunc = strrput_proto;
80970Sstevel@tonic-gate else
80980Sstevel@tonic-gate stp->sd_rprotofunc = protofunc;
80990Sstevel@tonic-gate
81000Sstevel@tonic-gate if (miscfunc == NULL)
81010Sstevel@tonic-gate stp->sd_rmiscfunc = strrput_misc;
81020Sstevel@tonic-gate else
81030Sstevel@tonic-gate stp->sd_rmiscfunc = miscfunc;
81040Sstevel@tonic-gate
81050Sstevel@tonic-gate if (flags & SH_CONSOL_DATA)
81060Sstevel@tonic-gate stp->sd_rput_opt |= SR_CONSOL_DATA;
81070Sstevel@tonic-gate else
81080Sstevel@tonic-gate stp->sd_rput_opt &= ~SR_CONSOL_DATA;
81090Sstevel@tonic-gate
81100Sstevel@tonic-gate if (flags & SH_SIGALLDATA)
81110Sstevel@tonic-gate stp->sd_rput_opt |= SR_SIGALLDATA;
81120Sstevel@tonic-gate else
81130Sstevel@tonic-gate stp->sd_rput_opt &= ~SR_SIGALLDATA;
81140Sstevel@tonic-gate
81150Sstevel@tonic-gate if (flags & SH_IGN_ZEROLEN)
81160Sstevel@tonic-gate stp->sd_rput_opt |= SR_IGN_ZEROLEN;
81170Sstevel@tonic-gate else
81180Sstevel@tonic-gate stp->sd_rput_opt &= ~SR_IGN_ZEROLEN;
81190Sstevel@tonic-gate
81200Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
81210Sstevel@tonic-gate }
81220Sstevel@tonic-gate
81230Sstevel@tonic-gate void
strsetwputhooks(vnode_t * vp,uint_t flags,clock_t closetime)81240Sstevel@tonic-gate strsetwputhooks(vnode_t *vp, uint_t flags, clock_t closetime)
81250Sstevel@tonic-gate {
81260Sstevel@tonic-gate struct stdata *stp = vp->v_stream;
81270Sstevel@tonic-gate
81280Sstevel@tonic-gate mutex_enter(&stp->sd_lock);
81290Sstevel@tonic-gate stp->sd_closetime = closetime;
81300Sstevel@tonic-gate
81310Sstevel@tonic-gate if (flags & SH_SIGPIPE)
81320Sstevel@tonic-gate stp->sd_wput_opt |= SW_SIGPIPE;
81330Sstevel@tonic-gate else
81340Sstevel@tonic-gate stp->sd_wput_opt &= ~SW_SIGPIPE;
81350Sstevel@tonic-gate if (flags & SH_RECHECK_ERR)
81360Sstevel@tonic-gate stp->sd_wput_opt |= SW_RECHECK_ERR;
81370Sstevel@tonic-gate else
81380Sstevel@tonic-gate stp->sd_wput_opt &= ~SW_RECHECK_ERR;
81390Sstevel@tonic-gate
81400Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
81410Sstevel@tonic-gate }
81420Sstevel@tonic-gate
8143898Skais void
strsetrwputdatahooks(vnode_t * vp,msgfunc_t rdatafunc,msgfunc_t wdatafunc)8144898Skais strsetrwputdatahooks(vnode_t *vp, msgfunc_t rdatafunc, msgfunc_t wdatafunc)
8145898Skais {
8146898Skais struct stdata *stp = vp->v_stream;
8147898Skais
8148898Skais mutex_enter(&stp->sd_lock);
8149898Skais
8150898Skais stp->sd_rputdatafunc = rdatafunc;
8151898Skais stp->sd_wputdatafunc = wdatafunc;
8152898Skais
8153898Skais mutex_exit(&stp->sd_lock);
8154898Skais }
8155898Skais
81560Sstevel@tonic-gate /* Used within framework when the queue is already locked */
81570Sstevel@tonic-gate void
qenable_locked(queue_t * q)81580Sstevel@tonic-gate qenable_locked(queue_t *q)
81590Sstevel@tonic-gate {
81600Sstevel@tonic-gate stdata_t *stp = STREAM(q);
81610Sstevel@tonic-gate
81620Sstevel@tonic-gate ASSERT(MUTEX_HELD(QLOCK(q)));
81630Sstevel@tonic-gate
81640Sstevel@tonic-gate if (!q->q_qinfo->qi_srvp)
81650Sstevel@tonic-gate return;
81660Sstevel@tonic-gate
81670Sstevel@tonic-gate /*
81680Sstevel@tonic-gate * Do not place on run queue if already enabled or closing.
81690Sstevel@tonic-gate */
81700Sstevel@tonic-gate if (q->q_flag & (QWCLOSE|QENAB))
81710Sstevel@tonic-gate return;
81720Sstevel@tonic-gate
81730Sstevel@tonic-gate /*
81740Sstevel@tonic-gate * mark queue enabled and place on run list if it is not already being
81750Sstevel@tonic-gate * serviced. If it is serviced, the runservice() function will detect
81760Sstevel@tonic-gate * that QENAB is set and call service procedure before clearing
81770Sstevel@tonic-gate * QINSERVICE flag.
81780Sstevel@tonic-gate */
81790Sstevel@tonic-gate q->q_flag |= QENAB;
81800Sstevel@tonic-gate if (q->q_flag & QINSERVICE)
81810Sstevel@tonic-gate return;
81820Sstevel@tonic-gate
81830Sstevel@tonic-gate /* Record the time of qenable */
818411066Srafael.vanoni@sun.com q->q_qtstamp = ddi_get_lbolt();
81850Sstevel@tonic-gate
81860Sstevel@tonic-gate /*
81870Sstevel@tonic-gate * Put the queue in the stp list and schedule it for background
81880Sstevel@tonic-gate * processing if it is not already scheduled or if stream head does not
81890Sstevel@tonic-gate * intent to process it in the foreground later by setting
81900Sstevel@tonic-gate * STRS_WILLSERVICE flag.
81910Sstevel@tonic-gate */
81920Sstevel@tonic-gate mutex_enter(&stp->sd_qlock);
81930Sstevel@tonic-gate /*
81940Sstevel@tonic-gate * If there are already something on the list, stp flags should show
81950Sstevel@tonic-gate * intention to drain it.
81960Sstevel@tonic-gate */
81970Sstevel@tonic-gate IMPLY(STREAM_NEEDSERVICE(stp),
81980Sstevel@tonic-gate (stp->sd_svcflags & (STRS_WILLSERVICE | STRS_SCHEDULED)));
81990Sstevel@tonic-gate
82000Sstevel@tonic-gate ENQUEUE(q, stp->sd_qhead, stp->sd_qtail, q_link);
82010Sstevel@tonic-gate stp->sd_nqueues++;
82020Sstevel@tonic-gate
82030Sstevel@tonic-gate /*
82040Sstevel@tonic-gate * If no one will drain this stream we are the first producer and
82050Sstevel@tonic-gate * need to schedule it for background thread.
82060Sstevel@tonic-gate */
82070Sstevel@tonic-gate if (!(stp->sd_svcflags & (STRS_WILLSERVICE | STRS_SCHEDULED))) {
82080Sstevel@tonic-gate /*
82090Sstevel@tonic-gate * No one will service this stream later, so we have to
82100Sstevel@tonic-gate * schedule it now.
82110Sstevel@tonic-gate */
82120Sstevel@tonic-gate STRSTAT(stenables);
82130Sstevel@tonic-gate stp->sd_svcflags |= STRS_SCHEDULED;
82140Sstevel@tonic-gate stp->sd_servid = (void *)taskq_dispatch(streams_taskq,
82150Sstevel@tonic-gate (task_func_t *)stream_service, stp, TQ_NOSLEEP|TQ_NOQUEUE);
82160Sstevel@tonic-gate
82170Sstevel@tonic-gate if (stp->sd_servid == NULL) {
82180Sstevel@tonic-gate /*
82190Sstevel@tonic-gate * Task queue failed so fail over to the backup
82200Sstevel@tonic-gate * servicing thread.
82210Sstevel@tonic-gate */
82220Sstevel@tonic-gate STRSTAT(taskqfails);
82230Sstevel@tonic-gate /*
82240Sstevel@tonic-gate * It is safe to clear STRS_SCHEDULED flag because it
82250Sstevel@tonic-gate * was set by this thread above.
82260Sstevel@tonic-gate */
82270Sstevel@tonic-gate stp->sd_svcflags &= ~STRS_SCHEDULED;
82280Sstevel@tonic-gate
82290Sstevel@tonic-gate /*
82300Sstevel@tonic-gate * Failover scheduling is protected by service_queue
82310Sstevel@tonic-gate * lock.
82320Sstevel@tonic-gate */
82330Sstevel@tonic-gate mutex_enter(&service_queue);
82340Sstevel@tonic-gate ASSERT((stp->sd_qhead == q) && (stp->sd_qtail == q));
82350Sstevel@tonic-gate ASSERT(q->q_link == NULL);
82360Sstevel@tonic-gate /*
82370Sstevel@tonic-gate * Append the queue to qhead/qtail list.
82380Sstevel@tonic-gate */
82390Sstevel@tonic-gate if (qhead == NULL)
82400Sstevel@tonic-gate qhead = q;
82410Sstevel@tonic-gate else
82420Sstevel@tonic-gate qtail->q_link = q;
82430Sstevel@tonic-gate qtail = q;
82440Sstevel@tonic-gate /*
82450Sstevel@tonic-gate * Clear stp queue list.
82460Sstevel@tonic-gate */
82470Sstevel@tonic-gate stp->sd_qhead = stp->sd_qtail = NULL;
82480Sstevel@tonic-gate stp->sd_nqueues = 0;
82490Sstevel@tonic-gate /*
82500Sstevel@tonic-gate * Wakeup background queue processing thread.
82510Sstevel@tonic-gate */
82520Sstevel@tonic-gate cv_signal(&services_to_run);
82530Sstevel@tonic-gate mutex_exit(&service_queue);
82540Sstevel@tonic-gate }
82550Sstevel@tonic-gate }
82560Sstevel@tonic-gate mutex_exit(&stp->sd_qlock);
82570Sstevel@tonic-gate }
82580Sstevel@tonic-gate
82590Sstevel@tonic-gate static void
queue_service(queue_t * q)82600Sstevel@tonic-gate queue_service(queue_t *q)
82610Sstevel@tonic-gate {
82620Sstevel@tonic-gate /*
82630Sstevel@tonic-gate * The queue in the list should have
82640Sstevel@tonic-gate * QENAB flag set and should not have
82650Sstevel@tonic-gate * QINSERVICE flag set. QINSERVICE is
82660Sstevel@tonic-gate * set when the queue is dequeued and
82670Sstevel@tonic-gate * qenable_locked doesn't enqueue a
82680Sstevel@tonic-gate * queue with QINSERVICE set.
82690Sstevel@tonic-gate */
82700Sstevel@tonic-gate
82710Sstevel@tonic-gate ASSERT(!(q->q_flag & QINSERVICE));
82720Sstevel@tonic-gate ASSERT((q->q_flag & QENAB));
82730Sstevel@tonic-gate mutex_enter(QLOCK(q));
82740Sstevel@tonic-gate q->q_flag &= ~QENAB;
82750Sstevel@tonic-gate q->q_flag |= QINSERVICE;
82760Sstevel@tonic-gate mutex_exit(QLOCK(q));
82770Sstevel@tonic-gate runservice(q);
82780Sstevel@tonic-gate }
82790Sstevel@tonic-gate
82800Sstevel@tonic-gate static void
syncq_service(syncq_t * sq)82810Sstevel@tonic-gate syncq_service(syncq_t *sq)
82820Sstevel@tonic-gate {
82830Sstevel@tonic-gate STRSTAT(syncqservice);
82840Sstevel@tonic-gate mutex_enter(SQLOCK(sq));
82850Sstevel@tonic-gate ASSERT(!(sq->sq_svcflags & SQ_SERVICE));
82860Sstevel@tonic-gate ASSERT(sq->sq_servcount != 0);
82870Sstevel@tonic-gate ASSERT(sq->sq_next == NULL);
82880Sstevel@tonic-gate
82890Sstevel@tonic-gate /* if we came here from the background thread, clear the flag */
82900Sstevel@tonic-gate if (sq->sq_svcflags & SQ_BGTHREAD)
82910Sstevel@tonic-gate sq->sq_svcflags &= ~SQ_BGTHREAD;
82920Sstevel@tonic-gate
82930Sstevel@tonic-gate /* let drain_syncq know that it's being called in the background */
82940Sstevel@tonic-gate sq->sq_svcflags |= SQ_SERVICE;
82950Sstevel@tonic-gate drain_syncq(sq);
82960Sstevel@tonic-gate }
82970Sstevel@tonic-gate
82980Sstevel@tonic-gate static void
qwriter_outer_service(syncq_t * outer)82990Sstevel@tonic-gate qwriter_outer_service(syncq_t *outer)
83000Sstevel@tonic-gate {
83010Sstevel@tonic-gate /*
83020Sstevel@tonic-gate * Note that SQ_WRITER is used on the outer perimeter
83030Sstevel@tonic-gate * to signal that a qwriter(OUTER) is either investigating
83040Sstevel@tonic-gate * running or that it is actually running a function.
83050Sstevel@tonic-gate */
83060Sstevel@tonic-gate outer_enter(outer, SQ_BLOCKED|SQ_WRITER);
83070Sstevel@tonic-gate
83080Sstevel@tonic-gate /*
83090Sstevel@tonic-gate * All inner syncq are empty and have SQ_WRITER set
83100Sstevel@tonic-gate * to block entering the outer perimeter.
83110Sstevel@tonic-gate *
83120Sstevel@tonic-gate * We do not need to explicitly call write_now since
83130Sstevel@tonic-gate * outer_exit does it for us.
83140Sstevel@tonic-gate */
83150Sstevel@tonic-gate outer_exit(outer);
83160Sstevel@tonic-gate }
83170Sstevel@tonic-gate
83180Sstevel@tonic-gate static void
mblk_free(mblk_t * mp)83190Sstevel@tonic-gate mblk_free(mblk_t *mp)
83200Sstevel@tonic-gate {
83210Sstevel@tonic-gate dblk_t *dbp = mp->b_datap;
83220Sstevel@tonic-gate frtn_t *frp = dbp->db_frtnp;
83230Sstevel@tonic-gate
83240Sstevel@tonic-gate mp->b_next = NULL;
83250Sstevel@tonic-gate if (dbp->db_fthdr != NULL)
83260Sstevel@tonic-gate str_ftfree(dbp);
83270Sstevel@tonic-gate
83280Sstevel@tonic-gate ASSERT(dbp->db_fthdr == NULL);
83290Sstevel@tonic-gate frp->free_func(frp->free_arg);
83300Sstevel@tonic-gate ASSERT(dbp->db_mblk == mp);
83310Sstevel@tonic-gate
83320Sstevel@tonic-gate if (dbp->db_credp != NULL) {
83330Sstevel@tonic-gate crfree(dbp->db_credp);
83340Sstevel@tonic-gate dbp->db_credp = NULL;
83350Sstevel@tonic-gate }
83360Sstevel@tonic-gate dbp->db_cpid = -1;
83370Sstevel@tonic-gate dbp->db_struioflag = 0;
83380Sstevel@tonic-gate dbp->db_struioun.cksum.flags = 0;
83390Sstevel@tonic-gate
83400Sstevel@tonic-gate kmem_cache_free(dbp->db_cache, dbp);
83410Sstevel@tonic-gate }
83420Sstevel@tonic-gate
83430Sstevel@tonic-gate /*
83440Sstevel@tonic-gate * Background processing of the stream queue list.
83450Sstevel@tonic-gate */
83460Sstevel@tonic-gate static void
stream_service(stdata_t * stp)83470Sstevel@tonic-gate stream_service(stdata_t *stp)
83480Sstevel@tonic-gate {
83490Sstevel@tonic-gate queue_t *q;
83500Sstevel@tonic-gate
83510Sstevel@tonic-gate mutex_enter(&stp->sd_qlock);
83520Sstevel@tonic-gate
83530Sstevel@tonic-gate STR_SERVICE(stp, q);
83540Sstevel@tonic-gate
83550Sstevel@tonic-gate stp->sd_svcflags &= ~STRS_SCHEDULED;
83560Sstevel@tonic-gate stp->sd_servid = NULL;
83570Sstevel@tonic-gate cv_signal(&stp->sd_qcv);
83580Sstevel@tonic-gate mutex_exit(&stp->sd_qlock);
83590Sstevel@tonic-gate }
83600Sstevel@tonic-gate
83610Sstevel@tonic-gate /*
83620Sstevel@tonic-gate * Foreground processing of the stream queue list.
83630Sstevel@tonic-gate */
83640Sstevel@tonic-gate void
stream_runservice(stdata_t * stp)83650Sstevel@tonic-gate stream_runservice(stdata_t *stp)
83660Sstevel@tonic-gate {
83670Sstevel@tonic-gate queue_t *q;
83680Sstevel@tonic-gate
83690Sstevel@tonic-gate mutex_enter(&stp->sd_qlock);
83700Sstevel@tonic-gate STRSTAT(rservice);
83710Sstevel@tonic-gate /*
83720Sstevel@tonic-gate * We are going to drain this stream queue list, so qenable_locked will
83730Sstevel@tonic-gate * not schedule it until we finish.
83740Sstevel@tonic-gate */
83750Sstevel@tonic-gate stp->sd_svcflags |= STRS_WILLSERVICE;
83760Sstevel@tonic-gate
83770Sstevel@tonic-gate STR_SERVICE(stp, q);
83780Sstevel@tonic-gate
83790Sstevel@tonic-gate stp->sd_svcflags &= ~STRS_WILLSERVICE;
83800Sstevel@tonic-gate mutex_exit(&stp->sd_qlock);
83810Sstevel@tonic-gate /*
83820Sstevel@tonic-gate * Help backup background thread to drain the qhead/qtail list.
83830Sstevel@tonic-gate */
83840Sstevel@tonic-gate while (qhead != NULL) {
83850Sstevel@tonic-gate STRSTAT(qhelps);
83860Sstevel@tonic-gate mutex_enter(&service_queue);
83870Sstevel@tonic-gate DQ(q, qhead, qtail, q_link);
83880Sstevel@tonic-gate mutex_exit(&service_queue);
83890Sstevel@tonic-gate if (q != NULL)
83900Sstevel@tonic-gate queue_service(q);
83910Sstevel@tonic-gate }
83920Sstevel@tonic-gate }
83930Sstevel@tonic-gate
83940Sstevel@tonic-gate void
stream_willservice(stdata_t * stp)83950Sstevel@tonic-gate stream_willservice(stdata_t *stp)
83960Sstevel@tonic-gate {
83970Sstevel@tonic-gate mutex_enter(&stp->sd_qlock);
83980Sstevel@tonic-gate stp->sd_svcflags |= STRS_WILLSERVICE;
83990Sstevel@tonic-gate mutex_exit(&stp->sd_qlock);
84000Sstevel@tonic-gate }
84010Sstevel@tonic-gate
84020Sstevel@tonic-gate /*
84030Sstevel@tonic-gate * Replace the cred currently in the mblk with a different one.
84048778SErik.Nordmark@Sun.COM * Also update db_cpid.
84050Sstevel@tonic-gate */
84060Sstevel@tonic-gate void
mblk_setcred(mblk_t * mp,cred_t * cr,pid_t cpid)84078778SErik.Nordmark@Sun.COM mblk_setcred(mblk_t *mp, cred_t *cr, pid_t cpid)
84088778SErik.Nordmark@Sun.COM {
84098778SErik.Nordmark@Sun.COM dblk_t *dbp = mp->b_datap;
84108778SErik.Nordmark@Sun.COM cred_t *ocr = dbp->db_credp;
84110Sstevel@tonic-gate
84120Sstevel@tonic-gate ASSERT(cr != NULL);
84130Sstevel@tonic-gate
84140Sstevel@tonic-gate if (cr != ocr) {
84158778SErik.Nordmark@Sun.COM crhold(dbp->db_credp = cr);
84160Sstevel@tonic-gate if (ocr != NULL)
84170Sstevel@tonic-gate crfree(ocr);
84180Sstevel@tonic-gate }
84198778SErik.Nordmark@Sun.COM /* Don't overwrite with NOPID */
84208778SErik.Nordmark@Sun.COM if (cpid != NOPID)
84218778SErik.Nordmark@Sun.COM dbp->db_cpid = cpid;
84228778SErik.Nordmark@Sun.COM }
84238778SErik.Nordmark@Sun.COM
84248778SErik.Nordmark@Sun.COM /*
84258778SErik.Nordmark@Sun.COM * If the src message has a cred, then replace the cred currently in the mblk
84268778SErik.Nordmark@Sun.COM * with it.
84278778SErik.Nordmark@Sun.COM * Also update db_cpid.
84288348SEric.Yu@Sun.COM */
84298348SEric.Yu@Sun.COM void
mblk_copycred(mblk_t * mp,const mblk_t * src)84308778SErik.Nordmark@Sun.COM mblk_copycred(mblk_t *mp, const mblk_t *src)
84318778SErik.Nordmark@Sun.COM {
84328778SErik.Nordmark@Sun.COM dblk_t *dbp = mp->b_datap;
84338778SErik.Nordmark@Sun.COM cred_t *cr, *ocr;
84348778SErik.Nordmark@Sun.COM pid_t cpid;
84358778SErik.Nordmark@Sun.COM
84368778SErik.Nordmark@Sun.COM cr = msg_getcred(src, &cpid);
84378778SErik.Nordmark@Sun.COM if (cr == NULL)
84388778SErik.Nordmark@Sun.COM return;
84398778SErik.Nordmark@Sun.COM
84408778SErik.Nordmark@Sun.COM ocr = dbp->db_credp;
84418778SErik.Nordmark@Sun.COM if (cr != ocr) {
84428778SErik.Nordmark@Sun.COM crhold(dbp->db_credp = cr);
84438778SErik.Nordmark@Sun.COM if (ocr != NULL)
84448778SErik.Nordmark@Sun.COM crfree(ocr);
84458778SErik.Nordmark@Sun.COM }
84468778SErik.Nordmark@Sun.COM /* Don't overwrite with NOPID */
84478778SErik.Nordmark@Sun.COM if (cpid != NOPID)
84488778SErik.Nordmark@Sun.COM dbp->db_cpid = cpid;
84498348SEric.Yu@Sun.COM }
84508348SEric.Yu@Sun.COM
84510Sstevel@tonic-gate int
hcksum_assoc(mblk_t * mp,multidata_t * mmd,pdesc_t * pd,uint32_t start,uint32_t stuff,uint32_t end,uint32_t value,uint32_t flags,int km_flags)84520Sstevel@tonic-gate hcksum_assoc(mblk_t *mp, multidata_t *mmd, pdesc_t *pd,
84530Sstevel@tonic-gate uint32_t start, uint32_t stuff, uint32_t end, uint32_t value,
84540Sstevel@tonic-gate uint32_t flags, int km_flags)
84550Sstevel@tonic-gate {
84560Sstevel@tonic-gate int rc = 0;
84570Sstevel@tonic-gate
84580Sstevel@tonic-gate ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_MULTIDATA);
84590Sstevel@tonic-gate if (mp->b_datap->db_type == M_DATA) {
84600Sstevel@tonic-gate /* Associate values for M_DATA type */
8461741Smasputra DB_CKSUMSTART(mp) = (intptr_t)start;
8462741Smasputra DB_CKSUMSTUFF(mp) = (intptr_t)stuff;
8463741Smasputra DB_CKSUMEND(mp) = (intptr_t)end;
8464741Smasputra DB_CKSUMFLAGS(mp) = flags;
8465741Smasputra DB_CKSUM16(mp) = (uint16_t)value;
84660Sstevel@tonic-gate
84670Sstevel@tonic-gate } else {
84680Sstevel@tonic-gate pattrinfo_t pa_info;
84690Sstevel@tonic-gate
84700Sstevel@tonic-gate ASSERT(mmd != NULL);
84710Sstevel@tonic-gate
84720Sstevel@tonic-gate pa_info.type = PATTR_HCKSUM;
84730Sstevel@tonic-gate pa_info.len = sizeof (pattr_hcksum_t);
84740Sstevel@tonic-gate
84750Sstevel@tonic-gate if (mmd_addpattr(mmd, pd, &pa_info, B_TRUE, km_flags) != NULL) {
84760Sstevel@tonic-gate pattr_hcksum_t *hck = (pattr_hcksum_t *)pa_info.buf;
84770Sstevel@tonic-gate
84780Sstevel@tonic-gate hck->hcksum_start_offset = start;
84790Sstevel@tonic-gate hck->hcksum_stuff_offset = stuff;
84800Sstevel@tonic-gate hck->hcksum_end_offset = end;
84810Sstevel@tonic-gate hck->hcksum_cksum_val.inet_cksum = (uint16_t)value;
84820Sstevel@tonic-gate hck->hcksum_flags = flags;
8483741Smasputra } else {
8484741Smasputra rc = -1;
84850Sstevel@tonic-gate }
84860Sstevel@tonic-gate }
84870Sstevel@tonic-gate return (rc);
84880Sstevel@tonic-gate }
84890Sstevel@tonic-gate
84900Sstevel@tonic-gate void
hcksum_retrieve(mblk_t * mp,multidata_t * mmd,pdesc_t * pd,uint32_t * start,uint32_t * stuff,uint32_t * end,uint32_t * value,uint32_t * flags)84910Sstevel@tonic-gate hcksum_retrieve(mblk_t *mp, multidata_t *mmd, pdesc_t *pd,
84920Sstevel@tonic-gate uint32_t *start, uint32_t *stuff, uint32_t *end,
84930Sstevel@tonic-gate uint32_t *value, uint32_t *flags)
84940Sstevel@tonic-gate {
84950Sstevel@tonic-gate ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_MULTIDATA);
84960Sstevel@tonic-gate if (mp->b_datap->db_type == M_DATA) {
84970Sstevel@tonic-gate if (flags != NULL) {
849811042SErik.Nordmark@Sun.COM *flags = DB_CKSUMFLAGS(mp) & HCK_FLAGS;
84998275SEric Cheng if ((*flags & (HCK_PARTIALCKSUM |
85008275SEric Cheng HCK_FULLCKSUM)) != 0) {
85010Sstevel@tonic-gate if (value != NULL)
8502741Smasputra *value = (uint32_t)DB_CKSUM16(mp);
85038275SEric Cheng if ((*flags & HCK_PARTIALCKSUM) != 0) {
85048275SEric Cheng if (start != NULL)
85058275SEric Cheng *start =
85068275SEric Cheng (uint32_t)DB_CKSUMSTART(mp);
85078275SEric Cheng if (stuff != NULL)
85088275SEric Cheng *stuff =
85098275SEric Cheng (uint32_t)DB_CKSUMSTUFF(mp);
85108275SEric Cheng if (end != NULL)
85118275SEric Cheng *end =
85128275SEric Cheng (uint32_t)DB_CKSUMEND(mp);
85138275SEric Cheng }
85148275SEric Cheng }
85150Sstevel@tonic-gate }
85160Sstevel@tonic-gate } else {
85170Sstevel@tonic-gate pattrinfo_t hck_attr = {PATTR_HCKSUM};
85180Sstevel@tonic-gate
85190Sstevel@tonic-gate ASSERT(mmd != NULL);
85200Sstevel@tonic-gate
85210Sstevel@tonic-gate /* get hardware checksum attribute */
85220Sstevel@tonic-gate if (mmd_getpattr(mmd, pd, &hck_attr) != NULL) {
85230Sstevel@tonic-gate pattr_hcksum_t *hck = (pattr_hcksum_t *)hck_attr.buf;
85240Sstevel@tonic-gate
85250Sstevel@tonic-gate ASSERT(hck_attr.len >= sizeof (pattr_hcksum_t));
85260Sstevel@tonic-gate if (flags != NULL)
85270Sstevel@tonic-gate *flags = hck->hcksum_flags;
85280Sstevel@tonic-gate if (start != NULL)
85290Sstevel@tonic-gate *start = hck->hcksum_start_offset;
85300Sstevel@tonic-gate if (stuff != NULL)
85310Sstevel@tonic-gate *stuff = hck->hcksum_stuff_offset;
85320Sstevel@tonic-gate if (end != NULL)
85330Sstevel@tonic-gate *end = hck->hcksum_end_offset;
85340Sstevel@tonic-gate if (value != NULL)
85350Sstevel@tonic-gate *value = (uint32_t)
85360Sstevel@tonic-gate hck->hcksum_cksum_val.inet_cksum;
85370Sstevel@tonic-gate }
85380Sstevel@tonic-gate }
85390Sstevel@tonic-gate }
85400Sstevel@tonic-gate
85418275SEric Cheng void
lso_info_set(mblk_t * mp,uint32_t mss,uint32_t flags)85428275SEric Cheng lso_info_set(mblk_t *mp, uint32_t mss, uint32_t flags)
85438275SEric Cheng {
85448275SEric Cheng ASSERT(DB_TYPE(mp) == M_DATA);
854511469SRoamer@Sun.COM ASSERT((flags & ~HW_LSO_FLAGS) == 0);
85468275SEric Cheng
85478275SEric Cheng /* Set the flags */
85488275SEric Cheng DB_LSOFLAGS(mp) |= flags;
85498275SEric Cheng DB_LSOMSS(mp) = mss;
85508275SEric Cheng }
85518275SEric Cheng
85528275SEric Cheng void
lso_info_cleanup(mblk_t * mp)855311469SRoamer@Sun.COM lso_info_cleanup(mblk_t *mp)
855411469SRoamer@Sun.COM {
855511469SRoamer@Sun.COM ASSERT(DB_TYPE(mp) == M_DATA);
855611469SRoamer@Sun.COM
855711469SRoamer@Sun.COM /* Clear the flags */
855811469SRoamer@Sun.COM DB_LSOFLAGS(mp) &= ~HW_LSO_FLAGS;
855911469SRoamer@Sun.COM DB_LSOMSS(mp) = 0;
856011469SRoamer@Sun.COM }
856111469SRoamer@Sun.COM
85620Sstevel@tonic-gate /*
85630Sstevel@tonic-gate * Checksum buffer *bp for len bytes with psum partial checksum,
85640Sstevel@tonic-gate * or 0 if none, and return the 16 bit partial checksum.
85650Sstevel@tonic-gate */
85660Sstevel@tonic-gate unsigned
bcksum(uchar_t * bp,int len,unsigned int psum)85670Sstevel@tonic-gate bcksum(uchar_t *bp, int len, unsigned int psum)
85680Sstevel@tonic-gate {
85690Sstevel@tonic-gate int odd = len & 1;
85700Sstevel@tonic-gate extern unsigned int ip_ocsum();
85710Sstevel@tonic-gate
85720Sstevel@tonic-gate if (((intptr_t)bp & 1) == 0 && !odd) {
85730Sstevel@tonic-gate /*
85740Sstevel@tonic-gate * Bp is 16 bit aligned and len is multiple of 16 bit word.
85750Sstevel@tonic-gate */
85760Sstevel@tonic-gate return (ip_ocsum((ushort_t *)bp, len >> 1, psum));
85770Sstevel@tonic-gate }
85780Sstevel@tonic-gate if (((intptr_t)bp & 1) != 0) {
85790Sstevel@tonic-gate /*
85800Sstevel@tonic-gate * Bp isn't 16 bit aligned.
85810Sstevel@tonic-gate */
85820Sstevel@tonic-gate unsigned int tsum;
85830Sstevel@tonic-gate
85840Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
85850Sstevel@tonic-gate psum += *bp;
85860Sstevel@tonic-gate #else
85870Sstevel@tonic-gate psum += *bp << 8;
85880Sstevel@tonic-gate #endif
85890Sstevel@tonic-gate len--;
85900Sstevel@tonic-gate bp++;
85910Sstevel@tonic-gate tsum = ip_ocsum((ushort_t *)bp, len >> 1, 0);
85920Sstevel@tonic-gate psum += (tsum << 8) & 0xffff | (tsum >> 8);
85930Sstevel@tonic-gate if (len & 1) {
85940Sstevel@tonic-gate bp += len - 1;
85950Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
85960Sstevel@tonic-gate psum += *bp << 8;
85970Sstevel@tonic-gate #else
85980Sstevel@tonic-gate psum += *bp;
85990Sstevel@tonic-gate #endif
86000Sstevel@tonic-gate }
86010Sstevel@tonic-gate } else {
86020Sstevel@tonic-gate /*
86030Sstevel@tonic-gate * Bp is 16 bit aligned.
86040Sstevel@tonic-gate */
86050Sstevel@tonic-gate psum = ip_ocsum((ushort_t *)bp, len >> 1, psum);
86060Sstevel@tonic-gate if (odd) {
86070Sstevel@tonic-gate bp += len - 1;
86080Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
86090Sstevel@tonic-gate psum += *bp;
86100Sstevel@tonic-gate #else
86110Sstevel@tonic-gate psum += *bp << 8;
86120Sstevel@tonic-gate #endif
86130Sstevel@tonic-gate }
86140Sstevel@tonic-gate }
86150Sstevel@tonic-gate /*
86160Sstevel@tonic-gate * Normalize psum to 16 bits before returning the new partial
86170Sstevel@tonic-gate * checksum. The max psum value before normalization is 0x3FDFE.
86180Sstevel@tonic-gate */
86190Sstevel@tonic-gate return ((psum >> 16) + (psum & 0xFFFF));
86200Sstevel@tonic-gate }
86210Sstevel@tonic-gate
86220Sstevel@tonic-gate boolean_t
is_vmloaned_mblk(mblk_t * mp,multidata_t * mmd,pdesc_t * pd)86230Sstevel@tonic-gate is_vmloaned_mblk(mblk_t *mp, multidata_t *mmd, pdesc_t *pd)
86240Sstevel@tonic-gate {
86250Sstevel@tonic-gate boolean_t rc;
86260Sstevel@tonic-gate
86270Sstevel@tonic-gate ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_MULTIDATA);
86280Sstevel@tonic-gate if (DB_TYPE(mp) == M_DATA) {
86290Sstevel@tonic-gate rc = (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0);
86300Sstevel@tonic-gate } else {
86310Sstevel@tonic-gate pattrinfo_t zcopy_attr = {PATTR_ZCOPY};
86320Sstevel@tonic-gate
86330Sstevel@tonic-gate ASSERT(mmd != NULL);
86340Sstevel@tonic-gate rc = (mmd_getpattr(mmd, pd, &zcopy_attr) != NULL);
86350Sstevel@tonic-gate }
86360Sstevel@tonic-gate return (rc);
86370Sstevel@tonic-gate }
86380Sstevel@tonic-gate
86390Sstevel@tonic-gate void
freemsgchain(mblk_t * mp)86400Sstevel@tonic-gate freemsgchain(mblk_t *mp)
86410Sstevel@tonic-gate {
86420Sstevel@tonic-gate mblk_t *next;
86430Sstevel@tonic-gate
86440Sstevel@tonic-gate while (mp != NULL) {
86450Sstevel@tonic-gate next = mp->b_next;
86460Sstevel@tonic-gate mp->b_next = NULL;
86470Sstevel@tonic-gate
86480Sstevel@tonic-gate freemsg(mp);
86490Sstevel@tonic-gate mp = next;
86500Sstevel@tonic-gate }
86510Sstevel@tonic-gate }
86520Sstevel@tonic-gate
86530Sstevel@tonic-gate mblk_t *
copymsgchain(mblk_t * mp)86540Sstevel@tonic-gate copymsgchain(mblk_t *mp)
86550Sstevel@tonic-gate {
86560Sstevel@tonic-gate mblk_t *nmp = NULL;
86570Sstevel@tonic-gate mblk_t **nmpp = &nmp;
86580Sstevel@tonic-gate
86590Sstevel@tonic-gate for (; mp != NULL; mp = mp->b_next) {
86600Sstevel@tonic-gate if ((*nmpp = copymsg(mp)) == NULL) {
86610Sstevel@tonic-gate freemsgchain(nmp);
86620Sstevel@tonic-gate return (NULL);
86630Sstevel@tonic-gate }
86640Sstevel@tonic-gate
86650Sstevel@tonic-gate nmpp = &((*nmpp)->b_next);
86660Sstevel@tonic-gate }
86670Sstevel@tonic-gate
86680Sstevel@tonic-gate return (nmp);
86690Sstevel@tonic-gate }
86700Sstevel@tonic-gate
86710Sstevel@tonic-gate /* NOTE: Do not add code after this point. */
86720Sstevel@tonic-gate #undef QLOCK
86730Sstevel@tonic-gate
86740Sstevel@tonic-gate /*
86759671SBrian.Ruthven@Sun.COM * Replacement for QLOCK macro for those that can't use it.
86760Sstevel@tonic-gate */
86770Sstevel@tonic-gate kmutex_t *
QLOCK(queue_t * q)86780Sstevel@tonic-gate QLOCK(queue_t *q)
86790Sstevel@tonic-gate {
86800Sstevel@tonic-gate return (&(q)->q_lock);
86810Sstevel@tonic-gate }
86820Sstevel@tonic-gate
86830Sstevel@tonic-gate /*
86840Sstevel@tonic-gate * Dummy runqueues/queuerun functions functions for backwards compatibility.
86850Sstevel@tonic-gate */
86860Sstevel@tonic-gate #undef runqueues
86870Sstevel@tonic-gate void
runqueues(void)86880Sstevel@tonic-gate runqueues(void)
86890Sstevel@tonic-gate {
86900Sstevel@tonic-gate }
86910Sstevel@tonic-gate
86920Sstevel@tonic-gate #undef queuerun
86930Sstevel@tonic-gate void
queuerun(void)86940Sstevel@tonic-gate queuerun(void)
86950Sstevel@tonic-gate {
86960Sstevel@tonic-gate }
86973448Sdh155122
86983448Sdh155122 /*
86993448Sdh155122 * Initialize the STR stack instance, which tracks autopush and persistent
87003448Sdh155122 * links.
87013448Sdh155122 */
87023448Sdh155122 /* ARGSUSED */
87033448Sdh155122 static void *
str_stack_init(netstackid_t stackid,netstack_t * ns)87043448Sdh155122 str_stack_init(netstackid_t stackid, netstack_t *ns)
87053448Sdh155122 {
87063448Sdh155122 str_stack_t *ss;
87073448Sdh155122 int i;
87083448Sdh155122
87093448Sdh155122 ss = (str_stack_t *)kmem_zalloc(sizeof (*ss), KM_SLEEP);
87103448Sdh155122 ss->ss_netstack = ns;
87113448Sdh155122
87123448Sdh155122 /*
87133448Sdh155122 * set up autopush
87143448Sdh155122 */
87153448Sdh155122 sad_initspace(ss);
87163448Sdh155122
87173448Sdh155122 /*
87183448Sdh155122 * set up mux_node structures.
87193448Sdh155122 */
87203448Sdh155122 ss->ss_devcnt = devcnt; /* In case it should change before free */
87213448Sdh155122 ss->ss_mux_nodes = kmem_zalloc((sizeof (struct mux_node) *
87225753Sgww ss->ss_devcnt), KM_SLEEP);
87233448Sdh155122 for (i = 0; i < ss->ss_devcnt; i++)
87243448Sdh155122 ss->ss_mux_nodes[i].mn_imaj = i;
87253448Sdh155122 return (ss);
87263448Sdh155122 }
87273448Sdh155122
87283448Sdh155122 /*
87293448Sdh155122 * Note: run at zone shutdown and not destroy so that the PLINKs are
87303448Sdh155122 * gone by the time other cleanup happens from the destroy callbacks.
87313448Sdh155122 */
87323448Sdh155122 static void
str_stack_shutdown(netstackid_t stackid,void * arg)87333448Sdh155122 str_stack_shutdown(netstackid_t stackid, void *arg)
87343448Sdh155122 {
87353448Sdh155122 str_stack_t *ss = (str_stack_t *)arg;
87363448Sdh155122 int i;
87373448Sdh155122 cred_t *cr;
87383448Sdh155122
87393448Sdh155122 cr = zone_get_kcred(netstackid_to_zoneid(stackid));
87403448Sdh155122 ASSERT(cr != NULL);
87413448Sdh155122
87423448Sdh155122 /* Undo all the I_PLINKs for this zone */
87433448Sdh155122 for (i = 0; i < ss->ss_devcnt; i++) {
87443448Sdh155122 struct mux_edge *ep;
87453448Sdh155122 ldi_handle_t lh;
87463448Sdh155122 ldi_ident_t li;
87473448Sdh155122 int ret;
87483448Sdh155122 int rval;
87493448Sdh155122 dev_t rdev;
87503448Sdh155122
87513448Sdh155122 ep = ss->ss_mux_nodes[i].mn_outp;
87523448Sdh155122 if (ep == NULL)
87533448Sdh155122 continue;
87543448Sdh155122 ret = ldi_ident_from_major((major_t)i, &li);
87553448Sdh155122 if (ret != 0) {
87563448Sdh155122 continue;
87573448Sdh155122 }
87583448Sdh155122 rdev = ep->me_dev;
87593448Sdh155122 ret = ldi_open_by_dev(&rdev, OTYP_CHR, FREAD|FWRITE,
87603448Sdh155122 cr, &lh, li);
87613448Sdh155122 if (ret != 0) {
87623448Sdh155122 ldi_ident_release(li);
87633448Sdh155122 continue;
87643448Sdh155122 }
87653448Sdh155122
87663448Sdh155122 ret = ldi_ioctl(lh, I_PUNLINK, (intptr_t)MUXID_ALL, FKIOCTL,
87673448Sdh155122 cr, &rval);
87683448Sdh155122 if (ret) {
87693448Sdh155122 (void) ldi_close(lh, FREAD|FWRITE, cr);
87703448Sdh155122 ldi_ident_release(li);
87713448Sdh155122 continue;
87723448Sdh155122 }
87733448Sdh155122 (void) ldi_close(lh, FREAD|FWRITE, cr);
87743448Sdh155122
87753448Sdh155122 /* Close layered handles */
87763448Sdh155122 ldi_ident_release(li);
87773448Sdh155122 }
87783448Sdh155122 crfree(cr);
87793448Sdh155122
87803448Sdh155122 sad_freespace(ss);
87813448Sdh155122
87823448Sdh155122 kmem_free(ss->ss_mux_nodes, sizeof (struct mux_node) * ss->ss_devcnt);
87833448Sdh155122 ss->ss_mux_nodes = NULL;
87843448Sdh155122 }
87853448Sdh155122
87863448Sdh155122 /*
87873448Sdh155122 * Free the structure; str_stack_shutdown did the other cleanup work.
87883448Sdh155122 */
87893448Sdh155122 /* ARGSUSED */
87903448Sdh155122 static void
str_stack_fini(netstackid_t stackid,void * arg)87913448Sdh155122 str_stack_fini(netstackid_t stackid, void *arg)
87923448Sdh155122 {
87933448Sdh155122 str_stack_t *ss = (str_stack_t *)arg;
87943448Sdh155122
87953448Sdh155122 kmem_free(ss, sizeof (*ss));
87963448Sdh155122 }
8797