dk.c revision 1.1 1 /* $NetBSD: dk.c,v 1.1 2004/10/04 01:07:25 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2004 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.1 2004/10/04 01:07:25 thorpej 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/vnode.h>
54 #include <sys/conf.h>
55 #include <sys/callout.h>
56 #include <sys/kernel.h>
57 #include <sys/lock.h>
58 #include <sys/malloc.h>
59
60 #include <miscfs/specfs/specdev.h>
61
62 MALLOC_DEFINE(M_DKWEDGE, "dkwedge", "Disk wedge structures");
63
64 typedef enum {
65 DKW_STATE_LARVAL = 0,
66 DKW_STATE_RUNNING = 1,
67 DKW_STATE_DYING = 2,
68 DKW_STATE_DEAD = 666
69 } dkwedge_state_t;
70
71 struct dkwedge_softc {
72 char sc_devname[16]; /* device-style name (e.g. "dk0") */
73 uint8_t sc_wname[128]; /* wedge name (Unicode, UTF-8) */
74 u_int sc_unit; /* our unit # */
75
76 dkwedge_state_t sc_state; /* state this wedge is in */
77
78 struct disk *sc_parent; /* parent disk */
79 daddr_t sc_offset; /* LBA offset of wedge in parent */
80 uint64_t sc_size; /* size of wedge in blocks */
81 char sc_ptype[32]; /* partition type */
82 dev_t sc_pdev; /* cached parent's dev_t */
83 /* link on parent's wedge list */
84 LIST_ENTRY(dkwedge_softc) sc_plink;
85
86 int sc_open; /* locked by parent's rawlock */
87
88 struct disk sc_dk; /* our own disk structure */
89 struct bufq_state sc_bufq; /* buffer queue */
90 struct callout sc_restart_ch; /* callout to restart I/O */
91
92 u_int sc_iopend; /* I/Os pending */
93 int sc_flags; /* flags (splbio) */
94 };
95
96 #define DK_F_WAIT_DRAIN 0x0001 /* waiting for I/O to drain */
97
98 static void dkstart(struct dkwedge_softc *);
99 static void dkiodone(struct buf *);
100 static void dkrestart(void *);
101
102 static dev_type_open(dkopen);
103 static dev_type_close(dkclose);
104 static dev_type_read(dkread);
105 static dev_type_write(dkwrite);
106 static dev_type_ioctl(dkioctl);
107 static dev_type_strategy(dkstrategy);
108 static dev_type_dump(dkdump);
109 static dev_type_size(dksize);
110
111 const struct bdevsw dk_bdevsw = {
112 dkopen, dkclose, dkstrategy, dkioctl, dkdump, dksize, D_DISK
113 };
114
115 const struct cdevsw dk_cdevsw = {
116 dkopen, dkclose, dkread, dkwrite, dkioctl,
117 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
118 };
119
120 static struct dkwedge_softc **dkwedges;
121 static u_int ndkwedges;
122 static struct lock dkwedges_lock = LOCK_INITIALIZER(PRIBIO, "dkwgs", 0, 0);
123
124 static LIST_HEAD(, dkwedge_discovery_method) dkwedge_discovery_methods;
125 static int dkwedge_discovery_methods_initialized;
126 static struct lock dkwedge_discovery_methods_lock =
127 LOCK_INITIALIZER(PRIBIO, "dkddm", 0, 0);
128
129 /*
130 * dkwedge_wait_drain:
131 *
132 * Wait for I/O on the wedge to drain.
133 * NOTE: Must be called at splbio()!
134 */
135 static void
136 dkwedge_wait_drain(struct dkwedge_softc *sc)
137 {
138
139 while (sc->sc_iopend != 0) {
140 sc->sc_flags |= DK_F_WAIT_DRAIN;
141 (void) tsleep(&sc->sc_iopend, PRIBIO, "dkdrn", 0);
142 }
143 }
144
145 /*
146 * dkwedge_compute_pdev:
147 *
148 * Compute the parent disk's dev_t.
149 */
150 static int
151 dkwedge_compute_pdev(const char *pname, dev_t *pdevp)
152 {
153 const char *name, *cp;
154 int punit, pmaj;
155 char devname[16];
156
157 name = pname;
158 if ((pmaj = devsw_name2blk(name, devname, sizeof(devname))) == -1)
159 return (ENODEV);
160
161 name += strlen(devname);
162 for (cp = name, punit = 0; *cp >= '0' && *cp <= '9'; cp++)
163 punit = (punit * 10) + (*cp - '0');
164 if (cp == name) {
165 /* Invalid parent disk name. */
166 return (ENODEV);
167 }
168
169 *pdevp = MAKEDISKDEV(pmaj, punit, RAW_PART);
170
171 return (0);
172 }
173
174 /*
175 * dkwedge_array_expand:
176 *
177 * Expand the dkwedges array.
178 */
179 static void
180 dkwedge_array_expand(void)
181 {
182 int newcnt = ndkwedges + 16;
183 struct dkwedge_softc **newarray, **oldarray;
184
185 newarray = malloc(newcnt * sizeof(*newarray), M_DKWEDGE,
186 M_WAITOK|M_ZERO);
187 if ((oldarray = dkwedges) != NULL)
188 memcpy(newarray, dkwedges, ndkwedges * sizeof(*newarray));
189 dkwedges = newarray;
190 ndkwedges = newcnt;
191 if (oldarray != NULL)
192 free(oldarray, M_DKWEDGE);
193 }
194
195 /*
196 * dkwedge_add: [exported function]
197 *
198 * Add a disk wedge based on the provided information.
199 *
200 * The incoming dkw_devname[] is ignored, instead being
201 * filled in and returned to the caller.
202 */
203 int
204 dkwedge_add(struct dkwedge_info *dkw)
205 {
206 struct dkwedge_softc *sc, *lsc;
207 struct disk *pdk;
208 u_int unit;
209 int error;
210 dev_t pdev;
211
212 dkw->dkw_parent[sizeof(dkw->dkw_parent) - 1] = '\0';
213 pdk = disk_find(dkw->dkw_parent);
214 if (pdk == NULL)
215 return (ENODEV);
216
217 error = dkwedge_compute_pdev(pdk->dk_name, &pdev);
218 if (error)
219 return (error);
220
221 if (dkw->dkw_offset < 0)
222 return (EINVAL);
223
224 sc = malloc(sizeof(*sc), M_DKWEDGE, M_WAITOK|M_ZERO);
225 sc->sc_state = DKW_STATE_LARVAL;
226 sc->sc_parent = pdk;
227 sc->sc_pdev = pdev;
228 sc->sc_offset = dkw->dkw_offset;
229 sc->sc_size = dkw->dkw_size;
230
231 memcpy(sc->sc_wname, dkw->dkw_wname, sizeof(sc->sc_wname));
232 sc->sc_wname[sizeof(sc->sc_wname) - 1] = '\0';
233
234 memcpy(sc->sc_ptype, dkw->dkw_ptype, sizeof(sc->sc_ptype));
235 sc->sc_ptype[sizeof(sc->sc_ptype) - 1] = '\0';
236
237 bufq_alloc(&sc->sc_bufq, BUFQ_FCFS);
238
239 callout_init(&sc->sc_restart_ch);
240 callout_setfunc(&sc->sc_restart_ch, dkrestart, sc);
241
242 /*
243 * Wedge will be added; increment the wedge count for the parent.
244 * Only allow this to happend if RAW_PART is the only thing open.
245 */
246 (void) lockmgr(&pdk->dk_openlock, LK_EXCLUSIVE, NULL);
247 if (pdk->dk_openmask & ~(1 << RAW_PART))
248 error = EBUSY;
249 else {
250 /* Check for wedge overlap. */
251 LIST_FOREACH(lsc, &pdk->dk_wedges, sc_plink) {
252 daddr_t lastblk = sc->sc_offset + sc->sc_size - 1;
253 daddr_t llastblk = lsc->sc_offset + lsc->sc_size - 1;
254
255 if (sc->sc_offset >= lsc->sc_offset &&
256 sc->sc_offset <= llastblk) {
257 /* Overlaps the tail of the exsiting wedge. */
258 break;
259 }
260 if (lastblk >= lsc->sc_offset &&
261 lastblk <= llastblk) {
262 /* Overlaps the head of the existing wedge. */
263 break;
264 }
265 }
266 if (lsc != NULL)
267 error = EINVAL;
268 else {
269 pdk->dk_nwedges++;
270 LIST_INSERT_HEAD(&pdk->dk_wedges, sc, sc_plink);
271 }
272 }
273 (void) lockmgr(&pdk->dk_openlock, LK_RELEASE, NULL);
274 if (error) {
275 bufq_free(&sc->sc_bufq);
276 free(sc, M_DKWEDGE);
277 return (error);
278 }
279
280 /* Insert the larval wedge into the array. */
281 (void) lockmgr(&dkwedges_lock, LK_EXCLUSIVE, NULL);
282 for (error = 0;;) {
283 struct dkwedge_softc **scpp;
284
285 /*
286 * Check for a duplicate wname while searching for
287 * a slot.
288 */
289 for (scpp = NULL, unit = 0; unit < ndkwedges; unit++) {
290 if (dkwedges[unit] == NULL) {
291 if (scpp == NULL) {
292 scpp = &dkwedges[unit];
293 sc->sc_unit = unit;
294 }
295 } else {
296 /* XXX Unicode. */
297 if (strcmp(dkwedges[unit]->sc_wname,
298 sc->sc_wname) == 0) {
299 error = EEXIST;
300 break;
301 }
302 }
303 }
304 if (error)
305 break;
306 KASSERT(unit == ndkwedges);
307 if (scpp == NULL)
308 dkwedge_array_expand();
309 else {
310 KASSERT(scpp == &dkwedges[sc->sc_unit]);
311 *scpp = sc;
312 break;
313 }
314 }
315 (void) lockmgr(&dkwedges_lock, LK_RELEASE, NULL);
316 if (error) {
317 (void) lockmgr(&pdk->dk_openlock, LK_EXCLUSIVE, NULL);
318 pdk->dk_nwedges--;
319 LIST_REMOVE(sc, sc_plink);
320 (void) lockmgr(&pdk->dk_openlock, LK_RELEASE, NULL);
321
322 bufq_free(&sc->sc_bufq);
323 free(sc, M_DKWEDGE);
324 return (error);
325 }
326
327 /* Now that we know the unit #, set the devname. */
328 sprintf(sc->sc_devname, "dk%u", sc->sc_unit);
329 sc->sc_dk.dk_name = sc->sc_devname;
330
331 /* Return the devname to the caller. */
332 strcpy(dkw->dkw_devname, sc->sc_devname);
333
334 /*
335 * XXX Really ought to make the disk_attach() and the changing
336 * of state to RUNNING atomic.
337 */
338
339 disk_attach(&sc->sc_dk);
340
341 /* Disk wedge is ready for use! */
342 sc->sc_state = DKW_STATE_RUNNING;
343
344 /* Announce our arrival. */
345 aprint_normal("%s at %s: %s\n", sc->sc_devname, pdk->dk_name,
346 sc->sc_wname); /* XXX Unicode */
347 aprint_normal("%s: %"PRIu64" blocks at %"PRId64", type: %s\n",
348 sc->sc_devname, sc->sc_size, sc->sc_offset, sc->sc_ptype);
349
350 return (0);
351 }
352
353 /*
354 * dkwedge_del: [exported function]
355 *
356 * Delete a disk wedge based on the provided information.
357 * NOTE: We look up the wedge based on the wedge devname,
358 * not wname.
359 */
360 int
361 dkwedge_del(struct dkwedge_info *dkw)
362 {
363 struct dkwedge_softc *sc = NULL;
364 u_int unit;
365 int bmaj, cmaj, i, mn, s;
366
367 /* Find our softc. */
368 dkw->dkw_devname[sizeof(dkw->dkw_devname) - 1] = '\0';
369 (void) lockmgr(&dkwedges_lock, LK_EXCLUSIVE, NULL);
370 for (unit = 0; unit < ndkwedges; unit++) {
371 if ((sc = dkwedges[unit]) != NULL &&
372 strcmp(sc->sc_devname, dkw->dkw_devname) == 0 &&
373 strcmp(sc->sc_parent->dk_name, dkw->dkw_parent) == 0) {
374 /* Mark the wedge as dying. */
375 sc->sc_state = DKW_STATE_DYING;
376 break;
377 }
378 }
379 (void) lockmgr(&dkwedges_lock, LK_RELEASE, NULL);
380 if (unit == ndkwedges)
381 return (ESRCH);
382
383 KASSERT(sc != NULL);
384
385 /* Locate the wedge major numbers. */
386 bmaj = bdevsw_lookup_major(&dk_bdevsw);
387 cmaj = cdevsw_lookup_major(&dk_cdevsw);
388
389 /* Kill any pending restart. */
390 callout_stop(&sc->sc_restart_ch);
391
392 /*
393 * dkstart() will kill any queued buffers now that the
394 * state of the wedge is not RUNNING. Once we've done
395 * that, wait for any other pending I/O to complete.
396 */
397 s = splbio();
398 dkstart(sc);
399 dkwedge_wait_drain(sc);
400 splx(s);
401
402 /* Nuke the vnodes for any open instances. */
403 for (i = 0; i < MAXPARTITIONS; i++) {
404 mn = DISKMINOR(unit, i);
405 vdevgone(bmaj, mn, mn, VBLK);
406 vdevgone(cmaj, mn, mn, VCHR);
407 }
408
409 /* Clean up the parent. */
410 (void) lockmgr(&sc->sc_parent->dk_rawlock, LK_EXCLUSIVE, NULL);
411 if (sc->sc_open) {
412 if (sc->sc_parent->dk_rawopens-- == 1) {
413 KASSERT(sc->sc_parent->dk_rawvp != NULL);
414 (void) vn_close(sc->sc_parent->dk_rawvp, FREAD | FWRITE,
415 NOCRED, curproc);
416 sc->sc_parent->dk_rawvp = NULL;
417 }
418 sc->sc_open = 0;
419 }
420 (void) lockmgr(&sc->sc_parent->dk_rawlock, LK_RELEASE, NULL);
421
422 /* Announce our departure. */
423 aprint_normal("%s at %s (%s) deleted\n", sc->sc_devname,
424 sc->sc_parent->dk_name,
425 sc->sc_wname); /* XXX Unicode */
426
427 (void) lockmgr(&sc->sc_parent->dk_openlock, LK_EXCLUSIVE, NULL);
428 sc->sc_parent->dk_nwedges--;
429 LIST_REMOVE(sc, sc_plink);
430 (void) lockmgr(&sc->sc_parent->dk_openlock, LK_RELEASE, NULL);
431
432 /* Delete our buffer queue. */
433 bufq_free(&sc->sc_bufq);
434
435 /* Detach from the disk list. */
436 disk_detach(&sc->sc_dk);
437
438 /* Poof. */
439 (void) lockmgr(&dkwedges_lock, LK_EXCLUSIVE, NULL);
440 dkwedges[unit] = NULL;
441 sc->sc_state = DKW_STATE_DEAD;
442 (void) lockmgr(&dkwedges_lock, LK_RELEASE, NULL);
443
444 free(sc, M_DKWEDGE);
445
446 return (0);
447 }
448
449 /*
450 * dkwedge_delall: [exported function]
451 *
452 * Delete all of the wedges on the specified disk. Used when
453 * a disk is being detached.
454 */
455 void
456 dkwedge_delall(struct disk *pdk)
457 {
458 struct dkwedge_info dkw;
459 struct dkwedge_softc *sc;
460
461 for (;;) {
462 (void) lockmgr(&pdk->dk_openlock, LK_EXCLUSIVE, NULL);
463 if ((sc = LIST_FIRST(&pdk->dk_wedges)) == NULL) {
464 KASSERT(pdk->dk_nwedges == 0);
465 (void) lockmgr(&pdk->dk_openlock, LK_RELEASE, NULL);
466 return;
467 }
468 strcpy(dkw.dkw_parent, pdk->dk_name);
469 strcpy(dkw.dkw_devname, sc->sc_devname);
470 (void) lockmgr(&pdk->dk_openlock, LK_RELEASE, NULL);
471 (void) dkwedge_del(&dkw);
472 }
473 }
474
475 /*
476 * dkwedge_list: [exported function]
477 *
478 * List all of the wedges on a particular disk.
479 * If p == NULL, the buffer is in kernel space. Otherwise, it is
480 * in user space of the specified process.
481 */
482 int
483 dkwedge_list(struct disk *pdk, struct dkwedge_list *dkwl, struct proc *p)
484 {
485 struct uio uio;
486 struct iovec iov;
487 struct dkwedge_softc *sc;
488 struct dkwedge_info dkw;
489 int error = 0;
490
491 iov.iov_base = dkwl->dkwl_buf;
492 iov.iov_len = dkwl->dkwl_bufsize;
493
494 uio.uio_iov = &iov;
495 uio.uio_iovcnt = 1;
496 uio.uio_offset = 0;
497 uio.uio_resid = dkwl->dkwl_bufsize;
498 uio.uio_segflg = p != NULL ? UIO_USERSPACE : UIO_SYSSPACE;
499 uio.uio_rw = UIO_READ;
500 uio.uio_procp = p;
501
502 dkwl->dkwl_ncopied = 0;
503
504 (void) lockmgr(&pdk->dk_openlock, LK_EXCLUSIVE, NULL);
505 LIST_FOREACH(sc, &pdk->dk_wedges, sc_plink) {
506 if (uio.uio_resid < sizeof(dkw))
507 break;
508
509 if (sc->sc_state != DKW_STATE_RUNNING)
510 continue;
511
512 strcpy(dkw.dkw_devname, sc->sc_devname);
513 memcpy(dkw.dkw_wname, sc->sc_wname, sizeof(dkw.dkw_wname));
514 dkw.dkw_wname[sizeof(dkw.dkw_wname) - 1] = '\0';
515 strcpy(dkw.dkw_parent, sc->sc_parent->dk_name);
516 dkw.dkw_offset = sc->sc_offset;
517 dkw.dkw_size = sc->sc_size;
518 strcpy(dkw.dkw_ptype, sc->sc_ptype);
519
520 error = uiomove(&dkw, sizeof(dkw), &uio);
521 if (error)
522 break;
523 dkwl->dkwl_ncopied++;
524 }
525 dkwl->dkwl_nwedges = pdk->dk_nwedges;
526 (void) lockmgr(&pdk->dk_openlock, LK_RELEASE, NULL);
527
528 return (error);
529 }
530
531 /*
532 * We need a dummy objet to stuff into the dkwedge discovery method link
533 * set to ensure that there is always at least one object in the set.
534 */
535 static struct dkwedge_discovery_method dummy_discovery_method;
536 __link_set_add_bss(dkwedge_methods, dummy_discovery_method);
537
538 /*
539 * dkwedge_discover_init:
540 *
541 * Initialize the disk wedge discovery method list.
542 */
543 static void
544 dkwedge_discover_init(void)
545 {
546 __link_set_decl(dkwedge_methods, struct dkwedge_discovery_method);
547 struct dkwedge_discovery_method * const *ddmp;
548 struct dkwedge_discovery_method *lddm, *ddm;
549
550 (void) lockmgr(&dkwedge_discovery_methods_lock, LK_EXCLUSIVE, NULL);
551
552 if (dkwedge_discovery_methods_initialized) {
553 (void) lockmgr(&dkwedge_discovery_methods_lock, LK_RELEASE,
554 NULL);
555 return;
556 }
557
558 LIST_INIT(&dkwedge_discovery_methods);
559
560 __link_set_foreach(ddmp, dkwedge_methods) {
561 ddm = *ddmp;
562 if (ddm == &dummy_discovery_method)
563 continue;
564 if (LIST_EMPTY(&dkwedge_discovery_methods)) {
565 LIST_INSERT_HEAD(&dkwedge_discovery_methods,
566 ddm, ddm_list);
567 continue;
568 }
569 LIST_FOREACH(lddm, &dkwedge_discovery_methods, ddm_list) {
570 if (ddm->ddm_priority == lddm->ddm_priority) {
571 aprint_error("dk-method-%s: method \"%s\" "
572 "already exists at priority %d\n",
573 ddm->ddm_name, lddm->ddm_name,
574 lddm->ddm_priority);
575 /* Not inserted. */
576 break;
577 }
578 if (ddm->ddm_priority < lddm->ddm_priority) {
579 /* Higher priority; insert before. */
580 LIST_INSERT_BEFORE(lddm, ddm, ddm_list);
581 break;
582 }
583 if (LIST_NEXT(lddm, ddm_list) == NULL) {
584 /* Last one; insert after. */
585 KASSERT(lddm->ddm_priority < ddm->ddm_priority);
586 LIST_INSERT_AFTER(lddm, ddm, ddm_list);
587 break;
588 }
589 }
590 }
591
592 dkwedge_discovery_methods_initialized = 1;
593
594 (void) lockmgr(&dkwedge_discovery_methods_lock, LK_RELEASE, NULL);
595 }
596
597 #ifdef DKWEDGE_AUTODISCOVER
598 int dkwedge_autodiscover = 1;
599 #else
600 int dkwedge_autodiscover = 0;
601 #endif
602
603 /*
604 * dkwedge_discover: [exported function]
605 *
606 * Discover the wedges on a newly attached disk.
607 */
608 void
609 dkwedge_discover(struct disk *pdk)
610 {
611 struct dkwedge_discovery_method *ddm;
612 struct vnode *vp;
613 int error;
614 dev_t pdev;
615
616 /*
617 * Require people playing with wedges to enable this explicitly.
618 */
619 if (dkwedge_autodiscover == 0)
620 return;
621
622 if (dkwedge_discovery_methods_initialized == 0)
623 dkwedge_discover_init();
624
625 (void) lockmgr(&dkwedge_discovery_methods_lock, LK_SHARED, NULL);
626
627 error = dkwedge_compute_pdev(pdk->dk_name, &pdev);
628 if (error) {
629 aprint_error("%s: unable to compute pdev, error = %d\n",
630 pdk->dk_name, error);
631 goto out;
632 }
633
634 error = bdevvp(pdev, &vp);
635 if (error) {
636 aprint_error("%s: unable to find vnode for pdev, error = %d\n",
637 pdk->dk_name, error);
638 goto out;
639 }
640
641 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
642 if (error) {
643 aprint_error("%s: unable to lock vnode for pdev, error = %d\n",
644 pdk->dk_name, error);
645 vrele(vp);
646 goto out;
647 }
648
649 error = VOP_OPEN(vp, FREAD | FWRITE, NOCRED, 0);
650 if (error) {
651 aprint_error("%s: unable to open device, error = %d\n",
652 pdk->dk_name, error);
653 vput(vp);
654 goto out;
655 }
656 /* VOP_OPEN() doesn't do this for us. */
657 vp->v_writecount++;
658 VOP_UNLOCK(vp, 0);
659
660 /*
661 * For each supported partition map type, look to see if
662 * this map type exists. If so, parse it and add the
663 * corresponding wedges.
664 */
665 LIST_FOREACH(ddm, &dkwedge_discovery_methods, ddm_list) {
666 error = (*ddm->ddm_discover)(pdk, vp);
667 if (error == 0) {
668 /* Successfully created wedges; we're done. */
669 break;
670 }
671 }
672
673 error = vn_close(vp, FREAD | FWRITE, NOCRED, curproc);
674 if (error) {
675 aprint_error("%s: unable to close device, error = %d\n",
676 pdk->dk_name, error);
677 /* We'll just assume the vnode has been cleaned up. */
678 }
679 out:
680 (void) lockmgr(&dkwedge_discovery_methods_lock, LK_RELEASE, NULL);
681 }
682
683 /*
684 * dkwedge_read:
685 *
686 * Read the some data from the specified disk, used for
687 * partition discovery.
688 */
689 int
690 dkwedge_read(struct disk *pdk, struct vnode *vp, daddr_t blkno, void *buf,
691 size_t len)
692 {
693 struct buf b;
694
695 BUF_INIT(&b);
696
697 b.b_vp = vp;
698 b.b_dev = vp->v_rdev;
699 b.b_blkno = blkno;
700 b.b_bcount = b.b_resid = len;
701 b.b_flags = B_READ;
702 b.b_proc = curproc;
703 b.b_data = buf;
704
705 VOP_STRATEGY(vp, &b);
706 return (biowait(&b));
707 }
708
709 /*
710 * dkwedge_lookup:
711 *
712 * Look up a dkwedge_softc based on the provided dev_t.
713 */
714 static struct dkwedge_softc *
715 dkwedge_lookup(dev_t dev)
716 {
717 int unit = DISKUNIT(dev);
718
719 if (unit >= ndkwedges)
720 return (NULL);
721
722 KASSERT(dkwedges != NULL);
723
724 return (dkwedges[unit]);
725 }
726
727 /*
728 * dkopen: [devsw entry point]
729 *
730 * Open a wedge.
731 */
732 static int
733 dkopen(dev_t dev, int flags, int fmt, struct proc *p)
734 {
735 struct dkwedge_softc *sc = dkwedge_lookup(dev);
736 struct vnode *vp;
737 int error;
738
739 if (sc == NULL)
740 return (ENODEV);
741
742 if (sc->sc_state != DKW_STATE_RUNNING)
743 return (ENXIO);
744
745 /*
746 * We go through a complicated little dance to only open the parent
747 * vnode once per wedge, no matter how many times the wedge is
748 * opened. The reason? We see one dkopen() per open call, but
749 * only dkclose() on the last close.
750 */
751 (void) lockmgr(&sc->sc_parent->dk_rawlock, LK_EXCLUSIVE, NULL);
752 if (sc->sc_open == 0) {
753 if (sc->sc_parent->dk_rawopens++ == 0) {
754 KASSERT(sc->sc_parent->dk_rawvp == NULL);
755 error = bdevvp(sc->sc_pdev, &vp);
756 if (error)
757 goto popen_fail;
758 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
759 if (error) {
760 vrele(vp);
761 goto popen_fail;
762 }
763 error = VOP_OPEN(vp, FREAD | FWRITE, NOCRED, 0);
764 if (error) {
765 vput(vp);
766 goto popen_fail;
767 }
768 /* VOP_OPEN() doesn't do this for us. */
769 vp->v_writecount++;
770 VOP_UNLOCK(vp, 0);
771 sc->sc_parent->dk_rawvp = vp;
772 }
773 sc->sc_open = 1;
774 }
775 (void) lockmgr(&sc->sc_parent->dk_rawlock, LK_RELEASE, NULL);
776
777 return (0);
778
779 popen_fail:
780 (void) lockmgr(&sc->sc_parent->dk_rawlock, LK_RELEASE, NULL);
781 return (error);
782 }
783
784 /*
785 * dkclose: [devsw entry point]
786 *
787 * Close a wedge.
788 */
789 static int
790 dkclose(dev_t dev, int flags, int fmt, struct proc *p)
791 {
792 struct dkwedge_softc *sc = dkwedge_lookup(dev);
793 int error = 0;
794
795 KASSERT(sc->sc_open);
796
797 (void) lockmgr(&sc->sc_parent->dk_rawlock, LK_EXCLUSIVE, NULL);
798
799 if (sc->sc_parent->dk_rawopens-- == 1) {
800 KASSERT(sc->sc_parent->dk_rawvp != NULL);
801 error = vn_close(sc->sc_parent->dk_rawvp, FREAD | FWRITE,
802 NOCRED, p);
803 sc->sc_parent->dk_rawvp = NULL;
804 }
805 sc->sc_open = 0;
806
807 (void) lockmgr(&sc->sc_parent->dk_rawlock, LK_RELEASE, NULL);
808
809 return (error);
810 }
811
812 /*
813 * dkstragegy: [devsw entry point]
814 *
815 * Perform I/O based on the wedge I/O strategy.
816 */
817 static void
818 dkstrategy(struct buf *bp)
819 {
820 struct dkwedge_softc *sc = dkwedge_lookup(bp->b_dev);
821 int s;
822
823 if (sc->sc_state != DKW_STATE_RUNNING) {
824 bp->b_error = ENXIO;
825 bp->b_flags |= B_ERROR;
826 goto done;
827 }
828
829 /* If it's an empty transfer, wake up the top half now. */
830 if (bp->b_bcount == 0)
831 goto done;
832
833 /* Make sure it's in-range. */
834 if (bounds_check_with_mediasize(bp, DEV_BSIZE, sc->sc_size) <= 0)
835 goto done;
836
837 /* Translate it to the parent's raw LBA. */
838 bp->b_rawblkno = bp->b_blkno + sc->sc_offset;
839
840 /* Place it in the queue and start I/O on the unit. */
841 s = splbio();
842 sc->sc_iopend++;
843 BUFQ_PUT(&sc->sc_bufq, bp);
844 dkstart(sc);
845 splx(s);
846 return;
847
848 done:
849 bp->b_resid = bp->b_bcount;
850 biodone(bp);
851 }
852
853 /*
854 * dkstart:
855 *
856 * Start I/O that has been enqueued on the wedge.
857 * NOTE: Must be called at splbio()!
858 */
859 static void
860 dkstart(struct dkwedge_softc *sc)
861 {
862 struct buf *bp, *nbp;
863
864 /* Do as much work as has been enqueued. */
865 while ((bp = BUFQ_PEEK(&sc->sc_bufq)) != NULL) {
866 if (sc->sc_state != DKW_STATE_RUNNING) {
867 (void) BUFQ_GET(&sc->sc_bufq);
868 if (sc->sc_iopend-- == 1 &&
869 (sc->sc_flags & DK_F_WAIT_DRAIN) != 0) {
870 sc->sc_flags &= ~DK_F_WAIT_DRAIN;
871 wakeup(&sc->sc_iopend);
872 }
873 bp->b_error = ENXIO;
874 bp->b_flags |= B_ERROR;
875 bp->b_resid = bp->b_bcount;
876 biodone(bp);
877 }
878
879 /* Instrumentation. */
880 disk_busy(&sc->sc_dk);
881
882 nbp = pool_get(&bufpool, PR_NOWAIT);
883 if (nbp == NULL) {
884 /*
885 * No resources to run this request; leave the
886 * buffer queued up, and schedule a timer to
887 * restart the queue in 1/2 a second.
888 */
889 disk_unbusy(&sc->sc_dk, 0, bp->b_flags & B_READ);
890 callout_schedule(&sc->sc_restart_ch, hz / 2);
891 return;
892 }
893
894 (void) BUFQ_GET(&sc->sc_bufq);
895
896 BUF_INIT(nbp);
897 nbp->b_data = bp->b_data;
898 nbp->b_flags = bp->b_flags | B_CALL;
899 nbp->b_iodone = dkiodone;
900 nbp->b_proc = bp->b_proc;
901 nbp->b_blkno = bp->b_rawblkno;
902 nbp->b_dev = sc->sc_parent->dk_rawvp->v_rdev;
903 nbp->b_vp = sc->sc_parent->dk_rawvp;
904 nbp->b_bcount = bp->b_bcount;
905 nbp->b_private = bp;
906 BIO_COPYPRIO(nbp, bp);
907
908 if ((nbp->b_flags & B_READ) == 0)
909 V_INCR_NUMOUTPUT(nbp->b_vp);
910 VOP_STRATEGY(nbp->b_vp, nbp);
911 }
912 }
913
914 /*
915 * dkiodone:
916 *
917 * I/O to a wedge has completed; alert the top half.
918 * NOTE: Must be called at splbio()!
919 */
920 static void
921 dkiodone(struct buf *bp)
922 {
923 struct buf *obp = bp->b_private;
924 struct dkwedge_softc *sc = dkwedge_lookup(obp->b_dev);
925
926 if (bp->b_flags & B_ERROR) {
927 obp->b_flags |= B_ERROR;
928 obp->b_error = bp->b_error;
929 }
930 obp->b_resid = bp->b_resid;
931 pool_put(&bufpool, bp);
932
933 if (sc->sc_iopend-- == 1 && (sc->sc_flags & DK_F_WAIT_DRAIN) != 0) {
934 sc->sc_flags &= ~DK_F_WAIT_DRAIN;
935 wakeup(&sc->sc_iopend);
936 }
937
938 disk_unbusy(&sc->sc_dk, obp->b_bcount - obp->b_resid,
939 obp->b_flags & B_READ);
940
941 biodone(obp);
942
943 /* Kick the queue in case there is more work we can do. */
944 dkstart(sc);
945 }
946
947 /*
948 * dkrestart:
949 *
950 * Restart the work queue after it was stalled due to
951 * a resource shortage. Invoked via a callout.
952 */
953 static void
954 dkrestart(void *v)
955 {
956 struct dkwedge_softc *sc = v;
957 int s;
958
959 s = splbio();
960 dkstart(sc);
961 splx(s);
962 }
963
964 /*
965 * dkread: [devsw entry point]
966 *
967 * Read from a wedge.
968 */
969 static int
970 dkread(dev_t dev, struct uio *uio, int flags)
971 {
972 struct dkwedge_softc *sc = dkwedge_lookup(dev);
973
974 if (sc->sc_state != DKW_STATE_RUNNING)
975 return (ENXIO);
976
977 return (physio(dkstrategy, NULL, dev, B_READ,
978 sc->sc_parent->dk_driver->d_minphys, uio));
979 }
980
981 /*
982 * dkwrite: [devsw entry point]
983 *
984 * Write to a wedge.
985 */
986 static int
987 dkwrite(dev_t dev, struct uio *uio, int flags)
988 {
989 struct dkwedge_softc *sc = dkwedge_lookup(dev);
990
991 if (sc->sc_state != DKW_STATE_RUNNING)
992 return (ENXIO);
993
994 return (physio(dkstrategy, NULL, dev, B_WRITE,
995 sc->sc_parent->dk_driver->d_minphys, uio));
996 }
997
998 /*
999 * dkioctl: [devsw entry point]
1000 *
1001 * Perform an ioctl request on a wedge.
1002 */
1003 static int
1004 dkioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
1005 {
1006 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1007 int error = 0;
1008
1009 if (sc->sc_state != DKW_STATE_RUNNING)
1010 return (ENXIO);
1011
1012 switch (cmd) {
1013 case DIOCGWEDGEINFO:
1014 {
1015 struct dkwedge_info *dkw = (void *) data;
1016
1017 strcpy(dkw->dkw_devname, sc->sc_devname);
1018 memcpy(dkw->dkw_wname, sc->sc_wname, sizeof(dkw->dkw_wname));
1019 dkw->dkw_wname[sizeof(dkw->dkw_wname) - 1] = '\0';
1020 strcpy(dkw->dkw_parent, sc->sc_parent->dk_name);
1021 dkw->dkw_offset = sc->sc_offset;
1022 dkw->dkw_size = sc->sc_size;
1023 strcpy(dkw->dkw_ptype, sc->sc_ptype);
1024
1025 break;
1026 }
1027
1028 default:
1029 error = ENOTTY;
1030 }
1031
1032 return (error);
1033 }
1034
1035 /*
1036 * dksize: [devsw entry point]
1037 *
1038 * Query the size of a wedge for the purpose of performing a dump
1039 * or for swapping to.
1040 */
1041 static int
1042 dksize(dev_t dev)
1043 {
1044
1045 /* XXX */
1046 return (-1);
1047 }
1048
1049 /*
1050 * dkdump: [devsw entry point]
1051 *
1052 * Perform a crash dump to a wedge.
1053 */
1054 static int
1055 dkdump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
1056 {
1057
1058 /* XXX */
1059 return (ENXIO);
1060 }
1061