dk.c revision 1.20 1 /* $NetBSD: dk.c,v 1.20 2006/11/16 01:32:50 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.20 2006/11/16 01:32:50 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 | FWRITE, 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_OPEN() doesn't do this for us. */
809 vp->v_writecount++;
810 VOP_UNLOCK(vp, 0);
811
812 /*
813 * For each supported partition map type, look to see if
814 * this map type exists. If so, parse it and add the
815 * corresponding wedges.
816 */
817 LIST_FOREACH(ddm, &dkwedge_discovery_methods, ddm_list) {
818 error = (*ddm->ddm_discover)(pdk, vp);
819 if (error == 0) {
820 /* Successfully created wedges; we're done. */
821 break;
822 }
823 }
824
825 error = vn_close(vp, FREAD | FWRITE, NOCRED, curlwp);
826 if (error) {
827 aprint_error("%s: unable to close device, error = %d\n",
828 pdk->dk_name, error);
829 /* We'll just assume the vnode has been cleaned up. */
830 }
831 out:
832 (void) lockmgr(&dkwedge_discovery_methods_lock, LK_RELEASE, NULL);
833 }
834
835 /*
836 * dkwedge_read:
837 *
838 * Read the some data from the specified disk, used for
839 * partition discovery.
840 */
841 int
842 dkwedge_read(struct disk *pdk, struct vnode *vp, daddr_t blkno,
843 void *tbuf, size_t len)
844 {
845 struct buf b;
846
847 BUF_INIT(&b);
848
849 b.b_vp = vp;
850 b.b_dev = vp->v_rdev;
851 b.b_blkno = blkno;
852 b.b_bcount = b.b_resid = len;
853 b.b_flags = B_READ;
854 b.b_proc = curproc;
855 b.b_data = tbuf;
856
857 VOP_STRATEGY(vp, &b);
858 return (biowait(&b));
859 }
860
861 /*
862 * dkwedge_lookup:
863 *
864 * Look up a dkwedge_softc based on the provided dev_t.
865 */
866 static struct dkwedge_softc *
867 dkwedge_lookup(dev_t dev)
868 {
869 int unit = minor(dev);
870
871 if (unit >= ndkwedges)
872 return (NULL);
873
874 KASSERT(dkwedges != NULL);
875
876 return (dkwedges[unit]);
877 }
878
879 /*
880 * dkopen: [devsw entry point]
881 *
882 * Open a wedge.
883 */
884 static int
885 dkopen(dev_t dev, int flags, int fmt, struct lwp *l)
886 {
887 struct dkwedge_softc *sc = dkwedge_lookup(dev);
888 struct vnode *vp;
889 int error = 0;
890
891 if (sc == NULL)
892 return (ENODEV);
893
894 if (sc->sc_state != DKW_STATE_RUNNING)
895 return (ENXIO);
896
897 /*
898 * We go through a complicated little dance to only open the parent
899 * vnode once per wedge, no matter how many times the wedge is
900 * opened. The reason? We see one dkopen() per open call, but
901 * only dkclose() on the last close.
902 */
903 (void) lockmgr(&sc->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL);
904 (void) lockmgr(&sc->sc_parent->dk_rawlock, LK_EXCLUSIVE, NULL);
905 if (sc->sc_dk.dk_openmask == 0) {
906 if (sc->sc_parent->dk_rawopens++ == 0) {
907 KASSERT(sc->sc_parent->dk_rawvp == NULL);
908 error = bdevvp(sc->sc_pdev, &vp);
909 if (error)
910 goto popen_fail;
911 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
912 if (error) {
913 vrele(vp);
914 goto popen_fail;
915 }
916 error = VOP_OPEN(vp, FREAD | FWRITE, NOCRED, 0);
917 if (error) {
918 vput(vp);
919 goto popen_fail;
920 }
921 /* VOP_OPEN() doesn't do this for us. */
922 vp->v_writecount++;
923 VOP_UNLOCK(vp, 0);
924 sc->sc_parent->dk_rawvp = vp;
925 }
926 }
927 if (fmt == S_IFCHR)
928 sc->sc_dk.dk_copenmask |= 1;
929 else
930 sc->sc_dk.dk_bopenmask |= 1;
931 sc->sc_dk.dk_openmask =
932 sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
933
934 popen_fail:
935 (void) lockmgr(&sc->sc_parent->dk_rawlock, LK_RELEASE, NULL);
936 (void) lockmgr(&sc->sc_dk.dk_openlock, LK_RELEASE, NULL);
937 return (error);
938 }
939
940 /*
941 * dkclose: [devsw entry point]
942 *
943 * Close a wedge.
944 */
945 static int
946 dkclose(dev_t dev, int flags, int fmt, struct lwp *l)
947 {
948 struct dkwedge_softc *sc = dkwedge_lookup(dev);
949 int error = 0;
950
951 KASSERT(sc->sc_dk.dk_openmask != 0);
952
953 (void) lockmgr(&sc->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL);
954 (void) lockmgr(&sc->sc_parent->dk_rawlock, LK_EXCLUSIVE, NULL);
955
956 if (fmt == S_IFCHR)
957 sc->sc_dk.dk_copenmask &= ~1;
958 else
959 sc->sc_dk.dk_bopenmask &= ~1;
960 sc->sc_dk.dk_openmask =
961 sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
962
963 if (sc->sc_dk.dk_openmask == 0) {
964 if (sc->sc_parent->dk_rawopens-- == 1) {
965 KASSERT(sc->sc_parent->dk_rawvp != NULL);
966 error = vn_close(sc->sc_parent->dk_rawvp,
967 FREAD | FWRITE, NOCRED, l);
968 sc->sc_parent->dk_rawvp = NULL;
969 }
970 }
971
972 (void) lockmgr(&sc->sc_parent->dk_rawlock, LK_RELEASE, NULL);
973 (void) lockmgr(&sc->sc_dk.dk_openlock, LK_RELEASE, NULL);
974
975 return (error);
976 }
977
978 /*
979 * dkstragegy: [devsw entry point]
980 *
981 * Perform I/O based on the wedge I/O strategy.
982 */
983 static void
984 dkstrategy(struct buf *bp)
985 {
986 struct dkwedge_softc *sc = dkwedge_lookup(bp->b_dev);
987 int s;
988
989 if (sc->sc_state != DKW_STATE_RUNNING) {
990 bp->b_error = ENXIO;
991 bp->b_flags |= B_ERROR;
992 goto done;
993 }
994
995 /* If it's an empty transfer, wake up the top half now. */
996 if (bp->b_bcount == 0)
997 goto done;
998
999 /* Make sure it's in-range. */
1000 if (bounds_check_with_mediasize(bp, DEV_BSIZE, sc->sc_size) <= 0)
1001 goto done;
1002
1003 /* Translate it to the parent's raw LBA. */
1004 bp->b_rawblkno = bp->b_blkno + sc->sc_offset;
1005
1006 /* Place it in the queue and start I/O on the unit. */
1007 s = splbio();
1008 sc->sc_iopend++;
1009 BUFQ_PUT(sc->sc_bufq, bp);
1010 dkstart(sc);
1011 splx(s);
1012 return;
1013
1014 done:
1015 bp->b_resid = bp->b_bcount;
1016 biodone(bp);
1017 }
1018
1019 /*
1020 * dkstart:
1021 *
1022 * Start I/O that has been enqueued on the wedge.
1023 * NOTE: Must be called at splbio()!
1024 */
1025 static void
1026 dkstart(struct dkwedge_softc *sc)
1027 {
1028 struct buf *bp, *nbp;
1029
1030 /* Do as much work as has been enqueued. */
1031 while ((bp = BUFQ_PEEK(sc->sc_bufq)) != NULL) {
1032 if (sc->sc_state != DKW_STATE_RUNNING) {
1033 (void) BUFQ_GET(sc->sc_bufq);
1034 if (sc->sc_iopend-- == 1 &&
1035 (sc->sc_flags & DK_F_WAIT_DRAIN) != 0) {
1036 sc->sc_flags &= ~DK_F_WAIT_DRAIN;
1037 wakeup(&sc->sc_iopend);
1038 }
1039 bp->b_error = ENXIO;
1040 bp->b_flags |= B_ERROR;
1041 bp->b_resid = bp->b_bcount;
1042 biodone(bp);
1043 }
1044
1045 /* Instrumentation. */
1046 disk_busy(&sc->sc_dk);
1047
1048 nbp = getiobuf_nowait();
1049 if (nbp == NULL) {
1050 /*
1051 * No resources to run this request; leave the
1052 * buffer queued up, and schedule a timer to
1053 * restart the queue in 1/2 a second.
1054 */
1055 disk_unbusy(&sc->sc_dk, 0, bp->b_flags & B_READ);
1056 callout_schedule(&sc->sc_restart_ch, hz / 2);
1057 return;
1058 }
1059
1060 (void) BUFQ_GET(sc->sc_bufq);
1061
1062 BUF_INIT(nbp);
1063 nbp->b_data = bp->b_data;
1064 nbp->b_flags = bp->b_flags | B_CALL;
1065 nbp->b_iodone = dkiodone;
1066 nbp->b_proc = bp->b_proc;
1067 nbp->b_blkno = bp->b_rawblkno;
1068 nbp->b_dev = sc->sc_parent->dk_rawvp->v_rdev;
1069 nbp->b_vp = sc->sc_parent->dk_rawvp;
1070 nbp->b_bcount = bp->b_bcount;
1071 nbp->b_private = bp;
1072 BIO_COPYPRIO(nbp, bp);
1073
1074 if ((nbp->b_flags & B_READ) == 0)
1075 V_INCR_NUMOUTPUT(nbp->b_vp);
1076 VOP_STRATEGY(nbp->b_vp, nbp);
1077 }
1078 }
1079
1080 /*
1081 * dkiodone:
1082 *
1083 * I/O to a wedge has completed; alert the top half.
1084 * NOTE: Must be called at splbio()!
1085 */
1086 static void
1087 dkiodone(struct buf *bp)
1088 {
1089 struct buf *obp = bp->b_private;
1090 struct dkwedge_softc *sc = dkwedge_lookup(obp->b_dev);
1091
1092 if (bp->b_flags & B_ERROR) {
1093 obp->b_flags |= B_ERROR;
1094 obp->b_error = bp->b_error;
1095 }
1096 obp->b_resid = bp->b_resid;
1097 putiobuf(bp);
1098
1099 if (sc->sc_iopend-- == 1 && (sc->sc_flags & DK_F_WAIT_DRAIN) != 0) {
1100 sc->sc_flags &= ~DK_F_WAIT_DRAIN;
1101 wakeup(&sc->sc_iopend);
1102 }
1103
1104 disk_unbusy(&sc->sc_dk, obp->b_bcount - obp->b_resid,
1105 obp->b_flags & B_READ);
1106
1107 biodone(obp);
1108
1109 /* Kick the queue in case there is more work we can do. */
1110 dkstart(sc);
1111 }
1112
1113 /*
1114 * dkrestart:
1115 *
1116 * Restart the work queue after it was stalled due to
1117 * a resource shortage. Invoked via a callout.
1118 */
1119 static void
1120 dkrestart(void *v)
1121 {
1122 struct dkwedge_softc *sc = v;
1123 int s;
1124
1125 s = splbio();
1126 dkstart(sc);
1127 splx(s);
1128 }
1129
1130 /*
1131 * dkread: [devsw entry point]
1132 *
1133 * Read from a wedge.
1134 */
1135 static int
1136 dkread(dev_t dev, struct uio *uio, int flags)
1137 {
1138 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1139
1140 if (sc->sc_state != DKW_STATE_RUNNING)
1141 return (ENXIO);
1142
1143 return (physio(dkstrategy, NULL, dev, B_READ,
1144 sc->sc_parent->dk_driver->d_minphys, uio));
1145 }
1146
1147 /*
1148 * dkwrite: [devsw entry point]
1149 *
1150 * Write to a wedge.
1151 */
1152 static int
1153 dkwrite(dev_t dev, struct uio *uio, int flags)
1154 {
1155 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1156
1157 if (sc->sc_state != DKW_STATE_RUNNING)
1158 return (ENXIO);
1159
1160 return (physio(dkstrategy, NULL, dev, B_WRITE,
1161 sc->sc_parent->dk_driver->d_minphys, uio));
1162 }
1163
1164 /*
1165 * dkioctl: [devsw entry point]
1166 *
1167 * Perform an ioctl request on a wedge.
1168 */
1169 static int
1170 dkioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
1171 {
1172 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1173 int error = 0;
1174
1175 if (sc->sc_state != DKW_STATE_RUNNING)
1176 return (ENXIO);
1177
1178 switch (cmd) {
1179 case DIOCCACHESYNC:
1180 /*
1181 * XXX Do we really need to care about having a writable
1182 * file descriptor here?
1183 */
1184 if ((flag & FWRITE) == 0)
1185 error = EBADF;
1186 else
1187 error = VOP_IOCTL(sc->sc_parent->dk_rawvp,
1188 cmd, data, flag,
1189 l != NULL ? l->l_cred : NOCRED, l);
1190 break;
1191 case DIOCGWEDGEINFO:
1192 {
1193 struct dkwedge_info *dkw = (void *) data;
1194
1195 strcpy(dkw->dkw_devname, sc->sc_dev->dv_xname);
1196 memcpy(dkw->dkw_wname, sc->sc_wname, sizeof(dkw->dkw_wname));
1197 dkw->dkw_wname[sizeof(dkw->dkw_wname) - 1] = '\0';
1198 strcpy(dkw->dkw_parent, sc->sc_parent->dk_name);
1199 dkw->dkw_offset = sc->sc_offset;
1200 dkw->dkw_size = sc->sc_size;
1201 strcpy(dkw->dkw_ptype, sc->sc_ptype);
1202
1203 break;
1204 }
1205
1206 default:
1207 error = ENOTTY;
1208 }
1209
1210 return (error);
1211 }
1212
1213 /*
1214 * dksize: [devsw entry point]
1215 *
1216 * Query the size of a wedge for the purpose of performing a dump
1217 * or for swapping to.
1218 */
1219 static int
1220 dksize(dev_t dev)
1221 {
1222 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1223 int rv = -1;
1224
1225 if (sc == NULL)
1226 return (-1);
1227
1228 if (sc->sc_state != DKW_STATE_RUNNING)
1229 return (ENXIO);
1230
1231 (void) lockmgr(&sc->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL);
1232 (void) lockmgr(&sc->sc_parent->dk_rawlock, LK_EXCLUSIVE, NULL);
1233
1234 /* Our content type is static, no need to open the device. */
1235
1236 if (strcmp(sc->sc_ptype, DKW_PTYPE_SWAP) == 0) {
1237 /* Saturate if we are larger than INT_MAX. */
1238 if (sc->sc_size > INT_MAX)
1239 rv = INT_MAX;
1240 else
1241 rv = (int) sc->sc_size;
1242 }
1243
1244 (void) lockmgr(&sc->sc_parent->dk_rawlock, LK_RELEASE, NULL);
1245 (void) lockmgr(&sc->sc_dk.dk_openlock, LK_RELEASE, NULL);
1246
1247 return (rv);
1248 }
1249
1250 /*
1251 * dkdump: [devsw entry point]
1252 *
1253 * Perform a crash dump to a wedge.
1254 */
1255 static int
1256 dkdump(dev_t dev, daddr_t blkno, caddr_t va,
1257 size_t size)
1258 {
1259
1260 /* XXX */
1261 return (ENXIO);
1262 }
1263