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/kern/subr_disk.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: subr_disk.c,v 1.93.10.3 2011/01/07 06:33:45 riz Exp $  */
    2 
    3 /*-
    4  * Copyright (c) 1996, 1997, 1999, 2000, 2009 The NetBSD Foundation, Inc.
    5  * All rights reserved.
    6  *
    7  * This code is derived from software contributed to The NetBSD Foundation
    8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
    9  * NASA Ames Research Center.
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions and the following disclaimer.
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   30  * POSSIBILITY OF SUCH DAMAGE.
   31  */
   32 
   33 /*
   34  * Copyright (c) 1982, 1986, 1988, 1993
   35  *      The Regents of the University of California.  All rights reserved.
   36  * (c) UNIX System Laboratories, Inc.
   37  * All or some portions of this file are derived from material licensed
   38  * to the University of California by American Telephone and Telegraph
   39  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
   40  * the permission of UNIX System Laboratories, Inc.
   41  *
   42  * Redistribution and use in source and binary forms, with or without
   43  * modification, are permitted provided that the following conditions
   44  * are met:
   45  * 1. Redistributions of source code must retain the above copyright
   46  *    notice, this list of conditions and the following disclaimer.
   47  * 2. Redistributions in binary form must reproduce the above copyright
   48  *    notice, this list of conditions and the following disclaimer in the
   49  *    documentation and/or other materials provided with the distribution.
   50  * 3. Neither the name of the University nor the names of its contributors
   51  *    may be used to endorse or promote products derived from this software
   52  *    without specific prior written permission.
   53  *
   54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   64  * SUCH DAMAGE.
   65  *
   66  *      @(#)ufs_disksubr.c      8.5 (Berkeley) 1/21/94
   67  */
   68 
   69 #include <sys/cdefs.h>
   70 __KERNEL_RCSID(0, "$NetBSD: subr_disk.c,v 1.93.10.3 2011/01/07 06:33:45 riz Exp $");
   71 
   72 #include <sys/param.h>
   73 #include <sys/kernel.h>
   74 #include <sys/malloc.h>
   75 #include <sys/buf.h>
   76 #include <sys/syslog.h>
   77 #include <sys/disklabel.h>
   78 #include <sys/disk.h>
   79 #include <sys/sysctl.h>
   80 #include <sys/fcntl.h>
   81 #include <sys/kauth.h>
   82 #include <sys/vnode_if.h>
   83 #include <lib/libkern/libkern.h>
   84 
   85 /*
   86  * Compute checksum for disk label.
   87  */
   88 u_int
   89 dkcksum(struct disklabel *lp)
   90 {
   91         return dkcksum_sized(lp, lp->d_npartitions);
   92 }
   93 
   94 u_int
   95 dkcksum_sized(struct disklabel *lp, size_t npartitions)
   96 {
   97         u_short *start, *end;
   98         u_short sum = 0;
   99 
  100         start = (u_short *)lp;
  101         end = (u_short *)&lp->d_partitions[npartitions];
  102         while (start < end)
  103                 sum ^= *start++;
  104         return (sum);
  105 }
  106 
  107 /*
  108  * Disk error is the preface to plaintive error messages
  109  * about failing disk transfers.  It prints messages of the form
  110 
  111 hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
  112 
  113  * if the offset of the error in the transfer and a disk label
  114  * are both available.  blkdone should be -1 if the position of the error
  115  * is unknown; the disklabel pointer may be null from drivers that have not
  116  * been converted to use them.  The message is printed with printf
  117  * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
  118  * The message should be completed (with at least a newline) with printf
  119  * or addlog, respectively.  There is no trailing space.
  120  */
  121 #ifndef PRIdaddr
  122 #define PRIdaddr PRId64
  123 #endif
  124 void
  125 diskerr(const struct buf *bp, const char *dname, const char *what, int pri,
  126     int blkdone, const struct disklabel *lp)
  127 {
  128         int unit = DISKUNIT(bp->b_dev), part = DISKPART(bp->b_dev);
  129         void (*pr)(const char *, ...);
  130         char partname = 'a' + part;
  131         daddr_t sn;
  132 
  133         if (/*CONSTCOND*/0)
  134                 /* Compiler will error this is the format is wrong... */
  135                 printf("%" PRIdaddr, bp->b_blkno);
  136 
  137         if (pri != LOG_PRINTF) {
  138                 static const char fmt[] = "";
  139                 log(pri, fmt);
  140                 pr = addlog;
  141         } else
  142                 pr = printf;
  143         (*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
  144             bp->b_flags & B_READ ? "read" : "writ");
  145         sn = bp->b_blkno;
  146         if (bp->b_bcount <= DEV_BSIZE)
  147                 (*pr)("%" PRIdaddr, sn);
  148         else {
  149                 if (blkdone >= 0) {
  150                         sn += blkdone;
  151                         (*pr)("%" PRIdaddr " of ", sn);
  152                 }
  153                 (*pr)("%" PRIdaddr "-%" PRIdaddr "", bp->b_blkno,
  154                     bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
  155         }
  156         if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
  157                 sn += lp->d_partitions[part].p_offset;
  158                 (*pr)(" (%s%d bn %" PRIdaddr "; cn %" PRIdaddr "",
  159                     dname, unit, sn, sn / lp->d_secpercyl);
  160                 sn %= lp->d_secpercyl;
  161                 (*pr)(" tn %" PRIdaddr " sn %" PRIdaddr ")",
  162                     sn / lp->d_nsectors, sn % lp->d_nsectors);
  163         }
  164 }
  165 
  166 /*
  167  * Searches the iostatlist for the disk corresponding to the
  168  * name provided.
  169  */
  170 struct disk *
  171 disk_find(const char *name)
  172 {
  173         struct io_stats *stat;
  174 
  175         stat = iostat_find(name);
  176 
  177         if ((stat != NULL) && (stat->io_type == IOSTAT_DISK))
  178                 return stat->io_parent;
  179 
  180         return (NULL);
  181 }
  182 
  183 void
  184 disk_init(struct disk *diskp, const char *name, const struct dkdriver *driver)
  185 {
  186 
  187         /*
  188          * Initialize the wedge-related locks and other fields.
  189          */
  190         mutex_init(&diskp->dk_rawlock, MUTEX_DEFAULT, IPL_NONE);
  191         mutex_init(&diskp->dk_openlock, MUTEX_DEFAULT, IPL_NONE);
  192         LIST_INIT(&diskp->dk_wedges);
  193         diskp->dk_nwedges = 0;
  194         diskp->dk_labelsector = LABELSECTOR;
  195         disk_blocksize(diskp, DEV_BSIZE);
  196         diskp->dk_name = name;
  197         diskp->dk_driver = driver;
  198 }
  199 
  200 /*
  201  * Attach a disk.
  202  */
  203 void
  204 disk_attach(struct disk *diskp)
  205 {
  206 
  207         /*
  208          * Allocate and initialize the disklabel structures.  Note that
  209          * it's not safe to sleep here, since we're probably going to be
  210          * called during autoconfiguration.
  211          */
  212         diskp->dk_label = malloc(sizeof(struct disklabel), M_DEVBUF, M_NOWAIT);
  213         diskp->dk_cpulabel = malloc(sizeof(struct cpu_disklabel), M_DEVBUF,
  214             M_NOWAIT);
  215         if ((diskp->dk_label == NULL) || (diskp->dk_cpulabel == NULL))
  216                 panic("disk_attach: can't allocate storage for disklabel");
  217 
  218         memset(diskp->dk_label, 0, sizeof(struct disklabel));
  219         memset(diskp->dk_cpulabel, 0, sizeof(struct cpu_disklabel));
  220 
  221         /*
  222          * Set up the stats collection.
  223          */
  224         diskp->dk_stats = iostat_alloc(IOSTAT_DISK, diskp, diskp->dk_name);
  225 }
  226 
  227 /*
  228  * Detach a disk.
  229  */
  230 void
  231 disk_detach(struct disk *diskp)
  232 {
  233 
  234         /*
  235          * Remove from the drivelist.
  236          */
  237         iostat_free(diskp->dk_stats);
  238 
  239         /*
  240          * Release the disk-info dictionary.
  241          */
  242         if (diskp->dk_info) {
  243                 prop_object_release(diskp->dk_info);
  244                 diskp->dk_info = NULL;
  245         }
  246 
  247         /*
  248          * Free the space used by the disklabel structures.
  249          */
  250         free(diskp->dk_label, M_DEVBUF);
  251         free(diskp->dk_cpulabel, M_DEVBUF);
  252 }
  253 
  254 void
  255 disk_destroy(struct disk *diskp)
  256 {
  257 
  258         mutex_destroy(&diskp->dk_openlock);
  259         mutex_destroy(&diskp->dk_rawlock);
  260 }
  261 
  262 /*
  263  * Mark the disk as busy for metrics collection.
  264  */
  265 void
  266 disk_busy(struct disk *diskp)
  267 {
  268 
  269         iostat_busy(diskp->dk_stats);
  270 }
  271 
  272 /*
  273  * Finished disk operations, gather metrics.
  274  */
  275 void
  276 disk_unbusy(struct disk *diskp, long bcount, int read)
  277 {
  278 
  279         iostat_unbusy(diskp->dk_stats, bcount, read);
  280 }
  281 
  282 /*
  283  * Return true if disk has an I/O operation in flight.
  284  */
  285 bool
  286 disk_isbusy(struct disk *diskp)
  287 {
  288 
  289         return iostat_isbusy(diskp->dk_stats);
  290 }
  291 
  292 /*
  293  * Set the physical blocksize of a disk, in bytes.
  294  * Only necessary if blocksize != DEV_BSIZE.
  295  */
  296 void
  297 disk_blocksize(struct disk *diskp, int blocksize)
  298 {
  299 
  300         diskp->dk_blkshift = DK_BSIZE2BLKSHIFT(blocksize);
  301         diskp->dk_byteshift = DK_BSIZE2BYTESHIFT(blocksize);
  302 }
  303 
  304 /*
  305  * Bounds checking against the media size, used for the raw partition.
  306  * The sector size passed in should currently always be DEV_BSIZE,
  307  * and the media size the size of the device in DEV_BSIZE sectors.
  308  */
  309 int
  310 bounds_check_with_mediasize(struct buf *bp, int secsize, uint64_t mediasize)
  311 {
  312         int64_t sz;
  313 
  314         sz = howmany(bp->b_bcount, secsize);
  315 
  316         if (bp->b_blkno + sz > mediasize) {
  317                 sz = mediasize - bp->b_blkno;
  318                 if (sz == 0) {
  319                         /* If exactly at end of disk, return EOF. */
  320                         bp->b_resid = bp->b_bcount;
  321                         return 0;
  322                 }
  323                 if (sz < 0) {
  324                         /* If past end of disk, return EINVAL. */
  325                         bp->b_error = EINVAL;
  326                         return 0;
  327                 }
  328                 /* Otherwise, truncate request. */
  329                 bp->b_bcount = sz << DEV_BSHIFT;
  330         }
  331 
  332         return 1;
  333 }
  334 
  335 /*
  336  * Determine the size of the transfer, and make sure it is
  337  * within the boundaries of the partition. Adjust transfer
  338  * if needed, and signal errors or early completion.
  339  */
  340 int
  341 bounds_check_with_label(struct disk *dk, struct buf *bp, int wlabel)
  342 {
  343         struct disklabel *lp = dk->dk_label;
  344         struct partition *p = lp->d_partitions + DISKPART(bp->b_dev);
  345         uint64_t p_size, p_offset, labelsector;
  346         int64_t sz;
  347 
  348         /* Protect against division by zero. XXX: Should never happen?!?! */
  349         if (lp->d_secpercyl == 0) {
  350                 bp->b_error = EINVAL;
  351                 return -1;
  352         }
  353 
  354         p_size = (uint64_t)p->p_size << dk->dk_blkshift;
  355         p_offset = (uint64_t)p->p_offset << dk->dk_blkshift;
  356 #if RAW_PART == 3
  357         labelsector = lp->d_partitions[2].p_offset;
  358 #else
  359         labelsector = lp->d_partitions[RAW_PART].p_offset;
  360 #endif
  361         labelsector = (labelsector + dk->dk_labelsector) << dk->dk_blkshift;
  362 
  363         sz = howmany(bp->b_bcount, DEV_BSIZE);
  364         if ((bp->b_blkno + sz) > p_size) {
  365                 sz = p_size - bp->b_blkno;
  366                 if (sz == 0) {
  367                         /* If exactly at end of disk, return EOF. */
  368                         bp->b_resid = bp->b_bcount;
  369                         return 0;
  370                 }
  371                 if (sz < 0) {
  372                         /* If past end of disk, return EINVAL. */
  373                         bp->b_error = EINVAL;
  374                         return -1;
  375                 }
  376                 /* Otherwise, truncate request. */
  377                 bp->b_bcount = sz << DEV_BSHIFT;
  378         }
  379 
  380         /* Overwriting disk label? */
  381         if (bp->b_blkno + p_offset <= labelsector &&
  382             bp->b_blkno + p_offset + sz > labelsector &&
  383             (bp->b_flags & B_READ) == 0 && !wlabel) {
  384                 bp->b_error = EROFS;
  385                 return -1;
  386         }
  387 
  388         /* calculate cylinder for disksort to order transfers with */
  389         bp->b_cylinder = (bp->b_blkno + p->p_offset) /
  390             (lp->d_secsize / DEV_BSIZE) / lp->d_secpercyl;
  391         return 1;
  392 }
  393 
  394 int
  395 disk_read_sectors(void (*strat)(struct buf *), const struct disklabel *lp,
  396     struct buf *bp, unsigned int sector, int count)
  397 {
  398         bp->b_blkno = sector;
  399         bp->b_bcount = count * lp->d_secsize;
  400         bp->b_flags = (bp->b_flags & ~B_WRITE) | B_READ;
  401         bp->b_oflags &= ~BO_DONE;
  402         bp->b_cylinder = sector / lp->d_secpercyl;
  403         (*strat)(bp);
  404         return biowait(bp);
  405 }
  406 
  407 const char *
  408 convertdisklabel(struct disklabel *lp, void (*strat)(struct buf *),
  409     struct buf *bp, uint32_t secperunit)
  410 {
  411         struct partition rp, *altp, *p;
  412         int geom_ok;
  413 
  414         memset(&rp, 0, sizeof(rp));
  415         rp.p_size = secperunit;
  416         rp.p_fstype = FS_UNUSED;
  417 
  418         /* If we can seek to d_secperunit - 1, believe the disk geometry. */
  419         if (secperunit != 0 &&
  420             disk_read_sectors(strat, lp, bp, secperunit - 1, 1) == 0)
  421                 geom_ok = 1;
  422         else
  423                 geom_ok = 0;
  424 
  425 #if 0
  426         printf("%s: secperunit (%" PRIu32 ") %s\n", __func__,
  427             secperunit, geom_ok ? "ok" : "not ok");
  428 #endif
  429 
  430         p = &lp->d_partitions[RAW_PART];
  431         if (RAW_PART == 'c' - 'a')
  432                 altp = &lp->d_partitions['d' - 'a'];
  433         else
  434                 altp = &lp->d_partitions['c' - 'a'];
  435 
  436         if (lp->d_npartitions > RAW_PART && p->p_offset == 0 && p->p_size != 0)
  437                 ;       /* already a raw partition */
  438         else if (lp->d_npartitions > MAX('c', 'd') - 'a' &&
  439                  altp->p_offset == 0 && altp->p_size != 0) {
  440                 /* alternate partition ('c' or 'd') is suitable for raw slot,
  441                  * swap with 'd' or 'c'.
  442                  */
  443                 rp = *p;
  444                 *p = *altp;
  445                 *altp = rp;
  446         } else if (lp->d_npartitions <= RAW_PART &&
  447                    lp->d_npartitions > 'c' - 'a') {
  448                 /* No raw partition is present, but the alternate is present.
  449                  * Copy alternate to raw partition.
  450                  */
  451                 lp->d_npartitions = RAW_PART + 1;
  452                 *p = *altp;
  453         } else if (!geom_ok)
  454                 return "no raw partition and disk reports bad geometry";
  455         else if (lp->d_npartitions <= RAW_PART) {
  456                 memset(&lp->d_partitions[lp->d_npartitions], 0,
  457                     sizeof(struct partition) * (RAW_PART - lp->d_npartitions));
  458                 *p = rp;
  459                 lp->d_npartitions = RAW_PART + 1;
  460         } else if (lp->d_npartitions < MAXPARTITIONS) {
  461                 memmove(p + 1, p,
  462                     sizeof(struct partition) * (lp->d_npartitions - RAW_PART));
  463                 *p = rp;
  464                 lp->d_npartitions++;
  465         } else
  466                 return "no raw partition and partition table is full";
  467         return NULL;
  468 }
  469 
  470 /*
  471  * disk_ioctl --
  472  *      Generic disk ioctl handling.
  473  */
  474 int
  475 disk_ioctl(struct disk *diskp, u_long cmd, void *data, int flag,
  476            struct lwp *l)
  477 {
  478         int error;
  479 
  480         switch (cmd) {
  481         case DIOCGDISKINFO:
  482             {
  483                 struct plistref *pref = (struct plistref *) data;
  484 
  485                 if (diskp->dk_info == NULL)
  486                         error = ENOTSUP;
  487                 else
  488                         error = prop_dictionary_copyout_ioctl(pref, cmd,
  489                                                         diskp->dk_info);
  490                 break;
  491             }
  492 
  493         default:
  494                 error = EPASSTHROUGH;
  495         }
  496 
  497         return (error);
  498 }
  499 
  500 int
  501 getdisksize(struct vnode *vp, uint64_t *numsecp, unsigned *secsizep)
  502 {
  503         struct partinfo dpart;
  504         struct dkwedge_info dkw;
  505         struct disk *pdk;
  506         int error;
  507 
  508         error = VOP_IOCTL(vp, DIOCGPART, &dpart, FREAD, NOCRED);
  509         if (error == 0) {
  510                 *secsizep = dpart.disklab->d_secsize;
  511                 *numsecp  = dpart.part->p_size;
  512                 return 0;
  513         }
  514 
  515         error = VOP_IOCTL(vp, DIOCGWEDGEINFO, &dkw, FREAD, NOCRED);
  516         if (error == 0) {
  517                 pdk = disk_find(dkw.dkw_parent);
  518                 if (pdk != NULL) {
  519                         *secsizep = DEV_BSIZE << pdk->dk_blkshift;
  520                         *numsecp  = dkw.dkw_size;
  521                 } else
  522                         error = ENODEV;
  523         }
  524 
  525         return error;
  526 }

Cache object: e707d706a6ec4036969e19dfc1f83417


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