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 * ----------------------------------------------------------------------------
9 * $FreeBSD: src/sys/dev/md/md.c,v 1.8.2.2 2002/08/19 17:43:34 jdp Exp $
10 * $DragonFly: src/sys/dev/disk/md/md.c,v 1.15 2006/12/22 23:26:16 swildner Exp $
14 #include "opt_md.h" /* We have adopted some tasks from MFS */
16 #include <sys/param.h>
17 #include <sys/systm.h>
20 #include <sys/devicestat.h>
22 #include <sys/kernel.h>
23 #include <sys/malloc.h>
24 #include <sys/sysctl.h>
25 #include <sys/linker.h>
28 #include <sys/thread2.h>
31 #define MD_NSECT (10000 * 2)
34 MALLOC_DEFINE(M_MD, "MD disk", "Memory Disk");
35 MALLOC_DEFINE(M_MDSECT, "MD sectors", "Memory Disk Sectors");
38 SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0, "");
40 #if defined(MD_ROOT) && defined(MD_ROOT_SIZE)
41 /* Image gets put here: */
42 static u_char mfs_root[MD_ROOT_SIZE*1024] = "MFS Filesystem goes here";
43 static u_char end_mfs_root[] __unused = "MFS Filesystem had better STOP here";
46 static int mdrootready;
48 static void mdcreate_malloc(void);
52 static d_strategy_t mdstrategy;
53 static d_strategy_t mdstrategy_preload;
54 static d_strategy_t mdstrategy_malloc;
55 static d_open_t mdopen;
56 static d_ioctl_t mdioctl;
58 static struct dev_ops md_ops = {
59 { "md", CDEV_MAJOR, D_DISK | D_CANFREE | D_MEMDISK },
65 .d_strategy = mdstrategy,
71 struct bio_queue_head bio_queue;
75 enum {MD_MALLOC, MD_PRELOAD} type;
78 /* MD_MALLOC related fields */
82 /* MD_PRELOAD related fields */
90 mdopen(struct dev_open_args *ap)
92 cdev_t dev = ap->a_head.a_dev;
97 kprintf("mdopen(%s %x %x)\n",
98 devtoname(dev), ap->a_oflags, ap->a_devtype);
101 if (sc->unit + 1 == mdunits)
104 dl = &sc->disk.d_label;
105 bzero(dl, sizeof(*dl));
106 dl->d_secsize = DEV_BSIZE;
107 dl->d_nsectors = 1024;
109 dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks;
110 dl->d_secperunit = sc->nsect;
111 dl->d_ncylinders = dl->d_secperunit / dl->d_secpercyl;
116 mdioctl(struct dev_ioctl_args *ap)
118 cdev_t dev = ap->a_head.a_dev;
121 kprintf("mdioctl(%s %lx %p %x)\n",
122 devtoname(dev), ap->a_cmd, ap->a_data, ap->a_fflag);
128 mdstrategy(struct dev_strategy_args *ap)
130 cdev_t dev = ap->a_head.a_dev;
131 struct bio *bio = ap->a_bio;
132 struct buf *bp = bio->bio_buf;
136 kprintf("mdstrategy(%p) %s %08x, %lld, %d, %p)\n",
137 bp, devtoname(dev), bp->b_flags, bio->bio_offset,
138 bp->b_bcount, bp->b_data);
140 bio->bio_driver_info = dev;
142 if (sc->type == MD_MALLOC) {
143 mdstrategy_malloc(ap);
145 mdstrategy_preload(ap);
152 mdstrategy_malloc(struct dev_strategy_args *ap)
154 cdev_t dev = ap->a_head.a_dev;
155 struct bio *bio = ap->a_bio;
156 struct buf *bp = bio->bio_buf;
157 unsigned secno, nsec, secval, uc;
158 u_char *secp, **secpp, *dst;
159 devstat_trans_flags dop;
164 kprintf("mdstrategy_malloc(%p) %s %08xx, %lld, %d, %p)\n",
165 bp, devtoname(dev), bp->b_flags, bio->bio_offset,
166 bp->b_bcount, bp->b_data);
172 bioqdisksort(&sc->bio_queue, bio);
182 bio = bioq_first(&sc->bio_queue);
184 bioq_remove(&sc->bio_queue, bio);
189 devstat_start_transaction(&sc->stats);
192 case BUF_CMD_FREEBLKS:
193 dop = DEVSTAT_NO_DATA;
202 panic("md: bad b_cmd %d", bp->b_cmd);
205 nsec = bp->b_bcount >> DEV_BSHIFT;
206 secno = (unsigned)(bio->bio_offset >> DEV_BSHIFT);
209 if (secno < sc->nsecp) {
210 secpp = &sc->secp[secno];
211 if ((u_int)*secpp > 255) {
216 secval = (u_int) *secpp;
224 kprintf("%08x %p %p %d\n", bp->b_flags, secpp, secp, secval);
227 case BUF_CMD_FREEBLKS:
230 FREE(secp, M_MDSECT);
236 bcopy(secp, dst, DEV_BSIZE);
238 for (i = 0; i < DEV_BSIZE; i++)
241 bzero(dst, DEV_BSIZE);
246 for (i = 1; i < DEV_BSIZE; i++)
249 if (i == DEV_BSIZE && !uc) {
251 FREE(secp, M_MDSECT);
253 *secpp = (u_char *)uc;
256 MALLOC(secpp, u_char **, (secno + nsec + 1) * sizeof(u_char *), M_MD, M_WAITOK);
257 bzero(secpp, (secno + nsec + 1) * sizeof(u_char *));
258 bcopy(sc->secp, secpp, sc->nsecp * sizeof(u_char *));
259 FREE(sc->secp, M_MD);
261 sc->nsecp = secno + nsec + 1;
262 secpp = &sc->secp[secno];
264 if (i == DEV_BSIZE) {
266 FREE(secp, M_MDSECT);
267 *secpp = (u_char *)uc;
270 MALLOC(secp, u_char *, DEV_BSIZE, M_MDSECT, M_WAITOK);
271 bcopy(dst, secp, DEV_BSIZE);
278 panic("md: bad b_cmd %d", bp->b_cmd);
285 devstat_end_transaction_buf(&sc->stats, bp);
295 mdstrategy_preload(struct dev_strategy_args *ap)
297 cdev_t dev = ap->a_head.a_dev;
298 struct bio *bio = ap->a_bio;
299 struct buf *bp = bio->bio_buf;
300 devstat_trans_flags dop;
304 kprintf("mdstrategy_preload(%p) %s %08x, %lld, %d, %p)\n",
305 bp, devtoname(dev), bp->b_flags, bio->bio_offset,
306 bp->b_bcount, bp->b_data);
312 bioqdisksort(&sc->bio_queue, bio);
322 bio = bioq_first(&sc->bio_queue);
324 bioq_remove(&sc->bio_queue, bio);
329 devstat_start_transaction(&sc->stats);
332 case BUF_CMD_FREEBLKS:
333 dop = DEVSTAT_NO_DATA;
337 bcopy(sc->pl_ptr + bio->bio_offset,
338 bp->b_data, bp->b_bcount);
342 bcopy(bp->b_data, sc->pl_ptr + bio->bio_offset,
346 panic("md: bad cmd %d\n", bp->b_cmd);
349 devstat_end_transaction_buf(&sc->stats, bp);
362 MALLOC(sc, struct md_s *,sizeof(*sc), M_MD, M_WAITOK);
363 bzero(sc, sizeof(*sc));
364 sc->unit = mdunits++;
365 bioq_init(&sc->bio_queue);
366 devstat_add_entry(&sc->stats, "md", sc->unit, DEV_BSIZE,
367 DEVSTAT_NO_ORDERED_TAGS,
368 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER,
369 DEVSTAT_PRIORITY_OTHER);
370 sc->dev = disk_create(sc->unit, &sc->disk, 0, &md_ops);
371 sc->dev->si_drv1 = sc;
376 mdcreate_preload(u_char *image, unsigned length)
381 sc->type = MD_PRELOAD;
382 sc->nsect = length / DEV_BSIZE;
391 mdcreate_malloc(void)
396 sc->type = MD_MALLOC;
398 sc->nsect = MD_NSECT; /* for now */
399 MALLOC(sc->secp, u_char **, sizeof(u_char *), M_MD, M_WAITOK);
400 bzero(sc->secp, sizeof(u_char *));
402 kprintf("md%d: Malloc disk\n", sc->unit);
406 md_drvinit(void *unused)
411 u_char *ptr, *name, *type;
415 mdcreate_preload(mfs_root, MD_ROOT_SIZE*1024);
418 while ((mod = preload_search_next_name(mod)) != NULL) {
419 name = (char *)preload_search_info(mod, MODINFO_NAME);
420 type = (char *)preload_search_info(mod, MODINFO_TYPE);
425 if (strcmp(type, "md_image") && strcmp(type, "mfs_root"))
427 c = preload_search_info(mod, MODINFO_ADDR);
429 c = preload_search_info(mod, MODINFO_SIZE);
430 len = *(unsigned *)c;
431 kprintf("md%d: Preloaded image <%s> %d bytes at %p\n",
432 mdunits, name, len, ptr);
433 mdcreate_preload(ptr, len);
438 SYSINIT(mddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR, md_drvinit,NULL)
442 md_takeroot(void *junk)
445 rootdevnames[0] = "ufs:/dev/md0c";
448 SYSINIT(md_root, SI_SUB_MOUNT_ROOT, SI_ORDER_FIRST, md_takeroot, NULL);