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