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.134 2022/03/28 12:33:59 riastradh 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.134 2022/03/28 12:33:59 riastradh Exp $");
   71 
   72 #include <sys/param.h>
   73 #include <sys/kernel.h>
   74 #include <sys/kmem.h>
   75 #include <sys/buf.h>
   76 #include <sys/fcntl.h>
   77 #include <sys/syslog.h>
   78 #include <sys/disklabel.h>
   79 #include <sys/disk.h>
   80 #include <sys/sysctl.h>
   81 #include <lib/libkern/libkern.h>
   82 
   83 /*
   84  * Disk error is the preface to plaintive error messages
   85  * about failing disk transfers.  It prints messages of the form
   86 
   87 hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
   88 
   89  * if the offset of the error in the transfer and a disk label
   90  * are both available.  blkdone should be -1 if the position of the error
   91  * is unknown; the disklabel pointer may be null from drivers that have not
   92  * been converted to use them.  The message is printed with printf
   93  * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
   94  * The message should be completed (with at least a newline) with printf
   95  * or addlog, respectively.  There is no trailing space.
   96  */
   97 #ifndef PRIdaddr
   98 #define PRIdaddr PRId64
   99 #endif
  100 void
  101 diskerr(const struct buf *bp, const char *dname, const char *what, int pri,
  102     int blkdone, const struct disklabel *lp)
  103 {
  104         int unit = DISKUNIT(bp->b_dev), part = DISKPART(bp->b_dev);
  105         void (*pr)(const char *, ...) __printflike(1, 2);
  106         char partname = 'a' + part;
  107         daddr_t sn;
  108 
  109         if (/*CONSTCOND*/0)
  110                 /* Compiler will error this if the format is wrong... */
  111                 printf("%" PRIdaddr, bp->b_blkno);
  112 
  113         if (pri != LOG_PRINTF) {
  114                 static const char fmt[] = "";
  115                 log(pri, fmt);
  116                 pr = addlog;
  117         } else
  118                 pr = printf;
  119         (*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
  120             bp->b_flags & B_READ ? "read" : "writ");
  121         sn = bp->b_blkno;
  122         if (bp->b_bcount <= DEV_BSIZE)
  123                 (*pr)("%" PRIdaddr, sn);
  124         else {
  125                 if (blkdone >= 0) {
  126                         sn += blkdone;
  127                         (*pr)("%" PRIdaddr " of ", sn);
  128                 }
  129                 (*pr)("%" PRIdaddr "-%" PRIdaddr "", bp->b_blkno,
  130                     bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
  131         }
  132         if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
  133                 sn += lp->d_partitions[part].p_offset;
  134                 (*pr)(" (%s%d bn %" PRIdaddr "; cn %" PRIdaddr "",
  135                     dname, unit, sn, sn / lp->d_secpercyl);
  136                 sn %= lp->d_secpercyl;
  137                 (*pr)(" tn %" PRIdaddr " sn %" PRIdaddr ")",
  138                     sn / lp->d_nsectors, sn % lp->d_nsectors);
  139         }
  140 }
  141 
  142 /*
  143  * Searches the iostatlist for the disk corresponding to the
  144  * name provided.
  145  */
  146 struct disk *
  147 disk_find(const char *name)
  148 {
  149         struct io_stats *stat;
  150 
  151         stat = iostat_find(name);
  152 
  153         if ((stat != NULL) && (stat->io_type == IOSTAT_DISK))
  154                 return stat->io_parent;
  155 
  156         return (NULL);
  157 }
  158 
  159 void
  160 disk_init(struct disk *diskp, const char *name, const struct dkdriver *driver)
  161 {
  162         u_int blocksize = DEV_BSIZE;
  163 
  164         /*
  165          * Initialize the wedge-related locks and other fields.
  166          */
  167         mutex_init(&diskp->dk_rawlock, MUTEX_DEFAULT, IPL_NONE);
  168         mutex_init(&diskp->dk_openlock, MUTEX_DEFAULT, IPL_NONE);
  169         LIST_INIT(&diskp->dk_wedges);
  170         diskp->dk_nwedges = 0;
  171         diskp->dk_labelsector = LABELSECTOR;
  172         diskp->dk_blkshift = DK_BSIZE2BLKSHIFT(blocksize);
  173         diskp->dk_byteshift = DK_BSIZE2BYTESHIFT(blocksize);
  174         diskp->dk_name = name;
  175         diskp->dk_driver = driver;
  176 }
  177 
  178 /*
  179  * Rename a disk.
  180  */
  181 void
  182 disk_rename(struct disk *diskp, const char *name)
  183 {
  184 
  185         diskp->dk_name = name;
  186         iostat_rename(diskp->dk_stats, diskp->dk_name);
  187 }
  188 
  189 /*
  190  * Attach a disk.
  191  */
  192 void
  193 disk_attach(struct disk *diskp)
  194 {
  195 
  196         /*
  197          * Allocate and initialize the disklabel structures.
  198          */
  199         diskp->dk_label = kmem_zalloc(sizeof(struct disklabel), KM_SLEEP);
  200         diskp->dk_cpulabel = kmem_zalloc(sizeof(struct cpu_disklabel),
  201             KM_SLEEP);
  202 
  203         /*
  204          * Set up the stats collection.
  205          */
  206         diskp->dk_stats = iostat_alloc(IOSTAT_DISK, diskp, diskp->dk_name);
  207 }
  208 
  209 int
  210 disk_begindetach(struct disk *dk, int (*lastclose)(device_t),
  211     device_t self, int flags)
  212 {
  213         int rc;
  214 
  215         rc = 0;
  216         mutex_enter(&dk->dk_openlock);
  217         if (dk->dk_openmask == 0)
  218                 ;       /* nothing to do */
  219         else if ((flags & DETACH_FORCE) == 0)
  220                 rc = EBUSY;
  221         else if (lastclose != NULL)
  222                 rc = (*lastclose)(self);
  223         mutex_exit(&dk->dk_openlock);
  224 
  225         return rc;
  226 }
  227 
  228 /*
  229  * Detach a disk.
  230  */
  231 void
  232 disk_detach(struct disk *diskp)
  233 {
  234 
  235         /*
  236          * Remove from the drivelist.
  237          */
  238         iostat_free(diskp->dk_stats);
  239 
  240         /*
  241          * Release the disk-info dictionary.
  242          */
  243         if (diskp->dk_info) {
  244                 prop_object_release(diskp->dk_info);
  245                 diskp->dk_info = NULL;
  246         }
  247 
  248         /*
  249          * Free the space used by the disklabel structures.
  250          */
  251         kmem_free(diskp->dk_label, sizeof(*diskp->dk_label));
  252         kmem_free(diskp->dk_cpulabel, sizeof(*diskp->dk_cpulabel));
  253 }
  254 
  255 void
  256 disk_destroy(struct disk *diskp)
  257 {
  258 
  259         mutex_destroy(&diskp->dk_openlock);
  260         mutex_destroy(&diskp->dk_rawlock);
  261 }
  262 
  263 /*
  264  * Mark the disk as having work queued for metrics collection.
  265  */
  266 void
  267 disk_wait(struct disk *diskp)
  268 {
  269 
  270         iostat_wait(diskp->dk_stats);
  271 }
  272 
  273 /*
  274  * Mark the disk as busy for metrics collection.
  275  */
  276 void
  277 disk_busy(struct disk *diskp)
  278 {
  279 
  280         iostat_busy(diskp->dk_stats);
  281 }
  282 
  283 /*
  284  * Finished disk operations, gather metrics.
  285  */
  286 void
  287 disk_unbusy(struct disk *diskp, long bcount, int read)
  288 {
  289 
  290         iostat_unbusy(diskp->dk_stats, bcount, read);
  291 }
  292 
  293 /*
  294  * Return true if disk has an I/O operation in flight.
  295  */
  296 bool
  297 disk_isbusy(struct disk *diskp)
  298 {
  299 
  300         return iostat_isbusy(diskp->dk_stats);
  301 }
  302 
  303 /*
  304  * Bounds checking against the media size, used for the raw partition.
  305  * secsize, mediasize and b_blkno must all be the same units.
  306  * Possibly this has to be DEV_BSIZE (512).
  307  */
  308 int
  309 bounds_check_with_mediasize(struct buf *bp, int secsize, uint64_t mediasize)
  310 {
  311         int64_t sz;
  312 
  313         if (bp->b_blkno < 0) {
  314                 /* Reject negative offsets immediately. */
  315                 bp->b_error = EINVAL;
  316                 return 0;
  317         }
  318 
  319         sz = howmany((int64_t)bp->b_bcount, secsize);
  320 
  321         /*
  322          * bp->b_bcount is a 32-bit value, and we rejected a negative
  323          * bp->b_blkno already, so "bp->b_blkno + sz" cannot overflow.
  324          */
  325 
  326         if (bp->b_blkno + sz > mediasize) {
  327                 sz = mediasize - bp->b_blkno;
  328                 if (sz == 0) {
  329                         /* If exactly at end of disk, return EOF. */
  330                         bp->b_resid = bp->b_bcount;
  331                         return 0;
  332                 }
  333                 if (sz < 0) {
  334                         /* If past end of disk, return EINVAL. */
  335                         bp->b_error = EINVAL;
  336                         return 0;
  337                 }
  338                 /* Otherwise, truncate request. */
  339                 bp->b_bcount = sz * secsize;
  340         }
  341 
  342         return 1;
  343 }
  344 
  345 /*
  346  * Determine the size of the transfer, and make sure it is
  347  * within the boundaries of the partition. Adjust transfer
  348  * if needed, and signal errors or early completion.
  349  */
  350 int
  351 bounds_check_with_label(struct disk *dk, struct buf *bp, int wlabel)
  352 {
  353         struct disklabel *lp = dk->dk_label;
  354         struct partition *p = lp->d_partitions + DISKPART(bp->b_dev);
  355         uint64_t p_size, p_offset, labelsector;
  356         int64_t sz;
  357 
  358         if (bp->b_blkno < 0) {
  359                 /* Reject negative offsets immediately. */
  360                 bp->b_error = EINVAL;
  361                 return -1;
  362         }
  363 
  364         /* Protect against division by zero. XXX: Should never happen?!?! */
  365         if ((lp->d_secsize / DEV_BSIZE) == 0 || lp->d_secpercyl == 0) {
  366                 bp->b_error = EINVAL;
  367                 return -1;
  368         }
  369 
  370         p_size = (uint64_t)p->p_size << dk->dk_blkshift;
  371         p_offset = (uint64_t)p->p_offset << dk->dk_blkshift;
  372 #if RAW_PART == 3
  373         labelsector = lp->d_partitions[2].p_offset;
  374 #else
  375         labelsector = lp->d_partitions[RAW_PART].p_offset;
  376 #endif
  377         labelsector = (labelsector + dk->dk_labelsector) << dk->dk_blkshift;
  378 
  379         sz = howmany((int64_t)bp->b_bcount, DEV_BSIZE);
  380 
  381         /*
  382          * bp->b_bcount is a 32-bit value, and we rejected a negative
  383          * bp->b_blkno already, so "bp->b_blkno + sz" cannot overflow.
  384          */
  385 
  386         if (bp->b_blkno + sz > p_size) {
  387                 sz = p_size - bp->b_blkno;
  388                 if (sz == 0) {
  389                         /* If exactly at end of disk, return EOF. */
  390                         bp->b_resid = bp->b_bcount;
  391                         return 0;
  392                 }
  393                 if (sz < 0) {
  394                         /* If past end of disk, return EINVAL. */
  395                         bp->b_error = EINVAL;
  396                         return -1;
  397                 }
  398                 /* Otherwise, truncate request. */
  399                 bp->b_bcount = sz << DEV_BSHIFT;
  400         }
  401 
  402         /* Overwriting disk label? */
  403         if (bp->b_blkno + p_offset <= labelsector &&
  404             bp->b_blkno + p_offset + sz > labelsector &&
  405             (bp->b_flags & B_READ) == 0 && !wlabel) {
  406                 bp->b_error = EROFS;
  407                 return -1;
  408         }
  409 
  410         /* calculate cylinder for disksort to order transfers with */
  411         bp->b_cylinder = (bp->b_blkno + p->p_offset) /
  412             (lp->d_secsize / DEV_BSIZE) / lp->d_secpercyl;
  413         return 1;
  414 }
  415 
  416 int
  417 disk_read_sectors(void (*strat)(struct buf *), const struct disklabel *lp,
  418     struct buf *bp, unsigned int sector, int count)
  419 {
  420 
  421         if ((lp->d_secsize / DEV_BSIZE) == 0 || lp->d_secpercyl == 0)
  422                 return EINVAL;
  423 
  424         bp->b_blkno = btodb((off_t)sector * lp->d_secsize);
  425         bp->b_bcount = count * lp->d_secsize;
  426         bp->b_flags = (bp->b_flags & ~B_WRITE) | B_READ;
  427         bp->b_oflags &= ~BO_DONE;
  428         bp->b_cylinder = sector / lp->d_secpercyl;
  429         (*strat)(bp);
  430         return biowait(bp);
  431 }
  432 
  433 const char *
  434 convertdisklabel(struct disklabel *lp, void (*strat)(struct buf *),
  435     struct buf *bp, uint32_t secperunit)
  436 {
  437         struct partition rp, *altp, *p;
  438         int geom_ok;
  439         const char *str;
  440 
  441         memset(&rp, 0, sizeof(rp));
  442         rp.p_size = secperunit;
  443         rp.p_fstype = FS_UNUSED;
  444 
  445         /* If we can seek to d_secperunit - 1, believe the disk geometry. */
  446         if (secperunit != 0 &&
  447             disk_read_sectors(strat, lp, bp, secperunit - 1, 1) == 0)
  448                 geom_ok = 1;
  449         else
  450                 geom_ok = 0;
  451 
  452 #if 0
  453         printf("%s: secperunit (%" PRIu32 ") %s\n", __func__,
  454             secperunit, geom_ok ? "ok" : "not ok");
  455 #endif
  456 
  457         p = &lp->d_partitions[RAW_PART];
  458         if (RAW_PART == 'c' - 'a')
  459                 altp = &lp->d_partitions['d' - 'a'];
  460         else
  461                 altp = &lp->d_partitions['c' - 'a'];
  462 
  463         if (lp->d_npartitions > RAW_PART && p->p_offset == 0 && p->p_size != 0)
  464                 return NULL;    /* already a raw partition */
  465         else if (lp->d_npartitions > MAX('c', 'd') - 'a' &&
  466                  altp->p_offset == 0 && altp->p_size != 0) {
  467                 /* alternate partition ('c' or 'd') is suitable for raw slot,
  468                  * swap with 'd' or 'c'.
  469                  */
  470                 rp = *p;
  471                 *p = *altp;
  472                 *altp = rp;
  473                 return NULL;
  474         } else if (lp->d_npartitions <= RAW_PART &&
  475                    lp->d_npartitions > 'c' - 'a') {
  476                 /* No raw partition is present, but the alternate is present.
  477                  * Copy alternate to raw partition.
  478                  */
  479                 lp->d_npartitions = RAW_PART + 1;
  480                 *p = *altp;
  481                 return NULL;
  482         } else if (!geom_ok)
  483                 str = "no raw partition and disk reports bad geometry";
  484         else if (lp->d_npartitions <= RAW_PART) {
  485                 memset(&lp->d_partitions[lp->d_npartitions], 0,
  486                     sizeof(struct partition) * (RAW_PART - lp->d_npartitions));
  487                 *p = rp;
  488                 lp->d_npartitions = RAW_PART + 1;
  489                 return NULL;
  490         } else if (lp->d_npartitions < MAXPARTITIONS) {
  491                 memmove(p + 1, p,
  492                     sizeof(struct partition) * (lp->d_npartitions - RAW_PART));
  493                 *p = rp;
  494                 lp->d_npartitions++;
  495                 return NULL;
  496         } else
  497                 str = "no raw partition and partition table is full";
  498 #ifdef DIAGNOSTIC
  499         printf("Bad partition: %s\n", str);
  500         printf("type = %u, subtype = %u, typename = %s\n",
  501             lp->d_type, lp->d_subtype, lp->d_typename);
  502         printf("secsize = %u, nsectors = %u, ntracks = %u\n",
  503             lp->d_secsize, lp->d_nsectors, lp->d_ntracks);
  504         printf("ncylinders = %u, secpercyl = %u, secperunit = %u\n",
  505             lp->d_ncylinders, lp->d_secpercyl, lp->d_secperunit);
  506         printf("npartitions = %u\n", lp->d_npartitions);
  507 
  508         for (size_t i = 0; i < MIN(lp->d_npartitions, MAXPARTITIONS); i++) {
  509                 p = &lp->d_partitions[i];
  510                 printf("\t%c: offset = %u size = %u fstype = %u\n",
  511                     (char)(i + 'a'), p->p_offset, p->p_size, p->p_fstype);
  512         }
  513 #endif                  
  514         return str;
  515 }
  516 
  517 /*
  518  * disk_ioctl --
  519  *      Generic disk ioctl handling.
  520  */
  521 int
  522 disk_ioctl(struct disk *dk, dev_t dev, u_long cmd, void *data, int flag,
  523     struct lwp *l)
  524 {
  525         struct dkwedge_info *dkw;
  526         struct partinfo *pi;
  527         struct partition *dp;
  528 #ifdef __HAVE_OLD_DISKLABEL
  529         struct disklabel newlabel;
  530 #endif
  531 
  532         switch (cmd) {
  533         case DIOCGDISKINFO:
  534                 if (dk->dk_info == NULL)
  535                         return ENOTSUP;
  536                 return prop_dictionary_copyout_ioctl(data, cmd, dk->dk_info);
  537 
  538         case DIOCGSECTORSIZE:
  539                 *(u_int *)data = dk->dk_geom.dg_secsize;
  540                 return 0;
  541 
  542         case DIOCGMEDIASIZE:
  543                 *(off_t *)data = (off_t)dk->dk_geom.dg_secsize *
  544                     dk->dk_geom.dg_secperunit;
  545                 return 0;
  546         default:
  547                 break;
  548         }
  549 
  550         if (dev == NODEV)
  551                 return EPASSTHROUGH;
  552 
  553         /* The following should be moved to dk_ioctl */
  554         switch (cmd) {
  555         case DIOCGDINFO:
  556                 if (dk->dk_label == NULL)
  557                         return EBUSY;
  558                 memcpy(data, dk->dk_label, sizeof (*dk->dk_label));
  559                 return 0;
  560 
  561 #ifdef __HAVE_OLD_DISKLABEL
  562         case ODIOCGDINFO:
  563                 if (dk->dk_label == NULL)
  564                         return EBUSY;
  565                 memcpy(&newlabel, dk->dk_label, sizeof(newlabel));
  566                 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
  567                         return ENOTTY;
  568                 memcpy(data, &newlabel, sizeof(struct olddisklabel));
  569                 return 0;
  570 #endif
  571 
  572         case DIOCGPARTINFO:
  573                 pi = data;
  574                 memset(pi, 0, sizeof(*pi));
  575                 pi->pi_secsize = dk->dk_geom.dg_secsize;
  576                 pi->pi_bsize = MAX(BLKDEV_IOSIZE, pi->pi_secsize);
  577 
  578                 if (DISKPART(dev) == RAW_PART) {
  579                         pi->pi_size = dk->dk_geom.dg_secperunit;
  580                         return 0;
  581                 }
  582 
  583                 if (dk->dk_label == NULL)
  584                         return EBUSY;
  585 
  586                 dp = &dk->dk_label->d_partitions[DISKPART(dev)];
  587                 pi->pi_offset = dp->p_offset;
  588                 pi->pi_size = dp->p_size;
  589 
  590                 pi->pi_fstype = dp->p_fstype;
  591                 pi->pi_frag = dp->p_frag;
  592                 pi->pi_fsize = dp->p_fsize;
  593                 pi->pi_cpg = dp->p_cpg;
  594                 
  595                 /*
  596                  * dholland 20130616: XXX this logic should not be
  597                  * here. It is here because the old buffer cache
  598                  * demands that all accesses to the same blocks need
  599                  * to be the same size; but it only works for FFS and
  600                  * nowadays I think it'll fail silently if the size
  601                  * info in the disklabel is wrong. (Or missing.) The
  602                  * buffer cache needs to be smarter; or failing that
  603                  * we need a reliable way here to get the right block
  604                  * size; or a reliable way to guarantee that (a) the
  605                  * fs is not mounted when we get here and (b) any
  606                  * buffers generated here will get purged when the fs
  607                  * does get mounted.
  608                  */
  609                 if (dp->p_fstype == FS_BSDFFS &&
  610                     dp->p_frag != 0 && dp->p_fsize != 0)
  611                         pi->pi_bsize = dp->p_frag * dp->p_fsize;
  612                 return 0;
  613 
  614         case DIOCAWEDGE:
  615                 if ((flag & FWRITE) == 0)
  616                         return EBADF;
  617 
  618                 dkw = data;
  619                 strlcpy(dkw->dkw_parent, dk->dk_name, sizeof(dkw->dkw_parent));
  620                 return dkwedge_add(dkw);
  621 
  622         case DIOCDWEDGE:
  623                 if ((flag & FWRITE) == 0)
  624                         return EBADF;
  625 
  626                 dkw = data;
  627                 strlcpy(dkw->dkw_parent, dk->dk_name, sizeof(dkw->dkw_parent));
  628                 return dkwedge_del(dkw);
  629 
  630         case DIOCLWEDGES:
  631                 return dkwedge_list(dk, data, l);
  632 
  633         case DIOCMWEDGES:
  634                 if ((flag & FWRITE) == 0)
  635                         return EBADF;
  636 
  637                 dkwedge_discover(dk);
  638                 return 0;
  639 
  640         case DIOCRMWEDGES:
  641                 if ((flag & FWRITE) == 0)
  642                         return EBADF;
  643 
  644                 dkwedge_delall(dk);
  645                 return 0;
  646 
  647         default:
  648                 return EPASSTHROUGH;
  649         }
  650 }
  651 
  652 void
  653 disk_set_info(device_t dev, struct disk *dk, const char *type)
  654 {
  655         struct disk_geom *dg = &dk->dk_geom;
  656 
  657         if (dg->dg_secsize == 0) {
  658 #ifdef DIAGNOSTIC
  659                 printf("%s: fixing 0 sector size\n", dk->dk_name);
  660 #endif
  661                 dg->dg_secsize = DEV_BSIZE;
  662         }
  663 
  664         dk->dk_blkshift = DK_BSIZE2BLKSHIFT(dg->dg_secsize);
  665         dk->dk_byteshift = DK_BSIZE2BYTESHIFT(dg->dg_secsize);
  666 
  667         if (dg->dg_secperunit == 0) {
  668 #ifdef DIAGNOSTIC
  669                 if (dg->dg_ncylinders == 0) {
  670                         printf("%s: secperunit and ncylinders are zero\n",
  671                             dk->dk_name);
  672                 }
  673                 if (dg->dg_nsectors == 0 || dg->dg_ntracks == 0) {
  674                         printf("%s: secperunit and (sectors or tracks) "
  675                             "are zero\n", dk->dk_name);
  676                 }
  677 #endif
  678                 dg->dg_secperunit = (int64_t) dg->dg_nsectors *
  679                     dg->dg_ntracks * dg->dg_ncylinders;
  680         }
  681 
  682         if (dg->dg_ncylinders == 0) {
  683                 if (dg->dg_ntracks && dg->dg_nsectors)
  684                         dg->dg_ncylinders = dg->dg_secperunit /
  685                             (dg->dg_ntracks * dg->dg_nsectors);
  686         }
  687 
  688         prop_dictionary_t disk_info, odisk_info, geom;
  689 
  690         disk_info = prop_dictionary_create();
  691         geom = prop_dictionary_create();
  692 
  693         prop_dictionary_set_uint64(geom, "sectors-per-unit",
  694             dg->dg_secperunit);
  695 
  696         prop_dictionary_set_uint32(geom, "sector-size", dg->dg_secsize);
  697 
  698         if (dg->dg_nsectors)
  699                 prop_dictionary_set_uint16(geom, "sectors-per-track",
  700                     dg->dg_nsectors);
  701 
  702         if (dg->dg_ntracks)
  703                 prop_dictionary_set_uint16(geom, "tracks-per-cylinder",
  704                     dg->dg_ntracks);
  705 
  706         if (dg->dg_ncylinders)
  707                 prop_dictionary_set_uint64(geom, "cylinders-per-unit",
  708                     dg->dg_ncylinders);
  709 
  710         prop_dictionary_set(disk_info, "geometry", geom);
  711 
  712         if (type)
  713                 prop_dictionary_set_string_nocopy(disk_info, "type", type);
  714 
  715         prop_object_release(geom);
  716 
  717         odisk_info = dk->dk_info;
  718         dk->dk_info = disk_info;
  719 
  720         if (dev)
  721                 prop_dictionary_set(device_properties(dev), "disk-info",
  722                     disk_info);
  723 
  724         /*
  725          * Don't release disk_info here; we keep a reference to it.
  726          * disk_detach() will release it when we go away.
  727          */
  728         if (odisk_info)
  729                 prop_object_release(odisk_info);
  730 }
  731 
  732 int
  733 disklabel_dev_unit(dev_t dev)
  734 {
  735 
  736         return DISKUNIT(dev);
  737 }

Cache object: b192cfda00fe2ebddaeb5dfa5defd790


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