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_SWAP related fields */ 179 vm_object_t object; 180 }; 181 182 static int 183 mdopen(dev_t dev, int flag, int fmt, struct thread *td) 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, td->td_proc); 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 thread *td) 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 thread *td) 217 { 218 219 if (md_debug) 220 printf("mdioctl(%s %lx %p %x %p)\n", 221 devtoname(dev), cmd, addr, flags, td); 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_td = curthread; 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, curthread); 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, curthread); 397 error = VOP_WRITE(sc->vnode, &auio, 0, sc->cred); 398 vn_finished_write(mp); 399 } 400 VOP_UNLOCK(sc->vnode, 0, curthread); 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 sc->flags = mdio->md_options & MD_FORCE; 557 /* Cast to pointer size, then to pointer to avoid warning */ 558 sc->pl_ptr = (u_char *)(uintptr_t)mdio->md_base; 559 sc->pl_len = (mdio->md_size << DEV_BSHIFT); 560 mdinit(sc); 561 return (0); 562 } 563 564 565 static int 566 mdcreate_malloc(struct md_ioctl *mdio) 567 { 568 struct md_s *sc; 569 unsigned u; 570 571 if (mdio->md_size == 0) 572 return (EINVAL); 573 if (mdio->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE)) 574 return (EINVAL); 575 /* Compression doesn't make sense if we have reserved space */ 576 if (mdio->md_options & MD_RESERVE) 577 mdio->md_options &= ~MD_COMPRESS; 578 if (mdio->md_options & MD_AUTOUNIT) { 579 sc = mdnew(-1); 580 if (sc == NULL) 581 return (ENOMEM); 582 mdio->md_unit = sc->unit; 583 } else { 584 sc = mdnew(mdio->md_unit); 585 if (sc == NULL) 586 return (EBUSY); 587 } 588 sc->type = MD_MALLOC; 589 sc->secsize = DEV_BSIZE; 590 sc->nsect = mdio->md_size; 591 sc->flags = mdio->md_options & (MD_COMPRESS | MD_FORCE); 592 MALLOC(sc->secp, u_char **, sc->nsect * sizeof(u_char *), M_MD, M_WAITOK | M_ZERO); 593 if (mdio->md_options & MD_RESERVE) { 594 for (u = 0; u < sc->nsect; u++) 595 MALLOC(sc->secp[u], u_char *, DEV_BSIZE, M_MDSECT, M_WAITOK | M_ZERO); 596 } 597 printf("%s%d: Malloc disk\n", MD_NAME, sc->unit); 598 mdinit(sc); 599 return (0); 600 } 601 602 603 static int 604 mdsetcred(struct md_s *sc, struct ucred *cred) 605 { 606 char *tmpbuf; 607 int error = 0; 608 609 /* 610 * Set credits in our softc 611 */ 612 613 if (sc->cred) 614 crfree(sc->cred); 615 sc->cred = crhold(cred); 616 617 /* 618 * Horrible kludge to establish credentials for NFS XXX. 619 */ 620 621 if (sc->vnode) { 622 struct uio auio; 623 struct iovec aiov; 624 625 tmpbuf = malloc(sc->secsize, M_TEMP, M_WAITOK); 626 bzero(&auio, sizeof(auio)); 627 628 aiov.iov_base = tmpbuf; 629 aiov.iov_len = sc->secsize; 630 auio.uio_iov = &aiov; 631 auio.uio_iovcnt = 1; 632 auio.uio_offset = 0; 633 auio.uio_rw = UIO_READ; 634 auio.uio_segflg = UIO_SYSSPACE; 635 auio.uio_resid = aiov.iov_len; 636 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY, curthread); 637 error = VOP_READ(sc->vnode, &auio, 0, sc->cred); 638 VOP_UNLOCK(sc->vnode, 0, curthread); 639 free(tmpbuf, M_TEMP); 640 } 641 return (error); 642 } 643 644 static int 645 mdcreate_vnode(struct md_ioctl *mdio, struct thread *td) 646 { 647 struct proc *p = td->td_proc; 648 struct md_s *sc; 649 struct vattr vattr; 650 struct nameidata nd; 651 int error, flags; 652 653 if (mdio->md_options & MD_AUTOUNIT) { 654 sc = mdnew(-1); 655 mdio->md_unit = sc->unit; 656 } else { 657 sc = mdnew(mdio->md_unit); 658 } 659 if (sc == NULL) 660 return (EBUSY); 661 662 sc->type = MD_VNODE; 663 sc->flags = mdio->md_options & MD_FORCE; 664 665 flags = FREAD|FWRITE; 666 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, mdio->md_file, td); 667 error = vn_open(&nd, &flags, 0); 668 if (error) { 669 if (error != EACCES && error != EPERM && error != EROFS) 670 return (error); 671 flags &= ~FWRITE; 672 sc->flags |= MD_READONLY; 673 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, mdio->md_file, td); 674 error = vn_open(&nd, &flags, 0); 675 if (error) 676 return (error); 677 } 678 NDFREE(&nd, NDF_ONLY_PNBUF); 679 if (nd.ni_vp->v_type != VREG || 680 (error = VOP_GETATTR(nd.ni_vp, &vattr, p->p_ucred, td))) { 681 VOP_UNLOCK(nd.ni_vp, 0, td); 682 (void) vn_close(nd.ni_vp, flags, p->p_ucred, td); 683 return (error ? error : EINVAL); 684 } 685 VOP_UNLOCK(nd.ni_vp, 0, td); 686 sc->secsize = DEV_BSIZE; 687 sc->vnode = nd.ni_vp; 688 689 /* 690 * If the size is specified, override the file attributes. 691 */ 692 if (mdio->md_size) 693 sc->nsect = mdio->md_size; 694 else 695 sc->nsect = vattr.va_size / sc->secsize; /* XXX: round up ? */ 696 if (sc->nsect == 0) { 697 (void) vn_close(nd.ni_vp, flags, p->p_ucred, td); 698 return (EINVAL); 699 } 700 error = mdsetcred(sc, p->p_ucred); 701 if (error) { 702 (void) vn_close(nd.ni_vp, flags, p->p_ucred, td); 703 return (error); 704 } 705 mdinit(sc); 706 return (0); 707 } 708 709 static int 710 mddestroy(struct md_s *sc, struct thread *td) 711 { 712 unsigned u; 713 714 GIANT_REQUIRED; 715 716 if (sc->dev != NULL) { 717 devstat_remove_entry(&sc->stats); 718 disk_destroy(sc->dev); 719 } 720 if (sc->vnode != NULL) 721 (void)vn_close(sc->vnode, sc->flags & MD_READONLY ? 722 FREAD : (FREAD|FWRITE), sc->cred, td); 723 if (sc->cred != NULL) 724 crfree(sc->cred); 725 if (sc->object != NULL) { 726 vm_pager_deallocate(sc->object); 727 } 728 if (sc->secp != NULL) { 729 for (u = 0; u < sc->nsect; u++) 730 if ((uintptr_t)sc->secp[u] > 255) 731 FREE(sc->secp[u], M_MDSECT); 732 FREE(sc->secp, M_MD); 733 } 734 735 /* XXX: LOCK(unique unit numbers) */ 736 LIST_REMOVE(sc, list); 737 /* XXX: UNLOCK(unique unit numbers) */ 738 FREE(sc, M_MD); 739 return (0); 740 } 741 742 static int 743 mdcreate_swap(struct md_ioctl *mdio, struct thread *td) 744 { 745 int error; 746 struct md_s *sc; 747 748 GIANT_REQUIRED; 749 750 if (mdio->md_options & MD_AUTOUNIT) { 751 sc = mdnew(-1); 752 mdio->md_unit = sc->unit; 753 } else { 754 sc = mdnew(mdio->md_unit); 755 } 756 if (sc == NULL) 757 return (EBUSY); 758 759 sc->type = MD_SWAP; 760 761 /* 762 * Range check. Disallow negative sizes or any size less then the 763 * size of a page. Then round to a page. 764 */ 765 766 if (mdio->md_size == 0) { 767 mddestroy(sc, td); 768 return (EDOM); 769 } 770 771 /* 772 * Allocate an OBJT_SWAP object. 773 * 774 * sc_secsize is PAGE_SIZE'd 775 * 776 * mdio->size is in DEV_BSIZE'd chunks. 777 * Note the truncation. 778 */ 779 780 sc->secsize = PAGE_SIZE; 781 sc->nsect = mdio->md_size / (PAGE_SIZE / DEV_BSIZE); 782 sc->object = vm_pager_allocate(OBJT_SWAP, NULL, sc->secsize * (vm_offset_t)sc->nsect, VM_PROT_DEFAULT, 0); 783 sc->flags = mdio->md_options & MD_FORCE; 784 if (mdio->md_options & MD_RESERVE) { 785 if (swap_pager_reserve(sc->object, 0, sc->nsect) < 0) { 786 vm_pager_deallocate(sc->object); 787 sc->object = NULL; 788 mddestroy(sc, td); 789 return (EDOM); 790 } 791 } 792 error = mdsetcred(sc, td->td_proc->p_ucred); 793 if (error) 794 mddestroy(sc, td); 795 else 796 mdinit(sc); 797 return (error); 798 } 799 800 static int 801 mddetach(int unit, struct thread *td) 802 { 803 struct md_s *sc; 804 805 sc = mdfind(unit); 806 if (sc == NULL) 807 return (ENOENT); 808 if (sc->opencount != 0 && !(sc->flags & MD_FORCE)) 809 return (EBUSY); 810 switch(sc->type) { 811 case MD_VNODE: 812 case MD_SWAP: 813 case MD_MALLOC: 814 case MD_PRELOAD: 815 return (mddestroy(sc, td)); 816 default: 817 return (EOPNOTSUPP); 818 } 819 } 820 821 static int 822 mdctlioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td) 823 { 824 struct md_ioctl *mdio; 825 struct md_s *sc; 826 827 if (md_debug) 828 printf("mdctlioctl(%s %lx %p %x %p)\n", 829 devtoname(dev), cmd, addr, flags, td); 830 831 mdio = (struct md_ioctl *)addr; 832 switch (cmd) { 833 case MDIOCATTACH: 834 switch (mdio->md_type) { 835 case MD_MALLOC: 836 return (mdcreate_malloc(mdio)); 837 case MD_PRELOAD: 838 return (mdcreate_preload(mdio)); 839 case MD_VNODE: 840 return (mdcreate_vnode(mdio, td)); 841 case MD_SWAP: 842 return (mdcreate_swap(mdio, td)); 843 default: 844 return (EINVAL); 845 } 846 case MDIOCDETACH: 847 if (mdio->md_file != NULL || mdio->md_size != 0 || 848 mdio->md_options != 0) 849 return (EINVAL); 850 return (mddetach(mdio->md_unit, td)); 851 case MDIOCQUERY: 852 sc = mdfind(mdio->md_unit); 853 if (sc == NULL) 854 return (ENOENT); 855 mdio->md_type = sc->type; 856 mdio->md_options = sc->flags; 857 switch (sc->type) { 858 case MD_MALLOC: 859 mdio->md_size = sc->nsect; 860 break; 861 case MD_PRELOAD: 862 mdio->md_size = sc->nsect; 863 (u_char *)(uintptr_t)mdio->md_base = sc->pl_ptr; 864 break; 865 case MD_SWAP: 866 mdio->md_size = sc->nsect * (PAGE_SIZE / DEV_BSIZE); 867 break; 868 case MD_VNODE: 869 mdio->md_size = sc->nsect; 870 /* XXX fill this in */ 871 mdio->md_file = NULL; 872 break; 873 } 874 return (0); 875 default: 876 return (ENOIOCTL); 877 }; 878 return (ENOIOCTL); 879 } 880 881 static void 882 md_preloaded(u_char *image, unsigned length) 883 { 884 struct md_s *sc; 885 886 sc = mdnew(-1); 887 if (sc == NULL) 888 return; 889 sc->type = MD_PRELOAD; 890 sc->secsize = DEV_BSIZE; 891 sc->nsect = length / DEV_BSIZE; 892 sc->pl_ptr = image; 893 sc->pl_len = length; 894 if (sc->unit == 0) 895 mdrootready = 1; 896 mdinit(sc); 897 } 898 899 static void 900 md_drvinit(void *unused) 901 { 902 903 caddr_t mod; 904 caddr_t c; 905 u_char *ptr, *name, *type; 906 unsigned len; 907 908 #ifdef MD_ROOT_SIZE 909 md_preloaded(mfs_root, MD_ROOT_SIZE*1024); 910 #endif 911 mod = NULL; 912 while ((mod = preload_search_next_name(mod)) != NULL) { 913 name = (char *)preload_search_info(mod, MODINFO_NAME); 914 type = (char *)preload_search_info(mod, MODINFO_TYPE); 915 if (name == NULL) 916 continue; 917 if (type == NULL) 918 continue; 919 if (strcmp(type, "md_image") && strcmp(type, "mfs_root")) 920 continue; 921 c = preload_search_info(mod, MODINFO_ADDR); 922 ptr = *(u_char **)c; 923 c = preload_search_info(mod, MODINFO_SIZE); 924 len = *(unsigned *)c; 925 printf("%s%d: Preloaded image <%s> %d bytes at %p\n", 926 MD_NAME, mdunits, name, len, ptr); 927 md_preloaded(ptr, len); 928 } 929 status_dev = make_dev(&mdctl_cdevsw, 0xffff00ff, UID_ROOT, GID_WHEEL, 930 0600, MDCTL_NAME); 931 } 932 933 static int 934 md_modevent(module_t mod, int type, void *data) 935 { 936 int error; 937 struct md_s *sc; 938 939 switch (type) { 940 case MOD_LOAD: 941 md_drvinit(NULL); 942 break; 943 case MOD_UNLOAD: 944 LIST_FOREACH(sc, &md_softc_list, list) { 945 error = mddetach(sc->unit, curthread); 946 if (error != 0) 947 return (error); 948 } 949 if (status_dev) 950 destroy_dev(status_dev); 951 status_dev = 0; 952 break; 953 default: 954 break; 955 } 956 return (0); 957 } 958 959 static moduledata_t md_mod = { 960 MD_NAME, 961 md_modevent, 962 NULL 963 }; 964 DECLARE_MODULE(md, md_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE+CDEV_MAJOR); 965 MODULE_VERSION(md, MD_MODVER); 966 967 968 #ifdef MD_ROOT 969 static void 970 md_takeroot(void *junk) 971 { 972 if (mdrootready) 973 rootdevnames[0] = "ufs:/dev/md0c"; 974 } 975 976 SYSINIT(md_root, SI_SUB_MOUNT_ROOT, SI_ORDER_FIRST, md_takeroot, NULL); 977 #endif 978