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