1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <sys/types.h> 30 #include <sys/vnode.h> 31 #include <sys/kmem.h> 32 #include <fs/fs_subr.h> 33 #include <sys/proc.h> 34 #include <sys/kstat.h> 35 #include <sys/port_impl.h> 36 37 /* local functions */ 38 static int port_open(struct vnode **, int, cred_t *); 39 static int port_close(struct vnode *, int, int, offset_t, cred_t *); 40 static int port_getattr(struct vnode *, struct vattr *, int, cred_t *); 41 static int port_access(struct vnode *, int, int, cred_t *); 42 static int port_realvp(vnode_t *, vnode_t **); 43 static int port_poll(vnode_t *, short, int, short *, struct pollhead **); 44 static void port_inactive(struct vnode *, cred_t *); 45 46 const fs_operation_def_t port_vnodeops_template[] = { 47 VOPNAME_OPEN, port_open, 48 VOPNAME_CLOSE, port_close, 49 VOPNAME_GETATTR, port_getattr, 50 VOPNAME_ACCESS, port_access, 51 VOPNAME_INACTIVE, (fs_generic_func_p) port_inactive, 52 VOPNAME_FRLOCK, fs_error, 53 VOPNAME_REALVP, port_realvp, 54 VOPNAME_POLL, (fs_generic_func_p) port_poll, 55 VOPNAME_PATHCONF, fs_error, 56 VOPNAME_DISPOSE, fs_error, 57 VOPNAME_GETSECATTR, fs_error, 58 VOPNAME_SHRLOCK, fs_error, 59 NULL, NULL 60 }; 61 62 /* ARGSUSED */ 63 static int 64 port_open(struct vnode **vpp, int flag, cred_t *cr) 65 { 66 return (0); 67 } 68 69 /* 70 * port_discard_events() scans the port event queue for events owned 71 * by current proc. Non-shareable events will be discarded, all other 72 * events remain in the event queue. 73 */ 74 void 75 port_discard_events(port_queue_t *portq) 76 { 77 port_kevent_t *kevp; 78 pid_t pid = curproc->p_pid; 79 80 /* 81 * Both mutexes are required to avoid interaction with other threads 82 * in port_get(n). 83 */ 84 mutex_enter(&portq->portq_mutex); 85 mutex_enter(&portq->portq_block_mutex); 86 port_push_eventq(portq); /* empty temporary queue */ 87 kevp = list_head(&portq->portq_list); 88 while (kevp) { 89 if (kevp->portkev_pid == pid) { 90 /* own event, check if it is shareable */ 91 if (kevp->portkev_flags & PORT_KEV_NOSHARE) 92 kevp->portkev_flags |= PORT_KEV_FREE; 93 } 94 kevp = list_next(&portq->portq_list, kevp); 95 } 96 mutex_exit(&portq->portq_block_mutex); 97 mutex_exit(&portq->portq_mutex); 98 } 99 100 /* 101 * Called from port_close(). 102 * Free all kernel events structures which are still in the event queue. 103 */ 104 static void 105 port_close_events(port_queue_t *portq) 106 { 107 port_kevent_t *pkevp; 108 int events; /* ignore events */ 109 110 mutex_enter(&portq->portq_mutex); 111 while (pkevp = list_head(&portq->portq_list)) { 112 portq->portq_nent--; 113 list_remove(&portq->portq_list, pkevp); 114 if (pkevp->portkev_callback) { 115 (void) (*pkevp->portkev_callback)(pkevp->portkev_arg, 116 &events, pkevp->portkev_pid, PORT_CALLBACK_CLOSE, 117 pkevp); 118 } 119 mutex_exit(&portq->portq_mutex); 120 port_free_event_local(pkevp, 0); 121 mutex_enter(&portq->portq_mutex); 122 } 123 mutex_exit(&portq->portq_mutex); 124 } 125 126 /* 127 * The port_close() function is called from standard close(2) when 128 * the file descriptor is of type S_IFPORT/VPORT. 129 * Port file descriptors behave like standard file descriptors. It means, 130 * the port file/vnode is only destroyed on last close. 131 * If the reference counter is > 1 then 132 * - sources associated with the port will be notified about the close, 133 * - objects associated with the port will be dissociated, 134 * - pending and delivered events will be discarded. 135 * On last close all references and caches will be removed. The vnode itself 136 * will be destroyed with VOP_RELE(). 137 */ 138 /* ARGSUSED */ 139 static int 140 port_close(struct vnode *vp, int flag, int count, offset_t offset, cred_t *cr) 141 { 142 port_t *pp; 143 port_queue_t *portq; 144 port_source_t *ps; 145 port_source_t *ps_next; 146 int source; 147 148 pp = VTOEP(vp); 149 mutex_enter(&pp->port_mutex); 150 if (pp->port_flags & PORT_CLOSED) { 151 mutex_exit(&pp->port_mutex); 152 return (0); 153 } 154 mutex_exit(&pp->port_mutex); 155 156 portq = &pp->port_queue; 157 if (count > 1) { 158 /* 159 * It is not the last close. 160 * Remove/free all event resources owned by the current proc 161 * First notify all with the port associated sources about the 162 * close(2). The last argument of the close callback function 163 * advises the source about the type of of the close. 164 * If the port was set in alert mode by the curren process then 165 * remove the alert mode. 166 */ 167 168 /* check alert mode of the port */ 169 mutex_enter(&portq->portq_mutex); 170 if ((portq->portq_flags & PORTQ_ALERT) && 171 (portq->portq_alert.portal_pid == curproc->p_pid)) 172 portq->portq_flags &= ~PORTQ_ALERT; 173 mutex_exit(&portq->portq_mutex); 174 175 /* notify all event sources about port_close() */ 176 mutex_enter(&portq->portq_source_mutex); 177 for (source = 0; source < PORT_SCACHE_SIZE; source++) { 178 ps = portq->portq_scache[PORT_SHASH(source)]; 179 for (; ps != NULL; ps = ps->portsrc_next) { 180 if (ps->portsrc_close != NULL) 181 (*ps->portsrc_close) 182 (ps->portsrc_closearg, pp->port_fd, 183 curproc->p_pid, 0); 184 } 185 } 186 mutex_exit(&portq->portq_source_mutex); 187 port_discard_events(&pp->port_queue); 188 return (0); 189 } 190 191 /* 192 * We are executing the last close of the port -> discard everything 193 * Make sure that all threads/processes accessing this port leave 194 * the kernel immediately. 195 */ 196 197 mutex_enter(&portq->portq_mutex); 198 portq->portq_flags |= PORTQ_CLOSE; 199 while (portq->portq_thrcnt > 0) { 200 if (portq->portq_thread != NULL) 201 cv_signal(&portq->portq_thread->portget_cv); 202 cv_wait(&portq->portq_closecv, &portq->portq_mutex); 203 } 204 mutex_exit(&portq->portq_mutex); 205 206 /* 207 * Send "last close" message to associated sources. 208 * - new event allocation requests are being denied since uf_file entry 209 * was set to NULL in closeandsetf(). 210 * - all still allocated event structures must be returned to the 211 * port immediately: 212 * - call port_free_event(*event) or 213 * - call port_send_event(*event) to complete event operations 214 * which need activities in a dedicated process environment. 215 * The port_close() function waits until all allocated event structures 216 * are delivered back to the port. 217 */ 218 219 mutex_enter(&portq->portq_source_mutex); 220 for (source = 0; source < PORT_SCACHE_SIZE; source++) { 221 ps = portq->portq_scache[PORT_SHASH(source)]; 222 for (; ps != NULL; ps = ps_next) { 223 ps_next = ps->portsrc_next; 224 if (ps->portsrc_close != NULL) 225 (*ps->portsrc_close)(ps->portsrc_closearg, 226 pp->port_fd, curproc->p_pid, 1); 227 kmem_free(ps, sizeof (port_source_t)); 228 } 229 } 230 kmem_free(portq->portq_scache, 231 PORT_SCACHE_SIZE * sizeof (port_source_t *)); 232 portq->portq_scache = NULL; 233 mutex_exit(&portq->portq_source_mutex); 234 235 mutex_enter(&portq->portq_mutex); 236 /* Wait for outstanding events */ 237 while (pp->port_curr > portq->portq_nent) 238 cv_wait(&portq->portq_closecv, &portq->portq_mutex); 239 mutex_exit(&portq->portq_mutex); 240 241 /* 242 * If PORT_SOURCE_FD objects were not associated with the port then 243 * it is necessary to free the port_fdcache structure here. 244 */ 245 246 if (portq->portq_pcp != NULL) { 247 mutex_destroy(&portq->portq_pcp->pc_lock); 248 kmem_free(portq->portq_pcp, sizeof (port_fdcache_t)); 249 portq->portq_pcp = NULL; 250 } 251 252 /* 253 * Now all events are passed back to the port, 254 * discard remaining events in the port queue 255 */ 256 257 port_close_events(portq); 258 return (0); 259 } 260 261 /* 262 * The port_poll() function is the VOP_POLL() entry of event ports. 263 * Event ports return: 264 * POLLIN : events are available in the event queue 265 * POLLOUT : event queue can still accept events 266 */ 267 /*ARGSUSED*/ 268 static int 269 port_poll(vnode_t *vp, short events, int anyyet, short *reventsp, 270 struct pollhead **phpp) 271 { 272 port_t *pp; 273 port_queue_t *portq; 274 short levents; 275 276 pp = VTOEP(vp); 277 portq = &pp->port_queue; 278 levents = 0; 279 mutex_enter(&portq->portq_mutex); 280 if (portq->portq_nent) 281 levents = POLLIN; 282 if (pp->port_curr < pp->port_max_events) 283 levents |= POLLOUT; 284 levents &= events; 285 *reventsp = levents; 286 if (levents == 0) { 287 if (!anyyet) { 288 *phpp = &pp->port_pollhd; 289 portq->portq_flags |= 290 events & POLLIN ? PORTQ_POLLIN : 0; 291 portq->portq_flags |= 292 events & POLLOUT ? PORTQ_POLLOUT : 0; 293 } 294 } 295 mutex_exit(&portq->portq_mutex); 296 return (0); 297 } 298 299 300 /* ARGSUSED */ 301 static int 302 port_getattr(struct vnode *vp, struct vattr *vap, int flags, cred_t *cr) 303 { 304 port_t *pp; 305 extern dev_t portdev; 306 307 pp = VTOEP(vp); 308 309 vap->va_mask = 0; /* bit-mask of attributes */ 310 vap->va_type = vp->v_type; /* vnode type (for create) */ 311 vap->va_mode = 0; /* file access mode */ 312 vap->va_uid = pp->port_uid; /* owner user id */ 313 vap->va_gid = pp->port_gid; /* owner group id */ 314 vap->va_fsid = portdev; /* file system id */ 315 vap->va_nodeid = (ino64_t)0; /* node id */ 316 vap->va_nlink = vp->v_count; /* number of references to file */ 317 vap->va_size = (u_offset_t)pp->port_queue.portq_nent; /* file size */ 318 vap->va_atime = pp->port_ctime; /* time of last access */ 319 vap->va_mtime = pp->port_ctime; /* time of last modification */ 320 vap->va_ctime = pp->port_ctime; /* time file ``created'' */ 321 vap->va_rdev = portdev; /* device the file represents */ 322 vap->va_blksize = 0; /* fundamental block size */ 323 vap->va_nblocks = (fsblkcnt64_t)0; /* # of blocks allocated */ 324 vap->va_seq = 0; /* sequence number */ 325 326 return (0); 327 } 328 329 /* 330 * Destroy the port. 331 */ 332 /* ARGSUSED */ 333 static void 334 port_inactive(struct vnode *vp, cred_t *cr) 335 { 336 port_t *pp = VTOEP(vp); 337 extern port_kstat_t port_kstat; 338 339 mutex_enter(&port_control.pc_mutex); 340 port_control.pc_nents--; 341 curproc->p_portcnt--; 342 port_kstat.pks_ports.value.ui32--; 343 mutex_exit(&port_control.pc_mutex); 344 vn_free(vp); 345 mutex_destroy(&pp->port_mutex); 346 mutex_destroy(&pp->port_queue.portq_mutex); 347 mutex_destroy(&pp->port_queue.portq_source_mutex); 348 kmem_free(pp, sizeof (port_t)); 349 } 350 351 /* ARGSUSED */ 352 static int 353 port_access(struct vnode *vp, int mode, int flags, cred_t *cr) 354 { 355 return (0); 356 } 357 358 /* ARGSUSED */ 359 static int 360 port_realvp(vnode_t *vp, vnode_t **vpp) 361 { 362 *vpp = vp; 363 return (0); 364 } 365