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: 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.20 2008/09/07 08:09:39 swildner Exp $ 11 * 12 */ 13 14 #include "opt_md.h" /* We have adopted some tasks from MFS */ 15 16 #include <sys/param.h> 17 #include <sys/systm.h> 18 #include <sys/buf.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/proc.h> 27 #include <sys/buf2.h> 28 #include <sys/thread2.h> 29 30 #ifndef MD_NSECT 31 #define MD_NSECT (10000 * 2) 32 #endif 33 34 MALLOC_DEFINE(M_MD, "MD disk", "Memory Disk"); 35 MALLOC_DEFINE(M_MDSECT, "MD sectors", "Memory Disk Sectors"); 36 37 static int md_debug; 38 SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0, ""); 39 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"; 44 #endif 45 46 static int mdrootready; 47 48 static void mdcreate_malloc(void); 49 50 #define CDEV_MAJOR 95 51 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; 57 58 static struct dev_ops md_ops = { 59 { "md", CDEV_MAJOR, D_DISK | D_CANFREE | D_MEMDISK }, 60 .d_open = mdopen, 61 .d_close = nullclose, 62 .d_read = physread, 63 .d_write = physwrite, 64 .d_ioctl = mdioctl, 65 .d_strategy = mdstrategy, 66 }; 67 68 struct md_s { 69 int unit; 70 struct devstat stats; 71 struct bio_queue_head bio_queue; 72 struct disk disk; 73 cdev_t dev; 74 int busy; 75 enum {MD_MALLOC, MD_PRELOAD} type; 76 unsigned nsect; 77 78 /* MD_MALLOC related fields */ 79 unsigned nsecp; 80 u_char **secp; 81 82 /* MD_PRELOAD related fields */ 83 u_char *pl_ptr; 84 unsigned pl_len; 85 }; 86 87 static int mdunits; 88 89 static int 90 mdopen(struct dev_open_args *ap) 91 { 92 cdev_t dev = ap->a_head.a_dev; 93 struct md_s *sc; 94 struct disk_info info; 95 96 if (md_debug) 97 kprintf("mdopen(%s %x %x)\n", 98 devtoname(dev), ap->a_oflags, ap->a_devtype); 99 100 sc = dev->si_drv1; 101 if (sc->unit + 1 == mdunits) 102 mdcreate_malloc(); 103 104 bzero(&info, sizeof(info)); 105 info.d_media_blksize = DEV_BSIZE; /* mandatory */ 106 info.d_media_blocks = sc->nsect; 107 108 info.d_secpertrack = 1024; /* optional */ 109 info.d_nheads = 1; 110 info.d_secpercyl = info.d_secpertrack * info.d_nheads; 111 info.d_ncylinders = (u_int)(info.d_media_blocks / info.d_secpercyl); 112 disk_setdiskinfo(&sc->disk, &info); 113 114 return (0); 115 } 116 117 static int 118 mdioctl(struct dev_ioctl_args *ap) 119 { 120 cdev_t dev = ap->a_head.a_dev; 121 122 if (md_debug) 123 kprintf("mdioctl(%s %lx %p %x)\n", 124 devtoname(dev), ap->a_cmd, ap->a_data, ap->a_fflag); 125 126 return (ENOIOCTL); 127 } 128 129 static int 130 mdstrategy(struct dev_strategy_args *ap) 131 { 132 cdev_t dev = ap->a_head.a_dev; 133 struct bio *bio = ap->a_bio; 134 struct buf *bp = bio->bio_buf; 135 struct md_s *sc; 136 137 if (md_debug > 1) { 138 kprintf("mdstrategy(%p) %s %08x, %lld, %d, %p)\n", 139 bp, devtoname(dev), bp->b_flags, bio->bio_offset, 140 bp->b_bcount, bp->b_data); 141 } 142 bio->bio_driver_info = dev; 143 sc = dev->si_drv1; 144 if (sc->type == MD_MALLOC) { 145 mdstrategy_malloc(ap); 146 } else { 147 mdstrategy_preload(ap); 148 } 149 return(0); 150 } 151 152 153 static int 154 mdstrategy_malloc(struct dev_strategy_args *ap) 155 { 156 cdev_t dev = ap->a_head.a_dev; 157 struct bio *bio = ap->a_bio; 158 struct buf *bp = bio->bio_buf; 159 unsigned secno, nsec, secval, uc; 160 u_char *secp, **secpp, *dst; 161 struct md_s *sc; 162 int i; 163 164 if (md_debug > 1) 165 kprintf("mdstrategy_malloc(%p) %s %08xx, %lld, %d, %p)\n", 166 bp, devtoname(dev), bp->b_flags, bio->bio_offset, 167 bp->b_bcount, bp->b_data); 168 169 sc = dev->si_drv1; 170 171 crit_enter(); 172 173 bioqdisksort(&sc->bio_queue, bio); 174 175 if (sc->busy) { 176 crit_exit(); 177 return(0); 178 } 179 180 sc->busy++; 181 182 while (1) { 183 bio = bioq_first(&sc->bio_queue); 184 if (bio == NULL) { 185 crit_exit(); 186 break; 187 } 188 crit_exit(); 189 bioq_remove(&sc->bio_queue, bio); 190 bp = bio->bio_buf; 191 192 devstat_start_transaction(&sc->stats); 193 194 switch (bp->b_cmd) { 195 case BUF_CMD_FREEBLKS: 196 case BUF_CMD_READ: 197 case BUF_CMD_WRITE: 198 break; 199 default: 200 panic("md: bad b_cmd %d", bp->b_cmd); 201 } 202 203 nsec = bp->b_bcount >> DEV_BSHIFT; 204 secno = (unsigned)(bio->bio_offset >> DEV_BSHIFT); 205 dst = bp->b_data; 206 while (nsec--) { 207 if (secno < sc->nsecp) { 208 secpp = &sc->secp[secno]; 209 if ((u_int)*secpp > 255) { 210 secp = *secpp; 211 secval = 0; 212 } else { 213 secp = 0; 214 secval = (u_int) *secpp; 215 } 216 } else { 217 secpp = 0; 218 secp = 0; 219 secval = 0; 220 } 221 if (md_debug > 2) 222 kprintf("%08x %p %p %d\n", bp->b_flags, secpp, secp, secval); 223 224 switch (bp->b_cmd) { 225 case BUF_CMD_FREEBLKS: 226 if (secpp) { 227 if (secp) 228 FREE(secp, M_MDSECT); 229 *secpp = 0; 230 } 231 break; 232 case BUF_CMD_READ: 233 if (secp) { 234 bcopy(secp, dst, DEV_BSIZE); 235 } else if (secval) { 236 for (i = 0; i < DEV_BSIZE; i++) 237 dst[i] = secval; 238 } else { 239 bzero(dst, DEV_BSIZE); 240 } 241 break; 242 case BUF_CMD_WRITE: 243 uc = dst[0]; 244 for (i = 1; i < DEV_BSIZE; i++) 245 if (dst[i] != uc) 246 break; 247 if (i == DEV_BSIZE && !uc) { 248 if (secp) 249 FREE(secp, M_MDSECT); 250 if (secpp) 251 *secpp = (u_char *)uc; 252 } else { 253 if (!secpp) { 254 MALLOC(secpp, u_char **, (secno + nsec + 1) * sizeof(u_char *), M_MD, M_WAITOK | M_ZERO); 255 bcopy(sc->secp, secpp, sc->nsecp * sizeof(u_char *)); 256 FREE(sc->secp, M_MD); 257 sc->secp = secpp; 258 sc->nsecp = secno + nsec + 1; 259 secpp = &sc->secp[secno]; 260 } 261 if (i == DEV_BSIZE) { 262 if (secp) 263 FREE(secp, M_MDSECT); 264 *secpp = (u_char *)uc; 265 } else { 266 if (!secp) 267 MALLOC(secp, u_char *, DEV_BSIZE, M_MDSECT, M_WAITOK); 268 bcopy(dst, secp, DEV_BSIZE); 269 270 *secpp = secp; 271 } 272 } 273 break; 274 default: 275 panic("md: bad b_cmd %d", bp->b_cmd); 276 277 } 278 secno++; 279 dst += DEV_BSIZE; 280 } 281 bp->b_resid = 0; 282 devstat_end_transaction_buf(&sc->stats, bp); 283 biodone(bio); 284 crit_enter(); 285 } 286 sc->busy = 0; 287 return(0); 288 } 289 290 291 static int 292 mdstrategy_preload(struct dev_strategy_args *ap) 293 { 294 cdev_t dev = ap->a_head.a_dev; 295 struct bio *bio = ap->a_bio; 296 struct buf *bp = bio->bio_buf; 297 struct md_s *sc; 298 299 if (md_debug > 1) 300 kprintf("mdstrategy_preload(%p) %s %08x, %lld, %d, %p)\n", 301 bp, devtoname(dev), bp->b_flags, bio->bio_offset, 302 bp->b_bcount, bp->b_data); 303 304 sc = dev->si_drv1; 305 306 crit_enter(); 307 308 bioqdisksort(&sc->bio_queue, bio); 309 310 if (sc->busy) { 311 crit_exit(); 312 return(0); 313 } 314 315 sc->busy++; 316 317 while (1) { 318 bio = bioq_first(&sc->bio_queue); 319 if (bio) 320 bioq_remove(&sc->bio_queue, bio); 321 crit_exit(); 322 if (bio == NULL) 323 break; 324 325 devstat_start_transaction(&sc->stats); 326 327 switch (bp->b_cmd) { 328 case BUF_CMD_FREEBLKS: 329 break; 330 case BUF_CMD_READ: 331 bcopy(sc->pl_ptr + bio->bio_offset, 332 bp->b_data, bp->b_bcount); 333 break; 334 case BUF_CMD_WRITE: 335 bcopy(bp->b_data, sc->pl_ptr + bio->bio_offset, 336 bp->b_bcount); 337 break; 338 default: 339 panic("md: bad cmd %d\n", bp->b_cmd); 340 } 341 bp->b_resid = 0; 342 devstat_end_transaction_buf(&sc->stats, bp); 343 biodone(bio); 344 crit_enter(); 345 } 346 sc->busy = 0; 347 return(0); 348 } 349 350 static struct md_s * 351 mdcreate(void) 352 { 353 struct md_s *sc; 354 355 MALLOC(sc, struct md_s *,sizeof(*sc), M_MD, M_WAITOK | M_ZERO); 356 sc->unit = mdunits++; 357 bioq_init(&sc->bio_queue); 358 devstat_add_entry(&sc->stats, "md", sc->unit, DEV_BSIZE, 359 DEVSTAT_NO_ORDERED_TAGS, 360 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER, 361 DEVSTAT_PRIORITY_OTHER); 362 sc->dev = disk_create(sc->unit, &sc->disk, &md_ops); 363 sc->dev->si_drv1 = sc; 364 sc->dev->si_iosize_max = DFLTPHYS; 365 366 return (sc); 367 } 368 369 static void 370 mdcreate_preload(u_char *image, unsigned length) 371 { 372 struct md_s *sc; 373 374 sc = mdcreate(); 375 sc->type = MD_PRELOAD; 376 sc->nsect = length / DEV_BSIZE; 377 sc->pl_ptr = image; 378 sc->pl_len = length; 379 380 if (sc->unit == 0) 381 mdrootready = 1; 382 } 383 384 static void 385 mdcreate_malloc(void) 386 { 387 struct md_s *sc; 388 389 sc = mdcreate(); 390 sc->type = MD_MALLOC; 391 392 sc->nsect = MD_NSECT; /* for now */ 393 MALLOC(sc->secp, u_char **, sizeof(u_char *), M_MD, M_WAITOK | M_ZERO); 394 sc->nsecp = 1; 395 kprintf("md%d: Malloc disk\n", sc->unit); 396 } 397 398 static void 399 md_drvinit(void *unused) 400 { 401 402 caddr_t mod; 403 caddr_t c; 404 u_char *ptr, *name, *type; 405 unsigned len; 406 407 #ifdef MD_ROOT_SIZE 408 mdcreate_preload(mfs_root, MD_ROOT_SIZE*1024); 409 #endif 410 mod = NULL; 411 while ((mod = preload_search_next_name(mod)) != NULL) { 412 name = (char *)preload_search_info(mod, MODINFO_NAME); 413 type = (char *)preload_search_info(mod, MODINFO_TYPE); 414 if (name == NULL) 415 continue; 416 if (type == NULL) 417 continue; 418 if (strcmp(type, "md_image") && strcmp(type, "mfs_root")) 419 continue; 420 c = preload_search_info(mod, MODINFO_ADDR); 421 ptr = *(u_char **)c; 422 c = preload_search_info(mod, MODINFO_SIZE); 423 len = *(unsigned *)c; 424 kprintf("md%d: Preloaded image <%s> %d bytes at %p\n", 425 mdunits, name, len, ptr); 426 mdcreate_preload(ptr, len); 427 } 428 mdcreate_malloc(); 429 } 430 431 SYSINIT(mddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR, md_drvinit,NULL) 432 433 #ifdef MD_ROOT 434 static void 435 md_takeroot(void *junk) 436 { 437 if (mdrootready) 438 rootdevnames[0] = "ufs:/dev/md0c"; 439 } 440 441 SYSINIT(md_root, SI_SUB_MOUNT_ROOT, SI_ORDER_FIRST, md_takeroot, NULL); 442 #endif 443