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