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.36 2003/06/29 22:30:00 fvdl 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.36 2003/06/29 22:30:00 fvdl Exp $");
   50 
   51 #include "opt_md.h"
   52 
   53 #include <sys/param.h>
   54 #include <sys/kernel.h>
   55 #include <sys/malloc.h>
   56 #include <sys/systm.h>
   57 #include <sys/buf.h>
   58 #include <sys/device.h>
   59 #include <sys/disk.h>
   60 #include <sys/proc.h>
   61 #include <sys/conf.h>
   62 #include <sys/disklabel.h>
   63 
   64 #include <uvm/uvm_extern.h>
   65 
   66 #include <dev/md.h>
   67 
   68 /*
   69  * By default, include the user-space functionality.
   70  * Use  `options MEMORY_DISK_SERVER=0' to turn it off.
   71  */
   72 #ifndef MEMORY_DISK_SERVER
   73 #define MEMORY_DISK_SERVER 1
   74 #endif  /* MEMORY_DISK_SERVER */
   75 
   76 /*
   77  * We should use the raw partition for ioctl.
   78  */
   79 #define MD_MAX_UNITS    0x10
   80 #define MD_UNIT(unit)   DISKUNIT(unit)
   81 
   82 /* autoconfig stuff... */
   83 
   84 struct md_softc {
   85         struct device sc_dev;   /* REQUIRED first entry */
   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 __P((int));
   96 static void md_attach __P((struct device *, struct device *, void *));
   97 
   98 dev_type_open(mdopen);
   99 dev_type_close(mdclose);
  100 dev_type_read(mdread);
  101 dev_type_write(mdwrite);
  102 dev_type_ioctl(mdioctl);
  103 dev_type_strategy(mdstrategy);
  104 dev_type_size(mdsize);
  105 
  106 const struct bdevsw md_bdevsw = {
  107         mdopen, mdclose, mdstrategy, mdioctl, nodump, mdsize, D_DISK
  108 };
  109 
  110 const struct cdevsw md_cdevsw = {
  111         mdopen, mdclose, mdread, mdwrite, mdioctl,
  112         nostop, notty, nopoll, nommap, nokqfilter, D_DISK
  113 };
  114 
  115 struct dkdriver mddkdriver = { mdstrategy };
  116 
  117 static int   ramdisk_ndevs;
  118 static void *ramdisk_devs[MD_MAX_UNITS];
  119 
  120 /*
  121  * This is called if we are configured as a pseudo-device
  122  */
  123 void
  124 mdattach(n)
  125         int n;
  126 {
  127         struct md_softc *sc;
  128         int i;
  129 
  130 #ifdef  DIAGNOSTIC
  131         if (ramdisk_ndevs) {
  132                 aprint_error("ramdisk: multiple attach calls?\n");
  133                 return;
  134         }
  135 #endif
  136 
  137         /* XXX:  Are we supposed to provide a default? */
  138         if (n <= 1)
  139                 n = 1;
  140         if (n > MD_MAX_UNITS)
  141                 n = MD_MAX_UNITS;
  142         ramdisk_ndevs = n;
  143 
  144         /* Attach as if by autoconfig. */
  145         for (i = 0; i < n; i++) {
  146 
  147                 sc = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT|M_ZERO);
  148                 if (!sc) {
  149                         aprint_error("ramdisk: malloc for attach failed!\n");
  150                         return;
  151                 }
  152                 ramdisk_devs[i] = sc;
  153                 sc->sc_dev.dv_unit = i;
  154                 sprintf(sc->sc_dev.dv_xname, "md%d", i);
  155                 md_attach(NULL, &sc->sc_dev, NULL);
  156         }
  157 }
  158 
  159 static void
  160 md_attach(parent, self, aux)
  161         struct device   *parent, *self;
  162         void            *aux;
  163 {
  164         struct md_softc *sc = (struct md_softc *)self;
  165 
  166         bufq_alloc(&sc->sc_buflist, BUFQ_FCFS);
  167 
  168         /* XXX - Could accept aux info here to set the config. */
  169 #ifdef  MEMORY_DISK_HOOKS
  170         /*
  171          * This external function might setup a pre-loaded disk.
  172          * All it would need to do is setup the md_conf struct.
  173          * See sys/dev/md_root.c for an example.
  174          */
  175         md_attach_hook(sc->sc_dev.dv_unit, &sc->sc_md);
  176 #endif
  177 
  178         /*
  179          * Initialize and attach the disk structure.
  180          */
  181         sc->sc_dkdev.dk_driver = &mddkdriver;
  182         sc->sc_dkdev.dk_name = sc->sc_dev.dv_xname;
  183         disk_attach(&sc->sc_dkdev);
  184 }
  185 
  186 /*
  187  * operational routines:
  188  * open, close, read, write, strategy,
  189  * ioctl, dump, size
  190  */
  191 
  192 #if MEMORY_DISK_SERVER
  193 static int md_server_loop __P((struct md_softc *sc));
  194 static int md_ioctl_server __P((struct md_softc *sc,
  195                 struct md_conf *umd, struct proc *proc));
  196 #endif  /* MEMORY_DISK_SERVER */
  197 static int md_ioctl_kalloc __P((struct md_softc *sc,
  198                 struct md_conf *umd, struct proc *proc));
  199 
  200 int
  201 mdsize(dev_t dev)
  202 {
  203         int unit;
  204         struct md_softc *sc;
  205 
  206         unit = MD_UNIT(dev);
  207         if (unit >= ramdisk_ndevs)
  208                 return 0;
  209         sc = ramdisk_devs[unit];
  210         if (sc == NULL)
  211                 return 0;
  212 
  213         if (sc->sc_type == MD_UNCONFIGURED)
  214                 return 0;
  215 
  216         return (sc->sc_size >> DEV_BSHIFT);
  217 }
  218 
  219 int
  220 mdopen(dev, flag, fmt, proc)
  221         dev_t dev;
  222         int flag, fmt;
  223         struct proc *proc;
  224 {
  225         int unit;
  226         struct md_softc *sc;
  227 
  228         unit = MD_UNIT(dev);
  229         if (unit >= ramdisk_ndevs)
  230                 return ENXIO;
  231         sc = ramdisk_devs[unit];
  232         if (sc == NULL)
  233                 return ENXIO;
  234 
  235         /*
  236          * The raw partition is used for ioctl to configure.
  237          */
  238         if (DISKPART(dev) == RAW_PART)
  239                 return 0;
  240 
  241 #ifdef  MEMORY_DISK_HOOKS
  242         /* Call the open hook to allow loading the device. */
  243         md_open_hook(unit, &sc->sc_md);
  244 #endif
  245 
  246         /*
  247          * This is a normal, "slave" device, so
  248          * enforce initialized.
  249          */
  250         if (sc->sc_type == MD_UNCONFIGURED)
  251                 return ENXIO;
  252 
  253         return 0;
  254 }
  255 
  256 int
  257 mdclose(dev, flag, fmt, proc)
  258         dev_t dev;
  259         int flag, fmt;
  260         struct proc *proc;
  261 {
  262         int unit;
  263 
  264         unit = MD_UNIT(dev);
  265 
  266         if (unit >= ramdisk_ndevs)
  267                 return ENXIO;
  268 
  269         return 0;
  270 }
  271 
  272 int
  273 mdread(dev, uio, flags)
  274         dev_t dev;
  275         struct uio *uio;
  276         int flags;
  277 {
  278         int unit;
  279         struct md_softc *sc;
  280 
  281         unit = MD_UNIT(dev);
  282 
  283         if (unit >= ramdisk_ndevs)
  284                 return ENXIO;
  285 
  286         sc = ramdisk_devs[unit];
  287 
  288         if (sc->sc_type == MD_UNCONFIGURED)
  289                 return ENXIO;
  290 
  291         return (physio(mdstrategy, NULL, dev, B_READ, minphys, uio));
  292 }
  293 
  294 int
  295 mdwrite(dev, uio, flags)
  296         dev_t dev;
  297         struct uio *uio;
  298         int flags;
  299 {
  300         int unit;
  301         struct md_softc *sc;
  302 
  303         unit = MD_UNIT(dev);
  304 
  305         if (unit >= ramdisk_ndevs)
  306                 return ENXIO;
  307 
  308         sc = ramdisk_devs[unit];
  309 
  310         if (sc->sc_type == MD_UNCONFIGURED)
  311                 return ENXIO;
  312 
  313         return (physio(mdstrategy, NULL, dev, B_WRITE, minphys, uio));
  314 }
  315 
  316 /*
  317  * Handle I/O requests, either directly, or
  318  * by passing them to the server process.
  319  */
  320 void
  321 mdstrategy(bp)
  322         struct buf *bp;
  323 {
  324         int unit;
  325         struct md_softc *sc;
  326         caddr_t addr;
  327         size_t off, xfer;
  328 
  329         unit = MD_UNIT(bp->b_dev);
  330         sc = ramdisk_devs[unit];
  331 
  332         if (sc->sc_type == MD_UNCONFIGURED) {
  333                 bp->b_error = ENXIO;
  334                 bp->b_flags |= B_ERROR;
  335                 goto done;
  336         }
  337 
  338         switch (sc->sc_type) {
  339 #if MEMORY_DISK_SERVER
  340         case MD_UMEM_SERVER:
  341                 /* Just add this job to the server's queue. */
  342                 BUFQ_PUT(&sc->sc_buflist, bp);
  343                 wakeup((caddr_t)sc);
  344                 /* see md_server_loop() */
  345                 /* no biodone in this case */
  346                 return;
  347 #endif  /* MEMORY_DISK_SERVER */
  348 
  349         case MD_KMEM_FIXED:
  350         case MD_KMEM_ALLOCATED:
  351                 /* These are in kernel space.  Access directly. */
  352                 bp->b_resid = bp->b_bcount;
  353                 off = (bp->b_blkno << DEV_BSHIFT);
  354                 if (off >= sc->sc_size) {
  355                         if (bp->b_flags & B_READ)
  356                                 break;  /* EOF */
  357                         goto set_eio;
  358                 }
  359                 xfer = bp->b_resid;
  360                 if (xfer > (sc->sc_size - off))
  361                         xfer = (sc->sc_size - off);
  362                 addr = sc->sc_addr + off;
  363                 if (bp->b_flags & B_READ)
  364                         memcpy(bp->b_data, addr, xfer);
  365                 else
  366                         memcpy(addr, bp->b_data, xfer);
  367                 bp->b_resid -= xfer;
  368                 break;
  369 
  370         default:
  371                 bp->b_resid = bp->b_bcount;
  372         set_eio:
  373                 bp->b_error = EIO;
  374                 bp->b_flags |= B_ERROR;
  375                 break;
  376         }
  377  done:
  378         biodone(bp);
  379 }
  380 
  381 int
  382 mdioctl(dev, cmd, data, flag, proc)
  383         dev_t dev;
  384         u_long cmd;
  385         int flag;
  386         caddr_t data;
  387         struct proc *proc;
  388 {
  389         int unit;
  390         struct md_softc *sc;
  391         struct md_conf *umd;
  392 
  393         unit = MD_UNIT(dev);
  394         sc = ramdisk_devs[unit];
  395 
  396         /* If this is not the raw partition, punt! */
  397         if (DISKPART(dev) != RAW_PART)
  398                 return ENOTTY;
  399 
  400         umd = (struct md_conf *)data;
  401         switch (cmd) {
  402         case MD_GETCONF:
  403                 *umd = sc->sc_md;
  404                 return 0;
  405 
  406         case MD_SETCONF:
  407                 /* Can only set it once. */
  408                 if (sc->sc_type != MD_UNCONFIGURED)
  409                         break;
  410                 switch (umd->md_type) {
  411                 case MD_KMEM_ALLOCATED:
  412                         return md_ioctl_kalloc(sc, umd, proc);
  413 #if MEMORY_DISK_SERVER
  414                 case MD_UMEM_SERVER:
  415                         return md_ioctl_server(sc, umd, proc);
  416 #endif  /* MEMORY_DISK_SERVER */
  417                 default:
  418                         break;
  419                 }
  420                 break;
  421         }
  422         return EINVAL;
  423 }
  424 
  425 /*
  426  * Handle ioctl MD_SETCONF for (sc_type == MD_KMEM_ALLOCATED)
  427  * Just allocate some kernel memory and return.
  428  */
  429 static int
  430 md_ioctl_kalloc(sc, umd, proc)
  431         struct md_softc *sc;
  432         struct md_conf *umd;
  433         struct proc *proc;
  434 {
  435         vaddr_t addr;
  436         vsize_t size;
  437 
  438         /* Sanity check the size. */
  439         size = umd->md_size;
  440         addr = uvm_km_zalloc(kernel_map, size);
  441         if (!addr)
  442                 return ENOMEM;
  443 
  444         /* This unit is now configured. */
  445         sc->sc_addr = (caddr_t)addr;    /* kernel space */
  446         sc->sc_size = (size_t)size;
  447         sc->sc_type = MD_KMEM_ALLOCATED;
  448         return 0;
  449 }       
  450 
  451 #if MEMORY_DISK_SERVER
  452 
  453 /*
  454  * Handle ioctl MD_SETCONF for (sc_type == MD_UMEM_SERVER)
  455  * Set config, then become the I/O server for this unit.
  456  */
  457 static int
  458 md_ioctl_server(sc, umd, proc)
  459         struct md_softc *sc;
  460         struct md_conf *umd;
  461         struct proc *proc;
  462 {
  463         vaddr_t end;
  464         int error;
  465 
  466         /* Sanity check addr, size. */
  467         end = (vaddr_t) (umd->md_addr + umd->md_size);
  468 
  469         if ((end >= VM_MAXUSER_ADDRESS) ||
  470                 (end < ((vaddr_t) umd->md_addr)) )
  471                 return EINVAL;
  472 
  473         /* This unit is now configured. */
  474         sc->sc_addr = umd->md_addr;     /* user space */
  475         sc->sc_size = umd->md_size;
  476         sc->sc_type = MD_UMEM_SERVER;
  477 
  478         /* Become the server daemon */
  479         error = md_server_loop(sc);
  480 
  481         /* This server is now going away! */
  482         sc->sc_type = MD_UNCONFIGURED;
  483         sc->sc_addr = 0;
  484         sc->sc_size = 0;
  485 
  486         return (error);
  487 }       
  488 
  489 int md_sleep_pri = PWAIT | PCATCH;
  490 
  491 static int
  492 md_server_loop(sc)
  493         struct md_softc *sc;
  494 {
  495         struct buf *bp;
  496         caddr_t addr;   /* user space address */
  497         size_t off;     /* offset into "device" */
  498         size_t xfer;    /* amount to transfer */
  499         int error;
  500 
  501         for (;;) {
  502                 /* Wait for some work to arrive. */
  503                 while ((bp = BUFQ_GET(&sc->sc_buflist)) == NULL) {
  504                         error = tsleep((caddr_t)sc, md_sleep_pri, "md_idle", 0);
  505                         if (error)
  506                                 return error;
  507                 }
  508 
  509                 /* Do the transfer to/from user space. */
  510                 error = 0;
  511                 bp->b_resid = bp->b_bcount;
  512                 off = (bp->b_blkno << DEV_BSHIFT);
  513                 if (off >= sc->sc_size) {
  514                         if (bp->b_flags & B_READ)
  515                                 goto done;      /* EOF (not an error) */
  516                         error = EIO;
  517                         goto done;
  518                 }
  519                 xfer = bp->b_resid;
  520                 if (xfer > (sc->sc_size - off))
  521                         xfer = (sc->sc_size - off);
  522                 addr = sc->sc_addr + off;
  523                 if (bp->b_flags & B_READ)
  524                         error = copyin(addr, bp->b_data, xfer);
  525                 else
  526                         error = copyout(bp->b_data, addr, xfer);
  527                 if (!error)
  528                         bp->b_resid -= xfer;
  529 
  530         done:
  531                 if (error) {
  532                         bp->b_error = error;
  533                         bp->b_flags |= B_ERROR;
  534                 }
  535                 biodone(bp);
  536         }
  537 }
  538 #endif  /* MEMORY_DISK_SERVER */

Cache object: c2c09cfb82bd0737bfe1b1c399cba624


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