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