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*9154SAmrita.Sadhukhan@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/vnode.h>
293898Srsb #include <sys/vfs_opreg.h>
300Sstevel@tonic-gate #include <sys/kmem.h>
310Sstevel@tonic-gate #include <fs/fs_subr.h>
320Sstevel@tonic-gate #include <sys/proc.h>
330Sstevel@tonic-gate #include <sys/kstat.h>
340Sstevel@tonic-gate #include <sys/port_impl.h>
350Sstevel@tonic-gate
360Sstevel@tonic-gate /* local functions */
375331Samw static int port_open(struct vnode **, int, cred_t *, caller_context_t *);
385331Samw static int port_close(struct vnode *, int, int, offset_t, cred_t *,
395331Samw caller_context_t *);
405331Samw static int port_getattr(struct vnode *, struct vattr *, int, cred_t *,
415331Samw caller_context_t *);
425331Samw static int port_access(struct vnode *, int, int, cred_t *, caller_context_t *);
435331Samw static int port_realvp(vnode_t *, vnode_t **, caller_context_t *);
445331Samw static int port_poll(vnode_t *, short, int, short *, struct pollhead **,
455331Samw caller_context_t *);
465331Samw static void port_inactive(struct vnode *, cred_t *, caller_context_t *);
470Sstevel@tonic-gate
480Sstevel@tonic-gate const fs_operation_def_t port_vnodeops_template[] = {
493898Srsb VOPNAME_OPEN, { .vop_open = port_open },
503898Srsb VOPNAME_CLOSE, { .vop_close = port_close },
513898Srsb VOPNAME_GETATTR, { .vop_getattr = port_getattr },
523898Srsb VOPNAME_ACCESS, { .vop_access = port_access },
533898Srsb VOPNAME_INACTIVE, { .vop_inactive = port_inactive },
543898Srsb VOPNAME_FRLOCK, { .error = fs_error },
553898Srsb VOPNAME_REALVP, { .vop_realvp = port_realvp },
563898Srsb VOPNAME_POLL, { .vop_poll = port_poll },
573898Srsb VOPNAME_PATHCONF, { .error = fs_error },
583898Srsb VOPNAME_DISPOSE, { .error = fs_error },
593898Srsb VOPNAME_GETSECATTR, { .error = fs_error },
603898Srsb VOPNAME_SHRLOCK, { .error = fs_error },
613898Srsb NULL, NULL
620Sstevel@tonic-gate };
630Sstevel@tonic-gate
640Sstevel@tonic-gate /* ARGSUSED */
650Sstevel@tonic-gate static int
port_open(struct vnode ** vpp,int flag,cred_t * cr,caller_context_t * ct)665331Samw port_open(struct vnode **vpp, int flag, cred_t *cr, caller_context_t *ct)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate return (0);
690Sstevel@tonic-gate }
700Sstevel@tonic-gate
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate * port_discard_events() scans the port event queue for events owned
730Sstevel@tonic-gate * by current proc. Non-shareable events will be discarded, all other
740Sstevel@tonic-gate * events remain in the event queue.
750Sstevel@tonic-gate */
760Sstevel@tonic-gate void
port_discard_events(port_queue_t * portq)770Sstevel@tonic-gate port_discard_events(port_queue_t *portq)
780Sstevel@tonic-gate {
790Sstevel@tonic-gate port_kevent_t *kevp;
800Sstevel@tonic-gate pid_t pid = curproc->p_pid;
810Sstevel@tonic-gate
820Sstevel@tonic-gate /*
831885Sraf * The call to port_block() is required to avoid interaction
841885Sraf * with other threads in port_get(n).
850Sstevel@tonic-gate */
860Sstevel@tonic-gate mutex_enter(&portq->portq_mutex);
871885Sraf port_block(portq);
880Sstevel@tonic-gate port_push_eventq(portq); /* empty temporary queue */
890Sstevel@tonic-gate kevp = list_head(&portq->portq_list);
900Sstevel@tonic-gate while (kevp) {
910Sstevel@tonic-gate if (kevp->portkev_pid == pid) {
920Sstevel@tonic-gate /* own event, check if it is shareable */
930Sstevel@tonic-gate if (kevp->portkev_flags & PORT_KEV_NOSHARE)
940Sstevel@tonic-gate kevp->portkev_flags |= PORT_KEV_FREE;
950Sstevel@tonic-gate }
960Sstevel@tonic-gate kevp = list_next(&portq->portq_list, kevp);
970Sstevel@tonic-gate }
981885Sraf port_unblock(portq);
990Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate * Called from port_close().
1040Sstevel@tonic-gate * Free all kernel events structures which are still in the event queue.
1050Sstevel@tonic-gate */
1060Sstevel@tonic-gate static void
port_close_events(port_queue_t * portq)1070Sstevel@tonic-gate port_close_events(port_queue_t *portq)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate port_kevent_t *pkevp;
1100Sstevel@tonic-gate int events; /* ignore events */
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate mutex_enter(&portq->portq_mutex);
1130Sstevel@tonic-gate while (pkevp = list_head(&portq->portq_list)) {
1140Sstevel@tonic-gate portq->portq_nent--;
1150Sstevel@tonic-gate list_remove(&portq->portq_list, pkevp);
1160Sstevel@tonic-gate if (pkevp->portkev_callback) {
1170Sstevel@tonic-gate (void) (*pkevp->portkev_callback)(pkevp->portkev_arg,
1180Sstevel@tonic-gate &events, pkevp->portkev_pid, PORT_CALLBACK_CLOSE,
1190Sstevel@tonic-gate pkevp);
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
1220Sstevel@tonic-gate port_free_event_local(pkevp, 0);
1230Sstevel@tonic-gate mutex_enter(&portq->portq_mutex);
1240Sstevel@tonic-gate }
1252948Spraks
1262948Spraks /*
1272948Spraks * Wait for any thread in pollwakeup(), accessing this port to
1282948Spraks * finish.
1292948Spraks */
1302948Spraks while (portq->portq_flags & PORTQ_POLLWK_PEND) {
1312948Spraks cv_wait(&portq->portq_closecv, &portq->portq_mutex);
1322948Spraks }
1330Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate * The port_close() function is called from standard close(2) when
1380Sstevel@tonic-gate * the file descriptor is of type S_IFPORT/VPORT.
1390Sstevel@tonic-gate * Port file descriptors behave like standard file descriptors. It means,
1400Sstevel@tonic-gate * the port file/vnode is only destroyed on last close.
1410Sstevel@tonic-gate * If the reference counter is > 1 then
1420Sstevel@tonic-gate * - sources associated with the port will be notified about the close,
1430Sstevel@tonic-gate * - objects associated with the port will be dissociated,
1440Sstevel@tonic-gate * - pending and delivered events will be discarded.
1450Sstevel@tonic-gate * On last close all references and caches will be removed. The vnode itself
1460Sstevel@tonic-gate * will be destroyed with VOP_RELE().
1470Sstevel@tonic-gate */
1480Sstevel@tonic-gate /* ARGSUSED */
1490Sstevel@tonic-gate static int
port_close(struct vnode * vp,int flag,int count,offset_t offset,cred_t * cr,caller_context_t * ct)1505331Samw port_close(struct vnode *vp, int flag, int count, offset_t offset, cred_t *cr,
1515331Samw caller_context_t *ct)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate port_t *pp;
1540Sstevel@tonic-gate port_queue_t *portq;
1550Sstevel@tonic-gate port_source_t *ps;
1560Sstevel@tonic-gate port_source_t *ps_next;
1570Sstevel@tonic-gate int source;
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate pp = VTOEP(vp);
1600Sstevel@tonic-gate mutex_enter(&pp->port_mutex);
1610Sstevel@tonic-gate if (pp->port_flags & PORT_CLOSED) {
1620Sstevel@tonic-gate mutex_exit(&pp->port_mutex);
1630Sstevel@tonic-gate return (0);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate mutex_exit(&pp->port_mutex);
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate portq = &pp->port_queue;
1680Sstevel@tonic-gate if (count > 1) {
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate * It is not the last close.
1710Sstevel@tonic-gate * Remove/free all event resources owned by the current proc
1720Sstevel@tonic-gate * First notify all with the port associated sources about the
1730Sstevel@tonic-gate * close(2). The last argument of the close callback function
1740Sstevel@tonic-gate * advises the source about the type of of the close.
1750Sstevel@tonic-gate * If the port was set in alert mode by the curren process then
1760Sstevel@tonic-gate * remove the alert mode.
1770Sstevel@tonic-gate */
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate /* check alert mode of the port */
1800Sstevel@tonic-gate mutex_enter(&portq->portq_mutex);
1810Sstevel@tonic-gate if ((portq->portq_flags & PORTQ_ALERT) &&
1820Sstevel@tonic-gate (portq->portq_alert.portal_pid == curproc->p_pid))
1830Sstevel@tonic-gate portq->portq_flags &= ~PORTQ_ALERT;
1840Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate /* notify all event sources about port_close() */
1870Sstevel@tonic-gate mutex_enter(&portq->portq_source_mutex);
1880Sstevel@tonic-gate for (source = 0; source < PORT_SCACHE_SIZE; source++) {
1890Sstevel@tonic-gate ps = portq->portq_scache[PORT_SHASH(source)];
1900Sstevel@tonic-gate for (; ps != NULL; ps = ps->portsrc_next) {
1910Sstevel@tonic-gate if (ps->portsrc_close != NULL)
1920Sstevel@tonic-gate (*ps->portsrc_close)
1930Sstevel@tonic-gate (ps->portsrc_closearg, pp->port_fd,
1940Sstevel@tonic-gate curproc->p_pid, 0);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate mutex_exit(&portq->portq_source_mutex);
1980Sstevel@tonic-gate port_discard_events(&pp->port_queue);
1990Sstevel@tonic-gate return (0);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate /*
2030Sstevel@tonic-gate * We are executing the last close of the port -> discard everything
2040Sstevel@tonic-gate * Make sure that all threads/processes accessing this port leave
2050Sstevel@tonic-gate * the kernel immediately.
2060Sstevel@tonic-gate */
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate mutex_enter(&portq->portq_mutex);
2090Sstevel@tonic-gate portq->portq_flags |= PORTQ_CLOSE;
2100Sstevel@tonic-gate while (portq->portq_thrcnt > 0) {
2110Sstevel@tonic-gate if (portq->portq_thread != NULL)
2120Sstevel@tonic-gate cv_signal(&portq->portq_thread->portget_cv);
2130Sstevel@tonic-gate cv_wait(&portq->portq_closecv, &portq->portq_mutex);
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate /*
2180Sstevel@tonic-gate * Send "last close" message to associated sources.
2190Sstevel@tonic-gate * - new event allocation requests are being denied since uf_file entry
2200Sstevel@tonic-gate * was set to NULL in closeandsetf().
2210Sstevel@tonic-gate * - all still allocated event structures must be returned to the
2220Sstevel@tonic-gate * port immediately:
2230Sstevel@tonic-gate * - call port_free_event(*event) or
2240Sstevel@tonic-gate * - call port_send_event(*event) to complete event operations
2250Sstevel@tonic-gate * which need activities in a dedicated process environment.
2260Sstevel@tonic-gate * The port_close() function waits until all allocated event structures
2270Sstevel@tonic-gate * are delivered back to the port.
2280Sstevel@tonic-gate */
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate mutex_enter(&portq->portq_source_mutex);
2310Sstevel@tonic-gate for (source = 0; source < PORT_SCACHE_SIZE; source++) {
2320Sstevel@tonic-gate ps = portq->portq_scache[PORT_SHASH(source)];
2330Sstevel@tonic-gate for (; ps != NULL; ps = ps_next) {
2340Sstevel@tonic-gate ps_next = ps->portsrc_next;
2350Sstevel@tonic-gate if (ps->portsrc_close != NULL)
2360Sstevel@tonic-gate (*ps->portsrc_close)(ps->portsrc_closearg,
2370Sstevel@tonic-gate pp->port_fd, curproc->p_pid, 1);
2380Sstevel@tonic-gate kmem_free(ps, sizeof (port_source_t));
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate kmem_free(portq->portq_scache,
2420Sstevel@tonic-gate PORT_SCACHE_SIZE * sizeof (port_source_t *));
2430Sstevel@tonic-gate portq->portq_scache = NULL;
2440Sstevel@tonic-gate mutex_exit(&portq->portq_source_mutex);
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate mutex_enter(&portq->portq_mutex);
2470Sstevel@tonic-gate /* Wait for outstanding events */
2480Sstevel@tonic-gate while (pp->port_curr > portq->portq_nent)
2490Sstevel@tonic-gate cv_wait(&portq->portq_closecv, &portq->portq_mutex);
2500Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate /*
2530Sstevel@tonic-gate * If PORT_SOURCE_FD objects were not associated with the port then
2540Sstevel@tonic-gate * it is necessary to free the port_fdcache structure here.
2550Sstevel@tonic-gate */
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate if (portq->portq_pcp != NULL) {
2580Sstevel@tonic-gate mutex_destroy(&portq->portq_pcp->pc_lock);
2590Sstevel@tonic-gate kmem_free(portq->portq_pcp, sizeof (port_fdcache_t));
2600Sstevel@tonic-gate portq->portq_pcp = NULL;
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate
2630Sstevel@tonic-gate /*
2640Sstevel@tonic-gate * Now all events are passed back to the port,
2650Sstevel@tonic-gate * discard remaining events in the port queue
2660Sstevel@tonic-gate */
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate port_close_events(portq);
2690Sstevel@tonic-gate return (0);
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate /*
2730Sstevel@tonic-gate * The port_poll() function is the VOP_POLL() entry of event ports.
2740Sstevel@tonic-gate * Event ports return:
2750Sstevel@tonic-gate * POLLIN : events are available in the event queue
2760Sstevel@tonic-gate * POLLOUT : event queue can still accept events
2770Sstevel@tonic-gate */
2780Sstevel@tonic-gate /*ARGSUSED*/
2790Sstevel@tonic-gate static int
port_poll(vnode_t * vp,short events,int anyyet,short * reventsp,struct pollhead ** phpp,caller_context_t * ct)2800Sstevel@tonic-gate port_poll(vnode_t *vp, short events, int anyyet, short *reventsp,
2815331Samw struct pollhead **phpp, caller_context_t *ct)
2820Sstevel@tonic-gate {
2830Sstevel@tonic-gate port_t *pp;
2840Sstevel@tonic-gate port_queue_t *portq;
2850Sstevel@tonic-gate short levents;
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate pp = VTOEP(vp);
2880Sstevel@tonic-gate portq = &pp->port_queue;
2890Sstevel@tonic-gate levents = 0;
2900Sstevel@tonic-gate mutex_enter(&portq->portq_mutex);
2910Sstevel@tonic-gate if (portq->portq_nent)
2920Sstevel@tonic-gate levents = POLLIN;
2930Sstevel@tonic-gate if (pp->port_curr < pp->port_max_events)
2940Sstevel@tonic-gate levents |= POLLOUT;
2950Sstevel@tonic-gate levents &= events;
2960Sstevel@tonic-gate *reventsp = levents;
2970Sstevel@tonic-gate if (levents == 0) {
2980Sstevel@tonic-gate if (!anyyet) {
2990Sstevel@tonic-gate *phpp = &pp->port_pollhd;
3000Sstevel@tonic-gate portq->portq_flags |=
3010Sstevel@tonic-gate events & POLLIN ? PORTQ_POLLIN : 0;
3020Sstevel@tonic-gate portq->portq_flags |=
3030Sstevel@tonic-gate events & POLLOUT ? PORTQ_POLLOUT : 0;
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate mutex_exit(&portq->portq_mutex);
3070Sstevel@tonic-gate return (0);
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate
3100Sstevel@tonic-gate
3110Sstevel@tonic-gate /* ARGSUSED */
3120Sstevel@tonic-gate static int
port_getattr(struct vnode * vp,struct vattr * vap,int flags,cred_t * cr,caller_context_t * ct)3135331Samw port_getattr(struct vnode *vp, struct vattr *vap, int flags, cred_t *cr,
3145331Samw caller_context_t *ct)
3150Sstevel@tonic-gate {
3160Sstevel@tonic-gate port_t *pp;
3170Sstevel@tonic-gate extern dev_t portdev;
3180Sstevel@tonic-gate
3190Sstevel@tonic-gate pp = VTOEP(vp);
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate vap->va_type = vp->v_type; /* vnode type (for create) */
3220Sstevel@tonic-gate vap->va_mode = 0; /* file access mode */
3230Sstevel@tonic-gate vap->va_uid = pp->port_uid; /* owner user id */
3240Sstevel@tonic-gate vap->va_gid = pp->port_gid; /* owner group id */
3250Sstevel@tonic-gate vap->va_fsid = portdev; /* file system id */
3260Sstevel@tonic-gate vap->va_nodeid = (ino64_t)0; /* node id */
3270Sstevel@tonic-gate vap->va_nlink = vp->v_count; /* number of references to file */
3280Sstevel@tonic-gate vap->va_size = (u_offset_t)pp->port_queue.portq_nent; /* file size */
3290Sstevel@tonic-gate vap->va_atime = pp->port_ctime; /* time of last access */
3300Sstevel@tonic-gate vap->va_mtime = pp->port_ctime; /* time of last modification */
3310Sstevel@tonic-gate vap->va_ctime = pp->port_ctime; /* time file ``created'' */
3320Sstevel@tonic-gate vap->va_rdev = portdev; /* device the file represents */
3330Sstevel@tonic-gate vap->va_blksize = 0; /* fundamental block size */
3340Sstevel@tonic-gate vap->va_nblocks = (fsblkcnt64_t)0; /* # of blocks allocated */
3350Sstevel@tonic-gate vap->va_seq = 0; /* sequence number */
3360Sstevel@tonic-gate
3370Sstevel@tonic-gate return (0);
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate
3400Sstevel@tonic-gate /*
3410Sstevel@tonic-gate * Destroy the port.
3420Sstevel@tonic-gate */
3430Sstevel@tonic-gate /* ARGSUSED */
3440Sstevel@tonic-gate static void
port_inactive(struct vnode * vp,cred_t * cr,caller_context_t * ct)3455331Samw port_inactive(struct vnode *vp, cred_t *cr, caller_context_t *ct)
3460Sstevel@tonic-gate {
3470Sstevel@tonic-gate port_t *pp = VTOEP(vp);
3480Sstevel@tonic-gate extern port_kstat_t port_kstat;
3490Sstevel@tonic-gate
3500Sstevel@tonic-gate mutex_enter(&port_control.pc_mutex);
3510Sstevel@tonic-gate port_control.pc_nents--;
3520Sstevel@tonic-gate curproc->p_portcnt--;
3530Sstevel@tonic-gate port_kstat.pks_ports.value.ui32--;
3540Sstevel@tonic-gate mutex_exit(&port_control.pc_mutex);
3550Sstevel@tonic-gate vn_free(vp);
3560Sstevel@tonic-gate mutex_destroy(&pp->port_mutex);
3570Sstevel@tonic-gate mutex_destroy(&pp->port_queue.portq_mutex);
3580Sstevel@tonic-gate mutex_destroy(&pp->port_queue.portq_source_mutex);
3590Sstevel@tonic-gate kmem_free(pp, sizeof (port_t));
3600Sstevel@tonic-gate }
3610Sstevel@tonic-gate
3620Sstevel@tonic-gate /* ARGSUSED */
3630Sstevel@tonic-gate static int
port_access(struct vnode * vp,int mode,int flags,cred_t * cr,caller_context_t * ct)3645331Samw port_access(struct vnode *vp, int mode, int flags, cred_t *cr,
3655331Samw caller_context_t *ct)
3660Sstevel@tonic-gate {
3670Sstevel@tonic-gate return (0);
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate
3700Sstevel@tonic-gate /* ARGSUSED */
3710Sstevel@tonic-gate static int
port_realvp(vnode_t * vp,vnode_t ** vpp,caller_context_t * ct)3725331Samw port_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct)
3730Sstevel@tonic-gate {
3740Sstevel@tonic-gate *vpp = vp;
3750Sstevel@tonic-gate return (0);
3760Sstevel@tonic-gate }
377