xref: /onnv-gate/usr/src/uts/common/os/port_subr.c (revision 2948:c89bd1dbe6d0)
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
51885Sraf  * Common Development and Distribution License (the "License").
61885Sraf  * 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  */
211885Sraf 
220Sstevel@tonic-gate /*
231425Spraks  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * This file containts all the functions required for interactions of
310Sstevel@tonic-gate  * event sources with the event port file system.
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <sys/conf.h>
360Sstevel@tonic-gate #include <sys/stat.h>
370Sstevel@tonic-gate #include <sys/errno.h>
380Sstevel@tonic-gate #include <sys/kmem.h>
390Sstevel@tonic-gate #include <sys/debug.h>
400Sstevel@tonic-gate #include <sys/file.h>
410Sstevel@tonic-gate #include <sys/sysmacros.h>
420Sstevel@tonic-gate #include <sys/systm.h>
430Sstevel@tonic-gate #include <sys/bitmap.h>
440Sstevel@tonic-gate #include <sys/rctl.h>
450Sstevel@tonic-gate #include <sys/atomic.h>
460Sstevel@tonic-gate #include <sys/poll_impl.h>
470Sstevel@tonic-gate #include <sys/port_impl.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  * Maximum number of elements allowed to be passed in a single call of a
510Sstevel@tonic-gate  * port function (port_sendn(), port_getn().  We need to allocate kernel memory
520Sstevel@tonic-gate  * for all of them at once, so we can't let it scale without limit.
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate uint_t		port_max_list = PORT_MAX_LIST;
550Sstevel@tonic-gate port_control_t	port_control;	/* Event port framework main structure */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*
581885Sraf  * Block other threads from using a port.
591885Sraf  * We enter holding portq->portq_mutex but
601885Sraf  * we may drop and reacquire this lock.
611885Sraf  * Callers must deal with this fact.
621885Sraf  */
631885Sraf void
641885Sraf port_block(port_queue_t *portq)
651885Sraf {
661885Sraf 	ASSERT(MUTEX_HELD(&portq->portq_mutex));
671885Sraf 
681885Sraf 	while (portq->portq_flags & PORTQ_BLOCKED)
691885Sraf 		cv_wait(&portq->portq_block_cv, &portq->portq_mutex);
701885Sraf 	portq->portq_flags |= PORTQ_BLOCKED;
711885Sraf }
721885Sraf 
731885Sraf /*
741885Sraf  * Undo port_block(portq).
751885Sraf  */
761885Sraf void
771885Sraf port_unblock(port_queue_t *portq)
781885Sraf {
791885Sraf 	ASSERT(MUTEX_HELD(&portq->portq_mutex));
801885Sraf 
811885Sraf 	portq->portq_flags &= ~PORTQ_BLOCKED;
821885Sraf 	cv_signal(&portq->portq_block_cv);
831885Sraf }
841885Sraf 
851885Sraf /*
86*2948Spraks  * Called from pollwakeup(PORT_SOURCE_FD source) to determine
87*2948Spraks  * if the port's fd needs to be notified of poll events. If yes,
88*2948Spraks  * we mark the port indicating that pollwakeup() is referring
89*2948Spraks  * it so that the port_t does not disappear.  pollwakeup()
90*2948Spraks  * calls port_pollwkdone() after notifying. In port_pollwkdone(),
91*2948Spraks  * we clear the hold on the port_t (clear PORTQ_POLLWK_PEND).
92*2948Spraks  */
93*2948Spraks int
94*2948Spraks port_pollwkup(port_t *pp)
95*2948Spraks {
96*2948Spraks 	int events = 0;
97*2948Spraks 	port_queue_t *portq;
98*2948Spraks 	portq = &pp->port_queue;
99*2948Spraks 	mutex_enter(&portq->portq_mutex);
100*2948Spraks 
101*2948Spraks 	/*
102*2948Spraks 	 * Normally, we should not have a situation where PORTQ_POLLIN
103*2948Spraks 	 * and PORTQ_POLLWK_PEND are set at the same time, but it is
104*2948Spraks 	 * possible. So, in pollwakeup() we ensure that no new fd's get
105*2948Spraks 	 * added to the pollhead between the time it notifies poll events
106*2948Spraks 	 * and calls poll_wkupdone() where we clear the PORTQ_POLLWK_PEND flag.
107*2948Spraks 	 */
108*2948Spraks 	if (portq->portq_flags & PORTQ_POLLIN &&
109*2948Spraks 	    !(portq->portq_flags & PORTQ_POLLWK_PEND)) {
110*2948Spraks 		portq->portq_flags &= ~PORTQ_POLLIN;
111*2948Spraks 		portq->portq_flags |= PORTQ_POLLWK_PEND;
112*2948Spraks 		events = POLLIN;
113*2948Spraks 	}
114*2948Spraks 	mutex_exit(&portq->portq_mutex);
115*2948Spraks 	return (events);
116*2948Spraks }
117*2948Spraks 
118*2948Spraks void
119*2948Spraks port_pollwkdone(port_t *pp)
120*2948Spraks {
121*2948Spraks 	port_queue_t *portq;
122*2948Spraks 	portq = &pp->port_queue;
123*2948Spraks 	ASSERT(portq->portq_flags & PORTQ_POLLWK_PEND);
124*2948Spraks 	mutex_enter(&portq->portq_mutex);
125*2948Spraks 	portq->portq_flags &= ~PORTQ_POLLWK_PEND;
126*2948Spraks 	cv_signal(&pp->port_cv);
127*2948Spraks 	mutex_exit(&portq->portq_mutex);
128*2948Spraks }
129*2948Spraks 
130*2948Spraks 
131*2948Spraks /*
1320Sstevel@tonic-gate  * The port_send_event() function is used by all event sources to submit
1330Sstevel@tonic-gate  * trigerred events to a port. All the data  required for the event management
1340Sstevel@tonic-gate  * is already stored in the port_kevent_t structure.
1350Sstevel@tonic-gate  * The event port internal data is stored in the port_kevent_t structure
1360Sstevel@tonic-gate  * during the allocation time (see port_alloc_event()). The data related to
1370Sstevel@tonic-gate  * the event itself and to the event source management is stored in the
1380Sstevel@tonic-gate  * port_kevent_t structure between the allocation time and submit time
1390Sstevel@tonic-gate  * (see port_init_event()).
1400Sstevel@tonic-gate  *
1410Sstevel@tonic-gate  * This function is often called from interrupt level.
1420Sstevel@tonic-gate  */
1431885Sraf void
1440Sstevel@tonic-gate port_send_event(port_kevent_t *pkevp)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate 	port_queue_t	*portq;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	portq = &pkevp->portkev_port->port_queue;
1490Sstevel@tonic-gate 	mutex_enter(&portq->portq_mutex);
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	if (pkevp->portkev_flags & PORT_KEV_DONEQ) {
1520Sstevel@tonic-gate 		/* Event already in the port queue */
153*2948Spraks 		if (pkevp->portkev_source == PORT_SOURCE_FD) {
1541425Spraks 			mutex_exit(&pkevp->portkev_lock);
1551425Spraks 		}
1560Sstevel@tonic-gate 		mutex_exit(&portq->portq_mutex);
1571885Sraf 		return;
1580Sstevel@tonic-gate 	}
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	/* put event in the port queue */
1610Sstevel@tonic-gate 	list_insert_tail(&portq->portq_list, pkevp);
1620Sstevel@tonic-gate 	portq->portq_nent++;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	/*
1651885Sraf 	 * Remove the PORTQ_WAIT_EVENTS flag to indicate
1661885Sraf 	 * that new events are available.
1670Sstevel@tonic-gate 	 */
1680Sstevel@tonic-gate 	portq->portq_flags &= ~PORTQ_WAIT_EVENTS;
1690Sstevel@tonic-gate 	pkevp->portkev_flags |= PORT_KEV_DONEQ;		/* event enqueued */
1700Sstevel@tonic-gate 
171*2948Spraks 	if (pkevp->portkev_source == PORT_SOURCE_FD) {
1721425Spraks 		mutex_exit(&pkevp->portkev_lock);
1731425Spraks 	}
1741425Spraks 
1750Sstevel@tonic-gate 	/* Check if thread is in port_close() waiting for outstanding events */
1760Sstevel@tonic-gate 	if (portq->portq_flags & PORTQ_CLOSE) {
1770Sstevel@tonic-gate 		/* Check if all outstanding events are already in port queue */
1780Sstevel@tonic-gate 		if (pkevp->portkev_port->port_curr <= portq->portq_nent)
1790Sstevel@tonic-gate 			cv_signal(&portq->portq_closecv);
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	if (portq->portq_getn == 0) {
1830Sstevel@tonic-gate 		/*
1840Sstevel@tonic-gate 		 * No thread retrieving events -> check if enough events are
1850Sstevel@tonic-gate 		 * available to satify waiting threads.
1860Sstevel@tonic-gate 		 */
1870Sstevel@tonic-gate 		if (portq->portq_thread &&
1880Sstevel@tonic-gate 		    (portq->portq_nent >= portq->portq_nget))
1890Sstevel@tonic-gate 			cv_signal(&portq->portq_thread->portget_cv);
1900Sstevel@tonic-gate 	}
1910Sstevel@tonic-gate 
192*2948Spraks 	/*
193*2948Spraks 	 * If some thread is polling the port's fd, then notify it.
194*2948Spraks 	 * For PORT_SOURCE_FD source, we don't need to call pollwakeup()
195*2948Spraks 	 * here as it will result in a recursive call(PORT_SOURCE_FD source
196*2948Spraks 	 * is pollwakeup()). Therefore pollwakeup() itself will  notify the
197*2948Spraks 	 * ports if being polled.
198*2948Spraks 	 */
199*2948Spraks 	if (pkevp->portkev_source != PORT_SOURCE_FD &&
200*2948Spraks 				portq->portq_flags & PORTQ_POLLIN) {
2010Sstevel@tonic-gate 		portq->portq_flags &= ~PORTQ_POLLIN;
2020Sstevel@tonic-gate 		mutex_exit(&portq->portq_mutex);
2030Sstevel@tonic-gate 		pollwakeup(&pkevp->portkev_port->port_pollhd, POLLIN);
2040Sstevel@tonic-gate 	} else {
2050Sstevel@tonic-gate 		mutex_exit(&portq->portq_mutex);
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate /*
2100Sstevel@tonic-gate  * The port_alloc_event() function has to be used by all event sources
2110Sstevel@tonic-gate  * to request an slot for event notification.
2120Sstevel@tonic-gate  * The slot reservation could be denied because of lack of resources.
2130Sstevel@tonic-gate  * For that reason the event source should allocate an event slot as early
2140Sstevel@tonic-gate  * as possible and be prepared to get an error code instead of the
2150Sstevel@tonic-gate  * port event pointer.
2160Sstevel@tonic-gate  * Al current event sources allocate an event slot during a system call
2170Sstevel@tonic-gate  * entry. They return an error code to the application if an event slot
2180Sstevel@tonic-gate  * could not be reserved.
2190Sstevel@tonic-gate  * It is also recommended to associate the event source with the port
2200Sstevel@tonic-gate  * before some other port function is used.
2210Sstevel@tonic-gate  * The port argument is a file descriptor obtained by the application as
2220Sstevel@tonic-gate  * a return value of port_create().
2230Sstevel@tonic-gate  * Possible values of flags are:
2240Sstevel@tonic-gate  * PORT_ALLOC_DEFAULT
2250Sstevel@tonic-gate  *	This is the standard type of port events. port_get(n) will free this
2260Sstevel@tonic-gate  *	type of event structures as soon as the events are delivered to the
2270Sstevel@tonic-gate  *	application.
2280Sstevel@tonic-gate  * PORT_ALLOC_PRIVATE
2290Sstevel@tonic-gate  *	This type of event will be use for private use of the event source.
2300Sstevel@tonic-gate  *	The port_get(n) function will deliver events of such an structure to
2310Sstevel@tonic-gate  *	the application but it will not free the event structure itself.
2320Sstevel@tonic-gate  *	The event source must free this structure using port_free_event().
2330Sstevel@tonic-gate  * PORT_ALLOC_CACHED
2340Sstevel@tonic-gate  *	This type of events is used when the event source helds an own
2350Sstevel@tonic-gate  *	cache.
2360Sstevel@tonic-gate  *	The port_get(n) function will deliver events of such an structure to
2370Sstevel@tonic-gate  *	the application but it will not free the event structure itself.
2380Sstevel@tonic-gate  *	The event source must free this structure using port_free_event().
2390Sstevel@tonic-gate  */
2400Sstevel@tonic-gate int
2410Sstevel@tonic-gate port_alloc_event(int port, int flags, int source, port_kevent_t **pkevpp)
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate 	port_t		*pp;
2440Sstevel@tonic-gate 	file_t		*fp;
2451885Sraf 	port_kevent_t	*pkevp;
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	if ((fp = getf(port)) == NULL)
2480Sstevel@tonic-gate 		return (EBADF);
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	if (fp->f_vnode->v_type != VPORT) {
2510Sstevel@tonic-gate 		releasef(port);
2520Sstevel@tonic-gate 		return (EBADFD);
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 
2551885Sraf 	pkevp = kmem_cache_alloc(port_control.pc_cache, KM_NOSLEEP);
2561885Sraf 	if (pkevp == NULL) {
2571885Sraf 		releasef(port);
2581885Sraf 		return (ENOMEM);
2591885Sraf 	}
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	/*
2620Sstevel@tonic-gate 	 * port_max_events is controlled by the resource control
2630Sstevel@tonic-gate 	 * process.port-max-events
2640Sstevel@tonic-gate 	 */
2651885Sraf 	pp = VTOEP(fp->f_vnode);
2661885Sraf 	mutex_enter(&pp->port_queue.portq_mutex);
2670Sstevel@tonic-gate 	if (pp->port_curr >= pp->port_max_events) {
2681885Sraf 		mutex_exit(&pp->port_queue.portq_mutex);
2691885Sraf 		kmem_cache_free(port_control.pc_cache, pkevp);
2700Sstevel@tonic-gate 		releasef(port);
2710Sstevel@tonic-gate 		return (EAGAIN);
2720Sstevel@tonic-gate 	}
2731885Sraf 	pp->port_curr++;
2741885Sraf 	mutex_exit(&pp->port_queue.portq_mutex);
2750Sstevel@tonic-gate 
2761885Sraf 	bzero(pkevp, sizeof (port_kevent_t));
2771885Sraf 	mutex_init(&pkevp->portkev_lock, NULL, MUTEX_DEFAULT, NULL);
2781885Sraf 	pkevp->portkev_source = source;
2791885Sraf 	pkevp->portkev_flags = flags;
2801885Sraf 	pkevp->portkev_pid = curproc->p_pid;
2811885Sraf 	pkevp->portkev_port = pp;
2821885Sraf 	*pkevpp = pkevp;
2830Sstevel@tonic-gate 	releasef(port);
2840Sstevel@tonic-gate 	return (0);
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate /*
2880Sstevel@tonic-gate  * This function is faster than the standard port_alloc_event() and
2890Sstevel@tonic-gate  * can be used when the event source already allocated an event from
2900Sstevel@tonic-gate  * a port.
2910Sstevel@tonic-gate  */
2920Sstevel@tonic-gate int
2930Sstevel@tonic-gate port_dup_event(port_kevent_t *pkevp, port_kevent_t **pkevdupp, int flags)
2940Sstevel@tonic-gate {
2950Sstevel@tonic-gate 	int	error;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	error = port_alloc_event_local(pkevp->portkev_port,
2980Sstevel@tonic-gate 	    pkevp->portkev_source, flags, pkevdupp);
2990Sstevel@tonic-gate 	if (error == 0)
3000Sstevel@tonic-gate 		(*pkevdupp)->portkev_pid = pkevp->portkev_pid;
3010Sstevel@tonic-gate 	return (error);
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate /*
3050Sstevel@tonic-gate  * port_alloc_event_local() is reserved for internal use only.
3060Sstevel@tonic-gate  * It is doing the same job as port_alloc_event() but with the event port
3070Sstevel@tonic-gate  * pointer as the first argument.
3080Sstevel@tonic-gate  * The check of the validity of the port file descriptor is skipped here.
3090Sstevel@tonic-gate  */
3100Sstevel@tonic-gate int
3110Sstevel@tonic-gate port_alloc_event_local(port_t *pp, int source, int flags,
3120Sstevel@tonic-gate     port_kevent_t **pkevpp)
3130Sstevel@tonic-gate {
3141885Sraf 	port_kevent_t	*pkevp;
3150Sstevel@tonic-gate 
3161885Sraf 	pkevp = kmem_cache_alloc(port_control.pc_cache, KM_NOSLEEP);
3171885Sraf 	if (pkevp == NULL)
3180Sstevel@tonic-gate 		return (ENOMEM);
3190Sstevel@tonic-gate 
3201885Sraf 	mutex_enter(&pp->port_queue.portq_mutex);
3211885Sraf 	if (pp->port_curr >= pp->port_max_events) {
3221885Sraf 		mutex_exit(&pp->port_queue.portq_mutex);
3231885Sraf 		kmem_cache_free(port_control.pc_cache, pkevp);
3241885Sraf 		return (EAGAIN);
3251885Sraf 	}
3261885Sraf 	pp->port_curr++;
3271885Sraf 	mutex_exit(&pp->port_queue.portq_mutex);
3281885Sraf 
3291885Sraf 	bzero(pkevp, sizeof (port_kevent_t));
3301885Sraf 	mutex_init(&pkevp->portkev_lock, NULL, MUTEX_DEFAULT, NULL);
3311885Sraf 	pkevp->portkev_flags = flags;
3321885Sraf 	pkevp->portkev_port = pp;
3331885Sraf 	pkevp->portkev_source = source;
3341885Sraf 	pkevp->portkev_pid = curproc->p_pid;
3351885Sraf 	*pkevpp = pkevp;
3360Sstevel@tonic-gate 	return (0);
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate /*
3400Sstevel@tonic-gate  * port_alloc_event_block() has the same functionality of port_alloc_event() +
3410Sstevel@tonic-gate  * - it blocks if not enough event slots are available and
3420Sstevel@tonic-gate  * - it blocks if not enough memory is available.
3430Sstevel@tonic-gate  * Currently port_dispatch() is using this function to increase the
3440Sstevel@tonic-gate  * reliability of event delivery for library event sources.
3450Sstevel@tonic-gate  */
3460Sstevel@tonic-gate int
3470Sstevel@tonic-gate port_alloc_event_block(port_t *pp, int source, int flags,
3481885Sraf     port_kevent_t **pkevpp)
3490Sstevel@tonic-gate {
3501885Sraf 	port_kevent_t *pkevp =
3511885Sraf 	    kmem_cache_alloc(port_control.pc_cache, KM_SLEEP);
3520Sstevel@tonic-gate 
3531885Sraf 	mutex_enter(&pp->port_queue.portq_mutex);
3541885Sraf 	while (pp->port_curr >= pp->port_max_events) {
3551885Sraf 		if (!cv_wait_sig(&pp->port_cv, &pp->port_queue.portq_mutex)) {
3561885Sraf 			/* signal detected */
3571885Sraf 			mutex_exit(&pp->port_queue.portq_mutex);
3581885Sraf 			kmem_cache_free(port_control.pc_cache, pkevp);
3591885Sraf 			return (EINTR);
3600Sstevel@tonic-gate 		}
3610Sstevel@tonic-gate 	}
3621885Sraf 	pp->port_curr++;
3631885Sraf 	mutex_exit(&pp->port_queue.portq_mutex);
3640Sstevel@tonic-gate 
3651885Sraf 	bzero(pkevp, sizeof (port_kevent_t));
3661885Sraf 	mutex_init(&pkevp->portkev_lock, NULL, MUTEX_DEFAULT, NULL);
3671885Sraf 	pkevp->portkev_flags = flags;
3681885Sraf 	pkevp->portkev_port = pp;
3691885Sraf 	pkevp->portkev_source = source;
3701885Sraf 	pkevp->portkev_pid = curproc->p_pid;
3711885Sraf 	*pkevpp = pkevp;
3720Sstevel@tonic-gate 	return (0);
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate /*
3760Sstevel@tonic-gate  * Take an event out of the port queue
3770Sstevel@tonic-gate  */
3780Sstevel@tonic-gate static void
3790Sstevel@tonic-gate port_remove_event_doneq(port_kevent_t *pkevp, port_queue_t *portq)
3800Sstevel@tonic-gate {
3810Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&portq->portq_mutex));
3820Sstevel@tonic-gate 	list_remove(&portq->portq_list, pkevp);
3830Sstevel@tonic-gate 	portq->portq_nent--;
3840Sstevel@tonic-gate 	pkevp->portkev_flags &= ~PORT_KEV_DONEQ;
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate /*
3880Sstevel@tonic-gate  * The port_remove_done_event() function takes a fired event out of the
3890Sstevel@tonic-gate  * port queue.
3900Sstevel@tonic-gate  * Currently this function is required to cancel a fired event because
3910Sstevel@tonic-gate  * the application is delivering new association data (see port_associate_fd()).
3920Sstevel@tonic-gate  */
3930Sstevel@tonic-gate void
3940Sstevel@tonic-gate port_remove_done_event(port_kevent_t *pkevp)
3950Sstevel@tonic-gate {
3960Sstevel@tonic-gate 	port_queue_t	*portq;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	portq = &pkevp->portkev_port->port_queue;
3990Sstevel@tonic-gate 	mutex_enter(&portq->portq_mutex);
4000Sstevel@tonic-gate 	/* wait for port_get() or port_getn() */
4011885Sraf 	port_block(portq);
4020Sstevel@tonic-gate 	if (pkevp->portkev_flags & PORT_KEV_DONEQ) {
4030Sstevel@tonic-gate 		/* event still in port queue */
4040Sstevel@tonic-gate 		if (portq->portq_getn) {
4050Sstevel@tonic-gate 			/*
4060Sstevel@tonic-gate 			 * There could be still fired events in the temp queue;
4070Sstevel@tonic-gate 			 * push those events back to the port queue and
4080Sstevel@tonic-gate 			 * remove requested event afterwards.
4090Sstevel@tonic-gate 			 */
4100Sstevel@tonic-gate 			port_push_eventq(portq);
4110Sstevel@tonic-gate 		}
4120Sstevel@tonic-gate 		/* now remove event from the port queue */
4130Sstevel@tonic-gate 		port_remove_event_doneq(pkevp, portq);
4140Sstevel@tonic-gate 	}
4151885Sraf 	port_unblock(portq);
4160Sstevel@tonic-gate 	mutex_exit(&portq->portq_mutex);
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate /*
4200Sstevel@tonic-gate  * Return port event back to the kmem_cache.
4210Sstevel@tonic-gate  * If the event is currently in the port queue the event itself will only
4220Sstevel@tonic-gate  * be set as invalid. The port_get(n) function will not deliver such events
4230Sstevel@tonic-gate  * to the application and it will return them back to the kmem_cache.
4240Sstevel@tonic-gate  */
4250Sstevel@tonic-gate void
4260Sstevel@tonic-gate port_free_event(port_kevent_t *pkevp)
4270Sstevel@tonic-gate {
4280Sstevel@tonic-gate 	port_queue_t	*portq;
4290Sstevel@tonic-gate 	port_t		*pp;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	pp = pkevp->portkev_port;
4320Sstevel@tonic-gate 	if (pp == NULL)
4330Sstevel@tonic-gate 		return;
4340Sstevel@tonic-gate 	if (pkevp->portkev_flags & PORT_ALLOC_PRIVATE) {
4350Sstevel@tonic-gate 		port_free_event_local(pkevp, 0);
4360Sstevel@tonic-gate 		return;
4370Sstevel@tonic-gate 	}
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	portq = &pp->port_queue;
4400Sstevel@tonic-gate 	mutex_enter(&portq->portq_mutex);
4411885Sraf 	port_block(portq);
4420Sstevel@tonic-gate 	if (pkevp->portkev_flags & PORT_KEV_DONEQ) {
4430Sstevel@tonic-gate 		pkevp->portkev_flags |= PORT_KEV_FREE;
4440Sstevel@tonic-gate 		pkevp->portkev_callback = NULL;
4451885Sraf 		port_unblock(portq);
4460Sstevel@tonic-gate 		mutex_exit(&portq->portq_mutex);
4470Sstevel@tonic-gate 		return;
4480Sstevel@tonic-gate 	}
4491885Sraf 	port_unblock(portq);
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	if (pkevp->portkev_flags & PORT_KEV_CACHED) {
4520Sstevel@tonic-gate 		mutex_exit(&portq->portq_mutex);
4530Sstevel@tonic-gate 		return;
4540Sstevel@tonic-gate 	}
4550Sstevel@tonic-gate 
4561885Sraf 	if (--pp->port_curr < pp->port_max_events)
4571885Sraf 		cv_signal(&pp->port_cv);
4580Sstevel@tonic-gate 	if (portq->portq_flags & PORTQ_CLOSE) {
4590Sstevel@tonic-gate 		/*
4600Sstevel@tonic-gate 		 * Another thread is closing the event port.
4610Sstevel@tonic-gate 		 * That thread will sleep until all allocated event
4620Sstevel@tonic-gate 		 * structures returned to the event port framework.
4630Sstevel@tonic-gate 		 * The portq_mutex is used to synchronize the status
4640Sstevel@tonic-gate 		 * of the allocated event structures (port_curr).
4650Sstevel@tonic-gate 		 */
4660Sstevel@tonic-gate 		if (pp->port_curr <= portq->portq_nent)
4670Sstevel@tonic-gate 			cv_signal(&portq->portq_closecv);
4680Sstevel@tonic-gate 	}
4690Sstevel@tonic-gate 	mutex_exit(&portq->portq_mutex);
4700Sstevel@tonic-gate 	port_free_event_local(pkevp, 1);
4710Sstevel@tonic-gate }
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate /*
4740Sstevel@tonic-gate  * This event port internal function is used by port_free_event() and
4750Sstevel@tonic-gate  * other port internal functions to return event structures back to the
4760Sstevel@tonic-gate  * kmem_cache.
4770Sstevel@tonic-gate  */
4780Sstevel@tonic-gate void
4790Sstevel@tonic-gate port_free_event_local(port_kevent_t *pkevp, int counter)
4800Sstevel@tonic-gate {
4811885Sraf 	port_t *pp = pkevp->portkev_port;
4821885Sraf 	port_queue_t *portq = &pp->port_queue;
4831885Sraf 	int wakeup;
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	pkevp->portkev_callback = NULL;
4860Sstevel@tonic-gate 	pkevp->portkev_flags = 0;
4870Sstevel@tonic-gate 	pkevp->portkev_port = NULL;
4881425Spraks 	mutex_destroy(&pkevp->portkev_lock);
4890Sstevel@tonic-gate 	kmem_cache_free(port_control.pc_cache, pkevp);
4900Sstevel@tonic-gate 
4911885Sraf 	mutex_enter(&portq->portq_mutex);
4921885Sraf 	if (counter == 0) {
4931885Sraf 		if (--pp->port_curr < pp->port_max_events)
4941885Sraf 			cv_signal(&pp->port_cv);
4950Sstevel@tonic-gate 	}
4961885Sraf 	wakeup = (portq->portq_flags & PORTQ_POLLOUT);
4971885Sraf 	portq->portq_flags &= ~PORTQ_POLLOUT;
4981885Sraf 	mutex_exit(&portq->portq_mutex);
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	/* Submit a POLLOUT event if requested */
5011885Sraf 	if (wakeup)
5020Sstevel@tonic-gate 		pollwakeup(&pp->port_pollhd, POLLOUT);
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate /*
5060Sstevel@tonic-gate  * port_init_event(port_event_t *pev, uintptr_t object, void *user,
5070Sstevel@tonic-gate  *	int (*port_callback)(void *, int *, pid_t, int, void *), void *sysarg);
5080Sstevel@tonic-gate  *	This function initializes most of the "wired" elements of the port
5090Sstevel@tonic-gate  *	event structure. This is normally being used just after the allocation
5100Sstevel@tonic-gate  *	of the port event structure.
5110Sstevel@tonic-gate  *	pkevp	: pointer to the port event structure
5120Sstevel@tonic-gate  *	object	: object associated with this event structure
5130Sstevel@tonic-gate  *	user	: user defined pointer delivered with the association function
5140Sstevel@tonic-gate  *	port_callback:
5150Sstevel@tonic-gate  *		  Address of the callback function which will be called
5160Sstevel@tonic-gate  *		  - just before the event is delivered to the application.
5170Sstevel@tonic-gate  *		    The callback function is called in user context and can be
5180Sstevel@tonic-gate  *		    used for copyouts, e.g.
5190Sstevel@tonic-gate  *		  - on close() or dissociation of the event. The sub-system
5200Sstevel@tonic-gate  *		    must remove immediately every existing association of
5210Sstevel@tonic-gate  *		    some object with this event.
5220Sstevel@tonic-gate  *	sysarg	: event source propietary data
5230Sstevel@tonic-gate  */
5240Sstevel@tonic-gate void
5250Sstevel@tonic-gate port_init_event(port_kevent_t *pkevp, uintptr_t object, void *user,
5260Sstevel@tonic-gate     int (*port_callback)(void *, int *, pid_t, int, void *),
5270Sstevel@tonic-gate     void *sysarg)
5280Sstevel@tonic-gate {
5290Sstevel@tonic-gate 	pkevp->portkev_object = object;
5300Sstevel@tonic-gate 	pkevp->portkev_user = user;
5310Sstevel@tonic-gate 	pkevp->portkev_callback = port_callback;
5320Sstevel@tonic-gate 	pkevp->portkev_arg = sysarg;
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate /*
5360Sstevel@tonic-gate  * This routine removes a portfd_t from the fd cache's hash table.
5370Sstevel@tonic-gate  */
5380Sstevel@tonic-gate void
5390Sstevel@tonic-gate port_pcache_remove_fd(port_fdcache_t *pcp, portfd_t *pfd)
5400Sstevel@tonic-gate {
5410Sstevel@tonic-gate 	polldat_t	*lpdp;
5420Sstevel@tonic-gate 	polldat_t	*cpdp;
5430Sstevel@tonic-gate 	portfd_t	**bucket;
5440Sstevel@tonic-gate 	polldat_t	*pdp = PFTOD(pfd);
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pcp->pc_lock));
5470Sstevel@tonic-gate 	bucket = PORT_FD_BUCKET(pcp, pdp->pd_fd);
5480Sstevel@tonic-gate 	cpdp = PFTOD(*bucket);
5490Sstevel@tonic-gate 	if (pdp == cpdp) {
5500Sstevel@tonic-gate 		*bucket = PDTOF(pdp->pd_hashnext);
5511425Spraks 		if (--pcp->pc_fdcount == 0) {
5521425Spraks 			/*
5531425Spraks 			 * signal the thread which may have blocked in
5541425Spraks 			 * port_close_sourcefd() on lastclose waiting
5551425Spraks 			 * for pc_fdcount to drop to 0.
5561425Spraks 			 */
5571425Spraks 			cv_signal(&pcp->pc_lclosecv);
5581425Spraks 		}
5590Sstevel@tonic-gate 		kmem_free(pfd, sizeof (portfd_t));
5600Sstevel@tonic-gate 		return;
5610Sstevel@tonic-gate 	}
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	while (cpdp != NULL) {
5640Sstevel@tonic-gate 		lpdp = cpdp;
5650Sstevel@tonic-gate 		cpdp = cpdp->pd_hashnext;
5660Sstevel@tonic-gate 		if (cpdp == pdp) {
5670Sstevel@tonic-gate 			/* polldat struct found */
5680Sstevel@tonic-gate 			lpdp->pd_hashnext = pdp->pd_hashnext;
5691425Spraks 			if (--pcp->pc_fdcount == 0) {
5701425Spraks 				/*
5711425Spraks 				 * signal the thread which may have blocked in
5721425Spraks 				 * port_close_sourcefd() on lastclose waiting
5731425Spraks 				 * for pc_fdcount to drop to 0.
5741425Spraks 				 */
5751425Spraks 				cv_signal(&pcp->pc_lclosecv);
5761425Spraks 			}
5770Sstevel@tonic-gate 			break;
5780Sstevel@tonic-gate 		}
5790Sstevel@tonic-gate 	}
5800Sstevel@tonic-gate 	ASSERT(cpdp != NULL);
5810Sstevel@tonic-gate 	kmem_free(pfd, sizeof (portfd_t));
5820Sstevel@tonic-gate }
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate /*
5850Sstevel@tonic-gate  * The port_push_eventq() function is used to move all remaining events
5860Sstevel@tonic-gate  * from the temporary queue used in port_get(n)() to the standard port
5870Sstevel@tonic-gate  * queue.
5880Sstevel@tonic-gate  */
5890Sstevel@tonic-gate void
5900Sstevel@tonic-gate port_push_eventq(port_queue_t *portq)
5910Sstevel@tonic-gate {
5920Sstevel@tonic-gate 	/*
5930Sstevel@tonic-gate 	 * Append temporary portq_get_list to the port queue. On return
5940Sstevel@tonic-gate 	 * the temporary portq_get_list is empty.
5950Sstevel@tonic-gate 	 */
5960Sstevel@tonic-gate 	list_move_tail(&portq->portq_list, &portq->portq_get_list);
5970Sstevel@tonic-gate 	portq->portq_nent += portq->portq_tnent;
5980Sstevel@tonic-gate 	portq->portq_tnent = 0;
5990Sstevel@tonic-gate }
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate /*
6020Sstevel@tonic-gate  * The port_remove_fd_object() function frees all resources associated with
6030Sstevel@tonic-gate  * delivered portfd_t structure.
6040Sstevel@tonic-gate  */
6050Sstevel@tonic-gate void
6060Sstevel@tonic-gate port_remove_fd_object(portfd_t *pfd, port_t *pp, port_fdcache_t *pcp)
6070Sstevel@tonic-gate {
6080Sstevel@tonic-gate 	port_queue_t	*portq;
6090Sstevel@tonic-gate 	polldat_t	*pdp = PFTOD(pfd);
6100Sstevel@tonic-gate 	port_kevent_t	*pkevp;
6110Sstevel@tonic-gate 	int		error;
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pcp->pc_lock));
6140Sstevel@tonic-gate 	if (pdp->pd_php != NULL) {
6150Sstevel@tonic-gate 		pollhead_delete(pdp->pd_php, pdp);
6160Sstevel@tonic-gate 		pdp->pd_php = NULL;
6170Sstevel@tonic-gate 	}
6180Sstevel@tonic-gate 	pkevp =  pdp->pd_portev;
6190Sstevel@tonic-gate 	portq = &pp->port_queue;
6200Sstevel@tonic-gate 	mutex_enter(&portq->portq_mutex);
6211885Sraf 	port_block(portq);
6220Sstevel@tonic-gate 	if (pkevp->portkev_flags & PORT_KEV_DONEQ) {
6230Sstevel@tonic-gate 		if (portq->portq_getn && portq->portq_tnent) {
6240Sstevel@tonic-gate 			/*
6250Sstevel@tonic-gate 			 * move events from the temporary "get" queue
6260Sstevel@tonic-gate 			 * back to the port queue
6270Sstevel@tonic-gate 			 */
6280Sstevel@tonic-gate 			port_push_eventq(portq);
6290Sstevel@tonic-gate 		}
6300Sstevel@tonic-gate 		/* cleanup merged port queue */
6310Sstevel@tonic-gate 		port_remove_event_doneq(pkevp, portq);
6320Sstevel@tonic-gate 	}
6331885Sraf 	port_unblock(portq);
6340Sstevel@tonic-gate 	mutex_exit(&portq->portq_mutex);
6350Sstevel@tonic-gate 	if (pkevp->portkev_callback) {
6360Sstevel@tonic-gate 		(void) (*pkevp->portkev_callback)(pkevp->portkev_arg,
6370Sstevel@tonic-gate 		    &error, pkevp->portkev_pid, PORT_CALLBACK_DISSOCIATE,
6380Sstevel@tonic-gate 		    pkevp);
6390Sstevel@tonic-gate 	}
6400Sstevel@tonic-gate 	port_free_event_local(pkevp, 0);
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	/* remove polldat struct */
6430Sstevel@tonic-gate 	port_pcache_remove_fd(pcp, pfd);
6440Sstevel@tonic-gate }
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate /*
6470Sstevel@tonic-gate  * The port_close_fd() function dissociates a file descriptor from a port
6480Sstevel@tonic-gate  * and removes all allocated resources.
6490Sstevel@tonic-gate  * close(2) detects in the uf_entry_t structure that the fd is associated
6500Sstevel@tonic-gate  * with a port (at least one port).
6510Sstevel@tonic-gate  * The fd can be associated with several ports.
6520Sstevel@tonic-gate  */
6530Sstevel@tonic-gate void
6540Sstevel@tonic-gate port_close_pfd(portfd_t *pfd)
6550Sstevel@tonic-gate {
6560Sstevel@tonic-gate 	port_t		*pp;
6570Sstevel@tonic-gate 	port_fdcache_t	*pcp;
6580Sstevel@tonic-gate 
6591425Spraks 	/*
6601425Spraks 	 * the portfd_t passed in should be for this proc.
6611425Spraks 	 */
6621425Spraks 	ASSERT(curproc->p_pid == PFTOD(pfd)->pd_portev->portkev_pid);
6630Sstevel@tonic-gate 	pp = PFTOD(pfd)->pd_portev->portkev_port;
6640Sstevel@tonic-gate 	pcp = pp->port_queue.portq_pcp;
6650Sstevel@tonic-gate 	mutex_enter(&pcp->pc_lock);
6660Sstevel@tonic-gate 	port_remove_fd_object(pfd, pp, pcp);
6670Sstevel@tonic-gate 	mutex_exit(&pcp->pc_lock);
6680Sstevel@tonic-gate }
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate /*
6710Sstevel@tonic-gate  * The port_associate_ksource() function associates an event source with a port.
6720Sstevel@tonic-gate  * On port_close() all associated sources are requested to free all local
6730Sstevel@tonic-gate  * resources associated with the event port.
6740Sstevel@tonic-gate  * The association of a source with a port can only be done one time. Further
6750Sstevel@tonic-gate  * calls of this function will only increment the reference counter.
6760Sstevel@tonic-gate  * The allocated port_source_t structure is removed from the port as soon as
6770Sstevel@tonic-gate  * the reference counter becomes 0.
6780Sstevel@tonic-gate  */
6790Sstevel@tonic-gate /* ARGSUSED */
6800Sstevel@tonic-gate int
6810Sstevel@tonic-gate port_associate_ksource(int port, int source, port_source_t **portsrc,
6820Sstevel@tonic-gate     void (*port_src_close)(void *, int, pid_t, int), void *arg,
6830Sstevel@tonic-gate     int (*port_src_associate)(port_kevent_t *, int, int, uintptr_t, void *))
6840Sstevel@tonic-gate {
6850Sstevel@tonic-gate 	port_t		*pp;
6860Sstevel@tonic-gate 	file_t		*fp;
6870Sstevel@tonic-gate 	port_source_t	**ps;
6880Sstevel@tonic-gate 	port_source_t	*pse;
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	if ((fp = getf(port)) == NULL)
6910Sstevel@tonic-gate 		return (EBADF);
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	if (fp->f_vnode->v_type != VPORT) {
6940Sstevel@tonic-gate 		releasef(port);
6950Sstevel@tonic-gate 		return (EBADFD);
6960Sstevel@tonic-gate 	}
6970Sstevel@tonic-gate 	pp = VTOEP(fp->f_vnode);
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	mutex_enter(&pp->port_queue.portq_source_mutex);
7000Sstevel@tonic-gate 	ps = &pp->port_queue.portq_scache[PORT_SHASH(source)];
7010Sstevel@tonic-gate 	for (pse = *ps; pse != NULL; pse = pse->portsrc_next) {
7020Sstevel@tonic-gate 		if (pse->portsrc_source == source)
7030Sstevel@tonic-gate 			break;
7040Sstevel@tonic-gate 	}
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	if (pse == NULL) {
7070Sstevel@tonic-gate 		/* Create association of the event source with the port */
7080Sstevel@tonic-gate 		pse = kmem_zalloc(sizeof (port_source_t), KM_NOSLEEP);
7090Sstevel@tonic-gate 		if (pse == NULL) {
7100Sstevel@tonic-gate 			mutex_exit(&pp->port_queue.portq_source_mutex);
7110Sstevel@tonic-gate 			releasef(port);
7120Sstevel@tonic-gate 			return (ENOMEM);
7130Sstevel@tonic-gate 		}
7140Sstevel@tonic-gate 		pse->portsrc_source = source;
7150Sstevel@tonic-gate 		pse->portsrc_close = port_src_close;
7160Sstevel@tonic-gate 		pse->portsrc_closearg = arg;
7170Sstevel@tonic-gate 		pse->portsrc_cnt = 1;
7180Sstevel@tonic-gate 		if (*ps)
7190Sstevel@tonic-gate 			pse->portsrc_next = (*ps)->portsrc_next;
7200Sstevel@tonic-gate 		*ps = pse;
7210Sstevel@tonic-gate 	} else {
7220Sstevel@tonic-gate 		/* entry already available, source is only requesting count */
7230Sstevel@tonic-gate 		pse->portsrc_cnt++;
7240Sstevel@tonic-gate 	}
7250Sstevel@tonic-gate 	mutex_exit(&pp->port_queue.portq_source_mutex);
7260Sstevel@tonic-gate 	releasef(port);
7270Sstevel@tonic-gate 	if (portsrc)
7280Sstevel@tonic-gate 		*portsrc = pse;
7290Sstevel@tonic-gate 	return (0);
7300Sstevel@tonic-gate }
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate /*
7330Sstevel@tonic-gate  * The port_dissociate_ksource() function dissociates an event source from
7340Sstevel@tonic-gate  * a port.
7350Sstevel@tonic-gate  */
7360Sstevel@tonic-gate int
7370Sstevel@tonic-gate port_dissociate_ksource(int port, int source, port_source_t *ps)
7380Sstevel@tonic-gate {
7390Sstevel@tonic-gate 	port_t		*pp;
7400Sstevel@tonic-gate 	file_t		*fp;
7410Sstevel@tonic-gate 	port_source_t	**psh;
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	if (ps == NULL)
7440Sstevel@tonic-gate 		return (EINVAL);
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	if ((fp = getf(port)) == NULL)
7470Sstevel@tonic-gate 		return (EBADF);
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	if (fp->f_vnode->v_type != VPORT) {
7500Sstevel@tonic-gate 		releasef(port);
7510Sstevel@tonic-gate 		return (EBADFD);
7520Sstevel@tonic-gate 	}
7530Sstevel@tonic-gate 	pp = VTOEP(fp->f_vnode);
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	mutex_enter(&pp->port_queue.portq_source_mutex);
7560Sstevel@tonic-gate 	if (--ps->portsrc_cnt == 0) {
7570Sstevel@tonic-gate 		/* last association removed -> free source structure */
7580Sstevel@tonic-gate 		if (ps->portsrc_prev == NULL) {
7590Sstevel@tonic-gate 			/* first entry */
7600Sstevel@tonic-gate 			psh = &pp->port_queue.portq_scache[PORT_SHASH(source)];
7610Sstevel@tonic-gate 			*psh = ps->portsrc_next;
7620Sstevel@tonic-gate 			if (ps->portsrc_next)
7630Sstevel@tonic-gate 				ps->portsrc_next->portsrc_prev = NULL;
7640Sstevel@tonic-gate 		} else {
7650Sstevel@tonic-gate 			ps->portsrc_prev->portsrc_next = ps->portsrc_next;
7660Sstevel@tonic-gate 			if (ps->portsrc_next)
7670Sstevel@tonic-gate 				ps->portsrc_next->portsrc_prev =
7680Sstevel@tonic-gate 				    ps->portsrc_prev;
7690Sstevel@tonic-gate 		}
7700Sstevel@tonic-gate 		kmem_free(ps, sizeof (port_source_t));
7710Sstevel@tonic-gate 	}
7720Sstevel@tonic-gate 	mutex_exit(&pp->port_queue.portq_source_mutex);
7730Sstevel@tonic-gate 	releasef(port);
7740Sstevel@tonic-gate 	return (0);
7750Sstevel@tonic-gate }
776