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 /*
23*5b54b623Sethindra * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate * This file containts all the functions required for interactions of
317c478bd9Sstevel@tonic-gate * event sources with the event port file system.
327c478bd9Sstevel@tonic-gate */
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/conf.h>
367c478bd9Sstevel@tonic-gate #include <sys/stat.h>
377c478bd9Sstevel@tonic-gate #include <sys/errno.h>
387c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
397c478bd9Sstevel@tonic-gate #include <sys/debug.h>
407c478bd9Sstevel@tonic-gate #include <sys/file.h>
417c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
427c478bd9Sstevel@tonic-gate #include <sys/systm.h>
437c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
447c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
457c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
467c478bd9Sstevel@tonic-gate #include <sys/poll_impl.h>
477c478bd9Sstevel@tonic-gate #include <sys/port_impl.h>
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate * Maximum number of elements allowed to be passed in a single call of a
517c478bd9Sstevel@tonic-gate * port function (port_sendn(), port_getn(). We need to allocate kernel memory
527c478bd9Sstevel@tonic-gate * for all of them at once, so we can't let it scale without limit.
537c478bd9Sstevel@tonic-gate */
547c478bd9Sstevel@tonic-gate uint_t port_max_list = PORT_MAX_LIST;
557c478bd9Sstevel@tonic-gate port_control_t port_control; /* Event port framework main structure */
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate /*
5834709573Sraf * Block other threads from using a port.
5934709573Sraf * We enter holding portq->portq_mutex but
6034709573Sraf * we may drop and reacquire this lock.
6134709573Sraf * Callers must deal with this fact.
6234709573Sraf */
6334709573Sraf void
port_block(port_queue_t * portq)6434709573Sraf port_block(port_queue_t *portq)
6534709573Sraf {
6634709573Sraf ASSERT(MUTEX_HELD(&portq->portq_mutex));
6734709573Sraf
6834709573Sraf while (portq->portq_flags & PORTQ_BLOCKED)
6934709573Sraf cv_wait(&portq->portq_block_cv, &portq->portq_mutex);
7034709573Sraf portq->portq_flags |= PORTQ_BLOCKED;
7134709573Sraf }
7234709573Sraf
7334709573Sraf /*
7434709573Sraf * Undo port_block(portq).
7534709573Sraf */
7634709573Sraf void
port_unblock(port_queue_t * portq)7734709573Sraf port_unblock(port_queue_t *portq)
7834709573Sraf {
7934709573Sraf ASSERT(MUTEX_HELD(&portq->portq_mutex));
8034709573Sraf
8134709573Sraf portq->portq_flags &= ~PORTQ_BLOCKED;
8234709573Sraf cv_signal(&portq->portq_block_cv);
8334709573Sraf }
8434709573Sraf
8534709573Sraf /*
8611dc39ddSpraks * Called from pollwakeup(PORT_SOURCE_FD source) to determine
8711dc39ddSpraks * if the port's fd needs to be notified of poll events. If yes,
8811dc39ddSpraks * we mark the port indicating that pollwakeup() is referring
8911dc39ddSpraks * it so that the port_t does not disappear. pollwakeup()
9011dc39ddSpraks * calls port_pollwkdone() after notifying. In port_pollwkdone(),
9111dc39ddSpraks * we clear the hold on the port_t (clear PORTQ_POLLWK_PEND).
9211dc39ddSpraks */
9311dc39ddSpraks int
port_pollwkup(port_t * pp)9411dc39ddSpraks port_pollwkup(port_t *pp)
9511dc39ddSpraks {
9611dc39ddSpraks int events = 0;
9711dc39ddSpraks port_queue_t *portq;
9811dc39ddSpraks portq = &pp->port_queue;
9911dc39ddSpraks mutex_enter(&portq->portq_mutex);
10011dc39ddSpraks
10111dc39ddSpraks /*
10211dc39ddSpraks * Normally, we should not have a situation where PORTQ_POLLIN
10311dc39ddSpraks * and PORTQ_POLLWK_PEND are set at the same time, but it is
10411dc39ddSpraks * possible. So, in pollwakeup() we ensure that no new fd's get
10511dc39ddSpraks * added to the pollhead between the time it notifies poll events
10611dc39ddSpraks * and calls poll_wkupdone() where we clear the PORTQ_POLLWK_PEND flag.
10711dc39ddSpraks */
10811dc39ddSpraks if (portq->portq_flags & PORTQ_POLLIN &&
10911dc39ddSpraks !(portq->portq_flags & PORTQ_POLLWK_PEND)) {
11011dc39ddSpraks portq->portq_flags &= ~PORTQ_POLLIN;
11111dc39ddSpraks portq->portq_flags |= PORTQ_POLLWK_PEND;
11211dc39ddSpraks events = POLLIN;
11311dc39ddSpraks }
11411dc39ddSpraks mutex_exit(&portq->portq_mutex);
11511dc39ddSpraks return (events);
11611dc39ddSpraks }
11711dc39ddSpraks
11811dc39ddSpraks void
port_pollwkdone(port_t * pp)11911dc39ddSpraks port_pollwkdone(port_t *pp)
12011dc39ddSpraks {
12111dc39ddSpraks port_queue_t *portq;
12211dc39ddSpraks portq = &pp->port_queue;
12311dc39ddSpraks ASSERT(portq->portq_flags & PORTQ_POLLWK_PEND);
12411dc39ddSpraks mutex_enter(&portq->portq_mutex);
12511dc39ddSpraks portq->portq_flags &= ~PORTQ_POLLWK_PEND;
12611dc39ddSpraks cv_signal(&pp->port_cv);
12711dc39ddSpraks mutex_exit(&portq->portq_mutex);
12811dc39ddSpraks }
12911dc39ddSpraks
13011dc39ddSpraks
13111dc39ddSpraks /*
1327c478bd9Sstevel@tonic-gate * The port_send_event() function is used by all event sources to submit
1337c478bd9Sstevel@tonic-gate * trigerred events to a port. All the data required for the event management
1347c478bd9Sstevel@tonic-gate * is already stored in the port_kevent_t structure.
1357c478bd9Sstevel@tonic-gate * The event port internal data is stored in the port_kevent_t structure
1367c478bd9Sstevel@tonic-gate * during the allocation time (see port_alloc_event()). The data related to
1377c478bd9Sstevel@tonic-gate * the event itself and to the event source management is stored in the
1387c478bd9Sstevel@tonic-gate * port_kevent_t structure between the allocation time and submit time
1397c478bd9Sstevel@tonic-gate * (see port_init_event()).
1407c478bd9Sstevel@tonic-gate *
1417c478bd9Sstevel@tonic-gate * This function is often called from interrupt level.
1427c478bd9Sstevel@tonic-gate */
14334709573Sraf void
port_send_event(port_kevent_t * pkevp)1447c478bd9Sstevel@tonic-gate port_send_event(port_kevent_t *pkevp)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate port_queue_t *portq;
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate portq = &pkevp->portkev_port->port_queue;
1497c478bd9Sstevel@tonic-gate mutex_enter(&portq->portq_mutex);
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate if (pkevp->portkev_flags & PORT_KEV_DONEQ) {
1527c478bd9Sstevel@tonic-gate /* Event already in the port queue */
15311dc39ddSpraks if (pkevp->portkev_source == PORT_SOURCE_FD) {
15461b4b1efSpraks mutex_exit(&pkevp->portkev_lock);
15561b4b1efSpraks }
1567c478bd9Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
15734709573Sraf return;
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate /* put event in the port queue */
1617c478bd9Sstevel@tonic-gate list_insert_tail(&portq->portq_list, pkevp);
1627c478bd9Sstevel@tonic-gate portq->portq_nent++;
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate /*
16534709573Sraf * Remove the PORTQ_WAIT_EVENTS flag to indicate
16634709573Sraf * that new events are available.
1677c478bd9Sstevel@tonic-gate */
1687c478bd9Sstevel@tonic-gate portq->portq_flags &= ~PORTQ_WAIT_EVENTS;
1697c478bd9Sstevel@tonic-gate pkevp->portkev_flags |= PORT_KEV_DONEQ; /* event enqueued */
1707c478bd9Sstevel@tonic-gate
17111dc39ddSpraks if (pkevp->portkev_source == PORT_SOURCE_FD) {
17261b4b1efSpraks mutex_exit(&pkevp->portkev_lock);
17361b4b1efSpraks }
17461b4b1efSpraks
1757c478bd9Sstevel@tonic-gate /* Check if thread is in port_close() waiting for outstanding events */
1767c478bd9Sstevel@tonic-gate if (portq->portq_flags & PORTQ_CLOSE) {
1777c478bd9Sstevel@tonic-gate /* Check if all outstanding events are already in port queue */
1787c478bd9Sstevel@tonic-gate if (pkevp->portkev_port->port_curr <= portq->portq_nent)
1797c478bd9Sstevel@tonic-gate cv_signal(&portq->portq_closecv);
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate if (portq->portq_getn == 0) {
1837c478bd9Sstevel@tonic-gate /*
1847c478bd9Sstevel@tonic-gate * No thread retrieving events -> check if enough events are
1857c478bd9Sstevel@tonic-gate * available to satify waiting threads.
1867c478bd9Sstevel@tonic-gate */
1877c478bd9Sstevel@tonic-gate if (portq->portq_thread &&
1887c478bd9Sstevel@tonic-gate (portq->portq_nent >= portq->portq_nget))
1897c478bd9Sstevel@tonic-gate cv_signal(&portq->portq_thread->portget_cv);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate
19211dc39ddSpraks /*
19311dc39ddSpraks * If some thread is polling the port's fd, then notify it.
19411dc39ddSpraks * For PORT_SOURCE_FD source, we don't need to call pollwakeup()
19511dc39ddSpraks * here as it will result in a recursive call(PORT_SOURCE_FD source
19611dc39ddSpraks * is pollwakeup()). Therefore pollwakeup() itself will notify the
19711dc39ddSpraks * ports if being polled.
19811dc39ddSpraks */
19911dc39ddSpraks if (pkevp->portkev_source != PORT_SOURCE_FD &&
20011dc39ddSpraks portq->portq_flags & PORTQ_POLLIN) {
201*5b54b623Sethindra port_t *pp;
202*5b54b623Sethindra
2037c478bd9Sstevel@tonic-gate portq->portq_flags &= ~PORTQ_POLLIN;
204*5b54b623Sethindra /*
205*5b54b623Sethindra * Need to save port_t for calling pollwakeup since port_getn()
206*5b54b623Sethindra * may end up freeing pkevp once portq_mutex is dropped.
207*5b54b623Sethindra */
208*5b54b623Sethindra pp = pkevp->portkev_port;
2097c478bd9Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
210*5b54b623Sethindra pollwakeup(&pp->port_pollhd, POLLIN);
2117c478bd9Sstevel@tonic-gate } else {
2127c478bd9Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate /*
2177c478bd9Sstevel@tonic-gate * The port_alloc_event() function has to be used by all event sources
2187c478bd9Sstevel@tonic-gate * to request an slot for event notification.
2197c478bd9Sstevel@tonic-gate * The slot reservation could be denied because of lack of resources.
2207c478bd9Sstevel@tonic-gate * For that reason the event source should allocate an event slot as early
2217c478bd9Sstevel@tonic-gate * as possible and be prepared to get an error code instead of the
2227c478bd9Sstevel@tonic-gate * port event pointer.
2237c478bd9Sstevel@tonic-gate * Al current event sources allocate an event slot during a system call
2247c478bd9Sstevel@tonic-gate * entry. They return an error code to the application if an event slot
2257c478bd9Sstevel@tonic-gate * could not be reserved.
2267c478bd9Sstevel@tonic-gate * It is also recommended to associate the event source with the port
2277c478bd9Sstevel@tonic-gate * before some other port function is used.
2287c478bd9Sstevel@tonic-gate * The port argument is a file descriptor obtained by the application as
2297c478bd9Sstevel@tonic-gate * a return value of port_create().
2307c478bd9Sstevel@tonic-gate * Possible values of flags are:
2317c478bd9Sstevel@tonic-gate * PORT_ALLOC_DEFAULT
2327c478bd9Sstevel@tonic-gate * This is the standard type of port events. port_get(n) will free this
2337c478bd9Sstevel@tonic-gate * type of event structures as soon as the events are delivered to the
2347c478bd9Sstevel@tonic-gate * application.
2357c478bd9Sstevel@tonic-gate * PORT_ALLOC_PRIVATE
2367c478bd9Sstevel@tonic-gate * This type of event will be use for private use of the event source.
2377c478bd9Sstevel@tonic-gate * The port_get(n) function will deliver events of such an structure to
2387c478bd9Sstevel@tonic-gate * the application but it will not free the event structure itself.
2397c478bd9Sstevel@tonic-gate * The event source must free this structure using port_free_event().
2407c478bd9Sstevel@tonic-gate * PORT_ALLOC_CACHED
2417c478bd9Sstevel@tonic-gate * This type of events is used when the event source helds an own
2427c478bd9Sstevel@tonic-gate * cache.
2437c478bd9Sstevel@tonic-gate * The port_get(n) function will deliver events of such an structure to
2447c478bd9Sstevel@tonic-gate * the application but it will not free the event structure itself.
2457c478bd9Sstevel@tonic-gate * The event source must free this structure using port_free_event().
2467c478bd9Sstevel@tonic-gate */
2477c478bd9Sstevel@tonic-gate int
port_alloc_event(int port,int flags,int source,port_kevent_t ** pkevpp)2487c478bd9Sstevel@tonic-gate port_alloc_event(int port, int flags, int source, port_kevent_t **pkevpp)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate port_t *pp;
2517c478bd9Sstevel@tonic-gate file_t *fp;
25234709573Sraf port_kevent_t *pkevp;
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate if ((fp = getf(port)) == NULL)
2557c478bd9Sstevel@tonic-gate return (EBADF);
2567c478bd9Sstevel@tonic-gate
2577c478bd9Sstevel@tonic-gate if (fp->f_vnode->v_type != VPORT) {
2587c478bd9Sstevel@tonic-gate releasef(port);
2597c478bd9Sstevel@tonic-gate return (EBADFD);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate
26234709573Sraf pkevp = kmem_cache_alloc(port_control.pc_cache, KM_NOSLEEP);
26334709573Sraf if (pkevp == NULL) {
26434709573Sraf releasef(port);
26534709573Sraf return (ENOMEM);
26634709573Sraf }
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate /*
2697c478bd9Sstevel@tonic-gate * port_max_events is controlled by the resource control
2707c478bd9Sstevel@tonic-gate * process.port-max-events
2717c478bd9Sstevel@tonic-gate */
27234709573Sraf pp = VTOEP(fp->f_vnode);
27334709573Sraf mutex_enter(&pp->port_queue.portq_mutex);
2747c478bd9Sstevel@tonic-gate if (pp->port_curr >= pp->port_max_events) {
27534709573Sraf mutex_exit(&pp->port_queue.portq_mutex);
27634709573Sraf kmem_cache_free(port_control.pc_cache, pkevp);
2777c478bd9Sstevel@tonic-gate releasef(port);
2787c478bd9Sstevel@tonic-gate return (EAGAIN);
2797c478bd9Sstevel@tonic-gate }
28034709573Sraf pp->port_curr++;
28134709573Sraf mutex_exit(&pp->port_queue.portq_mutex);
2827c478bd9Sstevel@tonic-gate
28334709573Sraf bzero(pkevp, sizeof (port_kevent_t));
28434709573Sraf mutex_init(&pkevp->portkev_lock, NULL, MUTEX_DEFAULT, NULL);
28534709573Sraf pkevp->portkev_source = source;
28634709573Sraf pkevp->portkev_flags = flags;
28734709573Sraf pkevp->portkev_pid = curproc->p_pid;
28834709573Sraf pkevp->portkev_port = pp;
28934709573Sraf *pkevpp = pkevp;
2907c478bd9Sstevel@tonic-gate releasef(port);
2917c478bd9Sstevel@tonic-gate return (0);
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate
2947c478bd9Sstevel@tonic-gate /*
2957c478bd9Sstevel@tonic-gate * This function is faster than the standard port_alloc_event() and
2967c478bd9Sstevel@tonic-gate * can be used when the event source already allocated an event from
2977c478bd9Sstevel@tonic-gate * a port.
2987c478bd9Sstevel@tonic-gate */
2997c478bd9Sstevel@tonic-gate int
port_dup_event(port_kevent_t * pkevp,port_kevent_t ** pkevdupp,int flags)3007c478bd9Sstevel@tonic-gate port_dup_event(port_kevent_t *pkevp, port_kevent_t **pkevdupp, int flags)
3017c478bd9Sstevel@tonic-gate {
3027c478bd9Sstevel@tonic-gate int error;
3037c478bd9Sstevel@tonic-gate
3047c478bd9Sstevel@tonic-gate error = port_alloc_event_local(pkevp->portkev_port,
3057c478bd9Sstevel@tonic-gate pkevp->portkev_source, flags, pkevdupp);
3067c478bd9Sstevel@tonic-gate if (error == 0)
3077c478bd9Sstevel@tonic-gate (*pkevdupp)->portkev_pid = pkevp->portkev_pid;
3087c478bd9Sstevel@tonic-gate return (error);
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate
3117c478bd9Sstevel@tonic-gate /*
3127c478bd9Sstevel@tonic-gate * port_alloc_event_local() is reserved for internal use only.
3137c478bd9Sstevel@tonic-gate * It is doing the same job as port_alloc_event() but with the event port
3147c478bd9Sstevel@tonic-gate * pointer as the first argument.
3157c478bd9Sstevel@tonic-gate * The check of the validity of the port file descriptor is skipped here.
3167c478bd9Sstevel@tonic-gate */
3177c478bd9Sstevel@tonic-gate int
port_alloc_event_local(port_t * pp,int source,int flags,port_kevent_t ** pkevpp)3187c478bd9Sstevel@tonic-gate port_alloc_event_local(port_t *pp, int source, int flags,
3197c478bd9Sstevel@tonic-gate port_kevent_t **pkevpp)
3207c478bd9Sstevel@tonic-gate {
32134709573Sraf port_kevent_t *pkevp;
3227c478bd9Sstevel@tonic-gate
32334709573Sraf pkevp = kmem_cache_alloc(port_control.pc_cache, KM_NOSLEEP);
32434709573Sraf if (pkevp == NULL)
3257c478bd9Sstevel@tonic-gate return (ENOMEM);
3267c478bd9Sstevel@tonic-gate
32734709573Sraf mutex_enter(&pp->port_queue.portq_mutex);
32834709573Sraf if (pp->port_curr >= pp->port_max_events) {
32934709573Sraf mutex_exit(&pp->port_queue.portq_mutex);
33034709573Sraf kmem_cache_free(port_control.pc_cache, pkevp);
33134709573Sraf return (EAGAIN);
33234709573Sraf }
33334709573Sraf pp->port_curr++;
33434709573Sraf mutex_exit(&pp->port_queue.portq_mutex);
33534709573Sraf
33634709573Sraf bzero(pkevp, sizeof (port_kevent_t));
33734709573Sraf mutex_init(&pkevp->portkev_lock, NULL, MUTEX_DEFAULT, NULL);
33834709573Sraf pkevp->portkev_flags = flags;
33934709573Sraf pkevp->portkev_port = pp;
34034709573Sraf pkevp->portkev_source = source;
34134709573Sraf pkevp->portkev_pid = curproc->p_pid;
34234709573Sraf *pkevpp = pkevp;
3437c478bd9Sstevel@tonic-gate return (0);
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate
3467c478bd9Sstevel@tonic-gate /*
3477c478bd9Sstevel@tonic-gate * port_alloc_event_block() has the same functionality of port_alloc_event() +
3487c478bd9Sstevel@tonic-gate * - it blocks if not enough event slots are available and
3497c478bd9Sstevel@tonic-gate * - it blocks if not enough memory is available.
3507c478bd9Sstevel@tonic-gate * Currently port_dispatch() is using this function to increase the
3517c478bd9Sstevel@tonic-gate * reliability of event delivery for library event sources.
3527c478bd9Sstevel@tonic-gate */
3537c478bd9Sstevel@tonic-gate int
port_alloc_event_block(port_t * pp,int source,int flags,port_kevent_t ** pkevpp)3547c478bd9Sstevel@tonic-gate port_alloc_event_block(port_t *pp, int source, int flags,
35534709573Sraf port_kevent_t **pkevpp)
3567c478bd9Sstevel@tonic-gate {
35734709573Sraf port_kevent_t *pkevp =
35834709573Sraf kmem_cache_alloc(port_control.pc_cache, KM_SLEEP);
3597c478bd9Sstevel@tonic-gate
36034709573Sraf mutex_enter(&pp->port_queue.portq_mutex);
3617c478bd9Sstevel@tonic-gate while (pp->port_curr >= pp->port_max_events) {
36234709573Sraf if (!cv_wait_sig(&pp->port_cv, &pp->port_queue.portq_mutex)) {
3637c478bd9Sstevel@tonic-gate /* signal detected */
36434709573Sraf mutex_exit(&pp->port_queue.portq_mutex);
36534709573Sraf kmem_cache_free(port_control.pc_cache, pkevp);
3667c478bd9Sstevel@tonic-gate return (EINTR);
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate }
36934709573Sraf pp->port_curr++;
37034709573Sraf mutex_exit(&pp->port_queue.portq_mutex);
3717c478bd9Sstevel@tonic-gate
37234709573Sraf bzero(pkevp, sizeof (port_kevent_t));
37334709573Sraf mutex_init(&pkevp->portkev_lock, NULL, MUTEX_DEFAULT, NULL);
37434709573Sraf pkevp->portkev_flags = flags;
37534709573Sraf pkevp->portkev_port = pp;
37634709573Sraf pkevp->portkev_source = source;
37734709573Sraf pkevp->portkev_pid = curproc->p_pid;
37834709573Sraf *pkevpp = pkevp;
3797c478bd9Sstevel@tonic-gate return (0);
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate
3827c478bd9Sstevel@tonic-gate /*
3837c478bd9Sstevel@tonic-gate * Take an event out of the port queue
3847c478bd9Sstevel@tonic-gate */
3857c478bd9Sstevel@tonic-gate static void
port_remove_event_doneq(port_kevent_t * pkevp,port_queue_t * portq)3867c478bd9Sstevel@tonic-gate port_remove_event_doneq(port_kevent_t *pkevp, port_queue_t *portq)
3877c478bd9Sstevel@tonic-gate {
3887c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&portq->portq_mutex));
3897c478bd9Sstevel@tonic-gate list_remove(&portq->portq_list, pkevp);
3907c478bd9Sstevel@tonic-gate portq->portq_nent--;
3917c478bd9Sstevel@tonic-gate pkevp->portkev_flags &= ~PORT_KEV_DONEQ;
3927c478bd9Sstevel@tonic-gate }
3937c478bd9Sstevel@tonic-gate
3947c478bd9Sstevel@tonic-gate /*
3957c478bd9Sstevel@tonic-gate * The port_remove_done_event() function takes a fired event out of the
3967c478bd9Sstevel@tonic-gate * port queue.
3977c478bd9Sstevel@tonic-gate * Currently this function is required to cancel a fired event because
3987c478bd9Sstevel@tonic-gate * the application is delivering new association data (see port_associate_fd()).
3997c478bd9Sstevel@tonic-gate */
400df2381bfSpraks int
port_remove_done_event(port_kevent_t * pkevp)4017c478bd9Sstevel@tonic-gate port_remove_done_event(port_kevent_t *pkevp)
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate port_queue_t *portq;
404df2381bfSpraks int removed = 0;
4057c478bd9Sstevel@tonic-gate
4067c478bd9Sstevel@tonic-gate portq = &pkevp->portkev_port->port_queue;
4077c478bd9Sstevel@tonic-gate mutex_enter(&portq->portq_mutex);
4087c478bd9Sstevel@tonic-gate /* wait for port_get() or port_getn() */
40934709573Sraf port_block(portq);
4107c478bd9Sstevel@tonic-gate if (pkevp->portkev_flags & PORT_KEV_DONEQ) {
4117c478bd9Sstevel@tonic-gate /* event still in port queue */
4127c478bd9Sstevel@tonic-gate if (portq->portq_getn) {
4137c478bd9Sstevel@tonic-gate /*
4147c478bd9Sstevel@tonic-gate * There could be still fired events in the temp queue;
4157c478bd9Sstevel@tonic-gate * push those events back to the port queue and
4167c478bd9Sstevel@tonic-gate * remove requested event afterwards.
4177c478bd9Sstevel@tonic-gate */
4187c478bd9Sstevel@tonic-gate port_push_eventq(portq);
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate /* now remove event from the port queue */
4217c478bd9Sstevel@tonic-gate port_remove_event_doneq(pkevp, portq);
422df2381bfSpraks removed = 1;
4237c478bd9Sstevel@tonic-gate }
42434709573Sraf port_unblock(portq);
4257c478bd9Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
426df2381bfSpraks return (removed);
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate
4297c478bd9Sstevel@tonic-gate /*
4307c478bd9Sstevel@tonic-gate * Return port event back to the kmem_cache.
4317c478bd9Sstevel@tonic-gate * If the event is currently in the port queue the event itself will only
4327c478bd9Sstevel@tonic-gate * be set as invalid. The port_get(n) function will not deliver such events
4337c478bd9Sstevel@tonic-gate * to the application and it will return them back to the kmem_cache.
4347c478bd9Sstevel@tonic-gate */
4357c478bd9Sstevel@tonic-gate void
port_free_event(port_kevent_t * pkevp)4367c478bd9Sstevel@tonic-gate port_free_event(port_kevent_t *pkevp)
4377c478bd9Sstevel@tonic-gate {
4387c478bd9Sstevel@tonic-gate port_queue_t *portq;
4397c478bd9Sstevel@tonic-gate port_t *pp;
4407c478bd9Sstevel@tonic-gate
4417c478bd9Sstevel@tonic-gate pp = pkevp->portkev_port;
4427c478bd9Sstevel@tonic-gate if (pp == NULL)
4437c478bd9Sstevel@tonic-gate return;
4447c478bd9Sstevel@tonic-gate if (pkevp->portkev_flags & PORT_ALLOC_PRIVATE) {
4457c478bd9Sstevel@tonic-gate port_free_event_local(pkevp, 0);
4467c478bd9Sstevel@tonic-gate return;
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate
4497c478bd9Sstevel@tonic-gate portq = &pp->port_queue;
4507c478bd9Sstevel@tonic-gate mutex_enter(&portq->portq_mutex);
45134709573Sraf port_block(portq);
4527c478bd9Sstevel@tonic-gate if (pkevp->portkev_flags & PORT_KEV_DONEQ) {
4537c478bd9Sstevel@tonic-gate pkevp->portkev_flags |= PORT_KEV_FREE;
4547c478bd9Sstevel@tonic-gate pkevp->portkev_callback = NULL;
45534709573Sraf port_unblock(portq);
4567c478bd9Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
4577c478bd9Sstevel@tonic-gate return;
4587c478bd9Sstevel@tonic-gate }
45934709573Sraf port_unblock(portq);
4607c478bd9Sstevel@tonic-gate
4617c478bd9Sstevel@tonic-gate if (pkevp->portkev_flags & PORT_KEV_CACHED) {
4627c478bd9Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
4637c478bd9Sstevel@tonic-gate return;
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate
46634709573Sraf if (--pp->port_curr < pp->port_max_events)
46734709573Sraf cv_signal(&pp->port_cv);
4687c478bd9Sstevel@tonic-gate if (portq->portq_flags & PORTQ_CLOSE) {
4697c478bd9Sstevel@tonic-gate /*
4707c478bd9Sstevel@tonic-gate * Another thread is closing the event port.
4717c478bd9Sstevel@tonic-gate * That thread will sleep until all allocated event
4727c478bd9Sstevel@tonic-gate * structures returned to the event port framework.
4737c478bd9Sstevel@tonic-gate * The portq_mutex is used to synchronize the status
4747c478bd9Sstevel@tonic-gate * of the allocated event structures (port_curr).
4757c478bd9Sstevel@tonic-gate */
4767c478bd9Sstevel@tonic-gate if (pp->port_curr <= portq->portq_nent)
4777c478bd9Sstevel@tonic-gate cv_signal(&portq->portq_closecv);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
4807c478bd9Sstevel@tonic-gate port_free_event_local(pkevp, 1);
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate
4837c478bd9Sstevel@tonic-gate /*
4847c478bd9Sstevel@tonic-gate * This event port internal function is used by port_free_event() and
4857c478bd9Sstevel@tonic-gate * other port internal functions to return event structures back to the
4867c478bd9Sstevel@tonic-gate * kmem_cache.
4877c478bd9Sstevel@tonic-gate */
4887c478bd9Sstevel@tonic-gate void
port_free_event_local(port_kevent_t * pkevp,int counter)4897c478bd9Sstevel@tonic-gate port_free_event_local(port_kevent_t *pkevp, int counter)
4907c478bd9Sstevel@tonic-gate {
4917c478bd9Sstevel@tonic-gate port_t *pp = pkevp->portkev_port;
49234709573Sraf port_queue_t *portq = &pp->port_queue;
49334709573Sraf int wakeup;
4947c478bd9Sstevel@tonic-gate
4957c478bd9Sstevel@tonic-gate pkevp->portkev_callback = NULL;
4967c478bd9Sstevel@tonic-gate pkevp->portkev_flags = 0;
4977c478bd9Sstevel@tonic-gate pkevp->portkev_port = NULL;
49861b4b1efSpraks mutex_destroy(&pkevp->portkev_lock);
4997c478bd9Sstevel@tonic-gate kmem_cache_free(port_control.pc_cache, pkevp);
5007c478bd9Sstevel@tonic-gate
5017c478bd9Sstevel@tonic-gate mutex_enter(&portq->portq_mutex);
50234709573Sraf if (counter == 0) {
50334709573Sraf if (--pp->port_curr < pp->port_max_events)
50434709573Sraf cv_signal(&pp->port_cv);
50534709573Sraf }
50634709573Sraf wakeup = (portq->portq_flags & PORTQ_POLLOUT);
5077c478bd9Sstevel@tonic-gate portq->portq_flags &= ~PORTQ_POLLOUT;
5087c478bd9Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
50934709573Sraf
51034709573Sraf /* Submit a POLLOUT event if requested */
51134709573Sraf if (wakeup)
5127c478bd9Sstevel@tonic-gate pollwakeup(&pp->port_pollhd, POLLOUT);
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate
5157c478bd9Sstevel@tonic-gate /*
5167c478bd9Sstevel@tonic-gate * port_init_event(port_event_t *pev, uintptr_t object, void *user,
5177c478bd9Sstevel@tonic-gate * int (*port_callback)(void *, int *, pid_t, int, void *), void *sysarg);
5187c478bd9Sstevel@tonic-gate * This function initializes most of the "wired" elements of the port
5197c478bd9Sstevel@tonic-gate * event structure. This is normally being used just after the allocation
5207c478bd9Sstevel@tonic-gate * of the port event structure.
5217c478bd9Sstevel@tonic-gate * pkevp : pointer to the port event structure
5227c478bd9Sstevel@tonic-gate * object : object associated with this event structure
5237c478bd9Sstevel@tonic-gate * user : user defined pointer delivered with the association function
5247c478bd9Sstevel@tonic-gate * port_callback:
5257c478bd9Sstevel@tonic-gate * Address of the callback function which will be called
5267c478bd9Sstevel@tonic-gate * - just before the event is delivered to the application.
5277c478bd9Sstevel@tonic-gate * The callback function is called in user context and can be
5287c478bd9Sstevel@tonic-gate * used for copyouts, e.g.
5297c478bd9Sstevel@tonic-gate * - on close() or dissociation of the event. The sub-system
5307c478bd9Sstevel@tonic-gate * must remove immediately every existing association of
5317c478bd9Sstevel@tonic-gate * some object with this event.
5327c478bd9Sstevel@tonic-gate * sysarg : event source propietary data
5337c478bd9Sstevel@tonic-gate */
5347c478bd9Sstevel@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)5357c478bd9Sstevel@tonic-gate port_init_event(port_kevent_t *pkevp, uintptr_t object, void *user,
5367c478bd9Sstevel@tonic-gate int (*port_callback)(void *, int *, pid_t, int, void *),
5377c478bd9Sstevel@tonic-gate void *sysarg)
5387c478bd9Sstevel@tonic-gate {
5397c478bd9Sstevel@tonic-gate pkevp->portkev_object = object;
5407c478bd9Sstevel@tonic-gate pkevp->portkev_user = user;
5417c478bd9Sstevel@tonic-gate pkevp->portkev_callback = port_callback;
5427c478bd9Sstevel@tonic-gate pkevp->portkev_arg = sysarg;
5437c478bd9Sstevel@tonic-gate }
5447c478bd9Sstevel@tonic-gate
5457c478bd9Sstevel@tonic-gate /*
5467c478bd9Sstevel@tonic-gate * This routine removes a portfd_t from the fd cache's hash table.
5477c478bd9Sstevel@tonic-gate */
5487c478bd9Sstevel@tonic-gate void
port_pcache_remove_fd(port_fdcache_t * pcp,portfd_t * pfd)5497c478bd9Sstevel@tonic-gate port_pcache_remove_fd(port_fdcache_t *pcp, portfd_t *pfd)
5507c478bd9Sstevel@tonic-gate {
5517c478bd9Sstevel@tonic-gate polldat_t *lpdp;
5527c478bd9Sstevel@tonic-gate polldat_t *cpdp;
5537c478bd9Sstevel@tonic-gate portfd_t **bucket;
5547c478bd9Sstevel@tonic-gate polldat_t *pdp = PFTOD(pfd);
5557c478bd9Sstevel@tonic-gate
5567c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pcp->pc_lock));
5577c478bd9Sstevel@tonic-gate bucket = PORT_FD_BUCKET(pcp, pdp->pd_fd);
5587c478bd9Sstevel@tonic-gate cpdp = PFTOD(*bucket);
5597c478bd9Sstevel@tonic-gate if (pdp == cpdp) {
5607c478bd9Sstevel@tonic-gate *bucket = PDTOF(pdp->pd_hashnext);
56161b4b1efSpraks if (--pcp->pc_fdcount == 0) {
56261b4b1efSpraks /*
56361b4b1efSpraks * signal the thread which may have blocked in
56461b4b1efSpraks * port_close_sourcefd() on lastclose waiting
56561b4b1efSpraks * for pc_fdcount to drop to 0.
56661b4b1efSpraks */
56761b4b1efSpraks cv_signal(&pcp->pc_lclosecv);
56861b4b1efSpraks }
5697c478bd9Sstevel@tonic-gate kmem_free(pfd, sizeof (portfd_t));
5707c478bd9Sstevel@tonic-gate return;
5717c478bd9Sstevel@tonic-gate }
5727c478bd9Sstevel@tonic-gate
5737c478bd9Sstevel@tonic-gate while (cpdp != NULL) {
5747c478bd9Sstevel@tonic-gate lpdp = cpdp;
5757c478bd9Sstevel@tonic-gate cpdp = cpdp->pd_hashnext;
5767c478bd9Sstevel@tonic-gate if (cpdp == pdp) {
5777c478bd9Sstevel@tonic-gate /* polldat struct found */
5787c478bd9Sstevel@tonic-gate lpdp->pd_hashnext = pdp->pd_hashnext;
57961b4b1efSpraks if (--pcp->pc_fdcount == 0) {
58061b4b1efSpraks /*
58161b4b1efSpraks * signal the thread which may have blocked in
58261b4b1efSpraks * port_close_sourcefd() on lastclose waiting
58361b4b1efSpraks * for pc_fdcount to drop to 0.
58461b4b1efSpraks */
58561b4b1efSpraks cv_signal(&pcp->pc_lclosecv);
58661b4b1efSpraks }
5877c478bd9Sstevel@tonic-gate break;
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate }
5907c478bd9Sstevel@tonic-gate ASSERT(cpdp != NULL);
5917c478bd9Sstevel@tonic-gate kmem_free(pfd, sizeof (portfd_t));
5927c478bd9Sstevel@tonic-gate }
5937c478bd9Sstevel@tonic-gate
5947c478bd9Sstevel@tonic-gate /*
5957c478bd9Sstevel@tonic-gate * The port_push_eventq() function is used to move all remaining events
5967c478bd9Sstevel@tonic-gate * from the temporary queue used in port_get(n)() to the standard port
5977c478bd9Sstevel@tonic-gate * queue.
5987c478bd9Sstevel@tonic-gate */
5997c478bd9Sstevel@tonic-gate void
port_push_eventq(port_queue_t * portq)6007c478bd9Sstevel@tonic-gate port_push_eventq(port_queue_t *portq)
6017c478bd9Sstevel@tonic-gate {
6027c478bd9Sstevel@tonic-gate /*
6037c478bd9Sstevel@tonic-gate * Append temporary portq_get_list to the port queue. On return
6047c478bd9Sstevel@tonic-gate * the temporary portq_get_list is empty.
6057c478bd9Sstevel@tonic-gate */
6067c478bd9Sstevel@tonic-gate list_move_tail(&portq->portq_list, &portq->portq_get_list);
6077c478bd9Sstevel@tonic-gate portq->portq_nent += portq->portq_tnent;
6087c478bd9Sstevel@tonic-gate portq->portq_tnent = 0;
6097c478bd9Sstevel@tonic-gate }
6107c478bd9Sstevel@tonic-gate
6117c478bd9Sstevel@tonic-gate /*
6127c478bd9Sstevel@tonic-gate * The port_remove_fd_object() function frees all resources associated with
6139f23d599Spraks * delivered portfd_t structure. Returns 1 if the port_kevent was found
6149f23d599Spraks * and removed from the port queue.
6157c478bd9Sstevel@tonic-gate */
6169f23d599Spraks int
port_remove_fd_object(portfd_t * pfd,port_t * pp,port_fdcache_t * pcp)6177c478bd9Sstevel@tonic-gate port_remove_fd_object(portfd_t *pfd, port_t *pp, port_fdcache_t *pcp)
6187c478bd9Sstevel@tonic-gate {
6197c478bd9Sstevel@tonic-gate port_queue_t *portq;
6207c478bd9Sstevel@tonic-gate polldat_t *pdp = PFTOD(pfd);
6217c478bd9Sstevel@tonic-gate port_kevent_t *pkevp;
6227c478bd9Sstevel@tonic-gate int error;
6239f23d599Spraks int removed = 0;
6247c478bd9Sstevel@tonic-gate
6257c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pcp->pc_lock));
6267c478bd9Sstevel@tonic-gate if (pdp->pd_php != NULL) {
6277c478bd9Sstevel@tonic-gate pollhead_delete(pdp->pd_php, pdp);
6287c478bd9Sstevel@tonic-gate pdp->pd_php = NULL;
6297c478bd9Sstevel@tonic-gate }
6307c478bd9Sstevel@tonic-gate pkevp = pdp->pd_portev;
6317c478bd9Sstevel@tonic-gate portq = &pp->port_queue;
6327c478bd9Sstevel@tonic-gate mutex_enter(&portq->portq_mutex);
63334709573Sraf port_block(portq);
6347c478bd9Sstevel@tonic-gate if (pkevp->portkev_flags & PORT_KEV_DONEQ) {
6357c478bd9Sstevel@tonic-gate if (portq->portq_getn && portq->portq_tnent) {
6367c478bd9Sstevel@tonic-gate /*
6377c478bd9Sstevel@tonic-gate * move events from the temporary "get" queue
6387c478bd9Sstevel@tonic-gate * back to the port queue
6397c478bd9Sstevel@tonic-gate */
6407c478bd9Sstevel@tonic-gate port_push_eventq(portq);
6417c478bd9Sstevel@tonic-gate }
6427c478bd9Sstevel@tonic-gate /* cleanup merged port queue */
6437c478bd9Sstevel@tonic-gate port_remove_event_doneq(pkevp, portq);
6449f23d599Spraks removed = 1;
6457c478bd9Sstevel@tonic-gate }
64634709573Sraf port_unblock(portq);
6477c478bd9Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
6487c478bd9Sstevel@tonic-gate if (pkevp->portkev_callback) {
6497c478bd9Sstevel@tonic-gate (void) (*pkevp->portkev_callback)(pkevp->portkev_arg,
6507c478bd9Sstevel@tonic-gate &error, pkevp->portkev_pid, PORT_CALLBACK_DISSOCIATE,
6517c478bd9Sstevel@tonic-gate pkevp);
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate port_free_event_local(pkevp, 0);
6547c478bd9Sstevel@tonic-gate
6557c478bd9Sstevel@tonic-gate /* remove polldat struct */
6567c478bd9Sstevel@tonic-gate port_pcache_remove_fd(pcp, pfd);
6579f23d599Spraks return (removed);
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate
6607c478bd9Sstevel@tonic-gate /*
6617c478bd9Sstevel@tonic-gate * The port_close_fd() function dissociates a file descriptor from a port
6627c478bd9Sstevel@tonic-gate * and removes all allocated resources.
6637c478bd9Sstevel@tonic-gate * close(2) detects in the uf_entry_t structure that the fd is associated
6647c478bd9Sstevel@tonic-gate * with a port (at least one port).
6657c478bd9Sstevel@tonic-gate * The fd can be associated with several ports.
6667c478bd9Sstevel@tonic-gate */
6677c478bd9Sstevel@tonic-gate void
port_close_pfd(portfd_t * pfd)6687c478bd9Sstevel@tonic-gate port_close_pfd(portfd_t *pfd)
6697c478bd9Sstevel@tonic-gate {
6707c478bd9Sstevel@tonic-gate port_t *pp;
6717c478bd9Sstevel@tonic-gate port_fdcache_t *pcp;
6727c478bd9Sstevel@tonic-gate
67361b4b1efSpraks /*
67461b4b1efSpraks * the portfd_t passed in should be for this proc.
67561b4b1efSpraks */
67661b4b1efSpraks ASSERT(curproc->p_pid == PFTOD(pfd)->pd_portev->portkev_pid);
6777c478bd9Sstevel@tonic-gate pp = PFTOD(pfd)->pd_portev->portkev_port;
6787c478bd9Sstevel@tonic-gate pcp = pp->port_queue.portq_pcp;
6797c478bd9Sstevel@tonic-gate mutex_enter(&pcp->pc_lock);
6809f23d599Spraks (void) port_remove_fd_object(pfd, pp, pcp);
6817c478bd9Sstevel@tonic-gate mutex_exit(&pcp->pc_lock);
6827c478bd9Sstevel@tonic-gate }
6837c478bd9Sstevel@tonic-gate
6847c478bd9Sstevel@tonic-gate /*
6857c478bd9Sstevel@tonic-gate * The port_associate_ksource() function associates an event source with a port.
6867c478bd9Sstevel@tonic-gate * On port_close() all associated sources are requested to free all local
6877c478bd9Sstevel@tonic-gate * resources associated with the event port.
6887c478bd9Sstevel@tonic-gate * The association of a source with a port can only be done one time. Further
6897c478bd9Sstevel@tonic-gate * calls of this function will only increment the reference counter.
6907c478bd9Sstevel@tonic-gate * The allocated port_source_t structure is removed from the port as soon as
6917c478bd9Sstevel@tonic-gate * the reference counter becomes 0.
6927c478bd9Sstevel@tonic-gate */
6937c478bd9Sstevel@tonic-gate /* ARGSUSED */
6947c478bd9Sstevel@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 *))6957c478bd9Sstevel@tonic-gate port_associate_ksource(int port, int source, port_source_t **portsrc,
6967c478bd9Sstevel@tonic-gate void (*port_src_close)(void *, int, pid_t, int), void *arg,
6977c478bd9Sstevel@tonic-gate int (*port_src_associate)(port_kevent_t *, int, int, uintptr_t, void *))
6987c478bd9Sstevel@tonic-gate {
6997c478bd9Sstevel@tonic-gate port_t *pp;
7007c478bd9Sstevel@tonic-gate file_t *fp;
7017c478bd9Sstevel@tonic-gate port_source_t **ps;
7027c478bd9Sstevel@tonic-gate port_source_t *pse;
7037c478bd9Sstevel@tonic-gate
7047c478bd9Sstevel@tonic-gate if ((fp = getf(port)) == NULL)
7057c478bd9Sstevel@tonic-gate return (EBADF);
7067c478bd9Sstevel@tonic-gate
7077c478bd9Sstevel@tonic-gate if (fp->f_vnode->v_type != VPORT) {
7087c478bd9Sstevel@tonic-gate releasef(port);
7097c478bd9Sstevel@tonic-gate return (EBADFD);
7107c478bd9Sstevel@tonic-gate }
7117c478bd9Sstevel@tonic-gate pp = VTOEP(fp->f_vnode);
7127c478bd9Sstevel@tonic-gate
7137c478bd9Sstevel@tonic-gate mutex_enter(&pp->port_queue.portq_source_mutex);
7147c478bd9Sstevel@tonic-gate ps = &pp->port_queue.portq_scache[PORT_SHASH(source)];
7157c478bd9Sstevel@tonic-gate for (pse = *ps; pse != NULL; pse = pse->portsrc_next) {
7167c478bd9Sstevel@tonic-gate if (pse->portsrc_source == source)
7177c478bd9Sstevel@tonic-gate break;
7187c478bd9Sstevel@tonic-gate }
7197c478bd9Sstevel@tonic-gate
7207c478bd9Sstevel@tonic-gate if (pse == NULL) {
7217c478bd9Sstevel@tonic-gate /* Create association of the event source with the port */
7227c478bd9Sstevel@tonic-gate pse = kmem_zalloc(sizeof (port_source_t), KM_NOSLEEP);
7237c478bd9Sstevel@tonic-gate if (pse == NULL) {
7247c478bd9Sstevel@tonic-gate mutex_exit(&pp->port_queue.portq_source_mutex);
7257c478bd9Sstevel@tonic-gate releasef(port);
7267c478bd9Sstevel@tonic-gate return (ENOMEM);
7277c478bd9Sstevel@tonic-gate }
7287c478bd9Sstevel@tonic-gate pse->portsrc_source = source;
7297c478bd9Sstevel@tonic-gate pse->portsrc_close = port_src_close;
7307c478bd9Sstevel@tonic-gate pse->portsrc_closearg = arg;
7317c478bd9Sstevel@tonic-gate pse->portsrc_cnt = 1;
7327c478bd9Sstevel@tonic-gate if (*ps)
7337c478bd9Sstevel@tonic-gate pse->portsrc_next = (*ps)->portsrc_next;
7347c478bd9Sstevel@tonic-gate *ps = pse;
7357c478bd9Sstevel@tonic-gate } else {
7367c478bd9Sstevel@tonic-gate /* entry already available, source is only requesting count */
7377c478bd9Sstevel@tonic-gate pse->portsrc_cnt++;
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate mutex_exit(&pp->port_queue.portq_source_mutex);
7407c478bd9Sstevel@tonic-gate releasef(port);
7417c478bd9Sstevel@tonic-gate if (portsrc)
7427c478bd9Sstevel@tonic-gate *portsrc = pse;
7437c478bd9Sstevel@tonic-gate return (0);
7447c478bd9Sstevel@tonic-gate }
7457c478bd9Sstevel@tonic-gate
7467c478bd9Sstevel@tonic-gate /*
7477c478bd9Sstevel@tonic-gate * The port_dissociate_ksource() function dissociates an event source from
7487c478bd9Sstevel@tonic-gate * a port.
7497c478bd9Sstevel@tonic-gate */
7507c478bd9Sstevel@tonic-gate int
port_dissociate_ksource(int port,int source,port_source_t * ps)7517c478bd9Sstevel@tonic-gate port_dissociate_ksource(int port, int source, port_source_t *ps)
7527c478bd9Sstevel@tonic-gate {
7537c478bd9Sstevel@tonic-gate port_t *pp;
7547c478bd9Sstevel@tonic-gate file_t *fp;
7557c478bd9Sstevel@tonic-gate port_source_t **psh;
7567c478bd9Sstevel@tonic-gate
7577c478bd9Sstevel@tonic-gate if (ps == NULL)
7587c478bd9Sstevel@tonic-gate return (EINVAL);
7597c478bd9Sstevel@tonic-gate
7607c478bd9Sstevel@tonic-gate if ((fp = getf(port)) == NULL)
7617c478bd9Sstevel@tonic-gate return (EBADF);
7627c478bd9Sstevel@tonic-gate
7637c478bd9Sstevel@tonic-gate if (fp->f_vnode->v_type != VPORT) {
7647c478bd9Sstevel@tonic-gate releasef(port);
7657c478bd9Sstevel@tonic-gate return (EBADFD);
7667c478bd9Sstevel@tonic-gate }
7677c478bd9Sstevel@tonic-gate pp = VTOEP(fp->f_vnode);
7687c478bd9Sstevel@tonic-gate
7697c478bd9Sstevel@tonic-gate mutex_enter(&pp->port_queue.portq_source_mutex);
7707c478bd9Sstevel@tonic-gate if (--ps->portsrc_cnt == 0) {
7717c478bd9Sstevel@tonic-gate /* last association removed -> free source structure */
7727c478bd9Sstevel@tonic-gate if (ps->portsrc_prev == NULL) {
7737c478bd9Sstevel@tonic-gate /* first entry */
7747c478bd9Sstevel@tonic-gate psh = &pp->port_queue.portq_scache[PORT_SHASH(source)];
7757c478bd9Sstevel@tonic-gate *psh = ps->portsrc_next;
7767c478bd9Sstevel@tonic-gate if (ps->portsrc_next)
7777c478bd9Sstevel@tonic-gate ps->portsrc_next->portsrc_prev = NULL;
7787c478bd9Sstevel@tonic-gate } else {
7797c478bd9Sstevel@tonic-gate ps->portsrc_prev->portsrc_next = ps->portsrc_next;
7807c478bd9Sstevel@tonic-gate if (ps->portsrc_next)
7817c478bd9Sstevel@tonic-gate ps->portsrc_next->portsrc_prev =
7827c478bd9Sstevel@tonic-gate ps->portsrc_prev;
7837c478bd9Sstevel@tonic-gate }
7847c478bd9Sstevel@tonic-gate kmem_free(ps, sizeof (port_source_t));
7857c478bd9Sstevel@tonic-gate }
7867c478bd9Sstevel@tonic-gate mutex_exit(&pp->port_queue.portq_source_mutex);
7877c478bd9Sstevel@tonic-gate releasef(port);
7887c478bd9Sstevel@tonic-gate return (0);
7897c478bd9Sstevel@tonic-gate }
790df2381bfSpraks
791df2381bfSpraks void
free_fopdata(vnode_t * vp)792df2381bfSpraks free_fopdata(vnode_t *vp)
793df2381bfSpraks {
794df2381bfSpraks portfop_vp_t *pvp;
795df2381bfSpraks pvp = vp->v_fopdata;
796df2381bfSpraks ASSERT(pvp->pvp_femp == NULL);
797df2381bfSpraks mutex_destroy(&pvp->pvp_mutex);
798df2381bfSpraks list_destroy(&pvp->pvp_pfoplist);
799df2381bfSpraks kmem_free(pvp, sizeof (*pvp));
800df2381bfSpraks vp->v_fopdata = NULL;
801df2381bfSpraks }
802