dk.c revision 1.64.2.1 1 /* $NetBSD: dk.c,v 1.64.2.1 2012/11/20 03:02:00 tls Exp $ */
2
3 /*-
4 * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.64.2.1 2012/11/20 03:02:00 tls 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 dkopen, dkclose, dkstrategy, dkioctl, dkdump, dksize, D_DISK
114 };
115
116 const struct cdevsw dk_cdevsw = {
117 dkopen, dkclose, dkread, dkwrite, dkioctl,
118 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
119 };
120
121 static struct dkwedge_softc **dkwedges;
122 static u_int ndkwedges;
123 static krwlock_t dkwedges_lock;
124
125 static LIST_HEAD(, dkwedge_discovery_method) dkwedge_discovery_methods;
126 static krwlock_t dkwedge_discovery_methods_lock;
127
128 /*
129 * dkwedge_match:
130 *
131 * Autoconfiguration match function for pseudo-device glue.
132 */
133 static int
134 dkwedge_match(device_t parent, cfdata_t match,
135 void *aux)
136 {
137
138 /* Pseudo-device; always present. */
139 return (1);
140 }
141
142 /*
143 * dkwedge_attach:
144 *
145 * Autoconfiguration attach function for pseudo-device glue.
146 */
147 static void
148 dkwedge_attach(device_t parent, device_t self,
149 void *aux)
150 {
151
152 if (!pmf_device_register(self, NULL, NULL))
153 aprint_error_dev(self, "couldn't establish power handler\n");
154 }
155
156 CFDRIVER_DECL(dk, DV_DISK, NULL);
157 CFATTACH_DECL3_NEW(dk, 0,
158 dkwedge_match, dkwedge_attach, dkwedge_detach, NULL, NULL, NULL,
159 DVF_DETACH_SHUTDOWN);
160
161 /*
162 * dkwedge_wait_drain:
163 *
164 * Wait for I/O on the wedge to drain.
165 * NOTE: Must be called at splbio()!
166 */
167 static void
168 dkwedge_wait_drain(struct dkwedge_softc *sc)
169 {
170
171 while (sc->sc_iopend != 0) {
172 sc->sc_flags |= DK_F_WAIT_DRAIN;
173 (void) tsleep(&sc->sc_iopend, PRIBIO, "dkdrn", 0);
174 }
175 }
176
177 /*
178 * dkwedge_compute_pdev:
179 *
180 * Compute the parent disk's dev_t.
181 */
182 static int
183 dkwedge_compute_pdev(const char *pname, dev_t *pdevp)
184 {
185 const char *name, *cp;
186 devmajor_t pmaj;
187 int punit;
188 char devname[16];
189
190 name = pname;
191 if ((pmaj = devsw_name2blk(name, devname, sizeof(devname))) == -1)
192 return (ENODEV);
193
194 name += strlen(devname);
195 for (cp = name, punit = 0; *cp >= '0' && *cp <= '9'; cp++)
196 punit = (punit * 10) + (*cp - '0');
197 if (cp == name) {
198 /* Invalid parent disk name. */
199 return (ENODEV);
200 }
201
202 *pdevp = MAKEDISKDEV(pmaj, punit, RAW_PART);
203
204 return (0);
205 }
206
207 /*
208 * dkwedge_array_expand:
209 *
210 * Expand the dkwedges array.
211 */
212 static void
213 dkwedge_array_expand(void)
214 {
215 int newcnt = ndkwedges + 16;
216 struct dkwedge_softc **newarray, **oldarray;
217
218 newarray = malloc(newcnt * sizeof(*newarray), M_DKWEDGE,
219 M_WAITOK|M_ZERO);
220 if ((oldarray = dkwedges) != NULL)
221 memcpy(newarray, dkwedges, ndkwedges * sizeof(*newarray));
222 dkwedges = newarray;
223 ndkwedges = newcnt;
224 if (oldarray != NULL)
225 free(oldarray, M_DKWEDGE);
226 }
227
228 static void
229 dkgetproperties(struct disk *disk, struct dkwedge_info *dkw)
230 {
231 prop_dictionary_t disk_info, odisk_info, geom;
232
233 disk_info = prop_dictionary_create();
234
235 prop_dictionary_set_cstring_nocopy(disk_info, "type", "ESDI");
236
237 geom = prop_dictionary_create();
238
239 prop_dictionary_set_uint64(geom, "sectors-per-unit",
240 dkw->dkw_size >> disk->dk_blkshift);
241
242 prop_dictionary_set_uint32(geom, "sector-size",
243 DEV_BSIZE << disk->dk_blkshift);
244
245 prop_dictionary_set_uint32(geom, "sectors-per-track", 32);
246
247 prop_dictionary_set_uint32(geom, "tracks-per-cylinder", 64);
248
249 prop_dictionary_set_uint32(geom, "cylinders-per-unit", dkw->dkw_size / 2048);
250
251 prop_dictionary_set(disk_info, "geometry", geom);
252 prop_object_release(geom);
253
254 odisk_info = disk->dk_info;
255
256 disk->dk_info = disk_info;
257
258 if (odisk_info != NULL)
259 prop_object_release(odisk_info);
260 }
261
262 /*
263 * dkwedge_add: [exported function]
264 *
265 * Add a disk wedge based on the provided information.
266 *
267 * The incoming dkw_devname[] is ignored, instead being
268 * filled in and returned to the caller.
269 */
270 int
271 dkwedge_add(struct dkwedge_info *dkw)
272 {
273 struct dkwedge_softc *sc, *lsc;
274 struct disk *pdk;
275 u_int unit;
276 int error;
277 dev_t pdev;
278
279 dkw->dkw_parent[sizeof(dkw->dkw_parent) - 1] = '\0';
280 pdk = disk_find(dkw->dkw_parent);
281 if (pdk == NULL)
282 return (ENODEV);
283
284 error = dkwedge_compute_pdev(pdk->dk_name, &pdev);
285 if (error)
286 return (error);
287
288 if (dkw->dkw_offset < 0)
289 return (EINVAL);
290
291 sc = malloc(sizeof(*sc), M_DKWEDGE, M_WAITOK|M_ZERO);
292 sc->sc_state = DKW_STATE_LARVAL;
293 sc->sc_parent = pdk;
294 sc->sc_pdev = pdev;
295 sc->sc_offset = dkw->dkw_offset;
296 sc->sc_size = dkw->dkw_size;
297
298 memcpy(sc->sc_wname, dkw->dkw_wname, sizeof(sc->sc_wname));
299 sc->sc_wname[sizeof(sc->sc_wname) - 1] = '\0';
300
301 memcpy(sc->sc_ptype, dkw->dkw_ptype, sizeof(sc->sc_ptype));
302 sc->sc_ptype[sizeof(sc->sc_ptype) - 1] = '\0';
303
304 bufq_alloc(&sc->sc_bufq, "fcfs", 0);
305
306 callout_init(&sc->sc_restart_ch, 0);
307 callout_setfunc(&sc->sc_restart_ch, dkrestart, sc);
308
309 /*
310 * Wedge will be added; increment the wedge count for the parent.
311 * Only allow this to happend if RAW_PART is the only thing open.
312 */
313 mutex_enter(&pdk->dk_openlock);
314 if (pdk->dk_openmask & ~(1 << RAW_PART))
315 error = EBUSY;
316 else {
317 /* Check for wedge overlap. */
318 LIST_FOREACH(lsc, &pdk->dk_wedges, sc_plink) {
319 daddr_t lastblk = sc->sc_offset + sc->sc_size - 1;
320 daddr_t llastblk = lsc->sc_offset + lsc->sc_size - 1;
321
322 if (sc->sc_offset >= lsc->sc_offset &&
323 sc->sc_offset <= llastblk) {
324 /* Overlaps the tail of the existing wedge. */
325 break;
326 }
327 if (lastblk >= lsc->sc_offset &&
328 lastblk <= llastblk) {
329 /* Overlaps the head of the existing wedge. */
330 break;
331 }
332 }
333 if (lsc != NULL)
334 error = EINVAL;
335 else {
336 pdk->dk_nwedges++;
337 LIST_INSERT_HEAD(&pdk->dk_wedges, sc, sc_plink);
338 }
339 }
340 mutex_exit(&pdk->dk_openlock);
341 if (error) {
342 bufq_free(sc->sc_bufq);
343 free(sc, M_DKWEDGE);
344 return (error);
345 }
346
347 /* Fill in our cfdata for the pseudo-device glue. */
348 sc->sc_cfdata.cf_name = dk_cd.cd_name;
349 sc->sc_cfdata.cf_atname = dk_ca.ca_name;
350 /* sc->sc_cfdata.cf_unit set below */
351 sc->sc_cfdata.cf_fstate = FSTATE_STAR;
352
353 /* Insert the larval wedge into the array. */
354 rw_enter(&dkwedges_lock, RW_WRITER);
355 for (error = 0;;) {
356 struct dkwedge_softc **scpp;
357
358 /*
359 * Check for a duplicate wname while searching for
360 * a slot.
361 */
362 for (scpp = NULL, unit = 0; unit < ndkwedges; unit++) {
363 if (dkwedges[unit] == NULL) {
364 if (scpp == NULL) {
365 scpp = &dkwedges[unit];
366 sc->sc_cfdata.cf_unit = unit;
367 }
368 } else {
369 /* XXX Unicode. */
370 if (strcmp(dkwedges[unit]->sc_wname,
371 sc->sc_wname) == 0) {
372 error = EEXIST;
373 break;
374 }
375 }
376 }
377 if (error)
378 break;
379 KASSERT(unit == ndkwedges);
380 if (scpp == NULL)
381 dkwedge_array_expand();
382 else {
383 KASSERT(scpp == &dkwedges[sc->sc_cfdata.cf_unit]);
384 *scpp = sc;
385 break;
386 }
387 }
388 rw_exit(&dkwedges_lock);
389 if (error) {
390 mutex_enter(&pdk->dk_openlock);
391 pdk->dk_nwedges--;
392 LIST_REMOVE(sc, sc_plink);
393 mutex_exit(&pdk->dk_openlock);
394
395 bufq_free(sc->sc_bufq);
396 free(sc, M_DKWEDGE);
397 return (error);
398 }
399
400 /*
401 * Now that we know the unit #, attach a pseudo-device for
402 * this wedge instance. This will provide us with the
403 * 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 aprint_error("%s: unable to open device, error = %d\n",
867 pdk->dk_name, error);
868 vput(vp);
869 goto out;
870 }
871 VOP_UNLOCK(vp);
872
873 /*
874 * For each supported partition map type, look to see if
875 * this map type exists. If so, parse it and add the
876 * corresponding wedges.
877 */
878 LIST_FOREACH(ddm, &dkwedge_discovery_methods, ddm_list) {
879 error = (*ddm->ddm_discover)(pdk, vp);
880 if (error == 0) {
881 /* Successfully created wedges; we're done. */
882 break;
883 }
884 }
885
886 error = vn_close(vp, FREAD, NOCRED);
887 if (error) {
888 aprint_error("%s: unable to close device, error = %d\n",
889 pdk->dk_name, error);
890 /* We'll just assume the vnode has been cleaned up. */
891 }
892 out:
893 rw_exit(&dkwedge_discovery_methods_lock);
894 }
895
896 /*
897 * dkwedge_read:
898 *
899 * Read some data from the specified disk, used for
900 * partition discovery.
901 */
902 int
903 dkwedge_read(struct disk *pdk, struct vnode *vp, daddr_t blkno,
904 void *tbuf, size_t len)
905 {
906 struct buf *bp;
907 int result;
908
909 bp = getiobuf(vp, true);
910
911 bp->b_dev = vp->v_rdev;
912 bp->b_blkno = blkno;
913 bp->b_bcount = len;
914 bp->b_resid = len;
915 bp->b_flags = B_READ;
916 bp->b_data = tbuf;
917 SET(bp->b_cflags, BC_BUSY); /* mark buffer busy */
918
919 VOP_STRATEGY(vp, bp);
920 result = biowait(bp);
921 putiobuf(bp);
922
923 return result;
924 }
925
926 /*
927 * dkwedge_lookup:
928 *
929 * Look up a dkwedge_softc based on the provided dev_t.
930 */
931 static struct dkwedge_softc *
932 dkwedge_lookup(dev_t dev)
933 {
934 int unit = minor(dev);
935
936 if (unit >= ndkwedges)
937 return (NULL);
938
939 KASSERT(dkwedges != NULL);
940
941 return (dkwedges[unit]);
942 }
943
944 /*
945 * dkopen: [devsw entry point]
946 *
947 * Open a wedge.
948 */
949 static int
950 dkopen(dev_t dev, int flags, int fmt, struct lwp *l)
951 {
952 struct dkwedge_softc *sc = dkwedge_lookup(dev);
953 struct vnode *vp;
954 int error = 0;
955
956 if (sc == NULL)
957 return (ENODEV);
958 if (sc->sc_state != DKW_STATE_RUNNING)
959 return (ENXIO);
960
961 /*
962 * We go through a complicated little dance to only open the parent
963 * vnode once per wedge, no matter how many times the wedge is
964 * opened. The reason? We see one dkopen() per open call, but
965 * only dkclose() on the last close.
966 */
967 mutex_enter(&sc->sc_dk.dk_openlock);
968 mutex_enter(&sc->sc_parent->dk_rawlock);
969 if (sc->sc_dk.dk_openmask == 0) {
970 if (sc->sc_parent->dk_rawopens == 0) {
971 KASSERT(sc->sc_parent->dk_rawvp == NULL);
972 error = bdevvp(sc->sc_pdev, &vp);
973 if (error)
974 goto popen_fail;
975 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
976 if (error) {
977 vrele(vp);
978 goto popen_fail;
979 }
980 error = VOP_OPEN(vp, FREAD | FWRITE, NOCRED);
981 if (error) {
982 vput(vp);
983 goto popen_fail;
984 }
985 /* VOP_OPEN() doesn't do this for us. */
986 mutex_enter(vp->v_interlock);
987 vp->v_writecount++;
988 mutex_exit(vp->v_interlock);
989 VOP_UNLOCK(vp);
990 sc->sc_parent->dk_rawvp = vp;
991 }
992 sc->sc_parent->dk_rawopens++;
993 }
994 if (fmt == S_IFCHR)
995 sc->sc_dk.dk_copenmask |= 1;
996 else
997 sc->sc_dk.dk_bopenmask |= 1;
998 sc->sc_dk.dk_openmask =
999 sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
1000
1001 popen_fail:
1002 mutex_exit(&sc->sc_parent->dk_rawlock);
1003 mutex_exit(&sc->sc_dk.dk_openlock);
1004 return (error);
1005 }
1006
1007 /*
1008 * Caller must hold sc->sc_dk.dk_openlock and sc->sc_parent->dk_rawlock.
1009 */
1010 static int
1011 dklastclose(struct dkwedge_softc *sc)
1012 {
1013 int error = 0;
1014
1015 if (sc->sc_parent->dk_rawopens-- == 1) {
1016 KASSERT(sc->sc_parent->dk_rawvp != NULL);
1017 mutex_exit(&sc->sc_parent->dk_rawlock);
1018 error = vn_close(sc->sc_parent->dk_rawvp,
1019 FREAD | FWRITE, NOCRED);
1020 sc->sc_parent->dk_rawvp = NULL;
1021 } else
1022 mutex_exit(&sc->sc_parent->dk_rawlock);
1023 return error;
1024 }
1025
1026 /*
1027 * dkclose: [devsw entry point]
1028 *
1029 * Close a wedge.
1030 */
1031 static int
1032 dkclose(dev_t dev, int flags, int fmt, struct lwp *l)
1033 {
1034 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1035 int error = 0;
1036
1037 if (sc == NULL)
1038 return (ENODEV);
1039 if (sc->sc_state != DKW_STATE_RUNNING)
1040 return (ENXIO);
1041
1042 KASSERT(sc->sc_dk.dk_openmask != 0);
1043
1044 mutex_enter(&sc->sc_dk.dk_openlock);
1045 mutex_enter(&sc->sc_parent->dk_rawlock);
1046
1047 if (fmt == S_IFCHR)
1048 sc->sc_dk.dk_copenmask &= ~1;
1049 else
1050 sc->sc_dk.dk_bopenmask &= ~1;
1051 sc->sc_dk.dk_openmask =
1052 sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
1053
1054 if (sc->sc_dk.dk_openmask == 0)
1055 error = dklastclose(sc); /* releases dk_rawlock */
1056 else
1057 mutex_exit(&sc->sc_parent->dk_rawlock);
1058
1059 mutex_exit(&sc->sc_dk.dk_openlock);
1060
1061 return (error);
1062 }
1063
1064 /*
1065 * dkstragegy: [devsw entry point]
1066 *
1067 * Perform I/O based on the wedge I/O strategy.
1068 */
1069 static void
1070 dkstrategy(struct buf *bp)
1071 {
1072 struct dkwedge_softc *sc = dkwedge_lookup(bp->b_dev);
1073 uint64_t p_size, p_offset;
1074 int s;
1075
1076 if (sc == NULL) {
1077 bp->b_error = ENODEV;
1078 goto done;
1079 }
1080
1081 if (sc->sc_state != DKW_STATE_RUNNING ||
1082 sc->sc_parent->dk_rawvp == NULL) {
1083 bp->b_error = ENXIO;
1084 goto done;
1085 }
1086
1087 /* If it's an empty transfer, wake up the top half now. */
1088 if (bp->b_bcount == 0)
1089 goto done;
1090
1091 p_offset = sc->sc_offset << sc->sc_parent->dk_blkshift;
1092 p_size = sc->sc_size << sc->sc_parent->dk_blkshift;
1093
1094 /* Make sure it's in-range. */
1095 if (bounds_check_with_mediasize(bp, DEV_BSIZE, p_size) <= 0)
1096 goto done;
1097
1098 /* Translate it to the parent's raw LBA. */
1099 bp->b_rawblkno = bp->b_blkno + p_offset;
1100
1101 /* Place it in the queue and start I/O on the unit. */
1102 s = splbio();
1103 sc->sc_iopend++;
1104 bufq_put(sc->sc_bufq, bp);
1105 dkstart(sc);
1106 splx(s);
1107 return;
1108
1109 done:
1110 bp->b_resid = bp->b_bcount;
1111 biodone(bp);
1112 }
1113
1114 /*
1115 * dkstart:
1116 *
1117 * Start I/O that has been enqueued on the wedge.
1118 * NOTE: Must be called at splbio()!
1119 */
1120 static void
1121 dkstart(struct dkwedge_softc *sc)
1122 {
1123 struct vnode *vp;
1124 struct buf *bp, *nbp;
1125
1126 /* Do as much work as has been enqueued. */
1127 while ((bp = bufq_peek(sc->sc_bufq)) != NULL) {
1128 if (sc->sc_state != DKW_STATE_RUNNING) {
1129 (void) bufq_get(sc->sc_bufq);
1130 if (sc->sc_iopend-- == 1 &&
1131 (sc->sc_flags & DK_F_WAIT_DRAIN) != 0) {
1132 sc->sc_flags &= ~DK_F_WAIT_DRAIN;
1133 wakeup(&sc->sc_iopend);
1134 }
1135 bp->b_error = ENXIO;
1136 bp->b_resid = bp->b_bcount;
1137 biodone(bp);
1138 }
1139
1140 /* Instrumentation. */
1141 disk_busy(&sc->sc_dk);
1142
1143 nbp = getiobuf(sc->sc_parent->dk_rawvp, false);
1144 if (nbp == NULL) {
1145 /*
1146 * No resources to run this request; leave the
1147 * buffer queued up, and schedule a timer to
1148 * restart the queue in 1/2 a second.
1149 */
1150 disk_unbusy(&sc->sc_dk, 0, bp->b_flags & B_READ);
1151 callout_schedule(&sc->sc_restart_ch, hz / 2);
1152 return;
1153 }
1154
1155 (void) bufq_get(sc->sc_bufq);
1156
1157 nbp->b_data = bp->b_data;
1158 nbp->b_flags = bp->b_flags;
1159 nbp->b_oflags = bp->b_oflags;
1160 nbp->b_cflags = bp->b_cflags;
1161 nbp->b_iodone = dkiodone;
1162 nbp->b_proc = bp->b_proc;
1163 nbp->b_blkno = bp->b_rawblkno;
1164 nbp->b_dev = sc->sc_parent->dk_rawvp->v_rdev;
1165 nbp->b_bcount = bp->b_bcount;
1166 nbp->b_private = bp;
1167 BIO_COPYPRIO(nbp, bp);
1168
1169 vp = nbp->b_vp;
1170 if ((nbp->b_flags & B_READ) == 0) {
1171 mutex_enter(vp->v_interlock);
1172 vp->v_numoutput++;
1173 mutex_exit(vp->v_interlock);
1174 }
1175 VOP_STRATEGY(vp, nbp);
1176 }
1177 }
1178
1179 /*
1180 * dkiodone:
1181 *
1182 * I/O to a wedge has completed; alert the top half.
1183 */
1184 static void
1185 dkiodone(struct buf *bp)
1186 {
1187 struct buf *obp = bp->b_private;
1188 struct dkwedge_softc *sc = dkwedge_lookup(obp->b_dev);
1189
1190 int s = splbio();
1191
1192 if (bp->b_error != 0)
1193 obp->b_error = bp->b_error;
1194 obp->b_resid = bp->b_resid;
1195 putiobuf(bp);
1196
1197 if (sc->sc_iopend-- == 1 && (sc->sc_flags & DK_F_WAIT_DRAIN) != 0) {
1198 sc->sc_flags &= ~DK_F_WAIT_DRAIN;
1199 wakeup(&sc->sc_iopend);
1200 }
1201
1202 disk_unbusy(&sc->sc_dk, obp->b_bcount - obp->b_resid,
1203 obp->b_flags & B_READ);
1204
1205 biodone(obp);
1206
1207 /* Kick the queue in case there is more work we can do. */
1208 dkstart(sc);
1209 splx(s);
1210 }
1211
1212 /*
1213 * dkrestart:
1214 *
1215 * Restart the work queue after it was stalled due to
1216 * a resource shortage. Invoked via a callout.
1217 */
1218 static void
1219 dkrestart(void *v)
1220 {
1221 struct dkwedge_softc *sc = v;
1222 int s;
1223
1224 s = splbio();
1225 dkstart(sc);
1226 splx(s);
1227 }
1228
1229 /*
1230 * dkminphys:
1231 *
1232 * Call parent's minphys function.
1233 */
1234 static void
1235 dkminphys(struct buf *bp)
1236 {
1237 struct dkwedge_softc *sc = dkwedge_lookup(bp->b_dev);
1238 dev_t dev;
1239
1240 dev = bp->b_dev;
1241 bp->b_dev = sc->sc_pdev;
1242 (*sc->sc_parent->dk_driver->d_minphys)(bp);
1243 bp->b_dev = dev;
1244 }
1245
1246 /*
1247 * dkread: [devsw entry point]
1248 *
1249 * Read from a wedge.
1250 */
1251 static int
1252 dkread(dev_t dev, struct uio *uio, int flags)
1253 {
1254 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1255
1256 if (sc == NULL)
1257 return (ENODEV);
1258 if (sc->sc_state != DKW_STATE_RUNNING)
1259 return (ENXIO);
1260
1261 return (physio(dkstrategy, NULL, dev, B_READ, dkminphys, uio));
1262 }
1263
1264 /*
1265 * dkwrite: [devsw entry point]
1266 *
1267 * Write to a wedge.
1268 */
1269 static int
1270 dkwrite(dev_t dev, struct uio *uio, int flags)
1271 {
1272 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1273
1274 if (sc == NULL)
1275 return (ENODEV);
1276 if (sc->sc_state != DKW_STATE_RUNNING)
1277 return (ENXIO);
1278
1279 return (physio(dkstrategy, NULL, dev, B_WRITE, dkminphys, uio));
1280 }
1281
1282 /*
1283 * dkioctl: [devsw entry point]
1284 *
1285 * Perform an ioctl request on a wedge.
1286 */
1287 static int
1288 dkioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
1289 {
1290 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1291 int error = 0;
1292
1293 if (sc == NULL)
1294 return (ENODEV);
1295 if (sc->sc_state != DKW_STATE_RUNNING)
1296 return (ENXIO);
1297 if (sc->sc_parent->dk_rawvp == NULL)
1298 return (ENXIO);
1299
1300 error = disk_ioctl(&sc->sc_dk, cmd, data, flag, l);
1301 if (error != EPASSTHROUGH)
1302 return (error);
1303
1304 error = 0;
1305
1306 switch (cmd) {
1307 case DIOCCACHESYNC:
1308 /*
1309 * XXX Do we really need to care about having a writable
1310 * file descriptor here?
1311 */
1312 if ((flag & FWRITE) == 0)
1313 error = EBADF;
1314 else
1315 error = VOP_IOCTL(sc->sc_parent->dk_rawvp,
1316 cmd, data, flag,
1317 l != NULL ? l->l_cred : NOCRED);
1318 break;
1319 case DIOCGWEDGEINFO:
1320 {
1321 struct dkwedge_info *dkw = (void *) data;
1322
1323 strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev),
1324 sizeof(dkw->dkw_devname));
1325 memcpy(dkw->dkw_wname, sc->sc_wname, sizeof(dkw->dkw_wname));
1326 dkw->dkw_wname[sizeof(dkw->dkw_wname) - 1] = '\0';
1327 strcpy(dkw->dkw_parent, sc->sc_parent->dk_name);
1328 dkw->dkw_offset = sc->sc_offset;
1329 dkw->dkw_size = sc->sc_size;
1330 strcpy(dkw->dkw_ptype, sc->sc_ptype);
1331
1332 break;
1333 }
1334
1335 default:
1336 error = ENOTTY;
1337 }
1338
1339 return (error);
1340 }
1341
1342 /*
1343 * dksize: [devsw entry point]
1344 *
1345 * Query the size of a wedge for the purpose of performing a dump
1346 * or for swapping to.
1347 */
1348 static int
1349 dksize(dev_t dev)
1350 {
1351 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1352 int rv = -1;
1353
1354 if (sc == NULL)
1355 return (-1);
1356 if (sc->sc_state != DKW_STATE_RUNNING)
1357 return (-1);
1358
1359 mutex_enter(&sc->sc_dk.dk_openlock);
1360 mutex_enter(&sc->sc_parent->dk_rawlock);
1361
1362 /* Our content type is static, no need to open the device. */
1363
1364 if (strcmp(sc->sc_ptype, DKW_PTYPE_SWAP) == 0) {
1365 /* Saturate if we are larger than INT_MAX. */
1366 if (sc->sc_size > INT_MAX)
1367 rv = INT_MAX;
1368 else
1369 rv = (int) sc->sc_size;
1370 }
1371
1372 mutex_exit(&sc->sc_parent->dk_rawlock);
1373 mutex_exit(&sc->sc_dk.dk_openlock);
1374
1375 return (rv);
1376 }
1377
1378 /*
1379 * dkdump: [devsw entry point]
1380 *
1381 * Perform a crash dump to a wedge.
1382 */
1383 static int
1384 dkdump(dev_t dev, daddr_t blkno, void *va, size_t size)
1385 {
1386 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1387 const struct bdevsw *bdev;
1388 int rv = 0;
1389
1390 if (sc == NULL)
1391 return (ENODEV);
1392 if (sc->sc_state != DKW_STATE_RUNNING)
1393 return (ENXIO);
1394
1395 mutex_enter(&sc->sc_dk.dk_openlock);
1396 mutex_enter(&sc->sc_parent->dk_rawlock);
1397
1398 /* Our content type is static, no need to open the device. */
1399
1400 if (strcmp(sc->sc_ptype, DKW_PTYPE_SWAP) != 0) {
1401 rv = ENXIO;
1402 goto out;
1403 }
1404 if (size % DEV_BSIZE != 0) {
1405 rv = EINVAL;
1406 goto out;
1407 }
1408 if (blkno + size / DEV_BSIZE > sc->sc_size) {
1409 printf("%s: blkno (%" PRIu64 ") + size / DEV_BSIZE (%zu) > "
1410 "sc->sc_size (%" PRIu64 ")\n", __func__, blkno,
1411 size / DEV_BSIZE, sc->sc_size);
1412 rv = EINVAL;
1413 goto out;
1414 }
1415
1416 bdev = bdevsw_lookup(sc->sc_pdev);
1417 rv = (*bdev->d_dump)(sc->sc_pdev, blkno + sc->sc_offset, va, size);
1418
1419 out:
1420 mutex_exit(&sc->sc_parent->dk_rawlock);
1421 mutex_exit(&sc->sc_dk.dk_openlock);
1422
1423 return rv;
1424 }
1425
1426 /*
1427 * config glue
1428 */
1429
1430 /*
1431 * dkwedge_find_partition
1432 *
1433 * Find wedge corresponding to the specified parent name
1434 * and offset/length.
1435 */
1436 device_t
1437 dkwedge_find_partition(device_t parent, daddr_t startblk, uint64_t nblks)
1438 {
1439 struct dkwedge_softc *sc;
1440 int i;
1441 device_t wedge = NULL;
1442
1443 rw_enter(&dkwedges_lock, RW_READER);
1444 for (i = 0; i < ndkwedges; i++) {
1445 if ((sc = dkwedges[i]) == NULL)
1446 continue;
1447 if (strcmp(sc->sc_parent->dk_name, device_xname(parent)) == 0 &&
1448 sc->sc_offset == startblk &&
1449 sc->sc_size == nblks) {
1450 if (wedge) {
1451 printf("WARNING: double match for boot wedge "
1452 "(%s, %s)\n",
1453 device_xname(wedge),
1454 device_xname(sc->sc_dev));
1455 continue;
1456 }
1457 wedge = sc->sc_dev;
1458 }
1459 }
1460 rw_exit(&dkwedges_lock);
1461
1462 return wedge;
1463 }
1464
1465