summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/iwscons.c
blob: 0d02ad8b241f73666504824c964a7939f36085c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * Redirecting driver; used to handle workstation console redirection.
 *
 * Redirects all I/O through a given device instance to the device designated
 * as the current target, as given by the vnode associated with the first
 * entry in the list of redirections for the given device instance.  The
 * implementation assumes that this vnode denotes a STREAMS device; this is
 * perhaps a bug.
 *
 * Supports the SRIOCSREDIR ioctl for designating a new redirection target.
 * The new target is added to the front of a list of potentially active
 * designees.  Should the device at the front of this list be closed, the new
 * front entry assumes active duty.  (Stated differently, redirection targets
 * stack, except that it's possible for entries in the interior of the stack
 * to go away.)
 *
 * Supports the SRIOCISREDIR ioctl for inquiring whether the descriptor given
 * as argument is the current front of the redirection list associated with
 * the descriptor on which the ioctl was issued.
 *
 * Every open instance of this driver corresponds to an instance of the
 * underlying client driver.  If the redirection stack would otherwise become
 * empty, this device (designated by the wd_vp field of the wcd_data
 * structure) is implicitly opened and added to the front of the list.  Thus,
 * there's always an active device for handling i/o through an open instance
 * of this driver.
 *
 * XXX: Names -- many of the names in this driver and its companion STREAMS
 *	module still reflect its origins as the workstation console
 *	redirection driver.  Ultimately, they should be changed to reflect the
 *	fact that this driver is potentially a general purpose redirection
 *	driver.  In the meantime, the driver is still specialized to have a
 *	single client -- the workstation console driver -- and its file name
 *	remains iwscons.c to reflect that specialization.
 *
 *	Proposed change: "iwscn" becomes either "dr" (for "streams redirecting
 *	driver") or "srm" (for "streams redirecting module"), as appropriate.
 *
 * XXX:	Add mechanism for notifying a redirectee that it's no longer the
 *	current redirectee?  (This in contrast to the current facility for
 *	letting it ask.)
 */

#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/open.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/signal.h>
#include <sys/cred.h>
#include <sys/user.h>
#include <sys/proc.h>
#include <sys/vnode.h>
#include <sys/uio.h>
#include <sys/file.h>
#include <sys/kmem.h>
#include <sys/stat.h>

#include <sys/stream.h>
#include <sys/stropts.h>
#include <sys/strsubr.h>
#include <sys/poll.h>

#include <sys/debug.h>

#include <sys/strredir.h>

#include <sys/conf.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>

static int	iwscninfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
static int	iwscnattach(dev_info_t *, ddi_attach_cmd_t);
static int	iwscnopen(dev_t *, int, int, cred_t *);
static int	iwscnclose(dev_t, int, int, cred_t *);
static int	iwscnread(dev_t, struct uio *, cred_t *);
static int	iwscnwrite(dev_t, struct uio *, cred_t *);
static int	iwscnioctl(dev_t, int, intptr_t, int, cred_t *, int *);
static int	iwscnpoll(dev_t, short, int, short *, struct pollhead **);

/*
 * Private copy of devinfo pointer; iwscninfo uses it.
 */
static dev_info_t	*iwscn_dip;

struct cb_ops	iwscn_cb_ops = {
	iwscnopen,		/* open */
	iwscnclose,		/* close */
	nodev,			/* strategy */
	nodev,			/* print */
	nodev,			/* dump */
	iwscnread,		/* read */
	iwscnwrite,		/* write */
	iwscnioctl,		/* ioctl */
	nodev,			/* devmap */
	nodev,			/* mmap */
	nodev, 			/* segmap */
	iwscnpoll,		/* poll */
	ddi_prop_op,		/* cb_prop_op */
	0,			/* streamtab  */
	D_NEW|D_MP		/* Driver compatibility flag */
};

struct dev_ops	iwscn_ops = {
	DEVO_REV,		/* devo_rev, */
	0,			/* refcnt  */
	iwscninfo,		/* info */
	nulldev,		/* identify */
	nulldev,		/* probe */
	iwscnattach,		/* attach */
	nodev,			/* detach */
	nodev,			/* reset */
	&iwscn_cb_ops,		/* driver operations */
	NULL			/* bus operations */
};

static krwlock_t	iwscn_lock; /* lock proecting almost everything here */

/*
 * A read/write lock was used to serialize reads,writes/opens,closes.
 * Sometime the open would hang due to a pending read.  The new lock
 * iwscn_open_lock and the read lock are held in open to assure a single
 * instance, while letting concurrent reads/writes to proceed.
 */
static kmutex_t 	iwscn_open_lock; /* Serializes opens  */

/*
 * These next two fields, protected by iwscn_lock, pass the data to wcmopen()
 * from the ioctl SRIOCSREDIR.  wcmopen() uses the data only if the thread
 * matches.  This keeps other threads from interfering.
 */
extern kthread_id_t	iwscn_thread;	/* thread that is allowed to */
					/* push redirm */
extern wcm_data_t	*iwscn_wcm_data;  /* allocated data for redirm */

/*
 * Forward declarations of private routines.
 */
static int		srreset(wcd_data_t *, int, cred_t *);
static wcrlist_t	*srrm(wcrlist_t **, vnode_t *, int);
static wcd_data_t	*srilookup(minor_t);
static wcd_data_t	*srialloc(minor_t);
static void		sridealloc(wcd_data_t *);
static wcrlist_t	*srpush(wcrlist_t **, vnode_t *);

/*
 * The head of the list of open instances.
 */
static wcd_data_t	*wcddata;

/*
 * Currently, the only client of this driver is the workstation console
 * driver.  Thus, we can get away with hard-wiring a reference to it here.
 *
 * To handle multiple clients, the driver must be revised as follows.
 * 1)	Add a registration routine that clients can call to announce
 *	themselves to this driver.  The routine should take as arguments the
 *	major device number of the corresponding instantiation of the
 *	redirecting driver and a pointer to its dedvnops ops vector.
 * 2)	Maintain a list (or perhaps hash array) or registered clients,
 *	recording for each the srvnops ops vector and a pointer to the list
 *	of open instances for that client.
 * 3)	Modify the driver entry points to use their dev argument to look up
 *	the proper instantiation, get the list of open instances, and then use
 *	that as they currently use the open instance list.
 * 4)	To allow clients to unload themselves, we probably need an unregister
 *	routine.  This routine would have to cope with active open instances.
 */
extern srvnops_t	wscons_srvnops;

#include <sys/types.h>
#include <sys/conf.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/modctl.h>

/*
 * Module linkage information for the kernel.
 */

static struct modldrv modldrv = {
	&mod_driverops, /* Type of module.  This one is a pseudo driver */
	"Workstation Redirection driver 'iwscn' %I%",
	&iwscn_ops,	/* driver ops */
};

static struct modlinkage modlinkage = {
	MODREV_1,
	&modldrv,
	NULL
};


int
_init(void)
{
	return (mod_install(&modlinkage));
}

int
_fini(void)
{
	return (EBUSY);
}

int
_info(struct modinfo *modinfop)
{
	return (mod_info(&modlinkage, modinfop));
}

/*
 * DDI glue routines.
 */

/*ARGSUSED*/
static int
iwscnattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
{
	static char	been_here;

	if (!been_here) {
		been_here = 1;
		rw_init(&iwscn_lock, NULL, RW_DEFAULT, NULL);
	}
	if (ddi_create_minor_node(devi, "iwscn", S_IFCHR,
	    0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
		ddi_remove_minor_node(devi, NULL);
		return (-1);
	}
	iwscn_dip = devi;
	return (DDI_SUCCESS);
}

/* ARGSUSED */
static int
iwscninfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
{
	int error;

	switch (infocmd) {
	case DDI_INFO_DEVT2DEVINFO:
		if (iwscn_dip == NULL) {
			error = DDI_FAILURE;
		} else {
			*result = (void *)iwscn_dip;
			error = DDI_SUCCESS;
		}
		break;
	case DDI_INFO_DEVT2INSTANCE:
		*result = (void *)0;
		error = DDI_SUCCESS;
		break;
	default:
		error = DDI_FAILURE;
	}
	return (error);
}


/* ARGSUSED */
static int
iwscnopen(
	dev_t	*devp,
	int	flag,
	int	state,		/* should be OTYP_CHR */
	cred_t	*cred)
{
	minor_t		unit = getminor(*devp);
	wcd_data_t	*wd;
	int		err = 0;
	struct wcrlist	*wwd;

	if (state != OTYP_CHR)
		return (ENXIO);
	rw_enter(&iwscn_lock, RW_READER);
	mutex_enter(&iwscn_open_lock);
	if ((wd = srilookup(unit)) == NULL) {
		vnode_t	*vp;

		/*
		 * First open for this instance; get a state structure for it.
		 */
		wd = srialloc(unit);

		/*
		 * Call the client driver to obtain a held vnode for the
		 * underlying "real" device instance.
		 *
		 * XXX:	There's wired in knowledge of the client driver here.
		 */
		err = wscons_srvnops.svn_get(unit, &vp);
		if (err != 0) {
			sridealloc(wd);
			mutex_exit(&iwscn_open_lock);
			rw_exit(&iwscn_lock);
			return (err);
		}
		wd->wd_vp = vp;
	}

	/*
	 * Reinitalize the list if necessary.
	 *
	 * XXX:	Is it possible for the list to empty completely while this
	 *	instance is still open?  If not, this if should be coalesced
	 *	with the previous one.
	 */
	if (wd->wd_list == NULL) {
		wcrlist_t	*e = srpush(&wd->wd_list, wd->wd_vp);

		/*
		 * There's no corresponding redirecting module instance for
		 * the underlying device.
		 */
		e->wl_data = NULL;
	}

	err = srreset(wd, flag, cred);
	/*
	 *  XXX cleanup the sig list.  Hook for console driver.
	 */
	for (wwd = wd->wd_list; wwd != NULL; wwd = wwd->wl_next) {
		ASSERT(wwd->wl_vp->v_stream != NULL);
		str_cn_clean(wwd->wl_vp);
	}
	mutex_exit(&iwscn_open_lock);
	rw_exit(&iwscn_lock);
	return (err);
}

/* ARGSUSED */
static int
iwscnclose(
	dev_t	dev,
	int	flag,
	int	state,		/* should be OTYP_CHR */
	cred_t	*cred)
{
	wcd_data_t	*wd;
	int		err = 0;

	if (state != OTYP_CHR)
		return (ENXIO);
	rw_enter(&iwscn_lock, RW_WRITER);
	wd = srilookup(getminor(dev));
	/*
	 * Remove all outstanding redirections for this instance.
	 */
	while (wd->wd_list != NULL)
		(void) srrm(&wd->wd_list, wd->wd_list->wl_vp, 1);

	/*
	 * Since this is the _last_ close, it's our last chance to close the
	 * underlying device.  (Note that if someone else has the underlying
	 * workstation console device open, we won't get here, since
	 * spec_close will see s_count > 1.)
	 */
	while ((wd->wd_wsconsopen != 0) && (!err)) {
		err = VOP_CLOSE(wd->wd_vp, flag, 1, (offset_t)0, cred);
		if (!err)
			wd->wd_wsconsopen--;
	}
	if (!err)
		wd->wd_vp->v_stream = NULL;

	/*
	 * We don't need the vnode that the client driver gave us any more.
	 *
	 * XXX:	There's wired in knowledge of the client driver here.
	 */
	wscons_srvnops.svn_rele(wd->wd_unit, wd->wd_vp);
	sridealloc(wd);

	rw_exit(&iwscn_lock);
	return (err);
}

static int
iwscnread(dev_t dev, uio_t *uio, cred_t *cred)
{
	wcd_data_t	*wd;
	int error;

	rw_enter(&iwscn_lock, RW_READER);
	wd = srilookup(getminor(dev));
	error = strread(wd->wd_list->wl_vp, uio, cred);
	rw_exit(&iwscn_lock);
	return (error);
}

static int
iwscnwrite(dev_t dev, uio_t *uio, cred_t *cred)
{
	wcd_data_t	*wd;
	int error;

	rw_enter(&iwscn_lock, RW_READER);
	wd = srilookup(getminor(dev));
	error = strwrite(wd->wd_list->wl_vp, uio, cred);
	rw_exit(&iwscn_lock);
	return (error);
}

static int
iwscnioctl(dev_t dev, int cmd, intptr_t arg, int flag,
    cred_t *cred, int *rvalp)
{
	wcd_data_t *wd;
	int err = 0;
	file_t *f;

	switch (cmd) {
	case SRIOCSREDIR: {
		wcrlist_t	*wlp;
		wcm_data_t	*mdp;

		if (!rw_tryenter(&iwscn_lock, RW_WRITER)) {
			return (EBUSY);
		}
		wd = srilookup(getminor(dev));
		/*
		 * Find the vnode corresponding to the file descriptor
		 * argument and verify that it names a stream.
		 */
		if ((f = getf((int)arg)) == NULL) {
			err = EBADF;
			break;
		}
		if (f->f_vnode->v_stream == NULL) {
			err = ENOSTR;
			releasef((int)arg);
			break;
		}
		/*
		 * allocate the private data for redirmod, and pass it through
		 * a global to wcmopen().  This is all protected by iwscn_lock.
		 */
		mdp = kmem_alloc(sizeof (*mdp), KM_SLEEP);
		iwscn_wcm_data = mdp;
		iwscn_thread = curthread;

		/*
		 * Push a new instance of the redirecting module onto the
		 * stream, so that its close routine can notify us when the
		 * overall stream is closed.  (In turn, we'll then remove it
		 * from the redirection list.)
		 */
		if ((err = VOP_IOCTL(f->f_vnode, I_PUSH, (intptr_t)"redirmod",
		    (FREAD | FKIOCTL), cred, rvalp)) != 0) {
			iwscn_thread = NULL;
			kmem_free(mdp, sizeof (*mdp));
			releasef((int)arg);
			break;
		}
		iwscn_thread = NULL;	/* clear authorization for wcmopen() */

		/*
		 * Push it onto the redirection stack.
		 */
		wlp = srpush(&wd->wd_list, f->f_vnode);
		/*
		 * Fill in the redirecting module instance's private data with
		 * information to let it get to our redirection list when its
		 * close routine is called.  Cross-link it with the
		 * redirection list entry.
		 */
		mdp->wm_wd = wd;
		mdp->wm_entry = wlp;
		wlp->wl_data = mdp;
		releasef((int)arg);

		break;
	    }

	case SRIOCISREDIR:
		rw_enter(&iwscn_lock, RW_READER);
		wd = srilookup(getminor(dev));
		if ((f = getf((int)arg)) == NULL) {
			err = EBADF;
			break;
		}
		/*
		 * Return value is 1 if the argument descriptor is the current
		 * redirection target, and 0 otherwise.
		 */
		*rvalp = (f->f_vnode == wd->wd_list->wl_vp) ? 1 : 0;
		releasef((int)arg);
		break;

	case I_POP: {
		/*
		 * XXX - This is a big kludge the handles a deadlock case
		 * when we are trying to pop off the redirection
		 * module.  Since this should only happen on a close
		 * of the device, and since it hangs the system, just
		 * do not allow a pop of the redirection module to happen.
		 * Popping other modules is allowed.
		 */
		struct stdata	*stp;
		char modname[FMNAMESZ + 1] = " ";

		rw_enter(&iwscn_lock, RW_READER);
		wd = srilookup(getminor(dev));
		(void) strioctl(wd->wd_list->wl_vp, I_LOOK, (intptr_t)modname,
		    flag, K_TO_K, cred, rvalp);
		if (strcmp("redirmod", modname) == 0) {
			if ((f = getf((int)arg)) == NULL) {
				err = EBADF;
				break;
			}
			if ((stp = f->f_vnode->v_stream) == NULL) {
				err = ENOSTR;
				releasef((int)arg);
				break;
			}
			if (!(stp->sd_flag & STRCLOSE)) {
				releasef((int)arg);
				rw_exit(&iwscn_lock);
				cmn_err(CE_WARN, "Popping of redirection "
				    "module not allowed");
				return (EINVAL);
			}

			releasef((int)arg);
		}

		/* Process ioctl normally */
		err = strioctl(wd->wd_list->wl_vp, cmd, arg, flag, U_TO_K,
		    cred, rvalp);
		break;
	}

	default:
		rw_enter(&iwscn_lock, RW_READER);
		wd = srilookup(getminor(dev));
		err = strioctl(wd->wd_list->wl_vp, cmd, arg, flag, U_TO_K,
			cred, rvalp);
		break;
	}

	rw_exit(&iwscn_lock);
	return (err);
}

static int
iwscnpoll(
	dev_t			dev,
	short			events,
	int			anyyet,
	short			*reventsp,
	struct pollhead		**phpp)
{
	wcd_data_t	*wd;
	int	error;

	rw_enter(&iwscn_lock, RW_READER);
	wd = srilookup(getminor(dev));
	error = strpoll(wd->wd_list->wl_vp->v_stream, events, anyyet,
		    reventsp, phpp);
	rw_exit(&iwscn_lock);
	return (error);
}


/*
 * Auxiliary routines.
 */

/*
 * Additional public interfaces.
 */

/*
 * Reset the current workstation console designee to the device denoted by the
 * wl_vp field of the first entry in the redirection list.  Called from
 * iwscnopen and from the SRIOCSREDIR case of iwscnioctl, in both cases after
 * the target vp has been set to its new value.
 */
static int
srreset(wcd_data_t *wd, int flag, cred_t *cred)
{
	wcrlist_t	*wlp;
	int		err = 0;

	ASSERT(RW_WRITE_HELD(&iwscn_lock) || MUTEX_HELD(&iwscn_open_lock));
	wlp = wd->wd_list;		/* first entry */

	/*
	 * If we're reverting back to the workstation console, make sure it's
	 * open.
	 */
	if (wlp != NULL && wlp->wl_vp == wd->wd_vp) {
		vnode_t	*vp = wd->wd_vp;	/* underlying device's vp */

		err = VOP_OPEN(&vp, flag, cred);
		/*
		 * The underlying driver is not allowed to have cloned itself
		 * for this open.
		 */
		if (vp != wd->wd_vp) {
			panic("srreset: Illegal clone");
			/*NOTREACHED*/
		}
		if (!err)
			wd->wd_wsconsopen++;
	}
	return (err);
}

/*
 * Remove vp from the redirection list rooted at *rwlp, should it be there.
 * If zap is nonzero, deallocate the entry and remove dangling references to
 * the it from the corresponding redirecting module instance's wcm_data
 * structure.
 *
 * If the entry doesn't exist upon completion, return NULL; otherwise return a
 * pointer to it.
 */
static wcrlist_t *
srrm(wcrlist_t **rwlp, vnode_t *vp, int zap)
{
	wcrlist_t	**delwlp;
	wcrlist_t	*wlp;
	wcm_data_t	*mdp;

	ASSERT(RW_WRITE_HELD(&iwscn_lock) || MUTEX_HELD(&iwscn_open_lock));
	for (delwlp = rwlp; (wlp = *delwlp) != NULL; delwlp = &wlp->wl_next)
		if (wlp->wl_vp == vp)
			break;
	if (wlp == NULL)
		return (NULL);
	*delwlp = wlp->wl_next;

	if (zap == 0)
		return (wlp);

	if (wlp->wl_vp == vp)
		VN_RELE(vp);
	/*
	 * Make sure there are no dangling references leading to the entry
	 * from the corresponding redirecting module instance.
	 */
	if ((mdp = wlp->wl_data) != NULL) {
		mdp->wm_wd = NULL;
		mdp->wm_entry = NULL;
	}

	kmem_free(wlp, sizeof (*wlp));
	return (NULL);
}

/*
 * srpop - remove redirection because the target stream is being closed.
 * Called from wcmclose().
 */
void
srpop(wcm_data_t *mdp, int flag, cred_t *cred)
{
	wcd_data_t	*ddp;

	rw_enter(&iwscn_lock, RW_WRITER);
	if ((ddp = mdp->wm_wd) != NULL) {
		ASSERT(mdp->wm_entry != NULL);
		(void) srrm(&ddp->wd_list, mdp->wm_entry->wl_vp, 1);
		(void) srreset(ddp, flag, cred);
	}
	rw_exit(&iwscn_lock);
}

/*
 * Routines for allocating, deallocating, and finding wcd_data structures.
 *
 * For a given instantiation of the driver, its open instance structures are
 * linked together into a list, on the assumption that there will never be
 * enough open instances to make search efficiency a serious concern.
 */

/*
 * Look up the instance structure denoted by unit.
 */
static wcd_data_t *
srilookup(minor_t unit)
{
	wcd_data_t	*wd = wcddata;

	ASSERT(RW_LOCK_HELD(&iwscn_lock));
	for (; wd != NULL && wd->wd_unit != unit; wd = wd->wd_next)
		continue;

	return (wd);
}

/*
 * Allocate a wcd_data structure for the instance denoted by unit, link it in
 * place, and return a pointer to it.  If it's already allocated, simply
 * return a pointer to it.
 */
static wcd_data_t *
srialloc(minor_t unit)
{
	wcd_data_t	*wdp;
	wcd_data_t	**wdpp;

	ASSERT(MUTEX_HELD(&iwscn_open_lock));
	for (wdpp = &wcddata; (wdp = *wdpp) != NULL; wdpp = &wdp->wd_next) {
		if (unit < wdp->wd_unit)
			break;
		if (unit == wdp->wd_unit) {
			/* Already allocated and in place. */
			return (wdp);
		}
	}
	/*
	 * wdpp now points to the proper insertion point for unit's
	 * per-instance structure.
	 */
	wdp = kmem_zalloc(sizeof (*wdp), KM_SLEEP);
	wdp->wd_unit = unit;
	wdp->wd_next = *wdpp;
	*wdpp = wdp;

	return (wdp);
}

/*
 * Deallocate the wcd_data structure denoted by wd and unlink it from the
 * list of open instances.
 */
static void
sridealloc(wcd_data_t *wd)
{
	wcd_data_t	*wdp;
	wcd_data_t	**wdpp;

	ASSERT(RW_WRITE_HELD(&iwscn_lock) || MUTEX_HELD(&iwscn_open_lock));
	for (wdpp = &wcddata; (wdp = *wdpp) != NULL; wdpp = &wdp->wd_next)
		if (wd == wdp)
			break;
	if (wdp == NULL) {
		/*
		 * Not there.  This should probably be a panic.
		 */
		return;
	}
	*wdpp = wdp->wd_next;
	kmem_free(wdp, sizeof (*wdp));
}


/*
 * Push vp onto the redirection list rooted at *wlpp.  If it's already there,
 * move it to the front position.  Return a pointer to its list entry.
 *
 * N.B.: It is the caller's responsibility to initialize all fields in the
 * entry other than the wl_next and wl_vp fields.
 */
static wcrlist_t *
srpush(wcrlist_t **wlpp, vnode_t *vp)
{
	wcrlist_t	*nwlp;

	ASSERT(RW_WRITE_HELD(&iwscn_lock) || MUTEX_HELD(&iwscn_open_lock));
	if ((nwlp = srrm(wlpp, vp, 0)) == NULL) {
		nwlp = kmem_zalloc(sizeof (*nwlp), KM_SLEEP);
		nwlp->wl_vp = vp;
		/*
		 * The hold will prevent underlying device from closing
		 * while this vnode is still on the redirection list.
		 */
		VN_HOLD(vp);
	}
	nwlp->wl_next = *wlpp;
	*wlpp = nwlp;

	return (nwlp);
}