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