dk.c revision 1.71 1 /* $NetBSD: dk.c,v 1.71 2014/07/25 08:10:36 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.71 2014/07/25 08:10:36 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_discard = nodiscard,
120 .d_flag = D_DISK
121 };
122
123 const struct cdevsw dk_cdevsw = {
124 .d_open = dkopen,
125 .d_close = dkclose,
126 .d_read = dkread,
127 .d_write = dkwrite,
128 .d_ioctl = dkioctl,
129 .d_stop = nostop,
130 .d_tty = notty,
131 .d_poll = nopoll,
132 .d_mmap = nommap,
133 .d_kqfilter = nokqfilter,
134 .d_discard = nodiscard,
135 .d_flag = D_DISK
136 };
137
138 static struct dkwedge_softc **dkwedges;
139 static u_int ndkwedges;
140 static krwlock_t dkwedges_lock;
141
142 static LIST_HEAD(, dkwedge_discovery_method) dkwedge_discovery_methods;
143 static krwlock_t dkwedge_discovery_methods_lock;
144
145 /*
146 * dkwedge_match:
147 *
148 * Autoconfiguration match function for pseudo-device glue.
149 */
150 static int
151 dkwedge_match(device_t parent, cfdata_t match,
152 void *aux)
153 {
154
155 /* Pseudo-device; always present. */
156 return (1);
157 }
158
159 /*
160 * dkwedge_attach:
161 *
162 * Autoconfiguration attach function for pseudo-device glue.
163 */
164 static void
165 dkwedge_attach(device_t parent, device_t self,
166 void *aux)
167 {
168
169 if (!pmf_device_register(self, NULL, NULL))
170 aprint_error_dev(self, "couldn't establish power handler\n");
171 }
172
173 CFDRIVER_DECL(dk, DV_DISK, NULL);
174 CFATTACH_DECL3_NEW(dk, 0,
175 dkwedge_match, dkwedge_attach, dkwedge_detach, NULL, NULL, NULL,
176 DVF_DETACH_SHUTDOWN);
177
178 /*
179 * dkwedge_wait_drain:
180 *
181 * Wait for I/O on the wedge to drain.
182 * NOTE: Must be called at splbio()!
183 */
184 static void
185 dkwedge_wait_drain(struct dkwedge_softc *sc)
186 {
187
188 while (sc->sc_iopend != 0) {
189 sc->sc_flags |= DK_F_WAIT_DRAIN;
190 (void) tsleep(&sc->sc_iopend, PRIBIO, "dkdrn", 0);
191 }
192 }
193
194 /*
195 * dkwedge_compute_pdev:
196 *
197 * Compute the parent disk's dev_t.
198 */
199 static int
200 dkwedge_compute_pdev(const char *pname, dev_t *pdevp)
201 {
202 const char *name, *cp;
203 devmajor_t pmaj;
204 int punit;
205 char devname[16];
206
207 name = pname;
208 if ((pmaj = devsw_name2blk(name, devname, sizeof(devname))) == -1)
209 return (ENODEV);
210
211 name += strlen(devname);
212 for (cp = name, punit = 0; *cp >= '0' && *cp <= '9'; cp++)
213 punit = (punit * 10) + (*cp - '0');
214 if (cp == name) {
215 /* Invalid parent disk name. */
216 return (ENODEV);
217 }
218
219 *pdevp = MAKEDISKDEV(pmaj, punit, RAW_PART);
220
221 return (0);
222 }
223
224 /*
225 * dkwedge_array_expand:
226 *
227 * Expand the dkwedges array.
228 */
229 static void
230 dkwedge_array_expand(void)
231 {
232 int newcnt = ndkwedges + 16;
233 struct dkwedge_softc **newarray, **oldarray;
234
235 newarray = malloc(newcnt * sizeof(*newarray), M_DKWEDGE,
236 M_WAITOK|M_ZERO);
237 if ((oldarray = dkwedges) != NULL)
238 memcpy(newarray, dkwedges, ndkwedges * sizeof(*newarray));
239 dkwedges = newarray;
240 ndkwedges = newcnt;
241 if (oldarray != NULL)
242 free(oldarray, M_DKWEDGE);
243 }
244
245 static void
246 dkgetproperties(struct disk *disk, struct dkwedge_info *dkw)
247 {
248 struct disk_geom *dg = &disk->dk_geom;
249
250 memset(dg, 0, sizeof(*dg));
251
252 dg->dg_secperunit = dkw->dkw_size >> disk->dk_blkshift;
253 dg->dg_secsize = DEV_BSIZE << disk->dk_blkshift;
254 dg->dg_nsectors = 32;
255 dg->dg_ntracks = 64;
256 /* XXX: why is that dkw->dkw_size instead of secperunit?!?! */
257 dg->dg_ncylinders = dkw->dkw_size / (dg->dg_nsectors * dg->dg_ntracks);
258
259 disk_set_info(NULL, disk, "ESDI");
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 * device_t necessary for glue to other parts of the system.
404 *
405 * This should never fail, unless we're almost totally out of
406 * memory.
407 */
408 if ((sc->sc_dev = config_attach_pseudo(&sc->sc_cfdata)) == NULL) {
409 aprint_error("%s%u: unable to attach pseudo-device\n",
410 sc->sc_cfdata.cf_name, sc->sc_cfdata.cf_unit);
411
412 rw_enter(&dkwedges_lock, RW_WRITER);
413 dkwedges[sc->sc_cfdata.cf_unit] = NULL;
414 rw_exit(&dkwedges_lock);
415
416 mutex_enter(&pdk->dk_openlock);
417 pdk->dk_nwedges--;
418 LIST_REMOVE(sc, sc_plink);
419 mutex_exit(&pdk->dk_openlock);
420
421 bufq_free(sc->sc_bufq);
422 free(sc, M_DKWEDGE);
423 return (ENOMEM);
424 }
425
426 /* Return the devname to the caller. */
427 strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev),
428 sizeof(dkw->dkw_devname));
429
430 /*
431 * XXX Really ought to make the disk_attach() and the changing
432 * of state to RUNNING atomic.
433 */
434
435 disk_init(&sc->sc_dk, device_xname(sc->sc_dev), NULL);
436 disk_blocksize(&sc->sc_dk, DEV_BSIZE << pdk->dk_blkshift);
437 dkgetproperties(&sc->sc_dk, dkw);
438 disk_attach(&sc->sc_dk);
439
440 /* Disk wedge is ready for use! */
441 sc->sc_state = DKW_STATE_RUNNING;
442
443 /* Announce our arrival. */
444 aprint_normal("%s at %s: %s\n", device_xname(sc->sc_dev), pdk->dk_name,
445 sc->sc_wname); /* XXX Unicode */
446 aprint_normal("%s: %"PRIu64" blocks at %"PRId64", type: %s\n",
447 device_xname(sc->sc_dev), sc->sc_size, sc->sc_offset, sc->sc_ptype);
448
449 return (0);
450 }
451
452 /*
453 * dkwedge_find:
454 *
455 * Lookup a disk wedge based on the provided information.
456 * NOTE: We look up the wedge based on the wedge devname,
457 * not wname.
458 *
459 * Return NULL if the wedge is not found, otherwise return
460 * the wedge's softc. Assign the wedge's unit number to unitp
461 * if unitp is not NULL.
462 */
463 static struct dkwedge_softc *
464 dkwedge_find(struct dkwedge_info *dkw, u_int *unitp)
465 {
466 struct dkwedge_softc *sc = NULL;
467 u_int unit;
468
469 /* Find our softc. */
470 dkw->dkw_devname[sizeof(dkw->dkw_devname) - 1] = '\0';
471 rw_enter(&dkwedges_lock, RW_READER);
472 for (unit = 0; unit < ndkwedges; unit++) {
473 if ((sc = dkwedges[unit]) != NULL &&
474 strcmp(device_xname(sc->sc_dev), dkw->dkw_devname) == 0 &&
475 strcmp(sc->sc_parent->dk_name, dkw->dkw_parent) == 0) {
476 break;
477 }
478 }
479 rw_exit(&dkwedges_lock);
480 if (unit == ndkwedges)
481 return NULL;
482
483 if (unitp != NULL)
484 *unitp = unit;
485
486 return sc;
487 }
488
489 /*
490 * dkwedge_del: [exported function]
491 *
492 * Delete a disk wedge based on the provided information.
493 * NOTE: We look up the wedge based on the wedge devname,
494 * not wname.
495 */
496 int
497 dkwedge_del(struct dkwedge_info *dkw)
498 {
499 struct dkwedge_softc *sc = NULL;
500
501 /* Find our softc. */
502 if ((sc = dkwedge_find(dkw, NULL)) == NULL)
503 return (ESRCH);
504
505 return config_detach(sc->sc_dev, DETACH_FORCE | DETACH_QUIET);
506 }
507
508 static int
509 dkwedge_begindetach(struct dkwedge_softc *sc, int flags)
510 {
511 struct disk *dk = &sc->sc_dk;
512 int rc;
513
514 rc = 0;
515 mutex_enter(&dk->dk_openlock);
516 if (dk->dk_openmask == 0)
517 ; /* nothing to do */
518 else if ((flags & DETACH_FORCE) == 0)
519 rc = EBUSY;
520 else {
521 mutex_enter(&sc->sc_parent->dk_rawlock);
522 rc = dklastclose(sc); /* releases dk_rawlock */
523 }
524 mutex_exit(&dk->dk_openlock);
525
526 return rc;
527 }
528
529 /*
530 * dkwedge_detach:
531 *
532 * Autoconfiguration detach function for pseudo-device glue.
533 */
534 static int
535 dkwedge_detach(device_t self, int flags)
536 {
537 struct dkwedge_softc *sc = NULL;
538 u_int unit;
539 int bmaj, cmaj, rc, s;
540
541 rw_enter(&dkwedges_lock, RW_WRITER);
542 for (unit = 0; unit < ndkwedges; unit++) {
543 if ((sc = dkwedges[unit]) != NULL && sc->sc_dev == self)
544 break;
545 }
546 if (unit == ndkwedges)
547 rc = ENXIO;
548 else if ((rc = dkwedge_begindetach(sc, flags)) == 0) {
549 /* Mark the wedge as dying. */
550 sc->sc_state = DKW_STATE_DYING;
551 }
552 rw_exit(&dkwedges_lock);
553
554 if (rc != 0)
555 return rc;
556
557 pmf_device_deregister(self);
558
559 /* Locate the wedge major numbers. */
560 bmaj = bdevsw_lookup_major(&dk_bdevsw);
561 cmaj = cdevsw_lookup_major(&dk_cdevsw);
562
563 /* Kill any pending restart. */
564 callout_stop(&sc->sc_restart_ch);
565
566 /*
567 * dkstart() will kill any queued buffers now that the
568 * state of the wedge is not RUNNING. Once we've done
569 * that, wait for any other pending I/O to complete.
570 */
571 s = splbio();
572 dkstart(sc);
573 dkwedge_wait_drain(sc);
574 splx(s);
575
576 /* Nuke the vnodes for any open instances. */
577 vdevgone(bmaj, unit, unit, VBLK);
578 vdevgone(cmaj, unit, unit, VCHR);
579
580 /* Clean up the parent. */
581 mutex_enter(&sc->sc_dk.dk_openlock);
582 if (sc->sc_dk.dk_openmask) {
583 mutex_enter(&sc->sc_parent->dk_rawlock);
584 if (sc->sc_parent->dk_rawopens-- == 1) {
585 KASSERT(sc->sc_parent->dk_rawvp != NULL);
586 mutex_exit(&sc->sc_parent->dk_rawlock);
587 (void) vn_close(sc->sc_parent->dk_rawvp, FREAD | FWRITE,
588 NOCRED);
589 sc->sc_parent->dk_rawvp = NULL;
590 } else
591 mutex_exit(&sc->sc_parent->dk_rawlock);
592 sc->sc_dk.dk_openmask = 0;
593 }
594 mutex_exit(&sc->sc_dk.dk_openlock);
595
596 /* Announce our departure. */
597 aprint_normal("%s at %s (%s) deleted\n", device_xname(sc->sc_dev),
598 sc->sc_parent->dk_name,
599 sc->sc_wname); /* XXX Unicode */
600
601 mutex_enter(&sc->sc_parent->dk_openlock);
602 sc->sc_parent->dk_nwedges--;
603 LIST_REMOVE(sc, sc_plink);
604 mutex_exit(&sc->sc_parent->dk_openlock);
605
606 /* Delete our buffer queue. */
607 bufq_free(sc->sc_bufq);
608
609 /* Detach from the disk list. */
610 disk_detach(&sc->sc_dk);
611 disk_destroy(&sc->sc_dk);
612
613 /* Poof. */
614 rw_enter(&dkwedges_lock, RW_WRITER);
615 dkwedges[unit] = NULL;
616 sc->sc_state = DKW_STATE_DEAD;
617 rw_exit(&dkwedges_lock);
618
619 free(sc, M_DKWEDGE);
620
621 return 0;
622 }
623
624 /*
625 * dkwedge_delall: [exported function]
626 *
627 * Delete all of the wedges on the specified disk. Used when
628 * a disk is being detached.
629 */
630 void
631 dkwedge_delall(struct disk *pdk)
632 {
633 struct dkwedge_info dkw;
634 struct dkwedge_softc *sc;
635
636 for (;;) {
637 mutex_enter(&pdk->dk_openlock);
638 if ((sc = LIST_FIRST(&pdk->dk_wedges)) == NULL) {
639 KASSERT(pdk->dk_nwedges == 0);
640 mutex_exit(&pdk->dk_openlock);
641 return;
642 }
643 strcpy(dkw.dkw_parent, pdk->dk_name);
644 strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev),
645 sizeof(dkw.dkw_devname));
646 mutex_exit(&pdk->dk_openlock);
647 (void) dkwedge_del(&dkw);
648 }
649 }
650
651 /*
652 * dkwedge_list: [exported function]
653 *
654 * List all of the wedges on a particular disk.
655 * If p == NULL, the buffer is in kernel space. Otherwise, it is
656 * in user space of the specified process.
657 */
658 int
659 dkwedge_list(struct disk *pdk, struct dkwedge_list *dkwl, struct lwp *l)
660 {
661 struct uio uio;
662 struct iovec iov;
663 struct dkwedge_softc *sc;
664 struct dkwedge_info dkw;
665 int error = 0;
666
667 iov.iov_base = dkwl->dkwl_buf;
668 iov.iov_len = dkwl->dkwl_bufsize;
669
670 uio.uio_iov = &iov;
671 uio.uio_iovcnt = 1;
672 uio.uio_offset = 0;
673 uio.uio_resid = dkwl->dkwl_bufsize;
674 uio.uio_rw = UIO_READ;
675 KASSERT(l == curlwp);
676 uio.uio_vmspace = l->l_proc->p_vmspace;
677
678 dkwl->dkwl_ncopied = 0;
679
680 mutex_enter(&pdk->dk_openlock);
681 LIST_FOREACH(sc, &pdk->dk_wedges, sc_plink) {
682 if (uio.uio_resid < sizeof(dkw))
683 break;
684
685 if (sc->sc_state != DKW_STATE_RUNNING)
686 continue;
687
688 strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev),
689 sizeof(dkw.dkw_devname));
690 memcpy(dkw.dkw_wname, sc->sc_wname, sizeof(dkw.dkw_wname));
691 dkw.dkw_wname[sizeof(dkw.dkw_wname) - 1] = '\0';
692 strcpy(dkw.dkw_parent, sc->sc_parent->dk_name);
693 dkw.dkw_offset = sc->sc_offset;
694 dkw.dkw_size = sc->sc_size;
695 strcpy(dkw.dkw_ptype, sc->sc_ptype);
696
697 error = uiomove(&dkw, sizeof(dkw), &uio);
698 if (error)
699 break;
700 dkwl->dkwl_ncopied++;
701 }
702 dkwl->dkwl_nwedges = pdk->dk_nwedges;
703 mutex_exit(&pdk->dk_openlock);
704
705 return (error);
706 }
707
708 device_t
709 dkwedge_find_by_wname(const char *wname)
710 {
711 device_t dv = NULL;
712 struct dkwedge_softc *sc;
713 int i;
714
715 rw_enter(&dkwedges_lock, RW_WRITER);
716 for (i = 0; i < ndkwedges; i++) {
717 if ((sc = dkwedges[i]) == NULL)
718 continue;
719 if (strcmp(sc->sc_wname, wname) == 0) {
720 if (dv != NULL) {
721 printf(
722 "WARNING: double match for wedge name %s "
723 "(%s, %s)\n", wname, device_xname(dv),
724 device_xname(sc->sc_dev));
725 continue;
726 }
727 dv = sc->sc_dev;
728 }
729 }
730 rw_exit(&dkwedges_lock);
731 return dv;
732 }
733
734 void
735 dkwedge_print_wnames(void)
736 {
737 struct dkwedge_softc *sc;
738 int i;
739
740 rw_enter(&dkwedges_lock, RW_WRITER);
741 for (i = 0; i < ndkwedges; i++) {
742 if ((sc = dkwedges[i]) == NULL)
743 continue;
744 printf(" wedge:%s", sc->sc_wname);
745 }
746 rw_exit(&dkwedges_lock);
747 }
748
749 /*
750 * We need a dummy object to stuff into the dkwedge discovery method link
751 * set to ensure that there is always at least one object in the set.
752 */
753 static struct dkwedge_discovery_method dummy_discovery_method;
754 __link_set_add_bss(dkwedge_methods, dummy_discovery_method);
755
756 /*
757 * dkwedge_init:
758 *
759 * Initialize the disk wedge subsystem.
760 */
761 void
762 dkwedge_init(void)
763 {
764 __link_set_decl(dkwedge_methods, struct dkwedge_discovery_method);
765 struct dkwedge_discovery_method * const *ddmp;
766 struct dkwedge_discovery_method *lddm, *ddm;
767
768 rw_init(&dkwedges_lock);
769 rw_init(&dkwedge_discovery_methods_lock);
770
771 if (config_cfdriver_attach(&dk_cd) != 0)
772 panic("dkwedge: unable to attach cfdriver");
773 if (config_cfattach_attach(dk_cd.cd_name, &dk_ca) != 0)
774 panic("dkwedge: unable to attach cfattach");
775
776 rw_enter(&dkwedge_discovery_methods_lock, RW_WRITER);
777
778 LIST_INIT(&dkwedge_discovery_methods);
779
780 __link_set_foreach(ddmp, dkwedge_methods) {
781 ddm = *ddmp;
782 if (ddm == &dummy_discovery_method)
783 continue;
784 if (LIST_EMPTY(&dkwedge_discovery_methods)) {
785 LIST_INSERT_HEAD(&dkwedge_discovery_methods,
786 ddm, ddm_list);
787 continue;
788 }
789 LIST_FOREACH(lddm, &dkwedge_discovery_methods, ddm_list) {
790 if (ddm->ddm_priority == lddm->ddm_priority) {
791 aprint_error("dk-method-%s: method \"%s\" "
792 "already exists at priority %d\n",
793 ddm->ddm_name, lddm->ddm_name,
794 lddm->ddm_priority);
795 /* Not inserted. */
796 break;
797 }
798 if (ddm->ddm_priority < lddm->ddm_priority) {
799 /* Higher priority; insert before. */
800 LIST_INSERT_BEFORE(lddm, ddm, ddm_list);
801 break;
802 }
803 if (LIST_NEXT(lddm, ddm_list) == NULL) {
804 /* Last one; insert after. */
805 KASSERT(lddm->ddm_priority < ddm->ddm_priority);
806 LIST_INSERT_AFTER(lddm, ddm, ddm_list);
807 break;
808 }
809 }
810 }
811
812 rw_exit(&dkwedge_discovery_methods_lock);
813 }
814
815 #ifdef DKWEDGE_AUTODISCOVER
816 int dkwedge_autodiscover = 1;
817 #else
818 int dkwedge_autodiscover = 0;
819 #endif
820
821 /*
822 * dkwedge_discover: [exported function]
823 *
824 * Discover the wedges on a newly attached disk.
825 */
826 void
827 dkwedge_discover(struct disk *pdk)
828 {
829 struct dkwedge_discovery_method *ddm;
830 struct vnode *vp;
831 int error;
832 dev_t pdev;
833
834 /*
835 * Require people playing with wedges to enable this explicitly.
836 */
837 if (dkwedge_autodiscover == 0)
838 return;
839
840 rw_enter(&dkwedge_discovery_methods_lock, RW_READER);
841
842 error = dkwedge_compute_pdev(pdk->dk_name, &pdev);
843 if (error) {
844 aprint_error("%s: unable to compute pdev, error = %d\n",
845 pdk->dk_name, error);
846 goto out;
847 }
848
849 error = bdevvp(pdev, &vp);
850 if (error) {
851 aprint_error("%s: unable to find vnode for pdev, error = %d\n",
852 pdk->dk_name, error);
853 goto out;
854 }
855
856 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
857 if (error) {
858 aprint_error("%s: unable to lock vnode for pdev, error = %d\n",
859 pdk->dk_name, error);
860 vrele(vp);
861 goto out;
862 }
863
864 error = VOP_OPEN(vp, FREAD | FSILENT, NOCRED);
865 if (error) {
866 if (error != ENODEV)
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 const char *
1467 dkwedge_get_parent_name(dev_t dev)
1468 {
1469 /* XXX: perhaps do this in lookup? */
1470 int bmaj = bdevsw_lookup_major(&dk_bdevsw);
1471 int cmaj = cdevsw_lookup_major(&dk_cdevsw);
1472 if (major(dev) != bmaj && major(dev) != cmaj)
1473 return NULL;
1474 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1475 if (sc == NULL)
1476 return NULL;
1477 return sc->sc_parent->dk_name;
1478 }
1479