xref: /titanic_51/usr/src/uts/common/fs/zev/zev.c (revision 231caef24406106540286485f630f823f4efe5c9)
1 #include <sys/modctl.h>
2 #include <sys/ddi.h>
3 #include <sys/sunddi.h>
4 #include <sys/conf.h>
5 #include <sys/devops.h>
6 #include <sys/stat.h>
7 #include <sys/fs/zev.h>
8 #include <sys/zev_callbacks.h>
9 
10 typedef struct zev_state {
11 	kmutex_t	mutex;
12 	dev_info_t	*dip;
13 	boolean_t	busy;
14 } zev_state_t;
15 
16 static void		*statep;
17 struct pollhead		zev_pollhead;
18 
19 kmutex_t		zev_mutex;
20 kcondvar_t		zev_condvar;
21 krwlock_t		zev_pool_list_rwlock;
22 static zev_statistics_t	zev_statistics;
23 static boolean_t	zev_busy;
24 
25 /*
26  * The longest potential message is from zev_zfs_mount() and
27  * contains the mountpoint, which might be close to MAXPATHLEN bytes long.
28  *
29  * Another candidate is zev_znode_rename_cb() and contains three inode
30  * numbers and two filenames of up to MAXNAMELEN bytes each.
31  */
32 #define ZEV_MAX_MESSAGE_LEN	4096
33 
34 /* If the queue size reaches 1GB, stop ZFS ops and block the threads.  */
35 #define ZEV_MAX_QUEUE_LEN		(1 * 1024 * 1024 * 1024)
36 
37 /* Don't wake up poll()ing processes for every single message. */
38 #define ZEV_MIN_POLL_WAKEUP_QUEUE_LEN	8192
39 
40 typedef struct zev_mq {
41 	struct zev_mq	*next;
42 	int		 used;
43 	int		 sent;
44 	char		 buf[ZEV_MAX_MESSAGE_LEN];
45 } zev_mq_t;
46 
47 static zev_mq_t *zev_mq_head = NULL;
48 static zev_mq_t *zev_mq_tail = NULL;
49 static uint64_t zev_mq_len = 0;
50 
51 typedef struct zev_pool_list_entry {
52 	struct zev_pool_list_entry	*next;
53 	char				name[MAXPATHLEN];
54 } zev_pool_list_entry_t;
55 
56 static zev_pool_list_entry_t *zev_muted_pools_head = NULL;
57 
58 /*
59  * poll() wakeup thread.  Used to check periodically whether we have
60  * bytes left in the queue that have not yet been made into a
61  * pollwakeup() call.  This is meant to insure a maximum waiting
62  * time until an event is presented as a poll wakeup, while at
63  * the same time not making every single event into a poll wakeup
64  * of it's own.
65  */
66 
67 static volatile int zev_wakeup_thread_run = 1;
68 static kthread_t *zev_poll_wakeup_thread = NULL;
69 
70 static void
71 zev_poll_wakeup_thread_main(void)
72 {
73 	int wakeup;
74 	while (zev_wakeup_thread_run) {
75 		delay(drv_usectohz(100 * 1000)); /* sleep 100ms */
76 		/* check message queue */
77 		mutex_enter(&zev_mutex);
78 		wakeup = 0;
79 		if (zev_mq_head)
80 			wakeup = 1;
81 		mutex_exit(&zev_mutex);
82 		if (wakeup)
83 			pollwakeup(&zev_pollhead, POLLIN);
84 	}
85 	thread_exit();
86 }
87 
88 static int
89 zev_ioc_mute_pool(char *poolname)
90 {
91 	zev_pool_list_entry_t *pe;
92 	rw_enter(&zev_pool_list_rwlock, RW_WRITER);
93 	/* pool already muted? */
94 	for (pe=zev_muted_pools_head; pe; pe=pe->next) {
95 		if (!strcmp(pe->name, poolname)) {
96 			rw_exit(&zev_pool_list_rwlock);
97 			return EEXIST;
98 		}
99 	}
100 	pe = kmem_zalloc(sizeof(*pe), KM_SLEEP);
101 	if (!pe) {
102 		rw_exit(&zev_pool_list_rwlock);
103 		return ENOMEM;
104 	}
105 	strncpy(pe->name, poolname, sizeof(pe->name));
106 	pe->next = zev_muted_pools_head;
107 	zev_muted_pools_head = pe;
108 	rw_exit(&zev_pool_list_rwlock);
109 	return (0);
110 }
111 
112 static int
113 zev_ioc_unmute_pool(char *poolname)
114 {
115 	zev_pool_list_entry_t *pe, *peprev;
116 	rw_enter(&zev_pool_list_rwlock, RW_WRITER);
117 	/* pool muted? */
118 	peprev = NULL;
119 	for (pe=zev_muted_pools_head; pe; pe=pe->next) {
120 		if (!strcmp(pe->name, poolname)) {
121 			goto found;
122 		}
123 		peprev = pe;
124 	}
125 	rw_exit(&zev_pool_list_rwlock);
126 	return ENOENT;
127 found:
128 	if (peprev != NULL) {
129 		peprev->next = pe->next;
130 	} else {
131 		zev_muted_pools_head = pe->next;
132 	}
133 	kmem_free(pe, sizeof(*pe));
134 	rw_exit(&zev_pool_list_rwlock);
135 	return (0);
136 }
137 
138 int
139 zev_skip_pool(objset_t *os)
140 {
141 	zev_pool_list_entry_t *pe;
142 	dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
143 	rw_enter(&zev_pool_list_rwlock, RW_READER);
144 	for (pe=zev_muted_pools_head; pe; pe=pe->next) {
145 		if (!strcmp(pe->name, dp->dp_spa->spa_name)) {
146 			rw_exit(&zev_pool_list_rwlock);
147 			return 1;
148 		}
149 	}
150 	rw_exit(&zev_pool_list_rwlock);
151 	return 0;
152 }
153 
154 void
155 zev_mq_printf(int op, int error, char *fmt, ...)
156 {
157 	char buf[ZEV_MAX_MESSAGE_LEN];
158 	int len;
159 	va_list ap;
160 	zev_mq_t *mq;
161 	uint64_t bytes_in_queue = 0;
162 	int wakeup = 0;
163 
164 	/* render message */
165 	va_start(ap, fmt);
166 	len = vsnprintf(buf, sizeof(buf), fmt, ap);
167 	va_end(ap);
168 	if (len >= sizeof(buf)) {
169 		strcpy(buf, "ZEV_ERROR: message too long\n");
170 		len = strlen(buf);
171 		error++;
172 	}
173 
174 	/* op type ok? */
175 	if (op < ZEV_OP_MIN || op > ZEV_OP_MAX) {
176 		len = snprintf(buf, sizeof(buf),
177 		    "ZEV_ERROR: unknown op %d\n", op);
178 		error++;
179 	}
180 
181 	mutex_enter(&zev_mutex);
182 	while (zev_statistics.zev_max_queue_len &&
183 	    zev_statistics.zev_queue_len >= zev_statistics.zev_max_queue_len) {
184 		/* queue full.  block until it's been shrunk. */
185 		cv_wait(&zev_condvar, &zev_mutex);
186 	}
187 
188 	mq = zev_mq_tail;
189 	/* make sure we have enough space in our queue */
190 	if (!mq || ((ZEV_MAX_MESSAGE_LEN - mq->used) < len)) {
191 		/* need new mq */
192 		mq = kmem_zalloc(sizeof(*mq), KM_SLEEP);
193 		if (zev_mq_tail)
194 			zev_mq_tail->next = mq;
195 		zev_mq_tail = mq;
196 		zev_mq_len++;
197 		if (!zev_mq_head)
198 			zev_mq_head = mq;
199 	}
200 
201 	/* copy message to queue */
202 	memcpy(mq->buf + mq->used, buf, len);
203 	mq->used += len;
204 
205 	/* update statistics */
206 	zev_statistics.zev_cnt_total_events++;
207 	zev_statistics.zev_queue_len += len;
208 	bytes_in_queue = zev_statistics.zev_queue_len;
209 	if (error)
210 		zev_statistics.zev_cnt_errors++;
211 
212 	switch (op) {
213 	case ZEV_OP_ZFS_MOUNT:
214 		zev_statistics.zev_cnt_zfs_mount++;
215 		break;
216 	case ZEV_OP_ZFS_UMOUNT:
217 		zev_statistics.zev_cnt_zfs_umount++;
218 		break;
219 	case ZEV_OP_ZVOL_WRITE:
220 		zev_statistics.zev_cnt_zvol_write++;
221 		break;
222 	case ZEV_OP_ZVOL_TRUNCATE:
223 		zev_statistics.zev_cnt_zvol_truncate++;
224 		break;
225 	case ZEV_OP_ZNODE_CLOSE_AFTER_UPDATE:
226 		zev_statistics.zev_cnt_znode_close_after_update++;
227 		break;
228 	case ZEV_OP_ZNODE_CREATE:
229 		zev_statistics.zev_cnt_znode_create++;
230 		break;
231 	case ZEV_OP_ZNODE_REMOVE:
232 		zev_statistics.zev_cnt_znode_remove++;
233 		break;
234 	case ZEV_OP_ZNODE_LINK:
235 		zev_statistics.zev_cnt_znode_link++;
236 		break;
237 	case ZEV_OP_ZNODE_SYMLINK:
238 		zev_statistics.zev_cnt_znode_symlink++;
239 		break;
240 	case ZEV_OP_ZNODE_RENAME:
241 		zev_statistics.zev_cnt_znode_rename++;
242 		break;
243 	case ZEV_OP_ZNODE_WRITE:
244 		zev_statistics.zev_cnt_znode_write++;
245 		break;
246 	case ZEV_OP_ZNODE_TRUNCATE:
247 		zev_statistics.zev_cnt_znode_truncate++;
248 		break;
249 	case ZEV_OP_ZNODE_SETATTR:
250 		zev_statistics.zev_cnt_znode_setattr++;
251 		break;
252 	case ZEV_OP_ZNODE_ACL:
253 		zev_statistics.zev_cnt_znode_acl++;
254 		break;
255 	}
256 
257 	if (bytes_in_queue > zev_statistics.zev_poll_wakeup_queue_len)
258 		wakeup = 1;
259 
260 	mutex_exit(&zev_mutex);
261 
262 	/* chpoll event, if necessary.  */
263 	if (wakeup)
264 		pollwakeup(&zev_pollhead, POLLIN);
265 }
266 
267 static int
268 zev_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
269 {
270 	int instance;
271 	zev_state_t *sp;
272 	zev_statistics_t zs;
273 	zev_ioctl_poolarg_t pa;
274 	uint64_t len;
275 
276 	instance = getminor(dev);
277 	if ((sp = ddi_get_soft_state(statep, instance)) == NULL)
278 		return (ENXIO);
279 	if (ddi_model_convert_from(mode) != DDI_MODEL_NONE) {
280 		/* userland has another data model.  (most
281 		   likely 32-bit) -> not supported. */
282 		return (EINVAL);
283 	}
284 	/* Remember to do 32/64 bit mode adjustments if
285 	   necessary.  See "Writing Device Drivers", 280pp */
286 	switch (cmd) {
287 	case ZEV_IOC_GET_STATISTICS:
288 		/* ddi_copyout() can take a long time.  Better make
289 		   a copy to be able to release the mutex faster. */
290 		mutex_enter(&zev_mutex);
291 		memcpy(&zs, &zev_statistics, sizeof(zs));
292 		mutex_exit(&zev_mutex);
293 		if (ddi_copyout(&zs, (void *)arg, sizeof(zs), mode) != 0)
294 			return EFAULT;
295 		break;
296 	case ZEV_IOC_MUTE_POOL:
297 	case ZEV_IOC_UNMUTE_POOL:
298 		if (ddi_copyin((void *)arg, &pa, sizeof(pa), mode) != 0)
299 			return EFAULT;
300 		if (pa.zev_poolname_len >=MAXPATHLEN)
301 			return EINVAL;
302 		pa.zev_poolname[pa.zev_poolname_len] = '\0';
303 		if (cmd == ZEV_IOC_MUTE_POOL) {
304 			return zev_ioc_mute_pool(pa.zev_poolname);
305 		} else {
306 			return zev_ioc_unmute_pool(pa.zev_poolname);
307 		}
308 		break;
309 	case ZEV_IOC_SET_MAX_QUEUE_LEN:
310 		if (ddi_copyin((void *)arg, &len, sizeof(len), mode) != 0)
311 			return EFAULT;
312 		if (len > ZEV_MAX_QUEUE_LEN)
313 			return EINVAL;
314 		mutex_enter(&zev_mutex);
315 		zev_statistics.zev_max_queue_len = len;
316 		cv_broadcast(&zev_condvar);
317 		mutex_exit(&zev_mutex);
318 		break;
319 	case ZEV_IOC_SET_POLL_WAKEUP_QUEUE_LEN:
320 		if (ddi_copyin((void *)arg, &len, sizeof(len), mode) != 0)
321 			return EFAULT;
322 		mutex_enter(&zev_mutex);
323 		zev_statistics.zev_poll_wakeup_queue_len = len;
324 		mutex_exit(&zev_mutex);
325 		break;
326 	default:
327 		/* generic "ioctl unknown" error */
328 		return (ENOTTY);
329 	}
330 	return (0);
331 }
332 
333 static int
334 zev_chpoll(dev_t dev, short events, int anyyet,
335     short *reventsp, struct pollhead **phpp)
336 {
337 	int instance;
338 	zev_state_t *sp;
339 	short revent = 0;
340 
341 	instance = getminor(dev);
342 	if ((sp = ddi_get_soft_state(statep, instance)) == NULL)
343 		return (ENXIO);
344 	revent = 0;
345 	if ((events & POLLIN)) {
346 		mutex_enter(&zev_mutex);
347 		if (zev_mq_head)
348 			revent |= POLLIN;
349 		mutex_exit(&zev_mutex);
350 	}
351 	if (revent == 0) {
352 		if (!anyyet) {
353 			*phpp = &zev_pollhead;
354 		}
355 	}
356 	*reventsp = revent;
357 	return (0);
358 }
359 
360 static int
361 zev_read(dev_t dev, struct uio *uio_p, cred_t *crep_p)
362 {
363 	zev_state_t *sp;
364 	int instance;
365 	offset_t off;
366 	int ret = 0;
367 	int mq_bytes;
368 	zev_mq_t *mq;
369 	int len;
370 
371 	instance = getminor(dev);
372 	if ((sp = ddi_get_soft_state(statep, instance)) == NULL)
373 		return (ENXIO);
374 	off = uio_p->uio_loffset;
375 	mutex_enter(&zev_mutex);
376 	while (zev_mq_head && uio_p->uio_resid) {
377 		mq_bytes = zev_mq_head->used - zev_mq_head->sent;
378 		if (mq_bytes <= 0) {
379 			mq = zev_mq_head;
380 			zev_mq_head = zev_mq_head->next;
381 			if (!zev_mq_head)
382 				zev_mq_tail = NULL;
383 			kmem_free(mq, sizeof(*mq));
384 			continue;
385 		}
386 		len = min(uio_p->uio_resid, mq_bytes);
387 		ret = uiomove(zev_mq_head->buf + zev_mq_head->sent, len,
388 		    UIO_READ, uio_p);
389 		if (ret != 0)
390 			break;
391 		zev_statistics.zev_bytes_read += len;
392 		zev_statistics.zev_queue_len -= len;
393 		zev_mq_head->sent += len;
394 		cv_broadcast(&zev_condvar);
395 	}
396 	mutex_exit(&zev_mutex);
397 	uio_p->uio_loffset = off;
398 	return (ret);
399 }
400 
401 static int
402 zev_close(dev_t dev, int flag, int otyp, cred_t *crepd)
403 {
404 	zev_state_t *sp;
405 	int instance;
406 
407 	instance = getminor(dev);
408 	if ((sp = ddi_get_soft_state(statep, instance)) == NULL)
409 		return (ENXIO);
410 	if (otyp != OTYP_CHR)
411 		return (EINVAL);
412 	mutex_enter(&sp->mutex);
413 	if (sp->busy != B_TRUE) {
414 		mutex_exit(&sp->mutex);
415 		return (EINVAL);
416 	}
417 	sp->busy = B_FALSE;
418 	mutex_exit(&sp->mutex);
419 	return (0);
420 }
421 
422 static int
423 zev_open(dev_t *devp, int flag, int otyp, cred_t *credp)
424 {
425 	zev_state_t *sp;
426 	int instance;
427 
428 	instance = getminor(*devp);
429 	if ((sp = ddi_get_soft_state(statep, instance)) == NULL)
430 		return (ENXIO);
431 	if (otyp != OTYP_CHR)
432 		return (EINVAL);
433 	if (drv_priv(credp) != 0)
434 		return (EPERM);
435 	mutex_enter(&sp->mutex);
436 	if (sp->busy == B_TRUE) {
437 		/* XXX: wait for the instance to become available? */
438 		/* XXX: if we wait, the wait should be signal-interruptable. */
439 		mutex_exit(&sp->mutex);
440 		return (EBUSY);
441 	}
442 	sp->busy = B_TRUE;	/* can only be opened exclusively */
443 	mutex_exit(&sp->mutex);
444 	return (0);
445 }
446 
447 static struct cb_ops zev_cb_ops = {
448 	zev_open,		/* open */
449 	zev_close,		/* close */
450 	nodev,			/* strategy */
451 	nodev,			/* print */
452 	nodev,			/* dump */
453 	zev_read,		/* read */
454 	nodev,			/* write */
455 	zev_ioctl,		/* ioctl */
456 	nodev,			/* devmap */
457 	nodev,			/* mmap */
458 	nodev,			/* segmap */
459 	zev_chpoll,		/* chpoll */
460 	ddi_prop_op,		/* prop_op */
461 	NULL,			/* streamtab */
462 	D_MP | D_64BIT,		/* cb_flag */
463 	CB_REV,			/* cb_rev */
464 	nodev,			/* aread */
465 	nodev,			/* awrite */
466 };
467 
468 static void
469 zev_free_instance(dev_info_t *dip)
470 {
471 	int instance;
472 	zev_state_t *sp;
473 	instance = ddi_get_instance(dip);
474 	//ddi_remove_minor_node(dip, ddi_get_name(dip));
475 	ddi_remove_minor_node(dip, NULL);
476 	sp = ddi_get_soft_state(statep, instance);
477 	if (sp) {
478 		mutex_destroy(&sp->mutex);
479 		ddi_soft_state_free(statep, instance);
480 	}
481 }
482 
483 static int
484 zev_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
485 {
486 	int instance;
487 	zev_state_t *sp;
488 	/* called once per instance with DDI_DETACH,
489 	   may be called to suspend */
490 	switch (cmd) {
491 	case DDI_DETACH:
492 		/* instance busy? */
493 		instance = ddi_get_instance(dip);
494 		if ((sp = ddi_get_soft_state(statep, instance)) == NULL)
495 			return (ENXIO);
496 		mutex_enter(&sp->mutex);
497 		if (sp->busy == B_TRUE) {
498 			mutex_exit(&sp->mutex);
499 			return (EBUSY);
500 		}
501 		mutex_exit(&sp->mutex);
502 		/* free resources allocated for this instance */
503 		zev_free_instance(dip);
504 		return (DDI_SUCCESS);
505 	case DDI_SUSPEND:
506 		/* kernel must not suspend zev devices while ZFS is running */
507 		return (DDI_FAILURE);
508 	default:
509 		return (DDI_FAILURE);
510 	}
511 }
512 
513 static int
514 zev_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
515 {
516 	/* called once per instance with DDI_ATTACH,
517 	   may be called to resume */
518 	int instance;
519 	zev_state_t *sp;
520 	switch (cmd) {
521 	case DDI_ATTACH:
522 		instance = ddi_get_instance(dip);
523 		if (ddi_soft_state_zalloc(statep, instance) != DDI_SUCCESS) {
524 			return (DDI_FAILURE);
525 		}
526 		sp = ddi_get_soft_state(statep, instance);
527 		ddi_set_driver_private(dip, sp);
528 		sp->dip = dip;
529 		sp->busy = B_FALSE;
530 		mutex_init(&sp->mutex, NULL, MUTEX_DRIVER, NULL);
531 		if (ddi_create_minor_node(dip, ddi_get_name(dip),
532 		    S_IFCHR, instance, DDI_PSEUDO, 0) == DDI_FAILURE) {
533 			zev_free_instance(dip);
534 			return (DDI_FAILURE);
535 		}
536 		ddi_report_dev(dip);
537 		return (DDI_SUCCESS);
538 	case DDI_RESUME:
539 		/* suspendeding zev devices should never happen */
540 		return (DDI_SUCCESS);
541 	default:
542 		return (DDI_FAILURE);
543 	}
544 }
545 
546 static int
547 zev_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **resultp)
548 {
549 	int instance;
550 	zev_state_t *sp;
551 	switch (infocmd) {
552 	case DDI_INFO_DEVT2DEVINFO:
553 		/* arg is dev_t */
554 		instance = getminor((dev_t)arg);
555 		if ((sp = ddi_get_soft_state(statep, instance)) != NULL) {
556 			*resultp = sp->dip;
557 			return (DDI_SUCCESS);
558 		}
559 		*resultp = NULL;
560 		return (DDI_FAILURE);
561 	case DDI_INFO_DEVT2INSTANCE:
562 		/* arg is dev_t */
563 		instance = getminor((dev_t)arg);
564 		*resultp = (void *)(uintptr_t)instance;
565 		return (DDI_FAILURE);
566 	}
567 	return (DDI_FAILURE);
568 }
569 
570 static struct dev_ops zev_dev_ops = {
571 	DEVO_REV,			/* driver build revision */
572 	0,				/* driver reference count */
573 	zev_getinfo,			/* getinfo */
574 	nulldev,			/* identify (obsolete) */
575 	nulldev,			/* probe (search for devices) */
576 	zev_attach,			/* attach */
577 	zev_detach,			/* detach */
578 	nodev,				/* reset (obsolete, use quiesce) */
579 	&zev_cb_ops,			/* character and block device ops */
580 	NULL,				/* bus driver ops */
581 	NULL,				/* power management, not needed */
582 	ddi_quiesce_not_needed,		/* quiesce */
583 };
584 
585 static struct modldrv zev_modldrv = {
586 	&mod_driverops,			/* all loadable modules use this */
587 	"zev ZFS event provider, v1.0",	/* driver name and version info */
588 	&zev_dev_ops			/* ops method pointers */
589 };
590 
591 static struct modlinkage zev_modlinkage = {
592 	MODREV_1,	/* fixed value */
593 	{
594 		&zev_modldrv,	/* driver linkage structure */
595 		NULL		/* list terminator */
596 	}
597 };
598 
599 int
600 _init(void)
601 {
602 	int error;
603 	boolean_t module_installed = B_FALSE;
604 
605 	if ((error = ddi_soft_state_init(&statep, sizeof(zev_state_t), 1)) != 0)
606 		return (error);
607 	zev_busy = B_FALSE;
608 
609 	mutex_init(&zev_mutex, NULL, MUTEX_DRIVER, NULL);
610 	cv_init(&zev_condvar, NULL, CV_DRIVER, NULL);
611 	rw_init(&zev_pool_list_rwlock, NULL, RW_DRIVER, NULL);
612 	bzero(&zev_statistics, sizeof(zev_statistics));
613 	zev_statistics.zev_max_queue_len = ZEV_MAX_QUEUE_LEN;
614 	zev_statistics.zev_poll_wakeup_queue_len =
615 	    ZEV_MIN_POLL_WAKEUP_QUEUE_LEN;
616 	if (zev_ioc_mute_pool("zg0")) {
617 		cmn_err(CE_WARN, "zev: could not init mute list");
618 		goto FAIL;
619 	}
620 
621 	if ((error = mod_install(&zev_modlinkage)) != 0) {
622 		cmn_err(CE_WARN, "zev: could not install module");
623 		goto FAIL;
624 	}
625 	module_installed = B_TRUE;
626 
627 	/*
628 	 * Note: _init() seems to be a bad place to access other modules'
629 	 * device files, as it can cause a kernel panic.
630 	 *
631 	 * For example, our _init() is called if our module isn't loaded
632 	 * when someone causes a readdir() in "/devices/pseudo".  For that,
633 	 * devfs_readdir() is used, which obtains an rwlock for the
634 	 * directory.
635 	 *
636 	 * Then, if we open a device file here, we will indirectly call
637 	 * devfs_lookup(), which tries to obtain the same rwlock
638 	 * again, which this thread already has.  That will result in
639 	 * a kernel panic. ("recursive entry")
640 	 *
641 	 * Therefor, we have switched from a zfs ioctl() to directly
642 	 * accessing symbols in the zfs module.
643 	 */
644 
645 	/* switch ZFS event callbacks to zev module callback functions */
646 	rw_enter(&rz_zev_rwlock, RW_WRITER);
647 	rz_zev_callbacks = &zev_callbacks;
648 	rw_exit(&rz_zev_rwlock);
649 
650 	zev_poll_wakeup_thread = thread_create(NULL, 0,
651 	    zev_poll_wakeup_thread_main, NULL, 0, &p0, TS_RUN, minclsyspri);
652 	return (0);
653 FAIL:
654 	/* free resources */
655 	if (module_installed == B_TRUE)
656 		(void) mod_remove(&zev_modlinkage);
657 	mutex_destroy(&zev_mutex);
658 	ddi_soft_state_fini(&statep);
659 	return (error);
660 }
661 
662 int
663 _info(struct modinfo *modinfop)
664 {
665 	return (mod_info(&zev_modlinkage, modinfop));
666 }
667 
668 int
669 _fini(void)
670 {
671 	int error = 0;
672 	zev_mq_t *mq;
673 	zev_pool_list_entry_t *pe, *npe;
674 
675 	mutex_enter(&zev_mutex);
676 	if (zev_busy == B_TRUE) {
677 		mutex_exit(&zev_mutex);
678 		return (SET_ERROR(EBUSY));
679 	}
680 	mutex_exit(&zev_mutex);
681 
682 	/* switch ZFS event callbacks back to default */
683 	rw_enter(&rz_zev_rwlock, RW_WRITER);
684 	rz_zev_callbacks = rz_zev_default_callbacks;
685 	rw_exit(&rz_zev_rwlock);
686 
687 	/* no thread is inside of the callbacks anymore.  Safe to remove. */
688 	zev_wakeup_thread_run = 0;
689 	if (zev_poll_wakeup_thread != 0) {
690 		thread_join(zev_poll_wakeup_thread->t_did);
691 		zev_poll_wakeup_thread = 0;
692 	}
693 	if ((error = mod_remove(&zev_modlinkage)) != 0) {
694 		cmn_err(CE_WARN, "mod_remove failed: %d", error);
695 		return (error);
696 	}
697 
698 	/* free resources */
699 	mutex_enter(&zev_mutex);
700 	while (zev_mq_head) {
701 		mq = zev_mq_head;
702 		zev_mq_head = zev_mq_head->next;
703 		if (mq)
704 			kmem_free(mq, sizeof(*mq));
705 	}
706 	mutex_exit(&zev_mutex);
707 	rw_enter(&zev_pool_list_rwlock, RW_WRITER);
708 	pe = zev_muted_pools_head;
709 	while (pe) {
710 		npe = pe;
711 		pe = pe->next;
712 		kmem_free(npe, sizeof(*npe));
713 	}
714 	rw_exit(&zev_pool_list_rwlock);
715 	ddi_soft_state_fini(&statep);
716 	rw_destroy(&zev_pool_list_rwlock);
717 	cv_destroy(&zev_condvar);
718 	mutex_destroy(&zev_mutex);
719 
720 	return (0);
721 }
722 
723