xref: /titanic_52/usr/src/uts/common/io/devpoll.c (revision 9b14cf1dc76dad8f2d9a74310e0a0200af96d217)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/devops.h>
30 #include <sys/conf.h>
31 #include <sys/modctl.h>
32 #include <sys/sunddi.h>
33 #include <sys/stat.h>
34 #include <sys/poll_impl.h>
35 #include <sys/errno.h>
36 #include <sys/kmem.h>
37 #include <sys/mkdev.h>
38 #include <sys/debug.h>
39 #include <sys/file.h>
40 #include <sys/sysmacros.h>
41 #include <sys/systm.h>
42 #include <sys/bitmap.h>
43 #include <sys/devpoll.h>
44 #include <sys/rctl.h>
45 #include <sys/resource.h>
46 
47 #define	RESERVED	1
48 
49 /* local data struct */
50 static	dp_entry_t	**devpolltbl; 	/* dev poll entries */
51 static	size_t		dptblsize;
52 
53 static	kmutex_t	devpoll_lock;	/* lock protecting dev tbl */
54 int			devpoll_init;	/* is /dev/poll initialized already */
55 
56 /* device local functions */
57 
58 static int dpopen(dev_t *devp, int flag, int otyp, cred_t *credp);
59 static int dpwrite(dev_t dev, struct uio *uiop, cred_t *credp);
60 static int dpioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
61     int *rvalp);
62 static int dppoll(dev_t dev, short events, int anyyet, short *reventsp,
63     struct pollhead **phpp);
64 static int dpclose(dev_t dev, int flag, int otyp, cred_t *credp);
65 static dev_info_t *dpdevi;
66 
67 
68 static struct cb_ops    dp_cb_ops = {
69 	dpopen,			/* open */
70 	dpclose,		/* close */
71 	nodev,			/* strategy */
72 	nodev,			/* print */
73 	nodev,			/* dump */
74 	nodev,			/* read */
75 	dpwrite,		/* write */
76 	dpioctl,		/* ioctl */
77 	nodev,			/* devmap */
78 	nodev,			/* mmap */
79 	nodev,			/* segmap */
80 	dppoll,			/* poll */
81 	ddi_prop_op,		/* prop_op */
82 	(struct streamtab *)0,	/* streamtab */
83 	D_MP,			/* flags */
84 	CB_REV,			/* cb_ops revision */
85 	nodev,			/* aread */
86 	nodev			/* awrite */
87 };
88 
89 static int dpattach(dev_info_t *, ddi_attach_cmd_t);
90 static int dpdetach(dev_info_t *, ddi_detach_cmd_t);
91 static int dpinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
92 
93 static struct dev_ops dp_ops = {
94 	DEVO_REV,		/* devo_rev */
95 	0,			/* refcnt */
96 	dpinfo,			/* info */
97 	nulldev,		/* identify */
98 	nulldev,		/* probe */
99 	dpattach,		/* attach */
100 	dpdetach,		/* detach */
101 	nodev,			/* reset */
102 	&dp_cb_ops,		/* driver operations */
103 	(struct bus_ops *)NULL, /* bus operations */
104 	nulldev			/* power */
105 };
106 
107 
108 static struct modldrv modldrv = {
109 	&mod_driverops,		/* type of module - a driver */
110 	"Dev Poll driver %I%",
111 	&dp_ops,
112 };
113 
114 static struct modlinkage modlinkage = {
115 	MODREV_1,
116 	(void *)&modldrv,
117 	NULL
118 };
119 
120 /*
121  * Locking Design
122  *
123  * The /dev/poll driver shares most of its code with poll sys call whose
124  * code is in common/syscall/poll.c. In poll(2) design, the pollcache
125  * structure is per lwp. An implicit assumption is made there that some
126  * portion of pollcache will never be touched by other lwps. E.g., in
127  * poll(2) design, no lwp will ever need to grow bitmap of other lwp.
128  * This assumption is not true for /dev/poll; hence the need for extra
129  * locking.
130  *
131  * To allow more paralellism, each /dev/poll file descriptor (indexed by
132  * minor number) has its own lock. Since read (dpioctl) is a much more
133  * frequent operation than write, we want to allow multiple reads on same
134  * /dev/poll fd. However, we prevent writes from being starved by giving
135  * priority to write operation. Theoretically writes can starve reads as
136  * well. But in pratical sense this is not important because (1) writes
137  * happens less often than reads, and (2) write operation defines the
138  * content of poll fd a cache set. If writes happens so often that they
139  * can starve reads, that means the cached set is very unstable. It may
140  * not make sense to read an unstable cache set anyway. Therefore, the
141  * writers starving readers case is not handled in this design.
142  */
143 
144 int
145 _init()
146 {
147 	int	error;
148 
149 	dptblsize = DEVPOLLSIZE;
150 	devpolltbl = kmem_zalloc(sizeof (caddr_t) * dptblsize, KM_SLEEP);
151 	mutex_init(&devpoll_lock, NULL, MUTEX_DEFAULT, NULL);
152 	devpoll_init = 1;
153 	if ((error = mod_install(&modlinkage)) != 0) {
154 		mutex_destroy(&devpoll_lock);
155 		kmem_free(devpolltbl, sizeof (caddr_t) * dptblsize);
156 		devpoll_init = 0;
157 	}
158 	return (error);
159 }
160 
161 int
162 _fini()
163 {
164 	int error;
165 
166 	if ((error = mod_remove(&modlinkage)) != 0) {
167 		return (error);
168 	}
169 	mutex_destroy(&devpoll_lock);
170 	kmem_free(devpolltbl, sizeof (caddr_t) * dptblsize);
171 	return (0);
172 }
173 
174 int
175 _info(struct modinfo *modinfop)
176 {
177 	return (mod_info(&modlinkage, modinfop));
178 }
179 
180 /*ARGSUSED*/
181 static int
182 dpattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
183 {
184 	if (ddi_create_minor_node(devi, "poll", S_IFCHR, 0, DDI_PSEUDO, NULL)
185 	    == DDI_FAILURE) {
186 		ddi_remove_minor_node(devi, NULL);
187 		return (DDI_FAILURE);
188 	}
189 	dpdevi = devi;
190 	return (DDI_SUCCESS);
191 }
192 
193 static int
194 dpdetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
195 {
196 	if (cmd != DDI_DETACH)
197 		return (DDI_FAILURE);
198 
199 	ddi_remove_minor_node(devi, NULL);
200 	return (DDI_SUCCESS);
201 }
202 
203 /* ARGSUSED */
204 static int
205 dpinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
206 {
207 	int error;
208 
209 	switch (infocmd) {
210 	case DDI_INFO_DEVT2DEVINFO:
211 		*result = (void *)dpdevi;
212 		error = DDI_SUCCESS;
213 		break;
214 	case DDI_INFO_DEVT2INSTANCE:
215 		*result = (void *)0;
216 		error = DDI_SUCCESS;
217 		break;
218 	default:
219 		error = DDI_FAILURE;
220 	}
221 	return (error);
222 }
223 
224 /*
225  * dp_pcache_poll has similar logic to pcache_poll() in poll.c. The major
226  * differences are: (1) /dev/poll requires scanning the bitmap starting at
227  * where it was stopped last time, instead of always starting from 0,
228  * (2) since user may not have cleaned up the cached fds when they are
229  * closed, some polldats in cache may refer to closed or reused fds. We
230  * need to check for those cases.
231  *
232  * NOTE: Upon closing an fd, automatic poll cache cleanup is done for
233  *	 poll(2) caches but NOT for /dev/poll caches. So expect some
234  *	 stale entries!
235  */
236 static int
237 dp_pcache_poll(pollfd_t *pfdp, pollcache_t *pcp, nfds_t nfds, int *fdcntp)
238 {
239 	int		start, ostart, end;
240 	int		fdcnt, fd;
241 	boolean_t 	done;
242 	file_t		*fp;
243 	short		revent;
244 	boolean_t	no_wrap;
245 	pollhead_t	*php;
246 	polldat_t	*pdp;
247 	int		error = 0;
248 
249 	ASSERT(MUTEX_HELD(&pcp->pc_lock));
250 	if (pcp->pc_bitmap == NULL) {
251 		/*
252 		 * No Need to search because no poll fd
253 		 * has been cached.
254 		 */
255 		return (error);
256 	}
257 retry:
258 	start = ostart = pcp->pc_mapstart;
259 	end = pcp->pc_mapend;
260 	php = NULL;
261 
262 	if (start == 0) {
263 		/*
264 		 * started from every begining, no need to wrap around.
265 		 */
266 		no_wrap = B_TRUE;
267 	} else {
268 		no_wrap = B_FALSE;
269 	}
270 	done = B_FALSE;
271 	fdcnt = 0;
272 	while ((fdcnt < nfds) && !done) {
273 		php = NULL;
274 		revent = 0;
275 		/*
276 		 * Examine the bit map in a circular fashion
277 		 * to avoid starvation. Always resume from
278 		 * last stop. Scan till end of the map. Then
279 		 * wrap around.
280 		 */
281 		fd = bt_getlowbit(pcp->pc_bitmap, start, end);
282 		ASSERT(fd <= end);
283 		if (fd >= 0) {
284 			if (fd == end) {
285 				if (no_wrap) {
286 					done = B_TRUE;
287 				} else {
288 					start = 0;
289 					end = ostart - 1;
290 					no_wrap = B_TRUE;
291 				}
292 			} else {
293 				start = fd + 1;
294 			}
295 			pdp = pcache_lookup_fd(pcp, fd);
296 			ASSERT(pdp != NULL);
297 			ASSERT(pdp->pd_fd == fd);
298 			if (pdp->pd_fp == NULL) {
299 				/*
300 				 * The fd is POLLREMOVed. This fd is
301 				 * logically no longer cached. So move
302 				 * on to the next one.
303 				 */
304 				continue;
305 			}
306 			if ((fp = getf(fd)) == NULL) {
307 				/*
308 				 * The fd has been closed, but user has not
309 				 * done a POLLREMOVE on this fd yet. Instead
310 				 * of cleaning it here implicitly, we return
311 				 * POLLNVAL. This is consistent with poll(2)
312 				 * polling a closed fd. Hope this will remind
313 				 * user to do a POLLREMOVE.
314 				 */
315 				pfdp[fdcnt].fd = fd;
316 				pfdp[fdcnt].revents = POLLNVAL;
317 				fdcnt++;
318 				continue;
319 			}
320 			if (fp != pdp->pd_fp) {
321 				/*
322 				 * user is polling on a cached fd which was
323 				 * closed and then reused. Unfortunately
324 				 * there is no good way to inform user.
325 				 * If the file struct is also reused, we
326 				 * may not be able to detect the fd reuse
327 				 * at all.  As long as this does not
328 				 * cause system failure and/or memory leak,
329 				 * we will play along. Man page states if
330 				 * user does not clean up closed fds, polling
331 				 * results will be indeterministic.
332 				 *
333 				 * XXX - perhaps log the detection of fd
334 				 *	 reuse?
335 				 */
336 				pdp->pd_fp = fp;
337 			}
338 			/*
339 			 * XXX - pollrelock() logic needs to know which
340 			 * which pollcache lock to grab. It'd be a
341 			 * cleaner solution if we could pass pcp as
342 			 * an arguement in VOP_POLL interface instead
343 			 * of implicitly passing it using thread_t
344 			 * struct. On the other hand, changing VOP_POLL
345 			 * interface will require all driver/file system
346 			 * poll routine to change. May want to revisit
347 			 * the tradeoff later.
348 			 */
349 			curthread->t_pollcache = pcp;
350 			error = VOP_POLL(fp->f_vnode, pdp->pd_events, 0,
351 			    &revent, &php);
352 			curthread->t_pollcache = NULL;
353 			releasef(fd);
354 			if (error != 0) {
355 				break;
356 			}
357 			/*
358 			 * layered devices (e.g. console driver)
359 			 * may change the vnode and thus the pollhead
360 			 * pointer out from underneath us.
361 			 */
362 			if (php != NULL && pdp->pd_php != NULL &&
363 			    php != pdp->pd_php) {
364 				pollhead_delete(pdp->pd_php, pdp);
365 				pdp->pd_php = php;
366 				pollhead_insert(php, pdp);
367 				/*
368 				 * The bit should still be set.
369 				 */
370 				ASSERT(BT_TEST(pcp->pc_bitmap, fd));
371 				goto retry;
372 			}
373 
374 			if (revent != 0) {
375 				pfdp[fdcnt].fd = fd;
376 				pfdp[fdcnt].events = pdp->pd_events;
377 				pfdp[fdcnt].revents = revent;
378 				fdcnt++;
379 			} else if (php != NULL) {
380 				/*
381 				 * We clear a bit or cache a poll fd if
382 				 * the driver returns a poll head ptr,
383 				 * which is expected in the case of 0
384 				 * revents. Some buggy driver may return
385 				 * NULL php pointer with 0 revents. In
386 				 * this case, we just treat the driver as
387 				 * "noncachable" and not clearing the bit
388 				 * in bitmap.
389 				 */
390 				if ((pdp->pd_php != NULL) &&
391 				    ((pcp->pc_flag & T_POLLWAKE) == 0)) {
392 					BT_CLEAR(pcp->pc_bitmap, fd);
393 				}
394 				if (pdp->pd_php == NULL) {
395 					pollhead_insert(php, pdp);
396 					pdp->pd_php = php;
397 				}
398 			}
399 		} else {
400 			/*
401 			 * No bit set in the range. Check for wrap around.
402 			 */
403 			if (!no_wrap) {
404 				start = 0;
405 				end = ostart - 1;
406 				no_wrap = B_TRUE;
407 			} else {
408 				done = B_TRUE;
409 			}
410 		}
411 	}
412 
413 	if (!done) {
414 		pcp->pc_mapstart = start;
415 	}
416 	ASSERT(*fdcntp == 0);
417 	*fdcntp = fdcnt;
418 	return (error);
419 }
420 
421 /*ARGSUSED*/
422 static int
423 dpopen(dev_t *devp, int flag, int otyp, cred_t *credp)
424 {
425 	minor_t		minordev;
426 	dp_entry_t	*dpep;
427 	pollcache_t	*pcp;
428 
429 	ASSERT(devpoll_init);
430 	ASSERT(dptblsize <= MAXMIN);
431 	mutex_enter(&devpoll_lock);
432 	for (minordev = 0; minordev < dptblsize; minordev++) {
433 		if (devpolltbl[minordev] == NULL) {
434 			devpolltbl[minordev] = (dp_entry_t *)RESERVED;
435 			break;
436 		}
437 	}
438 	if (minordev == dptblsize) {
439 		dp_entry_t	**newtbl;
440 		size_t		oldsize;
441 
442 		/*
443 		 * Used up every entry in the existing devpoll table.
444 		 * Grow the table by DEVPOLLSIZE.
445 		 */
446 		if ((oldsize = dptblsize) >= MAXMIN) {
447 			mutex_exit(&devpoll_lock);
448 			return (ENXIO);
449 		}
450 		dptblsize += DEVPOLLSIZE;
451 		if (dptblsize > MAXMIN) {
452 			dptblsize = MAXMIN;
453 		}
454 		newtbl = kmem_zalloc(sizeof (caddr_t) * dptblsize, KM_SLEEP);
455 		bcopy(devpolltbl, newtbl, sizeof (caddr_t) * oldsize);
456 		kmem_free(devpolltbl, sizeof (caddr_t) * oldsize);
457 		devpolltbl = newtbl;
458 		devpolltbl[minordev] = (dp_entry_t *)RESERVED;
459 	}
460 	mutex_exit(&devpoll_lock);
461 
462 	dpep = kmem_zalloc(sizeof (dp_entry_t), KM_SLEEP);
463 	/*
464 	 * allocate a pollcache skeleton here. Delay allocating bitmap
465 	 * structures until dpwrite() time, since we don't know the
466 	 * optimal size yet.
467 	 */
468 	pcp = pcache_alloc();
469 	dpep->dpe_pcache = pcp;
470 	pcp->pc_pid = curproc->p_pid;
471 	*devp = makedevice(getmajor(*devp), minordev);  /* clone the driver */
472 	mutex_enter(&devpoll_lock);
473 	ASSERT(minordev < dptblsize);
474 	ASSERT(devpolltbl[minordev] == (dp_entry_t *)RESERVED);
475 	devpolltbl[minordev] = dpep;
476 	mutex_exit(&devpoll_lock);
477 	return (0);
478 }
479 
480 /*
481  * Write to dev/poll add/remove fd's to/from a cached poll fd set,
482  * or change poll events for a watched fd.
483  */
484 /*ARGSUSED*/
485 static int
486 dpwrite(dev_t dev, struct uio *uiop, cred_t *credp)
487 {
488 	minor_t 	minor;
489 	dp_entry_t	*dpep;
490 	pollcache_t	*pcp;
491 	pollfd_t	*pollfdp, *pfdp;
492 	int		error;
493 	ssize_t		uiosize;
494 	nfds_t		pollfdnum;
495 	struct pollhead	*php = NULL;
496 	polldat_t	*pdp;
497 	int		fd;
498 	file_t		*fp;
499 
500 	minor = getminor(dev);
501 
502 	mutex_enter(&devpoll_lock);
503 	ASSERT(minor < dptblsize);
504 	dpep = devpolltbl[minor];
505 	ASSERT(dpep != NULL);
506 	mutex_exit(&devpoll_lock);
507 	pcp = dpep->dpe_pcache;
508 	if (curproc->p_pid != pcp->pc_pid) {
509 		return (EACCES);
510 	}
511 	uiosize = uiop->uio_resid;
512 	pollfdnum = uiosize / sizeof (pollfd_t);
513 	mutex_enter(&curproc->p_lock);
514 	if (pollfdnum > (uint_t)rctl_enforced_value(
515 	    rctlproc_legacy[RLIMIT_NOFILE], curproc->p_rctls, curproc)) {
516 		(void) rctl_action(rctlproc_legacy[RLIMIT_NOFILE],
517 		    curproc->p_rctls, curproc, RCA_SAFE);
518 		mutex_exit(&curproc->p_lock);
519 		return (set_errno(EINVAL));
520 	}
521 	mutex_exit(&curproc->p_lock);
522 	/*
523 	 * Copy in the pollfd array.  Walk through the array and add
524 	 * each polled fd to the cached set.
525 	 */
526 	pollfdp = kmem_alloc(uiosize, KM_SLEEP);
527 
528 	/*
529 	 * Although /dev/poll uses the write(2) interface to cache fds, it's
530 	 * not supposed to function as a seekable device. To prevent offset
531 	 * from growing and eventually exceed the maximum, reset the offset
532 	 * here for every call.
533 	 */
534 	uiop->uio_loffset = 0;
535 	if ((error = uiomove((caddr_t)pollfdp, uiosize, UIO_WRITE, uiop))
536 	    != 0) {
537 		kmem_free(pollfdp, uiosize);
538 		return (error);
539 	}
540 	/*
541 	 * We are about to enter the core portion of dpwrite(). Make sure this
542 	 * write has exclusive access in this portion of the code, i.e., no
543 	 * other writers in this code and no other readers in dpioctl.
544 	 */
545 	mutex_enter(&dpep->dpe_lock);
546 	dpep->dpe_writerwait++;
547 	while (dpep->dpe_refcnt != 0) {
548 		if (!cv_wait_sig_swap(&dpep->dpe_cv, &dpep->dpe_lock)) {
549 			dpep->dpe_writerwait--;
550 			mutex_exit(&dpep->dpe_lock);
551 			kmem_free(pollfdp, uiosize);
552 			return (set_errno(EINTR));
553 		}
554 	}
555 	dpep->dpe_writerwait--;
556 	dpep->dpe_flag |= DP_WRITER_PRESENT;
557 	dpep->dpe_refcnt++;
558 	mutex_exit(&dpep->dpe_lock);
559 
560 	mutex_enter(&pcp->pc_lock);
561 	if (pcp->pc_bitmap == NULL) {
562 		pcache_create(pcp, pollfdnum);
563 	}
564 	for (pfdp = pollfdp; pfdp < pollfdp + pollfdnum; pfdp++) {
565 		fd = pfdp->fd;
566 		if ((uint_t)fd >= P_FINFO(curproc)->fi_nfiles)
567 			continue;
568 		pdp = pcache_lookup_fd(pcp, fd);
569 		if (pfdp->events != POLLREMOVE) {
570 			if (pdp == NULL) {
571 				pdp = pcache_alloc_fd(0);
572 				pdp->pd_fd = fd;
573 				pdp->pd_pcache = pcp;
574 				pcache_insert_fd(pcp, pdp, pollfdnum);
575 			}
576 			ASSERT(pdp->pd_fd == fd);
577 			ASSERT(pdp->pd_pcache == pcp);
578 			if (fd >= pcp->pc_mapsize) {
579 				mutex_exit(&pcp->pc_lock);
580 				pcache_grow_map(pcp, fd);
581 				mutex_enter(&pcp->pc_lock);
582 			}
583 			if (fd > pcp->pc_mapend) {
584 				pcp->pc_mapend = fd;
585 			}
586 			if ((fp = getf(fd)) == NULL) {
587 				/*
588 				 * The fd is not valid. Since we can't pass
589 				 * this error back in the write() call, set
590 				 * the bit in bitmap to force DP_POLL ioctl
591 				 * to examine it.
592 				 */
593 				BT_SET(pcp->pc_bitmap, fd);
594 				pdp->pd_events |= pfdp->events;
595 				continue;
596 			}
597 			/*
598 			 * Don't do VOP_POLL for an already cached fd with
599 			 * same poll events.
600 			 */
601 			if ((pdp->pd_events == pfdp->events) &&
602 			    (pdp->pd_fp != NULL)) {
603 				/*
604 				 * the events are already cached
605 				 */
606 				releasef(fd);
607 				continue;
608 			}
609 
610 			/*
611 			 * do VOP_POLL and cache this poll fd.
612 			 */
613 			/*
614 			 * XXX - pollrelock() logic needs to know which
615 			 * which pollcache lock to grab. It'd be a
616 			 * cleaner solution if we could pass pcp as
617 			 * an arguement in VOP_POLL interface instead
618 			 * of implicitly passing it using thread_t
619 			 * struct. On the other hand, changing VOP_POLL
620 			 * interface will require all driver/file system
621 			 * poll routine to change. May want to revisit
622 			 * the tradeoff later.
623 			 */
624 			curthread->t_pollcache = pcp;
625 			error = VOP_POLL(fp->f_vnode, pfdp->events, 0,
626 			    &pfdp->revents, &php);
627 			curthread->t_pollcache = NULL;
628 			/*
629 			 * We always set the bit when this fd is cached.
630 			 * So we don't have to worry about missing a
631 			 * pollwakeup between VOP_POLL and pollhead_insert.
632 			 * This forces the first DP_POLL to poll this fd.
633 			 * Real performance gain comes from subsequent
634 			 * DP_POLL.
635 			 */
636 			BT_SET(pcp->pc_bitmap, fd);
637 			if (error != 0) {
638 				releasef(fd);
639 				break;
640 			}
641 			pdp->pd_fp = fp;
642 			pdp->pd_events |= pfdp->events;
643 			if (php != NULL) {
644 				if (pdp->pd_php == NULL) {
645 					pollhead_insert(php, pdp);
646 					pdp->pd_php = php;
647 				} else {
648 					if (pdp->pd_php != php) {
649 						pollhead_delete(pdp->pd_php,
650 						    pdp);
651 						pollhead_insert(php, pdp);
652 						pdp->pd_php = php;
653 					}
654 				}
655 
656 			}
657 			releasef(fd);
658 		} else {
659 			if (pdp == NULL) {
660 				continue;
661 			}
662 			ASSERT(pdp->pd_fd == fd);
663 			pdp->pd_fp = NULL;
664 			pdp->pd_events = 0;
665 			ASSERT(pdp->pd_thread == NULL);
666 			if (pdp->pd_php != NULL) {
667 				pollhead_delete(pdp->pd_php, pdp);
668 				pdp->pd_php = NULL;
669 			}
670 			BT_CLEAR(pcp->pc_bitmap, fd);
671 		}
672 	}
673 	mutex_exit(&pcp->pc_lock);
674 	mutex_enter(&dpep->dpe_lock);
675 	dpep->dpe_flag &= ~DP_WRITER_PRESENT;
676 	ASSERT(dpep->dpe_refcnt == 1);
677 	dpep->dpe_refcnt--;
678 	cv_broadcast(&dpep->dpe_cv);
679 	mutex_exit(&dpep->dpe_lock);
680 	kmem_free(pollfdp, uiosize);
681 	return (error);
682 }
683 
684 /*ARGSUSED*/
685 static int
686 dpioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
687 {
688 	timestruc_t	now;
689 	timestruc_t	rqtime;
690 	timestruc_t	*rqtp = NULL;
691 	minor_t 	minor;
692 	dp_entry_t	*dpep;
693 	pollcache_t	*pcp;
694 	int 		error = 0;
695 	STRUCT_DECL(dvpoll, dvpoll);
696 
697 	if (cmd == DP_POLL) {
698 		/* do this now, before we sleep on DP_WRITER_PRESENT below */
699 		gethrestime(&now);
700 	}
701 	minor = getminor(dev);
702 	mutex_enter(&devpoll_lock);
703 	ASSERT(minor < dptblsize);
704 	dpep = devpolltbl[minor];
705 	mutex_exit(&devpoll_lock);
706 	ASSERT(dpep != NULL);
707 	pcp = dpep->dpe_pcache;
708 	if (curproc->p_pid != pcp->pc_pid)
709 		return (EACCES);
710 
711 	mutex_enter(&dpep->dpe_lock);
712 	while ((dpep->dpe_flag & DP_WRITER_PRESENT) ||
713 	    (dpep->dpe_writerwait != 0)) {
714 		if (!cv_wait_sig_swap(&dpep->dpe_cv, &dpep->dpe_lock)) {
715 			mutex_exit(&dpep->dpe_lock);
716 			return (EINTR);
717 		}
718 	}
719 	dpep->dpe_refcnt++;
720 	mutex_exit(&dpep->dpe_lock);
721 
722 	switch (cmd) {
723 	case	DP_POLL:
724 	{
725 		pollstate_t *ps;
726 		nfds_t	nfds;
727 		int	fdcnt = 0;
728 		int	time_out;
729 		int	rval;
730 
731 		STRUCT_INIT(dvpoll, mode);
732 		error = copyin((caddr_t)arg, STRUCT_BUF(dvpoll),
733 		    STRUCT_SIZE(dvpoll));
734 		if (error) {
735 			DP_REFRELE(dpep);
736 			return (EFAULT);
737 		}
738 
739 		time_out = STRUCT_FGET(dvpoll, dp_timeout);
740 		if (time_out > 0) {
741 			/*
742 			 * Determine the future time of the requested timeout.
743 			 */
744 			rqtp = &rqtime;
745 			rqtp->tv_sec = time_out / MILLISEC;
746 			rqtp->tv_nsec = (time_out % MILLISEC) * MICROSEC;
747 			timespecadd(rqtp, &now);
748 		}
749 
750 		if ((nfds = STRUCT_FGET(dvpoll, dp_nfds)) == 0) {
751 			/*
752 			 * We are just using DP_POLL to sleep, so
753 			 * we don't any of the devpoll apparatus.
754 			 * Do not check for signals if we have a zero timeout.
755 			 */
756 			DP_REFRELE(dpep);
757 			if (time_out == 0)
758 				return (0);
759 			mutex_enter(&curthread->t_delay_lock);
760 			while ((rval = cv_waituntil_sig(&curthread->t_delay_cv,
761 			    &curthread->t_delay_lock, rqtp)) > 0)
762 				continue;
763 			mutex_exit(&curthread->t_delay_lock);
764 			return ((rval == 0)? EINTR : 0);
765 		}
766 
767 		/*
768 		 * XXX It'd be nice not to have to alloc each time.
769 		 * But it requires another per thread structure hook.
770 		 * Do it later if there is data suggest that.
771 		 */
772 		if ((ps = curthread->t_pollstate) == NULL) {
773 			curthread->t_pollstate = pollstate_create();
774 			ps = curthread->t_pollstate;
775 		}
776 		if (ps->ps_dpbufsize < nfds) {
777 			struct proc *p = ttoproc(curthread);
778 			/*
779 			 * The maximum size should be no large than
780 			 * current maximum open file count.
781 			 */
782 			mutex_enter(&p->p_lock);
783 			if (nfds > p->p_fno_ctl) {
784 				mutex_exit(&p->p_lock);
785 				DP_REFRELE(dpep);
786 				return (EINVAL);
787 			}
788 			mutex_exit(&p->p_lock);
789 			kmem_free(ps->ps_dpbuf, sizeof (pollfd_t) *
790 			    ps->ps_dpbufsize);
791 			ps->ps_dpbuf = kmem_zalloc(sizeof (pollfd_t) *
792 			    nfds, KM_SLEEP);
793 			ps->ps_dpbufsize = nfds;
794 		}
795 
796 		mutex_enter(&pcp->pc_lock);
797 		for (;;) {
798 			pcp->pc_flag = 0;
799 			error = dp_pcache_poll(ps->ps_dpbuf, pcp, nfds, &fdcnt);
800 			if (fdcnt > 0 || error != 0)
801 				break;
802 
803 			/*
804 			 * A pollwake has happened since we polled cache.
805 			 */
806 			if (pcp->pc_flag & T_POLLWAKE)
807 				continue;
808 
809 			/*
810 			 * Sleep until we are notified, signalled, or timed out.
811 			 * Do not check for signals if we have a zero timeout.
812 			 */
813 			if (time_out == 0)	/* immediate timeout */
814 				break;
815 			rval = cv_waituntil_sig(&pcp->pc_cv, &pcp->pc_lock,
816 				rqtp);
817 			/*
818 			 * If we were awakened by a signal or timeout
819 			 * then break the loop, else poll again.
820 			 */
821 			if (rval <= 0) {
822 				if (rval == 0)	/* signal */
823 					error = EINTR;
824 				break;
825 			}
826 		}
827 		mutex_exit(&pcp->pc_lock);
828 
829 		if (error == 0 && fdcnt > 0) {
830 			if (copyout(ps->ps_dpbuf, STRUCT_FGETP(dvpoll,
831 			    dp_fds), sizeof (pollfd_t) * fdcnt)) {
832 				DP_REFRELE(dpep);
833 				return (EFAULT);
834 			}
835 			*rvalp = fdcnt;
836 		}
837 		break;
838 	}
839 
840 	case	DP_ISPOLLED:
841 	{
842 		pollfd_t	pollfd;
843 		polldat_t	*pdp;
844 
845 		STRUCT_INIT(dvpoll, mode);
846 		error = copyin((caddr_t)arg, &pollfd, sizeof (pollfd_t));
847 		if (error) {
848 			DP_REFRELE(dpep);
849 			return (EFAULT);
850 		}
851 		mutex_enter(&pcp->pc_lock);
852 		if (pcp->pc_hash == NULL) {
853 			/*
854 			 * No Need to search because no poll fd
855 			 * has been cached.
856 			 */
857 			mutex_exit(&pcp->pc_lock);
858 			DP_REFRELE(dpep);
859 			return (0);
860 		}
861 		if (pollfd.fd < 0) {
862 			mutex_exit(&pcp->pc_lock);
863 			break;
864 		}
865 		pdp = pcache_lookup_fd(pcp, pollfd.fd);
866 		if ((pdp != NULL) && (pdp->pd_fd == pollfd.fd) &&
867 		    (pdp->pd_fp != NULL)) {
868 			pollfd.revents = pdp->pd_events;
869 			if (copyout(&pollfd, (caddr_t)arg, sizeof (pollfd_t))) {
870 				mutex_exit(&pcp->pc_lock);
871 				DP_REFRELE(dpep);
872 				return (EFAULT);
873 			}
874 			*rvalp = 1;
875 		}
876 		mutex_exit(&pcp->pc_lock);
877 		break;
878 	}
879 
880 	default:
881 		DP_REFRELE(dpep);
882 		return (EINVAL);
883 	}
884 	DP_REFRELE(dpep);
885 	return (error);
886 }
887 
888 /*ARGSUSED*/
889 static int
890 dppoll(dev_t dev, short events, int anyyet, short *reventsp,
891     struct pollhead **phpp)
892 {
893 	/*
894 	 * Polling on a /dev/poll fd is not fully supported yet.
895 	 */
896 	*reventsp = POLLERR;
897 	return (0);
898 }
899 
900 /*
901  * devpoll close should do enough clean up before the pollcache is deleted,
902  * i.e., it should ensure no one still references the pollcache later.
903  * There is no "permission" check in here. Any process having the last
904  * reference of this /dev/poll fd can close.
905  */
906 /*ARGSUSED*/
907 static int
908 dpclose(dev_t dev, int flag, int otyp, cred_t *credp)
909 {
910 	minor_t 	minor;
911 	dp_entry_t	*dpep;
912 	pollcache_t	*pcp;
913 	int		i;
914 	polldat_t	**hashtbl;
915 	polldat_t	*pdp;
916 
917 	minor = getminor(dev);
918 
919 	mutex_enter(&devpoll_lock);
920 	dpep = devpolltbl[minor];
921 	ASSERT(dpep != NULL);
922 	devpolltbl[minor] = NULL;
923 	mutex_exit(&devpoll_lock);
924 	pcp = dpep->dpe_pcache;
925 	ASSERT(pcp != NULL);
926 	/*
927 	 * At this point, no other lwp can access this pollcache via the
928 	 * /dev/poll fd. This pollcache is going away, so do the clean
929 	 * up without the pc_lock.
930 	 */
931 	hashtbl = pcp->pc_hash;
932 	for (i = 0; i < pcp->pc_hashsize; i++) {
933 		for (pdp = hashtbl[i]; pdp; pdp = pdp->pd_hashnext) {
934 			if (pdp->pd_php != NULL) {
935 				pollhead_delete(pdp->pd_php, pdp);
936 				pdp->pd_php = NULL;
937 				pdp->pd_fp = NULL;
938 			}
939 		}
940 	}
941 	/*
942 	 * pollwakeup() may still interact with this pollcache. Wait until
943 	 * it is done.
944 	 */
945 	mutex_enter(&pcp->pc_no_exit);
946 	ASSERT(pcp->pc_busy >= 0);
947 	while (pcp->pc_busy > 0)
948 		cv_wait(&pcp->pc_busy_cv, &pcp->pc_no_exit);
949 	mutex_exit(&pcp->pc_no_exit);
950 	pcache_destroy(pcp);
951 	ASSERT(dpep->dpe_refcnt == 0);
952 	kmem_free(dpep, sizeof (dp_entry_t));
953 	return (0);
954 }
955