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.7 2004/05/13 23:49:15 dillon Exp $ 11 * 12 */ 13 14 #include "opt_mfs.h" /* We have adopted some tasks from MFS */ 15 #include "opt_md.h" /* We have adopted some tasks from MFS */ 16 17 #include <sys/param.h> 18 #include <sys/systm.h> 19 #include <sys/buf.h> 20 #include <sys/conf.h> 21 #include <sys/devicestat.h> 22 #include <sys/disk.h> 23 #include <sys/kernel.h> 24 #include <sys/malloc.h> 25 #include <sys/sysctl.h> 26 #include <sys/linker.h> 27 #include <sys/proc.h> 28 #include <sys/buf2.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(MFS_ROOT) && !defined(MD_ROOT) 41 #define MD_ROOT MFS_ROOT 42 #warning "option MFS_ROOT has been superceeded by MD_ROOT" 43 #endif 44 45 #if defined(MFS_ROOT_SIZE) && !defined(MD_ROOT_SIZE) 46 #define MD_ROOT_SIZE MFS_ROOT_SIZE 47 #warning "option MFS_ROOT_SIZE has been superceeded by MD_ROOT_SIZE" 48 #endif 49 50 #if defined(MD_ROOT) && defined(MD_ROOT_SIZE) 51 /* Image gets put here: */ 52 static u_char mfs_root[MD_ROOT_SIZE*1024] = "MFS Filesystem goes here"; 53 static u_char end_mfs_root[] __unused = "MFS Filesystem had better STOP here"; 54 #endif 55 56 static int mdrootready; 57 58 static void mdcreate_malloc(void); 59 60 #define CDEV_MAJOR 95 61 62 static d_strategy_t mdstrategy; 63 static d_strategy_t mdstrategy_preload; 64 static d_strategy_t mdstrategy_malloc; 65 static d_open_t mdopen; 66 static d_ioctl_t mdioctl; 67 68 static struct cdevsw md_cdevsw = { 69 /* name */ "md", 70 /* maj */ CDEV_MAJOR, 71 /* flags */ D_DISK | D_CANFREE | D_MEMDISK, 72 /* port */ NULL, 73 /* clone */ NULL, 74 75 /* open */ mdopen, 76 /* close */ nullclose, 77 /* read */ physread, 78 /* write */ physwrite, 79 /* ioctl */ mdioctl, 80 /* poll */ nopoll, 81 /* mmap */ nommap, 82 /* strategy */ mdstrategy, 83 /* dump */ nodump, 84 /* psize */ nopsize, 85 }; 86 87 struct md_s { 88 int unit; 89 struct devstat stats; 90 struct buf_queue_head buf_queue; 91 struct disk disk; 92 dev_t dev; 93 int busy; 94 enum {MD_MALLOC, MD_PRELOAD} type; 95 unsigned nsect; 96 97 /* MD_MALLOC related fields */ 98 unsigned nsecp; 99 u_char **secp; 100 101 /* MD_PRELOAD related fields */ 102 u_char *pl_ptr; 103 unsigned pl_len; 104 }; 105 106 static int mdunits; 107 108 static int 109 mdopen(dev_t dev, int flag, int fmt, struct thread *td) 110 { 111 struct md_s *sc; 112 struct disklabel *dl; 113 114 if (md_debug) 115 printf("mdopen(%s %x %x %p)\n", 116 devtoname(dev), flag, fmt, td); 117 118 sc = dev->si_drv1; 119 if (sc->unit + 1 == mdunits) 120 mdcreate_malloc(); 121 122 dl = &sc->disk.d_label; 123 bzero(dl, sizeof(*dl)); 124 dl->d_secsize = DEV_BSIZE; 125 dl->d_nsectors = 1024; 126 dl->d_ntracks = 1; 127 dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks; 128 dl->d_secperunit = sc->nsect; 129 dl->d_ncylinders = dl->d_secperunit / dl->d_secpercyl; 130 return (0); 131 } 132 133 static int 134 mdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td) 135 { 136 137 if (md_debug) 138 printf("mdioctl(%s %lx %p %x %p)\n", 139 devtoname(dev), cmd, addr, flags, td); 140 141 return (ENOIOCTL); 142 } 143 144 static void 145 mdstrategy(struct buf *bp) 146 { 147 struct md_s *sc; 148 149 if (md_debug > 1) 150 printf("mdstrategy(%p) %s %lx, %d, %ld, %p)\n", 151 bp, devtoname(bp->b_dev), bp->b_flags, bp->b_blkno, 152 bp->b_bcount / DEV_BSIZE, bp->b_data); 153 154 sc = bp->b_dev->si_drv1; 155 if (sc->type == MD_MALLOC) { 156 mdstrategy_malloc(bp); 157 } else { 158 mdstrategy_preload(bp); 159 } 160 return; 161 } 162 163 164 static void 165 mdstrategy_malloc(struct buf *bp) 166 { 167 int s, i; 168 struct md_s *sc; 169 devstat_trans_flags dop; 170 u_char *secp, **secpp, *dst; 171 unsigned secno, nsec, secval, uc; 172 173 if (md_debug > 1) 174 printf("mdstrategy_malloc(%p) %s %lx, %d, %ld, %p)\n", 175 bp, devtoname(bp->b_dev), bp->b_flags, bp->b_blkno, 176 bp->b_bcount / DEV_BSIZE, bp->b_data); 177 178 sc = bp->b_dev->si_drv1; 179 180 s = splbio(); 181 182 bufqdisksort(&sc->buf_queue, bp); 183 184 if (sc->busy) { 185 splx(s); 186 return; 187 } 188 189 sc->busy++; 190 191 while (1) { 192 bp = bufq_first(&sc->buf_queue); 193 if (bp) 194 bufq_remove(&sc->buf_queue, bp); 195 splx(s); 196 if (!bp) 197 break; 198 199 devstat_start_transaction(&sc->stats); 200 201 if (bp->b_flags & B_FREEBUF) 202 dop = DEVSTAT_NO_DATA; 203 else if (bp->b_flags & B_READ) 204 dop = DEVSTAT_READ; 205 else 206 dop = DEVSTAT_WRITE; 207 208 nsec = bp->b_bcount / DEV_BSIZE; 209 secno = bp->b_pblkno; 210 dst = bp->b_data; 211 while (nsec--) { 212 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 printf("%lx %p %p %d\n", bp->b_flags, secpp, secp, secval); 229 230 if (bp->b_flags & B_FREEBUF) { 231 if (secpp) { 232 if (secp) 233 FREE(secp, M_MDSECT); 234 *secpp = 0; 235 } 236 } else if (bp->b_flags & B_READ) { 237 if (secp) { 238 bcopy(secp, dst, DEV_BSIZE); 239 } else if (secval) { 240 for (i = 0; i < DEV_BSIZE; i++) 241 dst[i] = secval; 242 } else { 243 bzero(dst, DEV_BSIZE); 244 } 245 } else { 246 uc = dst[0]; 247 for (i = 1; i < DEV_BSIZE; i++) 248 if (dst[i] != uc) 249 break; 250 if (i == DEV_BSIZE && !uc) { 251 if (secp) 252 FREE(secp, M_MDSECT); 253 if (secpp) 254 *secpp = (u_char *)uc; 255 } else { 256 if (!secpp) { 257 MALLOC(secpp, u_char **, (secno + nsec + 1) * sizeof(u_char *), M_MD, M_WAITOK); 258 bzero(secpp, (secno + nsec + 1) * sizeof(u_char *)); 259 bcopy(sc->secp, secpp, sc->nsecp * sizeof(u_char *)); 260 FREE(sc->secp, M_MD); 261 sc->secp = secpp; 262 sc->nsecp = secno + nsec + 1; 263 secpp = &sc->secp[secno]; 264 } 265 if (i == DEV_BSIZE) { 266 if (secp) 267 FREE(secp, M_MDSECT); 268 *secpp = (u_char *)uc; 269 } else { 270 if (!secp) 271 MALLOC(secp, u_char *, DEV_BSIZE, M_MDSECT, M_WAITOK); 272 bcopy(dst, secp, DEV_BSIZE); 273 274 *secpp = secp; 275 } 276 } 277 } 278 secno++; 279 dst += DEV_BSIZE; 280 } 281 bp->b_resid = 0; 282 devstat_end_transaction_buf(&sc->stats, bp); 283 biodone(bp); 284 s = splbio(); 285 } 286 sc->busy = 0; 287 return; 288 } 289 290 291 static void 292 mdstrategy_preload(struct buf *bp) 293 { 294 int s; 295 struct md_s *sc; 296 devstat_trans_flags dop; 297 298 if (md_debug > 1) 299 printf("mdstrategy_preload(%p) %s %lx, %d, %ld, %p)\n", 300 bp, devtoname(bp->b_dev), bp->b_flags, bp->b_blkno, 301 bp->b_bcount / DEV_BSIZE, bp->b_data); 302 303 sc = bp->b_dev->si_drv1; 304 305 s = splbio(); 306 307 bufqdisksort(&sc->buf_queue, bp); 308 309 if (sc->busy) { 310 splx(s); 311 return; 312 } 313 314 sc->busy++; 315 316 while (1) { 317 bp = bufq_first(&sc->buf_queue); 318 if (bp) 319 bufq_remove(&sc->buf_queue, bp); 320 splx(s); 321 if (!bp) 322 break; 323 324 devstat_start_transaction(&sc->stats); 325 326 if (bp->b_flags & B_FREEBUF) { 327 dop = DEVSTAT_NO_DATA; 328 } else if (bp->b_flags & B_READ) { 329 dop = DEVSTAT_READ; 330 bcopy(sc->pl_ptr + (bp->b_pblkno << DEV_BSHIFT), bp->b_data, bp->b_bcount); 331 } else { 332 dop = DEVSTAT_WRITE; 333 bcopy(bp->b_data, sc->pl_ptr + (bp->b_pblkno << DEV_BSHIFT), bp->b_bcount); 334 } 335 bp->b_resid = 0; 336 devstat_end_transaction_buf(&sc->stats, bp); 337 biodone(bp); 338 s = splbio(); 339 } 340 sc->busy = 0; 341 return; 342 } 343 344 static struct md_s * 345 mdcreate(void) 346 { 347 struct md_s *sc; 348 349 MALLOC(sc, struct md_s *,sizeof(*sc), M_MD, M_WAITOK); 350 bzero(sc, sizeof(*sc)); 351 sc->unit = mdunits++; 352 bufq_init(&sc->buf_queue); 353 devstat_add_entry(&sc->stats, "md", sc->unit, DEV_BSIZE, 354 DEVSTAT_NO_ORDERED_TAGS, 355 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER, 356 DEVSTAT_PRIORITY_OTHER); 357 sc->dev = disk_create(sc->unit, &sc->disk, 0, &md_cdevsw); 358 sc->dev->si_drv1 = sc; 359 return (sc); 360 } 361 362 static void 363 mdcreate_preload(u_char *image, unsigned length) 364 { 365 struct md_s *sc; 366 367 sc = mdcreate(); 368 sc->type = MD_PRELOAD; 369 sc->nsect = length / DEV_BSIZE; 370 sc->pl_ptr = image; 371 sc->pl_len = length; 372 373 if (sc->unit == 0) 374 mdrootready = 1; 375 } 376 377 static void 378 mdcreate_malloc(void) 379 { 380 struct md_s *sc; 381 382 sc = mdcreate(); 383 sc->type = MD_MALLOC; 384 385 sc->nsect = MD_NSECT; /* for now */ 386 MALLOC(sc->secp, u_char **, sizeof(u_char *), M_MD, M_WAITOK); 387 bzero(sc->secp, sizeof(u_char *)); 388 sc->nsecp = 1; 389 printf("md%d: Malloc disk\n", sc->unit); 390 } 391 392 static void 393 md_drvinit(void *unused) 394 { 395 396 caddr_t mod; 397 caddr_t c; 398 u_char *ptr, *name, *type; 399 unsigned len; 400 401 #ifdef MD_ROOT_SIZE 402 mdcreate_preload(mfs_root, MD_ROOT_SIZE*1024); 403 #endif 404 mod = NULL; 405 while ((mod = preload_search_next_name(mod)) != NULL) { 406 name = (char *)preload_search_info(mod, MODINFO_NAME); 407 type = (char *)preload_search_info(mod, MODINFO_TYPE); 408 if (name == NULL) 409 continue; 410 if (type == NULL) 411 continue; 412 if (strcmp(type, "md_image") && strcmp(type, "mfs_root")) 413 continue; 414 c = preload_search_info(mod, MODINFO_ADDR); 415 ptr = *(u_char **)c; 416 c = preload_search_info(mod, MODINFO_SIZE); 417 len = *(unsigned *)c; 418 printf("md%d: Preloaded image <%s> %d bytes at %p\n", 419 mdunits, name, len, ptr); 420 mdcreate_preload(ptr, len); 421 } 422 mdcreate_malloc(); 423 } 424 425 SYSINIT(mddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR, md_drvinit,NULL) 426 427 #ifdef MD_ROOT 428 static void 429 md_takeroot(void *junk) 430 { 431 if (mdrootready) 432 rootdevnames[0] = "ufs:/dev/md0c"; 433 } 434 435 SYSINIT(md_root, SI_SUB_MOUNT_ROOT, SI_ORDER_FIRST, md_takeroot, NULL); 436 #endif 437