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