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 #include "opt_mfs.h" /* We have adopted some tasks from MFS */ 14 #include "opt_md.h" 15 16 #include <sys/param.h> 17 #include <sys/systm.h> 18 #include <sys/bio.h> 19 #include <sys/conf.h> 20 #include <sys/devicestat.h> 21 #include <sys/disk.h> 22 #include <sys/kernel.h> 23 #include <sys/malloc.h> 24 #include <sys/sysctl.h> 25 #include <sys/linker.h> 26 #include <sys/queue.h> 27 28 #ifndef MD_NSECT 29 #define MD_NSECT (10000 * 2) 30 #endif 31 32 MALLOC_DEFINE(M_MD, "MD disk", "Memory Disk"); 33 MALLOC_DEFINE(M_MDSECT, "MD sectors", "Memory Disk Sectors"); 34 35 static int md_debug; 36 SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0, ""); 37 38 #if defined(MFS_ROOT) && !defined(MD_ROOT) 39 #define MD_ROOT MFS_ROOT 40 #warning "option MFS_ROOT has been superceeded by MD_ROOT" 41 #endif 42 43 #if defined(MFS_ROOT_SIZE) && !defined(MD_ROOT_SIZE) 44 #define MD_ROOT_SIZE MFS_ROOT_SIZE 45 #warning "option MFS_ROOT_SIZE has been superceeded by MD_ROOT_SIZE" 46 #endif 47 48 #if defined(MD_ROOT) && defined(MD_ROOT_SIZE) 49 /* Image gets put here: */ 50 static u_char mfs_root[MD_ROOT_SIZE*1024] = "MFS Filesystem goes here"; 51 static u_char end_mfs_root[] __unused = "MFS Filesystem had better STOP here"; 52 #endif 53 54 static int mdrootready; 55 56 static void mdcreate_malloc(int unit); 57 58 #define CDEV_MAJOR 95 59 #define BDEV_MAJOR 22 60 61 static d_strategy_t mdstrategy; 62 static d_strategy_t mdstrategy_preload; 63 static d_strategy_t mdstrategy_malloc; 64 static d_open_t mdopen; 65 static d_ioctl_t mdioctl; 66 67 static struct cdevsw md_cdevsw = { 68 /* open */ mdopen, 69 /* close */ nullclose, 70 /* read */ physread, 71 /* write */ physwrite, 72 /* ioctl */ mdioctl, 73 /* poll */ nopoll, 74 /* mmap */ nommap, 75 /* strategy */ mdstrategy, 76 /* name */ "md", 77 /* maj */ CDEV_MAJOR, 78 /* dump */ nodump, 79 /* psize */ nopsize, 80 /* flags */ D_DISK | D_CANFREE | D_MEMDISK, 81 /* bmaj */ BDEV_MAJOR 82 }; 83 84 static struct cdevsw mddisk_cdevsw; 85 86 static LIST_HEAD(, md_s) md_softc_list = LIST_HEAD_INITIALIZER(&md_softc_list); 87 88 struct md_s { 89 int unit; 90 LIST_ENTRY(md_s) list; 91 struct devstat stats; 92 struct bio_queue_head bio_queue; 93 struct disk disk; 94 dev_t dev; 95 int busy; 96 enum {MD_MALLOC, MD_PRELOAD} type; 97 unsigned nsect; 98 99 /* MD_MALLOC related fields */ 100 unsigned nsecp; 101 u_char **secp; 102 103 /* MD_PRELOAD related fields */ 104 u_char *pl_ptr; 105 unsigned pl_len; 106 }; 107 108 static int mdunits; 109 110 static int 111 mdopen(dev_t dev, int flag, int fmt, struct proc *p) 112 { 113 struct md_s *sc; 114 struct disklabel *dl; 115 116 if (md_debug) 117 printf("mdopen(%s %x %x %p)\n", 118 devtoname(dev), flag, fmt, p); 119 120 sc = dev->si_drv1; 121 if ((!devfs_present) && sc->unit + 1 == mdunits) 122 mdcreate_malloc(-1); 123 124 dl = &sc->disk.d_label; 125 bzero(dl, sizeof(*dl)); 126 dl->d_secsize = DEV_BSIZE; 127 dl->d_nsectors = 1024; 128 dl->d_ntracks = 1; 129 dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks; 130 dl->d_secperunit = sc->nsect; 131 dl->d_ncylinders = dl->d_secperunit / dl->d_secpercyl; 132 return (0); 133 } 134 135 static int 136 mdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct proc *p) 137 { 138 139 if (md_debug) 140 printf("mdioctl(%s %lx %p %x %p)\n", 141 devtoname(dev), cmd, addr, flags, p); 142 143 return (ENOIOCTL); 144 } 145 146 static void 147 mdstrategy(struct bio *bp) 148 { 149 struct md_s *sc; 150 151 if (md_debug > 1) 152 printf("mdstrategy(%p) %s %x, %d, %ld, %p)\n", 153 bp, devtoname(bp->bio_dev), bp->bio_flags, bp->bio_blkno, 154 bp->bio_bcount / DEV_BSIZE, bp->bio_data); 155 156 sc = bp->bio_dev->si_drv1; 157 if (sc->type == MD_MALLOC) { 158 mdstrategy_malloc(bp); 159 } else { 160 mdstrategy_preload(bp); 161 } 162 return; 163 } 164 165 166 static void 167 mdstrategy_malloc(struct bio *bp) 168 { 169 int s, i; 170 struct md_s *sc; 171 devstat_trans_flags dop; 172 u_char *secp, **secpp, *dst; 173 unsigned secno, nsec, secval, uc; 174 175 if (md_debug > 1) 176 printf("mdstrategy_malloc(%p) %s %x, %d, %ld, %p)\n", 177 bp, devtoname(bp->bio_dev), bp->bio_flags, bp->bio_blkno, 178 bp->bio_bcount / DEV_BSIZE, bp->bio_data); 179 180 sc = bp->bio_dev->si_drv1; 181 182 s = splbio(); 183 184 bioqdisksort(&sc->bio_queue, bp); 185 186 if (sc->busy) { 187 splx(s); 188 return; 189 } 190 191 sc->busy++; 192 193 while (1) { 194 bp = bioq_first(&sc->bio_queue); 195 if (bp) 196 bioq_remove(&sc->bio_queue, bp); 197 splx(s); 198 if (!bp) 199 break; 200 201 devstat_start_transaction(&sc->stats); 202 203 if (bp->bio_cmd == BIO_DELETE) 204 dop = DEVSTAT_NO_DATA; 205 else if (bp->bio_cmd == BIO_READ) 206 dop = DEVSTAT_READ; 207 else 208 dop = DEVSTAT_WRITE; 209 210 nsec = bp->bio_bcount / DEV_BSIZE; 211 secno = bp->bio_pblkno; 212 dst = bp->bio_data; 213 while (nsec--) { 214 215 if (secno < sc->nsecp) { 216 secpp = &sc->secp[secno]; 217 if ((uintptr_t)*secpp > 255) { 218 secp = *secpp; 219 secval = 0; 220 } else { 221 secp = 0; 222 secval = (uintptr_t) *secpp; 223 } 224 } else { 225 secpp = 0; 226 secp = 0; 227 secval = 0; 228 } 229 if (md_debug > 2) 230 printf("%x %p %p %d\n", 231 bp->bio_flags, secpp, secp, secval); 232 233 if (bp->bio_cmd == BIO_DELETE) { 234 if (secpp) { 235 if (secp) 236 FREE(secp, M_MDSECT); 237 *secpp = 0; 238 } 239 } else if (bp->bio_cmd == BIO_READ) { 240 if (secp) { 241 bcopy(secp, dst, DEV_BSIZE); 242 } else if (secval) { 243 for (i = 0; i < DEV_BSIZE; i++) 244 dst[i] = secval; 245 } else { 246 bzero(dst, DEV_BSIZE); 247 } 248 } else { 249 uc = dst[0]; 250 for (i = 1; i < DEV_BSIZE; i++) 251 if (dst[i] != uc) 252 break; 253 if (i == DEV_BSIZE && !uc) { 254 if (secp) 255 FREE(secp, M_MDSECT); 256 if (secpp) 257 *secpp = (u_char *)(uintptr_t)uc; 258 } else { 259 if (!secpp) { 260 MALLOC(secpp, u_char **, (secno + nsec + 1) * sizeof(u_char *), M_MD, M_WAITOK); 261 bzero(secpp, (secno + nsec + 1) * sizeof(u_char *)); 262 bcopy(sc->secp, secpp, sc->nsecp * sizeof(u_char *)); 263 FREE(sc->secp, M_MD); 264 sc->secp = secpp; 265 sc->nsecp = secno + nsec + 1; 266 secpp = &sc->secp[secno]; 267 } 268 if (i == DEV_BSIZE) { 269 if (secp) 270 FREE(secp, M_MDSECT); 271 *secpp = (u_char *)(uintptr_t)uc; 272 } else { 273 if (!secp) 274 MALLOC(secp, u_char *, DEV_BSIZE, M_MDSECT, M_WAITOK); 275 bcopy(dst, secp, DEV_BSIZE); 276 277 *secpp = secp; 278 } 279 } 280 } 281 secno++; 282 dst += DEV_BSIZE; 283 } 284 bp->bio_resid = 0; 285 devstat_end_transaction_bio(&sc->stats, bp); 286 biodone(bp); 287 s = splbio(); 288 } 289 sc->busy = 0; 290 return; 291 } 292 293 294 static void 295 mdstrategy_preload(struct bio *bp) 296 { 297 int s; 298 struct md_s *sc; 299 devstat_trans_flags dop; 300 301 if (md_debug > 1) 302 printf("mdstrategy_preload(%p) %s %x, %d, %ld, %p)\n", 303 bp, devtoname(bp->bio_dev), bp->bio_flags, bp->bio_blkno, 304 bp->bio_bcount / DEV_BSIZE, bp->bio_data); 305 306 sc = bp->bio_dev->si_drv1; 307 308 s = splbio(); 309 310 bioqdisksort(&sc->bio_queue, bp); 311 312 if (sc->busy) { 313 splx(s); 314 return; 315 } 316 317 sc->busy++; 318 319 while (1) { 320 bp = bioq_first(&sc->bio_queue); 321 if (bp) 322 bioq_remove(&sc->bio_queue, bp); 323 splx(s); 324 if (!bp) 325 break; 326 327 devstat_start_transaction(&sc->stats); 328 329 if (bp->bio_cmd == BIO_DELETE) { 330 dop = DEVSTAT_NO_DATA; 331 } else if (bp->bio_cmd == BIO_READ) { 332 dop = DEVSTAT_READ; 333 bcopy(sc->pl_ptr + (bp->bio_pblkno << DEV_BSHIFT), bp->bio_data, bp->bio_bcount); 334 } else { 335 dop = DEVSTAT_WRITE; 336 bcopy(bp->bio_data, sc->pl_ptr + (bp->bio_pblkno << DEV_BSHIFT), bp->bio_bcount); 337 } 338 bp->bio_resid = 0; 339 devstat_end_transaction_bio(&sc->stats, bp); 340 biodone(bp); 341 s = splbio(); 342 } 343 sc->busy = 0; 344 return; 345 } 346 347 static struct md_s * 348 mdcreate(int unit) 349 { 350 struct md_s *sc; 351 352 if (unit == -1) 353 unit = mdunits++; 354 /* Make sure this unit isn't already in action */ 355 LIST_FOREACH(sc, &md_softc_list, list) { 356 if (sc->unit == unit) 357 return (NULL); 358 } 359 MALLOC(sc, struct md_s *,sizeof(*sc), M_MD, M_WAITOK); 360 bzero(sc, sizeof(*sc)); 361 LIST_INSERT_HEAD(&md_softc_list, sc, list); 362 sc->unit = unit; 363 bioq_init(&sc->bio_queue); 364 devstat_add_entry(&sc->stats, "md", sc->unit, DEV_BSIZE, 365 DEVSTAT_NO_ORDERED_TAGS, 366 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER, 367 DEVSTAT_PRIORITY_OTHER); 368 sc->dev = disk_create(sc->unit, &sc->disk, 0, &md_cdevsw, &mddisk_cdevsw); 369 sc->dev->si_drv1 = sc; 370 return (sc); 371 } 372 373 static void 374 mdcreate_preload(u_char *image, unsigned length) 375 { 376 struct md_s *sc; 377 378 sc = mdcreate(-1); 379 sc->type = MD_PRELOAD; 380 sc->nsect = length / DEV_BSIZE; 381 sc->pl_ptr = image; 382 sc->pl_len = length; 383 384 if (sc->unit == 0) 385 mdrootready = 1; 386 } 387 388 static void 389 mdcreate_malloc(int unit) 390 { 391 struct md_s *sc; 392 393 sc = mdcreate(unit); 394 if (sc == NULL) 395 return; 396 397 sc->type = MD_MALLOC; 398 399 sc->nsect = MD_NSECT; /* for now */ 400 MALLOC(sc->secp, u_char **, sizeof(u_char *), M_MD, M_WAITOK); 401 bzero(sc->secp, sizeof(u_char *)); 402 sc->nsecp = 1; 403 printf("md%d: Malloc disk\n", sc->unit); 404 } 405 406 static void 407 md_clone (void *arg, char *name, int namelen, dev_t *dev) 408 { 409 int i, u; 410 411 if (*dev != NODEV) 412 return; 413 i = dev_stdclone(name, NULL, "md", &u); 414 if (i == 0) 415 return; 416 /* XXX: should check that next char is [\0sa-h] */ 417 /* 418 * Now we cheat: We just create the disk, but don't match. 419 * Since we run before it, subr_disk.c::disk_clone() will 420 * find our disk and match the sought for device. 421 */ 422 mdcreate_malloc(u); 423 return; 424 } 425 426 static void 427 md_drvinit(void *unused) 428 { 429 430 caddr_t mod; 431 caddr_t c; 432 u_char *ptr, *name, *type; 433 unsigned len; 434 435 #ifdef MD_ROOT_SIZE 436 mdcreate_preload(mfs_root, MD_ROOT_SIZE*1024); 437 #endif 438 mod = NULL; 439 while ((mod = preload_search_next_name(mod)) != NULL) { 440 name = (char *)preload_search_info(mod, MODINFO_NAME); 441 type = (char *)preload_search_info(mod, MODINFO_TYPE); 442 if (name == NULL) 443 continue; 444 if (type == NULL) 445 continue; 446 if (strcmp(type, "md_image") && strcmp(type, "mfs_root")) 447 continue; 448 c = preload_search_info(mod, MODINFO_ADDR); 449 ptr = *(u_char **)c; 450 c = preload_search_info(mod, MODINFO_SIZE); 451 len = *(unsigned *)c; 452 printf("md%d: Preloaded image <%s> %d bytes at %p\n", 453 mdunits, name, len, ptr); 454 mdcreate_preload(ptr, len); 455 } 456 EVENTHANDLER_REGISTER(dev_clone, md_clone, 0, 999); 457 if (!devfs_present) 458 mdcreate_malloc(-1); 459 } 460 461 SYSINIT(mddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR, md_drvinit,NULL) 462 463 #ifdef MD_ROOT 464 static void 465 md_takeroot(void *junk) 466 { 467 if (mdrootready) 468 rootdevnames[0] = "ufs:/dev/md0c"; 469 } 470 471 SYSINIT(md_root, SI_SUB_MOUNT_ROOT, SI_ORDER_FIRST, md_takeroot, NULL); 472 #endif 473 474