17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
55f684e24Ssp92102 * Common Development and Distribution License (the "License").
65f684e24Ssp92102 * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22a85084caSmeem * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
26cd1c8b85SMatthew Ahrens /*
27cd1c8b85SMatthew Ahrens * Copyright (c) 2012 by Delphix. All rights reserved.
28cd1c8b85SMatthew Ahrens */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/devops.h>
327c478bd9Sstevel@tonic-gate #include <sys/conf.h>
337c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
347c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
357c478bd9Sstevel@tonic-gate #include <sys/stat.h>
367c478bd9Sstevel@tonic-gate #include <sys/poll_impl.h>
377c478bd9Sstevel@tonic-gate #include <sys/errno.h>
387c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
397c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
407c478bd9Sstevel@tonic-gate #include <sys/debug.h>
417c478bd9Sstevel@tonic-gate #include <sys/file.h>
427c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
437c478bd9Sstevel@tonic-gate #include <sys/systm.h>
447c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
457c478bd9Sstevel@tonic-gate #include <sys/devpoll.h>
467c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
477c478bd9Sstevel@tonic-gate #include <sys/resource.h>
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate #define RESERVED 1
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate /* local data struct */
527c478bd9Sstevel@tonic-gate static dp_entry_t **devpolltbl; /* dev poll entries */
537c478bd9Sstevel@tonic-gate static size_t dptblsize;
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate static kmutex_t devpoll_lock; /* lock protecting dev tbl */
567c478bd9Sstevel@tonic-gate int devpoll_init; /* is /dev/poll initialized already */
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate /* device local functions */
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate static int dpopen(dev_t *devp, int flag, int otyp, cred_t *credp);
617c478bd9Sstevel@tonic-gate static int dpwrite(dev_t dev, struct uio *uiop, cred_t *credp);
627c478bd9Sstevel@tonic-gate static int dpioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
637c478bd9Sstevel@tonic-gate int *rvalp);
647c478bd9Sstevel@tonic-gate static int dppoll(dev_t dev, short events, int anyyet, short *reventsp,
657c478bd9Sstevel@tonic-gate struct pollhead **phpp);
667c478bd9Sstevel@tonic-gate static int dpclose(dev_t dev, int flag, int otyp, cred_t *credp);
677c478bd9Sstevel@tonic-gate static dev_info_t *dpdevi;
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate static struct cb_ops dp_cb_ops = {
717c478bd9Sstevel@tonic-gate dpopen, /* open */
727c478bd9Sstevel@tonic-gate dpclose, /* close */
737c478bd9Sstevel@tonic-gate nodev, /* strategy */
747c478bd9Sstevel@tonic-gate nodev, /* print */
757c478bd9Sstevel@tonic-gate nodev, /* dump */
767c478bd9Sstevel@tonic-gate nodev, /* read */
777c478bd9Sstevel@tonic-gate dpwrite, /* write */
787c478bd9Sstevel@tonic-gate dpioctl, /* ioctl */
797c478bd9Sstevel@tonic-gate nodev, /* devmap */
807c478bd9Sstevel@tonic-gate nodev, /* mmap */
817c478bd9Sstevel@tonic-gate nodev, /* segmap */
827c478bd9Sstevel@tonic-gate dppoll, /* poll */
83a913d554Scth ddi_prop_op, /* prop_op */
847c478bd9Sstevel@tonic-gate (struct streamtab *)0, /* streamtab */
85a913d554Scth D_MP, /* flags */
86a913d554Scth CB_REV, /* cb_ops revision */
87a913d554Scth nodev, /* aread */
88a913d554Scth nodev /* awrite */
897c478bd9Sstevel@tonic-gate };
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate static int dpattach(dev_info_t *, ddi_attach_cmd_t);
927c478bd9Sstevel@tonic-gate static int dpdetach(dev_info_t *, ddi_detach_cmd_t);
937c478bd9Sstevel@tonic-gate static int dpinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate static struct dev_ops dp_ops = {
967c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev */
977c478bd9Sstevel@tonic-gate 0, /* refcnt */
987c478bd9Sstevel@tonic-gate dpinfo, /* info */
997c478bd9Sstevel@tonic-gate nulldev, /* identify */
1007c478bd9Sstevel@tonic-gate nulldev, /* probe */
1017c478bd9Sstevel@tonic-gate dpattach, /* attach */
1027c478bd9Sstevel@tonic-gate dpdetach, /* detach */
1037c478bd9Sstevel@tonic-gate nodev, /* reset */
1047c478bd9Sstevel@tonic-gate &dp_cb_ops, /* driver operations */
1057c478bd9Sstevel@tonic-gate (struct bus_ops *)NULL, /* bus operations */
10619397407SSherry Moore nulldev, /* power */
10719397407SSherry Moore ddi_quiesce_not_needed, /* quiesce */
1087c478bd9Sstevel@tonic-gate };
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
1127c478bd9Sstevel@tonic-gate &mod_driverops, /* type of module - a driver */
113a85084caSmeem "/dev/poll driver",
1147c478bd9Sstevel@tonic-gate &dp_ops,
1157c478bd9Sstevel@tonic-gate };
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1187c478bd9Sstevel@tonic-gate MODREV_1,
1197c478bd9Sstevel@tonic-gate (void *)&modldrv,
1207c478bd9Sstevel@tonic-gate NULL
1217c478bd9Sstevel@tonic-gate };
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate * Locking Design
1257c478bd9Sstevel@tonic-gate *
1267c478bd9Sstevel@tonic-gate * The /dev/poll driver shares most of its code with poll sys call whose
1277c478bd9Sstevel@tonic-gate * code is in common/syscall/poll.c. In poll(2) design, the pollcache
1287c478bd9Sstevel@tonic-gate * structure is per lwp. An implicit assumption is made there that some
1297c478bd9Sstevel@tonic-gate * portion of pollcache will never be touched by other lwps. E.g., in
1307c478bd9Sstevel@tonic-gate * poll(2) design, no lwp will ever need to grow bitmap of other lwp.
1317c478bd9Sstevel@tonic-gate * This assumption is not true for /dev/poll; hence the need for extra
1327c478bd9Sstevel@tonic-gate * locking.
1337c478bd9Sstevel@tonic-gate *
134da6c28aaSamw * To allow more parallelism, each /dev/poll file descriptor (indexed by
1357c478bd9Sstevel@tonic-gate * minor number) has its own lock. Since read (dpioctl) is a much more
1367c478bd9Sstevel@tonic-gate * frequent operation than write, we want to allow multiple reads on same
1377c478bd9Sstevel@tonic-gate * /dev/poll fd. However, we prevent writes from being starved by giving
1387c478bd9Sstevel@tonic-gate * priority to write operation. Theoretically writes can starve reads as
139da6c28aaSamw * well. But in practical sense this is not important because (1) writes
1407c478bd9Sstevel@tonic-gate * happens less often than reads, and (2) write operation defines the
1417c478bd9Sstevel@tonic-gate * content of poll fd a cache set. If writes happens so often that they
1427c478bd9Sstevel@tonic-gate * can starve reads, that means the cached set is very unstable. It may
1437c478bd9Sstevel@tonic-gate * not make sense to read an unstable cache set anyway. Therefore, the
1447c478bd9Sstevel@tonic-gate * writers starving readers case is not handled in this design.
1457c478bd9Sstevel@tonic-gate */
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate int
_init()1487c478bd9Sstevel@tonic-gate _init()
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate int error;
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate dptblsize = DEVPOLLSIZE;
1537c478bd9Sstevel@tonic-gate devpolltbl = kmem_zalloc(sizeof (caddr_t) * dptblsize, KM_SLEEP);
1547c478bd9Sstevel@tonic-gate mutex_init(&devpoll_lock, NULL, MUTEX_DEFAULT, NULL);
1557c478bd9Sstevel@tonic-gate devpoll_init = 1;
1567c478bd9Sstevel@tonic-gate if ((error = mod_install(&modlinkage)) != 0) {
1577c478bd9Sstevel@tonic-gate mutex_destroy(&devpoll_lock);
1587c478bd9Sstevel@tonic-gate kmem_free(devpolltbl, sizeof (caddr_t) * dptblsize);
1597c478bd9Sstevel@tonic-gate devpoll_init = 0;
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate return (error);
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate int
_fini()1657c478bd9Sstevel@tonic-gate _fini()
1667c478bd9Sstevel@tonic-gate {
1677c478bd9Sstevel@tonic-gate int error;
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate if ((error = mod_remove(&modlinkage)) != 0) {
1707c478bd9Sstevel@tonic-gate return (error);
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate mutex_destroy(&devpoll_lock);
1737c478bd9Sstevel@tonic-gate kmem_free(devpolltbl, sizeof (caddr_t) * dptblsize);
1747c478bd9Sstevel@tonic-gate return (0);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1787c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1847c478bd9Sstevel@tonic-gate static int
dpattach(dev_info_t * devi,ddi_attach_cmd_t cmd)1857c478bd9Sstevel@tonic-gate dpattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(devi, "poll", S_IFCHR, 0, DDI_PSEUDO, NULL)
1887c478bd9Sstevel@tonic-gate == DDI_FAILURE) {
1897c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
1907c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate dpdevi = devi;
1937c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate static int
dpdetach(dev_info_t * devi,ddi_detach_cmd_t cmd)1977c478bd9Sstevel@tonic-gate dpdetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate if (cmd != DDI_DETACH)
2007c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
2037c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate /* ARGSUSED */
2077c478bd9Sstevel@tonic-gate static int
dpinfo(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)2087c478bd9Sstevel@tonic-gate dpinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate int error;
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate switch (infocmd) {
2137c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO:
2147c478bd9Sstevel@tonic-gate *result = (void *)dpdevi;
2157c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
2167c478bd9Sstevel@tonic-gate break;
2177c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE:
2187c478bd9Sstevel@tonic-gate *result = (void *)0;
2197c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
2207c478bd9Sstevel@tonic-gate break;
2217c478bd9Sstevel@tonic-gate default:
2227c478bd9Sstevel@tonic-gate error = DDI_FAILURE;
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate return (error);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate /*
2287c478bd9Sstevel@tonic-gate * dp_pcache_poll has similar logic to pcache_poll() in poll.c. The major
2297c478bd9Sstevel@tonic-gate * differences are: (1) /dev/poll requires scanning the bitmap starting at
2307c478bd9Sstevel@tonic-gate * where it was stopped last time, instead of always starting from 0,
2317c478bd9Sstevel@tonic-gate * (2) since user may not have cleaned up the cached fds when they are
2327c478bd9Sstevel@tonic-gate * closed, some polldats in cache may refer to closed or reused fds. We
2337c478bd9Sstevel@tonic-gate * need to check for those cases.
2347c478bd9Sstevel@tonic-gate *
2357c478bd9Sstevel@tonic-gate * NOTE: Upon closing an fd, automatic poll cache cleanup is done for
2367c478bd9Sstevel@tonic-gate * poll(2) caches but NOT for /dev/poll caches. So expect some
2377c478bd9Sstevel@tonic-gate * stale entries!
2387c478bd9Sstevel@tonic-gate */
2397c478bd9Sstevel@tonic-gate static int
dp_pcache_poll(pollfd_t * pfdp,pollcache_t * pcp,nfds_t nfds,int * fdcntp)2407c478bd9Sstevel@tonic-gate dp_pcache_poll(pollfd_t *pfdp, pollcache_t *pcp, nfds_t nfds, int *fdcntp)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate int start, ostart, end;
2437c478bd9Sstevel@tonic-gate int fdcnt, fd;
2447c478bd9Sstevel@tonic-gate boolean_t done;
2457c478bd9Sstevel@tonic-gate file_t *fp;
2467c478bd9Sstevel@tonic-gate short revent;
2477c478bd9Sstevel@tonic-gate boolean_t no_wrap;
2487c478bd9Sstevel@tonic-gate pollhead_t *php;
2497c478bd9Sstevel@tonic-gate polldat_t *pdp;
2507c478bd9Sstevel@tonic-gate int error = 0;
2517c478bd9Sstevel@tonic-gate
2527c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pcp->pc_lock));
2537c478bd9Sstevel@tonic-gate if (pcp->pc_bitmap == NULL) {
2547c478bd9Sstevel@tonic-gate /*
2557c478bd9Sstevel@tonic-gate * No Need to search because no poll fd
2567c478bd9Sstevel@tonic-gate * has been cached.
2577c478bd9Sstevel@tonic-gate */
2587c478bd9Sstevel@tonic-gate return (error);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate retry:
2617c478bd9Sstevel@tonic-gate start = ostart = pcp->pc_mapstart;
2627c478bd9Sstevel@tonic-gate end = pcp->pc_mapend;
2637c478bd9Sstevel@tonic-gate php = NULL;
2647c478bd9Sstevel@tonic-gate
2657c478bd9Sstevel@tonic-gate if (start == 0) {
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate * started from every begining, no need to wrap around.
2687c478bd9Sstevel@tonic-gate */
2697c478bd9Sstevel@tonic-gate no_wrap = B_TRUE;
2707c478bd9Sstevel@tonic-gate } else {
2717c478bd9Sstevel@tonic-gate no_wrap = B_FALSE;
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate done = B_FALSE;
2747c478bd9Sstevel@tonic-gate fdcnt = 0;
2757c478bd9Sstevel@tonic-gate while ((fdcnt < nfds) && !done) {
2767c478bd9Sstevel@tonic-gate php = NULL;
2777c478bd9Sstevel@tonic-gate revent = 0;
2787c478bd9Sstevel@tonic-gate /*
2797c478bd9Sstevel@tonic-gate * Examine the bit map in a circular fashion
2807c478bd9Sstevel@tonic-gate * to avoid starvation. Always resume from
2817c478bd9Sstevel@tonic-gate * last stop. Scan till end of the map. Then
2827c478bd9Sstevel@tonic-gate * wrap around.
2837c478bd9Sstevel@tonic-gate */
2847c478bd9Sstevel@tonic-gate fd = bt_getlowbit(pcp->pc_bitmap, start, end);
2857c478bd9Sstevel@tonic-gate ASSERT(fd <= end);
2867c478bd9Sstevel@tonic-gate if (fd >= 0) {
2877c478bd9Sstevel@tonic-gate if (fd == end) {
2887c478bd9Sstevel@tonic-gate if (no_wrap) {
2897c478bd9Sstevel@tonic-gate done = B_TRUE;
2907c478bd9Sstevel@tonic-gate } else {
2917c478bd9Sstevel@tonic-gate start = 0;
2927c478bd9Sstevel@tonic-gate end = ostart - 1;
2937c478bd9Sstevel@tonic-gate no_wrap = B_TRUE;
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate } else {
2967c478bd9Sstevel@tonic-gate start = fd + 1;
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate pdp = pcache_lookup_fd(pcp, fd);
299a85084caSmeem repoll:
3007c478bd9Sstevel@tonic-gate ASSERT(pdp != NULL);
3017c478bd9Sstevel@tonic-gate ASSERT(pdp->pd_fd == fd);
3027c478bd9Sstevel@tonic-gate if (pdp->pd_fp == NULL) {
3037c478bd9Sstevel@tonic-gate /*
3047c478bd9Sstevel@tonic-gate * The fd is POLLREMOVed. This fd is
3057c478bd9Sstevel@tonic-gate * logically no longer cached. So move
3067c478bd9Sstevel@tonic-gate * on to the next one.
3077c478bd9Sstevel@tonic-gate */
3087c478bd9Sstevel@tonic-gate continue;
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate if ((fp = getf(fd)) == NULL) {
3117c478bd9Sstevel@tonic-gate /*
3127c478bd9Sstevel@tonic-gate * The fd has been closed, but user has not
3137c478bd9Sstevel@tonic-gate * done a POLLREMOVE on this fd yet. Instead
3147c478bd9Sstevel@tonic-gate * of cleaning it here implicitly, we return
3157c478bd9Sstevel@tonic-gate * POLLNVAL. This is consistent with poll(2)
3167c478bd9Sstevel@tonic-gate * polling a closed fd. Hope this will remind
3177c478bd9Sstevel@tonic-gate * user to do a POLLREMOVE.
3187c478bd9Sstevel@tonic-gate */
3197c478bd9Sstevel@tonic-gate pfdp[fdcnt].fd = fd;
3207c478bd9Sstevel@tonic-gate pfdp[fdcnt].revents = POLLNVAL;
3217c478bd9Sstevel@tonic-gate fdcnt++;
3227c478bd9Sstevel@tonic-gate continue;
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate if (fp != pdp->pd_fp) {
3257c478bd9Sstevel@tonic-gate /*
3267c478bd9Sstevel@tonic-gate * user is polling on a cached fd which was
3277c478bd9Sstevel@tonic-gate * closed and then reused. Unfortunately
3287c478bd9Sstevel@tonic-gate * there is no good way to inform user.
3297c478bd9Sstevel@tonic-gate * If the file struct is also reused, we
3307c478bd9Sstevel@tonic-gate * may not be able to detect the fd reuse
3317c478bd9Sstevel@tonic-gate * at all. As long as this does not
3327c478bd9Sstevel@tonic-gate * cause system failure and/or memory leak,
3337c478bd9Sstevel@tonic-gate * we will play along. Man page states if
3347c478bd9Sstevel@tonic-gate * user does not clean up closed fds, polling
3357c478bd9Sstevel@tonic-gate * results will be indeterministic.
3367c478bd9Sstevel@tonic-gate *
3377c478bd9Sstevel@tonic-gate * XXX - perhaps log the detection of fd
3387c478bd9Sstevel@tonic-gate * reuse?
3397c478bd9Sstevel@tonic-gate */
3407c478bd9Sstevel@tonic-gate pdp->pd_fp = fp;
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate /*
3437c478bd9Sstevel@tonic-gate * XXX - pollrelock() logic needs to know which
3447c478bd9Sstevel@tonic-gate * which pollcache lock to grab. It'd be a
3457c478bd9Sstevel@tonic-gate * cleaner solution if we could pass pcp as
3467c478bd9Sstevel@tonic-gate * an arguement in VOP_POLL interface instead
3477c478bd9Sstevel@tonic-gate * of implicitly passing it using thread_t
3487c478bd9Sstevel@tonic-gate * struct. On the other hand, changing VOP_POLL
3497c478bd9Sstevel@tonic-gate * interface will require all driver/file system
3507c478bd9Sstevel@tonic-gate * poll routine to change. May want to revisit
3517c478bd9Sstevel@tonic-gate * the tradeoff later.
3527c478bd9Sstevel@tonic-gate */
3537c478bd9Sstevel@tonic-gate curthread->t_pollcache = pcp;
3547c478bd9Sstevel@tonic-gate error = VOP_POLL(fp->f_vnode, pdp->pd_events, 0,
355da6c28aaSamw &revent, &php, NULL);
3567c478bd9Sstevel@tonic-gate curthread->t_pollcache = NULL;
3577c478bd9Sstevel@tonic-gate releasef(fd);
3587c478bd9Sstevel@tonic-gate if (error != 0) {
3597c478bd9Sstevel@tonic-gate break;
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate /*
3627c478bd9Sstevel@tonic-gate * layered devices (e.g. console driver)
3637c478bd9Sstevel@tonic-gate * may change the vnode and thus the pollhead
3647c478bd9Sstevel@tonic-gate * pointer out from underneath us.
3657c478bd9Sstevel@tonic-gate */
3667c478bd9Sstevel@tonic-gate if (php != NULL && pdp->pd_php != NULL &&
3677c478bd9Sstevel@tonic-gate php != pdp->pd_php) {
3687c478bd9Sstevel@tonic-gate pollhead_delete(pdp->pd_php, pdp);
3697c478bd9Sstevel@tonic-gate pdp->pd_php = php;
3707c478bd9Sstevel@tonic-gate pollhead_insert(php, pdp);
3717c478bd9Sstevel@tonic-gate /*
3727c478bd9Sstevel@tonic-gate * The bit should still be set.
3737c478bd9Sstevel@tonic-gate */
3747c478bd9Sstevel@tonic-gate ASSERT(BT_TEST(pcp->pc_bitmap, fd));
3757c478bd9Sstevel@tonic-gate goto retry;
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate
3787c478bd9Sstevel@tonic-gate if (revent != 0) {
3797c478bd9Sstevel@tonic-gate pfdp[fdcnt].fd = fd;
3807c478bd9Sstevel@tonic-gate pfdp[fdcnt].events = pdp->pd_events;
3817c478bd9Sstevel@tonic-gate pfdp[fdcnt].revents = revent;
3827c478bd9Sstevel@tonic-gate fdcnt++;
3837c478bd9Sstevel@tonic-gate } else if (php != NULL) {
3847c478bd9Sstevel@tonic-gate /*
3857c478bd9Sstevel@tonic-gate * We clear a bit or cache a poll fd if
3867c478bd9Sstevel@tonic-gate * the driver returns a poll head ptr,
3877c478bd9Sstevel@tonic-gate * which is expected in the case of 0
3887c478bd9Sstevel@tonic-gate * revents. Some buggy driver may return
3897c478bd9Sstevel@tonic-gate * NULL php pointer with 0 revents. In
3907c478bd9Sstevel@tonic-gate * this case, we just treat the driver as
3917c478bd9Sstevel@tonic-gate * "noncachable" and not clearing the bit
3927c478bd9Sstevel@tonic-gate * in bitmap.
3937c478bd9Sstevel@tonic-gate */
3947c478bd9Sstevel@tonic-gate if ((pdp->pd_php != NULL) &&
3957c478bd9Sstevel@tonic-gate ((pcp->pc_flag & T_POLLWAKE) == 0)) {
3967c478bd9Sstevel@tonic-gate BT_CLEAR(pcp->pc_bitmap, fd);
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate if (pdp->pd_php == NULL) {
3997c478bd9Sstevel@tonic-gate pollhead_insert(php, pdp);
4007c478bd9Sstevel@tonic-gate pdp->pd_php = php;
401a85084caSmeem /*
402a85084caSmeem * An event of interest may have
403a85084caSmeem * arrived between the VOP_POLL() and
404a85084caSmeem * the pollhead_insert(); check again.
405a85084caSmeem */
406a85084caSmeem goto repoll;
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate } else {
4107c478bd9Sstevel@tonic-gate /*
4117c478bd9Sstevel@tonic-gate * No bit set in the range. Check for wrap around.
4127c478bd9Sstevel@tonic-gate */
4137c478bd9Sstevel@tonic-gate if (!no_wrap) {
4147c478bd9Sstevel@tonic-gate start = 0;
4157c478bd9Sstevel@tonic-gate end = ostart - 1;
4167c478bd9Sstevel@tonic-gate no_wrap = B_TRUE;
4177c478bd9Sstevel@tonic-gate } else {
4187c478bd9Sstevel@tonic-gate done = B_TRUE;
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate
4237c478bd9Sstevel@tonic-gate if (!done) {
4247c478bd9Sstevel@tonic-gate pcp->pc_mapstart = start;
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate ASSERT(*fdcntp == 0);
4277c478bd9Sstevel@tonic-gate *fdcntp = fdcnt;
4287c478bd9Sstevel@tonic-gate return (error);
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate
4317c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4327c478bd9Sstevel@tonic-gate static int
dpopen(dev_t * devp,int flag,int otyp,cred_t * credp)4337c478bd9Sstevel@tonic-gate dpopen(dev_t *devp, int flag, int otyp, cred_t *credp)
4347c478bd9Sstevel@tonic-gate {
4357c478bd9Sstevel@tonic-gate minor_t minordev;
4367c478bd9Sstevel@tonic-gate dp_entry_t *dpep;
4377c478bd9Sstevel@tonic-gate pollcache_t *pcp;
4387c478bd9Sstevel@tonic-gate
4397c478bd9Sstevel@tonic-gate ASSERT(devpoll_init);
4407c478bd9Sstevel@tonic-gate ASSERT(dptblsize <= MAXMIN);
4417c478bd9Sstevel@tonic-gate mutex_enter(&devpoll_lock);
4427c478bd9Sstevel@tonic-gate for (minordev = 0; minordev < dptblsize; minordev++) {
4437c478bd9Sstevel@tonic-gate if (devpolltbl[minordev] == NULL) {
4447c478bd9Sstevel@tonic-gate devpolltbl[minordev] = (dp_entry_t *)RESERVED;
4457c478bd9Sstevel@tonic-gate break;
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate if (minordev == dptblsize) {
4497c478bd9Sstevel@tonic-gate dp_entry_t **newtbl;
4507c478bd9Sstevel@tonic-gate size_t oldsize;
4517c478bd9Sstevel@tonic-gate
4527c478bd9Sstevel@tonic-gate /*
4537c478bd9Sstevel@tonic-gate * Used up every entry in the existing devpoll table.
4547c478bd9Sstevel@tonic-gate * Grow the table by DEVPOLLSIZE.
4557c478bd9Sstevel@tonic-gate */
4567c478bd9Sstevel@tonic-gate if ((oldsize = dptblsize) >= MAXMIN) {
4577c478bd9Sstevel@tonic-gate mutex_exit(&devpoll_lock);
4587c478bd9Sstevel@tonic-gate return (ENXIO);
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate dptblsize += DEVPOLLSIZE;
4617c478bd9Sstevel@tonic-gate if (dptblsize > MAXMIN) {
4627c478bd9Sstevel@tonic-gate dptblsize = MAXMIN;
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate newtbl = kmem_zalloc(sizeof (caddr_t) * dptblsize, KM_SLEEP);
4657c478bd9Sstevel@tonic-gate bcopy(devpolltbl, newtbl, sizeof (caddr_t) * oldsize);
4667c478bd9Sstevel@tonic-gate kmem_free(devpolltbl, sizeof (caddr_t) * oldsize);
4677c478bd9Sstevel@tonic-gate devpolltbl = newtbl;
4687c478bd9Sstevel@tonic-gate devpolltbl[minordev] = (dp_entry_t *)RESERVED;
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate mutex_exit(&devpoll_lock);
4717c478bd9Sstevel@tonic-gate
4727c478bd9Sstevel@tonic-gate dpep = kmem_zalloc(sizeof (dp_entry_t), KM_SLEEP);
4737c478bd9Sstevel@tonic-gate /*
4747c478bd9Sstevel@tonic-gate * allocate a pollcache skeleton here. Delay allocating bitmap
4757c478bd9Sstevel@tonic-gate * structures until dpwrite() time, since we don't know the
4767c478bd9Sstevel@tonic-gate * optimal size yet.
4777c478bd9Sstevel@tonic-gate */
4787c478bd9Sstevel@tonic-gate pcp = pcache_alloc();
4797c478bd9Sstevel@tonic-gate dpep->dpe_pcache = pcp;
4807c478bd9Sstevel@tonic-gate pcp->pc_pid = curproc->p_pid;
4817c478bd9Sstevel@tonic-gate *devp = makedevice(getmajor(*devp), minordev); /* clone the driver */
4827c478bd9Sstevel@tonic-gate mutex_enter(&devpoll_lock);
4837c478bd9Sstevel@tonic-gate ASSERT(minordev < dptblsize);
4847c478bd9Sstevel@tonic-gate ASSERT(devpolltbl[minordev] == (dp_entry_t *)RESERVED);
4857c478bd9Sstevel@tonic-gate devpolltbl[minordev] = dpep;
4867c478bd9Sstevel@tonic-gate mutex_exit(&devpoll_lock);
4877c478bd9Sstevel@tonic-gate return (0);
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate
4907c478bd9Sstevel@tonic-gate /*
4917c478bd9Sstevel@tonic-gate * Write to dev/poll add/remove fd's to/from a cached poll fd set,
4927c478bd9Sstevel@tonic-gate * or change poll events for a watched fd.
4937c478bd9Sstevel@tonic-gate */
4947c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4957c478bd9Sstevel@tonic-gate static int
dpwrite(dev_t dev,struct uio * uiop,cred_t * credp)4967c478bd9Sstevel@tonic-gate dpwrite(dev_t dev, struct uio *uiop, cred_t *credp)
4977c478bd9Sstevel@tonic-gate {
4987c478bd9Sstevel@tonic-gate minor_t minor;
4997c478bd9Sstevel@tonic-gate dp_entry_t *dpep;
5007c478bd9Sstevel@tonic-gate pollcache_t *pcp;
5017c478bd9Sstevel@tonic-gate pollfd_t *pollfdp, *pfdp;
5027c478bd9Sstevel@tonic-gate int error;
5037c478bd9Sstevel@tonic-gate ssize_t uiosize;
5047c478bd9Sstevel@tonic-gate nfds_t pollfdnum;
5057c478bd9Sstevel@tonic-gate struct pollhead *php = NULL;
5067c478bd9Sstevel@tonic-gate polldat_t *pdp;
5077c478bd9Sstevel@tonic-gate int fd;
5087c478bd9Sstevel@tonic-gate file_t *fp;
5097c478bd9Sstevel@tonic-gate
5107c478bd9Sstevel@tonic-gate minor = getminor(dev);
5117c478bd9Sstevel@tonic-gate
5127c478bd9Sstevel@tonic-gate mutex_enter(&devpoll_lock);
5137c478bd9Sstevel@tonic-gate ASSERT(minor < dptblsize);
5147c478bd9Sstevel@tonic-gate dpep = devpolltbl[minor];
5157c478bd9Sstevel@tonic-gate ASSERT(dpep != NULL);
5167c478bd9Sstevel@tonic-gate mutex_exit(&devpoll_lock);
5177c478bd9Sstevel@tonic-gate pcp = dpep->dpe_pcache;
5187c478bd9Sstevel@tonic-gate if (curproc->p_pid != pcp->pc_pid) {
5197c478bd9Sstevel@tonic-gate return (EACCES);
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate uiosize = uiop->uio_resid;
5227c478bd9Sstevel@tonic-gate pollfdnum = uiosize / sizeof (pollfd_t);
5237c478bd9Sstevel@tonic-gate mutex_enter(&curproc->p_lock);
5247c478bd9Sstevel@tonic-gate if (pollfdnum > (uint_t)rctl_enforced_value(
5257c478bd9Sstevel@tonic-gate rctlproc_legacy[RLIMIT_NOFILE], curproc->p_rctls, curproc)) {
5267c478bd9Sstevel@tonic-gate (void) rctl_action(rctlproc_legacy[RLIMIT_NOFILE],
5277c478bd9Sstevel@tonic-gate curproc->p_rctls, curproc, RCA_SAFE);
5287c478bd9Sstevel@tonic-gate mutex_exit(&curproc->p_lock);
5297c478bd9Sstevel@tonic-gate return (set_errno(EINVAL));
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate mutex_exit(&curproc->p_lock);
5327c478bd9Sstevel@tonic-gate /*
5337c478bd9Sstevel@tonic-gate * Copy in the pollfd array. Walk through the array and add
5347c478bd9Sstevel@tonic-gate * each polled fd to the cached set.
5357c478bd9Sstevel@tonic-gate */
5367c478bd9Sstevel@tonic-gate pollfdp = kmem_alloc(uiosize, KM_SLEEP);
5377c478bd9Sstevel@tonic-gate
5387c478bd9Sstevel@tonic-gate /*
5397c478bd9Sstevel@tonic-gate * Although /dev/poll uses the write(2) interface to cache fds, it's
5407c478bd9Sstevel@tonic-gate * not supposed to function as a seekable device. To prevent offset
5417c478bd9Sstevel@tonic-gate * from growing and eventually exceed the maximum, reset the offset
5427c478bd9Sstevel@tonic-gate * here for every call.
5437c478bd9Sstevel@tonic-gate */
5447c478bd9Sstevel@tonic-gate uiop->uio_loffset = 0;
5457c478bd9Sstevel@tonic-gate if ((error = uiomove((caddr_t)pollfdp, uiosize, UIO_WRITE, uiop))
5467c478bd9Sstevel@tonic-gate != 0) {
5477c478bd9Sstevel@tonic-gate kmem_free(pollfdp, uiosize);
5487c478bd9Sstevel@tonic-gate return (error);
5497c478bd9Sstevel@tonic-gate }
5507c478bd9Sstevel@tonic-gate /*
5517c478bd9Sstevel@tonic-gate * We are about to enter the core portion of dpwrite(). Make sure this
5527c478bd9Sstevel@tonic-gate * write has exclusive access in this portion of the code, i.e., no
5537c478bd9Sstevel@tonic-gate * other writers in this code and no other readers in dpioctl.
5547c478bd9Sstevel@tonic-gate */
5557c478bd9Sstevel@tonic-gate mutex_enter(&dpep->dpe_lock);
5567c478bd9Sstevel@tonic-gate dpep->dpe_writerwait++;
5577c478bd9Sstevel@tonic-gate while (dpep->dpe_refcnt != 0) {
5587c478bd9Sstevel@tonic-gate if (!cv_wait_sig_swap(&dpep->dpe_cv, &dpep->dpe_lock)) {
5597c478bd9Sstevel@tonic-gate dpep->dpe_writerwait--;
5607c478bd9Sstevel@tonic-gate mutex_exit(&dpep->dpe_lock);
5617c478bd9Sstevel@tonic-gate kmem_free(pollfdp, uiosize);
5627c478bd9Sstevel@tonic-gate return (set_errno(EINTR));
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate dpep->dpe_writerwait--;
5667c478bd9Sstevel@tonic-gate dpep->dpe_flag |= DP_WRITER_PRESENT;
5677c478bd9Sstevel@tonic-gate dpep->dpe_refcnt++;
5687c478bd9Sstevel@tonic-gate mutex_exit(&dpep->dpe_lock);
5697c478bd9Sstevel@tonic-gate
5707c478bd9Sstevel@tonic-gate mutex_enter(&pcp->pc_lock);
5717c478bd9Sstevel@tonic-gate if (pcp->pc_bitmap == NULL) {
5727c478bd9Sstevel@tonic-gate pcache_create(pcp, pollfdnum);
5737c478bd9Sstevel@tonic-gate }
5747c478bd9Sstevel@tonic-gate for (pfdp = pollfdp; pfdp < pollfdp + pollfdnum; pfdp++) {
5757c478bd9Sstevel@tonic-gate fd = pfdp->fd;
5767c478bd9Sstevel@tonic-gate if ((uint_t)fd >= P_FINFO(curproc)->fi_nfiles)
5777c478bd9Sstevel@tonic-gate continue;
5787c478bd9Sstevel@tonic-gate pdp = pcache_lookup_fd(pcp, fd);
5797c478bd9Sstevel@tonic-gate if (pfdp->events != POLLREMOVE) {
5807c478bd9Sstevel@tonic-gate if (pdp == NULL) {
5817c478bd9Sstevel@tonic-gate pdp = pcache_alloc_fd(0);
5827c478bd9Sstevel@tonic-gate pdp->pd_fd = fd;
5837c478bd9Sstevel@tonic-gate pdp->pd_pcache = pcp;
5847c478bd9Sstevel@tonic-gate pcache_insert_fd(pcp, pdp, pollfdnum);
5857c478bd9Sstevel@tonic-gate }
5867c478bd9Sstevel@tonic-gate ASSERT(pdp->pd_fd == fd);
5877c478bd9Sstevel@tonic-gate ASSERT(pdp->pd_pcache == pcp);
5887c478bd9Sstevel@tonic-gate if (fd >= pcp->pc_mapsize) {
5897c478bd9Sstevel@tonic-gate mutex_exit(&pcp->pc_lock);
5907c478bd9Sstevel@tonic-gate pcache_grow_map(pcp, fd);
5917c478bd9Sstevel@tonic-gate mutex_enter(&pcp->pc_lock);
5927c478bd9Sstevel@tonic-gate }
5937c478bd9Sstevel@tonic-gate if (fd > pcp->pc_mapend) {
5947c478bd9Sstevel@tonic-gate pcp->pc_mapend = fd;
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate if ((fp = getf(fd)) == NULL) {
5977c478bd9Sstevel@tonic-gate /*
5987c478bd9Sstevel@tonic-gate * The fd is not valid. Since we can't pass
5997c478bd9Sstevel@tonic-gate * this error back in the write() call, set
6007c478bd9Sstevel@tonic-gate * the bit in bitmap to force DP_POLL ioctl
6017c478bd9Sstevel@tonic-gate * to examine it.
6027c478bd9Sstevel@tonic-gate */
6037c478bd9Sstevel@tonic-gate BT_SET(pcp->pc_bitmap, fd);
6047c478bd9Sstevel@tonic-gate pdp->pd_events |= pfdp->events;
6057c478bd9Sstevel@tonic-gate continue;
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate /*
6087c478bd9Sstevel@tonic-gate * Don't do VOP_POLL for an already cached fd with
6097c478bd9Sstevel@tonic-gate * same poll events.
6107c478bd9Sstevel@tonic-gate */
6117c478bd9Sstevel@tonic-gate if ((pdp->pd_events == pfdp->events) &&
6127c478bd9Sstevel@tonic-gate (pdp->pd_fp != NULL)) {
6137c478bd9Sstevel@tonic-gate /*
6147c478bd9Sstevel@tonic-gate * the events are already cached
6157c478bd9Sstevel@tonic-gate */
6167c478bd9Sstevel@tonic-gate releasef(fd);
6177c478bd9Sstevel@tonic-gate continue;
6187c478bd9Sstevel@tonic-gate }
6197c478bd9Sstevel@tonic-gate
6207c478bd9Sstevel@tonic-gate /*
6217c478bd9Sstevel@tonic-gate * do VOP_POLL and cache this poll fd.
6227c478bd9Sstevel@tonic-gate */
6237c478bd9Sstevel@tonic-gate /*
6247c478bd9Sstevel@tonic-gate * XXX - pollrelock() logic needs to know which
6257c478bd9Sstevel@tonic-gate * which pollcache lock to grab. It'd be a
6267c478bd9Sstevel@tonic-gate * cleaner solution if we could pass pcp as
6277c478bd9Sstevel@tonic-gate * an arguement in VOP_POLL interface instead
6287c478bd9Sstevel@tonic-gate * of implicitly passing it using thread_t
6297c478bd9Sstevel@tonic-gate * struct. On the other hand, changing VOP_POLL
6307c478bd9Sstevel@tonic-gate * interface will require all driver/file system
6317c478bd9Sstevel@tonic-gate * poll routine to change. May want to revisit
6327c478bd9Sstevel@tonic-gate * the tradeoff later.
6337c478bd9Sstevel@tonic-gate */
6347c478bd9Sstevel@tonic-gate curthread->t_pollcache = pcp;
6357c478bd9Sstevel@tonic-gate error = VOP_POLL(fp->f_vnode, pfdp->events, 0,
636da6c28aaSamw &pfdp->revents, &php, NULL);
6377c478bd9Sstevel@tonic-gate curthread->t_pollcache = NULL;
6387c478bd9Sstevel@tonic-gate /*
639a85084caSmeem * We always set the bit when this fd is cached;
640a85084caSmeem * this forces the first DP_POLL to poll this fd.
6417c478bd9Sstevel@tonic-gate * Real performance gain comes from subsequent
642a85084caSmeem * DP_POLL. We also attempt a pollhead_insert();
643a85084caSmeem * if it's not possible, we'll do it in dpioctl().
6447c478bd9Sstevel@tonic-gate */
6457c478bd9Sstevel@tonic-gate BT_SET(pcp->pc_bitmap, fd);
6467c478bd9Sstevel@tonic-gate if (error != 0) {
6477c478bd9Sstevel@tonic-gate releasef(fd);
6487c478bd9Sstevel@tonic-gate break;
6497c478bd9Sstevel@tonic-gate }
6507c478bd9Sstevel@tonic-gate pdp->pd_fp = fp;
6517c478bd9Sstevel@tonic-gate pdp->pd_events |= pfdp->events;
6527c478bd9Sstevel@tonic-gate if (php != NULL) {
6537c478bd9Sstevel@tonic-gate if (pdp->pd_php == NULL) {
6547c478bd9Sstevel@tonic-gate pollhead_insert(php, pdp);
6557c478bd9Sstevel@tonic-gate pdp->pd_php = php;
6567c478bd9Sstevel@tonic-gate } else {
6577c478bd9Sstevel@tonic-gate if (pdp->pd_php != php) {
6587c478bd9Sstevel@tonic-gate pollhead_delete(pdp->pd_php,
6597c478bd9Sstevel@tonic-gate pdp);
6607c478bd9Sstevel@tonic-gate pollhead_insert(php, pdp);
6617c478bd9Sstevel@tonic-gate pdp->pd_php = php;
6627c478bd9Sstevel@tonic-gate }
6637c478bd9Sstevel@tonic-gate }
6647c478bd9Sstevel@tonic-gate
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate releasef(fd);
6677c478bd9Sstevel@tonic-gate } else {
6687c478bd9Sstevel@tonic-gate if (pdp == NULL) {
6697c478bd9Sstevel@tonic-gate continue;
6707c478bd9Sstevel@tonic-gate }
6717c478bd9Sstevel@tonic-gate ASSERT(pdp->pd_fd == fd);
6727c478bd9Sstevel@tonic-gate pdp->pd_fp = NULL;
6737c478bd9Sstevel@tonic-gate pdp->pd_events = 0;
6747c478bd9Sstevel@tonic-gate ASSERT(pdp->pd_thread == NULL);
6757c478bd9Sstevel@tonic-gate if (pdp->pd_php != NULL) {
6767c478bd9Sstevel@tonic-gate pollhead_delete(pdp->pd_php, pdp);
6777c478bd9Sstevel@tonic-gate pdp->pd_php = NULL;
6787c478bd9Sstevel@tonic-gate }
6797c478bd9Sstevel@tonic-gate BT_CLEAR(pcp->pc_bitmap, fd);
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate }
6827c478bd9Sstevel@tonic-gate mutex_exit(&pcp->pc_lock);
6837c478bd9Sstevel@tonic-gate mutex_enter(&dpep->dpe_lock);
6847c478bd9Sstevel@tonic-gate dpep->dpe_flag &= ~DP_WRITER_PRESENT;
6857c478bd9Sstevel@tonic-gate ASSERT(dpep->dpe_refcnt == 1);
6867c478bd9Sstevel@tonic-gate dpep->dpe_refcnt--;
6877c478bd9Sstevel@tonic-gate cv_broadcast(&dpep->dpe_cv);
6887c478bd9Sstevel@tonic-gate mutex_exit(&dpep->dpe_lock);
6897c478bd9Sstevel@tonic-gate kmem_free(pollfdp, uiosize);
6907c478bd9Sstevel@tonic-gate return (error);
6917c478bd9Sstevel@tonic-gate }
6927c478bd9Sstevel@tonic-gate
6937c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6947c478bd9Sstevel@tonic-gate static int
dpioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)6957c478bd9Sstevel@tonic-gate dpioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
6967c478bd9Sstevel@tonic-gate {
6977c478bd9Sstevel@tonic-gate minor_t minor;
6987c478bd9Sstevel@tonic-gate dp_entry_t *dpep;
6997c478bd9Sstevel@tonic-gate pollcache_t *pcp;
700cd1c8b85SMatthew Ahrens hrtime_t now;
7017c478bd9Sstevel@tonic-gate int error = 0;
7027c478bd9Sstevel@tonic-gate STRUCT_DECL(dvpoll, dvpoll);
7037c478bd9Sstevel@tonic-gate
704cd1c8b85SMatthew Ahrens if (cmd == DP_POLL) {
705cd1c8b85SMatthew Ahrens /* do this now, before we sleep on DP_WRITER_PRESENT */
706cd1c8b85SMatthew Ahrens now = gethrtime();
707cd1c8b85SMatthew Ahrens }
708cd1c8b85SMatthew Ahrens
7097c478bd9Sstevel@tonic-gate minor = getminor(dev);
7107c478bd9Sstevel@tonic-gate mutex_enter(&devpoll_lock);
7117c478bd9Sstevel@tonic-gate ASSERT(minor < dptblsize);
7127c478bd9Sstevel@tonic-gate dpep = devpolltbl[minor];
7137c478bd9Sstevel@tonic-gate mutex_exit(&devpoll_lock);
7147c478bd9Sstevel@tonic-gate ASSERT(dpep != NULL);
7157c478bd9Sstevel@tonic-gate pcp = dpep->dpe_pcache;
7167c478bd9Sstevel@tonic-gate if (curproc->p_pid != pcp->pc_pid)
7177c478bd9Sstevel@tonic-gate return (EACCES);
7187c478bd9Sstevel@tonic-gate
7197c478bd9Sstevel@tonic-gate mutex_enter(&dpep->dpe_lock);
7207c478bd9Sstevel@tonic-gate while ((dpep->dpe_flag & DP_WRITER_PRESENT) ||
7217c478bd9Sstevel@tonic-gate (dpep->dpe_writerwait != 0)) {
7227c478bd9Sstevel@tonic-gate if (!cv_wait_sig_swap(&dpep->dpe_cv, &dpep->dpe_lock)) {
7237c478bd9Sstevel@tonic-gate mutex_exit(&dpep->dpe_lock);
7247c478bd9Sstevel@tonic-gate return (EINTR);
7257c478bd9Sstevel@tonic-gate }
7267c478bd9Sstevel@tonic-gate }
7277c478bd9Sstevel@tonic-gate dpep->dpe_refcnt++;
7287c478bd9Sstevel@tonic-gate mutex_exit(&dpep->dpe_lock);
7297c478bd9Sstevel@tonic-gate
7307c478bd9Sstevel@tonic-gate switch (cmd) {
7317c478bd9Sstevel@tonic-gate case DP_POLL:
7327c478bd9Sstevel@tonic-gate {
7337c478bd9Sstevel@tonic-gate pollstate_t *ps;
7347c478bd9Sstevel@tonic-gate nfds_t nfds;
7357c478bd9Sstevel@tonic-gate int fdcnt = 0;
736cd1c8b85SMatthew Ahrens hrtime_t deadline = 0;
7377c478bd9Sstevel@tonic-gate
7387c478bd9Sstevel@tonic-gate STRUCT_INIT(dvpoll, mode);
7397c478bd9Sstevel@tonic-gate error = copyin((caddr_t)arg, STRUCT_BUF(dvpoll),
7407c478bd9Sstevel@tonic-gate STRUCT_SIZE(dvpoll));
7417c478bd9Sstevel@tonic-gate if (error) {
7427c478bd9Sstevel@tonic-gate DP_REFRELE(dpep);
7437c478bd9Sstevel@tonic-gate return (EFAULT);
7447c478bd9Sstevel@tonic-gate }
7457c478bd9Sstevel@tonic-gate
746cd1c8b85SMatthew Ahrens deadline = STRUCT_FGET(dvpoll, dp_timeout);
747cd1c8b85SMatthew Ahrens if (deadline > 0) {
7487c478bd9Sstevel@tonic-gate /*
749cd1c8b85SMatthew Ahrens * Convert the deadline from relative milliseconds
750cd1c8b85SMatthew Ahrens * to absolute nanoseconds. They must wait for at
751cd1c8b85SMatthew Ahrens * least a tick.
7527c478bd9Sstevel@tonic-gate */
753*19449258SJosef 'Jeff' Sipek deadline = MSEC2NSEC(deadline);
754cd1c8b85SMatthew Ahrens deadline = MAX(deadline, nsec_per_tick);
755cd1c8b85SMatthew Ahrens deadline += now;
7567c478bd9Sstevel@tonic-gate }
7577c478bd9Sstevel@tonic-gate
7587c478bd9Sstevel@tonic-gate if ((nfds = STRUCT_FGET(dvpoll, dp_nfds)) == 0) {
7597c478bd9Sstevel@tonic-gate /*
7607c478bd9Sstevel@tonic-gate * We are just using DP_POLL to sleep, so
7617c478bd9Sstevel@tonic-gate * we don't any of the devpoll apparatus.
7627c478bd9Sstevel@tonic-gate * Do not check for signals if we have a zero timeout.
7637c478bd9Sstevel@tonic-gate */
7647c478bd9Sstevel@tonic-gate DP_REFRELE(dpep);
765cd1c8b85SMatthew Ahrens if (deadline == 0)
7667c478bd9Sstevel@tonic-gate return (0);
7677c478bd9Sstevel@tonic-gate mutex_enter(&curthread->t_delay_lock);
768cd1c8b85SMatthew Ahrens while ((error =
769cd1c8b85SMatthew Ahrens cv_timedwait_sig_hrtime(&curthread->t_delay_cv,
770cd1c8b85SMatthew Ahrens &curthread->t_delay_lock, deadline)) > 0)
7717c478bd9Sstevel@tonic-gate continue;
7727c478bd9Sstevel@tonic-gate mutex_exit(&curthread->t_delay_lock);
773cd1c8b85SMatthew Ahrens return (error == 0 ? EINTR : 0);
7747c478bd9Sstevel@tonic-gate }
7757c478bd9Sstevel@tonic-gate
7767c478bd9Sstevel@tonic-gate /*
777fe234e7cSMatt Amdur * XXX It would be nice not to have to alloc each time, but it
778fe234e7cSMatt Amdur * requires another per thread structure hook. This can be
779fe234e7cSMatt Amdur * implemented later if data suggests that it's necessary.
7807c478bd9Sstevel@tonic-gate */
7817c478bd9Sstevel@tonic-gate if ((ps = curthread->t_pollstate) == NULL) {
7827c478bd9Sstevel@tonic-gate curthread->t_pollstate = pollstate_create();
7837c478bd9Sstevel@tonic-gate ps = curthread->t_pollstate;
7847c478bd9Sstevel@tonic-gate }
7857c478bd9Sstevel@tonic-gate if (ps->ps_dpbufsize < nfds) {
7867c478bd9Sstevel@tonic-gate struct proc *p = ttoproc(curthread);
7877c478bd9Sstevel@tonic-gate /*
7887c478bd9Sstevel@tonic-gate * The maximum size should be no large than
7897c478bd9Sstevel@tonic-gate * current maximum open file count.
7907c478bd9Sstevel@tonic-gate */
7917c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
7925f684e24Ssp92102 if (nfds > p->p_fno_ctl) {
7937c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
7947c478bd9Sstevel@tonic-gate DP_REFRELE(dpep);
7957c478bd9Sstevel@tonic-gate return (EINVAL);
7967c478bd9Sstevel@tonic-gate }
7977c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
7987c478bd9Sstevel@tonic-gate kmem_free(ps->ps_dpbuf, sizeof (pollfd_t) *
7997c478bd9Sstevel@tonic-gate ps->ps_dpbufsize);
8007c478bd9Sstevel@tonic-gate ps->ps_dpbuf = kmem_zalloc(sizeof (pollfd_t) *
8017c478bd9Sstevel@tonic-gate nfds, KM_SLEEP);
8027c478bd9Sstevel@tonic-gate ps->ps_dpbufsize = nfds;
8037c478bd9Sstevel@tonic-gate }
8047c478bd9Sstevel@tonic-gate
8057c478bd9Sstevel@tonic-gate mutex_enter(&pcp->pc_lock);
8067c478bd9Sstevel@tonic-gate for (;;) {
8077c478bd9Sstevel@tonic-gate pcp->pc_flag = 0;
8087c478bd9Sstevel@tonic-gate error = dp_pcache_poll(ps->ps_dpbuf, pcp, nfds, &fdcnt);
8097c478bd9Sstevel@tonic-gate if (fdcnt > 0 || error != 0)
8107c478bd9Sstevel@tonic-gate break;
8117c478bd9Sstevel@tonic-gate
8127c478bd9Sstevel@tonic-gate /*
8137c478bd9Sstevel@tonic-gate * A pollwake has happened since we polled cache.
8147c478bd9Sstevel@tonic-gate */
8157c478bd9Sstevel@tonic-gate if (pcp->pc_flag & T_POLLWAKE)
8167c478bd9Sstevel@tonic-gate continue;
8177c478bd9Sstevel@tonic-gate
8187c478bd9Sstevel@tonic-gate /*
819da6c28aaSamw * Sleep until we are notified, signaled, or timed out.
8207c478bd9Sstevel@tonic-gate */
821cd1c8b85SMatthew Ahrens if (deadline == 0) {
822cd1c8b85SMatthew Ahrens /* immediate timeout; do not check signals */
8237c478bd9Sstevel@tonic-gate break;
824cd1c8b85SMatthew Ahrens }
825cd1c8b85SMatthew Ahrens error = cv_timedwait_sig_hrtime(&pcp->pc_cv,
826cd1c8b85SMatthew Ahrens &pcp->pc_lock, deadline);
8277c478bd9Sstevel@tonic-gate /*
8287c478bd9Sstevel@tonic-gate * If we were awakened by a signal or timeout
8297c478bd9Sstevel@tonic-gate * then break the loop, else poll again.
8307c478bd9Sstevel@tonic-gate */
831cd1c8b85SMatthew Ahrens if (error <= 0) {
832cd1c8b85SMatthew Ahrens error = (error == 0) ? EINTR : 0;
8337c478bd9Sstevel@tonic-gate break;
834cd1c8b85SMatthew Ahrens } else {
835cd1c8b85SMatthew Ahrens error = 0;
8367c478bd9Sstevel@tonic-gate }
8377c478bd9Sstevel@tonic-gate }
8387c478bd9Sstevel@tonic-gate mutex_exit(&pcp->pc_lock);
8397c478bd9Sstevel@tonic-gate
8407c478bd9Sstevel@tonic-gate if (error == 0 && fdcnt > 0) {
8417c478bd9Sstevel@tonic-gate if (copyout(ps->ps_dpbuf, STRUCT_FGETP(dvpoll,
8427c478bd9Sstevel@tonic-gate dp_fds), sizeof (pollfd_t) * fdcnt)) {
8437c478bd9Sstevel@tonic-gate DP_REFRELE(dpep);
8447c478bd9Sstevel@tonic-gate return (EFAULT);
8457c478bd9Sstevel@tonic-gate }
8467c478bd9Sstevel@tonic-gate *rvalp = fdcnt;
8477c478bd9Sstevel@tonic-gate }
8487c478bd9Sstevel@tonic-gate break;
8497c478bd9Sstevel@tonic-gate }
8507c478bd9Sstevel@tonic-gate
8517c478bd9Sstevel@tonic-gate case DP_ISPOLLED:
8527c478bd9Sstevel@tonic-gate {
8537c478bd9Sstevel@tonic-gate pollfd_t pollfd;
8547c478bd9Sstevel@tonic-gate polldat_t *pdp;
8557c478bd9Sstevel@tonic-gate
8567c478bd9Sstevel@tonic-gate STRUCT_INIT(dvpoll, mode);
8577c478bd9Sstevel@tonic-gate error = copyin((caddr_t)arg, &pollfd, sizeof (pollfd_t));
8587c478bd9Sstevel@tonic-gate if (error) {
8597c478bd9Sstevel@tonic-gate DP_REFRELE(dpep);
8607c478bd9Sstevel@tonic-gate return (EFAULT);
8617c478bd9Sstevel@tonic-gate }
8627c478bd9Sstevel@tonic-gate mutex_enter(&pcp->pc_lock);
8637c478bd9Sstevel@tonic-gate if (pcp->pc_hash == NULL) {
8647c478bd9Sstevel@tonic-gate /*
8657c478bd9Sstevel@tonic-gate * No Need to search because no poll fd
8667c478bd9Sstevel@tonic-gate * has been cached.
8677c478bd9Sstevel@tonic-gate */
8687c478bd9Sstevel@tonic-gate mutex_exit(&pcp->pc_lock);
8697c478bd9Sstevel@tonic-gate DP_REFRELE(dpep);
8707c478bd9Sstevel@tonic-gate return (0);
8717c478bd9Sstevel@tonic-gate }
8727c478bd9Sstevel@tonic-gate if (pollfd.fd < 0) {
8737c478bd9Sstevel@tonic-gate mutex_exit(&pcp->pc_lock);
8747c478bd9Sstevel@tonic-gate break;
8757c478bd9Sstevel@tonic-gate }
8767c478bd9Sstevel@tonic-gate pdp = pcache_lookup_fd(pcp, pollfd.fd);
8777c478bd9Sstevel@tonic-gate if ((pdp != NULL) && (pdp->pd_fd == pollfd.fd) &&
8787c478bd9Sstevel@tonic-gate (pdp->pd_fp != NULL)) {
8797c478bd9Sstevel@tonic-gate pollfd.revents = pdp->pd_events;
8807c478bd9Sstevel@tonic-gate if (copyout(&pollfd, (caddr_t)arg, sizeof (pollfd_t))) {
8817c478bd9Sstevel@tonic-gate mutex_exit(&pcp->pc_lock);
8827c478bd9Sstevel@tonic-gate DP_REFRELE(dpep);
8837c478bd9Sstevel@tonic-gate return (EFAULT);
8847c478bd9Sstevel@tonic-gate }
8857c478bd9Sstevel@tonic-gate *rvalp = 1;
8867c478bd9Sstevel@tonic-gate }
8877c478bd9Sstevel@tonic-gate mutex_exit(&pcp->pc_lock);
8887c478bd9Sstevel@tonic-gate break;
8897c478bd9Sstevel@tonic-gate }
8907c478bd9Sstevel@tonic-gate
8917c478bd9Sstevel@tonic-gate default:
8927c478bd9Sstevel@tonic-gate DP_REFRELE(dpep);
8937c478bd9Sstevel@tonic-gate return (EINVAL);
8947c478bd9Sstevel@tonic-gate }
8957c478bd9Sstevel@tonic-gate DP_REFRELE(dpep);
8967c478bd9Sstevel@tonic-gate return (error);
8977c478bd9Sstevel@tonic-gate }
8987c478bd9Sstevel@tonic-gate
8997c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9007c478bd9Sstevel@tonic-gate static int
dppoll(dev_t dev,short events,int anyyet,short * reventsp,struct pollhead ** phpp)9017c478bd9Sstevel@tonic-gate dppoll(dev_t dev, short events, int anyyet, short *reventsp,
9027c478bd9Sstevel@tonic-gate struct pollhead **phpp)
9037c478bd9Sstevel@tonic-gate {
9047c478bd9Sstevel@tonic-gate /*
9057c478bd9Sstevel@tonic-gate * Polling on a /dev/poll fd is not fully supported yet.
9067c478bd9Sstevel@tonic-gate */
9077c478bd9Sstevel@tonic-gate *reventsp = POLLERR;
9087c478bd9Sstevel@tonic-gate return (0);
9097c478bd9Sstevel@tonic-gate }
9107c478bd9Sstevel@tonic-gate
9117c478bd9Sstevel@tonic-gate /*
9127c478bd9Sstevel@tonic-gate * devpoll close should do enough clean up before the pollcache is deleted,
9137c478bd9Sstevel@tonic-gate * i.e., it should ensure no one still references the pollcache later.
9147c478bd9Sstevel@tonic-gate * There is no "permission" check in here. Any process having the last
9157c478bd9Sstevel@tonic-gate * reference of this /dev/poll fd can close.
9167c478bd9Sstevel@tonic-gate */
9177c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9187c478bd9Sstevel@tonic-gate static int
dpclose(dev_t dev,int flag,int otyp,cred_t * credp)9197c478bd9Sstevel@tonic-gate dpclose(dev_t dev, int flag, int otyp, cred_t *credp)
9207c478bd9Sstevel@tonic-gate {
9217c478bd9Sstevel@tonic-gate minor_t minor;
9227c478bd9Sstevel@tonic-gate dp_entry_t *dpep;
9237c478bd9Sstevel@tonic-gate pollcache_t *pcp;
9247c478bd9Sstevel@tonic-gate int i;
9257c478bd9Sstevel@tonic-gate polldat_t **hashtbl;
9267c478bd9Sstevel@tonic-gate polldat_t *pdp;
9277c478bd9Sstevel@tonic-gate
9287c478bd9Sstevel@tonic-gate minor = getminor(dev);
9297c478bd9Sstevel@tonic-gate
9307c478bd9Sstevel@tonic-gate mutex_enter(&devpoll_lock);
9317c478bd9Sstevel@tonic-gate dpep = devpolltbl[minor];
9327c478bd9Sstevel@tonic-gate ASSERT(dpep != NULL);
9337c478bd9Sstevel@tonic-gate devpolltbl[minor] = NULL;
9347c478bd9Sstevel@tonic-gate mutex_exit(&devpoll_lock);
9357c478bd9Sstevel@tonic-gate pcp = dpep->dpe_pcache;
9367c478bd9Sstevel@tonic-gate ASSERT(pcp != NULL);
9377c478bd9Sstevel@tonic-gate /*
9387c478bd9Sstevel@tonic-gate * At this point, no other lwp can access this pollcache via the
9397c478bd9Sstevel@tonic-gate * /dev/poll fd. This pollcache is going away, so do the clean
9407c478bd9Sstevel@tonic-gate * up without the pc_lock.
9417c478bd9Sstevel@tonic-gate */
9427c478bd9Sstevel@tonic-gate hashtbl = pcp->pc_hash;
9437c478bd9Sstevel@tonic-gate for (i = 0; i < pcp->pc_hashsize; i++) {
9447c478bd9Sstevel@tonic-gate for (pdp = hashtbl[i]; pdp; pdp = pdp->pd_hashnext) {
9457c478bd9Sstevel@tonic-gate if (pdp->pd_php != NULL) {
9467c478bd9Sstevel@tonic-gate pollhead_delete(pdp->pd_php, pdp);
9477c478bd9Sstevel@tonic-gate pdp->pd_php = NULL;
9487c478bd9Sstevel@tonic-gate pdp->pd_fp = NULL;
9497c478bd9Sstevel@tonic-gate }
9507c478bd9Sstevel@tonic-gate }
9517c478bd9Sstevel@tonic-gate }
9527c478bd9Sstevel@tonic-gate /*
9537c478bd9Sstevel@tonic-gate * pollwakeup() may still interact with this pollcache. Wait until
9547c478bd9Sstevel@tonic-gate * it is done.
9557c478bd9Sstevel@tonic-gate */
9567c478bd9Sstevel@tonic-gate mutex_enter(&pcp->pc_no_exit);
9577c478bd9Sstevel@tonic-gate ASSERT(pcp->pc_busy >= 0);
9587c478bd9Sstevel@tonic-gate while (pcp->pc_busy > 0)
9597c478bd9Sstevel@tonic-gate cv_wait(&pcp->pc_busy_cv, &pcp->pc_no_exit);
9607c478bd9Sstevel@tonic-gate mutex_exit(&pcp->pc_no_exit);
9617c478bd9Sstevel@tonic-gate pcache_destroy(pcp);
9627c478bd9Sstevel@tonic-gate ASSERT(dpep->dpe_refcnt == 0);
9637c478bd9Sstevel@tonic-gate kmem_free(dpep, sizeof (dp_entry_t));
9647c478bd9Sstevel@tonic-gate return (0);
9657c478bd9Sstevel@tonic-gate }
966