xref: /onnv-gate/usr/src/uts/common/os/port_subr.c (revision 1885:7bbaa5935f99)
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
5*1885Sraf  * Common Development and Distribution License (the "License").
6*1885Sraf  * 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  */
21*1885Sraf 
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 /*
58*1885Sraf  * Block other threads from using a port.
59*1885Sraf  * We enter holding portq->portq_mutex but
60*1885Sraf  * we may drop and reacquire this lock.
61*1885Sraf  * Callers must deal with this fact.
62*1885Sraf  */
63*1885Sraf void
64*1885Sraf port_block(port_queue_t *portq)
65*1885Sraf {
66*1885Sraf 	ASSERT(MUTEX_HELD(&portq->portq_mutex));
67*1885Sraf 
68*1885Sraf 	while (portq->portq_flags & PORTQ_BLOCKED)
69*1885Sraf 		cv_wait(&portq->portq_block_cv, &portq->portq_mutex);
70*1885Sraf 	portq->portq_flags |= PORTQ_BLOCKED;
71*1885Sraf }
72*1885Sraf 
73*1885Sraf /*
74*1885Sraf  * Undo port_block(portq).
75*1885Sraf  */
76*1885Sraf void
77*1885Sraf port_unblock(port_queue_t *portq)
78*1885Sraf {
79*1885Sraf 	ASSERT(MUTEX_HELD(&portq->portq_mutex));
80*1885Sraf 
81*1885Sraf 	portq->portq_flags &= ~PORTQ_BLOCKED;
82*1885Sraf 	cv_signal(&portq->portq_block_cv);
83*1885Sraf }
84*1885Sraf 
85*1885Sraf /*
860Sstevel@tonic-gate  * The port_send_event() function is used by all event sources to submit
870Sstevel@tonic-gate  * trigerred events to a port. All the data  required for the event management
880Sstevel@tonic-gate  * is already stored in the port_kevent_t structure.
890Sstevel@tonic-gate  * The event port internal data is stored in the port_kevent_t structure
900Sstevel@tonic-gate  * during the allocation time (see port_alloc_event()). The data related to
910Sstevel@tonic-gate  * the event itself and to the event source management is stored in the
920Sstevel@tonic-gate  * port_kevent_t structure between the allocation time and submit time
930Sstevel@tonic-gate  * (see port_init_event()).
940Sstevel@tonic-gate  *
950Sstevel@tonic-gate  * This function is often called from interrupt level.
960Sstevel@tonic-gate  */
97*1885Sraf void
980Sstevel@tonic-gate port_send_event(port_kevent_t *pkevp)
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate 	port_queue_t	*portq;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	portq = &pkevp->portkev_port->port_queue;
1030Sstevel@tonic-gate 	mutex_enter(&portq->portq_mutex);
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	if (pkevp->portkev_flags & PORT_KEV_DONEQ) {
1060Sstevel@tonic-gate 		/* Event already in the port queue */
1071425Spraks 		if (pkevp->portkev_flags & PORT_ALLOC_CACHED) {
1081425Spraks 			mutex_exit(&pkevp->portkev_lock);
1091425Spraks 		}
1100Sstevel@tonic-gate 		mutex_exit(&portq->portq_mutex);
111*1885Sraf 		return;
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	/* put event in the port queue */
1150Sstevel@tonic-gate 	list_insert_tail(&portq->portq_list, pkevp);
1160Sstevel@tonic-gate 	portq->portq_nent++;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	/*
119*1885Sraf 	 * Remove the PORTQ_WAIT_EVENTS flag to indicate
120*1885Sraf 	 * that new events are available.
1210Sstevel@tonic-gate 	 */
1220Sstevel@tonic-gate 	portq->portq_flags &= ~PORTQ_WAIT_EVENTS;
1230Sstevel@tonic-gate 	pkevp->portkev_flags |= PORT_KEV_DONEQ;		/* event enqueued */
1240Sstevel@tonic-gate 
1251425Spraks 	if (pkevp->portkev_flags & PORT_ALLOC_CACHED) {
1261425Spraks 		mutex_exit(&pkevp->portkev_lock);
1271425Spraks 	}
1281425Spraks 
1290Sstevel@tonic-gate 	/* Check if thread is in port_close() waiting for outstanding events */
1300Sstevel@tonic-gate 	if (portq->portq_flags & PORTQ_CLOSE) {
1310Sstevel@tonic-gate 		/* Check if all outstanding events are already in port queue */
1320Sstevel@tonic-gate 		if (pkevp->portkev_port->port_curr <= portq->portq_nent)
1330Sstevel@tonic-gate 			cv_signal(&portq->portq_closecv);
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	if (portq->portq_getn == 0) {
1370Sstevel@tonic-gate 		/*
1380Sstevel@tonic-gate 		 * No thread retrieving events -> check if enough events are
1390Sstevel@tonic-gate 		 * available to satify waiting threads.
1400Sstevel@tonic-gate 		 */
1410Sstevel@tonic-gate 		if (portq->portq_thread &&
1420Sstevel@tonic-gate 		    (portq->portq_nent >= portq->portq_nget))
1430Sstevel@tonic-gate 			cv_signal(&portq->portq_thread->portget_cv);
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	if (portq->portq_flags & PORTQ_POLLIN) {
1470Sstevel@tonic-gate 		portq->portq_flags &= ~PORTQ_POLLIN;
1480Sstevel@tonic-gate 		mutex_exit(&portq->portq_mutex);
1490Sstevel@tonic-gate 		pollwakeup(&pkevp->portkev_port->port_pollhd, POLLIN);
1500Sstevel@tonic-gate 	} else {
1510Sstevel@tonic-gate 		mutex_exit(&portq->portq_mutex);
1520Sstevel@tonic-gate 	}
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate /*
1560Sstevel@tonic-gate  * The port_alloc_event() function has to be used by all event sources
1570Sstevel@tonic-gate  * to request an slot for event notification.
1580Sstevel@tonic-gate  * The slot reservation could be denied because of lack of resources.
1590Sstevel@tonic-gate  * For that reason the event source should allocate an event slot as early
1600Sstevel@tonic-gate  * as possible and be prepared to get an error code instead of the
1610Sstevel@tonic-gate  * port event pointer.
1620Sstevel@tonic-gate  * Al current event sources allocate an event slot during a system call
1630Sstevel@tonic-gate  * entry. They return an error code to the application if an event slot
1640Sstevel@tonic-gate  * could not be reserved.
1650Sstevel@tonic-gate  * It is also recommended to associate the event source with the port
1660Sstevel@tonic-gate  * before some other port function is used.
1670Sstevel@tonic-gate  * The port argument is a file descriptor obtained by the application as
1680Sstevel@tonic-gate  * a return value of port_create().
1690Sstevel@tonic-gate  * Possible values of flags are:
1700Sstevel@tonic-gate  * PORT_ALLOC_DEFAULT
1710Sstevel@tonic-gate  *	This is the standard type of port events. port_get(n) will free this
1720Sstevel@tonic-gate  *	type of event structures as soon as the events are delivered to the
1730Sstevel@tonic-gate  *	application.
1740Sstevel@tonic-gate  * PORT_ALLOC_PRIVATE
1750Sstevel@tonic-gate  *	This type of event will be use for private use of the event source.
1760Sstevel@tonic-gate  *	The port_get(n) function will deliver events of such an structure to
1770Sstevel@tonic-gate  *	the application but it will not free the event structure itself.
1780Sstevel@tonic-gate  *	The event source must free this structure using port_free_event().
1790Sstevel@tonic-gate  * PORT_ALLOC_CACHED
1800Sstevel@tonic-gate  *	This type of events is used when the event source helds an own
1810Sstevel@tonic-gate  *	cache.
1820Sstevel@tonic-gate  *	The port_get(n) function will deliver events of such an structure to
1830Sstevel@tonic-gate  *	the application but it will not free the event structure itself.
1840Sstevel@tonic-gate  *	The event source must free this structure using port_free_event().
1850Sstevel@tonic-gate  */
1860Sstevel@tonic-gate int
1870Sstevel@tonic-gate port_alloc_event(int port, int flags, int source, port_kevent_t **pkevpp)
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate 	port_t		*pp;
1900Sstevel@tonic-gate 	file_t		*fp;
191*1885Sraf 	port_kevent_t	*pkevp;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	if ((fp = getf(port)) == NULL)
1940Sstevel@tonic-gate 		return (EBADF);
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	if (fp->f_vnode->v_type != VPORT) {
1970Sstevel@tonic-gate 		releasef(port);
1980Sstevel@tonic-gate 		return (EBADFD);
1990Sstevel@tonic-gate 	}
2000Sstevel@tonic-gate 
201*1885Sraf 	pkevp = kmem_cache_alloc(port_control.pc_cache, KM_NOSLEEP);
202*1885Sraf 	if (pkevp == NULL) {
203*1885Sraf 		releasef(port);
204*1885Sraf 		return (ENOMEM);
205*1885Sraf 	}
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	/*
2080Sstevel@tonic-gate 	 * port_max_events is controlled by the resource control
2090Sstevel@tonic-gate 	 * process.port-max-events
2100Sstevel@tonic-gate 	 */
211*1885Sraf 	pp = VTOEP(fp->f_vnode);
212*1885Sraf 	mutex_enter(&pp->port_queue.portq_mutex);
2130Sstevel@tonic-gate 	if (pp->port_curr >= pp->port_max_events) {
214*1885Sraf 		mutex_exit(&pp->port_queue.portq_mutex);
215*1885Sraf 		kmem_cache_free(port_control.pc_cache, pkevp);
2160Sstevel@tonic-gate 		releasef(port);
2170Sstevel@tonic-gate 		return (EAGAIN);
2180Sstevel@tonic-gate 	}
219*1885Sraf 	pp->port_curr++;
220*1885Sraf 	mutex_exit(&pp->port_queue.portq_mutex);
2210Sstevel@tonic-gate 
222*1885Sraf 	bzero(pkevp, sizeof (port_kevent_t));
223*1885Sraf 	mutex_init(&pkevp->portkev_lock, NULL, MUTEX_DEFAULT, NULL);
224*1885Sraf 	pkevp->portkev_source = source;
225*1885Sraf 	pkevp->portkev_flags = flags;
226*1885Sraf 	pkevp->portkev_pid = curproc->p_pid;
227*1885Sraf 	pkevp->portkev_port = pp;
228*1885Sraf 	*pkevpp = pkevp;
2290Sstevel@tonic-gate 	releasef(port);
2300Sstevel@tonic-gate 	return (0);
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate /*
2340Sstevel@tonic-gate  * This function is faster than the standard port_alloc_event() and
2350Sstevel@tonic-gate  * can be used when the event source already allocated an event from
2360Sstevel@tonic-gate  * a port.
2370Sstevel@tonic-gate  */
2380Sstevel@tonic-gate int
2390Sstevel@tonic-gate port_dup_event(port_kevent_t *pkevp, port_kevent_t **pkevdupp, int flags)
2400Sstevel@tonic-gate {
2410Sstevel@tonic-gate 	int	error;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	error = port_alloc_event_local(pkevp->portkev_port,
2440Sstevel@tonic-gate 	    pkevp->portkev_source, flags, pkevdupp);
2450Sstevel@tonic-gate 	if (error == 0)
2460Sstevel@tonic-gate 		(*pkevdupp)->portkev_pid = pkevp->portkev_pid;
2470Sstevel@tonic-gate 	return (error);
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate /*
2510Sstevel@tonic-gate  * port_alloc_event_local() is reserved for internal use only.
2520Sstevel@tonic-gate  * It is doing the same job as port_alloc_event() but with the event port
2530Sstevel@tonic-gate  * pointer as the first argument.
2540Sstevel@tonic-gate  * The check of the validity of the port file descriptor is skipped here.
2550Sstevel@tonic-gate  */
2560Sstevel@tonic-gate int
2570Sstevel@tonic-gate port_alloc_event_local(port_t *pp, int source, int flags,
2580Sstevel@tonic-gate     port_kevent_t **pkevpp)
2590Sstevel@tonic-gate {
260*1885Sraf 	port_kevent_t	*pkevp;
2610Sstevel@tonic-gate 
262*1885Sraf 	pkevp = kmem_cache_alloc(port_control.pc_cache, KM_NOSLEEP);
263*1885Sraf 	if (pkevp == NULL)
2640Sstevel@tonic-gate 		return (ENOMEM);
2650Sstevel@tonic-gate 
266*1885Sraf 	mutex_enter(&pp->port_queue.portq_mutex);
267*1885Sraf 	if (pp->port_curr >= pp->port_max_events) {
268*1885Sraf 		mutex_exit(&pp->port_queue.portq_mutex);
269*1885Sraf 		kmem_cache_free(port_control.pc_cache, pkevp);
270*1885Sraf 		return (EAGAIN);
271*1885Sraf 	}
272*1885Sraf 	pp->port_curr++;
273*1885Sraf 	mutex_exit(&pp->port_queue.portq_mutex);
274*1885Sraf 
275*1885Sraf 	bzero(pkevp, sizeof (port_kevent_t));
276*1885Sraf 	mutex_init(&pkevp->portkev_lock, NULL, MUTEX_DEFAULT, NULL);
277*1885Sraf 	pkevp->portkev_flags = flags;
278*1885Sraf 	pkevp->portkev_port = pp;
279*1885Sraf 	pkevp->portkev_source = source;
280*1885Sraf 	pkevp->portkev_pid = curproc->p_pid;
281*1885Sraf 	*pkevpp = pkevp;
2820Sstevel@tonic-gate 	return (0);
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate /*
2860Sstevel@tonic-gate  * port_alloc_event_block() has the same functionality of port_alloc_event() +
2870Sstevel@tonic-gate  * - it blocks if not enough event slots are available and
2880Sstevel@tonic-gate  * - it blocks if not enough memory is available.
2890Sstevel@tonic-gate  * Currently port_dispatch() is using this function to increase the
2900Sstevel@tonic-gate  * reliability of event delivery for library event sources.
2910Sstevel@tonic-gate  */
2920Sstevel@tonic-gate int
2930Sstevel@tonic-gate port_alloc_event_block(port_t *pp, int source, int flags,
294*1885Sraf     port_kevent_t **pkevpp)
2950Sstevel@tonic-gate {
296*1885Sraf 	port_kevent_t *pkevp =
297*1885Sraf 	    kmem_cache_alloc(port_control.pc_cache, KM_SLEEP);
2980Sstevel@tonic-gate 
299*1885Sraf 	mutex_enter(&pp->port_queue.portq_mutex);
300*1885Sraf 	while (pp->port_curr >= pp->port_max_events) {
301*1885Sraf 		if (!cv_wait_sig(&pp->port_cv, &pp->port_queue.portq_mutex)) {
302*1885Sraf 			/* signal detected */
303*1885Sraf 			mutex_exit(&pp->port_queue.portq_mutex);
304*1885Sraf 			kmem_cache_free(port_control.pc_cache, pkevp);
305*1885Sraf 			return (EINTR);
3060Sstevel@tonic-gate 		}
3070Sstevel@tonic-gate 	}
308*1885Sraf 	pp->port_curr++;
309*1885Sraf 	mutex_exit(&pp->port_queue.portq_mutex);
3100Sstevel@tonic-gate 
311*1885Sraf 	bzero(pkevp, sizeof (port_kevent_t));
312*1885Sraf 	mutex_init(&pkevp->portkev_lock, NULL, MUTEX_DEFAULT, NULL);
313*1885Sraf 	pkevp->portkev_flags = flags;
314*1885Sraf 	pkevp->portkev_port = pp;
315*1885Sraf 	pkevp->portkev_source = source;
316*1885Sraf 	pkevp->portkev_pid = curproc->p_pid;
317*1885Sraf 	*pkevpp = pkevp;
3180Sstevel@tonic-gate 	return (0);
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate /*
3220Sstevel@tonic-gate  * Take an event out of the port queue
3230Sstevel@tonic-gate  */
3240Sstevel@tonic-gate static void
3250Sstevel@tonic-gate port_remove_event_doneq(port_kevent_t *pkevp, port_queue_t *portq)
3260Sstevel@tonic-gate {
3270Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&portq->portq_mutex));
3280Sstevel@tonic-gate 	list_remove(&portq->portq_list, pkevp);
3290Sstevel@tonic-gate 	portq->portq_nent--;
3300Sstevel@tonic-gate 	pkevp->portkev_flags &= ~PORT_KEV_DONEQ;
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate /*
3340Sstevel@tonic-gate  * The port_remove_done_event() function takes a fired event out of the
3350Sstevel@tonic-gate  * port queue.
3360Sstevel@tonic-gate  * Currently this function is required to cancel a fired event because
3370Sstevel@tonic-gate  * the application is delivering new association data (see port_associate_fd()).
3380Sstevel@tonic-gate  */
3390Sstevel@tonic-gate void
3400Sstevel@tonic-gate port_remove_done_event(port_kevent_t *pkevp)
3410Sstevel@tonic-gate {
3420Sstevel@tonic-gate 	port_queue_t	*portq;
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	portq = &pkevp->portkev_port->port_queue;
3450Sstevel@tonic-gate 	mutex_enter(&portq->portq_mutex);
3460Sstevel@tonic-gate 	/* wait for port_get() or port_getn() */
347*1885Sraf 	port_block(portq);
3480Sstevel@tonic-gate 	if (pkevp->portkev_flags & PORT_KEV_DONEQ) {
3490Sstevel@tonic-gate 		/* event still in port queue */
3500Sstevel@tonic-gate 		if (portq->portq_getn) {
3510Sstevel@tonic-gate 			/*
3520Sstevel@tonic-gate 			 * There could be still fired events in the temp queue;
3530Sstevel@tonic-gate 			 * push those events back to the port queue and
3540Sstevel@tonic-gate 			 * remove requested event afterwards.
3550Sstevel@tonic-gate 			 */
3560Sstevel@tonic-gate 			port_push_eventq(portq);
3570Sstevel@tonic-gate 		}
3580Sstevel@tonic-gate 		/* now remove event from the port queue */
3590Sstevel@tonic-gate 		port_remove_event_doneq(pkevp, portq);
3600Sstevel@tonic-gate 	}
361*1885Sraf 	port_unblock(portq);
3620Sstevel@tonic-gate 	mutex_exit(&portq->portq_mutex);
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate /*
3660Sstevel@tonic-gate  * Return port event back to the kmem_cache.
3670Sstevel@tonic-gate  * If the event is currently in the port queue the event itself will only
3680Sstevel@tonic-gate  * be set as invalid. The port_get(n) function will not deliver such events
3690Sstevel@tonic-gate  * to the application and it will return them back to the kmem_cache.
3700Sstevel@tonic-gate  */
3710Sstevel@tonic-gate void
3720Sstevel@tonic-gate port_free_event(port_kevent_t *pkevp)
3730Sstevel@tonic-gate {
3740Sstevel@tonic-gate 	port_queue_t	*portq;
3750Sstevel@tonic-gate 	port_t		*pp;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	pp = pkevp->portkev_port;
3780Sstevel@tonic-gate 	if (pp == NULL)
3790Sstevel@tonic-gate 		return;
3800Sstevel@tonic-gate 	if (pkevp->portkev_flags & PORT_ALLOC_PRIVATE) {
3810Sstevel@tonic-gate 		port_free_event_local(pkevp, 0);
3820Sstevel@tonic-gate 		return;
3830Sstevel@tonic-gate 	}
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	portq = &pp->port_queue;
3860Sstevel@tonic-gate 	mutex_enter(&portq->portq_mutex);
387*1885Sraf 	port_block(portq);
3880Sstevel@tonic-gate 	if (pkevp->portkev_flags & PORT_KEV_DONEQ) {
3890Sstevel@tonic-gate 		pkevp->portkev_flags |= PORT_KEV_FREE;
3900Sstevel@tonic-gate 		pkevp->portkev_callback = NULL;
391*1885Sraf 		port_unblock(portq);
3920Sstevel@tonic-gate 		mutex_exit(&portq->portq_mutex);
3930Sstevel@tonic-gate 		return;
3940Sstevel@tonic-gate 	}
395*1885Sraf 	port_unblock(portq);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	if (pkevp->portkev_flags & PORT_KEV_CACHED) {
3980Sstevel@tonic-gate 		mutex_exit(&portq->portq_mutex);
3990Sstevel@tonic-gate 		return;
4000Sstevel@tonic-gate 	}
4010Sstevel@tonic-gate 
402*1885Sraf 	if (--pp->port_curr < pp->port_max_events)
403*1885Sraf 		cv_signal(&pp->port_cv);
4040Sstevel@tonic-gate 	if (portq->portq_flags & PORTQ_CLOSE) {
4050Sstevel@tonic-gate 		/*
4060Sstevel@tonic-gate 		 * Another thread is closing the event port.
4070Sstevel@tonic-gate 		 * That thread will sleep until all allocated event
4080Sstevel@tonic-gate 		 * structures returned to the event port framework.
4090Sstevel@tonic-gate 		 * The portq_mutex is used to synchronize the status
4100Sstevel@tonic-gate 		 * of the allocated event structures (port_curr).
4110Sstevel@tonic-gate 		 */
4120Sstevel@tonic-gate 		if (pp->port_curr <= portq->portq_nent)
4130Sstevel@tonic-gate 			cv_signal(&portq->portq_closecv);
4140Sstevel@tonic-gate 	}
4150Sstevel@tonic-gate 	mutex_exit(&portq->portq_mutex);
4160Sstevel@tonic-gate 	port_free_event_local(pkevp, 1);
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate /*
4200Sstevel@tonic-gate  * This event port internal function is used by port_free_event() and
4210Sstevel@tonic-gate  * other port internal functions to return event structures back to the
4220Sstevel@tonic-gate  * kmem_cache.
4230Sstevel@tonic-gate  */
4240Sstevel@tonic-gate void
4250Sstevel@tonic-gate port_free_event_local(port_kevent_t *pkevp, int counter)
4260Sstevel@tonic-gate {
427*1885Sraf 	port_t *pp = pkevp->portkev_port;
428*1885Sraf 	port_queue_t *portq = &pp->port_queue;
429*1885Sraf 	int wakeup;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	pkevp->portkev_callback = NULL;
4320Sstevel@tonic-gate 	pkevp->portkev_flags = 0;
4330Sstevel@tonic-gate 	pkevp->portkev_port = NULL;
4341425Spraks 	mutex_destroy(&pkevp->portkev_lock);
4350Sstevel@tonic-gate 	kmem_cache_free(port_control.pc_cache, pkevp);
4360Sstevel@tonic-gate 
437*1885Sraf 	mutex_enter(&portq->portq_mutex);
438*1885Sraf 	if (counter == 0) {
439*1885Sraf 		if (--pp->port_curr < pp->port_max_events)
440*1885Sraf 			cv_signal(&pp->port_cv);
4410Sstevel@tonic-gate 	}
442*1885Sraf 	wakeup = (portq->portq_flags & PORTQ_POLLOUT);
443*1885Sraf 	portq->portq_flags &= ~PORTQ_POLLOUT;
444*1885Sraf 	mutex_exit(&portq->portq_mutex);
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	/* Submit a POLLOUT event if requested */
447*1885Sraf 	if (wakeup)
4480Sstevel@tonic-gate 		pollwakeup(&pp->port_pollhd, POLLOUT);
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate /*
4520Sstevel@tonic-gate  * port_init_event(port_event_t *pev, uintptr_t object, void *user,
4530Sstevel@tonic-gate  *	int (*port_callback)(void *, int *, pid_t, int, void *), void *sysarg);
4540Sstevel@tonic-gate  *	This function initializes most of the "wired" elements of the port
4550Sstevel@tonic-gate  *	event structure. This is normally being used just after the allocation
4560Sstevel@tonic-gate  *	of the port event structure.
4570Sstevel@tonic-gate  *	pkevp	: pointer to the port event structure
4580Sstevel@tonic-gate  *	object	: object associated with this event structure
4590Sstevel@tonic-gate  *	user	: user defined pointer delivered with the association function
4600Sstevel@tonic-gate  *	port_callback:
4610Sstevel@tonic-gate  *		  Address of the callback function which will be called
4620Sstevel@tonic-gate  *		  - just before the event is delivered to the application.
4630Sstevel@tonic-gate  *		    The callback function is called in user context and can be
4640Sstevel@tonic-gate  *		    used for copyouts, e.g.
4650Sstevel@tonic-gate  *		  - on close() or dissociation of the event. The sub-system
4660Sstevel@tonic-gate  *		    must remove immediately every existing association of
4670Sstevel@tonic-gate  *		    some object with this event.
4680Sstevel@tonic-gate  *	sysarg	: event source propietary data
4690Sstevel@tonic-gate  */
4700Sstevel@tonic-gate void
4710Sstevel@tonic-gate port_init_event(port_kevent_t *pkevp, uintptr_t object, void *user,
4720Sstevel@tonic-gate     int (*port_callback)(void *, int *, pid_t, int, void *),
4730Sstevel@tonic-gate     void *sysarg)
4740Sstevel@tonic-gate {
4750Sstevel@tonic-gate 	pkevp->portkev_object = object;
4760Sstevel@tonic-gate 	pkevp->portkev_user = user;
4770Sstevel@tonic-gate 	pkevp->portkev_callback = port_callback;
4780Sstevel@tonic-gate 	pkevp->portkev_arg = sysarg;
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate /*
4820Sstevel@tonic-gate  * This routine removes a portfd_t from the fd cache's hash table.
4830Sstevel@tonic-gate  */
4840Sstevel@tonic-gate void
4850Sstevel@tonic-gate port_pcache_remove_fd(port_fdcache_t *pcp, portfd_t *pfd)
4860Sstevel@tonic-gate {
4870Sstevel@tonic-gate 	polldat_t	*lpdp;
4880Sstevel@tonic-gate 	polldat_t	*cpdp;
4890Sstevel@tonic-gate 	portfd_t	**bucket;
4900Sstevel@tonic-gate 	polldat_t	*pdp = PFTOD(pfd);
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pcp->pc_lock));
4930Sstevel@tonic-gate 	bucket = PORT_FD_BUCKET(pcp, pdp->pd_fd);
4940Sstevel@tonic-gate 	cpdp = PFTOD(*bucket);
4950Sstevel@tonic-gate 	if (pdp == cpdp) {
4960Sstevel@tonic-gate 		*bucket = PDTOF(pdp->pd_hashnext);
4971425Spraks 		if (--pcp->pc_fdcount == 0) {
4981425Spraks 			/*
4991425Spraks 			 * signal the thread which may have blocked in
5001425Spraks 			 * port_close_sourcefd() on lastclose waiting
5011425Spraks 			 * for pc_fdcount to drop to 0.
5021425Spraks 			 */
5031425Spraks 			cv_signal(&pcp->pc_lclosecv);
5041425Spraks 		}
5050Sstevel@tonic-gate 		kmem_free(pfd, sizeof (portfd_t));
5060Sstevel@tonic-gate 		return;
5070Sstevel@tonic-gate 	}
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	while (cpdp != NULL) {
5100Sstevel@tonic-gate 		lpdp = cpdp;
5110Sstevel@tonic-gate 		cpdp = cpdp->pd_hashnext;
5120Sstevel@tonic-gate 		if (cpdp == pdp) {
5130Sstevel@tonic-gate 			/* polldat struct found */
5140Sstevel@tonic-gate 			lpdp->pd_hashnext = pdp->pd_hashnext;
5151425Spraks 			if (--pcp->pc_fdcount == 0) {
5161425Spraks 				/*
5171425Spraks 				 * signal the thread which may have blocked in
5181425Spraks 				 * port_close_sourcefd() on lastclose waiting
5191425Spraks 				 * for pc_fdcount to drop to 0.
5201425Spraks 				 */
5211425Spraks 				cv_signal(&pcp->pc_lclosecv);
5221425Spraks 			}
5230Sstevel@tonic-gate 			break;
5240Sstevel@tonic-gate 		}
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate 	ASSERT(cpdp != NULL);
5270Sstevel@tonic-gate 	kmem_free(pfd, sizeof (portfd_t));
5280Sstevel@tonic-gate }
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate /*
5310Sstevel@tonic-gate  * The port_push_eventq() function is used to move all remaining events
5320Sstevel@tonic-gate  * from the temporary queue used in port_get(n)() to the standard port
5330Sstevel@tonic-gate  * queue.
5340Sstevel@tonic-gate  */
5350Sstevel@tonic-gate void
5360Sstevel@tonic-gate port_push_eventq(port_queue_t *portq)
5370Sstevel@tonic-gate {
5380Sstevel@tonic-gate 	/*
5390Sstevel@tonic-gate 	 * Append temporary portq_get_list to the port queue. On return
5400Sstevel@tonic-gate 	 * the temporary portq_get_list is empty.
5410Sstevel@tonic-gate 	 */
5420Sstevel@tonic-gate 	list_move_tail(&portq->portq_list, &portq->portq_get_list);
5430Sstevel@tonic-gate 	portq->portq_nent += portq->portq_tnent;
5440Sstevel@tonic-gate 	portq->portq_tnent = 0;
5450Sstevel@tonic-gate }
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate /*
5480Sstevel@tonic-gate  * The port_remove_fd_object() function frees all resources associated with
5490Sstevel@tonic-gate  * delivered portfd_t structure.
5500Sstevel@tonic-gate  */
5510Sstevel@tonic-gate void
5520Sstevel@tonic-gate port_remove_fd_object(portfd_t *pfd, port_t *pp, port_fdcache_t *pcp)
5530Sstevel@tonic-gate {
5540Sstevel@tonic-gate 	port_queue_t	*portq;
5550Sstevel@tonic-gate 	polldat_t	*pdp = PFTOD(pfd);
5560Sstevel@tonic-gate 	port_kevent_t	*pkevp;
5570Sstevel@tonic-gate 	int		error;
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pcp->pc_lock));
5600Sstevel@tonic-gate 	if (pdp->pd_php != NULL) {
5610Sstevel@tonic-gate 		pollhead_delete(pdp->pd_php, pdp);
5620Sstevel@tonic-gate 		pdp->pd_php = NULL;
5630Sstevel@tonic-gate 	}
5640Sstevel@tonic-gate 	pkevp =  pdp->pd_portev;
5650Sstevel@tonic-gate 	portq = &pp->port_queue;
5660Sstevel@tonic-gate 	mutex_enter(&portq->portq_mutex);
567*1885Sraf 	port_block(portq);
5680Sstevel@tonic-gate 	if (pkevp->portkev_flags & PORT_KEV_DONEQ) {
5690Sstevel@tonic-gate 		if (portq->portq_getn && portq->portq_tnent) {
5700Sstevel@tonic-gate 			/*
5710Sstevel@tonic-gate 			 * move events from the temporary "get" queue
5720Sstevel@tonic-gate 			 * back to the port queue
5730Sstevel@tonic-gate 			 */
5740Sstevel@tonic-gate 			port_push_eventq(portq);
5750Sstevel@tonic-gate 		}
5760Sstevel@tonic-gate 		/* cleanup merged port queue */
5770Sstevel@tonic-gate 		port_remove_event_doneq(pkevp, portq);
5780Sstevel@tonic-gate 	}
579*1885Sraf 	port_unblock(portq);
5800Sstevel@tonic-gate 	mutex_exit(&portq->portq_mutex);
5810Sstevel@tonic-gate 	if (pkevp->portkev_callback) {
5820Sstevel@tonic-gate 		(void) (*pkevp->portkev_callback)(pkevp->portkev_arg,
5830Sstevel@tonic-gate 		    &error, pkevp->portkev_pid, PORT_CALLBACK_DISSOCIATE,
5840Sstevel@tonic-gate 		    pkevp);
5850Sstevel@tonic-gate 	}
5860Sstevel@tonic-gate 	port_free_event_local(pkevp, 0);
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	/* remove polldat struct */
5890Sstevel@tonic-gate 	port_pcache_remove_fd(pcp, pfd);
5900Sstevel@tonic-gate }
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate /*
5930Sstevel@tonic-gate  * The port_close_fd() function dissociates a file descriptor from a port
5940Sstevel@tonic-gate  * and removes all allocated resources.
5950Sstevel@tonic-gate  * close(2) detects in the uf_entry_t structure that the fd is associated
5960Sstevel@tonic-gate  * with a port (at least one port).
5970Sstevel@tonic-gate  * The fd can be associated with several ports.
5980Sstevel@tonic-gate  */
5990Sstevel@tonic-gate void
6000Sstevel@tonic-gate port_close_pfd(portfd_t *pfd)
6010Sstevel@tonic-gate {
6020Sstevel@tonic-gate 	port_t		*pp;
6030Sstevel@tonic-gate 	port_fdcache_t	*pcp;
6040Sstevel@tonic-gate 
6051425Spraks 	/*
6061425Spraks 	 * the portfd_t passed in should be for this proc.
6071425Spraks 	 */
6081425Spraks 	ASSERT(curproc->p_pid == PFTOD(pfd)->pd_portev->portkev_pid);
6090Sstevel@tonic-gate 	pp = PFTOD(pfd)->pd_portev->portkev_port;
6100Sstevel@tonic-gate 	pcp = pp->port_queue.portq_pcp;
6110Sstevel@tonic-gate 	mutex_enter(&pcp->pc_lock);
6120Sstevel@tonic-gate 	port_remove_fd_object(pfd, pp, pcp);
6130Sstevel@tonic-gate 	mutex_exit(&pcp->pc_lock);
6140Sstevel@tonic-gate }
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate /*
6170Sstevel@tonic-gate  * The port_associate_ksource() function associates an event source with a port.
6180Sstevel@tonic-gate  * On port_close() all associated sources are requested to free all local
6190Sstevel@tonic-gate  * resources associated with the event port.
6200Sstevel@tonic-gate  * The association of a source with a port can only be done one time. Further
6210Sstevel@tonic-gate  * calls of this function will only increment the reference counter.
6220Sstevel@tonic-gate  * The allocated port_source_t structure is removed from the port as soon as
6230Sstevel@tonic-gate  * the reference counter becomes 0.
6240Sstevel@tonic-gate  */
6250Sstevel@tonic-gate /* ARGSUSED */
6260Sstevel@tonic-gate int
6270Sstevel@tonic-gate port_associate_ksource(int port, int source, port_source_t **portsrc,
6280Sstevel@tonic-gate     void (*port_src_close)(void *, int, pid_t, int), void *arg,
6290Sstevel@tonic-gate     int (*port_src_associate)(port_kevent_t *, int, int, uintptr_t, void *))
6300Sstevel@tonic-gate {
6310Sstevel@tonic-gate 	port_t		*pp;
6320Sstevel@tonic-gate 	file_t		*fp;
6330Sstevel@tonic-gate 	port_source_t	**ps;
6340Sstevel@tonic-gate 	port_source_t	*pse;
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	if ((fp = getf(port)) == NULL)
6370Sstevel@tonic-gate 		return (EBADF);
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	if (fp->f_vnode->v_type != VPORT) {
6400Sstevel@tonic-gate 		releasef(port);
6410Sstevel@tonic-gate 		return (EBADFD);
6420Sstevel@tonic-gate 	}
6430Sstevel@tonic-gate 	pp = VTOEP(fp->f_vnode);
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	mutex_enter(&pp->port_queue.portq_source_mutex);
6460Sstevel@tonic-gate 	ps = &pp->port_queue.portq_scache[PORT_SHASH(source)];
6470Sstevel@tonic-gate 	for (pse = *ps; pse != NULL; pse = pse->portsrc_next) {
6480Sstevel@tonic-gate 		if (pse->portsrc_source == source)
6490Sstevel@tonic-gate 			break;
6500Sstevel@tonic-gate 	}
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	if (pse == NULL) {
6530Sstevel@tonic-gate 		/* Create association of the event source with the port */
6540Sstevel@tonic-gate 		pse = kmem_zalloc(sizeof (port_source_t), KM_NOSLEEP);
6550Sstevel@tonic-gate 		if (pse == NULL) {
6560Sstevel@tonic-gate 			mutex_exit(&pp->port_queue.portq_source_mutex);
6570Sstevel@tonic-gate 			releasef(port);
6580Sstevel@tonic-gate 			return (ENOMEM);
6590Sstevel@tonic-gate 		}
6600Sstevel@tonic-gate 		pse->portsrc_source = source;
6610Sstevel@tonic-gate 		pse->portsrc_close = port_src_close;
6620Sstevel@tonic-gate 		pse->portsrc_closearg = arg;
6630Sstevel@tonic-gate 		pse->portsrc_cnt = 1;
6640Sstevel@tonic-gate 		if (*ps)
6650Sstevel@tonic-gate 			pse->portsrc_next = (*ps)->portsrc_next;
6660Sstevel@tonic-gate 		*ps = pse;
6670Sstevel@tonic-gate 	} else {
6680Sstevel@tonic-gate 		/* entry already available, source is only requesting count */
6690Sstevel@tonic-gate 		pse->portsrc_cnt++;
6700Sstevel@tonic-gate 	}
6710Sstevel@tonic-gate 	mutex_exit(&pp->port_queue.portq_source_mutex);
6720Sstevel@tonic-gate 	releasef(port);
6730Sstevel@tonic-gate 	if (portsrc)
6740Sstevel@tonic-gate 		*portsrc = pse;
6750Sstevel@tonic-gate 	return (0);
6760Sstevel@tonic-gate }
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate /*
6790Sstevel@tonic-gate  * The port_dissociate_ksource() function dissociates an event source from
6800Sstevel@tonic-gate  * a port.
6810Sstevel@tonic-gate  */
6820Sstevel@tonic-gate int
6830Sstevel@tonic-gate port_dissociate_ksource(int port, int source, port_source_t *ps)
6840Sstevel@tonic-gate {
6850Sstevel@tonic-gate 	port_t		*pp;
6860Sstevel@tonic-gate 	file_t		*fp;
6870Sstevel@tonic-gate 	port_source_t	**psh;
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	if (ps == NULL)
6900Sstevel@tonic-gate 		return (EINVAL);
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	if ((fp = getf(port)) == NULL)
6930Sstevel@tonic-gate 		return (EBADF);
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	if (fp->f_vnode->v_type != VPORT) {
6960Sstevel@tonic-gate 		releasef(port);
6970Sstevel@tonic-gate 		return (EBADFD);
6980Sstevel@tonic-gate 	}
6990Sstevel@tonic-gate 	pp = VTOEP(fp->f_vnode);
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	mutex_enter(&pp->port_queue.portq_source_mutex);
7020Sstevel@tonic-gate 	if (--ps->portsrc_cnt == 0) {
7030Sstevel@tonic-gate 		/* last association removed -> free source structure */
7040Sstevel@tonic-gate 		if (ps->portsrc_prev == NULL) {
7050Sstevel@tonic-gate 			/* first entry */
7060Sstevel@tonic-gate 			psh = &pp->port_queue.portq_scache[PORT_SHASH(source)];
7070Sstevel@tonic-gate 			*psh = ps->portsrc_next;
7080Sstevel@tonic-gate 			if (ps->portsrc_next)
7090Sstevel@tonic-gate 				ps->portsrc_next->portsrc_prev = NULL;
7100Sstevel@tonic-gate 		} else {
7110Sstevel@tonic-gate 			ps->portsrc_prev->portsrc_next = ps->portsrc_next;
7120Sstevel@tonic-gate 			if (ps->portsrc_next)
7130Sstevel@tonic-gate 				ps->portsrc_next->portsrc_prev =
7140Sstevel@tonic-gate 				    ps->portsrc_prev;
7150Sstevel@tonic-gate 		}
7160Sstevel@tonic-gate 		kmem_free(ps, sizeof (port_source_t));
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate 	mutex_exit(&pp->port_queue.portq_source_mutex);
7190Sstevel@tonic-gate 	releasef(port);
7200Sstevel@tonic-gate 	return (0);
7210Sstevel@tonic-gate }
722