dk.c revision 1.22.2.1 1 /* $NetBSD: dk.c,v 1.22.2.1 2007/03/13 17:50:28 ad 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.22.2.1 2007/03/13 17:50:28 ad 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 krwlock_t dkwedges_lock;
126
127 static LIST_HEAD(, dkwedge_discovery_method) dkwedge_discovery_methods;
128 static int dkwedge_discovery_methods_initialized;
129 static krwlock_t dkwedge_discovery_methods_lock;
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,
138 void *aux)
139 {
140
141 /* Pseudo-device; always present. */
142 return (1);
143 }
144
145 /*
146 * dkwedge_attach:
147 *
148 * Autoconfiguration attach function for pseudo-device glue.
149 */
150 static void
151 dkwedge_attach(struct device *parent, struct device *self,
152 void *aux)
153 {
154
155 /* Nothing to do. */
156 }
157
158 /*
159 * dkwedge_detach:
160 *
161 * Autoconfiguration detach function for pseudo-device glue.
162 */
163 static int
164 dkwedge_detach(struct device *self, int flags)
165 {
166
167 /* Always succeeds. */
168 return (0);
169 }
170
171 CFDRIVER_DECL(dk, DV_DISK, NULL);
172 CFATTACH_DECL(dk, sizeof(struct device),
173 dkwedge_match, dkwedge_attach, dkwedge_detach, NULL);
174
175 static int dkwedge_cfglue_initialized;
176 static struct simplelock dkwedge_cfglue_initialized_slock =
177 SIMPLELOCK_INITIALIZER;
178
179 static void
180 dkwedge_cfglue_init(void)
181 {
182
183 simple_lock(&dkwedge_cfglue_initialized_slock);
184 if (dkwedge_cfglue_initialized == 0) {
185 if (config_cfdriver_attach(&dk_cd) != 0)
186 panic("dkwedge: unable to attach cfdriver");
187 if (config_cfattach_attach(dk_cd.cd_name, &dk_ca) != 0)
188 panic("dkwedge: unable to attach cfattach");
189
190 dkwedge_cfglue_initialized = 1;
191 rw_init(&dkwedges_lock);
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 #if 0
358 rw_enter(&dkwedges_lock, RW_WRITER);
359 #endif
360 for (error = 0;;) {
361 struct dkwedge_softc **scpp;
362
363 /*
364 * Check for a duplicate wname while searching for
365 * a slot.
366 */
367 for (scpp = NULL, unit = 0; unit < ndkwedges; unit++) {
368 if (dkwedges[unit] == NULL) {
369 if (scpp == NULL) {
370 scpp = &dkwedges[unit];
371 sc->sc_cfdata.cf_unit = unit;
372 }
373 } else {
374 /* XXX Unicode. */
375 if (strcmp(dkwedges[unit]->sc_wname,
376 sc->sc_wname) == 0) {
377 error = EEXIST;
378 break;
379 }
380 }
381 }
382 if (error)
383 break;
384 KASSERT(unit == ndkwedges);
385 if (scpp == NULL)
386 dkwedge_array_expand();
387 else {
388 KASSERT(scpp == &dkwedges[sc->sc_cfdata.cf_unit]);
389 *scpp = sc;
390 break;
391 }
392 }
393 #if 0
394 rw_exit(&dkwedges_lock);
395 #endif
396 if (error) {
397 (void) lockmgr(&pdk->dk_openlock, LK_EXCLUSIVE, NULL);
398 pdk->dk_nwedges--;
399 LIST_REMOVE(sc, sc_plink);
400 (void) lockmgr(&pdk->dk_openlock, LK_RELEASE, NULL);
401
402 bufq_free(sc->sc_bufq);
403 free(sc, M_DKWEDGE);
404 return (error);
405 }
406
407 /*
408 * Now that we know the unit #, attach a pseudo-device for
409 * this wedge instance. This will provide us with the
410 * "struct device" necessary for glue to other parts of the
411 * system.
412 *
413 * This should never fail, unless we're almost totally out of
414 * memory.
415 */
416 if ((sc->sc_dev = config_attach_pseudo(&sc->sc_cfdata)) == NULL) {
417 aprint_error("%s%u: unable to attach pseudo-device\n",
418 sc->sc_cfdata.cf_name, sc->sc_cfdata.cf_unit);
419
420 #if 0
421 rw_enter(&dkwedges_lock, RW_WRITER);
422 #endif
423 dkwedges[sc->sc_cfdata.cf_unit] = NULL;
424 #if 0
425 rw_exit(&dkwedges_lock);
426 #endif
427
428 (void) lockmgr(&pdk->dk_openlock, LK_EXCLUSIVE, NULL);
429 pdk->dk_nwedges--;
430 LIST_REMOVE(sc, sc_plink);
431 (void) lockmgr(&pdk->dk_openlock, LK_RELEASE, NULL);
432
433 bufq_free(sc->sc_bufq);
434 free(sc, M_DKWEDGE);
435 return (ENOMEM);
436 }
437 sc->sc_dk.dk_name = sc->sc_dev->dv_xname;
438
439 /* Return the devname to the caller. */
440 strcpy(dkw->dkw_devname, sc->sc_dev->dv_xname);
441
442 /*
443 * XXX Really ought to make the disk_attach() and the changing
444 * of state to RUNNING atomic.
445 */
446
447 disk_attach(&sc->sc_dk);
448
449 /* Disk wedge is ready for use! */
450 sc->sc_state = DKW_STATE_RUNNING;
451
452 /* Announce our arrival. */
453 aprint_normal("%s at %s: %s\n", sc->sc_dev->dv_xname, pdk->dk_name,
454 sc->sc_wname); /* XXX Unicode */
455 aprint_normal("%s: %"PRIu64" blocks at %"PRId64", type: %s\n",
456 sc->sc_dev->dv_xname, sc->sc_size, sc->sc_offset, sc->sc_ptype);
457
458 return (0);
459 }
460
461 /*
462 * dkwedge_del: [exported function]
463 *
464 * Delete a disk wedge based on the provided information.
465 * NOTE: We look up the wedge based on the wedge devname,
466 * not wname.
467 */
468 int
469 dkwedge_del(struct dkwedge_info *dkw)
470 {
471 struct dkwedge_softc *sc = NULL;
472 u_int unit;
473 int bmaj, cmaj, s;
474
475 /* Find our softc. */
476 dkw->dkw_devname[sizeof(dkw->dkw_devname) - 1] = '\0';
477 #if 0
478 rw_enter(&dkwedges_lock, RW_WRITER);
479 #endif
480 for (unit = 0; unit < ndkwedges; unit++) {
481 if ((sc = dkwedges[unit]) != NULL &&
482 strcmp(sc->sc_dev->dv_xname, dkw->dkw_devname) == 0 &&
483 strcmp(sc->sc_parent->dk_name, dkw->dkw_parent) == 0) {
484 /* Mark the wedge as dying. */
485 sc->sc_state = DKW_STATE_DYING;
486 break;
487 }
488 }
489 #if 0
490 rw_exit(&dkwedges_lock);
491 #endif
492 if (unit == ndkwedges)
493 return (ESRCH);
494
495 KASSERT(sc != NULL);
496
497 /* Locate the wedge major numbers. */
498 bmaj = bdevsw_lookup_major(&dk_bdevsw);
499 cmaj = cdevsw_lookup_major(&dk_cdevsw);
500
501 /* Kill any pending restart. */
502 callout_stop(&sc->sc_restart_ch);
503
504 /*
505 * dkstart() will kill any queued buffers now that the
506 * state of the wedge is not RUNNING. Once we've done
507 * that, wait for any other pending I/O to complete.
508 */
509 s = splbio();
510 dkstart(sc);
511 dkwedge_wait_drain(sc);
512 splx(s);
513
514 /* Nuke the vnodes for any open instances. */
515 vdevgone(bmaj, unit, unit, VBLK);
516 vdevgone(cmaj, unit, unit, VCHR);
517
518 /* Clean up the parent. */
519 (void) lockmgr(&sc->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL);
520 (void) lockmgr(&sc->sc_parent->dk_rawlock, LK_EXCLUSIVE, NULL);
521 if (sc->sc_dk.dk_openmask) {
522 if (sc->sc_parent->dk_rawopens-- == 1) {
523 KASSERT(sc->sc_parent->dk_rawvp != NULL);
524 (void) vn_close(sc->sc_parent->dk_rawvp, FREAD | FWRITE,
525 NOCRED, curlwp);
526 sc->sc_parent->dk_rawvp = NULL;
527 }
528 sc->sc_dk.dk_openmask = 0;
529 }
530 (void) lockmgr(&sc->sc_parent->dk_rawlock, LK_RELEASE, NULL);
531 (void) lockmgr(&sc->sc_dk.dk_openlock, LK_RELEASE, NULL);
532
533 /* Announce our departure. */
534 aprint_normal("%s at %s (%s) deleted\n", sc->sc_dev->dv_xname,
535 sc->sc_parent->dk_name,
536 sc->sc_wname); /* XXX Unicode */
537
538 /* Delete our pseudo-device. */
539 (void) config_detach(sc->sc_dev, DETACH_FORCE | DETACH_QUIET);
540
541 (void) lockmgr(&sc->sc_parent->dk_openlock, LK_EXCLUSIVE, NULL);
542 sc->sc_parent->dk_nwedges--;
543 LIST_REMOVE(sc, sc_plink);
544 (void) lockmgr(&sc->sc_parent->dk_openlock, LK_RELEASE, NULL);
545
546 /* Delete our buffer queue. */
547 bufq_free(sc->sc_bufq);
548
549 /* Detach from the disk list. */
550 disk_detach(&sc->sc_dk);
551
552 /* Poof. */
553 #if 0
554 rw_enter(&dkwedges_lock, RW_WRITER);
555 #endif
556 dkwedges[unit] = NULL;
557 sc->sc_state = DKW_STATE_DEAD;
558 #if 0
559 rw_exit(&dkwedges_lock);
560 #endif
561
562 free(sc, M_DKWEDGE);
563
564 return (0);
565 }
566
567 /*
568 * dkwedge_delall: [exported function]
569 *
570 * Delete all of the wedges on the specified disk. Used when
571 * a disk is being detached.
572 */
573 void
574 dkwedge_delall(struct disk *pdk)
575 {
576 struct dkwedge_info dkw;
577 struct dkwedge_softc *sc;
578
579 for (;;) {
580 (void) lockmgr(&pdk->dk_openlock, LK_EXCLUSIVE, NULL);
581 if ((sc = LIST_FIRST(&pdk->dk_wedges)) == NULL) {
582 KASSERT(pdk->dk_nwedges == 0);
583 (void) lockmgr(&pdk->dk_openlock, LK_RELEASE, NULL);
584 return;
585 }
586 strcpy(dkw.dkw_parent, pdk->dk_name);
587 strcpy(dkw.dkw_devname, sc->sc_dev->dv_xname);
588 (void) lockmgr(&pdk->dk_openlock, LK_RELEASE, NULL);
589 (void) dkwedge_del(&dkw);
590 }
591 }
592
593 /*
594 * dkwedge_list: [exported function]
595 *
596 * List all of the wedges on a particular disk.
597 * If p == NULL, the buffer is in kernel space. Otherwise, it is
598 * in user space of the specified process.
599 */
600 int
601 dkwedge_list(struct disk *pdk, struct dkwedge_list *dkwl, struct lwp *l)
602 {
603 struct uio uio;
604 struct iovec iov;
605 struct dkwedge_softc *sc;
606 struct dkwedge_info dkw;
607 struct vmspace *vm;
608 int error = 0;
609
610 iov.iov_base = dkwl->dkwl_buf;
611 iov.iov_len = dkwl->dkwl_bufsize;
612
613 uio.uio_iov = &iov;
614 uio.uio_iovcnt = 1;
615 uio.uio_offset = 0;
616 uio.uio_resid = dkwl->dkwl_bufsize;
617 uio.uio_rw = UIO_READ;
618 if (l == NULL) {
619 UIO_SETUP_SYSSPACE(&uio);
620 } else {
621 error = proc_vmspace_getref(l->l_proc, &vm);
622 if (error) {
623 return error;
624 }
625 uio.uio_vmspace = vm;
626 }
627
628 dkwl->dkwl_ncopied = 0;
629
630 (void) lockmgr(&pdk->dk_openlock, LK_EXCLUSIVE, NULL);
631 LIST_FOREACH(sc, &pdk->dk_wedges, sc_plink) {
632 if (uio.uio_resid < sizeof(dkw))
633 break;
634
635 if (sc->sc_state != DKW_STATE_RUNNING)
636 continue;
637
638 strcpy(dkw.dkw_devname, sc->sc_dev->dv_xname);
639 memcpy(dkw.dkw_wname, sc->sc_wname, sizeof(dkw.dkw_wname));
640 dkw.dkw_wname[sizeof(dkw.dkw_wname) - 1] = '\0';
641 strcpy(dkw.dkw_parent, sc->sc_parent->dk_name);
642 dkw.dkw_offset = sc->sc_offset;
643 dkw.dkw_size = sc->sc_size;
644 strcpy(dkw.dkw_ptype, sc->sc_ptype);
645
646 error = uiomove(&dkw, sizeof(dkw), &uio);
647 if (error)
648 break;
649 dkwl->dkwl_ncopied++;
650 }
651 dkwl->dkwl_nwedges = pdk->dk_nwedges;
652 (void) lockmgr(&pdk->dk_openlock, LK_RELEASE, NULL);
653
654 if (l != NULL) {
655 uvmspace_free(vm);
656 }
657
658 return (error);
659 }
660
661 /*
662 * dkwedge_set_bootwedge
663 *
664 * Set the booted_wedge global based on the specified parent name
665 * and offset/length.
666 */
667 void
668 dkwedge_set_bootwedge(struct device *parent, daddr_t startblk, uint64_t nblks)
669 {
670 struct dkwedge_softc *sc;
671 int i;
672
673 #if 0
674 rw_enter(&dkwedges_lock, RW_WRITER);
675 #endif
676 for (i = 0; i < ndkwedges; i++) {
677 if ((sc = dkwedges[i]) == NULL)
678 continue;
679 if (strcmp(sc->sc_parent->dk_name, parent->dv_xname) == 0 &&
680 sc->sc_offset == startblk &&
681 sc->sc_size == nblks) {
682 if (booted_wedge) {
683 printf("WARNING: double match for boot wedge "
684 "(%s, %s)\n",
685 booted_wedge->dv_xname,
686 sc->sc_dev->dv_xname);
687 continue;
688 }
689 booted_device = parent;
690 booted_wedge = sc->sc_dev;
691 booted_partition = 0;
692 }
693 }
694 /*
695 * XXX What if we don't find one? Should we create a special
696 * XXX root wedge?
697 */
698 #if 0
699 rw_exit(&dkwedges_lock);
700 #endif
701 }
702
703 /*
704 * We need a dummy object to stuff into the dkwedge discovery method link
705 * set to ensure that there is always at least one object in the set.
706 */
707 static struct dkwedge_discovery_method dummy_discovery_method;
708 __link_set_add_bss(dkwedge_methods, dummy_discovery_method);
709
710 /*
711 * dkwedge_discover_init:
712 *
713 * Initialize the disk wedge discovery method list.
714 */
715 static void
716 dkwedge_discover_init(void)
717 {
718 __link_set_decl(dkwedge_methods, struct dkwedge_discovery_method);
719 struct dkwedge_discovery_method * const *ddmp;
720 struct dkwedge_discovery_method *lddm, *ddm;
721
722 if (dkwedge_discovery_methods_initialized)
723 return;
724 dkwedge_discovery_methods_initialized = 1;
725
726 rw_init(&dkwedge_discovery_methods_lock);
727 rw_enter(&dkwedge_discovery_methods_lock, RW_WRITER);
728
729 LIST_INIT(&dkwedge_discovery_methods);
730
731 __link_set_foreach(ddmp, dkwedge_methods) {
732 ddm = *ddmp;
733 if (ddm == &dummy_discovery_method)
734 continue;
735 if (LIST_EMPTY(&dkwedge_discovery_methods)) {
736 LIST_INSERT_HEAD(&dkwedge_discovery_methods,
737 ddm, ddm_list);
738 continue;
739 }
740 LIST_FOREACH(lddm, &dkwedge_discovery_methods, ddm_list) {
741 if (ddm->ddm_priority == lddm->ddm_priority) {
742 aprint_error("dk-method-%s: method \"%s\" "
743 "already exists at priority %d\n",
744 ddm->ddm_name, lddm->ddm_name,
745 lddm->ddm_priority);
746 /* Not inserted. */
747 break;
748 }
749 if (ddm->ddm_priority < lddm->ddm_priority) {
750 /* Higher priority; insert before. */
751 LIST_INSERT_BEFORE(lddm, ddm, ddm_list);
752 break;
753 }
754 if (LIST_NEXT(lddm, ddm_list) == NULL) {
755 /* Last one; insert after. */
756 KASSERT(lddm->ddm_priority < ddm->ddm_priority);
757 LIST_INSERT_AFTER(lddm, ddm, ddm_list);
758 break;
759 }
760 }
761 }
762
763 rw_exit(&dkwedge_discovery_methods_lock);
764 }
765
766 #ifdef DKWEDGE_AUTODISCOVER
767 int dkwedge_autodiscover = 1;
768 #else
769 int dkwedge_autodiscover = 0;
770 #endif
771
772 /*
773 * dkwedge_discover: [exported function]
774 *
775 * Discover the wedges on a newly attached disk.
776 */
777 void
778 dkwedge_discover(struct disk *pdk)
779 {
780 struct dkwedge_discovery_method *ddm;
781 struct vnode *vp;
782 int error;
783 dev_t pdev;
784
785 /*
786 * Require people playing with wedges to enable this explicitly.
787 */
788 if (dkwedge_autodiscover == 0)
789 return;
790
791 if (dkwedge_discovery_methods_initialized == 0)
792 dkwedge_discover_init();
793
794 rw_enter(&dkwedge_discovery_methods_lock, RW_READER);
795
796 error = dkwedge_compute_pdev(pdk->dk_name, &pdev);
797 if (error) {
798 aprint_error("%s: unable to compute pdev, error = %d\n",
799 pdk->dk_name, error);
800 goto out;
801 }
802
803 error = bdevvp(pdev, &vp);
804 if (error) {
805 aprint_error("%s: unable to find vnode for pdev, error = %d\n",
806 pdk->dk_name, error);
807 goto out;
808 }
809
810 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
811 if (error) {
812 aprint_error("%s: unable to lock vnode for pdev, error = %d\n",
813 pdk->dk_name, error);
814 vrele(vp);
815 goto out;
816 }
817
818 error = VOP_OPEN(vp, FREAD, NOCRED, 0);
819 if (error) {
820 aprint_error("%s: unable to open device, error = %d\n",
821 pdk->dk_name, error);
822 vput(vp);
823 goto out;
824 }
825 VOP_UNLOCK(vp, 0);
826
827 /*
828 * For each supported partition map type, look to see if
829 * this map type exists. If so, parse it and add the
830 * corresponding wedges.
831 */
832 LIST_FOREACH(ddm, &dkwedge_discovery_methods, ddm_list) {
833 error = (*ddm->ddm_discover)(pdk, vp);
834 if (error == 0) {
835 /* Successfully created wedges; we're done. */
836 break;
837 }
838 }
839
840 error = vn_close(vp, FREAD, NOCRED, curlwp);
841 if (error) {
842 aprint_error("%s: unable to close device, error = %d\n",
843 pdk->dk_name, error);
844 /* We'll just assume the vnode has been cleaned up. */
845 }
846 out:
847 rw_exit(&dkwedge_discovery_methods_lock);
848 }
849
850 /*
851 * dkwedge_read:
852 *
853 * Read the some data from the specified disk, used for
854 * partition discovery.
855 */
856 int
857 dkwedge_read(struct disk *pdk, struct vnode *vp, daddr_t blkno,
858 void *tbuf, size_t len)
859 {
860 struct buf b;
861
862 BUF_INIT(&b);
863
864 b.b_vp = vp;
865 b.b_dev = vp->v_rdev;
866 b.b_blkno = blkno;
867 b.b_bcount = b.b_resid = len;
868 b.b_flags = B_READ;
869 b.b_proc = curproc;
870 b.b_data = tbuf;
871
872 VOP_STRATEGY(vp, &b);
873 return (biowait(&b));
874 }
875
876 /*
877 * dkwedge_lookup:
878 *
879 * Look up a dkwedge_softc based on the provided dev_t.
880 */
881 static struct dkwedge_softc *
882 dkwedge_lookup(dev_t dev)
883 {
884 int unit = minor(dev);
885
886 if (unit >= ndkwedges)
887 return (NULL);
888
889 KASSERT(dkwedges != NULL);
890
891 return (dkwedges[unit]);
892 }
893
894 /*
895 * dkopen: [devsw entry point]
896 *
897 * Open a wedge.
898 */
899 static int
900 dkopen(dev_t dev, int flags, int fmt, struct lwp *l)
901 {
902 struct dkwedge_softc *sc = dkwedge_lookup(dev);
903 struct vnode *vp;
904 int error = 0;
905
906 if (sc == NULL)
907 return (ENODEV);
908
909 if (sc->sc_state != DKW_STATE_RUNNING)
910 return (ENXIO);
911
912 /*
913 * We go through a complicated little dance to only open the parent
914 * vnode once per wedge, no matter how many times the wedge is
915 * opened. The reason? We see one dkopen() per open call, but
916 * only dkclose() on the last close.
917 */
918 (void) lockmgr(&sc->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL);
919 (void) lockmgr(&sc->sc_parent->dk_rawlock, LK_EXCLUSIVE, NULL);
920 if (sc->sc_dk.dk_openmask == 0) {
921 if (sc->sc_parent->dk_rawopens++ == 0) {
922 KASSERT(sc->sc_parent->dk_rawvp == NULL);
923 error = bdevvp(sc->sc_pdev, &vp);
924 if (error)
925 goto popen_fail;
926 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
927 if (error) {
928 vrele(vp);
929 goto popen_fail;
930 }
931 error = VOP_OPEN(vp, FREAD | FWRITE, NOCRED, 0);
932 if (error) {
933 vput(vp);
934 goto popen_fail;
935 }
936 /* VOP_OPEN() doesn't do this for us. */
937 vp->v_writecount++;
938 VOP_UNLOCK(vp, 0);
939 sc->sc_parent->dk_rawvp = vp;
940 }
941 }
942 if (fmt == S_IFCHR)
943 sc->sc_dk.dk_copenmask |= 1;
944 else
945 sc->sc_dk.dk_bopenmask |= 1;
946 sc->sc_dk.dk_openmask =
947 sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
948
949 popen_fail:
950 (void) lockmgr(&sc->sc_parent->dk_rawlock, LK_RELEASE, NULL);
951 (void) lockmgr(&sc->sc_dk.dk_openlock, LK_RELEASE, NULL);
952 return (error);
953 }
954
955 /*
956 * dkclose: [devsw entry point]
957 *
958 * Close a wedge.
959 */
960 static int
961 dkclose(dev_t dev, int flags, int fmt, struct lwp *l)
962 {
963 struct dkwedge_softc *sc = dkwedge_lookup(dev);
964 int error = 0;
965
966 KASSERT(sc->sc_dk.dk_openmask != 0);
967
968 (void) lockmgr(&sc->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL);
969 (void) lockmgr(&sc->sc_parent->dk_rawlock, LK_EXCLUSIVE, NULL);
970
971 if (fmt == S_IFCHR)
972 sc->sc_dk.dk_copenmask &= ~1;
973 else
974 sc->sc_dk.dk_bopenmask &= ~1;
975 sc->sc_dk.dk_openmask =
976 sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
977
978 if (sc->sc_dk.dk_openmask == 0) {
979 if (sc->sc_parent->dk_rawopens-- == 1) {
980 KASSERT(sc->sc_parent->dk_rawvp != NULL);
981 error = vn_close(sc->sc_parent->dk_rawvp,
982 FREAD | FWRITE, NOCRED, l);
983 sc->sc_parent->dk_rawvp = NULL;
984 }
985 }
986
987 (void) lockmgr(&sc->sc_parent->dk_rawlock, LK_RELEASE, NULL);
988 (void) lockmgr(&sc->sc_dk.dk_openlock, LK_RELEASE, NULL);
989
990 return (error);
991 }
992
993 /*
994 * dkstragegy: [devsw entry point]
995 *
996 * Perform I/O based on the wedge I/O strategy.
997 */
998 static void
999 dkstrategy(struct buf *bp)
1000 {
1001 struct dkwedge_softc *sc = dkwedge_lookup(bp->b_dev);
1002 int s;
1003
1004 if (sc->sc_state != DKW_STATE_RUNNING) {
1005 bp->b_error = ENXIO;
1006 bp->b_flags |= B_ERROR;
1007 goto done;
1008 }
1009
1010 /* If it's an empty transfer, wake up the top half now. */
1011 if (bp->b_bcount == 0)
1012 goto done;
1013
1014 /* Make sure it's in-range. */
1015 if (bounds_check_with_mediasize(bp, DEV_BSIZE, sc->sc_size) <= 0)
1016 goto done;
1017
1018 /* Translate it to the parent's raw LBA. */
1019 bp->b_rawblkno = bp->b_blkno + sc->sc_offset;
1020
1021 /* Place it in the queue and start I/O on the unit. */
1022 s = splbio();
1023 sc->sc_iopend++;
1024 BUFQ_PUT(sc->sc_bufq, bp);
1025 dkstart(sc);
1026 splx(s);
1027 return;
1028
1029 done:
1030 bp->b_resid = bp->b_bcount;
1031 biodone(bp);
1032 }
1033
1034 /*
1035 * dkstart:
1036 *
1037 * Start I/O that has been enqueued on the wedge.
1038 * NOTE: Must be called at splbio()!
1039 */
1040 static void
1041 dkstart(struct dkwedge_softc *sc)
1042 {
1043 struct buf *bp, *nbp;
1044
1045 /* Do as much work as has been enqueued. */
1046 while ((bp = BUFQ_PEEK(sc->sc_bufq)) != NULL) {
1047 if (sc->sc_state != DKW_STATE_RUNNING) {
1048 (void) BUFQ_GET(sc->sc_bufq);
1049 if (sc->sc_iopend-- == 1 &&
1050 (sc->sc_flags & DK_F_WAIT_DRAIN) != 0) {
1051 sc->sc_flags &= ~DK_F_WAIT_DRAIN;
1052 wakeup(&sc->sc_iopend);
1053 }
1054 bp->b_error = ENXIO;
1055 bp->b_flags |= B_ERROR;
1056 bp->b_resid = bp->b_bcount;
1057 biodone(bp);
1058 }
1059
1060 /* Instrumentation. */
1061 disk_busy(&sc->sc_dk);
1062
1063 nbp = getiobuf_nowait();
1064 if (nbp == NULL) {
1065 /*
1066 * No resources to run this request; leave the
1067 * buffer queued up, and schedule a timer to
1068 * restart the queue in 1/2 a second.
1069 */
1070 disk_unbusy(&sc->sc_dk, 0, bp->b_flags & B_READ);
1071 callout_schedule(&sc->sc_restart_ch, hz / 2);
1072 return;
1073 }
1074
1075 (void) BUFQ_GET(sc->sc_bufq);
1076
1077 BUF_INIT(nbp);
1078 nbp->b_data = bp->b_data;
1079 nbp->b_flags = bp->b_flags | B_CALL;
1080 nbp->b_iodone = dkiodone;
1081 nbp->b_proc = bp->b_proc;
1082 nbp->b_blkno = bp->b_rawblkno;
1083 nbp->b_dev = sc->sc_parent->dk_rawvp->v_rdev;
1084 nbp->b_vp = sc->sc_parent->dk_rawvp;
1085 nbp->b_bcount = bp->b_bcount;
1086 nbp->b_private = bp;
1087 BIO_COPYPRIO(nbp, bp);
1088
1089 if ((nbp->b_flags & B_READ) == 0)
1090 V_INCR_NUMOUTPUT(nbp->b_vp);
1091 VOP_STRATEGY(nbp->b_vp, nbp);
1092 }
1093 }
1094
1095 /*
1096 * dkiodone:
1097 *
1098 * I/O to a wedge has completed; alert the top half.
1099 * NOTE: Must be called at splbio()!
1100 */
1101 static void
1102 dkiodone(struct buf *bp)
1103 {
1104 struct buf *obp = bp->b_private;
1105 struct dkwedge_softc *sc = dkwedge_lookup(obp->b_dev);
1106
1107 if (bp->b_flags & B_ERROR) {
1108 obp->b_flags |= B_ERROR;
1109 obp->b_error = bp->b_error;
1110 }
1111 obp->b_resid = bp->b_resid;
1112 putiobuf(bp);
1113
1114 if (sc->sc_iopend-- == 1 && (sc->sc_flags & DK_F_WAIT_DRAIN) != 0) {
1115 sc->sc_flags &= ~DK_F_WAIT_DRAIN;
1116 wakeup(&sc->sc_iopend);
1117 }
1118
1119 disk_unbusy(&sc->sc_dk, obp->b_bcount - obp->b_resid,
1120 obp->b_flags & B_READ);
1121
1122 biodone(obp);
1123
1124 /* Kick the queue in case there is more work we can do. */
1125 dkstart(sc);
1126 }
1127
1128 /*
1129 * dkrestart:
1130 *
1131 * Restart the work queue after it was stalled due to
1132 * a resource shortage. Invoked via a callout.
1133 */
1134 static void
1135 dkrestart(void *v)
1136 {
1137 struct dkwedge_softc *sc = v;
1138 int s;
1139
1140 s = splbio();
1141 dkstart(sc);
1142 splx(s);
1143 }
1144
1145 /*
1146 * dkread: [devsw entry point]
1147 *
1148 * Read from a wedge.
1149 */
1150 static int
1151 dkread(dev_t dev, struct uio *uio, int flags)
1152 {
1153 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1154
1155 if (sc->sc_state != DKW_STATE_RUNNING)
1156 return (ENXIO);
1157
1158 return (physio(dkstrategy, NULL, dev, B_READ,
1159 sc->sc_parent->dk_driver->d_minphys, uio));
1160 }
1161
1162 /*
1163 * dkwrite: [devsw entry point]
1164 *
1165 * Write to a wedge.
1166 */
1167 static int
1168 dkwrite(dev_t dev, struct uio *uio, int flags)
1169 {
1170 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1171
1172 if (sc->sc_state != DKW_STATE_RUNNING)
1173 return (ENXIO);
1174
1175 return (physio(dkstrategy, NULL, dev, B_WRITE,
1176 sc->sc_parent->dk_driver->d_minphys, uio));
1177 }
1178
1179 /*
1180 * dkioctl: [devsw entry point]
1181 *
1182 * Perform an ioctl request on a wedge.
1183 */
1184 static int
1185 dkioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
1186 {
1187 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1188 int error = 0;
1189
1190 if (sc->sc_state != DKW_STATE_RUNNING)
1191 return (ENXIO);
1192
1193 switch (cmd) {
1194 case DIOCCACHESYNC:
1195 /*
1196 * XXX Do we really need to care about having a writable
1197 * file descriptor here?
1198 */
1199 if ((flag & FWRITE) == 0)
1200 error = EBADF;
1201 else
1202 error = VOP_IOCTL(sc->sc_parent->dk_rawvp,
1203 cmd, data, flag,
1204 l != NULL ? l->l_cred : NOCRED, l);
1205 break;
1206 case DIOCGWEDGEINFO:
1207 {
1208 struct dkwedge_info *dkw = (void *) data;
1209
1210 strcpy(dkw->dkw_devname, sc->sc_dev->dv_xname);
1211 memcpy(dkw->dkw_wname, sc->sc_wname, sizeof(dkw->dkw_wname));
1212 dkw->dkw_wname[sizeof(dkw->dkw_wname) - 1] = '\0';
1213 strcpy(dkw->dkw_parent, sc->sc_parent->dk_name);
1214 dkw->dkw_offset = sc->sc_offset;
1215 dkw->dkw_size = sc->sc_size;
1216 strcpy(dkw->dkw_ptype, sc->sc_ptype);
1217
1218 break;
1219 }
1220
1221 default:
1222 error = ENOTTY;
1223 }
1224
1225 return (error);
1226 }
1227
1228 /*
1229 * dksize: [devsw entry point]
1230 *
1231 * Query the size of a wedge for the purpose of performing a dump
1232 * or for swapping to.
1233 */
1234 static int
1235 dksize(dev_t dev)
1236 {
1237 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1238 int rv = -1;
1239
1240 if (sc == NULL)
1241 return (-1);
1242
1243 if (sc->sc_state != DKW_STATE_RUNNING)
1244 return (ENXIO);
1245
1246 (void) lockmgr(&sc->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL);
1247 (void) lockmgr(&sc->sc_parent->dk_rawlock, LK_EXCLUSIVE, NULL);
1248
1249 /* Our content type is static, no need to open the device. */
1250
1251 if (strcmp(sc->sc_ptype, DKW_PTYPE_SWAP) == 0) {
1252 /* Saturate if we are larger than INT_MAX. */
1253 if (sc->sc_size > INT_MAX)
1254 rv = INT_MAX;
1255 else
1256 rv = (int) sc->sc_size;
1257 }
1258
1259 (void) lockmgr(&sc->sc_parent->dk_rawlock, LK_RELEASE, NULL);
1260 (void) lockmgr(&sc->sc_dk.dk_openlock, LK_RELEASE, NULL);
1261
1262 return (rv);
1263 }
1264
1265 /*
1266 * dkdump: [devsw entry point]
1267 *
1268 * Perform a crash dump to a wedge.
1269 */
1270 static int
1271 dkdump(dev_t dev, daddr_t blkno, void *va,
1272 size_t size)
1273 {
1274
1275 /* XXX */
1276 return (ENXIO);
1277 }
1278