10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51885Sraf * Common Development and Distribution License (the "License").
61885Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211885Sraf
220Sstevel@tonic-gate /*
23*6182Sethindra * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * This file containts all the functions required for interactions of
310Sstevel@tonic-gate * event sources with the event port file system.
320Sstevel@tonic-gate */
330Sstevel@tonic-gate
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <sys/conf.h>
360Sstevel@tonic-gate #include <sys/stat.h>
370Sstevel@tonic-gate #include <sys/errno.h>
380Sstevel@tonic-gate #include <sys/kmem.h>
390Sstevel@tonic-gate #include <sys/debug.h>
400Sstevel@tonic-gate #include <sys/file.h>
410Sstevel@tonic-gate #include <sys/sysmacros.h>
420Sstevel@tonic-gate #include <sys/systm.h>
430Sstevel@tonic-gate #include <sys/bitmap.h>
440Sstevel@tonic-gate #include <sys/rctl.h>
450Sstevel@tonic-gate #include <sys/atomic.h>
460Sstevel@tonic-gate #include <sys/poll_impl.h>
470Sstevel@tonic-gate #include <sys/port_impl.h>
480Sstevel@tonic-gate
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate * Maximum number of elements allowed to be passed in a single call of a
510Sstevel@tonic-gate * port function (port_sendn(), port_getn(). We need to allocate kernel memory
520Sstevel@tonic-gate * for all of them at once, so we can't let it scale without limit.
530Sstevel@tonic-gate */
540Sstevel@tonic-gate uint_t port_max_list = PORT_MAX_LIST;
550Sstevel@tonic-gate port_control_t port_control; /* Event port framework main structure */
560Sstevel@tonic-gate
570Sstevel@tonic-gate /*
581885Sraf * Block other threads from using a port.
591885Sraf * We enter holding portq->portq_mutex but
601885Sraf * we may drop and reacquire this lock.
611885Sraf * Callers must deal with this fact.
621885Sraf */
631885Sraf void
port_block(port_queue_t * portq)641885Sraf port_block(port_queue_t *portq)
651885Sraf {
661885Sraf ASSERT(MUTEX_HELD(&portq->portq_mutex));
671885Sraf
681885Sraf while (portq->portq_flags & PORTQ_BLOCKED)
691885Sraf cv_wait(&portq->portq_block_cv, &portq->portq_mutex);
701885Sraf portq->portq_flags |= PORTQ_BLOCKED;
711885Sraf }
721885Sraf
731885Sraf /*
741885Sraf * Undo port_block(portq).
751885Sraf */
761885Sraf void
port_unblock(port_queue_t * portq)771885Sraf port_unblock(port_queue_t *portq)
781885Sraf {
791885Sraf ASSERT(MUTEX_HELD(&portq->portq_mutex));
801885Sraf
811885Sraf portq->portq_flags &= ~PORTQ_BLOCKED;
821885Sraf cv_signal(&portq->portq_block_cv);
831885Sraf }
841885Sraf
851885Sraf /*
862948Spraks * Called from pollwakeup(PORT_SOURCE_FD source) to determine
872948Spraks * if the port's fd needs to be notified of poll events. If yes,
882948Spraks * we mark the port indicating that pollwakeup() is referring
892948Spraks * it so that the port_t does not disappear. pollwakeup()
902948Spraks * calls port_pollwkdone() after notifying. In port_pollwkdone(),
912948Spraks * we clear the hold on the port_t (clear PORTQ_POLLWK_PEND).
922948Spraks */
932948Spraks int
port_pollwkup(port_t * pp)942948Spraks port_pollwkup(port_t *pp)
952948Spraks {
962948Spraks int events = 0;
972948Spraks port_queue_t *portq;
982948Spraks portq = &pp->port_queue;
992948Spraks mutex_enter(&portq->portq_mutex);
1002948Spraks
1012948Spraks /*
1022948Spraks * Normally, we should not have a situation where PORTQ_POLLIN
1032948Spraks * and PORTQ_POLLWK_PEND are set at the same time, but it is
1042948Spraks * possible. So, in pollwakeup() we ensure that no new fd's get
1052948Spraks * added to the pollhead between the time it notifies poll events
1062948Spraks * and calls poll_wkupdone() where we clear the PORTQ_POLLWK_PEND flag.
1072948Spraks */
1082948Spraks if (portq->portq_flags & PORTQ_POLLIN &&
1092948Spraks !(portq->portq_flags & PORTQ_POLLWK_PEND)) {
1102948Spraks portq->portq_flags &= ~PORTQ_POLLIN;
1112948Spraks portq->portq_flags |= PORTQ_POLLWK_PEND;
1122948Spraks events = POLLIN;
1132948Spraks }
1142948Spraks mutex_exit(&portq->portq_mutex);
1152948Spraks return (events);
1162948Spraks }
1172948Spraks
1182948Spraks void
port_pollwkdone(port_t * pp)1192948Spraks port_pollwkdone(port_t *pp)
1202948Spraks {
1212948Spraks port_queue_t *portq;
1222948Spraks portq = &pp->port_queue;
1232948Spraks ASSERT(portq->portq_flags & PORTQ_POLLWK_PEND);
1242948Spraks mutex_enter(&portq->portq_mutex);
1252948Spraks portq->portq_flags &= ~PORTQ_POLLWK_PEND;
1262948Spraks cv_signal(&pp->port_cv);
1272948Spraks mutex_exit(&portq->portq_mutex);
1282948Spraks }
1292948Spraks
1302948Spraks
1312948Spraks /*
1320Sstevel@tonic-gate * The port_send_event() function is used by all event sources to submit
1330Sstevel@tonic-gate * trigerred events to a port. All the data required for the event management
1340Sstevel@tonic-gate * is already stored in the port_kevent_t structure.
1350Sstevel@tonic-gate * The event port internal data is stored in the port_kevent_t structure
1360Sstevel@tonic-gate * during the allocation time (see port_alloc_event()). The data related to
1370Sstevel@tonic-gate * the event itself and to the event source management is stored in the
1380Sstevel@tonic-gate * port_kevent_t structure between the allocation time and submit time
1390Sstevel@tonic-gate * (see port_init_event()).
1400Sstevel@tonic-gate *
1410Sstevel@tonic-gate * This function is often called from interrupt level.
1420Sstevel@tonic-gate */
1431885Sraf void
port_send_event(port_kevent_t * pkevp)1440Sstevel@tonic-gate port_send_event(port_kevent_t *pkevp)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate port_queue_t *portq;
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate portq = &pkevp->portkev_port->port_queue;
1490Sstevel@tonic-gate mutex_enter(&portq->portq_mutex);
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate if (pkevp->portkev_flags & PORT_KEV_DONEQ) {
1520Sstevel@tonic-gate /* Event already in the port queue */
1532948Spraks if (pkevp->portkev_source == PORT_SOURCE_FD) {
1541425Spraks mutex_exit(&pkevp->portkev_lock);
1551425Spraks }
1560Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
1571885Sraf return;
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate /* put event in the port queue */
1610Sstevel@tonic-gate list_insert_tail(&portq->portq_list, pkevp);
1620Sstevel@tonic-gate portq->portq_nent++;
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate /*
1651885Sraf * Remove the PORTQ_WAIT_EVENTS flag to indicate
1661885Sraf * that new events are available.
1670Sstevel@tonic-gate */
1680Sstevel@tonic-gate portq->portq_flags &= ~PORTQ_WAIT_EVENTS;
1690Sstevel@tonic-gate pkevp->portkev_flags |= PORT_KEV_DONEQ; /* event enqueued */
1700Sstevel@tonic-gate
1712948Spraks if (pkevp->portkev_source == PORT_SOURCE_FD) {
1721425Spraks mutex_exit(&pkevp->portkev_lock);
1731425Spraks }
1741425Spraks
1750Sstevel@tonic-gate /* Check if thread is in port_close() waiting for outstanding events */
1760Sstevel@tonic-gate if (portq->portq_flags & PORTQ_CLOSE) {
1770Sstevel@tonic-gate /* Check if all outstanding events are already in port queue */
1780Sstevel@tonic-gate if (pkevp->portkev_port->port_curr <= portq->portq_nent)
1790Sstevel@tonic-gate cv_signal(&portq->portq_closecv);
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate if (portq->portq_getn == 0) {
1830Sstevel@tonic-gate /*
1840Sstevel@tonic-gate * No thread retrieving events -> check if enough events are
1850Sstevel@tonic-gate * available to satify waiting threads.
1860Sstevel@tonic-gate */
1870Sstevel@tonic-gate if (portq->portq_thread &&
1880Sstevel@tonic-gate (portq->portq_nent >= portq->portq_nget))
1890Sstevel@tonic-gate cv_signal(&portq->portq_thread->portget_cv);
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate
1922948Spraks /*
1932948Spraks * If some thread is polling the port's fd, then notify it.
1942948Spraks * For PORT_SOURCE_FD source, we don't need to call pollwakeup()
1952948Spraks * here as it will result in a recursive call(PORT_SOURCE_FD source
1962948Spraks * is pollwakeup()). Therefore pollwakeup() itself will notify the
1972948Spraks * ports if being polled.
1982948Spraks */
1992948Spraks if (pkevp->portkev_source != PORT_SOURCE_FD &&
2004863Spraks portq->portq_flags & PORTQ_POLLIN) {
201*6182Sethindra port_t *pp;
202*6182Sethindra
2030Sstevel@tonic-gate portq->portq_flags &= ~PORTQ_POLLIN;
204*6182Sethindra /*
205*6182Sethindra * Need to save port_t for calling pollwakeup since port_getn()
206*6182Sethindra * may end up freeing pkevp once portq_mutex is dropped.
207*6182Sethindra */
208*6182Sethindra pp = pkevp->portkev_port;
2090Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
210*6182Sethindra pollwakeup(&pp->port_pollhd, POLLIN);
2110Sstevel@tonic-gate } else {
2120Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate /*
2170Sstevel@tonic-gate * The port_alloc_event() function has to be used by all event sources
2180Sstevel@tonic-gate * to request an slot for event notification.
2190Sstevel@tonic-gate * The slot reservation could be denied because of lack of resources.
2200Sstevel@tonic-gate * For that reason the event source should allocate an event slot as early
2210Sstevel@tonic-gate * as possible and be prepared to get an error code instead of the
2220Sstevel@tonic-gate * port event pointer.
2230Sstevel@tonic-gate * Al current event sources allocate an event slot during a system call
2240Sstevel@tonic-gate * entry. They return an error code to the application if an event slot
2250Sstevel@tonic-gate * could not be reserved.
2260Sstevel@tonic-gate * It is also recommended to associate the event source with the port
2270Sstevel@tonic-gate * before some other port function is used.
2280Sstevel@tonic-gate * The port argument is a file descriptor obtained by the application as
2290Sstevel@tonic-gate * a return value of port_create().
2300Sstevel@tonic-gate * Possible values of flags are:
2310Sstevel@tonic-gate * PORT_ALLOC_DEFAULT
2320Sstevel@tonic-gate * This is the standard type of port events. port_get(n) will free this
2330Sstevel@tonic-gate * type of event structures as soon as the events are delivered to the
2340Sstevel@tonic-gate * application.
2350Sstevel@tonic-gate * PORT_ALLOC_PRIVATE
2360Sstevel@tonic-gate * This type of event will be use for private use of the event source.
2370Sstevel@tonic-gate * The port_get(n) function will deliver events of such an structure to
2380Sstevel@tonic-gate * the application but it will not free the event structure itself.
2390Sstevel@tonic-gate * The event source must free this structure using port_free_event().
2400Sstevel@tonic-gate * PORT_ALLOC_CACHED
2410Sstevel@tonic-gate * This type of events is used when the event source helds an own
2420Sstevel@tonic-gate * cache.
2430Sstevel@tonic-gate * The port_get(n) function will deliver events of such an structure to
2440Sstevel@tonic-gate * the application but it will not free the event structure itself.
2450Sstevel@tonic-gate * The event source must free this structure using port_free_event().
2460Sstevel@tonic-gate */
2470Sstevel@tonic-gate int
port_alloc_event(int port,int flags,int source,port_kevent_t ** pkevpp)2480Sstevel@tonic-gate port_alloc_event(int port, int flags, int source, port_kevent_t **pkevpp)
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate port_t *pp;
2510Sstevel@tonic-gate file_t *fp;
2521885Sraf port_kevent_t *pkevp;
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate if ((fp = getf(port)) == NULL)
2550Sstevel@tonic-gate return (EBADF);
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate if (fp->f_vnode->v_type != VPORT) {
2580Sstevel@tonic-gate releasef(port);
2590Sstevel@tonic-gate return (EBADFD);
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate
2621885Sraf pkevp = kmem_cache_alloc(port_control.pc_cache, KM_NOSLEEP);
2631885Sraf if (pkevp == NULL) {
2641885Sraf releasef(port);
2651885Sraf return (ENOMEM);
2661885Sraf }
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate /*
2690Sstevel@tonic-gate * port_max_events is controlled by the resource control
2700Sstevel@tonic-gate * process.port-max-events
2710Sstevel@tonic-gate */
2721885Sraf pp = VTOEP(fp->f_vnode);
2731885Sraf mutex_enter(&pp->port_queue.portq_mutex);
2740Sstevel@tonic-gate if (pp->port_curr >= pp->port_max_events) {
2751885Sraf mutex_exit(&pp->port_queue.portq_mutex);
2761885Sraf kmem_cache_free(port_control.pc_cache, pkevp);
2770Sstevel@tonic-gate releasef(port);
2780Sstevel@tonic-gate return (EAGAIN);
2790Sstevel@tonic-gate }
2801885Sraf pp->port_curr++;
2811885Sraf mutex_exit(&pp->port_queue.portq_mutex);
2820Sstevel@tonic-gate
2831885Sraf bzero(pkevp, sizeof (port_kevent_t));
2841885Sraf mutex_init(&pkevp->portkev_lock, NULL, MUTEX_DEFAULT, NULL);
2851885Sraf pkevp->portkev_source = source;
2861885Sraf pkevp->portkev_flags = flags;
2871885Sraf pkevp->portkev_pid = curproc->p_pid;
2881885Sraf pkevp->portkev_port = pp;
2891885Sraf *pkevpp = pkevp;
2900Sstevel@tonic-gate releasef(port);
2910Sstevel@tonic-gate return (0);
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate /*
2950Sstevel@tonic-gate * This function is faster than the standard port_alloc_event() and
2960Sstevel@tonic-gate * can be used when the event source already allocated an event from
2970Sstevel@tonic-gate * a port.
2980Sstevel@tonic-gate */
2990Sstevel@tonic-gate int
port_dup_event(port_kevent_t * pkevp,port_kevent_t ** pkevdupp,int flags)3000Sstevel@tonic-gate port_dup_event(port_kevent_t *pkevp, port_kevent_t **pkevdupp, int flags)
3010Sstevel@tonic-gate {
3020Sstevel@tonic-gate int error;
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate error = port_alloc_event_local(pkevp->portkev_port,
3050Sstevel@tonic-gate pkevp->portkev_source, flags, pkevdupp);
3060Sstevel@tonic-gate if (error == 0)
3070Sstevel@tonic-gate (*pkevdupp)->portkev_pid = pkevp->portkev_pid;
3080Sstevel@tonic-gate return (error);
3090Sstevel@tonic-gate }
3100Sstevel@tonic-gate
3110Sstevel@tonic-gate /*
3120Sstevel@tonic-gate * port_alloc_event_local() is reserved for internal use only.
3130Sstevel@tonic-gate * It is doing the same job as port_alloc_event() but with the event port
3140Sstevel@tonic-gate * pointer as the first argument.
3150Sstevel@tonic-gate * The check of the validity of the port file descriptor is skipped here.
3160Sstevel@tonic-gate */
3170Sstevel@tonic-gate int
port_alloc_event_local(port_t * pp,int source,int flags,port_kevent_t ** pkevpp)3180Sstevel@tonic-gate port_alloc_event_local(port_t *pp, int source, int flags,
3190Sstevel@tonic-gate port_kevent_t **pkevpp)
3200Sstevel@tonic-gate {
3211885Sraf port_kevent_t *pkevp;
3220Sstevel@tonic-gate
3231885Sraf pkevp = kmem_cache_alloc(port_control.pc_cache, KM_NOSLEEP);
3241885Sraf if (pkevp == NULL)
3250Sstevel@tonic-gate return (ENOMEM);
3260Sstevel@tonic-gate
3271885Sraf mutex_enter(&pp->port_queue.portq_mutex);
3281885Sraf if (pp->port_curr >= pp->port_max_events) {
3291885Sraf mutex_exit(&pp->port_queue.portq_mutex);
3301885Sraf kmem_cache_free(port_control.pc_cache, pkevp);
3311885Sraf return (EAGAIN);
3321885Sraf }
3331885Sraf pp->port_curr++;
3341885Sraf mutex_exit(&pp->port_queue.portq_mutex);
3351885Sraf
3361885Sraf bzero(pkevp, sizeof (port_kevent_t));
3371885Sraf mutex_init(&pkevp->portkev_lock, NULL, MUTEX_DEFAULT, NULL);
3381885Sraf pkevp->portkev_flags = flags;
3391885Sraf pkevp->portkev_port = pp;
3401885Sraf pkevp->portkev_source = source;
3411885Sraf pkevp->portkev_pid = curproc->p_pid;
3421885Sraf *pkevpp = pkevp;
3430Sstevel@tonic-gate return (0);
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate /*
3470Sstevel@tonic-gate * port_alloc_event_block() has the same functionality of port_alloc_event() +
3480Sstevel@tonic-gate * - it blocks if not enough event slots are available and
3490Sstevel@tonic-gate * - it blocks if not enough memory is available.
3500Sstevel@tonic-gate * Currently port_dispatch() is using this function to increase the
3510Sstevel@tonic-gate * reliability of event delivery for library event sources.
3520Sstevel@tonic-gate */
3530Sstevel@tonic-gate int
port_alloc_event_block(port_t * pp,int source,int flags,port_kevent_t ** pkevpp)3540Sstevel@tonic-gate port_alloc_event_block(port_t *pp, int source, int flags,
3551885Sraf port_kevent_t **pkevpp)
3560Sstevel@tonic-gate {
3571885Sraf port_kevent_t *pkevp =
3581885Sraf kmem_cache_alloc(port_control.pc_cache, KM_SLEEP);
3590Sstevel@tonic-gate
3601885Sraf mutex_enter(&pp->port_queue.portq_mutex);
3611885Sraf while (pp->port_curr >= pp->port_max_events) {
3621885Sraf if (!cv_wait_sig(&pp->port_cv, &pp->port_queue.portq_mutex)) {
3631885Sraf /* signal detected */
3641885Sraf mutex_exit(&pp->port_queue.portq_mutex);
3651885Sraf kmem_cache_free(port_control.pc_cache, pkevp);
3661885Sraf return (EINTR);
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate }
3691885Sraf pp->port_curr++;
3701885Sraf mutex_exit(&pp->port_queue.portq_mutex);
3710Sstevel@tonic-gate
3721885Sraf bzero(pkevp, sizeof (port_kevent_t));
3731885Sraf mutex_init(&pkevp->portkev_lock, NULL, MUTEX_DEFAULT, NULL);
3741885Sraf pkevp->portkev_flags = flags;
3751885Sraf pkevp->portkev_port = pp;
3761885Sraf pkevp->portkev_source = source;
3771885Sraf pkevp->portkev_pid = curproc->p_pid;
3781885Sraf *pkevpp = pkevp;
3790Sstevel@tonic-gate return (0);
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate
3820Sstevel@tonic-gate /*
3830Sstevel@tonic-gate * Take an event out of the port queue
3840Sstevel@tonic-gate */
3850Sstevel@tonic-gate static void
port_remove_event_doneq(port_kevent_t * pkevp,port_queue_t * portq)3860Sstevel@tonic-gate port_remove_event_doneq(port_kevent_t *pkevp, port_queue_t *portq)
3870Sstevel@tonic-gate {
3880Sstevel@tonic-gate ASSERT(MUTEX_HELD(&portq->portq_mutex));
3890Sstevel@tonic-gate list_remove(&portq->portq_list, pkevp);
3900Sstevel@tonic-gate portq->portq_nent--;
3910Sstevel@tonic-gate pkevp->portkev_flags &= ~PORT_KEV_DONEQ;
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate
3940Sstevel@tonic-gate /*
3950Sstevel@tonic-gate * The port_remove_done_event() function takes a fired event out of the
3960Sstevel@tonic-gate * port queue.
3970Sstevel@tonic-gate * Currently this function is required to cancel a fired event because
3980Sstevel@tonic-gate * the application is delivering new association data (see port_associate_fd()).
3990Sstevel@tonic-gate */
4004863Spraks int
port_remove_done_event(port_kevent_t * pkevp)4010Sstevel@tonic-gate port_remove_done_event(port_kevent_t *pkevp)
4020Sstevel@tonic-gate {
4030Sstevel@tonic-gate port_queue_t *portq;
4044863Spraks int removed = 0;
4050Sstevel@tonic-gate
4060Sstevel@tonic-gate portq = &pkevp->portkev_port->port_queue;
4070Sstevel@tonic-gate mutex_enter(&portq->portq_mutex);
4080Sstevel@tonic-gate /* wait for port_get() or port_getn() */
4091885Sraf port_block(portq);
4100Sstevel@tonic-gate if (pkevp->portkev_flags & PORT_KEV_DONEQ) {
4110Sstevel@tonic-gate /* event still in port queue */
4120Sstevel@tonic-gate if (portq->portq_getn) {
4130Sstevel@tonic-gate /*
4140Sstevel@tonic-gate * There could be still fired events in the temp queue;
4150Sstevel@tonic-gate * push those events back to the port queue and
4160Sstevel@tonic-gate * remove requested event afterwards.
4170Sstevel@tonic-gate */
4180Sstevel@tonic-gate port_push_eventq(portq);
4190Sstevel@tonic-gate }
4200Sstevel@tonic-gate /* now remove event from the port queue */
4210Sstevel@tonic-gate port_remove_event_doneq(pkevp, portq);
4224863Spraks removed = 1;
4230Sstevel@tonic-gate }
4241885Sraf port_unblock(portq);
4250Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
4264863Spraks return (removed);
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate
4290Sstevel@tonic-gate /*
4300Sstevel@tonic-gate * Return port event back to the kmem_cache.
4310Sstevel@tonic-gate * If the event is currently in the port queue the event itself will only
4320Sstevel@tonic-gate * be set as invalid. The port_get(n) function will not deliver such events
4330Sstevel@tonic-gate * to the application and it will return them back to the kmem_cache.
4340Sstevel@tonic-gate */
4350Sstevel@tonic-gate void
port_free_event(port_kevent_t * pkevp)4360Sstevel@tonic-gate port_free_event(port_kevent_t *pkevp)
4370Sstevel@tonic-gate {
4380Sstevel@tonic-gate port_queue_t *portq;
4390Sstevel@tonic-gate port_t *pp;
4400Sstevel@tonic-gate
4410Sstevel@tonic-gate pp = pkevp->portkev_port;
4420Sstevel@tonic-gate if (pp == NULL)
4430Sstevel@tonic-gate return;
4440Sstevel@tonic-gate if (pkevp->portkev_flags & PORT_ALLOC_PRIVATE) {
4450Sstevel@tonic-gate port_free_event_local(pkevp, 0);
4460Sstevel@tonic-gate return;
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate
4490Sstevel@tonic-gate portq = &pp->port_queue;
4500Sstevel@tonic-gate mutex_enter(&portq->portq_mutex);
4511885Sraf port_block(portq);
4520Sstevel@tonic-gate if (pkevp->portkev_flags & PORT_KEV_DONEQ) {
4530Sstevel@tonic-gate pkevp->portkev_flags |= PORT_KEV_FREE;
4540Sstevel@tonic-gate pkevp->portkev_callback = NULL;
4551885Sraf port_unblock(portq);
4560Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
4570Sstevel@tonic-gate return;
4580Sstevel@tonic-gate }
4591885Sraf port_unblock(portq);
4600Sstevel@tonic-gate
4610Sstevel@tonic-gate if (pkevp->portkev_flags & PORT_KEV_CACHED) {
4620Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
4630Sstevel@tonic-gate return;
4640Sstevel@tonic-gate }
4650Sstevel@tonic-gate
4661885Sraf if (--pp->port_curr < pp->port_max_events)
4671885Sraf cv_signal(&pp->port_cv);
4680Sstevel@tonic-gate if (portq->portq_flags & PORTQ_CLOSE) {
4690Sstevel@tonic-gate /*
4700Sstevel@tonic-gate * Another thread is closing the event port.
4710Sstevel@tonic-gate * That thread will sleep until all allocated event
4720Sstevel@tonic-gate * structures returned to the event port framework.
4730Sstevel@tonic-gate * The portq_mutex is used to synchronize the status
4740Sstevel@tonic-gate * of the allocated event structures (port_curr).
4750Sstevel@tonic-gate */
4760Sstevel@tonic-gate if (pp->port_curr <= portq->portq_nent)
4770Sstevel@tonic-gate cv_signal(&portq->portq_closecv);
4780Sstevel@tonic-gate }
4790Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
4800Sstevel@tonic-gate port_free_event_local(pkevp, 1);
4810Sstevel@tonic-gate }
4820Sstevel@tonic-gate
4830Sstevel@tonic-gate /*
4840Sstevel@tonic-gate * This event port internal function is used by port_free_event() and
4850Sstevel@tonic-gate * other port internal functions to return event structures back to the
4860Sstevel@tonic-gate * kmem_cache.
4870Sstevel@tonic-gate */
4880Sstevel@tonic-gate void
port_free_event_local(port_kevent_t * pkevp,int counter)4890Sstevel@tonic-gate port_free_event_local(port_kevent_t *pkevp, int counter)
4900Sstevel@tonic-gate {
4911885Sraf port_t *pp = pkevp->portkev_port;
4921885Sraf port_queue_t *portq = &pp->port_queue;
4931885Sraf int wakeup;
4940Sstevel@tonic-gate
4950Sstevel@tonic-gate pkevp->portkev_callback = NULL;
4960Sstevel@tonic-gate pkevp->portkev_flags = 0;
4970Sstevel@tonic-gate pkevp->portkev_port = NULL;
4981425Spraks mutex_destroy(&pkevp->portkev_lock);
4990Sstevel@tonic-gate kmem_cache_free(port_control.pc_cache, pkevp);
5000Sstevel@tonic-gate
5011885Sraf mutex_enter(&portq->portq_mutex);
5021885Sraf if (counter == 0) {
5031885Sraf if (--pp->port_curr < pp->port_max_events)
5041885Sraf cv_signal(&pp->port_cv);
5050Sstevel@tonic-gate }
5061885Sraf wakeup = (portq->portq_flags & PORTQ_POLLOUT);
5071885Sraf portq->portq_flags &= ~PORTQ_POLLOUT;
5081885Sraf mutex_exit(&portq->portq_mutex);
5090Sstevel@tonic-gate
5100Sstevel@tonic-gate /* Submit a POLLOUT event if requested */
5111885Sraf if (wakeup)
5120Sstevel@tonic-gate pollwakeup(&pp->port_pollhd, POLLOUT);
5130Sstevel@tonic-gate }
5140Sstevel@tonic-gate
5150Sstevel@tonic-gate /*
5160Sstevel@tonic-gate * port_init_event(port_event_t *pev, uintptr_t object, void *user,
5170Sstevel@tonic-gate * int (*port_callback)(void *, int *, pid_t, int, void *), void *sysarg);
5180Sstevel@tonic-gate * This function initializes most of the "wired" elements of the port
5190Sstevel@tonic-gate * event structure. This is normally being used just after the allocation
5200Sstevel@tonic-gate * of the port event structure.
5210Sstevel@tonic-gate * pkevp : pointer to the port event structure
5220Sstevel@tonic-gate * object : object associated with this event structure
5230Sstevel@tonic-gate * user : user defined pointer delivered with the association function
5240Sstevel@tonic-gate * port_callback:
5250Sstevel@tonic-gate * Address of the callback function which will be called
5260Sstevel@tonic-gate * - just before the event is delivered to the application.
5270Sstevel@tonic-gate * The callback function is called in user context and can be
5280Sstevel@tonic-gate * used for copyouts, e.g.
5290Sstevel@tonic-gate * - on close() or dissociation of the event. The sub-system
5300Sstevel@tonic-gate * must remove immediately every existing association of
5310Sstevel@tonic-gate * some object with this event.
5320Sstevel@tonic-gate * sysarg : event source propietary data
5330Sstevel@tonic-gate */
5340Sstevel@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)5350Sstevel@tonic-gate port_init_event(port_kevent_t *pkevp, uintptr_t object, void *user,
5360Sstevel@tonic-gate int (*port_callback)(void *, int *, pid_t, int, void *),
5370Sstevel@tonic-gate void *sysarg)
5380Sstevel@tonic-gate {
5390Sstevel@tonic-gate pkevp->portkev_object = object;
5400Sstevel@tonic-gate pkevp->portkev_user = user;
5410Sstevel@tonic-gate pkevp->portkev_callback = port_callback;
5420Sstevel@tonic-gate pkevp->portkev_arg = sysarg;
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate
5450Sstevel@tonic-gate /*
5460Sstevel@tonic-gate * This routine removes a portfd_t from the fd cache's hash table.
5470Sstevel@tonic-gate */
5480Sstevel@tonic-gate void
port_pcache_remove_fd(port_fdcache_t * pcp,portfd_t * pfd)5490Sstevel@tonic-gate port_pcache_remove_fd(port_fdcache_t *pcp, portfd_t *pfd)
5500Sstevel@tonic-gate {
5510Sstevel@tonic-gate polldat_t *lpdp;
5520Sstevel@tonic-gate polldat_t *cpdp;
5530Sstevel@tonic-gate portfd_t **bucket;
5540Sstevel@tonic-gate polldat_t *pdp = PFTOD(pfd);
5550Sstevel@tonic-gate
5560Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pcp->pc_lock));
5570Sstevel@tonic-gate bucket = PORT_FD_BUCKET(pcp, pdp->pd_fd);
5580Sstevel@tonic-gate cpdp = PFTOD(*bucket);
5590Sstevel@tonic-gate if (pdp == cpdp) {
5600Sstevel@tonic-gate *bucket = PDTOF(pdp->pd_hashnext);
5611425Spraks if (--pcp->pc_fdcount == 0) {
5621425Spraks /*
5631425Spraks * signal the thread which may have blocked in
5641425Spraks * port_close_sourcefd() on lastclose waiting
5651425Spraks * for pc_fdcount to drop to 0.
5661425Spraks */
5671425Spraks cv_signal(&pcp->pc_lclosecv);
5681425Spraks }
5690Sstevel@tonic-gate kmem_free(pfd, sizeof (portfd_t));
5700Sstevel@tonic-gate return;
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate
5730Sstevel@tonic-gate while (cpdp != NULL) {
5740Sstevel@tonic-gate lpdp = cpdp;
5750Sstevel@tonic-gate cpdp = cpdp->pd_hashnext;
5760Sstevel@tonic-gate if (cpdp == pdp) {
5770Sstevel@tonic-gate /* polldat struct found */
5780Sstevel@tonic-gate lpdp->pd_hashnext = pdp->pd_hashnext;
5791425Spraks if (--pcp->pc_fdcount == 0) {
5801425Spraks /*
5811425Spraks * signal the thread which may have blocked in
5821425Spraks * port_close_sourcefd() on lastclose waiting
5831425Spraks * for pc_fdcount to drop to 0.
5841425Spraks */
5851425Spraks cv_signal(&pcp->pc_lclosecv);
5861425Spraks }
5870Sstevel@tonic-gate break;
5880Sstevel@tonic-gate }
5890Sstevel@tonic-gate }
5900Sstevel@tonic-gate ASSERT(cpdp != NULL);
5910Sstevel@tonic-gate kmem_free(pfd, sizeof (portfd_t));
5920Sstevel@tonic-gate }
5930Sstevel@tonic-gate
5940Sstevel@tonic-gate /*
5950Sstevel@tonic-gate * The port_push_eventq() function is used to move all remaining events
5960Sstevel@tonic-gate * from the temporary queue used in port_get(n)() to the standard port
5970Sstevel@tonic-gate * queue.
5980Sstevel@tonic-gate */
5990Sstevel@tonic-gate void
port_push_eventq(port_queue_t * portq)6000Sstevel@tonic-gate port_push_eventq(port_queue_t *portq)
6010Sstevel@tonic-gate {
6020Sstevel@tonic-gate /*
6030Sstevel@tonic-gate * Append temporary portq_get_list to the port queue. On return
6040Sstevel@tonic-gate * the temporary portq_get_list is empty.
6050Sstevel@tonic-gate */
6060Sstevel@tonic-gate list_move_tail(&portq->portq_list, &portq->portq_get_list);
6070Sstevel@tonic-gate portq->portq_nent += portq->portq_tnent;
6080Sstevel@tonic-gate portq->portq_tnent = 0;
6090Sstevel@tonic-gate }
6100Sstevel@tonic-gate
6110Sstevel@tonic-gate /*
6120Sstevel@tonic-gate * The port_remove_fd_object() function frees all resources associated with
6133734Spraks * delivered portfd_t structure. Returns 1 if the port_kevent was found
6143734Spraks * and removed from the port queue.
6150Sstevel@tonic-gate */
6163734Spraks int
port_remove_fd_object(portfd_t * pfd,port_t * pp,port_fdcache_t * pcp)6170Sstevel@tonic-gate port_remove_fd_object(portfd_t *pfd, port_t *pp, port_fdcache_t *pcp)
6180Sstevel@tonic-gate {
6190Sstevel@tonic-gate port_queue_t *portq;
6200Sstevel@tonic-gate polldat_t *pdp = PFTOD(pfd);
6210Sstevel@tonic-gate port_kevent_t *pkevp;
6220Sstevel@tonic-gate int error;
6233734Spraks int removed = 0;
6240Sstevel@tonic-gate
6250Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pcp->pc_lock));
6260Sstevel@tonic-gate if (pdp->pd_php != NULL) {
6270Sstevel@tonic-gate pollhead_delete(pdp->pd_php, pdp);
6280Sstevel@tonic-gate pdp->pd_php = NULL;
6290Sstevel@tonic-gate }
6300Sstevel@tonic-gate pkevp = pdp->pd_portev;
6310Sstevel@tonic-gate portq = &pp->port_queue;
6320Sstevel@tonic-gate mutex_enter(&portq->portq_mutex);
6331885Sraf port_block(portq);
6340Sstevel@tonic-gate if (pkevp->portkev_flags & PORT_KEV_DONEQ) {
6350Sstevel@tonic-gate if (portq->portq_getn && portq->portq_tnent) {
6360Sstevel@tonic-gate /*
6370Sstevel@tonic-gate * move events from the temporary "get" queue
6380Sstevel@tonic-gate * back to the port queue
6390Sstevel@tonic-gate */
6400Sstevel@tonic-gate port_push_eventq(portq);
6410Sstevel@tonic-gate }
6420Sstevel@tonic-gate /* cleanup merged port queue */
6430Sstevel@tonic-gate port_remove_event_doneq(pkevp, portq);
6443734Spraks removed = 1;
6450Sstevel@tonic-gate }
6461885Sraf port_unblock(portq);
6470Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
6480Sstevel@tonic-gate if (pkevp->portkev_callback) {
6490Sstevel@tonic-gate (void) (*pkevp->portkev_callback)(pkevp->portkev_arg,
6500Sstevel@tonic-gate &error, pkevp->portkev_pid, PORT_CALLBACK_DISSOCIATE,
6510Sstevel@tonic-gate pkevp);
6520Sstevel@tonic-gate }
6530Sstevel@tonic-gate port_free_event_local(pkevp, 0);
6540Sstevel@tonic-gate
6550Sstevel@tonic-gate /* remove polldat struct */
6560Sstevel@tonic-gate port_pcache_remove_fd(pcp, pfd);
6573734Spraks return (removed);
6580Sstevel@tonic-gate }
6590Sstevel@tonic-gate
6600Sstevel@tonic-gate /*
6610Sstevel@tonic-gate * The port_close_fd() function dissociates a file descriptor from a port
6620Sstevel@tonic-gate * and removes all allocated resources.
6630Sstevel@tonic-gate * close(2) detects in the uf_entry_t structure that the fd is associated
6640Sstevel@tonic-gate * with a port (at least one port).
6650Sstevel@tonic-gate * The fd can be associated with several ports.
6660Sstevel@tonic-gate */
6670Sstevel@tonic-gate void
port_close_pfd(portfd_t * pfd)6680Sstevel@tonic-gate port_close_pfd(portfd_t *pfd)
6690Sstevel@tonic-gate {
6700Sstevel@tonic-gate port_t *pp;
6710Sstevel@tonic-gate port_fdcache_t *pcp;
6720Sstevel@tonic-gate
6731425Spraks /*
6741425Spraks * the portfd_t passed in should be for this proc.
6751425Spraks */
6761425Spraks ASSERT(curproc->p_pid == PFTOD(pfd)->pd_portev->portkev_pid);
6770Sstevel@tonic-gate pp = PFTOD(pfd)->pd_portev->portkev_port;
6780Sstevel@tonic-gate pcp = pp->port_queue.portq_pcp;
6790Sstevel@tonic-gate mutex_enter(&pcp->pc_lock);
6803734Spraks (void) port_remove_fd_object(pfd, pp, pcp);
6810Sstevel@tonic-gate mutex_exit(&pcp->pc_lock);
6820Sstevel@tonic-gate }
6830Sstevel@tonic-gate
6840Sstevel@tonic-gate /*
6850Sstevel@tonic-gate * The port_associate_ksource() function associates an event source with a port.
6860Sstevel@tonic-gate * On port_close() all associated sources are requested to free all local
6870Sstevel@tonic-gate * resources associated with the event port.
6880Sstevel@tonic-gate * The association of a source with a port can only be done one time. Further
6890Sstevel@tonic-gate * calls of this function will only increment the reference counter.
6900Sstevel@tonic-gate * The allocated port_source_t structure is removed from the port as soon as
6910Sstevel@tonic-gate * the reference counter becomes 0.
6920Sstevel@tonic-gate */
6930Sstevel@tonic-gate /* ARGSUSED */
6940Sstevel@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 *))6950Sstevel@tonic-gate port_associate_ksource(int port, int source, port_source_t **portsrc,
6960Sstevel@tonic-gate void (*port_src_close)(void *, int, pid_t, int), void *arg,
6970Sstevel@tonic-gate int (*port_src_associate)(port_kevent_t *, int, int, uintptr_t, void *))
6980Sstevel@tonic-gate {
6990Sstevel@tonic-gate port_t *pp;
7000Sstevel@tonic-gate file_t *fp;
7010Sstevel@tonic-gate port_source_t **ps;
7020Sstevel@tonic-gate port_source_t *pse;
7030Sstevel@tonic-gate
7040Sstevel@tonic-gate if ((fp = getf(port)) == NULL)
7050Sstevel@tonic-gate return (EBADF);
7060Sstevel@tonic-gate
7070Sstevel@tonic-gate if (fp->f_vnode->v_type != VPORT) {
7080Sstevel@tonic-gate releasef(port);
7090Sstevel@tonic-gate return (EBADFD);
7100Sstevel@tonic-gate }
7110Sstevel@tonic-gate pp = VTOEP(fp->f_vnode);
7120Sstevel@tonic-gate
7130Sstevel@tonic-gate mutex_enter(&pp->port_queue.portq_source_mutex);
7140Sstevel@tonic-gate ps = &pp->port_queue.portq_scache[PORT_SHASH(source)];
7150Sstevel@tonic-gate for (pse = *ps; pse != NULL; pse = pse->portsrc_next) {
7160Sstevel@tonic-gate if (pse->portsrc_source == source)
7170Sstevel@tonic-gate break;
7180Sstevel@tonic-gate }
7190Sstevel@tonic-gate
7200Sstevel@tonic-gate if (pse == NULL) {
7210Sstevel@tonic-gate /* Create association of the event source with the port */
7220Sstevel@tonic-gate pse = kmem_zalloc(sizeof (port_source_t), KM_NOSLEEP);
7230Sstevel@tonic-gate if (pse == NULL) {
7240Sstevel@tonic-gate mutex_exit(&pp->port_queue.portq_source_mutex);
7250Sstevel@tonic-gate releasef(port);
7260Sstevel@tonic-gate return (ENOMEM);
7270Sstevel@tonic-gate }
7280Sstevel@tonic-gate pse->portsrc_source = source;
7290Sstevel@tonic-gate pse->portsrc_close = port_src_close;
7300Sstevel@tonic-gate pse->portsrc_closearg = arg;
7310Sstevel@tonic-gate pse->portsrc_cnt = 1;
7320Sstevel@tonic-gate if (*ps)
7330Sstevel@tonic-gate pse->portsrc_next = (*ps)->portsrc_next;
7340Sstevel@tonic-gate *ps = pse;
7350Sstevel@tonic-gate } else {
7360Sstevel@tonic-gate /* entry already available, source is only requesting count */
7370Sstevel@tonic-gate pse->portsrc_cnt++;
7380Sstevel@tonic-gate }
7390Sstevel@tonic-gate mutex_exit(&pp->port_queue.portq_source_mutex);
7400Sstevel@tonic-gate releasef(port);
7410Sstevel@tonic-gate if (portsrc)
7420Sstevel@tonic-gate *portsrc = pse;
7430Sstevel@tonic-gate return (0);
7440Sstevel@tonic-gate }
7450Sstevel@tonic-gate
7460Sstevel@tonic-gate /*
7470Sstevel@tonic-gate * The port_dissociate_ksource() function dissociates an event source from
7480Sstevel@tonic-gate * a port.
7490Sstevel@tonic-gate */
7500Sstevel@tonic-gate int
port_dissociate_ksource(int port,int source,port_source_t * ps)7510Sstevel@tonic-gate port_dissociate_ksource(int port, int source, port_source_t *ps)
7520Sstevel@tonic-gate {
7530Sstevel@tonic-gate port_t *pp;
7540Sstevel@tonic-gate file_t *fp;
7550Sstevel@tonic-gate port_source_t **psh;
7560Sstevel@tonic-gate
7570Sstevel@tonic-gate if (ps == NULL)
7580Sstevel@tonic-gate return (EINVAL);
7590Sstevel@tonic-gate
7600Sstevel@tonic-gate if ((fp = getf(port)) == NULL)
7610Sstevel@tonic-gate return (EBADF);
7620Sstevel@tonic-gate
7630Sstevel@tonic-gate if (fp->f_vnode->v_type != VPORT) {
7640Sstevel@tonic-gate releasef(port);
7650Sstevel@tonic-gate return (EBADFD);
7660Sstevel@tonic-gate }
7670Sstevel@tonic-gate pp = VTOEP(fp->f_vnode);
7680Sstevel@tonic-gate
7690Sstevel@tonic-gate mutex_enter(&pp->port_queue.portq_source_mutex);
7700Sstevel@tonic-gate if (--ps->portsrc_cnt == 0) {
7710Sstevel@tonic-gate /* last association removed -> free source structure */
7720Sstevel@tonic-gate if (ps->portsrc_prev == NULL) {
7730Sstevel@tonic-gate /* first entry */
7740Sstevel@tonic-gate psh = &pp->port_queue.portq_scache[PORT_SHASH(source)];
7750Sstevel@tonic-gate *psh = ps->portsrc_next;
7760Sstevel@tonic-gate if (ps->portsrc_next)
7770Sstevel@tonic-gate ps->portsrc_next->portsrc_prev = NULL;
7780Sstevel@tonic-gate } else {
7790Sstevel@tonic-gate ps->portsrc_prev->portsrc_next = ps->portsrc_next;
7800Sstevel@tonic-gate if (ps->portsrc_next)
7810Sstevel@tonic-gate ps->portsrc_next->portsrc_prev =
7820Sstevel@tonic-gate ps->portsrc_prev;
7830Sstevel@tonic-gate }
7840Sstevel@tonic-gate kmem_free(ps, sizeof (port_source_t));
7850Sstevel@tonic-gate }
7860Sstevel@tonic-gate mutex_exit(&pp->port_queue.portq_source_mutex);
7870Sstevel@tonic-gate releasef(port);
7880Sstevel@tonic-gate return (0);
7890Sstevel@tonic-gate }
7904863Spraks
7914863Spraks void
free_fopdata(vnode_t * vp)7924863Spraks free_fopdata(vnode_t *vp)
7934863Spraks {
7944863Spraks portfop_vp_t *pvp;
7954863Spraks pvp = vp->v_fopdata;
7964863Spraks ASSERT(pvp->pvp_femp == NULL);
7974863Spraks mutex_destroy(&pvp->pvp_mutex);
7984863Spraks list_destroy(&pvp->pvp_pfoplist);
7994863Spraks kmem_free(pvp, sizeof (*pvp));
8004863Spraks vp->v_fopdata = NULL;
8014863Spraks }
802