The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/md/md.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 2023f6735a09128a06627fa1b660b068


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.