dk.c revision 1.36 1 /* $NetBSD: dk.c,v 1.36 2008/04/06 08:33:31 cegger 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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.36 2008/04/06 08:33:31 cegger Exp $");
41
42 #include "opt_dkwedge.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/proc.h>
47 #include <sys/errno.h>
48 #include <sys/pool.h>
49 #include <sys/ioctl.h>
50 #include <sys/disklabel.h>
51 #include <sys/disk.h>
52 #include <sys/fcntl.h>
53 #include <sys/buf.h>
54 #include <sys/bufq.h>
55 #include <sys/vnode.h>
56 #include <sys/stat.h>
57 #include <sys/conf.h>
58 #include <sys/callout.h>
59 #include <sys/kernel.h>
60 #include <sys/malloc.h>
61 #include <sys/device.h>
62 #include <sys/kauth.h>
63
64 #include <miscfs/specfs/specdev.h>
65
66 MALLOC_DEFINE(M_DKWEDGE, "dkwedge", "Disk wedge structures");
67
68 typedef enum {
69 DKW_STATE_LARVAL = 0,
70 DKW_STATE_RUNNING = 1,
71 DKW_STATE_DYING = 2,
72 DKW_STATE_DEAD = 666
73 } dkwedge_state_t;
74
75 struct dkwedge_softc {
76 struct device *sc_dev; /* pointer to our pseudo-device */
77 struct cfdata sc_cfdata; /* our cfdata structure */
78 uint8_t sc_wname[128]; /* wedge name (Unicode, UTF-8) */
79
80 dkwedge_state_t sc_state; /* state this wedge is in */
81
82 struct disk *sc_parent; /* parent disk */
83 daddr_t sc_offset; /* LBA offset of wedge in parent */
84 uint64_t sc_size; /* size of wedge in blocks */
85 char sc_ptype[32]; /* partition type */
86 dev_t sc_pdev; /* cached parent's dev_t */
87 /* link on parent's wedge list */
88 LIST_ENTRY(dkwedge_softc) sc_plink;
89
90 struct disk sc_dk; /* our own disk structure */
91 struct bufq_state *sc_bufq; /* buffer queue */
92 struct callout sc_restart_ch; /* callout to restart I/O */
93
94 u_int sc_iopend; /* I/Os pending */
95 int sc_flags; /* flags (splbio) */
96 };
97
98 #define DK_F_WAIT_DRAIN 0x0001 /* waiting for I/O to drain */
99
100 static void dkstart(struct dkwedge_softc *);
101 static void dkiodone(struct buf *);
102 static void dkrestart(void *);
103
104 static dev_type_open(dkopen);
105 static dev_type_close(dkclose);
106 static dev_type_read(dkread);
107 static dev_type_write(dkwrite);
108 static dev_type_ioctl(dkioctl);
109 static dev_type_strategy(dkstrategy);
110 static dev_type_dump(dkdump);
111 static dev_type_size(dksize);
112
113 const struct bdevsw dk_bdevsw = {
114 dkopen, dkclose, dkstrategy, dkioctl, dkdump, dksize, D_DISK
115 };
116
117 const struct cdevsw dk_cdevsw = {
118 dkopen, dkclose, dkread, dkwrite, dkioctl,
119 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
120 };
121
122 static struct dkwedge_softc **dkwedges;
123 static u_int ndkwedges;
124 static krwlock_t dkwedges_lock;
125
126 static LIST_HEAD(, dkwedge_discovery_method) dkwedge_discovery_methods;
127 static krwlock_t dkwedge_discovery_methods_lock;
128
129 /*
130 * dkwedge_match:
131 *
132 * Autoconfiguration match function for pseudo-device glue.
133 */
134 static int
135 dkwedge_match(struct device *parent, struct cfdata *match,
136 void *aux)
137 {
138
139 /* Pseudo-device; always present. */
140 return (1);
141 }
142
143 /*
144 * dkwedge_attach:
145 *
146 * Autoconfiguration attach function for pseudo-device glue.
147 */
148 static void
149 dkwedge_attach(struct device *parent, struct device *self,
150 void *aux)
151 {
152
153 if (!pmf_device_register(self, NULL, NULL))
154 aprint_error_dev(self, "couldn't establish power handler\n");
155 }
156
157 /*
158 * dkwedge_detach:
159 *
160 * Autoconfiguration detach function for pseudo-device glue.
161 */
162 static int
163 dkwedge_detach(struct device *self, int flags)
164 {
165
166 pmf_device_deregister(self);
167 /* Always succeeds. */
168 return (0);
169 }
170
171 CFDRIVER_DECL(dk, DV_DISK, NULL);
172 CFATTACH_DECL_NEW(dk, 0,
173 dkwedge_match, dkwedge_attach, dkwedge_detach, NULL);
174
175 /*
176 * dkwedge_wait_drain:
177 *
178 * Wait for I/O on the wedge to drain.
179 * NOTE: Must be called at splbio()!
180 */
181 static void
182 dkwedge_wait_drain(struct dkwedge_softc *sc)
183 {
184
185 while (sc->sc_iopend != 0) {
186 sc->sc_flags |= DK_F_WAIT_DRAIN;
187 (void) tsleep(&sc->sc_iopend, PRIBIO, "dkdrn", 0);
188 }
189 }
190
191 /*
192 * dkwedge_compute_pdev:
193 *
194 * Compute the parent disk's dev_t.
195 */
196 static int
197 dkwedge_compute_pdev(const char *pname, dev_t *pdevp)
198 {
199 const char *name, *cp;
200 int punit, pmaj;
201 char devname[16];
202
203 name = pname;
204 if ((pmaj = devsw_name2blk(name, devname, sizeof(devname))) == -1)
205 return (ENODEV);
206
207 name += strlen(devname);
208 for (cp = name, punit = 0; *cp >= '0' && *cp <= '9'; cp++)
209 punit = (punit * 10) + (*cp - '0');
210 if (cp == name) {
211 /* Invalid parent disk name. */
212 return (ENODEV);
213 }
214
215 *pdevp = MAKEDISKDEV(pmaj, punit, RAW_PART);
216
217 return (0);
218 }
219
220 /*
221 * dkwedge_array_expand:
222 *
223 * Expand the dkwedges array.
224 */
225 static void
226 dkwedge_array_expand(void)
227 {
228 int newcnt = ndkwedges + 16;
229 struct dkwedge_softc **newarray, **oldarray;
230
231 newarray = malloc(newcnt * sizeof(*newarray), M_DKWEDGE,
232 M_WAITOK|M_ZERO);
233 if ((oldarray = dkwedges) != NULL)
234 memcpy(newarray, dkwedges, ndkwedges * sizeof(*newarray));
235 dkwedges = newarray;
236 ndkwedges = newcnt;
237 if (oldarray != NULL)
238 free(oldarray, M_DKWEDGE);
239 }
240
241 /*
242 * dkwedge_add: [exported function]
243 *
244 * Add a disk wedge based on the provided information.
245 *
246 * The incoming dkw_devname[] is ignored, instead being
247 * filled in and returned to the caller.
248 */
249 int
250 dkwedge_add(struct dkwedge_info *dkw)
251 {
252 struct dkwedge_softc *sc, *lsc;
253 struct disk *pdk;
254 u_int unit;
255 int error;
256 dev_t pdev;
257
258 dkw->dkw_parent[sizeof(dkw->dkw_parent) - 1] = '\0';
259 pdk = disk_find(dkw->dkw_parent);
260 if (pdk == NULL)
261 return (ENODEV);
262
263 error = dkwedge_compute_pdev(pdk->dk_name, &pdev);
264 if (error)
265 return (error);
266
267 if (dkw->dkw_offset < 0)
268 return (EINVAL);
269
270 sc = malloc(sizeof(*sc), M_DKWEDGE, M_WAITOK|M_ZERO);
271 sc->sc_state = DKW_STATE_LARVAL;
272 sc->sc_parent = pdk;
273 sc->sc_pdev = pdev;
274 sc->sc_offset = dkw->dkw_offset;
275 sc->sc_size = dkw->dkw_size;
276
277 memcpy(sc->sc_wname, dkw->dkw_wname, sizeof(sc->sc_wname));
278 sc->sc_wname[sizeof(sc->sc_wname) - 1] = '\0';
279
280 memcpy(sc->sc_ptype, dkw->dkw_ptype, sizeof(sc->sc_ptype));
281 sc->sc_ptype[sizeof(sc->sc_ptype) - 1] = '\0';
282
283 bufq_alloc(&sc->sc_bufq, "fcfs", 0);
284
285 callout_init(&sc->sc_restart_ch, 0);
286 callout_setfunc(&sc->sc_restart_ch, dkrestart, sc);
287
288 /*
289 * Wedge will be added; increment the wedge count for the parent.
290 * Only allow this to happend if RAW_PART is the only thing open.
291 */
292 mutex_enter(&pdk->dk_openlock);
293 if (pdk->dk_openmask & ~(1 << RAW_PART))
294 error = EBUSY;
295 else {
296 /* Check for wedge overlap. */
297 LIST_FOREACH(lsc, &pdk->dk_wedges, sc_plink) {
298 daddr_t lastblk = sc->sc_offset + sc->sc_size - 1;
299 daddr_t llastblk = lsc->sc_offset + lsc->sc_size - 1;
300
301 if (sc->sc_offset >= lsc->sc_offset &&
302 sc->sc_offset <= llastblk) {
303 /* Overlaps the tail of the exsiting wedge. */
304 break;
305 }
306 if (lastblk >= lsc->sc_offset &&
307 lastblk <= llastblk) {
308 /* Overlaps the head of the existing wedge. */
309 break;
310 }
311 }
312 if (lsc != NULL)
313 error = EINVAL;
314 else {
315 pdk->dk_nwedges++;
316 LIST_INSERT_HEAD(&pdk->dk_wedges, sc, sc_plink);
317 }
318 }
319 mutex_exit(&pdk->dk_openlock);
320 if (error) {
321 bufq_free(sc->sc_bufq);
322 free(sc, M_DKWEDGE);
323 return (error);
324 }
325
326 /* Fill in our cfdata for the pseudo-device glue. */
327 sc->sc_cfdata.cf_name = dk_cd.cd_name;
328 sc->sc_cfdata.cf_atname = dk_ca.ca_name;
329 /* sc->sc_cfdata.cf_unit set below */
330 sc->sc_cfdata.cf_fstate = FSTATE_STAR;
331
332 /* Insert the larval wedge into the array. */
333 rw_enter(&dkwedges_lock, RW_WRITER);
334 for (error = 0;;) {
335 struct dkwedge_softc **scpp;
336
337 /*
338 * Check for a duplicate wname while searching for
339 * a slot.
340 */
341 for (scpp = NULL, unit = 0; unit < ndkwedges; unit++) {
342 if (dkwedges[unit] == NULL) {
343 if (scpp == NULL) {
344 scpp = &dkwedges[unit];
345 sc->sc_cfdata.cf_unit = unit;
346 }
347 } else {
348 /* XXX Unicode. */
349 if (strcmp(dkwedges[unit]->sc_wname,
350 sc->sc_wname) == 0) {
351 error = EEXIST;
352 break;
353 }
354 }
355 }
356 if (error)
357 break;
358 KASSERT(unit == ndkwedges);
359 if (scpp == NULL)
360 dkwedge_array_expand();
361 else {
362 KASSERT(scpp == &dkwedges[sc->sc_cfdata.cf_unit]);
363 *scpp = sc;
364 break;
365 }
366 }
367 rw_exit(&dkwedges_lock);
368 if (error) {
369 mutex_enter(&pdk->dk_openlock);
370 pdk->dk_nwedges--;
371 LIST_REMOVE(sc, sc_plink);
372 mutex_exit(&pdk->dk_openlock);
373
374 bufq_free(sc->sc_bufq);
375 free(sc, M_DKWEDGE);
376 return (error);
377 }
378
379 /*
380 * Now that we know the unit #, attach a pseudo-device for
381 * this wedge instance. This will provide us with the
382 * "struct device" necessary for glue to other parts of the
383 * system.
384 *
385 * This should never fail, unless we're almost totally out of
386 * memory.
387 */
388 if ((sc->sc_dev = config_attach_pseudo(&sc->sc_cfdata)) == NULL) {
389 aprint_error("%s%u: unable to attach pseudo-device\n",
390 sc->sc_cfdata.cf_name, sc->sc_cfdata.cf_unit);
391
392 rw_enter(&dkwedges_lock, RW_WRITER);
393 dkwedges[sc->sc_cfdata.cf_unit] = NULL;
394 rw_exit(&dkwedges_lock);
395
396 mutex_enter(&pdk->dk_openlock);
397 pdk->dk_nwedges--;
398 LIST_REMOVE(sc, sc_plink);
399 mutex_exit(&pdk->dk_openlock);
400
401 bufq_free(sc->sc_bufq);
402 free(sc, M_DKWEDGE);
403 return (ENOMEM);
404 }
405
406 /* Return the devname to the caller. */
407 strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev),
408 sizeof(dkw->dkw_devname));
409
410 /*
411 * XXX Really ought to make the disk_attach() and the changing
412 * of state to RUNNING atomic.
413 */
414
415 disk_init(&sc->sc_dk, device_xname(sc->sc_dev), NULL);
416 disk_attach(&sc->sc_dk);
417
418 /* Disk wedge is ready for use! */
419 sc->sc_state = DKW_STATE_RUNNING;
420
421 /* Announce our arrival. */
422 aprint_normal("%s at %s: %s\n", device_xname(sc->sc_dev), pdk->dk_name,
423 sc->sc_wname); /* XXX Unicode */
424 aprint_normal("%s: %"PRIu64" blocks at %"PRId64", type: %s\n",
425 device_xname(sc->sc_dev), sc->sc_size, sc->sc_offset, sc->sc_ptype);
426
427 return (0);
428 }
429
430 /*
431 * dkwedge_del: [exported function]
432 *
433 * Delete a disk wedge based on the provided information.
434 * NOTE: We look up the wedge based on the wedge devname,
435 * not wname.
436 */
437 int
438 dkwedge_del(struct dkwedge_info *dkw)
439 {
440 struct dkwedge_softc *sc = NULL;
441 u_int unit;
442 int bmaj, cmaj, s;
443
444 /* Find our softc. */
445 dkw->dkw_devname[sizeof(dkw->dkw_devname) - 1] = '\0';
446 rw_enter(&dkwedges_lock, RW_WRITER);
447 for (unit = 0; unit < ndkwedges; unit++) {
448 if ((sc = dkwedges[unit]) != NULL &&
449 strcmp(device_xname(sc->sc_dev), dkw->dkw_devname) == 0 &&
450 strcmp(sc->sc_parent->dk_name, dkw->dkw_parent) == 0) {
451 /* Mark the wedge as dying. */
452 sc->sc_state = DKW_STATE_DYING;
453 break;
454 }
455 }
456 rw_exit(&dkwedges_lock);
457 if (unit == ndkwedges)
458 return (ESRCH);
459
460 KASSERT(sc != NULL);
461
462 /* Locate the wedge major numbers. */
463 bmaj = bdevsw_lookup_major(&dk_bdevsw);
464 cmaj = cdevsw_lookup_major(&dk_cdevsw);
465
466 /* Kill any pending restart. */
467 callout_stop(&sc->sc_restart_ch);
468
469 /*
470 * dkstart() will kill any queued buffers now that the
471 * state of the wedge is not RUNNING. Once we've done
472 * that, wait for any other pending I/O to complete.
473 */
474 s = splbio();
475 dkstart(sc);
476 dkwedge_wait_drain(sc);
477 splx(s);
478
479 /* Nuke the vnodes for any open instances. */
480 vdevgone(bmaj, unit, unit, VBLK);
481 vdevgone(cmaj, unit, unit, VCHR);
482
483 /* Clean up the parent. */
484 mutex_enter(&sc->sc_dk.dk_openlock);
485 mutex_enter(&sc->sc_parent->dk_rawlock);
486 if (sc->sc_dk.dk_openmask) {
487 if (sc->sc_parent->dk_rawopens-- == 1) {
488 KASSERT(sc->sc_parent->dk_rawvp != NULL);
489 (void) vn_close(sc->sc_parent->dk_rawvp, FREAD | FWRITE,
490 NOCRED);
491 sc->sc_parent->dk_rawvp = NULL;
492 }
493 sc->sc_dk.dk_openmask = 0;
494 }
495 mutex_exit(&sc->sc_parent->dk_rawlock);
496 mutex_exit(&sc->sc_dk.dk_openlock);
497
498 /* Announce our departure. */
499 aprint_normal("%s at %s (%s) deleted\n", device_xname(sc->sc_dev),
500 sc->sc_parent->dk_name,
501 sc->sc_wname); /* XXX Unicode */
502
503 /* Delete our pseudo-device. */
504 (void) config_detach(sc->sc_dev, DETACH_FORCE | DETACH_QUIET);
505
506 mutex_enter(&sc->sc_parent->dk_openlock);
507 sc->sc_parent->dk_nwedges--;
508 LIST_REMOVE(sc, sc_plink);
509 mutex_exit(&sc->sc_parent->dk_openlock);
510
511 /* Delete our buffer queue. */
512 bufq_free(sc->sc_bufq);
513
514 /* Detach from the disk list. */
515 disk_detach(&sc->sc_dk);
516
517 /* Poof. */
518 rw_enter(&dkwedges_lock, RW_WRITER);
519 dkwedges[unit] = NULL;
520 sc->sc_state = DKW_STATE_DEAD;
521 rw_exit(&dkwedges_lock);
522
523 free(sc, M_DKWEDGE);
524
525 return (0);
526 }
527
528 /*
529 * dkwedge_delall: [exported function]
530 *
531 * Delete all of the wedges on the specified disk. Used when
532 * a disk is being detached.
533 */
534 void
535 dkwedge_delall(struct disk *pdk)
536 {
537 struct dkwedge_info dkw;
538 struct dkwedge_softc *sc;
539
540 for (;;) {
541 mutex_enter(&pdk->dk_openlock);
542 if ((sc = LIST_FIRST(&pdk->dk_wedges)) == NULL) {
543 KASSERT(pdk->dk_nwedges == 0);
544 mutex_exit(&pdk->dk_openlock);
545 return;
546 }
547 strcpy(dkw.dkw_parent, pdk->dk_name);
548 strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev),
549 sizeof(dkw.dkw_devname));
550 mutex_exit(&pdk->dk_openlock);
551 (void) dkwedge_del(&dkw);
552 }
553 }
554
555 /*
556 * dkwedge_list: [exported function]
557 *
558 * List all of the wedges on a particular disk.
559 * If p == NULL, the buffer is in kernel space. Otherwise, it is
560 * in user space of the specified process.
561 */
562 int
563 dkwedge_list(struct disk *pdk, struct dkwedge_list *dkwl, struct lwp *l)
564 {
565 struct uio uio;
566 struct iovec iov;
567 struct dkwedge_softc *sc;
568 struct dkwedge_info dkw;
569 struct vmspace *vm;
570 int error = 0;
571
572 iov.iov_base = dkwl->dkwl_buf;
573 iov.iov_len = dkwl->dkwl_bufsize;
574
575 uio.uio_iov = &iov;
576 uio.uio_iovcnt = 1;
577 uio.uio_offset = 0;
578 uio.uio_resid = dkwl->dkwl_bufsize;
579 uio.uio_rw = UIO_READ;
580 if (l == NULL) {
581 UIO_SETUP_SYSSPACE(&uio);
582 } else {
583 error = proc_vmspace_getref(l->l_proc, &vm);
584 if (error) {
585 return error;
586 }
587 uio.uio_vmspace = vm;
588 }
589
590 dkwl->dkwl_ncopied = 0;
591
592 mutex_enter(&pdk->dk_openlock);
593 LIST_FOREACH(sc, &pdk->dk_wedges, sc_plink) {
594 if (uio.uio_resid < sizeof(dkw))
595 break;
596
597 if (sc->sc_state != DKW_STATE_RUNNING)
598 continue;
599
600 strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev),
601 sizeof(dkw.dkw_devname));
602 memcpy(dkw.dkw_wname, sc->sc_wname, sizeof(dkw.dkw_wname));
603 dkw.dkw_wname[sizeof(dkw.dkw_wname) - 1] = '\0';
604 strcpy(dkw.dkw_parent, sc->sc_parent->dk_name);
605 dkw.dkw_offset = sc->sc_offset;
606 dkw.dkw_size = sc->sc_size;
607 strcpy(dkw.dkw_ptype, sc->sc_ptype);
608
609 error = uiomove(&dkw, sizeof(dkw), &uio);
610 if (error)
611 break;
612 dkwl->dkwl_ncopied++;
613 }
614 dkwl->dkwl_nwedges = pdk->dk_nwedges;
615 mutex_exit(&pdk->dk_openlock);
616
617 if (l != NULL) {
618 uvmspace_free(vm);
619 }
620
621 return (error);
622 }
623
624 device_t
625 dkwedge_find_by_wname(const char *wname)
626 {
627 device_t dv = NULL;
628 struct dkwedge_softc *sc;
629 int i;
630
631 rw_enter(&dkwedges_lock, RW_WRITER);
632 for (i = 0; i < ndkwedges; i++) {
633 if ((sc = dkwedges[i]) == NULL)
634 continue;
635 if (strcmp(sc->sc_wname, wname) == 0) {
636 if (dv != NULL) {
637 printf(
638 "WARNING: double match for wedge name %s "
639 "(%s, %s)\n", wname, device_xname(dv),
640 device_xname(sc->sc_dev));
641 continue;
642 }
643 dv = sc->sc_dev;
644 }
645 }
646 rw_exit(&dkwedges_lock);
647 return dv;
648 }
649
650 void
651 dkwedge_print_wnames(void)
652 {
653 struct dkwedge_softc *sc;
654 int i;
655
656 rw_enter(&dkwedges_lock, RW_WRITER);
657 for (i = 0; i < ndkwedges; i++) {
658 if ((sc = dkwedges[i]) == NULL)
659 continue;
660 printf(" wedge:%s", sc->sc_wname);
661 }
662 rw_exit(&dkwedges_lock);
663 }
664
665 /*
666 * dkwedge_set_bootwedge
667 *
668 * Set the booted_wedge global based on the specified parent name
669 * and offset/length.
670 */
671 void
672 dkwedge_set_bootwedge(struct device *parent, daddr_t startblk, uint64_t nblks)
673 {
674 struct dkwedge_softc *sc;
675 int i;
676
677 rw_enter(&dkwedges_lock, RW_WRITER);
678 for (i = 0; i < ndkwedges; i++) {
679 if ((sc = dkwedges[i]) == NULL)
680 continue;
681 if (strcmp(sc->sc_parent->dk_name, device_xname(parent)) == 0 &&
682 sc->sc_offset == startblk &&
683 sc->sc_size == nblks) {
684 if (booted_wedge) {
685 printf("WARNING: double match for boot wedge "
686 "(%s, %s)\n",
687 device_xname(booted_wedge),
688 device_xname(sc->sc_dev));
689 continue;
690 }
691 booted_device = parent;
692 booted_wedge = sc->sc_dev;
693 booted_partition = 0;
694 }
695 }
696 /*
697 * XXX What if we don't find one? Should we create a special
698 * XXX root wedge?
699 */
700 rw_exit(&dkwedges_lock);
701 }
702
703 /*
704 * We need a dummy object to stuff into the dkwedge discovery method link
705 * set to ensure that there is always at least one object in the set.
706 */
707 static struct dkwedge_discovery_method dummy_discovery_method;
708 __link_set_add_bss(dkwedge_methods, dummy_discovery_method);
709
710 /*
711 * dkwedge_init:
712 *
713 * Initialize the disk wedge subsystem.
714 */
715 void
716 dkwedge_init(void)
717 {
718 __link_set_decl(dkwedge_methods, struct dkwedge_discovery_method);
719 struct dkwedge_discovery_method * const *ddmp;
720 struct dkwedge_discovery_method *lddm, *ddm;
721
722 rw_init(&dkwedges_lock);
723 rw_init(&dkwedge_discovery_methods_lock);
724
725 if (config_cfdriver_attach(&dk_cd) != 0)
726 panic("dkwedge: unable to attach cfdriver");
727 if (config_cfattach_attach(dk_cd.cd_name, &dk_ca) != 0)
728 panic("dkwedge: unable to attach cfattach");
729
730 rw_enter(&dkwedge_discovery_methods_lock, RW_WRITER);
731
732 LIST_INIT(&dkwedge_discovery_methods);
733
734 __link_set_foreach(ddmp, dkwedge_methods) {
735 ddm = *ddmp;
736 if (ddm == &dummy_discovery_method)
737 continue;
738 if (LIST_EMPTY(&dkwedge_discovery_methods)) {
739 LIST_INSERT_HEAD(&dkwedge_discovery_methods,
740 ddm, ddm_list);
741 continue;
742 }
743 LIST_FOREACH(lddm, &dkwedge_discovery_methods, ddm_list) {
744 if (ddm->ddm_priority == lddm->ddm_priority) {
745 aprint_error("dk-method-%s: method \"%s\" "
746 "already exists at priority %d\n",
747 ddm->ddm_name, lddm->ddm_name,
748 lddm->ddm_priority);
749 /* Not inserted. */
750 break;
751 }
752 if (ddm->ddm_priority < lddm->ddm_priority) {
753 /* Higher priority; insert before. */
754 LIST_INSERT_BEFORE(lddm, ddm, ddm_list);
755 break;
756 }
757 if (LIST_NEXT(lddm, ddm_list) == NULL) {
758 /* Last one; insert after. */
759 KASSERT(lddm->ddm_priority < ddm->ddm_priority);
760 LIST_INSERT_AFTER(lddm, ddm, ddm_list);
761 break;
762 }
763 }
764 }
765
766 rw_exit(&dkwedge_discovery_methods_lock);
767 }
768
769 #ifdef DKWEDGE_AUTODISCOVER
770 int dkwedge_autodiscover = 1;
771 #else
772 int dkwedge_autodiscover = 0;
773 #endif
774
775 /*
776 * dkwedge_discover: [exported function]
777 *
778 * Discover the wedges on a newly attached disk.
779 */
780 void
781 dkwedge_discover(struct disk *pdk)
782 {
783 struct dkwedge_discovery_method *ddm;
784 struct vnode *vp;
785 int error;
786 dev_t pdev;
787
788 /*
789 * Require people playing with wedges to enable this explicitly.
790 */
791 if (dkwedge_autodiscover == 0)
792 return;
793
794 rw_enter(&dkwedge_discovery_methods_lock, RW_READER);
795
796 error = dkwedge_compute_pdev(pdk->dk_name, &pdev);
797 if (error) {
798 aprint_error("%s: unable to compute pdev, error = %d\n",
799 pdk->dk_name, error);
800 goto out;
801 }
802
803 error = bdevvp(pdev, &vp);
804 if (error) {
805 aprint_error("%s: unable to find vnode for pdev, error = %d\n",
806 pdk->dk_name, error);
807 goto out;
808 }
809
810 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
811 if (error) {
812 aprint_error("%s: unable to lock vnode for pdev, error = %d\n",
813 pdk->dk_name, error);
814 vrele(vp);
815 goto out;
816 }
817
818 error = VOP_OPEN(vp, FREAD, NOCRED);
819 if (error) {
820 aprint_error("%s: unable to open device, error = %d\n",
821 pdk->dk_name, error);
822 vput(vp);
823 goto out;
824 }
825 VOP_UNLOCK(vp, 0);
826
827 /*
828 * For each supported partition map type, look to see if
829 * this map type exists. If so, parse it and add the
830 * corresponding wedges.
831 */
832 LIST_FOREACH(ddm, &dkwedge_discovery_methods, ddm_list) {
833 error = (*ddm->ddm_discover)(pdk, vp);
834 if (error == 0) {
835 /* Successfully created wedges; we're done. */
836 break;
837 }
838 }
839
840 error = vn_close(vp, FREAD, NOCRED);
841 if (error) {
842 aprint_error("%s: unable to close device, error = %d\n",
843 pdk->dk_name, error);
844 /* We'll just assume the vnode has been cleaned up. */
845 }
846 out:
847 rw_exit(&dkwedge_discovery_methods_lock);
848 }
849
850 /*
851 * dkwedge_read:
852 *
853 * Read the some data from the specified disk, used for
854 * partition discovery.
855 */
856 int
857 dkwedge_read(struct disk *pdk, struct vnode *vp, daddr_t blkno,
858 void *tbuf, size_t len)
859 {
860 struct buf b;
861
862 buf_init(&b);
863
864 b.b_vp = vp;
865 b.b_dev = vp->v_rdev;
866 b.b_blkno = blkno;
867 b.b_bcount = b.b_resid = len;
868 b.b_flags = B_READ;
869 b.b_proc = curproc;
870 b.b_data = tbuf;
871
872 VOP_STRATEGY(vp, &b);
873 return (biowait(&b));
874 }
875
876 /*
877 * dkwedge_lookup:
878 *
879 * Look up a dkwedge_softc based on the provided dev_t.
880 */
881 static struct dkwedge_softc *
882 dkwedge_lookup(dev_t dev)
883 {
884 int unit = minor(dev);
885
886 if (unit >= ndkwedges)
887 return (NULL);
888
889 KASSERT(dkwedges != NULL);
890
891 return (dkwedges[unit]);
892 }
893
894 /*
895 * dkopen: [devsw entry point]
896 *
897 * Open a wedge.
898 */
899 static int
900 dkopen(dev_t dev, int flags, int fmt, struct lwp *l)
901 {
902 struct dkwedge_softc *sc = dkwedge_lookup(dev);
903 struct vnode *vp;
904 int error = 0;
905
906 if (sc == NULL)
907 return (ENODEV);
908
909 if (sc->sc_state != DKW_STATE_RUNNING)
910 return (ENXIO);
911
912 /*
913 * We go through a complicated little dance to only open the parent
914 * vnode once per wedge, no matter how many times the wedge is
915 * opened. The reason? We see one dkopen() per open call, but
916 * only dkclose() on the last close.
917 */
918 mutex_enter(&sc->sc_dk.dk_openlock);
919 mutex_enter(&sc->sc_parent->dk_rawlock);
920 if (sc->sc_dk.dk_openmask == 0) {
921 if (sc->sc_parent->dk_rawopens == 0) {
922 KASSERT(sc->sc_parent->dk_rawvp == NULL);
923 error = bdevvp(sc->sc_pdev, &vp);
924 if (error)
925 goto popen_fail;
926 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
927 if (error) {
928 vrele(vp);
929 goto popen_fail;
930 }
931 error = VOP_OPEN(vp, FREAD | FWRITE, NOCRED);
932 if (error) {
933 vput(vp);
934 goto popen_fail;
935 }
936 /* VOP_OPEN() doesn't do this for us. */
937 mutex_enter(&vp->v_interlock);
938 vp->v_writecount++;
939 mutex_exit(&vp->v_interlock);
940 VOP_UNLOCK(vp, 0);
941 sc->sc_parent->dk_rawvp = vp;
942 }
943 sc->sc_parent->dk_rawopens++;
944 }
945 if (fmt == S_IFCHR)
946 sc->sc_dk.dk_copenmask |= 1;
947 else
948 sc->sc_dk.dk_bopenmask |= 1;
949 sc->sc_dk.dk_openmask =
950 sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
951
952 popen_fail:
953 mutex_exit(&sc->sc_parent->dk_rawlock);
954 mutex_exit(&sc->sc_dk.dk_openlock);
955 return (error);
956 }
957
958 /*
959 * dkclose: [devsw entry point]
960 *
961 * Close a wedge.
962 */
963 static int
964 dkclose(dev_t dev, int flags, int fmt, struct lwp *l)
965 {
966 struct dkwedge_softc *sc = dkwedge_lookup(dev);
967 int error = 0;
968
969 KASSERT(sc->sc_dk.dk_openmask != 0);
970
971 mutex_enter(&sc->sc_dk.dk_openlock);
972 mutex_enter(&sc->sc_parent->dk_rawlock);
973
974 if (fmt == S_IFCHR)
975 sc->sc_dk.dk_copenmask &= ~1;
976 else
977 sc->sc_dk.dk_bopenmask &= ~1;
978 sc->sc_dk.dk_openmask =
979 sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
980
981 if (sc->sc_dk.dk_openmask == 0) {
982 if (sc->sc_parent->dk_rawopens-- == 1) {
983 KASSERT(sc->sc_parent->dk_rawvp != NULL);
984 error = vn_close(sc->sc_parent->dk_rawvp,
985 FREAD | FWRITE, NOCRED);
986 sc->sc_parent->dk_rawvp = NULL;
987 }
988 }
989
990 mutex_exit(&sc->sc_parent->dk_rawlock);
991 mutex_exit(&sc->sc_dk.dk_openlock);
992
993 return (error);
994 }
995
996 /*
997 * dkstragegy: [devsw entry point]
998 *
999 * Perform I/O based on the wedge I/O strategy.
1000 */
1001 static void
1002 dkstrategy(struct buf *bp)
1003 {
1004 struct dkwedge_softc *sc = dkwedge_lookup(bp->b_dev);
1005 int s;
1006
1007 if (sc->sc_state != DKW_STATE_RUNNING) {
1008 bp->b_error = ENXIO;
1009 goto done;
1010 }
1011
1012 /* If it's an empty transfer, wake up the top half now. */
1013 if (bp->b_bcount == 0)
1014 goto done;
1015
1016 /* Make sure it's in-range. */
1017 if (bounds_check_with_mediasize(bp, DEV_BSIZE, sc->sc_size) <= 0)
1018 goto done;
1019
1020 /* Translate it to the parent's raw LBA. */
1021 bp->b_rawblkno = bp->b_blkno + sc->sc_offset;
1022
1023 /* Place it in the queue and start I/O on the unit. */
1024 s = splbio();
1025 sc->sc_iopend++;
1026 BUFQ_PUT(sc->sc_bufq, bp);
1027 dkstart(sc);
1028 splx(s);
1029 return;
1030
1031 done:
1032 bp->b_resid = bp->b_bcount;
1033 biodone(bp);
1034 }
1035
1036 /*
1037 * dkstart:
1038 *
1039 * Start I/O that has been enqueued on the wedge.
1040 * NOTE: Must be called at splbio()!
1041 */
1042 static void
1043 dkstart(struct dkwedge_softc *sc)
1044 {
1045 struct vnode *vp;
1046 struct buf *bp, *nbp;
1047
1048 /* Do as much work as has been enqueued. */
1049 while ((bp = BUFQ_PEEK(sc->sc_bufq)) != NULL) {
1050 if (sc->sc_state != DKW_STATE_RUNNING) {
1051 (void) BUFQ_GET(sc->sc_bufq);
1052 if (sc->sc_iopend-- == 1 &&
1053 (sc->sc_flags & DK_F_WAIT_DRAIN) != 0) {
1054 sc->sc_flags &= ~DK_F_WAIT_DRAIN;
1055 wakeup(&sc->sc_iopend);
1056 }
1057 bp->b_error = ENXIO;
1058 bp->b_resid = bp->b_bcount;
1059 biodone(bp);
1060 }
1061
1062 /* Instrumentation. */
1063 disk_busy(&sc->sc_dk);
1064
1065 nbp = getiobuf(sc->sc_parent->dk_rawvp, false);
1066 if (nbp == NULL) {
1067 /*
1068 * No resources to run this request; leave the
1069 * buffer queued up, and schedule a timer to
1070 * restart the queue in 1/2 a second.
1071 */
1072 disk_unbusy(&sc->sc_dk, 0, bp->b_flags & B_READ);
1073 callout_schedule(&sc->sc_restart_ch, hz / 2);
1074 return;
1075 }
1076
1077 (void) BUFQ_GET(sc->sc_bufq);
1078
1079 nbp->b_data = bp->b_data;
1080 nbp->b_flags = bp->b_flags;
1081 nbp->b_oflags = bp->b_oflags;
1082 nbp->b_cflags = bp->b_cflags;
1083 nbp->b_iodone = dkiodone;
1084 nbp->b_proc = bp->b_proc;
1085 nbp->b_blkno = bp->b_rawblkno;
1086 nbp->b_dev = sc->sc_parent->dk_rawvp->v_rdev;
1087 nbp->b_bcount = bp->b_bcount;
1088 nbp->b_private = bp;
1089 BIO_COPYPRIO(nbp, bp);
1090
1091 vp = nbp->b_vp;
1092 if ((nbp->b_flags & B_READ) == 0) {
1093 mutex_enter(&vp->v_interlock);
1094 vp->v_numoutput++;
1095 mutex_exit(&vp->v_interlock);
1096 }
1097 VOP_STRATEGY(vp, nbp);
1098 }
1099 }
1100
1101 /*
1102 * dkiodone:
1103 *
1104 * I/O to a wedge has completed; alert the top half.
1105 * NOTE: Must be called at splbio()!
1106 */
1107 static void
1108 dkiodone(struct buf *bp)
1109 {
1110 struct buf *obp = bp->b_private;
1111 struct dkwedge_softc *sc = dkwedge_lookup(obp->b_dev);
1112
1113 if (bp->b_error != 0)
1114 obp->b_error = bp->b_error;
1115 obp->b_resid = bp->b_resid;
1116 putiobuf(bp);
1117
1118 if (sc->sc_iopend-- == 1 && (sc->sc_flags & DK_F_WAIT_DRAIN) != 0) {
1119 sc->sc_flags &= ~DK_F_WAIT_DRAIN;
1120 wakeup(&sc->sc_iopend);
1121 }
1122
1123 disk_unbusy(&sc->sc_dk, obp->b_bcount - obp->b_resid,
1124 obp->b_flags & B_READ);
1125
1126 biodone(obp);
1127
1128 /* Kick the queue in case there is more work we can do. */
1129 dkstart(sc);
1130 }
1131
1132 /*
1133 * dkrestart:
1134 *
1135 * Restart the work queue after it was stalled due to
1136 * a resource shortage. Invoked via a callout.
1137 */
1138 static void
1139 dkrestart(void *v)
1140 {
1141 struct dkwedge_softc *sc = v;
1142 int s;
1143
1144 s = splbio();
1145 dkstart(sc);
1146 splx(s);
1147 }
1148
1149 /*
1150 * dkread: [devsw entry point]
1151 *
1152 * Read from a wedge.
1153 */
1154 static int
1155 dkread(dev_t dev, struct uio *uio, int flags)
1156 {
1157 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1158
1159 if (sc->sc_state != DKW_STATE_RUNNING)
1160 return (ENXIO);
1161
1162 return (physio(dkstrategy, NULL, dev, B_READ,
1163 sc->sc_parent->dk_driver->d_minphys, uio));
1164 }
1165
1166 /*
1167 * dkwrite: [devsw entry point]
1168 *
1169 * Write to a wedge.
1170 */
1171 static int
1172 dkwrite(dev_t dev, struct uio *uio, int flags)
1173 {
1174 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1175
1176 if (sc->sc_state != DKW_STATE_RUNNING)
1177 return (ENXIO);
1178
1179 return (physio(dkstrategy, NULL, dev, B_WRITE,
1180 sc->sc_parent->dk_driver->d_minphys, uio));
1181 }
1182
1183 /*
1184 * dkioctl: [devsw entry point]
1185 *
1186 * Perform an ioctl request on a wedge.
1187 */
1188 static int
1189 dkioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
1190 {
1191 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1192 int error = 0;
1193
1194 if (sc->sc_state != DKW_STATE_RUNNING)
1195 return (ENXIO);
1196
1197 switch (cmd) {
1198 case DIOCCACHESYNC:
1199 /*
1200 * XXX Do we really need to care about having a writable
1201 * file descriptor here?
1202 */
1203 if ((flag & FWRITE) == 0)
1204 error = EBADF;
1205 else
1206 error = VOP_IOCTL(sc->sc_parent->dk_rawvp,
1207 cmd, data, flag,
1208 l != NULL ? l->l_cred : NOCRED);
1209 break;
1210 case DIOCGWEDGEINFO:
1211 {
1212 struct dkwedge_info *dkw = (void *) data;
1213
1214 strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev),
1215 sizeof(dkw->dkw_devname));
1216 memcpy(dkw->dkw_wname, sc->sc_wname, sizeof(dkw->dkw_wname));
1217 dkw->dkw_wname[sizeof(dkw->dkw_wname) - 1] = '\0';
1218 strcpy(dkw->dkw_parent, sc->sc_parent->dk_name);
1219 dkw->dkw_offset = sc->sc_offset;
1220 dkw->dkw_size = sc->sc_size;
1221 strcpy(dkw->dkw_ptype, sc->sc_ptype);
1222
1223 break;
1224 }
1225
1226 default:
1227 error = ENOTTY;
1228 }
1229
1230 return (error);
1231 }
1232
1233 /*
1234 * dksize: [devsw entry point]
1235 *
1236 * Query the size of a wedge for the purpose of performing a dump
1237 * or for swapping to.
1238 */
1239 static int
1240 dksize(dev_t dev)
1241 {
1242 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1243 int rv = -1;
1244
1245 if (sc == NULL)
1246 return (-1);
1247
1248 if (sc->sc_state != DKW_STATE_RUNNING)
1249 return (ENXIO);
1250
1251 mutex_enter(&sc->sc_dk.dk_openlock);
1252 mutex_enter(&sc->sc_parent->dk_rawlock);
1253
1254 /* Our content type is static, no need to open the device. */
1255
1256 if (strcmp(sc->sc_ptype, DKW_PTYPE_SWAP) == 0) {
1257 /* Saturate if we are larger than INT_MAX. */
1258 if (sc->sc_size > INT_MAX)
1259 rv = INT_MAX;
1260 else
1261 rv = (int) sc->sc_size;
1262 }
1263
1264 mutex_exit(&sc->sc_parent->dk_rawlock);
1265 mutex_exit(&sc->sc_dk.dk_openlock);
1266
1267 return (rv);
1268 }
1269
1270 /*
1271 * dkdump: [devsw entry point]
1272 *
1273 * Perform a crash dump to a wedge.
1274 */
1275 static int
1276 dkdump(dev_t dev, daddr_t blkno, void *va, size_t size)
1277 {
1278 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1279 const struct bdevsw *bdev;
1280 int rv = 0;
1281
1282 if (sc == NULL)
1283 return (-1);
1284
1285 if (sc->sc_state != DKW_STATE_RUNNING)
1286 return (ENXIO);
1287
1288 mutex_enter(&sc->sc_dk.dk_openlock);
1289 mutex_enter(&sc->sc_parent->dk_rawlock);
1290
1291 /* Our content type is static, no need to open the device. */
1292
1293 if (strcmp(sc->sc_ptype, DKW_PTYPE_SWAP) != 0) {
1294 rv = ENXIO;
1295 goto out;
1296 }
1297 if (size % DEV_BSIZE != 0) {
1298 rv = EINVAL;
1299 goto out;
1300 }
1301 if (blkno + size / DEV_BSIZE > sc->sc_size) {
1302 printf("%s: blkno (%" PRIu64 ") + size / DEV_BSIZE (%zu) > "
1303 "sc->sc_size (%" PRIu64 ")\n", __func__, blkno,
1304 size / DEV_BSIZE, sc->sc_size);
1305 rv = EINVAL;
1306 goto out;
1307 }
1308
1309 bdev = bdevsw_lookup(sc->sc_pdev);
1310 rv = (*bdev->d_dump)(sc->sc_pdev, blkno + sc->sc_offset, va, size);
1311
1312 out:
1313 mutex_exit(&sc->sc_parent->dk_rawlock);
1314 mutex_exit(&sc->sc_dk.dk_openlock);
1315
1316 return rv;
1317 }
1318