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