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.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 /*      $NetBSD: md.c,v 1.56.6.1 2009/05/11 20:07:08 bouyer Exp $       */
    2 
    3 /*
    4  * Copyright (c) 1995 Gordon W. Ross, Leo Weppelman.
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. The name of the author may not be used to endorse or promote products
   16  *    derived from this software without specific prior written permission.
   17  * 4. All advertising materials mentioning features or use of this software
   18  *    must display the following acknowledgement:
   19  *      This product includes software developed by
   20  *                      Gordon W. Ross and Leo Weppelman.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   32  */
   33 
   34 /*
   35  * This implements a general-purpose memory-disk.
   36  * See md.h for notes on the config types.
   37  *
   38  * Note that this driver provides the same functionality
   39  * as the MFS filesystem hack, but this is better because
   40  * you can use this for any filesystem type you'd like!
   41  *
   42  * Credit for most of the kmem ramdisk code goes to:
   43  *   Leo Weppelman (atari) and Phil Nelson (pc532)
   44  * Credit for the ideas behind the "user space memory" code goes
   45  * to the authors of the MFS implementation.
   46  */
   47 
   48 #include <sys/cdefs.h>
   49 __KERNEL_RCSID(0, "$NetBSD: md.c,v 1.56.6.1 2009/05/11 20:07:08 bouyer Exp $");
   50 
   51 #include "opt_md.h"
   52 #include "opt_tftproot.h"
   53 
   54 #include <sys/param.h>
   55 #include <sys/kernel.h>
   56 #include <sys/malloc.h>
   57 #include <sys/systm.h>
   58 #include <sys/buf.h>
   59 #include <sys/bufq.h>
   60 #include <sys/device.h>
   61 #include <sys/disk.h>
   62 #include <sys/proc.h>
   63 #include <sys/conf.h>
   64 #include <sys/disklabel.h>
   65 
   66 #include <uvm/uvm_extern.h>
   67 
   68 #include <dev/md.h>
   69 
   70 /*
   71  * The user-space functionality is included by default.
   72  * Use  `options MEMORY_DISK_SERVER=0' to turn it off.
   73  */
   74 #ifndef MEMORY_DISK_SERVER
   75 #error MEMORY_DISK_SERVER should be defined by opt_md.h
   76 #endif  /* MEMORY_DISK_SERVER */
   77 
   78 /*
   79  * We should use the raw partition for ioctl.
   80  */
   81 #define MD_UNIT(unit)   DISKUNIT(unit)
   82 
   83 /* autoconfig stuff... */
   84 
   85 struct md_softc {
   86         struct disk sc_dkdev;   /* hook for generic disk handling */
   87         struct md_conf sc_md;
   88         struct bufq_state *sc_buflist;
   89 };
   90 /* shorthand for fields in sc_md: */
   91 #define sc_addr sc_md.md_addr
   92 #define sc_size sc_md.md_size
   93 #define sc_type sc_md.md_type
   94 
   95 void    mdattach(int);
   96 
   97 static void     md_attach(device_t, device_t, void *);
   98 
   99 static dev_type_open(mdopen);
  100 static dev_type_close(mdclose);
  101 static dev_type_read(mdread);
  102 static dev_type_write(mdwrite);
  103 static dev_type_ioctl(mdioctl);
  104 static dev_type_strategy(mdstrategy);
  105 static dev_type_size(mdsize);
  106 
  107 const struct bdevsw md_bdevsw = {
  108         mdopen, mdclose, mdstrategy, mdioctl, nodump, mdsize, D_DISK
  109 };
  110 
  111 const struct cdevsw md_cdevsw = {
  112         mdopen, mdclose, mdread, mdwrite, mdioctl,
  113         nostop, notty, nopoll, nommap, nokqfilter, D_DISK
  114 };
  115 
  116 static struct dkdriver mddkdriver = { mdstrategy, NULL };
  117 
  118 extern struct cfdriver md_cd;
  119 CFATTACH_DECL_NEW(md, sizeof(struct md_softc),
  120         0, md_attach, 0, NULL);
  121 
  122 extern size_t md_root_size;
  123 
  124 /*
  125  * This is called if we are configured as a pseudo-device
  126  */
  127 void
  128 mdattach(int n)
  129 {
  130         int i;
  131         cfdata_t cf;
  132 
  133 #ifdef TFTPROOT
  134         /* 
  135          * Attachement of md0 must be done after md_root_setconf(), 
  136          * because the RAMdisk is not loaded yet.
  137          */
  138         if (md_root_size == 0)
  139                 return;
  140 #endif
  141         if (config_cfattach_attach("md", &md_ca)) {
  142                 printf("md: cfattach_attach failed\n");
  143                 return;
  144         }
  145 
  146         /* XXX:  Are we supposed to provide a default? */
  147         if (n <= 1)
  148                 n = 1;
  149 
  150         /* Attach as if by autoconfig. */
  151         for (i = 0; i < n; i++) {
  152                 cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK);
  153                 cf->cf_name = "md";
  154                 cf->cf_atname = "md";
  155                 cf->cf_unit = i;
  156                 cf->cf_fstate = FSTATE_NOTFOUND;
  157                 (void)config_attach_pseudo(cf);
  158         }
  159 }
  160 
  161 static void
  162 md_attach(device_t parent, device_t self,
  163     void *aux)
  164 {
  165         struct md_softc *sc = device_private(self);
  166 
  167         bufq_alloc(&sc->sc_buflist, "fcfs", 0);
  168 
  169         /* XXX - Could accept aux info here to set the config. */
  170 #ifdef  MEMORY_DISK_HOOKS
  171         /*
  172          * This external function might setup a pre-loaded disk.
  173          * All it would need to do is setup the md_conf struct.
  174          * See sys/dev/md_root.c for an example.
  175          */
  176         md_attach_hook(device_unit(self), &sc->sc_md);
  177 #endif
  178 
  179         /*
  180          * Initialize and attach the disk structure.
  181          */
  182         disk_init(&sc->sc_dkdev, device_xname(self), &mddkdriver);
  183         disk_attach(&sc->sc_dkdev);
  184 
  185         if (!pmf_device_register(self, NULL, NULL))
  186                 aprint_error_dev(self, "couldn't establish power handler\n");
  187 }
  188 
  189 /*
  190  * operational routines:
  191  * open, close, read, write, strategy,
  192  * ioctl, dump, size
  193  */
  194 
  195 #if MEMORY_DISK_SERVER
  196 static int      md_server_loop(struct md_softc *sc);
  197 static int      md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
  198                     struct lwp *l);
  199 #endif  /* MEMORY_DISK_SERVER */
  200 static int      md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
  201                     struct lwp *l);
  202 
  203 static int
  204 mdsize(dev_t dev)
  205 {
  206         struct md_softc *sc;
  207 
  208         sc = device_lookup_private(&md_cd, MD_UNIT(dev));
  209         if (sc == NULL)
  210                 return 0;
  211 
  212         if (sc->sc_type == MD_UNCONFIGURED)
  213                 return 0;
  214 
  215         return (sc->sc_size >> DEV_BSHIFT);
  216 }
  217 
  218 static int
  219 mdopen(dev_t dev, int flag, int fmt, struct lwp *l)
  220 {
  221         int unit;
  222         struct md_softc *sc;
  223 
  224         unit = MD_UNIT(dev);
  225         sc = device_lookup_private(&md_cd, unit);
  226         if (sc == NULL)
  227                 return ENXIO;
  228 
  229         /*
  230          * The raw partition is used for ioctl to configure.
  231          */
  232         if (DISKPART(dev) == RAW_PART)
  233                 return 0;
  234 
  235 #ifdef  MEMORY_DISK_HOOKS
  236         /* Call the open hook to allow loading the device. */
  237         md_open_hook(unit, &sc->sc_md);
  238 #endif
  239 
  240         /*
  241          * This is a normal, "slave" device, so
  242          * enforce initialized.
  243          */
  244         if (sc->sc_type == MD_UNCONFIGURED)
  245                 return ENXIO;
  246 
  247         return 0;
  248 }
  249 
  250 static int
  251 mdclose(dev_t dev, int flag, int fmt, struct lwp *l)
  252 {
  253 
  254         return 0;
  255 }
  256 
  257 static int
  258 mdread(dev_t dev, struct uio *uio, int flags)
  259 {
  260         struct md_softc *sc;
  261 
  262         sc = device_lookup_private(&md_cd, MD_UNIT(dev));
  263 
  264         if (sc->sc_type == MD_UNCONFIGURED)
  265                 return ENXIO;
  266 
  267         return (physio(mdstrategy, NULL, dev, B_READ, minphys, uio));
  268 }
  269 
  270 static int
  271 mdwrite(dev_t dev, struct uio *uio, int flags)
  272 {
  273         struct md_softc *sc;
  274 
  275         sc = device_lookup_private(&md_cd, MD_UNIT(dev));
  276 
  277         if (sc->sc_type == MD_UNCONFIGURED)
  278                 return ENXIO;
  279 
  280         return (physio(mdstrategy, NULL, dev, B_WRITE, minphys, uio));
  281 }
  282 
  283 /*
  284  * Handle I/O requests, either directly, or
  285  * by passing them to the server process.
  286  */
  287 static void
  288 mdstrategy(struct buf *bp)
  289 {
  290         struct md_softc *sc;
  291         void *  addr;
  292         size_t off, xfer;
  293 
  294         sc = device_lookup_private(&md_cd, MD_UNIT(bp->b_dev));
  295 
  296         if (sc->sc_type == MD_UNCONFIGURED) {
  297                 bp->b_error = ENXIO;
  298                 goto done;
  299         }
  300 
  301         switch (sc->sc_type) {
  302 #if MEMORY_DISK_SERVER
  303         case MD_UMEM_SERVER:
  304                 /* Just add this job to the server's queue. */
  305                 BUFQ_PUT(sc->sc_buflist, bp);
  306                 wakeup((void *)sc);
  307                 /* see md_server_loop() */
  308                 /* no biodone in this case */
  309                 return;
  310 #endif  /* MEMORY_DISK_SERVER */
  311 
  312         case MD_KMEM_FIXED:
  313         case MD_KMEM_ALLOCATED:
  314                 /* These are in kernel space.  Access directly. */
  315                 bp->b_resid = bp->b_bcount;
  316                 off = (bp->b_blkno << DEV_BSHIFT);
  317                 if (off >= sc->sc_size) {
  318                         if (bp->b_flags & B_READ)
  319                                 break;  /* EOF */
  320                         goto set_eio;
  321                 }
  322                 xfer = bp->b_resid;
  323                 if (xfer > (sc->sc_size - off))
  324                         xfer = (sc->sc_size - off);
  325                 addr = (char *)sc->sc_addr + off;
  326                 if (bp->b_flags & B_READ)
  327                         memcpy(bp->b_data, addr, xfer);
  328                 else
  329                         memcpy(addr, bp->b_data, xfer);
  330                 bp->b_resid -= xfer;
  331                 break;
  332 
  333         default:
  334                 bp->b_resid = bp->b_bcount;
  335         set_eio:
  336                 bp->b_error = EIO;
  337                 break;
  338         }
  339  done:
  340         biodone(bp);
  341 }
  342 
  343 static int
  344 mdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
  345 {
  346         struct md_softc *sc;
  347         struct md_conf *umd;
  348 
  349         sc = device_lookup_private(&md_cd, MD_UNIT(dev));
  350 
  351         /* If this is not the raw partition, punt! */
  352         if (DISKPART(dev) != RAW_PART)
  353                 return ENOTTY;
  354 
  355         umd = (struct md_conf *)data;
  356         switch (cmd) {
  357         case MD_GETCONF:
  358                 *umd = sc->sc_md;
  359                 return 0;
  360 
  361         case MD_SETCONF:
  362                 /* Can only set it once. */
  363                 if (sc->sc_type != MD_UNCONFIGURED)
  364                         break;
  365                 switch (umd->md_type) {
  366                 case MD_KMEM_ALLOCATED:
  367                         return md_ioctl_kalloc(sc, umd, l);
  368 #if MEMORY_DISK_SERVER
  369                 case MD_UMEM_SERVER:
  370                         return md_ioctl_server(sc, umd, l);
  371 #endif  /* MEMORY_DISK_SERVER */
  372                 default:
  373                         break;
  374                 }
  375                 break;
  376         }
  377         return EINVAL;
  378 }
  379 
  380 /*
  381  * Handle ioctl MD_SETCONF for (sc_type == MD_KMEM_ALLOCATED)
  382  * Just allocate some kernel memory and return.
  383  */
  384 static int
  385 md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
  386     struct lwp *l)
  387 {
  388         vaddr_t addr;
  389         vsize_t size;
  390 
  391         /* Sanity check the size. */
  392         size = umd->md_size;
  393         addr = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_WIRED|UVM_KMF_ZERO);
  394         if (!addr)
  395                 return ENOMEM;
  396 
  397         /* This unit is now configured. */
  398         sc->sc_addr = (void *)addr;     /* kernel space */
  399         sc->sc_size = (size_t)size;
  400         sc->sc_type = MD_KMEM_ALLOCATED;
  401         return 0;
  402 }
  403 
  404 #if MEMORY_DISK_SERVER
  405 
  406 /*
  407  * Handle ioctl MD_SETCONF for (sc_type == MD_UMEM_SERVER)
  408  * Set config, then become the I/O server for this unit.
  409  */
  410 static int
  411 md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
  412     struct lwp *l)
  413 {
  414         vaddr_t end;
  415         int error;
  416 
  417         /* Sanity check addr, size. */
  418         end = (vaddr_t) ((char *)umd->md_addr + umd->md_size);
  419 
  420         if ((end >= VM_MAXUSER_ADDRESS) ||
  421                 (end < ((vaddr_t) umd->md_addr)) )
  422                 return EINVAL;
  423 
  424         /* This unit is now configured. */
  425         sc->sc_addr = umd->md_addr;     /* user space */
  426         sc->sc_size = umd->md_size;
  427         sc->sc_type = MD_UMEM_SERVER;
  428 
  429         /* Become the server daemon */
  430         error = md_server_loop(sc);
  431 
  432         /* This server is now going away! */
  433         sc->sc_type = MD_UNCONFIGURED;
  434         sc->sc_addr = 0;
  435         sc->sc_size = 0;
  436 
  437         return (error);
  438 }
  439 
  440 static int md_sleep_pri = PWAIT | PCATCH;
  441 
  442 static int
  443 md_server_loop(struct md_softc *sc)
  444 {
  445         struct buf *bp;
  446         void *addr;     /* user space address */
  447         size_t off;     /* offset into "device" */
  448         size_t xfer;    /* amount to transfer */
  449         int error;
  450 
  451         for (;;) {
  452                 /* Wait for some work to arrive. */
  453                 while ((bp = BUFQ_GET(sc->sc_buflist)) == NULL) {
  454                         error = tsleep((void *)sc, md_sleep_pri, "md_idle", 0);
  455                         if (error)
  456                                 return error;
  457                 }
  458 
  459                 /* Do the transfer to/from user space. */
  460                 error = 0;
  461                 bp->b_resid = bp->b_bcount;
  462                 off = (bp->b_blkno << DEV_BSHIFT);
  463                 if (off >= sc->sc_size) {
  464                         if (bp->b_flags & B_READ)
  465                                 goto done;      /* EOF (not an error) */
  466                         error = EIO;
  467                         goto done;
  468                 }
  469                 xfer = bp->b_resid;
  470                 if (xfer > (sc->sc_size - off))
  471                         xfer = (sc->sc_size - off);
  472                 addr = (char *)sc->sc_addr + off;
  473                 if (bp->b_flags & B_READ)
  474                         error = copyin(addr, bp->b_data, xfer);
  475                 else
  476                         error = copyout(bp->b_data, addr, xfer);
  477                 if (!error)
  478                         bp->b_resid -= xfer;
  479 
  480         done:
  481                 if (error) {
  482                         bp->b_error = error;
  483                 }
  484                 biodone(bp);
  485         }
  486 }
  487 #endif  /* MEMORY_DISK_SERVER */

Cache object: 902a305d6bc67105e388ef8ac4565fcb


[ 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.