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.19 2008/01/05 14:02:37 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 devstat_trans_flags dop; 162 struct md_s *sc; 163 int i; 164 165 if (md_debug > 1) 166 kprintf("mdstrategy_malloc(%p) %s %08xx, %lld, %d, %p)\n", 167 bp, devtoname(dev), bp->b_flags, bio->bio_offset, 168 bp->b_bcount, bp->b_data); 169 170 sc = dev->si_drv1; 171 172 crit_enter(); 173 174 bioqdisksort(&sc->bio_queue, bio); 175 176 if (sc->busy) { 177 crit_exit(); 178 return(0); 179 } 180 181 sc->busy++; 182 183 while (1) { 184 bio = bioq_first(&sc->bio_queue); 185 if (bio == NULL) { 186 crit_exit(); 187 break; 188 } 189 crit_exit(); 190 bioq_remove(&sc->bio_queue, bio); 191 bp = bio->bio_buf; 192 193 devstat_start_transaction(&sc->stats); 194 195 switch(bp->b_cmd) { 196 case BUF_CMD_FREEBLKS: 197 dop = DEVSTAT_NO_DATA; 198 break; 199 case BUF_CMD_READ: 200 dop = DEVSTAT_READ; 201 break; 202 case BUF_CMD_WRITE: 203 dop = DEVSTAT_WRITE; 204 break; 205 default: 206 panic("md: bad b_cmd %d", bp->b_cmd); 207 } 208 209 nsec = bp->b_bcount >> DEV_BSHIFT; 210 secno = (unsigned)(bio->bio_offset >> DEV_BSHIFT); 211 dst = bp->b_data; 212 while (nsec--) { 213 if (secno < sc->nsecp) { 214 secpp = &sc->secp[secno]; 215 if ((u_int)*secpp > 255) { 216 secp = *secpp; 217 secval = 0; 218 } else { 219 secp = 0; 220 secval = (u_int) *secpp; 221 } 222 } else { 223 secpp = 0; 224 secp = 0; 225 secval = 0; 226 } 227 if (md_debug > 2) 228 kprintf("%08x %p %p %d\n", bp->b_flags, secpp, secp, secval); 229 230 switch(bp->b_cmd) { 231 case BUF_CMD_FREEBLKS: 232 if (secpp) { 233 if (secp) 234 FREE(secp, M_MDSECT); 235 *secpp = 0; 236 } 237 break; 238 case BUF_CMD_READ: 239 if (secp) { 240 bcopy(secp, dst, DEV_BSIZE); 241 } else if (secval) { 242 for (i = 0; i < DEV_BSIZE; i++) 243 dst[i] = secval; 244 } else { 245 bzero(dst, DEV_BSIZE); 246 } 247 break; 248 case BUF_CMD_WRITE: 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 *)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 *)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 break; 280 default: 281 panic("md: bad b_cmd %d", bp->b_cmd); 282 283 } 284 secno++; 285 dst += DEV_BSIZE; 286 } 287 bp->b_resid = 0; 288 devstat_end_transaction_buf(&sc->stats, bp); 289 biodone(bio); 290 crit_enter(); 291 } 292 sc->busy = 0; 293 return(0); 294 } 295 296 297 static int 298 mdstrategy_preload(struct dev_strategy_args *ap) 299 { 300 cdev_t dev = ap->a_head.a_dev; 301 struct bio *bio = ap->a_bio; 302 struct buf *bp = bio->bio_buf; 303 devstat_trans_flags dop; 304 struct md_s *sc; 305 306 if (md_debug > 1) 307 kprintf("mdstrategy_preload(%p) %s %08x, %lld, %d, %p)\n", 308 bp, devtoname(dev), bp->b_flags, bio->bio_offset, 309 bp->b_bcount, bp->b_data); 310 311 sc = dev->si_drv1; 312 313 crit_enter(); 314 315 bioqdisksort(&sc->bio_queue, bio); 316 317 if (sc->busy) { 318 crit_exit(); 319 return(0); 320 } 321 322 sc->busy++; 323 324 while (1) { 325 bio = bioq_first(&sc->bio_queue); 326 if (bio) 327 bioq_remove(&sc->bio_queue, bio); 328 crit_exit(); 329 if (bio == NULL) 330 break; 331 332 devstat_start_transaction(&sc->stats); 333 334 switch(bp->b_cmd) { 335 case BUF_CMD_FREEBLKS: 336 dop = DEVSTAT_NO_DATA; 337 break; 338 case BUF_CMD_READ: 339 dop = DEVSTAT_READ; 340 bcopy(sc->pl_ptr + bio->bio_offset, 341 bp->b_data, bp->b_bcount); 342 break; 343 case BUF_CMD_WRITE: 344 dop = DEVSTAT_WRITE; 345 bcopy(bp->b_data, sc->pl_ptr + bio->bio_offset, 346 bp->b_bcount); 347 break; 348 default: 349 panic("md: bad cmd %d\n", bp->b_cmd); 350 } 351 bp->b_resid = 0; 352 devstat_end_transaction_buf(&sc->stats, bp); 353 biodone(bio); 354 crit_enter(); 355 } 356 sc->busy = 0; 357 return(0); 358 } 359 360 static struct md_s * 361 mdcreate(void) 362 { 363 struct md_s *sc; 364 365 MALLOC(sc, struct md_s *,sizeof(*sc), M_MD, M_WAITOK | M_ZERO); 366 sc->unit = mdunits++; 367 bioq_init(&sc->bio_queue); 368 devstat_add_entry(&sc->stats, "md", sc->unit, DEV_BSIZE, 369 DEVSTAT_NO_ORDERED_TAGS, 370 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER, 371 DEVSTAT_PRIORITY_OTHER); 372 sc->dev = disk_create(sc->unit, &sc->disk, &md_ops); 373 sc->dev->si_drv1 = sc; 374 sc->dev->si_iosize_max = DFLTPHYS; 375 376 return (sc); 377 } 378 379 static void 380 mdcreate_preload(u_char *image, unsigned length) 381 { 382 struct md_s *sc; 383 384 sc = mdcreate(); 385 sc->type = MD_PRELOAD; 386 sc->nsect = length / DEV_BSIZE; 387 sc->pl_ptr = image; 388 sc->pl_len = length; 389 390 if (sc->unit == 0) 391 mdrootready = 1; 392 } 393 394 static void 395 mdcreate_malloc(void) 396 { 397 struct md_s *sc; 398 399 sc = mdcreate(); 400 sc->type = MD_MALLOC; 401 402 sc->nsect = MD_NSECT; /* for now */ 403 MALLOC(sc->secp, u_char **, sizeof(u_char *), M_MD, M_WAITOK | M_ZERO); 404 sc->nsecp = 1; 405 kprintf("md%d: Malloc disk\n", sc->unit); 406 } 407 408 static void 409 md_drvinit(void *unused) 410 { 411 412 caddr_t mod; 413 caddr_t c; 414 u_char *ptr, *name, *type; 415 unsigned len; 416 417 #ifdef MD_ROOT_SIZE 418 mdcreate_preload(mfs_root, MD_ROOT_SIZE*1024); 419 #endif 420 mod = NULL; 421 while ((mod = preload_search_next_name(mod)) != NULL) { 422 name = (char *)preload_search_info(mod, MODINFO_NAME); 423 type = (char *)preload_search_info(mod, MODINFO_TYPE); 424 if (name == NULL) 425 continue; 426 if (type == NULL) 427 continue; 428 if (strcmp(type, "md_image") && strcmp(type, "mfs_root")) 429 continue; 430 c = preload_search_info(mod, MODINFO_ADDR); 431 ptr = *(u_char **)c; 432 c = preload_search_info(mod, MODINFO_SIZE); 433 len = *(unsigned *)c; 434 kprintf("md%d: Preloaded image <%s> %d bytes at %p\n", 435 mdunits, name, len, ptr); 436 mdcreate_preload(ptr, len); 437 } 438 mdcreate_malloc(); 439 } 440 441 SYSINIT(mddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR, md_drvinit,NULL) 442 443 #ifdef MD_ROOT 444 static void 445 md_takeroot(void *junk) 446 { 447 if (mdrootready) 448 rootdevnames[0] = "ufs:/dev/md0c"; 449 } 450 451 SYSINIT(md_root, SI_SUB_MOUNT_ROOT, SI_ORDER_FIRST, md_takeroot, NULL); 452 #endif 453