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