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 /* 390 * When reading set IO_DIRECT to try to avoid double-caching 391 * the data. When writing IO_DIRECT is not optimal, but we 392 * must set IO_NOWDRAIN to avoid a wdrain deadlock. 393 */ 394 if (bp->bio_cmd == BIO_READ) { 395 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY, curthread); 396 error = VOP_READ(sc->vnode, &auio, IO_DIRECT, sc->cred); 397 } else { 398 (void) vn_start_write(sc->vnode, &mp, V_WAIT); 399 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY, curthread); 400 error = VOP_WRITE(sc->vnode, &auio, IO_NOWDRAIN, sc->cred); 401 vn_finished_write(mp); 402 } 403 VOP_UNLOCK(sc->vnode, 0, curthread); 404 bp->bio_resid = auio.uio_resid; 405 biofinish(bp, &sc->stats, error); 406 } 407 return; 408 } 409 410 static void 411 mdstart_swap(struct md_s *sc) 412 { 413 struct bio *bp; 414 415 for (;;) { 416 /* XXX: LOCK(unique unit numbers) */ 417 bp = bioq_first(&sc->bio_queue); 418 if (bp) 419 bioq_remove(&sc->bio_queue, bp); 420 /* XXX: UNLOCK(unique unit numbers) */ 421 if (!bp) 422 break; 423 424 #if 0 425 devstat_start_transaction(&sc->stats); 426 #endif 427 428 if ((bp->bio_cmd == BIO_DELETE) && (sc->flags & MD_RESERVE)) 429 biodone(bp); 430 else 431 vm_pager_strategy(sc->object, bp); 432 433 #if 0 434 devstat_end_transaction_bio(&sc->stats, bp); 435 #endif 436 } 437 return; 438 } 439 440 static void 441 mdstrategy(struct bio *bp) 442 { 443 struct md_s *sc; 444 445 if (md_debug > 1) 446 printf("mdstrategy(%p) %s %x, %d, %ld, %p)\n", 447 bp, devtoname(bp->bio_dev), bp->bio_flags, bp->bio_blkno, 448 bp->bio_bcount / DEV_BSIZE, bp->bio_data); 449 450 sc = bp->bio_dev->si_drv1; 451 452 /* XXX: LOCK(sc->lock) */ 453 bioqdisksort(&sc->bio_queue, bp); 454 /* XXX: UNLOCK(sc->lock) */ 455 456 if (atomic_cmpset_int(&sc->busy, 0, 1) == 0) 457 return; 458 459 switch (sc->type) { 460 case MD_MALLOC: 461 mdstart_malloc(sc); 462 break; 463 case MD_PRELOAD: 464 mdstart_preload(sc); 465 break; 466 case MD_VNODE: 467 mdstart_vnode(sc); 468 break; 469 case MD_SWAP: 470 mdstart_swap(sc); 471 break; 472 default: 473 panic("Impossible md(type)"); 474 break; 475 } 476 sc->busy = 0; 477 } 478 479 static struct md_s * 480 mdfind(int unit) 481 { 482 struct md_s *sc; 483 484 /* XXX: LOCK(unique unit numbers) */ 485 LIST_FOREACH(sc, &md_softc_list, list) { 486 if (sc->unit == unit) 487 break; 488 } 489 /* XXX: UNLOCK(unique unit numbers) */ 490 return (sc); 491 } 492 493 static struct md_s * 494 mdnew(int unit) 495 { 496 struct md_s *sc; 497 int max = -1; 498 499 /* XXX: LOCK(unique unit numbers) */ 500 LIST_FOREACH(sc, &md_softc_list, list) { 501 if (sc->unit == unit) { 502 /* XXX: UNLOCK(unique unit numbers) */ 503 return (NULL); 504 } 505 if (sc->unit > max) 506 max = sc->unit; 507 } 508 if (unit == -1) 509 unit = max + 1; 510 if (unit > DKMAXUNIT) 511 return (NULL); 512 MALLOC(sc, struct md_s *, sizeof(*sc), M_MD, M_WAITOK | M_ZERO); 513 sc->unit = unit; 514 LIST_INSERT_HEAD(&md_softc_list, sc, list); 515 /* XXX: UNLOCK(unique unit numbers) */ 516 return (sc); 517 } 518 519 static void 520 mdinit(struct md_s *sc) 521 { 522 523 bioq_init(&sc->bio_queue); 524 devstat_add_entry(&sc->stats, MD_NAME, sc->unit, sc->secsize, 525 DEVSTAT_NO_ORDERED_TAGS, 526 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER, 527 DEVSTAT_PRIORITY_OTHER); 528 sc->dev = disk_create(sc->unit, &sc->disk, 0, &md_cdevsw, &mddisk_cdevsw); 529 sc->dev->si_drv1 = sc; 530 } 531 532 /* 533 * XXX: we should check that the range they feed us is mapped. 534 * XXX: we should implement read-only. 535 */ 536 537 static int 538 mdcreate_preload(struct md_ioctl *mdio) 539 { 540 struct md_s *sc; 541 542 if (mdio->md_size == 0) 543 return (EINVAL); 544 if (mdio->md_options & ~(MD_AUTOUNIT)) 545 return (EINVAL); 546 if (mdio->md_options & MD_AUTOUNIT) { 547 sc = mdnew(-1); 548 if (sc == NULL) 549 return (ENOMEM); 550 mdio->md_unit = sc->unit; 551 } else { 552 sc = mdnew(mdio->md_unit); 553 if (sc == NULL) 554 return (EBUSY); 555 } 556 sc->type = MD_PRELOAD; 557 sc->secsize = DEV_BSIZE; 558 sc->nsect = mdio->md_size; 559 sc->flags = mdio->md_options & MD_FORCE; 560 /* Cast to pointer size, then to pointer to avoid warning */ 561 sc->pl_ptr = (u_char *)(uintptr_t)mdio->md_base; 562 sc->pl_len = (mdio->md_size << DEV_BSHIFT); 563 mdinit(sc); 564 return (0); 565 } 566 567 568 static int 569 mdcreate_malloc(struct md_ioctl *mdio) 570 { 571 struct md_s *sc; 572 unsigned u; 573 574 if (mdio->md_size == 0) 575 return (EINVAL); 576 if (mdio->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE)) 577 return (EINVAL); 578 /* Compression doesn't make sense if we have reserved space */ 579 if (mdio->md_options & MD_RESERVE) 580 mdio->md_options &= ~MD_COMPRESS; 581 if (mdio->md_options & MD_AUTOUNIT) { 582 sc = mdnew(-1); 583 if (sc == NULL) 584 return (ENOMEM); 585 mdio->md_unit = sc->unit; 586 } else { 587 sc = mdnew(mdio->md_unit); 588 if (sc == NULL) 589 return (EBUSY); 590 } 591 sc->type = MD_MALLOC; 592 sc->secsize = DEV_BSIZE; 593 sc->nsect = mdio->md_size; 594 sc->flags = mdio->md_options & (MD_COMPRESS | MD_FORCE); 595 MALLOC(sc->secp, u_char **, sc->nsect * sizeof(u_char *), M_MD, M_WAITOK | M_ZERO); 596 if (mdio->md_options & MD_RESERVE) { 597 for (u = 0; u < sc->nsect; u++) 598 MALLOC(sc->secp[u], u_char *, DEV_BSIZE, M_MDSECT, M_WAITOK | M_ZERO); 599 } 600 printf("%s%d: Malloc disk\n", MD_NAME, sc->unit); 601 mdinit(sc); 602 return (0); 603 } 604 605 606 static int 607 mdsetcred(struct md_s *sc, struct ucred *cred) 608 { 609 char *tmpbuf; 610 int error = 0; 611 612 /* 613 * Set credits in our softc 614 */ 615 616 if (sc->cred) 617 crfree(sc->cred); 618 sc->cred = crhold(cred); 619 620 /* 621 * Horrible kludge to establish credentials for NFS XXX. 622 */ 623 624 if (sc->vnode) { 625 struct uio auio; 626 struct iovec aiov; 627 628 tmpbuf = malloc(sc->secsize, M_TEMP, M_WAITOK); 629 bzero(&auio, sizeof(auio)); 630 631 aiov.iov_base = tmpbuf; 632 aiov.iov_len = sc->secsize; 633 auio.uio_iov = &aiov; 634 auio.uio_iovcnt = 1; 635 auio.uio_offset = 0; 636 auio.uio_rw = UIO_READ; 637 auio.uio_segflg = UIO_SYSSPACE; 638 auio.uio_resid = aiov.iov_len; 639 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY, curthread); 640 error = VOP_READ(sc->vnode, &auio, 0, sc->cred); 641 VOP_UNLOCK(sc->vnode, 0, curthread); 642 free(tmpbuf, M_TEMP); 643 } 644 return (error); 645 } 646 647 static int 648 mdcreate_vnode(struct md_ioctl *mdio, struct thread *td) 649 { 650 struct proc *p = td->td_proc; 651 struct md_s *sc; 652 struct vattr vattr; 653 struct nameidata nd; 654 int error, flags; 655 656 if (mdio->md_options & MD_AUTOUNIT) { 657 sc = mdnew(-1); 658 mdio->md_unit = sc->unit; 659 } else { 660 sc = mdnew(mdio->md_unit); 661 } 662 if (sc == NULL) 663 return (EBUSY); 664 665 sc->type = MD_VNODE; 666 sc->flags = mdio->md_options & MD_FORCE; 667 668 flags = FREAD|FWRITE; 669 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, mdio->md_file, td); 670 error = vn_open(&nd, &flags, 0); 671 if (error) { 672 if (error != EACCES && error != EPERM && error != EROFS) 673 return (error); 674 flags &= ~FWRITE; 675 sc->flags |= MD_READONLY; 676 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, mdio->md_file, td); 677 error = vn_open(&nd, &flags, 0); 678 if (error) 679 return (error); 680 } 681 NDFREE(&nd, NDF_ONLY_PNBUF); 682 if (nd.ni_vp->v_type != VREG || 683 (error = VOP_GETATTR(nd.ni_vp, &vattr, p->p_ucred, td))) { 684 VOP_UNLOCK(nd.ni_vp, 0, td); 685 (void) vn_close(nd.ni_vp, flags, p->p_ucred, td); 686 return (error ? error : EINVAL); 687 } 688 VOP_UNLOCK(nd.ni_vp, 0, td); 689 sc->secsize = DEV_BSIZE; 690 sc->vnode = nd.ni_vp; 691 692 /* 693 * If the size is specified, override the file attributes. 694 */ 695 if (mdio->md_size) 696 sc->nsect = mdio->md_size; 697 else 698 sc->nsect = vattr.va_size / sc->secsize; /* XXX: round up ? */ 699 if (sc->nsect == 0) { 700 (void) vn_close(nd.ni_vp, flags, p->p_ucred, td); 701 return (EINVAL); 702 } 703 error = mdsetcred(sc, p->p_ucred); 704 if (error) { 705 (void) vn_close(nd.ni_vp, flags, p->p_ucred, td); 706 return (error); 707 } 708 mdinit(sc); 709 return (0); 710 } 711 712 static int 713 mddestroy(struct md_s *sc, struct thread *td) 714 { 715 unsigned u; 716 717 GIANT_REQUIRED; 718 719 if (sc->dev != NULL) { 720 devstat_remove_entry(&sc->stats); 721 disk_destroy(sc->dev); 722 } 723 if (sc->vnode != NULL) 724 (void)vn_close(sc->vnode, sc->flags & MD_READONLY ? 725 FREAD : (FREAD|FWRITE), sc->cred, td); 726 if (sc->cred != NULL) 727 crfree(sc->cred); 728 if (sc->object != NULL) { 729 vm_pager_deallocate(sc->object); 730 } 731 if (sc->secp != NULL) { 732 for (u = 0; u < sc->nsect; u++) 733 if ((uintptr_t)sc->secp[u] > 255) 734 FREE(sc->secp[u], M_MDSECT); 735 FREE(sc->secp, M_MD); 736 } 737 738 /* XXX: LOCK(unique unit numbers) */ 739 LIST_REMOVE(sc, list); 740 /* XXX: UNLOCK(unique unit numbers) */ 741 FREE(sc, M_MD); 742 return (0); 743 } 744 745 static int 746 mdcreate_swap(struct md_ioctl *mdio, struct thread *td) 747 { 748 int error; 749 struct md_s *sc; 750 751 GIANT_REQUIRED; 752 753 if (mdio->md_options & MD_AUTOUNIT) { 754 sc = mdnew(-1); 755 mdio->md_unit = sc->unit; 756 } else { 757 sc = mdnew(mdio->md_unit); 758 } 759 if (sc == NULL) 760 return (EBUSY); 761 762 sc->type = MD_SWAP; 763 764 /* 765 * Range check. Disallow negative sizes or any size less then the 766 * size of a page. Then round to a page. 767 */ 768 769 if (mdio->md_size == 0) { 770 mddestroy(sc, td); 771 return (EDOM); 772 } 773 774 /* 775 * Allocate an OBJT_SWAP object. 776 * 777 * sc_secsize is PAGE_SIZE'd 778 * 779 * mdio->size is in DEV_BSIZE'd chunks. 780 * Note the truncation. 781 */ 782 783 sc->secsize = PAGE_SIZE; 784 sc->nsect = mdio->md_size / (PAGE_SIZE / DEV_BSIZE); 785 sc->object = vm_pager_allocate(OBJT_SWAP, NULL, sc->secsize * (vm_offset_t)sc->nsect, VM_PROT_DEFAULT, 0); 786 sc->flags = mdio->md_options & MD_FORCE; 787 if (mdio->md_options & MD_RESERVE) { 788 if (swap_pager_reserve(sc->object, 0, sc->nsect) < 0) { 789 vm_pager_deallocate(sc->object); 790 sc->object = NULL; 791 mddestroy(sc, td); 792 return (EDOM); 793 } 794 } 795 error = mdsetcred(sc, td->td_proc->p_ucred); 796 if (error) 797 mddestroy(sc, td); 798 else 799 mdinit(sc); 800 return (error); 801 } 802 803 static int 804 mddetach(int unit, struct thread *td) 805 { 806 struct md_s *sc; 807 808 sc = mdfind(unit); 809 if (sc == NULL) 810 return (ENOENT); 811 if (sc->opencount != 0 && !(sc->flags & MD_FORCE)) 812 return (EBUSY); 813 switch(sc->type) { 814 case MD_VNODE: 815 case MD_SWAP: 816 case MD_MALLOC: 817 case MD_PRELOAD: 818 return (mddestroy(sc, td)); 819 default: 820 return (EOPNOTSUPP); 821 } 822 } 823 824 static int 825 mdctlioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td) 826 { 827 struct md_ioctl *mdio; 828 struct md_s *sc; 829 830 if (md_debug) 831 printf("mdctlioctl(%s %lx %p %x %p)\n", 832 devtoname(dev), cmd, addr, flags, td); 833 834 mdio = (struct md_ioctl *)addr; 835 switch (cmd) { 836 case MDIOCATTACH: 837 switch (mdio->md_type) { 838 case MD_MALLOC: 839 return (mdcreate_malloc(mdio)); 840 case MD_PRELOAD: 841 return (mdcreate_preload(mdio)); 842 case MD_VNODE: 843 return (mdcreate_vnode(mdio, td)); 844 case MD_SWAP: 845 return (mdcreate_swap(mdio, td)); 846 default: 847 return (EINVAL); 848 } 849 case MDIOCDETACH: 850 if (mdio->md_file != NULL || mdio->md_size != 0 || 851 mdio->md_options != 0) 852 return (EINVAL); 853 return (mddetach(mdio->md_unit, td)); 854 case MDIOCQUERY: 855 sc = mdfind(mdio->md_unit); 856 if (sc == NULL) 857 return (ENOENT); 858 mdio->md_type = sc->type; 859 mdio->md_options = sc->flags; 860 switch (sc->type) { 861 case MD_MALLOC: 862 mdio->md_size = sc->nsect; 863 break; 864 case MD_PRELOAD: 865 mdio->md_size = sc->nsect; 866 (u_char *)(uintptr_t)mdio->md_base = sc->pl_ptr; 867 break; 868 case MD_SWAP: 869 mdio->md_size = sc->nsect * (PAGE_SIZE / DEV_BSIZE); 870 break; 871 case MD_VNODE: 872 mdio->md_size = sc->nsect; 873 /* XXX fill this in */ 874 mdio->md_file = NULL; 875 break; 876 } 877 return (0); 878 default: 879 return (ENOIOCTL); 880 }; 881 return (ENOIOCTL); 882 } 883 884 static void 885 md_preloaded(u_char *image, unsigned length) 886 { 887 struct md_s *sc; 888 889 sc = mdnew(-1); 890 if (sc == NULL) 891 return; 892 sc->type = MD_PRELOAD; 893 sc->secsize = DEV_BSIZE; 894 sc->nsect = length / DEV_BSIZE; 895 sc->pl_ptr = image; 896 sc->pl_len = length; 897 if (sc->unit == 0) 898 mdrootready = 1; 899 mdinit(sc); 900 } 901 902 static void 903 md_drvinit(void *unused) 904 { 905 906 caddr_t mod; 907 caddr_t c; 908 u_char *ptr, *name, *type; 909 unsigned len; 910 911 #ifdef MD_ROOT_SIZE 912 md_preloaded(mfs_root, MD_ROOT_SIZE*1024); 913 #endif 914 mod = NULL; 915 while ((mod = preload_search_next_name(mod)) != NULL) { 916 name = (char *)preload_search_info(mod, MODINFO_NAME); 917 type = (char *)preload_search_info(mod, MODINFO_TYPE); 918 if (name == NULL) 919 continue; 920 if (type == NULL) 921 continue; 922 if (strcmp(type, "md_image") && strcmp(type, "mfs_root")) 923 continue; 924 c = preload_search_info(mod, MODINFO_ADDR); 925 ptr = *(u_char **)c; 926 c = preload_search_info(mod, MODINFO_SIZE); 927 len = *(unsigned *)c; 928 printf("%s%d: Preloaded image <%s> %d bytes at %p\n", 929 MD_NAME, mdunits, name, len, ptr); 930 md_preloaded(ptr, len); 931 } 932 status_dev = make_dev(&mdctl_cdevsw, 0xffff00ff, UID_ROOT, GID_WHEEL, 933 0600, MDCTL_NAME); 934 } 935 936 static int 937 md_modevent(module_t mod, int type, void *data) 938 { 939 int error; 940 struct md_s *sc; 941 942 switch (type) { 943 case MOD_LOAD: 944 md_drvinit(NULL); 945 break; 946 case MOD_UNLOAD: 947 LIST_FOREACH(sc, &md_softc_list, list) { 948 error = mddetach(sc->unit, curthread); 949 if (error != 0) 950 return (error); 951 } 952 if (status_dev) 953 destroy_dev(status_dev); 954 status_dev = 0; 955 break; 956 default: 957 break; 958 } 959 return (0); 960 } 961 962 static moduledata_t md_mod = { 963 MD_NAME, 964 md_modevent, 965 NULL 966 }; 967 DECLARE_MODULE(md, md_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE+CDEV_MAJOR); 968 MODULE_VERSION(md, MD_MODVER); 969 970 971 #ifdef MD_ROOT 972 static void 973 md_takeroot(void *junk) 974 { 975 if (mdrootready) 976 rootdevnames[0] = "ufs:/dev/md0c"; 977 } 978 979 SYSINIT(md_root, SI_SUB_MOUNT_ROOT, SI_ORDER_FIRST, md_takeroot, NULL); 980 #endif 981