md.c revision 69781
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: head/sys/dev/md/md.c 69781 2000-12-08 21:51:06Z dwmalone $ 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 32MALLOC_DEFINE(M_MD, "MD disk", "Memory Disk"); 33MALLOC_DEFINE(M_MDSECT, "MD sectors", "Memory Disk Sectors"); 34 35static int md_debug; 36SYSCTL_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: */ 50static u_char mfs_root[MD_ROOT_SIZE*1024] = "MFS Filesystem goes here"; 51static u_char end_mfs_root[] __unused = "MFS Filesystem had better STOP here"; 52#endif 53 54static int mdrootready; 55 56static void mdcreate_malloc(int unit); 57 58#define CDEV_MAJOR 95 59#define BDEV_MAJOR 22 60 61static d_strategy_t mdstrategy; 62static d_strategy_t mdstrategy_preload; 63static d_strategy_t mdstrategy_malloc; 64static d_open_t mdopen; 65static d_ioctl_t mdioctl; 66 67static 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 84static struct cdevsw mddisk_cdevsw; 85 86static LIST_HEAD(, md_s) md_softc_list = LIST_HEAD_INITIALIZER(&md_softc_list); 87 88struct 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 108static int mdunits; 109 110static int 111mdopen(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 135static int 136mdioctl(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 146static void 147mdstrategy(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 166static void 167mdstrategy_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 | M_ZERO); 261 bcopy(sc->secp, secpp, sc->nsecp * sizeof(u_char *)); 262 FREE(sc->secp, M_MD); 263 sc->secp = secpp; 264 sc->nsecp = secno + nsec + 1; 265 secpp = &sc->secp[secno]; 266 } 267 if (i == DEV_BSIZE) { 268 if (secp) 269 FREE(secp, M_MDSECT); 270 *secpp = (u_char *)(uintptr_t)uc; 271 } else { 272 if (!secp) 273 MALLOC(secp, u_char *, DEV_BSIZE, M_MDSECT, M_WAITOK); 274 bcopy(dst, secp, DEV_BSIZE); 275 276 *secpp = secp; 277 } 278 } 279 } 280 secno++; 281 dst += DEV_BSIZE; 282 } 283 bp->bio_resid = 0; 284 devstat_end_transaction_bio(&sc->stats, bp); 285 biodone(bp); 286 s = splbio(); 287 } 288 sc->busy = 0; 289 return; 290} 291 292 293static void 294mdstrategy_preload(struct bio *bp) 295{ 296 int s; 297 struct md_s *sc; 298 devstat_trans_flags dop; 299 300 if (md_debug > 1) 301 printf("mdstrategy_preload(%p) %s %x, %d, %ld, %p)\n", 302 bp, devtoname(bp->bio_dev), bp->bio_flags, bp->bio_blkno, 303 bp->bio_bcount / DEV_BSIZE, bp->bio_data); 304 305 sc = bp->bio_dev->si_drv1; 306 307 s = splbio(); 308 309 bioqdisksort(&sc->bio_queue, bp); 310 311 if (sc->busy) { 312 splx(s); 313 return; 314 } 315 316 sc->busy++; 317 318 while (1) { 319 bp = bioq_first(&sc->bio_queue); 320 if (bp) 321 bioq_remove(&sc->bio_queue, bp); 322 splx(s); 323 if (!bp) 324 break; 325 326 devstat_start_transaction(&sc->stats); 327 328 if (bp->bio_cmd == BIO_DELETE) { 329 dop = DEVSTAT_NO_DATA; 330 } else if (bp->bio_cmd == BIO_READ) { 331 dop = DEVSTAT_READ; 332 bcopy(sc->pl_ptr + (bp->bio_pblkno << DEV_BSHIFT), bp->bio_data, bp->bio_bcount); 333 } else { 334 dop = DEVSTAT_WRITE; 335 bcopy(bp->bio_data, sc->pl_ptr + (bp->bio_pblkno << DEV_BSHIFT), bp->bio_bcount); 336 } 337 bp->bio_resid = 0; 338 devstat_end_transaction_bio(&sc->stats, bp); 339 biodone(bp); 340 s = splbio(); 341 } 342 sc->busy = 0; 343 return; 344} 345 346static struct md_s * 347mdcreate(int unit) 348{ 349 struct md_s *sc; 350 351 if (unit == -1) 352 unit = mdunits++; 353 /* Make sure this unit isn't already in action */ 354 LIST_FOREACH(sc, &md_softc_list, list) { 355 if (sc->unit == unit) 356 return (NULL); 357 } 358 MALLOC(sc, struct md_s *,sizeof(*sc), M_MD, M_WAITOK | M_ZERO); 359 LIST_INSERT_HEAD(&md_softc_list, sc, list); 360 sc->unit = unit; 361 bioq_init(&sc->bio_queue); 362 devstat_add_entry(&sc->stats, "md", sc->unit, DEV_BSIZE, 363 DEVSTAT_NO_ORDERED_TAGS, 364 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER, 365 DEVSTAT_PRIORITY_OTHER); 366 sc->dev = disk_create(sc->unit, &sc->disk, 0, &md_cdevsw, &mddisk_cdevsw); 367 sc->dev->si_drv1 = sc; 368 return (sc); 369} 370 371static void 372mdcreate_preload(u_char *image, unsigned length) 373{ 374 struct md_s *sc; 375 376 sc = mdcreate(-1); 377 sc->type = MD_PRELOAD; 378 sc->nsect = length / DEV_BSIZE; 379 sc->pl_ptr = image; 380 sc->pl_len = length; 381 382 if (sc->unit == 0) 383 mdrootready = 1; 384} 385 386static void 387mdcreate_malloc(int unit) 388{ 389 struct md_s *sc; 390 391 sc = mdcreate(unit); 392 if (sc == NULL) 393 return; 394 395 sc->type = MD_MALLOC; 396 397 sc->nsect = MD_NSECT; /* for now */ 398 MALLOC(sc->secp, u_char **, sizeof(u_char *), M_MD, M_WAITOK | M_ZERO); 399 sc->nsecp = 1; 400 printf("md%d: Malloc disk\n", sc->unit); 401} 402 403static void 404md_clone (void *arg, char *name, int namelen, dev_t *dev) 405{ 406 int i, u; 407 408 if (*dev != NODEV) 409 return; 410 i = dev_stdclone(name, NULL, "md", &u); 411 if (i == 0) 412 return; 413 /* XXX: should check that next char is [\0sa-h] */ 414 /* 415 * Now we cheat: We just create the disk, but don't match. 416 * Since we run before it, subr_disk.c::disk_clone() will 417 * find our disk and match the sought for device. 418 */ 419 mdcreate_malloc(u); 420 return; 421} 422 423static void 424md_drvinit(void *unused) 425{ 426 427 caddr_t mod; 428 caddr_t c; 429 u_char *ptr, *name, *type; 430 unsigned len; 431 432#ifdef MD_ROOT_SIZE 433 mdcreate_preload(mfs_root, MD_ROOT_SIZE*1024); 434#endif 435 mod = NULL; 436 while ((mod = preload_search_next_name(mod)) != NULL) { 437 name = (char *)preload_search_info(mod, MODINFO_NAME); 438 type = (char *)preload_search_info(mod, MODINFO_TYPE); 439 if (name == NULL) 440 continue; 441 if (type == NULL) 442 continue; 443 if (strcmp(type, "md_image") && strcmp(type, "mfs_root")) 444 continue; 445 c = preload_search_info(mod, MODINFO_ADDR); 446 ptr = *(u_char **)c; 447 c = preload_search_info(mod, MODINFO_SIZE); 448 len = *(unsigned *)c; 449 printf("md%d: Preloaded image <%s> %d bytes at %p\n", 450 mdunits, name, len, ptr); 451 mdcreate_preload(ptr, len); 452 } 453 EVENTHANDLER_REGISTER(dev_clone, md_clone, 0, 999); 454 if (!devfs_present) 455 mdcreate_malloc(-1); 456} 457 458SYSINIT(mddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR, md_drvinit,NULL) 459 460#ifdef MD_ROOT 461static void 462md_takeroot(void *junk) 463{ 464 if (mdrootready) 465 rootdevnames[0] = "ufs:/dev/md0c"; 466} 467 468SYSINIT(md_root, SI_SUB_MOUNT_ROOT, SI_ORDER_FIRST, md_takeroot, NULL); 469#endif 470 471