xref: /titanic_50/usr/src/uts/common/syscall/poll.c (revision 94e7edb1e6b194fffd0e7901c0b32e9fc836bc5b)
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
534709573Sraf  * Common Development and Distribution License (the "License").
634709573Sraf  * 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  */
2134709573Sraf 
227c478bd9Sstevel@tonic-gate /*
23*94e7edb1Slucy wang - Sun Microsystems - Beijing China  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
327c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <sys/param.h>
367c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
397c478bd9Sstevel@tonic-gate #include <sys/user.h>
407c478bd9Sstevel@tonic-gate #include <sys/systm.h>
417c478bd9Sstevel@tonic-gate #include <sys/errno.h>
427c478bd9Sstevel@tonic-gate #include <sys/time.h>
437c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
447c478bd9Sstevel@tonic-gate #include <sys/file.h>
457c478bd9Sstevel@tonic-gate #include <sys/mode.h>
467c478bd9Sstevel@tonic-gate #include <sys/proc.h>
477c478bd9Sstevel@tonic-gate #include <sys/uio.h>
487c478bd9Sstevel@tonic-gate #include <sys/poll_impl.h>
497c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
507c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
517c478bd9Sstevel@tonic-gate #include <sys/debug.h>
527c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
537c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
547c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
5511dc39ddSpraks #include <sys/port_impl.h>
567c478bd9Sstevel@tonic-gate #include <sys/schedctl.h>
57eb4462acSDavid Plauger #include <sys/cpu.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #define	NPHLOCKS	64	/* Number of locks; must be power of 2 */
607c478bd9Sstevel@tonic-gate #define	PHLOCKADDR(php)	&plocks[(((uintptr_t)(php)) >> 8) & (NPHLOCKS - 1)]
617c478bd9Sstevel@tonic-gate #define	PHLOCK(php)	PHLOCKADDR(php).pp_lock
627c478bd9Sstevel@tonic-gate #define	PH_ENTER(php)	mutex_enter(PHLOCK(php))
637c478bd9Sstevel@tonic-gate #define	PH_EXIT(php)	mutex_exit(PHLOCK(php))
647c478bd9Sstevel@tonic-gate #define	VALID_POLL_EVENTS	(POLLIN | POLLPRI | POLLOUT | POLLRDNORM \
657c478bd9Sstevel@tonic-gate 	| POLLRDBAND | POLLWRBAND | POLLHUP | POLLERR | POLLNVAL)
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * global counters to collect some stats
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate static struct {
717c478bd9Sstevel@tonic-gate 	kstat_named_t	polllistmiss;	/* failed to find a cached poll list */
727c478bd9Sstevel@tonic-gate 	kstat_named_t	pollcachehit;	/* list matched 100% w/ cached one */
737c478bd9Sstevel@tonic-gate 	kstat_named_t	pollcachephit;	/* list matched < 100% w/ cached one */
747c478bd9Sstevel@tonic-gate 	kstat_named_t	pollcachemiss;	/* every list entry is dif from cache */
757c478bd9Sstevel@tonic-gate } pollstats = {
767c478bd9Sstevel@tonic-gate 	{ "polllistmiss",	KSTAT_DATA_UINT64 },
777c478bd9Sstevel@tonic-gate 	{ "pollcachehit",	KSTAT_DATA_UINT64 },
787c478bd9Sstevel@tonic-gate 	{ "pollcachephit",	KSTAT_DATA_UINT64 },
797c478bd9Sstevel@tonic-gate 	{ "pollcachemiss",	KSTAT_DATA_UINT64 }
807c478bd9Sstevel@tonic-gate };
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate kstat_named_t *pollstats_ptr = (kstat_named_t *)&pollstats;
837c478bd9Sstevel@tonic-gate uint_t pollstats_ndata = sizeof (pollstats) / sizeof (kstat_named_t);
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate struct pplock	{
867c478bd9Sstevel@tonic-gate 	kmutex_t	pp_lock;
877c478bd9Sstevel@tonic-gate 	short		pp_flag;
887c478bd9Sstevel@tonic-gate 	kcondvar_t	pp_wait_cv;
897c478bd9Sstevel@tonic-gate 	int32_t		pp_pad;		/* to a nice round 16 bytes */
907c478bd9Sstevel@tonic-gate };
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate static struct pplock plocks[NPHLOCKS];	/* Hash array of pollhead locks */
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate #ifdef DEBUG
957c478bd9Sstevel@tonic-gate static int pollchecksanity(pollstate_t *, nfds_t);
967c478bd9Sstevel@tonic-gate static int pollcheckxref(pollstate_t *, int);
977c478bd9Sstevel@tonic-gate static void pollcheckphlist(void);
987c478bd9Sstevel@tonic-gate static int pollcheckrevents(pollstate_t *, int, int, int);
997c478bd9Sstevel@tonic-gate static void checkpolldat(pollstate_t *);
1007c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
1017c478bd9Sstevel@tonic-gate static int plist_chkdupfd(file_t *, polldat_t *, pollstate_t *, pollfd_t *, int,
1027c478bd9Sstevel@tonic-gate     int *);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * Data structure overview:
1067c478bd9Sstevel@tonic-gate  * The per-thread poll state consists of
1077c478bd9Sstevel@tonic-gate  *	one pollstate_t
1087c478bd9Sstevel@tonic-gate  *	one pollcache_t
1097c478bd9Sstevel@tonic-gate  *	one bitmap with one event bit per fd
1107c478bd9Sstevel@tonic-gate  *	a (two-dimensional) hashed array of polldat_t structures - one entry
1117c478bd9Sstevel@tonic-gate  *	per fd
1127c478bd9Sstevel@tonic-gate  *
1137c478bd9Sstevel@tonic-gate  * This conglomerate of data structures interact with
1147c478bd9Sstevel@tonic-gate  *	the pollhead which is used by VOP_POLL and pollwakeup
1157c478bd9Sstevel@tonic-gate  *	(protected by the PHLOCK, cached array of plocks), and
1167c478bd9Sstevel@tonic-gate  *	the fpollinfo list hanging off the fi_list which is used to notify
1177c478bd9Sstevel@tonic-gate  *	poll when a cached fd is closed. This is protected by uf_lock.
1187c478bd9Sstevel@tonic-gate  *
1197c478bd9Sstevel@tonic-gate  * Invariants:
1207c478bd9Sstevel@tonic-gate  *	pd_php (pollhead pointer) is set iff (if and only if) the polldat
1217c478bd9Sstevel@tonic-gate  *	is on that pollhead. This is modified atomically under pc_lock.
1227c478bd9Sstevel@tonic-gate  *
1237c478bd9Sstevel@tonic-gate  *	pd_fp (file_t pointer) is set iff the thread is on the fpollinfo
1247c478bd9Sstevel@tonic-gate  *	list for that open file.
1257c478bd9Sstevel@tonic-gate  *	This is modified atomically under pc_lock.
1267c478bd9Sstevel@tonic-gate  *
1277c478bd9Sstevel@tonic-gate  *	pd_count is the sum (over all values of i) of pd_ref[i].xf_refcnt.
1287c478bd9Sstevel@tonic-gate  *	Iff pd_ref[i].xf_refcnt >= 1 then
1297c478bd9Sstevel@tonic-gate  *		ps_pcacheset[i].pcs_pollfd[pd_ref[i].xf_position].fd == pd_fd
1307c478bd9Sstevel@tonic-gate  *	Iff pd_ref[i].xf_refcnt > 1 then
1317c478bd9Sstevel@tonic-gate  *		In ps_pcacheset[i].pcs_pollfd between index
1327c478bd9Sstevel@tonic-gate  *		pd_ref[i].xf_position] and the end of the list
1337c478bd9Sstevel@tonic-gate  *		there are xf_refcnt entries with .fd == pd_fd
1347c478bd9Sstevel@tonic-gate  *
1357c478bd9Sstevel@tonic-gate  * Locking design:
1367c478bd9Sstevel@tonic-gate  * Whenever possible the design relies on the fact that the poll cache state
1377c478bd9Sstevel@tonic-gate  * is per thread thus for both poll and exit it is self-synchronizing.
1387c478bd9Sstevel@tonic-gate  * Thus the key interactions where other threads access the state are:
1397c478bd9Sstevel@tonic-gate  *	pollwakeup (and polltime), and
1407c478bd9Sstevel@tonic-gate  *	close cleaning up the cached references to an open file
1417c478bd9Sstevel@tonic-gate  *
1427c478bd9Sstevel@tonic-gate  * The two key locks in poll proper is ps_lock and pc_lock.
1437c478bd9Sstevel@tonic-gate  *
1447c478bd9Sstevel@tonic-gate  * The ps_lock is used for synchronization between poll, (lwp_)exit and close
1457c478bd9Sstevel@tonic-gate  * to ensure that modifications to pollcacheset structure are serialized.
1467c478bd9Sstevel@tonic-gate  * This lock is held through most of poll() except where poll sleeps
1477c478bd9Sstevel@tonic-gate  * since there is little need to handle closes concurrently with the execution
1487c478bd9Sstevel@tonic-gate  * of poll.
1497c478bd9Sstevel@tonic-gate  * The pc_lock protects most of the fields in pollcache structure and polldat
1507c478bd9Sstevel@tonic-gate  * structures (which are accessed by poll, pollwakeup, and polltime)
1517c478bd9Sstevel@tonic-gate  * with the exception of fields that are only modified when only one thread
1527c478bd9Sstevel@tonic-gate  * can access this per-thread state.
1537c478bd9Sstevel@tonic-gate  * Those exceptions occur in poll when first allocating the per-thread state,
1547c478bd9Sstevel@tonic-gate  * when poll grows the number of polldat (never shrinks), and when
1557c478bd9Sstevel@tonic-gate  * exit/pollcleanup has ensured that there are no references from either
1567c478bd9Sstevel@tonic-gate  * pollheads or fpollinfo to the threads poll state.
1577c478bd9Sstevel@tonic-gate  *
1587c478bd9Sstevel@tonic-gate  * Poll(2) system call is the only path which ps_lock and pc_lock are both
1597c478bd9Sstevel@tonic-gate  * held, in that order. It needs ps_lock to synchronize with close and
1607c478bd9Sstevel@tonic-gate  * lwp_exit; and pc_lock with pollwakeup.
1617c478bd9Sstevel@tonic-gate  *
1627c478bd9Sstevel@tonic-gate  * The locking interaction between pc_lock and PHLOCK take into account
1637c478bd9Sstevel@tonic-gate  * that poll acquires these locks in the order of pc_lock and then PHLOCK
1647c478bd9Sstevel@tonic-gate  * while pollwakeup does it in the reverse order. Thus pollwakeup implements
1657c478bd9Sstevel@tonic-gate  * deadlock avoidance by dropping the locks and reacquiring them in the
1667c478bd9Sstevel@tonic-gate  * reverse order. For this to work pollwakeup needs to prevent the thread
1677c478bd9Sstevel@tonic-gate  * from exiting and freeing all of the poll related state. Thus is done
1687c478bd9Sstevel@tonic-gate  * using
1697c478bd9Sstevel@tonic-gate  *	the pc_no_exit lock
1707c478bd9Sstevel@tonic-gate  *	the pc_busy counter
1717c478bd9Sstevel@tonic-gate  *	the pc_busy_cv condition variable
1727c478bd9Sstevel@tonic-gate  *
1737c478bd9Sstevel@tonic-gate  * The locking interaction between pc_lock and uf_lock has similar
1747c478bd9Sstevel@tonic-gate  * issues. Poll holds ps_lock and/or pc_lock across calls to getf/releasef
1757c478bd9Sstevel@tonic-gate  * which acquire uf_lock. The poll cleanup in close needs to hold uf_lock
1767c478bd9Sstevel@tonic-gate  * to prevent poll or exit from doing a delfpollinfo after which the thread
1777c478bd9Sstevel@tonic-gate  * might exit. But the cleanup needs to acquire pc_lock when modifying
1787c478bd9Sstevel@tonic-gate  * the poll cache state. The solution is to use pc_busy and do the close
1797c478bd9Sstevel@tonic-gate  * cleanup in two phases:
1807c478bd9Sstevel@tonic-gate  *	First close calls pollblockexit which increments pc_busy.
1817c478bd9Sstevel@tonic-gate  *	This prevents the per-thread poll related state from being freed.
1827c478bd9Sstevel@tonic-gate  *	Then close drops uf_lock and calls pollcacheclean.
1837c478bd9Sstevel@tonic-gate  *	This routine can then acquire pc_lock and remove any references
1847c478bd9Sstevel@tonic-gate  *	to the closing fd (as well as recording that it has been closed
1857c478bd9Sstevel@tonic-gate  *	so that a POLLNVAL can be generated even if the fd is reused before
1867c478bd9Sstevel@tonic-gate  *	poll has been woken up and checked getf() again).
1877c478bd9Sstevel@tonic-gate  *
1887c478bd9Sstevel@tonic-gate  * When removing a polled fd from poll cache, the fd is always removed
1897c478bd9Sstevel@tonic-gate  * from pollhead list first and then from fpollinfo list, i.e.,
1907c478bd9Sstevel@tonic-gate  * pollhead_delete() is called before delfpollinfo().
1917c478bd9Sstevel@tonic-gate  *
1927c478bd9Sstevel@tonic-gate  *
1937c478bd9Sstevel@tonic-gate  * Locking hierarchy:
1947c478bd9Sstevel@tonic-gate  *	pc_no_exit is a leaf level lock.
1957c478bd9Sstevel@tonic-gate  *	ps_lock is held when acquiring pc_lock (except when pollwakeup
1967c478bd9Sstevel@tonic-gate  *	acquires pc_lock).
1977c478bd9Sstevel@tonic-gate  *	pc_lock might be held when acquiring PHLOCK (pollhead_insert/
1987c478bd9Sstevel@tonic-gate  *	pollhead_delete)
1997c478bd9Sstevel@tonic-gate  *	pc_lock is always held (but this is not required)
2007c478bd9Sstevel@tonic-gate  *	when acquiring PHLOCK (in polladd/pollhead_delete and pollwakeup called
2017c478bd9Sstevel@tonic-gate  *	from pcache_clean_entry).
2027c478bd9Sstevel@tonic-gate  *	pc_lock is held across addfpollinfo/delfpollinfo which acquire
2037c478bd9Sstevel@tonic-gate  *	uf_lock.
2047c478bd9Sstevel@tonic-gate  *	pc_lock is held across getf/releasef which acquire uf_lock.
2057c478bd9Sstevel@tonic-gate  *	ps_lock might be held across getf/releasef which acquire uf_lock.
2067c478bd9Sstevel@tonic-gate  *	pollwakeup tries to acquire pc_lock while holding PHLOCK
2077c478bd9Sstevel@tonic-gate  *	but drops the locks and reacquire them in reverse order to avoid
2087c478bd9Sstevel@tonic-gate  *	deadlock.
2097c478bd9Sstevel@tonic-gate  *
2107c478bd9Sstevel@tonic-gate  * Note also that there is deadlock avoidance support for VOP_POLL routines
2117c478bd9Sstevel@tonic-gate  * and pollwakeup involving a file system or driver lock.
2127c478bd9Sstevel@tonic-gate  * See below.
2137c478bd9Sstevel@tonic-gate  */
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate  * Deadlock avoidance support for VOP_POLL() routines.  This is
2177c478bd9Sstevel@tonic-gate  * sometimes necessary to prevent deadlock between polling threads
2187c478bd9Sstevel@tonic-gate  * (which hold poll locks on entry to xx_poll(), then acquire foo)
2197c478bd9Sstevel@tonic-gate  * and pollwakeup() threads (which hold foo, then acquire poll locks).
2207c478bd9Sstevel@tonic-gate  *
2217c478bd9Sstevel@tonic-gate  * pollunlock(void) releases whatever poll locks the current thread holds,
2227c478bd9Sstevel@tonic-gate  *	returning a cookie for use by pollrelock();
2237c478bd9Sstevel@tonic-gate  *
2247c478bd9Sstevel@tonic-gate  * pollrelock(cookie) reacquires previously dropped poll locks;
2257c478bd9Sstevel@tonic-gate  *
2267c478bd9Sstevel@tonic-gate  * polllock(php, mutex) does the common case: pollunlock(),
2277c478bd9Sstevel@tonic-gate  *	acquire the problematic mutex, pollrelock().
2287c478bd9Sstevel@tonic-gate  */
2297c478bd9Sstevel@tonic-gate int
2307c478bd9Sstevel@tonic-gate pollunlock(void)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	pollcache_t *pcp;
2337c478bd9Sstevel@tonic-gate 	int lockstate = 0;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	/*
2367c478bd9Sstevel@tonic-gate 	 * t_pollcache is set by /dev/poll and event ports (port_fd.c).
2377c478bd9Sstevel@tonic-gate 	 * If the pollrelock/pollunlock is called as a result of poll(2),
2387c478bd9Sstevel@tonic-gate 	 * the t_pollcache should be NULL.
2397c478bd9Sstevel@tonic-gate 	 */
2407c478bd9Sstevel@tonic-gate 	if (curthread->t_pollcache == NULL)
2417c478bd9Sstevel@tonic-gate 		pcp = curthread->t_pollstate->ps_pcache;
2427c478bd9Sstevel@tonic-gate 	else
2437c478bd9Sstevel@tonic-gate 		pcp = curthread->t_pollcache;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if (mutex_owned(&pcp->pc_lock)) {
2467c478bd9Sstevel@tonic-gate 		lockstate = 1;
2477c478bd9Sstevel@tonic-gate 		mutex_exit(&pcp->pc_lock);
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 	return (lockstate);
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate void
2537c478bd9Sstevel@tonic-gate pollrelock(int lockstate)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate 	pollcache_t *pcp;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	/*
2587c478bd9Sstevel@tonic-gate 	 * t_pollcache is set by /dev/poll and event ports (port_fd.c).
2597c478bd9Sstevel@tonic-gate 	 * If the pollrelock/pollunlock is called as a result of poll(2),
2607c478bd9Sstevel@tonic-gate 	 * the t_pollcache should be NULL.
2617c478bd9Sstevel@tonic-gate 	 */
2627c478bd9Sstevel@tonic-gate 	if (curthread->t_pollcache == NULL)
2637c478bd9Sstevel@tonic-gate 		pcp = curthread->t_pollstate->ps_pcache;
2647c478bd9Sstevel@tonic-gate 	else
2657c478bd9Sstevel@tonic-gate 		pcp = curthread->t_pollcache;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	if (lockstate > 0)
2687c478bd9Sstevel@tonic-gate 		mutex_enter(&pcp->pc_lock);
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate /* ARGSUSED */
2727c478bd9Sstevel@tonic-gate void
2737c478bd9Sstevel@tonic-gate polllock(pollhead_t *php, kmutex_t *lp)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	if (!mutex_tryenter(lp)) {
2767c478bd9Sstevel@tonic-gate 		int lockstate = pollunlock();
2777c478bd9Sstevel@tonic-gate 		mutex_enter(lp);
2787c478bd9Sstevel@tonic-gate 		pollrelock(lockstate);
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate static int
2837c478bd9Sstevel@tonic-gate poll_common(pollfd_t *fds, nfds_t nfds, timespec_t *tsp, k_sigset_t *ksetp)
2847c478bd9Sstevel@tonic-gate {
2857c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
2867c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
2877c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
2887c478bd9Sstevel@tonic-gate 	int fdcnt = 0;
2897c478bd9Sstevel@tonic-gate 	int rval;
2907c478bd9Sstevel@tonic-gate 	int i;
2917c478bd9Sstevel@tonic-gate 	timespec_t *rqtp = NULL;
2923348528fSdm120769 	int timecheck = 0;
2937c478bd9Sstevel@tonic-gate 	int imm_timeout = 0;
2947c478bd9Sstevel@tonic-gate 	pollfd_t *pollfdp;
2957c478bd9Sstevel@tonic-gate 	pollstate_t *ps;
2967c478bd9Sstevel@tonic-gate 	pollcache_t *pcp;
2977c478bd9Sstevel@tonic-gate 	int error = 0;
2987c478bd9Sstevel@tonic-gate 	nfds_t old_nfds;
2997c478bd9Sstevel@tonic-gate 	int cacheindex = 0;	/* which cache set is used */
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	/*
3027c478bd9Sstevel@tonic-gate 	 * Determine the precise future time of the requested timeout, if any.
3037c478bd9Sstevel@tonic-gate 	 */
3047c478bd9Sstevel@tonic-gate 	if (tsp != NULL) {
3057c478bd9Sstevel@tonic-gate 		if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
3067c478bd9Sstevel@tonic-gate 			imm_timeout = 1;
3077c478bd9Sstevel@tonic-gate 		else {
3087c478bd9Sstevel@tonic-gate 			timespec_t now;
3093348528fSdm120769 			timecheck = timechanged;
3107c478bd9Sstevel@tonic-gate 			gethrestime(&now);
3117c478bd9Sstevel@tonic-gate 			rqtp = tsp;
3127c478bd9Sstevel@tonic-gate 			timespecadd(rqtp, &now);
3137c478bd9Sstevel@tonic-gate 		}
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	/*
3177c478bd9Sstevel@tonic-gate 	 * Reset our signal mask, if requested.
3187c478bd9Sstevel@tonic-gate 	 */
3197c478bd9Sstevel@tonic-gate 	if (ksetp != NULL) {
3207c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
3217c478bd9Sstevel@tonic-gate 		schedctl_finish_sigblock(t);
3227c478bd9Sstevel@tonic-gate 		lwp->lwp_sigoldmask = t->t_hold;
3237c478bd9Sstevel@tonic-gate 		t->t_hold = *ksetp;
3247c478bd9Sstevel@tonic-gate 		t->t_flag |= T_TOMASK;
3257c478bd9Sstevel@tonic-gate 		/*
3267c478bd9Sstevel@tonic-gate 		 * Call cv_timedwait_sig() just to check for signals.
3277c478bd9Sstevel@tonic-gate 		 * We will return immediately with either 0 or -1.
3287c478bd9Sstevel@tonic-gate 		 */
3297c478bd9Sstevel@tonic-gate 		if (!cv_timedwait_sig(&t->t_delay_cv, &p->p_lock, lbolt)) {
3307c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
3317c478bd9Sstevel@tonic-gate 			error = EINTR;
3327c478bd9Sstevel@tonic-gate 			goto pollout;
3337c478bd9Sstevel@tonic-gate 		}
3347c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	/*
3387c478bd9Sstevel@tonic-gate 	 * Check to see if this guy just wants to use poll() as a timeout.
3397c478bd9Sstevel@tonic-gate 	 * If yes then bypass all the other stuff and make him sleep.
3407c478bd9Sstevel@tonic-gate 	 */
3417c478bd9Sstevel@tonic-gate 	if (nfds == 0) {
3427c478bd9Sstevel@tonic-gate 		/*
3437c478bd9Sstevel@tonic-gate 		 * Sleep until we have passed the requested future
3447c478bd9Sstevel@tonic-gate 		 * time or until interrupted by a signal.
3457c478bd9Sstevel@tonic-gate 		 * Do not check for signals if we have a zero timeout.
3467c478bd9Sstevel@tonic-gate 		 */
3477c478bd9Sstevel@tonic-gate 		if (!imm_timeout) {
3487c478bd9Sstevel@tonic-gate 			mutex_enter(&t->t_delay_lock);
3497c478bd9Sstevel@tonic-gate 			while ((rval = cv_waituntil_sig(&t->t_delay_cv,
3503348528fSdm120769 			    &t->t_delay_lock, rqtp, timecheck)) > 0)
3517c478bd9Sstevel@tonic-gate 				continue;
3527c478bd9Sstevel@tonic-gate 			mutex_exit(&t->t_delay_lock);
3537c478bd9Sstevel@tonic-gate 			if (rval == 0)
3547c478bd9Sstevel@tonic-gate 				error = EINTR;
3557c478bd9Sstevel@tonic-gate 		}
3567c478bd9Sstevel@tonic-gate 		goto pollout;
3577c478bd9Sstevel@tonic-gate 	}
3587c478bd9Sstevel@tonic-gate 
3595f684e24Ssp92102 	if (nfds > p->p_fno_ctl) {
3607c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
3617c478bd9Sstevel@tonic-gate 		(void) rctl_action(rctlproc_legacy[RLIMIT_NOFILE],
3627c478bd9Sstevel@tonic-gate 		    p->p_rctls, p, RCA_SAFE);
3637c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
3647c478bd9Sstevel@tonic-gate 		error = EINVAL;
3657c478bd9Sstevel@tonic-gate 		goto pollout;
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	/*
3697c478bd9Sstevel@tonic-gate 	 * Need to allocate memory for pollstate before anything because
3707c478bd9Sstevel@tonic-gate 	 * the mutex and cv are created in this space
3717c478bd9Sstevel@tonic-gate 	 */
3727c478bd9Sstevel@tonic-gate 	if ((ps = t->t_pollstate) == NULL) {
3737c478bd9Sstevel@tonic-gate 		t->t_pollstate = pollstate_create();
3747c478bd9Sstevel@tonic-gate 		ps = t->t_pollstate;
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	if (ps->ps_pcache == NULL)
3787c478bd9Sstevel@tonic-gate 		ps->ps_pcache = pcache_alloc();
3797c478bd9Sstevel@tonic-gate 	pcp = ps->ps_pcache;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	/*
3827c478bd9Sstevel@tonic-gate 	 * NOTE: for performance, buffers are saved across poll() calls.
3837c478bd9Sstevel@tonic-gate 	 * The theory is that if a process polls heavily, it tends to poll
3847c478bd9Sstevel@tonic-gate 	 * on the same set of descriptors.  Therefore, we only reallocate
3857c478bd9Sstevel@tonic-gate 	 * buffers when nfds changes.  There is no hysteresis control,
3867c478bd9Sstevel@tonic-gate 	 * because there is no data to suggest that this is necessary;
3877c478bd9Sstevel@tonic-gate 	 * the penalty of reallocating is not *that* great in any event.
3887c478bd9Sstevel@tonic-gate 	 */
3897c478bd9Sstevel@tonic-gate 	old_nfds = ps->ps_nfds;
3907c478bd9Sstevel@tonic-gate 	if (nfds != old_nfds) {
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 		kmem_free(ps->ps_pollfd, old_nfds * sizeof (pollfd_t));
3937c478bd9Sstevel@tonic-gate 		pollfdp = kmem_alloc(nfds * sizeof (pollfd_t), KM_SLEEP);
3947c478bd9Sstevel@tonic-gate 		ps->ps_pollfd = pollfdp;
3957c478bd9Sstevel@tonic-gate 		ps->ps_nfds = nfds;
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	pollfdp = ps->ps_pollfd;
3997c478bd9Sstevel@tonic-gate 	if (copyin(fds, pollfdp, nfds * sizeof (pollfd_t))) {
4007c478bd9Sstevel@tonic-gate 		error = EFAULT;
4017c478bd9Sstevel@tonic-gate 		goto pollout;
4027c478bd9Sstevel@tonic-gate 	}
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	if (fds == NULL) {
4057c478bd9Sstevel@tonic-gate 		/*
4067c478bd9Sstevel@tonic-gate 		 * If the process has page 0 mapped, then the copyin() above
4077c478bd9Sstevel@tonic-gate 		 * will succeed even if fds is NULL.  However, our cached
4087c478bd9Sstevel@tonic-gate 		 * poll lists are keyed by the address of the passed-in fds
4097c478bd9Sstevel@tonic-gate 		 * structure, and we use the value NULL to indicate an unused
4107c478bd9Sstevel@tonic-gate 		 * poll cache list entry.  As such, we elect not to support
4117c478bd9Sstevel@tonic-gate 		 * NULL as a valid (user) memory address and fail the poll()
4127c478bd9Sstevel@tonic-gate 		 * call.
4137c478bd9Sstevel@tonic-gate 		 */
4147c478bd9Sstevel@tonic-gate 		error = EINVAL;
4157c478bd9Sstevel@tonic-gate 		goto pollout;
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	/*
4197c478bd9Sstevel@tonic-gate 	 * If this thread polls for the first time, allocate ALL poll
4207c478bd9Sstevel@tonic-gate 	 * cache data structures and cache the poll fd list. This
4217c478bd9Sstevel@tonic-gate 	 * allocation is delayed till now because lwp's polling 0 fd
4227c478bd9Sstevel@tonic-gate 	 * (i.e. using poll as timeout()) don't need this memory.
4237c478bd9Sstevel@tonic-gate 	 */
4247c478bd9Sstevel@tonic-gate 	mutex_enter(&ps->ps_lock);
4257c478bd9Sstevel@tonic-gate 	pcp = ps->ps_pcache;
4267c478bd9Sstevel@tonic-gate 	ASSERT(pcp != NULL);
4277c478bd9Sstevel@tonic-gate 	if (pcp->pc_bitmap == NULL) {
4287c478bd9Sstevel@tonic-gate 		pcache_create(pcp, nfds);
4297c478bd9Sstevel@tonic-gate 		/*
4307c478bd9Sstevel@tonic-gate 		 * poll and cache this poll fd list in ps_pcacheset[0].
4317c478bd9Sstevel@tonic-gate 		 */
4327c478bd9Sstevel@tonic-gate 		error = pcacheset_cache_list(ps, fds, &fdcnt, cacheindex);
4337c478bd9Sstevel@tonic-gate 		if (fdcnt || error) {
4347c478bd9Sstevel@tonic-gate 			mutex_exit(&ps->ps_lock);
4357c478bd9Sstevel@tonic-gate 			goto pollout;
4367c478bd9Sstevel@tonic-gate 		}
4377c478bd9Sstevel@tonic-gate 	} else {
4387c478bd9Sstevel@tonic-gate 		pollcacheset_t	*pcset = ps->ps_pcacheset;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 		/*
4417c478bd9Sstevel@tonic-gate 		 * Not first time polling. Select a cached poll list by
4427c478bd9Sstevel@tonic-gate 		 * matching user pollfd list buffer address.
4437c478bd9Sstevel@tonic-gate 		 */
4447c478bd9Sstevel@tonic-gate 		for (cacheindex = 0; cacheindex < ps->ps_nsets; cacheindex++) {
4457c478bd9Sstevel@tonic-gate 			if (pcset[cacheindex].pcs_usradr == (uintptr_t)fds) {
4467c478bd9Sstevel@tonic-gate 				if ((++pcset[cacheindex].pcs_count) == 0) {
4477c478bd9Sstevel@tonic-gate 					/*
4487c478bd9Sstevel@tonic-gate 					 * counter is wrapping around.
4497c478bd9Sstevel@tonic-gate 					 */
4507c478bd9Sstevel@tonic-gate 					pcacheset_reset_count(ps, cacheindex);
4517c478bd9Sstevel@tonic-gate 				}
4527c478bd9Sstevel@tonic-gate 				/*
4537c478bd9Sstevel@tonic-gate 				 * examine and resolve possible
4547c478bd9Sstevel@tonic-gate 				 * difference of the current poll
4557c478bd9Sstevel@tonic-gate 				 * list and previously cached one.
4567c478bd9Sstevel@tonic-gate 				 * If there is an error during resolve(),
4577c478bd9Sstevel@tonic-gate 				 * the callee will guarantee the consistency
4587c478bd9Sstevel@tonic-gate 				 * of cached poll list and cache content.
4597c478bd9Sstevel@tonic-gate 				 */
4607c478bd9Sstevel@tonic-gate 				error = pcacheset_resolve(ps, nfds, &fdcnt,
4617c478bd9Sstevel@tonic-gate 				    cacheindex);
4627c478bd9Sstevel@tonic-gate 				if (error) {
4637c478bd9Sstevel@tonic-gate 					mutex_exit(&ps->ps_lock);
4647c478bd9Sstevel@tonic-gate 					goto pollout;
4657c478bd9Sstevel@tonic-gate 				}
4667c478bd9Sstevel@tonic-gate 				break;
4677c478bd9Sstevel@tonic-gate 			}
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 			/*
4707c478bd9Sstevel@tonic-gate 			 * Note that pcs_usradr field of an used entry won't be
4717c478bd9Sstevel@tonic-gate 			 * NULL because it stores the address of passed-in fds,
4727c478bd9Sstevel@tonic-gate 			 * and NULL fds will not be cached (Then it is either
4737c478bd9Sstevel@tonic-gate 			 * the special timeout case when nfds is 0 or it returns
4747c478bd9Sstevel@tonic-gate 			 * failure directly).
4757c478bd9Sstevel@tonic-gate 			 */
4767c478bd9Sstevel@tonic-gate 			if (pcset[cacheindex].pcs_usradr == NULL) {
4777c478bd9Sstevel@tonic-gate 				/*
4787c478bd9Sstevel@tonic-gate 				 * found an unused entry. Use it to cache
4797c478bd9Sstevel@tonic-gate 				 * this poll list.
4807c478bd9Sstevel@tonic-gate 				 */
4817c478bd9Sstevel@tonic-gate 				error = pcacheset_cache_list(ps, fds, &fdcnt,
4827c478bd9Sstevel@tonic-gate 				    cacheindex);
4837c478bd9Sstevel@tonic-gate 				if (fdcnt || error) {
4847c478bd9Sstevel@tonic-gate 					mutex_exit(&ps->ps_lock);
4857c478bd9Sstevel@tonic-gate 					goto pollout;
4867c478bd9Sstevel@tonic-gate 				}
4877c478bd9Sstevel@tonic-gate 				break;
4887c478bd9Sstevel@tonic-gate 			}
4897c478bd9Sstevel@tonic-gate 		}
4907c478bd9Sstevel@tonic-gate 		if (cacheindex == ps->ps_nsets) {
4917c478bd9Sstevel@tonic-gate 			/*
4927c478bd9Sstevel@tonic-gate 			 * We failed to find a matching cached poll fd list.
4937c478bd9Sstevel@tonic-gate 			 * replace an old list.
4947c478bd9Sstevel@tonic-gate 			 */
4957c478bd9Sstevel@tonic-gate 			pollstats.polllistmiss.value.ui64++;
4967c478bd9Sstevel@tonic-gate 			cacheindex = pcacheset_replace(ps);
4977c478bd9Sstevel@tonic-gate 			ASSERT(cacheindex < ps->ps_nsets);
4987c478bd9Sstevel@tonic-gate 			pcset[cacheindex].pcs_usradr = (uintptr_t)fds;
4997c478bd9Sstevel@tonic-gate 			error = pcacheset_resolve(ps, nfds, &fdcnt, cacheindex);
5007c478bd9Sstevel@tonic-gate 			if (error) {
5017c478bd9Sstevel@tonic-gate 				mutex_exit(&ps->ps_lock);
5027c478bd9Sstevel@tonic-gate 				goto pollout;
5037c478bd9Sstevel@tonic-gate 			}
5047c478bd9Sstevel@tonic-gate 		}
5057c478bd9Sstevel@tonic-gate 	}
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	/*
5087c478bd9Sstevel@tonic-gate 	 * Always scan the bitmap with the lock on the pollcache held.
5097c478bd9Sstevel@tonic-gate 	 * This is to make sure that a wakeup does not come undetected.
5107c478bd9Sstevel@tonic-gate 	 * If the lock is not held, a pollwakeup could have come for an
5117c478bd9Sstevel@tonic-gate 	 * fd we already checked but before this thread sleeps, in which
5127c478bd9Sstevel@tonic-gate 	 * case the wakeup is missed. Now we hold the pcache lock and
5137c478bd9Sstevel@tonic-gate 	 * check the bitmap again. This will prevent wakeup from happening
5147c478bd9Sstevel@tonic-gate 	 * while we hold pcache lock since pollwakeup() will also lock
5157c478bd9Sstevel@tonic-gate 	 * the pcache before updating poll bitmap.
5167c478bd9Sstevel@tonic-gate 	 */
5177c478bd9Sstevel@tonic-gate 	mutex_enter(&pcp->pc_lock);
5187c478bd9Sstevel@tonic-gate 	for (;;) {
5197c478bd9Sstevel@tonic-gate 		pcp->pc_flag = 0;
5207c478bd9Sstevel@tonic-gate 		error = pcache_poll(pollfdp, ps, nfds, &fdcnt, cacheindex);
5217c478bd9Sstevel@tonic-gate 		if (fdcnt || error) {
5227c478bd9Sstevel@tonic-gate 			mutex_exit(&pcp->pc_lock);
5237c478bd9Sstevel@tonic-gate 			mutex_exit(&ps->ps_lock);
5247c478bd9Sstevel@tonic-gate 			break;
5257c478bd9Sstevel@tonic-gate 		}
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 		/*
5287c478bd9Sstevel@tonic-gate 		 * If T_POLLWAKE is set, a pollwakeup() was performed on
5297c478bd9Sstevel@tonic-gate 		 * one of the file descriptors.  This can happen only if
5307c478bd9Sstevel@tonic-gate 		 * one of the VOP_POLL() functions dropped pcp->pc_lock.
5317c478bd9Sstevel@tonic-gate 		 * The only current cases of this is in procfs (prpoll())
5327c478bd9Sstevel@tonic-gate 		 * and STREAMS (strpoll()).
5337c478bd9Sstevel@tonic-gate 		 */
5347c478bd9Sstevel@tonic-gate 		if (pcp->pc_flag & T_POLLWAKE)
5357c478bd9Sstevel@tonic-gate 			continue;
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 		/*
5387c478bd9Sstevel@tonic-gate 		 * If you get here, the poll of fds was unsuccessful.
5397c478bd9Sstevel@tonic-gate 		 * Wait until some fd becomes readable, writable, or gets
5407c478bd9Sstevel@tonic-gate 		 * an exception, or until a signal or a timeout occurs.
5417c478bd9Sstevel@tonic-gate 		 * Do not check for signals if we have a zero timeout.
5427c478bd9Sstevel@tonic-gate 		 */
5437c478bd9Sstevel@tonic-gate 		mutex_exit(&ps->ps_lock);
5447c478bd9Sstevel@tonic-gate 		if (imm_timeout)
5457c478bd9Sstevel@tonic-gate 			rval = -1;
5467c478bd9Sstevel@tonic-gate 		else
5477c478bd9Sstevel@tonic-gate 			rval = cv_waituntil_sig(&pcp->pc_cv, &pcp->pc_lock,
5483348528fSdm120769 			    rqtp, timecheck);
5497c478bd9Sstevel@tonic-gate 		mutex_exit(&pcp->pc_lock);
5507c478bd9Sstevel@tonic-gate 		/*
5517c478bd9Sstevel@tonic-gate 		 * If we have received a signal or timed out
5527c478bd9Sstevel@tonic-gate 		 * then break out and return.
5537c478bd9Sstevel@tonic-gate 		 */
5547c478bd9Sstevel@tonic-gate 		if (rval <= 0) {
5557c478bd9Sstevel@tonic-gate 			if (rval == 0)
5567c478bd9Sstevel@tonic-gate 				error = EINTR;
5577c478bd9Sstevel@tonic-gate 			break;
5587c478bd9Sstevel@tonic-gate 		}
5597c478bd9Sstevel@tonic-gate 		/*
5607c478bd9Sstevel@tonic-gate 		 * We have not received a signal or timed out.
5617c478bd9Sstevel@tonic-gate 		 * Continue around and poll fds again.
5627c478bd9Sstevel@tonic-gate 		 */
5637c478bd9Sstevel@tonic-gate 		mutex_enter(&ps->ps_lock);
5647c478bd9Sstevel@tonic-gate 		mutex_enter(&pcp->pc_lock);
5657c478bd9Sstevel@tonic-gate 	}
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate pollout:
5687c478bd9Sstevel@tonic-gate 	/*
5697c478bd9Sstevel@tonic-gate 	 * If we changed the signal mask but we received
5707c478bd9Sstevel@tonic-gate 	 * no signal then restore the signal mask.
5717c478bd9Sstevel@tonic-gate 	 * Otherwise psig() will deal with the signal mask.
5727c478bd9Sstevel@tonic-gate 	 */
5737c478bd9Sstevel@tonic-gate 	if (ksetp != NULL) {
5747c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
5757c478bd9Sstevel@tonic-gate 		if (lwp->lwp_cursig == 0) {
5767c478bd9Sstevel@tonic-gate 			t->t_hold = lwp->lwp_sigoldmask;
5777c478bd9Sstevel@tonic-gate 			t->t_flag &= ~T_TOMASK;
5787c478bd9Sstevel@tonic-gate 		}
5797c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
5807c478bd9Sstevel@tonic-gate 	}
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	if (error)
5837c478bd9Sstevel@tonic-gate 		return (set_errno(error));
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	/*
5867c478bd9Sstevel@tonic-gate 	 * Copy out the events and return the fdcnt to the user.
5877c478bd9Sstevel@tonic-gate 	 */
5887c478bd9Sstevel@tonic-gate 	if (nfds != 0 &&
5897c478bd9Sstevel@tonic-gate 	    copyout(pollfdp, fds, nfds * sizeof (pollfd_t)))
5907c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate #ifdef DEBUG
5937c478bd9Sstevel@tonic-gate 	/*
5947c478bd9Sstevel@tonic-gate 	 * Another sanity check:
5957c478bd9Sstevel@tonic-gate 	 */
5967c478bd9Sstevel@tonic-gate 	if (fdcnt) {
5977c478bd9Sstevel@tonic-gate 		int	reventcnt = 0;
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 		for (i = 0; i < nfds; i++) {
6007c478bd9Sstevel@tonic-gate 			if (pollfdp[i].fd < 0) {
6017c478bd9Sstevel@tonic-gate 				ASSERT(pollfdp[i].revents == 0);
6027c478bd9Sstevel@tonic-gate 				continue;
6037c478bd9Sstevel@tonic-gate 			}
6047c478bd9Sstevel@tonic-gate 			if (pollfdp[i].revents) {
6057c478bd9Sstevel@tonic-gate 				reventcnt++;
6067c478bd9Sstevel@tonic-gate 			}
6077c478bd9Sstevel@tonic-gate 		}
6087c478bd9Sstevel@tonic-gate 		ASSERT(fdcnt == reventcnt);
6097c478bd9Sstevel@tonic-gate 	} else {
6107c478bd9Sstevel@tonic-gate 		for (i = 0; i < nfds; i++) {
6117c478bd9Sstevel@tonic-gate 			ASSERT(pollfdp[i].revents == 0);
6127c478bd9Sstevel@tonic-gate 		}
6137c478bd9Sstevel@tonic-gate 	}
6147c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	return (fdcnt);
6177c478bd9Sstevel@tonic-gate }
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate /*
6207c478bd9Sstevel@tonic-gate  * This system call trap exists solely for binary compatibility with
6217c478bd9Sstevel@tonic-gate  * old statically-linked applications.  It is not called from libc.
6227c478bd9Sstevel@tonic-gate  * It should be removed in the next release.
6237c478bd9Sstevel@tonic-gate  */
6247c478bd9Sstevel@tonic-gate int
6257c478bd9Sstevel@tonic-gate poll(pollfd_t *fds, nfds_t nfds, int time_out)
6267c478bd9Sstevel@tonic-gate {
6277c478bd9Sstevel@tonic-gate 	timespec_t ts;
6287c478bd9Sstevel@tonic-gate 	timespec_t *tsp;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	if (time_out < 0)
6317c478bd9Sstevel@tonic-gate 		tsp = NULL;
6327c478bd9Sstevel@tonic-gate 	else {
6337c478bd9Sstevel@tonic-gate 		ts.tv_sec = time_out / MILLISEC;
6347c478bd9Sstevel@tonic-gate 		ts.tv_nsec = (time_out % MILLISEC) * MICROSEC;
6357c478bd9Sstevel@tonic-gate 		tsp = &ts;
6367c478bd9Sstevel@tonic-gate 	}
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	return (poll_common(fds, nfds, tsp, NULL));
6397c478bd9Sstevel@tonic-gate }
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate /*
6427c478bd9Sstevel@tonic-gate  * This is the system call trap that poll(),
6437c478bd9Sstevel@tonic-gate  * select() and pselect() are built upon.
6447c478bd9Sstevel@tonic-gate  * It is a private interface between libc and the kernel.
6457c478bd9Sstevel@tonic-gate  */
6467c478bd9Sstevel@tonic-gate int
6477c478bd9Sstevel@tonic-gate pollsys(pollfd_t *fds, nfds_t nfds, timespec_t *timeoutp, sigset_t *setp)
6487c478bd9Sstevel@tonic-gate {
6497c478bd9Sstevel@tonic-gate 	timespec_t ts;
6507c478bd9Sstevel@tonic-gate 	timespec_t *tsp;
6517c478bd9Sstevel@tonic-gate 	sigset_t set;
6527c478bd9Sstevel@tonic-gate 	k_sigset_t kset;
6537c478bd9Sstevel@tonic-gate 	k_sigset_t *ksetp;
6547c478bd9Sstevel@tonic-gate 	model_t datamodel = get_udatamodel();
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	if (timeoutp == NULL)
6577c478bd9Sstevel@tonic-gate 		tsp = NULL;
6587c478bd9Sstevel@tonic-gate 	else {
6597c478bd9Sstevel@tonic-gate 		if (datamodel == DATAMODEL_NATIVE) {
6607c478bd9Sstevel@tonic-gate 			if (copyin(timeoutp, &ts, sizeof (ts)))
6617c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
6627c478bd9Sstevel@tonic-gate 		} else {
6637c478bd9Sstevel@tonic-gate 			timespec32_t ts32;
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 			if (copyin(timeoutp, &ts32, sizeof (ts32)))
6667c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
6677c478bd9Sstevel@tonic-gate 			TIMESPEC32_TO_TIMESPEC(&ts, &ts32)
6687c478bd9Sstevel@tonic-gate 		}
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 		if (itimerspecfix(&ts))
6717c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
6727c478bd9Sstevel@tonic-gate 		tsp = &ts;
6737c478bd9Sstevel@tonic-gate 	}
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	if (setp == NULL)
6767c478bd9Sstevel@tonic-gate 		ksetp = NULL;
6777c478bd9Sstevel@tonic-gate 	else {
6787c478bd9Sstevel@tonic-gate 		if (copyin(setp, &set, sizeof (set)))
6797c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
6807c478bd9Sstevel@tonic-gate 		sigutok(&set, &kset);
6817c478bd9Sstevel@tonic-gate 		ksetp = &kset;
6827c478bd9Sstevel@tonic-gate 	}
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	return (poll_common(fds, nfds, tsp, ksetp));
6857c478bd9Sstevel@tonic-gate }
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate /*
6887c478bd9Sstevel@tonic-gate  * Clean up any state left around by poll(2). Called when a thread exits.
6897c478bd9Sstevel@tonic-gate  */
6907c478bd9Sstevel@tonic-gate void
6917c478bd9Sstevel@tonic-gate pollcleanup()
6927c478bd9Sstevel@tonic-gate {
6937c478bd9Sstevel@tonic-gate 	pollstate_t *ps = curthread->t_pollstate;
6947c478bd9Sstevel@tonic-gate 	pollcache_t *pcp;
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	if (ps == NULL)
6977c478bd9Sstevel@tonic-gate 		return;
6987c478bd9Sstevel@tonic-gate 	pcp = ps->ps_pcache;
6997c478bd9Sstevel@tonic-gate 	/*
7007c478bd9Sstevel@tonic-gate 	 * free up all cached poll fds
7017c478bd9Sstevel@tonic-gate 	 */
7027c478bd9Sstevel@tonic-gate 	if (pcp == NULL) {
7037c478bd9Sstevel@tonic-gate 		/* this pollstate is used by /dev/poll */
7047c478bd9Sstevel@tonic-gate 		goto pollcleanout;
7057c478bd9Sstevel@tonic-gate 	}
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	if (pcp->pc_bitmap != NULL) {
7087c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_NOT_HELD(&ps->ps_lock));
7097c478bd9Sstevel@tonic-gate 		/*
7107c478bd9Sstevel@tonic-gate 		 * a close lwp can race with us when cleaning up a polldat
7117c478bd9Sstevel@tonic-gate 		 * entry. We hold the ps_lock when cleaning hash table.
7127c478bd9Sstevel@tonic-gate 		 * Since this pollcache is going away anyway, there is no
7137c478bd9Sstevel@tonic-gate 		 * need to hold the pc_lock.
7147c478bd9Sstevel@tonic-gate 		 */
7157c478bd9Sstevel@tonic-gate 		mutex_enter(&ps->ps_lock);
7167c478bd9Sstevel@tonic-gate 		pcache_clean(pcp);
7177c478bd9Sstevel@tonic-gate 		mutex_exit(&ps->ps_lock);
7187c478bd9Sstevel@tonic-gate #ifdef DEBUG
7197c478bd9Sstevel@tonic-gate 		/*
7207c478bd9Sstevel@tonic-gate 		 * At this point, all fds cached by this lwp should be
7217c478bd9Sstevel@tonic-gate 		 * cleaned up. There should be no fd in fi_list still
7227c478bd9Sstevel@tonic-gate 		 * reference this thread.
7237c478bd9Sstevel@tonic-gate 		 */
7247c478bd9Sstevel@tonic-gate 		checkfpollinfo();	/* sanity check */
7257c478bd9Sstevel@tonic-gate 		pollcheckphlist();	/* sanity check */
7267c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate 	/*
7297c478bd9Sstevel@tonic-gate 	 * Be sure no one is referencing thread before exiting
7307c478bd9Sstevel@tonic-gate 	 */
7317c478bd9Sstevel@tonic-gate 	mutex_enter(&pcp->pc_no_exit);
7327c478bd9Sstevel@tonic-gate 	ASSERT(pcp->pc_busy >= 0);
7337c478bd9Sstevel@tonic-gate 	while (pcp->pc_busy > 0)
7347c478bd9Sstevel@tonic-gate 		cv_wait(&pcp->pc_busy_cv, &pcp->pc_no_exit);
7357c478bd9Sstevel@tonic-gate 	mutex_exit(&pcp->pc_no_exit);
7367c478bd9Sstevel@tonic-gate pollcleanout:
7377c478bd9Sstevel@tonic-gate 	pollstate_destroy(ps);
7387c478bd9Sstevel@tonic-gate 	curthread->t_pollstate = NULL;
7397c478bd9Sstevel@tonic-gate }
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate /*
7427c478bd9Sstevel@tonic-gate  * pollwakeup() - poke threads waiting in poll() for some event
7437c478bd9Sstevel@tonic-gate  * on a particular object.
7447c478bd9Sstevel@tonic-gate  *
7457c478bd9Sstevel@tonic-gate  * The threads hanging off of the specified pollhead structure are scanned.
7467c478bd9Sstevel@tonic-gate  * If their event mask matches the specified event(s), then pollnotify() is
7477c478bd9Sstevel@tonic-gate  * called to poke the thread.
7487c478bd9Sstevel@tonic-gate  *
7497c478bd9Sstevel@tonic-gate  * Multiple events may be specified.  When POLLHUP or POLLERR are specified,
7507c478bd9Sstevel@tonic-gate  * all waiting threads are poked.
7517c478bd9Sstevel@tonic-gate  *
7527c478bd9Sstevel@tonic-gate  * It is important that pollnotify() not drop the lock protecting the list
7537c478bd9Sstevel@tonic-gate  * of threads.
7547c478bd9Sstevel@tonic-gate  */
7557c478bd9Sstevel@tonic-gate void
7567c478bd9Sstevel@tonic-gate pollwakeup(pollhead_t *php, short events_arg)
7577c478bd9Sstevel@tonic-gate {
7587c478bd9Sstevel@tonic-gate 	polldat_t	*pdp;
7597c478bd9Sstevel@tonic-gate 	int		events = (ushort_t)events_arg;
76011dc39ddSpraks 	struct plist {
76111dc39ddSpraks 		port_t *pp;
76211dc39ddSpraks 		int	pevents;
76311dc39ddSpraks 		struct plist *next;
76411dc39ddSpraks 		};
76511dc39ddSpraks 	struct plist *plhead = NULL, *pltail = NULL;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate retry:
7687c478bd9Sstevel@tonic-gate 	PH_ENTER(php);
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	for (pdp = php->ph_list; pdp; pdp = pdp->pd_next) {
7717c478bd9Sstevel@tonic-gate 		if ((pdp->pd_events & events) ||
7727c478bd9Sstevel@tonic-gate 		    (events & (POLLHUP | POLLERR))) {
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 			pollcache_t 	*pcp;
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 			if (pdp->pd_portev != NULL) {
7777c478bd9Sstevel@tonic-gate 				port_kevent_t	*pkevp = pdp->pd_portev;
7787c478bd9Sstevel@tonic-gate 				/*
7797c478bd9Sstevel@tonic-gate 				 * Object (fd) is associated with an event port,
7807c478bd9Sstevel@tonic-gate 				 * => send event notification to the port.
7817c478bd9Sstevel@tonic-gate 				 */
78211dc39ddSpraks 				ASSERT(pkevp->portkev_source == PORT_SOURCE_FD);
78361b4b1efSpraks 				mutex_enter(&pkevp->portkev_lock);
7847c478bd9Sstevel@tonic-gate 				if (pkevp->portkev_flags & PORT_KEV_VALID) {
78511dc39ddSpraks 					int pevents;
78611dc39ddSpraks 
7877c478bd9Sstevel@tonic-gate 					pkevp->portkev_flags &= ~PORT_KEV_VALID;
78861b4b1efSpraks 					pkevp->portkev_events |= events &
78961b4b1efSpraks 					    (pdp->pd_events | POLLHUP |
79061b4b1efSpraks 					    POLLERR);
79161b4b1efSpraks 					/*
79261b4b1efSpraks 					 * portkev_lock mutex will be released
79311dc39ddSpraks 					 * by port_send_event().
79461b4b1efSpraks 					 */
79511dc39ddSpraks 					port_send_event(pkevp);
79611dc39ddSpraks 
79711dc39ddSpraks 					/*
79811dc39ddSpraks 					 * If we have some thread polling the
79911dc39ddSpraks 					 * port's fd, add it to the list. They
80011dc39ddSpraks 					 * will be notified later.
80111dc39ddSpraks 					 * The port_pollwkup() will flag the
80211dc39ddSpraks 					 * port_t so that it will not disappear
80311dc39ddSpraks 					 * till port_pollwkdone() is called.
80411dc39ddSpraks 					 */
80511dc39ddSpraks 					pevents =
80611dc39ddSpraks 					    port_pollwkup(pkevp->portkev_port);
80711dc39ddSpraks 					if (pevents) {
80811dc39ddSpraks 						struct plist *t;
80911dc39ddSpraks 						t = kmem_zalloc(
81011dc39ddSpraks 						    sizeof (struct plist),
81111dc39ddSpraks 						    KM_SLEEP);
81211dc39ddSpraks 						t->pp = pkevp->portkev_port;
81311dc39ddSpraks 						t->pevents = pevents;
81411dc39ddSpraks 						if (plhead == NULL) {
81511dc39ddSpraks 							plhead = t;
81611dc39ddSpraks 						} else {
81711dc39ddSpraks 							pltail->next = t;
81811dc39ddSpraks 						}
81911dc39ddSpraks 						pltail = t;
82011dc39ddSpraks 					}
82161b4b1efSpraks 				} else {
82261b4b1efSpraks 					mutex_exit(&pkevp->portkev_lock);
8237c478bd9Sstevel@tonic-gate 				}
8247c478bd9Sstevel@tonic-gate 				continue;
8257c478bd9Sstevel@tonic-gate 			}
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 			pcp = pdp->pd_pcache;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 			/*
8307c478bd9Sstevel@tonic-gate 			 * Try to grab the lock for this thread. If
8317c478bd9Sstevel@tonic-gate 			 * we don't get it then we may deadlock so
8327c478bd9Sstevel@tonic-gate 			 * back out and restart all over again. Note
8337c478bd9Sstevel@tonic-gate 			 * that the failure rate is very very low.
8347c478bd9Sstevel@tonic-gate 			 */
8357c478bd9Sstevel@tonic-gate 			if (mutex_tryenter(&pcp->pc_lock)) {
8367c478bd9Sstevel@tonic-gate 				pollnotify(pcp, pdp->pd_fd);
8377c478bd9Sstevel@tonic-gate 				mutex_exit(&pcp->pc_lock);
8387c478bd9Sstevel@tonic-gate 			} else {
8397c478bd9Sstevel@tonic-gate 				/*
8407c478bd9Sstevel@tonic-gate 				 * We are here because:
8417c478bd9Sstevel@tonic-gate 				 *	1) This thread has been woke up
8427c478bd9Sstevel@tonic-gate 				 *	   and is trying to get out of poll().
8437c478bd9Sstevel@tonic-gate 				 *	2) Some other thread is also here
8447c478bd9Sstevel@tonic-gate 				 *	   but with a different pollhead lock.
8457c478bd9Sstevel@tonic-gate 				 *
8467c478bd9Sstevel@tonic-gate 				 * So, we need to drop the lock on pollhead
8477c478bd9Sstevel@tonic-gate 				 * because of (1) but we want to prevent
8487c478bd9Sstevel@tonic-gate 				 * that thread from doing lwp_exit() or
8497c478bd9Sstevel@tonic-gate 				 * devpoll close. We want to ensure that
8507c478bd9Sstevel@tonic-gate 				 * the pollcache pointer is still invalid.
8517c478bd9Sstevel@tonic-gate 				 *
8527c478bd9Sstevel@tonic-gate 				 * Solution: Grab the pcp->pc_no_exit lock,
8537c478bd9Sstevel@tonic-gate 				 * increment the pc_busy counter, drop every
8547c478bd9Sstevel@tonic-gate 				 * lock in sight. Get out of the way and wait
8557c478bd9Sstevel@tonic-gate 				 * for type (2) threads to finish.
8567c478bd9Sstevel@tonic-gate 				 */
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 				mutex_enter(&pcp->pc_no_exit);
8597c478bd9Sstevel@tonic-gate 				pcp->pc_busy++;	/* prevents exit()'s */
8607c478bd9Sstevel@tonic-gate 				mutex_exit(&pcp->pc_no_exit);
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 				PH_EXIT(php);
8637c478bd9Sstevel@tonic-gate 				mutex_enter(&pcp->pc_lock);
8647c478bd9Sstevel@tonic-gate 				mutex_exit(&pcp->pc_lock);
8657c478bd9Sstevel@tonic-gate 				mutex_enter(&pcp->pc_no_exit);
8667c478bd9Sstevel@tonic-gate 				pcp->pc_busy--;
8677c478bd9Sstevel@tonic-gate 				if (pcp->pc_busy == 0) {
8687c478bd9Sstevel@tonic-gate 					/*
8697c478bd9Sstevel@tonic-gate 					 * Wakeup the thread waiting in
8707c478bd9Sstevel@tonic-gate 					 * thread_exit().
8717c478bd9Sstevel@tonic-gate 					 */
8727c478bd9Sstevel@tonic-gate 					cv_signal(&pcp->pc_busy_cv);
8737c478bd9Sstevel@tonic-gate 				}
8747c478bd9Sstevel@tonic-gate 				mutex_exit(&pcp->pc_no_exit);
8757c478bd9Sstevel@tonic-gate 				goto retry;
8767c478bd9Sstevel@tonic-gate 			}
8777c478bd9Sstevel@tonic-gate 		}
8787c478bd9Sstevel@tonic-gate 	}
87911dc39ddSpraks 
88011dc39ddSpraks 
88111dc39ddSpraks 	/*
88211dc39ddSpraks 	 * Event ports - If this php is of the port on the list,
88311dc39ddSpraks 	 * call port_pollwkdone() to release it. The port_pollwkdone()
88411dc39ddSpraks 	 * needs to be called before dropping the PH lock so that any new
88511dc39ddSpraks 	 * thread attempting to poll this port are blocked. There can be
88611dc39ddSpraks 	 * only one thread here in pollwakeup notifying this port's fd.
88711dc39ddSpraks 	 */
88811dc39ddSpraks 	if (plhead != NULL && &plhead->pp->port_pollhd == php) {
88911dc39ddSpraks 		struct plist *t;
89011dc39ddSpraks 		port_pollwkdone(plhead->pp);
89111dc39ddSpraks 		t = plhead;
89211dc39ddSpraks 		plhead = plhead->next;
89311dc39ddSpraks 		kmem_free(t, sizeof (struct plist));
89411dc39ddSpraks 	}
8957c478bd9Sstevel@tonic-gate 	PH_EXIT(php);
89611dc39ddSpraks 
89711dc39ddSpraks 	/*
89811dc39ddSpraks 	 * Event ports - Notify threads polling the event port's fd.
89911dc39ddSpraks 	 * This is normally done in port_send_event() where it calls
90011dc39ddSpraks 	 * pollwakeup() on the port. But, for PORT_SOURCE_FD source alone,
90111dc39ddSpraks 	 * we do it here in pollwakeup() to avoid a recursive call.
90211dc39ddSpraks 	 */
90311dc39ddSpraks 	if (plhead != NULL) {
90411dc39ddSpraks 		php = &plhead->pp->port_pollhd;
90511dc39ddSpraks 		events = plhead->pevents;
90611dc39ddSpraks 		goto retry;
90711dc39ddSpraks 	}
9087c478bd9Sstevel@tonic-gate }
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate /*
9117c478bd9Sstevel@tonic-gate  * This function is called to inform a thread that
9127c478bd9Sstevel@tonic-gate  * an event being polled for has occurred.
9137c478bd9Sstevel@tonic-gate  * The pollstate lock on the thread should be held on entry.
9147c478bd9Sstevel@tonic-gate  */
9157c478bd9Sstevel@tonic-gate void
9167c478bd9Sstevel@tonic-gate pollnotify(pollcache_t *pcp, int fd)
9177c478bd9Sstevel@tonic-gate {
9187c478bd9Sstevel@tonic-gate 	ASSERT(fd < pcp->pc_mapsize);
9197c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pcp->pc_lock));
9207c478bd9Sstevel@tonic-gate 	BT_SET(pcp->pc_bitmap, fd);
9217c478bd9Sstevel@tonic-gate 	pcp->pc_flag |= T_POLLWAKE;
9227c478bd9Sstevel@tonic-gate 	cv_signal(&pcp->pc_cv);
9237c478bd9Sstevel@tonic-gate }
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate /*
9267c478bd9Sstevel@tonic-gate  * add a polldat entry to pollhead ph_list. The polldat struct is used
9277c478bd9Sstevel@tonic-gate  * by pollwakeup to wake sleeping pollers when polled events has happened.
9287c478bd9Sstevel@tonic-gate  */
9297c478bd9Sstevel@tonic-gate void
9307c478bd9Sstevel@tonic-gate pollhead_insert(pollhead_t *php, polldat_t *pdp)
9317c478bd9Sstevel@tonic-gate {
9327c478bd9Sstevel@tonic-gate 	PH_ENTER(php);
9337c478bd9Sstevel@tonic-gate 	ASSERT(pdp->pd_next == NULL);
9347c478bd9Sstevel@tonic-gate #ifdef DEBUG
9357c478bd9Sstevel@tonic-gate 	{
9367c478bd9Sstevel@tonic-gate 		/*
9377c478bd9Sstevel@tonic-gate 		 * the polldat should not be already on the list
9387c478bd9Sstevel@tonic-gate 		 */
9397c478bd9Sstevel@tonic-gate 		polldat_t *wp;
9407c478bd9Sstevel@tonic-gate 		for (wp = php->ph_list; wp; wp = wp->pd_next) {
9417c478bd9Sstevel@tonic-gate 			ASSERT(wp != pdp);
9427c478bd9Sstevel@tonic-gate 		}
9437c478bd9Sstevel@tonic-gate 	}
9447c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
9457c478bd9Sstevel@tonic-gate 	pdp->pd_next = php->ph_list;
9467c478bd9Sstevel@tonic-gate 	php->ph_list = pdp;
9477c478bd9Sstevel@tonic-gate 	PH_EXIT(php);
9487c478bd9Sstevel@tonic-gate }
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate /*
9517c478bd9Sstevel@tonic-gate  * Delete the polldat entry from ph_list.
9527c478bd9Sstevel@tonic-gate  */
9537c478bd9Sstevel@tonic-gate void
9547c478bd9Sstevel@tonic-gate pollhead_delete(pollhead_t *php, polldat_t *pdp)
9557c478bd9Sstevel@tonic-gate {
9567c478bd9Sstevel@tonic-gate 	polldat_t *wp;
9577c478bd9Sstevel@tonic-gate 	polldat_t **wpp;
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	PH_ENTER(php);
9607c478bd9Sstevel@tonic-gate 	for (wpp = &php->ph_list; (wp = *wpp) != NULL; wpp = &wp->pd_next) {
9617c478bd9Sstevel@tonic-gate 		if (wp == pdp) {
9627c478bd9Sstevel@tonic-gate 			*wpp = pdp->pd_next;
9637c478bd9Sstevel@tonic-gate 			pdp->pd_next = NULL;
9647c478bd9Sstevel@tonic-gate 			break;
9657c478bd9Sstevel@tonic-gate 		}
9667c478bd9Sstevel@tonic-gate 	}
9677c478bd9Sstevel@tonic-gate #ifdef DEBUG
9687c478bd9Sstevel@tonic-gate 	/* assert that pdp is no longer in the list */
9697c478bd9Sstevel@tonic-gate 	for (wp = *wpp; wp; wp = wp->pd_next) {
9707c478bd9Sstevel@tonic-gate 		ASSERT(wp != pdp);
9717c478bd9Sstevel@tonic-gate 	}
9727c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
9737c478bd9Sstevel@tonic-gate 	PH_EXIT(php);
9747c478bd9Sstevel@tonic-gate }
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate /*
9777c478bd9Sstevel@tonic-gate  * walk through the poll fd lists to see if they are identical. This is an
9787c478bd9Sstevel@tonic-gate  * expensive operation and should not be done more than once for each poll()
9797c478bd9Sstevel@tonic-gate  * call.
9807c478bd9Sstevel@tonic-gate  *
9817c478bd9Sstevel@tonic-gate  * As an optimization (i.e., not having to go through the lists more than
9827c478bd9Sstevel@tonic-gate  * once), this routine also clear the revents field of pollfd in 'current'.
9837c478bd9Sstevel@tonic-gate  * Zeroing out the revents field of each entry in current poll list is
9847c478bd9Sstevel@tonic-gate  * required by poll man page.
9857c478bd9Sstevel@tonic-gate  *
9867c478bd9Sstevel@tonic-gate  * Since the events field of cached list has illegal poll events filtered
9877c478bd9Sstevel@tonic-gate  * out, the current list applies the same filtering before comparison.
9887c478bd9Sstevel@tonic-gate  *
9897c478bd9Sstevel@tonic-gate  * The routine stops when it detects a meaningful difference, or when it
9907c478bd9Sstevel@tonic-gate  * exhausts the lists.
9917c478bd9Sstevel@tonic-gate  */
9927c478bd9Sstevel@tonic-gate int
9937c478bd9Sstevel@tonic-gate pcacheset_cmp(pollfd_t *current, pollfd_t *cached, pollfd_t *newlist, int n)
9947c478bd9Sstevel@tonic-gate {
9957c478bd9Sstevel@tonic-gate 	int    ix;
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	for (ix = 0; ix < n; ix++) {
9985c7544f7SDavid Plauger 		/* Prefetch 64 bytes worth of 8-byte elements */
9995c7544f7SDavid Plauger 		if ((ix & 0x7) == 0) {
1000*94e7edb1Slucy wang - Sun Microsystems - Beijing China 			prefetch_write_many((caddr_t)&current[ix + 8]);
1001*94e7edb1Slucy wang - Sun Microsystems - Beijing China 			prefetch_write_many((caddr_t)&cached[ix + 8]);
10025c7544f7SDavid Plauger 		}
10037c478bd9Sstevel@tonic-gate 		if (current[ix].fd == cached[ix].fd) {
10047c478bd9Sstevel@tonic-gate 			/*
10057c478bd9Sstevel@tonic-gate 			 * Filter out invalid poll events while we are in
10067c478bd9Sstevel@tonic-gate 			 * inside the loop.
10077c478bd9Sstevel@tonic-gate 			 */
10087c478bd9Sstevel@tonic-gate 			if (current[ix].events & ~VALID_POLL_EVENTS) {
10097c478bd9Sstevel@tonic-gate 				current[ix].events &= VALID_POLL_EVENTS;
10107c478bd9Sstevel@tonic-gate 				if (newlist != NULL)
10117c478bd9Sstevel@tonic-gate 					newlist[ix].events = current[ix].events;
10127c478bd9Sstevel@tonic-gate 			}
10137c478bd9Sstevel@tonic-gate 			if (current[ix].events == cached[ix].events) {
10147c478bd9Sstevel@tonic-gate 				current[ix].revents = 0;
10157c478bd9Sstevel@tonic-gate 				continue;
10167c478bd9Sstevel@tonic-gate 			}
10177c478bd9Sstevel@tonic-gate 		}
10187c478bd9Sstevel@tonic-gate 		if ((current[ix].fd < 0) && (cached[ix].fd < 0)) {
10197c478bd9Sstevel@tonic-gate 			current[ix].revents = 0;
10207c478bd9Sstevel@tonic-gate 			continue;
10217c478bd9Sstevel@tonic-gate 		}
10227c478bd9Sstevel@tonic-gate 		return (ix);
10237c478bd9Sstevel@tonic-gate 	}
10247c478bd9Sstevel@tonic-gate 	return (ix);
10257c478bd9Sstevel@tonic-gate }
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate /*
10287c478bd9Sstevel@tonic-gate  * This routine returns a pointer to a cached poll fd entry, or NULL if it
10297c478bd9Sstevel@tonic-gate  * does not find it in the hash table.
10307c478bd9Sstevel@tonic-gate  */
10317c478bd9Sstevel@tonic-gate polldat_t *
10327c478bd9Sstevel@tonic-gate pcache_lookup_fd(pollcache_t *pcp, int fd)
10337c478bd9Sstevel@tonic-gate {
10347c478bd9Sstevel@tonic-gate 	int hashindex;
10357c478bd9Sstevel@tonic-gate 	polldat_t *pdp;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	hashindex = POLLHASH(pcp->pc_hashsize, fd);
10387c478bd9Sstevel@tonic-gate 	pdp = pcp->pc_hash[hashindex];
10397c478bd9Sstevel@tonic-gate 	while (pdp != NULL) {
10407c478bd9Sstevel@tonic-gate 		if (pdp->pd_fd == fd)
10417c478bd9Sstevel@tonic-gate 			break;
10427c478bd9Sstevel@tonic-gate 		pdp = pdp->pd_hashnext;
10437c478bd9Sstevel@tonic-gate 	}
10447c478bd9Sstevel@tonic-gate 	return (pdp);
10457c478bd9Sstevel@tonic-gate }
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate polldat_t *
10487c478bd9Sstevel@tonic-gate pcache_alloc_fd(int nsets)
10497c478bd9Sstevel@tonic-gate {
10507c478bd9Sstevel@tonic-gate 	polldat_t *pdp;
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	pdp = kmem_zalloc(sizeof (polldat_t), KM_SLEEP);
10537c478bd9Sstevel@tonic-gate 	if (nsets > 0) {
10547c478bd9Sstevel@tonic-gate 		pdp->pd_ref = kmem_zalloc(sizeof (xref_t) * nsets, KM_SLEEP);
10557c478bd9Sstevel@tonic-gate 		pdp->pd_nsets = nsets;
10567c478bd9Sstevel@tonic-gate 	}
10577c478bd9Sstevel@tonic-gate 	return (pdp);
10587c478bd9Sstevel@tonic-gate }
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate /*
10617c478bd9Sstevel@tonic-gate  * This routine  inserts a polldat into the pollcache's hash table. It
10627c478bd9Sstevel@tonic-gate  * may be necessary to grow the size of the hash table.
10637c478bd9Sstevel@tonic-gate  */
10647c478bd9Sstevel@tonic-gate void
10657c478bd9Sstevel@tonic-gate pcache_insert_fd(pollcache_t *pcp, polldat_t *pdp, nfds_t nfds)
10667c478bd9Sstevel@tonic-gate {
10677c478bd9Sstevel@tonic-gate 	int hashindex;
10687c478bd9Sstevel@tonic-gate 	int fd;
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	if ((pcp->pc_fdcount > pcp->pc_hashsize * POLLHASHTHRESHOLD) ||
10717c478bd9Sstevel@tonic-gate 	    (nfds > pcp->pc_hashsize * POLLHASHTHRESHOLD)) {
10727c478bd9Sstevel@tonic-gate 		pcache_grow_hashtbl(pcp, nfds);
10737c478bd9Sstevel@tonic-gate 	}
10747c478bd9Sstevel@tonic-gate 	fd = pdp->pd_fd;
10757c478bd9Sstevel@tonic-gate 	hashindex = POLLHASH(pcp->pc_hashsize, fd);
10767c478bd9Sstevel@tonic-gate 	pdp->pd_hashnext = pcp->pc_hash[hashindex];
10777c478bd9Sstevel@tonic-gate 	pcp->pc_hash[hashindex] = pdp;
10787c478bd9Sstevel@tonic-gate 	pcp->pc_fdcount++;
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate #ifdef DEBUG
10817c478bd9Sstevel@tonic-gate 	{
10827c478bd9Sstevel@tonic-gate 		/*
10837c478bd9Sstevel@tonic-gate 		 * same fd should not appear on a hash list twice
10847c478bd9Sstevel@tonic-gate 		 */
10857c478bd9Sstevel@tonic-gate 		polldat_t *pdp1;
10867c478bd9Sstevel@tonic-gate 		for (pdp1 = pdp->pd_hashnext; pdp1; pdp1 = pdp1->pd_hashnext) {
10877c478bd9Sstevel@tonic-gate 			ASSERT(pdp->pd_fd != pdp1->pd_fd);
10887c478bd9Sstevel@tonic-gate 		}
10897c478bd9Sstevel@tonic-gate 	}
10907c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
10917c478bd9Sstevel@tonic-gate }
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate /*
10947c478bd9Sstevel@tonic-gate  * Grow the hash table -- either double the table size or round it to the
10957c478bd9Sstevel@tonic-gate  * nearest multiples of POLLHASHCHUNKSZ, whichever is bigger. Rehash all the
10967c478bd9Sstevel@tonic-gate  * elements on the hash table.
10977c478bd9Sstevel@tonic-gate  */
10987c478bd9Sstevel@tonic-gate void
10997c478bd9Sstevel@tonic-gate pcache_grow_hashtbl(pollcache_t *pcp, nfds_t nfds)
11007c478bd9Sstevel@tonic-gate {
11017c478bd9Sstevel@tonic-gate 	int	oldsize;
11027c478bd9Sstevel@tonic-gate 	polldat_t **oldtbl;
11037c478bd9Sstevel@tonic-gate 	polldat_t *pdp, *pdp1;
11047c478bd9Sstevel@tonic-gate 	int	i;
11057c478bd9Sstevel@tonic-gate #ifdef DEBUG
11067c478bd9Sstevel@tonic-gate 	int	count = 0;
11077c478bd9Sstevel@tonic-gate #endif
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	ASSERT(pcp->pc_hashsize % POLLHASHCHUNKSZ == 0);
11107c478bd9Sstevel@tonic-gate 	oldsize = pcp->pc_hashsize;
11117c478bd9Sstevel@tonic-gate 	oldtbl = pcp->pc_hash;
11127c478bd9Sstevel@tonic-gate 	if (nfds > pcp->pc_hashsize * POLLHASHINC) {
11137c478bd9Sstevel@tonic-gate 		pcp->pc_hashsize = (nfds + POLLHASHCHUNKSZ - 1) &
11147c478bd9Sstevel@tonic-gate 		    ~(POLLHASHCHUNKSZ - 1);
11157c478bd9Sstevel@tonic-gate 	} else {
11167c478bd9Sstevel@tonic-gate 		pcp->pc_hashsize = pcp->pc_hashsize * POLLHASHINC;
11177c478bd9Sstevel@tonic-gate 	}
11187c478bd9Sstevel@tonic-gate 	pcp->pc_hash = kmem_zalloc(pcp->pc_hashsize * sizeof (polldat_t *),
11197c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
11207c478bd9Sstevel@tonic-gate 	/*
11217c478bd9Sstevel@tonic-gate 	 * rehash existing elements
11227c478bd9Sstevel@tonic-gate 	 */
11237c478bd9Sstevel@tonic-gate 	pcp->pc_fdcount = 0;
11247c478bd9Sstevel@tonic-gate 	for (i = 0; i < oldsize; i++) {
11257c478bd9Sstevel@tonic-gate 		pdp = oldtbl[i];
11267c478bd9Sstevel@tonic-gate 		while (pdp != NULL) {
11277c478bd9Sstevel@tonic-gate 			pdp1 = pdp->pd_hashnext;
11287c478bd9Sstevel@tonic-gate 			pcache_insert_fd(pcp, pdp, nfds);
11297c478bd9Sstevel@tonic-gate 			pdp = pdp1;
11307c478bd9Sstevel@tonic-gate #ifdef DEBUG
11317c478bd9Sstevel@tonic-gate 			count++;
11327c478bd9Sstevel@tonic-gate #endif
11337c478bd9Sstevel@tonic-gate 		}
11347c478bd9Sstevel@tonic-gate 	}
11357c478bd9Sstevel@tonic-gate 	kmem_free(oldtbl, oldsize * sizeof (polldat_t *));
11367c478bd9Sstevel@tonic-gate 	ASSERT(pcp->pc_fdcount == count);
11377c478bd9Sstevel@tonic-gate }
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate void
11407c478bd9Sstevel@tonic-gate pcache_grow_map(pollcache_t *pcp, int fd)
11417c478bd9Sstevel@tonic-gate {
11427c478bd9Sstevel@tonic-gate 	int  	newsize;
11437c478bd9Sstevel@tonic-gate 	ulong_t	*newmap;
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 	/*
11467c478bd9Sstevel@tonic-gate 	 * grow to nearest multiple of POLLMAPCHUNK, assuming POLLMAPCHUNK is
11477c478bd9Sstevel@tonic-gate 	 * power of 2.
11487c478bd9Sstevel@tonic-gate 	 */
11497c478bd9Sstevel@tonic-gate 	newsize = (fd + POLLMAPCHUNK) & ~(POLLMAPCHUNK - 1);
11507c478bd9Sstevel@tonic-gate 	newmap = kmem_zalloc((newsize / BT_NBIPUL) * sizeof (ulong_t),
11517c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
11527c478bd9Sstevel@tonic-gate 	/*
11537c478bd9Sstevel@tonic-gate 	 * don't want pollwakeup to set a bit while growing the bitmap.
11547c478bd9Sstevel@tonic-gate 	 */
11557c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&pcp->pc_lock) == 0);
11567c478bd9Sstevel@tonic-gate 	mutex_enter(&pcp->pc_lock);
11577c478bd9Sstevel@tonic-gate 	bcopy(pcp->pc_bitmap, newmap,
11587c478bd9Sstevel@tonic-gate 	    (pcp->pc_mapsize / BT_NBIPUL) * sizeof (ulong_t));
11597c478bd9Sstevel@tonic-gate 	kmem_free(pcp->pc_bitmap,
11607c478bd9Sstevel@tonic-gate 	    (pcp->pc_mapsize /BT_NBIPUL) * sizeof (ulong_t));
11617c478bd9Sstevel@tonic-gate 	pcp->pc_bitmap = newmap;
11627c478bd9Sstevel@tonic-gate 	pcp->pc_mapsize = newsize;
11637c478bd9Sstevel@tonic-gate 	mutex_exit(&pcp->pc_lock);
11647c478bd9Sstevel@tonic-gate }
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate /*
11677c478bd9Sstevel@tonic-gate  * remove all the reference from pollhead list and fpollinfo lists.
11687c478bd9Sstevel@tonic-gate  */
11697c478bd9Sstevel@tonic-gate void
11707c478bd9Sstevel@tonic-gate pcache_clean(pollcache_t *pcp)
11717c478bd9Sstevel@tonic-gate {
11727c478bd9Sstevel@tonic-gate 	int i;
11737c478bd9Sstevel@tonic-gate 	polldat_t **hashtbl;
11747c478bd9Sstevel@tonic-gate 	polldat_t *pdp;
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&curthread->t_pollstate->ps_lock));
11777c478bd9Sstevel@tonic-gate 	hashtbl = pcp->pc_hash;
11787c478bd9Sstevel@tonic-gate 	for (i = 0; i < pcp->pc_hashsize; i++) {
11797c478bd9Sstevel@tonic-gate 		for (pdp = hashtbl[i]; pdp; pdp = pdp->pd_hashnext) {
11807c478bd9Sstevel@tonic-gate 			if (pdp->pd_php != NULL) {
11817c478bd9Sstevel@tonic-gate 				pollhead_delete(pdp->pd_php, pdp);
11827c478bd9Sstevel@tonic-gate 				pdp->pd_php = NULL;
11837c478bd9Sstevel@tonic-gate 			}
11847c478bd9Sstevel@tonic-gate 			if (pdp->pd_fp != NULL) {
11857c478bd9Sstevel@tonic-gate 				delfpollinfo(pdp->pd_fd);
11867c478bd9Sstevel@tonic-gate 				pdp->pd_fp = NULL;
11877c478bd9Sstevel@tonic-gate 			}
11887c478bd9Sstevel@tonic-gate 		}
11897c478bd9Sstevel@tonic-gate 	}
11907c478bd9Sstevel@tonic-gate }
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate void
11937c478bd9Sstevel@tonic-gate pcacheset_invalidate(pollstate_t *ps, polldat_t *pdp)
11947c478bd9Sstevel@tonic-gate {
11957c478bd9Sstevel@tonic-gate 	int 	i;
11967c478bd9Sstevel@tonic-gate 	int	fd = pdp->pd_fd;
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	/*
11997c478bd9Sstevel@tonic-gate 	 * we come here because an earlier close() on this cached poll fd.
12007c478bd9Sstevel@tonic-gate 	 */
12017c478bd9Sstevel@tonic-gate 	ASSERT(pdp->pd_fp == NULL);
12027c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ps->ps_lock));
12037c478bd9Sstevel@tonic-gate 	pdp->pd_events = 0;
12047c478bd9Sstevel@tonic-gate 	for (i = 0; i < ps->ps_nsets; i++) {
12057c478bd9Sstevel@tonic-gate 		xref_t		*refp;
12067c478bd9Sstevel@tonic-gate 		pollcacheset_t	*pcsp;
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 		ASSERT(pdp->pd_ref != NULL);
12097c478bd9Sstevel@tonic-gate 		refp = &pdp->pd_ref[i];
12107c478bd9Sstevel@tonic-gate 		if (refp->xf_refcnt) {
12117c478bd9Sstevel@tonic-gate 			ASSERT(refp->xf_position >= 0);
12127c478bd9Sstevel@tonic-gate 			pcsp = &ps->ps_pcacheset[i];
12137c478bd9Sstevel@tonic-gate 			if (refp->xf_refcnt == 1) {
12147c478bd9Sstevel@tonic-gate 				pcsp->pcs_pollfd[refp->xf_position].fd = -1;
12157c478bd9Sstevel@tonic-gate 				refp->xf_refcnt = 0;
12167c478bd9Sstevel@tonic-gate 				pdp->pd_count--;
12177c478bd9Sstevel@tonic-gate 			} else if (refp->xf_refcnt > 1) {
12187c478bd9Sstevel@tonic-gate 				int	j;
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 				/*
12217c478bd9Sstevel@tonic-gate 				 * turn off every appearance in pcs_pollfd list
12227c478bd9Sstevel@tonic-gate 				 */
12237c478bd9Sstevel@tonic-gate 				for (j = refp->xf_position;
12247c478bd9Sstevel@tonic-gate 				    j < pcsp->pcs_nfds; j++) {
12257c478bd9Sstevel@tonic-gate 					if (pcsp->pcs_pollfd[j].fd == fd) {
12267c478bd9Sstevel@tonic-gate 						pcsp->pcs_pollfd[j].fd = -1;
12277c478bd9Sstevel@tonic-gate 						refp->xf_refcnt--;
12287c478bd9Sstevel@tonic-gate 						pdp->pd_count--;
12297c478bd9Sstevel@tonic-gate 					}
12307c478bd9Sstevel@tonic-gate 				}
12317c478bd9Sstevel@tonic-gate 			}
12327c478bd9Sstevel@tonic-gate 			ASSERT(refp->xf_refcnt == 0);
12337c478bd9Sstevel@tonic-gate 			refp->xf_position = POLLPOSINVAL;
12347c478bd9Sstevel@tonic-gate 		}
12357c478bd9Sstevel@tonic-gate 	}
12367c478bd9Sstevel@tonic-gate 	ASSERT(pdp->pd_count == 0);
12377c478bd9Sstevel@tonic-gate }
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate /*
12407c478bd9Sstevel@tonic-gate  * Insert poll fd into the pollcache, and add poll registration.
12417c478bd9Sstevel@tonic-gate  * This routine is called after getf() and before releasef(). So the vnode
12427c478bd9Sstevel@tonic-gate  * can not disappear even if we block here.
12437c478bd9Sstevel@tonic-gate  * If there is an error, the polled fd is not cached.
12447c478bd9Sstevel@tonic-gate  */
12457c478bd9Sstevel@tonic-gate int
12467c478bd9Sstevel@tonic-gate pcache_insert(pollstate_t *ps, file_t *fp, pollfd_t *pollfdp, int *fdcntp,
12477c478bd9Sstevel@tonic-gate     ssize_t pos, int which)
12487c478bd9Sstevel@tonic-gate {
12497c478bd9Sstevel@tonic-gate 	pollcache_t	*pcp = ps->ps_pcache;
12507c478bd9Sstevel@tonic-gate 	polldat_t	*pdp;
12517c478bd9Sstevel@tonic-gate 	int		error;
12527c478bd9Sstevel@tonic-gate 	int		fd;
12537c478bd9Sstevel@tonic-gate 	pollhead_t	*memphp = NULL;
12547c478bd9Sstevel@tonic-gate 	xref_t		*refp;
12557c478bd9Sstevel@tonic-gate 	int		newpollfd = 0;
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ps->ps_lock));
12587c478bd9Sstevel@tonic-gate 	/*
12597c478bd9Sstevel@tonic-gate 	 * The poll caching uses the existing VOP_POLL interface. If there
12607c478bd9Sstevel@tonic-gate 	 * is no polled events, we want the polled device to set its "some
12617c478bd9Sstevel@tonic-gate 	 * one is sleeping in poll" flag. When the polled events happen
12627c478bd9Sstevel@tonic-gate 	 * later, the driver will call pollwakeup(). We achieve this by
12637c478bd9Sstevel@tonic-gate 	 * always passing 0 in the third parameter ("anyyet") when calling
12647c478bd9Sstevel@tonic-gate 	 * VOP_POLL. This parameter is not looked at by drivers when the
12657c478bd9Sstevel@tonic-gate 	 * polled events exist. If a driver chooses to ignore this parameter
12667c478bd9Sstevel@tonic-gate 	 * and call pollwakeup whenever the polled events happen, that will
12677c478bd9Sstevel@tonic-gate 	 * be OK too.
12687c478bd9Sstevel@tonic-gate 	 */
12697c478bd9Sstevel@tonic-gate 	ASSERT(curthread->t_pollcache == NULL);
12707c478bd9Sstevel@tonic-gate 	error = VOP_POLL(fp->f_vnode, pollfdp->events, 0, &pollfdp->revents,
1271da6c28aaSamw 	    &memphp, NULL);
12727c478bd9Sstevel@tonic-gate 	if (error) {
12737c478bd9Sstevel@tonic-gate 		return (error);
12747c478bd9Sstevel@tonic-gate 	}
12757c478bd9Sstevel@tonic-gate 	if (pollfdp->revents) {
12767c478bd9Sstevel@tonic-gate 		(*fdcntp)++;
12777c478bd9Sstevel@tonic-gate 	}
12787c478bd9Sstevel@tonic-gate 	/*
12797c478bd9Sstevel@tonic-gate 	 * polling the underlying device succeeded. Now we can cache it.
12807c478bd9Sstevel@tonic-gate 	 * A close can't come in here because we have not done a releasef()
12817c478bd9Sstevel@tonic-gate 	 * yet.
12827c478bd9Sstevel@tonic-gate 	 */
12837c478bd9Sstevel@tonic-gate 	fd = pollfdp->fd;
12847c478bd9Sstevel@tonic-gate 	pdp = pcache_lookup_fd(pcp, fd);
12857c478bd9Sstevel@tonic-gate 	if (pdp == NULL) {
12867c478bd9Sstevel@tonic-gate 		ASSERT(ps->ps_nsets > 0);
12877c478bd9Sstevel@tonic-gate 		pdp = pcache_alloc_fd(ps->ps_nsets);
12887c478bd9Sstevel@tonic-gate 		newpollfd = 1;
12897c478bd9Sstevel@tonic-gate 	}
12907c478bd9Sstevel@tonic-gate 	/*
12917c478bd9Sstevel@tonic-gate 	 * If this entry was used to cache a poll fd which was closed, and
12927c478bd9Sstevel@tonic-gate 	 * this entry has not been cleaned, do it now.
12937c478bd9Sstevel@tonic-gate 	 */
12947c478bd9Sstevel@tonic-gate 	if ((pdp->pd_count > 0) && (pdp->pd_fp == NULL)) {
12957c478bd9Sstevel@tonic-gate 		pcacheset_invalidate(ps, pdp);
12967c478bd9Sstevel@tonic-gate 		ASSERT(pdp->pd_next == NULL);
12977c478bd9Sstevel@tonic-gate 	}
12987c478bd9Sstevel@tonic-gate 	if (pdp->pd_count == 0) {
12997c478bd9Sstevel@tonic-gate 		pdp->pd_fd = fd;
13007c478bd9Sstevel@tonic-gate 		pdp->pd_fp = fp;
13017c478bd9Sstevel@tonic-gate 		addfpollinfo(fd);
13027c478bd9Sstevel@tonic-gate 		pdp->pd_thread = curthread;
13037c478bd9Sstevel@tonic-gate 		pdp->pd_pcache = pcp;
13047c478bd9Sstevel@tonic-gate 		/*
13057c478bd9Sstevel@tonic-gate 		 * the entry is never used or cleared by removing a cached
13067c478bd9Sstevel@tonic-gate 		 * pollfd (pcache_delete_fd). So all the fields should be clear.
13077c478bd9Sstevel@tonic-gate 		 */
13087c478bd9Sstevel@tonic-gate 		ASSERT(pdp->pd_next == NULL);
13097c478bd9Sstevel@tonic-gate 	}
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 	/*
13127c478bd9Sstevel@tonic-gate 	 * A polled fd is considered cached. So there should be a fpollinfo
13137c478bd9Sstevel@tonic-gate 	 * entry on uf_fpollinfo list.
13147c478bd9Sstevel@tonic-gate 	 */
13157c478bd9Sstevel@tonic-gate 	ASSERT(infpollinfo(fd));
13167c478bd9Sstevel@tonic-gate 	/*
13177c478bd9Sstevel@tonic-gate 	 * If there is an inconsistency, we want to know it here.
13187c478bd9Sstevel@tonic-gate 	 */
13197c478bd9Sstevel@tonic-gate 	ASSERT(pdp->pd_fp == fp);
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	/*
13227c478bd9Sstevel@tonic-gate 	 * XXX pd_events is a union of all polled events on this fd, possibly
13237c478bd9Sstevel@tonic-gate 	 * by different threads. Unless this is a new first poll(), pd_events
13247c478bd9Sstevel@tonic-gate 	 * never shrinks. If an event is no longer polled by a process, there
13257c478bd9Sstevel@tonic-gate 	 * is no way to cancel that event. In that case, poll degrade to its
13267c478bd9Sstevel@tonic-gate 	 * old form -- polling on this fd every time poll() is called. The
13277c478bd9Sstevel@tonic-gate 	 * assumption is an app always polls the same type of events.
13287c478bd9Sstevel@tonic-gate 	 */
13297c478bd9Sstevel@tonic-gate 	pdp->pd_events |= pollfdp->events;
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	pdp->pd_count++;
13327c478bd9Sstevel@tonic-gate 	/*
13337c478bd9Sstevel@tonic-gate 	 * There is not much special handling for multiple appearances of
13347c478bd9Sstevel@tonic-gate 	 * same fd other than xf_position always recording the first
13357c478bd9Sstevel@tonic-gate 	 * appearance in poll list. If this is called from pcacheset_cache_list,
13367c478bd9Sstevel@tonic-gate 	 * a VOP_POLL is called on every pollfd entry; therefore each
13377c478bd9Sstevel@tonic-gate 	 * revents and fdcnt should be set correctly. If this is called from
13387c478bd9Sstevel@tonic-gate 	 * pcacheset_resolve, we don't care about fdcnt here. Pollreadmap will
13397c478bd9Sstevel@tonic-gate 	 * pick up the right count and handle revents field of each pollfd
13407c478bd9Sstevel@tonic-gate 	 * entry.
13417c478bd9Sstevel@tonic-gate 	 */
13427c478bd9Sstevel@tonic-gate 	ASSERT(pdp->pd_ref != NULL);
13437c478bd9Sstevel@tonic-gate 	refp = &pdp->pd_ref[which];
13447c478bd9Sstevel@tonic-gate 	if (refp->xf_refcnt == 0) {
13457c478bd9Sstevel@tonic-gate 		refp->xf_position = pos;
13467c478bd9Sstevel@tonic-gate 	} else {
13477c478bd9Sstevel@tonic-gate 		/*
13487c478bd9Sstevel@tonic-gate 		 * xf_position records the fd's first appearance in poll list
13497c478bd9Sstevel@tonic-gate 		 */
13507c478bd9Sstevel@tonic-gate 		if (pos < refp->xf_position) {
13517c478bd9Sstevel@tonic-gate 			refp->xf_position = pos;
13527c478bd9Sstevel@tonic-gate 		}
13537c478bd9Sstevel@tonic-gate 	}
13547c478bd9Sstevel@tonic-gate 	ASSERT(pollfdp->fd == ps->ps_pollfd[refp->xf_position].fd);
13557c478bd9Sstevel@tonic-gate 	refp->xf_refcnt++;
13567c478bd9Sstevel@tonic-gate 	if (fd >= pcp->pc_mapsize) {
13577c478bd9Sstevel@tonic-gate 		pcache_grow_map(pcp, fd);
13587c478bd9Sstevel@tonic-gate 	}
13597c478bd9Sstevel@tonic-gate 	if (fd > pcp->pc_mapend) {
13607c478bd9Sstevel@tonic-gate 		pcp->pc_mapend = fd;
13617c478bd9Sstevel@tonic-gate 	}
13627c478bd9Sstevel@tonic-gate 	if (newpollfd != 0) {
13637c478bd9Sstevel@tonic-gate 		pcache_insert_fd(ps->ps_pcache, pdp, ps->ps_nfds);
13647c478bd9Sstevel@tonic-gate 	}
13657c478bd9Sstevel@tonic-gate 	if (memphp) {
13667c478bd9Sstevel@tonic-gate 		if (pdp->pd_php == NULL) {
13677c478bd9Sstevel@tonic-gate 			pollhead_insert(memphp, pdp);
13687c478bd9Sstevel@tonic-gate 			pdp->pd_php = memphp;
13697c478bd9Sstevel@tonic-gate 		} else {
13707c478bd9Sstevel@tonic-gate 			if (memphp != pdp->pd_php) {
13717c478bd9Sstevel@tonic-gate 				/*
13727c478bd9Sstevel@tonic-gate 				 * layered devices (e.g. console driver)
13737c478bd9Sstevel@tonic-gate 				 * may change the vnode and thus the pollhead
13747c478bd9Sstevel@tonic-gate 				 * pointer out from underneath us.
13757c478bd9Sstevel@tonic-gate 				 */
13767c478bd9Sstevel@tonic-gate 				pollhead_delete(pdp->pd_php, pdp);
13777c478bd9Sstevel@tonic-gate 				pollhead_insert(memphp, pdp);
13787c478bd9Sstevel@tonic-gate 				pdp->pd_php = memphp;
13797c478bd9Sstevel@tonic-gate 			}
13807c478bd9Sstevel@tonic-gate 		}
13817c478bd9Sstevel@tonic-gate 	}
13827c478bd9Sstevel@tonic-gate 	/*
13837c478bd9Sstevel@tonic-gate 	 * Since there is a considerable window between VOP_POLL and when
13847c478bd9Sstevel@tonic-gate 	 * we actually put the polldat struct on the pollhead list, we could
13857c478bd9Sstevel@tonic-gate 	 * miss a pollwakeup. In the case of polling additional events, we
13867c478bd9Sstevel@tonic-gate 	 * don't update the events until after VOP_POLL. So we could miss
13877c478bd9Sstevel@tonic-gate 	 * pollwakeup there too. So we always set the bit here just to be
13887c478bd9Sstevel@tonic-gate 	 * safe. The real performance gain is in subsequent pcache_poll.
13897c478bd9Sstevel@tonic-gate 	 */
13907c478bd9Sstevel@tonic-gate 	mutex_enter(&pcp->pc_lock);
13917c478bd9Sstevel@tonic-gate 	BT_SET(pcp->pc_bitmap, fd);
13927c478bd9Sstevel@tonic-gate 	mutex_exit(&pcp->pc_lock);
13937c478bd9Sstevel@tonic-gate 	return (0);
13947c478bd9Sstevel@tonic-gate }
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate /*
13977c478bd9Sstevel@tonic-gate  * The entry is not really deleted. The fields are cleared so that the
13987c478bd9Sstevel@tonic-gate  * entry is no longer useful, but it will remain in the hash table for reuse
13997c478bd9Sstevel@tonic-gate  * later. It will be freed when the polling lwp exits.
14007c478bd9Sstevel@tonic-gate  */
14017c478bd9Sstevel@tonic-gate int
14027c478bd9Sstevel@tonic-gate pcache_delete_fd(pollstate_t *ps, int fd, size_t pos, int which, uint_t cevent)
14037c478bd9Sstevel@tonic-gate {
14047c478bd9Sstevel@tonic-gate 	pollcache_t	*pcp = ps->ps_pcache;
14057c478bd9Sstevel@tonic-gate 	polldat_t	*pdp;
14067c478bd9Sstevel@tonic-gate 	xref_t		*refp;
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 	ASSERT(fd < pcp->pc_mapsize);
14097c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ps->ps_lock));
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 	pdp = pcache_lookup_fd(pcp, fd);
14127c478bd9Sstevel@tonic-gate 	ASSERT(pdp != NULL);
14137c478bd9Sstevel@tonic-gate 	ASSERT(pdp->pd_count > 0);
14147c478bd9Sstevel@tonic-gate 	ASSERT(pdp->pd_ref != NULL);
14157c478bd9Sstevel@tonic-gate 	refp = &pdp->pd_ref[which];
14167c478bd9Sstevel@tonic-gate 	if (pdp->pd_count == 1) {
14177c478bd9Sstevel@tonic-gate 		pdp->pd_events = 0;
14187c478bd9Sstevel@tonic-gate 		refp->xf_position = POLLPOSINVAL;
14197c478bd9Sstevel@tonic-gate 		ASSERT(refp->xf_refcnt == 1);
14207c478bd9Sstevel@tonic-gate 		refp->xf_refcnt = 0;
14217c478bd9Sstevel@tonic-gate 		if (pdp->pd_php) {
14227c478bd9Sstevel@tonic-gate 			/*
14237c478bd9Sstevel@tonic-gate 			 * It is possible for a wakeup thread to get ahead
14247c478bd9Sstevel@tonic-gate 			 * of the following pollhead_delete and set the bit in
14257c478bd9Sstevel@tonic-gate 			 * bitmap.  It is OK because the bit will be cleared
14267c478bd9Sstevel@tonic-gate 			 * here anyway.
14277c478bd9Sstevel@tonic-gate 			 */
14287c478bd9Sstevel@tonic-gate 			pollhead_delete(pdp->pd_php, pdp);
14297c478bd9Sstevel@tonic-gate 			pdp->pd_php = NULL;
14307c478bd9Sstevel@tonic-gate 		}
14317c478bd9Sstevel@tonic-gate 		pdp->pd_count = 0;
14327c478bd9Sstevel@tonic-gate 		if (pdp->pd_fp != NULL) {
14337c478bd9Sstevel@tonic-gate 			pdp->pd_fp = NULL;
14347c478bd9Sstevel@tonic-gate 			delfpollinfo(fd);
14357c478bd9Sstevel@tonic-gate 		}
14367c478bd9Sstevel@tonic-gate 		mutex_enter(&pcp->pc_lock);
14377c478bd9Sstevel@tonic-gate 		BT_CLEAR(pcp->pc_bitmap, fd);
14387c478bd9Sstevel@tonic-gate 		mutex_exit(&pcp->pc_lock);
14397c478bd9Sstevel@tonic-gate 		return (0);
14407c478bd9Sstevel@tonic-gate 	}
14417c478bd9Sstevel@tonic-gate 	if ((cevent & POLLCLOSED) == POLLCLOSED) {
14427c478bd9Sstevel@tonic-gate 		/*
14437c478bd9Sstevel@tonic-gate 		 * fd cached here has been closed. This is the first
14447c478bd9Sstevel@tonic-gate 		 * pcache_delete_fd called after the close. Clean up the
14457c478bd9Sstevel@tonic-gate 		 * entire entry.
14467c478bd9Sstevel@tonic-gate 		 */
14477c478bd9Sstevel@tonic-gate 		pcacheset_invalidate(ps, pdp);
14487c478bd9Sstevel@tonic-gate 		ASSERT(pdp->pd_php == NULL);
14497c478bd9Sstevel@tonic-gate 		mutex_enter(&pcp->pc_lock);
14507c478bd9Sstevel@tonic-gate 		BT_CLEAR(pcp->pc_bitmap, fd);
14517c478bd9Sstevel@tonic-gate 		mutex_exit(&pcp->pc_lock);
14527c478bd9Sstevel@tonic-gate 		return (0);
14537c478bd9Sstevel@tonic-gate 	}
14547c478bd9Sstevel@tonic-gate #ifdef DEBUG
14557c478bd9Sstevel@tonic-gate 	if (getf(fd) != NULL) {
14567c478bd9Sstevel@tonic-gate 		ASSERT(infpollinfo(fd));
14577c478bd9Sstevel@tonic-gate 		releasef(fd);
14587c478bd9Sstevel@tonic-gate 	}
14597c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
14607c478bd9Sstevel@tonic-gate 	pdp->pd_count--;
14617c478bd9Sstevel@tonic-gate 	ASSERT(refp->xf_refcnt > 0);
14627c478bd9Sstevel@tonic-gate 	if (--refp->xf_refcnt == 0) {
14637c478bd9Sstevel@tonic-gate 		refp->xf_position = POLLPOSINVAL;
14647c478bd9Sstevel@tonic-gate 	} else {
14657c478bd9Sstevel@tonic-gate 		ASSERT(pos >= refp->xf_position);
14667c478bd9Sstevel@tonic-gate 		if (pos == refp->xf_position) {
14677c478bd9Sstevel@tonic-gate 			/*
14687c478bd9Sstevel@tonic-gate 			 * The xref position is no longer valid.
14697c478bd9Sstevel@tonic-gate 			 * Reset it to a special value and let
14707c478bd9Sstevel@tonic-gate 			 * caller know it needs to updatexref()
14717c478bd9Sstevel@tonic-gate 			 * with a new xf_position value.
14727c478bd9Sstevel@tonic-gate 			 */
14737c478bd9Sstevel@tonic-gate 			refp->xf_position = POLLPOSTRANS;
14747c478bd9Sstevel@tonic-gate 			return (1);
14757c478bd9Sstevel@tonic-gate 		}
14767c478bd9Sstevel@tonic-gate 	}
14777c478bd9Sstevel@tonic-gate 	return (0);
14787c478bd9Sstevel@tonic-gate }
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate void
14817c478bd9Sstevel@tonic-gate pcache_update_xref(pollcache_t *pcp, int fd, ssize_t pos, int which)
14827c478bd9Sstevel@tonic-gate {
14837c478bd9Sstevel@tonic-gate 	polldat_t	*pdp;
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 	pdp = pcache_lookup_fd(pcp, fd);
14867c478bd9Sstevel@tonic-gate 	ASSERT(pdp != NULL);
14877c478bd9Sstevel@tonic-gate 	ASSERT(pdp->pd_ref != NULL);
14887c478bd9Sstevel@tonic-gate 	pdp->pd_ref[which].xf_position = pos;
14897c478bd9Sstevel@tonic-gate }
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate #ifdef DEBUG
14927c478bd9Sstevel@tonic-gate /*
14937c478bd9Sstevel@tonic-gate  * For each polled fd, it's either in the bitmap or cached in
14947c478bd9Sstevel@tonic-gate  * pcache hash table. If this routine returns 0, something is wrong.
14957c478bd9Sstevel@tonic-gate  */
14967c478bd9Sstevel@tonic-gate static int
14977c478bd9Sstevel@tonic-gate pollchecksanity(pollstate_t *ps, nfds_t nfds)
14987c478bd9Sstevel@tonic-gate {
14997c478bd9Sstevel@tonic-gate 	int    		i;
15007c478bd9Sstevel@tonic-gate 	int		fd;
15017c478bd9Sstevel@tonic-gate 	pollcache_t	*pcp = ps->ps_pcache;
15027c478bd9Sstevel@tonic-gate 	polldat_t	*pdp;
15037c478bd9Sstevel@tonic-gate 	pollfd_t	*pollfdp = ps->ps_pollfd;
15047c478bd9Sstevel@tonic-gate 	file_t		*fp;
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ps->ps_lock));
15077c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfds; i++) {
15087c478bd9Sstevel@tonic-gate 		fd = pollfdp[i].fd;
15097c478bd9Sstevel@tonic-gate 		if (fd < 0) {
15107c478bd9Sstevel@tonic-gate 			ASSERT(pollfdp[i].revents == 0);
15117c478bd9Sstevel@tonic-gate 			continue;
15127c478bd9Sstevel@tonic-gate 		}
15137c478bd9Sstevel@tonic-gate 		if (pollfdp[i].revents == POLLNVAL)
15147c478bd9Sstevel@tonic-gate 			continue;
15157c478bd9Sstevel@tonic-gate 		if ((fp = getf(fd)) == NULL)
15167c478bd9Sstevel@tonic-gate 			continue;
15177c478bd9Sstevel@tonic-gate 		pdp = pcache_lookup_fd(pcp, fd);
15187c478bd9Sstevel@tonic-gate 		ASSERT(pdp != NULL);
15197c478bd9Sstevel@tonic-gate 		ASSERT(infpollinfo(fd));
15207c478bd9Sstevel@tonic-gate 		ASSERT(pdp->pd_fp == fp);
15217c478bd9Sstevel@tonic-gate 		releasef(fd);
15227c478bd9Sstevel@tonic-gate 		if (BT_TEST(pcp->pc_bitmap, fd))
15237c478bd9Sstevel@tonic-gate 			continue;
15247c478bd9Sstevel@tonic-gate 		if (pdp->pd_php == NULL)
15257c478bd9Sstevel@tonic-gate 			return (0);
15267c478bd9Sstevel@tonic-gate 	}
15277c478bd9Sstevel@tonic-gate 	return (1);
15287c478bd9Sstevel@tonic-gate }
15297c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate /*
15327c478bd9Sstevel@tonic-gate  * resolve the difference between the current poll list and a cached one.
15337c478bd9Sstevel@tonic-gate  */
15347c478bd9Sstevel@tonic-gate int
15357c478bd9Sstevel@tonic-gate pcacheset_resolve(pollstate_t *ps, nfds_t nfds, int *fdcntp, int which)
15367c478bd9Sstevel@tonic-gate {
15377c478bd9Sstevel@tonic-gate 	int    		i;
15387c478bd9Sstevel@tonic-gate 	pollcache_t	*pcp = ps->ps_pcache;
15397c478bd9Sstevel@tonic-gate 	pollfd_t	*newlist = NULL;
15407c478bd9Sstevel@tonic-gate 	pollfd_t	*current = ps->ps_pollfd;
15417c478bd9Sstevel@tonic-gate 	pollfd_t	*cached;
15427c478bd9Sstevel@tonic-gate 	pollcacheset_t	*pcsp;
15437c478bd9Sstevel@tonic-gate 	int		common;
15447c478bd9Sstevel@tonic-gate 	int		count = 0;
15457c478bd9Sstevel@tonic-gate 	int		offset;
15467c478bd9Sstevel@tonic-gate 	int		remain;
15477c478bd9Sstevel@tonic-gate 	int		fd;
15487c478bd9Sstevel@tonic-gate 	file_t		*fp;
15497c478bd9Sstevel@tonic-gate 	int		fdcnt = 0;
15507c478bd9Sstevel@tonic-gate 	int		cnt = 0;
15517c478bd9Sstevel@tonic-gate 	nfds_t		old_nfds;
15527c478bd9Sstevel@tonic-gate 	int		error = 0;
15537c478bd9Sstevel@tonic-gate 	int		mismatch = 0;
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ps->ps_lock));
15567c478bd9Sstevel@tonic-gate #ifdef DEBUG
15577c478bd9Sstevel@tonic-gate 	checkpolldat(ps);
15587c478bd9Sstevel@tonic-gate #endif
15597c478bd9Sstevel@tonic-gate 	pcsp = &ps->ps_pcacheset[which];
15607c478bd9Sstevel@tonic-gate 	old_nfds = pcsp->pcs_nfds;
15617c478bd9Sstevel@tonic-gate 	common = (nfds > old_nfds) ? old_nfds : nfds;
15627c478bd9Sstevel@tonic-gate 	if (nfds != old_nfds) {
15637c478bd9Sstevel@tonic-gate 		/*
15647c478bd9Sstevel@tonic-gate 		 * the length of poll list has changed. allocate a new
15657c478bd9Sstevel@tonic-gate 		 * pollfd list.
15667c478bd9Sstevel@tonic-gate 		 */
15677c478bd9Sstevel@tonic-gate 		newlist = kmem_alloc(nfds * sizeof (pollfd_t), KM_SLEEP);
15687c478bd9Sstevel@tonic-gate 		bcopy(current, newlist, sizeof (pollfd_t) * nfds);
15697c478bd9Sstevel@tonic-gate 	}
15707c478bd9Sstevel@tonic-gate 	/*
15717c478bd9Sstevel@tonic-gate 	 * Compare the overlapping part of the current fd list with the
15727c478bd9Sstevel@tonic-gate 	 * cached one. Whenever a difference is found, resolve it.
15737c478bd9Sstevel@tonic-gate 	 * The comparison is done on the current poll list and the
15747c478bd9Sstevel@tonic-gate 	 * cached list. But we may be setting up the newlist to be the
15757c478bd9Sstevel@tonic-gate 	 * cached list for next poll.
15767c478bd9Sstevel@tonic-gate 	 */
15777c478bd9Sstevel@tonic-gate 	cached = pcsp->pcs_pollfd;
15787c478bd9Sstevel@tonic-gate 	remain = common;
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 	while (count < common) {
15817c478bd9Sstevel@tonic-gate 		int	tmpfd;
15827c478bd9Sstevel@tonic-gate 		pollfd_t *np;
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 		np = (newlist != NULL) ? &newlist[count] : NULL;
15857c478bd9Sstevel@tonic-gate 		offset = pcacheset_cmp(&current[count], &cached[count], np,
15867c478bd9Sstevel@tonic-gate 		    remain);
15877c478bd9Sstevel@tonic-gate 		/*
15887c478bd9Sstevel@tonic-gate 		 * Collect stats. If lists are completed the first time,
15897c478bd9Sstevel@tonic-gate 		 * it's a hit. Otherwise, it's a partial hit or miss.
15907c478bd9Sstevel@tonic-gate 		 */
15917c478bd9Sstevel@tonic-gate 		if ((count == 0) && (offset == common)) {
15927c478bd9Sstevel@tonic-gate 			pollstats.pollcachehit.value.ui64++;
15937c478bd9Sstevel@tonic-gate 		} else {
15947c478bd9Sstevel@tonic-gate 			mismatch++;
15957c478bd9Sstevel@tonic-gate 		}
15967c478bd9Sstevel@tonic-gate 		count += offset;
15977c478bd9Sstevel@tonic-gate 		if (offset < remain) {
15987c478bd9Sstevel@tonic-gate 			ASSERT(count < common);
15997c478bd9Sstevel@tonic-gate 			ASSERT((current[count].fd != cached[count].fd) ||
16007c478bd9Sstevel@tonic-gate 			    (current[count].events != cached[count].events));
16017c478bd9Sstevel@tonic-gate 			/*
16027c478bd9Sstevel@tonic-gate 			 * Filter out invalid events.
16037c478bd9Sstevel@tonic-gate 			 */
16047c478bd9Sstevel@tonic-gate 			if (current[count].events & ~VALID_POLL_EVENTS) {
16057c478bd9Sstevel@tonic-gate 				if (newlist != NULL) {
16067c478bd9Sstevel@tonic-gate 					newlist[count].events =
16077c478bd9Sstevel@tonic-gate 					    current[count].events &=
16087c478bd9Sstevel@tonic-gate 					    VALID_POLL_EVENTS;
16097c478bd9Sstevel@tonic-gate 				} else {
16107c478bd9Sstevel@tonic-gate 					current[count].events &=
16117c478bd9Sstevel@tonic-gate 					    VALID_POLL_EVENTS;
16127c478bd9Sstevel@tonic-gate 				}
16137c478bd9Sstevel@tonic-gate 			}
16147c478bd9Sstevel@tonic-gate 			/*
16157c478bd9Sstevel@tonic-gate 			 * when resolving a difference, we always remove the
16167c478bd9Sstevel@tonic-gate 			 * fd from cache before inserting one into cache.
16177c478bd9Sstevel@tonic-gate 			 */
16187c478bd9Sstevel@tonic-gate 			if (cached[count].fd >= 0) {
16197c478bd9Sstevel@tonic-gate 				tmpfd = cached[count].fd;
16207c478bd9Sstevel@tonic-gate 				if (pcache_delete_fd(ps, tmpfd, count, which,
16217c478bd9Sstevel@tonic-gate 				    (uint_t)cached[count].events)) {
16227c478bd9Sstevel@tonic-gate 					/*
16237c478bd9Sstevel@tonic-gate 					 * This should be rare but needed for
16247c478bd9Sstevel@tonic-gate 					 * correctness.
16257c478bd9Sstevel@tonic-gate 					 *
16267c478bd9Sstevel@tonic-gate 					 * The first appearance in cached list
16277c478bd9Sstevel@tonic-gate 					 * is being "turned off". The same fd
16287c478bd9Sstevel@tonic-gate 					 * appear more than once in the cached
16297c478bd9Sstevel@tonic-gate 					 * poll list. Find the next one on the
16307c478bd9Sstevel@tonic-gate 					 * list and update the cached
16317c478bd9Sstevel@tonic-gate 					 * xf_position field.
16327c478bd9Sstevel@tonic-gate 					 */
16337c478bd9Sstevel@tonic-gate 					for (i = count + 1; i < old_nfds; i++) {
16347c478bd9Sstevel@tonic-gate 						if (cached[i].fd == tmpfd) {
16357c478bd9Sstevel@tonic-gate 							pcache_update_xref(pcp,
16367c478bd9Sstevel@tonic-gate 							    tmpfd, (ssize_t)i,
16377c478bd9Sstevel@tonic-gate 							    which);
16387c478bd9Sstevel@tonic-gate 							break;
16397c478bd9Sstevel@tonic-gate 						}
16407c478bd9Sstevel@tonic-gate 					}
16417c478bd9Sstevel@tonic-gate 					ASSERT(i <= old_nfds);
16427c478bd9Sstevel@tonic-gate 				}
16437c478bd9Sstevel@tonic-gate 				/*
16447c478bd9Sstevel@tonic-gate 				 * In case a new cache list is allocated,
16457c478bd9Sstevel@tonic-gate 				 * need to keep both cache lists in sync
16467c478bd9Sstevel@tonic-gate 				 * b/c the new one can be freed if we have
16477c478bd9Sstevel@tonic-gate 				 * an error later.
16487c478bd9Sstevel@tonic-gate 				 */
16497c478bd9Sstevel@tonic-gate 				cached[count].fd = -1;
16507c478bd9Sstevel@tonic-gate 				if (newlist != NULL) {
16517c478bd9Sstevel@tonic-gate 					newlist[count].fd = -1;
16527c478bd9Sstevel@tonic-gate 				}
16537c478bd9Sstevel@tonic-gate 			}
16547c478bd9Sstevel@tonic-gate 			if ((tmpfd = current[count].fd) >= 0) {
16557c478bd9Sstevel@tonic-gate 				/*
16567c478bd9Sstevel@tonic-gate 				 * add to the cached fd tbl and bitmap.
16577c478bd9Sstevel@tonic-gate 				 */
16587c478bd9Sstevel@tonic-gate 				if ((fp = getf(tmpfd)) == NULL) {
16597c478bd9Sstevel@tonic-gate 					current[count].revents = POLLNVAL;
16607c478bd9Sstevel@tonic-gate 					if (newlist != NULL) {
16617c478bd9Sstevel@tonic-gate 						newlist[count].fd = -1;
16627c478bd9Sstevel@tonic-gate 					}
16637c478bd9Sstevel@tonic-gate 					cached[count].fd = -1;
16647c478bd9Sstevel@tonic-gate 					fdcnt++;
16657c478bd9Sstevel@tonic-gate 				} else {
16667c478bd9Sstevel@tonic-gate 					/*
16677c478bd9Sstevel@tonic-gate 					 * Here we don't care about the
16687c478bd9Sstevel@tonic-gate 					 * fdcnt. We will examine the bitmap
16697c478bd9Sstevel@tonic-gate 					 * later and pick up the correct
16707c478bd9Sstevel@tonic-gate 					 * fdcnt there. So we never bother
16717c478bd9Sstevel@tonic-gate 					 * to check value of 'cnt'.
16727c478bd9Sstevel@tonic-gate 					 */
16737c478bd9Sstevel@tonic-gate 					error = pcache_insert(ps, fp,
16747c478bd9Sstevel@tonic-gate 					    &current[count], &cnt,
16757c478bd9Sstevel@tonic-gate 					    (ssize_t)count, which);
16767c478bd9Sstevel@tonic-gate 					/*
16777c478bd9Sstevel@tonic-gate 					 * if no error, we want to do releasef
16787c478bd9Sstevel@tonic-gate 					 * after we updated cache poll list
16797c478bd9Sstevel@tonic-gate 					 * entry so that close() won't race
16807c478bd9Sstevel@tonic-gate 					 * us.
16817c478bd9Sstevel@tonic-gate 					 */
16827c478bd9Sstevel@tonic-gate 					if (error) {
16837c478bd9Sstevel@tonic-gate 						/*
16847c478bd9Sstevel@tonic-gate 						 * If we encountered an error,
16857c478bd9Sstevel@tonic-gate 						 * we have invalidated an
16867c478bd9Sstevel@tonic-gate 						 * entry in cached poll list
16877c478bd9Sstevel@tonic-gate 						 * (in pcache_delete_fd() above)
16887c478bd9Sstevel@tonic-gate 						 * but failed to add one here.
16897c478bd9Sstevel@tonic-gate 						 * This is OK b/c what's in the
16907c478bd9Sstevel@tonic-gate 						 * cached list is consistent
16917c478bd9Sstevel@tonic-gate 						 * with content of cache.
16927c478bd9Sstevel@tonic-gate 						 * It will not have any ill
16937c478bd9Sstevel@tonic-gate 						 * effect on next poll().
16947c478bd9Sstevel@tonic-gate 						 */
16957c478bd9Sstevel@tonic-gate 						releasef(tmpfd);
16967c478bd9Sstevel@tonic-gate 						if (newlist != NULL) {
16977c478bd9Sstevel@tonic-gate 							kmem_free(newlist,
16987c478bd9Sstevel@tonic-gate 							    nfds *
16997c478bd9Sstevel@tonic-gate 							    sizeof (pollfd_t));
17007c478bd9Sstevel@tonic-gate 						}
17017c478bd9Sstevel@tonic-gate 						return (error);
17027c478bd9Sstevel@tonic-gate 					}
17037c478bd9Sstevel@tonic-gate 					/*
17047c478bd9Sstevel@tonic-gate 					 * If we have allocated a new(temp)
17057c478bd9Sstevel@tonic-gate 					 * cache list, we need to keep both
17067c478bd9Sstevel@tonic-gate 					 * in sync b/c the new one can be freed
17077c478bd9Sstevel@tonic-gate 					 * if we have an error later.
17087c478bd9Sstevel@tonic-gate 					 */
17097c478bd9Sstevel@tonic-gate 					if (newlist != NULL) {
17107c478bd9Sstevel@tonic-gate 						newlist[count].fd =
17117c478bd9Sstevel@tonic-gate 						    current[count].fd;
17127c478bd9Sstevel@tonic-gate 						newlist[count].events =
17137c478bd9Sstevel@tonic-gate 						    current[count].events;
17147c478bd9Sstevel@tonic-gate 					}
17157c478bd9Sstevel@tonic-gate 					cached[count].fd = current[count].fd;
17167c478bd9Sstevel@tonic-gate 					cached[count].events =
17177c478bd9Sstevel@tonic-gate 					    current[count].events;
17187c478bd9Sstevel@tonic-gate 					releasef(tmpfd);
17197c478bd9Sstevel@tonic-gate 				}
17207c478bd9Sstevel@tonic-gate 			} else {
17217c478bd9Sstevel@tonic-gate 				current[count].revents = 0;
17227c478bd9Sstevel@tonic-gate 			}
17237c478bd9Sstevel@tonic-gate 			count++;
17247c478bd9Sstevel@tonic-gate 			remain = common - count;
17257c478bd9Sstevel@tonic-gate 		}
17267c478bd9Sstevel@tonic-gate 	}
17277c478bd9Sstevel@tonic-gate 	if (mismatch != 0) {
17287c478bd9Sstevel@tonic-gate 		if (mismatch == common) {
17297c478bd9Sstevel@tonic-gate 			pollstats.pollcachemiss.value.ui64++;
17307c478bd9Sstevel@tonic-gate 		} else {
17317c478bd9Sstevel@tonic-gate 			pollstats.pollcachephit.value.ui64++;
17327c478bd9Sstevel@tonic-gate 		}
17337c478bd9Sstevel@tonic-gate 	}
17347c478bd9Sstevel@tonic-gate 	/*
17357c478bd9Sstevel@tonic-gate 	 * take care of the non overlapping part of a list
17367c478bd9Sstevel@tonic-gate 	 */
17377c478bd9Sstevel@tonic-gate 	if (nfds > old_nfds) {
17387c478bd9Sstevel@tonic-gate 		ASSERT(newlist != NULL);
17397c478bd9Sstevel@tonic-gate 		for (i = old_nfds; i < nfds; i++) {
17407c478bd9Sstevel@tonic-gate 			/* filter out invalid events */
17417c478bd9Sstevel@tonic-gate 			if (current[i].events & ~VALID_POLL_EVENTS) {
17427c478bd9Sstevel@tonic-gate 				newlist[i].events = current[i].events =
17437c478bd9Sstevel@tonic-gate 				    current[i].events & VALID_POLL_EVENTS;
17447c478bd9Sstevel@tonic-gate 			}
17457c478bd9Sstevel@tonic-gate 			if ((fd = current[i].fd) < 0) {
17467c478bd9Sstevel@tonic-gate 				current[i].revents = 0;
17477c478bd9Sstevel@tonic-gate 				continue;
17487c478bd9Sstevel@tonic-gate 			}
17497c478bd9Sstevel@tonic-gate 			/*
17507c478bd9Sstevel@tonic-gate 			 * add to the cached fd tbl and bitmap.
17517c478bd9Sstevel@tonic-gate 			 */
17527c478bd9Sstevel@tonic-gate 			if ((fp = getf(fd)) == NULL) {
17537c478bd9Sstevel@tonic-gate 				current[i].revents = POLLNVAL;
17547c478bd9Sstevel@tonic-gate 				newlist[i].fd = -1;
17557c478bd9Sstevel@tonic-gate 				fdcnt++;
17567c478bd9Sstevel@tonic-gate 				continue;
17577c478bd9Sstevel@tonic-gate 			}
17587c478bd9Sstevel@tonic-gate 			/*
17597c478bd9Sstevel@tonic-gate 			 * Here we don't care about the
17607c478bd9Sstevel@tonic-gate 			 * fdcnt. We will examine the bitmap
17617c478bd9Sstevel@tonic-gate 			 * later and pick up the correct
17627c478bd9Sstevel@tonic-gate 			 * fdcnt there. So we never bother to
17637c478bd9Sstevel@tonic-gate 			 * check 'cnt'.
17647c478bd9Sstevel@tonic-gate 			 */
17657c478bd9Sstevel@tonic-gate 			error = pcache_insert(ps, fp, &current[i], &cnt,
17667c478bd9Sstevel@tonic-gate 			    (ssize_t)i, which);
17677c478bd9Sstevel@tonic-gate 			releasef(fd);
17687c478bd9Sstevel@tonic-gate 			if (error) {
17697c478bd9Sstevel@tonic-gate 				/*
17707c478bd9Sstevel@tonic-gate 				 * Here we are half way through adding newly
17717c478bd9Sstevel@tonic-gate 				 * polled fd. Undo enough to keep the cache
17727c478bd9Sstevel@tonic-gate 				 * list consistent with the cache content.
17737c478bd9Sstevel@tonic-gate 				 */
17747c478bd9Sstevel@tonic-gate 				pcacheset_remove_list(ps, current, old_nfds,
17757c478bd9Sstevel@tonic-gate 				    i, which, 0);
17767c478bd9Sstevel@tonic-gate 				kmem_free(newlist, nfds * sizeof (pollfd_t));
17777c478bd9Sstevel@tonic-gate 				return (error);
17787c478bd9Sstevel@tonic-gate 			}
17797c478bd9Sstevel@tonic-gate 		}
17807c478bd9Sstevel@tonic-gate 	}
17817c478bd9Sstevel@tonic-gate 	if (old_nfds > nfds) {
17827c478bd9Sstevel@tonic-gate 		/*
17837c478bd9Sstevel@tonic-gate 		 * remove the fd's which are no longer polled.
17847c478bd9Sstevel@tonic-gate 		 */
17857c478bd9Sstevel@tonic-gate 		pcacheset_remove_list(ps, pcsp->pcs_pollfd, nfds, old_nfds,
17867c478bd9Sstevel@tonic-gate 		    which, 1);
17877c478bd9Sstevel@tonic-gate 	}
17887c478bd9Sstevel@tonic-gate 	/*
17897c478bd9Sstevel@tonic-gate 	 * set difference resolved. update nfds and cachedlist
17907c478bd9Sstevel@tonic-gate 	 * in pollstate struct.
17917c478bd9Sstevel@tonic-gate 	 */
17927c478bd9Sstevel@tonic-gate 	if (newlist != NULL) {
17937c478bd9Sstevel@tonic-gate 		kmem_free(pcsp->pcs_pollfd, old_nfds * sizeof (pollfd_t));
17947c478bd9Sstevel@tonic-gate 		/*
17957c478bd9Sstevel@tonic-gate 		 * By now, the pollfd.revents field should
17967c478bd9Sstevel@tonic-gate 		 * all be zeroed.
17977c478bd9Sstevel@tonic-gate 		 */
17987c478bd9Sstevel@tonic-gate 		pcsp->pcs_pollfd = newlist;
17997c478bd9Sstevel@tonic-gate 		pcsp->pcs_nfds = nfds;
18007c478bd9Sstevel@tonic-gate 	}
18017c478bd9Sstevel@tonic-gate 	ASSERT(*fdcntp == 0);
18027c478bd9Sstevel@tonic-gate 	*fdcntp = fdcnt;
18037c478bd9Sstevel@tonic-gate 	/*
18047c478bd9Sstevel@tonic-gate 	 * By now for every fd in pollfdp, one of the following should be
18057c478bd9Sstevel@tonic-gate 	 * true. Otherwise we will miss a polled event.
18067c478bd9Sstevel@tonic-gate 	 *
18077c478bd9Sstevel@tonic-gate 	 * 1. the bit corresponding to the fd in bitmap is set. So VOP_POLL
18087c478bd9Sstevel@tonic-gate 	 *    will be called on this fd in next poll.
18097c478bd9Sstevel@tonic-gate 	 * 2. the fd is cached in the pcache (i.e. pd_php is set). So
18107c478bd9Sstevel@tonic-gate 	 *    pollnotify will happen.
18117c478bd9Sstevel@tonic-gate 	 */
18127c478bd9Sstevel@tonic-gate 	ASSERT(pollchecksanity(ps, nfds));
18137c478bd9Sstevel@tonic-gate 	/*
18147c478bd9Sstevel@tonic-gate 	 * make sure cross reference between cached poll lists and cached
18157c478bd9Sstevel@tonic-gate 	 * poll fds are correct.
18167c478bd9Sstevel@tonic-gate 	 */
18177c478bd9Sstevel@tonic-gate 	ASSERT(pollcheckxref(ps, which));
18187c478bd9Sstevel@tonic-gate 	/*
18197c478bd9Sstevel@tonic-gate 	 * ensure each polldat in pollcache reference a polled fd in
18207c478bd9Sstevel@tonic-gate 	 * pollcacheset.
18217c478bd9Sstevel@tonic-gate 	 */
18227c478bd9Sstevel@tonic-gate #ifdef DEBUG
18237c478bd9Sstevel@tonic-gate 	checkpolldat(ps);
18247c478bd9Sstevel@tonic-gate #endif
18257c478bd9Sstevel@tonic-gate 	return (0);
18267c478bd9Sstevel@tonic-gate }
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate #ifdef DEBUG
18297c478bd9Sstevel@tonic-gate static int
18307c478bd9Sstevel@tonic-gate pollscanrevents(pollcache_t *pcp, pollfd_t *pollfdp, nfds_t nfds)
18317c478bd9Sstevel@tonic-gate {
18327c478bd9Sstevel@tonic-gate 	int i;
18337c478bd9Sstevel@tonic-gate 	int reventcnt = 0;
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfds; i++) {
18367c478bd9Sstevel@tonic-gate 		if (pollfdp[i].fd < 0) {
18377c478bd9Sstevel@tonic-gate 			ASSERT(pollfdp[i].revents == 0);
18387c478bd9Sstevel@tonic-gate 			continue;
18397c478bd9Sstevel@tonic-gate 		}
18407c478bd9Sstevel@tonic-gate 		if (pollfdp[i].revents) {
18417c478bd9Sstevel@tonic-gate 			reventcnt++;
18427c478bd9Sstevel@tonic-gate 		}
18437c478bd9Sstevel@tonic-gate 		if (pollfdp[i].revents && (pollfdp[i].revents != POLLNVAL)) {
18447c478bd9Sstevel@tonic-gate 			ASSERT(BT_TEST(pcp->pc_bitmap, pollfdp[i].fd));
18457c478bd9Sstevel@tonic-gate 		}
18467c478bd9Sstevel@tonic-gate 	}
18477c478bd9Sstevel@tonic-gate 	return (reventcnt);
18487c478bd9Sstevel@tonic-gate }
18497c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate /*
18527c478bd9Sstevel@tonic-gate  * read the bitmap and poll on fds corresponding to the '1' bits. The ps_lock
18537c478bd9Sstevel@tonic-gate  * is held upon entry.
18547c478bd9Sstevel@tonic-gate  */
18557c478bd9Sstevel@tonic-gate int
18567c478bd9Sstevel@tonic-gate pcache_poll(pollfd_t *pollfdp, pollstate_t *ps, nfds_t nfds, int *fdcntp,
18577c478bd9Sstevel@tonic-gate     int which)
18587c478bd9Sstevel@tonic-gate {
18597c478bd9Sstevel@tonic-gate 	int		i;
18607c478bd9Sstevel@tonic-gate 	pollcache_t	*pcp;
18617c478bd9Sstevel@tonic-gate 	int 		fd;
18627c478bd9Sstevel@tonic-gate 	int 		begin, end, done;
18637c478bd9Sstevel@tonic-gate 	pollhead_t	*php;
18647c478bd9Sstevel@tonic-gate 	int		fdcnt;
18657c478bd9Sstevel@tonic-gate 	int		error = 0;
18667c478bd9Sstevel@tonic-gate 	file_t		*fp;
18677c478bd9Sstevel@tonic-gate 	polldat_t	*pdp;
18687c478bd9Sstevel@tonic-gate 	xref_t		*refp;
18697c478bd9Sstevel@tonic-gate 	int		entry;
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate 	pcp = ps->ps_pcache;
18727c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ps->ps_lock));
18737c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pcp->pc_lock));
18747c478bd9Sstevel@tonic-gate retry:
18757c478bd9Sstevel@tonic-gate 	done = 0;
18767c478bd9Sstevel@tonic-gate 	begin = 0;
18777c478bd9Sstevel@tonic-gate 	fdcnt = 0;
18787c478bd9Sstevel@tonic-gate 	end = pcp->pc_mapend;
18797c478bd9Sstevel@tonic-gate 	while ((fdcnt < nfds) && !done) {
18807c478bd9Sstevel@tonic-gate 		php = NULL;
18817c478bd9Sstevel@tonic-gate 		/*
18827c478bd9Sstevel@tonic-gate 		 * only poll fds which may have events
18837c478bd9Sstevel@tonic-gate 		 */
18847c478bd9Sstevel@tonic-gate 		fd = bt_getlowbit(pcp->pc_bitmap, begin, end);
18857c478bd9Sstevel@tonic-gate 		ASSERT(fd <= end);
18867c478bd9Sstevel@tonic-gate 		if (fd >= 0) {
18877c478bd9Sstevel@tonic-gate 			ASSERT(pollcheckrevents(ps, begin, fd, which));
18887c478bd9Sstevel@tonic-gate 			/*
18897c478bd9Sstevel@tonic-gate 			 * adjust map pointers for next round
18907c478bd9Sstevel@tonic-gate 			 */
18917c478bd9Sstevel@tonic-gate 			if (fd == end) {
18927c478bd9Sstevel@tonic-gate 				done = 1;
18937c478bd9Sstevel@tonic-gate 			} else {
18947c478bd9Sstevel@tonic-gate 				begin = fd + 1;
18957c478bd9Sstevel@tonic-gate 			}
18967c478bd9Sstevel@tonic-gate 			/*
18977c478bd9Sstevel@tonic-gate 			 * A bitmap caches poll state information of
18987c478bd9Sstevel@tonic-gate 			 * multiple poll lists. Call VOP_POLL only if
18997c478bd9Sstevel@tonic-gate 			 * the bit corresponds to an fd in this poll
19007c478bd9Sstevel@tonic-gate 			 * list.
19017c478bd9Sstevel@tonic-gate 			 */
19027c478bd9Sstevel@tonic-gate 			pdp = pcache_lookup_fd(pcp, fd);
19037c478bd9Sstevel@tonic-gate 			ASSERT(pdp != NULL);
19047c478bd9Sstevel@tonic-gate 			ASSERT(pdp->pd_ref != NULL);
19057c478bd9Sstevel@tonic-gate 			refp = &pdp->pd_ref[which];
19067c478bd9Sstevel@tonic-gate 			if (refp->xf_refcnt == 0)
19077c478bd9Sstevel@tonic-gate 				continue;
19087c478bd9Sstevel@tonic-gate 			entry = refp->xf_position;
19097c478bd9Sstevel@tonic-gate 			ASSERT((entry >= 0) && (entry < nfds));
19107c478bd9Sstevel@tonic-gate 			ASSERT(pollfdp[entry].fd == fd);
19117c478bd9Sstevel@tonic-gate 			/*
19127c478bd9Sstevel@tonic-gate 			 * we are in this routine implies that we have
19137c478bd9Sstevel@tonic-gate 			 * successfully polled this fd in the past.
19147c478bd9Sstevel@tonic-gate 			 * Check to see this fd is closed while we are
19157c478bd9Sstevel@tonic-gate 			 * blocked in poll. This ensures that we don't
19167c478bd9Sstevel@tonic-gate 			 * miss a close on the fd in the case this fd is
19177c478bd9Sstevel@tonic-gate 			 * reused.
19187c478bd9Sstevel@tonic-gate 			 */
19197c478bd9Sstevel@tonic-gate 			if (pdp->pd_fp == NULL) {
19207c478bd9Sstevel@tonic-gate 				ASSERT(pdp->pd_count > 0);
19217c478bd9Sstevel@tonic-gate 				pollfdp[entry].revents = POLLNVAL;
19227c478bd9Sstevel@tonic-gate 				fdcnt++;
19237c478bd9Sstevel@tonic-gate 				if (refp->xf_refcnt > 1) {
19247c478bd9Sstevel@tonic-gate 					/*
19257c478bd9Sstevel@tonic-gate 					 * this fd appeared multiple time
19267c478bd9Sstevel@tonic-gate 					 * in the poll list. Find all of them.
19277c478bd9Sstevel@tonic-gate 					 */
19287c478bd9Sstevel@tonic-gate 					for (i = entry + 1; i < nfds; i++) {
19297c478bd9Sstevel@tonic-gate 						if (pollfdp[i].fd == fd) {
19307c478bd9Sstevel@tonic-gate 							pollfdp[i].revents =
19317c478bd9Sstevel@tonic-gate 							    POLLNVAL;
19327c478bd9Sstevel@tonic-gate 							fdcnt++;
19337c478bd9Sstevel@tonic-gate 						}
19347c478bd9Sstevel@tonic-gate 					}
19357c478bd9Sstevel@tonic-gate 				}
19367c478bd9Sstevel@tonic-gate 				pcacheset_invalidate(ps, pdp);
19377c478bd9Sstevel@tonic-gate 				continue;
19387c478bd9Sstevel@tonic-gate 			}
19397c478bd9Sstevel@tonic-gate 			/*
19407c478bd9Sstevel@tonic-gate 			 * We can be here polling a device that is being
19417c478bd9Sstevel@tonic-gate 			 * closed (i.e. the file pointer is set to NULL,
19427c478bd9Sstevel@tonic-gate 			 * but pollcacheclean has not happened yet).
19437c478bd9Sstevel@tonic-gate 			 */
19447c478bd9Sstevel@tonic-gate 			if ((fp = getf(fd)) == NULL) {
19457c478bd9Sstevel@tonic-gate 				pollfdp[entry].revents = POLLNVAL;
19467c478bd9Sstevel@tonic-gate 				fdcnt++;
19477c478bd9Sstevel@tonic-gate 				if (refp->xf_refcnt > 1) {
19487c478bd9Sstevel@tonic-gate 					/*
19497c478bd9Sstevel@tonic-gate 					 * this fd appeared multiple time
19507c478bd9Sstevel@tonic-gate 					 * in the poll list. Find all of them.
19517c478bd9Sstevel@tonic-gate 					 */
19527c478bd9Sstevel@tonic-gate 					for (i = entry + 1; i < nfds; i++) {
19537c478bd9Sstevel@tonic-gate 						if (pollfdp[i].fd == fd) {
19547c478bd9Sstevel@tonic-gate 							pollfdp[i].revents =
19557c478bd9Sstevel@tonic-gate 							    POLLNVAL;
19567c478bd9Sstevel@tonic-gate 							fdcnt++;
19577c478bd9Sstevel@tonic-gate 						}
19587c478bd9Sstevel@tonic-gate 					}
19597c478bd9Sstevel@tonic-gate 				}
19607c478bd9Sstevel@tonic-gate 				continue;
19617c478bd9Sstevel@tonic-gate 			}
19627c478bd9Sstevel@tonic-gate 			ASSERT(pdp->pd_fp == fp);
19637c478bd9Sstevel@tonic-gate 			ASSERT(infpollinfo(fd));
19647c478bd9Sstevel@tonic-gate 			/*
19657c478bd9Sstevel@tonic-gate 			 * Since we no longer hold poll head lock across
19667c478bd9Sstevel@tonic-gate 			 * VOP_POLL, pollunlock logic can be simplifed.
19677c478bd9Sstevel@tonic-gate 			 */
19687c478bd9Sstevel@tonic-gate 			ASSERT(pdp->pd_php == NULL ||
19697c478bd9Sstevel@tonic-gate 			    MUTEX_NOT_HELD(PHLOCK(pdp->pd_php)));
19707c478bd9Sstevel@tonic-gate 			/*
19717c478bd9Sstevel@tonic-gate 			 * underlying file systems may set a "pollpending"
19727c478bd9Sstevel@tonic-gate 			 * flag when it sees the poll may block. Pollwakeup()
19737c478bd9Sstevel@tonic-gate 			 * is called by wakeup thread if pollpending is set.
19747c478bd9Sstevel@tonic-gate 			 * Pass a 0 fdcnt so that the underlying file system
19757c478bd9Sstevel@tonic-gate 			 * will set the "pollpending" flag set when there is
19767c478bd9Sstevel@tonic-gate 			 * no polled events.
19777c478bd9Sstevel@tonic-gate 			 *
19787c478bd9Sstevel@tonic-gate 			 * Use pollfdp[].events for actual polling because
19797c478bd9Sstevel@tonic-gate 			 * the pd_events is union of all cached poll events
19807c478bd9Sstevel@tonic-gate 			 * on this fd. The events parameter also affects
19817c478bd9Sstevel@tonic-gate 			 * how the polled device sets the "poll pending"
19827c478bd9Sstevel@tonic-gate 			 * flag.
19837c478bd9Sstevel@tonic-gate 			 */
19847c478bd9Sstevel@tonic-gate 			ASSERT(curthread->t_pollcache == NULL);
19857c478bd9Sstevel@tonic-gate 			error = VOP_POLL(fp->f_vnode, pollfdp[entry].events, 0,
1986da6c28aaSamw 			    &pollfdp[entry].revents, &php, NULL);
19877c478bd9Sstevel@tonic-gate 			/*
19887c478bd9Sstevel@tonic-gate 			 * releasef after completely done with this cached
19897c478bd9Sstevel@tonic-gate 			 * poll entry. To prevent close() coming in to clear
19907c478bd9Sstevel@tonic-gate 			 * this entry.
19917c478bd9Sstevel@tonic-gate 			 */
19927c478bd9Sstevel@tonic-gate 			if (error) {
19937c478bd9Sstevel@tonic-gate 				releasef(fd);
19947c478bd9Sstevel@tonic-gate 				break;
19957c478bd9Sstevel@tonic-gate 			}
19967c478bd9Sstevel@tonic-gate 			/*
19977c478bd9Sstevel@tonic-gate 			 * layered devices (e.g. console driver)
19987c478bd9Sstevel@tonic-gate 			 * may change the vnode and thus the pollhead
19997c478bd9Sstevel@tonic-gate 			 * pointer out from underneath us.
20007c478bd9Sstevel@tonic-gate 			 */
20017c478bd9Sstevel@tonic-gate 			if (php != NULL && pdp->pd_php != NULL &&
20027c478bd9Sstevel@tonic-gate 			    php != pdp->pd_php) {
20037c478bd9Sstevel@tonic-gate 				releasef(fd);
20047c478bd9Sstevel@tonic-gate 				pollhead_delete(pdp->pd_php, pdp);
20057c478bd9Sstevel@tonic-gate 				pdp->pd_php = php;
20067c478bd9Sstevel@tonic-gate 				pollhead_insert(php, pdp);
20077c478bd9Sstevel@tonic-gate 				/*
20087c478bd9Sstevel@tonic-gate 				 * We could have missed a wakeup on the new
20097c478bd9Sstevel@tonic-gate 				 * target device. Make sure the new target
20107c478bd9Sstevel@tonic-gate 				 * gets polled once.
20117c478bd9Sstevel@tonic-gate 				 */
20127c478bd9Sstevel@tonic-gate 				BT_SET(pcp->pc_bitmap, fd);
20137c478bd9Sstevel@tonic-gate 				goto retry;
20147c478bd9Sstevel@tonic-gate 			}
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 			if (pollfdp[entry].revents) {
20177c478bd9Sstevel@tonic-gate 				ASSERT(refp->xf_refcnt >= 1);
20187c478bd9Sstevel@tonic-gate 				fdcnt++;
20197c478bd9Sstevel@tonic-gate 				if (refp->xf_refcnt > 1) {
20207c478bd9Sstevel@tonic-gate 					/*
20217c478bd9Sstevel@tonic-gate 					 * this fd appeared multiple time
20227c478bd9Sstevel@tonic-gate 					 * in the poll list. This is rare but
20237c478bd9Sstevel@tonic-gate 					 * we have to look at all of them for
20247c478bd9Sstevel@tonic-gate 					 * correctness.
20257c478bd9Sstevel@tonic-gate 					 */
20267c478bd9Sstevel@tonic-gate 					error = plist_chkdupfd(fp, pdp, ps,
20277c478bd9Sstevel@tonic-gate 					    pollfdp, entry, &fdcnt);
20287c478bd9Sstevel@tonic-gate 					if (error > 0) {
20297c478bd9Sstevel@tonic-gate 						releasef(fd);
20307c478bd9Sstevel@tonic-gate 						break;
20317c478bd9Sstevel@tonic-gate 					}
20327c478bd9Sstevel@tonic-gate 					if (error < 0) {
20337c478bd9Sstevel@tonic-gate 						goto retry;
20347c478bd9Sstevel@tonic-gate 					}
20357c478bd9Sstevel@tonic-gate 				}
20367c478bd9Sstevel@tonic-gate 				releasef(fd);
20377c478bd9Sstevel@tonic-gate 			} else {
20387c478bd9Sstevel@tonic-gate 				/*
20397c478bd9Sstevel@tonic-gate 				 * VOP_POLL didn't return any revents. We can
20407c478bd9Sstevel@tonic-gate 				 * clear the bit in bitmap only if we have the
20417c478bd9Sstevel@tonic-gate 				 * pollhead ptr cached and no other cached
20427c478bd9Sstevel@tonic-gate 				 * entry is polling different events on this fd.
20437c478bd9Sstevel@tonic-gate 				 * VOP_POLL may have dropped the ps_lock. Make
20447c478bd9Sstevel@tonic-gate 				 * sure pollwakeup has not happened before clear
20457c478bd9Sstevel@tonic-gate 				 * the bit.
20467c478bd9Sstevel@tonic-gate 				 */
20477c478bd9Sstevel@tonic-gate 				if ((pdp->pd_php != NULL) &&
20487c478bd9Sstevel@tonic-gate 				    (pollfdp[entry].events == pdp->pd_events) &&
20497c478bd9Sstevel@tonic-gate 				    ((pcp->pc_flag & T_POLLWAKE) == 0)) {
20507c478bd9Sstevel@tonic-gate 					BT_CLEAR(pcp->pc_bitmap, fd);
20517c478bd9Sstevel@tonic-gate 				}
20527c478bd9Sstevel@tonic-gate 				/*
20537c478bd9Sstevel@tonic-gate 				 * if the fd can be cached now but not before,
20547c478bd9Sstevel@tonic-gate 				 * do it now.
20557c478bd9Sstevel@tonic-gate 				 */
20567c478bd9Sstevel@tonic-gate 				if ((pdp->pd_php == NULL) && (php != NULL)) {
20577c478bd9Sstevel@tonic-gate 					pdp->pd_php = php;
20587c478bd9Sstevel@tonic-gate 					pollhead_insert(php, pdp);
20597c478bd9Sstevel@tonic-gate 					/*
20607c478bd9Sstevel@tonic-gate 					 * We are inserting a polldat struct for
20617c478bd9Sstevel@tonic-gate 					 * the first time. We may have missed a
20627c478bd9Sstevel@tonic-gate 					 * wakeup on this device. Re-poll once.
20637c478bd9Sstevel@tonic-gate 					 * This should be a rare event.
20647c478bd9Sstevel@tonic-gate 					 */
20657c478bd9Sstevel@tonic-gate 					releasef(fd);
20667c478bd9Sstevel@tonic-gate 					goto retry;
20677c478bd9Sstevel@tonic-gate 				}
20687c478bd9Sstevel@tonic-gate 				if (refp->xf_refcnt > 1) {
20697c478bd9Sstevel@tonic-gate 					/*
20707c478bd9Sstevel@tonic-gate 					 * this fd appeared multiple time
20717c478bd9Sstevel@tonic-gate 					 * in the poll list. This is rare but
20727c478bd9Sstevel@tonic-gate 					 * we have to look at all of them for
20737c478bd9Sstevel@tonic-gate 					 * correctness.
20747c478bd9Sstevel@tonic-gate 					 */
20757c478bd9Sstevel@tonic-gate 					error = plist_chkdupfd(fp, pdp, ps,
20767c478bd9Sstevel@tonic-gate 					    pollfdp, entry, &fdcnt);
20777c478bd9Sstevel@tonic-gate 					if (error > 0) {
20787c478bd9Sstevel@tonic-gate 						releasef(fd);
20797c478bd9Sstevel@tonic-gate 						break;
20807c478bd9Sstevel@tonic-gate 					}
20817c478bd9Sstevel@tonic-gate 					if (error < 0) {
20827c478bd9Sstevel@tonic-gate 						goto retry;
20837c478bd9Sstevel@tonic-gate 					}
20847c478bd9Sstevel@tonic-gate 				}
20857c478bd9Sstevel@tonic-gate 				releasef(fd);
20867c478bd9Sstevel@tonic-gate 			}
20877c478bd9Sstevel@tonic-gate 		} else {
20887c478bd9Sstevel@tonic-gate 			done = 1;
20897c478bd9Sstevel@tonic-gate 			ASSERT(pollcheckrevents(ps, begin, end + 1, which));
20907c478bd9Sstevel@tonic-gate 		}
20917c478bd9Sstevel@tonic-gate 	}
20927c478bd9Sstevel@tonic-gate 	if (!error) {
20937c478bd9Sstevel@tonic-gate 		ASSERT(*fdcntp + fdcnt == pollscanrevents(pcp, pollfdp, nfds));
20947c478bd9Sstevel@tonic-gate 		*fdcntp += fdcnt;
20957c478bd9Sstevel@tonic-gate 	}
20967c478bd9Sstevel@tonic-gate 	return (error);
20977c478bd9Sstevel@tonic-gate }
20987c478bd9Sstevel@tonic-gate 
20997c478bd9Sstevel@tonic-gate /*
21007c478bd9Sstevel@tonic-gate  * Going through the poll list without much locking. Poll all fds and
21017c478bd9Sstevel@tonic-gate  * cache all valid fds in the pollcache.
21027c478bd9Sstevel@tonic-gate  */
21037c478bd9Sstevel@tonic-gate int
21047c478bd9Sstevel@tonic-gate pcacheset_cache_list(pollstate_t *ps, pollfd_t *fds, int *fdcntp, int which)
21057c478bd9Sstevel@tonic-gate {
21067c478bd9Sstevel@tonic-gate 	pollfd_t	*pollfdp = ps->ps_pollfd;
21077c478bd9Sstevel@tonic-gate 	pollcacheset_t	*pcacheset = ps->ps_pcacheset;
21087c478bd9Sstevel@tonic-gate 	pollfd_t	*newfdlist;
21097c478bd9Sstevel@tonic-gate 	int		i;
21107c478bd9Sstevel@tonic-gate 	int		fd;
21117c478bd9Sstevel@tonic-gate 	file_t		*fp;
21127c478bd9Sstevel@tonic-gate 	int		error = 0;
21137c478bd9Sstevel@tonic-gate 
21147c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ps->ps_lock));
21157c478bd9Sstevel@tonic-gate 	ASSERT(which < ps->ps_nsets);
21167c478bd9Sstevel@tonic-gate 	ASSERT(pcacheset != NULL);
21177c478bd9Sstevel@tonic-gate 	ASSERT(pcacheset[which].pcs_pollfd == NULL);
21187c478bd9Sstevel@tonic-gate 	newfdlist  = kmem_alloc(ps->ps_nfds * sizeof (pollfd_t), KM_SLEEP);
21197c478bd9Sstevel@tonic-gate 	/*
21207c478bd9Sstevel@tonic-gate 	 * cache the new poll list in pollcachset.
21217c478bd9Sstevel@tonic-gate 	 */
21227c478bd9Sstevel@tonic-gate 	bcopy(pollfdp, newfdlist, sizeof (pollfd_t) * ps->ps_nfds);
21237c478bd9Sstevel@tonic-gate 
21247c478bd9Sstevel@tonic-gate 	pcacheset[which].pcs_pollfd = newfdlist;
21257c478bd9Sstevel@tonic-gate 	pcacheset[which].pcs_nfds = ps->ps_nfds;
21267c478bd9Sstevel@tonic-gate 	pcacheset[which].pcs_usradr = (uintptr_t)fds;
21277c478bd9Sstevel@tonic-gate 
21287c478bd9Sstevel@tonic-gate 	/*
21297c478bd9Sstevel@tonic-gate 	 * We have saved a copy of current poll fd list in one pollcacheset.
21307c478bd9Sstevel@tonic-gate 	 * The 'revents' field of the new list is not yet set to 0. Loop
21317c478bd9Sstevel@tonic-gate 	 * through the new list just to do that is expensive. We do that
21327c478bd9Sstevel@tonic-gate 	 * while polling the list.
21337c478bd9Sstevel@tonic-gate 	 */
21347c478bd9Sstevel@tonic-gate 	for (i = 0; i < ps->ps_nfds; i++) {
21357c478bd9Sstevel@tonic-gate 		fd = pollfdp[i].fd;
21367c478bd9Sstevel@tonic-gate 		/*
21377c478bd9Sstevel@tonic-gate 		 * We also filter out the illegal poll events in the event
21387c478bd9Sstevel@tonic-gate 		 * field for the cached poll list/set.
21397c478bd9Sstevel@tonic-gate 		 */
21407c478bd9Sstevel@tonic-gate 		if (pollfdp[i].events & ~VALID_POLL_EVENTS) {
21417c478bd9Sstevel@tonic-gate 			newfdlist[i].events = pollfdp[i].events =
21427c478bd9Sstevel@tonic-gate 			    pollfdp[i].events & VALID_POLL_EVENTS;
21437c478bd9Sstevel@tonic-gate 		}
21447c478bd9Sstevel@tonic-gate 		if (fd < 0) {
21457c478bd9Sstevel@tonic-gate 			pollfdp[i].revents = 0;
21467c478bd9Sstevel@tonic-gate 			continue;
21477c478bd9Sstevel@tonic-gate 		}
21487c478bd9Sstevel@tonic-gate 		if ((fp = getf(fd)) == NULL) {
21497c478bd9Sstevel@tonic-gate 			pollfdp[i].revents = POLLNVAL;
21507c478bd9Sstevel@tonic-gate 			/*
21517c478bd9Sstevel@tonic-gate 			 * invalidate this cache entry in the cached poll list
21527c478bd9Sstevel@tonic-gate 			 */
21537c478bd9Sstevel@tonic-gate 			newfdlist[i].fd = -1;
21547c478bd9Sstevel@tonic-gate 			(*fdcntp)++;
21557c478bd9Sstevel@tonic-gate 			continue;
21567c478bd9Sstevel@tonic-gate 		}
21577c478bd9Sstevel@tonic-gate 		/*
21587c478bd9Sstevel@tonic-gate 		 * cache this fd.
21597c478bd9Sstevel@tonic-gate 		 */
21607c478bd9Sstevel@tonic-gate 		error = pcache_insert(ps, fp, &pollfdp[i], fdcntp, (ssize_t)i,
21617c478bd9Sstevel@tonic-gate 		    which);
21627c478bd9Sstevel@tonic-gate 		releasef(fd);
21637c478bd9Sstevel@tonic-gate 		if (error) {
21647c478bd9Sstevel@tonic-gate 			/*
21657c478bd9Sstevel@tonic-gate 			 * Here we are half way through caching a new
21667c478bd9Sstevel@tonic-gate 			 * poll list. Undo every thing.
21677c478bd9Sstevel@tonic-gate 			 */
21687c478bd9Sstevel@tonic-gate 			pcacheset_remove_list(ps, pollfdp, 0, i, which, 0);
21697c478bd9Sstevel@tonic-gate 			kmem_free(newfdlist, ps->ps_nfds * sizeof (pollfd_t));
21707c478bd9Sstevel@tonic-gate 			pcacheset[which].pcs_pollfd = NULL;
21717c478bd9Sstevel@tonic-gate 			pcacheset[which].pcs_usradr = NULL;
21727c478bd9Sstevel@tonic-gate 			break;
21737c478bd9Sstevel@tonic-gate 		}
21747c478bd9Sstevel@tonic-gate 	}
21757c478bd9Sstevel@tonic-gate 	return (error);
21767c478bd9Sstevel@tonic-gate }
21777c478bd9Sstevel@tonic-gate 
21787c478bd9Sstevel@tonic-gate /*
21797c478bd9Sstevel@tonic-gate  * called by pollcacheclean() to set the fp NULL. It also sets polled events
21807c478bd9Sstevel@tonic-gate  * in pcacheset entries to a special events 'POLLCLOSED'. Do a pollwakeup to
21817c478bd9Sstevel@tonic-gate  * wake any sleeping poller, then remove the polldat from the driver.
21827c478bd9Sstevel@tonic-gate  * The routine is called with ps_pcachelock held.
21837c478bd9Sstevel@tonic-gate  */
21847c478bd9Sstevel@tonic-gate void
21857c478bd9Sstevel@tonic-gate pcache_clean_entry(pollstate_t *ps, int fd)
21867c478bd9Sstevel@tonic-gate {
21877c478bd9Sstevel@tonic-gate 	pollcache_t	*pcp;
21887c478bd9Sstevel@tonic-gate 	polldat_t	*pdp;
21897c478bd9Sstevel@tonic-gate 	int		i;
21907c478bd9Sstevel@tonic-gate 
21917c478bd9Sstevel@tonic-gate 	ASSERT(ps != NULL);
21927c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ps->ps_lock));
21937c478bd9Sstevel@tonic-gate 	pcp = ps->ps_pcache;
21947c478bd9Sstevel@tonic-gate 	ASSERT(pcp);
21957c478bd9Sstevel@tonic-gate 	pdp = pcache_lookup_fd(pcp, fd);
21967c478bd9Sstevel@tonic-gate 	ASSERT(pdp != NULL);
21977c478bd9Sstevel@tonic-gate 	/*
21987c478bd9Sstevel@tonic-gate 	 * the corresponding fpollinfo in fi_list has been removed by
21997c478bd9Sstevel@tonic-gate 	 * a close on this fd. Reset the cached fp ptr here.
22007c478bd9Sstevel@tonic-gate 	 */
22017c478bd9Sstevel@tonic-gate 	pdp->pd_fp = NULL;
22027c478bd9Sstevel@tonic-gate 	/*
22037c478bd9Sstevel@tonic-gate 	 * XXX - This routine also touches data in pcacheset struct.
22047c478bd9Sstevel@tonic-gate 	 *
22057c478bd9Sstevel@tonic-gate 	 * set the event in cached poll lists to POLLCLOSED. This invalidate
22067c478bd9Sstevel@tonic-gate 	 * the cached poll fd entry in that poll list, which will force a
22077c478bd9Sstevel@tonic-gate 	 * removal of this cached entry in next poll(). The cleanup is done
22087c478bd9Sstevel@tonic-gate 	 * at the removal time.
22097c478bd9Sstevel@tonic-gate 	 */
22107c478bd9Sstevel@tonic-gate 	ASSERT(pdp->pd_ref != NULL);
22117c478bd9Sstevel@tonic-gate 	for (i = 0; i < ps->ps_nsets; i++) {
22127c478bd9Sstevel@tonic-gate 		xref_t		*refp;
22137c478bd9Sstevel@tonic-gate 		pollcacheset_t	*pcsp;
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate 		refp = &pdp->pd_ref[i];
22167c478bd9Sstevel@tonic-gate 		if (refp->xf_refcnt) {
22177c478bd9Sstevel@tonic-gate 			ASSERT(refp->xf_position >= 0);
22187c478bd9Sstevel@tonic-gate 			pcsp = &ps->ps_pcacheset[i];
22197c478bd9Sstevel@tonic-gate 			if (refp->xf_refcnt == 1) {
22207c478bd9Sstevel@tonic-gate 				pcsp->pcs_pollfd[refp->xf_position].events =
22217c478bd9Sstevel@tonic-gate 				    (short)POLLCLOSED;
22227c478bd9Sstevel@tonic-gate 			}
22237c478bd9Sstevel@tonic-gate 			if (refp->xf_refcnt > 1) {
22247c478bd9Sstevel@tonic-gate 				int	j;
22257c478bd9Sstevel@tonic-gate 				/*
22267c478bd9Sstevel@tonic-gate 				 * mark every matching entry in pcs_pollfd
22277c478bd9Sstevel@tonic-gate 				 */
22287c478bd9Sstevel@tonic-gate 				for (j = refp->xf_position;
22297c478bd9Sstevel@tonic-gate 				    j < pcsp->pcs_nfds; j++) {
22307c478bd9Sstevel@tonic-gate 					if (pcsp->pcs_pollfd[j].fd == fd) {
22317c478bd9Sstevel@tonic-gate 						pcsp->pcs_pollfd[j].events =
22327c478bd9Sstevel@tonic-gate 						    (short)POLLCLOSED;
22337c478bd9Sstevel@tonic-gate 					}
22347c478bd9Sstevel@tonic-gate 				}
22357c478bd9Sstevel@tonic-gate 			}
22367c478bd9Sstevel@tonic-gate 		}
22377c478bd9Sstevel@tonic-gate 	}
22387c478bd9Sstevel@tonic-gate 	if (pdp->pd_php) {
22397c478bd9Sstevel@tonic-gate 		pollwakeup(pdp->pd_php, POLLHUP);
22407c478bd9Sstevel@tonic-gate 		pollhead_delete(pdp->pd_php, pdp);
22417c478bd9Sstevel@tonic-gate 		pdp->pd_php = NULL;
22427c478bd9Sstevel@tonic-gate 	}
22437c478bd9Sstevel@tonic-gate }
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate /*
22467c478bd9Sstevel@tonic-gate  * This is the first time this thread has ever polled,
22477c478bd9Sstevel@tonic-gate  * so we have to create its pollstate structure.
22487c478bd9Sstevel@tonic-gate  * This will persist for the life of the thread,
22497c478bd9Sstevel@tonic-gate  * until it calls pollcleanup().
22507c478bd9Sstevel@tonic-gate  */
22517c478bd9Sstevel@tonic-gate pollstate_t *
22527c478bd9Sstevel@tonic-gate pollstate_create(void)
22537c478bd9Sstevel@tonic-gate {
22547c478bd9Sstevel@tonic-gate 	pollstate_t *ps;
22557c478bd9Sstevel@tonic-gate 
22567c478bd9Sstevel@tonic-gate 	ps = kmem_zalloc(sizeof (pollstate_t), KM_SLEEP);
22577c478bd9Sstevel@tonic-gate 	ps->ps_nsets = POLLFDSETS;
22587c478bd9Sstevel@tonic-gate 	ps->ps_pcacheset = pcacheset_create(ps->ps_nsets);
22597c478bd9Sstevel@tonic-gate 	return (ps);
22607c478bd9Sstevel@tonic-gate }
22617c478bd9Sstevel@tonic-gate 
22627c478bd9Sstevel@tonic-gate void
22637c478bd9Sstevel@tonic-gate pollstate_destroy(pollstate_t *ps)
22647c478bd9Sstevel@tonic-gate {
22657c478bd9Sstevel@tonic-gate 	if (ps->ps_pollfd != NULL) {
22667c478bd9Sstevel@tonic-gate 		kmem_free(ps->ps_pollfd, ps->ps_nfds * sizeof (pollfd_t));
22677c478bd9Sstevel@tonic-gate 		ps->ps_pollfd = NULL;
22687c478bd9Sstevel@tonic-gate 	}
22697c478bd9Sstevel@tonic-gate 	if (ps->ps_pcache != NULL) {
22707c478bd9Sstevel@tonic-gate 		pcache_destroy(ps->ps_pcache);
22717c478bd9Sstevel@tonic-gate 		ps->ps_pcache = NULL;
22727c478bd9Sstevel@tonic-gate 	}
22737c478bd9Sstevel@tonic-gate 	pcacheset_destroy(ps->ps_pcacheset, ps->ps_nsets);
22747c478bd9Sstevel@tonic-gate 	ps->ps_pcacheset = NULL;
22757c478bd9Sstevel@tonic-gate 	if (ps->ps_dpbuf != NULL) {
22767c478bd9Sstevel@tonic-gate 		kmem_free(ps->ps_dpbuf, ps->ps_dpbufsize * sizeof (pollfd_t));
22777c478bd9Sstevel@tonic-gate 		ps->ps_dpbuf = NULL;
22787c478bd9Sstevel@tonic-gate 	}
22797c478bd9Sstevel@tonic-gate 	mutex_destroy(&ps->ps_lock);
22807c478bd9Sstevel@tonic-gate 	kmem_free(ps, sizeof (pollstate_t));
22817c478bd9Sstevel@tonic-gate }
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate /*
22847c478bd9Sstevel@tonic-gate  * We are holding the appropriate uf_lock entering this routine.
22857c478bd9Sstevel@tonic-gate  * Bump up the ps_busy count to prevent the thread from exiting.
22867c478bd9Sstevel@tonic-gate  */
22877c478bd9Sstevel@tonic-gate void
22887c478bd9Sstevel@tonic-gate pollblockexit(fpollinfo_t *fpip)
22897c478bd9Sstevel@tonic-gate {
22907c478bd9Sstevel@tonic-gate 	for (; fpip; fpip = fpip->fp_next) {
22917c478bd9Sstevel@tonic-gate 		pollcache_t *pcp = fpip->fp_thread->t_pollstate->ps_pcache;
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate 		mutex_enter(&pcp->pc_no_exit);
22947c478bd9Sstevel@tonic-gate 		pcp->pc_busy++;  /* prevents exit()'s */
22957c478bd9Sstevel@tonic-gate 		mutex_exit(&pcp->pc_no_exit);
22967c478bd9Sstevel@tonic-gate 	}
22977c478bd9Sstevel@tonic-gate }
22987c478bd9Sstevel@tonic-gate 
22997c478bd9Sstevel@tonic-gate /*
23007c478bd9Sstevel@tonic-gate  * Complete phase 2 of cached poll fd cleanup. Call pcache_clean_entry to mark
23017c478bd9Sstevel@tonic-gate  * the pcacheset events field POLLCLOSED to force the next poll() to remove
23027c478bd9Sstevel@tonic-gate  * this cache entry. We can't clean the polldat entry clean up here because
23037c478bd9Sstevel@tonic-gate  * lwp block in poll() needs the info to return. Wakeup anyone blocked in
23047c478bd9Sstevel@tonic-gate  * poll and let exiting lwp go. No lock is help upon entry. So it's OK for
23057c478bd9Sstevel@tonic-gate  * pcache_clean_entry to call pollwakeup().
23067c478bd9Sstevel@tonic-gate  */
23077c478bd9Sstevel@tonic-gate void
23087c478bd9Sstevel@tonic-gate pollcacheclean(fpollinfo_t *fip, int fd)
23097c478bd9Sstevel@tonic-gate {
23107c478bd9Sstevel@tonic-gate 	struct fpollinfo	*fpip, *fpip2;
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate 	fpip = fip;
23137c478bd9Sstevel@tonic-gate 	while (fpip) {
23147c478bd9Sstevel@tonic-gate 		pollstate_t *ps = fpip->fp_thread->t_pollstate;
23157c478bd9Sstevel@tonic-gate 		pollcache_t *pcp = ps->ps_pcache;
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate 		mutex_enter(&ps->ps_lock);
23187c478bd9Sstevel@tonic-gate 		pcache_clean_entry(ps, fd);
23197c478bd9Sstevel@tonic-gate 		mutex_exit(&ps->ps_lock);
23207c478bd9Sstevel@tonic-gate 		mutex_enter(&pcp->pc_no_exit);
23217c478bd9Sstevel@tonic-gate 		pcp->pc_busy--;
23227c478bd9Sstevel@tonic-gate 		if (pcp->pc_busy == 0) {
23237c478bd9Sstevel@tonic-gate 			/*
23247c478bd9Sstevel@tonic-gate 			 * Wakeup the thread waiting in
23257c478bd9Sstevel@tonic-gate 			 * thread_exit().
23267c478bd9Sstevel@tonic-gate 			 */
23277c478bd9Sstevel@tonic-gate 			cv_signal(&pcp->pc_busy_cv);
23287c478bd9Sstevel@tonic-gate 		}
23297c478bd9Sstevel@tonic-gate 		mutex_exit(&pcp->pc_no_exit);
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate 		fpip2 = fpip;
23327c478bd9Sstevel@tonic-gate 		fpip = fpip->fp_next;
23337c478bd9Sstevel@tonic-gate 		kmem_free(fpip2, sizeof (fpollinfo_t));
23347c478bd9Sstevel@tonic-gate 	}
23357c478bd9Sstevel@tonic-gate }
23367c478bd9Sstevel@tonic-gate 
23377c478bd9Sstevel@tonic-gate /*
23387c478bd9Sstevel@tonic-gate  * one of the cache line's counter is wrapping around. Reset all cache line
23397c478bd9Sstevel@tonic-gate  * counters to zero except one. This is simplistic, but probably works
23407c478bd9Sstevel@tonic-gate  * effectively.
23417c478bd9Sstevel@tonic-gate  */
23427c478bd9Sstevel@tonic-gate void
23437c478bd9Sstevel@tonic-gate pcacheset_reset_count(pollstate_t *ps, int index)
23447c478bd9Sstevel@tonic-gate {
23457c478bd9Sstevel@tonic-gate 	int	i;
23467c478bd9Sstevel@tonic-gate 
23477c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ps->ps_lock));
23487c478bd9Sstevel@tonic-gate 	for (i = 0; i < ps->ps_nsets; i++) {
23497c478bd9Sstevel@tonic-gate 		if (ps->ps_pcacheset[i].pcs_pollfd != NULL) {
23507c478bd9Sstevel@tonic-gate 			ps->ps_pcacheset[i].pcs_count = 0;
23517c478bd9Sstevel@tonic-gate 		}
23527c478bd9Sstevel@tonic-gate 	}
23537c478bd9Sstevel@tonic-gate 	ps->ps_pcacheset[index].pcs_count = 1;
23547c478bd9Sstevel@tonic-gate }
23557c478bd9Sstevel@tonic-gate 
23567c478bd9Sstevel@tonic-gate /*
23577c478bd9Sstevel@tonic-gate  * this routine implements poll cache list replacement policy.
23587c478bd9Sstevel@tonic-gate  * It is currently choose the "least used".
23597c478bd9Sstevel@tonic-gate  */
23607c478bd9Sstevel@tonic-gate int
23617c478bd9Sstevel@tonic-gate pcacheset_replace(pollstate_t *ps)
23627c478bd9Sstevel@tonic-gate {
23637c478bd9Sstevel@tonic-gate 	int i;
23647c478bd9Sstevel@tonic-gate 	int index = 0;
23657c478bd9Sstevel@tonic-gate 
23667c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ps->ps_lock));
23677c478bd9Sstevel@tonic-gate 	for (i = 1; i < ps->ps_nsets; i++) {
23687c478bd9Sstevel@tonic-gate 		if (ps->ps_pcacheset[index].pcs_count >
23697c478bd9Sstevel@tonic-gate 		    ps->ps_pcacheset[i].pcs_count) {
23707c478bd9Sstevel@tonic-gate 			index = i;
23717c478bd9Sstevel@tonic-gate 		}
23727c478bd9Sstevel@tonic-gate 	}
23737c478bd9Sstevel@tonic-gate 	ps->ps_pcacheset[index].pcs_count = 0;
23747c478bd9Sstevel@tonic-gate 	return (index);
23757c478bd9Sstevel@tonic-gate }
23767c478bd9Sstevel@tonic-gate 
23777c478bd9Sstevel@tonic-gate /*
23787c478bd9Sstevel@tonic-gate  * this routine is called by strclose to remove remaining polldat struct on
23797c478bd9Sstevel@tonic-gate  * the pollhead list of the device being closed. There are two reasons as why
23807c478bd9Sstevel@tonic-gate  * the polldat structures still remain on the pollhead list:
23817c478bd9Sstevel@tonic-gate  *
23827c478bd9Sstevel@tonic-gate  * (1) The layered device(e.g.the console driver).
23837c478bd9Sstevel@tonic-gate  * In this case, the existence of a polldat implies that the thread putting
23847c478bd9Sstevel@tonic-gate  * the polldat on this list has not exited yet. Before the thread exits, it
23857c478bd9Sstevel@tonic-gate  * will have to hold this pollhead lock to remove the polldat. So holding the
23867c478bd9Sstevel@tonic-gate  * pollhead lock here effectively prevents the thread which put the polldat
23877c478bd9Sstevel@tonic-gate  * on this list from exiting.
23887c478bd9Sstevel@tonic-gate  *
23897c478bd9Sstevel@tonic-gate  * (2) /dev/poll.
23907c478bd9Sstevel@tonic-gate  * When a polled fd is cached in /dev/poll, its polldat will remain on the
23917c478bd9Sstevel@tonic-gate  * pollhead list if the process has not done a POLLREMOVE before closing the
23927c478bd9Sstevel@tonic-gate  * polled fd. We just unlink it here.
23937c478bd9Sstevel@tonic-gate  */
23947c478bd9Sstevel@tonic-gate void
23957c478bd9Sstevel@tonic-gate pollhead_clean(pollhead_t *php)
23967c478bd9Sstevel@tonic-gate {
23977c478bd9Sstevel@tonic-gate 	polldat_t	*pdp;
23987c478bd9Sstevel@tonic-gate 
23997c478bd9Sstevel@tonic-gate 	/*
24007c478bd9Sstevel@tonic-gate 	 * In case(1), while we must prevent the thread in question from
24017c478bd9Sstevel@tonic-gate 	 * exiting, we must also obey the proper locking order, i.e.
24027c478bd9Sstevel@tonic-gate 	 * (ps_lock -> phlock).
24037c478bd9Sstevel@tonic-gate 	 */
24047c478bd9Sstevel@tonic-gate 	PH_ENTER(php);
24057c478bd9Sstevel@tonic-gate 	while (php->ph_list != NULL) {
24067c478bd9Sstevel@tonic-gate 		pollstate_t	*ps;
24077c478bd9Sstevel@tonic-gate 		pollcache_t	*pcp;
24087c478bd9Sstevel@tonic-gate 
24097c478bd9Sstevel@tonic-gate 		pdp = php->ph_list;
24107c478bd9Sstevel@tonic-gate 		ASSERT(pdp->pd_php == php);
24117c478bd9Sstevel@tonic-gate 		if (pdp->pd_thread == NULL) {
24127c478bd9Sstevel@tonic-gate 			/*
24137c478bd9Sstevel@tonic-gate 			 * This is case(2). Since the ph_lock is sufficient
24147c478bd9Sstevel@tonic-gate 			 * to synchronize this lwp with any other /dev/poll
24157c478bd9Sstevel@tonic-gate 			 * lwp, just unlink the polldat.
24167c478bd9Sstevel@tonic-gate 			 */
24177c478bd9Sstevel@tonic-gate 			php->ph_list = pdp->pd_next;
24187c478bd9Sstevel@tonic-gate 			pdp->pd_php = NULL;
24197c478bd9Sstevel@tonic-gate 			pdp->pd_next = NULL;
24207c478bd9Sstevel@tonic-gate 			continue;
24217c478bd9Sstevel@tonic-gate 		}
24227c478bd9Sstevel@tonic-gate 		ps = pdp->pd_thread->t_pollstate;
24237c478bd9Sstevel@tonic-gate 		ASSERT(ps != NULL);
24247c478bd9Sstevel@tonic-gate 		pcp = pdp->pd_pcache;
24257c478bd9Sstevel@tonic-gate 		ASSERT(pcp != NULL);
24267c478bd9Sstevel@tonic-gate 		mutex_enter(&pcp->pc_no_exit);
24277c478bd9Sstevel@tonic-gate 		pcp->pc_busy++;  /* prevents exit()'s */
24287c478bd9Sstevel@tonic-gate 		mutex_exit(&pcp->pc_no_exit);
24297c478bd9Sstevel@tonic-gate 		/*
24307c478bd9Sstevel@tonic-gate 		 * Now get the locks in proper order to avoid deadlock.
24317c478bd9Sstevel@tonic-gate 		 */
24327c478bd9Sstevel@tonic-gate 		PH_EXIT(php);
24337c478bd9Sstevel@tonic-gate 		mutex_enter(&ps->ps_lock);
24347c478bd9Sstevel@tonic-gate 		/*
24357c478bd9Sstevel@tonic-gate 		 * while we dropped the pollhead lock, the element could be
24367c478bd9Sstevel@tonic-gate 		 * taken off the list already.
24377c478bd9Sstevel@tonic-gate 		 */
24387c478bd9Sstevel@tonic-gate 		PH_ENTER(php);
24397c478bd9Sstevel@tonic-gate 		if (pdp->pd_php == php) {
24407c478bd9Sstevel@tonic-gate 			ASSERT(pdp == php->ph_list);
24417c478bd9Sstevel@tonic-gate 			php->ph_list = pdp->pd_next;
24427c478bd9Sstevel@tonic-gate 			pdp->pd_php = NULL;
24437c478bd9Sstevel@tonic-gate 			pdp->pd_next = NULL;
24447c478bd9Sstevel@tonic-gate 		}
24457c478bd9Sstevel@tonic-gate 		PH_EXIT(php);
24467c478bd9Sstevel@tonic-gate 		mutex_exit(&ps->ps_lock);
24477c478bd9Sstevel@tonic-gate 		mutex_enter(&pcp->pc_no_exit);
24487c478bd9Sstevel@tonic-gate 		pcp->pc_busy--;
24497c478bd9Sstevel@tonic-gate 		if (pcp->pc_busy == 0) {
24507c478bd9Sstevel@tonic-gate 			/*
24517c478bd9Sstevel@tonic-gate 			 * Wakeup the thread waiting in
24527c478bd9Sstevel@tonic-gate 			 * thread_exit().
24537c478bd9Sstevel@tonic-gate 			 */
24547c478bd9Sstevel@tonic-gate 			cv_signal(&pcp->pc_busy_cv);
24557c478bd9Sstevel@tonic-gate 		}
24567c478bd9Sstevel@tonic-gate 		mutex_exit(&pcp->pc_no_exit);
24577c478bd9Sstevel@tonic-gate 		PH_ENTER(php);
24587c478bd9Sstevel@tonic-gate 	}
24597c478bd9Sstevel@tonic-gate 	PH_EXIT(php);
24607c478bd9Sstevel@tonic-gate }
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate /*
24637c478bd9Sstevel@tonic-gate  * The remove_list is called to cleanup a partially cached 'current' list or
24647c478bd9Sstevel@tonic-gate  * to remove a partial list which is no longer cached. The flag value of 1
24657c478bd9Sstevel@tonic-gate  * indicates the second case.
24667c478bd9Sstevel@tonic-gate  */
24677c478bd9Sstevel@tonic-gate void
24687c478bd9Sstevel@tonic-gate pcacheset_remove_list(pollstate_t *ps, pollfd_t *pollfdp, int start, int end,
24697c478bd9Sstevel@tonic-gate     int cacheindex, int flag)
24707c478bd9Sstevel@tonic-gate {
24717c478bd9Sstevel@tonic-gate 	int i;
24727c478bd9Sstevel@tonic-gate 
24737c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ps->ps_lock));
24747c478bd9Sstevel@tonic-gate 	for (i = start; i < end; i++) {
24757c478bd9Sstevel@tonic-gate 		if ((pollfdp[i].fd >= 0) &&
24767c478bd9Sstevel@tonic-gate 		    (flag || !(pollfdp[i].revents & POLLNVAL))) {
24777c478bd9Sstevel@tonic-gate 			if (pcache_delete_fd(ps, pollfdp[i].fd, i, cacheindex,
24787c478bd9Sstevel@tonic-gate 			    (uint_t)pollfdp[i].events)) {
24797c478bd9Sstevel@tonic-gate 				int j;
24807c478bd9Sstevel@tonic-gate 				int fd = pollfdp[i].fd;
24817c478bd9Sstevel@tonic-gate 
24827c478bd9Sstevel@tonic-gate 				for (j = i + 1; j < end; j++) {
24837c478bd9Sstevel@tonic-gate 					if (pollfdp[j].fd == fd) {
24847c478bd9Sstevel@tonic-gate 						pcache_update_xref(
24857c478bd9Sstevel@tonic-gate 						    ps->ps_pcache, fd,
24867c478bd9Sstevel@tonic-gate 						    (ssize_t)j, cacheindex);
24877c478bd9Sstevel@tonic-gate 						break;
24887c478bd9Sstevel@tonic-gate 					}
24897c478bd9Sstevel@tonic-gate 				}
24907c478bd9Sstevel@tonic-gate 				ASSERT(j <= end);
24917c478bd9Sstevel@tonic-gate 			}
24927c478bd9Sstevel@tonic-gate 		}
24937c478bd9Sstevel@tonic-gate 	}
24947c478bd9Sstevel@tonic-gate }
24957c478bd9Sstevel@tonic-gate 
24967c478bd9Sstevel@tonic-gate #ifdef DEBUG
24977c478bd9Sstevel@tonic-gate 
24987c478bd9Sstevel@tonic-gate #include<sys/strsubr.h>
24997c478bd9Sstevel@tonic-gate /*
25007c478bd9Sstevel@tonic-gate  * make sure curthread is not on anyone's pollhead list any more.
25017c478bd9Sstevel@tonic-gate  */
25027c478bd9Sstevel@tonic-gate static void
25037c478bd9Sstevel@tonic-gate pollcheckphlist()
25047c478bd9Sstevel@tonic-gate {
25057c478bd9Sstevel@tonic-gate 	int i;
25067c478bd9Sstevel@tonic-gate 	file_t *fp;
25077c478bd9Sstevel@tonic-gate 	uf_entry_t *ufp;
25087c478bd9Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(curproc);
25097c478bd9Sstevel@tonic-gate 	struct stdata *stp;
25107c478bd9Sstevel@tonic-gate 	polldat_t *pdp;
25117c478bd9Sstevel@tonic-gate 
25127c478bd9Sstevel@tonic-gate 	mutex_enter(&fip->fi_lock);
25137c478bd9Sstevel@tonic-gate 	for (i = 0; i < fip->fi_nfiles; i++) {
25147c478bd9Sstevel@tonic-gate 		UF_ENTER(ufp, fip, i);
25157c478bd9Sstevel@tonic-gate 		if ((fp = ufp->uf_file) != NULL) {
25167c478bd9Sstevel@tonic-gate 			if ((stp = fp->f_vnode->v_stream) != NULL) {
25177c478bd9Sstevel@tonic-gate 				PH_ENTER(&stp->sd_pollist);
25187c478bd9Sstevel@tonic-gate 				pdp = stp->sd_pollist.ph_list;
25197c478bd9Sstevel@tonic-gate 				while (pdp) {
25207c478bd9Sstevel@tonic-gate 					ASSERT(pdp->pd_thread != curthread);
25217c478bd9Sstevel@tonic-gate 					pdp = pdp->pd_next;
25227c478bd9Sstevel@tonic-gate 				}
25237c478bd9Sstevel@tonic-gate 				PH_EXIT(&stp->sd_pollist);
25247c478bd9Sstevel@tonic-gate 			}
25257c478bd9Sstevel@tonic-gate 		}
25267c478bd9Sstevel@tonic-gate 		UF_EXIT(ufp);
25277c478bd9Sstevel@tonic-gate 	}
25287c478bd9Sstevel@tonic-gate 	mutex_exit(&fip->fi_lock);
25297c478bd9Sstevel@tonic-gate }
25307c478bd9Sstevel@tonic-gate 
25317c478bd9Sstevel@tonic-gate /*
25327c478bd9Sstevel@tonic-gate  * for resolved set poll list, the xref info in the pcache should be
25337c478bd9Sstevel@tonic-gate  * consistent with this poll list.
25347c478bd9Sstevel@tonic-gate  */
25357c478bd9Sstevel@tonic-gate static int
25367c478bd9Sstevel@tonic-gate pollcheckxref(pollstate_t *ps, int cacheindex)
25377c478bd9Sstevel@tonic-gate {
25387c478bd9Sstevel@tonic-gate 	pollfd_t *pollfdp = ps->ps_pcacheset[cacheindex].pcs_pollfd;
25397c478bd9Sstevel@tonic-gate 	pollcache_t *pcp = ps->ps_pcache;
25407c478bd9Sstevel@tonic-gate 	polldat_t *pdp;
25417c478bd9Sstevel@tonic-gate 	int	i;
25427c478bd9Sstevel@tonic-gate 	xref_t	*refp;
25437c478bd9Sstevel@tonic-gate 
25447c478bd9Sstevel@tonic-gate 	for (i = 0; i < ps->ps_pcacheset[cacheindex].pcs_nfds; i++) {
25457c478bd9Sstevel@tonic-gate 		if (pollfdp[i].fd < 0) {
25467c478bd9Sstevel@tonic-gate 			continue;
25477c478bd9Sstevel@tonic-gate 		}
25487c478bd9Sstevel@tonic-gate 		pdp = pcache_lookup_fd(pcp, pollfdp[i].fd);
25497c478bd9Sstevel@tonic-gate 		ASSERT(pdp != NULL);
25507c478bd9Sstevel@tonic-gate 		ASSERT(pdp->pd_ref != NULL);
25517c478bd9Sstevel@tonic-gate 		refp = &pdp->pd_ref[cacheindex];
25527c478bd9Sstevel@tonic-gate 		if (refp->xf_position >= 0) {
25537c478bd9Sstevel@tonic-gate 			ASSERT(refp->xf_refcnt >= 1);
25547c478bd9Sstevel@tonic-gate 			ASSERT(pollfdp[refp->xf_position].fd == pdp->pd_fd);
25557c478bd9Sstevel@tonic-gate 			if (refp->xf_refcnt > 1) {
25567c478bd9Sstevel@tonic-gate 				int	j;
25577c478bd9Sstevel@tonic-gate 				int	count = 0;
25587c478bd9Sstevel@tonic-gate 
25597c478bd9Sstevel@tonic-gate 				for (j = refp->xf_position;
25607c478bd9Sstevel@tonic-gate 				    j < ps->ps_pcacheset[cacheindex].pcs_nfds;
25617c478bd9Sstevel@tonic-gate 				    j++) {
25627c478bd9Sstevel@tonic-gate 					if (pollfdp[j].fd == pdp->pd_fd) {
25637c478bd9Sstevel@tonic-gate 						count++;
25647c478bd9Sstevel@tonic-gate 					}
25657c478bd9Sstevel@tonic-gate 				}
25667c478bd9Sstevel@tonic-gate 				ASSERT(count == refp->xf_refcnt);
25677c478bd9Sstevel@tonic-gate 			}
25687c478bd9Sstevel@tonic-gate 		}
25697c478bd9Sstevel@tonic-gate 	}
25707c478bd9Sstevel@tonic-gate 	return (1);
25717c478bd9Sstevel@tonic-gate }
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate /*
25747c478bd9Sstevel@tonic-gate  * For every cached pollfd, its polldat struct should be consistent with
25757c478bd9Sstevel@tonic-gate  * what is in the pcacheset lists.
25767c478bd9Sstevel@tonic-gate  */
25777c478bd9Sstevel@tonic-gate static void
25787c478bd9Sstevel@tonic-gate checkpolldat(pollstate_t *ps)
25797c478bd9Sstevel@tonic-gate {
25807c478bd9Sstevel@tonic-gate 	pollcache_t	*pcp = ps->ps_pcache;
25817c478bd9Sstevel@tonic-gate 	polldat_t	**hashtbl;
25827c478bd9Sstevel@tonic-gate 	int		i;
25837c478bd9Sstevel@tonic-gate 
25847c478bd9Sstevel@tonic-gate 	hashtbl = pcp->pc_hash;
25857c478bd9Sstevel@tonic-gate 	for (i = 0; i < pcp->pc_hashsize; i++) {
25867c478bd9Sstevel@tonic-gate 		polldat_t	*pdp;
25877c478bd9Sstevel@tonic-gate 
25887c478bd9Sstevel@tonic-gate 		for (pdp = hashtbl[i]; pdp; pdp = pdp->pd_hashnext) {
25897c478bd9Sstevel@tonic-gate 			ASSERT(pdp->pd_ref != NULL);
25907c478bd9Sstevel@tonic-gate 			if (pdp->pd_count > 0) {
25917c478bd9Sstevel@tonic-gate 				xref_t		*refp;
25927c478bd9Sstevel@tonic-gate 				int		j;
25937c478bd9Sstevel@tonic-gate 				pollcacheset_t	*pcsp;
25947c478bd9Sstevel@tonic-gate 				pollfd_t	*pollfd;
25957c478bd9Sstevel@tonic-gate 
25967c478bd9Sstevel@tonic-gate 				for (j = 0; j < ps->ps_nsets; j++) {
25977c478bd9Sstevel@tonic-gate 					refp = &pdp->pd_ref[j];
25987c478bd9Sstevel@tonic-gate 					if (refp->xf_refcnt > 0) {
25997c478bd9Sstevel@tonic-gate 						pcsp = &ps->ps_pcacheset[j];
26007c478bd9Sstevel@tonic-gate 				ASSERT(refp->xf_position < pcsp->pcs_nfds);
26017c478bd9Sstevel@tonic-gate 						pollfd = pcsp->pcs_pollfd;
26027c478bd9Sstevel@tonic-gate 			ASSERT(pdp->pd_fd == pollfd[refp->xf_position].fd);
26037c478bd9Sstevel@tonic-gate 					}
26047c478bd9Sstevel@tonic-gate 				}
26057c478bd9Sstevel@tonic-gate 			}
26067c478bd9Sstevel@tonic-gate 		}
26077c478bd9Sstevel@tonic-gate 	}
26087c478bd9Sstevel@tonic-gate }
26097c478bd9Sstevel@tonic-gate 
26107c478bd9Sstevel@tonic-gate /*
26117c478bd9Sstevel@tonic-gate  * every wfd element on ph_list must have a corresponding fpollinfo on the
26127c478bd9Sstevel@tonic-gate  * uf_fpollinfo list. This is a variation of infpollinfo() w/o holding locks.
26137c478bd9Sstevel@tonic-gate  */
26147c478bd9Sstevel@tonic-gate void
26157c478bd9Sstevel@tonic-gate checkwfdlist(vnode_t *vp, fpollinfo_t *fpip)
26167c478bd9Sstevel@tonic-gate {
26177c478bd9Sstevel@tonic-gate 	stdata_t *stp;
26187c478bd9Sstevel@tonic-gate 	polldat_t *pdp;
26197c478bd9Sstevel@tonic-gate 	fpollinfo_t *fpip2;
26207c478bd9Sstevel@tonic-gate 
26217c478bd9Sstevel@tonic-gate 	if ((stp = vp->v_stream) == NULL) {
26227c478bd9Sstevel@tonic-gate 		return;
26237c478bd9Sstevel@tonic-gate 	}
26247c478bd9Sstevel@tonic-gate 	PH_ENTER(&stp->sd_pollist);
26257c478bd9Sstevel@tonic-gate 	for (pdp = stp->sd_pollist.ph_list; pdp; pdp = pdp->pd_next) {
26267c478bd9Sstevel@tonic-gate 		if (pdp->pd_thread->t_procp == curthread->t_procp) {
26277c478bd9Sstevel@tonic-gate 			for (fpip2 = fpip; fpip2; fpip2 = fpip2->fp_next) {
26287c478bd9Sstevel@tonic-gate 				if (pdp->pd_thread == fpip2->fp_thread) {
26297c478bd9Sstevel@tonic-gate 					break;
26307c478bd9Sstevel@tonic-gate 				}
26317c478bd9Sstevel@tonic-gate 			}
26327c478bd9Sstevel@tonic-gate 			ASSERT(fpip2 != NULL);
26337c478bd9Sstevel@tonic-gate 		}
26347c478bd9Sstevel@tonic-gate 	}
26357c478bd9Sstevel@tonic-gate 	PH_EXIT(&stp->sd_pollist);
26367c478bd9Sstevel@tonic-gate }
26377c478bd9Sstevel@tonic-gate 
26387c478bd9Sstevel@tonic-gate /*
26397c478bd9Sstevel@tonic-gate  * For each cached fd whose bit is not set in bitmap, its revents field in
26407c478bd9Sstevel@tonic-gate  * current poll list should be 0.
26417c478bd9Sstevel@tonic-gate  */
26427c478bd9Sstevel@tonic-gate static int
26437c478bd9Sstevel@tonic-gate pollcheckrevents(pollstate_t *ps, int begin, int end, int cacheindex)
26447c478bd9Sstevel@tonic-gate {
26457c478bd9Sstevel@tonic-gate 	pollcache_t	*pcp = ps->ps_pcache;
26467c478bd9Sstevel@tonic-gate 	pollfd_t	*pollfdp = ps->ps_pollfd;
26477c478bd9Sstevel@tonic-gate 	int		i;
26487c478bd9Sstevel@tonic-gate 
26497c478bd9Sstevel@tonic-gate 	for (i = begin; i < end; i++) {
26507c478bd9Sstevel@tonic-gate 		polldat_t	*pdp;
26517c478bd9Sstevel@tonic-gate 
26527c478bd9Sstevel@tonic-gate 		ASSERT(!BT_TEST(pcp->pc_bitmap, i));
26537c478bd9Sstevel@tonic-gate 		pdp = pcache_lookup_fd(pcp, i);
26547c478bd9Sstevel@tonic-gate 		if (pdp && pdp->pd_fp != NULL) {
26557c478bd9Sstevel@tonic-gate 			xref_t *refp;
26567c478bd9Sstevel@tonic-gate 			int entry;
26577c478bd9Sstevel@tonic-gate 
26587c478bd9Sstevel@tonic-gate 			ASSERT(pdp->pd_ref != NULL);
26597c478bd9Sstevel@tonic-gate 			refp = &pdp->pd_ref[cacheindex];
26607c478bd9Sstevel@tonic-gate 			if (refp->xf_refcnt == 0) {
26617c478bd9Sstevel@tonic-gate 				continue;
26627c478bd9Sstevel@tonic-gate 			}
26637c478bd9Sstevel@tonic-gate 			entry = refp->xf_position;
26647c478bd9Sstevel@tonic-gate 			ASSERT(entry >= 0);
26657c478bd9Sstevel@tonic-gate 			ASSERT(pollfdp[entry].revents == 0);
26667c478bd9Sstevel@tonic-gate 			if (refp->xf_refcnt > 1) {
26677c478bd9Sstevel@tonic-gate 				int j;
26687c478bd9Sstevel@tonic-gate 
26697c478bd9Sstevel@tonic-gate 				for (j = entry + 1; j < ps->ps_nfds; j++) {
26707c478bd9Sstevel@tonic-gate 					if (pollfdp[j].fd == i) {
26717c478bd9Sstevel@tonic-gate 						ASSERT(pollfdp[j].revents == 0);
26727c478bd9Sstevel@tonic-gate 					}
26737c478bd9Sstevel@tonic-gate 				}
26747c478bd9Sstevel@tonic-gate 			}
26757c478bd9Sstevel@tonic-gate 		}
26767c478bd9Sstevel@tonic-gate 	}
26777c478bd9Sstevel@tonic-gate 	return (1);
26787c478bd9Sstevel@tonic-gate }
26797c478bd9Sstevel@tonic-gate 
26807c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
26817c478bd9Sstevel@tonic-gate 
26827c478bd9Sstevel@tonic-gate pollcache_t *
26837c478bd9Sstevel@tonic-gate pcache_alloc()
26847c478bd9Sstevel@tonic-gate {
26857c478bd9Sstevel@tonic-gate 	return (kmem_zalloc(sizeof (pollcache_t), KM_SLEEP));
26867c478bd9Sstevel@tonic-gate }
26877c478bd9Sstevel@tonic-gate 
26887c478bd9Sstevel@tonic-gate void
26897c478bd9Sstevel@tonic-gate pcache_create(pollcache_t *pcp, nfds_t nfds)
26907c478bd9Sstevel@tonic-gate {
26917c478bd9Sstevel@tonic-gate 	size_t	mapsize;
26927c478bd9Sstevel@tonic-gate 
26937c478bd9Sstevel@tonic-gate 	/*
26947c478bd9Sstevel@tonic-gate 	 * allocate enough bits for the poll fd list
26957c478bd9Sstevel@tonic-gate 	 */
26967c478bd9Sstevel@tonic-gate 	if ((mapsize = POLLMAPCHUNK) <= nfds) {
26977c478bd9Sstevel@tonic-gate 		mapsize = (nfds + POLLMAPCHUNK - 1) & ~(POLLMAPCHUNK - 1);
26987c478bd9Sstevel@tonic-gate 	}
26997c478bd9Sstevel@tonic-gate 	pcp->pc_bitmap = kmem_zalloc((mapsize / BT_NBIPUL) * sizeof (ulong_t),
27007c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
27017c478bd9Sstevel@tonic-gate 	pcp->pc_mapsize = mapsize;
27027c478bd9Sstevel@tonic-gate 	/*
27037c478bd9Sstevel@tonic-gate 	 * The hash size is at least POLLHASHCHUNKSZ. If user polls a large
27047c478bd9Sstevel@tonic-gate 	 * number of fd to start with, allocate a bigger hash table (to the
27057c478bd9Sstevel@tonic-gate 	 * nearest multiple of POLLHASHCHUNKSZ) because dynamically growing a
27067c478bd9Sstevel@tonic-gate 	 * hash table is expensive.
27077c478bd9Sstevel@tonic-gate 	 */
27087c478bd9Sstevel@tonic-gate 	if (nfds < POLLHASHCHUNKSZ) {
27097c478bd9Sstevel@tonic-gate 		pcp->pc_hashsize = POLLHASHCHUNKSZ;
27107c478bd9Sstevel@tonic-gate 	} else {
27117c478bd9Sstevel@tonic-gate 		pcp->pc_hashsize = (nfds + POLLHASHCHUNKSZ - 1) &
27127c478bd9Sstevel@tonic-gate 		    ~(POLLHASHCHUNKSZ - 1);
27137c478bd9Sstevel@tonic-gate 	}
27147c478bd9Sstevel@tonic-gate 	pcp->pc_hash = kmem_zalloc(pcp->pc_hashsize * sizeof (polldat_t *),
27157c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
27167c478bd9Sstevel@tonic-gate }
27177c478bd9Sstevel@tonic-gate 
27187c478bd9Sstevel@tonic-gate void
27197c478bd9Sstevel@tonic-gate pcache_destroy(pollcache_t *pcp)
27207c478bd9Sstevel@tonic-gate {
27217c478bd9Sstevel@tonic-gate 	polldat_t	**hashtbl;
27227c478bd9Sstevel@tonic-gate 	int i;
27237c478bd9Sstevel@tonic-gate 
27247c478bd9Sstevel@tonic-gate 	hashtbl = pcp->pc_hash;
27257c478bd9Sstevel@tonic-gate 	for (i = 0; i < pcp->pc_hashsize; i++) {
27267c478bd9Sstevel@tonic-gate 		if (hashtbl[i] != NULL) {
27277c478bd9Sstevel@tonic-gate 			polldat_t *pdp, *pdp2;
27287c478bd9Sstevel@tonic-gate 
27297c478bd9Sstevel@tonic-gate 			pdp = hashtbl[i];
27307c478bd9Sstevel@tonic-gate 			while (pdp != NULL) {
27317c478bd9Sstevel@tonic-gate 				pdp2 = pdp->pd_hashnext;
27327c478bd9Sstevel@tonic-gate 				if (pdp->pd_ref != NULL) {
27337c478bd9Sstevel@tonic-gate 					kmem_free(pdp->pd_ref, sizeof (xref_t) *
27347c478bd9Sstevel@tonic-gate 					    pdp->pd_nsets);
27357c478bd9Sstevel@tonic-gate 				}
27367c478bd9Sstevel@tonic-gate 				kmem_free(pdp, sizeof (polldat_t));
27377c478bd9Sstevel@tonic-gate 				pdp = pdp2;
27387c478bd9Sstevel@tonic-gate 				pcp->pc_fdcount--;
27397c478bd9Sstevel@tonic-gate 			}
27407c478bd9Sstevel@tonic-gate 		}
27417c478bd9Sstevel@tonic-gate 	}
27427c478bd9Sstevel@tonic-gate 	ASSERT(pcp->pc_fdcount == 0);
27437c478bd9Sstevel@tonic-gate 	kmem_free(pcp->pc_hash, sizeof (polldat_t *) * pcp->pc_hashsize);
27447c478bd9Sstevel@tonic-gate 	kmem_free(pcp->pc_bitmap,
27457c478bd9Sstevel@tonic-gate 	    sizeof (ulong_t) * (pcp->pc_mapsize/BT_NBIPUL));
27467c478bd9Sstevel@tonic-gate 	mutex_destroy(&pcp->pc_no_exit);
27477c478bd9Sstevel@tonic-gate 	mutex_destroy(&pcp->pc_lock);
27487c478bd9Sstevel@tonic-gate 	cv_destroy(&pcp->pc_cv);
27497c478bd9Sstevel@tonic-gate 	cv_destroy(&pcp->pc_busy_cv);
27507c478bd9Sstevel@tonic-gate 	kmem_free(pcp, sizeof (pollcache_t));
27517c478bd9Sstevel@tonic-gate }
27527c478bd9Sstevel@tonic-gate 
27537c478bd9Sstevel@tonic-gate pollcacheset_t *
27547c478bd9Sstevel@tonic-gate pcacheset_create(int nsets)
27557c478bd9Sstevel@tonic-gate {
27567c478bd9Sstevel@tonic-gate 	return (kmem_zalloc(sizeof (pollcacheset_t) * nsets, KM_SLEEP));
27577c478bd9Sstevel@tonic-gate }
27587c478bd9Sstevel@tonic-gate 
27597c478bd9Sstevel@tonic-gate void
27607c478bd9Sstevel@tonic-gate pcacheset_destroy(pollcacheset_t *pcsp, int nsets)
27617c478bd9Sstevel@tonic-gate {
27627c478bd9Sstevel@tonic-gate 	int i;
27637c478bd9Sstevel@tonic-gate 
27647c478bd9Sstevel@tonic-gate 	for (i = 0; i < nsets; i++) {
27657c478bd9Sstevel@tonic-gate 		if (pcsp[i].pcs_pollfd != NULL) {
27667c478bd9Sstevel@tonic-gate 			kmem_free(pcsp[i].pcs_pollfd, pcsp[i].pcs_nfds *
27677c478bd9Sstevel@tonic-gate 			    sizeof (pollfd_t));
27687c478bd9Sstevel@tonic-gate 		}
27697c478bd9Sstevel@tonic-gate 	}
27707c478bd9Sstevel@tonic-gate 	kmem_free(pcsp, sizeof (pollcacheset_t) * nsets);
27717c478bd9Sstevel@tonic-gate }
27727c478bd9Sstevel@tonic-gate 
27737c478bd9Sstevel@tonic-gate /*
27747c478bd9Sstevel@tonic-gate  * Check each duplicated poll fd in the poll list. It may be necessary to
27757c478bd9Sstevel@tonic-gate  * VOP_POLL the same fd again using different poll events. getf() has been
27767c478bd9Sstevel@tonic-gate  * done by caller. This routine returns 0 if it can sucessfully process the
27777c478bd9Sstevel@tonic-gate  * entire poll fd list. It returns -1 if underlying vnode has changed during
27787c478bd9Sstevel@tonic-gate  * a VOP_POLL, in which case the caller has to repoll. It returns a positive
27797c478bd9Sstevel@tonic-gate  * value if VOP_POLL failed.
27807c478bd9Sstevel@tonic-gate  */
27817c478bd9Sstevel@tonic-gate static int
27827c478bd9Sstevel@tonic-gate plist_chkdupfd(file_t *fp, polldat_t *pdp, pollstate_t *psp, pollfd_t *pollfdp,
27837c478bd9Sstevel@tonic-gate     int entry, int *fdcntp)
27847c478bd9Sstevel@tonic-gate {
27857c478bd9Sstevel@tonic-gate 	int	i;
27867c478bd9Sstevel@tonic-gate 	int	fd;
27877c478bd9Sstevel@tonic-gate 	nfds_t	nfds = psp->ps_nfds;
27887c478bd9Sstevel@tonic-gate 
27897c478bd9Sstevel@tonic-gate 	fd = pollfdp[entry].fd;
27907c478bd9Sstevel@tonic-gate 	for (i = entry + 1; i < nfds; i++) {
27917c478bd9Sstevel@tonic-gate 		if (pollfdp[i].fd == fd) {
27927c478bd9Sstevel@tonic-gate 			if (pollfdp[i].events == pollfdp[entry].events) {
27937c478bd9Sstevel@tonic-gate 				if ((pollfdp[i].revents =
27947c478bd9Sstevel@tonic-gate 				    pollfdp[entry].revents) != 0) {
27957c478bd9Sstevel@tonic-gate 					(*fdcntp)++;
27967c478bd9Sstevel@tonic-gate 				}
27977c478bd9Sstevel@tonic-gate 			} else {
27987c478bd9Sstevel@tonic-gate 
27997c478bd9Sstevel@tonic-gate 				int	error;
28007c478bd9Sstevel@tonic-gate 				pollhead_t *php;
28017c478bd9Sstevel@tonic-gate 				pollcache_t *pcp = psp->ps_pcache;
28027c478bd9Sstevel@tonic-gate 
28037c478bd9Sstevel@tonic-gate 				/*
28047c478bd9Sstevel@tonic-gate 				 * the events are different. VOP_POLL on this
28057c478bd9Sstevel@tonic-gate 				 * fd so that we don't miss any revents.
28067c478bd9Sstevel@tonic-gate 				 */
28077c478bd9Sstevel@tonic-gate 				php = NULL;
28087c478bd9Sstevel@tonic-gate 				ASSERT(curthread->t_pollcache == NULL);
28097c478bd9Sstevel@tonic-gate 				error = VOP_POLL(fp->f_vnode,
28107c478bd9Sstevel@tonic-gate 				    pollfdp[i].events, 0,
2811da6c28aaSamw 				    &pollfdp[i].revents, &php, NULL);
28127c478bd9Sstevel@tonic-gate 				if (error) {
28137c478bd9Sstevel@tonic-gate 					return (error);
28147c478bd9Sstevel@tonic-gate 				}
28157c478bd9Sstevel@tonic-gate 				/*
28167c478bd9Sstevel@tonic-gate 				 * layered devices(e.g. console driver)
28177c478bd9Sstevel@tonic-gate 				 * may change the vnode and thus the pollhead
28187c478bd9Sstevel@tonic-gate 				 * pointer out from underneath us.
28197c478bd9Sstevel@tonic-gate 				 */
28207c478bd9Sstevel@tonic-gate 				if (php != NULL && pdp->pd_php != NULL &&
28217c478bd9Sstevel@tonic-gate 				    php != pdp->pd_php) {
28227c478bd9Sstevel@tonic-gate 					pollhead_delete(pdp->pd_php, pdp);
28237c478bd9Sstevel@tonic-gate 					pdp->pd_php = php;
28247c478bd9Sstevel@tonic-gate 					pollhead_insert(php, pdp);
28257c478bd9Sstevel@tonic-gate 					/*
28267c478bd9Sstevel@tonic-gate 					 * We could have missed a wakeup on the
28277c478bd9Sstevel@tonic-gate 					 * new target device. Make sure the new
28287c478bd9Sstevel@tonic-gate 					 * target gets polled once.
28297c478bd9Sstevel@tonic-gate 					 */
28307c478bd9Sstevel@tonic-gate 					BT_SET(pcp->pc_bitmap, fd);
28317c478bd9Sstevel@tonic-gate 					return (-1);
28327c478bd9Sstevel@tonic-gate 				}
28337c478bd9Sstevel@tonic-gate 				if (pollfdp[i].revents) {
28347c478bd9Sstevel@tonic-gate 					(*fdcntp)++;
28357c478bd9Sstevel@tonic-gate 				}
28367c478bd9Sstevel@tonic-gate 			}
28377c478bd9Sstevel@tonic-gate 		}
28387c478bd9Sstevel@tonic-gate 	}
28397c478bd9Sstevel@tonic-gate 	return (0);
28407c478bd9Sstevel@tonic-gate }
2841