xref: /netbsd-src/sys/dev/dkwedge/dk.c (revision 2b3d1ee8a773e028429b331332895d44f445d720)
1 /*	$NetBSD: dk.c,v 1.64 2012/06/10 17:05:19 mlelstv Exp $	*/
2 
3 /*-
4  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.64 2012/06/10 17:05:19 mlelstv Exp $");
34 
35 #ifdef _KERNEL_OPT
36 #include "opt_dkwedge.h"
37 #endif
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/proc.h>
42 #include <sys/errno.h>
43 #include <sys/pool.h>
44 #include <sys/ioctl.h>
45 #include <sys/disklabel.h>
46 #include <sys/disk.h>
47 #include <sys/fcntl.h>
48 #include <sys/buf.h>
49 #include <sys/bufq.h>
50 #include <sys/vnode.h>
51 #include <sys/stat.h>
52 #include <sys/conf.h>
53 #include <sys/callout.h>
54 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56 #include <sys/device.h>
57 #include <sys/kauth.h>
58 
59 #include <miscfs/specfs/specdev.h>
60 
61 MALLOC_DEFINE(M_DKWEDGE, "dkwedge", "Disk wedge structures");
62 
63 typedef enum {
64 	DKW_STATE_LARVAL	= 0,
65 	DKW_STATE_RUNNING	= 1,
66 	DKW_STATE_DYING		= 2,
67 	DKW_STATE_DEAD		= 666
68 } dkwedge_state_t;
69 
70 struct dkwedge_softc {
71 	struct device	*sc_dev;	/* pointer to our pseudo-device */
72 	struct cfdata	sc_cfdata;	/* our cfdata structure */
73 	uint8_t		sc_wname[128];	/* wedge name (Unicode, UTF-8) */
74 
75 	dkwedge_state_t sc_state;	/* state this wedge is in */
76 
77 	struct disk	*sc_parent;	/* parent disk */
78 	daddr_t		sc_offset;	/* LBA offset of wedge in parent */
79 	uint64_t	sc_size;	/* size of wedge in blocks */
80 	char		sc_ptype[32];	/* partition type */
81 	dev_t		sc_pdev;	/* cached parent's dev_t */
82 					/* link on parent's wedge list */
83 	LIST_ENTRY(dkwedge_softc) sc_plink;
84 
85 	struct disk	sc_dk;		/* our own disk structure */
86 	struct bufq_state *sc_bufq;	/* buffer queue */
87 	struct callout	sc_restart_ch;	/* callout to restart I/O */
88 
89 	u_int		sc_iopend;	/* I/Os pending */
90 	int		sc_flags;	/* flags (splbio) */
91 };
92 
93 #define	DK_F_WAIT_DRAIN		0x0001	/* waiting for I/O to drain */
94 
95 static void	dkstart(struct dkwedge_softc *);
96 static void	dkiodone(struct buf *);
97 static void	dkrestart(void *);
98 static void	dkminphys(struct buf *);
99 
100 static int	dklastclose(struct dkwedge_softc *);
101 static int	dkwedge_detach(device_t, int);
102 
103 static dev_type_open(dkopen);
104 static dev_type_close(dkclose);
105 static dev_type_read(dkread);
106 static dev_type_write(dkwrite);
107 static dev_type_ioctl(dkioctl);
108 static dev_type_strategy(dkstrategy);
109 static dev_type_dump(dkdump);
110 static dev_type_size(dksize);
111 
112 const struct bdevsw dk_bdevsw = {
113 	dkopen, dkclose, dkstrategy, dkioctl, dkdump, dksize, D_DISK
114 };
115 
116 const struct cdevsw dk_cdevsw = {
117 	dkopen, dkclose, dkread, dkwrite, dkioctl,
118 	    nostop, notty, nopoll, nommap, nokqfilter, D_DISK
119 };
120 
121 static struct dkwedge_softc **dkwedges;
122 static u_int ndkwedges;
123 static krwlock_t dkwedges_lock;
124 
125 static LIST_HEAD(, dkwedge_discovery_method) dkwedge_discovery_methods;
126 static krwlock_t dkwedge_discovery_methods_lock;
127 
128 /*
129  * dkwedge_match:
130  *
131  *	Autoconfiguration match function for pseudo-device glue.
132  */
133 static int
134 dkwedge_match(device_t parent, cfdata_t match,
135     void *aux)
136 {
137 
138 	/* Pseudo-device; always present. */
139 	return (1);
140 }
141 
142 /*
143  * dkwedge_attach:
144  *
145  *	Autoconfiguration attach function for pseudo-device glue.
146  */
147 static void
148 dkwedge_attach(device_t parent, device_t self,
149     void *aux)
150 {
151 
152 	if (!pmf_device_register(self, NULL, NULL))
153 		aprint_error_dev(self, "couldn't establish power handler\n");
154 }
155 
156 CFDRIVER_DECL(dk, DV_DISK, NULL);
157 CFATTACH_DECL3_NEW(dk, 0,
158     dkwedge_match, dkwedge_attach, dkwedge_detach, NULL, NULL, NULL,
159     DVF_DETACH_SHUTDOWN);
160 
161 /*
162  * dkwedge_wait_drain:
163  *
164  *	Wait for I/O on the wedge to drain.
165  *	NOTE: Must be called at splbio()!
166  */
167 static void
168 dkwedge_wait_drain(struct dkwedge_softc *sc)
169 {
170 
171 	while (sc->sc_iopend != 0) {
172 		sc->sc_flags |= DK_F_WAIT_DRAIN;
173 		(void) tsleep(&sc->sc_iopend, PRIBIO, "dkdrn", 0);
174 	}
175 }
176 
177 /*
178  * dkwedge_compute_pdev:
179  *
180  *	Compute the parent disk's dev_t.
181  */
182 static int
183 dkwedge_compute_pdev(const char *pname, dev_t *pdevp)
184 {
185 	const char *name, *cp;
186 	devmajor_t pmaj;
187 	int punit;
188 	char devname[16];
189 
190 	name = pname;
191 	if ((pmaj = devsw_name2blk(name, devname, sizeof(devname))) == -1)
192 		return (ENODEV);
193 
194 	name += strlen(devname);
195 	for (cp = name, punit = 0; *cp >= '0' && *cp <= '9'; cp++)
196 		punit = (punit * 10) + (*cp - '0');
197 	if (cp == name) {
198 		/* Invalid parent disk name. */
199 		return (ENODEV);
200 	}
201 
202 	*pdevp = MAKEDISKDEV(pmaj, punit, RAW_PART);
203 
204 	return (0);
205 }
206 
207 /*
208  * dkwedge_array_expand:
209  *
210  *	Expand the dkwedges array.
211  */
212 static void
213 dkwedge_array_expand(void)
214 {
215 	int newcnt = ndkwedges + 16;
216 	struct dkwedge_softc **newarray, **oldarray;
217 
218 	newarray = malloc(newcnt * sizeof(*newarray), M_DKWEDGE,
219 	    M_WAITOK|M_ZERO);
220 	if ((oldarray = dkwedges) != NULL)
221 		memcpy(newarray, dkwedges, ndkwedges * sizeof(*newarray));
222 	dkwedges = newarray;
223 	ndkwedges = newcnt;
224 	if (oldarray != NULL)
225 		free(oldarray, M_DKWEDGE);
226 }
227 
228 static void
229 dkgetproperties(struct disk *disk, struct dkwedge_info *dkw)
230 {
231 	prop_dictionary_t disk_info, odisk_info, geom;
232 
233 	disk_info = prop_dictionary_create();
234 
235 	prop_dictionary_set_cstring_nocopy(disk_info, "type", "ESDI");
236 
237 	geom = prop_dictionary_create();
238 
239 	prop_dictionary_set_uint64(geom, "sectors-per-unit",
240 	    dkw->dkw_size >> disk->dk_blkshift);
241 
242 	prop_dictionary_set_uint32(geom, "sector-size",
243 	    DEV_BSIZE << disk->dk_blkshift);
244 
245 	prop_dictionary_set_uint32(geom, "sectors-per-track", 32);
246 
247 	prop_dictionary_set_uint32(geom, "tracks-per-cylinder", 64);
248 
249 	prop_dictionary_set_uint32(geom, "cylinders-per-unit", dkw->dkw_size / 2048);
250 
251 	prop_dictionary_set(disk_info, "geometry", geom);
252 	prop_object_release(geom);
253 
254 	odisk_info = disk->dk_info;
255 
256 	disk->dk_info = disk_info;
257 
258 	if (odisk_info != NULL)
259 		prop_object_release(odisk_info);
260 }
261 
262 /*
263  * dkwedge_add:		[exported function]
264  *
265  *	Add a disk wedge based on the provided information.
266  *
267  *	The incoming dkw_devname[] is ignored, instead being
268  *	filled in and returned to the caller.
269  */
270 int
271 dkwedge_add(struct dkwedge_info *dkw)
272 {
273 	struct dkwedge_softc *sc, *lsc;
274 	struct disk *pdk;
275 	u_int unit;
276 	int error;
277 	dev_t pdev;
278 
279 	dkw->dkw_parent[sizeof(dkw->dkw_parent) - 1] = '\0';
280 	pdk = disk_find(dkw->dkw_parent);
281 	if (pdk == NULL)
282 		return (ENODEV);
283 
284 	error = dkwedge_compute_pdev(pdk->dk_name, &pdev);
285 	if (error)
286 		return (error);
287 
288 	if (dkw->dkw_offset < 0)
289 		return (EINVAL);
290 
291 	sc = malloc(sizeof(*sc), M_DKWEDGE, M_WAITOK|M_ZERO);
292 	sc->sc_state = DKW_STATE_LARVAL;
293 	sc->sc_parent = pdk;
294 	sc->sc_pdev = pdev;
295 	sc->sc_offset = dkw->dkw_offset;
296 	sc->sc_size = dkw->dkw_size;
297 
298 	memcpy(sc->sc_wname, dkw->dkw_wname, sizeof(sc->sc_wname));
299 	sc->sc_wname[sizeof(sc->sc_wname) - 1] = '\0';
300 
301 	memcpy(sc->sc_ptype, dkw->dkw_ptype, sizeof(sc->sc_ptype));
302 	sc->sc_ptype[sizeof(sc->sc_ptype) - 1] = '\0';
303 
304 	bufq_alloc(&sc->sc_bufq, "fcfs", 0);
305 
306 	callout_init(&sc->sc_restart_ch, 0);
307 	callout_setfunc(&sc->sc_restart_ch, dkrestart, sc);
308 
309 	/*
310 	 * Wedge will be added; increment the wedge count for the parent.
311 	 * Only allow this to happend if RAW_PART is the only thing open.
312 	 */
313 	mutex_enter(&pdk->dk_openlock);
314 	if (pdk->dk_openmask & ~(1 << RAW_PART))
315 		error = EBUSY;
316 	else {
317 		/* Check for wedge overlap. */
318 		LIST_FOREACH(lsc, &pdk->dk_wedges, sc_plink) {
319 			daddr_t lastblk = sc->sc_offset + sc->sc_size - 1;
320 			daddr_t llastblk = lsc->sc_offset + lsc->sc_size - 1;
321 
322 			if (sc->sc_offset >= lsc->sc_offset &&
323 			    sc->sc_offset <= llastblk) {
324 				/* Overlaps the tail of the existing wedge. */
325 				break;
326 			}
327 			if (lastblk >= lsc->sc_offset &&
328 			    lastblk <= llastblk) {
329 				/* Overlaps the head of the existing wedge. */
330 			    	break;
331 			}
332 		}
333 		if (lsc != NULL)
334 			error = EINVAL;
335 		else {
336 			pdk->dk_nwedges++;
337 			LIST_INSERT_HEAD(&pdk->dk_wedges, sc, sc_plink);
338 		}
339 	}
340 	mutex_exit(&pdk->dk_openlock);
341 	if (error) {
342 		bufq_free(sc->sc_bufq);
343 		free(sc, M_DKWEDGE);
344 		return (error);
345 	}
346 
347 	/* Fill in our cfdata for the pseudo-device glue. */
348 	sc->sc_cfdata.cf_name = dk_cd.cd_name;
349 	sc->sc_cfdata.cf_atname = dk_ca.ca_name;
350 	/* sc->sc_cfdata.cf_unit set below */
351 	sc->sc_cfdata.cf_fstate = FSTATE_STAR;
352 
353 	/* Insert the larval wedge into the array. */
354 	rw_enter(&dkwedges_lock, RW_WRITER);
355 	for (error = 0;;) {
356 		struct dkwedge_softc **scpp;
357 
358 		/*
359 		 * Check for a duplicate wname while searching for
360 		 * a slot.
361 		 */
362 		for (scpp = NULL, unit = 0; unit < ndkwedges; unit++) {
363 			if (dkwedges[unit] == NULL) {
364 				if (scpp == NULL) {
365 					scpp = &dkwedges[unit];
366 					sc->sc_cfdata.cf_unit = unit;
367 				}
368 			} else {
369 				/* XXX Unicode. */
370 				if (strcmp(dkwedges[unit]->sc_wname,
371 					   sc->sc_wname) == 0) {
372 					error = EEXIST;
373 					break;
374 				}
375 			}
376 		}
377 		if (error)
378 			break;
379 		KASSERT(unit == ndkwedges);
380 		if (scpp == NULL)
381 			dkwedge_array_expand();
382 		else {
383 			KASSERT(scpp == &dkwedges[sc->sc_cfdata.cf_unit]);
384 			*scpp = sc;
385 			break;
386 		}
387 	}
388 	rw_exit(&dkwedges_lock);
389 	if (error) {
390 		mutex_enter(&pdk->dk_openlock);
391 		pdk->dk_nwedges--;
392 		LIST_REMOVE(sc, sc_plink);
393 		mutex_exit(&pdk->dk_openlock);
394 
395 		bufq_free(sc->sc_bufq);
396 		free(sc, M_DKWEDGE);
397 		return (error);
398 	}
399 
400 	/*
401 	 * Now that we know the unit #, attach a pseudo-device for
402 	 * this wedge instance.  This will provide us with the
403 	 * "struct device" necessary for glue to other parts of the
404 	 * system.
405 	 *
406 	 * This should never fail, unless we're almost totally out of
407 	 * memory.
408 	 */
409 	if ((sc->sc_dev = config_attach_pseudo(&sc->sc_cfdata)) == NULL) {
410 		aprint_error("%s%u: unable to attach pseudo-device\n",
411 		    sc->sc_cfdata.cf_name, sc->sc_cfdata.cf_unit);
412 
413 		rw_enter(&dkwedges_lock, RW_WRITER);
414 		dkwedges[sc->sc_cfdata.cf_unit] = NULL;
415 		rw_exit(&dkwedges_lock);
416 
417 		mutex_enter(&pdk->dk_openlock);
418 		pdk->dk_nwedges--;
419 		LIST_REMOVE(sc, sc_plink);
420 		mutex_exit(&pdk->dk_openlock);
421 
422 		bufq_free(sc->sc_bufq);
423 		free(sc, M_DKWEDGE);
424 		return (ENOMEM);
425 	}
426 
427 	/* Return the devname to the caller. */
428 	strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev),
429 		sizeof(dkw->dkw_devname));
430 
431 	/*
432 	 * XXX Really ought to make the disk_attach() and the changing
433 	 * of state to RUNNING atomic.
434 	 */
435 
436 	disk_init(&sc->sc_dk, device_xname(sc->sc_dev), NULL);
437 	disk_blocksize(&sc->sc_dk, DEV_BSIZE << pdk->dk_blkshift);
438 	dkgetproperties(&sc->sc_dk, dkw);
439 	disk_attach(&sc->sc_dk);
440 
441 	/* Disk wedge is ready for use! */
442 	sc->sc_state = DKW_STATE_RUNNING;
443 
444 	/* Announce our arrival. */
445 	aprint_normal("%s at %s: %s\n", device_xname(sc->sc_dev), pdk->dk_name,
446 	    sc->sc_wname);	/* XXX Unicode */
447 	aprint_normal("%s: %"PRIu64" blocks at %"PRId64", type: %s\n",
448 	    device_xname(sc->sc_dev), sc->sc_size, sc->sc_offset, sc->sc_ptype);
449 
450 	return (0);
451 }
452 
453 /*
454  * dkwedge_find:
455  *
456  *	Lookup a disk wedge based on the provided information.
457  *	NOTE: We look up the wedge based on the wedge devname,
458  *	not wname.
459  *
460  *	Return NULL if the wedge is not found, otherwise return
461  *	the wedge's softc.  Assign the wedge's unit number to unitp
462  *	if unitp is not NULL.
463  */
464 static struct dkwedge_softc *
465 dkwedge_find(struct dkwedge_info *dkw, u_int *unitp)
466 {
467 	struct dkwedge_softc *sc = NULL;
468 	u_int unit;
469 
470 	/* Find our softc. */
471 	dkw->dkw_devname[sizeof(dkw->dkw_devname) - 1] = '\0';
472 	rw_enter(&dkwedges_lock, RW_READER);
473 	for (unit = 0; unit < ndkwedges; unit++) {
474 		if ((sc = dkwedges[unit]) != NULL &&
475 		    strcmp(device_xname(sc->sc_dev), dkw->dkw_devname) == 0 &&
476 		    strcmp(sc->sc_parent->dk_name, dkw->dkw_parent) == 0) {
477 			break;
478 		}
479 	}
480 	rw_exit(&dkwedges_lock);
481 	if (unit == ndkwedges)
482 		return NULL;
483 
484 	if (unitp != NULL)
485 		*unitp = unit;
486 
487 	return sc;
488 }
489 
490 /*
491  * dkwedge_del:		[exported function]
492  *
493  *	Delete a disk wedge based on the provided information.
494  *	NOTE: We look up the wedge based on the wedge devname,
495  *	not wname.
496  */
497 int
498 dkwedge_del(struct dkwedge_info *dkw)
499 {
500 	struct dkwedge_softc *sc = NULL;
501 
502 	/* Find our softc. */
503 	if ((sc = dkwedge_find(dkw, NULL)) == NULL)
504 		return (ESRCH);
505 
506 	return config_detach(sc->sc_dev, DETACH_FORCE | DETACH_QUIET);
507 }
508 
509 static int
510 dkwedge_begindetach(struct dkwedge_softc *sc, int flags)
511 {
512 	struct disk *dk = &sc->sc_dk;
513 	int rc;
514 
515 	rc = 0;
516 	mutex_enter(&dk->dk_openlock);
517 	if (dk->dk_openmask == 0)
518 		;	/* nothing to do */
519 	else if ((flags & DETACH_FORCE) == 0)
520 		rc = EBUSY;
521 	else {
522 		mutex_enter(&sc->sc_parent->dk_rawlock);
523 		rc = dklastclose(sc); /* releases dk_rawlock */
524 	}
525 	mutex_exit(&dk->dk_openlock);
526 
527 	return rc;
528 }
529 
530 /*
531  * dkwedge_detach:
532  *
533  *	Autoconfiguration detach function for pseudo-device glue.
534  */
535 static int
536 dkwedge_detach(device_t self, int flags)
537 {
538 	struct dkwedge_softc *sc = NULL;
539 	u_int unit;
540 	int bmaj, cmaj, rc, s;
541 
542 	rw_enter(&dkwedges_lock, RW_WRITER);
543 	for (unit = 0; unit < ndkwedges; unit++) {
544 		if ((sc = dkwedges[unit]) != NULL && sc->sc_dev == self)
545 			break;
546 	}
547 	if (unit == ndkwedges)
548 		rc = ENXIO;
549 	else if ((rc = dkwedge_begindetach(sc, flags)) == 0) {
550 		/* Mark the wedge as dying. */
551 		sc->sc_state = DKW_STATE_DYING;
552 	}
553 	rw_exit(&dkwedges_lock);
554 
555 	if (rc != 0)
556 		return rc;
557 
558 	pmf_device_deregister(self);
559 
560 	/* Locate the wedge major numbers. */
561 	bmaj = bdevsw_lookup_major(&dk_bdevsw);
562 	cmaj = cdevsw_lookup_major(&dk_cdevsw);
563 
564 	/* Kill any pending restart. */
565 	callout_stop(&sc->sc_restart_ch);
566 
567 	/*
568 	 * dkstart() will kill any queued buffers now that the
569 	 * state of the wedge is not RUNNING.  Once we've done
570 	 * that, wait for any other pending I/O to complete.
571 	 */
572 	s = splbio();
573 	dkstart(sc);
574 	dkwedge_wait_drain(sc);
575 	splx(s);
576 
577 	/* Nuke the vnodes for any open instances. */
578 	vdevgone(bmaj, unit, unit, VBLK);
579 	vdevgone(cmaj, unit, unit, VCHR);
580 
581 	/* Clean up the parent. */
582 	mutex_enter(&sc->sc_dk.dk_openlock);
583 	if (sc->sc_dk.dk_openmask) {
584 		mutex_enter(&sc->sc_parent->dk_rawlock);
585 		if (sc->sc_parent->dk_rawopens-- == 1) {
586 			KASSERT(sc->sc_parent->dk_rawvp != NULL);
587 			mutex_exit(&sc->sc_parent->dk_rawlock);
588 			(void) vn_close(sc->sc_parent->dk_rawvp, FREAD | FWRITE,
589 			    NOCRED);
590 			sc->sc_parent->dk_rawvp = NULL;
591 		} else
592 			mutex_exit(&sc->sc_parent->dk_rawlock);
593 		sc->sc_dk.dk_openmask = 0;
594 	}
595 	mutex_exit(&sc->sc_dk.dk_openlock);
596 
597 	/* Announce our departure. */
598 	aprint_normal("%s at %s (%s) deleted\n", device_xname(sc->sc_dev),
599 	    sc->sc_parent->dk_name,
600 	    sc->sc_wname);	/* XXX Unicode */
601 
602 	mutex_enter(&sc->sc_parent->dk_openlock);
603 	sc->sc_parent->dk_nwedges--;
604 	LIST_REMOVE(sc, sc_plink);
605 	mutex_exit(&sc->sc_parent->dk_openlock);
606 
607 	/* Delete our buffer queue. */
608 	bufq_free(sc->sc_bufq);
609 
610 	/* Detach from the disk list. */
611 	disk_detach(&sc->sc_dk);
612 	disk_destroy(&sc->sc_dk);
613 
614 	/* Poof. */
615 	rw_enter(&dkwedges_lock, RW_WRITER);
616 	dkwedges[unit] = NULL;
617 	sc->sc_state = DKW_STATE_DEAD;
618 	rw_exit(&dkwedges_lock);
619 
620 	free(sc, M_DKWEDGE);
621 
622 	return 0;
623 }
624 
625 /*
626  * dkwedge_delall:	[exported function]
627  *
628  *	Delete all of the wedges on the specified disk.  Used when
629  *	a disk is being detached.
630  */
631 void
632 dkwedge_delall(struct disk *pdk)
633 {
634 	struct dkwedge_info dkw;
635 	struct dkwedge_softc *sc;
636 
637 	for (;;) {
638 		mutex_enter(&pdk->dk_openlock);
639 		if ((sc = LIST_FIRST(&pdk->dk_wedges)) == NULL) {
640 			KASSERT(pdk->dk_nwedges == 0);
641 			mutex_exit(&pdk->dk_openlock);
642 			return;
643 		}
644 		strcpy(dkw.dkw_parent, pdk->dk_name);
645 		strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev),
646 			sizeof(dkw.dkw_devname));
647 		mutex_exit(&pdk->dk_openlock);
648 		(void) dkwedge_del(&dkw);
649 	}
650 }
651 
652 /*
653  * dkwedge_list:	[exported function]
654  *
655  *	List all of the wedges on a particular disk.
656  *	If p == NULL, the buffer is in kernel space.  Otherwise, it is
657  *	in user space of the specified process.
658  */
659 int
660 dkwedge_list(struct disk *pdk, struct dkwedge_list *dkwl, struct lwp *l)
661 {
662 	struct uio uio;
663 	struct iovec iov;
664 	struct dkwedge_softc *sc;
665 	struct dkwedge_info dkw;
666 	int error = 0;
667 
668 	iov.iov_base = dkwl->dkwl_buf;
669 	iov.iov_len = dkwl->dkwl_bufsize;
670 
671 	uio.uio_iov = &iov;
672 	uio.uio_iovcnt = 1;
673 	uio.uio_offset = 0;
674 	uio.uio_resid = dkwl->dkwl_bufsize;
675 	uio.uio_rw = UIO_READ;
676 	KASSERT(l == curlwp);
677 	uio.uio_vmspace = l->l_proc->p_vmspace;
678 
679 	dkwl->dkwl_ncopied = 0;
680 
681 	mutex_enter(&pdk->dk_openlock);
682 	LIST_FOREACH(sc, &pdk->dk_wedges, sc_plink) {
683 		if (uio.uio_resid < sizeof(dkw))
684 			break;
685 
686 		if (sc->sc_state != DKW_STATE_RUNNING)
687 			continue;
688 
689 		strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev),
690 			sizeof(dkw.dkw_devname));
691 		memcpy(dkw.dkw_wname, sc->sc_wname, sizeof(dkw.dkw_wname));
692 		dkw.dkw_wname[sizeof(dkw.dkw_wname) - 1] = '\0';
693 		strcpy(dkw.dkw_parent, sc->sc_parent->dk_name);
694 		dkw.dkw_offset = sc->sc_offset;
695 		dkw.dkw_size = sc->sc_size;
696 		strcpy(dkw.dkw_ptype, sc->sc_ptype);
697 
698 		error = uiomove(&dkw, sizeof(dkw), &uio);
699 		if (error)
700 			break;
701 		dkwl->dkwl_ncopied++;
702 	}
703 	dkwl->dkwl_nwedges = pdk->dk_nwedges;
704 	mutex_exit(&pdk->dk_openlock);
705 
706 	return (error);
707 }
708 
709 device_t
710 dkwedge_find_by_wname(const char *wname)
711 {
712 	device_t dv = NULL;
713 	struct dkwedge_softc *sc;
714 	int i;
715 
716 	rw_enter(&dkwedges_lock, RW_WRITER);
717 	for (i = 0; i < ndkwedges; i++) {
718 		if ((sc = dkwedges[i]) == NULL)
719 			continue;
720 		if (strcmp(sc->sc_wname, wname) == 0) {
721 			if (dv != NULL) {
722 				printf(
723 				    "WARNING: double match for wedge name %s "
724 				    "(%s, %s)\n", wname, device_xname(dv),
725 				    device_xname(sc->sc_dev));
726 				continue;
727 			}
728 			dv = sc->sc_dev;
729 		}
730 	}
731 	rw_exit(&dkwedges_lock);
732 	return dv;
733 }
734 
735 void
736 dkwedge_print_wnames(void)
737 {
738 	struct dkwedge_softc *sc;
739 	int i;
740 
741 	rw_enter(&dkwedges_lock, RW_WRITER);
742 	for (i = 0; i < ndkwedges; i++) {
743 		if ((sc = dkwedges[i]) == NULL)
744 			continue;
745 		printf(" wedge:%s", sc->sc_wname);
746 	}
747 	rw_exit(&dkwedges_lock);
748 }
749 
750 /*
751  * We need a dummy object to stuff into the dkwedge discovery method link
752  * set to ensure that there is always at least one object in the set.
753  */
754 static struct dkwedge_discovery_method dummy_discovery_method;
755 __link_set_add_bss(dkwedge_methods, dummy_discovery_method);
756 
757 /*
758  * dkwedge_init:
759  *
760  *	Initialize the disk wedge subsystem.
761  */
762 void
763 dkwedge_init(void)
764 {
765 	__link_set_decl(dkwedge_methods, struct dkwedge_discovery_method);
766 	struct dkwedge_discovery_method * const *ddmp;
767 	struct dkwedge_discovery_method *lddm, *ddm;
768 
769 	rw_init(&dkwedges_lock);
770 	rw_init(&dkwedge_discovery_methods_lock);
771 
772 	if (config_cfdriver_attach(&dk_cd) != 0)
773 		panic("dkwedge: unable to attach cfdriver");
774 	if (config_cfattach_attach(dk_cd.cd_name, &dk_ca) != 0)
775 		panic("dkwedge: unable to attach cfattach");
776 
777 	rw_enter(&dkwedge_discovery_methods_lock, RW_WRITER);
778 
779 	LIST_INIT(&dkwedge_discovery_methods);
780 
781 	__link_set_foreach(ddmp, dkwedge_methods) {
782 		ddm = *ddmp;
783 		if (ddm == &dummy_discovery_method)
784 			continue;
785 		if (LIST_EMPTY(&dkwedge_discovery_methods)) {
786 			LIST_INSERT_HEAD(&dkwedge_discovery_methods,
787 					 ddm, ddm_list);
788 			continue;
789 		}
790 		LIST_FOREACH(lddm, &dkwedge_discovery_methods, ddm_list) {
791 			if (ddm->ddm_priority == lddm->ddm_priority) {
792 				aprint_error("dk-method-%s: method \"%s\" "
793 				    "already exists at priority %d\n",
794 				    ddm->ddm_name, lddm->ddm_name,
795 				    lddm->ddm_priority);
796 				/* Not inserted. */
797 				break;
798 			}
799 			if (ddm->ddm_priority < lddm->ddm_priority) {
800 				/* Higher priority; insert before. */
801 				LIST_INSERT_BEFORE(lddm, ddm, ddm_list);
802 				break;
803 			}
804 			if (LIST_NEXT(lddm, ddm_list) == NULL) {
805 				/* Last one; insert after. */
806 				KASSERT(lddm->ddm_priority < ddm->ddm_priority);
807 				LIST_INSERT_AFTER(lddm, ddm, ddm_list);
808 				break;
809 			}
810 		}
811 	}
812 
813 	rw_exit(&dkwedge_discovery_methods_lock);
814 }
815 
816 #ifdef DKWEDGE_AUTODISCOVER
817 int	dkwedge_autodiscover = 1;
818 #else
819 int	dkwedge_autodiscover = 0;
820 #endif
821 
822 /*
823  * dkwedge_discover:	[exported function]
824  *
825  *	Discover the wedges on a newly attached disk.
826  */
827 void
828 dkwedge_discover(struct disk *pdk)
829 {
830 	struct dkwedge_discovery_method *ddm;
831 	struct vnode *vp;
832 	int error;
833 	dev_t pdev;
834 
835 	/*
836 	 * Require people playing with wedges to enable this explicitly.
837 	 */
838 	if (dkwedge_autodiscover == 0)
839 		return;
840 
841 	rw_enter(&dkwedge_discovery_methods_lock, RW_READER);
842 
843 	error = dkwedge_compute_pdev(pdk->dk_name, &pdev);
844 	if (error) {
845 		aprint_error("%s: unable to compute pdev, error = %d\n",
846 		    pdk->dk_name, error);
847 		goto out;
848 	}
849 
850 	error = bdevvp(pdev, &vp);
851 	if (error) {
852 		aprint_error("%s: unable to find vnode for pdev, error = %d\n",
853 		    pdk->dk_name, error);
854 		goto out;
855 	}
856 
857 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
858 	if (error) {
859 		aprint_error("%s: unable to lock vnode for pdev, error = %d\n",
860 		    pdk->dk_name, error);
861 		vrele(vp);
862 		goto out;
863 	}
864 
865 	error = VOP_OPEN(vp, FREAD | FSILENT, NOCRED);
866 	if (error) {
867 		aprint_error("%s: unable to open device, error = %d\n",
868 		    pdk->dk_name, error);
869 		vput(vp);
870 		goto out;
871 	}
872 	VOP_UNLOCK(vp);
873 
874 	/*
875 	 * For each supported partition map type, look to see if
876 	 * this map type exists.  If so, parse it and add the
877 	 * corresponding wedges.
878 	 */
879 	LIST_FOREACH(ddm, &dkwedge_discovery_methods, ddm_list) {
880 		error = (*ddm->ddm_discover)(pdk, vp);
881 		if (error == 0) {
882 			/* Successfully created wedges; we're done. */
883 			break;
884 		}
885 	}
886 
887 	error = vn_close(vp, FREAD, NOCRED);
888 	if (error) {
889 		aprint_error("%s: unable to close device, error = %d\n",
890 		    pdk->dk_name, error);
891 		/* We'll just assume the vnode has been cleaned up. */
892 	}
893  out:
894 	rw_exit(&dkwedge_discovery_methods_lock);
895 }
896 
897 /*
898  * dkwedge_read:
899  *
900  *	Read some data from the specified disk, used for
901  *	partition discovery.
902  */
903 int
904 dkwedge_read(struct disk *pdk, struct vnode *vp, daddr_t blkno,
905     void *tbuf, size_t len)
906 {
907 	struct buf *bp;
908 	int result;
909 
910 	bp = getiobuf(vp, true);
911 
912 	bp->b_dev = vp->v_rdev;
913 	bp->b_blkno = blkno;
914 	bp->b_bcount = len;
915 	bp->b_resid = len;
916 	bp->b_flags = B_READ;
917 	bp->b_data = tbuf;
918 	SET(bp->b_cflags, BC_BUSY);	/* mark buffer busy */
919 
920 	VOP_STRATEGY(vp, bp);
921 	result = biowait(bp);
922 	putiobuf(bp);
923 
924 	return result;
925 }
926 
927 /*
928  * dkwedge_lookup:
929  *
930  *	Look up a dkwedge_softc based on the provided dev_t.
931  */
932 static struct dkwedge_softc *
933 dkwedge_lookup(dev_t dev)
934 {
935 	int unit = minor(dev);
936 
937 	if (unit >= ndkwedges)
938 		return (NULL);
939 
940 	KASSERT(dkwedges != NULL);
941 
942 	return (dkwedges[unit]);
943 }
944 
945 /*
946  * dkopen:		[devsw entry point]
947  *
948  *	Open a wedge.
949  */
950 static int
951 dkopen(dev_t dev, int flags, int fmt, struct lwp *l)
952 {
953 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
954 	struct vnode *vp;
955 	int error = 0;
956 
957 	if (sc == NULL)
958 		return (ENODEV);
959 	if (sc->sc_state != DKW_STATE_RUNNING)
960 		return (ENXIO);
961 
962 	/*
963 	 * We go through a complicated little dance to only open the parent
964 	 * vnode once per wedge, no matter how many times the wedge is
965 	 * opened.  The reason?  We see one dkopen() per open call, but
966 	 * only dkclose() on the last close.
967 	 */
968 	mutex_enter(&sc->sc_dk.dk_openlock);
969 	mutex_enter(&sc->sc_parent->dk_rawlock);
970 	if (sc->sc_dk.dk_openmask == 0) {
971 		if (sc->sc_parent->dk_rawopens == 0) {
972 			KASSERT(sc->sc_parent->dk_rawvp == NULL);
973 			error = bdevvp(sc->sc_pdev, &vp);
974 			if (error)
975 				goto popen_fail;
976 			error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
977 			if (error) {
978 				vrele(vp);
979 				goto popen_fail;
980 			}
981 			error = VOP_OPEN(vp, FREAD | FWRITE, NOCRED);
982 			if (error) {
983 				vput(vp);
984 				goto popen_fail;
985 			}
986 			/* VOP_OPEN() doesn't do this for us. */
987 			mutex_enter(vp->v_interlock);
988 			vp->v_writecount++;
989 			mutex_exit(vp->v_interlock);
990 			VOP_UNLOCK(vp);
991 			sc->sc_parent->dk_rawvp = vp;
992 		}
993 		sc->sc_parent->dk_rawopens++;
994 	}
995 	if (fmt == S_IFCHR)
996 		sc->sc_dk.dk_copenmask |= 1;
997 	else
998 		sc->sc_dk.dk_bopenmask |= 1;
999 	sc->sc_dk.dk_openmask =
1000 	    sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
1001 
1002  popen_fail:
1003 	mutex_exit(&sc->sc_parent->dk_rawlock);
1004 	mutex_exit(&sc->sc_dk.dk_openlock);
1005 	return (error);
1006 }
1007 
1008 /*
1009  * Caller must hold sc->sc_dk.dk_openlock and sc->sc_parent->dk_rawlock.
1010  */
1011 static int
1012 dklastclose(struct dkwedge_softc *sc)
1013 {
1014 	int error = 0;
1015 
1016 	if (sc->sc_parent->dk_rawopens-- == 1) {
1017 		KASSERT(sc->sc_parent->dk_rawvp != NULL);
1018 		mutex_exit(&sc->sc_parent->dk_rawlock);
1019 		error = vn_close(sc->sc_parent->dk_rawvp,
1020 		    FREAD | FWRITE, NOCRED);
1021 		sc->sc_parent->dk_rawvp = NULL;
1022 	} else
1023 		mutex_exit(&sc->sc_parent->dk_rawlock);
1024 	return error;
1025 }
1026 
1027 /*
1028  * dkclose:		[devsw entry point]
1029  *
1030  *	Close a wedge.
1031  */
1032 static int
1033 dkclose(dev_t dev, int flags, int fmt, struct lwp *l)
1034 {
1035 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
1036 	int error = 0;
1037 
1038 	if (sc == NULL)
1039 		return (ENODEV);
1040 	if (sc->sc_state != DKW_STATE_RUNNING)
1041 		return (ENXIO);
1042 
1043 	KASSERT(sc->sc_dk.dk_openmask != 0);
1044 
1045 	mutex_enter(&sc->sc_dk.dk_openlock);
1046 	mutex_enter(&sc->sc_parent->dk_rawlock);
1047 
1048 	if (fmt == S_IFCHR)
1049 		sc->sc_dk.dk_copenmask &= ~1;
1050 	else
1051 		sc->sc_dk.dk_bopenmask &= ~1;
1052 	sc->sc_dk.dk_openmask =
1053 	    sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
1054 
1055 	if (sc->sc_dk.dk_openmask == 0)
1056 		error = dklastclose(sc); /* releases dk_rawlock */
1057 	else
1058 		mutex_exit(&sc->sc_parent->dk_rawlock);
1059 
1060 	mutex_exit(&sc->sc_dk.dk_openlock);
1061 
1062 	return (error);
1063 }
1064 
1065 /*
1066  * dkstragegy:		[devsw entry point]
1067  *
1068  *	Perform I/O based on the wedge I/O strategy.
1069  */
1070 static void
1071 dkstrategy(struct buf *bp)
1072 {
1073 	struct dkwedge_softc *sc = dkwedge_lookup(bp->b_dev);
1074 	uint64_t p_size, p_offset;
1075 	int s;
1076 
1077 	if (sc == NULL) {
1078 		bp->b_error = ENODEV;
1079 		goto done;
1080 	}
1081 
1082 	if (sc->sc_state != DKW_STATE_RUNNING ||
1083 	    sc->sc_parent->dk_rawvp == NULL) {
1084 		bp->b_error = ENXIO;
1085 		goto done;
1086 	}
1087 
1088 	/* If it's an empty transfer, wake up the top half now. */
1089 	if (bp->b_bcount == 0)
1090 		goto done;
1091 
1092 	p_offset = sc->sc_offset << sc->sc_parent->dk_blkshift;
1093 	p_size   = sc->sc_size << sc->sc_parent->dk_blkshift;
1094 
1095 	/* Make sure it's in-range. */
1096 	if (bounds_check_with_mediasize(bp, DEV_BSIZE, p_size) <= 0)
1097 		goto done;
1098 
1099 	/* Translate it to the parent's raw LBA. */
1100 	bp->b_rawblkno = bp->b_blkno + p_offset;
1101 
1102 	/* Place it in the queue and start I/O on the unit. */
1103 	s = splbio();
1104 	sc->sc_iopend++;
1105 	bufq_put(sc->sc_bufq, bp);
1106 	dkstart(sc);
1107 	splx(s);
1108 	return;
1109 
1110  done:
1111 	bp->b_resid = bp->b_bcount;
1112 	biodone(bp);
1113 }
1114 
1115 /*
1116  * dkstart:
1117  *
1118  *	Start I/O that has been enqueued on the wedge.
1119  *	NOTE: Must be called at splbio()!
1120  */
1121 static void
1122 dkstart(struct dkwedge_softc *sc)
1123 {
1124 	struct vnode *vp;
1125 	struct buf *bp, *nbp;
1126 
1127 	/* Do as much work as has been enqueued. */
1128 	while ((bp = bufq_peek(sc->sc_bufq)) != NULL) {
1129 		if (sc->sc_state != DKW_STATE_RUNNING) {
1130 			(void) bufq_get(sc->sc_bufq);
1131 			if (sc->sc_iopend-- == 1 &&
1132 			    (sc->sc_flags & DK_F_WAIT_DRAIN) != 0) {
1133 				sc->sc_flags &= ~DK_F_WAIT_DRAIN;
1134 				wakeup(&sc->sc_iopend);
1135 			}
1136 			bp->b_error = ENXIO;
1137 			bp->b_resid = bp->b_bcount;
1138 			biodone(bp);
1139 		}
1140 
1141 		/* Instrumentation. */
1142 		disk_busy(&sc->sc_dk);
1143 
1144 		nbp = getiobuf(sc->sc_parent->dk_rawvp, false);
1145 		if (nbp == NULL) {
1146 			/*
1147 			 * No resources to run this request; leave the
1148 			 * buffer queued up, and schedule a timer to
1149 			 * restart the queue in 1/2 a second.
1150 			 */
1151 			disk_unbusy(&sc->sc_dk, 0, bp->b_flags & B_READ);
1152 			callout_schedule(&sc->sc_restart_ch, hz / 2);
1153 			return;
1154 		}
1155 
1156 		(void) bufq_get(sc->sc_bufq);
1157 
1158 		nbp->b_data = bp->b_data;
1159 		nbp->b_flags = bp->b_flags;
1160 		nbp->b_oflags = bp->b_oflags;
1161 		nbp->b_cflags = bp->b_cflags;
1162 		nbp->b_iodone = dkiodone;
1163 		nbp->b_proc = bp->b_proc;
1164 		nbp->b_blkno = bp->b_rawblkno;
1165 		nbp->b_dev = sc->sc_parent->dk_rawvp->v_rdev;
1166 		nbp->b_bcount = bp->b_bcount;
1167 		nbp->b_private = bp;
1168 		BIO_COPYPRIO(nbp, bp);
1169 
1170 		vp = nbp->b_vp;
1171 		if ((nbp->b_flags & B_READ) == 0) {
1172 			mutex_enter(vp->v_interlock);
1173 			vp->v_numoutput++;
1174 			mutex_exit(vp->v_interlock);
1175 		}
1176 		VOP_STRATEGY(vp, nbp);
1177 	}
1178 }
1179 
1180 /*
1181  * dkiodone:
1182  *
1183  *	I/O to a wedge has completed; alert the top half.
1184  */
1185 static void
1186 dkiodone(struct buf *bp)
1187 {
1188 	struct buf *obp = bp->b_private;
1189 	struct dkwedge_softc *sc = dkwedge_lookup(obp->b_dev);
1190 
1191 	int s = splbio();
1192 
1193 	if (bp->b_error != 0)
1194 		obp->b_error = bp->b_error;
1195 	obp->b_resid = bp->b_resid;
1196 	putiobuf(bp);
1197 
1198 	if (sc->sc_iopend-- == 1 && (sc->sc_flags & DK_F_WAIT_DRAIN) != 0) {
1199 		sc->sc_flags &= ~DK_F_WAIT_DRAIN;
1200 		wakeup(&sc->sc_iopend);
1201 	}
1202 
1203 	disk_unbusy(&sc->sc_dk, obp->b_bcount - obp->b_resid,
1204 	    obp->b_flags & B_READ);
1205 
1206 	biodone(obp);
1207 
1208 	/* Kick the queue in case there is more work we can do. */
1209 	dkstart(sc);
1210 	splx(s);
1211 }
1212 
1213 /*
1214  * dkrestart:
1215  *
1216  *	Restart the work queue after it was stalled due to
1217  *	a resource shortage.  Invoked via a callout.
1218  */
1219 static void
1220 dkrestart(void *v)
1221 {
1222 	struct dkwedge_softc *sc = v;
1223 	int s;
1224 
1225 	s = splbio();
1226 	dkstart(sc);
1227 	splx(s);
1228 }
1229 
1230 /*
1231  * dkminphys:
1232  *
1233  *	Call parent's minphys function.
1234  */
1235 static void
1236 dkminphys(struct buf *bp)
1237 {
1238 	struct dkwedge_softc *sc = dkwedge_lookup(bp->b_dev);
1239 	dev_t dev;
1240 
1241 	dev = bp->b_dev;
1242 	bp->b_dev = sc->sc_pdev;
1243 	(*sc->sc_parent->dk_driver->d_minphys)(bp);
1244 	bp->b_dev = dev;
1245 }
1246 
1247 /*
1248  * dkread:		[devsw entry point]
1249  *
1250  *	Read from a wedge.
1251  */
1252 static int
1253 dkread(dev_t dev, struct uio *uio, int flags)
1254 {
1255 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
1256 
1257 	if (sc == NULL)
1258 		return (ENODEV);
1259 	if (sc->sc_state != DKW_STATE_RUNNING)
1260 		return (ENXIO);
1261 
1262 	return (physio(dkstrategy, NULL, dev, B_READ, dkminphys, uio));
1263 }
1264 
1265 /*
1266  * dkwrite:		[devsw entry point]
1267  *
1268  *	Write to a wedge.
1269  */
1270 static int
1271 dkwrite(dev_t dev, struct uio *uio, int flags)
1272 {
1273 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
1274 
1275 	if (sc == NULL)
1276 		return (ENODEV);
1277 	if (sc->sc_state != DKW_STATE_RUNNING)
1278 		return (ENXIO);
1279 
1280 	return (physio(dkstrategy, NULL, dev, B_WRITE, dkminphys, uio));
1281 }
1282 
1283 /*
1284  * dkioctl:		[devsw entry point]
1285  *
1286  *	Perform an ioctl request on a wedge.
1287  */
1288 static int
1289 dkioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
1290 {
1291 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
1292 	int error = 0;
1293 
1294 	if (sc == NULL)
1295 		return (ENODEV);
1296 	if (sc->sc_state != DKW_STATE_RUNNING)
1297 		return (ENXIO);
1298 	if (sc->sc_parent->dk_rawvp == NULL)
1299 		return (ENXIO);
1300 
1301 	error = disk_ioctl(&sc->sc_dk, cmd, data, flag, l);
1302 	if (error != EPASSTHROUGH)
1303 		return (error);
1304 
1305 	error = 0;
1306 
1307 	switch (cmd) {
1308 	case DIOCCACHESYNC:
1309 		/*
1310 		 * XXX Do we really need to care about having a writable
1311 		 * file descriptor here?
1312 		 */
1313 		if ((flag & FWRITE) == 0)
1314 			error = EBADF;
1315 		else
1316 			error = VOP_IOCTL(sc->sc_parent->dk_rawvp,
1317 					  cmd, data, flag,
1318 					  l != NULL ? l->l_cred : NOCRED);
1319 		break;
1320 	case DIOCGWEDGEINFO:
1321 	    {
1322 		struct dkwedge_info *dkw = (void *) data;
1323 
1324 		strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev),
1325 			sizeof(dkw->dkw_devname));
1326 	    	memcpy(dkw->dkw_wname, sc->sc_wname, sizeof(dkw->dkw_wname));
1327 		dkw->dkw_wname[sizeof(dkw->dkw_wname) - 1] = '\0';
1328 		strcpy(dkw->dkw_parent, sc->sc_parent->dk_name);
1329 		dkw->dkw_offset = sc->sc_offset;
1330 		dkw->dkw_size = sc->sc_size;
1331 		strcpy(dkw->dkw_ptype, sc->sc_ptype);
1332 
1333 		break;
1334 	    }
1335 
1336 	default:
1337 		error = ENOTTY;
1338 	}
1339 
1340 	return (error);
1341 }
1342 
1343 /*
1344  * dksize:		[devsw entry point]
1345  *
1346  *	Query the size of a wedge for the purpose of performing a dump
1347  *	or for swapping to.
1348  */
1349 static int
1350 dksize(dev_t dev)
1351 {
1352 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
1353 	int rv = -1;
1354 
1355 	if (sc == NULL)
1356 		return (-1);
1357 	if (sc->sc_state != DKW_STATE_RUNNING)
1358 		return (-1);
1359 
1360 	mutex_enter(&sc->sc_dk.dk_openlock);
1361 	mutex_enter(&sc->sc_parent->dk_rawlock);
1362 
1363 	/* Our content type is static, no need to open the device. */
1364 
1365 	if (strcmp(sc->sc_ptype, DKW_PTYPE_SWAP) == 0) {
1366 		/* Saturate if we are larger than INT_MAX. */
1367 		if (sc->sc_size > INT_MAX)
1368 			rv = INT_MAX;
1369 		else
1370 			rv = (int) sc->sc_size;
1371 	}
1372 
1373 	mutex_exit(&sc->sc_parent->dk_rawlock);
1374 	mutex_exit(&sc->sc_dk.dk_openlock);
1375 
1376 	return (rv);
1377 }
1378 
1379 /*
1380  * dkdump:		[devsw entry point]
1381  *
1382  *	Perform a crash dump to a wedge.
1383  */
1384 static int
1385 dkdump(dev_t dev, daddr_t blkno, void *va, size_t size)
1386 {
1387 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
1388 	const struct bdevsw *bdev;
1389 	int rv = 0;
1390 
1391 	if (sc == NULL)
1392 		return (ENODEV);
1393 	if (sc->sc_state != DKW_STATE_RUNNING)
1394 		return (ENXIO);
1395 
1396 	mutex_enter(&sc->sc_dk.dk_openlock);
1397 	mutex_enter(&sc->sc_parent->dk_rawlock);
1398 
1399 	/* Our content type is static, no need to open the device. */
1400 
1401 	if (strcmp(sc->sc_ptype, DKW_PTYPE_SWAP) != 0) {
1402 		rv = ENXIO;
1403 		goto out;
1404 	}
1405 	if (size % DEV_BSIZE != 0) {
1406 		rv = EINVAL;
1407 		goto out;
1408 	}
1409 	if (blkno + size / DEV_BSIZE > sc->sc_size) {
1410 		printf("%s: blkno (%" PRIu64 ") + size / DEV_BSIZE (%zu) > "
1411 		    "sc->sc_size (%" PRIu64 ")\n", __func__, blkno,
1412 		    size / DEV_BSIZE, sc->sc_size);
1413 		rv = EINVAL;
1414 		goto out;
1415 	}
1416 
1417 	bdev = bdevsw_lookup(sc->sc_pdev);
1418 	rv = (*bdev->d_dump)(sc->sc_pdev, blkno + sc->sc_offset, va, size);
1419 
1420 out:
1421 	mutex_exit(&sc->sc_parent->dk_rawlock);
1422 	mutex_exit(&sc->sc_dk.dk_openlock);
1423 
1424 	return rv;
1425 }
1426 
1427 /*
1428  * config glue
1429  */
1430 
1431 /*
1432  * dkwedge_find_partition
1433  *
1434  *	Find wedge corresponding to the specified parent name
1435  *	and offset/length.
1436  */
1437 device_t
1438 dkwedge_find_partition(device_t parent, daddr_t startblk, uint64_t nblks)
1439 {
1440 	struct dkwedge_softc *sc;
1441 	int i;
1442 	device_t wedge = NULL;
1443 
1444 	rw_enter(&dkwedges_lock, RW_READER);
1445 	for (i = 0; i < ndkwedges; i++) {
1446 		if ((sc = dkwedges[i]) == NULL)
1447 			continue;
1448 		if (strcmp(sc->sc_parent->dk_name, device_xname(parent)) == 0 &&
1449 		    sc->sc_offset == startblk &&
1450 		    sc->sc_size == nblks) {
1451 			if (wedge) {
1452 				printf("WARNING: double match for boot wedge "
1453 				    "(%s, %s)\n",
1454 				    device_xname(wedge),
1455 				    device_xname(sc->sc_dev));
1456 				continue;
1457 			}
1458 			wedge = sc->sc_dev;
1459 		}
1460 	}
1461 	rw_exit(&dkwedges_lock);
1462 
1463 	return wedge;
1464 }
1465 
1466