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.
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.
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]
21 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
22 /* All Rights Reserved */
26 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/errno.h>
38 #include <sys/signal.h>
40 #include <sys/vnode.h>
43 #include <sys/stropts.h>
44 #include <sys/stream.h>
45 #include <sys/errno.h>
46 #include <sys/sysinfo.h>
47 #include <sys/systm.h>
49 #include <sys/debug.h>
51 #include <sys/mkdev.h>
53 #include <sys/strsubr.h>
55 #include <sys/sunddi.h>
56 #include <sys/modctl.h>
57 #include <sys/policy.h>
59 int clnopen(queue_t
*rq
, dev_t
*devp
, int flag
, int sflag
, cred_t
*crp
);
61 static struct module_info clnm_info
= { 0, "CLONE", 0, 0, 0, 0 };
62 static struct qinit clnrinit
= { NULL
, NULL
, clnopen
, NULL
, NULL
, &clnm_info
,
64 static struct qinit clnwinit
= { NULL
, NULL
, NULL
, NULL
, NULL
, &clnm_info
,
66 struct streamtab clninfo
= { &clnrinit
, &clnwinit
};
68 static int cln_info(dev_info_t
*, ddi_info_cmd_t
, void *, void **);
69 static int cln_attach(dev_info_t
*, ddi_attach_cmd_t
);
70 static dev_info_t
*cln_dip
; /* private copy of devinfo pointer */
72 #define CLONE_CONF_FLAG (D_NEW|D_MP)
74 DDI_DEFINE_STREAM_OPS(clone_ops
, nulldev
, nulldev
, cln_attach
, nodev
, nodev
, \
75 cln_info
, CLONE_CONF_FLAG
, &clninfo
, ddi_quiesce_not_needed
);
78 * Module linkage information for the kernel.
81 static struct modldrv modldrv
= {
82 &mod_driverops
, /* Type of module. This one is a pseudo driver */
83 "Clone Pseudodriver 'clone'",
84 &clone_ops
, /* driver ops */
87 static struct modlinkage modlinkage
= {
97 return (mod_install(&modlinkage
));
104 * Since the clone driver's reference count is unreliable,
105 * make sure we are never unloaded.
111 _info(struct modinfo
*modinfop
)
113 return (mod_info(&modlinkage
, modinfop
));
118 cln_attach(dev_info_t
*devi
, ddi_attach_cmd_t cmd
)
121 return (DDI_SUCCESS
);
126 cln_info(dev_info_t
*dip
, ddi_info_cmd_t infocmd
, void *arg
,
132 case DDI_INFO_DEVT2DEVINFO
:
133 if (cln_dip
== NULL
) {
136 *result
= (void *)cln_dip
;
140 case DDI_INFO_DEVT2INSTANCE
:
151 * Clone open. Maj is the major device number of the streams
152 * device to open. Look up the device in the cdevsw[]. Attach
153 * its qinit structures to the read and write queues and call its
154 * open with the sflag set to CLONEOPEN. Swap in a new vnode with
155 * the real device number constructed from either
156 * a) for old-style drivers:
157 * maj and the minor returned by the device open, or
158 * b) for new-style drivers:
159 * the whole dev passed back as a reference parameter
160 * from the device open.
163 clnopen(queue_t
*rq
, dev_t
*devp
, int flag
, int sflag
, cred_t
*crp
)
165 struct streamtab
*str
;
170 struct qinit
*rinit
, *winit
;
181 * Get the device to open.
183 emaj
= getminor(*devp
); /* minor is major for a cloned driver */
184 maj
= etoimajor(emaj
); /* get internal major of cloned driver */
190 * NOTE We call ddi_hold_installed_driver() here to attach
191 * all instances of the driver, since we do not know
192 * a priori which instance the Stream is associated with.
194 * For Style-2 network drivers, we know that the association
195 * happens at DL_ATTACH time. For other types of drivers,
196 * open probably requires attaching instance 0 (pseudo dip).
198 * To eliminate ddi_hold_installed_driver(), the following
201 * - GLD be modified to include gld_init(). The driver will
202 * register information for gld_open() to succeed. It will
203 * also inform framework if driver assigns instance=PPA.
204 * - ddi_hold_devi_by_instance() be modified to actively
205 * attach the instance via top-down enumeration.
207 if (ddi_hold_installed_driver(maj
) == NULL
)
210 if ((str
= STREAMSTAB(maj
)) == NULL
) {
211 ddi_rele_driver(maj
);
215 newdev
= makedevice(emaj
, 0); /* create new style device number */
218 * Check for security here. For DLPI style 2 network
219 * drivers, we need to apply the default network policy.
220 * Clone is weird in that the network driver isn't loaded
221 * and attached at spec_open() time, we need to load the
222 * driver to see if it is a network driver. Hence, we
223 * check security here (after ddi_hold_installed_driver
226 vp
= makespecvp(newdev
, VCHR
);
227 error
= secpolicy_spec_open(crp
, vp
, flag
);
230 ddi_rele_driver(maj
);
235 * Save so that we can restore the q on failure.
238 winit
= WR(rq
)->q_qinfo
;
239 ASSERT(rq
->q_syncq
->sq_type
== (SQ_CI
|SQ_CO
));
240 ASSERT((rq
->q_flag
& QMT_TYPEMASK
) == QMTSAFE
);
243 ASSERT(str
== dp
->d_str
);
246 sqtype
= dp
->d_sqtype
;
248 /* create perdm_t if needed */
249 if (NEED_DM(dp
->d_dmp
, qflag
))
250 dp
->d_dmp
= hold_dm(str
, qflag
, sqtype
);
255 * Set the syncq state what qattach started off with. This is safe
256 * since no other thread can access this queue at this point
257 * (stream open, close, push, and pop are single threaded
260 leavesq(rq
->q_syncq
, SQ_OPENCLOSE
);
263 * Substitute the real qinit values for the current ones.
265 /* setq might sleep in kmem_alloc - avoid holding locks. */
266 setq(rq
, str
->st_rdinit
, str
->st_wrinit
, dmp
, qflag
, sqtype
, B_FALSE
);
269 * Open the attached module or driver.
271 * If there is an outer perimeter get exclusive access during
272 * the open procedure.
273 * Bump up the reference count on the queue.
275 entersq(rq
->q_syncq
, SQ_OPENCLOSE
);
278 * Call the device open with the stream flag CLONEOPEN. The device
279 * will either fail this or return the device number.
281 error
= (*rq
->q_qinfo
->qi_qopen
)(rq
, &newdev
, flag
, CLONEOPEN
, crp
);
286 if (getmajor(newdev
) != emaj
)
295 (*rq
->q_qinfo
->qi_qclose
)(rq
, flag
, crp
);
298 cmn_err(CE_NOTE
, "cannot clone major number %d(%s)->%d", emaj
,
299 ddi_major_to_name(emaj
), getmajor(newdev
));
305 * open failed; pretty up to look like original
308 if (backq(WR(rq
)) && backq(WR(rq
))->q_next
== WR(rq
))
310 leavesq(rq
->q_syncq
, SQ_OPENCLOSE
);
311 rq
->q_next
= WR(rq
)->q_next
= NULL
;
312 ASSERT(flush_syncq(rq
->q_syncq
, rq
) == 0);
313 ASSERT(flush_syncq(WR(rq
)->q_syncq
, WR(rq
)) == 0);
314 rq
->q_ptr
= WR(rq
)->q_ptr
= NULL
;
315 /* setq might sleep in kmem_alloc - avoid holding locks. */
316 setq(rq
, rinit
, winit
, NULL
, QMTSAFE
, SQ_CI
|SQ_CO
,
319 /* Restore back to what qattach will expect */
320 entersq(rq
->q_syncq
, SQ_OPENCLOSE
);
322 ddi_rele_driver(maj
);