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 /* 14 * The following functions are based in the vn(4) driver: mdstart_swap(), 15 * mdstart_vnode(), mdcreate_swap(), mdcreate_vnode() and mddestroy(), 16 * and as such under the following copyright: 17 * 18 * Copyright (c) 1988 University of Utah. 19 * Copyright (c) 1990, 1993 20 * The Regents of the University of California. All rights reserved. 21 * 22 * This code is derived from software contributed to Berkeley by 23 * the Systems Programming Group of the University of Utah Computer 24 * Science Department. 25 * 26 * Redistribution and use in source and binary forms, with or without 27 * modification, are permitted provided that the following conditions 28 * are met: 29 * 1. Redistributions of source code must retain the above copyright 30 * notice, this list of conditions and the following disclaimer. 31 * 2. Redistributions in binary form must reproduce the above copyright 32 * notice, this list of conditions and the following disclaimer in the 33 * documentation and/or other materials provided with the distribution. 34 * 3. All advertising materials mentioning features or use of this software 35 * must display the following acknowledgement: 36 * This product includes software developed by the University of 37 * California, Berkeley and its contributors. 38 * 4. Neither the name of the University nor the names of its contributors 39 * may be used to endorse or promote products derived from this software 40 * without specific prior written permission. 41 * 42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * 54 * from: Utah Hdr: vn.c 1.13 94/04/02 55 * 56 * from: @(#)vn.c 8.6 (Berkeley) 4/1/94 57 * From: src/sys/dev/vn/vn.c,v 1.122 2000/12/16 16:06:03 58 */ 59 60 #include "opt_md.h" 61 62 #include <sys/param.h> 63 #include <sys/systm.h> 64 #include <sys/bio.h> 65 #include <sys/conf.h> 66 #include <sys/devicestat.h> 67 #include <sys/disk.h> 68 #include <sys/fcntl.h> 69 #include <sys/kernel.h> 70 #include <sys/linker.h> 71 #include <sys/lock.h> 72 #include <sys/malloc.h> 73 #include <sys/mdioctl.h> 74 #include <sys/mutex.h> 75 #include <sys/namei.h> 76 #include <sys/proc.h> 77 #include <sys/queue.h> 78 #include <sys/sysctl.h> 79 #include <sys/vnode.h> 80 81 #include <machine/atomic.h> 82 83 #include <vm/vm.h> 84 #include <vm/vm_object.h> 85 #include <vm/vm_page.h> 86 #include <vm/vm_pager.h> 87 #include <vm/vm_zone.h> 88 #include <vm/swap_pager.h> 89 90 #define MD_MODVER 1 91 92 #ifndef MD_NSECT 93 #define MD_NSECT (10000 * 2) 94 #endif 95 96 MALLOC_DEFINE(M_MD, "MD disk", "Memory Disk"); 97 MALLOC_DEFINE(M_MDSECT, "MD sectors", "Memory Disk Sectors"); 98 99 static int md_debug; 100 SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0, ""); 101 102 #if defined(MD_ROOT) && defined(MD_ROOT_SIZE) 103 /* Image gets put here: */ 104 static u_char mfs_root[MD_ROOT_SIZE*1024] = "MFS Filesystem goes here"; 105 static u_char end_mfs_root[] __unused = "MFS Filesystem had better STOP here"; 106 #endif 107 108 static int mdrootready; 109 static int mdunits; 110 static dev_t status_dev = 0; 111 112 113 #define CDEV_MAJOR 95 114 115 static d_strategy_t mdstrategy; 116 static d_open_t mdopen; 117 static d_close_t mdclose; 118 static d_ioctl_t mdioctl, mdctlioctl; 119 120 static struct cdevsw md_cdevsw = { 121 /* open */ mdopen, 122 /* close */ mdclose, 123 /* read */ physread, 124 /* write */ physwrite, 125 /* ioctl */ mdioctl, 126 /* poll */ nopoll, 127 /* mmap */ nommap, 128 /* strategy */ mdstrategy, 129 /* name */ MD_NAME, 130 /* maj */ CDEV_MAJOR, 131 /* dump */ nodump, 132 /* psize */ nopsize, 133 /* flags */ D_DISK | D_CANFREE | D_MEMDISK, 134 }; 135 136 static struct cdevsw mdctl_cdevsw = { 137 /* open */ nullopen, 138 /* close */ nullclose, 139 /* read */ noread, 140 /* write */ nowrite, 141 /* ioctl */ mdctlioctl, 142 /* poll */ nopoll, 143 /* mmap */ nommap, 144 /* strategy */ nostrategy, 145 /* name */ MD_NAME, 146 /* maj */ CDEV_MAJOR 147 }; 148 149 static struct cdevsw mddisk_cdevsw; 150 151 static LIST_HEAD(, md_s) md_softc_list = LIST_HEAD_INITIALIZER(&md_softc_list); 152 153 struct md_s { 154 int unit; 155 LIST_ENTRY(md_s) list; 156 struct devstat stats; 157 struct bio_queue_head bio_queue; 158 struct disk disk; 159 dev_t dev; 160 int busy; 161 enum md_types type; 162 unsigned nsect; 163 unsigned opencount; 164 unsigned secsize; 165 unsigned flags; 166 167 /* MD_MALLOC related fields */ 168 u_char **secp; 169 170 /* MD_PRELOAD related fields */ 171 u_char *pl_ptr; 172 unsigned pl_len; 173 174 /* MD_VNODE related fields */ 175 struct vnode *vnode; 176 struct ucred *cred; 177 178 /* MD_OBJET related fields */ 179 vm_object_t object; 180 }; 181 182 static int 183 mdopen(dev_t dev, int flag, int fmt, struct proc *p) 184 { 185 struct md_s *sc; 186 struct disklabel *dl; 187 188 if (md_debug) 189 printf("mdopen(%s %x %x %p)\n", 190 devtoname(dev), flag, fmt, p); 191 192 sc = dev->si_drv1; 193 194 dl = &sc->disk.d_label; 195 bzero(dl, sizeof(*dl)); 196 dl->d_secsize = sc->secsize; 197 dl->d_nsectors = sc->nsect > 63 ? 63 : sc->nsect; 198 dl->d_ntracks = 1; 199 dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks; 200 dl->d_secperunit = sc->nsect; 201 dl->d_ncylinders = dl->d_secperunit / dl->d_secpercyl; 202 sc->opencount++; 203 return (0); 204 } 205 206 static int 207 mdclose(dev_t dev, int flags, int fmt, struct proc *p) 208 { 209 struct md_s *sc = dev->si_drv1; 210 211 sc->opencount--; 212 return (0); 213 } 214 215 static int 216 mdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct proc *p) 217 { 218 219 if (md_debug) 220 printf("mdioctl(%s %lx %p %x %p)\n", 221 devtoname(dev), cmd, addr, flags, p); 222 223 return (ENOIOCTL); 224 } 225 226 static void 227 mdstart_malloc(struct md_s *sc) 228 { 229 int i; 230 struct bio *bp; 231 devstat_trans_flags dop; 232 u_char *secp, **secpp, *dst; 233 unsigned secno, nsec, secval, uc; 234 235 for (;;) { 236 /* XXX: LOCK(unique unit numbers) */ 237 bp = bioq_first(&sc->bio_queue); 238 if (bp) 239 bioq_remove(&sc->bio_queue, bp); 240 /* XXX: UNLOCK(unique unit numbers) */ 241 if (!bp) 242 break; 243 244 devstat_start_transaction(&sc->stats); 245 246 if (bp->bio_cmd == BIO_DELETE) 247 dop = DEVSTAT_NO_DATA; 248 else if (bp->bio_cmd == BIO_READ) 249 dop = DEVSTAT_READ; 250 else 251 dop = DEVSTAT_WRITE; 252 253 nsec = bp->bio_bcount / sc->secsize; 254 secno = bp->bio_pblkno; 255 dst = bp->bio_data; 256 while (nsec--) { 257 secpp = &sc->secp[secno]; 258 if ((uintptr_t)*secpp > 255) { 259 secp = *secpp; 260 secval = 0; 261 } else { 262 secp = NULL; 263 secval = (uintptr_t) *secpp; 264 } 265 266 if (md_debug > 2) 267 printf("%x %p %p %d\n", 268 bp->bio_flags, secpp, secp, secval); 269 270 if (bp->bio_cmd == BIO_DELETE) { 271 if (!(sc->flags & MD_RESERVE) && secp != NULL) { 272 FREE(secp, M_MDSECT); 273 *secpp = 0; 274 } 275 } else if (bp->bio_cmd == BIO_READ) { 276 if (secp != NULL) { 277 bcopy(secp, dst, sc->secsize); 278 } else if (secval) { 279 for (i = 0; i < sc->secsize; i++) 280 dst[i] = secval; 281 } else { 282 bzero(dst, sc->secsize); 283 } 284 } else { 285 if (sc->flags & MD_COMPRESS) { 286 uc = dst[0]; 287 for (i = 1; i < sc->secsize; i++) 288 if (dst[i] != uc) 289 break; 290 } else { 291 i = 0; 292 uc = 0; 293 } 294 if (i == sc->secsize) { 295 if (secp) 296 FREE(secp, M_MDSECT); 297 *secpp = (u_char *)(uintptr_t)uc; 298 } else { 299 if (secp == NULL) 300 MALLOC(secp, u_char *, sc->secsize, M_MDSECT, M_WAITOK); 301 bcopy(dst, secp, sc->secsize); 302 *secpp = secp; 303 } 304 } 305 secno++; 306 dst += sc->secsize; 307 } 308 bp->bio_resid = 0; 309 biofinish(bp, &sc->stats, 0); 310 } 311 return; 312 } 313 314 315 static void 316 mdstart_preload(struct md_s *sc) 317 { 318 struct bio *bp; 319 devstat_trans_flags dop; 320 321 for (;;) { 322 /* XXX: LOCK(unique unit numbers) */ 323 bp = bioq_first(&sc->bio_queue); 324 if (bp) 325 bioq_remove(&sc->bio_queue, bp); 326 /* XXX: UNLOCK(unique unit numbers) */ 327 if (!bp) 328 break; 329 330 devstat_start_transaction(&sc->stats); 331 332 if (bp->bio_cmd == BIO_DELETE) { 333 dop = DEVSTAT_NO_DATA; 334 } else if (bp->bio_cmd == BIO_READ) { 335 dop = DEVSTAT_READ; 336 bcopy(sc->pl_ptr + (bp->bio_pblkno << DEV_BSHIFT), bp->bio_data, bp->bio_bcount); 337 } else { 338 dop = DEVSTAT_WRITE; 339 bcopy(bp->bio_data, sc->pl_ptr + (bp->bio_pblkno << DEV_BSHIFT), bp->bio_bcount); 340 } 341 bp->bio_resid = 0; 342 biofinish(bp, &sc->stats, 0); 343 } 344 return; 345 } 346 347 static void 348 mdstart_vnode(struct md_s *sc) 349 { 350 int error; 351 struct bio *bp; 352 struct uio auio; 353 struct iovec aiov; 354 struct mount *mp; 355 356 /* 357 * VNODE I/O 358 * 359 * If an error occurs, we set BIO_ERROR but we do not set 360 * B_INVAL because (for a write anyway), the buffer is 361 * still valid. 362 */ 363 364 for (;;) { 365 /* XXX: LOCK(unique unit numbers) */ 366 bp = bioq_first(&sc->bio_queue); 367 if (bp) 368 bioq_remove(&sc->bio_queue, bp); 369 /* XXX: UNLOCK(unique unit numbers) */ 370 if (!bp) 371 break; 372 373 devstat_start_transaction(&sc->stats); 374 375 bzero(&auio, sizeof(auio)); 376 377 aiov.iov_base = bp->bio_data; 378 aiov.iov_len = bp->bio_bcount; 379 auio.uio_iov = &aiov; 380 auio.uio_iovcnt = 1; 381 auio.uio_offset = (vm_ooffset_t)bp->bio_pblkno * sc->secsize; 382 auio.uio_segflg = UIO_SYSSPACE; 383 if(bp->bio_cmd == BIO_READ) 384 auio.uio_rw = UIO_READ; 385 else 386 auio.uio_rw = UIO_WRITE; 387 auio.uio_resid = bp->bio_bcount; 388 auio.uio_procp = curproc; 389 if (VOP_ISLOCKED(sc->vnode, NULL)) 390 vprint("unexpected md driver lock", sc->vnode); 391 if (bp->bio_cmd == BIO_READ) { 392 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY, curproc); 393 error = VOP_READ(sc->vnode, &auio, 0, sc->cred); 394 } else { 395 (void) vn_start_write(sc->vnode, &mp, V_WAIT); 396 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY, curproc); 397 error = VOP_WRITE(sc->vnode, &auio, 0, sc->cred); 398 vn_finished_write(mp); 399 } 400 VOP_UNLOCK(sc->vnode, 0, curproc); 401 bp->bio_resid = auio.uio_resid; 402 biofinish(bp, &sc->stats, error); 403 } 404 return; 405 } 406 407 static void 408 mdstart_swap(struct md_s *sc) 409 { 410 struct bio *bp; 411 412 for (;;) { 413 /* XXX: LOCK(unique unit numbers) */ 414 bp = bioq_first(&sc->bio_queue); 415 if (bp) 416 bioq_remove(&sc->bio_queue, bp); 417 /* XXX: UNLOCK(unique unit numbers) */ 418 if (!bp) 419 break; 420 421 #if 0 422 devstat_start_transaction(&sc->stats); 423 #endif 424 425 if ((bp->bio_cmd == BIO_DELETE) && (sc->flags & MD_RESERVE)) 426 biodone(bp); 427 else 428 vm_pager_strategy(sc->object, bp); 429 430 #if 0 431 devstat_end_transaction_bio(&sc->stats, bp); 432 #endif 433 } 434 return; 435 } 436 437 static void 438 mdstrategy(struct bio *bp) 439 { 440 struct md_s *sc; 441 442 if (md_debug > 1) 443 printf("mdstrategy(%p) %s %x, %d, %ld, %p)\n", 444 bp, devtoname(bp->bio_dev), bp->bio_flags, bp->bio_blkno, 445 bp->bio_bcount / DEV_BSIZE, bp->bio_data); 446 447 sc = bp->bio_dev->si_drv1; 448 449 /* XXX: LOCK(sc->lock) */ 450 bioqdisksort(&sc->bio_queue, bp); 451 /* XXX: UNLOCK(sc->lock) */ 452 453 if (atomic_cmpset_int(&sc->busy, 0, 1) == 0) 454 return; 455 456 switch (sc->type) { 457 case MD_MALLOC: 458 mdstart_malloc(sc); 459 break; 460 case MD_PRELOAD: 461 mdstart_preload(sc); 462 break; 463 case MD_VNODE: 464 mdstart_vnode(sc); 465 break; 466 case MD_SWAP: 467 mdstart_swap(sc); 468 break; 469 default: 470 panic("Impossible md(type)"); 471 break; 472 } 473 sc->busy = 0; 474 } 475 476 static struct md_s * 477 mdfind(int unit) 478 { 479 struct md_s *sc; 480 481 /* XXX: LOCK(unique unit numbers) */ 482 LIST_FOREACH(sc, &md_softc_list, list) { 483 if (sc->unit == unit) 484 break; 485 } 486 /* XXX: UNLOCK(unique unit numbers) */ 487 return (sc); 488 } 489 490 static struct md_s * 491 mdnew(int unit) 492 { 493 struct md_s *sc; 494 int max = -1; 495 496 /* XXX: LOCK(unique unit numbers) */ 497 LIST_FOREACH(sc, &md_softc_list, list) { 498 if (sc->unit == unit) { 499 /* XXX: UNLOCK(unique unit numbers) */ 500 return (NULL); 501 } 502 if (sc->unit > max) 503 max = sc->unit; 504 } 505 if (unit == -1) 506 unit = max + 1; 507 if (unit > DKMAXUNIT) 508 return (NULL); 509 MALLOC(sc, struct md_s *,sizeof(*sc), M_MD, M_WAITOK | M_ZERO); 510 sc->unit = unit; 511 LIST_INSERT_HEAD(&md_softc_list, sc, list); 512 /* XXX: UNLOCK(unique unit numbers) */ 513 return (sc); 514 } 515 516 static void 517 mdinit(struct md_s *sc) 518 { 519 520 bioq_init(&sc->bio_queue); 521 devstat_add_entry(&sc->stats, MD_NAME, sc->unit, sc->secsize, 522 DEVSTAT_NO_ORDERED_TAGS, 523 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER, 524 DEVSTAT_PRIORITY_OTHER); 525 sc->dev = disk_create(sc->unit, &sc->disk, 0, &md_cdevsw, &mddisk_cdevsw); 526 sc->dev->si_drv1 = sc; 527 } 528 529 /* 530 * XXX: we should check that the range they feed us is mapped. 531 * XXX: we should implement read-only. 532 */ 533 534 static int 535 mdcreate_preload(struct md_ioctl *mdio) 536 { 537 struct md_s *sc; 538 539 if (mdio->md_size == 0) 540 return(EINVAL); 541 if (mdio->md_options & ~(MD_AUTOUNIT)) 542 return(EINVAL); 543 if (mdio->md_options & MD_AUTOUNIT) { 544 sc = mdnew(-1); 545 if (sc == NULL) 546 return (ENOMEM); 547 mdio->md_unit = sc->unit; 548 } else { 549 sc = mdnew(mdio->md_unit); 550 if (sc == NULL) 551 return (EBUSY); 552 } 553 sc->type = MD_PRELOAD; 554 sc->secsize = DEV_BSIZE; 555 sc->nsect = mdio->md_size; 556 /* Cast to pointer size, then to pointer to avoid warning */ 557 sc->pl_ptr = (u_char *)(uintptr_t)mdio->md_base; 558 sc->pl_len = (mdio->md_size << DEV_BSHIFT); 559 mdinit(sc); 560 return (0); 561 } 562 563 564 static int 565 mdcreate_malloc(struct md_ioctl *mdio) 566 { 567 struct md_s *sc; 568 unsigned u; 569 570 if (mdio->md_size == 0) 571 return(EINVAL); 572 if (mdio->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE)) 573 return(EINVAL); 574 /* Compression doesn't make sense if we have reserved space */ 575 if (mdio->md_options & MD_RESERVE) 576 mdio->md_options &= ~MD_COMPRESS; 577 if (mdio->md_options & MD_AUTOUNIT) { 578 sc = mdnew(-1); 579 if (sc == NULL) 580 return (ENOMEM); 581 mdio->md_unit = sc->unit; 582 } else { 583 sc = mdnew(mdio->md_unit); 584 if (sc == NULL) 585 return (EBUSY); 586 } 587 sc->type = MD_MALLOC; 588 sc->secsize = DEV_BSIZE; 589 sc->nsect = mdio->md_size; 590 sc->flags = mdio->md_options & MD_COMPRESS; 591 MALLOC(sc->secp, u_char **, sc->nsect * sizeof(u_char *), M_MD, M_WAITOK | M_ZERO); 592 if (mdio->md_options & MD_RESERVE) { 593 for (u = 0; u < sc->nsect; u++) 594 MALLOC(sc->secp[u], u_char *, DEV_BSIZE, M_MDSECT, M_WAITOK | M_ZERO); 595 } 596 printf("%s%d: Malloc disk\n", MD_NAME, sc->unit); 597 mdinit(sc); 598 return (0); 599 } 600 601 602 static int 603 mdsetcred(struct md_s *sc, struct ucred *cred) 604 { 605 char *tmpbuf; 606 int error = 0; 607 608 /* 609 * Set credits in our softc 610 */ 611 612 if (sc->cred) 613 crfree(sc->cred); 614 sc->cred = crdup(cred); 615 616 /* 617 * Horrible kludge to establish credentials for NFS XXX. 618 */ 619 620 if (sc->vnode) { 621 struct uio auio; 622 struct iovec aiov; 623 624 tmpbuf = malloc(sc->secsize, M_TEMP, M_WAITOK); 625 bzero(&auio, sizeof(auio)); 626 627 aiov.iov_base = tmpbuf; 628 aiov.iov_len = sc->secsize; 629 auio.uio_iov = &aiov; 630 auio.uio_iovcnt = 1; 631 auio.uio_offset = 0; 632 auio.uio_rw = UIO_READ; 633 auio.uio_segflg = UIO_SYSSPACE; 634 auio.uio_resid = aiov.iov_len; 635 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY, curproc); 636 error = VOP_READ(sc->vnode, &auio, 0, sc->cred); 637 VOP_UNLOCK(sc->vnode, 0, curproc); 638 free(tmpbuf, M_TEMP); 639 } 640 return (error); 641 } 642 643 static int 644 mdcreate_vnode(struct md_ioctl *mdio, struct proc *p) 645 { 646 struct md_s *sc; 647 struct vattr vattr; 648 struct nameidata nd; 649 int error, flags; 650 651 if (mdio->md_options & MD_AUTOUNIT) { 652 sc = mdnew(-1); 653 mdio->md_unit = sc->unit; 654 } else { 655 sc = mdnew(mdio->md_unit); 656 } 657 if (sc == NULL) 658 return (EBUSY); 659 660 sc->type = MD_VNODE; 661 662 flags = FREAD|FWRITE; 663 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, mdio->md_file, p); 664 error = vn_open(&nd, &flags, 0); 665 if (error) { 666 if (error != EACCES && error != EPERM && error != EROFS) 667 return (error); 668 flags &= ~FWRITE; 669 sc->flags |= MD_READONLY; 670 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, mdio->md_file, p); 671 error = vn_open(&nd, &flags, 0); 672 if (error) 673 return (error); 674 } 675 NDFREE(&nd, NDF_ONLY_PNBUF); 676 if (nd.ni_vp->v_type != VREG || 677 (error = VOP_GETATTR(nd.ni_vp, &vattr, p->p_ucred, p))) { 678 VOP_UNLOCK(nd.ni_vp, 0, p); 679 (void) vn_close(nd.ni_vp, flags, p->p_ucred, p); 680 return (error ? error : EINVAL); 681 } 682 VOP_UNLOCK(nd.ni_vp, 0, p); 683 sc->secsize = DEV_BSIZE; 684 sc->vnode = nd.ni_vp; 685 686 /* 687 * If the size is specified, override the file attributes. 688 */ 689 if (mdio->md_size) 690 sc->nsect = mdio->md_size; 691 else 692 sc->nsect = vattr.va_size / sc->secsize; /* XXX: round up ? */ 693 if (sc->nsect == 0) { 694 (void) vn_close(nd.ni_vp, flags, p->p_ucred, p); 695 return (EINVAL); 696 } 697 error = mdsetcred(sc, p->p_ucred); 698 if (error) { 699 (void) vn_close(nd.ni_vp, flags, p->p_ucred, p); 700 return(error); 701 } 702 mdinit(sc); 703 return (0); 704 } 705 706 static int 707 mddestroy(struct md_s *sc, struct md_ioctl *mdio, struct proc *p) 708 { 709 unsigned u; 710 711 GIANT_REQUIRED; 712 713 if (sc->dev != NULL) { 714 devstat_remove_entry(&sc->stats); 715 disk_destroy(sc->dev); 716 } 717 if (sc->vnode != NULL) 718 (void)vn_close(sc->vnode, sc->flags & MD_READONLY ? FREAD : (FREAD|FWRITE), sc->cred, p); 719 if (sc->cred != NULL) 720 crfree(sc->cred); 721 if (sc->object != NULL) { 722 vm_pager_deallocate(sc->object); 723 } 724 if (sc->secp != NULL) { 725 for (u = 0; u < sc->nsect; u++) 726 if ((uintptr_t)sc->secp[u] > 255) 727 FREE(sc->secp[u], M_MDSECT); 728 FREE(sc->secp, M_MD); 729 } 730 731 /* XXX: LOCK(unique unit numbers) */ 732 LIST_REMOVE(sc, list); 733 /* XXX: UNLOCK(unique unit numbers) */ 734 FREE(sc, M_MD); 735 return (0); 736 } 737 738 static int 739 mdcreate_swap(struct md_ioctl *mdio, struct proc *p) 740 { 741 int error; 742 struct md_s *sc; 743 744 GIANT_REQUIRED; 745 746 if (mdio->md_options & MD_AUTOUNIT) { 747 sc = mdnew(-1); 748 mdio->md_unit = sc->unit; 749 } else { 750 sc = mdnew(mdio->md_unit); 751 } 752 if (sc == NULL) 753 return (EBUSY); 754 755 sc->type = MD_SWAP; 756 757 /* 758 * Range check. Disallow negative sizes or any size less then the 759 * size of a page. Then round to a page. 760 */ 761 762 if (mdio->md_size == 0) { 763 mddestroy(sc, mdio, p); 764 return(EDOM); 765 } 766 767 /* 768 * Allocate an OBJT_SWAP object. 769 * 770 * sc_secsize is PAGE_SIZE'd 771 * 772 * mdio->size is in DEV_BSIZE'd chunks. 773 * Note the truncation. 774 */ 775 776 sc->secsize = PAGE_SIZE; 777 sc->nsect = mdio->md_size / (PAGE_SIZE / DEV_BSIZE); 778 sc->object = vm_pager_allocate(OBJT_SWAP, NULL, sc->secsize * (vm_offset_t)sc->nsect, VM_PROT_DEFAULT, 0); 779 if (mdio->md_options & MD_RESERVE) { 780 if (swap_pager_reserve(sc->object, 0, sc->nsect) < 0) { 781 vm_pager_deallocate(sc->object); 782 sc->object = NULL; 783 mddestroy(sc, mdio, p); 784 return(EDOM); 785 } 786 } 787 error = mdsetcred(sc, p->p_ucred); 788 if (error) 789 mddestroy(sc, mdio, p); 790 else 791 mdinit(sc); 792 return(error); 793 } 794 795 static int 796 mdctlioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct proc *p) 797 { 798 struct md_ioctl *mdio; 799 struct md_s *sc; 800 801 if (md_debug) 802 printf("mdctlioctl(%s %lx %p %x %p)\n", 803 devtoname(dev), cmd, addr, flags, p); 804 805 mdio = (struct md_ioctl *)addr; 806 switch (cmd) { 807 case MDIOCATTACH: 808 switch (mdio->md_type) { 809 case MD_MALLOC: 810 return(mdcreate_malloc(mdio)); 811 case MD_PRELOAD: 812 return(mdcreate_preload(mdio)); 813 case MD_VNODE: 814 return(mdcreate_vnode(mdio, p)); 815 case MD_SWAP: 816 return(mdcreate_swap(mdio, p)); 817 default: 818 return (EINVAL); 819 } 820 case MDIOCDETACH: 821 if (mdio->md_file != NULL) 822 return(EINVAL); 823 if (mdio->md_size != 0) 824 return(EINVAL); 825 if (mdio->md_options != 0) 826 return(EINVAL); 827 sc = mdfind(mdio->md_unit); 828 if (sc == NULL) 829 return (ENOENT); 830 if (sc->opencount != 0) 831 return (EBUSY); 832 switch(sc->type) { 833 case MD_VNODE: 834 case MD_SWAP: 835 case MD_MALLOC: 836 case MD_PRELOAD: 837 return(mddestroy(sc, mdio, p)); 838 default: 839 return (EOPNOTSUPP); 840 } 841 case MDIOCQUERY: 842 sc = mdfind(mdio->md_unit); 843 if (sc == NULL) 844 return (ENOENT); 845 mdio->md_type = sc->type; 846 mdio->md_options = sc->flags; 847 switch (sc->type) { 848 case MD_MALLOC: 849 mdio->md_size = sc->nsect; 850 break; 851 case MD_PRELOAD: 852 mdio->md_size = sc->nsect; 853 (u_char *)(uintptr_t)mdio->md_base = sc->pl_ptr; 854 break; 855 case MD_SWAP: 856 mdio->md_size = sc->nsect * (PAGE_SIZE / DEV_BSIZE); 857 break; 858 case MD_VNODE: 859 mdio->md_size = sc->nsect; 860 /* XXX fill this in */ 861 mdio->md_file = NULL; 862 break; 863 } 864 return (0); 865 default: 866 return (ENOIOCTL); 867 }; 868 return (ENOIOCTL); 869 } 870 871 static void 872 md_preloaded(u_char *image, unsigned length) 873 { 874 struct md_s *sc; 875 876 sc = mdnew(-1); 877 if (sc == NULL) 878 return; 879 sc->type = MD_PRELOAD; 880 sc->secsize = DEV_BSIZE; 881 sc->nsect = length / DEV_BSIZE; 882 sc->pl_ptr = image; 883 sc->pl_len = length; 884 if (sc->unit == 0) 885 mdrootready = 1; 886 mdinit(sc); 887 } 888 889 static void 890 md_drvinit(void *unused) 891 { 892 893 caddr_t mod; 894 caddr_t c; 895 u_char *ptr, *name, *type; 896 unsigned len; 897 898 #ifdef MD_ROOT_SIZE 899 md_preloaded(mfs_root, MD_ROOT_SIZE*1024); 900 #endif 901 mod = NULL; 902 while ((mod = preload_search_next_name(mod)) != NULL) { 903 name = (char *)preload_search_info(mod, MODINFO_NAME); 904 type = (char *)preload_search_info(mod, MODINFO_TYPE); 905 if (name == NULL) 906 continue; 907 if (type == NULL) 908 continue; 909 if (strcmp(type, "md_image") && strcmp(type, "mfs_root")) 910 continue; 911 c = preload_search_info(mod, MODINFO_ADDR); 912 ptr = *(u_char **)c; 913 c = preload_search_info(mod, MODINFO_SIZE); 914 len = *(unsigned *)c; 915 printf("%s%d: Preloaded image <%s> %d bytes at %p\n", 916 MD_NAME, mdunits, name, len, ptr); 917 md_preloaded(ptr, len); 918 } 919 status_dev = make_dev(&mdctl_cdevsw, 0xffff00ff, UID_ROOT, GID_WHEEL, 920 0600, MDCTL_NAME); 921 } 922 923 static int 924 md_modevent(module_t mod, int type, void *data) 925 { 926 switch (type) { 927 case MOD_LOAD: 928 md_drvinit(NULL); 929 break; 930 case MOD_UNLOAD: 931 if (!LIST_EMPTY(&md_softc_list)) 932 return EBUSY; 933 if (status_dev) 934 destroy_dev(status_dev); 935 status_dev = 0; 936 break; 937 default: 938 break; 939 } 940 return 0; 941 } 942 943 static moduledata_t md_mod = { 944 MD_NAME, 945 md_modevent, 946 NULL 947 }; 948 DECLARE_MODULE(md, md_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE+CDEV_MAJOR); 949 MODULE_VERSION(md, MD_MODVER); 950 951 952 #ifdef MD_ROOT 953 static void 954 md_takeroot(void *junk) 955 { 956 if (mdrootready) 957 rootdevnames[0] = "ufs:/dev/md0c"; 958 } 959 960 SYSINIT(md_root, SI_SUB_MOUNT_ROOT, SI_ORDER_FIRST, md_takeroot, NULL); 961 #endif 962 963