xref: /freebsd/sys/dev/md/md.c (revision 7aa383846770374466b1dcb2cefd71bde9acf463)
1 /*-
2  * ----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
5  * can do whatever you want with this stuff. If we meet some day, and you think
6  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7  * ----------------------------------------------------------------------------
8  *
9  * $FreeBSD$
10  *
11  */
12 
13 /*-
14  * The following functions are based in the vn(4) driver: mdstart_swap(),
15  * mdstart_vnode(), mdcreate_swap(), mdcreate_vnode() and mddestroy(),
16  * and as such under the following copyright:
17  *
18  * Copyright (c) 1988 University of Utah.
19  * Copyright (c) 1990, 1993
20  *	The Regents of the University of California.  All rights reserved.
21  *
22  * This code is derived from software contributed to Berkeley by
23  * the Systems Programming Group of the University of Utah Computer
24  * Science Department.
25  *
26  * Redistribution and use in source and binary forms, with or without
27  * modification, are permitted provided that the following conditions
28  * are met:
29  * 1. Redistributions of source code must retain the above copyright
30  *    notice, this list of conditions and the following disclaimer.
31  * 2. Redistributions in binary form must reproduce the above copyright
32  *    notice, this list of conditions and the following disclaimer in the
33  *    documentation and/or other materials provided with the distribution.
34  * 4. Neither the name of the University nor the names of its contributors
35  *    may be used to endorse or promote products derived from this software
36  *    without specific prior written permission.
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
39  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
42  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  *
50  * from: Utah Hdr: vn.c 1.13 94/04/02
51  *
52  *	from: @(#)vn.c	8.6 (Berkeley) 4/1/94
53  * From: src/sys/dev/vn/vn.c,v 1.122 2000/12/16 16:06:03
54  */
55 
56 #include "opt_geom.h"
57 #include "opt_md.h"
58 
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/bio.h>
62 #include <sys/conf.h>
63 #include <sys/devicestat.h>
64 #include <sys/fcntl.h>
65 #include <sys/kernel.h>
66 #include <sys/kthread.h>
67 #include <sys/limits.h>
68 #include <sys/linker.h>
69 #include <sys/lock.h>
70 #include <sys/malloc.h>
71 #include <sys/mdioctl.h>
72 #include <sys/mount.h>
73 #include <sys/mutex.h>
74 #include <sys/sx.h>
75 #include <sys/namei.h>
76 #include <sys/proc.h>
77 #include <sys/queue.h>
78 #include <sys/sched.h>
79 #include <sys/sf_buf.h>
80 #include <sys/sysctl.h>
81 #include <sys/vnode.h>
82 
83 #include <geom/geom.h>
84 
85 #include <vm/vm.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
88 #include <vm/vm_pager.h>
89 #include <vm/swap_pager.h>
90 #include <vm/uma.h>
91 
92 #define MD_MODVER 1
93 
94 #define MD_SHUTDOWN	0x10000		/* Tell worker thread to terminate. */
95 #define	MD_EXITING	0x20000		/* Worker thread is exiting. */
96 
97 #ifndef MD_NSECT
98 #define MD_NSECT (10000 * 2)
99 #endif
100 
101 static MALLOC_DEFINE(M_MD, "md_disk", "Memory Disk");
102 static MALLOC_DEFINE(M_MDSECT, "md_sectors", "Memory Disk Sectors");
103 
104 static int md_debug;
105 SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0, "");
106 
107 #if defined(MD_ROOT) && defined(MD_ROOT_SIZE)
108 /*
109  * Preloaded image gets put here.
110  * Applications that patch the object with the image can determine
111  * the size looking at the start and end markers (strings),
112  * so we want them contiguous.
113  */
114 static struct {
115 	u_char start[MD_ROOT_SIZE*1024];
116 	u_char end[128];
117 } mfs_root = {
118 	.start = "MFS Filesystem goes here",
119 	.end = "MFS Filesystem had better STOP here",
120 };
121 #endif
122 
123 static g_init_t g_md_init;
124 static g_fini_t g_md_fini;
125 static g_start_t g_md_start;
126 static g_access_t g_md_access;
127 static void g_md_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
128     struct g_consumer *cp __unused, struct g_provider *pp);
129 
130 static int	mdunits;
131 static struct cdev *status_dev = 0;
132 static struct sx md_sx;
133 
134 static d_ioctl_t mdctlioctl;
135 
136 static struct cdevsw mdctl_cdevsw = {
137 	.d_version =	D_VERSION,
138 	.d_ioctl =	mdctlioctl,
139 	.d_name =	MD_NAME,
140 };
141 
142 struct g_class g_md_class = {
143 	.name = "MD",
144 	.version = G_VERSION,
145 	.init = g_md_init,
146 	.fini = g_md_fini,
147 	.start = g_md_start,
148 	.access = g_md_access,
149 	.dumpconf = g_md_dumpconf,
150 };
151 
152 DECLARE_GEOM_CLASS(g_md_class, g_md);
153 
154 
155 static LIST_HEAD(, md_s) md_softc_list = LIST_HEAD_INITIALIZER(md_softc_list);
156 
157 #define NINDIR	(PAGE_SIZE / sizeof(uintptr_t))
158 #define NMASK	(NINDIR-1)
159 static int nshift;
160 
161 struct indir {
162 	uintptr_t	*array;
163 	u_int		total;
164 	u_int		used;
165 	u_int		shift;
166 };
167 
168 struct md_s {
169 	int unit;
170 	LIST_ENTRY(md_s) list;
171 	struct bio_queue_head bio_queue;
172 	struct mtx queue_mtx;
173 	struct cdev *dev;
174 	enum md_types type;
175 	off_t mediasize;
176 	unsigned sectorsize;
177 	unsigned opencount;
178 	unsigned fwheads;
179 	unsigned fwsectors;
180 	unsigned flags;
181 	char name[20];
182 	struct proc *procp;
183 	struct g_geom *gp;
184 	struct g_provider *pp;
185 	int (*start)(struct md_s *sc, struct bio *bp);
186 	struct devstat *devstat;
187 
188 	/* MD_MALLOC related fields */
189 	struct indir *indir;
190 	uma_zone_t uma;
191 
192 	/* MD_PRELOAD related fields */
193 	u_char *pl_ptr;
194 	size_t pl_len;
195 
196 	/* MD_VNODE related fields */
197 	struct vnode *vnode;
198 	char file[PATH_MAX];
199 	struct ucred *cred;
200 
201 	/* MD_SWAP related fields */
202 	vm_object_t object;
203 };
204 
205 static struct indir *
206 new_indir(u_int shift)
207 {
208 	struct indir *ip;
209 
210 	ip = malloc(sizeof *ip, M_MD, M_NOWAIT | M_ZERO);
211 	if (ip == NULL)
212 		return (NULL);
213 	ip->array = malloc(sizeof(uintptr_t) * NINDIR,
214 	    M_MDSECT, M_NOWAIT | M_ZERO);
215 	if (ip->array == NULL) {
216 		free(ip, M_MD);
217 		return (NULL);
218 	}
219 	ip->total = NINDIR;
220 	ip->shift = shift;
221 	return (ip);
222 }
223 
224 static void
225 del_indir(struct indir *ip)
226 {
227 
228 	free(ip->array, M_MDSECT);
229 	free(ip, M_MD);
230 }
231 
232 static void
233 destroy_indir(struct md_s *sc, struct indir *ip)
234 {
235 	int i;
236 
237 	for (i = 0; i < NINDIR; i++) {
238 		if (!ip->array[i])
239 			continue;
240 		if (ip->shift)
241 			destroy_indir(sc, (struct indir*)(ip->array[i]));
242 		else if (ip->array[i] > 255)
243 			uma_zfree(sc->uma, (void *)(ip->array[i]));
244 	}
245 	del_indir(ip);
246 }
247 
248 /*
249  * This function does the math and allocates the top level "indir" structure
250  * for a device of "size" sectors.
251  */
252 
253 static struct indir *
254 dimension(off_t size)
255 {
256 	off_t rcnt;
257 	struct indir *ip;
258 	int layer;
259 
260 	rcnt = size;
261 	layer = 0;
262 	while (rcnt > NINDIR) {
263 		rcnt /= NINDIR;
264 		layer++;
265 	}
266 
267 	/*
268 	 * XXX: the top layer is probably not fully populated, so we allocate
269 	 * too much space for ip->array in here.
270 	 */
271 	ip = malloc(sizeof *ip, M_MD, M_WAITOK | M_ZERO);
272 	ip->array = malloc(sizeof(uintptr_t) * NINDIR,
273 	    M_MDSECT, M_WAITOK | M_ZERO);
274 	ip->total = NINDIR;
275 	ip->shift = layer * nshift;
276 	return (ip);
277 }
278 
279 /*
280  * Read a given sector
281  */
282 
283 static uintptr_t
284 s_read(struct indir *ip, off_t offset)
285 {
286 	struct indir *cip;
287 	int idx;
288 	uintptr_t up;
289 
290 	if (md_debug > 1)
291 		printf("s_read(%jd)\n", (intmax_t)offset);
292 	up = 0;
293 	for (cip = ip; cip != NULL;) {
294 		if (cip->shift) {
295 			idx = (offset >> cip->shift) & NMASK;
296 			up = cip->array[idx];
297 			cip = (struct indir *)up;
298 			continue;
299 		}
300 		idx = offset & NMASK;
301 		return (cip->array[idx]);
302 	}
303 	return (0);
304 }
305 
306 /*
307  * Write a given sector, prune the tree if the value is 0
308  */
309 
310 static int
311 s_write(struct indir *ip, off_t offset, uintptr_t ptr)
312 {
313 	struct indir *cip, *lip[10];
314 	int idx, li;
315 	uintptr_t up;
316 
317 	if (md_debug > 1)
318 		printf("s_write(%jd, %p)\n", (intmax_t)offset, (void *)ptr);
319 	up = 0;
320 	li = 0;
321 	cip = ip;
322 	for (;;) {
323 		lip[li++] = cip;
324 		if (cip->shift) {
325 			idx = (offset >> cip->shift) & NMASK;
326 			up = cip->array[idx];
327 			if (up != 0) {
328 				cip = (struct indir *)up;
329 				continue;
330 			}
331 			/* Allocate branch */
332 			cip->array[idx] =
333 			    (uintptr_t)new_indir(cip->shift - nshift);
334 			if (cip->array[idx] == 0)
335 				return (ENOSPC);
336 			cip->used++;
337 			up = cip->array[idx];
338 			cip = (struct indir *)up;
339 			continue;
340 		}
341 		/* leafnode */
342 		idx = offset & NMASK;
343 		up = cip->array[idx];
344 		if (up != 0)
345 			cip->used--;
346 		cip->array[idx] = ptr;
347 		if (ptr != 0)
348 			cip->used++;
349 		break;
350 	}
351 	if (cip->used != 0 || li == 1)
352 		return (0);
353 	li--;
354 	while (cip->used == 0 && cip != ip) {
355 		li--;
356 		idx = (offset >> lip[li]->shift) & NMASK;
357 		up = lip[li]->array[idx];
358 		KASSERT(up == (uintptr_t)cip, ("md screwed up"));
359 		del_indir(cip);
360 		lip[li]->array[idx] = 0;
361 		lip[li]->used--;
362 		cip = lip[li];
363 	}
364 	return (0);
365 }
366 
367 
368 static int
369 g_md_access(struct g_provider *pp, int r, int w, int e)
370 {
371 	struct md_s *sc;
372 
373 	sc = pp->geom->softc;
374 	if (sc == NULL) {
375 		if (r <= 0 && w <= 0 && e <= 0)
376 			return (0);
377 		return (ENXIO);
378 	}
379 	r += pp->acr;
380 	w += pp->acw;
381 	e += pp->ace;
382 	if ((sc->flags & MD_READONLY) != 0 && w > 0)
383 		return (EROFS);
384 	if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
385 		sc->opencount = 1;
386 	} else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
387 		sc->opencount = 0;
388 	}
389 	return (0);
390 }
391 
392 static void
393 g_md_start(struct bio *bp)
394 {
395 	struct md_s *sc;
396 
397 	sc = bp->bio_to->geom->softc;
398 	if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE))
399 		devstat_start_transaction_bio(sc->devstat, bp);
400 	mtx_lock(&sc->queue_mtx);
401 	bioq_disksort(&sc->bio_queue, bp);
402 	mtx_unlock(&sc->queue_mtx);
403 	wakeup(sc);
404 }
405 
406 static int
407 mdstart_malloc(struct md_s *sc, struct bio *bp)
408 {
409 	int i, error;
410 	u_char *dst;
411 	off_t secno, nsec, uc;
412 	uintptr_t sp, osp;
413 
414 	switch (bp->bio_cmd) {
415 	case BIO_READ:
416 	case BIO_WRITE:
417 	case BIO_DELETE:
418 		break;
419 	default:
420 		return (EOPNOTSUPP);
421 	}
422 
423 	nsec = bp->bio_length / sc->sectorsize;
424 	secno = bp->bio_offset / sc->sectorsize;
425 	dst = bp->bio_data;
426 	error = 0;
427 	while (nsec--) {
428 		osp = s_read(sc->indir, secno);
429 		if (bp->bio_cmd == BIO_DELETE) {
430 			if (osp != 0)
431 				error = s_write(sc->indir, secno, 0);
432 		} else if (bp->bio_cmd == BIO_READ) {
433 			if (osp == 0)
434 				bzero(dst, sc->sectorsize);
435 			else if (osp <= 255)
436 				memset(dst, osp, sc->sectorsize);
437 			else {
438 				bcopy((void *)osp, dst, sc->sectorsize);
439 				cpu_flush_dcache(dst, sc->sectorsize);
440 			}
441 			osp = 0;
442 		} else if (bp->bio_cmd == BIO_WRITE) {
443 			if (sc->flags & MD_COMPRESS) {
444 				uc = dst[0];
445 				for (i = 1; i < sc->sectorsize; i++)
446 					if (dst[i] != uc)
447 						break;
448 			} else {
449 				i = 0;
450 				uc = 0;
451 			}
452 			if (i == sc->sectorsize) {
453 				if (osp != uc)
454 					error = s_write(sc->indir, secno, uc);
455 			} else {
456 				if (osp <= 255) {
457 					sp = (uintptr_t)uma_zalloc(sc->uma,
458 					    M_NOWAIT);
459 					if (sp == 0) {
460 						error = ENOSPC;
461 						break;
462 					}
463 					bcopy(dst, (void *)sp, sc->sectorsize);
464 					error = s_write(sc->indir, secno, sp);
465 				} else {
466 					bcopy(dst, (void *)osp, sc->sectorsize);
467 					osp = 0;
468 				}
469 			}
470 		} else {
471 			error = EOPNOTSUPP;
472 		}
473 		if (osp > 255)
474 			uma_zfree(sc->uma, (void*)osp);
475 		if (error != 0)
476 			break;
477 		secno++;
478 		dst += sc->sectorsize;
479 	}
480 	bp->bio_resid = 0;
481 	return (error);
482 }
483 
484 static int
485 mdstart_preload(struct md_s *sc, struct bio *bp)
486 {
487 
488 	switch (bp->bio_cmd) {
489 	case BIO_READ:
490 		bcopy(sc->pl_ptr + bp->bio_offset, bp->bio_data,
491 		    bp->bio_length);
492 		cpu_flush_dcache(bp->bio_data, bp->bio_length);
493 		break;
494 	case BIO_WRITE:
495 		bcopy(bp->bio_data, sc->pl_ptr + bp->bio_offset,
496 		    bp->bio_length);
497 		break;
498 	}
499 	bp->bio_resid = 0;
500 	return (0);
501 }
502 
503 static int
504 mdstart_vnode(struct md_s *sc, struct bio *bp)
505 {
506 	int error, vfslocked;
507 	struct uio auio;
508 	struct iovec aiov;
509 	struct mount *mp;
510 	struct vnode *vp;
511 	struct thread *td;
512 
513 	switch (bp->bio_cmd) {
514 	case BIO_READ:
515 	case BIO_WRITE:
516 	case BIO_FLUSH:
517 		break;
518 	default:
519 		return (EOPNOTSUPP);
520 	}
521 
522 	td = curthread;
523 	vp = sc->vnode;
524 
525 	/*
526 	 * VNODE I/O
527 	 *
528 	 * If an error occurs, we set BIO_ERROR but we do not set
529 	 * B_INVAL because (for a write anyway), the buffer is
530 	 * still valid.
531 	 */
532 
533 	if (bp->bio_cmd == BIO_FLUSH) {
534 		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
535 		(void) vn_start_write(vp, &mp, V_WAIT);
536 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
537 		error = VOP_FSYNC(vp, MNT_WAIT, td);
538 		VOP_UNLOCK(vp, 0);
539 		vn_finished_write(mp);
540 		VFS_UNLOCK_GIANT(vfslocked);
541 		return (error);
542 	}
543 
544 	bzero(&auio, sizeof(auio));
545 
546 	aiov.iov_base = bp->bio_data;
547 	aiov.iov_len = bp->bio_length;
548 	auio.uio_iov = &aiov;
549 	auio.uio_iovcnt = 1;
550 	auio.uio_offset = (vm_ooffset_t)bp->bio_offset;
551 	auio.uio_segflg = UIO_SYSSPACE;
552 	if (bp->bio_cmd == BIO_READ)
553 		auio.uio_rw = UIO_READ;
554 	else if (bp->bio_cmd == BIO_WRITE)
555 		auio.uio_rw = UIO_WRITE;
556 	else
557 		panic("wrong BIO_OP in mdstart_vnode");
558 	auio.uio_resid = bp->bio_length;
559 	auio.uio_td = td;
560 	/*
561 	 * When reading set IO_DIRECT to try to avoid double-caching
562 	 * the data.  When writing IO_DIRECT is not optimal.
563 	 */
564 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
565 	if (bp->bio_cmd == BIO_READ) {
566 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
567 		error = VOP_READ(vp, &auio, IO_DIRECT, sc->cred);
568 		VOP_UNLOCK(vp, 0);
569 	} else {
570 		(void) vn_start_write(vp, &mp, V_WAIT);
571 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
572 		error = VOP_WRITE(vp, &auio, sc->flags & MD_ASYNC ? 0 : IO_SYNC,
573 		    sc->cred);
574 		VOP_UNLOCK(vp, 0);
575 		vn_finished_write(mp);
576 	}
577 	VFS_UNLOCK_GIANT(vfslocked);
578 	bp->bio_resid = auio.uio_resid;
579 	return (error);
580 }
581 
582 static int
583 mdstart_swap(struct md_s *sc, struct bio *bp)
584 {
585 	struct sf_buf *sf;
586 	int rv, offs, len, lastend;
587 	vm_pindex_t i, lastp;
588 	vm_page_t m;
589 	u_char *p;
590 
591 	switch (bp->bio_cmd) {
592 	case BIO_READ:
593 	case BIO_WRITE:
594 	case BIO_DELETE:
595 		break;
596 	default:
597 		return (EOPNOTSUPP);
598 	}
599 
600 	p = bp->bio_data;
601 
602 	/*
603 	 * offs is the offset at which to start operating on the
604 	 * next (ie, first) page.  lastp is the last page on
605 	 * which we're going to operate.  lastend is the ending
606 	 * position within that last page (ie, PAGE_SIZE if
607 	 * we're operating on complete aligned pages).
608 	 */
609 	offs = bp->bio_offset % PAGE_SIZE;
610 	lastp = (bp->bio_offset + bp->bio_length - 1) / PAGE_SIZE;
611 	lastend = (bp->bio_offset + bp->bio_length - 1) % PAGE_SIZE + 1;
612 
613 	rv = VM_PAGER_OK;
614 	VM_OBJECT_LOCK(sc->object);
615 	vm_object_pip_add(sc->object, 1);
616 	for (i = bp->bio_offset / PAGE_SIZE; i <= lastp; i++) {
617 		len = ((i == lastp) ? lastend : PAGE_SIZE) - offs;
618 
619 		m = vm_page_grab(sc->object, i,
620 		    VM_ALLOC_NORMAL|VM_ALLOC_RETRY);
621 		VM_OBJECT_UNLOCK(sc->object);
622 		sched_pin();
623 		sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
624 		VM_OBJECT_LOCK(sc->object);
625 		if (bp->bio_cmd == BIO_READ) {
626 			if (m->valid != VM_PAGE_BITS_ALL)
627 				rv = vm_pager_get_pages(sc->object, &m, 1, 0);
628 			if (rv == VM_PAGER_ERROR) {
629 				sf_buf_free(sf);
630 				sched_unpin();
631 				vm_page_wakeup(m);
632 				break;
633 			}
634 			bcopy((void *)(sf_buf_kva(sf) + offs), p, len);
635 			cpu_flush_dcache(p, len);
636 		} else if (bp->bio_cmd == BIO_WRITE) {
637 			if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL)
638 				rv = vm_pager_get_pages(sc->object, &m, 1, 0);
639 			if (rv == VM_PAGER_ERROR) {
640 				sf_buf_free(sf);
641 				sched_unpin();
642 				vm_page_wakeup(m);
643 				break;
644 			}
645 			bcopy(p, (void *)(sf_buf_kva(sf) + offs), len);
646 			m->valid = VM_PAGE_BITS_ALL;
647 #if 0
648 		} else if (bp->bio_cmd == BIO_DELETE) {
649 			if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL)
650 				rv = vm_pager_get_pages(sc->object, &m, 1, 0);
651 			if (rv == VM_PAGER_ERROR) {
652 				sf_buf_free(sf);
653 				sched_unpin();
654 				vm_page_wakeup(m);
655 				break;
656 			}
657 			bzero((void *)(sf_buf_kva(sf) + offs), len);
658 			vm_page_dirty(m);
659 			m->valid = VM_PAGE_BITS_ALL;
660 #endif
661 		}
662 		sf_buf_free(sf);
663 		sched_unpin();
664 		vm_page_wakeup(m);
665 		vm_page_lock(m);
666 		vm_page_activate(m);
667 		vm_page_unlock(m);
668 		if (bp->bio_cmd == BIO_WRITE)
669 			vm_page_dirty(m);
670 
671 		/* Actions on further pages start at offset 0 */
672 		p += PAGE_SIZE - offs;
673 		offs = 0;
674 #if 0
675 if (bootverbose || bp->bio_offset / PAGE_SIZE < 17)
676 printf("wire_count %d busy %d flags %x hold_count %d act_count %d queue %d valid %d dirty %d @ %d\n",
677     m->wire_count, m->busy,
678     m->flags, m->hold_count, m->act_count, m->queue, m->valid, m->dirty, i);
679 #endif
680 	}
681 	vm_object_pip_subtract(sc->object, 1);
682 	vm_object_set_writeable_dirty(sc->object);
683 	VM_OBJECT_UNLOCK(sc->object);
684 	return (rv != VM_PAGER_ERROR ? 0 : ENOSPC);
685 }
686 
687 static void
688 md_kthread(void *arg)
689 {
690 	struct md_s *sc;
691 	struct bio *bp;
692 	int error;
693 
694 	sc = arg;
695 	thread_lock(curthread);
696 	sched_prio(curthread, PRIBIO);
697 	thread_unlock(curthread);
698 	if (sc->type == MD_VNODE)
699 		curthread->td_pflags |= TDP_NORUNNINGBUF;
700 
701 	for (;;) {
702 		mtx_lock(&sc->queue_mtx);
703 		if (sc->flags & MD_SHUTDOWN) {
704 			sc->flags |= MD_EXITING;
705 			mtx_unlock(&sc->queue_mtx);
706 			kproc_exit(0);
707 		}
708 		bp = bioq_takefirst(&sc->bio_queue);
709 		if (!bp) {
710 			msleep(sc, &sc->queue_mtx, PRIBIO | PDROP, "mdwait", 0);
711 			continue;
712 		}
713 		mtx_unlock(&sc->queue_mtx);
714 		if (bp->bio_cmd == BIO_GETATTR) {
715 			if (sc->fwsectors && sc->fwheads &&
716 			    (g_handleattr_int(bp, "GEOM::fwsectors",
717 			    sc->fwsectors) ||
718 			    g_handleattr_int(bp, "GEOM::fwheads",
719 			    sc->fwheads)))
720 				error = -1;
721 			else
722 				error = EOPNOTSUPP;
723 		} else {
724 			error = sc->start(sc, bp);
725 		}
726 
727 		if (error != -1) {
728 			bp->bio_completed = bp->bio_length;
729 			g_io_deliver(bp, error);
730 			if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE))
731 				devstat_end_transaction_bio(sc->devstat, bp);
732 		}
733 	}
734 }
735 
736 static struct md_s *
737 mdfind(int unit)
738 {
739 	struct md_s *sc;
740 
741 	LIST_FOREACH(sc, &md_softc_list, list) {
742 		if (sc->unit == unit)
743 			break;
744 	}
745 	return (sc);
746 }
747 
748 static struct md_s *
749 mdnew(int unit, int *errp, enum md_types type)
750 {
751 	struct md_s *sc, *sc2;
752 	int error, max = -1;
753 
754 	*errp = 0;
755 	LIST_FOREACH(sc2, &md_softc_list, list) {
756 		if (unit == sc2->unit) {
757 			*errp = EBUSY;
758 			return (NULL);
759 		}
760 		if (unit == -1 && sc2->unit > max)
761 			max = sc2->unit;
762 	}
763 	if (unit == -1)
764 		unit = max + 1;
765 	sc = (struct md_s *)malloc(sizeof *sc, M_MD, M_WAITOK | M_ZERO);
766 	sc->type = type;
767 	bioq_init(&sc->bio_queue);
768 	mtx_init(&sc->queue_mtx, "md bio queue", NULL, MTX_DEF);
769 	sc->unit = unit;
770 	sprintf(sc->name, "md%d", unit);
771 	LIST_INSERT_HEAD(&md_softc_list, sc, list);
772 	error = kproc_create(md_kthread, sc, &sc->procp, 0, 0,"%s", sc->name);
773 	if (error == 0)
774 		return (sc);
775 	LIST_REMOVE(sc, list);
776 	mtx_destroy(&sc->queue_mtx);
777 	free(sc, M_MD);
778 	*errp = error;
779 	return (NULL);
780 }
781 
782 static void
783 mdinit(struct md_s *sc)
784 {
785 
786 	struct g_geom *gp;
787 	struct g_provider *pp;
788 
789 	g_topology_lock();
790 	gp = g_new_geomf(&g_md_class, "md%d", sc->unit);
791 	gp->softc = sc;
792 	pp = g_new_providerf(gp, "md%d", sc->unit);
793 	pp->mediasize = sc->mediasize;
794 	pp->sectorsize = sc->sectorsize;
795 	sc->gp = gp;
796 	sc->pp = pp;
797 	g_error_provider(pp, 0);
798 	g_topology_unlock();
799 	sc->devstat = devstat_new_entry("md", sc->unit, sc->sectorsize,
800 	    DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
801 }
802 
803 /*
804  * XXX: we should check that the range they feed us is mapped.
805  * XXX: we should implement read-only.
806  */
807 
808 static int
809 mdcreate_preload(struct md_s *sc, struct md_ioctl *mdio)
810 {
811 
812 	if (mdio->md_options & ~(MD_AUTOUNIT | MD_FORCE))
813 		return (EINVAL);
814 	if (mdio->md_base == 0)
815 		return (EINVAL);
816 	sc->flags = mdio->md_options & MD_FORCE;
817 	/* Cast to pointer size, then to pointer to avoid warning */
818 	sc->pl_ptr = (u_char *)(uintptr_t)mdio->md_base;
819 	sc->pl_len = (size_t)sc->mediasize;
820 	return (0);
821 }
822 
823 
824 static int
825 mdcreate_malloc(struct md_s *sc, struct md_ioctl *mdio)
826 {
827 	uintptr_t sp;
828 	int error;
829 	off_t u;
830 
831 	error = 0;
832 	if (mdio->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE))
833 		return (EINVAL);
834 	if (mdio->md_sectorsize != 0 && !powerof2(mdio->md_sectorsize))
835 		return (EINVAL);
836 	/* Compression doesn't make sense if we have reserved space */
837 	if (mdio->md_options & MD_RESERVE)
838 		mdio->md_options &= ~MD_COMPRESS;
839 	if (mdio->md_fwsectors != 0)
840 		sc->fwsectors = mdio->md_fwsectors;
841 	if (mdio->md_fwheads != 0)
842 		sc->fwheads = mdio->md_fwheads;
843 	sc->flags = mdio->md_options & (MD_COMPRESS | MD_FORCE);
844 	sc->indir = dimension(sc->mediasize / sc->sectorsize);
845 	sc->uma = uma_zcreate(sc->name, sc->sectorsize, NULL, NULL, NULL, NULL,
846 	    0x1ff, 0);
847 	if (mdio->md_options & MD_RESERVE) {
848 		off_t nsectors;
849 
850 		nsectors = sc->mediasize / sc->sectorsize;
851 		for (u = 0; u < nsectors; u++) {
852 			sp = (uintptr_t)uma_zalloc(sc->uma, M_NOWAIT | M_ZERO);
853 			if (sp != 0)
854 				error = s_write(sc->indir, u, sp);
855 			else
856 				error = ENOMEM;
857 			if (error != 0)
858 				break;
859 		}
860 	}
861 	return (error);
862 }
863 
864 
865 static int
866 mdsetcred(struct md_s *sc, struct ucred *cred)
867 {
868 	char *tmpbuf;
869 	int error = 0;
870 
871 	/*
872 	 * Set credits in our softc
873 	 */
874 
875 	if (sc->cred)
876 		crfree(sc->cred);
877 	sc->cred = crhold(cred);
878 
879 	/*
880 	 * Horrible kludge to establish credentials for NFS  XXX.
881 	 */
882 
883 	if (sc->vnode) {
884 		struct uio auio;
885 		struct iovec aiov;
886 
887 		tmpbuf = malloc(sc->sectorsize, M_TEMP, M_WAITOK);
888 		bzero(&auio, sizeof(auio));
889 
890 		aiov.iov_base = tmpbuf;
891 		aiov.iov_len = sc->sectorsize;
892 		auio.uio_iov = &aiov;
893 		auio.uio_iovcnt = 1;
894 		auio.uio_offset = 0;
895 		auio.uio_rw = UIO_READ;
896 		auio.uio_segflg = UIO_SYSSPACE;
897 		auio.uio_resid = aiov.iov_len;
898 		vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY);
899 		error = VOP_READ(sc->vnode, &auio, 0, sc->cred);
900 		VOP_UNLOCK(sc->vnode, 0);
901 		free(tmpbuf, M_TEMP);
902 	}
903 	return (error);
904 }
905 
906 static int
907 mdcreate_vnode(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
908 {
909 	struct vattr vattr;
910 	struct nameidata nd;
911 	int error, flags, vfslocked;
912 
913 	error = copyinstr(mdio->md_file, sc->file, sizeof(sc->file), NULL);
914 	if (error != 0)
915 		return (error);
916 	flags = FREAD|FWRITE;
917 	/*
918 	 * If the user specified that this is a read only device, unset the
919 	 * FWRITE mask before trying to open the backing store.
920 	 */
921 	if ((mdio->md_options & MD_READONLY) != 0)
922 		flags &= ~FWRITE;
923 	NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, sc->file, td);
924 	error = vn_open(&nd, &flags, 0, NULL);
925 	if (error != 0)
926 		return (error);
927 	vfslocked = NDHASGIANT(&nd);
928 	NDFREE(&nd, NDF_ONLY_PNBUF);
929 	if (nd.ni_vp->v_type != VREG) {
930 		error = EINVAL;
931 		goto bad;
932 	}
933 	error = VOP_GETATTR(nd.ni_vp, &vattr, td->td_ucred);
934 	if (error != 0)
935 		goto bad;
936 	if (VOP_ISLOCKED(nd.ni_vp) != LK_EXCLUSIVE) {
937 		vn_lock(nd.ni_vp, LK_UPGRADE | LK_RETRY);
938 		if (nd.ni_vp->v_iflag & VI_DOOMED) {
939 			/* Forced unmount. */
940 			error = EBADF;
941 			goto bad;
942 		}
943 	}
944 	nd.ni_vp->v_vflag |= VV_MD;
945 	VOP_UNLOCK(nd.ni_vp, 0);
946 
947 	if (mdio->md_fwsectors != 0)
948 		sc->fwsectors = mdio->md_fwsectors;
949 	if (mdio->md_fwheads != 0)
950 		sc->fwheads = mdio->md_fwheads;
951 	sc->flags = mdio->md_options & (MD_FORCE | MD_ASYNC);
952 	if (!(flags & FWRITE))
953 		sc->flags |= MD_READONLY;
954 	sc->vnode = nd.ni_vp;
955 
956 	error = mdsetcred(sc, td->td_ucred);
957 	if (error != 0) {
958 		sc->vnode = NULL;
959 		vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY);
960 		nd.ni_vp->v_vflag &= ~VV_MD;
961 		goto bad;
962 	}
963 	VFS_UNLOCK_GIANT(vfslocked);
964 	return (0);
965 bad:
966 	VOP_UNLOCK(nd.ni_vp, 0);
967 	(void)vn_close(nd.ni_vp, flags, td->td_ucred, td);
968 	VFS_UNLOCK_GIANT(vfslocked);
969 	return (error);
970 }
971 
972 static int
973 mddestroy(struct md_s *sc, struct thread *td)
974 {
975 	int vfslocked;
976 
977 	if (sc->gp) {
978 		sc->gp->softc = NULL;
979 		g_topology_lock();
980 		g_wither_geom(sc->gp, ENXIO);
981 		g_topology_unlock();
982 		sc->gp = NULL;
983 		sc->pp = NULL;
984 	}
985 	if (sc->devstat) {
986 		devstat_remove_entry(sc->devstat);
987 		sc->devstat = NULL;
988 	}
989 	mtx_lock(&sc->queue_mtx);
990 	sc->flags |= MD_SHUTDOWN;
991 	wakeup(sc);
992 	while (!(sc->flags & MD_EXITING))
993 		msleep(sc->procp, &sc->queue_mtx, PRIBIO, "mddestroy", hz / 10);
994 	mtx_unlock(&sc->queue_mtx);
995 	mtx_destroy(&sc->queue_mtx);
996 	if (sc->vnode != NULL) {
997 		vfslocked = VFS_LOCK_GIANT(sc->vnode->v_mount);
998 		vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY);
999 		sc->vnode->v_vflag &= ~VV_MD;
1000 		VOP_UNLOCK(sc->vnode, 0);
1001 		(void)vn_close(sc->vnode, sc->flags & MD_READONLY ?
1002 		    FREAD : (FREAD|FWRITE), sc->cred, td);
1003 		VFS_UNLOCK_GIANT(vfslocked);
1004 	}
1005 	if (sc->cred != NULL)
1006 		crfree(sc->cred);
1007 	if (sc->object != NULL)
1008 		vm_object_deallocate(sc->object);
1009 	if (sc->indir)
1010 		destroy_indir(sc, sc->indir);
1011 	if (sc->uma)
1012 		uma_zdestroy(sc->uma);
1013 
1014 	LIST_REMOVE(sc, list);
1015 	free(sc, M_MD);
1016 	return (0);
1017 }
1018 
1019 static int
1020 mdcreate_swap(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
1021 {
1022 	vm_ooffset_t npage;
1023 	int error;
1024 
1025 	/*
1026 	 * Range check.  Disallow negative sizes or any size less then the
1027 	 * size of a page.  Then round to a page.
1028 	 */
1029 	if (sc->mediasize == 0 || (sc->mediasize % PAGE_SIZE) != 0)
1030 		return (EDOM);
1031 
1032 	/*
1033 	 * Allocate an OBJT_SWAP object.
1034 	 *
1035 	 * Note the truncation.
1036 	 */
1037 
1038 	npage = mdio->md_mediasize / PAGE_SIZE;
1039 	if (mdio->md_fwsectors != 0)
1040 		sc->fwsectors = mdio->md_fwsectors;
1041 	if (mdio->md_fwheads != 0)
1042 		sc->fwheads = mdio->md_fwheads;
1043 	sc->object = vm_pager_allocate(OBJT_SWAP, NULL, PAGE_SIZE * npage,
1044 	    VM_PROT_DEFAULT, 0, td->td_ucred);
1045 	if (sc->object == NULL)
1046 		return (ENOMEM);
1047 	sc->flags = mdio->md_options & MD_FORCE;
1048 	if (mdio->md_options & MD_RESERVE) {
1049 		if (swap_pager_reserve(sc->object, 0, npage) < 0) {
1050 			error = EDOM;
1051 			goto finish;
1052 		}
1053 	}
1054 	error = mdsetcred(sc, td->td_ucred);
1055  finish:
1056 	if (error != 0) {
1057 		vm_object_deallocate(sc->object);
1058 		sc->object = NULL;
1059 	}
1060 	return (error);
1061 }
1062 
1063 
1064 static int
1065 xmdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
1066 {
1067 	struct md_ioctl *mdio;
1068 	struct md_s *sc;
1069 	int error, i;
1070 
1071 	if (md_debug)
1072 		printf("mdctlioctl(%s %lx %p %x %p)\n",
1073 			devtoname(dev), cmd, addr, flags, td);
1074 
1075 	mdio = (struct md_ioctl *)addr;
1076 	if (mdio->md_version != MDIOVERSION)
1077 		return (EINVAL);
1078 
1079 	/*
1080 	 * We assert the version number in the individual ioctl
1081 	 * handlers instead of out here because (a) it is possible we
1082 	 * may add another ioctl in the future which doesn't read an
1083 	 * mdio, and (b) the correct return value for an unknown ioctl
1084 	 * is ENOIOCTL, not EINVAL.
1085 	 */
1086 	error = 0;
1087 	switch (cmd) {
1088 	case MDIOCATTACH:
1089 		switch (mdio->md_type) {
1090 		case MD_MALLOC:
1091 		case MD_PRELOAD:
1092 		case MD_VNODE:
1093 		case MD_SWAP:
1094 			break;
1095 		default:
1096 			return (EINVAL);
1097 		}
1098 		if (mdio->md_options & MD_AUTOUNIT)
1099 			sc = mdnew(-1, &error, mdio->md_type);
1100 		else
1101 			sc = mdnew(mdio->md_unit, &error, mdio->md_type);
1102 		if (sc == NULL)
1103 			return (error);
1104 		if (mdio->md_options & MD_AUTOUNIT)
1105 			mdio->md_unit = sc->unit;
1106 		sc->mediasize = mdio->md_mediasize;
1107 		if (mdio->md_sectorsize == 0)
1108 			sc->sectorsize = DEV_BSIZE;
1109 		else
1110 			sc->sectorsize = mdio->md_sectorsize;
1111 		error = EDOOFUS;
1112 		switch (sc->type) {
1113 		case MD_MALLOC:
1114 			sc->start = mdstart_malloc;
1115 			error = mdcreate_malloc(sc, mdio);
1116 			break;
1117 		case MD_PRELOAD:
1118 			sc->start = mdstart_preload;
1119 			error = mdcreate_preload(sc, mdio);
1120 			break;
1121 		case MD_VNODE:
1122 			sc->start = mdstart_vnode;
1123 			error = mdcreate_vnode(sc, mdio, td);
1124 			break;
1125 		case MD_SWAP:
1126 			sc->start = mdstart_swap;
1127 			error = mdcreate_swap(sc, mdio, td);
1128 			break;
1129 		}
1130 		if (error != 0) {
1131 			mddestroy(sc, td);
1132 			return (error);
1133 		}
1134 
1135 		/* Prune off any residual fractional sector */
1136 		i = sc->mediasize % sc->sectorsize;
1137 		sc->mediasize -= i;
1138 
1139 		mdinit(sc);
1140 		return (0);
1141 	case MDIOCDETACH:
1142 		if (mdio->md_mediasize != 0 ||
1143 		    (mdio->md_options & ~MD_FORCE) != 0)
1144 			return (EINVAL);
1145 
1146 		sc = mdfind(mdio->md_unit);
1147 		if (sc == NULL)
1148 			return (ENOENT);
1149 		if (sc->opencount != 0 && !(sc->flags & MD_FORCE) &&
1150 		    !(mdio->md_options & MD_FORCE))
1151 			return (EBUSY);
1152 		return (mddestroy(sc, td));
1153 	case MDIOCQUERY:
1154 		sc = mdfind(mdio->md_unit);
1155 		if (sc == NULL)
1156 			return (ENOENT);
1157 		mdio->md_type = sc->type;
1158 		mdio->md_options = sc->flags;
1159 		mdio->md_mediasize = sc->mediasize;
1160 		mdio->md_sectorsize = sc->sectorsize;
1161 		if (sc->type == MD_VNODE)
1162 			error = copyout(sc->file, mdio->md_file,
1163 			    strlen(sc->file) + 1);
1164 		return (error);
1165 	case MDIOCLIST:
1166 		i = 1;
1167 		LIST_FOREACH(sc, &md_softc_list, list) {
1168 			if (i == MDNPAD - 1)
1169 				mdio->md_pad[i] = -1;
1170 			else
1171 				mdio->md_pad[i++] = sc->unit;
1172 		}
1173 		mdio->md_pad[0] = i - 1;
1174 		return (0);
1175 	default:
1176 		return (ENOIOCTL);
1177 	};
1178 }
1179 
1180 static int
1181 mdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
1182 {
1183 	int error;
1184 
1185 	sx_xlock(&md_sx);
1186 	error = xmdctlioctl(dev, cmd, addr, flags, td);
1187 	sx_xunlock(&md_sx);
1188 	return (error);
1189 }
1190 
1191 static void
1192 md_preloaded(u_char *image, size_t length)
1193 {
1194 	struct md_s *sc;
1195 	int error;
1196 
1197 	sc = mdnew(-1, &error, MD_PRELOAD);
1198 	if (sc == NULL)
1199 		return;
1200 	sc->mediasize = length;
1201 	sc->sectorsize = DEV_BSIZE;
1202 	sc->pl_ptr = image;
1203 	sc->pl_len = length;
1204 	sc->start = mdstart_preload;
1205 #ifdef MD_ROOT
1206 	if (sc->unit == 0)
1207 		rootdevnames[0] = "ufs:/dev/md0";
1208 #endif
1209 	mdinit(sc);
1210 }
1211 
1212 static void
1213 g_md_init(struct g_class *mp __unused)
1214 {
1215 
1216 	caddr_t mod;
1217 	caddr_t c;
1218 	u_char *ptr, *name, *type;
1219 	unsigned len;
1220 	int i;
1221 
1222 	/* figure out log2(NINDIR) */
1223 	for (i = NINDIR, nshift = -1; i; nshift++)
1224 		i >>= 1;
1225 
1226 	mod = NULL;
1227 	sx_init(&md_sx, "MD config lock");
1228 	g_topology_unlock();
1229 #ifdef MD_ROOT_SIZE
1230 	sx_xlock(&md_sx);
1231 	md_preloaded(mfs_root.start, sizeof(mfs_root.start));
1232 	sx_xunlock(&md_sx);
1233 #endif
1234 	/* XXX: are preload_* static or do they need Giant ? */
1235 	while ((mod = preload_search_next_name(mod)) != NULL) {
1236 		name = (char *)preload_search_info(mod, MODINFO_NAME);
1237 		if (name == NULL)
1238 			continue;
1239 		type = (char *)preload_search_info(mod, MODINFO_TYPE);
1240 		if (type == NULL)
1241 			continue;
1242 		if (strcmp(type, "md_image") && strcmp(type, "mfs_root"))
1243 			continue;
1244 		c = preload_search_info(mod, MODINFO_ADDR);
1245 		ptr = *(u_char **)c;
1246 		c = preload_search_info(mod, MODINFO_SIZE);
1247 		len = *(size_t *)c;
1248 		printf("%s%d: Preloaded image <%s> %d bytes at %p\n",
1249 		    MD_NAME, mdunits, name, len, ptr);
1250 		sx_xlock(&md_sx);
1251 		md_preloaded(ptr, len);
1252 		sx_xunlock(&md_sx);
1253 	}
1254 	status_dev = make_dev(&mdctl_cdevsw, INT_MAX, UID_ROOT, GID_WHEEL,
1255 	    0600, MDCTL_NAME);
1256 	g_topology_lock();
1257 }
1258 
1259 static void
1260 g_md_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1261     struct g_consumer *cp __unused, struct g_provider *pp)
1262 {
1263 	struct md_s *mp;
1264 	char *type;
1265 
1266 	mp = gp->softc;
1267 	if (mp == NULL)
1268 		return;
1269 
1270 	switch (mp->type) {
1271 	case MD_MALLOC:
1272 		type = "malloc";
1273 		break;
1274 	case MD_PRELOAD:
1275 		type = "preload";
1276 		break;
1277 	case MD_VNODE:
1278 		type = "vnode";
1279 		break;
1280 	case MD_SWAP:
1281 		type = "swap";
1282 		break;
1283 	default:
1284 		type = "unknown";
1285 		break;
1286 	}
1287 
1288 	if (pp != NULL) {
1289 		if (indent == NULL) {
1290 			sbuf_printf(sb, " u %d", mp->unit);
1291 			sbuf_printf(sb, " s %ju", (uintmax_t) mp->sectorsize);
1292 			sbuf_printf(sb, " f %ju", (uintmax_t) mp->fwheads);
1293 			sbuf_printf(sb, " fs %ju", (uintmax_t) mp->fwsectors);
1294 			sbuf_printf(sb, " l %ju", (uintmax_t) mp->mediasize);
1295 			sbuf_printf(sb, " t %s", type);
1296 			if (mp->type == MD_VNODE && mp->vnode != NULL)
1297 				sbuf_printf(sb, " file %s", mp->file);
1298 		} else {
1299 			sbuf_printf(sb, "%s<unit>%d</unit>\n", indent,
1300 			    mp->unit);
1301 			sbuf_printf(sb, "%s<sectorsize>%ju</sectorsize>\n",
1302 			    indent, (uintmax_t) mp->sectorsize);
1303 			sbuf_printf(sb, "%s<fwheads>%ju</fwheads>\n",
1304 			    indent, (uintmax_t) mp->fwheads);
1305 			sbuf_printf(sb, "%s<fwsectors>%ju</fwsectors>\n",
1306 			    indent, (uintmax_t) mp->fwsectors);
1307 			sbuf_printf(sb, "%s<length>%ju</length>\n",
1308 			    indent, (uintmax_t) mp->mediasize);
1309 			sbuf_printf(sb, "%s<type>%s</type>\n", indent,
1310 			    type);
1311 			if (mp->type == MD_VNODE && mp->vnode != NULL)
1312 				sbuf_printf(sb, "%s<file>%s</file>\n",
1313 				    indent, mp->file);
1314 		}
1315 	}
1316 }
1317 
1318 static void
1319 g_md_fini(struct g_class *mp __unused)
1320 {
1321 
1322 	sx_destroy(&md_sx);
1323 	if (status_dev != NULL)
1324 		destroy_dev(status_dev);
1325 }
1326