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