No empty .Rs/.Re
[netbsd-mini2440.git] / sys / dev / dkwedge / dk.c
blob523745cbc54a7a7f60fd230becc0d2b11fee928d
1 /* $NetBSD: dk.c,v 1.51 2009/09/08 21:14:33 pooka Exp $ */
3 /*-
4 * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.51 2009/09/08 21:14:33 pooka Exp $");
35 #ifdef _KERNEL_OPT
36 #include "opt_dkwedge.h"
37 #endif
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/proc.h>
42 #include <sys/errno.h>
43 #include <sys/pool.h>
44 #include <sys/ioctl.h>
45 #include <sys/disklabel.h>
46 #include <sys/disk.h>
47 #include <sys/fcntl.h>
48 #include <sys/buf.h>
49 #include <sys/bufq.h>
50 #include <sys/vnode.h>
51 #include <sys/stat.h>
52 #include <sys/conf.h>
53 #include <sys/callout.h>
54 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56 #include <sys/device.h>
57 #include <sys/kauth.h>
59 #include <miscfs/specfs/specdev.h>
61 MALLOC_DEFINE(M_DKWEDGE, "dkwedge", "Disk wedge structures");
63 typedef enum {
64 DKW_STATE_LARVAL = 0,
65 DKW_STATE_RUNNING = 1,
66 DKW_STATE_DYING = 2,
67 DKW_STATE_DEAD = 666
68 } dkwedge_state_t;
70 struct dkwedge_softc {
71 struct device *sc_dev; /* pointer to our pseudo-device */
72 struct cfdata sc_cfdata; /* our cfdata structure */
73 uint8_t sc_wname[128]; /* wedge name (Unicode, UTF-8) */
75 dkwedge_state_t sc_state; /* state this wedge is in */
77 struct disk *sc_parent; /* parent disk */
78 daddr_t sc_offset; /* LBA offset of wedge in parent */
79 uint64_t sc_size; /* size of wedge in blocks */
80 char sc_ptype[32]; /* partition type */
81 dev_t sc_pdev; /* cached parent's dev_t */
82 /* link on parent's wedge list */
83 LIST_ENTRY(dkwedge_softc) sc_plink;
85 struct disk sc_dk; /* our own disk structure */
86 struct bufq_state *sc_bufq; /* buffer queue */
87 struct callout sc_restart_ch; /* callout to restart I/O */
89 u_int sc_iopend; /* I/Os pending */
90 int sc_flags; /* flags (splbio) */
93 #define DK_F_WAIT_DRAIN 0x0001 /* waiting for I/O to drain */
95 static void dkstart(struct dkwedge_softc *);
96 static void dkiodone(struct buf *);
97 static void dkrestart(void *);
98 static void dkminphys(struct buf *);
100 static int dklastclose(struct dkwedge_softc *);
101 static int dkwedge_detach(device_t, int);
103 static dev_type_open(dkopen);
104 static dev_type_close(dkclose);
105 static dev_type_read(dkread);
106 static dev_type_write(dkwrite);
107 static dev_type_ioctl(dkioctl);
108 static dev_type_strategy(dkstrategy);
109 static dev_type_dump(dkdump);
110 static dev_type_size(dksize);
112 const struct bdevsw dk_bdevsw = {
113 dkopen, dkclose, dkstrategy, dkioctl, dkdump, dksize, D_DISK
116 const struct cdevsw dk_cdevsw = {
117 dkopen, dkclose, dkread, dkwrite, dkioctl,
118 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
121 static struct dkwedge_softc **dkwedges;
122 static u_int ndkwedges;
123 static krwlock_t dkwedges_lock;
125 static LIST_HEAD(, dkwedge_discovery_method) dkwedge_discovery_methods;
126 static krwlock_t dkwedge_discovery_methods_lock;
129 * dkwedge_match:
131 * Autoconfiguration match function for pseudo-device glue.
133 static int
134 dkwedge_match(device_t parent, cfdata_t match,
135 void *aux)
138 /* Pseudo-device; always present. */
139 return (1);
143 * dkwedge_attach:
145 * Autoconfiguration attach function for pseudo-device glue.
147 static void
148 dkwedge_attach(device_t parent, device_t self,
149 void *aux)
152 if (!pmf_device_register(self, NULL, NULL))
153 aprint_error_dev(self, "couldn't establish power handler\n");
156 CFDRIVER_DECL(dk, DV_DISK, NULL);
157 CFATTACH_DECL3_NEW(dk, 0,
158 dkwedge_match, dkwedge_attach, dkwedge_detach, NULL, NULL, NULL,
159 DVF_DETACH_SHUTDOWN);
162 * dkwedge_wait_drain:
164 * Wait for I/O on the wedge to drain.
165 * NOTE: Must be called at splbio()!
167 static void
168 dkwedge_wait_drain(struct dkwedge_softc *sc)
171 while (sc->sc_iopend != 0) {
172 sc->sc_flags |= DK_F_WAIT_DRAIN;
173 (void) tsleep(&sc->sc_iopend, PRIBIO, "dkdrn", 0);
178 * dkwedge_compute_pdev:
180 * Compute the parent disk's dev_t.
182 static int
183 dkwedge_compute_pdev(const char *pname, dev_t *pdevp)
185 const char *name, *cp;
186 int punit, pmaj;
187 char devname[16];
189 name = pname;
190 if ((pmaj = devsw_name2blk(name, devname, sizeof(devname))) == -1)
191 return (ENODEV);
193 name += strlen(devname);
194 for (cp = name, punit = 0; *cp >= '0' && *cp <= '9'; cp++)
195 punit = (punit * 10) + (*cp - '0');
196 if (cp == name) {
197 /* Invalid parent disk name. */
198 return (ENODEV);
201 *pdevp = MAKEDISKDEV(pmaj, punit, RAW_PART);
203 return (0);
207 * dkwedge_array_expand:
209 * Expand the dkwedges array.
211 static void
212 dkwedge_array_expand(void)
214 int newcnt = ndkwedges + 16;
215 struct dkwedge_softc **newarray, **oldarray;
217 newarray = malloc(newcnt * sizeof(*newarray), M_DKWEDGE,
218 M_WAITOK|M_ZERO);
219 if ((oldarray = dkwedges) != NULL)
220 memcpy(newarray, dkwedges, ndkwedges * sizeof(*newarray));
221 dkwedges = newarray;
222 ndkwedges = newcnt;
223 if (oldarray != NULL)
224 free(oldarray, M_DKWEDGE);
227 static void
228 dkgetproperties(struct disk *disk, struct dkwedge_info *dkw)
230 prop_dictionary_t disk_info, odisk_info, geom;
232 disk_info = prop_dictionary_create();
234 prop_dictionary_set_cstring_nocopy(disk_info, "type", "ESDI");
236 geom = prop_dictionary_create();
238 prop_dictionary_set_uint64(geom, "sectors-per-unit", dkw->dkw_size);
240 prop_dictionary_set_uint32(geom, "sector-size",
241 DEV_BSIZE /* XXX 512? */);
243 prop_dictionary_set_uint32(geom, "sectors-per-track", 32);
245 prop_dictionary_set_uint32(geom, "tracks-per-cylinder", 64);
247 prop_dictionary_set_uint32(geom, "cylinders-per-unit", dkw->dkw_size / 2048);
249 prop_dictionary_set(disk_info, "geometry", geom);
250 prop_object_release(geom);
252 odisk_info = disk->dk_info;
254 disk->dk_info = disk_info;
256 if (odisk_info != NULL)
257 prop_object_release(odisk_info);
261 * dkwedge_add: [exported function]
263 * Add a disk wedge based on the provided information.
265 * The incoming dkw_devname[] is ignored, instead being
266 * filled in and returned to the caller.
269 dkwedge_add(struct dkwedge_info *dkw)
271 struct dkwedge_softc *sc, *lsc;
272 struct disk *pdk;
273 u_int unit;
274 int error;
275 dev_t pdev;
277 dkw->dkw_parent[sizeof(dkw->dkw_parent) - 1] = '\0';
278 pdk = disk_find(dkw->dkw_parent);
279 if (pdk == NULL)
280 return (ENODEV);
282 error = dkwedge_compute_pdev(pdk->dk_name, &pdev);
283 if (error)
284 return (error);
286 if (dkw->dkw_offset < 0)
287 return (EINVAL);
289 sc = malloc(sizeof(*sc), M_DKWEDGE, M_WAITOK|M_ZERO);
290 sc->sc_state = DKW_STATE_LARVAL;
291 sc->sc_parent = pdk;
292 sc->sc_pdev = pdev;
293 sc->sc_offset = dkw->dkw_offset;
294 sc->sc_size = dkw->dkw_size;
296 memcpy(sc->sc_wname, dkw->dkw_wname, sizeof(sc->sc_wname));
297 sc->sc_wname[sizeof(sc->sc_wname) - 1] = '\0';
299 memcpy(sc->sc_ptype, dkw->dkw_ptype, sizeof(sc->sc_ptype));
300 sc->sc_ptype[sizeof(sc->sc_ptype) - 1] = '\0';
302 bufq_alloc(&sc->sc_bufq, "fcfs", 0);
304 callout_init(&sc->sc_restart_ch, 0);
305 callout_setfunc(&sc->sc_restart_ch, dkrestart, sc);
308 * Wedge will be added; increment the wedge count for the parent.
309 * Only allow this to happend if RAW_PART is the only thing open.
311 mutex_enter(&pdk->dk_openlock);
312 if (pdk->dk_openmask & ~(1 << RAW_PART))
313 error = EBUSY;
314 else {
315 /* Check for wedge overlap. */
316 LIST_FOREACH(lsc, &pdk->dk_wedges, sc_plink) {
317 daddr_t lastblk = sc->sc_offset + sc->sc_size - 1;
318 daddr_t llastblk = lsc->sc_offset + lsc->sc_size - 1;
320 if (sc->sc_offset >= lsc->sc_offset &&
321 sc->sc_offset <= llastblk) {
322 /* Overlaps the tail of the exsiting wedge. */
323 break;
325 if (lastblk >= lsc->sc_offset &&
326 lastblk <= llastblk) {
327 /* Overlaps the head of the existing wedge. */
328 break;
331 if (lsc != NULL)
332 error = EINVAL;
333 else {
334 pdk->dk_nwedges++;
335 LIST_INSERT_HEAD(&pdk->dk_wedges, sc, sc_plink);
338 mutex_exit(&pdk->dk_openlock);
339 if (error) {
340 bufq_free(sc->sc_bufq);
341 free(sc, M_DKWEDGE);
342 return (error);
345 /* Fill in our cfdata for the pseudo-device glue. */
346 sc->sc_cfdata.cf_name = dk_cd.cd_name;
347 sc->sc_cfdata.cf_atname = dk_ca.ca_name;
348 /* sc->sc_cfdata.cf_unit set below */
349 sc->sc_cfdata.cf_fstate = FSTATE_STAR;
351 /* Insert the larval wedge into the array. */
352 rw_enter(&dkwedges_lock, RW_WRITER);
353 for (error = 0;;) {
354 struct dkwedge_softc **scpp;
357 * Check for a duplicate wname while searching for
358 * a slot.
360 for (scpp = NULL, unit = 0; unit < ndkwedges; unit++) {
361 if (dkwedges[unit] == NULL) {
362 if (scpp == NULL) {
363 scpp = &dkwedges[unit];
364 sc->sc_cfdata.cf_unit = unit;
366 } else {
367 /* XXX Unicode. */
368 if (strcmp(dkwedges[unit]->sc_wname,
369 sc->sc_wname) == 0) {
370 error = EEXIST;
371 break;
375 if (error)
376 break;
377 KASSERT(unit == ndkwedges);
378 if (scpp == NULL)
379 dkwedge_array_expand();
380 else {
381 KASSERT(scpp == &dkwedges[sc->sc_cfdata.cf_unit]);
382 *scpp = sc;
383 break;
386 rw_exit(&dkwedges_lock);
387 if (error) {
388 mutex_enter(&pdk->dk_openlock);
389 pdk->dk_nwedges--;
390 LIST_REMOVE(sc, sc_plink);
391 mutex_exit(&pdk->dk_openlock);
393 bufq_free(sc->sc_bufq);
394 free(sc, M_DKWEDGE);
395 return (error);
399 * Now that we know the unit #, attach a pseudo-device for
400 * this wedge instance. This will provide us with the
401 * "struct device" necessary for glue to other parts of the
402 * system.
404 * This should never fail, unless we're almost totally out of
405 * memory.
407 if ((sc->sc_dev = config_attach_pseudo(&sc->sc_cfdata)) == NULL) {
408 aprint_error("%s%u: unable to attach pseudo-device\n",
409 sc->sc_cfdata.cf_name, sc->sc_cfdata.cf_unit);
411 rw_enter(&dkwedges_lock, RW_WRITER);
412 dkwedges[sc->sc_cfdata.cf_unit] = NULL;
413 rw_exit(&dkwedges_lock);
415 mutex_enter(&pdk->dk_openlock);
416 pdk->dk_nwedges--;
417 LIST_REMOVE(sc, sc_plink);
418 mutex_exit(&pdk->dk_openlock);
420 bufq_free(sc->sc_bufq);
421 free(sc, M_DKWEDGE);
422 return (ENOMEM);
425 /* Return the devname to the caller. */
426 strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev),
427 sizeof(dkw->dkw_devname));
430 * XXX Really ought to make the disk_attach() and the changing
431 * of state to RUNNING atomic.
434 disk_init(&sc->sc_dk, device_xname(sc->sc_dev), NULL);
435 dkgetproperties(&sc->sc_dk, dkw);
436 disk_attach(&sc->sc_dk);
438 /* Disk wedge is ready for use! */
439 sc->sc_state = DKW_STATE_RUNNING;
441 /* Announce our arrival. */
442 aprint_normal("%s at %s: %s\n", device_xname(sc->sc_dev), pdk->dk_name,
443 sc->sc_wname); /* XXX Unicode */
444 aprint_normal("%s: %"PRIu64" blocks at %"PRId64", type: %s\n",
445 device_xname(sc->sc_dev), sc->sc_size, sc->sc_offset, sc->sc_ptype);
447 return (0);
451 * dkwedge_find:
453 * Lookup a disk wedge based on the provided information.
454 * NOTE: We look up the wedge based on the wedge devname,
455 * not wname.
457 * Return NULL if the wedge is not found, otherwise return
458 * the wedge's softc. Assign the wedge's unit number to unitp
459 * if unitp is not NULL.
461 static struct dkwedge_softc *
462 dkwedge_find(struct dkwedge_info *dkw, u_int *unitp)
464 struct dkwedge_softc *sc = NULL;
465 u_int unit;
467 /* Find our softc. */
468 dkw->dkw_devname[sizeof(dkw->dkw_devname) - 1] = '\0';
469 rw_enter(&dkwedges_lock, RW_READER);
470 for (unit = 0; unit < ndkwedges; unit++) {
471 if ((sc = dkwedges[unit]) != NULL &&
472 strcmp(device_xname(sc->sc_dev), dkw->dkw_devname) == 0 &&
473 strcmp(sc->sc_parent->dk_name, dkw->dkw_parent) == 0) {
474 break;
477 rw_exit(&dkwedges_lock);
478 if (unit == ndkwedges)
479 return NULL;
481 if (unitp != NULL)
482 *unitp = unit;
484 return sc;
488 * dkwedge_del: [exported function]
490 * Delete a disk wedge based on the provided information.
491 * NOTE: We look up the wedge based on the wedge devname,
492 * not wname.
495 dkwedge_del(struct dkwedge_info *dkw)
497 struct dkwedge_softc *sc = NULL;
499 /* Find our softc. */
500 if ((sc = dkwedge_find(dkw, NULL)) == NULL)
501 return (ESRCH);
503 return config_detach(sc->sc_dev, DETACH_FORCE | DETACH_QUIET);
506 static int
507 dkwedge_begindetach(struct dkwedge_softc *sc, int flags)
509 struct disk *dk = &sc->sc_dk;
510 int rc;
512 rc = 0;
513 mutex_enter(&dk->dk_openlock);
514 mutex_enter(&sc->sc_parent->dk_rawlock);
515 if (dk->dk_openmask == 0)
516 ; /* nothing to do */
517 else if ((flags & DETACH_FORCE) == 0)
518 rc = EBUSY;
519 else
520 rc = dklastclose(sc);
521 mutex_exit(&sc->sc_parent->dk_rawlock);
522 mutex_exit(&dk->dk_openlock);
524 return rc;
528 * dkwedge_detach:
530 * Autoconfiguration detach function for pseudo-device glue.
532 static int
533 dkwedge_detach(device_t self, int flags)
535 struct dkwedge_softc *sc = NULL;
536 u_int unit;
537 int bmaj, cmaj, rc, s;
539 rw_enter(&dkwedges_lock, RW_WRITER);
540 for (unit = 0; unit < ndkwedges; unit++) {
541 if ((sc = dkwedges[unit]) != NULL && sc->sc_dev == self)
542 break;
544 if (unit == ndkwedges)
545 rc = ENXIO;
546 else if ((rc = dkwedge_begindetach(sc, flags)) == 0) {
547 /* Mark the wedge as dying. */
548 sc->sc_state = DKW_STATE_DYING;
550 rw_exit(&dkwedges_lock);
552 if (rc != 0)
553 return rc;
555 pmf_device_deregister(self);
557 /* Locate the wedge major numbers. */
558 bmaj = bdevsw_lookup_major(&dk_bdevsw);
559 cmaj = cdevsw_lookup_major(&dk_cdevsw);
561 /* Kill any pending restart. */
562 callout_stop(&sc->sc_restart_ch);
565 * dkstart() will kill any queued buffers now that the
566 * state of the wedge is not RUNNING. Once we've done
567 * that, wait for any other pending I/O to complete.
569 s = splbio();
570 dkstart(sc);
571 dkwedge_wait_drain(sc);
572 splx(s);
574 /* Nuke the vnodes for any open instances. */
575 vdevgone(bmaj, unit, unit, VBLK);
576 vdevgone(cmaj, unit, unit, VCHR);
578 /* Clean up the parent. */
579 mutex_enter(&sc->sc_dk.dk_openlock);
580 mutex_enter(&sc->sc_parent->dk_rawlock);
581 if (sc->sc_dk.dk_openmask) {
582 if (sc->sc_parent->dk_rawopens-- == 1) {
583 KASSERT(sc->sc_parent->dk_rawvp != NULL);
584 (void) vn_close(sc->sc_parent->dk_rawvp, FREAD | FWRITE,
585 NOCRED);
586 sc->sc_parent->dk_rawvp = NULL;
588 sc->sc_dk.dk_openmask = 0;
590 mutex_exit(&sc->sc_parent->dk_rawlock);
591 mutex_exit(&sc->sc_dk.dk_openlock);
593 /* Announce our departure. */
594 aprint_normal("%s at %s (%s) deleted\n", device_xname(sc->sc_dev),
595 sc->sc_parent->dk_name,
596 sc->sc_wname); /* XXX Unicode */
598 mutex_enter(&sc->sc_parent->dk_openlock);
599 sc->sc_parent->dk_nwedges--;
600 LIST_REMOVE(sc, sc_plink);
601 mutex_exit(&sc->sc_parent->dk_openlock);
603 /* Delete our buffer queue. */
604 bufq_free(sc->sc_bufq);
606 /* Detach from the disk list. */
607 disk_detach(&sc->sc_dk);
608 disk_destroy(&sc->sc_dk);
610 /* Poof. */
611 rw_enter(&dkwedges_lock, RW_WRITER);
612 dkwedges[unit] = NULL;
613 sc->sc_state = DKW_STATE_DEAD;
614 rw_exit(&dkwedges_lock);
616 free(sc, M_DKWEDGE);
618 return 0;
622 * dkwedge_delall: [exported function]
624 * Delete all of the wedges on the specified disk. Used when
625 * a disk is being detached.
627 void
628 dkwedge_delall(struct disk *pdk)
630 struct dkwedge_info dkw;
631 struct dkwedge_softc *sc;
633 for (;;) {
634 mutex_enter(&pdk->dk_openlock);
635 if ((sc = LIST_FIRST(&pdk->dk_wedges)) == NULL) {
636 KASSERT(pdk->dk_nwedges == 0);
637 mutex_exit(&pdk->dk_openlock);
638 return;
640 strcpy(dkw.dkw_parent, pdk->dk_name);
641 strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev),
642 sizeof(dkw.dkw_devname));
643 mutex_exit(&pdk->dk_openlock);
644 (void) dkwedge_del(&dkw);
649 * dkwedge_list: [exported function]
651 * List all of the wedges on a particular disk.
652 * If p == NULL, the buffer is in kernel space. Otherwise, it is
653 * in user space of the specified process.
656 dkwedge_list(struct disk *pdk, struct dkwedge_list *dkwl, struct lwp *l)
658 struct uio uio;
659 struct iovec iov;
660 struct dkwedge_softc *sc;
661 struct dkwedge_info dkw;
662 int error = 0;
664 iov.iov_base = dkwl->dkwl_buf;
665 iov.iov_len = dkwl->dkwl_bufsize;
667 uio.uio_iov = &iov;
668 uio.uio_iovcnt = 1;
669 uio.uio_offset = 0;
670 uio.uio_resid = dkwl->dkwl_bufsize;
671 uio.uio_rw = UIO_READ;
672 KASSERT(l == curlwp);
673 uio.uio_vmspace = l->l_proc->p_vmspace;
675 dkwl->dkwl_ncopied = 0;
677 mutex_enter(&pdk->dk_openlock);
678 LIST_FOREACH(sc, &pdk->dk_wedges, sc_plink) {
679 if (uio.uio_resid < sizeof(dkw))
680 break;
682 if (sc->sc_state != DKW_STATE_RUNNING)
683 continue;
685 strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev),
686 sizeof(dkw.dkw_devname));
687 memcpy(dkw.dkw_wname, sc->sc_wname, sizeof(dkw.dkw_wname));
688 dkw.dkw_wname[sizeof(dkw.dkw_wname) - 1] = '\0';
689 strcpy(dkw.dkw_parent, sc->sc_parent->dk_name);
690 dkw.dkw_offset = sc->sc_offset;
691 dkw.dkw_size = sc->sc_size;
692 strcpy(dkw.dkw_ptype, sc->sc_ptype);
694 error = uiomove(&dkw, sizeof(dkw), &uio);
695 if (error)
696 break;
697 dkwl->dkwl_ncopied++;
699 dkwl->dkwl_nwedges = pdk->dk_nwedges;
700 mutex_exit(&pdk->dk_openlock);
702 return (error);
705 device_t
706 dkwedge_find_by_wname(const char *wname)
708 device_t dv = NULL;
709 struct dkwedge_softc *sc;
710 int i;
712 rw_enter(&dkwedges_lock, RW_WRITER);
713 for (i = 0; i < ndkwedges; i++) {
714 if ((sc = dkwedges[i]) == NULL)
715 continue;
716 if (strcmp(sc->sc_wname, wname) == 0) {
717 if (dv != NULL) {
718 printf(
719 "WARNING: double match for wedge name %s "
720 "(%s, %s)\n", wname, device_xname(dv),
721 device_xname(sc->sc_dev));
722 continue;
724 dv = sc->sc_dev;
727 rw_exit(&dkwedges_lock);
728 return dv;
731 void
732 dkwedge_print_wnames(void)
734 struct dkwedge_softc *sc;
735 int i;
737 rw_enter(&dkwedges_lock, RW_WRITER);
738 for (i = 0; i < ndkwedges; i++) {
739 if ((sc = dkwedges[i]) == NULL)
740 continue;
741 printf(" wedge:%s", sc->sc_wname);
743 rw_exit(&dkwedges_lock);
747 * dkwedge_set_bootwedge
749 * Set the booted_wedge global based on the specified parent name
750 * and offset/length.
752 void
753 dkwedge_set_bootwedge(device_t parent, daddr_t startblk, uint64_t nblks)
755 struct dkwedge_softc *sc;
756 int i;
758 rw_enter(&dkwedges_lock, RW_WRITER);
759 for (i = 0; i < ndkwedges; i++) {
760 if ((sc = dkwedges[i]) == NULL)
761 continue;
762 if (strcmp(sc->sc_parent->dk_name, device_xname(parent)) == 0 &&
763 sc->sc_offset == startblk &&
764 sc->sc_size == nblks) {
765 if (booted_wedge) {
766 printf("WARNING: double match for boot wedge "
767 "(%s, %s)\n",
768 device_xname(booted_wedge),
769 device_xname(sc->sc_dev));
770 continue;
772 booted_device = parent;
773 booted_wedge = sc->sc_dev;
774 booted_partition = 0;
778 * XXX What if we don't find one? Should we create a special
779 * XXX root wedge?
781 rw_exit(&dkwedges_lock);
785 * We need a dummy object to stuff into the dkwedge discovery method link
786 * set to ensure that there is always at least one object in the set.
788 static struct dkwedge_discovery_method dummy_discovery_method;
789 __link_set_add_bss(dkwedge_methods, dummy_discovery_method);
792 * dkwedge_init:
794 * Initialize the disk wedge subsystem.
796 void
797 dkwedge_init(void)
799 __link_set_decl(dkwedge_methods, struct dkwedge_discovery_method);
800 struct dkwedge_discovery_method * const *ddmp;
801 struct dkwedge_discovery_method *lddm, *ddm;
803 rw_init(&dkwedges_lock);
804 rw_init(&dkwedge_discovery_methods_lock);
806 if (config_cfdriver_attach(&dk_cd) != 0)
807 panic("dkwedge: unable to attach cfdriver");
808 if (config_cfattach_attach(dk_cd.cd_name, &dk_ca) != 0)
809 panic("dkwedge: unable to attach cfattach");
811 rw_enter(&dkwedge_discovery_methods_lock, RW_WRITER);
813 LIST_INIT(&dkwedge_discovery_methods);
815 __link_set_foreach(ddmp, dkwedge_methods) {
816 ddm = *ddmp;
817 if (ddm == &dummy_discovery_method)
818 continue;
819 if (LIST_EMPTY(&dkwedge_discovery_methods)) {
820 LIST_INSERT_HEAD(&dkwedge_discovery_methods,
821 ddm, ddm_list);
822 continue;
824 LIST_FOREACH(lddm, &dkwedge_discovery_methods, ddm_list) {
825 if (ddm->ddm_priority == lddm->ddm_priority) {
826 aprint_error("dk-method-%s: method \"%s\" "
827 "already exists at priority %d\n",
828 ddm->ddm_name, lddm->ddm_name,
829 lddm->ddm_priority);
830 /* Not inserted. */
831 break;
833 if (ddm->ddm_priority < lddm->ddm_priority) {
834 /* Higher priority; insert before. */
835 LIST_INSERT_BEFORE(lddm, ddm, ddm_list);
836 break;
838 if (LIST_NEXT(lddm, ddm_list) == NULL) {
839 /* Last one; insert after. */
840 KASSERT(lddm->ddm_priority < ddm->ddm_priority);
841 LIST_INSERT_AFTER(lddm, ddm, ddm_list);
842 break;
847 rw_exit(&dkwedge_discovery_methods_lock);
850 #ifdef DKWEDGE_AUTODISCOVER
851 int dkwedge_autodiscover = 1;
852 #else
853 int dkwedge_autodiscover = 0;
854 #endif
857 * dkwedge_discover: [exported function]
859 * Discover the wedges on a newly attached disk.
861 void
862 dkwedge_discover(struct disk *pdk)
864 struct dkwedge_discovery_method *ddm;
865 struct vnode *vp;
866 int error;
867 dev_t pdev;
870 * Require people playing with wedges to enable this explicitly.
872 if (dkwedge_autodiscover == 0)
873 return;
875 rw_enter(&dkwedge_discovery_methods_lock, RW_READER);
877 error = dkwedge_compute_pdev(pdk->dk_name, &pdev);
878 if (error) {
879 aprint_error("%s: unable to compute pdev, error = %d\n",
880 pdk->dk_name, error);
881 goto out;
884 error = bdevvp(pdev, &vp);
885 if (error) {
886 aprint_error("%s: unable to find vnode for pdev, error = %d\n",
887 pdk->dk_name, error);
888 goto out;
891 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
892 if (error) {
893 aprint_error("%s: unable to lock vnode for pdev, error = %d\n",
894 pdk->dk_name, error);
895 vrele(vp);
896 goto out;
899 error = VOP_OPEN(vp, FREAD, NOCRED);
900 if (error) {
901 aprint_error("%s: unable to open device, error = %d\n",
902 pdk->dk_name, error);
903 vput(vp);
904 goto out;
906 VOP_UNLOCK(vp, 0);
909 * For each supported partition map type, look to see if
910 * this map type exists. If so, parse it and add the
911 * corresponding wedges.
913 LIST_FOREACH(ddm, &dkwedge_discovery_methods, ddm_list) {
914 error = (*ddm->ddm_discover)(pdk, vp);
915 if (error == 0) {
916 /* Successfully created wedges; we're done. */
917 break;
921 error = vn_close(vp, FREAD, NOCRED);
922 if (error) {
923 aprint_error("%s: unable to close device, error = %d\n",
924 pdk->dk_name, error);
925 /* We'll just assume the vnode has been cleaned up. */
927 out:
928 rw_exit(&dkwedge_discovery_methods_lock);
932 * dkwedge_read:
934 * Read some data from the specified disk, used for
935 * partition discovery.
938 dkwedge_read(struct disk *pdk, struct vnode *vp, daddr_t blkno,
939 void *tbuf, size_t len)
941 struct buf *bp;
942 int result;
944 bp = getiobuf(vp, true);
946 bp->b_dev = vp->v_rdev;
947 bp->b_blkno = blkno;
948 bp->b_bcount = len;
949 bp->b_resid = len;
950 bp->b_flags = B_READ;
951 bp->b_data = tbuf;
952 SET(bp->b_cflags, BC_BUSY); /* mark buffer busy */
954 VOP_STRATEGY(vp, bp);
955 result = biowait(bp);
956 putiobuf(bp);
958 return result;
962 * dkwedge_lookup:
964 * Look up a dkwedge_softc based on the provided dev_t.
966 static struct dkwedge_softc *
967 dkwedge_lookup(dev_t dev)
969 int unit = minor(dev);
971 if (unit >= ndkwedges)
972 return (NULL);
974 KASSERT(dkwedges != NULL);
976 return (dkwedges[unit]);
980 * dkopen: [devsw entry point]
982 * Open a wedge.
984 static int
985 dkopen(dev_t dev, int flags, int fmt, struct lwp *l)
987 struct dkwedge_softc *sc = dkwedge_lookup(dev);
988 struct vnode *vp;
989 int error = 0;
991 if (sc == NULL)
992 return (ENODEV);
994 if (sc->sc_state != DKW_STATE_RUNNING)
995 return (ENXIO);
998 * We go through a complicated little dance to only open the parent
999 * vnode once per wedge, no matter how many times the wedge is
1000 * opened. The reason? We see one dkopen() per open call, but
1001 * only dkclose() on the last close.
1003 mutex_enter(&sc->sc_dk.dk_openlock);
1004 mutex_enter(&sc->sc_parent->dk_rawlock);
1005 if (sc->sc_dk.dk_openmask == 0) {
1006 if (sc->sc_parent->dk_rawopens == 0) {
1007 KASSERT(sc->sc_parent->dk_rawvp == NULL);
1008 error = bdevvp(sc->sc_pdev, &vp);
1009 if (error)
1010 goto popen_fail;
1011 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1012 if (error) {
1013 vrele(vp);
1014 goto popen_fail;
1016 error = VOP_OPEN(vp, FREAD | FWRITE, NOCRED);
1017 if (error) {
1018 vput(vp);
1019 goto popen_fail;
1021 /* VOP_OPEN() doesn't do this for us. */
1022 mutex_enter(&vp->v_interlock);
1023 vp->v_writecount++;
1024 mutex_exit(&vp->v_interlock);
1025 VOP_UNLOCK(vp, 0);
1026 sc->sc_parent->dk_rawvp = vp;
1028 sc->sc_parent->dk_rawopens++;
1030 if (fmt == S_IFCHR)
1031 sc->sc_dk.dk_copenmask |= 1;
1032 else
1033 sc->sc_dk.dk_bopenmask |= 1;
1034 sc->sc_dk.dk_openmask =
1035 sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
1037 popen_fail:
1038 mutex_exit(&sc->sc_parent->dk_rawlock);
1039 mutex_exit(&sc->sc_dk.dk_openlock);
1040 return (error);
1044 * Caller must hold sc->sc_dk.dk_openlock and sc->sc_parent->dk_rawlock.
1046 static int
1047 dklastclose(struct dkwedge_softc *sc)
1049 int error = 0;
1051 if (sc->sc_parent->dk_rawopens-- == 1) {
1052 KASSERT(sc->sc_parent->dk_rawvp != NULL);
1053 error = vn_close(sc->sc_parent->dk_rawvp,
1054 FREAD | FWRITE, NOCRED);
1055 sc->sc_parent->dk_rawvp = NULL;
1057 return error;
1061 * dkclose: [devsw entry point]
1063 * Close a wedge.
1065 static int
1066 dkclose(dev_t dev, int flags, int fmt, struct lwp *l)
1068 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1069 int error = 0;
1071 KASSERT(sc->sc_dk.dk_openmask != 0);
1073 mutex_enter(&sc->sc_dk.dk_openlock);
1074 mutex_enter(&sc->sc_parent->dk_rawlock);
1076 if (fmt == S_IFCHR)
1077 sc->sc_dk.dk_copenmask &= ~1;
1078 else
1079 sc->sc_dk.dk_bopenmask &= ~1;
1080 sc->sc_dk.dk_openmask =
1081 sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
1083 if (sc->sc_dk.dk_openmask == 0)
1084 error = dklastclose(sc);
1086 mutex_exit(&sc->sc_parent->dk_rawlock);
1087 mutex_exit(&sc->sc_dk.dk_openlock);
1089 return (error);
1093 * dkstragegy: [devsw entry point]
1095 * Perform I/O based on the wedge I/O strategy.
1097 static void
1098 dkstrategy(struct buf *bp)
1100 struct dkwedge_softc *sc = dkwedge_lookup(bp->b_dev);
1101 int s;
1103 if (sc->sc_state != DKW_STATE_RUNNING) {
1104 bp->b_error = ENXIO;
1105 goto done;
1108 /* If it's an empty transfer, wake up the top half now. */
1109 if (bp->b_bcount == 0)
1110 goto done;
1112 /* Make sure it's in-range. */
1113 if (bounds_check_with_mediasize(bp, DEV_BSIZE, sc->sc_size) <= 0)
1114 goto done;
1116 /* Translate it to the parent's raw LBA. */
1117 bp->b_rawblkno = bp->b_blkno + sc->sc_offset;
1119 /* Place it in the queue and start I/O on the unit. */
1120 s = splbio();
1121 sc->sc_iopend++;
1122 bufq_put(sc->sc_bufq, bp);
1123 dkstart(sc);
1124 splx(s);
1125 return;
1127 done:
1128 bp->b_resid = bp->b_bcount;
1129 biodone(bp);
1133 * dkstart:
1135 * Start I/O that has been enqueued on the wedge.
1136 * NOTE: Must be called at splbio()!
1138 static void
1139 dkstart(struct dkwedge_softc *sc)
1141 struct vnode *vp;
1142 struct buf *bp, *nbp;
1144 /* Do as much work as has been enqueued. */
1145 while ((bp = bufq_peek(sc->sc_bufq)) != NULL) {
1146 if (sc->sc_state != DKW_STATE_RUNNING) {
1147 (void) bufq_get(sc->sc_bufq);
1148 if (sc->sc_iopend-- == 1 &&
1149 (sc->sc_flags & DK_F_WAIT_DRAIN) != 0) {
1150 sc->sc_flags &= ~DK_F_WAIT_DRAIN;
1151 wakeup(&sc->sc_iopend);
1153 bp->b_error = ENXIO;
1154 bp->b_resid = bp->b_bcount;
1155 biodone(bp);
1158 /* Instrumentation. */
1159 disk_busy(&sc->sc_dk);
1161 nbp = getiobuf(sc->sc_parent->dk_rawvp, false);
1162 if (nbp == NULL) {
1164 * No resources to run this request; leave the
1165 * buffer queued up, and schedule a timer to
1166 * restart the queue in 1/2 a second.
1168 disk_unbusy(&sc->sc_dk, 0, bp->b_flags & B_READ);
1169 callout_schedule(&sc->sc_restart_ch, hz / 2);
1170 return;
1173 (void) bufq_get(sc->sc_bufq);
1175 nbp->b_data = bp->b_data;
1176 nbp->b_flags = bp->b_flags;
1177 nbp->b_oflags = bp->b_oflags;
1178 nbp->b_cflags = bp->b_cflags;
1179 nbp->b_iodone = dkiodone;
1180 nbp->b_proc = bp->b_proc;
1181 nbp->b_blkno = bp->b_rawblkno;
1182 nbp->b_dev = sc->sc_parent->dk_rawvp->v_rdev;
1183 nbp->b_bcount = bp->b_bcount;
1184 nbp->b_private = bp;
1185 BIO_COPYPRIO(nbp, bp);
1187 vp = nbp->b_vp;
1188 if ((nbp->b_flags & B_READ) == 0) {
1189 mutex_enter(&vp->v_interlock);
1190 vp->v_numoutput++;
1191 mutex_exit(&vp->v_interlock);
1193 VOP_STRATEGY(vp, nbp);
1198 * dkiodone:
1200 * I/O to a wedge has completed; alert the top half.
1201 * NOTE: Must be called at splbio()!
1203 static void
1204 dkiodone(struct buf *bp)
1206 struct buf *obp = bp->b_private;
1207 struct dkwedge_softc *sc = dkwedge_lookup(obp->b_dev);
1209 if (bp->b_error != 0)
1210 obp->b_error = bp->b_error;
1211 obp->b_resid = bp->b_resid;
1212 putiobuf(bp);
1214 if (sc->sc_iopend-- == 1 && (sc->sc_flags & DK_F_WAIT_DRAIN) != 0) {
1215 sc->sc_flags &= ~DK_F_WAIT_DRAIN;
1216 wakeup(&sc->sc_iopend);
1219 disk_unbusy(&sc->sc_dk, obp->b_bcount - obp->b_resid,
1220 obp->b_flags & B_READ);
1222 biodone(obp);
1224 /* Kick the queue in case there is more work we can do. */
1225 dkstart(sc);
1229 * dkrestart:
1231 * Restart the work queue after it was stalled due to
1232 * a resource shortage. Invoked via a callout.
1234 static void
1235 dkrestart(void *v)
1237 struct dkwedge_softc *sc = v;
1238 int s;
1240 s = splbio();
1241 dkstart(sc);
1242 splx(s);
1246 * dkminphys:
1248 * Call parent's minphys function.
1250 static void
1251 dkminphys(struct buf *bp)
1253 struct dkwedge_softc *sc = dkwedge_lookup(bp->b_dev);
1254 dev_t dev;
1256 dev = bp->b_dev;
1257 bp->b_dev = sc->sc_pdev;
1258 (*sc->sc_parent->dk_driver->d_minphys)(bp);
1259 bp->b_dev = dev;
1263 * dkread: [devsw entry point]
1265 * Read from a wedge.
1267 static int
1268 dkread(dev_t dev, struct uio *uio, int flags)
1270 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1272 if (sc->sc_state != DKW_STATE_RUNNING)
1273 return (ENXIO);
1275 return (physio(dkstrategy, NULL, dev, B_READ, dkminphys, uio));
1279 * dkwrite: [devsw entry point]
1281 * Write to a wedge.
1283 static int
1284 dkwrite(dev_t dev, struct uio *uio, int flags)
1286 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1288 if (sc->sc_state != DKW_STATE_RUNNING)
1289 return (ENXIO);
1291 return (physio(dkstrategy, NULL, dev, B_WRITE, dkminphys, uio));
1295 * dkioctl: [devsw entry point]
1297 * Perform an ioctl request on a wedge.
1299 static int
1300 dkioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
1302 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1303 int error = 0;
1305 if (sc->sc_state != DKW_STATE_RUNNING)
1306 return (ENXIO);
1308 error = disk_ioctl(&sc->sc_dk, cmd, data, flag, l);
1309 if (error != EPASSTHROUGH)
1310 return (error);
1312 error = 0;
1314 switch (cmd) {
1315 case DIOCCACHESYNC:
1317 * XXX Do we really need to care about having a writable
1318 * file descriptor here?
1320 if ((flag & FWRITE) == 0)
1321 error = EBADF;
1322 else
1323 error = VOP_IOCTL(sc->sc_parent->dk_rawvp,
1324 cmd, data, flag,
1325 l != NULL ? l->l_cred : NOCRED);
1326 break;
1327 case DIOCGWEDGEINFO:
1329 struct dkwedge_info *dkw = (void *) data;
1331 strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev),
1332 sizeof(dkw->dkw_devname));
1333 memcpy(dkw->dkw_wname, sc->sc_wname, sizeof(dkw->dkw_wname));
1334 dkw->dkw_wname[sizeof(dkw->dkw_wname) - 1] = '\0';
1335 strcpy(dkw->dkw_parent, sc->sc_parent->dk_name);
1336 dkw->dkw_offset = sc->sc_offset;
1337 dkw->dkw_size = sc->sc_size;
1338 strcpy(dkw->dkw_ptype, sc->sc_ptype);
1340 break;
1343 default:
1344 error = ENOTTY;
1347 return (error);
1351 * dksize: [devsw entry point]
1353 * Query the size of a wedge for the purpose of performing a dump
1354 * or for swapping to.
1356 static int
1357 dksize(dev_t dev)
1359 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1360 int rv = -1;
1362 if (sc == NULL)
1363 return (-1);
1365 if (sc->sc_state != DKW_STATE_RUNNING)
1366 return (ENXIO);
1368 mutex_enter(&sc->sc_dk.dk_openlock);
1369 mutex_enter(&sc->sc_parent->dk_rawlock);
1371 /* Our content type is static, no need to open the device. */
1373 if (strcmp(sc->sc_ptype, DKW_PTYPE_SWAP) == 0) {
1374 /* Saturate if we are larger than INT_MAX. */
1375 if (sc->sc_size > INT_MAX)
1376 rv = INT_MAX;
1377 else
1378 rv = (int) sc->sc_size;
1381 mutex_exit(&sc->sc_parent->dk_rawlock);
1382 mutex_exit(&sc->sc_dk.dk_openlock);
1384 return (rv);
1388 * dkdump: [devsw entry point]
1390 * Perform a crash dump to a wedge.
1392 static int
1393 dkdump(dev_t dev, daddr_t blkno, void *va, size_t size)
1395 struct dkwedge_softc *sc = dkwedge_lookup(dev);
1396 const struct bdevsw *bdev;
1397 int rv = 0;
1399 if (sc == NULL)
1400 return (-1);
1402 if (sc->sc_state != DKW_STATE_RUNNING)
1403 return (ENXIO);
1405 mutex_enter(&sc->sc_dk.dk_openlock);
1406 mutex_enter(&sc->sc_parent->dk_rawlock);
1408 /* Our content type is static, no need to open the device. */
1410 if (strcmp(sc->sc_ptype, DKW_PTYPE_SWAP) != 0) {
1411 rv = ENXIO;
1412 goto out;
1414 if (size % DEV_BSIZE != 0) {
1415 rv = EINVAL;
1416 goto out;
1418 if (blkno + size / DEV_BSIZE > sc->sc_size) {
1419 printf("%s: blkno (%" PRIu64 ") + size / DEV_BSIZE (%zu) > "
1420 "sc->sc_size (%" PRIu64 ")\n", __func__, blkno,
1421 size / DEV_BSIZE, sc->sc_size);
1422 rv = EINVAL;
1423 goto out;
1426 bdev = bdevsw_lookup(sc->sc_pdev);
1427 rv = (*bdev->d_dump)(sc->sc_pdev, blkno + sc->sc_offset, va, size);
1429 out:
1430 mutex_exit(&sc->sc_parent->dk_rawlock);
1431 mutex_exit(&sc->sc_dk.dk_openlock);
1433 return rv;
1437 * config glue
1441 config_handle_wedges(struct device *dv, int par)
1443 struct dkwedge_list wl;
1444 struct dkwedge_info *wi;
1445 struct vnode *vn;
1446 char diskname[16];
1447 int i, error;
1449 if ((vn = opendisk(dv)) == NULL)
1450 return -1;
1452 wl.dkwl_bufsize = sizeof(*wi) * 16;
1453 wl.dkwl_buf = wi = malloc(wl.dkwl_bufsize, M_TEMP, M_WAITOK);
1455 error = VOP_IOCTL(vn, DIOCLWEDGES, &wl, FREAD, NOCRED);
1456 VOP_CLOSE(vn, FREAD, NOCRED);
1457 vput(vn);
1458 if (error) {
1459 #ifdef DEBUG_WEDGE
1460 printf("%s: List wedges returned %d\n",
1461 device_xname(dv), error);
1462 #endif
1463 free(wi, M_TEMP);
1464 return -1;
1467 #ifdef DEBUG_WEDGE
1468 printf("%s: Returned %u(%u) wedges\n", device_xname(dv),
1469 wl.dkwl_nwedges, wl.dkwl_ncopied);
1470 #endif
1471 snprintf(diskname, sizeof(diskname), "%s%c", device_xname(dv),
1472 par + 'a');
1474 for (i = 0; i < wl.dkwl_ncopied; i++) {
1475 #ifdef DEBUG_WEDGE
1476 printf("%s: Looking for %s in %s\n",
1477 device_xname(dv), diskname, wi[i].dkw_wname);
1478 #endif
1479 if (strcmp(wi[i].dkw_wname, diskname) == 0)
1480 break;
1483 if (i == wl.dkwl_ncopied) {
1484 #ifdef DEBUG_WEDGE
1485 printf("%s: Cannot find wedge with parent %s\n",
1486 device_xname(dv), diskname);
1487 #endif
1488 free(wi, M_TEMP);
1489 return -1;
1492 #ifdef DEBUG_WEDGE
1493 printf("%s: Setting boot wedge %s (%s) at %llu %llu\n",
1494 device_xname(dv), wi[i].dkw_devname, wi[i].dkw_wname,
1495 (unsigned long long)wi[i].dkw_offset,
1496 (unsigned long long)wi[i].dkw_size);
1497 #endif
1498 dkwedge_set_bootwedge(dv, wi[i].dkw_offset, wi[i].dkw_size);
1499 free(wi, M_TEMP);
1500 return 0;