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 * 4. Neither the name of the University nor the names of its contributors 35 * may be used to endorse or promote products derived from this software 36 * without specific prior written permission. 37 * 38 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 39 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 41 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 42 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 44 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 46 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 47 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * 50 * from: Utah Hdr: vn.c 1.13 94/04/02 51 * 52 * from: @(#)vn.c 8.6 (Berkeley) 4/1/94 53 * From: src/sys/dev/vn/vn.c,v 1.122 2000/12/16 16:06:03 54 */ 55 56 #include "opt_geom.h" 57 #include "opt_md.h" 58 59 #include <sys/param.h> 60 #include <sys/systm.h> 61 #include <sys/bio.h> 62 #include <sys/conf.h> 63 #include <sys/devicestat.h> 64 #include <sys/fcntl.h> 65 #include <sys/kernel.h> 66 #include <sys/kthread.h> 67 #include <sys/limits.h> 68 #include <sys/linker.h> 69 #include <sys/lock.h> 70 #include <sys/malloc.h> 71 #include <sys/mdioctl.h> 72 #include <sys/mount.h> 73 #include <sys/mutex.h> 74 #include <sys/sx.h> 75 #include <sys/namei.h> 76 #include <sys/proc.h> 77 #include <sys/queue.h> 78 #include <sys/sched.h> 79 #include <sys/sf_buf.h> 80 #include <sys/sysctl.h> 81 #include <sys/vnode.h> 82 83 #include <geom/geom.h> 84 85 #include <vm/vm.h> 86 #include <vm/vm_object.h> 87 #include <vm/vm_page.h> 88 #include <vm/vm_pager.h> 89 #include <vm/swap_pager.h> 90 #include <vm/uma.h> 91 92 #define MD_MODVER 1 93 94 #define MD_SHUTDOWN 0x10000 /* Tell worker thread to terminate. */ 95 #define MD_EXITING 0x20000 /* Worker thread is exiting. */ 96 97 #ifndef MD_NSECT 98 #define MD_NSECT (10000 * 2) 99 #endif 100 101 static MALLOC_DEFINE(M_MD, "md_disk", "Memory Disk"); 102 static MALLOC_DEFINE(M_MDSECT, "md_sectors", "Memory Disk Sectors"); 103 104 static int md_debug; 105 SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0, ""); 106 107 #if defined(MD_ROOT) && defined(MD_ROOT_SIZE) 108 /* 109 * Preloaded image gets put here. 110 * Applications that patch the object with the image can determine 111 * the size looking at the start and end markers (strings), 112 * so we want them contiguous. 113 */ 114 static struct { 115 u_char start[MD_ROOT_SIZE*1024]; 116 u_char end[128]; 117 } mfs_root = { 118 .start = "MFS Filesystem goes here", 119 .end = "MFS Filesystem had better STOP here", 120 }; 121 #endif 122 123 static g_init_t g_md_init; 124 static g_fini_t g_md_fini; 125 static g_start_t g_md_start; 126 static g_access_t g_md_access; 127 static void g_md_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 128 struct g_consumer *cp __unused, struct g_provider *pp); 129 130 static int mdunits; 131 static struct cdev *status_dev = 0; 132 static struct sx md_sx; 133 134 static d_ioctl_t mdctlioctl; 135 136 static struct cdevsw mdctl_cdevsw = { 137 .d_version = D_VERSION, 138 .d_ioctl = mdctlioctl, 139 .d_name = MD_NAME, 140 }; 141 142 struct g_class g_md_class = { 143 .name = "MD", 144 .version = G_VERSION, 145 .init = g_md_init, 146 .fini = g_md_fini, 147 .start = g_md_start, 148 .access = g_md_access, 149 .dumpconf = g_md_dumpconf, 150 }; 151 152 DECLARE_GEOM_CLASS(g_md_class, g_md); 153 154 155 static LIST_HEAD(, md_s) md_softc_list = LIST_HEAD_INITIALIZER(&md_softc_list); 156 157 #define NINDIR (PAGE_SIZE / sizeof(uintptr_t)) 158 #define NMASK (NINDIR-1) 159 static int nshift; 160 161 struct indir { 162 uintptr_t *array; 163 u_int total; 164 u_int used; 165 u_int shift; 166 }; 167 168 struct md_s { 169 int unit; 170 LIST_ENTRY(md_s) list; 171 struct bio_queue_head bio_queue; 172 struct mtx queue_mtx; 173 struct cdev *dev; 174 enum md_types type; 175 off_t mediasize; 176 unsigned sectorsize; 177 unsigned opencount; 178 unsigned fwheads; 179 unsigned fwsectors; 180 unsigned flags; 181 char name[20]; 182 struct proc *procp; 183 struct g_geom *gp; 184 struct g_provider *pp; 185 int (*start)(struct md_s *sc, struct bio *bp); 186 struct devstat *devstat; 187 188 /* MD_MALLOC related fields */ 189 struct indir *indir; 190 uma_zone_t uma; 191 192 /* MD_PRELOAD related fields */ 193 u_char *pl_ptr; 194 size_t pl_len; 195 196 /* MD_VNODE related fields */ 197 struct vnode *vnode; 198 char file[PATH_MAX]; 199 struct ucred *cred; 200 201 /* MD_SWAP related fields */ 202 vm_object_t object; 203 }; 204 205 static struct indir * 206 new_indir(u_int shift) 207 { 208 struct indir *ip; 209 210 ip = malloc(sizeof *ip, M_MD, M_NOWAIT | M_ZERO); 211 if (ip == NULL) 212 return (NULL); 213 ip->array = malloc(sizeof(uintptr_t) * NINDIR, 214 M_MDSECT, M_NOWAIT | M_ZERO); 215 if (ip->array == NULL) { 216 free(ip, M_MD); 217 return (NULL); 218 } 219 ip->total = NINDIR; 220 ip->shift = shift; 221 return (ip); 222 } 223 224 static void 225 del_indir(struct indir *ip) 226 { 227 228 free(ip->array, M_MDSECT); 229 free(ip, M_MD); 230 } 231 232 static void 233 destroy_indir(struct md_s *sc, struct indir *ip) 234 { 235 int i; 236 237 for (i = 0; i < NINDIR; i++) { 238 if (!ip->array[i]) 239 continue; 240 if (ip->shift) 241 destroy_indir(sc, (struct indir*)(ip->array[i])); 242 else if (ip->array[i] > 255) 243 uma_zfree(sc->uma, (void *)(ip->array[i])); 244 } 245 del_indir(ip); 246 } 247 248 /* 249 * This function does the math and allocates the top level "indir" structure 250 * for a device of "size" sectors. 251 */ 252 253 static struct indir * 254 dimension(off_t size) 255 { 256 off_t rcnt; 257 struct indir *ip; 258 int i, layer; 259 260 rcnt = size; 261 layer = 0; 262 while (rcnt > NINDIR) { 263 rcnt /= NINDIR; 264 layer++; 265 } 266 /* figure out log2(NINDIR) */ 267 for (i = NINDIR, nshift = -1; i; nshift++) 268 i >>= 1; 269 270 /* 271 * XXX: the top layer is probably not fully populated, so we allocate 272 * too much space for ip->array in here. 273 */ 274 ip = malloc(sizeof *ip, M_MD, M_WAITOK | M_ZERO); 275 ip->array = malloc(sizeof(uintptr_t) * NINDIR, 276 M_MDSECT, M_WAITOK | M_ZERO); 277 ip->total = NINDIR; 278 ip->shift = layer * nshift; 279 return (ip); 280 } 281 282 /* 283 * Read a given sector 284 */ 285 286 static uintptr_t 287 s_read(struct indir *ip, off_t offset) 288 { 289 struct indir *cip; 290 int idx; 291 uintptr_t up; 292 293 if (md_debug > 1) 294 printf("s_read(%jd)\n", (intmax_t)offset); 295 up = 0; 296 for (cip = ip; cip != NULL;) { 297 if (cip->shift) { 298 idx = (offset >> cip->shift) & NMASK; 299 up = cip->array[idx]; 300 cip = (struct indir *)up; 301 continue; 302 } 303 idx = offset & NMASK; 304 return (cip->array[idx]); 305 } 306 return (0); 307 } 308 309 /* 310 * Write a given sector, prune the tree if the value is 0 311 */ 312 313 static int 314 s_write(struct indir *ip, off_t offset, uintptr_t ptr) 315 { 316 struct indir *cip, *lip[10]; 317 int idx, li; 318 uintptr_t up; 319 320 if (md_debug > 1) 321 printf("s_write(%jd, %p)\n", (intmax_t)offset, (void *)ptr); 322 up = 0; 323 li = 0; 324 cip = ip; 325 for (;;) { 326 lip[li++] = cip; 327 if (cip->shift) { 328 idx = (offset >> cip->shift) & NMASK; 329 up = cip->array[idx]; 330 if (up != 0) { 331 cip = (struct indir *)up; 332 continue; 333 } 334 /* Allocate branch */ 335 cip->array[idx] = 336 (uintptr_t)new_indir(cip->shift - nshift); 337 if (cip->array[idx] == 0) 338 return (ENOSPC); 339 cip->used++; 340 up = cip->array[idx]; 341 cip = (struct indir *)up; 342 continue; 343 } 344 /* leafnode */ 345 idx = offset & NMASK; 346 up = cip->array[idx]; 347 if (up != 0) 348 cip->used--; 349 cip->array[idx] = ptr; 350 if (ptr != 0) 351 cip->used++; 352 break; 353 } 354 if (cip->used != 0 || li == 1) 355 return (0); 356 li--; 357 while (cip->used == 0 && cip != ip) { 358 li--; 359 idx = (offset >> lip[li]->shift) & NMASK; 360 up = lip[li]->array[idx]; 361 KASSERT(up == (uintptr_t)cip, ("md screwed up")); 362 del_indir(cip); 363 lip[li]->array[idx] = 0; 364 lip[li]->used--; 365 cip = lip[li]; 366 } 367 return (0); 368 } 369 370 371 static int 372 g_md_access(struct g_provider *pp, int r, int w, int e) 373 { 374 struct md_s *sc; 375 376 sc = pp->geom->softc; 377 if (sc == NULL) 378 return (ENXIO); 379 r += pp->acr; 380 w += pp->acw; 381 e += pp->ace; 382 if ((sc->flags & MD_READONLY) != 0 && w > 0) 383 return (EROFS); 384 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 385 sc->opencount = 1; 386 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 387 sc->opencount = 0; 388 } 389 return (0); 390 } 391 392 static void 393 g_md_start(struct bio *bp) 394 { 395 struct md_s *sc; 396 397 sc = bp->bio_to->geom->softc; 398 if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE)) 399 devstat_start_transaction_bio(sc->devstat, bp); 400 mtx_lock(&sc->queue_mtx); 401 bioq_disksort(&sc->bio_queue, bp); 402 mtx_unlock(&sc->queue_mtx); 403 wakeup(sc); 404 } 405 406 static int 407 mdstart_malloc(struct md_s *sc, struct bio *bp) 408 { 409 int i, error; 410 u_char *dst; 411 off_t secno, nsec, uc; 412 uintptr_t sp, osp; 413 414 switch (bp->bio_cmd) { 415 case BIO_READ: 416 case BIO_WRITE: 417 case BIO_DELETE: 418 break; 419 default: 420 return (EOPNOTSUPP); 421 } 422 423 nsec = bp->bio_length / sc->sectorsize; 424 secno = bp->bio_offset / sc->sectorsize; 425 dst = bp->bio_data; 426 error = 0; 427 while (nsec--) { 428 osp = s_read(sc->indir, secno); 429 if (bp->bio_cmd == BIO_DELETE) { 430 if (osp != 0) 431 error = s_write(sc->indir, secno, 0); 432 } else if (bp->bio_cmd == BIO_READ) { 433 if (osp == 0) 434 bzero(dst, sc->sectorsize); 435 else if (osp <= 255) 436 for (i = 0; i < sc->sectorsize; i++) 437 dst[i] = osp; 438 else 439 bcopy((void *)osp, dst, sc->sectorsize); 440 osp = 0; 441 } else if (bp->bio_cmd == BIO_WRITE) { 442 if (sc->flags & MD_COMPRESS) { 443 uc = dst[0]; 444 for (i = 1; i < sc->sectorsize; i++) 445 if (dst[i] != uc) 446 break; 447 } else { 448 i = 0; 449 uc = 0; 450 } 451 if (i == sc->sectorsize) { 452 if (osp != uc) 453 error = s_write(sc->indir, secno, uc); 454 } else { 455 if (osp <= 255) { 456 sp = (uintptr_t)uma_zalloc(sc->uma, 457 M_NOWAIT); 458 if (sp == 0) { 459 error = ENOSPC; 460 break; 461 } 462 bcopy(dst, (void *)sp, sc->sectorsize); 463 error = s_write(sc->indir, secno, sp); 464 } else { 465 bcopy(dst, (void *)osp, sc->sectorsize); 466 osp = 0; 467 } 468 } 469 } else { 470 error = EOPNOTSUPP; 471 } 472 if (osp > 255) 473 uma_zfree(sc->uma, (void*)osp); 474 if (error != 0) 475 break; 476 secno++; 477 dst += sc->sectorsize; 478 } 479 bp->bio_resid = 0; 480 return (error); 481 } 482 483 static int 484 mdstart_preload(struct md_s *sc, struct bio *bp) 485 { 486 487 switch (bp->bio_cmd) { 488 case BIO_READ: 489 bcopy(sc->pl_ptr + bp->bio_offset, bp->bio_data, 490 bp->bio_length); 491 break; 492 case BIO_WRITE: 493 bcopy(bp->bio_data, sc->pl_ptr + bp->bio_offset, 494 bp->bio_length); 495 break; 496 } 497 bp->bio_resid = 0; 498 return (0); 499 } 500 501 static int 502 mdstart_vnode(struct md_s *sc, struct bio *bp) 503 { 504 int error, vfslocked; 505 struct uio auio; 506 struct iovec aiov; 507 struct mount *mp; 508 struct vnode *vp; 509 struct thread *td; 510 511 switch (bp->bio_cmd) { 512 case BIO_READ: 513 case BIO_WRITE: 514 case BIO_FLUSH: 515 break; 516 default: 517 return (EOPNOTSUPP); 518 } 519 520 td = curthread; 521 vp = sc->vnode; 522 523 /* 524 * VNODE I/O 525 * 526 * If an error occurs, we set BIO_ERROR but we do not set 527 * B_INVAL because (for a write anyway), the buffer is 528 * still valid. 529 */ 530 531 if (bp->bio_cmd == BIO_FLUSH) { 532 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 533 (void) vn_start_write(vp, &mp, V_WAIT); 534 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 535 error = VOP_FSYNC(vp, MNT_WAIT, td); 536 VOP_UNLOCK(vp, 0); 537 vn_finished_write(mp); 538 VFS_UNLOCK_GIANT(vfslocked); 539 return (error); 540 } 541 542 bzero(&auio, sizeof(auio)); 543 544 aiov.iov_base = bp->bio_data; 545 aiov.iov_len = bp->bio_length; 546 auio.uio_iov = &aiov; 547 auio.uio_iovcnt = 1; 548 auio.uio_offset = (vm_ooffset_t)bp->bio_offset; 549 auio.uio_segflg = UIO_SYSSPACE; 550 if (bp->bio_cmd == BIO_READ) 551 auio.uio_rw = UIO_READ; 552 else if (bp->bio_cmd == BIO_WRITE) 553 auio.uio_rw = UIO_WRITE; 554 else 555 panic("wrong BIO_OP in mdstart_vnode"); 556 auio.uio_resid = bp->bio_length; 557 auio.uio_td = td; 558 /* 559 * When reading set IO_DIRECT to try to avoid double-caching 560 * the data. When writing IO_DIRECT is not optimal. 561 */ 562 vfslocked = VFS_LOCK_GIANT(vp->v_mount); 563 if (bp->bio_cmd == BIO_READ) { 564 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 565 error = VOP_READ(vp, &auio, IO_DIRECT, sc->cred); 566 VOP_UNLOCK(vp, 0); 567 } else { 568 (void) vn_start_write(vp, &mp, V_WAIT); 569 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 570 error = VOP_WRITE(vp, &auio, sc->flags & MD_ASYNC ? 0 : IO_SYNC, 571 sc->cred); 572 VOP_UNLOCK(vp, 0); 573 vn_finished_write(mp); 574 } 575 VFS_UNLOCK_GIANT(vfslocked); 576 bp->bio_resid = auio.uio_resid; 577 return (error); 578 } 579 580 static int 581 mdstart_swap(struct md_s *sc, struct bio *bp) 582 { 583 struct sf_buf *sf; 584 int rv, offs, len, lastend; 585 vm_pindex_t i, lastp; 586 vm_page_t m; 587 u_char *p; 588 589 switch (bp->bio_cmd) { 590 case BIO_READ: 591 case BIO_WRITE: 592 case BIO_DELETE: 593 break; 594 default: 595 return (EOPNOTSUPP); 596 } 597 598 p = bp->bio_data; 599 600 /* 601 * offs is the offset at which to start operating on the 602 * next (ie, first) page. lastp is the last page on 603 * which we're going to operate. lastend is the ending 604 * position within that last page (ie, PAGE_SIZE if 605 * we're operating on complete aligned pages). 606 */ 607 offs = bp->bio_offset % PAGE_SIZE; 608 lastp = (bp->bio_offset + bp->bio_length - 1) / PAGE_SIZE; 609 lastend = (bp->bio_offset + bp->bio_length - 1) % PAGE_SIZE + 1; 610 611 rv = VM_PAGER_OK; 612 VM_OBJECT_LOCK(sc->object); 613 vm_object_pip_add(sc->object, 1); 614 for (i = bp->bio_offset / PAGE_SIZE; i <= lastp; i++) { 615 len = ((i == lastp) ? lastend : PAGE_SIZE) - offs; 616 617 m = vm_page_grab(sc->object, i, 618 VM_ALLOC_NORMAL|VM_ALLOC_RETRY); 619 VM_OBJECT_UNLOCK(sc->object); 620 sched_pin(); 621 sf = sf_buf_alloc(m, SFB_CPUPRIVATE); 622 VM_OBJECT_LOCK(sc->object); 623 if (bp->bio_cmd == BIO_READ) { 624 if (m->valid != VM_PAGE_BITS_ALL) 625 rv = vm_pager_get_pages(sc->object, &m, 1, 0); 626 if (rv == VM_PAGER_ERROR) { 627 sf_buf_free(sf); 628 sched_unpin(); 629 vm_page_lock_queues(); 630 vm_page_wakeup(m); 631 vm_page_unlock_queues(); 632 break; 633 } 634 bcopy((void *)(sf_buf_kva(sf) + offs), p, len); 635 } else if (bp->bio_cmd == BIO_WRITE) { 636 if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL) 637 rv = vm_pager_get_pages(sc->object, &m, 1, 0); 638 if (rv == VM_PAGER_ERROR) { 639 sf_buf_free(sf); 640 sched_unpin(); 641 vm_page_lock_queues(); 642 vm_page_wakeup(m); 643 vm_page_unlock_queues(); 644 break; 645 } 646 bcopy(p, (void *)(sf_buf_kva(sf) + offs), len); 647 m->valid = VM_PAGE_BITS_ALL; 648 #if 0 649 } else if (bp->bio_cmd == BIO_DELETE) { 650 if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL) 651 rv = vm_pager_get_pages(sc->object, &m, 1, 0); 652 if (rv == VM_PAGER_ERROR) { 653 sf_buf_free(sf); 654 sched_unpin(); 655 vm_page_lock_queues(); 656 vm_page_wakeup(m); 657 vm_page_unlock_queues(); 658 break; 659 } 660 bzero((void *)(sf_buf_kva(sf) + offs), len); 661 vm_page_dirty(m); 662 m->valid = VM_PAGE_BITS_ALL; 663 #endif 664 } 665 sf_buf_free(sf); 666 sched_unpin(); 667 vm_page_lock_queues(); 668 vm_page_wakeup(m); 669 vm_page_activate(m); 670 if (bp->bio_cmd == BIO_WRITE) 671 vm_page_dirty(m); 672 vm_page_unlock_queues(); 673 674 /* Actions on further pages start at offset 0 */ 675 p += PAGE_SIZE - offs; 676 offs = 0; 677 #if 0 678 if (bootverbose || bp->bio_offset / PAGE_SIZE < 17) 679 printf("wire_count %d busy %d flags %x hold_count %d act_count %d queue %d valid %d dirty %d @ %d\n", 680 m->wire_count, m->busy, 681 m->flags, m->hold_count, m->act_count, m->queue, m->valid, m->dirty, i); 682 #endif 683 } 684 vm_object_pip_subtract(sc->object, 1); 685 vm_object_set_writeable_dirty(sc->object); 686 VM_OBJECT_UNLOCK(sc->object); 687 return (rv != VM_PAGER_ERROR ? 0 : ENOSPC); 688 } 689 690 static void 691 md_kthread(void *arg) 692 { 693 struct md_s *sc; 694 struct bio *bp; 695 int error; 696 697 sc = arg; 698 thread_lock(curthread); 699 sched_prio(curthread, PRIBIO); 700 thread_unlock(curthread); 701 if (sc->type == MD_VNODE) 702 curthread->td_pflags |= TDP_NORUNNINGBUF; 703 704 for (;;) { 705 mtx_lock(&sc->queue_mtx); 706 if (sc->flags & MD_SHUTDOWN) { 707 sc->flags |= MD_EXITING; 708 mtx_unlock(&sc->queue_mtx); 709 kproc_exit(0); 710 } 711 bp = bioq_takefirst(&sc->bio_queue); 712 if (!bp) { 713 msleep(sc, &sc->queue_mtx, PRIBIO | PDROP, "mdwait", 0); 714 continue; 715 } 716 mtx_unlock(&sc->queue_mtx); 717 if (bp->bio_cmd == BIO_GETATTR) { 718 if (sc->fwsectors && sc->fwheads && 719 (g_handleattr_int(bp, "GEOM::fwsectors", 720 sc->fwsectors) || 721 g_handleattr_int(bp, "GEOM::fwheads", 722 sc->fwheads))) 723 error = -1; 724 else 725 error = EOPNOTSUPP; 726 } else { 727 error = sc->start(sc, bp); 728 } 729 730 if (error != -1) { 731 bp->bio_completed = bp->bio_length; 732 g_io_deliver(bp, error); 733 if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE)) 734 devstat_end_transaction_bio(sc->devstat, bp); 735 } 736 } 737 } 738 739 static struct md_s * 740 mdfind(int unit) 741 { 742 struct md_s *sc; 743 744 LIST_FOREACH(sc, &md_softc_list, list) { 745 if (sc->unit == unit) 746 break; 747 } 748 return (sc); 749 } 750 751 static struct md_s * 752 mdnew(int unit, int *errp, enum md_types type) 753 { 754 struct md_s *sc, *sc2; 755 int error, max = -1; 756 757 *errp = 0; 758 LIST_FOREACH(sc2, &md_softc_list, list) { 759 if (unit == sc2->unit) { 760 *errp = EBUSY; 761 return (NULL); 762 } 763 if (unit == -1 && sc2->unit > max) 764 max = sc2->unit; 765 } 766 if (unit == -1) 767 unit = max + 1; 768 sc = (struct md_s *)malloc(sizeof *sc, M_MD, M_WAITOK | M_ZERO); 769 sc->type = type; 770 bioq_init(&sc->bio_queue); 771 mtx_init(&sc->queue_mtx, "md bio queue", NULL, MTX_DEF); 772 sc->unit = unit; 773 sprintf(sc->name, "md%d", unit); 774 LIST_INSERT_HEAD(&md_softc_list, sc, list); 775 error = kproc_create(md_kthread, sc, &sc->procp, 0, 0,"%s", sc->name); 776 if (error == 0) 777 return (sc); 778 LIST_REMOVE(sc, list); 779 mtx_destroy(&sc->queue_mtx); 780 free(sc, M_MD); 781 *errp = error; 782 return (NULL); 783 } 784 785 static void 786 mdinit(struct md_s *sc) 787 { 788 789 struct g_geom *gp; 790 struct g_provider *pp; 791 792 g_topology_lock(); 793 gp = g_new_geomf(&g_md_class, "md%d", sc->unit); 794 gp->softc = sc; 795 pp = g_new_providerf(gp, "md%d", sc->unit); 796 pp->mediasize = sc->mediasize; 797 pp->sectorsize = sc->sectorsize; 798 sc->gp = gp; 799 sc->pp = pp; 800 g_error_provider(pp, 0); 801 g_topology_unlock(); 802 sc->devstat = devstat_new_entry("md", sc->unit, sc->sectorsize, 803 DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 804 } 805 806 /* 807 * XXX: we should check that the range they feed us is mapped. 808 * XXX: we should implement read-only. 809 */ 810 811 static int 812 mdcreate_preload(struct md_s *sc, struct md_ioctl *mdio) 813 { 814 815 if (mdio->md_options & ~(MD_AUTOUNIT | MD_FORCE)) 816 return (EINVAL); 817 sc->flags = mdio->md_options & MD_FORCE; 818 /* Cast to pointer size, then to pointer to avoid warning */ 819 sc->pl_ptr = (u_char *)(uintptr_t)mdio->md_base; 820 sc->pl_len = (size_t)sc->mediasize; 821 return (0); 822 } 823 824 825 static int 826 mdcreate_malloc(struct md_s *sc, struct md_ioctl *mdio) 827 { 828 uintptr_t sp; 829 int error; 830 off_t u; 831 832 error = 0; 833 if (mdio->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE)) 834 return (EINVAL); 835 if (mdio->md_sectorsize != 0 && !powerof2(mdio->md_sectorsize)) 836 return (EINVAL); 837 /* Compression doesn't make sense if we have reserved space */ 838 if (mdio->md_options & MD_RESERVE) 839 mdio->md_options &= ~MD_COMPRESS; 840 if (mdio->md_fwsectors != 0) 841 sc->fwsectors = mdio->md_fwsectors; 842 if (mdio->md_fwheads != 0) 843 sc->fwheads = mdio->md_fwheads; 844 sc->flags = mdio->md_options & (MD_COMPRESS | MD_FORCE); 845 sc->indir = dimension(sc->mediasize / sc->sectorsize); 846 sc->uma = uma_zcreate(sc->name, sc->sectorsize, NULL, NULL, NULL, NULL, 847 0x1ff, 0); 848 if (mdio->md_options & MD_RESERVE) { 849 off_t nsectors; 850 851 nsectors = sc->mediasize / sc->sectorsize; 852 for (u = 0; u < nsectors; u++) { 853 sp = (uintptr_t)uma_zalloc(sc->uma, M_NOWAIT | M_ZERO); 854 if (sp != 0) 855 error = s_write(sc->indir, u, sp); 856 else 857 error = ENOMEM; 858 if (error != 0) 859 break; 860 } 861 } 862 return (error); 863 } 864 865 866 static int 867 mdsetcred(struct md_s *sc, struct ucred *cred) 868 { 869 char *tmpbuf; 870 int error = 0; 871 872 /* 873 * Set credits in our softc 874 */ 875 876 if (sc->cred) 877 crfree(sc->cred); 878 sc->cred = crhold(cred); 879 880 /* 881 * Horrible kludge to establish credentials for NFS XXX. 882 */ 883 884 if (sc->vnode) { 885 struct uio auio; 886 struct iovec aiov; 887 888 tmpbuf = malloc(sc->sectorsize, M_TEMP, M_WAITOK); 889 bzero(&auio, sizeof(auio)); 890 891 aiov.iov_base = tmpbuf; 892 aiov.iov_len = sc->sectorsize; 893 auio.uio_iov = &aiov; 894 auio.uio_iovcnt = 1; 895 auio.uio_offset = 0; 896 auio.uio_rw = UIO_READ; 897 auio.uio_segflg = UIO_SYSSPACE; 898 auio.uio_resid = aiov.iov_len; 899 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY); 900 error = VOP_READ(sc->vnode, &auio, 0, sc->cred); 901 VOP_UNLOCK(sc->vnode, 0); 902 free(tmpbuf, M_TEMP); 903 } 904 return (error); 905 } 906 907 static int 908 mdcreate_vnode(struct md_s *sc, struct md_ioctl *mdio, struct thread *td) 909 { 910 struct vattr vattr; 911 struct nameidata nd; 912 int error, flags, vfslocked; 913 914 error = copyinstr(mdio->md_file, sc->file, sizeof(sc->file), NULL); 915 if (error != 0) 916 return (error); 917 flags = FREAD|FWRITE; 918 /* 919 * If the user specified that this is a read only device, unset the 920 * FWRITE mask before trying to open the backing store. 921 */ 922 if ((mdio->md_options & MD_READONLY) != 0) 923 flags &= ~FWRITE; 924 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, sc->file, td); 925 error = vn_open(&nd, &flags, 0, NULL); 926 if (error != 0) 927 return (error); 928 vfslocked = NDHASGIANT(&nd); 929 NDFREE(&nd, NDF_ONLY_PNBUF); 930 if (nd.ni_vp->v_type != VREG || 931 (error = VOP_GETATTR(nd.ni_vp, &vattr, td->td_ucred))) { 932 VOP_UNLOCK(nd.ni_vp, 0); 933 (void)vn_close(nd.ni_vp, flags, td->td_ucred, td); 934 VFS_UNLOCK_GIANT(vfslocked); 935 return (error ? error : EINVAL); 936 } 937 nd.ni_vp->v_vflag |= VV_MD; 938 VOP_UNLOCK(nd.ni_vp, 0); 939 940 if (mdio->md_fwsectors != 0) 941 sc->fwsectors = mdio->md_fwsectors; 942 if (mdio->md_fwheads != 0) 943 sc->fwheads = mdio->md_fwheads; 944 sc->flags = mdio->md_options & (MD_FORCE | MD_ASYNC); 945 if (!(flags & FWRITE)) 946 sc->flags |= MD_READONLY; 947 sc->vnode = nd.ni_vp; 948 949 error = mdsetcred(sc, td->td_ucred); 950 if (error != 0) { 951 sc->vnode = NULL; 952 vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY); 953 nd.ni_vp->v_vflag &= ~VV_MD; 954 VOP_UNLOCK(nd.ni_vp, 0); 955 (void)vn_close(nd.ni_vp, flags, td->td_ucred, td); 956 VFS_UNLOCK_GIANT(vfslocked); 957 return (error); 958 } 959 VFS_UNLOCK_GIANT(vfslocked); 960 return (0); 961 } 962 963 static int 964 mddestroy(struct md_s *sc, struct thread *td) 965 { 966 int vfslocked; 967 968 if (sc->gp) { 969 sc->gp->softc = NULL; 970 g_topology_lock(); 971 g_wither_geom(sc->gp, ENXIO); 972 g_topology_unlock(); 973 sc->gp = NULL; 974 sc->pp = NULL; 975 } 976 if (sc->devstat) { 977 devstat_remove_entry(sc->devstat); 978 sc->devstat = NULL; 979 } 980 mtx_lock(&sc->queue_mtx); 981 sc->flags |= MD_SHUTDOWN; 982 wakeup(sc); 983 while (!(sc->flags & MD_EXITING)) 984 msleep(sc->procp, &sc->queue_mtx, PRIBIO, "mddestroy", hz / 10); 985 mtx_unlock(&sc->queue_mtx); 986 mtx_destroy(&sc->queue_mtx); 987 if (sc->vnode != NULL) { 988 vfslocked = VFS_LOCK_GIANT(sc->vnode->v_mount); 989 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY); 990 sc->vnode->v_vflag &= ~VV_MD; 991 VOP_UNLOCK(sc->vnode, 0); 992 (void)vn_close(sc->vnode, sc->flags & MD_READONLY ? 993 FREAD : (FREAD|FWRITE), sc->cred, td); 994 VFS_UNLOCK_GIANT(vfslocked); 995 } 996 if (sc->cred != NULL) 997 crfree(sc->cred); 998 if (sc->object != NULL) 999 vm_object_deallocate(sc->object); 1000 if (sc->indir) 1001 destroy_indir(sc, sc->indir); 1002 if (sc->uma) 1003 uma_zdestroy(sc->uma); 1004 1005 LIST_REMOVE(sc, list); 1006 free(sc, M_MD); 1007 return (0); 1008 } 1009 1010 static int 1011 mdcreate_swap(struct md_s *sc, struct md_ioctl *mdio, struct thread *td) 1012 { 1013 vm_ooffset_t npage; 1014 int error; 1015 1016 /* 1017 * Range check. Disallow negative sizes or any size less then the 1018 * size of a page. Then round to a page. 1019 */ 1020 if (sc->mediasize == 0 || (sc->mediasize % PAGE_SIZE) != 0) 1021 return (EDOM); 1022 1023 /* 1024 * Allocate an OBJT_SWAP object. 1025 * 1026 * Note the truncation. 1027 */ 1028 1029 npage = mdio->md_mediasize / PAGE_SIZE; 1030 if (mdio->md_fwsectors != 0) 1031 sc->fwsectors = mdio->md_fwsectors; 1032 if (mdio->md_fwheads != 0) 1033 sc->fwheads = mdio->md_fwheads; 1034 sc->object = vm_pager_allocate(OBJT_SWAP, NULL, PAGE_SIZE * npage, 1035 VM_PROT_DEFAULT, 0); 1036 if (sc->object == NULL) 1037 return (ENOMEM); 1038 sc->flags = mdio->md_options & MD_FORCE; 1039 if (mdio->md_options & MD_RESERVE) { 1040 if (swap_pager_reserve(sc->object, 0, npage) < 0) { 1041 vm_object_deallocate(sc->object); 1042 sc->object = NULL; 1043 return (EDOM); 1044 } 1045 } 1046 error = mdsetcred(sc, td->td_ucred); 1047 if (error != 0) { 1048 vm_object_deallocate(sc->object); 1049 sc->object = NULL; 1050 } 1051 return (error); 1052 } 1053 1054 1055 static int 1056 xmdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td) 1057 { 1058 struct md_ioctl *mdio; 1059 struct md_s *sc; 1060 int error, i; 1061 1062 if (md_debug) 1063 printf("mdctlioctl(%s %lx %p %x %p)\n", 1064 devtoname(dev), cmd, addr, flags, td); 1065 1066 mdio = (struct md_ioctl *)addr; 1067 if (mdio->md_version != MDIOVERSION) 1068 return (EINVAL); 1069 1070 /* 1071 * We assert the version number in the individual ioctl 1072 * handlers instead of out here because (a) it is possible we 1073 * may add another ioctl in the future which doesn't read an 1074 * mdio, and (b) the correct return value for an unknown ioctl 1075 * is ENOIOCTL, not EINVAL. 1076 */ 1077 error = 0; 1078 switch (cmd) { 1079 case MDIOCATTACH: 1080 switch (mdio->md_type) { 1081 case MD_MALLOC: 1082 case MD_PRELOAD: 1083 case MD_VNODE: 1084 case MD_SWAP: 1085 break; 1086 default: 1087 return (EINVAL); 1088 } 1089 if (mdio->md_options & MD_AUTOUNIT) 1090 sc = mdnew(-1, &error, mdio->md_type); 1091 else 1092 sc = mdnew(mdio->md_unit, &error, mdio->md_type); 1093 if (sc == NULL) 1094 return (error); 1095 if (mdio->md_options & MD_AUTOUNIT) 1096 mdio->md_unit = sc->unit; 1097 sc->mediasize = mdio->md_mediasize; 1098 if (mdio->md_sectorsize == 0) 1099 sc->sectorsize = DEV_BSIZE; 1100 else 1101 sc->sectorsize = mdio->md_sectorsize; 1102 error = EDOOFUS; 1103 switch (sc->type) { 1104 case MD_MALLOC: 1105 sc->start = mdstart_malloc; 1106 error = mdcreate_malloc(sc, mdio); 1107 break; 1108 case MD_PRELOAD: 1109 sc->start = mdstart_preload; 1110 error = mdcreate_preload(sc, mdio); 1111 break; 1112 case MD_VNODE: 1113 sc->start = mdstart_vnode; 1114 error = mdcreate_vnode(sc, mdio, td); 1115 break; 1116 case MD_SWAP: 1117 sc->start = mdstart_swap; 1118 error = mdcreate_swap(sc, mdio, td); 1119 break; 1120 } 1121 if (error != 0) { 1122 mddestroy(sc, td); 1123 return (error); 1124 } 1125 1126 /* Prune off any residual fractional sector */ 1127 i = sc->mediasize % sc->sectorsize; 1128 sc->mediasize -= i; 1129 1130 mdinit(sc); 1131 return (0); 1132 case MDIOCDETACH: 1133 if (mdio->md_mediasize != 0 || mdio->md_options != 0) 1134 return (EINVAL); 1135 1136 sc = mdfind(mdio->md_unit); 1137 if (sc == NULL) 1138 return (ENOENT); 1139 if (sc->opencount != 0 && !(sc->flags & MD_FORCE)) 1140 return (EBUSY); 1141 return (mddestroy(sc, td)); 1142 case MDIOCQUERY: 1143 sc = mdfind(mdio->md_unit); 1144 if (sc == NULL) 1145 return (ENOENT); 1146 mdio->md_type = sc->type; 1147 mdio->md_options = sc->flags; 1148 mdio->md_mediasize = sc->mediasize; 1149 mdio->md_sectorsize = sc->sectorsize; 1150 if (sc->type == MD_VNODE) 1151 error = copyout(sc->file, mdio->md_file, 1152 strlen(sc->file) + 1); 1153 return (error); 1154 case MDIOCLIST: 1155 i = 1; 1156 LIST_FOREACH(sc, &md_softc_list, list) { 1157 if (i == MDNPAD - 1) 1158 mdio->md_pad[i] = -1; 1159 else 1160 mdio->md_pad[i++] = sc->unit; 1161 } 1162 mdio->md_pad[0] = i - 1; 1163 return (0); 1164 default: 1165 return (ENOIOCTL); 1166 }; 1167 } 1168 1169 static int 1170 mdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td) 1171 { 1172 int error; 1173 1174 sx_xlock(&md_sx); 1175 error = xmdctlioctl(dev, cmd, addr, flags, td); 1176 sx_xunlock(&md_sx); 1177 return (error); 1178 } 1179 1180 static void 1181 md_preloaded(u_char *image, size_t length) 1182 { 1183 struct md_s *sc; 1184 int error; 1185 1186 sc = mdnew(-1, &error, MD_PRELOAD); 1187 if (sc == NULL) 1188 return; 1189 sc->mediasize = length; 1190 sc->sectorsize = DEV_BSIZE; 1191 sc->pl_ptr = image; 1192 sc->pl_len = length; 1193 sc->start = mdstart_preload; 1194 #ifdef MD_ROOT 1195 if (sc->unit == 0) 1196 rootdevnames[0] = "ufs:/dev/md0"; 1197 #endif 1198 mdinit(sc); 1199 } 1200 1201 static void 1202 g_md_init(struct g_class *mp __unused) 1203 { 1204 1205 caddr_t mod; 1206 caddr_t c; 1207 u_char *ptr, *name, *type; 1208 unsigned len; 1209 1210 mod = NULL; 1211 sx_init(&md_sx, "MD config lock"); 1212 g_topology_unlock(); 1213 #ifdef MD_ROOT_SIZE 1214 sx_xlock(&md_sx); 1215 md_preloaded(mfs_root.start, sizeof(mfs_root.start)); 1216 sx_xunlock(&md_sx); 1217 #endif 1218 /* XXX: are preload_* static or do they need Giant ? */ 1219 while ((mod = preload_search_next_name(mod)) != NULL) { 1220 name = (char *)preload_search_info(mod, MODINFO_NAME); 1221 if (name == NULL) 1222 continue; 1223 type = (char *)preload_search_info(mod, MODINFO_TYPE); 1224 if (type == NULL) 1225 continue; 1226 if (strcmp(type, "md_image") && strcmp(type, "mfs_root")) 1227 continue; 1228 c = preload_search_info(mod, MODINFO_ADDR); 1229 ptr = *(u_char **)c; 1230 c = preload_search_info(mod, MODINFO_SIZE); 1231 len = *(size_t *)c; 1232 printf("%s%d: Preloaded image <%s> %d bytes at %p\n", 1233 MD_NAME, mdunits, name, len, ptr); 1234 sx_xlock(&md_sx); 1235 md_preloaded(ptr, len); 1236 sx_xunlock(&md_sx); 1237 } 1238 status_dev = make_dev(&mdctl_cdevsw, INT_MAX, UID_ROOT, GID_WHEEL, 1239 0600, MDCTL_NAME); 1240 g_topology_lock(); 1241 } 1242 1243 static void 1244 g_md_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 1245 struct g_consumer *cp __unused, struct g_provider *pp) 1246 { 1247 struct md_s *mp; 1248 char *type; 1249 1250 mp = gp->softc; 1251 if (mp == NULL) 1252 return; 1253 1254 switch (mp->type) { 1255 case MD_MALLOC: 1256 type = "malloc"; 1257 break; 1258 case MD_PRELOAD: 1259 type = "preload"; 1260 break; 1261 case MD_VNODE: 1262 type = "vnode"; 1263 break; 1264 case MD_SWAP: 1265 type = "swap"; 1266 break; 1267 default: 1268 type = "unknown"; 1269 break; 1270 } 1271 1272 if (pp != NULL) { 1273 if (indent == NULL) { 1274 sbuf_printf(sb, " u %d", mp->unit); 1275 sbuf_printf(sb, " s %ju", (uintmax_t) mp->sectorsize); 1276 sbuf_printf(sb, " f %ju", (uintmax_t) mp->fwheads); 1277 sbuf_printf(sb, " fs %ju", (uintmax_t) mp->fwsectors); 1278 sbuf_printf(sb, " l %ju", (uintmax_t) mp->mediasize); 1279 sbuf_printf(sb, " t %s", type); 1280 if (mp->type == MD_VNODE && mp->vnode != NULL) 1281 sbuf_printf(sb, " file %s", mp->file); 1282 } else { 1283 sbuf_printf(sb, "%s<unit>%d</unit>\n", indent, 1284 mp->unit); 1285 sbuf_printf(sb, "%s<sectorsize>%ju</sectorsize>\n", 1286 indent, (uintmax_t) mp->sectorsize); 1287 sbuf_printf(sb, "%s<fwheads>%ju</fwheads>\n", 1288 indent, (uintmax_t) mp->fwheads); 1289 sbuf_printf(sb, "%s<fwsectors>%ju</fwsectors>\n", 1290 indent, (uintmax_t) mp->fwsectors); 1291 sbuf_printf(sb, "%s<length>%ju</length>\n", 1292 indent, (uintmax_t) mp->mediasize); 1293 sbuf_printf(sb, "%s<type>%s</type>\n", indent, 1294 type); 1295 if (mp->type == MD_VNODE && mp->vnode != NULL) 1296 sbuf_printf(sb, "%s<file>%s</file>\n", 1297 indent, mp->file); 1298 } 1299 } 1300 } 1301 1302 static void 1303 g_md_fini(struct g_class *mp __unused) 1304 { 1305 1306 sx_destroy(&md_sx); 1307 if (status_dev != NULL) 1308 destroy_dev(status_dev); 1309 } 1310