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 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23*1425Spraks * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * This file containts all the functions required for interactions of 310Sstevel@tonic-gate * event sources with the event port file system. 320Sstevel@tonic-gate */ 330Sstevel@tonic-gate 340Sstevel@tonic-gate #include <sys/types.h> 350Sstevel@tonic-gate #include <sys/conf.h> 360Sstevel@tonic-gate #include <sys/stat.h> 370Sstevel@tonic-gate #include <sys/errno.h> 380Sstevel@tonic-gate #include <sys/kmem.h> 390Sstevel@tonic-gate #include <sys/debug.h> 400Sstevel@tonic-gate #include <sys/file.h> 410Sstevel@tonic-gate #include <sys/sysmacros.h> 420Sstevel@tonic-gate #include <sys/systm.h> 430Sstevel@tonic-gate #include <sys/bitmap.h> 440Sstevel@tonic-gate #include <sys/rctl.h> 450Sstevel@tonic-gate #include <sys/atomic.h> 460Sstevel@tonic-gate #include <sys/poll_impl.h> 470Sstevel@tonic-gate #include <sys/port_impl.h> 480Sstevel@tonic-gate 490Sstevel@tonic-gate /* 500Sstevel@tonic-gate * Maximum number of elements allowed to be passed in a single call of a 510Sstevel@tonic-gate * port function (port_sendn(), port_getn(). We need to allocate kernel memory 520Sstevel@tonic-gate * for all of them at once, so we can't let it scale without limit. 530Sstevel@tonic-gate */ 540Sstevel@tonic-gate uint_t port_max_list = PORT_MAX_LIST; 550Sstevel@tonic-gate port_control_t port_control; /* Event port framework main structure */ 560Sstevel@tonic-gate 570Sstevel@tonic-gate /* 580Sstevel@tonic-gate * The port_send_event() function is used by all event sources to submit 590Sstevel@tonic-gate * trigerred events to a port. All the data required for the event management 600Sstevel@tonic-gate * is already stored in the port_kevent_t structure. 610Sstevel@tonic-gate * The event port internal data is stored in the port_kevent_t structure 620Sstevel@tonic-gate * during the allocation time (see port_alloc_event()). The data related to 630Sstevel@tonic-gate * the event itself and to the event source management is stored in the 640Sstevel@tonic-gate * port_kevent_t structure between the allocation time and submit time 650Sstevel@tonic-gate * (see port_init_event()). 660Sstevel@tonic-gate * 670Sstevel@tonic-gate * This function is often called from interrupt level. 680Sstevel@tonic-gate */ 690Sstevel@tonic-gate int 700Sstevel@tonic-gate port_send_event(port_kevent_t *pkevp) 710Sstevel@tonic-gate { 720Sstevel@tonic-gate port_queue_t *portq; 730Sstevel@tonic-gate 740Sstevel@tonic-gate portq = &pkevp->portkev_port->port_queue; 750Sstevel@tonic-gate mutex_enter(&portq->portq_mutex); 760Sstevel@tonic-gate 770Sstevel@tonic-gate if (pkevp->portkev_flags & PORT_KEV_DONEQ) { 780Sstevel@tonic-gate /* Event already in the port queue */ 79*1425Spraks if (pkevp->portkev_flags & PORT_ALLOC_CACHED) { 80*1425Spraks mutex_exit(&pkevp->portkev_lock); 81*1425Spraks } 820Sstevel@tonic-gate mutex_exit(&portq->portq_mutex); 830Sstevel@tonic-gate return (0); 840Sstevel@tonic-gate } 850Sstevel@tonic-gate 860Sstevel@tonic-gate /* put event in the port queue */ 870Sstevel@tonic-gate list_insert_tail(&portq->portq_list, pkevp); 880Sstevel@tonic-gate portq->portq_nent++; 890Sstevel@tonic-gate 900Sstevel@tonic-gate /* 910Sstevel@tonic-gate * Remove PORTQ_WAIT_EVENTS flags to indicate that new events are 920Sstevel@tonic-gate * available. 930Sstevel@tonic-gate */ 940Sstevel@tonic-gate portq->portq_flags &= ~PORTQ_WAIT_EVENTS; 950Sstevel@tonic-gate pkevp->portkev_flags |= PORT_KEV_DONEQ; /* event enqueued */ 960Sstevel@tonic-gate 97*1425Spraks if (pkevp->portkev_flags & PORT_ALLOC_CACHED) { 98*1425Spraks mutex_exit(&pkevp->portkev_lock); 99*1425Spraks } 100*1425Spraks 1010Sstevel@tonic-gate /* Check if thread is in port_close() waiting for outstanding events */ 1020Sstevel@tonic-gate if (portq->portq_flags & PORTQ_CLOSE) { 1030Sstevel@tonic-gate /* Check if all outstanding events are already in port queue */ 1040Sstevel@tonic-gate if (pkevp->portkev_port->port_curr <= portq->portq_nent) 1050Sstevel@tonic-gate cv_signal(&portq->portq_closecv); 1060Sstevel@tonic-gate } 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate if (portq->portq_getn == 0) { 1090Sstevel@tonic-gate /* 1100Sstevel@tonic-gate * No thread retrieving events -> check if enough events are 1110Sstevel@tonic-gate * available to satify waiting threads. 1120Sstevel@tonic-gate */ 1130Sstevel@tonic-gate if (portq->portq_thread && 1140Sstevel@tonic-gate (portq->portq_nent >= portq->portq_nget)) 1150Sstevel@tonic-gate cv_signal(&portq->portq_thread->portget_cv); 1160Sstevel@tonic-gate } 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate if (portq->portq_flags & PORTQ_POLLIN) { 1190Sstevel@tonic-gate portq->portq_flags &= ~PORTQ_POLLIN; 1200Sstevel@tonic-gate mutex_exit(&portq->portq_mutex); 1210Sstevel@tonic-gate pollwakeup(&pkevp->portkev_port->port_pollhd, POLLIN); 1220Sstevel@tonic-gate } else { 1230Sstevel@tonic-gate mutex_exit(&portq->portq_mutex); 1240Sstevel@tonic-gate } 1250Sstevel@tonic-gate return (0); 1260Sstevel@tonic-gate } 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate /* 1290Sstevel@tonic-gate * The port_alloc_event() function has to be used by all event sources 1300Sstevel@tonic-gate * to request an slot for event notification. 1310Sstevel@tonic-gate * The slot reservation could be denied because of lack of resources. 1320Sstevel@tonic-gate * For that reason the event source should allocate an event slot as early 1330Sstevel@tonic-gate * as possible and be prepared to get an error code instead of the 1340Sstevel@tonic-gate * port event pointer. 1350Sstevel@tonic-gate * Al current event sources allocate an event slot during a system call 1360Sstevel@tonic-gate * entry. They return an error code to the application if an event slot 1370Sstevel@tonic-gate * could not be reserved. 1380Sstevel@tonic-gate * It is also recommended to associate the event source with the port 1390Sstevel@tonic-gate * before some other port function is used. 1400Sstevel@tonic-gate * The port argument is a file descriptor obtained by the application as 1410Sstevel@tonic-gate * a return value of port_create(). 1420Sstevel@tonic-gate * Possible values of flags are: 1430Sstevel@tonic-gate * PORT_ALLOC_DEFAULT 1440Sstevel@tonic-gate * This is the standard type of port events. port_get(n) will free this 1450Sstevel@tonic-gate * type of event structures as soon as the events are delivered to the 1460Sstevel@tonic-gate * application. 1470Sstevel@tonic-gate * PORT_ALLOC_PRIVATE 1480Sstevel@tonic-gate * This type of event will be use for private use of the event source. 1490Sstevel@tonic-gate * The port_get(n) function will deliver events of such an structure to 1500Sstevel@tonic-gate * the application but it will not free the event structure itself. 1510Sstevel@tonic-gate * The event source must free this structure using port_free_event(). 1520Sstevel@tonic-gate * PORT_ALLOC_CACHED 1530Sstevel@tonic-gate * This type of events is used when the event source helds an own 1540Sstevel@tonic-gate * cache. 1550Sstevel@tonic-gate * The port_get(n) function will deliver events of such an structure to 1560Sstevel@tonic-gate * the application but it will not free the event structure itself. 1570Sstevel@tonic-gate * The event source must free this structure using port_free_event(). 1580Sstevel@tonic-gate */ 1590Sstevel@tonic-gate int 1600Sstevel@tonic-gate port_alloc_event(int port, int flags, int source, port_kevent_t **pkevpp) 1610Sstevel@tonic-gate { 1620Sstevel@tonic-gate port_t *pp; 1630Sstevel@tonic-gate file_t *fp; 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate if ((fp = getf(port)) == NULL) 1660Sstevel@tonic-gate return (EBADF); 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate if (fp->f_vnode->v_type != VPORT) { 1690Sstevel@tonic-gate releasef(port); 1700Sstevel@tonic-gate return (EBADFD); 1710Sstevel@tonic-gate } 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate pp = VTOEP(fp->f_vnode); 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate /* 1760Sstevel@tonic-gate * port_max_events is controlled by the resource control 1770Sstevel@tonic-gate * process.port-max-events 1780Sstevel@tonic-gate */ 1790Sstevel@tonic-gate if (pp->port_curr >= pp->port_max_events) { 1800Sstevel@tonic-gate releasef(port); 1810Sstevel@tonic-gate return (EAGAIN); 1820Sstevel@tonic-gate } 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate *pkevpp = kmem_cache_alloc(port_control.pc_cache, KM_NOSLEEP); 1850Sstevel@tonic-gate if (*pkevpp == NULL) { 1860Sstevel@tonic-gate releasef(port); 1870Sstevel@tonic-gate return (ENOMEM); 1880Sstevel@tonic-gate } 1890Sstevel@tonic-gate atomic_add_32(&pp->port_curr, 1); 190*1425Spraks mutex_init(&(*pkevpp)->portkev_lock, NULL, MUTEX_DEFAULT, NULL); 1910Sstevel@tonic-gate (*pkevpp)->portkev_source = source; 1920Sstevel@tonic-gate (*pkevpp)->portkev_flags = flags; 1930Sstevel@tonic-gate (*pkevpp)->portkev_pid = curproc->p_pid; 1940Sstevel@tonic-gate (*pkevpp)->portkev_port = pp; 1950Sstevel@tonic-gate (*pkevpp)->portkev_callback = NULL; 1960Sstevel@tonic-gate releasef(port); 1970Sstevel@tonic-gate return (0); 1980Sstevel@tonic-gate } 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate /* 2010Sstevel@tonic-gate * This function is faster than the standard port_alloc_event() and 2020Sstevel@tonic-gate * can be used when the event source already allocated an event from 2030Sstevel@tonic-gate * a port. 2040Sstevel@tonic-gate */ 2050Sstevel@tonic-gate int 2060Sstevel@tonic-gate port_dup_event(port_kevent_t *pkevp, port_kevent_t **pkevdupp, int flags) 2070Sstevel@tonic-gate { 2080Sstevel@tonic-gate int error; 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate error = port_alloc_event_local(pkevp->portkev_port, 2110Sstevel@tonic-gate pkevp->portkev_source, flags, pkevdupp); 2120Sstevel@tonic-gate if (error == 0) 2130Sstevel@tonic-gate (*pkevdupp)->portkev_pid = pkevp->portkev_pid; 2140Sstevel@tonic-gate return (error); 2150Sstevel@tonic-gate } 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate /* 2180Sstevel@tonic-gate * port_alloc_event_local() is reserved for internal use only. 2190Sstevel@tonic-gate * It is doing the same job as port_alloc_event() but with the event port 2200Sstevel@tonic-gate * pointer as the first argument. 2210Sstevel@tonic-gate * The check of the validity of the port file descriptor is skipped here. 2220Sstevel@tonic-gate */ 2230Sstevel@tonic-gate int 2240Sstevel@tonic-gate port_alloc_event_local(port_t *pp, int source, int flags, 2250Sstevel@tonic-gate port_kevent_t **pkevpp) 2260Sstevel@tonic-gate { 2270Sstevel@tonic-gate if (pp->port_curr >= pp->port_max_events) 2280Sstevel@tonic-gate return (EAGAIN); 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate *pkevpp = kmem_cache_alloc(port_control.pc_cache, KM_NOSLEEP); 2310Sstevel@tonic-gate if (*pkevpp == NULL) 2320Sstevel@tonic-gate return (ENOMEM); 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate atomic_add_32(&pp->port_curr, 1); 235*1425Spraks mutex_init(&(*pkevpp)->portkev_lock, NULL, MUTEX_DEFAULT, NULL); 2360Sstevel@tonic-gate (*pkevpp)->portkev_flags = flags; 2370Sstevel@tonic-gate (*pkevpp)->portkev_port = pp; 2380Sstevel@tonic-gate (*pkevpp)->portkev_source = source; 2390Sstevel@tonic-gate (*pkevpp)->portkev_pid = curproc->p_pid; 2400Sstevel@tonic-gate return (0); 2410Sstevel@tonic-gate } 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate /* 2440Sstevel@tonic-gate * port_alloc_event_block() has the same functionality of port_alloc_event() + 2450Sstevel@tonic-gate * - it blocks if not enough event slots are available and 2460Sstevel@tonic-gate * - it blocks if not enough memory is available. 2470Sstevel@tonic-gate * Currently port_dispatch() is using this function to increase the 2480Sstevel@tonic-gate * reliability of event delivery for library event sources. 2490Sstevel@tonic-gate */ 2500Sstevel@tonic-gate int 2510Sstevel@tonic-gate port_alloc_event_block(port_t *pp, int source, int flags, 2520Sstevel@tonic-gate port_kevent_t **pkevp) 2530Sstevel@tonic-gate { 2540Sstevel@tonic-gate int rval; 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate if (pp->port_curr >= pp->port_max_events) { 2570Sstevel@tonic-gate mutex_enter(&pp->port_mutex); 2580Sstevel@tonic-gate pp->port_flags |= PORT_EVENTS; 2590Sstevel@tonic-gate while (pp->port_curr >= pp->port_max_events) { 2600Sstevel@tonic-gate rval = cv_wait_sig(&pp->port_cv, &pp->port_mutex); 2610Sstevel@tonic-gate if (rval == 0) { 2620Sstevel@tonic-gate /* signal detected */ 2630Sstevel@tonic-gate mutex_exit(&pp->port_mutex); 2640Sstevel@tonic-gate return (EINTR); 2650Sstevel@tonic-gate } 2660Sstevel@tonic-gate } 2670Sstevel@tonic-gate mutex_exit(&pp->port_mutex); 2680Sstevel@tonic-gate } 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate *pkevp = kmem_cache_alloc(port_control.pc_cache, KM_SLEEP); 2710Sstevel@tonic-gate atomic_add_32(&pp->port_curr, 1); 272*1425Spraks mutex_init(&(*pkevp)->portkev_lock, NULL, MUTEX_DEFAULT, NULL); 2730Sstevel@tonic-gate (*pkevp)->portkev_flags = flags; 2740Sstevel@tonic-gate (*pkevp)->portkev_port = pp; 2750Sstevel@tonic-gate (*pkevp)->portkev_source = source; 2760Sstevel@tonic-gate (*pkevp)->portkev_pid = curproc->p_pid; 2770Sstevel@tonic-gate return (0); 2780Sstevel@tonic-gate } 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate /* 2810Sstevel@tonic-gate * Take an event out of the port queue 2820Sstevel@tonic-gate */ 2830Sstevel@tonic-gate static void 2840Sstevel@tonic-gate port_remove_event_doneq(port_kevent_t *pkevp, port_queue_t *portq) 2850Sstevel@tonic-gate { 2860Sstevel@tonic-gate ASSERT(MUTEX_HELD(&portq->portq_mutex)); 2870Sstevel@tonic-gate list_remove(&portq->portq_list, pkevp); 2880Sstevel@tonic-gate portq->portq_nent--; 2890Sstevel@tonic-gate pkevp->portkev_flags &= ~PORT_KEV_DONEQ; 2900Sstevel@tonic-gate } 2910Sstevel@tonic-gate 2920Sstevel@tonic-gate /* 2930Sstevel@tonic-gate * The port_remove_done_event() function takes a fired event out of the 2940Sstevel@tonic-gate * port queue. 2950Sstevel@tonic-gate * Currently this function is required to cancel a fired event because 2960Sstevel@tonic-gate * the application is delivering new association data (see port_associate_fd()). 2970Sstevel@tonic-gate */ 2980Sstevel@tonic-gate void 2990Sstevel@tonic-gate port_remove_done_event(port_kevent_t *pkevp) 3000Sstevel@tonic-gate { 3010Sstevel@tonic-gate port_queue_t *portq; 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate portq = &pkevp->portkev_port->port_queue; 3040Sstevel@tonic-gate mutex_enter(&portq->portq_mutex); 3050Sstevel@tonic-gate /* wait for port_get() or port_getn() */ 3060Sstevel@tonic-gate mutex_enter(&portq->portq_block_mutex); 3070Sstevel@tonic-gate if (pkevp->portkev_flags & PORT_KEV_DONEQ) { 3080Sstevel@tonic-gate /* event still in port queue */ 3090Sstevel@tonic-gate if (portq->portq_getn) { 3100Sstevel@tonic-gate /* 3110Sstevel@tonic-gate * There could be still fired events in the temp queue; 3120Sstevel@tonic-gate * push those events back to the port queue and 3130Sstevel@tonic-gate * remove requested event afterwards. 3140Sstevel@tonic-gate */ 3150Sstevel@tonic-gate port_push_eventq(portq); 3160Sstevel@tonic-gate } 3170Sstevel@tonic-gate /* now remove event from the port queue */ 3180Sstevel@tonic-gate port_remove_event_doneq(pkevp, portq); 3190Sstevel@tonic-gate } 3200Sstevel@tonic-gate mutex_exit(&portq->portq_block_mutex); 3210Sstevel@tonic-gate mutex_exit(&portq->portq_mutex); 3220Sstevel@tonic-gate } 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate /* 3250Sstevel@tonic-gate * Return port event back to the kmem_cache. 3260Sstevel@tonic-gate * If the event is currently in the port queue the event itself will only 3270Sstevel@tonic-gate * be set as invalid. The port_get(n) function will not deliver such events 3280Sstevel@tonic-gate * to the application and it will return them back to the kmem_cache. 3290Sstevel@tonic-gate */ 3300Sstevel@tonic-gate void 3310Sstevel@tonic-gate port_free_event(port_kevent_t *pkevp) 3320Sstevel@tonic-gate { 3330Sstevel@tonic-gate port_queue_t *portq; 3340Sstevel@tonic-gate port_t *pp; 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate pp = pkevp->portkev_port; 3370Sstevel@tonic-gate if (pp == NULL) 3380Sstevel@tonic-gate return; 3390Sstevel@tonic-gate if (pkevp->portkev_flags & PORT_ALLOC_PRIVATE) { 3400Sstevel@tonic-gate port_free_event_local(pkevp, 0); 3410Sstevel@tonic-gate return; 3420Sstevel@tonic-gate } 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate portq = &pp->port_queue; 3450Sstevel@tonic-gate mutex_enter(&portq->portq_mutex); 3460Sstevel@tonic-gate mutex_enter(&portq->portq_block_mutex); 3470Sstevel@tonic-gate if (pkevp->portkev_flags & PORT_KEV_DONEQ) { 3480Sstevel@tonic-gate pkevp->portkev_flags |= PORT_KEV_FREE; 3490Sstevel@tonic-gate pkevp->portkev_callback = NULL; 3500Sstevel@tonic-gate mutex_exit(&portq->portq_block_mutex); 3510Sstevel@tonic-gate mutex_exit(&portq->portq_mutex); 3520Sstevel@tonic-gate return; 3530Sstevel@tonic-gate } 3540Sstevel@tonic-gate mutex_exit(&portq->portq_block_mutex); 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate if (pkevp->portkev_flags & PORT_KEV_CACHED) { 3570Sstevel@tonic-gate mutex_exit(&portq->portq_mutex); 3580Sstevel@tonic-gate return; 3590Sstevel@tonic-gate } 3600Sstevel@tonic-gate 3610Sstevel@tonic-gate atomic_add_32(&pp->port_curr, -1); 3620Sstevel@tonic-gate if (portq->portq_flags & PORTQ_CLOSE) { 3630Sstevel@tonic-gate /* 3640Sstevel@tonic-gate * Another thread is closing the event port. 3650Sstevel@tonic-gate * That thread will sleep until all allocated event 3660Sstevel@tonic-gate * structures returned to the event port framework. 3670Sstevel@tonic-gate * The portq_mutex is used to synchronize the status 3680Sstevel@tonic-gate * of the allocated event structures (port_curr). 3690Sstevel@tonic-gate */ 3700Sstevel@tonic-gate if (pp->port_curr <= portq->portq_nent) 3710Sstevel@tonic-gate cv_signal(&portq->portq_closecv); 3720Sstevel@tonic-gate } 3730Sstevel@tonic-gate mutex_exit(&portq->portq_mutex); 3740Sstevel@tonic-gate port_free_event_local(pkevp, 1); 3750Sstevel@tonic-gate } 3760Sstevel@tonic-gate 3770Sstevel@tonic-gate /* 3780Sstevel@tonic-gate * This event port internal function is used by port_free_event() and 3790Sstevel@tonic-gate * other port internal functions to return event structures back to the 3800Sstevel@tonic-gate * kmem_cache. 3810Sstevel@tonic-gate */ 3820Sstevel@tonic-gate void 3830Sstevel@tonic-gate port_free_event_local(port_kevent_t *pkevp, int counter) 3840Sstevel@tonic-gate { 3850Sstevel@tonic-gate port_t *pp = pkevp->portkev_port; 3860Sstevel@tonic-gate 3870Sstevel@tonic-gate ASSERT(pp != NULL); 3880Sstevel@tonic-gate if (counter == 0) 3890Sstevel@tonic-gate atomic_add_32(&pp->port_curr, -1); 3900Sstevel@tonic-gate pkevp->portkev_callback = NULL; 3910Sstevel@tonic-gate pkevp->portkev_flags = 0; 3920Sstevel@tonic-gate pkevp->portkev_port = NULL; 393*1425Spraks mutex_destroy(&pkevp->portkev_lock); 3940Sstevel@tonic-gate kmem_cache_free(port_control.pc_cache, pkevp); 3950Sstevel@tonic-gate 3960Sstevel@tonic-gate /* Check if blocking calls are waiting for event slots */ 3970Sstevel@tonic-gate if (pp->port_flags & PORT_EVENTS) { 3980Sstevel@tonic-gate mutex_enter(&pp->port_mutex); 3990Sstevel@tonic-gate pp->port_flags &= ~PORT_EVENTS; 4000Sstevel@tonic-gate cv_signal(&pp->port_cv); 4010Sstevel@tonic-gate mutex_exit(&pp->port_mutex); 4020Sstevel@tonic-gate } 4030Sstevel@tonic-gate 4040Sstevel@tonic-gate /* Submit a POLLOUT event if requested */ 4050Sstevel@tonic-gate if (pp->port_queue.portq_flags & PORTQ_POLLOUT) { 4060Sstevel@tonic-gate port_queue_t *portq = &pp->port_queue; 4070Sstevel@tonic-gate mutex_enter(&portq->portq_mutex); 4080Sstevel@tonic-gate portq->portq_flags &= ~PORTQ_POLLOUT; 4090Sstevel@tonic-gate mutex_exit(&portq->portq_mutex); 4100Sstevel@tonic-gate pollwakeup(&pp->port_pollhd, POLLOUT); 4110Sstevel@tonic-gate } 4120Sstevel@tonic-gate } 4130Sstevel@tonic-gate 4140Sstevel@tonic-gate /* 4150Sstevel@tonic-gate * port_init_event(port_event_t *pev, uintptr_t object, void *user, 4160Sstevel@tonic-gate * int (*port_callback)(void *, int *, pid_t, int, void *), void *sysarg); 4170Sstevel@tonic-gate * This function initializes most of the "wired" elements of the port 4180Sstevel@tonic-gate * event structure. This is normally being used just after the allocation 4190Sstevel@tonic-gate * of the port event structure. 4200Sstevel@tonic-gate * pkevp : pointer to the port event structure 4210Sstevel@tonic-gate * object : object associated with this event structure 4220Sstevel@tonic-gate * user : user defined pointer delivered with the association function 4230Sstevel@tonic-gate * port_callback: 4240Sstevel@tonic-gate * Address of the callback function which will be called 4250Sstevel@tonic-gate * - just before the event is delivered to the application. 4260Sstevel@tonic-gate * The callback function is called in user context and can be 4270Sstevel@tonic-gate * used for copyouts, e.g. 4280Sstevel@tonic-gate * - on close() or dissociation of the event. The sub-system 4290Sstevel@tonic-gate * must remove immediately every existing association of 4300Sstevel@tonic-gate * some object with this event. 4310Sstevel@tonic-gate * sysarg : event source propietary data 4320Sstevel@tonic-gate */ 4330Sstevel@tonic-gate void 4340Sstevel@tonic-gate port_init_event(port_kevent_t *pkevp, uintptr_t object, void *user, 4350Sstevel@tonic-gate int (*port_callback)(void *, int *, pid_t, int, void *), 4360Sstevel@tonic-gate void *sysarg) 4370Sstevel@tonic-gate { 4380Sstevel@tonic-gate pkevp->portkev_object = object; 4390Sstevel@tonic-gate pkevp->portkev_user = user; 4400Sstevel@tonic-gate pkevp->portkev_callback = port_callback; 4410Sstevel@tonic-gate pkevp->portkev_arg = sysarg; 4420Sstevel@tonic-gate } 4430Sstevel@tonic-gate 4440Sstevel@tonic-gate /* 4450Sstevel@tonic-gate * This routine removes a portfd_t from the fd cache's hash table. 4460Sstevel@tonic-gate */ 4470Sstevel@tonic-gate void 4480Sstevel@tonic-gate port_pcache_remove_fd(port_fdcache_t *pcp, portfd_t *pfd) 4490Sstevel@tonic-gate { 4500Sstevel@tonic-gate polldat_t *lpdp; 4510Sstevel@tonic-gate polldat_t *cpdp; 4520Sstevel@tonic-gate portfd_t **bucket; 4530Sstevel@tonic-gate polldat_t *pdp = PFTOD(pfd); 4540Sstevel@tonic-gate 4550Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pcp->pc_lock)); 4560Sstevel@tonic-gate bucket = PORT_FD_BUCKET(pcp, pdp->pd_fd); 4570Sstevel@tonic-gate cpdp = PFTOD(*bucket); 4580Sstevel@tonic-gate if (pdp == cpdp) { 4590Sstevel@tonic-gate *bucket = PDTOF(pdp->pd_hashnext); 460*1425Spraks if (--pcp->pc_fdcount == 0) { 461*1425Spraks /* 462*1425Spraks * signal the thread which may have blocked in 463*1425Spraks * port_close_sourcefd() on lastclose waiting 464*1425Spraks * for pc_fdcount to drop to 0. 465*1425Spraks */ 466*1425Spraks cv_signal(&pcp->pc_lclosecv); 467*1425Spraks } 4680Sstevel@tonic-gate kmem_free(pfd, sizeof (portfd_t)); 4690Sstevel@tonic-gate return; 4700Sstevel@tonic-gate } 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate while (cpdp != NULL) { 4730Sstevel@tonic-gate lpdp = cpdp; 4740Sstevel@tonic-gate cpdp = cpdp->pd_hashnext; 4750Sstevel@tonic-gate if (cpdp == pdp) { 4760Sstevel@tonic-gate /* polldat struct found */ 4770Sstevel@tonic-gate lpdp->pd_hashnext = pdp->pd_hashnext; 478*1425Spraks if (--pcp->pc_fdcount == 0) { 479*1425Spraks /* 480*1425Spraks * signal the thread which may have blocked in 481*1425Spraks * port_close_sourcefd() on lastclose waiting 482*1425Spraks * for pc_fdcount to drop to 0. 483*1425Spraks */ 484*1425Spraks cv_signal(&pcp->pc_lclosecv); 485*1425Spraks } 4860Sstevel@tonic-gate break; 4870Sstevel@tonic-gate } 4880Sstevel@tonic-gate } 4890Sstevel@tonic-gate ASSERT(cpdp != NULL); 4900Sstevel@tonic-gate kmem_free(pfd, sizeof (portfd_t)); 4910Sstevel@tonic-gate } 4920Sstevel@tonic-gate 4930Sstevel@tonic-gate /* 4940Sstevel@tonic-gate * The port_push_eventq() function is used to move all remaining events 4950Sstevel@tonic-gate * from the temporary queue used in port_get(n)() to the standard port 4960Sstevel@tonic-gate * queue. 4970Sstevel@tonic-gate */ 4980Sstevel@tonic-gate void 4990Sstevel@tonic-gate port_push_eventq(port_queue_t *portq) 5000Sstevel@tonic-gate { 5010Sstevel@tonic-gate /* 5020Sstevel@tonic-gate * Append temporary portq_get_list to the port queue. On return 5030Sstevel@tonic-gate * the temporary portq_get_list is empty. 5040Sstevel@tonic-gate */ 5050Sstevel@tonic-gate list_move_tail(&portq->portq_list, &portq->portq_get_list); 5060Sstevel@tonic-gate portq->portq_nent += portq->portq_tnent; 5070Sstevel@tonic-gate portq->portq_tnent = 0; 5080Sstevel@tonic-gate } 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate /* 5110Sstevel@tonic-gate * The port_remove_fd_object() function frees all resources associated with 5120Sstevel@tonic-gate * delivered portfd_t structure. 5130Sstevel@tonic-gate */ 5140Sstevel@tonic-gate void 5150Sstevel@tonic-gate port_remove_fd_object(portfd_t *pfd, port_t *pp, port_fdcache_t *pcp) 5160Sstevel@tonic-gate { 5170Sstevel@tonic-gate port_queue_t *portq; 5180Sstevel@tonic-gate polldat_t *pdp = PFTOD(pfd); 5190Sstevel@tonic-gate port_kevent_t *pkevp; 5200Sstevel@tonic-gate int error; 5210Sstevel@tonic-gate 5220Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pcp->pc_lock)); 5230Sstevel@tonic-gate if (pdp->pd_php != NULL) { 5240Sstevel@tonic-gate pollhead_delete(pdp->pd_php, pdp); 5250Sstevel@tonic-gate pdp->pd_php = NULL; 5260Sstevel@tonic-gate } 5270Sstevel@tonic-gate pkevp = pdp->pd_portev; 5280Sstevel@tonic-gate portq = &pp->port_queue; 5290Sstevel@tonic-gate mutex_enter(&portq->portq_mutex); 5300Sstevel@tonic-gate mutex_enter(&portq->portq_block_mutex); 5310Sstevel@tonic-gate if (pkevp->portkev_flags & PORT_KEV_DONEQ) { 5320Sstevel@tonic-gate if (portq->portq_getn && portq->portq_tnent) { 5330Sstevel@tonic-gate /* 5340Sstevel@tonic-gate * move events from the temporary "get" queue 5350Sstevel@tonic-gate * back to the port queue 5360Sstevel@tonic-gate */ 5370Sstevel@tonic-gate port_push_eventq(portq); 5380Sstevel@tonic-gate } 5390Sstevel@tonic-gate /* cleanup merged port queue */ 5400Sstevel@tonic-gate port_remove_event_doneq(pkevp, portq); 5410Sstevel@tonic-gate } 5420Sstevel@tonic-gate mutex_exit(&portq->portq_block_mutex); 5430Sstevel@tonic-gate mutex_exit(&portq->portq_mutex); 5440Sstevel@tonic-gate if (pkevp->portkev_callback) { 5450Sstevel@tonic-gate (void) (*pkevp->portkev_callback)(pkevp->portkev_arg, 5460Sstevel@tonic-gate &error, pkevp->portkev_pid, PORT_CALLBACK_DISSOCIATE, 5470Sstevel@tonic-gate pkevp); 5480Sstevel@tonic-gate } 5490Sstevel@tonic-gate port_free_event_local(pkevp, 0); 5500Sstevel@tonic-gate 5510Sstevel@tonic-gate /* remove polldat struct */ 5520Sstevel@tonic-gate port_pcache_remove_fd(pcp, pfd); 5530Sstevel@tonic-gate } 5540Sstevel@tonic-gate 5550Sstevel@tonic-gate /* 5560Sstevel@tonic-gate * The port_close_fd() function dissociates a file descriptor from a port 5570Sstevel@tonic-gate * and removes all allocated resources. 5580Sstevel@tonic-gate * close(2) detects in the uf_entry_t structure that the fd is associated 5590Sstevel@tonic-gate * with a port (at least one port). 5600Sstevel@tonic-gate * The fd can be associated with several ports. 5610Sstevel@tonic-gate */ 5620Sstevel@tonic-gate void 5630Sstevel@tonic-gate port_close_pfd(portfd_t *pfd) 5640Sstevel@tonic-gate { 5650Sstevel@tonic-gate port_t *pp; 5660Sstevel@tonic-gate port_fdcache_t *pcp; 5670Sstevel@tonic-gate 568*1425Spraks /* 569*1425Spraks * the portfd_t passed in should be for this proc. 570*1425Spraks */ 571*1425Spraks ASSERT(curproc->p_pid == PFTOD(pfd)->pd_portev->portkev_pid); 5720Sstevel@tonic-gate pp = PFTOD(pfd)->pd_portev->portkev_port; 5730Sstevel@tonic-gate pcp = pp->port_queue.portq_pcp; 5740Sstevel@tonic-gate mutex_enter(&pcp->pc_lock); 5750Sstevel@tonic-gate port_remove_fd_object(pfd, pp, pcp); 5760Sstevel@tonic-gate mutex_exit(&pcp->pc_lock); 5770Sstevel@tonic-gate } 5780Sstevel@tonic-gate 5790Sstevel@tonic-gate /* 5800Sstevel@tonic-gate * The port_associate_ksource() function associates an event source with a port. 5810Sstevel@tonic-gate * On port_close() all associated sources are requested to free all local 5820Sstevel@tonic-gate * resources associated with the event port. 5830Sstevel@tonic-gate * The association of a source with a port can only be done one time. Further 5840Sstevel@tonic-gate * calls of this function will only increment the reference counter. 5850Sstevel@tonic-gate * The allocated port_source_t structure is removed from the port as soon as 5860Sstevel@tonic-gate * the reference counter becomes 0. 5870Sstevel@tonic-gate */ 5880Sstevel@tonic-gate /* ARGSUSED */ 5890Sstevel@tonic-gate int 5900Sstevel@tonic-gate port_associate_ksource(int port, int source, port_source_t **portsrc, 5910Sstevel@tonic-gate void (*port_src_close)(void *, int, pid_t, int), void *arg, 5920Sstevel@tonic-gate int (*port_src_associate)(port_kevent_t *, int, int, uintptr_t, void *)) 5930Sstevel@tonic-gate { 5940Sstevel@tonic-gate port_t *pp; 5950Sstevel@tonic-gate file_t *fp; 5960Sstevel@tonic-gate port_source_t **ps; 5970Sstevel@tonic-gate port_source_t *pse; 5980Sstevel@tonic-gate 5990Sstevel@tonic-gate if ((fp = getf(port)) == NULL) 6000Sstevel@tonic-gate return (EBADF); 6010Sstevel@tonic-gate 6020Sstevel@tonic-gate if (fp->f_vnode->v_type != VPORT) { 6030Sstevel@tonic-gate releasef(port); 6040Sstevel@tonic-gate return (EBADFD); 6050Sstevel@tonic-gate } 6060Sstevel@tonic-gate pp = VTOEP(fp->f_vnode); 6070Sstevel@tonic-gate 6080Sstevel@tonic-gate mutex_enter(&pp->port_queue.portq_source_mutex); 6090Sstevel@tonic-gate ps = &pp->port_queue.portq_scache[PORT_SHASH(source)]; 6100Sstevel@tonic-gate for (pse = *ps; pse != NULL; pse = pse->portsrc_next) { 6110Sstevel@tonic-gate if (pse->portsrc_source == source) 6120Sstevel@tonic-gate break; 6130Sstevel@tonic-gate } 6140Sstevel@tonic-gate 6150Sstevel@tonic-gate if (pse == NULL) { 6160Sstevel@tonic-gate /* Create association of the event source with the port */ 6170Sstevel@tonic-gate pse = kmem_zalloc(sizeof (port_source_t), KM_NOSLEEP); 6180Sstevel@tonic-gate if (pse == NULL) { 6190Sstevel@tonic-gate mutex_exit(&pp->port_queue.portq_source_mutex); 6200Sstevel@tonic-gate releasef(port); 6210Sstevel@tonic-gate return (ENOMEM); 6220Sstevel@tonic-gate } 6230Sstevel@tonic-gate pse->portsrc_source = source; 6240Sstevel@tonic-gate pse->portsrc_close = port_src_close; 6250Sstevel@tonic-gate pse->portsrc_closearg = arg; 6260Sstevel@tonic-gate pse->portsrc_cnt = 1; 6270Sstevel@tonic-gate if (*ps) 6280Sstevel@tonic-gate pse->portsrc_next = (*ps)->portsrc_next; 6290Sstevel@tonic-gate *ps = pse; 6300Sstevel@tonic-gate } else { 6310Sstevel@tonic-gate /* entry already available, source is only requesting count */ 6320Sstevel@tonic-gate pse->portsrc_cnt++; 6330Sstevel@tonic-gate } 6340Sstevel@tonic-gate mutex_exit(&pp->port_queue.portq_source_mutex); 6350Sstevel@tonic-gate releasef(port); 6360Sstevel@tonic-gate if (portsrc) 6370Sstevel@tonic-gate *portsrc = pse; 6380Sstevel@tonic-gate return (0); 6390Sstevel@tonic-gate } 6400Sstevel@tonic-gate 6410Sstevel@tonic-gate /* 6420Sstevel@tonic-gate * The port_dissociate_ksource() function dissociates an event source from 6430Sstevel@tonic-gate * a port. 6440Sstevel@tonic-gate */ 6450Sstevel@tonic-gate int 6460Sstevel@tonic-gate port_dissociate_ksource(int port, int source, port_source_t *ps) 6470Sstevel@tonic-gate { 6480Sstevel@tonic-gate port_t *pp; 6490Sstevel@tonic-gate file_t *fp; 6500Sstevel@tonic-gate port_source_t **psh; 6510Sstevel@tonic-gate 6520Sstevel@tonic-gate if (ps == NULL) 6530Sstevel@tonic-gate return (EINVAL); 6540Sstevel@tonic-gate 6550Sstevel@tonic-gate if ((fp = getf(port)) == NULL) 6560Sstevel@tonic-gate return (EBADF); 6570Sstevel@tonic-gate 6580Sstevel@tonic-gate if (fp->f_vnode->v_type != VPORT) { 6590Sstevel@tonic-gate releasef(port); 6600Sstevel@tonic-gate return (EBADFD); 6610Sstevel@tonic-gate } 6620Sstevel@tonic-gate pp = VTOEP(fp->f_vnode); 6630Sstevel@tonic-gate 6640Sstevel@tonic-gate mutex_enter(&pp->port_queue.portq_source_mutex); 6650Sstevel@tonic-gate if (--ps->portsrc_cnt == 0) { 6660Sstevel@tonic-gate /* last association removed -> free source structure */ 6670Sstevel@tonic-gate if (ps->portsrc_prev == NULL) { 6680Sstevel@tonic-gate /* first entry */ 6690Sstevel@tonic-gate psh = &pp->port_queue.portq_scache[PORT_SHASH(source)]; 6700Sstevel@tonic-gate *psh = ps->portsrc_next; 6710Sstevel@tonic-gate if (ps->portsrc_next) 6720Sstevel@tonic-gate ps->portsrc_next->portsrc_prev = NULL; 6730Sstevel@tonic-gate } else { 6740Sstevel@tonic-gate ps->portsrc_prev->portsrc_next = ps->portsrc_next; 6750Sstevel@tonic-gate if (ps->portsrc_next) 6760Sstevel@tonic-gate ps->portsrc_next->portsrc_prev = 6770Sstevel@tonic-gate ps->portsrc_prev; 6780Sstevel@tonic-gate } 6790Sstevel@tonic-gate kmem_free(ps, sizeof (port_source_t)); 6800Sstevel@tonic-gate } 6810Sstevel@tonic-gate mutex_exit(&pp->port_queue.portq_source_mutex); 6820Sstevel@tonic-gate releasef(port); 6830Sstevel@tonic-gate return (0); 6840Sstevel@tonic-gate } 685