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