xref: /onnv-gate/usr/src/uts/common/os/port_subr.c (revision 4863:7b14ad153d91)
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 /*
233734Spraks  * Copyright 2007 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 /*
862948Spraks  * Called from pollwakeup(PORT_SOURCE_FD source) to determine
872948Spraks  * if the port's fd needs to be notified of poll events. If yes,
882948Spraks  * we mark the port indicating that pollwakeup() is referring
892948Spraks  * it so that the port_t does not disappear.  pollwakeup()
902948Spraks  * calls port_pollwkdone() after notifying. In port_pollwkdone(),
912948Spraks  * we clear the hold on the port_t (clear PORTQ_POLLWK_PEND).
922948Spraks  */
932948Spraks int
942948Spraks port_pollwkup(port_t *pp)
952948Spraks {
962948Spraks 	int events = 0;
972948Spraks 	port_queue_t *portq;
982948Spraks 	portq = &pp->port_queue;
992948Spraks 	mutex_enter(&portq->portq_mutex);
1002948Spraks 
1012948Spraks 	/*
1022948Spraks 	 * Normally, we should not have a situation where PORTQ_POLLIN
1032948Spraks 	 * and PORTQ_POLLWK_PEND are set at the same time, but it is
1042948Spraks 	 * possible. So, in pollwakeup() we ensure that no new fd's get
1052948Spraks 	 * added to the pollhead between the time it notifies poll events
1062948Spraks 	 * and calls poll_wkupdone() where we clear the PORTQ_POLLWK_PEND flag.
1072948Spraks 	 */
1082948Spraks 	if (portq->portq_flags & PORTQ_POLLIN &&
1092948Spraks 	    !(portq->portq_flags & PORTQ_POLLWK_PEND)) {
1102948Spraks 		portq->portq_flags &= ~PORTQ_POLLIN;
1112948Spraks 		portq->portq_flags |= PORTQ_POLLWK_PEND;
1122948Spraks 		events = POLLIN;
1132948Spraks 	}
1142948Spraks 	mutex_exit(&portq->portq_mutex);
1152948Spraks 	return (events);
1162948Spraks }
1172948Spraks 
1182948Spraks void
1192948Spraks port_pollwkdone(port_t *pp)
1202948Spraks {
1212948Spraks 	port_queue_t *portq;
1222948Spraks 	portq = &pp->port_queue;
1232948Spraks 	ASSERT(portq->portq_flags & PORTQ_POLLWK_PEND);
1242948Spraks 	mutex_enter(&portq->portq_mutex);
1252948Spraks 	portq->portq_flags &= ~PORTQ_POLLWK_PEND;
1262948Spraks 	cv_signal(&pp->port_cv);
1272948Spraks 	mutex_exit(&portq->portq_mutex);
1282948Spraks }
1292948Spraks 
1302948Spraks 
1312948Spraks /*
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 */
1532948Spraks 		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 
1712948Spraks 	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 
1922948Spraks 	/*
1932948Spraks 	 * If some thread is polling the port's fd, then notify it.
1942948Spraks 	 * For PORT_SOURCE_FD source, we don't need to call pollwakeup()
1952948Spraks 	 * here as it will result in a recursive call(PORT_SOURCE_FD source
1962948Spraks 	 * is pollwakeup()). Therefore pollwakeup() itself will  notify the
1972948Spraks 	 * ports if being polled.
1982948Spraks 	 */
1992948Spraks 	if (pkevp->portkev_source != PORT_SOURCE_FD &&
200*4863Spraks 	    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  */
393*4863Spraks int
3940Sstevel@tonic-gate port_remove_done_event(port_kevent_t *pkevp)
3950Sstevel@tonic-gate {
3960Sstevel@tonic-gate 	port_queue_t	*portq;
397*4863Spraks 	int	removed = 0;
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	portq = &pkevp->portkev_port->port_queue;
4000Sstevel@tonic-gate 	mutex_enter(&portq->portq_mutex);
4010Sstevel@tonic-gate 	/* wait for port_get() or port_getn() */
4021885Sraf 	port_block(portq);
4030Sstevel@tonic-gate 	if (pkevp->portkev_flags & PORT_KEV_DONEQ) {
4040Sstevel@tonic-gate 		/* event still in port queue */
4050Sstevel@tonic-gate 		if (portq->portq_getn) {
4060Sstevel@tonic-gate 			/*
4070Sstevel@tonic-gate 			 * There could be still fired events in the temp queue;
4080Sstevel@tonic-gate 			 * push those events back to the port queue and
4090Sstevel@tonic-gate 			 * remove requested event afterwards.
4100Sstevel@tonic-gate 			 */
4110Sstevel@tonic-gate 			port_push_eventq(portq);
4120Sstevel@tonic-gate 		}
4130Sstevel@tonic-gate 		/* now remove event from the port queue */
4140Sstevel@tonic-gate 		port_remove_event_doneq(pkevp, portq);
415*4863Spraks 		removed = 1;
4160Sstevel@tonic-gate 	}
4171885Sraf 	port_unblock(portq);
4180Sstevel@tonic-gate 	mutex_exit(&portq->portq_mutex);
419*4863Spraks 	return (removed);
4200Sstevel@tonic-gate }
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate /*
4230Sstevel@tonic-gate  * Return port event back to the kmem_cache.
4240Sstevel@tonic-gate  * If the event is currently in the port queue the event itself will only
4250Sstevel@tonic-gate  * be set as invalid. The port_get(n) function will not deliver such events
4260Sstevel@tonic-gate  * to the application and it will return them back to the kmem_cache.
4270Sstevel@tonic-gate  */
4280Sstevel@tonic-gate void
4290Sstevel@tonic-gate port_free_event(port_kevent_t *pkevp)
4300Sstevel@tonic-gate {
4310Sstevel@tonic-gate 	port_queue_t	*portq;
4320Sstevel@tonic-gate 	port_t		*pp;
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	pp = pkevp->portkev_port;
4350Sstevel@tonic-gate 	if (pp == NULL)
4360Sstevel@tonic-gate 		return;
4370Sstevel@tonic-gate 	if (pkevp->portkev_flags & PORT_ALLOC_PRIVATE) {
4380Sstevel@tonic-gate 		port_free_event_local(pkevp, 0);
4390Sstevel@tonic-gate 		return;
4400Sstevel@tonic-gate 	}
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	portq = &pp->port_queue;
4430Sstevel@tonic-gate 	mutex_enter(&portq->portq_mutex);
4441885Sraf 	port_block(portq);
4450Sstevel@tonic-gate 	if (pkevp->portkev_flags & PORT_KEV_DONEQ) {
4460Sstevel@tonic-gate 		pkevp->portkev_flags |= PORT_KEV_FREE;
4470Sstevel@tonic-gate 		pkevp->portkev_callback = NULL;
4481885Sraf 		port_unblock(portq);
4490Sstevel@tonic-gate 		mutex_exit(&portq->portq_mutex);
4500Sstevel@tonic-gate 		return;
4510Sstevel@tonic-gate 	}
4521885Sraf 	port_unblock(portq);
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	if (pkevp->portkev_flags & PORT_KEV_CACHED) {
4550Sstevel@tonic-gate 		mutex_exit(&portq->portq_mutex);
4560Sstevel@tonic-gate 		return;
4570Sstevel@tonic-gate 	}
4580Sstevel@tonic-gate 
4591885Sraf 	if (--pp->port_curr < pp->port_max_events)
4601885Sraf 		cv_signal(&pp->port_cv);
4610Sstevel@tonic-gate 	if (portq->portq_flags & PORTQ_CLOSE) {
4620Sstevel@tonic-gate 		/*
4630Sstevel@tonic-gate 		 * Another thread is closing the event port.
4640Sstevel@tonic-gate 		 * That thread will sleep until all allocated event
4650Sstevel@tonic-gate 		 * structures returned to the event port framework.
4660Sstevel@tonic-gate 		 * The portq_mutex is used to synchronize the status
4670Sstevel@tonic-gate 		 * of the allocated event structures (port_curr).
4680Sstevel@tonic-gate 		 */
4690Sstevel@tonic-gate 		if (pp->port_curr <= portq->portq_nent)
4700Sstevel@tonic-gate 			cv_signal(&portq->portq_closecv);
4710Sstevel@tonic-gate 	}
4720Sstevel@tonic-gate 	mutex_exit(&portq->portq_mutex);
4730Sstevel@tonic-gate 	port_free_event_local(pkevp, 1);
4740Sstevel@tonic-gate }
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate /*
4770Sstevel@tonic-gate  * This event port internal function is used by port_free_event() and
4780Sstevel@tonic-gate  * other port internal functions to return event structures back to the
4790Sstevel@tonic-gate  * kmem_cache.
4800Sstevel@tonic-gate  */
4810Sstevel@tonic-gate void
4820Sstevel@tonic-gate port_free_event_local(port_kevent_t *pkevp, int counter)
4830Sstevel@tonic-gate {
4841885Sraf 	port_t *pp = pkevp->portkev_port;
4851885Sraf 	port_queue_t *portq = &pp->port_queue;
4861885Sraf 	int wakeup;
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	pkevp->portkev_callback = NULL;
4890Sstevel@tonic-gate 	pkevp->portkev_flags = 0;
4900Sstevel@tonic-gate 	pkevp->portkev_port = NULL;
4911425Spraks 	mutex_destroy(&pkevp->portkev_lock);
4920Sstevel@tonic-gate 	kmem_cache_free(port_control.pc_cache, pkevp);
4930Sstevel@tonic-gate 
4941885Sraf 	mutex_enter(&portq->portq_mutex);
4951885Sraf 	if (counter == 0) {
4961885Sraf 		if (--pp->port_curr < pp->port_max_events)
4971885Sraf 			cv_signal(&pp->port_cv);
4980Sstevel@tonic-gate 	}
4991885Sraf 	wakeup = (portq->portq_flags & PORTQ_POLLOUT);
5001885Sraf 	portq->portq_flags &= ~PORTQ_POLLOUT;
5011885Sraf 	mutex_exit(&portq->portq_mutex);
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	/* Submit a POLLOUT event if requested */
5041885Sraf 	if (wakeup)
5050Sstevel@tonic-gate 		pollwakeup(&pp->port_pollhd, POLLOUT);
5060Sstevel@tonic-gate }
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate /*
5090Sstevel@tonic-gate  * port_init_event(port_event_t *pev, uintptr_t object, void *user,
5100Sstevel@tonic-gate  *	int (*port_callback)(void *, int *, pid_t, int, void *), void *sysarg);
5110Sstevel@tonic-gate  *	This function initializes most of the "wired" elements of the port
5120Sstevel@tonic-gate  *	event structure. This is normally being used just after the allocation
5130Sstevel@tonic-gate  *	of the port event structure.
5140Sstevel@tonic-gate  *	pkevp	: pointer to the port event structure
5150Sstevel@tonic-gate  *	object	: object associated with this event structure
5160Sstevel@tonic-gate  *	user	: user defined pointer delivered with the association function
5170Sstevel@tonic-gate  *	port_callback:
5180Sstevel@tonic-gate  *		  Address of the callback function which will be called
5190Sstevel@tonic-gate  *		  - just before the event is delivered to the application.
5200Sstevel@tonic-gate  *		    The callback function is called in user context and can be
5210Sstevel@tonic-gate  *		    used for copyouts, e.g.
5220Sstevel@tonic-gate  *		  - on close() or dissociation of the event. The sub-system
5230Sstevel@tonic-gate  *		    must remove immediately every existing association of
5240Sstevel@tonic-gate  *		    some object with this event.
5250Sstevel@tonic-gate  *	sysarg	: event source propietary data
5260Sstevel@tonic-gate  */
5270Sstevel@tonic-gate void
5280Sstevel@tonic-gate port_init_event(port_kevent_t *pkevp, uintptr_t object, void *user,
5290Sstevel@tonic-gate     int (*port_callback)(void *, int *, pid_t, int, void *),
5300Sstevel@tonic-gate     void *sysarg)
5310Sstevel@tonic-gate {
5320Sstevel@tonic-gate 	pkevp->portkev_object = object;
5330Sstevel@tonic-gate 	pkevp->portkev_user = user;
5340Sstevel@tonic-gate 	pkevp->portkev_callback = port_callback;
5350Sstevel@tonic-gate 	pkevp->portkev_arg = sysarg;
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate /*
5390Sstevel@tonic-gate  * This routine removes a portfd_t from the fd cache's hash table.
5400Sstevel@tonic-gate  */
5410Sstevel@tonic-gate void
5420Sstevel@tonic-gate port_pcache_remove_fd(port_fdcache_t *pcp, portfd_t *pfd)
5430Sstevel@tonic-gate {
5440Sstevel@tonic-gate 	polldat_t	*lpdp;
5450Sstevel@tonic-gate 	polldat_t	*cpdp;
5460Sstevel@tonic-gate 	portfd_t	**bucket;
5470Sstevel@tonic-gate 	polldat_t	*pdp = PFTOD(pfd);
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pcp->pc_lock));
5500Sstevel@tonic-gate 	bucket = PORT_FD_BUCKET(pcp, pdp->pd_fd);
5510Sstevel@tonic-gate 	cpdp = PFTOD(*bucket);
5520Sstevel@tonic-gate 	if (pdp == cpdp) {
5530Sstevel@tonic-gate 		*bucket = PDTOF(pdp->pd_hashnext);
5541425Spraks 		if (--pcp->pc_fdcount == 0) {
5551425Spraks 			/*
5561425Spraks 			 * signal the thread which may have blocked in
5571425Spraks 			 * port_close_sourcefd() on lastclose waiting
5581425Spraks 			 * for pc_fdcount to drop to 0.
5591425Spraks 			 */
5601425Spraks 			cv_signal(&pcp->pc_lclosecv);
5611425Spraks 		}
5620Sstevel@tonic-gate 		kmem_free(pfd, sizeof (portfd_t));
5630Sstevel@tonic-gate 		return;
5640Sstevel@tonic-gate 	}
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	while (cpdp != NULL) {
5670Sstevel@tonic-gate 		lpdp = cpdp;
5680Sstevel@tonic-gate 		cpdp = cpdp->pd_hashnext;
5690Sstevel@tonic-gate 		if (cpdp == pdp) {
5700Sstevel@tonic-gate 			/* polldat struct found */
5710Sstevel@tonic-gate 			lpdp->pd_hashnext = pdp->pd_hashnext;
5721425Spraks 			if (--pcp->pc_fdcount == 0) {
5731425Spraks 				/*
5741425Spraks 				 * signal the thread which may have blocked in
5751425Spraks 				 * port_close_sourcefd() on lastclose waiting
5761425Spraks 				 * for pc_fdcount to drop to 0.
5771425Spraks 				 */
5781425Spraks 				cv_signal(&pcp->pc_lclosecv);
5791425Spraks 			}
5800Sstevel@tonic-gate 			break;
5810Sstevel@tonic-gate 		}
5820Sstevel@tonic-gate 	}
5830Sstevel@tonic-gate 	ASSERT(cpdp != NULL);
5840Sstevel@tonic-gate 	kmem_free(pfd, sizeof (portfd_t));
5850Sstevel@tonic-gate }
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate /*
5880Sstevel@tonic-gate  * The port_push_eventq() function is used to move all remaining events
5890Sstevel@tonic-gate  * from the temporary queue used in port_get(n)() to the standard port
5900Sstevel@tonic-gate  * queue.
5910Sstevel@tonic-gate  */
5920Sstevel@tonic-gate void
5930Sstevel@tonic-gate port_push_eventq(port_queue_t *portq)
5940Sstevel@tonic-gate {
5950Sstevel@tonic-gate 	/*
5960Sstevel@tonic-gate 	 * Append temporary portq_get_list to the port queue. On return
5970Sstevel@tonic-gate 	 * the temporary portq_get_list is empty.
5980Sstevel@tonic-gate 	 */
5990Sstevel@tonic-gate 	list_move_tail(&portq->portq_list, &portq->portq_get_list);
6000Sstevel@tonic-gate 	portq->portq_nent += portq->portq_tnent;
6010Sstevel@tonic-gate 	portq->portq_tnent = 0;
6020Sstevel@tonic-gate }
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate /*
6050Sstevel@tonic-gate  * The port_remove_fd_object() function frees all resources associated with
6063734Spraks  * delivered portfd_t structure. Returns 1 if the port_kevent was found
6073734Spraks  * and removed from the port queue.
6080Sstevel@tonic-gate  */
6093734Spraks int
6100Sstevel@tonic-gate port_remove_fd_object(portfd_t *pfd, port_t *pp, port_fdcache_t *pcp)
6110Sstevel@tonic-gate {
6120Sstevel@tonic-gate 	port_queue_t	*portq;
6130Sstevel@tonic-gate 	polldat_t	*pdp = PFTOD(pfd);
6140Sstevel@tonic-gate 	port_kevent_t	*pkevp;
6150Sstevel@tonic-gate 	int		error;
6163734Spraks 	int		removed = 0;
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pcp->pc_lock));
6190Sstevel@tonic-gate 	if (pdp->pd_php != NULL) {
6200Sstevel@tonic-gate 		pollhead_delete(pdp->pd_php, pdp);
6210Sstevel@tonic-gate 		pdp->pd_php = NULL;
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate 	pkevp =  pdp->pd_portev;
6240Sstevel@tonic-gate 	portq = &pp->port_queue;
6250Sstevel@tonic-gate 	mutex_enter(&portq->portq_mutex);
6261885Sraf 	port_block(portq);
6270Sstevel@tonic-gate 	if (pkevp->portkev_flags & PORT_KEV_DONEQ) {
6280Sstevel@tonic-gate 		if (portq->portq_getn && portq->portq_tnent) {
6290Sstevel@tonic-gate 			/*
6300Sstevel@tonic-gate 			 * move events from the temporary "get" queue
6310Sstevel@tonic-gate 			 * back to the port queue
6320Sstevel@tonic-gate 			 */
6330Sstevel@tonic-gate 			port_push_eventq(portq);
6340Sstevel@tonic-gate 		}
6350Sstevel@tonic-gate 		/* cleanup merged port queue */
6360Sstevel@tonic-gate 		port_remove_event_doneq(pkevp, portq);
6373734Spraks 		removed = 1;
6380Sstevel@tonic-gate 	}
6391885Sraf 	port_unblock(portq);
6400Sstevel@tonic-gate 	mutex_exit(&portq->portq_mutex);
6410Sstevel@tonic-gate 	if (pkevp->portkev_callback) {
6420Sstevel@tonic-gate 		(void) (*pkevp->portkev_callback)(pkevp->portkev_arg,
6430Sstevel@tonic-gate 		    &error, pkevp->portkev_pid, PORT_CALLBACK_DISSOCIATE,
6440Sstevel@tonic-gate 		    pkevp);
6450Sstevel@tonic-gate 	}
6460Sstevel@tonic-gate 	port_free_event_local(pkevp, 0);
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	/* remove polldat struct */
6490Sstevel@tonic-gate 	port_pcache_remove_fd(pcp, pfd);
6503734Spraks 	return (removed);
6510Sstevel@tonic-gate }
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate /*
6540Sstevel@tonic-gate  * The port_close_fd() function dissociates a file descriptor from a port
6550Sstevel@tonic-gate  * and removes all allocated resources.
6560Sstevel@tonic-gate  * close(2) detects in the uf_entry_t structure that the fd is associated
6570Sstevel@tonic-gate  * with a port (at least one port).
6580Sstevel@tonic-gate  * The fd can be associated with several ports.
6590Sstevel@tonic-gate  */
6600Sstevel@tonic-gate void
6610Sstevel@tonic-gate port_close_pfd(portfd_t *pfd)
6620Sstevel@tonic-gate {
6630Sstevel@tonic-gate 	port_t		*pp;
6640Sstevel@tonic-gate 	port_fdcache_t	*pcp;
6650Sstevel@tonic-gate 
6661425Spraks 	/*
6671425Spraks 	 * the portfd_t passed in should be for this proc.
6681425Spraks 	 */
6691425Spraks 	ASSERT(curproc->p_pid == PFTOD(pfd)->pd_portev->portkev_pid);
6700Sstevel@tonic-gate 	pp = PFTOD(pfd)->pd_portev->portkev_port;
6710Sstevel@tonic-gate 	pcp = pp->port_queue.portq_pcp;
6720Sstevel@tonic-gate 	mutex_enter(&pcp->pc_lock);
6733734Spraks 	(void) port_remove_fd_object(pfd, pp, pcp);
6740Sstevel@tonic-gate 	mutex_exit(&pcp->pc_lock);
6750Sstevel@tonic-gate }
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate /*
6780Sstevel@tonic-gate  * The port_associate_ksource() function associates an event source with a port.
6790Sstevel@tonic-gate  * On port_close() all associated sources are requested to free all local
6800Sstevel@tonic-gate  * resources associated with the event port.
6810Sstevel@tonic-gate  * The association of a source with a port can only be done one time. Further
6820Sstevel@tonic-gate  * calls of this function will only increment the reference counter.
6830Sstevel@tonic-gate  * The allocated port_source_t structure is removed from the port as soon as
6840Sstevel@tonic-gate  * the reference counter becomes 0.
6850Sstevel@tonic-gate  */
6860Sstevel@tonic-gate /* ARGSUSED */
6870Sstevel@tonic-gate int
6880Sstevel@tonic-gate port_associate_ksource(int port, int source, port_source_t **portsrc,
6890Sstevel@tonic-gate     void (*port_src_close)(void *, int, pid_t, int), void *arg,
6900Sstevel@tonic-gate     int (*port_src_associate)(port_kevent_t *, int, int, uintptr_t, void *))
6910Sstevel@tonic-gate {
6920Sstevel@tonic-gate 	port_t		*pp;
6930Sstevel@tonic-gate 	file_t		*fp;
6940Sstevel@tonic-gate 	port_source_t	**ps;
6950Sstevel@tonic-gate 	port_source_t	*pse;
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	if ((fp = getf(port)) == NULL)
6980Sstevel@tonic-gate 		return (EBADF);
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	if (fp->f_vnode->v_type != VPORT) {
7010Sstevel@tonic-gate 		releasef(port);
7020Sstevel@tonic-gate 		return (EBADFD);
7030Sstevel@tonic-gate 	}
7040Sstevel@tonic-gate 	pp = VTOEP(fp->f_vnode);
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	mutex_enter(&pp->port_queue.portq_source_mutex);
7070Sstevel@tonic-gate 	ps = &pp->port_queue.portq_scache[PORT_SHASH(source)];
7080Sstevel@tonic-gate 	for (pse = *ps; pse != NULL; pse = pse->portsrc_next) {
7090Sstevel@tonic-gate 		if (pse->portsrc_source == source)
7100Sstevel@tonic-gate 			break;
7110Sstevel@tonic-gate 	}
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	if (pse == NULL) {
7140Sstevel@tonic-gate 		/* Create association of the event source with the port */
7150Sstevel@tonic-gate 		pse = kmem_zalloc(sizeof (port_source_t), KM_NOSLEEP);
7160Sstevel@tonic-gate 		if (pse == NULL) {
7170Sstevel@tonic-gate 			mutex_exit(&pp->port_queue.portq_source_mutex);
7180Sstevel@tonic-gate 			releasef(port);
7190Sstevel@tonic-gate 			return (ENOMEM);
7200Sstevel@tonic-gate 		}
7210Sstevel@tonic-gate 		pse->portsrc_source = source;
7220Sstevel@tonic-gate 		pse->portsrc_close = port_src_close;
7230Sstevel@tonic-gate 		pse->portsrc_closearg = arg;
7240Sstevel@tonic-gate 		pse->portsrc_cnt = 1;
7250Sstevel@tonic-gate 		if (*ps)
7260Sstevel@tonic-gate 			pse->portsrc_next = (*ps)->portsrc_next;
7270Sstevel@tonic-gate 		*ps = pse;
7280Sstevel@tonic-gate 	} else {
7290Sstevel@tonic-gate 		/* entry already available, source is only requesting count */
7300Sstevel@tonic-gate 		pse->portsrc_cnt++;
7310Sstevel@tonic-gate 	}
7320Sstevel@tonic-gate 	mutex_exit(&pp->port_queue.portq_source_mutex);
7330Sstevel@tonic-gate 	releasef(port);
7340Sstevel@tonic-gate 	if (portsrc)
7350Sstevel@tonic-gate 		*portsrc = pse;
7360Sstevel@tonic-gate 	return (0);
7370Sstevel@tonic-gate }
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate /*
7400Sstevel@tonic-gate  * The port_dissociate_ksource() function dissociates an event source from
7410Sstevel@tonic-gate  * a port.
7420Sstevel@tonic-gate  */
7430Sstevel@tonic-gate int
7440Sstevel@tonic-gate port_dissociate_ksource(int port, int source, port_source_t *ps)
7450Sstevel@tonic-gate {
7460Sstevel@tonic-gate 	port_t		*pp;
7470Sstevel@tonic-gate 	file_t		*fp;
7480Sstevel@tonic-gate 	port_source_t	**psh;
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	if (ps == NULL)
7510Sstevel@tonic-gate 		return (EINVAL);
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	if ((fp = getf(port)) == NULL)
7540Sstevel@tonic-gate 		return (EBADF);
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 	if (fp->f_vnode->v_type != VPORT) {
7570Sstevel@tonic-gate 		releasef(port);
7580Sstevel@tonic-gate 		return (EBADFD);
7590Sstevel@tonic-gate 	}
7600Sstevel@tonic-gate 	pp = VTOEP(fp->f_vnode);
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	mutex_enter(&pp->port_queue.portq_source_mutex);
7630Sstevel@tonic-gate 	if (--ps->portsrc_cnt == 0) {
7640Sstevel@tonic-gate 		/* last association removed -> free source structure */
7650Sstevel@tonic-gate 		if (ps->portsrc_prev == NULL) {
7660Sstevel@tonic-gate 			/* first entry */
7670Sstevel@tonic-gate 			psh = &pp->port_queue.portq_scache[PORT_SHASH(source)];
7680Sstevel@tonic-gate 			*psh = ps->portsrc_next;
7690Sstevel@tonic-gate 			if (ps->portsrc_next)
7700Sstevel@tonic-gate 				ps->portsrc_next->portsrc_prev = NULL;
7710Sstevel@tonic-gate 		} else {
7720Sstevel@tonic-gate 			ps->portsrc_prev->portsrc_next = ps->portsrc_next;
7730Sstevel@tonic-gate 			if (ps->portsrc_next)
7740Sstevel@tonic-gate 				ps->portsrc_next->portsrc_prev =
7750Sstevel@tonic-gate 				    ps->portsrc_prev;
7760Sstevel@tonic-gate 		}
7770Sstevel@tonic-gate 		kmem_free(ps, sizeof (port_source_t));
7780Sstevel@tonic-gate 	}
7790Sstevel@tonic-gate 	mutex_exit(&pp->port_queue.portq_source_mutex);
7800Sstevel@tonic-gate 	releasef(port);
7810Sstevel@tonic-gate 	return (0);
7820Sstevel@tonic-gate }
783*4863Spraks 
784*4863Spraks void
785*4863Spraks free_fopdata(vnode_t *vp)
786*4863Spraks {
787*4863Spraks 	portfop_vp_t *pvp;
788*4863Spraks 	pvp = vp->v_fopdata;
789*4863Spraks 	ASSERT(pvp->pvp_femp == NULL);
790*4863Spraks 	mutex_destroy(&pvp->pvp_mutex);
791*4863Spraks 	list_destroy(&pvp->pvp_pfoplist);
792*4863Spraks 	kmem_free(pvp, sizeof (*pvp));
793*4863Spraks 	vp->v_fopdata = NULL;
794*4863Spraks }
795