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