xref: /illumos-gate/usr/src/uts/common/os/port_subr.c (revision 2c76d751)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
534709573Sraf  * Common Development and Distribution License (the "License").
634709573Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2134709573Sraf 
227c478bd9Sstevel@tonic-gate /*
235b54b623Sethindra  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*2c76d751SPatrick Mooney  * Copyright 2022 Oxide Computer Company
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * This file containts all the functions required for interactions of
307c478bd9Sstevel@tonic-gate  * event sources with the event port file system.
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/conf.h>
357c478bd9Sstevel@tonic-gate #include <sys/stat.h>
367c478bd9Sstevel@tonic-gate #include <sys/errno.h>
377c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
387c478bd9Sstevel@tonic-gate #include <sys/debug.h>
397c478bd9Sstevel@tonic-gate #include <sys/file.h>
407c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
417c478bd9Sstevel@tonic-gate #include <sys/systm.h>
427c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
437c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
447c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
457c478bd9Sstevel@tonic-gate #include <sys/poll_impl.h>
467c478bd9Sstevel@tonic-gate #include <sys/port_impl.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * Maximum number of elements allowed to be passed in a single call of a
507c478bd9Sstevel@tonic-gate  * port function (port_sendn(), port_getn().  We need to allocate kernel memory
517c478bd9Sstevel@tonic-gate  * for all of them at once, so we can't let it scale without limit.
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate uint_t		port_max_list = PORT_MAX_LIST;
547c478bd9Sstevel@tonic-gate port_control_t	port_control;	/* Event port framework main structure */
557c478bd9Sstevel@tonic-gate 
5634709573Sraf /*
5734709573Sraf  * Block other threads from using a port.
5834709573Sraf  * We enter holding portq->portq_mutex but
5934709573Sraf  * we may drop and reacquire this lock.
6034709573Sraf  * Callers must deal with this fact.
6134709573Sraf  */
6234709573Sraf void
port_block(port_queue_t * portq)6334709573Sraf port_block(port_queue_t *portq)
6434709573Sraf {
6534709573Sraf 	ASSERT(MUTEX_HELD(&portq->portq_mutex));
6634709573Sraf 
6734709573Sraf 	while (portq->portq_flags & PORTQ_BLOCKED)
6834709573Sraf 		cv_wait(&portq->portq_block_cv, &portq->portq_mutex);
6934709573Sraf 	portq->portq_flags |= PORTQ_BLOCKED;
7034709573Sraf }
7134709573Sraf 
7234709573Sraf /*
7334709573Sraf  * Undo port_block(portq).
7434709573Sraf  */
7534709573Sraf void
port_unblock(port_queue_t * portq)7634709573Sraf port_unblock(port_queue_t *portq)
7734709573Sraf {
7834709573Sraf 	ASSERT(MUTEX_HELD(&portq->portq_mutex));
7934709573Sraf 
8034709573Sraf 	portq->portq_flags &= ~PORTQ_BLOCKED;
8134709573Sraf 	cv_signal(&portq->portq_block_cv);
8234709573Sraf }
8334709573Sraf 
8411dc39ddSpraks /*
8511dc39ddSpraks  * Called from pollwakeup(PORT_SOURCE_FD source) to determine
8611dc39ddSpraks  * if the port's fd needs to be notified of poll events. If yes,
8711dc39ddSpraks  * we mark the port indicating that pollwakeup() is referring
8811dc39ddSpraks  * it so that the port_t does not disappear.  pollwakeup()
8911dc39ddSpraks  * calls port_pollwkdone() after notifying. In port_pollwkdone(),
9011dc39ddSpraks  * we clear the hold on the port_t (clear PORTQ_POLLWK_PEND).
9111dc39ddSpraks  */
9211dc39ddSpraks int
port_pollwkup(port_t * pp)9311dc39ddSpraks port_pollwkup(port_t *pp)
9411dc39ddSpraks {
9511dc39ddSpraks 	int events = 0;
9611dc39ddSpraks 	port_queue_t *portq;
9711dc39ddSpraks 	portq = &pp->port_queue;
9811dc39ddSpraks 	mutex_enter(&portq->portq_mutex);
9911dc39ddSpraks 
10011dc39ddSpraks 	/*
10111dc39ddSpraks 	 * Normally, we should not have a situation where PORTQ_POLLIN
10211dc39ddSpraks 	 * and PORTQ_POLLWK_PEND are set at the same time, but it is
10311dc39ddSpraks 	 * possible. So, in pollwakeup() we ensure that no new fd's get
10411dc39ddSpraks 	 * added to the pollhead between the time it notifies poll events
10511dc39ddSpraks 	 * and calls poll_wkupdone() where we clear the PORTQ_POLLWK_PEND flag.
10611dc39ddSpraks 	 */
10711dc39ddSpraks 	if (portq->portq_flags & PORTQ_POLLIN &&
10811dc39ddSpraks 	    !(portq->portq_flags & PORTQ_POLLWK_PEND)) {
10911dc39ddSpraks 		portq->portq_flags &= ~PORTQ_POLLIN;
11011dc39ddSpraks 		portq->portq_flags |= PORTQ_POLLWK_PEND;
11111dc39ddSpraks 		events = POLLIN;
11211dc39ddSpraks 	}
11311dc39ddSpraks 	mutex_exit(&portq->portq_mutex);
11411dc39ddSpraks 	return (events);
11511dc39ddSpraks }
11611dc39ddSpraks 
11711dc39ddSpraks void
port_pollwkdone(port_t * pp)11811dc39ddSpraks port_pollwkdone(port_t *pp)
11911dc39ddSpraks {
12011dc39ddSpraks 	port_queue_t *portq;
12111dc39ddSpraks 	portq = &pp->port_queue;
12211dc39ddSpraks 	ASSERT(portq->portq_flags & PORTQ_POLLWK_PEND);
12311dc39ddSpraks 	mutex_enter(&portq->portq_mutex);
12411dc39ddSpraks 	portq->portq_flags &= ~PORTQ_POLLWK_PEND;
12511dc39ddSpraks 	cv_signal(&pp->port_cv);
12611dc39ddSpraks 	mutex_exit(&portq->portq_mutex);
12711dc39ddSpraks }
12811dc39ddSpraks 
12911dc39ddSpraks 
1307c478bd9Sstevel@tonic-gate /*
1317c478bd9Sstevel@tonic-gate  * The port_send_event() function is used by all event sources to submit
1327c478bd9Sstevel@tonic-gate  * trigerred events to a port. All the data  required for the event management
1337c478bd9Sstevel@tonic-gate  * is already stored in the port_kevent_t structure.
1347c478bd9Sstevel@tonic-gate  * The event port internal data is stored in the port_kevent_t structure
1357c478bd9Sstevel@tonic-gate  * during the allocation time (see port_alloc_event()). The data related to
1367c478bd9Sstevel@tonic-gate  * the event itself and to the event source management is stored in the
1377c478bd9Sstevel@tonic-gate  * port_kevent_t structure between the allocation time and submit time
1387c478bd9Sstevel@tonic-gate  * (see port_init_event()).
1397c478bd9Sstevel@tonic-gate  *
1407c478bd9Sstevel@tonic-gate  * This function is often called from interrupt level.
1417c478bd9Sstevel@tonic-gate  */
14234709573Sraf void
port_send_event(port_kevent_t * pkevp)1437c478bd9Sstevel@tonic-gate port_send_event(port_kevent_t *pkevp)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	port_queue_t	*portq;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	portq = &pkevp->portkev_port->port_queue;
1487c478bd9Sstevel@tonic-gate 	mutex_enter(&portq->portq_mutex);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	if (pkevp->portkev_flags & PORT_KEV_DONEQ) {
1517c478bd9Sstevel@tonic-gate 		/* Event already in the port queue */
15211dc39ddSpraks 		if (pkevp->portkev_source == PORT_SOURCE_FD) {
15361b4b1efSpraks 			mutex_exit(&pkevp->portkev_lock);
15461b4b1efSpraks 		}
1557c478bd9Sstevel@tonic-gate 		mutex_exit(&portq->portq_mutex);
15634709573Sraf 		return;
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	/* put event in the port queue */
1607c478bd9Sstevel@tonic-gate 	list_insert_tail(&portq->portq_list, pkevp);
1617c478bd9Sstevel@tonic-gate 	portq->portq_nent++;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	/*
16434709573Sraf 	 * Remove the PORTQ_WAIT_EVENTS flag to indicate
16534709573Sraf 	 * that new events are available.
1667c478bd9Sstevel@tonic-gate 	 */
1677c478bd9Sstevel@tonic-gate 	portq->portq_flags &= ~PORTQ_WAIT_EVENTS;
1687c478bd9Sstevel@tonic-gate 	pkevp->portkev_flags |= PORT_KEV_DONEQ;		/* event enqueued */
1697c478bd9Sstevel@tonic-gate 
17011dc39ddSpraks 	if (pkevp->portkev_source == PORT_SOURCE_FD) {
17161b4b1efSpraks 		mutex_exit(&pkevp->portkev_lock);
17261b4b1efSpraks 	}
17361b4b1efSpraks 
1747c478bd9Sstevel@tonic-gate 	/* Check if thread is in port_close() waiting for outstanding events */
1757c478bd9Sstevel@tonic-gate 	if (portq->portq_flags & PORTQ_CLOSE) {
1767c478bd9Sstevel@tonic-gate 		/* Check if all outstanding events are already in port queue */
1777c478bd9Sstevel@tonic-gate 		if (pkevp->portkev_port->port_curr <= portq->portq_nent)
1787c478bd9Sstevel@tonic-gate 			cv_signal(&portq->portq_closecv);
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	if (portq->portq_getn == 0) {
1827c478bd9Sstevel@tonic-gate 		/*
1837c478bd9Sstevel@tonic-gate 		 * No thread retrieving events -> check if enough events are
1847c478bd9Sstevel@tonic-gate 		 * available to satify waiting threads.
1857c478bd9Sstevel@tonic-gate 		 */
1867c478bd9Sstevel@tonic-gate 		if (portq->portq_thread &&
1877c478bd9Sstevel@tonic-gate 		    (portq->portq_nent >= portq->portq_nget))
1887c478bd9Sstevel@tonic-gate 			cv_signal(&portq->portq_thread->portget_cv);
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
19111dc39ddSpraks 	/*
19211dc39ddSpraks 	 * If some thread is polling the port's fd, then notify it.
19311dc39ddSpraks 	 * For PORT_SOURCE_FD source, we don't need to call pollwakeup()
19411dc39ddSpraks 	 * here as it will result in a recursive call(PORT_SOURCE_FD source
19511dc39ddSpraks 	 * is pollwakeup()). Therefore pollwakeup() itself will  notify the
19611dc39ddSpraks 	 * ports if being polled.
19711dc39ddSpraks 	 */
19811dc39ddSpraks 	if (pkevp->portkev_source != PORT_SOURCE_FD &&
199df2381bfSpraks 	    portq->portq_flags & PORTQ_POLLIN) {
2005b54b623Sethindra 		port_t	*pp;
2015b54b623Sethindra 
2027c478bd9Sstevel@tonic-gate 		portq->portq_flags &= ~PORTQ_POLLIN;
2035b54b623Sethindra 		/*
2045b54b623Sethindra 		 * Need to save port_t for calling pollwakeup since port_getn()
2055b54b623Sethindra 		 * may end up freeing pkevp once portq_mutex is dropped.
2065b54b623Sethindra 		 */
2075b54b623Sethindra 		pp = pkevp->portkev_port;
2087c478bd9Sstevel@tonic-gate 		mutex_exit(&portq->portq_mutex);
2095b54b623Sethindra 		pollwakeup(&pp->port_pollhd, POLLIN);
2107c478bd9Sstevel@tonic-gate 	} else {
2117c478bd9Sstevel@tonic-gate 		mutex_exit(&portq->portq_mutex);
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate  * The port_alloc_event() function has to be used by all event sources
2177c478bd9Sstevel@tonic-gate  * to request an slot for event notification.
2187c478bd9Sstevel@tonic-gate  * The slot reservation could be denied because of lack of resources.
2197c478bd9Sstevel@tonic-gate  * For that reason the event source should allocate an event slot as early
2207c478bd9Sstevel@tonic-gate  * as possible and be prepared to get an error code instead of the
2217c478bd9Sstevel@tonic-gate  * port event pointer.
2227c478bd9Sstevel@tonic-gate  * Al current event sources allocate an event slot during a system call
2237c478bd9Sstevel@tonic-gate  * entry. They return an error code to the application if an event slot
2247c478bd9Sstevel@tonic-gate  * could not be reserved.
2257c478bd9Sstevel@tonic-gate  * It is also recommended to associate the event source with the port
2267c478bd9Sstevel@tonic-gate  * before some other port function is used.
2277c478bd9Sstevel@tonic-gate  * The port argument is a file descriptor obtained by the application as
2287c478bd9Sstevel@tonic-gate  * a return value of port_create().
2297c478bd9Sstevel@tonic-gate  * Possible values of flags are:
2307c478bd9Sstevel@tonic-gate  * PORT_ALLOC_DEFAULT
2317c478bd9Sstevel@tonic-gate  *	This is the standard type of port events. port_get(n) will free this
2327c478bd9Sstevel@tonic-gate  *	type of event structures as soon as the events are delivered to the
2337c478bd9Sstevel@tonic-gate  *	application.
2347c478bd9Sstevel@tonic-gate  * PORT_ALLOC_PRIVATE
2357c478bd9Sstevel@tonic-gate  *	This type of event will be use for private use of the event source.
2367c478bd9Sstevel@tonic-gate  *	The port_get(n) function will deliver events of such an structure to
2377c478bd9Sstevel@tonic-gate  *	the application but it will not free the event structure itself.
2387c478bd9Sstevel@tonic-gate  *	The event source must free this structure using port_free_event().
2397c478bd9Sstevel@tonic-gate  * PORT_ALLOC_CACHED
2407c478bd9Sstevel@tonic-gate  *	This type of events is used when the event source helds an own
2417c478bd9Sstevel@tonic-gate  *	cache.
2427c478bd9Sstevel@tonic-gate  *	The port_get(n) function will deliver events of such an structure to
2437c478bd9Sstevel@tonic-gate  *	the application but it will not free the event structure itself.
2447c478bd9Sstevel@tonic-gate  *	The event source must free this structure using port_free_event().
2457c478bd9Sstevel@tonic-gate  */
2467c478bd9Sstevel@tonic-gate int
port_alloc_event(int port,int flags,int source,port_kevent_t ** pkevpp)2477c478bd9Sstevel@tonic-gate port_alloc_event(int port, int flags, int source, port_kevent_t **pkevpp)
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 	port_t		*pp;
2507c478bd9Sstevel@tonic-gate 	file_t		*fp;
25134709573Sraf 	port_kevent_t	*pkevp;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	if ((fp = getf(port)) == NULL)
2547c478bd9Sstevel@tonic-gate 		return (EBADF);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	if (fp->f_vnode->v_type != VPORT) {
2577c478bd9Sstevel@tonic-gate 		releasef(port);
2587c478bd9Sstevel@tonic-gate 		return (EBADFD);
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 
26134709573Sraf 	pkevp = kmem_cache_alloc(port_control.pc_cache, KM_NOSLEEP);
26234709573Sraf 	if (pkevp == NULL) {
26334709573Sraf 		releasef(port);
26434709573Sraf 		return (ENOMEM);
26534709573Sraf 	}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	/*
2687c478bd9Sstevel@tonic-gate 	 * port_max_events is controlled by the resource control
2697c478bd9Sstevel@tonic-gate 	 * process.port-max-events
2707c478bd9Sstevel@tonic-gate 	 */
27134709573Sraf 	pp = VTOEP(fp->f_vnode);
27234709573Sraf 	mutex_enter(&pp->port_queue.portq_mutex);
2737c478bd9Sstevel@tonic-gate 	if (pp->port_curr >= pp->port_max_events) {
27434709573Sraf 		mutex_exit(&pp->port_queue.portq_mutex);
27534709573Sraf 		kmem_cache_free(port_control.pc_cache, pkevp);
2767c478bd9Sstevel@tonic-gate 		releasef(port);
2777c478bd9Sstevel@tonic-gate 		return (EAGAIN);
2787c478bd9Sstevel@tonic-gate 	}
27934709573Sraf 	pp->port_curr++;
28034709573Sraf 	mutex_exit(&pp->port_queue.portq_mutex);
28134709573Sraf 
28234709573Sraf 	bzero(pkevp, sizeof (port_kevent_t));
28334709573Sraf 	mutex_init(&pkevp->portkev_lock, NULL, MUTEX_DEFAULT, NULL);
28434709573Sraf 	pkevp->portkev_source = source;
28534709573Sraf 	pkevp->portkev_flags = flags;
28634709573Sraf 	pkevp->portkev_pid = curproc->p_pid;
28734709573Sraf 	pkevp->portkev_port = pp;
28834709573Sraf 	*pkevpp = pkevp;
2897c478bd9Sstevel@tonic-gate 	releasef(port);
2907c478bd9Sstevel@tonic-gate 	return (0);
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate /*
2947c478bd9Sstevel@tonic-gate  * This function is faster than the standard port_alloc_event() and
2957c478bd9Sstevel@tonic-gate  * can be used when the event source already allocated an event from
2967c478bd9Sstevel@tonic-gate  * a port.
2977c478bd9Sstevel@tonic-gate  */
2987c478bd9Sstevel@tonic-gate int
port_dup_event(port_kevent_t * pkevp,port_kevent_t ** pkevdupp,int flags)2997c478bd9Sstevel@tonic-gate port_dup_event(port_kevent_t *pkevp, port_kevent_t **pkevdupp, int flags)
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate 	int	error;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	error = port_alloc_event_local(pkevp->portkev_port,
3047c478bd9Sstevel@tonic-gate 	    pkevp->portkev_source, flags, pkevdupp);
3057c478bd9Sstevel@tonic-gate 	if (error == 0)
3067c478bd9Sstevel@tonic-gate 		(*pkevdupp)->portkev_pid = pkevp->portkev_pid;
3077c478bd9Sstevel@tonic-gate 	return (error);
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate /*
3117c478bd9Sstevel@tonic-gate  * port_alloc_event_local() is reserved for internal use only.
3127c478bd9Sstevel@tonic-gate  * It is doing the same job as port_alloc_event() but with the event port
3137c478bd9Sstevel@tonic-gate  * pointer as the first argument.
3147c478bd9Sstevel@tonic-gate  * The check of the validity of the port file descriptor is skipped here.
3157c478bd9Sstevel@tonic-gate  */
3167c478bd9Sstevel@tonic-gate int
port_alloc_event_local(port_t * pp,int source,int flags,port_kevent_t ** pkevpp)3177c478bd9Sstevel@tonic-gate port_alloc_event_local(port_t *pp, int source, int flags,
3187c478bd9Sstevel@tonic-gate     port_kevent_t **pkevpp)
3197c478bd9Sstevel@tonic-gate {
32034709573Sraf 	port_kevent_t	*pkevp;
3217c478bd9Sstevel@tonic-gate 
32234709573Sraf 	pkevp = kmem_cache_alloc(port_control.pc_cache, KM_NOSLEEP);
32334709573Sraf 	if (pkevp == NULL)
3247c478bd9Sstevel@tonic-gate 		return (ENOMEM);
3257c478bd9Sstevel@tonic-gate 
32634709573Sraf 	mutex_enter(&pp->port_queue.portq_mutex);
32734709573Sraf 	if (pp->port_curr >= pp->port_max_events) {
32834709573Sraf 		mutex_exit(&pp->port_queue.portq_mutex);
32934709573Sraf 		kmem_cache_free(port_control.pc_cache, pkevp);
33034709573Sraf 		return (EAGAIN);
33134709573Sraf 	}
33234709573Sraf 	pp->port_curr++;
33334709573Sraf 	mutex_exit(&pp->port_queue.portq_mutex);
33434709573Sraf 
33534709573Sraf 	bzero(pkevp, sizeof (port_kevent_t));
33634709573Sraf 	mutex_init(&pkevp->portkev_lock, NULL, MUTEX_DEFAULT, NULL);
33734709573Sraf 	pkevp->portkev_flags = flags;
33834709573Sraf 	pkevp->portkev_port = pp;
33934709573Sraf 	pkevp->portkev_source = source;
34034709573Sraf 	pkevp->portkev_pid = curproc->p_pid;
34134709573Sraf 	*pkevpp = pkevp;
3427c478bd9Sstevel@tonic-gate 	return (0);
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate /*
3467c478bd9Sstevel@tonic-gate  * port_alloc_event_block() has the same functionality of port_alloc_event() +
3477c478bd9Sstevel@tonic-gate  * - it blocks if not enough event slots are available and
3487c478bd9Sstevel@tonic-gate  * - it blocks if not enough memory is available.
3497c478bd9Sstevel@tonic-gate  * Currently port_dispatch() is using this function to increase the
3507c478bd9Sstevel@tonic-gate  * reliability of event delivery for library event sources.
3517c478bd9Sstevel@tonic-gate  */
3527c478bd9Sstevel@tonic-gate int
port_alloc_event_block(port_t * pp,int source,int flags,port_kevent_t ** pkevpp)3537c478bd9Sstevel@tonic-gate port_alloc_event_block(port_t *pp, int source, int flags,
35434709573Sraf     port_kevent_t **pkevpp)
3557c478bd9Sstevel@tonic-gate {
35634709573Sraf 	port_kevent_t *pkevp =
35734709573Sraf 	    kmem_cache_alloc(port_control.pc_cache, KM_SLEEP);
35834709573Sraf 
35934709573Sraf 	mutex_enter(&pp->port_queue.portq_mutex);
36034709573Sraf 	while (pp->port_curr >= pp->port_max_events) {
36134709573Sraf 		if (!cv_wait_sig(&pp->port_cv, &pp->port_queue.portq_mutex)) {
36234709573Sraf 			/* signal detected */
36334709573Sraf 			mutex_exit(&pp->port_queue.portq_mutex);
36434709573Sraf 			kmem_cache_free(port_control.pc_cache, pkevp);
36534709573Sraf 			return (EINTR);
3667c478bd9Sstevel@tonic-gate 		}
3677c478bd9Sstevel@tonic-gate 	}
36834709573Sraf 	pp->port_curr++;
36934709573Sraf 	mutex_exit(&pp->port_queue.portq_mutex);
37034709573Sraf 
37134709573Sraf 	bzero(pkevp, sizeof (port_kevent_t));
37234709573Sraf 	mutex_init(&pkevp->portkev_lock, NULL, MUTEX_DEFAULT, NULL);
37334709573Sraf 	pkevp->portkev_flags = flags;
37434709573Sraf 	pkevp->portkev_port = pp;
37534709573Sraf 	pkevp->portkev_source = source;
37634709573Sraf 	pkevp->portkev_pid = curproc->p_pid;
37734709573Sraf 	*pkevpp = pkevp;
3787c478bd9Sstevel@tonic-gate 	return (0);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate /*
3827c478bd9Sstevel@tonic-gate  * Take an event out of the port queue
3837c478bd9Sstevel@tonic-gate  */
3847c478bd9Sstevel@tonic-gate static void
port_remove_event_doneq(port_kevent_t * pkevp,port_queue_t * portq)3857c478bd9Sstevel@tonic-gate port_remove_event_doneq(port_kevent_t *pkevp, port_queue_t *portq)
3867c478bd9Sstevel@tonic-gate {
3877c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&portq->portq_mutex));
3887c478bd9Sstevel@tonic-gate 	list_remove(&portq->portq_list, pkevp);
3897c478bd9Sstevel@tonic-gate 	portq->portq_nent--;
3907c478bd9Sstevel@tonic-gate 	pkevp->portkev_flags &= ~PORT_KEV_DONEQ;
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate /*
3947c478bd9Sstevel@tonic-gate  * The port_remove_done_event() function takes a fired event out of the
3957c478bd9Sstevel@tonic-gate  * port queue.
3967c478bd9Sstevel@tonic-gate  * Currently this function is required to cancel a fired event because
3977c478bd9Sstevel@tonic-gate  * the application is delivering new association data (see port_associate_fd()).
3987c478bd9Sstevel@tonic-gate  */
399df2381bfSpraks int
port_remove_done_event(port_kevent_t * pkevp)4007c478bd9Sstevel@tonic-gate port_remove_done_event(port_kevent_t *pkevp)
4017c478bd9Sstevel@tonic-gate {
4027c478bd9Sstevel@tonic-gate 	port_queue_t	*portq;
403df2381bfSpraks 	int	removed = 0;
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	portq = &pkevp->portkev_port->port_queue;
4067c478bd9Sstevel@tonic-gate 	mutex_enter(&portq->portq_mutex);
4077c478bd9Sstevel@tonic-gate 	/* wait for port_get() or port_getn() */
40834709573Sraf 	port_block(portq);
4097c478bd9Sstevel@tonic-gate 	if (pkevp->portkev_flags & PORT_KEV_DONEQ) {
4107c478bd9Sstevel@tonic-gate 		/* event still in port queue */
4117c478bd9Sstevel@tonic-gate 		if (portq->portq_getn) {
4127c478bd9Sstevel@tonic-gate 			/*
4137c478bd9Sstevel@tonic-gate 			 * There could be still fired events in the temp queue;
4147c478bd9Sstevel@tonic-gate 			 * push those events back to the port queue and
4157c478bd9Sstevel@tonic-gate 			 * remove requested event afterwards.
4167c478bd9Sstevel@tonic-gate 			 */
4177c478bd9Sstevel@tonic-gate 			port_push_eventq(portq);
4187c478bd9Sstevel@tonic-gate 		}
4197c478bd9Sstevel@tonic-gate 		/* now remove event from the port queue */
4207c478bd9Sstevel@tonic-gate 		port_remove_event_doneq(pkevp, portq);
421df2381bfSpraks 		removed = 1;
4227c478bd9Sstevel@tonic-gate 	}
42334709573Sraf 	port_unblock(portq);
4247c478bd9Sstevel@tonic-gate 	mutex_exit(&portq->portq_mutex);
425df2381bfSpraks 	return (removed);
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate /*
4297c478bd9Sstevel@tonic-gate  * Return port event back to the kmem_cache.
4307c478bd9Sstevel@tonic-gate  * If the event is currently in the port queue the event itself will only
4317c478bd9Sstevel@tonic-gate  * be set as invalid. The port_get(n) function will not deliver such events
4327c478bd9Sstevel@tonic-gate  * to the application and it will return them back to the kmem_cache.
4337c478bd9Sstevel@tonic-gate  */
4347c478bd9Sstevel@tonic-gate void
port_free_event(port_kevent_t * pkevp)4357c478bd9Sstevel@tonic-gate port_free_event(port_kevent_t *pkevp)
4367c478bd9Sstevel@tonic-gate {
4377c478bd9Sstevel@tonic-gate 	port_queue_t	*portq;
4387c478bd9Sstevel@tonic-gate 	port_t		*pp;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	pp = pkevp->portkev_port;
4417c478bd9Sstevel@tonic-gate 	if (pp == NULL)
4427c478bd9Sstevel@tonic-gate 		return;
4437c478bd9Sstevel@tonic-gate 	if (pkevp->portkev_flags & PORT_ALLOC_PRIVATE) {
4447c478bd9Sstevel@tonic-gate 		port_free_event_local(pkevp, 0);
4457c478bd9Sstevel@tonic-gate 		return;
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	portq = &pp->port_queue;
4497c478bd9Sstevel@tonic-gate 	mutex_enter(&portq->portq_mutex);
45034709573Sraf 	port_block(portq);
4517c478bd9Sstevel@tonic-gate 	if (pkevp->portkev_flags & PORT_KEV_DONEQ) {
4527c478bd9Sstevel@tonic-gate 		pkevp->portkev_flags |= PORT_KEV_FREE;
4537c478bd9Sstevel@tonic-gate 		pkevp->portkev_callback = NULL;
45434709573Sraf 		port_unblock(portq);
4557c478bd9Sstevel@tonic-gate 		mutex_exit(&portq->portq_mutex);
4567c478bd9Sstevel@tonic-gate 		return;
4577c478bd9Sstevel@tonic-gate 	}
45834709573Sraf 	port_unblock(portq);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	if (pkevp->portkev_flags & PORT_KEV_CACHED) {
4617c478bd9Sstevel@tonic-gate 		mutex_exit(&portq->portq_mutex);
4627c478bd9Sstevel@tonic-gate 		return;
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 
46534709573Sraf 	if (--pp->port_curr < pp->port_max_events)
46634709573Sraf 		cv_signal(&pp->port_cv);
4677c478bd9Sstevel@tonic-gate 	if (portq->portq_flags & PORTQ_CLOSE) {
4687c478bd9Sstevel@tonic-gate 		/*
4697c478bd9Sstevel@tonic-gate 		 * Another thread is closing the event port.
4707c478bd9Sstevel@tonic-gate 		 * That thread will sleep until all allocated event
4717c478bd9Sstevel@tonic-gate 		 * structures returned to the event port framework.
4727c478bd9Sstevel@tonic-gate 		 * The portq_mutex is used to synchronize the status
4737c478bd9Sstevel@tonic-gate 		 * of the allocated event structures (port_curr).
4747c478bd9Sstevel@tonic-gate 		 */
4757c478bd9Sstevel@tonic-gate 		if (pp->port_curr <= portq->portq_nent)
4767c478bd9Sstevel@tonic-gate 			cv_signal(&portq->portq_closecv);
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 	mutex_exit(&portq->portq_mutex);
4797c478bd9Sstevel@tonic-gate 	port_free_event_local(pkevp, 1);
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate /*
4837c478bd9Sstevel@tonic-gate  * This event port internal function is used by port_free_event() and
4847c478bd9Sstevel@tonic-gate  * other port internal functions to return event structures back to the
4857c478bd9Sstevel@tonic-gate  * kmem_cache.
4867c478bd9Sstevel@tonic-gate  */
4877c478bd9Sstevel@tonic-gate void
port_free_event_local(port_kevent_t * pkevp,int counter)4887c478bd9Sstevel@tonic-gate port_free_event_local(port_kevent_t *pkevp, int counter)
4897c478bd9Sstevel@tonic-gate {
49034709573Sraf 	port_t *pp = pkevp->portkev_port;
49134709573Sraf 	port_queue_t *portq = &pp->port_queue;
49234709573Sraf 	int wakeup;
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	pkevp->portkev_callback = NULL;
4957c478bd9Sstevel@tonic-gate 	pkevp->portkev_flags = 0;
4967c478bd9Sstevel@tonic-gate 	pkevp->portkev_port = NULL;
49761b4b1efSpraks 	mutex_destroy(&pkevp->portkev_lock);
4987c478bd9Sstevel@tonic-gate 	kmem_cache_free(port_control.pc_cache, pkevp);
4997c478bd9Sstevel@tonic-gate 
50034709573Sraf 	mutex_enter(&portq->portq_mutex);
50134709573Sraf 	if (counter == 0) {
50234709573Sraf 		if (--pp->port_curr < pp->port_max_events)
50334709573Sraf 			cv_signal(&pp->port_cv);
5047c478bd9Sstevel@tonic-gate 	}
50534709573Sraf 	wakeup = (portq->portq_flags & PORTQ_POLLOUT);
50634709573Sraf 	portq->portq_flags &= ~PORTQ_POLLOUT;
50734709573Sraf 	mutex_exit(&portq->portq_mutex);
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	/* Submit a POLLOUT event if requested */
51034709573Sraf 	if (wakeup)
5117c478bd9Sstevel@tonic-gate 		pollwakeup(&pp->port_pollhd, POLLOUT);
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate /*
5157c478bd9Sstevel@tonic-gate  * port_init_event(port_event_t *pev, uintptr_t object, void *user,
5167c478bd9Sstevel@tonic-gate  *	int (*port_callback)(void *, int *, pid_t, int, void *), void *sysarg);
5177c478bd9Sstevel@tonic-gate  *	This function initializes most of the "wired" elements of the port
5187c478bd9Sstevel@tonic-gate  *	event structure. This is normally being used just after the allocation
5197c478bd9Sstevel@tonic-gate  *	of the port event structure.
5207c478bd9Sstevel@tonic-gate  *	pkevp	: pointer to the port event structure
5217c478bd9Sstevel@tonic-gate  *	object	: object associated with this event structure
5227c478bd9Sstevel@tonic-gate  *	user	: user defined pointer delivered with the association function
5237c478bd9Sstevel@tonic-gate  *	port_callback:
5247c478bd9Sstevel@tonic-gate  *		  Address of the callback function which will be called
5257c478bd9Sstevel@tonic-gate  *		  - just before the event is delivered to the application.
5267c478bd9Sstevel@tonic-gate  *		    The callback function is called in user context and can be
5277c478bd9Sstevel@tonic-gate  *		    used for copyouts, e.g.
5287c478bd9Sstevel@tonic-gate  *		  - on close() or dissociation of the event. The sub-system
5297c478bd9Sstevel@tonic-gate  *		    must remove immediately every existing association of
5307c478bd9Sstevel@tonic-gate  *		    some object with this event.
5317c478bd9Sstevel@tonic-gate  *	sysarg	: event source propietary data
5327c478bd9Sstevel@tonic-gate  */
5337c478bd9Sstevel@tonic-gate void
port_init_event(port_kevent_t * pkevp,uintptr_t object,void * user,int (* port_callback)(void *,int *,pid_t,int,void *),void * sysarg)5347c478bd9Sstevel@tonic-gate port_init_event(port_kevent_t *pkevp, uintptr_t object, void *user,
5357c478bd9Sstevel@tonic-gate     int (*port_callback)(void *, int *, pid_t, int, void *),
5367c478bd9Sstevel@tonic-gate     void *sysarg)
5377c478bd9Sstevel@tonic-gate {
5387c478bd9Sstevel@tonic-gate 	pkevp->portkev_object = object;
5397c478bd9Sstevel@tonic-gate 	pkevp->portkev_user = user;
5407c478bd9Sstevel@tonic-gate 	pkevp->portkev_callback = port_callback;
5417c478bd9Sstevel@tonic-gate 	pkevp->portkev_arg = sysarg;
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate /*
5457c478bd9Sstevel@tonic-gate  * This routine removes a portfd_t from the fd cache's hash table.
5467c478bd9Sstevel@tonic-gate  */
5477c478bd9Sstevel@tonic-gate void
port_pcache_remove_fd(port_fdcache_t * pcp,portfd_t * pfd)5487c478bd9Sstevel@tonic-gate port_pcache_remove_fd(port_fdcache_t *pcp, portfd_t *pfd)
5497c478bd9Sstevel@tonic-gate {
5507c478bd9Sstevel@tonic-gate 	polldat_t	*lpdp;
5517c478bd9Sstevel@tonic-gate 	polldat_t	*cpdp;
5527c478bd9Sstevel@tonic-gate 	portfd_t	**bucket;
5537c478bd9Sstevel@tonic-gate 	polldat_t	*pdp = PFTOD(pfd);
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pcp->pc_lock));
5567c478bd9Sstevel@tonic-gate 	bucket = PORT_FD_BUCKET(pcp, pdp->pd_fd);
5577c478bd9Sstevel@tonic-gate 	cpdp = PFTOD(*bucket);
5587c478bd9Sstevel@tonic-gate 	if (pdp == cpdp) {
5597c478bd9Sstevel@tonic-gate 		*bucket = PDTOF(pdp->pd_hashnext);
56061b4b1efSpraks 		if (--pcp->pc_fdcount == 0) {
56161b4b1efSpraks 			/*
56261b4b1efSpraks 			 * signal the thread which may have blocked in
56361b4b1efSpraks 			 * port_close_sourcefd() on lastclose waiting
56461b4b1efSpraks 			 * for pc_fdcount to drop to 0.
56561b4b1efSpraks 			 */
56661b4b1efSpraks 			cv_signal(&pcp->pc_lclosecv);
56761b4b1efSpraks 		}
5687c478bd9Sstevel@tonic-gate 		kmem_free(pfd, sizeof (portfd_t));
5697c478bd9Sstevel@tonic-gate 		return;
5707c478bd9Sstevel@tonic-gate 	}
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	while (cpdp != NULL) {
5737c478bd9Sstevel@tonic-gate 		lpdp = cpdp;
5747c478bd9Sstevel@tonic-gate 		cpdp = cpdp->pd_hashnext;
5757c478bd9Sstevel@tonic-gate 		if (cpdp == pdp) {
5767c478bd9Sstevel@tonic-gate 			/* polldat struct found */
5777c478bd9Sstevel@tonic-gate 			lpdp->pd_hashnext = pdp->pd_hashnext;
57861b4b1efSpraks 			if (--pcp->pc_fdcount == 0) {
57961b4b1efSpraks 				/*
58061b4b1efSpraks 				 * signal the thread which may have blocked in
58161b4b1efSpraks 				 * port_close_sourcefd() on lastclose waiting
58261b4b1efSpraks 				 * for pc_fdcount to drop to 0.
58361b4b1efSpraks 				 */
58461b4b1efSpraks 				cv_signal(&pcp->pc_lclosecv);
58561b4b1efSpraks 			}
5867c478bd9Sstevel@tonic-gate 			break;
5877c478bd9Sstevel@tonic-gate 		}
5887c478bd9Sstevel@tonic-gate 	}
5897c478bd9Sstevel@tonic-gate 	ASSERT(cpdp != NULL);
5907c478bd9Sstevel@tonic-gate 	kmem_free(pfd, sizeof (portfd_t));
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate /*
5947c478bd9Sstevel@tonic-gate  * The port_push_eventq() function is used to move all remaining events
5957c478bd9Sstevel@tonic-gate  * from the temporary queue used in port_get(n)() to the standard port
5967c478bd9Sstevel@tonic-gate  * queue.
5977c478bd9Sstevel@tonic-gate  */
5987c478bd9Sstevel@tonic-gate void
port_push_eventq(port_queue_t * portq)5997c478bd9Sstevel@tonic-gate port_push_eventq(port_queue_t *portq)
6007c478bd9Sstevel@tonic-gate {
6017c478bd9Sstevel@tonic-gate 	/*
6027c478bd9Sstevel@tonic-gate 	 * Append temporary portq_get_list to the port queue. On return
6037c478bd9Sstevel@tonic-gate 	 * the temporary portq_get_list is empty.
6047c478bd9Sstevel@tonic-gate 	 */
6057c478bd9Sstevel@tonic-gate 	list_move_tail(&portq->portq_list, &portq->portq_get_list);
6067c478bd9Sstevel@tonic-gate 	portq->portq_nent += portq->portq_tnent;
6077c478bd9Sstevel@tonic-gate 	portq->portq_tnent = 0;
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate /*
6117c478bd9Sstevel@tonic-gate  * The port_remove_fd_object() function frees all resources associated with
6129f23d599Spraks  * delivered portfd_t structure. Returns 1 if the port_kevent was found
6139f23d599Spraks  * and removed from the port queue.
6147c478bd9Sstevel@tonic-gate  */
6159f23d599Spraks int
port_remove_fd_object(portfd_t * pfd,port_t * pp,port_fdcache_t * pcp)6167c478bd9Sstevel@tonic-gate port_remove_fd_object(portfd_t *pfd, port_t *pp, port_fdcache_t *pcp)
6177c478bd9Sstevel@tonic-gate {
6187c478bd9Sstevel@tonic-gate 	port_queue_t	*portq;
6197c478bd9Sstevel@tonic-gate 	polldat_t	*pdp = PFTOD(pfd);
6207c478bd9Sstevel@tonic-gate 	port_kevent_t	*pkevp;
6217c478bd9Sstevel@tonic-gate 	int		error;
6229f23d599Spraks 	int		removed = 0;
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pcp->pc_lock));
625*2c76d751SPatrick Mooney 	polldat_disassociate(pdp);
6267c478bd9Sstevel@tonic-gate 	pkevp =  pdp->pd_portev;
6277c478bd9Sstevel@tonic-gate 	portq = &pp->port_queue;
6287c478bd9Sstevel@tonic-gate 	mutex_enter(&portq->portq_mutex);
62934709573Sraf 	port_block(portq);
6307c478bd9Sstevel@tonic-gate 	if (pkevp->portkev_flags & PORT_KEV_DONEQ) {
6317c478bd9Sstevel@tonic-gate 		if (portq->portq_getn && portq->portq_tnent) {
6327c478bd9Sstevel@tonic-gate 			/*
6337c478bd9Sstevel@tonic-gate 			 * move events from the temporary "get" queue
6347c478bd9Sstevel@tonic-gate 			 * back to the port queue
6357c478bd9Sstevel@tonic-gate 			 */
6367c478bd9Sstevel@tonic-gate 			port_push_eventq(portq);
6377c478bd9Sstevel@tonic-gate 		}
6387c478bd9Sstevel@tonic-gate 		/* cleanup merged port queue */
6397c478bd9Sstevel@tonic-gate 		port_remove_event_doneq(pkevp, portq);
6409f23d599Spraks 		removed = 1;
6417c478bd9Sstevel@tonic-gate 	}
64234709573Sraf 	port_unblock(portq);
6437c478bd9Sstevel@tonic-gate 	mutex_exit(&portq->portq_mutex);
6447c478bd9Sstevel@tonic-gate 	if (pkevp->portkev_callback) {
6457c478bd9Sstevel@tonic-gate 		(void) (*pkevp->portkev_callback)(pkevp->portkev_arg,
6467c478bd9Sstevel@tonic-gate 		    &error, pkevp->portkev_pid, PORT_CALLBACK_DISSOCIATE,
6477c478bd9Sstevel@tonic-gate 		    pkevp);
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate 	port_free_event_local(pkevp, 0);
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	/* remove polldat struct */
6527c478bd9Sstevel@tonic-gate 	port_pcache_remove_fd(pcp, pfd);
6539f23d599Spraks 	return (removed);
6547c478bd9Sstevel@tonic-gate }
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate /*
6577c478bd9Sstevel@tonic-gate  * The port_close_fd() function dissociates a file descriptor from a port
6587c478bd9Sstevel@tonic-gate  * and removes all allocated resources.
6597c478bd9Sstevel@tonic-gate  * close(2) detects in the uf_entry_t structure that the fd is associated
6607c478bd9Sstevel@tonic-gate  * with a port (at least one port).
6617c478bd9Sstevel@tonic-gate  * The fd can be associated with several ports.
6627c478bd9Sstevel@tonic-gate  */
6637c478bd9Sstevel@tonic-gate void
port_close_pfd(portfd_t * pfd)6647c478bd9Sstevel@tonic-gate port_close_pfd(portfd_t *pfd)
6657c478bd9Sstevel@tonic-gate {
6667c478bd9Sstevel@tonic-gate 	port_t		*pp;
6677c478bd9Sstevel@tonic-gate 	port_fdcache_t	*pcp;
6687c478bd9Sstevel@tonic-gate 
66961b4b1efSpraks 	/*
67061b4b1efSpraks 	 * the portfd_t passed in should be for this proc.
67161b4b1efSpraks 	 */
67261b4b1efSpraks 	ASSERT(curproc->p_pid == PFTOD(pfd)->pd_portev->portkev_pid);
6737c478bd9Sstevel@tonic-gate 	pp = PFTOD(pfd)->pd_portev->portkev_port;
6747c478bd9Sstevel@tonic-gate 	pcp = pp->port_queue.portq_pcp;
6757c478bd9Sstevel@tonic-gate 	mutex_enter(&pcp->pc_lock);
6769f23d599Spraks 	(void) port_remove_fd_object(pfd, pp, pcp);
6777c478bd9Sstevel@tonic-gate 	mutex_exit(&pcp->pc_lock);
6787c478bd9Sstevel@tonic-gate }
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate /*
6817c478bd9Sstevel@tonic-gate  * The port_associate_ksource() function associates an event source with a port.
6827c478bd9Sstevel@tonic-gate  * On port_close() all associated sources are requested to free all local
6837c478bd9Sstevel@tonic-gate  * resources associated with the event port.
6847c478bd9Sstevel@tonic-gate  * The association of a source with a port can only be done one time. Further
6857c478bd9Sstevel@tonic-gate  * calls of this function will only increment the reference counter.
6867c478bd9Sstevel@tonic-gate  * The allocated port_source_t structure is removed from the port as soon as
6877c478bd9Sstevel@tonic-gate  * the reference counter becomes 0.
6887c478bd9Sstevel@tonic-gate  */
6897c478bd9Sstevel@tonic-gate /* ARGSUSED */
6907c478bd9Sstevel@tonic-gate int
port_associate_ksource(int port,int source,port_source_t ** portsrc,void (* port_src_close)(void *,int,pid_t,int),void * arg,int (* port_src_associate)(port_kevent_t *,int,int,uintptr_t,void *))6917c478bd9Sstevel@tonic-gate port_associate_ksource(int port, int source, port_source_t **portsrc,
6927c478bd9Sstevel@tonic-gate     void (*port_src_close)(void *, int, pid_t, int), void *arg,
6937c478bd9Sstevel@tonic-gate     int (*port_src_associate)(port_kevent_t *, int, int, uintptr_t, void *))
6947c478bd9Sstevel@tonic-gate {
6957c478bd9Sstevel@tonic-gate 	port_t		*pp;
6967c478bd9Sstevel@tonic-gate 	file_t		*fp;
6977c478bd9Sstevel@tonic-gate 	port_source_t	**ps;
6987c478bd9Sstevel@tonic-gate 	port_source_t	*pse;
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	if ((fp = getf(port)) == NULL)
7017c478bd9Sstevel@tonic-gate 		return (EBADF);
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	if (fp->f_vnode->v_type != VPORT) {
7047c478bd9Sstevel@tonic-gate 		releasef(port);
7057c478bd9Sstevel@tonic-gate 		return (EBADFD);
7067c478bd9Sstevel@tonic-gate 	}
7077c478bd9Sstevel@tonic-gate 	pp = VTOEP(fp->f_vnode);
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->port_queue.portq_source_mutex);
7107c478bd9Sstevel@tonic-gate 	ps = &pp->port_queue.portq_scache[PORT_SHASH(source)];
7117c478bd9Sstevel@tonic-gate 	for (pse = *ps; pse != NULL; pse = pse->portsrc_next) {
7127c478bd9Sstevel@tonic-gate 		if (pse->portsrc_source == source)
7137c478bd9Sstevel@tonic-gate 			break;
7147c478bd9Sstevel@tonic-gate 	}
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	if (pse == NULL) {
7177c478bd9Sstevel@tonic-gate 		/* Create association of the event source with the port */
7187c478bd9Sstevel@tonic-gate 		pse = kmem_zalloc(sizeof (port_source_t), KM_NOSLEEP);
7197c478bd9Sstevel@tonic-gate 		if (pse == NULL) {
7207c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->port_queue.portq_source_mutex);
7217c478bd9Sstevel@tonic-gate 			releasef(port);
7227c478bd9Sstevel@tonic-gate 			return (ENOMEM);
7237c478bd9Sstevel@tonic-gate 		}
7247c478bd9Sstevel@tonic-gate 		pse->portsrc_source = source;
7257c478bd9Sstevel@tonic-gate 		pse->portsrc_close = port_src_close;
7267c478bd9Sstevel@tonic-gate 		pse->portsrc_closearg = arg;
7277c478bd9Sstevel@tonic-gate 		pse->portsrc_cnt = 1;
7287c478bd9Sstevel@tonic-gate 		if (*ps)
7297c478bd9Sstevel@tonic-gate 			pse->portsrc_next = (*ps)->portsrc_next;
7307c478bd9Sstevel@tonic-gate 		*ps = pse;
7317c478bd9Sstevel@tonic-gate 	} else {
7327c478bd9Sstevel@tonic-gate 		/* entry already available, source is only requesting count */
7337c478bd9Sstevel@tonic-gate 		pse->portsrc_cnt++;
7347c478bd9Sstevel@tonic-gate 	}
7357c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->port_queue.portq_source_mutex);
7367c478bd9Sstevel@tonic-gate 	releasef(port);
7377c478bd9Sstevel@tonic-gate 	if (portsrc)
7387c478bd9Sstevel@tonic-gate 		*portsrc = pse;
7397c478bd9Sstevel@tonic-gate 	return (0);
7407c478bd9Sstevel@tonic-gate }
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate /*
7437c478bd9Sstevel@tonic-gate  * The port_dissociate_ksource() function dissociates an event source from
7447c478bd9Sstevel@tonic-gate  * a port.
7457c478bd9Sstevel@tonic-gate  */
7467c478bd9Sstevel@tonic-gate int
port_dissociate_ksource(int port,int source,port_source_t * ps)7477c478bd9Sstevel@tonic-gate port_dissociate_ksource(int port, int source, port_source_t *ps)
7487c478bd9Sstevel@tonic-gate {
7497c478bd9Sstevel@tonic-gate 	port_t		*pp;
7507c478bd9Sstevel@tonic-gate 	file_t		*fp;
7517c478bd9Sstevel@tonic-gate 	port_source_t	**psh;
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	if (ps == NULL)
7547c478bd9Sstevel@tonic-gate 		return (EINVAL);
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	if ((fp = getf(port)) == NULL)
7577c478bd9Sstevel@tonic-gate 		return (EBADF);
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	if (fp->f_vnode->v_type != VPORT) {
7607c478bd9Sstevel@tonic-gate 		releasef(port);
7617c478bd9Sstevel@tonic-gate 		return (EBADFD);
7627c478bd9Sstevel@tonic-gate 	}
7637c478bd9Sstevel@tonic-gate 	pp = VTOEP(fp->f_vnode);
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->port_queue.portq_source_mutex);
7667c478bd9Sstevel@tonic-gate 	if (--ps->portsrc_cnt == 0) {
7677c478bd9Sstevel@tonic-gate 		/* last association removed -> free source structure */
7687c478bd9Sstevel@tonic-gate 		if (ps->portsrc_prev == NULL) {
7697c478bd9Sstevel@tonic-gate 			/* first entry */
7707c478bd9Sstevel@tonic-gate 			psh = &pp->port_queue.portq_scache[PORT_SHASH(source)];
7717c478bd9Sstevel@tonic-gate 			*psh = ps->portsrc_next;
7727c478bd9Sstevel@tonic-gate 			if (ps->portsrc_next)
7737c478bd9Sstevel@tonic-gate 				ps->portsrc_next->portsrc_prev = NULL;
7747c478bd9Sstevel@tonic-gate 		} else {
7757c478bd9Sstevel@tonic-gate 			ps->portsrc_prev->portsrc_next = ps->portsrc_next;
7767c478bd9Sstevel@tonic-gate 			if (ps->portsrc_next)
7777c478bd9Sstevel@tonic-gate 				ps->portsrc_next->portsrc_prev =
7787c478bd9Sstevel@tonic-gate 				    ps->portsrc_prev;
7797c478bd9Sstevel@tonic-gate 		}
7807c478bd9Sstevel@tonic-gate 		kmem_free(ps, sizeof (port_source_t));
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->port_queue.portq_source_mutex);
7837c478bd9Sstevel@tonic-gate 	releasef(port);
7847c478bd9Sstevel@tonic-gate 	return (0);
7857c478bd9Sstevel@tonic-gate }
786df2381bfSpraks 
787df2381bfSpraks void
free_fopdata(vnode_t * vp)788df2381bfSpraks free_fopdata(vnode_t *vp)
789df2381bfSpraks {
790df2381bfSpraks 	portfop_vp_t *pvp;
791df2381bfSpraks 	pvp = vp->v_fopdata;
792df2381bfSpraks 	ASSERT(pvp->pvp_femp == NULL);
793df2381bfSpraks 	mutex_destroy(&pvp->pvp_mutex);
794df2381bfSpraks 	list_destroy(&pvp->pvp_pfoplist);
795df2381bfSpraks 	kmem_free(pvp, sizeof (*pvp));
796df2381bfSpraks 	vp->v_fopdata = NULL;
797df2381bfSpraks }
798